aboutsummaryrefslogtreecommitdiff
path: root/lib/lua/src
diff options
context:
space:
mode:
authorGabriel Pérez-Cerezo <gabriel@gpcf.eu>2021-03-21 22:06:04 +0100
committerGabriel Pérez-Cerezo <gabriel@gpcf.eu>2021-03-26 01:47:27 +0100
commit98021002253fd1646230c889bc0c8ff4dd1a48e8 (patch)
treecc00067c4874f4ab6f8e078d8431af5e3fb871c8 /lib/lua/src
parentec76fb87e6c329d008e9d05be7caae10bb46fb1c (diff)
anonymize player list
Diffstat (limited to 'lib/lua/src')
0 files changed, 0 insertions, 0 deletions
a> 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
-- 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)
	-- hack for old signals. Compatibility only, DO NOT USE for new signals!
	if advtrains.interlocking and ndef.advtrains._is_passivenode_signal then
		-- forcefully clears any set aspect, so that aspect system doesnt override it again
		-- implicitly does an signal.notify_trains(pos)
		advtrains.interlocking.signal.clear_aspect(pos)
	end

	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