Difference between revisions of "User:Hybrid Dog"
Hybrid Dog (talk | contribs) |
ROllerozxa (talk | contribs) |
||
(14 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) | ||
+ | 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)</source> | ||
+ | the exp thing is faster by the factor 1.37 | ||
+ | |||
+ | <br/> | ||
== minetest.on_place == | == minetest.on_place == | ||
Line 5: | 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 11: | Line 30: | ||
local previous_on_place = minetest.registered_nodes[name].on_place | local previous_on_place = minetest.registered_nodes[name].on_place | ||
minetest.override_item(name, { | minetest.override_item(name, { | ||
− | on_place = function( | + | on_place = function(...) |
− | if func( | + | if func(...) then |
− | return previous_on_place( | + | return previous_on_place(...) |
end | end | ||
end | end | ||
Line 19: | 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 45: | 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 51: | Line 147: | ||
=== 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) | ||
{| class="wikitable collapsible sortable" | {| class="wikitable collapsible sortable" | ||
! Function | ! Function | ||
Line 57: | Line 156: | ||
! Status | ! Status | ||
|- | |- | ||
− | |<source enclose="none"> | + | |<source enclose="none">pos_to_string(pos)</source> |
| string | | string | ||
− | | similar to minetest.pos_to_string | + | | similar to minetest.pos_to_string, but better readable in my opinion |
− | | | + | | works |
|- | |- | ||
− | |<source enclose="none"> | + | |<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"> | + | |<source enclose="none">fine_line([pos, dir[, range], scale])</source> |
− | | table of | + | | table of vectors |
| | | | ||
− | * like 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 | ||
|- | |- | ||
− | |<source enclose="none"> | + | |<source enclose="none">twoline(x, y)</source> |
| table | | table | ||
| | | | ||
Line 85: | Line 184: | ||
| works | | works | ||
|- | |- | ||
− | |<source enclose="none"> | + | |<source enclose="none">threeline(x, y, z)</source> |
| table | | table | ||
| | | | ||
Line 93: | Line 192: | ||
| works | | works | ||
|- | |- | ||
− | |<source enclose="none">vector | + | |<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 | | number | ||
| | | | ||
Line 101: | Line 222: | ||
| works | | works | ||
|- | |- | ||
− | |<source enclose="none"> | + | |<source enclose="none">sun_dir(t)</source> |
− | | | + | | vector |
| | | | ||
* t = timeofday | * t = timeofday | ||
| works | | works | ||
|- | |- | ||
− | |<source enclose="none"> | + | |<source enclose="none">inside(pos, minp, maxp)</source> |
| bool | | bool | ||
| returns true if pos is inside or on the corners of minp and maxp | | returns true if pos is inside or on the corners of minp and maxp | ||
| untested | | untested | ||
|- | |- | ||
− | |<source enclose="none"> | + | |<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"> | + | |<source enclose="none">move(p1, p2, s)</source> |
− | | | + | | vector |
| | | | ||
* s = length | * s = length | ||
Line 125: | Line 246: | ||
| untested | | untested | ||
|- | |- | ||
− | |<source enclose="none">vector | + | |<source enclose="none">from_number(i)</source> |
+ | | vector | ||
+ | | returns {x=i, y=i, z=i} | ||
+ | | works | ||
+ | |- | ||
+ | |<source enclose="none">explosion_table(r)</source> | ||
| table | | table | ||
| | | | ||
* r = radius | * r = radius | ||
* returns sth like <source enclose="none">{{pos1, true}, {pos2}}</source> | * 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 | | works | ||
|- | |- | ||
− | |<source enclose="none"> | + | |<source enclose="none">circle(r)</source> |
− | | table of | + | | table of vectors |
| | | | ||
* r = radius | * r = radius | ||
| works | | works | ||
|- | |- | ||
− | |<source enclose="none"> | + | |<source enclose="none">ring(r)</source> |
− | | table of | + | | table of vectors |
| | | | ||
* r = radius | * r = radius | ||
Line 146: | Line 278: | ||
| works | | works | ||
|- | |- | ||
− | |<source enclose="none"> | + | |<source enclose="none">chunkcorner(pos)</source> |
− | | | + | | vector |
| should return the chunkcorner near pos | | should return the chunkcorner near pos | ||
| could work | | could work | ||
|- | |- | ||
− | |<source enclose="none">vector.zero</source> | + | |<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} | | {x=0, y=0, z=0} | ||
| not a function | | not a function | ||
| works | | 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:
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 |