User:Hybrid Dog

From Minetest Developer Wiki
(Redirected from minetest.on place)
Jump to navigation Jump to search

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