Difference between revisions of "User:Hybrid Dog"

From Minetest Developer Wiki
Jump to navigation Jump to search
(Redirected page to Main Page)
 
(26 intermediate revisions by one other user not shown)
Line 1: Line 1:
#REDIRECT [[Main_Page]]
+
== various ==
 +
performance test shows
 +
<source>
 +
local t0 = benchmark_function(function()
 +
for i = 1,1000 do
 +
local v = math.pow(2, i)
 +
end
 +
end)
  
http://forum.minetest.net/profile.php?id=3419
+
local t1 = benchmark_function(function()
 +
local ln2 = math.log(2)
 +
for i = 1,1000 do
 +
local v = math.exp(i * ln2)
 +
end
 +
end)</source>
 +
the exp thing is faster by the factor 1.37
  
 +
<br/>
 +
== minetest.on_place ==
  
== mods and textures ==
 
  
https://github.com/HybridDog?tab=repositories
+
=== Syntax ===
 +
<source>minetest.on_place(nodename, func)</source>
 +
 
 +
<br/>
 +
=== Description ===
 +
This changes on_place of a node after it was defined.<br/>
 +
It is not implemented into minetest but can be added via mod(s):
 +
<source>minetest.on_place = minetest.on_place or function(name, func)
 +
local previous_on_place = minetest.registered_nodes[name].on_place
 +
minetest.override_item(name, {
 +
on_place = function(...)
 +
if func(...) then
 +
return previous_on_place(...)
 +
end
 +
end
 +
})
 +
end</source>
 +
 
 +
<br/>
 +
==== nodename ====
 +
The name of the node which should become changed
 +
 
 +
<br/>
 +
==== func ====
 +
should return true if the node becomes set
 +
<source>function(itemstack, placer, pointed_thing)</source>
 +
 
 +
<br/>
 +
=== Example ===
 +
<source>minetest.on_place("hydro:growlamp", function(itemstack, placer, pointed_thing)
 +
if not pointed_thing then
 +
return
 +
end
 +
local pos = minetest.get_pointed_thing_position(pointed_thing, true)
 +
if not pos then
 +
return
 +
end
 +
local nd_above = minetest.get_node({x=pos.x, y=pos.y+1, z=pos.z}).name
 +
local nd_above_info = minetest.registered_nodes[nd_above]
 +
if nd_above == "air"
 +
or nd_above == "hydro:growlamp"
 +
or not nd_above_info.walkable
 +
or nd_above_info.buildable_to then
 +
return
 +
end
 +
return true
 +
end)</source>
 +
 
 +
<br/>
 +
== minetest key press and release functions ==
 +
 
 +
 
 +
=== Syntax ===
 +
<source>minetest.register_on_key_press(func(player, key))</source>
 +
<source>minetest.register_on_key_release(func(player, key))</source>
 +
 
 +
<br/>
 +
 
 +
=== Description ===
 +
These functions get executed when a player presses or releases a key.<br>
 +
They're not implemented into minetest but can be added via mod(s):
 +
<source>local on_key_releases,nr = {},0
 +
function minetest.register_on_key_release(func)
 +
nr = nr+1
 +
on_key_releases[nr] = func
 +
end
 +
 
 +
local on_key_presses,np = {},0
 +
function minetest.register_on_key_press(func)
 +
np = np+1
 +
on_key_presses[np] = func
 +
end
 +
 
 +
local playerkeys = {}
 +
minetest.register_globalstep(function()
 +
for _,player in pairs(minetest.get_connected_players()) do
 +
local last_keys = playerkeys[player:get_player_name()]
 +
for key,stat in pairs(player:get_player_control()) do
 +
if stat then
 +
if not last_keys[key] then
 +
for i = 1,np do
 +
on_key_presses[i](player, key)
 +
end
 +
last_keys[key] = true
 +
end
 +
elseif last_keys[key] then
 +
for i = 1,nr do
 +
on_key_releases[i](player, key)
 +
end
 +
last_keys[key] = false
 +
end
 +
end
 +
end
 +
end)
 +
 
 +
minetest.register_on_joinplayer(function(player)
 +
playerkeys[player:get_player_name()] = {}
 +
end)</source>
 +
 
 +
 
 +
<br/>
 +
==== player ====
 +
The player who pressed the key
 +
 
 +
<br/>
 +
==== key ====
 +
A string, the released or pressed key, see the get_player_control at Player.
 +
 
 +
<br/>
 +
=== Examples ===
 +
<source>-- tell the player which keys he/she/it presses and releases
 +
 
 +
minetest.register_on_key_release(function(player, key)
 +
minetest.chat_send_player(player:get_player_name(), "you released "..key)
 +
end)
 +
 
 +
minetest.register_on_key_press(function(player, key)
 +
minetest.chat_send_player(player:get_player_name(), "you keydownd "..key)
 +
end)</source>
 +
 
 +
<br/>
 +
 
 +
== Mods ==
 +
 
 +
 
 +
=== vector_extras ===
 +
 
 +
Note that this is very outdated. The documentation is now at [https://github.com/HybridDog/vector_extras].
 +
 
 +
Those are added as fields of the vector table, e.g. vector.pos_to_string(pos)
 +
{| class="wikitable collapsible sortable"
 +
! Function
 +
! Return value
 +
! Comments
 +
! Status
 +
|-
 +
|<source enclose="none">pos_to_string(pos)</source>
 +
| string
 +
| similar to minetest.pos_to_string, but better readable in my opinion
 +
| works
 +
|-
 +
|<source enclose="none">line([pos, dir[, range][, alt)</source>
 +
| table of vectors
 +
| dir can be following:
 +
* a direction
 +
* a position (range not needed)
 +
if alt then the old path calculation is used
 +
| works
 +
|-
 +
|<source enclose="none">fine_line([pos, dir[, range], scale])</source>
 +
| table of vectors
 +
|
 +
* like the old vector.line but more precise
 +
* needed for not round positions
 +
* try using minetest.line_of_sight instead
 +
| works but<br/>old and slow
 +
|-
 +
|<source enclose="none">twoline(x, y)</source>
 +
| table
 +
|
 +
* returns sth like {{0,0}, {0,1}}
 +
* used for a 2d line
 +
| works
 +
|-
 +
|<source enclose="none">threeline(x, y, z)</source>
 +
| table
 +
|
 +
* returns sth like {{0,0,0}, {0,1,0}}
 +
* used for a 3d line
 +
* x, y and z should be round
 +
| works
 +
|-
 +
|<source enclose="none">sort(ps, [preferred_coords])</source>
 +
| nil
 +
| ps, which gets changed, is a list of vectors,
 +
which become sorted by the coord in the order of preferred_coords
 +
| untested
 +
|-
 +
|<source enclose="none">scalar(v1, v2)</source>
 +
| number
 +
| v1 and v2 are vectors, returns scalar product
 +
| works
 +
|-
 +
|<source enclose="none">cross(v1, v2)</source>
 +
| vector
 +
| v1 and v2 are vectors, returns cross product
 +
| works
 +
|-
 +
|<source enclose="none">plane(ps)</source>
 +
| table of vectors
 +
| ps is a table of three vectors
 +
returns a list of vectors which make a filled triangle plane together
 +
| unfinished
 +
|-
 +
|<source enclose="none">straightdelay([s, v[, a)</source>
 +
| number
 +
|
 +
* s = length
 +
* v = velocity
 +
* a = acceleration (optional)
 +
| works
 +
|-
 +
|<source enclose="none">sun_dir(t)</source>
 +
| vector
 +
|
 +
* t = timeofday
 +
| works
 +
|-
 +
|<source enclose="none">inside(pos, minp, maxp)</source>
 +
| bool
 +
| returns true if pos is inside or on the corners of minp and maxp
 +
| untested
 +
|-
 +
|<source enclose="none">minmax(p1, p2)</source>
 +
| vector, vector
 +
| the first vector's x, y and z are smaller than the second one's
 +
| untested
 +
|-
 +
|<source enclose="none">move(p1, p2, s)</source>
 +
| vector
 +
|
 +
* s = length
 +
* moves s to p2 from p1
 +
* made for rubenwardy
 +
| untested
 +
|-
 +
|<source enclose="none">from_number(i)</source>
 +
| vector
 +
| returns {x=i, y=i, z=i}
 +
| works
 +
|-
 +
|<source enclose="none">explosion_table(r)</source>
 +
| table
 +
|
 +
* r = radius
 +
* returns sth like <source enclose="none">{{pos1, true}, {pos2}}</source>
 +
* using explosion_perlin instead is recommended
 +
| works
 +
|-
 +
|<source enclose="none">explosion_perlin(rmin, rmax[, nparams])</source>
 +
| table
 +
| it is used to make perlin noise surface on spheres, which looks good for explosions
 +
| works
 +
|-
 +
|<source enclose="none">circle(r)</source>
 +
| table of vectors
 +
|
 +
* r = radius
 +
| works
 +
|-
 +
|<source enclose="none">ring(r)</source>
 +
| table of vectors
 +
|
 +
* r = radius
 +
* r can be float
 +
* each positions "touch" their next ones
 +
| works
 +
|-
 +
|<source enclose="none">chunkcorner(pos)</source>
 +
| vector
 +
| should return the chunkcorner near pos
 +
| could work
 +
|-
 +
|<source enclose="none">point_distance_minmax(p1, p2)</source>
 +
| 2 numbers
 +
|
 +
|
 +
|-
 +
|<source enclose="none">collision(p1, p2)</source>
 +
|
 +
|
 +
|
 +
|-
 +
|<source enclose="none">get_data_from_pos(tab, z,y,x)</source>
 +
| tab[z][y][x] or nil
 +
| used to get stored information in a table about these coords
 +
| works
 +
|-
 +
|<source enclose="none">set_data_to_pos(tab, z,y,x, data)</source>
 +
| nil
 +
| used to store information in a table about these coords
 +
| works
 +
|-
 +
|<source enclose="none">set_data_to_pos_optional(tab, z,y,x, data)</source>
 +
| nil
 +
| runs set_data_from_pos if get_data_from_pos returned nil there
 +
| works
 +
|-
 +
|<source enclose="none">remove_data_from_pos(tab, z,y,x)</source>
 +
| nil
 +
| used to remove information from a table about these coords
 +
| works
 +
|-
 +
|<source enclose="none">get_data_pos_table(tab)</source>
 +
| table
 +
|
 +
* tab was used by set_data_to_pos etc.
 +
* returns {{z,y,x, v}, {z,y,x, v}, …}, {x=minx, y=miny, z=minz}, {x=maxx, y=maxy, z=maxz}, count
 +
| works
 +
|-
 +
|<source enclose="none">update_minp_maxp(minp, maxp, pos)</source>
 +
| nil
 +
| changes minp and maxp to exceed their borders
 +
| works
 +
|-
 +
|<source enclose="none">quickadd(pos[, z,y,x])</source>
 +
| nil
 +
| adds those to pos', works more than 4.3 times as fast as using vector.add
 +
| works
 +
|-
 +
|<source enclose="none">unpack(pos)</source>
 +
| 3 numbers
 +
| returns pos.z, pos.y, pos.x
 +
| works
 +
|-
 +
|<source enclose="none">zero</source>
 +
| {x=0, y=0, z=0}
 +
| not a function
 +
| works
 +
|}
 +
 
 +
<br/>

Latest revision as of 19:45, 25 January 2023

various

performance test shows

	local t0 = benchmark_function(function()
		for i = 1,1000 do
			local v = math.pow(2, i)
		end
	end)

	local t1 = benchmark_function(function()
		local ln2 = math.log(2)
		for i = 1,1000 do
			local v = math.exp(i * ln2)
		end
	end)

the exp thing is faster by the factor 1.37


minetest.on_place

Syntax

minetest.on_place(nodename, func)


Description

This changes on_place of a node after it was defined.
It is not implemented into minetest but can be added via mod(s):

minetest.on_place = minetest.on_place or function(name, func)
	local previous_on_place = minetest.registered_nodes[name].on_place
	minetest.override_item(name, {
		on_place = function(...)
			if func(...) then
				return previous_on_place(...)
			end
		end
	})
end


nodename

The name of the node which should become changed


func

should return true if the node becomes set

function(itemstack, placer, pointed_thing)


Example

minetest.on_place("hydro:growlamp", function(itemstack, placer, pointed_thing)
	if not pointed_thing then
		return
	end
	local pos = minetest.get_pointed_thing_position(pointed_thing, true)
	if not pos then
		return
	end
	local nd_above = minetest.get_node({x=pos.x, y=pos.y+1, z=pos.z}).name
	local nd_above_info = minetest.registered_nodes[nd_above]
	if nd_above == "air"
	or nd_above == "hydro:growlamp"
	or not nd_above_info.walkable
	or nd_above_info.buildable_to then
		return
	end
	return true
end)


minetest key press and release functions

Syntax

minetest.register_on_key_press(func(player, key))
minetest.register_on_key_release(func(player, key))


Description

These functions get executed when a player presses or releases a key.
They're not implemented into minetest but can be added via mod(s):

local on_key_releases,nr = {},0
function minetest.register_on_key_release(func)
	nr = nr+1
	on_key_releases[nr] = func
end

local on_key_presses,np = {},0
function minetest.register_on_key_press(func)
	np = np+1
	on_key_presses[np] = func
end

local playerkeys = {}
minetest.register_globalstep(function()
	for _,player in pairs(minetest.get_connected_players()) do
		local last_keys = playerkeys[player:get_player_name()]
		for key,stat in pairs(player:get_player_control()) do
			if stat then
				if not last_keys[key] then
					for i = 1,np do
						on_key_presses[i](player, key)
					end
					last_keys[key] = true
				end
			elseif last_keys[key] then
				for i = 1,nr do
					on_key_releases[i](player, key)
				end
				last_keys[key] = false
			end
		end
	end
end)

minetest.register_on_joinplayer(function(player)
	playerkeys[player:get_player_name()] = {}
end)



player

The player who pressed the key


key

A string, the released or pressed key, see the get_player_control at Player.


Examples

-- tell the player which keys he/she/it presses and releases

minetest.register_on_key_release(function(player, key)
	minetest.chat_send_player(player:get_player_name(), "you released "..key)
end)

minetest.register_on_key_press(function(player, key)
	minetest.chat_send_player(player:get_player_name(), "you keydownd "..key)
end)


Mods

vector_extras

Note that this is very outdated. The documentation is now at [1].

Those are added as fields of the vector table, e.g. vector.pos_to_string(pos)

Function Return value Comments Status
pos_to_string(pos) string similar to minetest.pos_to_string, but better readable in my opinion works
line([pos, dir[, range][, alt) table of vectors dir can be following:
  • a direction
  • a position (range not needed)

if alt then the old path calculation is used

works
fine_line([pos, dir[, range], scale]) table of vectors
  • like the old vector.line but more precise
  • needed for not round positions
  • try using minetest.line_of_sight instead
works but
old and slow
twoline(x, y) table
  • returns sth like {{0,0}, {0,1}}
  • used for a 2d line
works
threeline(x, y, z) table
  • returns sth like {{0,0,0}, {0,1,0}}
  • used for a 3d line
  • x, y and z should be round
works
sort(ps, [preferred_coords]) nil ps, which gets changed, is a list of vectors,

which become sorted by the coord in the order of preferred_coords

untested
scalar(v1, v2) number v1 and v2 are vectors, returns scalar product works
cross(v1, v2) vector v1 and v2 are vectors, returns cross product works
plane(ps) table of vectors ps is a table of three vectors

returns a list of vectors which make a filled triangle plane together

unfinished
straightdelay([s, v[, a) number
  • s = length
  • v = velocity
  • a = acceleration (optional)
works
sun_dir(t) vector
  • t = timeofday
works
inside(pos, minp, maxp) bool returns true if pos is inside or on the corners of minp and maxp untested
minmax(p1, p2) vector, vector the first vector's x, y and z are smaller than the second one's untested
move(p1, p2, s) vector
  • s = length
  • moves s to p2 from p1
  • made for rubenwardy
untested
from_number(i) vector returns {x=i, y=i, z=i} works
explosion_table(r) table
  • r = radius
  • returns sth like {{pos1, true}, {pos2}}
  • using explosion_perlin instead is recommended
works
explosion_perlin(rmin, rmax[, nparams]) table it is used to make perlin noise surface on spheres, which looks good for explosions works
circle(r) table of vectors
  • r = radius
works
ring(r) table of vectors
  • r = radius
  • r can be float
  • each positions "touch" their next ones
works
chunkcorner(pos) vector should return the chunkcorner near pos could work
point_distance_minmax(p1, p2) 2 numbers
collision(p1, p2)
get_data_from_pos(tab, z,y,x) tab[z][y][x] or nil used to get stored information in a table about these coords works
set_data_to_pos(tab, z,y,x, data) nil used to store information in a table about these coords works
set_data_to_pos_optional(tab, z,y,x, data) nil runs set_data_from_pos if get_data_from_pos returned nil there works
remove_data_from_pos(tab, z,y,x) nil used to remove information from a table about these coords works
get_data_pos_table(tab) table
  • tab was used by set_data_to_pos etc.
  • returns {{z,y,x, v}, {z,y,x, v}, …}, {x=minx, y=miny, z=minz}, {x=maxx, y=maxy, z=maxz}, count
works
update_minp_maxp(minp, maxp, pos) nil changes minp and maxp to exceed their borders works
quickadd(pos[, z,y,x]) nil adds those to pos', works more than 4.3 times as fast as using vector.add works
unpack(pos) 3 numbers returns pos.z, pos.y, pos.x works
zero {x=0, y=0, z=0} not a function works