-- 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) "]" end elseif caps >= 3 then -- it's a buffer! form = form.."label[0.5,2.5;"..S("This is an always-halt signal (e.g. a buffer)\nNo routes can be set from here.").."]" form = form.."button_exit[5.5,4;1.5,1;unassign;"..S("Unassign\nSignal").."]" else -- signal caps say it cannot be route start/end form = form.."label[0.5,2.5;"..S("This is a pure distant signal\nNo route is currently set through.").."]" form = form.."button_exit[5.5,4;1.5,1;unassign;"..S("Unassign\nSignal").."]" end end elseif sigd_equal(tcbs.route_origin, sigd) then -- something has gone wrong: tcbs.routeset should have been set... form = form.."label[0.5,2.5;".."Inconsistent state: route_origin is same TCBS but no route set. Try again.".."]" ilrs.cancel_route_from(sigd) else form = form.."label[0.5,2.5;"..S("Route is set over this signal by:").."\n"..sigd_to_string(tcbs.route_origin).."]" form = form.."label[0.5,4;"..S("Wait for this route to be cancelled in order to do anything here.").."]" end end sig_pselidx[pname] = sel_rte minetest.show_formspec(pname, "at_il_signalling_"..minetest.pos_to_string(sigd.p).."_"..sigd.s, form) p_open_sig_form[pname] = sigd -- always a good idea to update the signal aspect if not called_from_form_update then -- FIX prevent a callback loop advtrains.interlocking.signal.update_route_aspect(tcbs) end end function advtrains.interlocking.update_player_forms(sigd) for pname, tsigd in pairs(p_open_sig_form) do if advtrains.interlocking.sigd_equal(sigd, tsigd) then advtrains.interlocking.show_signalling_form(sigd, pname, nil, true) end end end minetest.register_on_player_receive_fields(function(player, formname, fields) local pname = player:get_player_name() if not minetest.check_player_privs(pname, "train_operator") then return end local hasprivs = minetest.check_player_privs(pname, "interlocking") local tpsi = sig_pselidx[pname] local pts, connids = string.match(formname, "^at_il_signalling_([^_]+)_(%d)$") local pos, connid if pts then pos = minetest.string_to_pos(pts) connid = tonumber(connids) if not connid or connid<1 or connid>2 then return end end if pos and connid then local sigd = {p=pos, s=connid} local tcbs = ildb.get_tcbs(sigd) if not tcbs then return end if fields.quit and not fields.unassign then sig_pselidx[pname] = nil p_open_sig_form[pname] = nil -- form quit: disable temporary ARS ignore tcbs.ars_ignore_next = nil return end local sel_rte if fields.rtelist then local tev = minetest.explode_textlist_event(fields.rtelist) if tev.type ~= "INV" then sel_rte = tev.index end elseif tpsi then sel_rte = tpsi end if fields.setname and fields.name and hasprivs then if fields.name == "" then tcbs.signal_name = nil -- do not save a signal name if it isnt used (equivalent to track sections) else tcbs.signal_name = fields.name end end if tcbs.routeset and fields.cancelroute then tcbs.ars_ignore_next = true -- if route committed, cancel route ts info ilrs.update_route(sigd, tcbs, nil, true) end if not tcbs.routeset then if fields.newroute and hasprivs then advtrains.interlocking.init_route_prog(pname, sigd) minetest.close_formspec(pname, formname) tcbs.ars_ignore_next = nil p_open_sig_form[pname] = nil -- form is closed/left, do not reopen return end if fields.smartroute and hasprivs then advtrains.interlocking.smartroute.start(pname, sigd) tcbs.ars_ignore_next = nil p_open_sig_form[pname] = nil -- form is closed/left, do not reopen return end if sel_rte and tcbs.routes[sel_rte] then if fields.setroute then ilrs.update_route(sigd, tcbs, sel_rte) end if fields.dsproute then local t = os.clock() advtrains.interlocking.visualize_route(sigd, tcbs.routes[sel_rte], "disp_"..t) minetest.after(10, function() advtrains.interlocking.clear_visu_context("disp_"..t) end) end if fields.editroute and hasprivs then advtrains.interlocking.show_route_edit_form(pname, sigd, sel_rte) p_open_sig_form[pname] = nil -- form is closed/left, do not reopen return end if fields.setarsdefault and hasprivs then for rid, route in ipairs(tcbs.routes) do local isdefault = rid == sel_rte if route.ars then if route.ars.default and isdefault then -- D button pressed but route was already default - remove ars default field! route.ars.default = nil elseif isdefault then route.ars.default = true else route.ars.default = nil end -- if the table is nouw empty delete it if not next(route.ars) then route.ars = nil end elseif isdefault then route.ars = {default = true} end end end if fields.delroute and hasprivs then if tcbs.routes[sel_rte] and tcbs.routes[sel_rte].ars then minetest.chat_send_player(pname, S("Cannot delete route which has ARS rules, please review and then delete through edit dialog!")) else table.remove(tcbs.routes,sel_rte) end end end end if fields.unassign and hasprivs then -- unassigning the signal from the tcbs -- only when no route is set. -- Routes and name remain saved, in case the player wants to reassign a new signal if not tcbs.routeset then local signal_pos = tcbs.signal ildb.set_sigd_for_signal(signal_pos, nil) tcbs.signal = nil tcbs.route_aspect = nil tcbs.route_remote = nil p_open_sig_form[pname] = nil -- form is closed/left, do not reopen minetest.chat_send_player(pname, S("Signal has been unassigned. Name and routes are kept for reuse.")) return else minetest.chat_send_player(pname, S("Please cancel route first!")) end end if fields.ars then tcbs.ars_disabled = not minetest.is_yes(fields.ars) end if fields.dstarstrig then tcbs.no_dst_ars_trig = not minetest.is_yes(fields.dstarstrig) end if fields.auto then tcbs.route_auto = true end if fields.noauto then tcbs.route_auto = false end if sel_rte and tcbs.routes[sel_rte]and not tcbs.routeset then if fields.moveup then if tcbs.routes[sel_rte - 1] then tcbs.routes[sel_rte - 1], tcbs.routes[sel_rte] = tcbs.routes[sel_rte], tcbs.routes[sel_rte - 1] sel_rte = sel_rte - 1 end elseif fields.movedown then if tcbs.routes[sel_rte + 1] then tcbs.routes[sel_rte + 1], tcbs.routes[sel_rte] = tcbs.routes[sel_rte], tcbs.routes[sel_rte + 1] sel_rte = sel_rte + 1 end end end advtrains.interlocking.show_signalling_form(sigd, pname, sel_rte, true) return end end)