Difference between revisions of "User:Hybrid Dog"
Hybrid Dog (talk | contribs) (→vector_extras: mooore) |
ROllerozxa (talk | contribs) |
||
(10 intermediate revisions by one other user not shown) | |||
Line 1: | Line 1: | ||
− | + | == various == | |
− | + | performance test shows | |
− | + | <source> | |
− | + | local t0 = benchmark_function(function() | |
− | + | for i = 1,1000 do | |
− | + | local v = math.pow(2, i) | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | local | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | for | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
end | 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 | ||
− | + | end)</source> | |
− | + | the exp thing is faster by the factor 1.37 | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | end | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
+ | <br/> | ||
== minetest.on_place == | == minetest.on_place == | ||
Line 356: | Line 23: | ||
<source>minetest.on_place(nodename, func)</source> | <source>minetest.on_place(nodename, func)</source> | ||
+ | <br/> | ||
=== Description === | === Description === | ||
This changes on_place of a node after it was defined.<br/> | This changes on_place of a node after it was defined.<br/> | ||
Line 370: | Line 38: | ||
end</source> | end</source> | ||
+ | <br/> | ||
==== nodename ==== | ==== nodename ==== | ||
The name of the node which should become changed | The name of the node which should become changed | ||
+ | <br/> | ||
==== func ==== | ==== func ==== | ||
should return true if the node becomes set | should return true if the node becomes set | ||
<source>function(itemstack, placer, pointed_thing)</source> | <source>function(itemstack, placer, pointed_thing)</source> | ||
+ | <br/> | ||
=== Example === | === Example === | ||
<source>minetest.on_place("hydro:growlamp", function(itemstack, placer, pointed_thing) | <source>minetest.on_place("hydro:growlamp", function(itemstack, placer, pointed_thing) | ||
Line 396: | Line 67: | ||
return true | return true | ||
end)</source> | 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 == | == Mods == | ||
Line 401: | Line 146: | ||
=== vector_extras === | === 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) | Those are added as fields of the vector table, e.g. vector.pos_to_string(pos) | ||
Line 414: | Line 161: | ||
| works | | works | ||
|- | |- | ||
− | |<source enclose="none">line([pos, dir[, range][, alt | + | |<source enclose="none">line([pos, dir[, range][, alt)</source> |
− | | table of | + | | table of vectors |
| dir can be following: | | dir can be following: | ||
* a direction | * a direction | ||
− | * a | + | * a position (range not needed) |
if alt then the old path calculation is used | if alt then the old path calculation is used | ||
| works | | works | ||
|- | |- | ||
|<source enclose="none">fine_line([pos, dir[, range], scale])</source> | |<source enclose="none">fine_line([pos, dir[, range], scale])</source> | ||
− | | table of | + | | table of vectors |
| | | | ||
* like the old vector.line but more precise | * like the old vector.line but more precise | ||
* needed for not round positions | * needed for not round positions | ||
− | * try using | + | * try using minetest.line_of_sight instead |
| works but<br/>old and slow | | works but<br/>old and slow | ||
|- | |- | ||
Line 446: | Line 193: | ||
|- | |- | ||
|<source enclose="none">sort(ps, [preferred_coords])</source> | |<source enclose="none">sort(ps, [preferred_coords])</source> | ||
− | | | + | | nil |
| ps, which gets changed, is a list of vectors, | | ps, which gets changed, is a list of vectors, | ||
which become sorted by the coord in the order of preferred_coords | which become sorted by the coord in the order of preferred_coords | ||
Line 467: | Line 214: | ||
| unfinished | | unfinished | ||
|- | |- | ||
− | |<source enclose="none">straightdelay([s, v[, a | + | |<source enclose="none">straightdelay([s, v[, a)</source> |
| number | | number | ||
| | | | ||
Line 476: | Line 223: | ||
|- | |- | ||
|<source enclose="none">sun_dir(t)</source> | |<source enclose="none">sun_dir(t)</source> | ||
− | | | + | | vector |
| | | | ||
* t = timeofday | * t = timeofday | ||
Line 487: | Line 234: | ||
|- | |- | ||
|<source enclose="none">minmax(p1, p2)</source> | |<source enclose="none">minmax(p1, p2)</source> | ||
− | | | + | | vector, vector |
− | | the first | + | | the first vector's x, y and z are smaller than the second one's |
| untested | | untested | ||
|- | |- | ||
|<source enclose="none">move(p1, p2, s)</source> | |<source enclose="none">move(p1, p2, s)</source> | ||
− | | | + | | vector |
| | | | ||
* s = length | * s = length | ||
Line 518: | Line 265: | ||
|- | |- | ||
|<source enclose="none">circle(r)</source> | |<source enclose="none">circle(r)</source> | ||
− | | table of | + | | table of vectors |
| | | | ||
* r = radius | * r = radius | ||
Line 524: | Line 271: | ||
|- | |- | ||
|<source enclose="none">ring(r)</source> | |<source enclose="none">ring(r)</source> | ||
− | | table of | + | | table of vectors |
| | | | ||
* r = radius | * r = radius | ||
Line 532: | Line 279: | ||
|- | |- | ||
|<source enclose="none">chunkcorner(pos)</source> | |<source enclose="none">chunkcorner(pos)</source> | ||
− | | | + | | vector |
| should return the chunkcorner near pos | | should return the chunkcorner near pos | ||
| could work | | could work | ||
Line 580: | Line 327: | ||
|<source enclose="none">quickadd(pos[, z,y,x])</source> | |<source enclose="none">quickadd(pos[, z,y,x])</source> | ||
| nil | | nil | ||
− | | adds those to pos' | + | | adds those to pos', works more than 4.3 times as fast as using vector.add |
| works | | works | ||
|- | |- |
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:
if alt then the old path calculation is used |
works |
fine_line([pos, dir[, range], scale])
|
table of vectors |
|
works but old and slow |
twoline(x, y)
|
table |
|
works |
threeline(x, y, z)
|
table |
|
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 |
|
works |
sun_dir(t)
|
vector |
|
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 |
|
untested |
from_number(i)
|
vector | returns {x=i, y=i, z=i} | works |
explosion_table(r)
|
table |
|
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 |
|
works |
ring(r)
|
table of vectors |
|
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 |
|
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 |