-- Track Circuit Breaks and Track Sections - Player interaction -- Get current translator local S = advtrains.interlocking.translate local players_assign_tcb = {} local players_assign_signal = {} local players_assign_xlink = {} local players_link_ts = {} local players_assign_fixedlocks = {} local atil = advtrains.interlocking local ildb = advtrains.interlocking.db local ilrs = advtrains.interlocking.route local sigd_equal = advtrains.interlocking.sigd_equal local lntrans = { "A", "B" } local function sigd_to_string(sigd) return minetest.pos_to_string(sigd.p).." / "..lntrans[sigd.s] end advtrains.interlocking.sigd_to_string = sigd_to_string minetest.register_node("advtrains_interlocking:tcb_node", { drawtype = "mesh", paramtype="light", paramtype2="facedir", walkable = false, selection_box = { type = "fixed", fixed = {-1/6, -1/2, -1/6, 1/6, 1/4, 1/6}, }, mesh = "at_il_tcb_node.obj", tiles = {"at_il_tcb_node.png"}, description=S("Track Circuit Break"), sunlight_propagates=true, groups = { cracky=3, not_blocking_trains=1, --save_in_at_nodedb=2, at_il_track_circuit_break = 1, }, after_place_node = function(pos, node, player) local meta = minetest.get_meta(pos) meta:set_string("infotext", S("Unconfigured Track Circuit Break, right-click to assign.")) end, on_rightclick = function(pos, node, player) local pname = player:get_player_name() if not minetest.check_player_privs(pname, "interlocking") then minetest.chat_send_player(pname, S("Insufficient privileges to use this!")) return end local meta = minetest.get_meta(pos) local tcbpts = meta:get_string("tcb_pos") if tcbpts ~= "" then local tcbpos = minetest.string_to_pos(tcbpts) local tcb = ildb.get_tcb(tcbpos) if tcb then advtrains.interlocking.show_tcb_form(tcbpos, pname) else minetest.chat_send_player(pname, S("This TCB has been removed. Please dig marker.")) end else --unconfigured minetest.chat_send_player(pname, S("Configuring TCB: Please punch the rail you want to assign this TCB to.")) players_assign_tcb[pname] = pos end end, --on_punch = function(pos, node, player) -- local meta = minetest.get_meta(pos) -- local tcbpts = meta:get_string("tcb_pos") -- if tcbpts ~= "" then -- local tcbpos = minetest.string_to_pos(tcbpts) -- advtrains.interlocking.show_tcb_marker(tcbpos) -- end --end, can_dig = function(pos, player) if player == nil then return false end local pname = player:get_player_name() -- Those markers can only be dug when all adjacent TS's are set -- as EOI. local meta = minetest.get_meta(pos) local tcbpts = meta:get_string("tcb_pos") if tcbpts ~= "" then if not minetest.check_player_privs(pname, "interlocking") then minetest.chat_send_player(pname, S("Insufficient privileges to use this!")) return end local tcbpos = minetest.string_to_pos(tcbpts) local tcb = ildb.get_tcb(tcbpos) if not tcb then return true end for connid=1,2 do if tcb[connid].signal then minetest.chat_send_player(pname, S("Can't remove TCB: Both sides must have no signal assigned!")) return false end end end return true end, after_dig_node = function(pos, oldnode, oldmetadata, player) if not oldmetadata or not oldmetadata.fields then return end local pname = player:get_player_name() local tcbpts = oldmetadata.fields.tcb_pos if tcbpts and tcbpts ~= "" then local tcbpos = minetest.string_to_pos(tcbpts) ildb.remove_tcb_at(tcbpos, pname) end end, }) -- Crafting -- set some fallbacks local tcb_core = "default:mese_crystal" local tcb_secondary = "default:mese_crystal_fragment" --alternative recipe items --core if minetest.get_modpath("basic_materials") then tcb_core = "basic_materials:ic" elseif minetest.get_modpath("technic") then tcb_core = "technic:control_logic_unit" end --print("TCB Core: "..tcb_core) --secondary if minetest.get_modpath("mesecons") then tcb_secondary = 'mesecons:wire_00000000_off' end --print("TCB Secondary: "..tcb_secondary) minetest.register_craft({ output = 'advtrains_interlocking:tcb_node 4', recipe = { {tcb_secondary,tcb_core,tcb_secondary}, {'advtrains:dtrack_placer','','advtrains:dtrack_placer'} }, --actually use track in the tcb recipe replacements = { {"advtrains:dtrack_placer","advtrains:dtrack_placer"}, {"advtrains:dtrack_placer","advtrains:dtrack_placer"}, } }) --nil the temp crafting variables tcb_core= nil tcb_secondary = nil minetest.register_on_punchnode(function(pos, node, player, pointed_thing) local pname = player:get_player_name() if not minetest.check_player_privs(pname, "interlocking") then return end -- TCB assignment local tcbnpos = players_assign_tcb[pname] if tcbnpos then if vector.distance(pos, tcbnpos)<=20 then local node_ok, conns, rhe = advtrains.get_rail_info_at(pos) if node_ok and #conns == 2 then -- if there is already a tcb here, reassign it if ildb.get_tcb(pos) then minetest.chat_send_player(pname, S("Configuring TCB: Already existed at this position, it is now linked to this TCB marker")) else ildb.create_tcb_at(pos, pname) end local meta = minetest.get_meta(tcbnpos) meta:set_string("tcb_pos", minetest.pos_to_string(pos)) meta:set_string("infotext", S("TCB assigned to @1", minetest.pos_to_string(pos))) minetest.chat_send_player(pname, S("Configuring TCB: Successfully configured TCB")) advtrains.interlocking.show_tcb_marker(pos) else minetest.chat_send_player(pname, S("Configuring TCB: This is not a normal two-connection rail! Aborted.")) end else minetest.chat_send_player(pname, S("Configuring TCB: Node is too far away. Aborted.")) end players_assign_tcb[pname] = nil end -- Signal assignment local sigd = players_assign_signal[pname] if sigd then if vector.distance(pos, sigd.p)<=50 then local is_signal = minetest.get_item_group(node.name, "advtrains_signal") >= 2 if is_signal then local ndef = minetest.registered_nodes[node.name] if ndef and ndef.advtrains and ndef.advtrains.apply_aspect then local tcbs = ildb.get_tcbs(sigd) if tcbs then ildb.assign_signal_to_tcbs(pos, sigd) -- use auto-naming advtrains.interlocking.add_autoname_to_tcbs(tcbs, pname) minetest.chat_send_player(pname, S("Configuring TCB: Successfully assigned signal.")) advtrains.interlocking.show_ip_form(pos, pname, true) else minetest.chat_send_player(pname, S("Configuring TCB: Internal error, TCBS doesn't exist. Aborted.")) end else minetest.chat_send_player(pname, S("Configuring TCB: Cannot use static signals for routesetting. Aborted.")) end else minetest.chat_send_player(pname, S("Configuring TCB: Not a compatible signal. Aborted.")) end else minetest.chat_send_player(pname, S("Configuring TCB: Node is too far away. Aborted.")) end players_assign_signal[pname] = nil end -- FixedLocks assignment local ts_id = players_assign_fixedlocks[pname] if ts_id then if advtrains.is_passive(pos) then local pts = advtrains.encode_pos(pos) local state = advtrains.getstate(pos) local ts = ildb.get_ts(ts_id) if ts and ts.fixed_locks then minetest.chat_send_player(pname, S("@1 locks in state @2", minetest.pos_to_string(pos), state)) ts.fixed_locks[pts] = state else minetest.chat_send_player(pname, S("Error: TS modified, abort!")) players_assign_fixedlocks[pname] = nil end else minetest.chat_send_player(pname, S("Setting fixed locks finished!")) players_assign_fixedlocks[pname] = nil ildb.update_rs_cache(ts_id) advtrains.interlocking.show_ts_form(ts_id, pname) end end end) -- "Self-contained TCB" -- 2024-11-25: Buffers should become their own TCB (and signal) automatically to permit setting routes to them -- These are support functions for this kind of node. -- Create an after_place_node callback for a self-contained TCB node. The parameters control additional behavior: -- fail_silently_on_noprivs: (boolean) Does not give an error in case the placer does not have the interlocking privilege -- auto_create_self_signal: (boolean) Automatically assign this same node as signal to the A side of the newly-created TCB -- (this is useful for buffers as they serve both as TCB and as an always-halt signal) function advtrains.interlocking.self_tcb_make_after_place_callback(fail_silently_on_noprivs, auto_create_self_signal) return function(pos, player, itemstack, pointed_thing) local pname = player:get_player_name() if not minetest.check_player_privs(pname, "interlocking") then if not fail_silently_on_noprivs then minetest.chat_send_player(pname, S("Insufficient privileges to use this!")) end return end if ildb.get_tcb(pos) then minetest.chat_send_player(pname, S("TCB already existed at this position, now linked to this node")) else ildb.create_tcb_at(pos, pname) end if auto_create_self_signal then local sigd = { p = pos, s = 1 } local tcbs = ildb.get_tcbs(sigd) -- make sure signal doesn't already exist if tcbs.signal then minetest.chat_send_player(pname, S("Signal on B side already assigned!")) return end ildb.assign_signal_to_tcbs(pos, sigd) -- assign influence point to itself ildb.set_ip_signal(advtrains.roundfloorpts(pos), 1, pos) -- use auto-naming advtrains.interlocking.add_autoname_to_tcbs(tcbs, pname) end end end -- Create an can_dig callback for a self-contained TCB node. The parameters control additional behavior: -- is_signal: (boolean) Whether this node is also a signal (in addition to being a TCB), e.g. when auto_create_self_signal was set. -- Causes also the signal API's can_dig to be called function advtrains.interlocking.self_tcb_make_can_dig_callback(is_signal) return function(pos, player) local pname = player and player:get_player_name-- train_related.lua -- Occupation of track sections - mainly implementation of train callbacks --[[ Track section occupation is saved as follows In train: train.il_sections = { [n] = {ts_id = <...> (origin = <sigd>)} } -- "origin" is the TCB (signal describer) the train initially entered this section In track section ts.trains = { [n] = <train_id> } When any inconsistency is detected, we will assume the most restrictive setup. It will be possible to indicate a section "free" via the GUI. ]] local ildb = advtrains.interlocking.db local sigd_equal = advtrains.interlocking.sigd_equal local function itexist(tbl, com) for _,item in ipairs(tbl) do if (item==com) then return true end end return false end local function itkexist(tbl, ikey, com) for _,item in ipairs(tbl) do if item[ikey] == com then return true end end return false end local function itremove(tbl, com) local i=1 while i <= #tbl do if tbl[i] == com then table.remove(tbl, i) else i = i + 1 end end end local function itkremove(tbl, ikey, com) local i=1 while i <= #tbl do if tbl[i][ikey] == com then table.remove(tbl, i) else i = i + 1 end end end local function setsection(tid, train, ts_id, ts, sigd) -- train if not train.il_sections then train.il_sections = {} end if not itkexist(train.il_sections, "ts_id", ts_id) then table.insert(train.il_sections, {ts_id = ts_id, origin = sigd}) end -- ts if not ts.trains then ts.trains = {} end if not itexist(ts.trains, tid) then table.insert(ts.trains, tid) end -- routes local tcbs = advtrains.interlocking.db.get_tcbs(sigd) -- route setting - clear route state if ts.route then --atdebug(tid,"enters",ts_id,"examining Routestate",ts.route) if not sigd_equal(ts.route.entry, sigd) then -- Train entered not from the route. Locate origin and cancel route! atwarn("Train",tid,"hit route",ts.route.rsn,"!") advtrains.interlocking.route.cancel_route_from(ts.route.origin) atwarn("Route was cancelled.") else -- train entered route regularily. Reset route and signal tcbs.route_committed = nil tcbs.route_comitted = nil -- TODO compatibility cleanup tcbs.aspect = nil tcbs.route_origin = nil advtrains.interlocking.update_signal_aspect(tcbs) if tcbs.signal and sigd_equal(ts.route.entry, ts.route.origin) then if tcbs.route_auto and tcbs.routeset then --atdebug("Resetting route (",ts.route.origin,")") advtrains.interlocking.route.update_route(ts.route.origin, tcbs) else tcbs.routeset = nil end end end ts.route = nil end if tcbs.signal then advtrains.interlocking.route.update_route(sigd, tcbs) end end local function freesection(tid, train, ts_id, ts) -- train if not train.il_sections then train.il_sections = {} end itkremove(train.il_sections, "ts_id", ts_id) -- ts if not ts.trains then ts.trains = {} end itremove(ts.trains, tid) if ts.route_post then advtrains.interlocking.route.free_route_locks(ts_id, ts.route_post.locks) if ts.route_post.next then --this does nothing when the train went the right way, because -- "route" info is already cleared. advtrains.interlocking.route.cancel_route_from(ts.route_post.next) end ts.route_post = nil end -- This must be delayed, because this code is executed in-between a train step -- TODO use luaautomation timers? minetest.after(0, advtrains.interlocking.route.update_waiting, "ts", ts_id) end -- This is regular operation -- The train is on a track and drives back and forth -- This sets the section for both directions, to be failsafe advtrains.tnc_register_on_enter(function(pos, id, train, index) local tcb = ildb.get_tcb(pos) if tcb then for connid=1,2 do local ts = tcb[connid].ts_id and ildb.get_ts(tcb[connid].ts_id) if ts then setsection(id, train, tcb[connid].ts_id, ts, {p=pos, s=connid}) end end end end) -- this time, of course, only clear the backside (cp connid) advtrains.tnc_register_on_leave(function(pos, id, train, index) local tcb = ildb.get_tcb(pos) if tcb and train.path_cp[index] then local connid = train.path_cp[index] local ts = tcb[connid].ts_id and ildb.get_ts(tcb[connid].ts_id) if ts then freesection(id, train, tcb[connid].ts_id, ts) end end end) -- those callbacks are needed to account for created and removed trains (also regarding coupling) advtrains.te_register_on_create(function(id, train) -- let's see what track sections we find here local index = atround(train.index) local pos = advtrains.path_get(train, index) local ts_id, origin = ildb.get_ts_at_pos(pos) if ts_id then local ts = ildb.get_ts(ts_id) if ts then setsection(id, train, ts_id, ts, origin) else atwarn("ILDB corruption: TCB",origin," has invalid TS reference") end -- Make train a shunt move train.is_shunt = true elseif ts_id==nil then atlog("Train",id,": Unable to determine whether to block a track section!") else --atdebug("Train",id,": Outside of interlocked area!") end end) advtrains.te_register_on_remove(function(id, train) if train.il_sections then for idx, item in ipairs(train.il_sections) do local ts = item.ts_id and ildb.get_ts(item.ts_id) if ts and ts.trains then itremove(ts.trains, id) end end train.il_sections = nil end end)