aboutsummaryrefslogtreecommitdiff
path: root/advtrains/license_media.txt
diff options
context:
space:
mode:
Diffstat (limited to 'advtrains/license_media.txt')
0 files changed, 0 insertions, 0 deletions
a id='n59' href='#n59'>59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
-- mesecon_controller.lua
-- Mesecon-interfaceable Operation Panel alternative
-- Looks like a Mesecon Luacontroller

-- Luacontroller Adapted Code
-- From Mesecons mod https://mesecons.net/
-- (c) Jeija and Contributors

local BASENAME = "advtrains_luaautomation:mesecon_controller"

local rules = {
	a = {x = -1, y = 0, z =  0, name="A"},
	b = {x =  0, y = 0, z =  1, name="B"},
	c = {x =  1, y = 0, z =  0, name="C"},
	d = {x =  0, y = 0, z = -1, name="D"},
}

local function generate_name(ports)
	local d = ports.d and 1 or 0
	local c = ports.c and 1 or 0
	local b = ports.b and 1 or 0
	local a = ports.a and 1 or 0
	return BASENAME..d..c..b..a
end


local function set_port(pos, rule, state)
	if state then
		mesecon.receptor_on(pos, {rule})
	else
		mesecon.receptor_off(pos, {rule})
	end
end

local function clean_port_states(ports)
	ports.a = ports.a and true or false
	ports.b = ports.b and true or false
	ports.c = ports.c and true or false
	ports.d = ports.d and true or false
end

-- Local table for storing which Mesecons off events should be ignored
-- Indexed by hex encoded position
local ignored_off_events = {}

local function set_port_states(pos, ports)
	local node = advtrains.ndb.get_node(pos)
	local name = node.name
	clean_port_states(ports)
	local vports = minetest.registered_nodes[name].virtual_portstates
	local new_name = generate_name(ports)

	if name ~= new_name and vports then
		-- Problem:
		-- We need to place the new node first so that when turning
		-- off some port, it won't stay on because the rules indicate
		-- there is an onstate output port there.
		-- When turning the output off then, it will however cause feedback
		-- so that the luacontroller will receive an "off" event by turning
		-- its output off.
		-- Solution / Workaround:
		-- Remember which output was turned off and ignore next "off" event.
		local ph=minetest.pos_to_string(pos)
		local railtbl = atlatc.active.nodes[ph]
		if not railtbl then return end

		local ign = railtbl.ignored_off_events or {}
		if ports.a and not vports.a and not mesecon.is_powered(pos, rules.a) then ign.A = true end
		if ports.b and not vports.b and not mesecon.is_powered(pos, rules.b) then ign.B = true end
		if ports.c and not vports.c and not mesecon.is_powered(pos, rules.c) then ign.C = true end
		if ports.d and not vports.d and not mesecon.is_powered(pos, rules.d) then ign.D = true end
		railtbl.ignored_off_events = ign

		advtrains.ndb.swap_node(pos, {name = new_name, param2 = node.param2})

		-- Apply mesecon state only if node loaded
		-- If node is not loaded, mesecon update will occur on next load via on_updated_from_nodedb
		if advtrains.is_node_loaded(pos) then
			if ports.a ~= vports.a then set_port(pos, rules.a, ports.a) end
			if ports.b ~= vports.b then set_port(pos, rules.b, ports.b) end
			if ports.c ~= vports.c then set_port(pos, rules.c, ports.c) end
			if ports.d ~= vports.d then set_port(pos, rules.d, ports.d) end
		end
	end
end

local function on_updated_from_nodedb(pos, newnode, oldnode)
	-- Switch appropriate Mesecon receptors depending on the node change
	local vports = minetest.registered_nodes[oldnode.name].virtual_portstates
	local ports = minetest.registered_nodes[newnode.name].virtual_portstates
	if ports.a ~= vports.a then set_port(pos, rules.a, ports.a) end
	if ports.b ~= vports.b then set_port(pos, rules.b, ports.b) end
	if ports.c ~= vports.c then set_port(pos, rules.c, ports.c) end
	if ports.d ~= vports.d then set_port(pos, rules.d, ports.d) end
end

local function ignore_offevent(pos, rule)
	local ph=minetest.pos_to_string(pos)
	local railtbl = atlatc.active.nodes[ph]
	if not railtbl then return nil end
	local ign = railtbl.ignored_off_events
	if ign and ign[rule.name] then
		ign[rule.name] = nil
		return true
	end
	return false
end

local valid_ports = {a=true, b=true, c=true, d=true}

local function fire_event(pos, evtdata)
	local customfct={
		set_mesecon_outputs = function(states)
			assertt(states, "table")
			set_port_states(pos, states)
		end,
		get_mesecon_input = function(port)
			local portl = string.lower(port)
			if not valid_ports[portl] then
				error("get_mesecon_input: Invalid port (expected a,b,c,d)")
			end
			if mesecon.is_powered(pos, rules[portl]) then
				return true
			end
			return false
		end,
	}
	atlatc.active.run_in_env(pos, evtdata, customfct, true)
	
end

local output_rules = {}
local input_rules = {}

local node_box = {
	type = "fixed",
	fixed = {
		{-8/16, -8/16, -8/16, 8/16, -7/16, 8/16}, -- Bottom slab
		{-5/16, -7/16, -5/16, 5/16, -6/16, 5/16}, -- Circuit board
		{-3/16, -6/16, -3/16, 3/16, -5/16, 3/16}, -- IC
	}
}

local selection_box = {