aboutsummaryrefslogtreecommitdiff
path: root/util
diff options
context:
space:
mode:
authorLoic Blot <loic.blot@unix-experience.fr>2021-04-29 09:30:19 +0200
committerLoïc Blot <nerzhul@users.noreply.github.com>2021-05-03 19:49:19 +0200
commitccdd886e273ec2fa5f8cfe1d1f474914eccb2abf (patch)
tree076dd76bafc39c824aaa4e372502514c14d6640a /util
parenta47a00228b7be97a740081ec9ed86f108021ad6d (diff)
refacto: Hud: make driver, client, player, inventory, tsrc private & drop unused guienv
also fix c_content.h, on client it includes the src/client/hud.h instead of src/hud.h, which leads to wrong file dependency on the lua stack
Diffstat (limited to 'util')
0 files changed, 0 insertions, 0 deletions
href='#n88'>88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
-- passive.lua
-- Rework for advtrains 2.5: The passive API now uses the reworked node_state system. Please see the comment in tracks.lua

function advtrains.getstate(parpos, pnode)
	local pos
	if atlatc then
		pos = atlatc.pcnaming.resolve_pos(parpos)
	else
		pos = advtrains.round_vector_floor_y(parpos)
	end
	if type(pos)~="table" or (not pos.x or not pos.y or not pos.z) then
		debug.sethook()
		error("Invalid position supplied to getstate")
	end
	local node=pnode or advtrains.ndb.get_node(pos)
	local ndef=minetest.registered_nodes[node.name]
	local st
	if ndef and ndef.advtrains then
		return ndef.advtrains.node_state
	end
end

function advtrains.setstate(parpos, newstate, pnode)
	local pos
	if atlatc then
		pos = atlatc.pcnaming.resolve_pos(parpos)
	else
		pos = advtrains.round_vector_floor_y(parpos)
	end
	if type(pos)~="table" or (not pos.x or not pos.y or not pos.z) then
		debug.sethook()
		error("Invalid position supplied to setstate")
	end
	local node=pnode or advtrains.ndb.get_node(pos)
	local ndef=minetest.registered_nodes[node.name]
	
	if not ndef or not ndef.advtrains then
		return false, "missing_node_def"
	end
	local old_state = ndef.advtrains.node_state
	
	if old_state == newstate then
		-- nothing needs to be done
		return true
	end
	
	if not ndef.advtrains.node_state_map then
		return false, "missing_node_state_map"
	end
	local new_node_name = ndef.advtrains.node_state_map[newstate]
	if not new_node_name then
		return false, "no_such_state"
	end
	
	-- prevent state switching when route lock or train is present
	if advtrains.get_train_at_pos(pos) then
		return false, "train_here"
	end
	
	if advtrains.interlocking and advtrains.interlocking.route.has_route_lock(advtrains.encode_pos(pos)) then
		return false, "route_lock_here"
	end
	
	-- perform the switch
	local new_node = {name = new_node_name, param2 = node.param2}
	advtrains.ndb.swap_node(pos, new_node)
	-- if callback is present, call it
	if ndef.advtrains.node_on_switch_state then
		ndef.advtrains.node_on_switch_state(pos, new_node, old_state, newstate)
	end
	-- invalidate paths (only relevant if this is a track)
	advtrains.invalidate_all_paths(pos)

	return true
end

function advtrains.is_passive(parpos, pnode)
	local pos
	if atlatc then
		pos = atlatc.pcnaming.resolve_pos(parpos)
	else
		pos = advtrains.round_vector_floor_y(parpos)
	end
	if type(pos)~="table" or (not pos.x or not pos.y or not pos.z) then
		debug.sethook()
		error("Invalid position supplied to getstate")
	end
	local node=pnode or advtrains.ndb.get_node(pos)
	local ndef=minetest.registered_nodes[node.name]
	if ndef and ndef.advtrains and ndef.advtrains.node_state_map then
		return true
	else
		return false
	end
end

-- switches a node back to fallback state, if defined. Doesn't support pcnaming.
function advtrains.set_fallback_state(pos, pnode)
	local node=pnode or advtrains.ndb.get_node(pos)
	local ndef=minetest.registered_nodes[node.name]
	
	if not ndef or not ndef.advtrains or not ndef.advtrains.node_fallback_state then