From 5c8962b39bd4f6871ec87a988ac43d7bfad04d2b Mon Sep 17 00:00:00 2001 From: "Y. Wang" Date: Mon, 11 Apr 2022 16:55:50 +0200 Subject: Implement basic route signaling with Japanese signals for demo --- advtrains_interlocking/init.lua | 3 +++ 1 file changed, 3 insertions(+) (limited to 'advtrains_interlocking/init.lua') diff --git a/advtrains_interlocking/init.lua b/advtrains_interlocking/init.lua index a2f5882..d0b75a8 100644 --- a/advtrains_interlocking/init.lua +++ b/advtrains_interlocking/init.lua @@ -12,8 +12,11 @@ end local modpath = minetest.get_modpath(minetest.get_current_modname()) .. DIR_DELIM +advtrains.interlocking.aspects = dofile(modpath.."signal_aspects.lua") + dofile(modpath.."database.lua") dofile(modpath.."signal_api.lua") +dofile(modpath.."signal_aspect_ui.lua") dofile(modpath.."demosignals.lua") dofile(modpath.."train_sections.lua") dofile(modpath.."route_prog.lua") -- cgit v1.2.3 From 98c37108762c6d7c9f1d691b84f49bfa65b81b28 Mon Sep 17 00:00:00 2001 From: "Y. Wang" Date: Sat, 11 Jun 2022 18:07:00 +0200 Subject: Implement primitive distant signaling --- advtrains/formspec.lua | 10 ++ advtrains_interlocking/database.lua | 4 + advtrains_interlocking/demosignals.lua | 6 +- advtrains_interlocking/distant.lua | 137 +++++++++++++++++++++ advtrains_interlocking/distant_ui.lua | 76 ++++++++++++ advtrains_interlocking/init.lua | 3 + advtrains_interlocking/routesetting.lua | 21 ++++ advtrains_interlocking/signal_api.lua | 85 +------------ advtrains_interlocking/signal_aspect_accessors.lua | 127 +++++++++++++++++++ advtrains_interlocking/signal_aspect_ui.lua | 6 +- advtrains_interlocking/signal_aspects.lua | 26 +++- advtrains_interlocking/signal_main_ui.lua | 0 .../spec/basic_signalling_spec.lua | 87 +++++++++++++ advtrains_interlocking/spec/signal_api_spec.lua | 49 -------- advtrains_interlocking/tcb_ts_ui.lua | 8 +- 15 files changed, 507 insertions(+), 138 deletions(-) create mode 100644 advtrains_interlocking/distant.lua create mode 100644 advtrains_interlocking/distant_ui.lua create mode 100644 advtrains_interlocking/signal_aspect_accessors.lua delete mode 100644 advtrains_interlocking/signal_main_ui.lua create mode 100644 advtrains_interlocking/spec/basic_signalling_spec.lua delete mode 100644 advtrains_interlocking/spec/signal_api_spec.lua (limited to 'advtrains_interlocking/init.lua') diff --git a/advtrains/formspec.lua b/advtrains/formspec.lua index aa5aa69..20dab59 100644 --- a/advtrains/formspec.lua +++ b/advtrains/formspec.lua @@ -9,6 +9,14 @@ local function make_list(entries) return table.concat(t, ",") end +local function f_button(x, y, w, h, id, text) + return sformat("button[%f,%f;%f,%f;%s;%s]", x, y, w, h, id, text) +end + +local function S_button(x, y, w, h, id, ...) + return f_button(x, y, w, h, id, attrans(...)) +end + local function f_button_exit(x, y, w, h, id, text) return sformat("button_exit[%f,%f;%f,%f;%s;%s]", x, y, w, h, id, text) end @@ -54,6 +62,8 @@ local function f_tabheader(x, y, w, h, id, entries, sel, transparent, border) end return { + button = f_button, + S_button = S_button, button_exit = f_button_exit, S_button_exit = S_button_exit, dropdown = f_dropdown, diff --git a/advtrains_interlocking/database.lua b/advtrains_interlocking/database.lua index efa5eb8..c5ae906 100644 --- a/advtrains_interlocking/database.lua +++ b/advtrains_interlocking/database.lua @@ -134,6 +134,9 @@ function ildb.load(data) if data.supposed_aspects then advtrains.interlocking.load_supposed_aspects(data.supposed_aspects) end + if data.distant then + advtrains.distant.load(data.distant) + end --COMPATIBILITY to Signal aspect format -- TODO remove in time... @@ -177,6 +180,7 @@ function ildb.save() influence_points = influence_points, npr_rails = advtrains.interlocking.npr_rails, supposed_aspects = advtrains.interlocking.save_supposed_aspects(), + distant = advtrains.distant.save(), } end diff --git a/advtrains_interlocking/demosignals.lua b/advtrains_interlocking/demosignals.lua index 1c1b8b2..de6926a 100644 --- a/advtrains_interlocking/demosignals.lua +++ b/advtrains_interlocking/demosignals.lua @@ -50,7 +50,7 @@ minetest.register_node("advtrains_interlocking:ds_danger", { }, on_rightclick = advtrains.interlocking.signal_rc_handler, can_dig = advtrains.interlocking.signal_can_dig, - after_dig_node = advtrains.interlocking.signal_after_dig, + after_destruct = advtrains.interlocking.signal_after_dig, }) minetest.register_node("advtrains_interlocking:ds_free", { description = "Demo signal at Free", @@ -71,7 +71,7 @@ minetest.register_node("advtrains_interlocking:ds_free", { }, on_rightclick = advtrains.interlocking.signal_rc_handler, can_dig = advtrains.interlocking.signal_can_dig, - after_dig_node = advtrains.interlocking.signal_after_dig, + after_destruct = advtrains.interlocking.signal_after_dig, }) minetest.register_node("advtrains_interlocking:ds_slow", { description = "Demo signal at Slow", @@ -92,6 +92,6 @@ minetest.register_node("advtrains_interlocking:ds_slow", { }, on_rightclick = advtrains.interlocking.signal_rc_handler, can_dig = advtrains.interlocking.signal_can_dig, - after_dig_node = advtrains.interlocking.signal_after_dig, + after_destruct = advtrains.interlocking.signal_after_dig, }) diff --git a/advtrains_interlocking/distant.lua b/advtrains_interlocking/distant.lua new file mode 100644 index 0000000..ffa9e08 --- /dev/null +++ b/advtrains_interlocking/distant.lua @@ -0,0 +1,137 @@ +local db_distant = {} +local db_distant_of = {} + +local A = advtrains.interlocking.aspects +local pts = advtrains.encode_pos +local stp = advtrains.decode_pos + +local function db_load(x) + if type(x) ~= "table" then + return + end + db_distant = x.distant + db_distant_of = x.distant_of +end + +local function db_save() + return { + distant = db_distant, + distant_of = db_distant_of, + } +end + +local update_signal, update_main, update_dst + +local function unassign_dst(dst, force) + local pts_dst = pts(dst) + local main = db_distant_of[pts_dst] + db_distant_of[pts_dst] = nil + if main then + local pts_main = main[1] + local t = db_distant[pts_main] + if t then + t[pts_dst] = nil + end + end + if not force then + update_dst(dst) + end +end + +local function unassign_main(main, force) + local pts_main = pts(main) + local t = db_distant[pts_main] + if not t then + return + end + for pts_dst in pairs(t) do + local realmain = db_distant_of[pts_dst] + if realmain and realmain[1] == pts_main then + db_distant_of[pts_dst] = nil + if not force then + local dst = stp(pts_dst) + update_dst(dst) + end + end + end + db_distant[pts_main] = nil +end + +local function unassign_all(pos, force) + unassign_main(pos) + unassign_dst(pos, force) +end + +local function assign(main, dst, by) + local pts_main = pts(main) + local pts_dst = pts(dst) + local t = db_distant[pts_main] + if not t then + t = {} + db_distant[pts_main] = t + end + if not by then + by = "manual" + end + unassign_dst(dst, true) + t[pts_dst] = by + db_distant_of[pts_dst] = {pts_main, by} + update_dst(dst) +end + +local function pre_occupy(dst, by) + local pts_dst = pts(dst) + unassign_dst(dst) + db_distant_of[pts_dst] = {nil, by} +end + +local function get_distant(main) + local pts_main = pts(main) + return db_distant[pts_main] or {} +end + +local function get_main(dst) + local pts_dst = pts(dst) + local main = db_distant_of[pts_dst] + if not main then + return + end + if main[1] then + return stp(main[1]), unpack(main, 2) + else + return unpack(main) + end +end + +update_main = function(main) + local pts_main = pts(main) + local t = get_distant(main) + for pts_dst in pairs(t) do + local dst = stp(pts_dst) + advtrains.interlocking.signal_readjust_aspect(dst) + end +end + +update_dst = function(dst) + advtrains.interlocking.signal_readjust_aspect(dst) +end + +update_signal = function(pos) + update_main(pos) + update_dst(pos) +end + +advtrains.distant = { + load = db_load, + save = db_save, + assign = assign, + unassign_dst = unassign_dst, + unassign_main = unassign_main, + unassign_all = unassign_all, + get_distant = get_distant, + get_dst = get_distant, + get_main = get_main, + update_main = update_main, + update_dst = update_dst, + update_signal = update_signal, +} diff --git a/advtrains_interlocking/distant_ui.lua b/advtrains_interlocking/distant_ui.lua new file mode 100644 index 0000000..4ec2255 --- /dev/null +++ b/advtrains_interlocking/distant_ui.lua @@ -0,0 +1,76 @@ +local F = advtrains.formspec +local D = advtrains.distant +local I = advtrains.interlocking + +function advtrains.interlocking.show_distant_signal_form(pos, pname) + local form = {"size[7,7]"} + form[#form+1] = advtrains.interlocking.make_signal_formspec_tabheader(pname, pos, 7, 3) + local main, set_by = D.get_main(pos) + if main then + local pts_main = minetest.pos_to_string(main) + form[#form+1] = F.S_label(0.5, 0.5, "This signal is a distant signal of @1.", pts_main) + if set_by == "manual" then + form[#form+1] = F.S_label(0.5, 1, "The assignment is made manually.") + elseif set_by == "routesetting" then + form[#form+1] = F.S_label(0.5, 1, "The assignment is made by the routesetting system.") + end + else + form[#form+1] = F.S_label(0.5, 0.5, "This signal is not assigned to a main signal.") + form[#form+1] = F.S_label(0.5, 1, "The distant aspect of the signal is not used.") + end + if set_by ~= nil then + form[#form+1] = F.S_button_exit(0.5, 1.5, 3, 1, "unassign_dst", "Unassign") + form[#form+1] = F.S_button_exit(3.5, 1.5, 3, 1, "assign_dst", "Reassign") + else + form[#form+1] = F.S_button_exit(0.5, 1.5, 6, 1, "assign_dst", "Assign") + end + minetest.show_formspec(pname, "advtrains:distant_" .. minetest.pos_to_string(pos), table.concat(form)) +end + +local signal_pos = {} +local function init_signal_assignment(pname, pos) + if not minetest.check_player_privs(pname, "interlocking") then + minetest.chat_send_player(pname, attrans("This operation is not allowed without the @1 privilege.", "interlocking")) + return + end + signal_pos[pname] = pos + minetest.chat_send_player(pname, attrans("Please punch the signal to use as the main signal.")) +end + +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 + local spos = signal_pos[pname] + if not spos then + return + end + signal_pos[pname] = nil + local is_signal = minetest.get_item_group(node.name, "advtrains_signal") >= 2 + if not is_signal then + minetest.chat_send_player(pname, attrans("Incompatible signal.")) + return + end + minetest.chat_send_player(pname, attrans("Successfully assigned signal.")) + D.assign(pos, spos, "manual") +end) + +minetest.register_on_player_receive_fields(function(player, formname, fields) + local pname = player:get_player_name() + local pos = minetest.string_to_pos(string.match(formname, "^advtrains:distant_(.+)$") or "") + if not pos then + return + end + if not minetest.check_player_privs(pname, "interlocking") then + return + end + if advtrains.interlocking.handle_signal_formspec_tabheader_fields(pname, fields) then + return true + end + if fields.unassign_dst then + D.unassign_dst(pos) + elseif fields.assign_dst then + init_signal_assignment(pname, pos) + end +end) diff --git a/advtrains_interlocking/init.lua b/advtrains_interlocking/init.lua index d0b75a8..908d998 100644 --- a/advtrains_interlocking/init.lua +++ b/advtrains_interlocking/init.lua @@ -15,6 +15,9 @@ local modpath = minetest.get_modpath(minetest.get_current_modname()) .. DIR_DELI advtrains.interlocking.aspects = dofile(modpath.."signal_aspects.lua") dofile(modpath.."database.lua") +dofile(modpath.."distant.lua") +dofile(modpath.."distant_ui.lua") +dofile(modpath.."signal_aspect_accessors.lua") dofile(modpath.."signal_api.lua") dofile(modpath.."signal_aspect_ui.lua") dofile(modpath.."demosignals.lua") diff --git a/advtrains_interlocking/routesetting.lua b/advtrains_interlocking/routesetting.lua index 67efaea..f1b4455 100644 --- a/advtrains_interlocking/routesetting.lua +++ b/advtrains_interlocking/routesetting.lua @@ -45,6 +45,7 @@ function ilrs.set_route(signal, route, try) local rtename = route.name local signalname = ildb.get_tcbs(signal).signal_name local c_tcbs, c_ts_id, c_ts, c_rseg, c_lckp + local signals = {} while c_sigd and i<=#route do c_tcbs = ildb.get_tcbs(c_sigd) if not c_tcbs then @@ -115,6 +116,7 @@ function ilrs.set_route(signal, route, try) c_tcbs.aspect = route.aspect or advtrains.interlocking.GENERIC_FREE c_tcbs.route_origin = signal advtrains.interlocking.update_signal_aspect(c_tcbs) + signals[#signals+1] = c_tcbs.signal end end -- advance @@ -122,6 +124,25 @@ function ilrs.set_route(signal, route, try) c_sigd = c_rseg.next i = i + 1 end + + -- Distant signaling + local lastsig = nil + if c_sigd then + local e_tcbs = ildb.get_tcbs(c_sigd) + local pos = e_tcbs and e_tcbs.signal + if pos then + lastsig = pos + end + end + for i = #signals, 1, -1 do + if lastsig then + local pos = signals[i] + local _, assigned_by = advtrains.distant.get_main(pos) + if assigned_by ~= "manual" then + advtrains.distant.assign(lastsig, signals[i], "routesetting") + end + end + end return true end diff --git a/advtrains_interlocking/signal_api.lua b/advtrains_interlocking/signal_api.lua index 5b3baf8..1fd4e34 100644 --- a/advtrains_interlocking/signal_api.lua +++ b/advtrains_interlocking/signal_api.lua @@ -167,7 +167,6 @@ This function will query get_aspect to retrieve the new aspect. local DANGER = { main = 0, - dst = false, shunt = false, } advtrains.interlocking.DANGER = DANGER @@ -178,8 +177,6 @@ advtrains.interlocking.GENERIC_FREE = { dst = false, } -local supposed_aspects = {} - local function convert_aspect_if_necessary(asp) if type(asp.main) == "table" then local newasp = {} @@ -200,24 +197,7 @@ local function convert_aspect_if_necessary(asp) end return asp end - -function advtrains.interlocking.load_supposed_aspects(tbl) - if tbl then - supposed_aspects = tbl - end -end - -function advtrains.interlocking.save_supposed_aspects() - return supposed_aspects -end - -local function set_supposed_aspect(pos, asp) - supposed_aspects[advtrains.roundfloorpts(pos)] = asp -end - -local function get_supposed_aspect(pos) - return supposed_aspects[advtrains.roundfloorpts(pos)] -end +advtrains.interlocking.signal_convert_aspect_if_necessary = convert_aspect_if_necessary function advtrains.interlocking.update_signal_aspect(tcbs) if tcbs.signal then @@ -233,27 +213,8 @@ end function advtrains.interlocking.signal_after_dig(pos) -- clear influence point advtrains.interlocking.db.clear_ip_by_signalpos(pos) - set_supposed_aspect(pos, nil) -end - -function advtrains.interlocking.signal_set_aspect(pos, asp) - asp = convert_aspect_if_necessary(asp) - local node=advtrains.ndb.get_node(pos) - local ndef=minetest.registered_nodes[node.name] - if ndef and ndef.advtrains and ndef.advtrains.set_aspect then - local oldasp = advtrains.interlocking.signal_get_aspect(pos) or DANGER - local suppasp = advtrains.interlocking.signal_get_supported_aspects(pos) or {} - local newasp = asp - if suppasp.type == 2 then - asp = advtrains.interlocking.aspects.type1_to_type2main(asp, suppasp.group) - end - set_supposed_aspect(pos, newasp) - ndef.advtrains.set_aspect(pos, node, asp) - local aspect_changed = advtrains.interlocking.aspects.not_equalp(oldasp, newasp) - if aspect_changed then - advtrains.interlocking.signal_on_aspect_changed(pos) - end - end + advtrains.interlocking.signal_clear_aspect(pos) + advtrains.distant.unassign_all(pos, true) end -- should be called when aspect has changed on this signal. @@ -312,46 +273,6 @@ function advtrains.interlocking.signal_get_supposed_aspect(pos) return DANGER; end --- Returns the actual aspect of the signal at position, as returned by the nodedef. --- returns nil when there's no signal at the position -function advtrains.interlocking.signal_get_real_aspect(pos) - local node=advtrains.ndb.get_node(pos) - local ndef=minetest.registered_nodes[node.name] - if ndef and ndef.advtrains and ndef.advtrains.get_aspect then - local asp = ndef.advtrains.get_aspect(pos, node) - local suppasp = advtrains.interlocking.signal_get_supported_aspects(pos) or {} - if suppasp.type == 2 then - asp = advtrains.interlocking.aspects.type2main_to_type1(suppasp.group, asp) - end - if not asp then asp = DANGER end - asp = convert_aspect_if_necessary(asp) - return asp - end - return nil -end - --- Returns the signal aspect as reported in the suppasp table. -function advtrains.interlocking.signal_get_aspect(pos) - local asp = get_supposed_aspect(pos) - if not asp then - asp = advtrains.interlocking.signal_get_real_aspect(pos) - set_supposed_aspect(pos, asp) - end - return asp -end - --- Returns the "supported_aspects" of the signal at position, as returned by the nodedef. --- returns nil when there's no signal at the position -function advtrains.interlocking.signal_get_supported_aspects(pos) - local node=advtrains.ndb.get_node(pos) - local ndef=minetest.registered_nodes[node.name] - if ndef and ndef.advtrains and ndef.advtrains.supported_aspects then - local asp = ndef.advtrains.supported_aspects - return asp - end - return nil -end - local players_assign_ip = {} local function ipmarker(ipos, connid) diff --git a/advtrains_interlocking/signal_aspect_accessors.lua b/advtrains_interlocking/signal_aspect_accessors.lua new file mode 100644 index 0000000..02a03ea --- /dev/null +++ b/advtrains_interlocking/signal_aspect_accessors.lua @@ -0,0 +1,127 @@ +local A = advtrains.interlocking.aspects +local D = advtrains.distant +local I = advtrains.interlocking +local N = advtrains.ndb +local pts = advtrains.roundfloorpts + +local get_aspect + +local supposed_aspects = {} + +function I.load_supposed_aspects(tbl) + if tbl then + supposed_aspects = tbl + end +end + +function I.save_supposed_aspects() + return supposed_aspects +end + +local function get_supposed_aspect(pos) + return supposed_aspects[pts(pos)] +end + +local function set_supposed_aspect(pos, asp) + supposed_aspects[pts(pos)] = asp +end + +local function get_ndef(pos) + local node = N.get_node(pos) + return minetest.registered_nodes[node.name] or {} +end + +local function get_supported_aspects(pos) + local ndef = get_ndef(pos) + if ndef.advtrains and ndef.advtrains.supported_aspects then + return ndef.advtrains.supported_aspects + end + return nil +end + +local function adjust_aspect(pos, asp) + asp = table.copy(I.signal_convert_aspect_if_necessary(asp)) + + local mainpos = D.get_main(pos) + local nxtasp + if asp.main ~= 0 and mainpos then + nxtasp = get_aspect(mainpos) + asp.dst = nxtasp.main + else + asp.dst = nil + end + + local suppasp = get_supported_aspects(pos) + if not suppasp then + return asp, asp + end + local stype = suppasp.type + if stype == 2 then + local group = suppasp.group + local name + if asp.main ~= 0 and nxtasp and nxtasp.type2group == group and nxtasp.type2name then + name = A.get_type2_dst(group, nxtasp.type2name) + else + name = A.type1_to_type2main(asp, group) + end + asp.type2group = group + asp.type2name = name + return asp, name + end + asp.type2name = nil + asp.type2group = nil + return asp, asp +end + +local function get_real_aspect(pos) + local ndef = get_ndef(pos) + if ndef.advtrains and ndef.advtrains.get_aspect then + local asp = ndef.advtrains.get_aspect(pos, node) or I.DANGER + local suppasp = get_supported_aspects(pos) + if suppasp.type == 2 then + asp = A.type2main_to_type1(suppasp.group, asp) + end + return adjust_aspect(pos, asp) + end + return nil +end + +get_aspect = function(pos) + local asp = get_supposed_aspect(pos) + if not asp then + asp = get_real_aspect(pos) + set_supposed_aspect(pos, asp) + end + return asp +end + +local function set_aspect(pos, asp) + local node = N.get_node(pos) + local ndef = minetest.registered_nodes[node.name] + if ndef and ndef.advtrains and ndef.advtrains.set_aspect then + local oldasp = I.signal_get_aspect(pos) or DANGER + local newasp, aspval = adjust_aspect(pos, asp) + set_supposed_aspect(pos, newasp) + ndef.advtrains.set_aspect(pos, node, aspval) + local aspect_changed = A.not_equalp(oldasp, newasp) + if aspect_changed then + I.signal_on_aspect_changed(pos) + D.update_main(pos) + end + end +end + +local function clear_aspect(pos) + set_supposed_aspect(pos, nil) +end + +local function readjust_aspect(pos) + set_aspect(pos, get_aspect(pos)) +end + +I.signal_get_supported_aspects = get_supported_aspects +I.signal_get_real_aspect = get_real_aspect +I.signal_get_aspect = get_aspect +I.signal_set_aspect = set_aspect +I.signal_clear_aspect = clear_aspect +I.signal_readjust_aspect = readjust_aspect diff --git a/advtrains_interlocking/signal_aspect_ui.lua b/advtrains_interlocking/signal_aspect_ui.lua index 4b41187..30b5165 100644 --- a/advtrains_interlocking/signal_aspect_ui.lua +++ b/advtrains_interlocking/signal_aspect_ui.lua @@ -43,7 +43,7 @@ local function describe_supported_aspects_t1(suppasp, isasp) local entries = {} local selid = 1 for idx, spv in ipairs(suppasp.main) do - if isasp and spv == isasp.main then + if isasp and spv == (isasp.main or false) then selid = idx end entries[idx] = describe_t1_main_aspect(spv) @@ -67,7 +67,7 @@ local function describe_supported_aspects_t1(suppasp, isasp) entries = {} selid = 1 for idx, spv in ipairs(suppasp.dst) do - if isasp and spv == isasp.dst then + if isasp and spv == (isasp.dst or false) then selid = idx end entries[idx] = describe_t1_distant_aspect(spv) @@ -102,6 +102,8 @@ local function handle_signal_formspec_tabheader_fields(pname, fields) advtrains.interlocking.show_signal_form(pos, node, pname) elseif n == 2 then advtrains.interlocking.show_ip_form(pos, pname) + elseif n == 3 then + advtrains.interlocking.show_distant_signal_form(pos, pname) end return true end diff --git a/advtrains_interlocking/signal_aspects.lua b/advtrains_interlocking/signal_aspects.lua index eebb4ba..2866ae1 100644 --- a/advtrains_interlocking/signal_aspects.lua +++ b/advtrains_interlocking/signal_aspects.lua @@ -44,6 +44,27 @@ local function get_type2_definition(name) return type2defs[name] end +local function get_type2_danger(group) + local def = type2defs[group] + if not def then + return nil + end + local main = def.main + return main[#main] +end + +local function get_type2_dst(group, name) + local def = type2defs[group] + if not def then + return nil + end + local aspidx = name + if type(name) ~= "number" then + aspidx = def.main[name] or 1 + end + return def.main[math.max(1, aspidx-1)].name +end + local function type2main_to_type1(name, asp) local def = type2defs[name] if not def then @@ -53,7 +74,7 @@ local function type2main_to_type1(name, asp) if type(asp) == "number" then aspidx = asp else - aspidx = def.main[asp] + aspidx = def.main[asp] or 2 end local asptbl = def.main[aspidx] if not asptbl then @@ -62,11 +83,13 @@ local function type2main_to_type1(name, asp) if type(asp) == "number" then asp = asptbl.name end + local dst = def.main[math.min(#def.main, aspidx+1)].main local t = { main = asptbl.main, type2name = asp, type2group = name, + dst = dst, } if aspidx > 1 and aspidx < #asptbl then t.dst = asptbl[aspidx+1].main @@ -116,6 +139,7 @@ end return { register_type2 = register_type2, get_type2_definition = get_type2_definition, + get_type2_dst = get_type2_dst, type2main_to_type1 = type2main_to_type1, type1_to_type2main = type1_to_type2main, equalp = equalp, diff --git a/advtrains_interlocking/signal_main_ui.lua b/advtrains_interlocking/signal_main_ui.lua deleted file mode 100644 index e69de29..0000000 diff --git a/advtrains_interlocking/spec/basic_signalling_spec.lua b/advtrains_interlocking/spec/basic_signalling_spec.lua new file mode 100644 index 0000000..0b79972 --- /dev/null +++ b/advtrains_interlocking/spec/basic_signalling_spec.lua @@ -0,0 +1,87 @@ +--[[ +This file tests a large part of the signaling system, as a lot of tests for the +signaling system tend to overlap for various parts of the system. +]] + +require("mineunit") +mineunit("core") + +_G.advtrains = { + interlocking = { + aspects = fixture("../../signal_aspects"), + }, + ndb = { + get_node = minetest.get_node, + swap_node = minetest.swap_node, + } +} + +fixture("advtrains_helpers") +fixture("../../database") +sourcefile("distant") +sourcefile("signal_api") +sourcefile("signal_aspect_accessors") +fixture("../../demosignals") + +local D = advtrains.distant +local I = advtrains.interlocking + +local stub_aspect_t1 = { + free = {main = -1}, + slow = {main = 6}, + danger = {main = 0, shunt = false}, +} +local stub_pos_t1 = {} +for i = 1, 3 do + stub_pos_t1[i] = {x = 1, y = 0, z = i} +end + +world.layout { + {stub_pos_t1[1], "advtrains_interlocking:ds_danger"}, + {stub_pos_t1[2], "advtrains_interlocking:ds_slow"}, + {stub_pos_t1[3], "advtrains_interlocking:ds_free"}, +} + +describe("API for supposed signal aspects", function() + it("should load and save data properly", function() + local tbl = {_foo = true} + I.load_supposed_aspects(tbl) + assert.same(tbl, I.save_supposed_aspects()) + end) + it("should set and get type 1 signals properly", function () + local pos = stub_pos_t1[2] + local asp = stub_aspect_t1.slow + local newasp = { main = math.random(1,5) } + assert.same(asp, I.signal_get_aspect(pos)) + I.signal_set_aspect(pos, newasp) + assert.same(newasp, I.signal_get_aspect(pos)) + assert.same(asp, I.signal_get_real_aspect(pos)) + I.signal_set_aspect(pos, asp) + end) +end) + +describe("Distant signaling", function() + it("should assign distant signals and set the distant aspect correspondingly", function() + for i = 1, 2 do + D.assign(stub_pos_t1[i], stub_pos_t1[i+1]) + end + assert.same(stub_aspect_t1.danger, I.signal_get_aspect(stub_pos_t1[1])) + assert.same({main = 6, dst = 0}, I.signal_get_aspect(stub_pos_t1[2])) + assert.same({main = -1, dst = 6}, I.signal_get_aspect(stub_pos_t1[3])) + end) + it("should report assignments properly", function() + assert.same({stub_pos_t1[1], "manual"}, {D.get_main(stub_pos_t1[2])}) + assert.same({[advtrains.encode_pos(stub_pos_t1[3])] = "manual"}, D.get_dst(stub_pos_t1[2])) + end) + it("should update distant aspects automatically", function() + I.signal_set_aspect(stub_pos_t1[2], {main = 2, dst = -1}) + assert.same({main = 2, dst = 0}, I.signal_get_aspect(stub_pos_t1[2])) + assert.same({main = -1, dst = 2}, I.signal_get_aspect(stub_pos_t1[3])) + end) + it("should unassign signals when one is removed", function() + world.set_node(stub_pos_t1[2], "air") + assert.same({}, D.get_dst(stub_pos_t1[1])) + assert.same({}, {D.get_main(stub_pos_t1[3])}) + assert.same(stub_aspect_t1.free, I.signal_get_aspect(stub_pos_t1[3])) + end) +end) diff --git a/advtrains_interlocking/spec/signal_api_spec.lua b/advtrains_interlocking/spec/signal_api_spec.lua deleted file mode 100644 index cd7a1d1..0000000 --- a/advtrains_interlocking/spec/signal_api_spec.lua +++ /dev/null @@ -1,49 +0,0 @@ -require("mineunit") - -mineunit("core") - -_G.advtrains = { - interlocking = { - aspects = fixture("../../signal_aspects"), - }, - ndb = { - get_node = minetest.get_node, - } -} - -fixture("advtrains_helpers") -fixture("../../database") -sourcefile("signal_api") - -local stub_aspect_t1 = { main = math.random() } -local stub_pos_t1 = {x = 1, y = 0, z = 1} - -minetest.register_node(":stubsignal_t1", { - advtrains = { - supported_aspects = {}, - get_aspect = function () return stub_aspect_t1 end, - set_aspect = function () end, - }, - groups = { advtrains_signal = 2 }, -}) - -world.layout { - {stub_pos_t1, "stubsignal_t1"}, -} - -describe("API for supposed signal aspects", function() - it("should load and save data properly", function() - local tbl = {_foo = true} - advtrains.interlocking.load_supposed_aspects(tbl) - assert.same(tbl, advtrains.interlocking.save_supposed_aspects()) - end) - it("should set and get type 1 signals properly", function () - local pos = stub_pos_t1 - local asp = stub_aspect_t1 - local newasp = { dst = math.random() } - assert.same(asp, advtrains.interlocking.signal_get_aspect(pos)) - advtrains.interlocking.signal_set_aspect(pos, newasp) - assert.same(newasp, advtrains.interlocking.signal_get_aspect(pos)) - assert.same(asp, advtrains.interlocking.signal_get_real_aspect(pos)) - end) -end) diff --git a/advtrains_interlocking/tcb_ts_ui.lua b/advtrains_interlocking/tcb_ts_ui.lua index b3b8221..9f88296 100755 --- a/advtrains_interlocking/tcb_ts_ui.lua +++ b/advtrains_interlocking/tcb_ts_ui.lua @@ -14,6 +14,7 @@ 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", @@ -608,7 +609,7 @@ function advtrains.interlocking.show_signalling_form(sigd, pname, sel_rte, calle if not tcbs.signal_name then tcbs.signal_name = "Signal at "..minetest.pos_to_string(sigd.p) end if not tcbs.routes then tcbs.routes = {} end - local form = "size[7,9.75]label[0.5,0.5;Signal at "..minetest.pos_to_string(sigd.p).."]" + local form = "size[7,10.25]label[0.5,0.5;Signal at "..minetest.pos_to_string(sigd.p).."]" form = form .. advtrains.interlocking.make_signal_formspec_tabheader(pname, tcbs.signal, 7, 1) form = form.."field[0.8,1.5;5.2,1;name;Signal name;"..minetest.formspec_escape(tcbs.signal_name).."]" form = form.."button[5.5,1.2;1,1;setname;Set]" @@ -670,6 +671,7 @@ function advtrains.interlocking.show_signalling_form(sigd, pname, sel_rte, calle form = form.."button[0.5,8;2.5,1;newroute;New Route]" form = form.."button[ 3,8;2.5,1;unassign;Unassign Signal]" form = form..string.format("checkbox[0.5,8.75;ars;Automatic routesetting;%s]", not tcbs.ars_disabled) + form = form..string.format("checkbox[0.5,9.25;dst;Distant signalling;%s]", not tcbs.nodst) end elseif sigd_equal(tcbs.route_origin, sigd) then -- something has gone wrong: tcbs.routeset should have been set... @@ -796,6 +798,10 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) if fields.ars then tcbs.ars_disabled = not minetest.is_yes(fields.ars) end + + if fields.dst then + tcbs.nodst = not minetest.is_yes(fields.dst) + end if fields.auto then tcbs.route_auto = true -- cgit v1.2.3 From 7c9fd9179dfcec06107ad1a66346418827bdc4eb Mon Sep 17 00:00:00 2001 From: "Y. Wang" Date: Mon, 31 Oct 2022 10:57:37 +0100 Subject: Add API documentation --- advtrains_interlocking/README.md | 96 ++++++++++++ advtrains_interlocking/distant.lua | 47 ++++++ advtrains_interlocking/init.lua | 4 +- advtrains_interlocking/signal_api.lua | 165 --------------------- advtrains_interlocking/signal_aspect_accessors.lua | 52 +++++++ advtrains_interlocking/signal_aspects.lua | 49 ++++-- 6 files changed, 237 insertions(+), 176 deletions(-) create mode 100644 advtrains_interlocking/README.md (limited to 'advtrains_interlocking/init.lua') diff --git a/advtrains_interlocking/README.md b/advtrains_interlocking/README.md new file mode 100644 index 0000000..636ad67 --- /dev/null +++ b/advtrains_interlocking/README.md @@ -0,0 +1,96 @@ +# Interlocking for Advtrains + +The `advtrains_interlocking` mod provides various interlocking and signaling features for Advtrains. + +## Signal types +There are two types of signals in Advtrains: + +* Type 1 (speed signals): These signals only give speed information. +* Type 2 (route signals): These signals mainly provide route information, but sometimes also provide speed information. + +## Signal aspect tables + +Signal aspects are represented using tables with the following (optional) fields: + +* `main`: The main signal aspect. It provides information on the permitted speed after passing the signal. +* `dst`: The distant signal aspect. It provides information on the permitted speed after passing the next signal. +* `shunt`: Whether the train may proceed in shunt mode and, if the main aspect is danger, proceed in shunt mode. +* `proceed_as_main`: Whether the train should exit shunt mode when proceeding. +* `type2group`: The type 2 group of the signal. +* `type2name`: The type 2 signal aspect name. + +The `main` and `dst` fields may be: + +* An positive number indicating the permitted speed, +* The number 0, indicating that the train should expect to stop at the current signal (or, for the `dst` field, the next signal), +* The number -1, indicating that the train can proceed (or, for the `dst` field, expect to proceed) at maximum speed, or +* The constant `false` or `nil`, indicating no change to the speed restriction. + +### Node definitions + +Signals should belong the following node groups: + +* `advtrains_signal`: `1` for static signals, `2` for signals with variable aspects. +* `save_in_at_nodedb`: This should be set to `1` to make sure that Advtrains always has access to the signal. +* `not_blocking_trains`: Setting this to `1` prevents trains from colliding with the signal. Setting this is not necessary, but recommended. + +The node definition should contain an `advtrains` field. + +The `advtrains` field of the node definition should contain a `supported_aspects` table for signals with variable aspects. + +For type 1 signals, the `supported_aspects` table should contain the following fields: + +* `main`: A list of values supported for the main aspect. +* `dst`: A list of values supported for the distant aspect. +* `shunt`: The value for the `shunt` field of the signal aspect or `nil` if the value is variable. +* `proceed_as_main`: The value for the `proceed_as_main` field of the signal aspect. + +For type 2 signals, the `supported_aspects` table should contain the following fields: + +* `type`: The numeric constant `2`. +* `group`: The type 2 signal group. +* `dst_shift`: The phase shift for distant/repeater signals. This field should not be set for main signals. + +The `advtrains` field of the node definition should contain a `get_aspect` function. This function is given the position of the signal and the node at the position. It should return the signal aspect table or, in the case of type 2 signals, the name of the signal aspect. + +For signals with variable aspects, a corresponding `set_aspect` function should also be defined. This function is given the position of the signal, the node at the position, and the new aspect (or, in the case of type 2 signals, the name of the new signal aspect). For type 1 signals, the new aspect is not guranteed to be supported by the signal itself. + +Signals should also have the following callbacks set: + +* `on_rightclick` should be set to `advtrains.interlocking.signal_rc_handler` +* `can_dig` should be set to `advtrains.interlocking.signal_can_dig` +* `after_dig_node` should be set to `advtrains.interlocking.signal_after_dig` + +Alternatively, custom callbacks should call the respective functions. + +## Type 2 signal groups + +Type 2 signals belong to signal gruops, which are registered using `advtrains.interlocking.aspects.register_type2`. + +Signal group definitions include the following fields: + +* `name`: The internal name of the signal group. It is recommended to use the mod name as a prefix to avoid name collisions. +* `label`: The description of the signal group. +* `main`: A list of signal aspects, from the least restrictive (i.e. proceed) to the most restrictive (i.e. danger). + +Each aspect in the signal group definition table should contain the following fields: + +* `name`: The internal name of the signal aspect. +* `label`: The description of the signal aspect. +* `main`, `shunt`, `proceed_as_main`: The fields corresponding to the ones in signal aspect tables. + +Type 2 signal aspects are then referred to with the aspect names within the group. + +## Notes + +It is allowed to provide other methods of setting the signal aspect. However: + +* These changes are ignored by the routesetting system. +* Please call `advtrains.interlocking.signal_readjust_aspect` after the signal aspect has changed. + +## Examples +An example of type 1 signals can be found in `advtrains_signals_ks`, which provides a subset of German signals. + +An example of type 2 signals can be found in `advtrains_signals_japan`, which provides a subset of Japanese signals. + +The mods mentioned above are also used for demonstation purposes and can also be used for testing. diff --git a/advtrains_interlocking/distant.lua b/advtrains_interlocking/distant.lua index f62ca36..22f1c9d 100644 --- a/advtrains_interlocking/distant.lua +++ b/advtrains_interlocking/distant.lua @@ -1,3 +1,8 @@ +--- Distant signaling. +-- This module implements a database backend for distant signal assignments. +-- The actual modifications to signal aspects are still done by signal aspect accessors. +-- @module advtrains.interlocking.distant + local db_distant = {} local db_distant_of = {} @@ -5,6 +10,9 @@ local A = advtrains.interlocking.aspects local pts = advtrains.encode_pos local stp = advtrains.decode_pos +--- Replace the distant signal assignment database. +-- @function load +-- @param db The new database to load. local function db_load(x) if type(x) ~= "table" then return @@ -13,6 +21,9 @@ local function db_load(x) db_distant_of = x.distant_of end +--- Retrieve the current distant signal assignment database. +-- @function save +-- @return The current database. local function db_save() return { distant = db_distant, @@ -22,6 +33,10 @@ end local update_signal, update_main, update_dst +--- Unassign a distant signal. +-- @function unassign_dst +-- @param dst The position of the distant signal. +-- @param[opt=false] force Whether to skip callbacks. local function unassign_dst(dst, force) local pts_dst = pts(dst) local main = db_distant_of[pts_dst] @@ -38,6 +53,10 @@ local function unassign_dst(dst, force) end end +--- Unassign a main signal. +-- @function unassign_main +-- @param main The position of the main signal. +-- @param[opt=false] force Whether to skip callbacks. local function unassign_main(main, force) local pts_main = pts(main) local t = db_distant[pts_main] @@ -57,11 +76,21 @@ local function unassign_main(main, force) db_distant[pts_main] = nil end +--- Remove all (main and distant) signal assignments from a signal. +-- @function unassign_all +-- @param pos The position of the signal. +-- @param[opt=false] force Whether to skip callbacks. local function unassign_all(pos, force) unassign_main(pos) unassign_dst(pos, force) end +--- Assign a distant signal to a main signal. +-- @function assign +-- @param main The position of the main signal. +-- @param dst The position of the distant signal. +-- @param[opt="manual"] by The method of assignment. +-- @param[opt=false] skip_update Whether to skip callbacks. local function assign(main, dst, by, skip_update) local pts_main = pts(main) local pts_dst = pts(dst) @@ -87,11 +116,20 @@ local function pre_occupy(dst, by) db_distant_of[pts_dst] = {nil, by} end +--- Get the distant signals assigned to a main signal. +-- @function get_distant +-- @param main The position of the main signal. +-- @treturn {[pos]=by,...} A table of distant signals, with the positions encoded using `advtrains.encode_pos`. local function get_distant(main) local pts_main = pts(main) return db_distant[pts_main] or {} end +--- Get the main signal assigned the a distant signal. +-- @function get_main +-- @param dst The position of the distant signal. +-- @return The position of the main signal. +-- @return The method of assignment. local function get_main(dst) local pts_dst = pts(dst) local main = db_distant_of[pts_dst] @@ -105,6 +143,9 @@ local function get_main(dst) end end +--- Update all distant signals assigned to a main signal. +-- @function update_main +-- @param main The position of the main signal. update_main = function(main) local pts_main = pts(main) local t = get_distant(main) @@ -114,10 +155,16 @@ update_main = function(main) end end +--- Update the aspect of a distant signal. +-- @function update_dst +-- @param dst The position of the distant signal. update_dst = function(dst) advtrains.interlocking.signal_readjust_aspect(dst) end +--- Update the aspect of a combined (main and distant) signal and all distant signals assigned to it. +-- @function update_signal +-- @param pos The position of the signal. update_signal = function(pos) update_main(pos) update_dst(pos) diff --git a/advtrains_interlocking/init.lua b/advtrains_interlocking/init.lua index 908d998..1a8ef07 100644 --- a/advtrains_interlocking/init.lua +++ b/advtrains_interlocking/init.lua @@ -1,5 +1,5 @@ --- Advtrains interlocking system --- See database.lua for a detailed explanation +--- Advtrains interlocking system. +-- @module advtrains.interlocking advtrains.interlocking = {} diff --git a/advtrains_interlocking/signal_api.lua b/advtrains_interlocking/signal_api.lua index e615692..54202f0 100644 --- a/advtrains_interlocking/signal_api.lua +++ b/advtrains_interlocking/signal_api.lua @@ -1,170 +1,5 @@ -- Signal API implementation - ---[[ -Signal aspect table: -Note: All speeds are measured in m/s, aka the number of + signs in the HUD. -asp = { - main = , - -- Main signal aspect, tells state and permitted speed of next section - -- 0 = section is blocked - -- >0 = section is free, speed limit is this value - -- -1 = section is free, maximum speed permitted - -- false/nil = Signal doesn't provide main signal information, retain current speed limit. - shunt = , - -- Whether train may proceed as shunt move, on sight - -- main aspect takes precedence over this - -- When main==0, train switches to shunt move and is restricted to speed 6 - proceed_as_main = , - -- If an approaching train is a shunt move and 'shunt' is false, - -- the train may proceed as a train move under the "main" aspect - -- if the main aspect permits it (i.e. main!=0) - -- If this is not set, shunt moves are NOT allowed to switch to - -- a train move, and must stop even if "main" would permit passing. - -- This is intended to be used for "Halt for shunt moves" signs. - - dst = , - -- Distant signal aspect, tells state and permitted speed of the section after next section - -- The character of these information is purely informational - -- At this time, this field is not actively used - -- 0 = section is blocked - -- >0 = section is free, speed limit is this value - -- -1 = section is free, maximum speed permitted - -- false/nil = Signal doesn't provide distant signal information. - - -- the character of call_on and dead_end is purely informative - call_on = , -- Call-on route, expect train in track ahead (not implemented yet) - dead_end = , -- Route ends on a dead end (e.g. bumper) (not implemented yet) - - w_speed = , - -- "Warning speed restriction". Supposed for short-term speed - -- restrictions which always override any other restrictions - -- imposed by "speed" fields, until lifted by a value of -1 - -- (Example: german Langsamfahrstellen-Signale) - } -} - -== How signals actually work in here == -Each signal (in the advtrains universe) is some node that has at least the -following things: -- An "influence point" that is set somewhere on a rail -- An aspect which trains that pass the "influence point" have to obey - -There can be static and dynamic signals. Static signals are, roughly -spoken, signs, while dynamic signals are "real" signals which can display -different things. - -The node definition of a signal node should contain those fields: -groups = { - advtrains_signal = 2, - save_in_at_nodedb = 1, -} -advtrains = { - set_aspect = function(pos, node, asp) - -- This function gets called whenever the signal should display - -- a new or changed signal aspect. It is not required that - -- the signal actually displays the exact same aspect, since - -- some signals can not do this by design. However, it must - -- display an aspect that is at least as restrictive as the passed - -- aspect as far as it is capable of doing so. - -- Examples: - -- - pure shunt signals can not display a "main" aspect - -- and have no effect on train moves, so they will only ever - -- honor the shunt.free field for their aspect. - -- - the german Hl system can only signal speeds of 40, 60 - -- and 100 km/h, a speed of 80km/h should then be signalled - -- as 60 km/h instead. - -- In turn, it is not guaranteed that the aspect will fulfill the - -- criteria put down in supported_aspects. - -- If set_aspect is present, supported_aspects should also be declared. - - -- The aspect passed in here can always be queried using the - -- advtrains.interlocking.signal_get_supposed_aspect(pos) function. - -- It is always DANGER when the signal is not used as route signal. - - -- For static signals, this function should be completely omitted - -- If this function is omitted, it won't be possible to use - -- route setting on this signal. - end, - supported_aspects = { - -- A table which tells which different types of aspects this signal - -- is able to display. It is used to construct the "aspect editing" - -- formspec for route programming (and others) It should always be - -- present alongside with set_aspect. If this is not specified but - -- set_aspect is, the user will be allowed to select any aspect. - -- Any of the fields marked with support 3 types of values: - nil: if this signal can switch between free/blocked - false: always shows "blocked", unchangable - true: always shows "free", unchangable - -- Any of the "speed" fields should contain a list of possible values - -- to be set as restriction. If omitted, the value of the described - -- field is always assumed to be false (no information) - -- A speed of 0 means that the signal can show a "blocked" aspect - -- (which is probably the case for most signals) - -- If the signal can signal "no information" on one of the fields - -- (thus false is an acceptable value), include false in the list - -- If your signal can only display a single speed (may it be -1), - -- always enclose that single value into a list. (such as {-1}) - main = {, ..., } or nil, - dst = {, ..., } or nil, - shunt = , - - call_on = , - dead_end = , - w_speed = {, ..., } or nil, - - }, - Example for supported_aspects: - supported_aspects = { - main = {0, 6, -1}, -- can show either "Section blocked", "Proceed at speed 6" or "Proceed at maximum speed" - dst = {0, false}, -- can show only if next signal shows "blocked", no other information. - shunt = false, -- shunting by this signal is never allowed. - - call_on = false, - dead_end = false, - w_speed = nil, - -- none of the information can be shown by the signal - - }, - - get_aspect = function(pos, node) - -- This function gets called by the train safety system. It - should return the aspect that this signal actually displays, - not preferably the input of set_aspect. - -- For regular, full-featured light signals, they will probably - honor all entries in the original aspect, however, e.g. - simple shunt signals always return main=false regardless of - the set_aspect input because they can not signal "Halt" to - train moves. - -- advtrains.interlocking.DANGER contains a default "all-danger" aspect. - -- If your signal does not cover certain sub-tables of the aspect, - the following reasonable defaults are automatically assumed: - main = false (unchanged) - dst = false (unchanged) - shunt = false (shunting not allowed) - info = {} (no further information) - end, -} -on_rightclick = advtrains.interlocking.signal_rc_handler -can_dig = advtrains.interlocking.signal_can_dig -after_dig_node = advtrains.interlocking.signal_after_dig - -(If you need to specify custom can_dig or after_dig_node callbacks, -please call those functions anyway!) - -Important note: If your signal should support external ways to set its -aspect (e.g. via mesecons), there are some things that need to be considered: -- advtrains.interlocking.signal_get_supposed_aspect(pos) won't respect this -- Whenever you change the signal aspect, and that aspect change -did not happen through a call to -advtrains.interlocking.signal_set_aspect(pos, asp), you are -*required* to call this function: -advtrains.interlocking.signal_on_aspect_changed(pos) -in order to notify trains about the aspect change. -This function will query get_aspect to retrieve the new aspect. - -]]-- - local DANGER = { main = 0, shunt = false, diff --git a/advtrains_interlocking/signal_aspect_accessors.lua b/advtrains_interlocking/signal_aspect_accessors.lua index 060f923..e55814e 100644 --- a/advtrains_interlocking/signal_aspect_accessors.lua +++ b/advtrains_interlocking/signal_aspect_accessors.lua @@ -1,3 +1,6 @@ +--- Signal aspect accessors +-- @module advtrains.interlocking + local A = advtrains.interlocking.aspects local D = advtrains.distant local I = advtrains.interlocking @@ -29,6 +32,9 @@ local get_aspect local supposed_aspects = {} +--- Replace the signal aspect cache. +-- @function load_supposed_aspects +-- @param db The new database. function I.load_supposed_aspects(tbl) if tbl then supposed_aspects = tbl @@ -38,23 +44,42 @@ function I.load_supposed_aspects(tbl) end end +--- Retrieve the signal aspect cache. +-- @function save_supposed_aspects +-- @return The current database in use. function I.save_supposed_aspects() return supposed_aspects end +--- Read the aspect of a signal strictly from cache. +-- @param pos The position of the signal. +-- @return[1] The aspect of the signal (if present in cache). +-- @return[2] The nil constant (otherwise). local function get_supposed_aspect(pos) return supposed_aspects[pts(pos)] end +--- Update the signal aspect information in cache. +-- @param pos The position of the signal. +-- @param asp The new signal aspect local function set_supposed_aspect(pos, asp) supposed_aspects[pts(pos)] = asp end +--- Get the definition of a node. +-- @param pos The position of the node. +-- @return[1] The definition of the node (if present). +-- @return[2] An empty table (otherwise). local function get_ndef(pos) local node = N.get_node(pos) return minetest.registered_nodes[node.name] or {} end +--- Get the aspects supported by a signal. +-- @function signal_get_supported_aspects +-- @param pos The position of the signal. +-- @return[1] The table of supported aspects (if present). +-- @return[2] The nil constant (otherwise). local function get_supported_aspects(pos) local ndef = get_ndef(pos) if ndef.advtrains and ndef.advtrains.supported_aspects then @@ -63,6 +88,11 @@ local function get_supported_aspects(pos) return nil end +--- Adjust a new signal aspect to fit a signal. +-- @param pos The position of the signal. +-- @param asp The new signal aspect. +-- @return The adjusted signal aspect. +-- @return The information to pass to the `advtrains.set_aspect` field in the node definitions. local function adjust_aspect(pos, asp) asp = table.copy(I.signal_convert_aspect_if_necessary(asp)) setmetatable(asp, signal_aspect_metatable) @@ -103,6 +133,12 @@ local function adjust_aspect(pos, asp) return asp, asp end +--- Get the aspect of a signal without accessing the cache. +-- For most cases, `get_aspect` should be used instead. +-- @function signal_get_real_aspect +-- @param pos The position of the signal. +-- @return[1] The signal aspect adjusted using `adjust_aspect` (if present). +-- @return[2] The nil constant (otherwise). local function get_real_aspect(pos) local ndef = get_ndef(pos) if ndef.advtrains and ndef.advtrains.get_aspect then @@ -116,6 +152,11 @@ local function get_real_aspect(pos) return nil end +--- Get the aspect of a signal. +-- @function signal_get_aspect +-- @param pos The position of the signal. +-- @return[1] The aspect of the signal (if present). +-- @return[2] The nil constant (otherwise). get_aspect = function(pos) local asp = get_supposed_aspect(pos) if not asp then @@ -125,6 +166,11 @@ get_aspect = function(pos) return asp end +--- Set the aspect of a signal. +-- @function signal_set_aspect +-- @param pos The position of the signal. +-- @param asp The new signal aspect. +-- @param[opt=false] skipdst Whether to skip updating distant signals. local function set_aspect(pos, asp, skipdst) local node = N.get_node(pos) local ndef = minetest.registered_nodes[node.name] @@ -141,10 +187,16 @@ local function set_aspect(pos, asp, skipdst) end end +--- Remove a signal from cache. +-- @function signal_clear_aspect +-- @param pos The position of the signal. local function clear_aspect(pos) set_supposed_aspect(pos, nil) end +--- Readjust the aspect of a signal. +-- @function signal_readjust_aspect +-- @param pos The position of the signal. local function readjust_aspect(pos) set_aspect(pos, get_aspect(pos)) end diff --git a/advtrains_interlocking/signal_aspects.lua b/advtrains_interlocking/signal_aspects.lua index c381fd2..37af7aa 100644 --- a/advtrains_interlocking/signal_aspects.lua +++ b/advtrains_interlocking/signal_aspects.lua @@ -1,5 +1,11 @@ +--- Signal aspect handling. +-- @module advtrains.interlocking.aspects + local type2defs = {} +--- Register a type 2 signal group. +-- @function register_type2 +-- @param def The definition table. local function register_type2(def) local t = {type = 2} local name = def.name @@ -42,19 +48,21 @@ local function register_type2(def) type2defs[name] = t end +--- Get the definition of a type 2 signal group. +-- @function get_type2_definition +-- @param name The name of the signal group. +-- @return[1] The definition for the signal group (if present). +-- @return[2] The nil constant (otherwise). local function get_type2_definition(name) return type2defs[name] end -local function get_type2_danger(group) - local def = type2defs[group] - if not def then - return nil - end - local main = def.main - return main[#main] -end - +--- Get the name of the distant aspect before the current aspect. +-- @function get_type2_dst +-- @param group The name of the group. +-- @param name The name of the current aspect. +-- @return[1] The name of the distant aspect (if present). +-- @return[2] The nil constant (otherwise). local function get_type2_dst(group, name) local def = type2defs[group] if not def then @@ -67,6 +75,12 @@ local function get_type2_dst(group, name) return def.main[math.max(1, aspidx-1)].name end +--- Convert a type 2 signal aspect to a type 1 signal aspect. +-- @function type2_to_type1 +-- @param suppasp The table of supported aspects for the signal. +-- @param asp The name of the signal aspect. +-- @return[1] The type 1 signal aspect table (if present). +-- @return[2] The nil constant (otherwise). local function type2_to_type1(suppasp, asp) local name = suppasp.group local shift = suppasp.dst_shift @@ -111,6 +125,13 @@ local function type2_to_type1(suppasp, asp) return t end +--- Convert a type 1 signal aspect table to a type 2 signal aspect. +-- @function type1_to_type2main +-- @param asp The type 1 signal aspect table +-- @param group The signal aspect group +-- @param[opt=0] shift The shift for the signal aspect. +-- @return[1] The name of the signal aspect (if present). +-- @return[2] The nil constant (otherwise). local function type1_to_type2main(asp, group, shift) local def = type2defs[group] if not def then @@ -130,6 +151,11 @@ local function type1_to_type2main(asp, group, shift) return t_main[math.max(1, idx-(shift or 0))].name end +--- Compare two signal aspect tables. +-- @function equalp +-- @param asp1 The first signal aspect table. +-- @param asp2 The second signal aspect table. +-- @return Whether the two signal aspect tables give the same (type 1 aspect) information. local function equalp(asp1, asp2) if asp1 == asp2 then -- same reference return true @@ -146,6 +172,11 @@ local function equalp(asp1, asp2) return true end +--- Compare two signal aspect tables. +-- @function not_equalp +-- @param asp1 The first signal aspect table. +-- @param asp2 The second signal aspect table. +-- @return The negation of `equalp``(asp1, asp2)`. local function not_equalp(asp1, asp2) return not equalp(asp1, asp2) end -- cgit v1.2.3 From e25b1c744dad7daa561ee0b23b006bc616f88f23 Mon Sep 17 00:00:00 2001 From: "Y. Wang" Date: Sun, 26 Mar 2023 11:53:00 +0200 Subject: Cancel type 2 signals; introduce signal groups for all signals --- advtrains_interlocking/README.md | 41 ++- advtrains_interlocking/aspect.lua | 290 +++++++++++++++++++++ advtrains_interlocking/distant.lua | 1 - advtrains_interlocking/init.lua | 2 +- advtrains_interlocking/signal_api.lua | 28 +- advtrains_interlocking/signal_aspect_accessors.lua | 80 ++---- advtrains_interlocking/signal_aspect_ui.lua | 127 ++------- advtrains_interlocking/signal_aspects.lua | 202 -------------- .../spec/basic_signalling_spec.lua | 26 +- advtrains_interlocking/spec/signal_group_spec.lua | 95 +++++++ advtrains_interlocking/spec/type2_spec.lua | 117 --------- advtrains_signals_japan/init.lua | 18 +- 12 files changed, 474 insertions(+), 553 deletions(-) create mode 100644 advtrains_interlocking/aspect.lua delete mode 100644 advtrains_interlocking/signal_aspects.lua create mode 100644 advtrains_interlocking/spec/signal_group_spec.lua delete mode 100644 advtrains_interlocking/spec/type2_spec.lua (limited to 'advtrains_interlocking/init.lua') diff --git a/advtrains_interlocking/README.md b/advtrains_interlocking/README.md index 636ad67..d4a2699 100644 --- a/advtrains_interlocking/README.md +++ b/advtrains_interlocking/README.md @@ -2,12 +2,6 @@ The `advtrains_interlocking` mod provides various interlocking and signaling features for Advtrains. -## Signal types -There are two types of signals in Advtrains: - -* Type 1 (speed signals): These signals only give speed information. -* Type 2 (route signals): These signals mainly provide route information, but sometimes also provide speed information. - ## Signal aspect tables Signal aspects are represented using tables with the following (optional) fields: @@ -16,15 +10,16 @@ Signal aspects are represented using tables with the following (optional) fields * `dst`: The distant signal aspect. It provides information on the permitted speed after passing the next signal. * `shunt`: Whether the train may proceed in shunt mode and, if the main aspect is danger, proceed in shunt mode. * `proceed_as_main`: Whether the train should exit shunt mode when proceeding. -* `type2group`: The type 2 group of the signal. -* `type2name`: The type 2 signal aspect name. +* `group`: The name of the signal group. +* `name`: The name of the signal aspect. The `main` and `dst` fields may be: * An positive number indicating the permitted speed, * The number 0, indicating that the train should expect to stop at the current signal (or, for the `dst` field, the next signal), -* The number -1, indicating that the train can proceed (or, for the `dst` field, expect to proceed) at maximum speed, or -* The constant `false` or `nil`, indicating no change to the speed restriction. +* The number -1, indicating that the train can proceed (or, for the `dst` field, expect to proceed) at maximum speed, +* The constant `false`, indicating no change to the speed restriction, or +* The constant `nil`, indicating that the default value for the name aspect (if present) is used. If no valid signal aspect is named, or the signal aspect does not provide a default value, the value is assumed to be `false`. ### Node definitions @@ -38,22 +33,19 @@ The node definition should contain an `advtrains` field. The `advtrains` field of the node definition should contain a `supported_aspects` table for signals with variable aspects. -For type 1 signals, the `supported_aspects` table should contain the following fields: +The `supported_aspects` table should contain the following fields: * `main`: A list of values supported for the main aspect. * `dst`: A list of values supported for the distant aspect. * `shunt`: The value for the `shunt` field of the signal aspect or `nil` if the value is variable. * `proceed_as_main`: The value for the `proceed_as_main` field of the signal aspect. - -For type 2 signals, the `supported_aspects` table should contain the following fields: - -* `type`: The numeric constant `2`. -* `group`: The type 2 signal group. +* `group`: The name of the signal group. +* `name`: A list of supported (named) aspects. * `dst_shift`: The phase shift for distant/repeater signals. This field should not be set for main signals. The `advtrains` field of the node definition should contain a `get_aspect` function. This function is given the position of the signal and the node at the position. It should return the signal aspect table or, in the case of type 2 signals, the name of the signal aspect. -For signals with variable aspects, a corresponding `set_aspect` function should also be defined. This function is given the position of the signal, the node at the position, and the new aspect (or, in the case of type 2 signals, the name of the new signal aspect). For type 1 signals, the new aspect is not guranteed to be supported by the signal itself. +For signals with variable aspects, a corresponding `set_aspect` function should also be defined. This function is given the position of the signal, the node at the position, and the new aspect. The new aspect is not guaranteed to be supported by the signal itself. Signals should also have the following callbacks set: @@ -63,23 +55,20 @@ Signals should also have the following callbacks set: Alternatively, custom callbacks should call the respective functions. -## Type 2 signal groups +## Signal groups -Type 2 signals belong to signal gruops, which are registered using `advtrains.interlocking.aspects.register_type2`. +Signals may belong to signal groups are registered using `advtrains.interlocking.aspect.register_group`. Signal group definitions include the following fields: * `name`: The internal name of the signal group. It is recommended to use the mod name as a prefix to avoid name collisions. * `label`: The description of the signal group. -* `main`: A list of signal aspects, from the least restrictive (i.e. proceed) to the most restrictive (i.e. danger). +* `aspects`: A table of signal aspects. Entries with string indices define the signal aspect with the name. Entries with numeric indices (starting from 1, counting upward) contain a list of corresponding aspect names (the first entry is preferred) and are mainly used for routing, where larger indices indicate that the signal with the aspect is closer to the signal with the "danger" (or similar) aspect. Each aspect in the signal group definition table should contain the following fields: -* `name`: The internal name of the signal aspect. * `label`: The description of the signal aspect. -* `main`, `shunt`, `proceed_as_main`: The fields corresponding to the ones in signal aspect tables. - -Type 2 signal aspects are then referred to with the aspect names within the group. +* `main`, `shunt`, `proceed_as_main`: The default values for the aspect. Note that the `dst` field has no default value as it is automatically adjusted. ## Notes @@ -89,8 +78,8 @@ It is allowed to provide other methods of setting the signal aspect. However: * Please call `advtrains.interlocking.signal_readjust_aspect` after the signal aspect has changed. ## Examples -An example of type 1 signals can be found in `advtrains_signals_ks`, which provides a subset of German signals. +An example of speed signals can be found in `advtrains_signals_ks`, which provides a subset of German signals. -An example of type 2 signals can be found in `advtrains_signals_japan`, which provides a subset of Japanese signals. +An example of route signals can be found in `advtrains_signals_japan`, which provides a subset of Japanese signals. The mods mentioned above are also used for demonstation purposes and can also be used for testing. diff --git a/advtrains_interlocking/aspect.lua b/advtrains_interlocking/aspect.lua new file mode 100644 index 0000000..1575fb1 --- /dev/null +++ b/advtrains_interlocking/aspect.lua @@ -0,0 +1,290 @@ +--- Signal aspect handling. +-- @module advtrains.interlocking.aspect + +local registered_groups = {} + +local named_aspect_aspfields = {main = true, shunt = true, proceed_as_main = true} + +local signal_aspect = {} + +local signal_aspect_metatable = { + __eq = function(asp1, asp2) + for _, k in pairs {"main", "dst", "shunt", "proceed_as_main"} do + local v1, v2 = (asp1[k] or false), (asp2[k] or false) + if v1 ~= v2 then + return false + end + end + if asp1.group and asp1.group == asp2.group then + return asp1.name == asp2.name + end + return true + end, + __index = function(asp, field) + local method = signal_aspect[field] + if method then + return method + end + if not named_aspect_aspfields[field] then + return nil + end + local group = registered_groups[rawget(asp, "group")] + if not group then + return false + end + local aspdef = group.aspects[rawget(asp, "name")] + if not aspdef then + return false + end + return aspdef[field] or false + end, + __tostring = function(asp) + local st = {} + if asp.group and asp.name then + table.insert(st, ("%q in %q"):format(asp.name, asp.group)) + end + if asp.main then + table.insert(st, ("current %d"):format(asp.main)) + end + if asp.main ~= 0 then + if asp.dst then + table.insert(st, string.format("next %d", asp.dst)) + end + end + if asp.main ~= 0 and asp.proceed_as_main then + table.insert(st, "proceed as main") + end + return ("[%s]"):format(table.concat(st, ", ")) + end, +} + +local function quicknew(t) + return setmetatable(t, signal_aspect_metatable) +end + +--- Signal aspect class. +-- @type signal_aspect + +--- Return a plain version of the signal aspect. +-- @param[opt=false] raw Bypass metamethods when fetching signal aspects +-- @return A plain copy of the signal aspect object. +function signal_aspect:plain(raw) + local t = {} + for _, k in pairs {"main", "dst", "shunt", "proceed_as_main", "group", "name"} do + local v + if raw then + v = rawget(self, k) + else + v = self[k] + end + t[k] = v + end + return t +end + +--- Create (or copy) a signal aspect object. +-- Note that signal aspect objects can also be created by calling the `advtrains.interlocking.aspect` table. +-- @return The newly created signal aspect object. +function signal_aspect:new() + if type(self) ~= "table" then + return quicknew{} + end + local newasp = {} + for _, k in pairs {"main", "dst"} do + if type(self[k]) == "table" then + if self[k].free then + newasp[k] = self[k].speed + else + newasp[k] = 0 + end + else + newasp[k] = self[k] + end + end + if type(self.shunt) == "table" then + newasp.shunt = self.shunt.free + newasp.proceed_as_main = self.shunt.proceed_as_main + else + newasp.shunt = self.shunt + end + for _, k in pairs {"group", "name"} do + newasp[k] = self[k] + end + return quicknew(newasp) +end + +--- Modify the signal aspect in-place to fit in the specific signal group. +-- @param group The signal group. The `nil` indicates a generic group. +-- @return The (now modified) signal aspect itself. +function signal_aspect:to_group(group) + local cg = self.group + local gdef = registered_groups[group] + if type(self.name) ~= "string" then + self.name = nil + end + if not gdef then + for k in pairs(named_aspect_aspfields) do + rawset(self, k, self[k]) + end + self.group = nil + self.name = nil + return self + elseif cg == group and gdef.aspects[self.name] then + return self + end + local newidx = 1 + if self.main == 0 then + newidx = #gdef.aspects + end + local cgdef = registered_groups[cg] + if cgdef then + local idx = (cgdef.aspects[self.name] or {}).index + if idx then + if idx >= #cgdef.aspects then + idx = #gdef.aspects + elseif idx >= #gdef.aspects then + idx = #gdef.aspects-1 + end + newidx = idx + end + end + self.group = group + self.name = group.aspects[newidx][1] + return self +end + +--- Modify the signal aspect in-place to indicate a specific distant aspect. +-- @param dst The distant aspect +-- @param[opt=1] shift The phase shift of the current signal. +-- @return The (now modified) signal aspect itself. +function signal_aspect:adjust_distant(dst, shift) + if (shift or -1) < 0 then + shift = 1 + end + if not dst then + self.dst = nil + return self + end + if self.main ~= 0 then + self.dst = dst.main + else + self.dst = nil + end + local dgdef = registered_groups[dst.group] + if dgdef then + if self.group == dst.group and shift == 0 then + self.name = dst.name + else + local idx = (dgdef.aspects[dst.name] or {}).index + if idx then + idx = math.max(idx-shift, 1) + self.group = dst.group + self.name = dgdef.aspects[idx][1] + end + end + end + return self +end + +--- Signal groups. +-- @section signal_group + +--- Register a signal group. +-- @function register_group +-- @param def The definition table. +local function register_group(def) + local t = {} + local name = def.name + if type(name) ~= "string" then + return error("Expected signal group name to be a string, got " .. type(name)) + elseif registered_groups[name] then + return error(string.format("Attempt to redefine signal group %q, previously defined in %s", name, registered_groups[name].defined)) + end + t.name = name + + t.defined = debug.getinfo(2, "S").short_src or "[?]" + + local label = def.label or name + if type(label) ~= "string" then + return error("Label is not a string") + end + t.label = label + + local mainasps = {} + for idx, asp in pairs(def.aspects) do + local idxtp = type(idx) + if idxtp == "string" then + local t = {} + t.name = idx + + local label = asp.label or idx + if type(label) ~= "string" then + return error("Aspect label is not a string") + end + t.label = label + + for k in pairs(named_aspect_aspfields) do + t[k] = asp[k] + end + + mainasps[idx] = t + end + end + if #def.aspects < 2 then + return error("Insufficient entries in signal aspect list") + end + for idx, asplist in ipairs(def.aspects) do + if type(asplist) ~= "table" then + asplist = {asplist} + else + asplist = table.copy(asplist) + end + if #asplist < 1 then + error("Invalid entry in signal aspect list") + end + for _, k in ipairs(asplist) do + if type(k) ~= "string" then + return error("Invalid signal aspect ID") + end + local asp = mainasps[k] + if not asp then + return error("Invalid signal aspect ID") + end + if asp.index ~= nil then + return error("Attempt to assign a signal aspect to multiple numeric indices") + end + asp.index = idx + end + mainasps[idx] = asplist + end + t.aspects = mainasps + + registered_groups[name] = t +end + +--- Get the definition of a signal group. +-- @function get_group_definition +-- @param name The name of the signal group. +-- @return[1] The definition for the signal group (if present). +-- @return[2] The nil constant (otherwise). +local function get_group_definition(name) + local t = registered_groups[name] + if t then + return table.copy(t) + else + return nil + end +end + +local lib = { + register_group = register_group, + get_group_definition = get_group_definition, +} + +local libmt = { + __call = function(_, ...) + return signal_aspect.new(...) + end, +} + +return setmetatable(lib, libmt) diff --git a/advtrains_interlocking/distant.lua b/advtrains_interlocking/distant.lua index 4175875..32ada82 100644 --- a/advtrains_interlocking/distant.lua +++ b/advtrains_interlocking/distant.lua @@ -6,7 +6,6 @@ local db_distant = {} local db_distant_of = {} -local A = advtrains.interlocking.aspects local pts = advtrains.encode_pos local stp = advtrains.decode_pos diff --git a/advtrains_interlocking/init.lua b/advtrains_interlocking/init.lua index 1a8ef07..4d959cc 100644 --- a/advtrains_interlocking/init.lua +++ b/advtrains_interlocking/init.lua @@ -12,7 +12,7 @@ end local modpath = minetest.get_modpath(minetest.get_current_modname()) .. DIR_DELIM -advtrains.interlocking.aspects = dofile(modpath.."signal_aspects.lua") +advtrains.interlocking.aspect = dofile(modpath.."aspect.lua") dofile(modpath.."database.lua") dofile(modpath.."distant.lua") diff --git a/advtrains_interlocking/signal_api.lua b/advtrains_interlocking/signal_api.lua index 1b4a21c..ce8854a 100644 --- a/advtrains_interlocking/signal_api.lua +++ b/advtrains_interlocking/signal_api.lua @@ -19,27 +19,7 @@ advtrains.interlocking.FULL_FREE = { proceed_as_main = true, } -local function convert_aspect_if_necessary(asp) - if type(asp.main) == "table" then - local newasp = {} - if asp.main.free then - newasp.main = asp.main.speed - else - newasp.main = 0 - end - if asp.dst and asp.dst.free then - newasp.dst = asp.dst.speed - else - newasp.dst = 0 - end - newasp.proceed_as_main = asp.shunt.proceed_as_main - newasp.shunt = asp.shunt.free - -- Note: info table not transferred, it's not used right now - return newasp - end - return asp -end -advtrains.interlocking.signal_convert_aspect_if_necessary = convert_aspect_if_necessary +advtrains.interlocking.signal_convert_aspect_if_necessary = advtrains.interlocking.aspect function advtrains.interlocking.update_signal_aspect(tcbs, skipdst) if tcbs.signal then @@ -79,7 +59,7 @@ function advtrains.interlocking.signal_rc_handler(pos, node, player, itemstack, end advtrains.interlocking.show_signal_form(pos, node, pname) end - + function advtrains.interlocking.show_signal_form(pos, node, pname) local sigd = advtrains.interlocking.db.get_sigd_for_signal(pos) if sigd then @@ -92,7 +72,7 @@ function advtrains.interlocking.show_signal_form(pos, node, pname) advtrains.interlocking.signal_set_aspect(pos, aspect) end local isasp = advtrains.interlocking.signal_get_aspect(pos, node) - + advtrains.interlocking.show_signal_aspect_selector( pname, ndef.advtrains.supported_aspects, @@ -123,7 +103,7 @@ local function ipmarker(ipos, connid) local node_ok, conns, rhe = advtrains.get_rail_info_at(ipos, advtrains.all_tracktypes) if not node_ok then return end local yaw = advtrains.dir_to_angle(conns[connid].c) - + -- using tcbmarker here local obj = minetest.add_entity(vector.add(ipos, {x=0, y=0.2, z=0}), "advtrains_interlocking:tcbmarker") if not obj then return end diff --git a/advtrains_interlocking/signal_aspect_accessors.lua b/advtrains_interlocking/signal_aspect_accessors.lua index e419515..d91df31 100644 --- a/advtrains_interlocking/signal_aspect_accessors.lua +++ b/advtrains_interlocking/signal_aspect_accessors.lua @@ -1,33 +1,12 @@ --- Signal aspect accessors -- @module advtrains.interlocking -local A = advtrains.interlocking.aspects +local A = advtrains.interlocking.aspect local D = advtrains.distant local I = advtrains.interlocking local N = advtrains.ndb local pts = advtrains.roundfloorpts -local signal_aspect_metatable = { - __tostring = function(asp) - local st = {} - if asp.type2group and asp.type2name then - table.insert(st, string.format("%q in group %q", asp.type2name, asp.type2group)) - end - if asp.main then - table.insert(st, string.format("current %d", asp.main)) - end - if asp.main ~= 0 then - if asp.dst then - table.insert(st, string.format("next %d", asp.dst)) - end - if asp.proceed_as_main then - table.insert(st, "proceed as main") - end - end - return string.format("[%s]", table.concat(st, ", ")) - end, -} - local get_aspect local supposed_aspects = {} @@ -37,9 +16,9 @@ local supposed_aspects = {} -- @param db The new database. function I.load_supposed_aspects(tbl) if tbl then - supposed_aspects = tbl - for _, v in pairs(tbl) do - setmetatable(v, signal_aspect_metatable) + supposed_aspects = {} + for k, v in pairs(tbl) do + supposed_aspects[k] = A(v) end end end @@ -48,7 +27,11 @@ end -- @function save_supposed_aspects -- @return The current database in use. function I.save_supposed_aspects() - return supposed_aspects + local t = {} + for k, v in pairs(supposed_aspects) do + t[k] = v:plain(true) + end + return t end --- Read the aspect of a signal strictly from cache. @@ -72,7 +55,7 @@ end -- @return[2] An empty table (otherwise). local function get_ndef(pos) local node = N.get_node(pos) - return minetest.registered_nodes[node.name] or {} + return (minetest.registered_nodes[node.name] or {}), node end --- Get the aspects supported by a signal. @@ -94,43 +77,18 @@ end -- @return The adjusted signal aspect. -- @return The information to pass to the `advtrains.set_aspect` field in the node definitions. local function adjust_aspect(pos, asp) - asp = table.copy(I.signal_convert_aspect_if_necessary(asp)) - setmetatable(asp, signal_aspect_metatable) + local asp = A(asp) local mainpos = D.get_main(pos) local nxtasp if mainpos then nxtasp = get_aspect(mainpos) end - if asp.main ~= 0 and mainpos then - asp.dst = nxtasp.main - else - asp.dst = nil - end - local suppasp = get_supported_aspects(pos) if not suppasp then - return asp, asp - end - local stype = suppasp.type - if stype == 2 then - local group = suppasp.group - local name - if suppasp.dst_shift and nxtasp then - asp.main = nil - name = A.type1_to_type2main(nxtasp, group, suppasp.dst_shift) - elseif asp.main ~= 0 and nxtasp and nxtasp.type2group == group and nxtasp.type2name then - name = A.get_type2_dst(group, nxtasp.type2name) - else - name = A.type1_to_type2main(asp, group) - end - asp.type2group = group - asp.type2name = name - return asp, name + return asp end - asp.type2name = nil - asp.type2group = nil - return asp, asp + return asp:adjust_distant(nxtasp, suppasp.dst_shift):to_group(suppasp.group) end --- Get the aspect of a signal without accessing the cache. @@ -140,13 +98,9 @@ end -- @return[1] The signal aspect adjusted using `adjust_aspect` (if present). -- @return[2] The nil constant (otherwise). local function get_real_aspect(pos) - local ndef = get_ndef(pos) + local ndef, node = get_ndef(pos) if ndef.advtrains and ndef.advtrains.get_aspect then local asp = ndef.advtrains.get_aspect(pos, node) or I.DANGER - local suppasp = get_supported_aspects(pos) - if suppasp and suppasp.type == 2 then - asp = A.type2_to_type1(suppasp, asp) - end return adjust_aspect(pos, asp) end return nil @@ -176,11 +130,11 @@ local function set_aspect(pos, asp, skipdst) local ndef = minetest.registered_nodes[node.name] if ndef and ndef.advtrains and ndef.advtrains.set_aspect then local oldasp = I.signal_get_aspect(pos) or DANGER - local newasp, aspval = adjust_aspect(pos, asp) + local newasp = adjust_aspect(pos, asp) set_supposed_aspect(pos, newasp) - ndef.advtrains.set_aspect(pos, node, aspval) + ndef.advtrains.set_aspect(pos, node, newasp) I.signal_on_aspect_changed(pos) - local aspect_changed = A.not_equalp(oldasp, newasp) + local aspect_changed = oldasp ~= newasp if (not skipdst) and aspect_changed then D.update_main(pos) end diff --git a/advtrains_interlocking/signal_aspect_ui.lua b/advtrains_interlocking/signal_aspect_ui.lua index 186d2fe..d36c6bc 100644 --- a/advtrains_interlocking/signal_aspect_ui.lua +++ b/advtrains_interlocking/signal_aspect_ui.lua @@ -1,7 +1,7 @@ local F = advtrains.formspec local players_aspsel = {} -local function describe_t1_main_aspect(spv) +local function describe_main_aspect(spv) if spv == 0 then return attrans("Danger (halt)") elseif spv == -1 then @@ -13,7 +13,7 @@ local function describe_t1_main_aspect(spv) end end -local function describe_t1_shunt_aspect(shunt) +local function describe_shunt_aspect(shunt) if shunt then return attrans("Shunting allowed") else @@ -21,7 +21,7 @@ local function describe_t1_shunt_aspect(shunt) end end -local function describe_t1_distant_aspect(spv) +local function describe_distant_aspect(spv) if spv == 0 then return attrans("Expect to stop at the next signal") elseif spv == -1 then @@ -33,9 +33,9 @@ local function describe_t1_distant_aspect(spv) end end -advtrains.interlocking.describe_t1_main_aspect = describe_t1_main_aspect -advtrains.interlocking.describe_t1_shunt_aspect = describe_t1_shunt_aspect -advtrains.interlocking.describe_t1_distant_aspect = describe_t1_distant_aspect +advtrains.interlocking.describe_main_aspect = describe_main_aspect +advtrains.interlocking.describe_shunt_aspect = describe_shunt_aspect +advtrains.interlocking.describe_distant_aspect = describe_distant_aspect local function dsel(p, q, x, y) if p == nil then @@ -51,19 +51,23 @@ local function dsel(p, q, x, y) end end -local function describe_supported_aspects_t1(suppasp, isasp) +local function describe_supported_aspects(suppasp, isasp) local t = {} - local entries = {} - local selid = 1 - for idx, spv in ipairs(suppasp.main) do - if isasp and spv == (isasp.main or false) then + local entries = {attrans("Use default value")} + local selid = 0 + local mainasps = suppasp.main + if type(mainasps) ~= "table" then + mainasps = {mainasps or false} + end + for idx, spv in ipairs(mainasps) do + if isasp and spv == rawget(isasp, "main") then selid = idx end - entries[idx] = describe_t1_main_aspect(spv) + entries[idx+1] = describe_main_aspect(spv) end t.main = entries - t.main_current = selid + t.main_current = selid+1 t.main_string = tostring(isasp.main) if t.main == nil then t.main_string = "" @@ -83,21 +87,21 @@ local function describe_supported_aspects_t1(suppasp, isasp) entries = {} selid = 1 - for idx, spv in ipairs(suppasp.dst) do + for idx, spv in ipairs(suppasp.dst or {}) do if isasp and spv == (isasp.dst or false) then selid = idx end - entries[idx] = describe_t1_distant_aspect(spv) + entries[idx] = describe_distant_aspect(spv) end t.dst = entries t.dst_current = selid return t end -advtrains.interlocking.describe_supported_aspects_t1 = describe_supported_aspects_t1 +advtrains.interlocking.describe_supported_aspects = describe_supported_aspects -local function make_signal_aspect_selector_t1(suppasp, purpose, isasp) - local t = describe_supported_aspects_t1(suppasp, isasp) +local function make_signal_aspect_selector(suppasp, purpose, isasp) + local t = describe_supported_aspects(suppasp, isasp) local formmode = 1 local pos @@ -142,55 +146,6 @@ local function make_signal_aspect_selector_t1(suppasp, purpose, isasp) return table.concat(form) end -local function make_signal_aspect_selector_t2(suppasp, purpose, isasp) - local def = advtrains.interlocking.aspects.get_type2_definition(suppasp.group) - if not def then - return nil - end - local formmode = 1 - - local pos - if type(purpose) == "table" then - formmode = 2 - pos = purpose.pos - end - local form = { - "formspec_version[4]", - string.format("size[8,%f]", ({4.25, 10.25})[formmode]), - F.S_label(0.5, 0.5, "Select signal aspect") - } - if formmode == 1 then - form[#form+1] = F.label(0.5, 1, purpose) - else - form[#form+1] = F.S_label(0.5, 1, "Signal at @1", minetest.pos_to_string(pos)) - end - - local entries = {} - local selid = #def.main - if isasp then - if isasp.type2name ~= def.main[selid].name then - selid = 1 - end - end - if selid > 1 then - selid = 2 - end - local entries = { - def.main[1].label, - def.main[#def.main].label, - } - form[#form+1] = F.S_label(0.5, 1.5, "Signal group: @1", def.label) - form[#form+1] = F.dropdown(0.5, 2, 7, "asp_sel", entries, selid, true) - form[#form+1] = F.S_button_exit(0.5, 3, 7, "asp_save", "Save signal aspect") - - if formmode == 2 then - form[#form+1] = advtrains.interlocking.make_ip_formspec_component(pos, 0.5, 4, 7) - form[#form+1] = advtrains.interlocking.make_dst_formspec_component(pos, 0.5, 5.5, 7, 4.25) - end - - return table.concat(form) -end - function advtrains.interlocking.show_signal_aspect_selector(pname, p_suppasp, p_purpose, callback, isasp) local suppasp = p_suppasp or { main = {0, -1}, @@ -205,18 +160,7 @@ function advtrains.interlocking.show_signal_aspect_selector(pname, p_suppasp, p_ purpose = {pname = pname, pos = pos} end - local form - if suppasp.type == 2 then - if suppasp.dst_shift then - if pos then - advtrains.interlocking.show_ip_form(pos, pname) - end - return - end - form = make_signal_aspect_selector_t2(suppasp, purpose, isasp) - else - form = make_signal_aspect_selector_t1(suppasp, purpose, isasp) - end + local form = make_signal_aspect_selector(suppasp, purpose, isasp) if not form then return end @@ -241,9 +185,9 @@ local function usebool(sup, val, free) end end -local function get_aspect_from_formspec_t1(suppasp, fields, psl) +local function get_aspect_from_formspec(suppasp, fields, psl) local maini = tonumber(fields.asp_mainsel) - local main = suppasp.main[maini] + local main = suppasp.main[(maini or 0)-1] if not maini then local mainval = fields.asp_mainval if mainval == "-1" then @@ -253,6 +197,8 @@ local function get_aspect_from_formspec_t1(suppasp, fields, psl) else main = nil end + elseif maini <= 1 then + main = nil end local shunti = tonumber(fields.asp_shunt) local shunt = suppasp.shunt @@ -271,19 +217,6 @@ local function get_aspect_from_formspec_t1(suppasp, fields, psl) } end -local function get_aspect_from_formspec_t2(suppasp, fields, psl) - local sel = tonumber(fields.asp_sel) - local def = advtrains.interlocking.aspects.get_type2_definition(suppasp.group) - if not (sel and def) then - return - end - if sel ~= 1 then - sel = #def.main - end - local asp = advtrains.interlocking.aspects.type2_to_type1(suppasp, sel) - return asp -end - minetest.register_on_player_receive_fields(function(player, formname, fields) local pname = player:get_player_name() local psl = players_aspsel[pname] @@ -292,11 +225,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) local suppasp = psl.suppasp if fields.asp_save then local asp - if suppasp.type == 2 then - asp = get_aspect_from_formspec_t2(suppasp, fields, psl) - else - asp = get_aspect_from_formspec_t1(suppasp, fields, psl) - end + asp = get_aspect_from_formspec(suppasp, fields, psl) if asp then psl.callback(pname, asp) end diff --git a/advtrains_interlocking/signal_aspects.lua b/advtrains_interlocking/signal_aspects.lua deleted file mode 100644 index 14e04c7..0000000 --- a/advtrains_interlocking/signal_aspects.lua +++ /dev/null @@ -1,202 +0,0 @@ ---- Signal aspect handling. --- @module advtrains.interlocking.aspects - -local type2defs = {} - ---- Register a type 2 signal group. --- @function register_type2 --- @param def The definition table. -local function register_type2(def) - local t = {type = 2} - local name = def.name - if type(name) ~= "string" then - return error("Name is not a string") - elseif type2defs[name] then - return error(string.format("Attempt to redefine type 2 signal aspect group %q, previously defined in %s", name, type2defs[name].defined)) - end - t.name = name - - t.defined = debug.getinfo(2, "S").short_src or "[?]" - - local label = def.label or name - if type(label) ~= "string" then - return error("Label is not a string") - end - t.label = label - - local mainasps = {} - for idx, asp in ipairs(def.main) do - local t = {} - local name = asp.name - if type(name) ~= "string" then - return error("Aspect name is not a string") - end - t.name = name - - local label = asp.label or name - if type(label) ~= "string" then - return error("Aspect label is not a string") - end - t.label = label - - t.main = asp.main - t.shunt = asp.shunt - t.proceed_as_main = asp.proceed_as_main - mainasps[idx] = t - mainasps[name] = idx - end - t.main = mainasps - - type2defs[name] = t -end - ---- Get the definition of a type 2 signal group. --- @function get_type2_definition --- @param name The name of the signal group. --- @return[1] The definition for the signal group (if present). --- @return[2] The nil constant (otherwise). -local function get_type2_definition(name) - local t = type2defs[name] - if t then - return table.copy(t) - else - return nil - end -end - ---- Get the name of the distant aspect before the current aspect. --- @function get_type2_dst --- @param group The name of the group. --- @param name The name of the current aspect. --- @return[1] The name of the distant aspect (if present). --- @return[2] The nil constant (otherwise). -local function get_type2_dst(group, name) - local def = type2defs[group] - if not def then - return nil - end - local aspidx = name - if type(name) ~= "number" then - aspidx = def.main[name] or 1 - end - return def.main[math.max(1, aspidx-1)].name -end - ---- Convert a type 2 signal aspect to a type 1 signal aspect. --- @function type2_to_type1 --- @param suppasp The table of supported aspects for the signal. --- @param asp The name of the signal aspect. --- @return[1] The type 1 signal aspect table (if present). --- @return[2] The nil constant (otherwise). -local function type2_to_type1(suppasp, asp) - local name = suppasp.group - local shift = suppasp.dst_shift - local def = type2defs[name] - if not def then - return nil - end - local aspidx - if type(asp) == "number" then - aspidx = asp - else - aspidx = def.main[asp] or 2 - end - local realidx = math.min(#def.main, aspidx+(shift or 0)) - local asptbl = def.main[realidx] - if not asptbl then - return nil - end - if type(asp) == "number" then - asp = asptbl.name - end - local main, shunt, dst - if shift then - dst = asptbl.main - else - main = asptbl.main - shunt = asptbl.shunt - dst = def.main[math.min(#def.main, aspidx+1)].main - end - if main == 0 then - dst = nil - end - - local t = { - main = main, - shunt = shunt, - proceed_as_main = asptbl.proceed_as_main, - type2name = asptbl.name, - type2group = name, - dst = dst, - } - if aspidx > 1 and aspidx < #asptbl then - t.dst = asptbl[aspidx+1].main - end - return t -end - ---- Convert a type 1 signal aspect table to a type 2 signal aspect. --- @function type1_to_type2main --- @param asp The type 1 signal aspect table --- @param group The signal aspect group --- @param[opt=0] shift The shift for the signal aspect. --- @return[1] The name of the signal aspect (if present). --- @return[2] The nil constant (otherwise). -local function type1_to_type2main(asp, group, shift) - local def = type2defs[group] - if not def then - return nil - end - local t_main = def.main - local idx - if group == asp.type2group and t_main[asp.type2name] then - idx = t_main[asp.type2name] - elseif not asp.main or asp.main == -1 then - idx = 1 - elseif asp.main == 0 then - idx = #t_main - else - idx = #t_main-1 - end - return t_main[math.max(1, idx-(shift or 0))].name -end - ---- Compare two type 1 signal aspect tables. --- @function equalp --- @param asp1 The first signal aspect table. --- @param asp2 The second signal aspect table. --- @return Whether the two signal aspect tables give the same (type 1 aspect) information. -local function equalp(asp1, asp2) - if asp1 == asp2 then -- same reference - return true - else - for _, k in pairs {"main", "shunt", "dst"} do - if asp1[k] ~= asp2[k] then - return false - end - end - end - if asp1.type2group and asp1.type2group == asp2.type2group then - return asp1.type2name == asp2.type2name - end - return true -end - ---- Compare two signal aspect tables. --- @function not_equalp --- @param asp1 The first signal aspect table. --- @param asp2 The second signal aspect table. --- @return The negation of `equalp``(asp1, asp2)`. -local function not_equalp(asp1, asp2) - return not equalp(asp1, asp2) -end - -return { - register_type2 = register_type2, - get_type2_definition = get_type2_definition, - get_type2_dst = get_type2_dst, - type2_to_type1 = type2_to_type1, - type1_to_type2main = type1_to_type2main, - equalp = equalp, - not_equalp = not_equalp, -} diff --git a/advtrains_interlocking/spec/basic_signalling_spec.lua b/advtrains_interlocking/spec/basic_signalling_spec.lua index cce0f15..a4e1e3a 100644 --- a/advtrains_interlocking/spec/basic_signalling_spec.lua +++ b/advtrains_interlocking/spec/basic_signalling_spec.lua @@ -8,7 +8,7 @@ mineunit("core") _G.advtrains = { interlocking = { - aspects = fixture("../../signal_aspects"), + aspect = fixture("../../aspect"), }, ndb = { get_node = minetest.get_node, @@ -31,12 +31,16 @@ minetest.register_node("advtrains_interlocking:signal_sign", { local D = advtrains.distant local I = advtrains.interlocking +local A = I.aspect local stub_aspect_t1 = { free = {main = -1}, slow = {main = 6}, danger = {main = 0, shunt = false}, } +for k, v in pairs(stub_aspect_t1) do + stub_aspect_t1[k] = A(v) +end local stub_pos_t1 = {} for i = 1, 4 do stub_pos_t1[i] = {x = 1, y = 0, z = i} @@ -55,14 +59,14 @@ describe("API for supposed signal aspects", function() I.load_supposed_aspects(tbl) assert.same(tbl, I.save_supposed_aspects()) end) - it("should set and get type 1 signals properly", function () + it("should set and get signals properly", function () local pos = stub_pos_t1[2] local asp = stub_aspect_t1.slow - local newasp = { main = math.random(1,5) } - assert.same(asp, I.signal_get_aspect(pos)) + local newasp = A{ main = math.random(1,5) } + assert.equal(asp, I.signal_get_aspect(pos)) I.signal_set_aspect(pos, newasp) - assert.same(newasp, I.signal_get_aspect(pos)) - assert.same(asp, I.signal_get_real_aspect(pos)) + assert.equal(newasp, I.signal_get_aspect(pos)) + assert.equal(asp, I.signal_get_real_aspect(pos)) I.signal_set_aspect(pos, asp) end) end) @@ -72,9 +76,9 @@ describe("Distant signaling", function() for i = 1, 2 do D.assign(stub_pos_t1[i], stub_pos_t1[i+1]) end - assert.same(stub_aspect_t1.danger, I.signal_get_aspect(stub_pos_t1[1])) - assert.same({main = 6, dst = 0}, I.signal_get_aspect(stub_pos_t1[2])) - assert.same({main = -1, dst = 6}, I.signal_get_aspect(stub_pos_t1[3])) + assert.equal(stub_aspect_t1.danger, I.signal_get_aspect(stub_pos_t1[1])) + assert.equal(A{main = 6, dst = 0}, I.signal_get_aspect(stub_pos_t1[2])) + assert.equal(A{main = -1, dst = 6}, I.signal_get_aspect(stub_pos_t1[3])) end) it("should report assignments properly", function() assert.same({stub_pos_t1[1], "manual"}, {D.get_main(stub_pos_t1[2])}) @@ -82,8 +86,8 @@ describe("Distant signaling", function() end) it("should update distant aspects automatically", function() I.signal_set_aspect(stub_pos_t1[2], {main = 2, dst = -1}) - assert.same({main = 2, dst = 0}, I.signal_get_aspect(stub_pos_t1[2])) - assert.same({main = -1, dst = 2}, I.signal_get_aspect(stub_pos_t1[3])) + assert.equal(A{main = 2, dst = 0}, I.signal_get_aspect(stub_pos_t1[2])) + assert.equal(A{main = -1, dst = 2}, I.signal_get_aspect(stub_pos_t1[3])) end) it("should unassign signals when one is removed", function() world.set_node(stub_pos_t1[2], "air") diff --git a/advtrains_interlocking/spec/signal_group_spec.lua b/advtrains_interlocking/spec/signal_group_spec.lua new file mode 100644 index 0000000..bc9d007 --- /dev/null +++ b/advtrains_interlocking/spec/signal_group_spec.lua @@ -0,0 +1,95 @@ +require "mineunit" +mineunit("core") + +_G.advtrains = { + interlocking = { + aspect = sourcefile("aspect"), + }, + ndb = { + get_node = minetest.get_node, + swap_node = minetest.swap_node, + } +} + +fixture("advtrains_helpers") +sourcefile("database") +sourcefile("signal_api") +sourcefile("distant") +sourcefile("signal_aspect_accessors") + +local A = advtrains.interlocking.aspect +local D = advtrains.distant +local I = advtrains.interlocking +local N = advtrains.ndb + +local groupdef = { + name = "foo", + aspects = { + proceed = {main = -1}, + caution = {}, + danger = {main = 0}, + "proceed", + {"caution"}, + "danger", + }, +} + +for k, v in pairs(groupdef.aspects) do + minetest.register_node("advtrains_interlocking:" .. k, { + advtrains = { + supported_aspects = { + group = "foo", + }, + get_aspect = function() return A{group = "foo", name = k} end, + set_aspect = function(pos, _, name) + N.swap_node(pos, {name = "advtrains_interlocking:" .. name}) + end, + } + }) +end + +local origin = vector.new(0, 0, 0) +local dstpos = vector.new(0, 0, 1) + +world.layout { + {origin, "advtrains_interlocking:danger"}, + {dstpos, "advtrains_interlocking:proceed"}, +} + +describe("signal group registration", function() + it("should work", function() + A.register_group(groupdef) + assert(A.get_group_definition("foo")) + end) + it("should only be allowed once for the same group", function() + assert.has.errors(function() A.register_group(type2def) end) + end) + it("should handle nonexistant groups", function() + assert.is_nil(A.get_group_definition("something_else")) + end) + it("should reject invalid definitions", function() + assert.has.errors(function() A.register_group({}) end) + assert.has.errors(function() A.register_group({name="",label={}}) end) + assert.has.errors(function() A.register_group({name="",aspects={}}) end) + end) +end) + +describe("signal aspect", function() + it("should handle empty fields properly", function() + assert.equal(A{main = 0}, A{group="foo", name="danger"}:to_group()) + end) + it("should be converted properly", function() + assert.equal(A{main = 0}, A{group="foo", name="danger"}) + assert.equal(A{}, A{group="foo", name="caution"}) + assert.equal(A{main = -1}, A{group="foo", name="proceed"}) + end) +end) + +describe("signals in groups", function() + it("should support distant signaling", function() + assert.equal("caution", A():adjust_distant(A{group="foo",name="danger"}).name) + assert.equal("proceed", A():adjust_distant(A{group="foo",name="caution"}).name) + assert.equal("proceed", A():adjust_distant(A{group="foo",name="proceed"}).name) + assert.equal("danger", A{group="foo",name="danger"}:adjust_distant{}.name) + end) +end) diff --git a/advtrains_interlocking/spec/type2_spec.lua b/advtrains_interlocking/spec/type2_spec.lua deleted file mode 100644 index ac23574..0000000 --- a/advtrains_interlocking/spec/type2_spec.lua +++ /dev/null @@ -1,117 +0,0 @@ -require "mineunit" -mineunit("core") - -_G.advtrains = { - interlocking = { - aspects = sourcefile("signal_aspects"), - }, - ndb = { - get_node = minetest.get_node, - swap_node = minetest.swap_node, - } -} - -fixture("advtrains_helpers") -sourcefile("database") -sourcefile("signal_api") -sourcefile("distant") -sourcefile("signal_aspect_accessors") - -local A = advtrains.interlocking.aspects -local D = advtrains.distant -local I = advtrains.interlocking -local N = advtrains.ndb - -local type2def = { - name = "foo", - main = { - {name = "proceed", main = -1}, - {name = "caution"}, - {name = "danger", main = 0}, - }, -} - -for _, v in pairs(type2def.main) do - minetest.register_node("advtrains_interlocking:" .. v.name, { - advtrains = { - supported_aspects = { - type = 2, - group = "foo", - }, - get_aspect = function() return v.name end, - set_aspect = function(pos, _, name) - N.swap_node(pos, {name = "advtrains_interlocking:" .. name}) - end, - } - }) -end - -local function asp(group, name, dst) - return A.type2_to_type1({group = group, dst_shift = shift}, name) -end - -local origin = vector.new(0, 0, 0) -local dstpos = vector.new(0, 0, 1) - -world.layout { - {origin, "advtrains_interlocking:danger"}, - {dstpos, "advtrains_interlocking:proceed"}, -} - -describe("type 2 signal group registration", function() - it("should work", function() - A.register_type2(type2def) - assert(A.get_type2_definition("foo")) - end) - it("should only be allowed once for the same group", function() - assert.has.errors(function() A.register_type2(type2def) end) - end) - it("should handle nonexistant groups", function() - assert.is_nil(A.get_type2_definition("something_else")) - end) - it("should reject invalid definitions", function() - assert.has.errors(function() A.register_type2({}) end) - assert.has.errors(function() A.register_type2({name="",label={}}) end) - assert.has.errors(function() A.register_type2({name="",main={{name={}}}}) end) - assert.has.errors(function() A.register_type2({name="",main={{name="",label={}}}}) end) - end) -end) - -describe("signal aspect conversion", function() - it("should work for converting from type 1 to type 2", function() - assert.equal("danger", A.type1_to_type2main({main = 0}, "foo")) - assert.equal("caution", A.type1_to_type2main({main = 6}, "foo")) - assert.equal("proceed", A.type1_to_type2main({}, "foo")) - end) - it("should reject invalid type 2 signal information", function() - assert.is_nil(A.type1_to_type2main({}, "?")) - assert.is_nil(A.type2_to_type1({}, "x")) - assert.same(asp("foo","caution"), asp("foo", "x")) - end) - it("should accept integer indices for type 2 signal aspects", function() - assert.same(asp("foo", "caution"), asp("foo", 2)) - assert.same(asp("foo", "danger"), asp("foo", 10)) - assert.same(asp("foo", "proceed"), asp("foo", 1)) - assert.is_nil(asp("foo", -0.5)) - end) -end) - -describe("type 2 signals", function() - it("should support distant signaling", function() - assert.equal("caution", A.get_type2_dst("foo", 3)) - assert.equal("proceed", A.get_type2_dst("foo", "caution")) - assert.equal("proceed", A.get_type2_dst("foo", "proceed")) - end) - it("should work with accessors", function() - assert.same(asp("foo","danger"), I.signal_get_aspect(origin)) - local newasp = {type2group = "foo", type2name = "proceed", main = 6} - I.signal_set_aspect(origin, newasp) - assert.same(newasp, I.signal_get_aspect(origin)) - end) - it("should work with distant signaling", function() - assert.same(asp("foo","proceed"), I.signal_get_aspect(dstpos)) - local dstasp = {type2group = "foo", type2name = "proceed", dst = 6, main = -1} - D.assign(origin, dstpos) - assert.same(dstasp, I.signal_get_aspect(dstpos)) - end) -end) diff --git a/advtrains_signals_japan/init.lua b/advtrains_signals_japan/init.lua index fe74259..7d8dc1e 100644 --- a/advtrains_signals_japan/init.lua +++ b/advtrains_signals_japan/init.lua @@ -291,10 +291,10 @@ local aspnames = { } local function process_signal(name, sigdata, isrpt) local typename = "advtrains_signals_japan:" .. name - local type2def = {} - type2def.name = typename - type2def.main = {} - type2def.label = S(string.format("Japanese signal (type %s)", string.upper(name))) + local groupdef = {} + groupdef.name = typename + groupdef.aspects = {} + groupdef.label = S(string.format("Japanese signal (type %s)", string.upper(name))) local def = {} local tx = {} def.typename = typename @@ -322,7 +322,8 @@ local function process_signal(name, sigdata, isrpt) tt[#tt+1] = string.format("0,%d=(advtrains_hud_bg.png\\^[colorize\\:%s)", lightcount-1, color) end tx[aspname] = table.concat(tt, ":") - type2def.main[idx] = {name = asp.name, label = S(aspnames[asp.name]), main = asp.main, proceed_as_main = true} + groupdef.aspects[idx] = {asp.name} + groupdef.aspects[asp.name] = {label = S(aspnames[asp.name]), main = asp.main, proceed_as_main = true} end local invimg = { string.format("[combine:%dx%d", lightcount*4+1, lightcount*4+1), @@ -337,7 +338,7 @@ local function process_signal(name, sigdata, isrpt) end def.inventory_image = table.concat(invimg, ":") if not isrpt then - advtrains.interlocking.aspects.register_type2(type2def) + advtrains.interlocking.aspect.register_group(groupdef) end return def end @@ -400,15 +401,14 @@ for _, rtab in ipairs { drop = "advtrains_signals_japan:"..sigtype.."_danger_0", advtrains = { supported_aspects = { - type = 2, group = siginfo.typename, dst_shift = siginfo.isdst and 0, }, get_aspect = function() - return asp + return {group = siginfo.typename, name = asp} end, set_aspect = function(pos, node, asp) - advtrains.ndb.swap_node(pos, {name = "advtrains_signals_japan:"..sigtype.."_"..asp.."_"..rot, param2 = node.param2}) + advtrains.ndb.swap_node(pos, {name = "advtrains_signals_japan:"..sigtype.."_"..(asp.name).."_"..rot, param2 = node.param2}) end, }, on_rightclick = advtrains.interlocking.signal_rc_handler, -- cgit v1.2.3 From 950d6f640cae76d28253fadb7974a064017b104c Mon Sep 17 00:00:00 2001 From: orwell96 Date: Mon, 4 Sep 2023 19:34:47 +0200 Subject: Begin major rework of track registration system --- advtrains/api_doc.txt | 2 - advtrains/copytool.lua | 2 +- advtrains/helpers.lua | 6 +- advtrains/nodedb.lua | 6 +- advtrains/oldtracks.lua | 751 ++++++++++++++++++++++++ advtrains/passive.lua | 90 ++- advtrains/path.lua | 7 +- advtrains/signals.lua | 7 +- advtrains/trackplacer.lua | 592 ++++++++----------- advtrains/tracks.lua | 1038 ++++++++++----------------------- advtrains/trainlogic.lua | 12 +- advtrains/wagons.lua | 5 +- advtrains_interlocking/init.lua | 4 +- advtrains_line_automation/init.lua | 4 +- advtrains_signals_ks/init.lua | 21 +- advtrains_train_track/init.lua | 1109 ++++++------------------------------ advtrains_train_track/oldinit.lua | 937 ++++++++++++++++++++++++++++++ 17 files changed, 2443 insertions(+), 2150 deletions(-) create mode 100644 advtrains/oldtracks.lua create mode 100644 advtrains_train_track/oldinit.lua (limited to 'advtrains_interlocking/init.lua') diff --git a/advtrains/api_doc.txt b/advtrains/api_doc.txt index 5668ba3..6b338a7 100644 --- a/advtrains/api_doc.txt +++ b/advtrains/api_doc.txt @@ -18,8 +18,6 @@ advtrains.register_wagon(name, prototype, description, inventory_image) # Wagon prototype properties { ... all regular luaentity properties (mesh, textures, collisionbox a.s.o)... - drives_on = {default=true}, - ^- used to define the tracktypes (see below) that wagon can drive on. The tracktype identifiers are given as keys, similar to privileges) max_speed = 10, ^- optional, default 10: defines the maximum speed this wagon can drive. The maximum speed of a train is determined by the wagon with the lowest max_speed value. seats = { diff --git a/advtrains/copytool.lua b/advtrains/copytool.lua index 0c1cdfe..8a6d2f7 100644 --- a/advtrains/copytool.lua +++ b/advtrains/copytool.lua @@ -21,7 +21,7 @@ minetest.register_tool("advtrains:copytool", { local node=minetest.get_node_or_nil(pointed_thing.under) if not node then atprint("[advtrains]Ignore at placer position") return itemstack end local nodename=node.name - if(not advtrains.is_track_and_drives_on(nodename, {default=true})) then + if(not advtrains.is_track(nodename)) then atprint("no track here, not placing.") return itemstack end diff --git a/advtrains/helpers.lua b/advtrains/helpers.lua index 7e078fb..7a774d9 100644 --- a/advtrains/helpers.lua +++ b/advtrains/helpers.lua @@ -294,7 +294,7 @@ end -- Going from the rail at pos (does not need to be rounded) along connection with id conn_idx, if there is a matching rail, return it and the matching connid -- returns: , , , -- parameter this_conns_p is connection table of this rail and is optional, is determined by get_rail_info_at if not provided. -function advtrains.get_adjacent_rail(this_posnr, this_conns_p, conn_idx, drives_on) +function advtrains.get_adjacent_rail(this_posnr, this_conns_p, conn_idx) local this_pos = advtrains.round_vector_floor_y(this_posnr) local this_conns = this_conns_p local _ @@ -318,11 +318,11 @@ function advtrains.get_adjacent_rail(this_posnr, this_conns_p, conn_idx, drives_ adj_pos.y = adj_pos.y + 1 end - local nextnode_ok, nextconns, nextrail_y=advtrains.get_rail_info_at(adj_pos, drives_on) + local nextnode_ok, nextconns, nextrail_y=advtrains.get_rail_info_at(adj_pos) if not nextnode_ok then adj_pos.y = adj_pos.y - 1 conn_y = conn_y + 1 - nextnode_ok, nextconns, nextrail_y=advtrains.get_rail_info_at(adj_pos, drives_on) + nextnode_ok, nextconns, nextrail_y=advtrains.get_rail_info_at(adj_pos) if not nextnode_ok then return nil end diff --git a/advtrains/nodedb.lua b/advtrains/nodedb.lua index 41ac089..39106b2 100644 --- a/advtrains/nodedb.lua +++ b/advtrains/nodedb.lua @@ -268,17 +268,17 @@ end --false if it's not a rail or the train does not drive on this rail, but it is loaded or --nil if the node is neither loaded nor in trackdb --the distraction between false and nil will be needed only in special cases.(train initpos) -function advtrains.get_rail_info_at(pos, drives_on) +function advtrains.get_rail_info_at(pos) local rdp=advtrains.round_vector_floor_y(pos) local node=ndb.get_node_or_nil(rdp) if not node then return end local nodename=node.name - if(not advtrains.is_track_and_drives_on(nodename, drives_on)) then + if(not advtrains.is_track(nodename)) then return false end - local conns, railheight, tracktype=advtrains.get_track_connections(node.name, node.param2) + local conns, railheight = advtrains.get_track_connections(node.name, node.param2) return true, conns, railheight end diff --git a/advtrains/oldtracks.lua b/advtrains/oldtracks.lua new file mode 100644 index 0000000..c415143 --- /dev/null +++ b/advtrains/oldtracks.lua @@ -0,0 +1,751 @@ +--advtrains by orwell96, see readme.txt + +--dev-time settings: +--EDIT HERE +--If the old non-model rails on straight tracks should be replaced by the new... +--false: no +--true: yes +advtrains.register_replacement_lbms=false + +--[[TracksDefinition +nodename_prefix +texture_prefix +description +common={} +straight={} +straight45={} +curve={} +curve45={} +lswitchst={} +lswitchst45={} +rswitchst={} +rswitchst45={} +lswitchcr={} +lswitchcr45={} +rswitchcr={} +rswitchcr45={} +vert1={ + --you'll probably want to override mesh here +} +vert2={ + --you'll probably want to override mesh here +} +]]-- +advtrains.all_tracktypes={} + +--definition preparation +local function conns(c1, c2, r1, r2) return {{c=c1, y=r1}, {c=c2, y=r2}} end +local function conns3(c1, c2, c3, r1, r2, r3) return {{c=c1, y=r1}, {c=c2, y=r2}, {c=c3, y=r3}} end + +advtrains.ap={} +advtrains.ap.t_30deg_flat={ + regstep=1, + variant={ + st={ + conns = conns(0,8), + desc = "straight", + tpdouble = true, + tpsingle = true, + trackworker = "cr", + }, + cr={ + conns = conns(0,7), + desc = "curve", + tpdouble = true, + trackworker = "swlst", + }, + swlst={ + conns = conns3(0,8,7), + desc = "left switch (straight)", + trackworker = "swrst", + switchalt = "cr", + switchmc = "on", + switchst = "st", + switchprefix = "swl", + }, + swlcr={ + conns = conns3(0,7,8), + desc = "left switch (curve)", + trackworker = "swrcr", + switchalt = "st", + switchmc = "off", + switchst = "cr", + switchprefix = "swl", + }, + swrst={ + conns = conns3(0,8,9), + desc = "right switch (straight)", + trackworker = "st", + switchalt = "cr", + switchmc = "on", + switchst = "st", + switchprefix = "swr", + }, + swrcr={ + conns = conns3(0,9,8), + desc = "right switch (curve)", + trackworker = "st", + switchalt = "st", + switchmc = "off", + switchst = "cr", + switchprefix = "swr", + }, + }, + regtp=true, + tpdefault="st", + trackworker={ + ["swrcr"]="st", + ["swrst"]="st", + ["cr"]="swlst", + ["swlcr"]="swrcr", + ["swlst"]="swrst", + }, + rotation={"", "_30", "_45", "_60"}, +} +advtrains.ap.t_yturnout={ + regstep=1, + variant={ + l={ + conns = conns3(0,7,9), + desc = "Y-turnout (left)", + switchalt = "r", + switchmc = "off", + switchst = "l", + switchprefix = "", + }, + r={ + conns = conns3(0,9,7), + desc = "Y-turnout (right)", + switchalt = "l", + switchmc = "on", + switchst = "r", + switchprefix = "", + } + }, + regtp=true, + tpdefault="l", + rotation={"", "_30", "_45", "_60"}, +} +advtrains.ap.t_s3way={ + regstep=1, + variant={ + l={ + conns = { {c=0}, {c=7}, {c=8}, {c=9}, {c=0} }, + desc = "3-way turnout (left)", + switchalt = "s", + switchst="l", + switchprefix = "", + }, + s={ + conns = { {c=0}, {c=8}, {c=7}, {c=9}, {c=0} }, + desc = "3-way turnout (straight)", + switchalt ="r", + switchst = "s", + switchprefix = "", + }, + r={ + conns = { {c=0}, {c=9}, {c=8}, {c=7}, {c=0} }, + desc = "3-way turnout (right)", + switchalt = "l", + switchst="r", + switchprefix = "", + } + }, + regtp=true, + tpdefault="l", + rotation={"", "_30", "_45", "_60"}, +} +advtrains.ap.t_30deg_slope={ + regstep=1, + variant={ + vst1={conns = conns(8,0,0,0.5), rail_y = 0.25, desc = "steep uphill 1/2", slope=true}, + vst2={conns = conns(8,0,0.5,1), rail_y = 0.75, desc = "steep uphill 2/2", slope=true}, + vst31={conns = conns(8,0,0,0.33), rail_y = 0.16, desc = "uphill 1/3", slope=true}, + vst32={conns = conns(8,0,0.33,0.66), rail_y = 0.5, desc = "uphill 2/3", slope=true}, + vst33={conns = conns(8,0,0.66,1), rail_y = 0.83, desc = "uphill 3/3", slope=true}, + }, + regsp=true, + slopeplacer={ + [2]={"vst1", "vst2"}, + [3]={"vst31", "vst32", "vst33"}, + max=3,--highest entry + }, + slopeplacer_45={ + [2]={"vst1_45", "vst2_45"}, + max=2, + }, + rotation={"", "_30", "_45", "_60"}, + trackworker={}, + increativeinv={}, +} +advtrains.ap.t_30deg_straightonly={ + regstep=1, + variant={ + st={ + conns = conns(0,8), + desc = "straight", + tpdouble = true, + tpsingle = true, + trackworker = "st", + }, + }, + regtp=true, + tpdefault="st", + rotation={"", "_30", "_45", "_60"}, +} +advtrains.ap.t_30deg_straightonly_noplacer={ + regstep=1, + variant={ + st={ + conns = conns(0,8), + desc = "straight", + tpdouble = true, + tpsingle = true, + trackworker = "st", + }, + }, + tpdefault="st", + rotation={"", "_30", "_45", "_60"}, +} +advtrains.ap.t_45deg={ + regstep=2, + variant={ + st={ + conns = conns(0,8), + desc = "straight", + tpdouble = true, + tpsingle = true, + trackworker = "cr", + }, + cr={ + conns = conns(0,6), + desc = "curve", + tpdouble = true, + trackworker = "swlst", + }, + swlst={ + conns = conns3(0,8,6), + desc = "left switch (straight)", + trackworker = "swrst", + switchalt = "cr", + switchmc = "on", + switchst = "st", + }, + swlcr={ + conns = conns3(0,6,8), + desc = "left switch (curve)", + trackworker = "swrcr", + switchalt = "st", + switchmc = "off", + switchst = "cr", + }, + swrst={ + conns = conns3(0,8,10), + desc = "right switch (straight)", + trackworker = "st", + switchalt = "cr", + switchmc = "on", + switchst = "st", + }, + swrcr={ + conns = conns3(0,10,8), + desc = "right switch (curve)", + trackworker = "st", + switchalt = "st", + switchmc = "off", + switchst = "cr", + }, + }, + regtp=true, + tpdefault="st", + trackworker={ + ["swrcr"]="st", + ["swrst"]="st", + ["cr"]="swlst", + ["swlcr"]="swrcr", + ["swlst"]="swrst", + }, + rotation={"", "_30", "_45", "_60"}, +} +advtrains.ap.t_perpcrossing={ + regstep = 1, + variant={ + st={ + conns = { {c=0}, {c=8}, {c=4}, {c=12} }, + desc = "perpendicular crossing", + tpdouble = true, + tpsingle = true, + trackworker = "st", + }, + }, + regtp=true, + tpdefault="st", + rotation={"", "_30", "_45", "_60"}, +} +advtrains.ap.t_90plusx_crossing={ + regstep = 1, + variant={ + ["30l"]={ + conns = { {c=0}, {c=8}, {c=1}, {c=9} }, + desc = "30/90 degree crossing (left)", + tpdouble = true, + tpsingle = true, + trackworker = "45l" + }, + ["45l"]={ + conns = { {c=0}, {c=8}, {c=2}, {c=10} }, + desc = "45/90 degree crossing (left)", + tpdouble = true, + tpsingle = true, + trackworker = "60l", + }, + ["60l"]={ + conns = { {c=0}, {c=8}, {c=3}, {c=11}}, + desc = "60/90 degree crossing (left)", + tpdouble = true, + tpsingle = true, + trackworker = "60r", + }, + ["60r"]={ + conns = { {c=0}, {c=8}, {c=5}, {c=13} }, + desc = "60/90 degree crossing (right)", + tpdouble = true, + tpsingle = true, + trackworker = "45r" + }, + ["45r"]={ + conns = { {c=0}, {c=8}, {c=6}, {c=14} }, + desc = "45/90 degree crossing (right)", + tpdouble = true, + tpsingle = true, + trackworker = "30r", + }, + ["30r"]={ + conns = { {c=0}, {c=8}, {c=7}, {c=15}}, + desc = "30/90 degree crossing (right)", + tpdouble = true, + tpsingle = true, + trackworker = "30l", + }, + }, + regtp=true, + tpdefault="30l", + rotation={""}, + trackworker = { + ["30l"] = "45l", + ["45l"] = "60l", + ["60l"] = "60r", + ["60r"] = "45r", + ["45r"] = "30r", + ["30r"] = "30l", + } +} + +advtrains.ap.t_diagonalcrossing = { + regstep=1, + variant={ + ["30l45r"]={ + conns = {{c=1}, {c=9}, {c=6}, {c=14}}, + desc = "30left-45right diagonal crossing", + tpdouble=true, + tpsingle=true, + trackworker="60l30l", + }, + ["60l30l"]={ + conns = {{c=3}, {c=11}, {c=1}, {c=9}}, + desc = "30left-60right diagonal crossing", + tpdouble=true, + tpsingle=true, + trackworker="60l45r" + }, + ["60l45r"]={ + conns = {{c=3}, {c=11}, {c=6}, {c=14}}, + desc = "60left-45right diagonal crossing", + tpdouble=true, + tpsingle=true, + trackworker="60l60r" + }, + ["60l60r"]={ + conns = {{c=3}, {c=11}, {c=5}, {c=13}}, + desc = "60left-60right diagonal crossing", + tpdouble=true, + tpsingle=true, + trackworker="60r45l", + }, + --If 60l60r had a mirror image, it would be here, but it's symmetric. + -- 60l60r is also equivalent to 30l30r but rotated 90 degrees. + ["60r45l"]={ + conns = {{c=5}, {c=13}, {c=2}, {c=10}}, + desc = "60right-45left diagonal crossing", + tpdouble=true, + tpsingle=true, + trackworker="60r30r", + }, + ["60r30r"]={ + conns = {{c=5}, {c=13}, {c=7}, {c=15}}, + desc = "60right-30right diagonal crossing", + tpdouble=true, + tpsingle=true, + trackworker="30r45l", + }, + ["30r45l"]={ + conns = {{c=7}, {c=15}, {c=2}, {c=10}}, + desc = "30right-45left diagonal crossing", + tpdouble=true, + tpsingle=true, + trackworker="30l45r", + }, + + }, + regtp=true, + tpdefault="30l45r", + rotation={""}, + trackworker = { + ["30l45r"] = "60l30l", + ["60l30l"] = "60l45r", + ["60l45r"] = "60l60r", + ["60l60r"] = "60r45l", + ["60r45l"] = "60r30r", + ["60r30r"] = "30r45l", + ["30r45l"] = "30l45r", + } +} + +advtrains.trackpresets = advtrains.ap + +--definition format: ([] optional) +--[[{ + nodename_prefix + texture_prefix + [shared_texture] + models_prefix + models_suffix (with dot) + [shared_model] + formats={ + st,cr,swlst,swlcr,swrst,swrcr,vst1,vst2 + (each a table with indices 0-3, for if to register a rail with this 'rotation' table entry. nil is assumed as 'all', set {} to not register at all) + } + common={} change something on common rail appearance +} +[18.12.17] Note on new connection system: +In order to support real rail crossing nodes and finally make the trackplacer respect switches, I changed the connection system. +There can be a variable number of connections available. These are specified as tuples {c=, y=} +The table "at_conns" consists of {, ...} +the "at_rail_y" property holds the value that was previously called "railheight" +Depending on the number of connections: +2 conns: regular rail +3 conns: switch: + - when train passes in at conn1, will move out of conn2 + - when train passes in at conn2 or conn3, will move out of conn1 +4 conns: cross (or cross switch, depending on arrangement of conns): + - conn1 <> conn2 + - conn3 <> conn4 +]] + +-- Notify the user if digging the rail is not allowed +local function can_dig_callback(pos, player) + local ok, reason = advtrains.can_dig_or_modify_track(pos) + if not ok and player then + minetest.chat_send_player(player:get_player_name(), attrans("This track can not be removed!") .. " " .. reason) + end + return ok +end + +function advtrains.register_tracks(tracktype, def, preset) + advtrains.trackplacer.register_tracktype(def.nodename_prefix, preset.tpdefault) + if preset.regtp then + advtrains.trackplacer.register_track_placer(def.nodename_prefix, def.texture_prefix, def.description, def) + end + if preset.regsp then + advtrains.slope.register_placer(def, preset) + end + for suffix, var in pairs(preset.variant) do + for rotid, rotation in ipairs(preset.rotation) do + if not def.formats[suffix] or def.formats[suffix][rotid] then + local img_suffix = suffix..rotation + local ndef = advtrains.merge_tables({ + description=def.description.."("..(var.desc or "any")..rotation..")", + drawtype = "mesh", + paramtype="light", + paramtype2="facedir", + walkable = false, + selection_box = { + type = "fixed", + fixed = {-1/2-1/16, -1/2, -1/2, 1/2+1/16, -1/2+2/16, 1/2}, + }, + + mesh = def.shared_model or (def.models_prefix.."_"..img_suffix..def.models_suffix), + tiles = {def.shared_texture or (def.texture_prefix.."_"..img_suffix..".png"), def.second_texture}, + + groups = { + attached_node = advtrains.IGNORE_WORLD and 0 or 1, + advtrains_track=1, + ["advtrains_track_"..tracktype]=1, + save_in_at_nodedb=1, + dig_immediate=2, + not_in_creative_inventory=1, + not_blocking_trains=1, + }, + + can_dig = can_dig_callback, + after_dig_node=function(pos) + advtrains.ndb.update(pos) + end, + after_place_node=function(pos) + advtrains.ndb.update(pos) + end, + at_nnpref = def.nodename_prefix, + at_suffix = suffix, + at_rotation = rotation, + at_rail_y = var.rail_y + }, def.common or {}) + + if preset.regtp then + ndef.drop = def.nodename_prefix.."_placer" + end + if preset.regsp and var.slope then + ndef.drop = def.nodename_prefix.."_slopeplacer" + end + + --connections + ndef.at_conns = advtrains.rotate_conn_by(var.conns, (rotid-1)*preset.regstep) + + local ndef_avt_table + + if var.switchalt and var.switchst then + local switchfunc=function(pos, node, newstate) + newstate = newstate or var.switchalt -- support for 3 (or more) state switches + -- this code is only called from the internal setstate function, which + -- ensures that it is safe to switch the turnout + if newstate~=var.switchst then + advtrains.ndb.swap_node(pos, {name=def.nodename_prefix.."_"..(var.switchprefix or "")..newstate..rotation, param2=node.param2}) + advtrains.invalidate_all_paths(pos) + end + end + ndef.on_rightclick = function(pos, node, player) + if advtrains.check_turnout_signal_protection(pos, player:get_player_name()) then + advtrains.setstate(pos, nil, node) + advtrains.log("Switch", player:get_player_name(), pos) + end + end + if var.switchmc then + ndef.mesecons = {effector = { + ["action_"..var.switchmc] = function(pos, node) + advtrains.setstate(pos, nil, node) + end, + rules=advtrains.meseconrules + }} + end + ndef_avt_table = { + getstate = var.switchst, + setstate = switchfunc, + } + end + + local adef={} + if def.get_additional_definiton then + adef=def.get_additional_definiton(def, preset, suffix, rotation) + end + ndef = advtrains.merge_tables(ndef, adef) + + -- insert getstate/setstate functions after merging the additional definitions + if ndef_avt_table then + ndef.advtrains = advtrains.merge_tables(ndef.advtrains or {}, ndef_avt_table) + end + + minetest.register_node(":"..def.nodename_prefix.."_"..suffix..rotation, ndef) + --trackplacer + if preset.regtp then + local tpconns = {conn1=ndef.at_conns[1].c, conn2=ndef.at_conns[2].c} + if var.tpdouble then + advtrains.trackplacer.add_double_conn(def.nodename_prefix, suffix, rotation, tpconns) + end + if var.tpsingle then + advtrains.trackplacer.add_single_conn(def.nodename_prefix, suffix, rotation, tpconns) + end + end + advtrains.trackplacer.add_worked(def.nodename_prefix, suffix, rotation, var.trackworker) + end + end + end + advtrains.all_tracktypes[tracktype]=true +end + +function advtrains.is_track_and_drives_on(nodename, drives_on_p) + local drives_on = drives_on_p + if not drives_on then drives_on = advtrains.all_tracktypes end + local hasentry = false + for _,_ in pairs(drives_on) do + hasentry=true + end + if not hasentry then drives_on = advtrains.all_tracktypes end + + if not minetest.registered_nodes[nodename] then + return false + end + local nodedef=minetest.registered_nodes[nodename] + for k,v in pairs(drives_on) do + if nodedef.groups["advtrains_track_"..k] then + return true + end + end + return false +end + +function advtrains.get_track_connections(name, param2) + local nodedef=minetest.registered_nodes[name] + if not nodedef then atprint(" get_track_connections couldn't find nodedef for nodename "..(name or "nil")) return nil end + local noderot=param2 + if not param2 then noderot=0 end + if noderot > 3 then atprint(" get_track_connections: rail has invaild param2 of "..noderot) noderot=0 end + + local tracktype + for k,_ in pairs(nodedef.groups) do + local tt=string.match(k, "^advtrains_track_(.+)$") + if tt then + tracktype=tt + end + end + return advtrains.rotate_conn_by(nodedef.at_conns, noderot*AT_CMAX/4), (nodedef.at_rail_y or 0), tracktype +end + +-- Function called when a track is about to be dug or modified by the trackworker +-- Returns either true (ok) or false,"translated string describing reason why it isn't allowed" +function advtrains.can_dig_or_modify_track(pos) + if advtrains.get_train_at_pos(pos) then + return false, attrans("Position is occupied by a train.") + end + -- interlocking: tcb, signal IP a.s.o. + if advtrains.interlocking then + -- TCB? + if advtrains.interlocking.db.get_tcb(pos) then + return false, attrans("There's a Track Circuit Break here.") + end + -- signal ip? + if advtrains.interlocking.db.is_ip_at(pos, true) then -- is_ip_at with purge parameter + return false, attrans("There's a Signal Influence Point here.") + end + end + return true +end + +-- slope placer. Defined in register_tracks. +--crafted with rail and gravel +local sl={} +function sl.register_placer(def, preset) + minetest.register_craftitem(":"..def.nodename_prefix.."_slopeplacer",{ + description = attrans("@1 Slope", def.description), + inventory_image = def.texture_prefix.."_slopeplacer.png", + wield_image = def.texture_prefix.."_slopeplacer.png", + groups={}, + on_place = sl.create_slopeplacer_on_place(def, preset) + }) +end +--(itemstack, placer, pointed_thing) +function sl.create_slopeplacer_on_place(def, preset) + return function(istack, player, pt) + if not pt.type=="node" then + minetest.chat_send_player(player:get_player_name(), attrans("Can't place: not pointing at node")) + return istack + end + local pos=pt.above + if not pos then + minetest.chat_send_player(player:get_player_name(), attrans("Can't place: not pointing at node")) + return istack + end + local node=minetest.get_node(pos) + if not minetest.registered_nodes[node.name] or not minetest.registered_nodes[node.name].buildable_to then + minetest.chat_send_player(player:get_player_name(), attrans("Can't place: space occupied!")) + return istack + end + if not advtrains.check_track_protection(pos, player:get_player_name()) then + minetest.record_protection_violation(pos, player:get_player_name()) + return istack + end + --determine player orientation (only horizontal component) + --get_look_horizontal may not be available + local yaw=player.get_look_horizontal and player:get_look_horizontal() or (player:get_look_yaw() - math.pi/2) + + --rounding unit vectors is a nice way for selecting 1 of 8 directions since sin(30°) is 0.5. + local dirvec={x=math.floor(math.sin(-yaw)+0.5), y=0, z=math.floor(math.cos(-yaw)+0.5)} + --translate to direction to look up inside the preset table + local param2, rot45=({ + [-1]={ + [-1]=2, + [0]=3, + [1]=3, + }, + [0]={ + [-1]=2, + [1]=0, + }, + [1]={ + [-1]=1, + [0]=1, + [1]=0, + }, + })[dirvec.x][dirvec.z], dirvec.x~=0 and dirvec.z~=0 + local lookup=preset.slopeplacer + if rot45 then lookup=preset.slopeplacer_45 end + + --go unitvector forward and look how far the next node is + local step=1 + while step<=lookup.max do + local node=minetest.get_node(vector.add(pos, dirvec)) + --next node solid? + if not minetest.registered_nodes[node.name] or not minetest.registered_nodes[node.name].buildable_to or advtrains.is_protected(pos, player:get_player_name()) then + --do slopes of this distance exist? + if lookup[step] then + if minetest.settings:get_bool("creative_mode") or istack:get_count()>=step then + --start placing + local placenodes=lookup[step] + while step>0 do + minetest.set_node(pos, {name=def.nodename_prefix.."_"..placenodes[step], param2=param2}) + if not minetest.settings:get_bool("creative_mode") then + istack:take_item() + end + step=step-1 + pos=vector.subtract(pos, dirvec) + end + else + minetest.chat_send_player(player:get_player_name(), attrans("Can't place: Not enough slope items left (@1 required)", step)) + end + else + minetest.chat_send_player(player:get_player_name(), attrans("Can't place: There's no slope of length @1",step)) + end + return istack + end + step=step+1 + pos=vector.add(pos, dirvec) + end + minetest.chat_send_player(player:get_player_name(), attrans("Can't place: no supporting node at upper end.")) + return itemstack + end +end + +advtrains.slope=sl + +--END code, BEGIN definition +--definition format: ([] optional) +--[[{ + nodename_prefix + texture_prefix + [shared_texture] + models_prefix + models_suffix (with dot) + [shared_model] + formats={ + st,cr,swlst,swlcr,swrst,swrcr,vst1,vst2 + (each a table with indices 0-3, for if to register a rail with this 'rotation' table entry. nil is assumed as 'all', set {} to not register at all) + } + common={} change something on common rail appearance +}]] + + + + + + + + + diff --git a/advtrains/passive.lua b/advtrains/passive.lua index fe4790c..76da720 100644 --- a/advtrains/passive.lua +++ b/advtrains/passive.lua @@ -1,9 +1,5 @@ -- passive.lua --- API to passive components, as described in passive_api.txt of advtrains_luaautomation --- This has been moved to the advtrains core in turn with the interlocking system, --- to prevent a dependency on luaautomation. - -local deprecation_warned = {} +-- 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 @@ -19,20 +15,8 @@ function advtrains.getstate(parpos, pnode) local node=pnode or advtrains.ndb.get_node(pos) local ndef=minetest.registered_nodes[node.name] local st - if ndef and ndef.advtrains and ndef.advtrains.getstate then - st=ndef.advtrains.getstate - elseif ndef and ndef.luaautomation and ndef.luaautomation.getstate then - if not deprecation_warned[node.name] then - minetest.log("warning", node.name.." uses deprecated definition of ATLATC functions in the 'luaautomation' field. Please move them to the 'advtrains' field!") - end - st=ndef.luaautomation.getstate - else - return nil - end - if type(st)=="function" then - return st(pos, node) - else - return st + if ndef and ndef.advtrains then + return ndef.advtrains.node_state end end @@ -45,31 +29,46 @@ function advtrains.setstate(parpos, newstate, pnode) 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") + error("Invalid position supplied to setstate") end local node=pnode or advtrains.ndb.get_node(pos) local ndef=minetest.registered_nodes[node.name] - local st - if ndef and ndef.advtrains and ndef.advtrains.setstate then - st=ndef.advtrains.setstate - elseif ndef and ndef.luaautomation and ndef.luaautomation.setstate then - if not deprecation_warned[node.name] then - minetest.log("warning", node.name.." uses deprecated definition of ATLATC functions in the 'luaautomation' field. Please move them to the 'advtrains' field!") - end - st=ndef.luaautomation.setstate - else - return nil + + 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 + return false, "train_here" end if advtrains.interlocking and advtrains.interlocking.route.has_route_lock(minetest.pos_to_string(pos)) then - return false + return false, "route_lock_here" end - st(pos, node, newstate) + -- 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 + return true end @@ -86,12 +85,7 @@ function advtrains.is_passive(parpos, pnode) 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.getstate then - return true - elseif ndef and ndef.luaautomation and ndef.luaautomation.getstate then - if not deprecation_warned[node.name] then - minetest.log("warning", node.name.." uses deprecated definition of ATLATC functions in the 'luaautomation' field. Please move them to the 'advtrains' field!") - end + if ndef and ndef.advtrains and ndef.advtrains.node_state_map then return true else return false @@ -102,20 +96,10 @@ end function advtrains.set_fallback_state(pos, pnode) local node=pnode or advtrains.ndb.get_node(pos) local ndef=minetest.registered_nodes[node.name] - local st - if ndef and ndef.advtrains and ndef.advtrains.setstate - and ndef.advtrains.fallback_state then - if advtrains.get_train_at_pos(pos) then - return false - end - - if advtrains.interlocking and advtrains.interlocking.route.has_route_lock(minetest.pos_to_string(pos)) then - return false - end - - ndef.advtrains.setstate(pos, node, ndef.advtrains.fallback_state) - return true - end + if not ndef or not ndef.advtrains or not ndef.advtrains.node_fallback_state then + return false, "no_fallback_state" + end + return advtrains.setstate(pos, ndef.advtrains.node_fallback_state, node) end diff --git a/advtrains/path.lua b/advtrains/path.lua index 19387b1..72ee05d 100644 --- a/advtrains/path.lua +++ b/advtrains/path.lua @@ -33,13 +33,12 @@ -- If you need to proceed along the path by a specific actual distance, it does NOT work to simply add it to the index. You should use the path_get_index_by_offset() function. -- creates the path data structure, reconstructing the train from a position and a connid --- Important! train.drives_on must exist while calling this method -- returns: true - successful -- nil - node not yet available/unloaded, please wait -- false - node definitely gone, remove train function advtrains.path_create(train, pos, connid, rel_index) local posr = advtrains.round_vector_floor_y(pos) - local node_ok, conns, rhe = advtrains.get_rail_info_at(pos, train.drives_on) + local node_ok, conns, rhe = advtrains.get_rail_info_at(pos) if not node_ok then return node_ok end @@ -211,7 +210,7 @@ function advtrains.path_get(train, index) if pef == train.path_trk_f then node_ok, this_conns = advtrains.get_rail_info_at(pos) if not node_ok then error("For train "..train.id..": Path item "..pef.." on-track but not a valid node!") end - adj_pos, adj_connid, conn_idx, nextrail_y, next_conns = advtrains.get_adjacent_rail(pos, this_conns, connid, train.drives_on) + adj_pos, adj_connid, conn_idx, nextrail_y, next_conns = advtrains.get_adjacent_rail(pos, this_conns, connid) end pef = pef + 1 if adj_pos then @@ -250,7 +249,7 @@ function advtrains.path_get(train, index) if peb == train.path_trk_b then node_ok, this_conns = advtrains.get_rail_info_at(pos) if not node_ok then error("For train "..train.id..": Path item "..peb.." on-track but not a valid node!") end - adj_pos, adj_connid, conn_idx, nextrail_y, next_conns = advtrains.get_adjacent_rail(pos, this_conns, connid, train.drives_on) + adj_pos, adj_connid, conn_idx, nextrail_y, next_conns = advtrains.get_adjacent_rail(pos, this_conns, connid) end peb = peb - 1 if adj_pos then diff --git a/advtrains/signals.lua b/advtrains/signals.lua index b26c950..58d28a5 100644 --- a/advtrains/signals.lua +++ b/advtrains/signals.lua @@ -40,9 +40,6 @@ local suppasp = { for r,f in pairs({on={as="off", ls="green", als="red"}, off={as="on", ls="red", als="green"}}) do - advtrains.trackplacer.register_tracktype("advtrains:retrosignal", "") - advtrains.trackplacer.register_tracktype("advtrains:signal", "") - for rotid, rotation in ipairs({"", "_30", "_45", "_60"}) do local crea=1 if rotid==1 and r=="off" then crea=0 end @@ -108,8 +105,8 @@ for r,f in pairs({on={as="off", ls="green", als="red"}, off={as="on", ls="red", }, can_dig = can_dig_func, after_dig_node = after_dig_func, + --TODO add rotation using trackworker }) - advtrains.trackplacer.add_worked("advtrains:retrosignal", r, rotation, nil) minetest.register_node("advtrains:signal_"..r..rotation, { drawtype = "mesh", @@ -179,8 +176,8 @@ for r,f in pairs({on={as="off", ls="green", als="red"}, off={as="on", ls="red", }, can_dig = can_dig_func, after_dig_node = after_dig_func, + --TODO add rotation using trackworker }) - advtrains.trackplacer.add_worked("advtrains:signal", r, rotation, nil) end local crea=1 diff --git a/advtrains/trackplacer.lua b/advtrains/trackplacer.lua index fe76290..9d63199 100644 --- a/advtrains/trackplacer.lua +++ b/advtrains/trackplacer.lua @@ -3,311 +3,204 @@ --all new trackplacer code local tp={ - tracks={} + groups={} } -function tp.register_tracktype(nnprefix, n_suffix) - if tp.tracks[nnprefix] then return end--due to the separate registration of slopes and flats for the same nnpref, definition would be overridden here. just don't. - tp.tracks[nnprefix]={ - default=n_suffix, - single_conn={}, - single_conn_1={}, - single_conn_2={}, - double_conn={}, - double_conn_1={}, - double_conn_2={}, - --keys:conn1_conn2 (example:1_4) - --values:{name=x, param2=x} - twcycle={}, - twrotate={},--indexed by suffix, list, tells order of rotations - modify={}, - } -end -function tp.add_double_conn(nnprefix, suffix, rotation, conns) - local nodename=nnprefix.."_"..suffix..rotation - for i=0,3 do - tp.tracks[nnprefix].double_conn[((conns.conn1+4*i)%16).."_"..((conns.conn2+4*i)%16)]={name=nodename, param2=i} - tp.tracks[nnprefix].double_conn[((conns.conn2+4*i)%16).."_"..((conns.conn1+4*i)%16)]={name=nodename, param2=i} - tp.tracks[nnprefix].double_conn_1[((conns.conn1+4*i)%16).."_"..((conns.conn2+4*i)%16)]={name=nodename, param2=i} - tp.tracks[nnprefix].double_conn_2[((conns.conn2+4*i)%16).."_"..((conns.conn1+4*i)%16)]={name=nodename, param2=i} - end - tp.tracks[nnprefix].modify[nodename]=true -end -function tp.add_single_conn(nnprefix, suffix, rotation, conns) - local nodename=nnprefix.."_"..suffix..rotation - for i=0,3 do - tp.tracks[nnprefix].single_conn[((conns.conn1+4*i)%16)]={name=nodename, param2=i} - tp.tracks[nnprefix].single_conn[((conns.conn2+4*i)%16)]={name=nodename, param2=i} - tp.tracks[nnprefix].single_conn_1[((conns.conn1+4*i)%16)]={name=nodename, param2=i} - tp.tracks[nnprefix].single_conn_2[((conns.conn2+4*i)%16)]={name=nodename, param2=i} - end - tp.tracks[nnprefix].modify[nodename]=true -end +--[[ New in version 2.5: +The track placer no longer uses hacky nodename pattern matching. +The base criterion for rotating or matching tracks is the common "ndef.advtrains.track_place_group" property. +Only rails where this field is set are considered for replacement. Other rails can still be considered for connection. +Replacement ("bending") of rails can only happen within their respective track place group. Only two-conn rails are allowed in the trackplacer. -function tp.add_worked(nnprefix, suffix, rotation, cycle_follows) - tp.tracks[nnprefix].twcycle[suffix]=cycle_follows - if not tp.tracks[nnprefix].twrotate[suffix] then tp.tracks[nnprefix].twrotate[suffix]={} end - table.insert(tp.tracks[nnprefix].twrotate[suffix], rotation) -end +The track registration functions register the candidates for any given track_place_group in two separate collections: +- double: tracks that can be used to connect both ends of the rail +- single: tracks that will be used to connect conn1 when only a single end is to be connected +When track placing is requested, the calling code just supplies the track_place_group to be placed. ---[[ - rewrite algorithm. - selection criteria: these will never be changed or even selected: - - tracks being already connected on both sides - - tracks that are already connected on one side but are not bendable to the desired position - the following situations can occur: - 1. there are two more than two rails around - 1.1 there is one or more subset(s) that can be directly connected - -> choose the first possibility - 2.2 not - -> choose the first one and orient straight - 2. there's exactly 1 rail around - -> choose and orient straight - 3. there's no rail around - -> set straight -]] +]]-- -local function istrackandbc(pos_p, conn) - local tpos = pos_p - local cnode=minetest.get_node(advtrains.dirCoordSet(tpos, conn.c)) - if advtrains.is_track_and_drives_on(cnode.name, advtrains.all_tracktypes) then - local cconns=advtrains.get_track_connections(cnode.name, cnode.param2) - return advtrains.conn_matches_to(conn, cconns) - end - --try the same 1 node below - tpos = {x=tpos.x, y=tpos.y-1, z=tpos.z} - cnode=minetest.get_node(advtrains.dirCoordSet(tpos, conn.c)) - if advtrains.is_track_and_drives_on(cnode.name, advtrains.all_tracktypes) then - local cconns=advtrains.get_track_connections(cnode.name, cnode.param2) - return advtrains.conn_matches_to(conn, cconns) - end - return false +local function rotate(conn, rot) + return (conn + rot) % 16 end -function tp.find_already_connected(pos) - local dnode=minetest.get_node(pos) - local dconns=advtrains.get_track_connections(dnode.name, dnode.param2) - local found_conn - for connid, conn in ipairs(dconns) do - if istrackandbc(pos, conn) then - if found_conn then --we found one in previous iteration - return true, true --signal that it's connected - else - found_conn = conn.c +-- Register a track node as candidate +-- tpg: the track place group to register the candidates for +-- name, ndef: the node name and node definition table to register +-- as_single: whether the rail should be considered as candidate for one-endpoint connection +-- Typically only set for the straight rail variants +-- as_double: whether the rail should be considered as candidate for two-endpoint connection +-- Typically set for straights and curves +function tp.register_candidate(tpg, name, ndef, as_single, as_double) + --get or create TP group + if not tp.groups[tpg] then + tp.groups[tpg] = {double = {}, single1 = {}, single2 = {}, default = {name = name, param2 = 0} } + -- note: this causes the first candidate to ever be registered to be the default (which is typically what you want) + end + local g = tp.groups[tpg] + + -- get conns + assert(#ndef.at_conns == 2) + local c1, c2 = ndef.at_conns[1].c, ndef.at_conns[2].c + local is_symmetrical = (rotate(c1, 8) == c2) + + -- store all possible rotations (param2 values) + for i=0,3 do + if as_double then + g.double[rotate(c1,i*4).."_"..rotate(c2,i*4)] = {name=name, param2=i} + if not is_symmmetrical then + g.double[rotate(c2,i*4).."_"..rotate(c1,i*4)] = {name=name, param2=i} + -- if the track is unsymmetric (e.g. a curve), we may require the "wrong" orientation to fill a gap. end end + if as_single then + g.single1[rotate(c1,i*4)] = {name=name, param2=i} + g.single2[rotate(c2,i*4)] = {name=name, param2=i} + end end - return found_conn end -function tp.rail_and_can_be_bent(originpos, conn) - local pos=advtrains.dirCoordSet(originpos, conn) - local newdir=(conn+8)%16 - local node=minetest.get_node(pos) - if not advtrains.is_track_and_drives_on(node.name, advtrains.all_tracktypes) then + +local function check_or_bend_rail(origin, dir, pname, commit) + local pos = advtrains.pos_add_dir(origin, dir) + local back_dir = advtrains.oppd(dir); + + local node_ok, conns = advtrains.get_rail_info_at(pos) + if not node_ok then + -- try the node one level below + pos.y = pos.y - 1 + node_ok, conns = advtrains.get_rail_info_at(pos) + end + if not node_ok then return false end - local ndef=minetest.registered_nodes[node.name] - local nnpref = ndef and ndef.at_nnpref - if not nnpref then return false end - local tr=tp.tracks[nnpref] - if not tr then return false end - if not tr.modify[node.name] then - --we actually can use this rail, but only if it already points to the desired direction. - if advtrains.is_track_and_drives_on(node.name, advtrains.all_tracktypes) then - local cconns=advtrains.get_track_connections(node.name, node.param2) - return advtrains.conn_matches_to(conn, cconns) + -- if one conn of the node here already points towards us, nothing to do + for connid, conn in ipairs(conns) do + if back_dir == conn.c then + return true end end - -- If the rail is not allowed to be modified, also only use if already in desired direction + -- can we bend the node here? + local node = advtrains.ndb.get_node(pos) + local ndef = minetest.registered_nodes[node.name] + if not ndef or not ndef.advtrains or not ndef.advtrains.track_place_group then + return false + end + -- now the track must be two-conn, else it wouldn't be allowed to have track_place_group set. + assert(#conns == 2) + -- Is player and game allowed to do this? if not advtrains.can_dig_or_modify_track(pos) then - local cconns=advtrains.get_track_connections(node.name, node.param2) - return advtrains.conn_matches_to(conn, cconns) + return false end - --rail at other end? - local adj1, adj2=tp.find_already_connected(pos) - if adj1 and adj2 then - return false--dont destroy existing track - elseif adj1 and not adj2 then - if tr.double_conn[adj1.."_"..newdir] then - return true--if exists, connect new rail and old end - end + if not advtrains.check_track_protection(pos, pname) then return false - else - if tr.single_conn[newdir] then--just rotate old rail to right orientation - return true + end + -- we confirmed that track can be modified. Does there exist a suitable connection candidate? + -- check if there are any unbound ends + local bound_connids = {} + for connid, conn in ipairs(conns) do + local adj_pos, adj_connid = advtrains.get_adjacent_rail(pos, conns, connid) + if adj_pos then + bound_connids[#bound_connids+1] = connid end - return false end -end -function tp.bend_rail(originpos, conn) - local pos=advtrains.dirCoordSet(originpos, conn) - local newdir=advtrains.oppd(conn) - local node=minetest.get_node(pos) - local ndef=minetest.registered_nodes[node.name] - local nnpref = ndef and ndef.at_nnpref - if not nnpref then return false end - local tr=tp.tracks[nnpref] - if not tr then return false end - --is rail already connected? no need to bend. - local conns=advtrains.get_track_connections(node.name, node.param2) - if advtrains.conn_matches_to(conn, conns) then - return + -- depending on the nummber of ends, decide + if #bound_connids == 2 then + -- rail is within a fixed track, do not break up + return false end - --rail at other end? - local adj1, adj2=tp.find_already_connected(pos) - if adj1 and adj2 then - return false--dont destroy existing track - elseif adj1 and not adj2 then - if tr.double_conn[adj1.."_"..newdir] then - advtrains.ndb.swap_node(pos, tr.double_conn[adj1.."_"..newdir]) - return true--if exists, connect new rail and old end + -- obtain the group table + local g = tp.groups[ndef.advtrains.track_place_group] + if #bound_connids == 1 then + -- we can attempt double + local bound_dir = conns[bound_connids[1]].c + if g.double[back_dir.."_"..bound_dir] then + if commit then + advtrains.ndb.swap_node(pos, g.double[back_dir.."_"..bound_dir]) + end + return true end - return false else - if tr.single_conn[newdir] then--just rotate old rail to right orientation - advtrains.ndb.swap_node(pos, tr.single_conn[newdir]) + -- rail is entirely unbound, we can attempt single1 + if g.single1[back_dir] then + if commit then + advtrains.ndb.swap_node(pos, g.single1[back_dir]) + end return true end - return false end end -function tp.placetrack(pos, nnpref, placer, itemstack, pointed_thing, yaw) - --1. find all rails that are likely to be connected - local tr=tp.tracks[nnpref] - local p_rails={} - local p_railpos={} + +local function track_place_node(pos, node, ndef) + advtrains.ndb.swap_node(pos, node) + local ndef = minetest.registered_nodes[node.name] + if ndef and ndef.after_place_node then + ndef.after_place_node(pos) + end +end + + +-- Main API function to place a track. Replaces the older "placetrack" +-- This function will attempt to place a track of the specified track placing group at the specified position, connecting it +-- with neighboring rails. Neighboring rails can themselves be replaced ("bent") within their own track place group, +-- if the player is permitted to do this. +-- Order of preference is: +-- Connect two track ends if possible +-- Connect one track end if any rail is near +-- Place the default track if no tracks are near +-- The function returns true on success. +function tp.place_track(pos, tpg, pname, yaw) + -- 1. collect neighboring tracks and whether they can be connected + local cand = {} for i=0,15 do - if tp.rail_and_can_be_bent(pos, i, nnpref) then - p_rails[#p_rails+1]=i - p_railpos[i] = pos - else - local upos = {x=pos.x, y=pos.y-1, z=pos.z} - if tp.rail_and_can_be_bent(upos, i, nnpref) then - p_rails[#p_rails+1]=i - p_railpos[i] = upos - end + if check_or_bend_rail(pos, i, pname) then + cand[#cand+1] = i end end - - -- try double_conn - if #p_rails > 1 then - --iterate subsets - for k1, conn1 in ipairs(p_rails) do - for k2, conn2 in ipairs(p_rails) do - if k1~=k2 then - local dconn1 = tr.double_conn_1 - local dconn2 = tr.double_conn_2 - if not (advtrains.yawToDirection(yaw, conn1, conn2) == conn1) then - dconn1 = tr.double_conn_2 - dconn2 = tr.double_conn_1 - end - -- Checks are made this way round so that dconn1 has priority (this will make arrows of atc rails - -- point in the right direction) - local using - if (dconn2[conn1.."_"..conn2]) then - using = dconn2[conn1.."_"..conn2] - end - if (dconn1[conn1.."_"..conn2]) then - using = dconn1[conn1.."_"..conn2] - end - if using then - -- has found a fitting rail in either direction - -- if not, continue loop - tp.bend_rail(p_railpos[conn1], conn1, nnpref) - tp.bend_rail(p_railpos[conn2], conn2, nnpref) - advtrains.ndb.swap_node(pos, using) - local nname=using.name - if minetest.registered_nodes[nname] and minetest.registered_nodes[nname].after_place_node then - minetest.registered_nodes[nname].after_place_node(pos, placer, itemstack, pointed_thing) - end - return + -- obtain the group table + local g = tp.groups[tpg] + -- 2. try all possible two-endpoint connections + for k1, conn1 in ipairs(cand) do + for k2, conn2 in ipairs(cand) do + if k1~=k2 then + -- order of conn1/conn2: prefer conn2 being in the direction of the player facing. + -- the combination the other way round will be run through in a later loop iteration + if advtrains.yawToDirection(yaw, conn1, conn2) == conn2 then + -- does there exist a suitable double-connection rail? + local node = g.double[conn1.."_"..conn2] + if node then + check_or_bend_rail(pos, conn1, pname, true) + check_or_bend_rail(pos, conn2, pname, true) + track_place_node(pos, node) -- calls after_place_node implicitly + return true end end end end end - -- try single_conn - if #p_rails > 0 then - for ix, p_rail in ipairs(p_rails) do - local sconn1 = tr.single_conn_1 - local sconn2 = tr.single_conn_2 - if not (advtrains.yawToDirection(yaw, p_rail, (p_rail+8)%16) == p_rail) then - sconn1 = tr.single_conn_2 - sconn2 = tr.single_conn_1 - end - if sconn1[p_rail] then - local using = sconn1[p_rail] - tp.bend_rail(p_railpos[p_rail], p_rail, nnpref) - advtrains.ndb.swap_node(pos, using) - local nname=using.name - if minetest.registered_nodes[nname] and minetest.registered_nodes[nname].after_place_node then - minetest.registered_nodes[nname].after_place_node(pos, placer, itemstack, pointed_thing) - end - return - end - if sconn2[p_rail] then - local using = sconn2[p_rail] - tp.bend_rail(p_railpos[p_rail], p_rail, nnpref) - advtrains.ndb.swap_node(pos, using) - local nname=using.name - if minetest.registered_nodes[nname] and minetest.registered_nodes[nname].after_place_node then - minetest.registered_nodes[nname].after_place_node(pos, placer, itemstack, pointed_thing) - end - return - end + -- 3. try all possible one_endpoint connections + for k1, conn1 in ipairs(cand) do + -- select single1 or single2? depending on yaw + local single + if advtrains.yawToDirection(yaw, conn1, advtrains.oppd(conn1)) == conn1 then + single = g.single1 + else + single = g.single2 + end + local node = single[conn1] + if node then + check_or_bend_rail(pos, conn1, pname, true) + track_place_node(pos, node) -- calls after_place_node implicitly + return true end end - --use default - minetest.set_node(pos, {name=nnpref.."_"..tr.default}) - if minetest.registered_nodes[nnpref.."_"..tr.default] and minetest.registered_nodes[nnpref.."_"..tr.default].after_place_node then - minetest.registered_nodes[nnpref.."_"..tr.default].after_place_node(pos, placer, itemstack, pointed_thing) - end + -- 4. if nothing worked, set the default + local node = g.default + track_place_node(pos, node) -- calls after_place_node implicitly + return true end - -function tp.register_track_placer(nnprefix, imgprefix, dispname, def) - minetest.register_craftitem(":"..nnprefix.."_placer",{ - description = dispname, - inventory_image = imgprefix.."_placer.png", - wield_image = imgprefix.."_placer.png", - groups={advtrains_trackplacer=1, digtron_on_place=1}, - liquids_pointable = def.liquids_pointable, - on_place = function(itemstack, placer, pointed_thing) - local name = placer:get_player_name() - if not name then - return itemstack, false - end - if pointed_thing.type=="node" then - local pos=pointed_thing.above - local upos=vector.subtract(pointed_thing.above, {x=0, y=1, z=0}) - if not advtrains.check_track_protection(pos, name) then - return itemstack, false - end - if minetest.registered_nodes[minetest.get_node(pos).name] and minetest.registered_nodes[minetest.get_node(pos).name].buildable_to then - local s - if def.suitable_substrate then - s = def.suitable_substrate(upos) - else - s = minetest.registered_nodes[minetest.get_node(upos).name] and minetest.registered_nodes[minetest.get_node(upos).name].walkable - end - if s then --- minetest.chat_send_all(nnprefix) - local yaw = placer:get_look_horizontal() - tp.placetrack(pos, nnprefix, placer, itemstack, pointed_thing, yaw) - if not advtrains.is_creative(name) then - itemstack:take_item() - end - end - end - end - return itemstack, true - end, - }) -end - - +-- TRACK WORKER -- minetest.register_craftitem("advtrains:trackworker",{ description = attrans("Track Worker Tool\n\nLeft-click: change rail type (straight/curve/switch)\nRight-click: rotate rail/bumper/signal/etc."), @@ -316,116 +209,93 @@ minetest.register_craftitem("advtrains:trackworker",{ wield_image = "advtrains_trackworker.png", stack_max = 1, on_place = function(itemstack, placer, pointed_thing) - local name = placer:get_player_name() - if not name then + local name = placer:get_player_name() + if not name then + return + end + local has_aux1_down = placer:get_player_control().aux1 + if pointed_thing.type=="node" then + local pos=pointed_thing.under + if not advtrains.check_track_protection(pos, name) then return end - local has_aux1_down = placer:get_player_control().aux1 - if pointed_thing.type=="node" then - local pos=pointed_thing.under - if not advtrains.check_track_protection(pos, name) then - return - end - local node=minetest.get_node(pos) + local node=minetest.get_node(pos) - --if not advtrains.is_track_and_drives_on(minetest.get_node(pos).name, advtrains.all_tracktypes) then return end - - local nnprefix, suffix, rotation=string.match(node.name, "^(.+)_([^_]+)(_[^_]+)$") - --atdebug(node.name.."\npattern recognizes:"..nnprefix.." / "..suffix.." / "..rotation) - --atdebug("nntab: ",tp.tracks[nnprefix]) - if not tp.tracks[nnprefix] or not tp.tracks[nnprefix].twrotate[suffix] then - nnprefix, suffix=string.match(node.name, "^(.+)_([^_]+)$") - rotation = "" - if not tp.tracks[nnprefix] or not tp.tracks[nnprefix].twrotate[suffix] then - minetest.chat_send_player(placer:get_player_name(), attrans("This node can't be rotated using the trackworker!")) - return - end - end - - -- check if the node is modify-protected - if advtrains.is_track_and_drives_on(minetest.get_node(pos).name, advtrains.all_tracktypes) then - -- is a track, we can query - local can_modify, reason = advtrains.can_dig_or_modify_track(pos) - if not can_modify then - local str = attrans("This track can not be rotated!") - if reason then - str = str .. " " .. reason - end - minetest.chat_send_player(placer:get_player_name(), str) - return + -- New since 2.5: only the fields in the node definition are considered, no more hacky pattern matching on the nodename + + local ndef = minetest.registered_nodes[node.name] + + if not ndef.advtrains or not ndef.advtrains.trackworker_next_rot then + minetest.chat_send_player(placer:get_player_name(), attrans("This node can't be rotated using the trackworker!")) + return + end + + -- check if the node is modify-protected + if advtrains.is_track(node.name) then + -- is a track, we can query + local can_modify, reason = advtrains.can_dig_or_modify_track(pos) + if not can_modify then + local str = attrans("This track can not be rotated!") + if reason then + str = str .. " " .. reason end - end - - if has_aux1_down then - --feature: flip the node by 180° - --i've always wanted this! - advtrains.ndb.swap_node(pos, {name=node.name, param2=(node.param2+2)%4}) + minetest.chat_send_player(placer:get_player_name(), str) return end - - local modext=tp.tracks[nnprefix].twrotate[suffix] - - if rotation==modext[#modext] then --increase param2 - advtrains.ndb.swap_node(pos, {name=nnprefix.."_"..suffix..modext[1], param2=(node.param2+1)%4}) - return - else - local modpos - for k,v in pairs(modext) do - if v==rotation then modpos=k end - end - if not modpos then - minetest.chat_send_player(placer:get_player_name(), attrans("This node can't be rotated using the trackworker!")) - return - end - advtrains.ndb.swap_node(pos, {name=nnprefix.."_"..suffix..modext[modpos+1], param2=node.param2}) - end end + + if has_aux1_down then + --feature: flip the node by 180° + --i've always wanted this! + advtrains.ndb.swap_node(pos, {name=node.name, param2=(node.param2+2)%4}) + return + end + + local new_node = {name = ndef.advtrains.trackworker_next_rot, param2 = node.param2} + if ndef.advtrains.trackworker_rot_incr_param2 then + new_node.param2 = ((node.param2 + 1) % 4) + end + advtrains.ndb.swap_node(pos, new_node) + end end, on_use=function(itemstack, user, pointed_thing) - local name = user:get_player_name() - if not name then - return + local name = user:get_player_name() + if not name then + return + end + if pointed_thing.type=="node" then + local pos=pointed_thing.under + local node=minetest.get_node(pos) + if not advtrains.check_track_protection(pos, name) then + return end - if pointed_thing.type=="node" then - local pos=pointed_thing.under - local node=minetest.get_node(pos) - if not advtrains.check_track_protection(pos, name) then - return - end - - --if not advtrains.is_track_and_drives_on(minetest.get_node(pos).name, advtrains.all_tracktypes) then return end - if advtrains.get_train_at_pos(pos) then return end - local nnprefix, suffix, rotation=string.match(node.name, "^(.+)_([^_]+)(_[^_]+)$") - --atdebug(node.name.."\npattern recognizes:"..nodeprefix.." / "..railtype.." / "..rotation) - if not tp.tracks[nnprefix] or not tp.tracks[nnprefix].twcycle[suffix] then - nnprefix, suffix=string.match(node.name, "^(.+)_([^_]+)$") - rotation = "" - if not tp.tracks[nnprefix] or not tp.tracks[nnprefix].twcycle[suffix] then - minetest.chat_send_player(user:get_player_name(), attrans("This node can't be changed using the trackworker!")) - return - end - end - - -- check if the node is modify-protected - if advtrains.is_track_and_drives_on(minetest.get_node(pos).name, advtrains.all_tracktypes) then - -- is a track, we can query - local can_modify, reason = advtrains.can_dig_or_modify_track(pos) - if not can_modify then - local str = attrans("This track can not be changed!") - if reason then - str = str .. " " .. reason - end - minetest.chat_send_player(user:get_player_name(), str) - return + + -- New since 2.5: only the fields in the node definition are considered, no more hacky pattern matching on the nodename + + local ndef = minetest.registered_nodes[node.name] + + if not ndef.advtrains or not ndef.advtrains.trackworker_next_var then + minetest.chat_send_player(placer:get_player_name(), attrans("This node can't be changed using the trackworker!")) + return + end + + -- check if the node is modify-protected + if advtrains.is_track(node.name) then + -- is a track, we can query + local can_modify, reason = advtrains.can_dig_or_modify_track(pos) + if not can_modify then + local str = attrans("This track can not be rotated!") + if reason then + str = str .. " " .. reason end + minetest.chat_send_player(placer:get_player_name(), str) + return end - - local nextsuffix=tp.tracks[nnprefix].twcycle[suffix] - advtrains.ndb.swap_node(pos, {name=nnprefix.."_"..nextsuffix..rotation, param2=node.param2}) - - else - atprint(name, dump(tp.tracks)) end + + local new_node = {name = ndef.advtrains.trackworker_next_var, param2 = node.param2} + advtrains.ndb.swap_node(pos, new_node) + end end, }) diff --git a/advtrains/tracks.lua b/advtrains/tracks.lua index c415143..dc2d909 100644 --- a/advtrains/tracks.lua +++ b/advtrains/tracks.lua @@ -1,751 +1,287 @@ ---advtrains by orwell96, see readme.txt - ---dev-time settings: ---EDIT HERE ---If the old non-model rails on straight tracks should be replaced by the new... ---false: no ---true: yes -advtrains.register_replacement_lbms=false - ---[[TracksDefinition -nodename_prefix -texture_prefix -description -common={} -straight={} -straight45={} -curve={} -curve45={} -lswitchst={} -lswitchst45={} -rswitchst={} -rswitchst45={} -lswitchcr={} -lswitchcr45={} -rswitchcr={} -rswitchcr45={} -vert1={ - --you'll probably want to override mesh here -} -vert2={ - --you'll probably want to override mesh here -} -]]-- -advtrains.all_tracktypes={} - ---definition preparation -local function conns(c1, c2, r1, r2) return {{c=c1, y=r1}, {c=c2, y=r2}} end -local function conns3(c1, c2, c3, r1, r2, r3) return {{c=c1, y=r1}, {c=c2, y=r2}, {c=c3, y=r3}} end - -advtrains.ap={} -advtrains.ap.t_30deg_flat={ - regstep=1, - variant={ - st={ - conns = conns(0,8), - desc = "straight", - tpdouble = true, - tpsingle = true, - trackworker = "cr", - }, - cr={ - conns = conns(0,7), - desc = "curve", - tpdouble = true, - trackworker = "swlst", - }, - swlst={ - conns = conns3(0,8,7), - desc = "left switch (straight)", - trackworker = "swrst", - switchalt = "cr", - switchmc = "on", - switchst = "st", - switchprefix = "swl", - }, - swlcr={ - conns = conns3(0,7,8), - desc = "left switch (curve)", - trackworker = "swrcr", - switchalt = "st", - switchmc = "off", - switchst = "cr", - switchprefix = "swl", - }, - swrst={ - conns = conns3(0,8,9), - desc = "right switch (straight)", - trackworker = "st", - switchalt = "cr", - switchmc = "on", - switchst = "st", - switchprefix = "swr", - }, - swrcr={ - conns = conns3(0,9,8), - desc = "right switch (curve)", - trackworker = "st", - switchalt = "st", - switchmc = "off", - switchst = "cr", - switchprefix = "swr", - }, - }, - regtp=true, - tpdefault="st", - trackworker={ - ["swrcr"]="st", - ["swrst"]="st", - ["cr"]="swlst", - ["swlcr"]="swrcr", - ["swlst"]="swrst", - }, - rotation={"", "_30", "_45", "_60"}, -} -advtrains.ap.t_yturnout={ - regstep=1, - variant={ - l={ - conns = conns3(0,7,9), - desc = "Y-turnout (left)", - switchalt = "r", - switchmc = "off", - switchst = "l", - switchprefix = "", - }, - r={ - conns = conns3(0,9,7), - desc = "Y-turnout (right)", - switchalt = "l", - switchmc = "on", - switchst = "r", - switchprefix = "", - } - }, - regtp=true, - tpdefault="l", - rotation={"", "_30", "_45", "_60"}, -} -advtrains.ap.t_s3way={ - regstep=1, - variant={ - l={ - conns = { {c=0}, {c=7}, {c=8}, {c=9}, {c=0} }, - desc = "3-way turnout (left)", - switchalt = "s", - switchst="l", - switchprefix = "", - }, - s={ - conns = { {c=0}, {c=8}, {c=7}, {c=9}, {c=0} }, - desc = "3-way turnout (straight)", - switchalt ="r", - switchst = "s", - switchprefix = "", - }, - r={ - conns = { {c=0}, {c=9}, {c=8}, {c=7}, {c=0} }, - desc = "3-way turnout (right)", - switchalt = "l", - switchst="r", - switchprefix = "", - } - }, - regtp=true, - tpdefault="l", - rotation={"", "_30", "_45", "_60"}, -} -advtrains.ap.t_30deg_slope={ - regstep=1, - variant={ - vst1={conns = conns(8,0,0,0.5), rail_y = 0.25, desc = "steep uphill 1/2", slope=true}, - vst2={conns = conns(8,0,0.5,1), rail_y = 0.75, desc = "steep uphill 2/2", slope=true}, - vst31={conns = conns(8,0,0,0.33), rail_y = 0.16, desc = "uphill 1/3", slope=true}, - vst32={conns = conns(8,0,0.33,0.66), rail_y = 0.5, desc = "uphill 2/3", slope=true}, - vst33={conns = conns(8,0,0.66,1), rail_y = 0.83, desc = "uphill 3/3", slope=true}, - }, - regsp=true, - slopeplacer={ - [2]={"vst1", "vst2"}, - [3]={"vst31", "vst32", "vst33"}, - max=3,--highest entry - }, - slopeplacer_45={ - [2]={"vst1_45", "vst2_45"}, - max=2, - }, - rotation={"", "_30", "_45", "_60"}, - trackworker={}, - increativeinv={}, -} -advtrains.ap.t_30deg_straightonly={ - regstep=1, - variant={ - st={ - conns = conns(0,8), - desc = "straight", - tpdouble = true, - tpsingle = true, - trackworker = "st", - }, - }, - regtp=true, - tpdefault="st", - rotation={"", "_30", "_45", "_60"}, -} -advtrains.ap.t_30deg_straightonly_noplacer={ - regstep=1, - variant={ - st={ - conns = conns(0,8), - desc = "straight", - tpdouble = true, - tpsingle = true, - trackworker = "st", - }, - }, - tpdefault="st", - rotation={"", "_30", "_45", "_60"}, -} -advtrains.ap.t_45deg={ - regstep=2, - variant={ - st={ - conns = conns(0,8), - desc = "straight", - tpdouble = true, - tpsingle = true, - trackworker = "cr", - }, - cr={ - conns = conns(0,6), - desc = "curve", - tpdouble = true, - trackworker = "swlst", - }, - swlst={ - conns = conns3(0,8,6), - desc = "left switch (straight)", - trackworker = "swrst", - switchalt = "cr", - switchmc = "on", - switchst = "st", - }, - swlcr={ - conns = conns3(0,6,8), - desc = "left switch (curve)", - trackworker = "swrcr", - switchalt = "st", - switchmc = "off", - switchst = "cr", - }, - swrst={ - conns = conns3(0,8,10), - desc = "right switch (straight)", - trackworker = "st", - switchalt = "cr", - switchmc = "on", - switchst = "st", - }, - swrcr={ - conns = conns3(0,10,8), - desc = "right switch (curve)", - trackworker = "st", - switchalt = "st", - switchmc = "off", - switchst = "cr", - }, - }, - regtp=true, - tpdefault="st", - trackworker={ - ["swrcr"]="st", - ["swrst"]="st", - ["cr"]="swlst", - ["swlcr"]="swrcr", - ["swlst"]="swrst", - }, - rotation={"", "_30", "_45", "_60"}, -} -advtrains.ap.t_perpcrossing={ - regstep = 1, - variant={ - st={ - conns = { {c=0}, {c=8}, {c=4}, {c=12} }, - desc = "perpendicular crossing", - tpdouble = true, - tpsingle = true, - trackworker = "st", - }, - }, - regtp=true, - tpdefault="st", - rotation={"", "_30", "_45", "_60"}, -} -advtrains.ap.t_90plusx_crossing={ - regstep = 1, - variant={ - ["30l"]={ - conns = { {c=0}, {c=8}, {c=1}, {c=9} }, - desc = "30/90 degree crossing (left)", - tpdouble = true, - tpsingle = true, - trackworker = "45l" - }, - ["45l"]={ - conns = { {c=0}, {c=8}, {c=2}, {c=10} }, - desc = "45/90 degree crossing (left)", - tpdouble = true, - tpsingle = true, - trackworker = "60l", - }, - ["60l"]={ - conns = { {c=0}, {c=8}, {c=3}, {c=11}}, - desc = "60/90 degree crossing (left)", - tpdouble = true, - tpsingle = true, - trackworker = "60r", - }, - ["60r"]={ - conns = { {c=0}, {c=8}, {c=5}, {c=13} }, - desc = "60/90 degree crossing (right)", - tpdouble = true, - tpsingle = true, - trackworker = "45r" - }, - ["45r"]={ - conns = { {c=0}, {c=8}, {c=6}, {c=14} }, - desc = "45/90 degree crossing (right)", - tpdouble = true, - tpsingle = true, - trackworker = "30r", - }, - ["30r"]={ - conns = { {c=0}, {c=8}, {c=7}, {c=15}}, - desc = "30/90 degree crossing (right)", - tpdouble = true, - tpsingle = true, - trackworker = "30l", - }, - }, - regtp=true, - tpdefault="30l", - rotation={""}, - trackworker = { - ["30l"] = "45l", - ["45l"] = "60l", - ["60l"] = "60r", - ["60r"] = "45r", - ["45r"] = "30r", - ["30r"] = "30l", - } -} - -advtrains.ap.t_diagonalcrossing = { - regstep=1, - variant={ - ["30l45r"]={ - conns = {{c=1}, {c=9}, {c=6}, {c=14}}, - desc = "30left-45right diagonal crossing", - tpdouble=true, - tpsingle=true, - trackworker="60l30l", - }, - ["60l30l"]={ - conns = {{c=3}, {c=11}, {c=1}, {c=9}}, - desc = "30left-60right diagonal crossing", - tpdouble=true, - tpsingle=true, - trackworker="60l45r" - }, - ["60l45r"]={ - conns = {{c=3}, {c=11}, {c=6}, {c=14}}, - desc = "60left-45right diagonal crossing", - tpdouble=true, - tpsingle=true, - trackworker="60l60r" - }, - ["60l60r"]={ - conns = {{c=3}, {c=11}, {c=5}, {c=13}}, - desc = "60left-60right diagonal crossing", - tpdouble=true, - tpsingle=true, - trackworker="60r45l", - }, - --If 60l60r had a mirror image, it would be here, but it's symmetric. - -- 60l60r is also equivalent to 30l30r but rotated 90 degrees. - ["60r45l"]={ - conns = {{c=5}, {c=13}, {c=2}, {c=10}}, - desc = "60right-45left diagonal crossing", - tpdouble=true, - tpsingle=true, - trackworker="60r30r", - }, - ["60r30r"]={ - conns = {{c=5}, {c=13}, {c=7}, {c=15}}, - desc = "60right-30right diagonal crossing", - tpdouble=true, - tpsingle=true, - trackworker="30r45l", - }, - ["30r45l"]={ - conns = {{c=7}, {c=15}, {c=2}, {c=10}}, - desc = "30right-45left diagonal crossing", - tpdouble=true, - tpsingle=true, - trackworker="30l45r", - }, - - }, - regtp=true, - tpdefault="30l45r", - rotation={""}, - trackworker = { - ["30l45r"] = "60l30l", - ["60l30l"] = "60l45r", - ["60l45r"] = "60l60r", - ["60l60r"] = "60r45l", - ["60r45l"] = "60r30r", - ["60r30r"] = "30r45l", - ["30r45l"] = "30l45r", - } -} - -advtrains.trackpresets = advtrains.ap - ---definition format: ([] optional) ---[[{ - nodename_prefix - texture_prefix - [shared_texture] - models_prefix - models_suffix (with dot) - [shared_model] - formats={ - st,cr,swlst,swlcr,swrst,swrcr,vst1,vst2 - (each a table with indices 0-3, for if to register a rail with this 'rotation' table entry. nil is assumed as 'all', set {} to not register at all) - } - common={} change something on common rail appearance -} -[18.12.17] Note on new connection system: -In order to support real rail crossing nodes and finally make the trackplacer respect switches, I changed the connection system. -There can be a variable number of connections available. These are specified as tuples {c=, y=} -The table "at_conns" consists of {, ...} -the "at_rail_y" property holds the value that was previously called "railheight" -Depending on the number of connections: -2 conns: regular rail -3 conns: switch: - - when train passes in at conn1, will move out of conn2 - - when train passes in at conn2 or conn3, will move out of conn1 -4 conns: cross (or cross switch, depending on arrangement of conns): - - conn1 <> conn2 - - conn3 <> conn4 -]] - --- Notify the user if digging the rail is not allowed -local function can_dig_callback(pos, player) - local ok, reason = advtrains.can_dig_or_modify_track(pos) - if not ok and player then - minetest.chat_send_player(player:get_player_name(), attrans("This track can not be removed!") .. " " .. reason) - end - return ok -end - -function advtrains.register_tracks(tracktype, def, preset) - advtrains.trackplacer.register_tracktype(def.nodename_prefix, preset.tpdefault) - if preset.regtp then - advtrains.trackplacer.register_track_placer(def.nodename_prefix, def.texture_prefix, def.description, def) - end - if preset.regsp then - advtrains.slope.register_placer(def, preset) - end - for suffix, var in pairs(preset.variant) do - for rotid, rotation in ipairs(preset.rotation) do - if not def.formats[suffix] or def.formats[suffix][rotid] then - local img_suffix = suffix..rotation - local ndef = advtrains.merge_tables({ - description=def.description.."("..(var.desc or "any")..rotation..")", - drawtype = "mesh", - paramtype="light", - paramtype2="facedir", - walkable = false, - selection_box = { - type = "fixed", - fixed = {-1/2-1/16, -1/2, -1/2, 1/2+1/16, -1/2+2/16, 1/2}, - }, - - mesh = def.shared_model or (def.models_prefix.."_"..img_suffix..def.models_suffix), - tiles = {def.shared_texture or (def.texture_prefix.."_"..img_suffix..".png"), def.second_texture}, - - groups = { - attached_node = advtrains.IGNORE_WORLD and 0 or 1, - advtrains_track=1, - ["advtrains_track_"..tracktype]=1, - save_in_at_nodedb=1, - dig_immediate=2, - not_in_creative_inventory=1, - not_blocking_trains=1, - }, - - can_dig = can_dig_callback, - after_dig_node=function(pos) - advtrains.ndb.update(pos) - end, - after_place_node=function(pos) - advtrains.ndb.update(pos) - end, - at_nnpref = def.nodename_prefix, - at_suffix = suffix, - at_rotation = rotation, - at_rail_y = var.rail_y - }, def.common or {}) - - if preset.regtp then - ndef.drop = def.nodename_prefix.."_placer" - end - if preset.regsp and var.slope then - ndef.drop = def.nodename_prefix.."_slopeplacer" - end - - --connections - ndef.at_conns = advtrains.rotate_conn_by(var.conns, (rotid-1)*preset.regstep) - - local ndef_avt_table - - if var.switchalt and var.switchst then - local switchfunc=function(pos, node, newstate) - newstate = newstate or var.switchalt -- support for 3 (or more) state switches - -- this code is only called from the internal setstate function, which - -- ensures that it is safe to switch the turnout - if newstate~=var.switchst then - advtrains.ndb.swap_node(pos, {name=def.nodename_prefix.."_"..(var.switchprefix or "")..newstate..rotation, param2=node.param2}) - advtrains.invalidate_all_paths(pos) - end - end - ndef.on_rightclick = function(pos, node, player) - if advtrains.check_turnout_signal_protection(pos, player:get_player_name()) then - advtrains.setstate(pos, nil, node) - advtrains.log("Switch", player:get_player_name(), pos) - end - end - if var.switchmc then - ndef.mesecons = {effector = { - ["action_"..var.switchmc] = function(pos, node) - advtrains.setstate(pos, nil, node) - end, - rules=advtrains.meseconrules - }} - end - ndef_avt_table = { - getstate = var.switchst, - setstate = switchfunc, - } - end - - local adef={} - if def.get_additional_definiton then - adef=def.get_additional_definiton(def, preset, suffix, rotation) - end - ndef = advtrains.merge_tables(ndef, adef) - - -- insert getstate/setstate functions after merging the additional definitions - if ndef_avt_table then - ndef.advtrains = advtrains.merge_tables(ndef.advtrains or {}, ndef_avt_table) - end - - minetest.register_node(":"..def.nodename_prefix.."_"..suffix..rotation, ndef) - --trackplacer - if preset.regtp then - local tpconns = {conn1=ndef.at_conns[1].c, conn2=ndef.at_conns[2].c} - if var.tpdouble then - advtrains.trackplacer.add_double_conn(def.nodename_prefix, suffix, rotation, tpconns) - end - if var.tpsingle then - advtrains.trackplacer.add_single_conn(def.nodename_prefix, suffix, rotation, tpconns) - end - end - advtrains.trackplacer.add_worked(def.nodename_prefix, suffix, rotation, var.trackworker) - end - end - end - advtrains.all_tracktypes[tracktype]=true -end - -function advtrains.is_track_and_drives_on(nodename, drives_on_p) - local drives_on = drives_on_p - if not drives_on then drives_on = advtrains.all_tracktypes end - local hasentry = false - for _,_ in pairs(drives_on) do - hasentry=true - end - if not hasentry then drives_on = advtrains.all_tracktypes end - - if not minetest.registered_nodes[nodename] then - return false - end - local nodedef=minetest.registered_nodes[nodename] - for k,v in pairs(drives_on) do - if nodedef.groups["advtrains_track_"..k] then - return true - end - end - return false -end - -function advtrains.get_track_connections(name, param2) - local nodedef=minetest.registered_nodes[name] - if not nodedef then atprint(" get_track_connections couldn't find nodedef for nodename "..(name or "nil")) return nil end - local noderot=param2 - if not param2 then noderot=0 end - if noderot > 3 then atprint(" get_track_connections: rail has invaild param2 of "..noderot) noderot=0 end - - local tracktype - for k,_ in pairs(nodedef.groups) do - local tt=string.match(k, "^advtrains_track_(.+)$") - if tt then - tracktype=tt - end - end - return advtrains.rotate_conn_by(nodedef.at_conns, noderot*AT_CMAX/4), (nodedef.at_rail_y or 0), tracktype -end - --- Function called when a track is about to be dug or modified by the trackworker --- Returns either true (ok) or false,"translated string describing reason why it isn't allowed" -function advtrains.can_dig_or_modify_track(pos) - if advtrains.get_train_at_pos(pos) then - return false, attrans("Position is occupied by a train.") - end - -- interlocking: tcb, signal IP a.s.o. - if advtrains.interlocking then - -- TCB? - if advtrains.interlocking.db.get_tcb(pos) then - return false, attrans("There's a Track Circuit Break here.") - end - -- signal ip? - if advtrains.interlocking.db.is_ip_at(pos, true) then -- is_ip_at with purge parameter - return false, attrans("There's a Signal Influence Point here.") - end - end - return true -end - --- slope placer. Defined in register_tracks. ---crafted with rail and gravel -local sl={} -function sl.register_placer(def, preset) - minetest.register_craftitem(":"..def.nodename_prefix.."_slopeplacer",{ - description = attrans("@1 Slope", def.description), - inventory_image = def.texture_prefix.."_slopeplacer.png", - wield_image = def.texture_prefix.."_slopeplacer.png", - groups={}, - on_place = sl.create_slopeplacer_on_place(def, preset) - }) -end ---(itemstack, placer, pointed_thing) -function sl.create_slopeplacer_on_place(def, preset) - return function(istack, player, pt) - if not pt.type=="node" then - minetest.chat_send_player(player:get_player_name(), attrans("Can't place: not pointing at node")) - return istack - end - local pos=pt.above - if not pos then - minetest.chat_send_player(player:get_player_name(), attrans("Can't place: not pointing at node")) - return istack - end - local node=minetest.get_node(pos) - if not minetest.registered_nodes[node.name] or not minetest.registered_nodes[node.name].buildable_to then - minetest.chat_send_player(player:get_player_name(), attrans("Can't place: space occupied!")) - return istack - end - if not advtrains.check_track_protection(pos, player:get_player_name()) then - minetest.record_protection_violation(pos, player:get_player_name()) - return istack - end - --determine player orientation (only horizontal component) - --get_look_horizontal may not be available - local yaw=player.get_look_horizontal and player:get_look_horizontal() or (player:get_look_yaw() - math.pi/2) - - --rounding unit vectors is a nice way for selecting 1 of 8 directions since sin(30°) is 0.5. - local dirvec={x=math.floor(math.sin(-yaw)+0.5), y=0, z=math.floor(math.cos(-yaw)+0.5)} - --translate to direction to look up inside the preset table - local param2, rot45=({ - [-1]={ - [-1]=2, - [0]=3, - [1]=3, - }, - [0]={ - [-1]=2, - [1]=0, - }, - [1]={ - [-1]=1, - [0]=1, - [1]=0, - }, - })[dirvec.x][dirvec.z], dirvec.x~=0 and dirvec.z~=0 - local lookup=preset.slopeplacer - if rot45 then lookup=preset.slopeplacer_45 end - - --go unitvector forward and look how far the next node is - local step=1 - while step<=lookup.max do - local node=minetest.get_node(vector.add(pos, dirvec)) - --next node solid? - if not minetest.registered_nodes[node.name] or not minetest.registered_nodes[node.name].buildable_to or advtrains.is_protected(pos, player:get_player_name()) then - --do slopes of this distance exist? - if lookup[step] then - if minetest.settings:get_bool("creative_mode") or istack:get_count()>=step then - --start placing - local placenodes=lookup[step] - while step>0 do - minetest.set_node(pos, {name=def.nodename_prefix.."_"..placenodes[step], param2=param2}) - if not minetest.settings:get_bool("creative_mode") then - istack:take_item() - end - step=step-1 - pos=vector.subtract(pos, dirvec) - end - else - minetest.chat_send_player(player:get_player_name(), attrans("Can't place: Not enough slope items left (@1 required)", step)) - end - else - minetest.chat_send_player(player:get_player_name(), attrans("Can't place: There's no slope of length @1",step)) - end - return istack - end - step=step+1 - pos=vector.add(pos, dirvec) - end - minetest.chat_send_player(player:get_player_name(), attrans("Can't place: no supporting node at upper end.")) - return itemstack - end -end - -advtrains.slope=sl - ---END code, BEGIN definition ---definition format: ([] optional) ---[[{ - nodename_prefix - texture_prefix - [shared_texture] - models_prefix - models_suffix (with dot) - [shared_model] - formats={ - st,cr,swlst,swlcr,swrst,swrcr,vst1,vst2 - (each a table with indices 0-3, for if to register a rail with this 'rotation' table entry. nil is assumed as 'all', set {} to not register at all) - } - common={} change something on common rail appearance -}]] - - - - - - - - - +-- tracks.lua +-- rewritten with advtrains 2.5 according to new track registration system + + +--[[ + +Tracks in advtrains are defined by the node definition. They must have at least 2 connections, but can have any number. +Switchable nodes (turnouts, single/double-slip switches) are implemented by having a separate node (node name) for each of the possible states. + + minetest.register_node(nodename, { + ... usual node definition ... + groups = { + advtrains_track = 1, + advtrains_track_=1 + ^- these groups tell that the node is a track + not_blocking_trains=1, + ^- this group tells that the node should not block trains although it's walkable. + }, + + at_rail_y = 0, + ^- Height of this rail node (the y position of a wagon that stands centered on this rail) + at_conns = { + [1] = { c=0..15, y=0..1 }, + [2] = { c=0..15, y=0..1 }, + ( [3] = { c=0..15, y=0..1 }, ) + ( [4] = { c=0..15, y=0..1 }, ) + ( ... ) + } + ^- Connections of this rail. There are two general cases: + a) SIMPLE TRACK - the track has exactly 2 connections, and does not feature a turnout, crossing or other contraption + For simple tracks, except for the at_conns table no further setup needs to be specified. A train entering on conn 1 will go out at conn 2 and vice versa. + A track with only one connection defined is not permitted. + b) COMPOUND TRACK - the track has more than 2 connections + This will be used for turnouts and crossings. Tracks with more than 2 conns MUST define 'at_conn_map'. + Switchable nodes, whose state can be changed (e.g. turnouts) MUST define a 'state_map' within the advtrains table as well. + This differs from the behavior up until 2.4.2, where the conn mapping was fixed. + ^- Connection definition: + - c is the direction of the connection (0-16). For the mapping to world directions see helpers.lua. + - Connections will be auto-rotated with param2 of the node (horizontal, param2 values 0-3 only) + - y is the height of the connection (rail will only connect when this matches) + ^- The index of a connection inside the conns table (1, 2, 3, ...) is referred throughout advtrains code as 'connid' + ^- IMPORTANT: For switchable nodes (any kind of turnout), it is crucial that for all of the node's variants the at_conns table stays the same. See below. + + at_conn_map = { + [1] = 2, + [2] = 1, + [3] = 1, + } + ^- Connection map of this rail. It specifies when a train enters the track on connid X, on which connid it will leave + This field MUST be specified when the number of connections in at_conns is greater than 2 + This field may, and obviously will, vary between nodes for switchable nodes. + + can_dig = advtrains.track_can_dig_callback + after_dig_node = advtrains.track_update_callback + after_place_node = advtrains.track_update_callback + ^- the code in these 3 default minetest API functions is required for advtrains to work, however you can add your own code + + on_rightclick = advtrains.state_node_on_rightclick_callback + ^- Must be added if the node is a turnout and if it should be switched by right-click. It will cause the turnout to be switched to next_state. + + advtrains = { + on_train_enter=function(pos, train_id, train, index) end + ^- called when a train enters the rail + on_train_leave=function(pos, train_id, train, index) end + ^- called when a train leaves the rail + + -- The following function is only in effect when interlocking is enabled: + on_train_approach = function(pos, train_id, train, index, has_entered, lzbdata) + ^- called when a train is approaching this position, called exactly once for every path recalculation (which can happen at any time) + ^- This is called so that if the train would start braking now, it would come to halt about(wide approx) 5 nodes before the rail. + ^- has_entered: when true, the train is already standing on this node with its front tip, and the enter callback has already been called. + Possibly, some actions need not to be taken in this case. Only set if it's the very first node the train is standing on. + ^- lzbdata should be ignored and nothing should be assigned to it + + -- The following information is required if the node is a turnout (e.g. can be switched into different positions) + node_state = "st" + ^- The name of the state this node represents + ^- Conventions for this field are as follows: + - Two-way straight/turn switches: 'st'=straight branch, 'cr'=diverting/turn branch + - 3-way turnouts, Y-turnouts: 'l'=left branch, 's'=straight branch, 'r'=right branch + + node_next_state = "cr" + ^- The name of the state that the turnout should be switched to when it is right-clicked + + node_fallback_state = "st" + ^- The name of the state that the turnout should "fall back" to when it is released + Only used by the interlocking system, when a route on the node is released it is switched back to this state. + + node_state_map = { + ["st"] = "", + ["cr"] = "", + ... etc ... + } + ^- Map of state name to the appropriate node name that should be set by advtrains when a switch is requested + Note that for all of those nodes, the at_conns table must be identical (however the conn_map will vary) + + node_on_switch_state = function(pos, node, oldstate, newstate) + ^- Called when the node state is switched by advtrains, after the node replacement has commenced. + + Turnout switching can happen programmatically via advtrains.setstate(pos, state), via user right_click or via the interlocking system. + In no other situation is it permissible to exchange track nodes in-place, unless both at_conns and at_conn_map stay identical. + + Note that the fields node_state, node_next_state and node_state_map completely replace the getstate/setstate functions. + There must be a one-to-one mapping between states and node names and no function can be defined for state switching. + This principle enables the seamless working of the interlocking autorouter and reduces failure points. + The node_state_* system can also be used as drop-in replacement for the passive-API-enabled nodes (andrews-cross, mesecon_switch etc.) + The advtrains API functions advtrains.getstate() and advtrains.setstate() remain the programmatic access points, but will now utilize the new state system. + + + trackworker_next_rot = , + ^- if set, right-click with trackworker will set this node + trackworker_rot_incr_param2 = true + ^- if set, trackworker will increase node param2 on rightclick + + trackworker_next_var = + ^- if set, left-click with trackworker will set this node + } + }) + +]]-- + +-- This file provides some utilities to register tracks, but tries to not get into the way too much + + +function advtrains.track_can_dig_callback(pos, player) + local ok, reason = advtrains.can_dig_or_modify_track(pos) + if not ok and player then + minetest.chat_send_player(player:get_player_name(), attrans("This track can not be removed!") .. " " .. reason) + end + return ok +end + +function advtrains.track_update_callback(pos) + advtrains.ndb.update(pos) +end + +function advtrains.state_node_on_rightclick_callback(pos, node, player) + if advtrains.check_turnout_signal_protection(pos, player:get_player_name()) then + local ndef = minetest.registered_nodes[node.name] + if ndef and ndef.advtrains and ndef.advtrains.node_next_state then + advtrains.setstate(pos, ndef.advtrains.node_next_state, node) + advtrains.log("Switch", player:get_player_name(), pos) + end + end +end + +-- advtrains.register_node_4rot(name, nodedef) +-- Registers four rotations for the node defined by nodedef (0°, 30°, 45° and 60°; the 4 90°-steps are already handled by the param2, resulting in 16 directions total). +-- You must provide the definition for the base node, and certain fields are altered automatically for the 3 additional rotations: +-- name: appends the suffix "_30", "_45" or "_60" +-- description: appends the rotation (human-readable) in parenthesis +-- tiles_prefix: if defined, "tiles" field will be set as prefix..rotationExtension..".png" +-- mesh_prefix, mesh_suffix: if defined, "mesh" field will be set as prefix..rotationExtension..suffix +-- at_conns: are rotated according to the node rotation +-- node_state_map, trackworker_next_var: appends the suffix appropriately. +-- groups: applies save_in_at_nodedb and not_blocking_trains groups if not already present +-- The nodes are registered in the trackworker to be rotated with right-click. +-- definition_mangling_function is an optional parameter. For each of the 4 rotations, it gets passed the modified node definition and may perform final modifications to it. +-- signature: function definition_mangling_function(name, nodedef, rotationIndex, rotationSuffix) +-- Example usage: define the setstate function of turnouts (if that is not done via the "automatic" way of state_node_map) +local rotations = { + {i = 0, s = "", h = " (0)", n = "_30"}, + {i = 1, s = "_30", h = " (30)", n = "_45"}, + {i = 2, s = "_45", h = " (45)", n = "_60"}, + {i = 3, s = "_60", h = " (60)", n = ""}, +} +function advtrains.register_node_4rot(ori_name, ori_ndef, definition_mangling_function) + for _, rot in ipairs(rotations) do + local ndef = table.copy(ori_ndef) + if ori_ndef.advtrains then + -- make sure advtrains table is deep-copied because we may need to replace node_state_map + ndef.advtrains = table.copy(ori_ndef.advtrains) + else + ndef.advtrains = {} -- we need the table later for trackworker + end + -- Perform the name mangling + local suffix = rot.s + local name = ori_name..suffix + ndef.description = ori_ndef.description .. rot.h + if ori_ndef.tiles_prefix then + ndef.tiles = { ori_ndef.tiles_prefix .. suffix .. ".png" } + end + if ori_ndef.mesh_prefix then + ndef.mesh = ori_ndef.mesh_prefix .. suffix .. ori_ndef.mesh_suffix + end + -- rotate connections + if ori_ndef.at_conns then + ndef.at_conns = advtrains.rotate_conn_by(ori_ndef.at_conns, rot.i) + end + -- update node state map if present + if ori_ndef.advtrains then + if ori_ndef.advtrains.node_state_map then + local new_nsm = {} + for state, nname in pairs(ori_ndef.advtrains.node_state_map) do + new_nsm[state] = nname .. suffix + end + ndef.advtrains.node_state_map = new_nsm + end + if ori_ndef.advtrains.trackworker_next_var then + ndef.advtrains.trackworker_next_var = ori_ndef.advtrains.trackworker_next_var .. suffix + end + -- apply trackworker rot field + ndef.advtrains.trackworker_next_rot = ori_name .. rot.n + ndef.advtrains.trackworker_rot_incr_param2 = (rot.n=="") + end + -- apply groups + ndef.groups.save_in_at_nodedb = 1 + ndef.groups.not_blocking_trains = 1 + + -- give the definition mangling function an option to do some adjustments + if definition_mangling_function then + definition_mangling_function(name, ndef, rot.i, suffix) + end + + -- register node + atdebug("Registering: ",name, ndef) + minetest.register_node(":"..name, ndef) + + -- if this has the track_place_group set, register as a candidate for the track_place_group + if ndef.advtrains.track_place_group then + advtrains.trackplacer.register_candidate(ndef.advtrains.track_place_group, name, ndef, ndef.advtrains.track_place_single, true) + end + end +end + + +-- Registers an item to place and automatically connect nearby tracks +function advtrains.register_track_placer(...) + +end + +-- Registers an item to place and adjust slope tracks +function advtrains.register_slope_placer(...) + +end + + + +-- track-related helper functions + +function advtrains.is_track(nodename) + if not minetest.registered_nodes[nodename] then + return false + end + local nodedef=minetest.registered_nodes[nodename] + if nodedef and nodedef.groups.advtrains_track then + return true + end + return false +end + +function advtrains.get_track_connections(name, param2) + local nodedef=minetest.registered_nodes[name] + if not nodedef then atprint(" get_track_connections couldn't find nodedef for nodename "..(name or "nil")) return nil end + local noderot=param2 + if not param2 then noderot=0 end + if noderot > 3 then atprint(" get_track_connections: rail has invaild param2 of "..noderot) noderot=0 end + + local tracktype + for k,_ in pairs(nodedef.groups) do + local tt=string.match(k, "^advtrains_track_(.+)$") + if tt then + tracktype=tt + end + end + return advtrains.rotate_conn_by(nodedef.at_conns, noderot*AT_CMAX/4), (nodedef.at_rail_y or 0), tracktype +end + +-- Function called when a track is about to be dug or modified by the trackworker +-- Returns either true (ok) or false,"translated string describing reason why it isn't allowed" +function advtrains.can_dig_or_modify_track(pos) + if advtrains.get_train_at_pos(pos) then + return false, attrans("Position is occupied by a train.") + end + -- interlocking: tcb, signal IP a.s.o. + if advtrains.interlocking then + -- TCB? + if advtrains.interlocking.db.get_tcb(pos) then + return false, attrans("There's a Track Circuit Break here.") + end + -- signal ip? + if advtrains.interlocking.db.is_ip_at(pos, true) then -- is_ip_at with purge parameter + return false, attrans("There's a Signal Influence Point here.") + end + end + return true +end \ No newline at end of file diff --git a/advtrains/trainlogic.lua b/advtrains/trainlogic.lua index 288e224..c6762c9 100644 --- a/advtrains/trainlogic.lua +++ b/advtrains/trainlogic.lua @@ -283,7 +283,7 @@ function advtrains.train_ensure_init(id, train) assertdef(train, "id", id) - if not train.drives_on or not train.max_speed then + if not train.max_speed then --atprint("in ensure_init: missing properties, updating!") advtrains.update_trainpart_properties(id) end @@ -1034,10 +1034,9 @@ end -- Note: safe_decouple_wagon() has been moved to wagons.lua --- this function sets wagon's pos_in_train(parts) properties and train's max_speed and drives_on (and more) +-- this function sets wagon's pos_in_train(parts) properties and train's max_speed (and more) function advtrains.update_trainpart_properties(train_id, invert_flipstate) local train=advtrains.trains[train_id] - train.drives_on=advtrains.merge_tables(advtrains.all_tracktypes) --FIX: deep-copy the table!!! train.max_speed=20 train.extent_h = 0; @@ -1079,13 +1078,6 @@ function advtrains.update_trainpart_properties(train_id, invert_flipstate) end rel_pos=rel_pos+wagon.wagon_span - if wagon.drives_on then - for k,_ in pairs(train.drives_on) do - if not wagon.drives_on[k] then - train.drives_on[k]=nil - end - end - end train.max_speed=math.min(train.max_speed, wagon.max_speed) train.extent_h = math.max(train.extent_h, wagon.extent_h or 1); end diff --git a/advtrains/wagons.lua b/advtrains/wagons.lua index b0fb575..f231546 100644 --- a/advtrains/wagons.lua +++ b/advtrains/wagons.lua @@ -1367,7 +1367,7 @@ function advtrains.register_wagon(sysname_p, prototype, desc, inv_img, nincreati local node=minetest.get_node_or_nil(pointed_thing.under) if not node then atprint("[advtrains]Ignore at placer position") return itemstack end local nodename=node.name - if(not advtrains.is_track_and_drives_on(nodename, prototype.drives_on)) then + if(not advtrains.is_track(nodename)) then atprint("no track here, not placing.") return itemstack end @@ -1382,7 +1382,7 @@ function advtrains.register_wagon(sysname_p, prototype, desc, inv_img, nincreati local yaw = placer:get_look_horizontal() local plconnid = advtrains.yawToClosestConn(yaw, tconns) - local prevpos = advtrains.get_adjacent_rail(pointed_thing.under, tconns, plconnid, prototype.drives_on) + local prevpos = advtrains.get_adjacent_rail(pointed_thing.under, tconns, plconnid) if not prevpos then minetest.chat_send_player(pname, "The track you are trying to place the wagon on is not long enough!") return @@ -1407,7 +1407,6 @@ advtrains.register_wagon("advtrains:wagon_placeholder", { collisionbox = {-0.3,-0.3,-0.3, 0.3,0.3,0.3}, visual_size = {x=0.7, y=0.7}, initial_sprite_basepos = {x=0, y=0}, - drives_on = advtrains.all_tracktypes, max_speed = 5, seats = { }, diff --git a/advtrains_interlocking/init.lua b/advtrains_interlocking/init.lua index a2f5882..cc46b83 100644 --- a/advtrains_interlocking/init.lua +++ b/advtrains_interlocking/init.lua @@ -24,7 +24,9 @@ dofile(modpath.."tool.lua") dofile(modpath.."approach.lua") dofile(modpath.."ars.lua") -dofile(modpath.."tsr_rail.lua") + +--TODO reenable tsr rail +--dofile(modpath.."tsr_rail.lua") minetest.register_privilege("interlocking", {description = "Can set up track sections, routes and signals.", give_to_singleplayer = true}) diff --git a/advtrains_line_automation/init.lua b/advtrains_line_automation/init.lua index 7b758bc..cc8df3c 100644 --- a/advtrains_line_automation/init.lua +++ b/advtrains_line_automation/init.lua @@ -20,7 +20,9 @@ local modpath = minetest.get_modpath(minetest.get_current_modname()) .. DIR_DELI dofile(modpath.."railwaytime.lua") dofile(modpath.."scheduler.lua") -dofile(modpath.."stoprail.lua") + +--TODO reenable stop rail +--dofile(modpath.."stoprail.lua") function advtrains.lines.load(data) diff --git a/advtrains_signals_ks/init.lua b/advtrains_signals_ks/init.lua index bdbd50d..99d059a 100755 --- a/advtrains_signals_ks/init.lua +++ b/advtrains_signals_ks/init.lua @@ -113,15 +113,6 @@ local suppasp_ra = { } } -advtrains.trackplacer.register_tracktype("advtrains_signals_ks:hs") -advtrains.trackplacer.register_tracktype("advtrains_signals_ks:ra") -advtrains.trackplacer.register_tracktype("advtrains_signals_ks:sign") -advtrains.trackplacer.register_tracktype("advtrains_signals_ks:sign_lf") -advtrains.trackplacer.register_tracktype("advtrains_signals_ks:sign_lf7") -advtrains.trackplacer.register_tracktype("advtrains_signals_ks:zs3") -advtrains.trackplacer.register_tracktype("advtrains_signals_ks:zs3v") -advtrains.trackplacer.register_tracktype("advtrains_signals_ks:mast") - for _, rtab in ipairs({ {rot = "0", sbox = {-1/8, -1/2, -1/2, 1/8, 1/2, -1/4}, ici=true}, {rot = "30", sbox = {-3/8, -1/2, -1/2, -1/8, 1/2, -1/4},}, @@ -201,7 +192,7 @@ for _, rtab in ipairs({ after_dig_node = advtrains.interlocking.signal_after_dig, }) -- rotatable by trackworker - advtrains.trackplacer.add_worked("advtrains_signals_ks:hs", typ, "_"..rot) + --TODO add rotation using trackworker end @@ -246,7 +237,7 @@ for _, rtab in ipairs({ after_dig_node = advtrains.interlocking.signal_after_dig, }) -- rotatable by trackworker - advtrains.trackplacer.add_worked("advtrains_signals_ks:ra", typ, "_"..rot) + --TODO add rotation using trackworker end -- Schilder: @@ -283,7 +274,7 @@ for _, rtab in ipairs({ after_dig_node = advtrains.interlocking.signal_after_dig, }) -- rotatable by trackworker - advtrains.trackplacer.add_worked("advtrains_signals_ks:"..prefix, typ, "_"..rot, nxt) + --TODO add rotation using trackworker end for typ, prts in pairs { @@ -378,7 +369,7 @@ for _, rtab in ipairs({ t.drop = "advtrains_signals_ks:zs3_off_0" t.selection_box.fixed[1][5] = 0 minetest.register_node("advtrains_signals_ks:zs3_"..typ.."_"..rot, t) - advtrains.trackplacer.add_worked("advtrains_signals_ks:zs3", typ, "_"..rot) + --TODO add rotation using trackworker -- Zs 3v local t = table.copy(def) @@ -387,7 +378,7 @@ for _, rtab in ipairs({ t.drop = "advtrains_signals_ks:zs3v_off_0" t.tiles[3] = t.tiles[3] .. "^[multiply:yellow" minetest.register_node("advtrains_signals_ks:zs3v_"..typ.."_"..rot, t) - advtrains.trackplacer.add_worked("advtrains_signals_ks:zs3v", typ, "_"..rot) + --TODO add rotation using trackworker end minetest.register_node("advtrains_signals_ks:mast_mast_"..rot, { @@ -412,7 +403,7 @@ for _, rtab in ipairs({ }, drop = "advtrains_signals_ks:mast_mast_0", }) - advtrains.trackplacer.add_worked("advtrains_signals_ks:mast","mast", "_"..rot) + --TODO add rotation using trackworker end -- Crafting diff --git a/advtrains_train_track/init.lua b/advtrains_train_track/init.lua index 5065155..55b1367 100755 --- a/advtrains_train_track/init.lua +++ b/advtrains_train_track/init.lua @@ -1,937 +1,172 @@ --- Default tracks for advtrains --- (c) orwell96 and contributors - -local default_boxen = { - ["st"] = { - [""] = { - selection_box = { - type = "fixed", - fixed = {-1/2-1/16, -1/2, -1/2, 1/2+1/16, -1/2+2/16, 1/2}, - } - }, - ["_30"] = { - selection_box = { - type = "fixed", - fixed = { - {-0.5000, -0.5000, -1.000, 0.5000, -0.3750, 1.000}, - {-0.8750, -0.5000, -1.000, -0.5000, -0.3750, 0.2500}, - {0.5000, -0.5000, -0.2500, 0.8750, -0.3750, 1.000}, - {-0.1250, -0.5000, -1.375, 0.1875, -0.3750, -1.000} - } - } - }, - ["_45"] = { - selection_box = { - type = "fixed", - fixed = { - {-0.5000, -0.5000, -0.8750, 0.5000, -0.3750, 0.8750}, - {0.5000, -0.5000, -0.5000, 0.8750, -0.3750, 0.5000}, - {-0.8750, -0.5000, -0.5000, -0.5000, -0.3750, 0.5000} - } - } - }, - ["_60"] = { - selection_box = { - type = "fixed", - fixed = { - {-1.000, -0.5000, -0.5000, 1.000, -0.3750, 0.5000}, - {-1.000, -0.5000, -0.8750, 0.2500, -0.3750, -0.5000}, - {-0.2500, -0.5000, 0.5000, 1.000, -0.3750, 0.8750}, - {-1.375, -0.5000, -0.1250, -1.000, -0.3750, 0.1875} - } - } - }, - }, - - ["cr"] = { - [""] = { - selection_box = { - type = "fixed", - fixed = { - {-0.5000, -0.5000, -0.5000, 0.6875, -0.3750, 0.5000}, - {-0.3750, -0.5000, -1.000, 1.000, -0.3750, 0.000} - } - } - }, - ["_30"] = { - selection_box = { - type = "fixed", - fixed = { - {-0.5000, -0.5000, -0.5000, 0.7500, -0.3750, 0.8750}, - {-0.3750, -0.5000, 0.8750, 0.2500, -0.3750, 1.188}, - {0.7500, -0.5000, 0.2500, 1.063, -0.3750, 0.8750} - } - } - }, - ["_45"] = { - selection_box = { - type = "fixed", - fixed = { - {-0.5000, -0.5000, -1.125, 0.5000, -0.3750, 0.6875}, - {-0.8750, -0.5000, -0.9375, -0.5000, -0.3750, 0.06250}, - {0.5000, -0.5000, -0.5000, 0.8750, -0.3750, 0.5000} - } - } - }, - ["_60"] = { - selection_box = { - type = "fixed", - fixed = { - {-0.8125, -0.5000, -0.5000, 1.188, -0.3750, 0.5000}, - {-0.1875, -0.5000, 0.5000, 0.8750, -0.3125, 0.8750}, - {-0.2500, -0.5000, -0.9375, 0.3125, -0.3125, -0.5000} - } - } - }, - }, - - ["swlst"] = { - [""] = { - selection_box = { - type = "fixed", - fixed = { - {-0.5000, -0.5000, -0.5000, 0.6250, -0.3750, 0.5000}, - {-0.3125, -0.5000, -1.000, 0.9375, -0.3125, -0.06250} - } - } - }, - ["_30"] = { - selection_box = { - type = "fixed", - fixed = { - {-0.5000, -0.5000, -1.000, 0.5000, -0.3750, 1.000}, - {-0.8750, -0.5000, -1.000, -0.5000, -0.3750, 0.2500}, - {0.5000, -0.5000, -0.2500, 0.8750, -0.3750, 1.000}, - {-0.1250, -0.5000, -1.375, 0.1875, -0.3750, -1.000} - } - } - }, - ["_45"] = { - selection_box = { - type = "fixed", - fixed = { - {-0.5000, -0.5000, -1.1875, 0.5000, -0.3750, 0.8750}, - {0.5000, -0.5000, -0.5000, 0.8750, -0.3750, 0.5000}, - {-0.8750, -0.5000, -0.8125, -0.5000, -0.3750, 0.5000} - } - } - }, - ["_60"] = { - selection_box = { - type = "fixed", - fixed = { - {-1.000, -0.5000, -0.5000, 1.000, -0.3750, 0.5000}, - {-1.000, -0.5000, -0.8750, 0.2500, -0.3750, -0.5000}, - {-0.2500, -0.5000, 0.5000, 1.000, -0.3750, 0.8750}, - {-1.375, -0.5000, -0.1250, -1.000, -0.3750, 0.1875} - } - } - }, - }, - - ["swrst"] = { - [""] = { - selection_box = { - type = "fixed", - fixed = { - {-0.5000, -0.5000, -0.5000, 0.6250, -0.3750, 0.5000}, - {-0.8125, -0.5000, -1.000, 0.4375, -0.3125, -0.06250} - } - } - }, - ["_30"] = { - selection_box = { - type = "fixed", - fixed = { - {-0.5000, -0.5000, -1.000, 0.5000, -0.3750, 1.000}, - {-0.8750, -0.5000, -1.000, -0.5000, -0.3750, 0.2500}, - {0.5000, -0.5000, -0.2500, 0.8750, -0.3750, 1.000}, - {-0.1250, -0.5000, -1.375, 0.1875, -0.3750, -1.000} - } - } - }, - ["_45"] = { - selection_box = { - type = "fixed", - fixed = { - {-1.1875, -0.5000, -0.5000, 0.8750, -0.3750, 0.5000}, - {-0.5000, -0.5000, 0.5000, 0.5000, -0.3750, 0.8750}, - {-0.8125, -0.5000, -0.8750, 0.5000, -0.3750, -0.5000} - } - } - }, - ["_60"] = { - selection_box = { - type = "fixed", - fixed = { - {-1.000, -0.5000, -0.5000, 1.000, -0.3750, 0.5000}, - {-1.000, -0.5000, -0.8750, 0.2500, -0.3750, -0.5000}, - {-0.2500, -0.5000, 0.5000, 1.000, -0.3750, 0.8750}, - {-1.375, -0.5000, -0.1250, -1.000, -0.3750, 0.1875} - } - } - }, - }, -} - -default_boxen["swlcr"] = default_boxen["swlst"] -default_boxen["swrcr"] = default_boxen["swrst"] - ---flat -advtrains.register_tracks("default", { - nodename_prefix="advtrains:dtrack", - texture_prefix="advtrains_dtrack", - models_prefix="advtrains_dtrack", - models_suffix=".b3d", - shared_texture="advtrains_dtrack_shared.png", - description=attrans("Track"), - formats={}, - - get_additional_definiton = function(def, preset, suffix, rotation) - if default_boxen[suffix] ~= nil and default_boxen[suffix][rotation] ~= nil then - return default_boxen[suffix][rotation] - else - return {} - end - end, -}, advtrains.ap.t_30deg_flat) - -minetest.register_craft({ - output = 'advtrains:dtrack_placer 50', - recipe = { - {'default:steel_ingot', 'group:stick', 'default:steel_ingot'}, - {'default:steel_ingot', 'group:stick', 'default:steel_ingot'}, - {'default:steel_ingot', 'group:stick', 'default:steel_ingot'}, - }, -}) - -local y3_boxen = { - [""] = { - selection_box = { - type = "fixed", - fixed = { - {-0.8750, -0.5000, -1.125, 0.8750, -0.3750, 0.4375} - } - } - }, - - ["_30"] = { - selection_box = { - type = "fixed", - fixed = { - {-0.5000, -0.5000, -0.875, 0.5000, -0.3750, 1.000}, - {-0.8750, -0.5000, -0.4375, -0.5000, -0.3750, 0.5625}, - {0.5000, -0.5000, -0.2500, 0.8125, -0.3750, 1.000}, - } - } - }, - - --UX FIXME: - 3way - have to place straight route before l and r or the - --nodebox overlaps too much and can't place the straight track node. - ["_45"] = { - selection_box = { - type = "fixed", - fixed = { - {-0.5000, -0.5000, -1.1250, 0.5000, -0.3750, 0.8750}, - {0.5000, -0.5000, -0.5000, 0.8750, -0.3750, 0.5000}, - {-1.1250, -0.5000, -0.9375, -0.5000, -0.3750, 0.5000} - } - } - }, - - ["_60"] = { - selection_box = { - type = "fixed", - fixed = { - --{-0.5000, -0.5000, -0.875, 0.5000, -0.3750, 1.000}, - {-0.875, -0.5000, -0.5, 1.0, -0.3750, 0.5}, - --{-0.8750, -0.5000, -0.4375, -0.5000, -0.3750, 0.5625}, - {-0.4375, -0.5000, -0.8750, 0.5625, -0.3750, -0.5000}, - --{0.5000, -0.5000, -0.2500, 0.8125, -0.3750, 1.000}, - {-0.2500, -0.5000, -0.2500, 1.0000, -0.3750, 0.8125}, - } - } - }, -} - - -local function y3_turnouts_addef(def, preset, suffix, rotation) - return y3_boxen[rotation] or {} -end --- y-turnout -advtrains.register_tracks("default", { - nodename_prefix="advtrains:dtrack_sy", - texture_prefix="advtrains_dtrack_sy", - models_prefix="advtrains_dtrack_sy", - models_suffix=".obj", - shared_texture="advtrains_dtrack_shared.png", - description=attrans("Y-turnout"), - formats = {}, - get_additional_definiton = y3_turnouts_addef, -}, advtrains.ap.t_yturnout) -minetest.register_craft({ - output = 'advtrains:dtrack_sy_placer 2', - recipe = { - {'advtrains:dtrack_placer', '', 'advtrains:dtrack_placer'}, - {'', 'advtrains:dtrack_placer', ''}, - {'', 'advtrains:dtrack_placer', ''}, - }, -}) ---3-way turnout -advtrains.register_tracks("default", { - nodename_prefix="advtrains:dtrack_s3", - texture_prefix="advtrains_dtrack_s3", - models_prefix="advtrains_dtrack_s3", - models_suffix=".obj", - shared_texture="advtrains_dtrack_shared.png", - description=attrans("3-way turnout"), - formats = {}, - get_additional_definiton = y3_turnouts_addef, -}, advtrains.ap.t_s3way) -minetest.register_craft({ - output = 'advtrains:dtrack_s3_placer 1', - recipe = { - {'advtrains:dtrack_placer', 'advtrains:dtrack_placer', 'advtrains:dtrack_placer'}, - {'', 'advtrains:dtrack_placer', ''}, - {'', '', ''}, - }, -}) - --- Diamond Crossings - -local perp_boxen = { - [""] = {}, --default size - ["_30"] = { - selection_box = { - type = "fixed", - fixed = { - {-1.000, -0.5000, -1.000, 1.000, -0.3750, 1.000} - } - } - }, - ["_45"] = { - selection_box = { - type = "fixed", - fixed = { - {-0.8125, -0.5000, -0.8125, 0.8125, -0.3750, 0.8125} - } - } - }, - ["_60"] = { - selection_box = { - type = "fixed", - fixed = { - {-1.000, -0.5000, -1.000, 1.000, -0.3750, 1.000} - } - } - }, -} - --- perpendicular -advtrains.register_tracks("default", { - nodename_prefix="advtrains:dtrack_xing", - texture_prefix="advtrains_dtrack_xing", - models_prefix="advtrains_dtrack_xing", - models_suffix=".obj", - shared_texture="advtrains_dtrack_shared.png", - description=attrans("Perpendicular Diamond Crossing Track"), - formats = {}, - get_additional_definiton = function(def, preset, suffix, rotation) - return perp_boxen[rotation] or {} - end -}, advtrains.ap.t_perpcrossing) - -minetest.register_craft({ - output = 'advtrains:dtrack_xing_placer 3', - recipe = { - {'', 'advtrains:dtrack_placer', ''}, - {'advtrains:dtrack_placer', 'advtrains:dtrack_placer', 'advtrains:dtrack_placer'}, - {'', 'advtrains:dtrack_placer', ''} - } -}) - -local ninety_plus_boxen = { - ["30l"] = { - selection_box = { - type = "fixed", - fixed = { - {-0.5000, -0.5000, -1.000, 0.5000, -0.3750, 1.000}, - {-0.8750, -0.5000, -1.000, -0.5000, -0.3750, 0.2500}, - {0.5000, -0.5000, -0.2500, 0.8750, -0.3750, 1.000}, - {-0.1250, -0.5000, -1.375, 0.1875, -0.3750, -1.000} - } - } - }, - ["30r"] = { - selection_box = { - type = "fixed", - fixed = { - {0.5000, -0.5000, -1.000, -0.5000, -0.3750, 1.000}, - {0.8750, -0.5000, -1.000, 0.5000, -0.3750, 0.2500}, - {-0.5000, -0.5000, -0.2500, -0.8750, -0.3750, 1.000}, - {0.1250, -0.5000, -1.375, -0.1875, -0.3750, -1.000} - } - } - }, - ["45l"] = { - selection_box = { - type = "fixed", - fixed = { - {-0.5000, -0.5000, -0.8750, 0.5000, -0.3750, 0.8750}, - {0.5000, -0.5000, -0.5000, 0.8750, -0.3750, 0.5000}, - {-0.8750, -0.5000, -0.5000, -0.5000, -0.3750, 0.5000} - } - } - }, - ["45r"] = { - selection_box = { - type = "fixed", - fixed = { - {-0.5000, -0.5000, -0.8750, 0.5000, -0.3750, 0.8750}, - {0.5000, -0.5000, -0.5000, 0.8750, -0.3750, 0.5000}, - {-0.8750, -0.5000, -0.5000, -0.5000, -0.3750, 0.5000} - } - } - }, - ["60l"] = { - selection_box = { - type = "fixed", - fixed = { - {-1.000, -0.5000, -0.5000, 1.000, -0.3750, 0.5000}, - {-1.000, -0.5000, -0.8750, 0.2500, -0.3750, -0.5000}, - {-0.2500, -0.5000, 0.5000, 1.000, -0.3750, 0.8750}, - {-1.375, -0.5000, -0.1250, -1.000, -0.3750, 0.1875} - } - } - }, - ["60r"] = { - selection_box = { - type = "fixed", - fixed = { - {1.000, -0.5000, -0.5000, -1.000, -0.3750, 0.5000}, - {1.000, -0.5000, -0.8750, -0.2500, -0.3750, -0.5000}, - {0.2500, -0.5000, 0.5000, -1.000, -0.3750, 0.8750}, - {1.375, -0.5000, -0.1250, 1.000, -0.3750, 0.1875} - } - } - }, -} - --- 90plusx --- When you face east and param2=0, then this set of rails has a rail at 90 --- degrees to the viewer, plus another rail crossing at 30, 45 or 60 degrees. -advtrains.register_tracks("default", { - nodename_prefix="advtrains:dtrack_xing90plusx", - texture_prefix="advtrains_dtrack_xing4590", - models_prefix="advtrains_dtrack_xing90plusx", - models_suffix=".obj", - shared_texture="advtrains_dtrack_shared.png", - description=attrans("90+Angle Diamond Crossing Track"), - formats = {}, - get_additional_definiton = function(def, preset, suffix, rotation) - return ninety_plus_boxen[suffix] or {} - end, -}, advtrains.ap.t_90plusx_crossing) -minetest.register_craft({ - output = 'advtrains:dtrack_xing90plusx_placer 2', - recipe = { - {'advtrains:dtrack_placer', '', ''}, - {'advtrains:dtrack_placer', 'advtrains:dtrack_placer', 'advtrains:dtrack_placer'}, - {'', '', 'advtrains:dtrack_placer'} - } -}) - --- Deprecate any rails using the old name scheme -minetest.register_lbm({ - label = "Upgrade legacy 4590 rails", - name = "advtrains_train_track:replace_legacy_4590", - nodenames = {"advtrains:dtrack_xing4590_st"}, - run_at_every_load = true, - action = function(pos, node) - minetest.log("actionPos!: " .. pos.x .. "," .. pos.y .. "," .. pos.z) - minetest.log("node!: " .. node.name .. "," .. node.param1 .. "," .. node.param2) - advtrains.ndb.swap_node(pos, - { - name="advtrains:dtrack_xing90plusx_45l", - param1=node.param1, - param2=node.param2, - }) - end -}) --- This will replace any items left in the inventory -minetest.register_alias("advtrains:dtrack_xing4590_placer", "advtrains:dtrack_xing90plusx_placer") - -local diagonal_boxen = { - ["30r45l"] = { - selection_box = { - type = "fixed", - fixed = { - {0.5000, -0.5000, -1.000, -0.5000, -0.3750, 1.000}, - {0.8750, -0.5000, -1.000, 0.5000, -0.3750, 0.2500}, - {-0.5000, -0.5000, -0.2500, -0.8750, -0.3750, 1.000}, - {0.1250, -0.5000, -1.375, -0.1875, -0.3750, -1.000} - } - } - }, - ["60l30l"] = { - selection_box = { - type = "fixed", - fixed = { - {-1.000, -0.5000, -0.5000, 1.000, -0.3750, 0.5000}, - {-1.000, -0.5000, -0.8750, 0.2500, -0.3750, -0.5000}, - {-0.2500, -0.5000, 0.5000, 1.000, -0.3750, 0.8750}, - {-1.375, -0.5000, -0.1250, -1.000, -0.3750, 0.1875} - } - } - }, - ["60l60r"] = { - selection_box = { - type = "fixed", - fixed = { - {-1.000, -0.5000, -1.000, 1.000, -0.3750, 1.000} - } - } - }, - ["60r30r"] = { - selection_box = { - type = "fixed", - fixed = { - {1.000, -0.5000, -0.5000, -1.000, -0.3750, 0.5000}, - {1.000, -0.5000, -0.8750, -0.2500, -0.3750, -0.5000}, - {0.2500, -0.5000, 0.5000, -1.000, -0.3750, 0.8750}, - {1.375, -0.5000, -0.1250, 1.000, -0.3750, 0.1875} - } - } - }, - ["30l45r"] = { - selection_box = { - type = "fixed", - fixed = { - {-0.5000, -0.5000, -1.000, 0.5000, -0.3750, 1.000}, - {-0.8750, -0.5000, -1.000, -0.5000, -0.3750, 0.2500}, - {0.5000, -0.5000, -0.2500, 0.8750, -0.3750, 1.000}, - {-0.1250, -0.5000, -1.375, 0.1875, -0.3750, -1.000} - } - } - }, - ["60l45r"] = { - selection_box = { - type = "fixed", - fixed = { - {-1.000, -0.5000, -0.5000, 1.000, -0.3750, 0.5000}, - {-1.000, -0.5000, -0.8750, 0.2500, -0.3750, -0.5000}, - {-0.2500, -0.5000, 0.5000, 1.000, -0.3750, 0.8750}, - {-1.375, -0.5000, -0.1250, -1.000, -0.3750, 0.1875} - } - } - }, - ["60r45l"] = { - selection_box = { - type = "fixed", - fixed = { - {1.000, -0.5000, -0.5000, -1.000, -0.3750, 0.5000}, - {1.000, -0.5000, -0.8750, -0.2500, -0.3750, -0.5000}, - {0.2500, -0.5000, 0.5000, -1.000, -0.3750, 0.8750}, - {1.375, -0.5000, -0.1250, 1.000, -0.3750, 0.1875} - } - } - }, -} - --- Diagonal --- This set of rail crossings is named based on the angle of each intersecting --- direction when facing east and param2=0. Rails with l/r swapped are mirror --- images. For example, 30r45l is the mirror image of 30l45r. -advtrains.register_tracks("default", { - nodename_prefix="advtrains:dtrack_xingdiag", - texture_prefix="advtrains_dtrack_xingdiag", - models_prefix="advtrains_dtrack_xingdiag", - models_suffix=".obj", - shared_texture="advtrains_dtrack_shared.png", - description=attrans("Diagonal Diamond Crossing Track"), - formats = {}, - get_additional_definiton = function(def, preset, suffix, rotation) - return diagonal_boxen[suffix] or {} - end, -}, advtrains.ap.t_diagonalcrossing) -minetest.register_craft({ - output = 'advtrains:dtrack_xingdiag_placer 2', - recipe = { - {'advtrains:dtrack_placer', '', 'advtrains:dtrack_placer'}, - {'', 'advtrains:dtrack_placer', ''}, - {'advtrains:dtrack_placer', '', 'advtrains:dtrack_placer'} - } -}) ----- Not included: very shallow crossings like (30/60)+45. ----- At an angle of only 18.4 degrees, the models would not ----- translate well to a block game. --- END crossings - ---slopes -advtrains.register_tracks("default", { - nodename_prefix="advtrains:dtrack", - texture_prefix="advtrains_dtrack", - models_prefix="advtrains_dtrack", - models_suffix=".obj", - shared_texture="advtrains_dtrack_shared.png", - second_texture="default_gravel.png", - description=attrans("Track"), - formats={vst1={true, false, true}, vst2={true, false, true}, vst31={true}, vst32={true}, vst33={true}}, -}, advtrains.ap.t_30deg_slope) - -minetest.register_craft({ - type = "shapeless", - output = 'advtrains:dtrack_slopeplacer 2', - recipe = { - "advtrains:dtrack_placer", - "advtrains:dtrack_placer", - "default:gravel", - }, -}) - - ---bumpers -advtrains.register_tracks("default", { - nodename_prefix="advtrains:dtrack_bumper", - texture_prefix="advtrains_dtrack_bumper", - models_prefix="advtrains_dtrack_bumper", - models_suffix=".b3d", - shared_texture="advtrains_dtrack_rail.png", - --bumpers still use the old texture until the models are redone. - description=attrans("Bumper"), - formats={}, -}, advtrains.ap.t_30deg_straightonly) -minetest.register_craft({ - output = 'advtrains:dtrack_bumper_placer 2', - recipe = { - {'group:wood', 'dye:red'}, - {'default:steel_ingot', 'default:steel_ingot'}, - {'advtrains:dtrack_placer', 'advtrains:dtrack_placer'}, - }, -}) ---legacy bumpers -for _,rot in ipairs({"", "_30", "_45", "_60"}) do - minetest.register_alias("advtrains:dtrack_bumper"..rot, "advtrains:dtrack_bumper_st"..rot) -end --- atc track -advtrains.register_tracks("default", { - nodename_prefix="advtrains:dtrack_atc", - texture_prefix="advtrains_dtrack_atc", - models_prefix="advtrains_dtrack", - models_suffix=".b3d", - shared_texture="advtrains_dtrack_shared_atc.png", - description=attrans("ATC controller"), - formats={}, - get_additional_definiton = advtrains.atc_function -}, advtrains.trackpresets.t_30deg_straightonly) - - --- Tracks for loading and unloading trains --- Copyright (C) 2017 Gabriel Pérez-Cerezo - -local function get_far_node(pos) - local node = minetest.get_node(pos) - if node.name == "ignore" then - minetest.get_voxel_manip():read_from_map(pos, pos) - node = minetest.get_node(pos) - end - return node -end - - -local function show_fc_formspec(pos,player) - local pname = player:get_player_name() - if minetest.is_protected(pos,pname) then - minetest.chat_send_player(pname, "Position is protected!") - return - end - - local meta = minetest.get_meta(pos) - local fc = meta:get_string("fc") or "" - - local form = 'formspec_version[4]'.. - 'size[10,5]'.. - 'label[0.5,0.4;Advtrains Loading/Unloading Track]'.. - 'label[0.5,1.1;Set the code to match against the wagon\'s freight code]'.. - 'label[0.5,1.6;A blank field matches all wagons (default)]'.. - 'label[0.5,2.1;Use code # to disable the track section]'.. - 'field[0.5,3;5.5,1;fc;FC;'..minetest.formspec_escape(fc)..']'.. - 'button[6.5,3;3,1;save;Submit]' - minetest.show_formspec(pname, "at_load_unload_"..advtrains.encode_pos(pos), form) -end - -minetest.register_on_player_receive_fields(function(player, formname, fields) - local pname = player:get_player_name() - local pe = string.match(formname, "^at_load_unload_(............)$") - local pos = advtrains.decode_pos(pe) - if pos then - if minetest.is_protected(pos, pname) then - minetest.chat_send_player(pname, "Position is protected!") - return - end - - if fields.save then - minetest.get_meta(pos):set_string("fc",tostring(fields.fc)) - minetest.chat_send_player(pname,"Freight code set: "..tostring(fields.fc)) - show_fc_formspec(pos,player) - end - end -end) - - -local function train_load(pos, train_id, unload) - local train=advtrains.trains[train_id] - local below = get_far_node({x=pos.x, y=pos.y-1, z=pos.z}) - if not string.match(below.name, "chest") then - atprint("this is not a chest! at "..minetest.pos_to_string(pos)) - return - end - - local node_fc = minetest.get_meta(pos):get_string("fc") or "" - if node_fc == "#" then - --track section is disabled - return - end - - local inv = minetest.get_inventory({type="node", pos={x=pos.x, y=pos.y-1, z=pos.z}}) - if inv and train.velocity < 2 then - for k, v in ipairs(train.trainparts) do - local i=minetest.get_inventory({type="detached", name="advtrains_wgn_"..v}) - if i and i:get_list("box") then - - local wagon_data = advtrains.wagons[v] - local wagon_fc - if wagon_data.fc then - if not wagon_data.fcind then wagon_data.fcind = 1 end - wagon_fc = tostring(wagon_data.fc[wagon_data.fcind]) or "" - end - - if node_fc == "" or wagon_fc == node_fc then - if not unload then - for _, item in ipairs(inv:get_list("main")) do - if i:get_list("box") and i:room_for_item("box", item) then - i:add_item("box", item) - inv:remove_item("main", item) - end - end - else - for _, item in ipairs(i:get_list("box")) do - if inv:get_list("main") and inv:room_for_item("main", item) then - i:remove_item("box", item) - inv:add_item("main", item) - end - end - end - end - end - end - end -end - - - -advtrains.register_tracks("default", { - nodename_prefix="advtrains:dtrack_unload", - texture_prefix="advtrains_dtrack_unload", - models_prefix="advtrains_dtrack", - models_suffix=".b3d", - shared_texture="advtrains_dtrack_shared_unload.png", - description=attrans("Unloading Track"), - formats={}, - get_additional_definiton = function(def, preset, suffix, rotation) - return { - after_dig_node=function(pos) - advtrains.invalidate_all_paths() - advtrains.ndb.clear(pos) - end, - on_rightclick = function(pos, node, player) - show_fc_formspec(pos, player) - end, - advtrains = { - on_train_enter = function(pos, train_id) - train_load(pos, train_id, true) - end, - }, - } - end - }, advtrains.trackpresets.t_30deg_straightonly) -advtrains.register_tracks("default", { - nodename_prefix="advtrains:dtrack_load", - texture_prefix="advtrains_dtrack_load", - models_prefix="advtrains_dtrack", - models_suffix=".b3d", - shared_texture="advtrains_dtrack_shared_load.png", - description=attrans("Loading Track"), - formats={}, - get_additional_definiton = function(def, preset, suffix, rotation) - return { - after_dig_node=function(pos) - advtrains.invalidate_all_paths() - advtrains.ndb.clear(pos) - end, - on_rightclick = function(pos, node, player) - show_fc_formspec(pos, player) - end, - advtrains = { - on_train_enter = function(pos, train_id) - train_load(pos, train_id, false) - end, - }, - } - end - }, advtrains.trackpresets.t_30deg_straightonly) - --- mod-dependent crafts -local loader_core = "default:mese_crystal" --fallback -if minetest.get_modpath("basic_materials") then - loader_core = "basic_materials:ic" -elseif minetest.get_modpath("technic") then - loader_core = "technic:control_logic_unit" -end ---print("Loader Core: "..loader_core) - -minetest.register_craft({ - type="shapeless", - output = 'advtrains:dtrack_load_placer', - recipe = { - "advtrains:dtrack_placer", - loader_core, - "default:chest" - }, -}) -loader_core = nil --nil the crafting variable - ---craft between load/unload tracks -minetest.register_craft({ - type="shapeless", - output = 'advtrains:dtrack_unload_placer', - recipe = { - "advtrains:dtrack_load_placer", - }, -}) -minetest.register_craft({ - type="shapeless", - output = 'advtrains:dtrack_load_placer', - recipe = { - "advtrains:dtrack_unload_placer", - }, -}) - - -if mesecon then - advtrains.register_tracks("default", { - nodename_prefix="advtrains:dtrack_detector_off", - texture_prefix="advtrains_dtrack_detector", - models_prefix="advtrains_dtrack", - models_suffix=".b3d", - shared_texture="advtrains_dtrack_shared_detector_off.png", - description=attrans("Detector Rail"), - formats={}, - get_additional_definiton = function(def, preset, suffix, rotation) - return { - mesecons = { - receptor = { - state = mesecon.state.off, - rules = advtrains.meseconrules - } - }, - advtrains = { - on_updated_from_nodedb = function(pos, node) - mesecon.receptor_off(pos, advtrains.meseconrules) - end, - on_train_enter=function(pos, train_id) - advtrains.ndb.swap_node(pos, {name="advtrains:dtrack_detector_on".."_"..suffix..rotation, param2=advtrains.ndb.get_node(pos).param2}) - if advtrains.is_node_loaded(pos) then - mesecon.receptor_on(pos, advtrains.meseconrules) - end - end - } - } - end - }, advtrains.ap.t_30deg_straightonly) - advtrains.register_tracks("default", { - nodename_prefix="advtrains:dtrack_detector_on", - texture_prefix="advtrains_dtrack", - models_prefix="advtrains_dtrack", - models_suffix=".b3d", - shared_texture="advtrains_dtrack_shared_detector_on.png", - description="Detector(on)(you hacker you)", - formats={}, - get_additional_definiton = function(def, preset, suffix, rotation) - return { - mesecons = { - receptor = { - state = mesecon.state.on, - rules = advtrains.meseconrules - } - }, - advtrains = { - on_updated_from_nodedb = function(pos, node) - mesecon.receptor_on(pos, advtrains.meseconrules) - end, - on_train_leave=function(pos, train_id) - advtrains.ndb.swap_node(pos, {name="advtrains:dtrack_detector_off".."_"..suffix..rotation, param2=advtrains.ndb.get_node(pos).param2}) - if advtrains.is_node_loaded(pos) then - mesecon.receptor_off(pos, advtrains.meseconrules) - end - end - } - } - end - }, advtrains.ap.t_30deg_straightonly_noplacer) -minetest.register_craft({ - type="shapeless", - output = 'advtrains:dtrack_detector_off_placer', - recipe = { - "advtrains:dtrack_placer", - "mesecons:wire_00000000_off" - }, -}) -end ---TODO legacy ---I know lbms are better for this purpose -for name,rep in pairs({swl_st="swlst", swr_st="swrst", swl_cr="swlcr", swr_cr="swrcr", }) do - minetest.register_abm({ - -- In the following two fields, also group:groupname will work. - nodenames = {"advtrains:track_"..name}, - interval = 1.0, -- Operation interval in seconds - chance = 1, -- Chance of trigger per-node per-interval is 1.0 / this - action = function(pos, node, active_object_count, active_object_count_wider) minetest.set_node(pos, {name="advtrains:track_"..rep, param2=node.param2}) end, - }) - minetest.register_abm({ - -- In the following two fields, also group:groupname will work. - nodenames = {"advtrains:track_"..name.."_45"}, - interval = 1.0, -- Operation interval in seconds - chance = 1, -- Chance of trigger per-node per-interval is 1.0 / this - action = function(pos, node, active_object_count, active_object_count_wider) minetest.set_node(pos, {name="advtrains:track_"..rep.."_45", param2=node.param2}) end, - }) -end - -if advtrains.register_replacement_lbms then -minetest.register_lbm({ - name = "advtrains:ramp_replacement_1", --- In the following two fields, also group:groupname will work. - nodenames = {"advtrains:track_vert1"}, - action = function(pos, node, active_object_count, active_object_count_wider) minetest.set_node(pos, {name="advtrains:dtrack_vst1", param2=(node.param2+2)%4}) end, -}) -minetest.register_lbm({ - name = "advtrains:ramp_replacement_1", --- -- In the following two fields, also group:groupname will work. - nodenames = {"advtrains:track_vert2"}, - action = function(pos, node, active_object_count, active_object_count_wider) minetest.set_node(pos, {name="advtrains:dtrack_vst2", param2=(node.param2+2)%4}) end, -}) - minetest.register_abm({ - name = "advtrains:st_rep_1", - -- In the following two fields, also group:groupname will work. - nodenames = {"advtrains:track_st"}, - interval=1, - chance=1, - action = function(pos, node, active_object_count, active_object_count_wider) minetest.set_node(pos, {name="advtrains:dtrack_st", param2=node.param2}) end, - }) - minetest.register_lbm({ - name = "advtrains:st_rep_1", - -- -- In the following two fields, also group:groupname will work. - nodenames = {"advtrains:track_st_45"}, - action = function(pos, node, active_object_count, active_object_count_wider) minetest.set_node(pos, {name="advtrains:dtrack_st_45", param2=node.param2}) end, - }) -end +-- advtrains_train_track +-- rewritten to work with advtrains 2.5 track system + +local function conns(c1, c2, r1, r2) return {{c=c1, y=r1}, {c=c2, y=r2}} end +local function conns3(c1, c2, c3, r1, r2, r3) return {{c=c1, y=r1}, {c=c2, y=r2}, {c=c3, y=r3}} end + + +local common_def = { + drawtype = "mesh", + paramtype = "light", + paramtype2 = "facedir", + walkable = false, + selection_box = { + type = "fixed", + fixed = {-1/2-1/16, -1/2, -1/2, 1/2+1/16, -1/2+2/16, 1/2}, + }, + + mesh_suffix = ".b3d", + tiles = { "advtrains_dtrack_shared.png" }, + + groups = { + advtrains_track=1, + advtrains_track_default=1, + dig_immediate=2, + --not_in_creative_inventory=1, + }, + + can_dig = advtrains.track_can_dig_callback, + after_dig_node = advtrains.track_update_callback, + after_place_node = advtrains.track_update_callback, + + drop = "advtrains:dtrack_placer" +} + +-- Normal tracks, straight and curved +advtrains.register_node_4rot("advtrains:dtrack_st", + advtrains.merge_tables(common_def, { + description=attrans("Track Straight"), + mesh_prefix="advtrains_dtrack_st", + at_conns = conns(0,8), + advtrains = { + trackworker_next_var = "advtrains:dtrack_cr", + track_place_group = "advtrains:dtrack", + track_place_single = true, + }, + }) +) + +advtrains.register_node_4rot("advtrains:dtrack_cr", + advtrains.merge_tables(common_def, { + description=attrans("Track Curve"), + mesh_prefix="advtrains_dtrack_cr", + at_conns = conns(0,7), + advtrains = { + trackworker_next_var = "advtrains:dtrack_swlst", + track_place_group = "advtrains:dtrack", + }, + }) +) + +-- simple turnouts left and right + +local stm_left = { + st = "advtrains:dtrack_swlst", + cr = "advtrains:dtrack_swlcr", +} + +advtrains.register_node_4rot("advtrains:dtrack_swlst", + advtrains.merge_tables(common_def, { + description=attrans("Track Turnout Left Straight"), + mesh_prefix="advtrains_dtrack_swlst", + at_conns = conns3(0,8,7), + at_conn_map = {2,1,1}, + on_rightclick = advtrains.state_node_on_rightclick_callback, + advtrains = { + node_state = "st", + node_next_state = "cr", + node_state_map = stm_left, + trackworker_next_var = "advtrains:dtrack_swrst" + }, + }) +) + +advtrains.register_node_4rot("advtrains:dtrack_swlcr", + advtrains.merge_tables(common_def, { + description=attrans("Track Turnout Left Curve"), + mesh_prefix="advtrains_dtrack_swlcr", + at_conns = conns3(0,8,7), -- note: conns must stay identical + at_conn_map = {3,1,1}, -- now points to curve branch + on_rightclick = advtrains.state_node_on_rightclick_callback, + advtrains = { + node_state = "cr", + node_next_state = "st", + node_state_map = stm_left, + trackworker_next_var = "advtrains:dtrack_swrcr" + }, + }) +) + +local stm_right = { + st = "advtrains:dtrack_swrst", + cr = "advtrains:dtrack_swrcr", +} + +advtrains.register_node_4rot("advtrains:dtrack_swrst", + advtrains.merge_tables(common_def, { + description=attrans("Track Turnout Right Straight"), + mesh_prefix="advtrains_dtrack_swrst", + at_conns = conns3(0,8,9), + at_conn_map = {2,1,1}, + on_rightclick = advtrains.state_node_on_rightclick_callback, + advtrains = { + node_state = "st", + node_next_state = "cr", + node_state_map = stm_right, + trackworker_next_var = "advtrains:dtrack_st" + }, + }) +) + +advtrains.register_node_4rot("advtrains:dtrack_swrcr", + advtrains.merge_tables(common_def, { + description=attrans("Track Turnout Right Curve"), + mesh_prefix="advtrains_dtrack_swrcr", + at_conns = conns3(0,8,9), -- note: conns must stay identical + at_conn_map = {3,1,1}, -- now points to curve branch + on_rightclick = advtrains.state_node_on_rightclick_callback, + advtrains = { + node_state = "cr", + node_next_state = "st", + node_state_map = stm_right, + trackworker_next_var = "advtrains:dtrack_st" + }, + }) +) + +-- register placer item +minetest.register_craftitem(":advtrains:dtrack_placer", { + description = attrans("Track"), + inventory_image = "advtrains_dtrack_placer.png", + wield_image = "advtrains_dtrack_placer.png", + groups={advtrains_trackplacer=1, digtron_on_place=1}, + liquids_pointable = false, + on_place = function(itemstack, placer, pointed_thing) + local name = placer:get_player_name() + if not name then + return itemstack, false + end + if pointed_thing.type=="node" then + local pos=pointed_thing.above + local upos=vector.subtract(pointed_thing.above, {x=0, y=1, z=0}) + if not advtrains.check_track_protection(pos, name) then + return itemstack, false + end + if minetest.registered_nodes[minetest.get_node(pos).name] and minetest.registered_nodes[minetest.get_node(pos).name].buildable_to then + local s = minetest.registered_nodes[minetest.get_node(upos).name] and minetest.registered_nodes[minetest.get_node(upos).name].walkable + if s then +-- minetest.chat_send_all(nnprefix) + local yaw = placer:get_look_horizontal() + advtrains.trackplacer.place_track(pos, "advtrains:dtrack", name, yaw) + if not advtrains.is_creative(name) then + itemstack:take_item() + end + end + end + end + return itemstack, true + end, +}) + + +--TODO restore mesecons! \ No newline at end of file diff --git a/advtrains_train_track/oldinit.lua b/advtrains_train_track/oldinit.lua new file mode 100644 index 0000000..5065155 --- /dev/null +++ b/advtrains_train_track/oldinit.lua @@ -0,0 +1,937 @@ +-- Default tracks for advtrains +-- (c) orwell96 and contributors + +local default_boxen = { + ["st"] = { + [""] = { + selection_box = { + type = "fixed", + fixed = {-1/2-1/16, -1/2, -1/2, 1/2+1/16, -1/2+2/16, 1/2}, + } + }, + ["_30"] = { + selection_box = { + type = "fixed", + fixed = { + {-0.5000, -0.5000, -1.000, 0.5000, -0.3750, 1.000}, + {-0.8750, -0.5000, -1.000, -0.5000, -0.3750, 0.2500}, + {0.5000, -0.5000, -0.2500, 0.8750, -0.3750, 1.000}, + {-0.1250, -0.5000, -1.375, 0.1875, -0.3750, -1.000} + } + } + }, + ["_45"] = { + selection_box = { + type = "fixed", + fixed = { + {-0.5000, -0.5000, -0.8750, 0.5000, -0.3750, 0.8750}, + {0.5000, -0.5000, -0.5000, 0.8750, -0.3750, 0.5000}, + {-0.8750, -0.5000, -0.5000, -0.5000, -0.3750, 0.5000} + } + } + }, + ["_60"] = { + selection_box = { + type = "fixed", + fixed = { + {-1.000, -0.5000, -0.5000, 1.000, -0.3750, 0.5000}, + {-1.000, -0.5000, -0.8750, 0.2500, -0.3750, -0.5000}, + {-0.2500, -0.5000, 0.5000, 1.000, -0.3750, 0.8750}, + {-1.375, -0.5000, -0.1250, -1.000, -0.3750, 0.1875} + } + } + }, + }, + + ["cr"] = { + [""] = { + selection_box = { + type = "fixed", + fixed = { + {-0.5000, -0.5000, -0.5000, 0.6875, -0.3750, 0.5000}, + {-0.3750, -0.5000, -1.000, 1.000, -0.3750, 0.000} + } + } + }, + ["_30"] = { + selection_box = { + type = "fixed", + fixed = { + {-0.5000, -0.5000, -0.5000, 0.7500, -0.3750, 0.8750}, + {-0.3750, -0.5000, 0.8750, 0.2500, -0.3750, 1.188}, + {0.7500, -0.5000, 0.2500, 1.063, -0.3750, 0.8750} + } + } + }, + ["_45"] = { + selection_box = { + type = "fixed", + fixed = { + {-0.5000, -0.5000, -1.125, 0.5000, -0.3750, 0.6875}, + {-0.8750, -0.5000, -0.9375, -0.5000, -0.3750, 0.06250}, + {0.5000, -0.5000, -0.5000, 0.8750, -0.3750, 0.5000} + } + } + }, + ["_60"] = { + selection_box = { + type = "fixed", + fixed = { + {-0.8125, -0.5000, -0.5000, 1.188, -0.3750, 0.5000}, + {-0.1875, -0.5000, 0.5000, 0.8750, -0.3125, 0.8750}, + {-0.2500, -0.5000, -0.9375, 0.3125, -0.3125, -0.5000} + } + } + }, + }, + + ["swlst"] = { + [""] = { + selection_box = { + type = "fixed", + fixed = { + {-0.5000, -0.5000, -0.5000, 0.6250, -0.3750, 0.5000}, + {-0.3125, -0.5000, -1.000, 0.9375, -0.3125, -0.06250} + } + } + }, + ["_30"] = { + selection_box = { + type = "fixed", + fixed = { + {-0.5000, -0.5000, -1.000, 0.5000, -0.3750, 1.000}, + {-0.8750, -0.5000, -1.000, -0.5000, -0.3750, 0.2500}, + {0.5000, -0.5000, -0.2500, 0.8750, -0.3750, 1.000}, + {-0.1250, -0.5000, -1.375, 0.1875, -0.3750, -1.000} + } + } + }, + ["_45"] = { + selection_box = { + type = "fixed", + fixed = { + {-0.5000, -0.5000, -1.1875, 0.5000, -0.3750, 0.8750}, + {0.5000, -0.5000, -0.5000, 0.8750, -0.3750, 0.5000}, + {-0.8750, -0.5000, -0.8125, -0.5000, -0.3750, 0.5000} + } + } + }, + ["_60"] = { + selection_box = { + type = "fixed", + fixed = { + {-1.000, -0.5000, -0.5000, 1.000, -0.3750, 0.5000}, + {-1.000, -0.5000, -0.8750, 0.2500, -0.3750, -0.5000}, + {-0.2500, -0.5000, 0.5000, 1.000, -0.3750, 0.8750}, + {-1.375, -0.5000, -0.1250, -1.000, -0.3750, 0.1875} + } + } + }, + }, + + ["swrst"] = { + [""] = { + selection_box = { + type = "fixed", + fixed = { + {-0.5000, -0.5000, -0.5000, 0.6250, -0.3750, 0.5000}, + {-0.8125, -0.5000, -1.000, 0.4375, -0.3125, -0.06250} + } + } + }, + ["_30"] = { + selection_box = { + type = "fixed", + fixed = { + {-0.5000, -0.5000, -1.000, 0.5000, -0.3750, 1.000}, + {-0.8750, -0.5000, -1.000, -0.5000, -0.3750, 0.2500}, + {0.5000, -0.5000, -0.2500, 0.8750, -0.3750, 1.000}, + {-0.1250, -0.5000, -1.375, 0.1875, -0.3750, -1.000} + } + } + }, + ["_45"] = { + selection_box = { + type = "fixed", + fixed = { + {-1.1875, -0.5000, -0.5000, 0.8750, -0.3750, 0.5000}, + {-0.5000, -0.5000, 0.5000, 0.5000, -0.3750, 0.8750}, + {-0.8125, -0.5000, -0.8750, 0.5000, -0.3750, -0.5000} + } + } + }, + ["_60"] = { + selection_box = { + type = "fixed", + fixed = { + {-1.000, -0.5000, -0.5000, 1.000, -0.3750, 0.5000}, + {-1.000, -0.5000, -0.8750, 0.2500, -0.3750, -0.5000}, + {-0.2500, -0.5000, 0.5000, 1.000, -0.3750, 0.8750}, + {-1.375, -0.5000, -0.1250, -1.000, -0.3750, 0.1875} + } + } + }, + }, +} + +default_boxen["swlcr"] = default_boxen["swlst"] +default_boxen["swrcr"] = default_boxen["swrst"] + +--flat +advtrains.register_tracks("default", { + nodename_prefix="advtrains:dtrack", + texture_prefix="advtrains_dtrack", + models_prefix="advtrains_dtrack", + models_suffix=".b3d", + shared_texture="advtrains_dtrack_shared.png", + description=attrans("Track"), + formats={}, + + get_additional_definiton = function(def, preset, suffix, rotation) + if default_boxen[suffix] ~= nil and default_boxen[suffix][rotation] ~= nil then + return default_boxen[suffix][rotation] + else + return {} + end + end, +}, advtrains.ap.t_30deg_flat) + +minetest.register_craft({ + output = 'advtrains:dtrack_placer 50', + recipe = { + {'default:steel_ingot', 'group:stick', 'default:steel_ingot'}, + {'default:steel_ingot', 'group:stick', 'default:steel_ingot'}, + {'default:steel_ingot', 'group:stick', 'default:steel_ingot'}, + }, +}) + +local y3_boxen = { + [""] = { + selection_box = { + type = "fixed", + fixed = { + {-0.8750, -0.5000, -1.125, 0.8750, -0.3750, 0.4375} + } + } + }, + + ["_30"] = { + selection_box = { + type = "fixed", + fixed = { + {-0.5000, -0.5000, -0.875, 0.5000, -0.3750, 1.000}, + {-0.8750, -0.5000, -0.4375, -0.5000, -0.3750, 0.5625}, + {0.5000, -0.5000, -0.2500, 0.8125, -0.3750, 1.000}, + } + } + }, + + --UX FIXME: - 3way - have to place straight route before l and r or the + --nodebox overlaps too much and can't place the straight track node. + ["_45"] = { + selection_box = { + type = "fixed", + fixed = { + {-0.5000, -0.5000, -1.1250, 0.5000, -0.3750, 0.8750}, + {0.5000, -0.5000, -0.5000, 0.8750, -0.3750, 0.5000}, + {-1.1250, -0.5000, -0.9375, -0.5000, -0.3750, 0.5000} + } + } + }, + + ["_60"] = { + selection_box = { + type = "fixed", + fixed = { + --{-0.5000, -0.5000, -0.875, 0.5000, -0.3750, 1.000}, + {-0.875, -0.5000, -0.5, 1.0, -0.3750, 0.5}, + --{-0.8750, -0.5000, -0.4375, -0.5000, -0.3750, 0.5625}, + {-0.4375, -0.5000, -0.8750, 0.5625, -0.3750, -0.5000}, + --{0.5000, -0.5000, -0.2500, 0.8125, -0.3750, 1.000}, + {-0.2500, -0.5000, -0.2500, 1.0000, -0.3750, 0.8125}, + } + } + }, +} + + +local function y3_turnouts_addef(def, preset, suffix, rotation) + return y3_boxen[rotation] or {} +end +-- y-turnout +advtrains.register_tracks("default", { + nodename_prefix="advtrains:dtrack_sy", + texture_prefix="advtrains_dtrack_sy", + models_prefix="advtrains_dtrack_sy", + models_suffix=".obj", + shared_texture="advtrains_dtrack_shared.png", + description=attrans("Y-turnout"), + formats = {}, + get_additional_definiton = y3_turnouts_addef, +}, advtrains.ap.t_yturnout) +minetest.register_craft({ + output = 'advtrains:dtrack_sy_placer 2', + recipe = { + {'advtrains:dtrack_placer', '', 'advtrains:dtrack_placer'}, + {'', 'advtrains:dtrack_placer', ''}, + {'', 'advtrains:dtrack_placer', ''}, + }, +}) +--3-way turnout +advtrains.register_tracks("default", { + nodename_prefix="advtrains:dtrack_s3", + texture_prefix="advtrains_dtrack_s3", + models_prefix="advtrains_dtrack_s3", + models_suffix=".obj", + shared_texture="advtrains_dtrack_shared.png", + description=attrans("3-way turnout"), + formats = {}, + get_additional_definiton = y3_turnouts_addef, +}, advtrains.ap.t_s3way) +minetest.register_craft({ + output = 'advtrains:dtrack_s3_placer 1', + recipe = { + {'advtrains:dtrack_placer', 'advtrains:dtrack_placer', 'advtrains:dtrack_placer'}, + {'', 'advtrains:dtrack_placer', ''}, + {'', '', ''}, + }, +}) + +-- Diamond Crossings + +local perp_boxen = { + [""] = {}, --default size + ["_30"] = { + selection_box = { + type = "fixed", + fixed = { + {-1.000, -0.5000, -1.000, 1.000, -0.3750, 1.000} + } + } + }, + ["_45"] = { + selection_box = { + type = "fixed", + fixed = { + {-0.8125, -0.5000, -0.8125, 0.8125, -0.3750, 0.8125} + } + } + }, + ["_60"] = { + selection_box = { + type = "fixed", + fixed = { + {-1.000, -0.5000, -1.000, 1.000, -0.3750, 1.000} + } + } + }, +} + +-- perpendicular +advtrains.register_tracks("default", { + nodename_prefix="advtrains:dtrack_xing", + texture_prefix="advtrains_dtrack_xing", + models_prefix="advtrains_dtrack_xing", + models_suffix=".obj", + shared_texture="advtrains_dtrack_shared.png", + description=attrans("Perpendicular Diamond Crossing Track"), + formats = {}, + get_additional_definiton = function(def, preset, suffix, rotation) + return perp_boxen[rotation] or {} + end +}, advtrains.ap.t_perpcrossing) + +minetest.register_craft({ + output = 'advtrains:dtrack_xing_placer 3', + recipe = { + {'', 'advtrains:dtrack_placer', ''}, + {'advtrains:dtrack_placer', 'advtrains:dtrack_placer', 'advtrains:dtrack_placer'}, + {'', 'advtrains:dtrack_placer', ''} + } +}) + +local ninety_plus_boxen = { + ["30l"] = { + selection_box = { + type = "fixed", + fixed = { + {-0.5000, -0.5000, -1.000, 0.5000, -0.3750, 1.000}, + {-0.8750, -0.5000, -1.000, -0.5000, -0.3750, 0.2500}, + {0.5000, -0.5000, -0.2500, 0.8750, -0.3750, 1.000}, + {-0.1250, -0.5000, -1.375, 0.1875, -0.3750, -1.000} + } + } + }, + ["30r"] = { + selection_box = { + type = "fixed", + fixed = { + {0.5000, -0.5000, -1.000, -0.5000, -0.3750, 1.000}, + {0.8750, -0.5000, -1.000, 0.5000, -0.3750, 0.2500}, + {-0.5000, -0.5000, -0.2500, -0.8750, -0.3750, 1.000}, + {0.1250, -0.5000, -1.375, -0.1875, -0.3750, -1.000} + } + } + }, + ["45l"] = { + selection_box = { + type = "fixed", + fixed = { + {-0.5000, -0.5000, -0.8750, 0.5000, -0.3750, 0.8750}, + {0.5000, -0.5000, -0.5000, 0.8750, -0.3750, 0.5000}, + {-0.8750, -0.5000, -0.5000, -0.5000, -0.3750, 0.5000} + } + } + }, + ["45r"] = { + selection_box = { + type = "fixed", + fixed = { + {-0.5000, -0.5000, -0.8750, 0.5000, -0.3750, 0.8750}, + {0.5000, -0.5000, -0.5000, 0.8750, -0.3750, 0.5000}, + {-0.8750, -0.5000, -0.5000, -0.5000, -0.3750, 0.5000} + } + } + }, + ["60l"] = { + selection_box = { + type = "fixed", + fixed = { + {-1.000, -0.5000, -0.5000, 1.000, -0.3750, 0.5000}, + {-1.000, -0.5000, -0.8750, 0.2500, -0.3750, -0.5000}, + {-0.2500, -0.5000, 0.5000, 1.000, -0.3750, 0.8750}, + {-1.375, -0.5000, -0.1250, -1.000, -0.3750, 0.1875} + } + } + }, + ["60r"] = { + selection_box = { + type = "fixed", + fixed = { + {1.000, -0.5000, -0.5000, -1.000, -0.3750, 0.5000}, + {1.000, -0.5000, -0.8750, -0.2500, -0.3750, -0.5000}, + {0.2500, -0.5000, 0.5000, -1.000, -0.3750, 0.8750}, + {1.375, -0.5000, -0.1250, 1.000, -0.3750, 0.1875} + } + } + }, +} + +-- 90plusx +-- When you face east and param2=0, then this set of rails has a rail at 90 +-- degrees to the viewer, plus another rail crossing at 30, 45 or 60 degrees. +advtrains.register_tracks("default", { + nodename_prefix="advtrains:dtrack_xing90plusx", + texture_prefix="advtrains_dtrack_xing4590", + models_prefix="advtrains_dtrack_xing90plusx", + models_suffix=".obj", + shared_texture="advtrains_dtrack_shared.png", + description=attrans("90+Angle Diamond Crossing Track"), + formats = {}, + get_additional_definiton = function(def, preset, suffix, rotation) + return ninety_plus_boxen[suffix] or {} + end, +}, advtrains.ap.t_90plusx_crossing) +minetest.register_craft({ + output = 'advtrains:dtrack_xing90plusx_placer 2', + recipe = { + {'advtrains:dtrack_placer', '', ''}, + {'advtrains:dtrack_placer', 'advtrains:dtrack_placer', 'advtrains:dtrack_placer'}, + {'', '', 'advtrains:dtrack_placer'} + } +}) + +-- Deprecate any rails using the old name scheme +minetest.register_lbm({ + label = "Upgrade legacy 4590 rails", + name = "advtrains_train_track:replace_legacy_4590", + nodenames = {"advtrains:dtrack_xing4590_st"}, + run_at_every_load = true, + action = function(pos, node) + minetest.log("actionPos!: " .. pos.x .. "," .. pos.y .. "," .. pos.z) + minetest.log("node!: " .. node.name .. "," .. node.param1 .. "," .. node.param2) + advtrains.ndb.swap_node(pos, + { + name="advtrains:dtrack_xing90plusx_45l", + param1=node.param1, + param2=node.param2, + }) + end +}) +-- This will replace any items left in the inventory +minetest.register_alias("advtrains:dtrack_xing4590_placer", "advtrains:dtrack_xing90plusx_placer") + +local diagonal_boxen = { + ["30r45l"] = { + selection_box = { + type = "fixed", + fixed = { + {0.5000, -0.5000, -1.000, -0.5000, -0.3750, 1.000}, + {0.8750, -0.5000, -1.000, 0.5000, -0.3750, 0.2500}, + {-0.5000, -0.5000, -0.2500, -0.8750, -0.3750, 1.000}, + {0.1250, -0.5000, -1.375, -0.1875, -0.3750, -1.000} + } + } + }, + ["60l30l"] = { + selection_box = { + type = "fixed", + fixed = { + {-1.000, -0.5000, -0.5000, 1.000, -0.3750, 0.5000}, + {-1.000, -0.5000, -0.8750, 0.2500, -0.3750, -0.5000}, + {-0.2500, -0.5000, 0.5000, 1.000, -0.3750, 0.8750}, + {-1.375, -0.5000, -0.1250, -1.000, -0.3750, 0.1875} + } + } + }, + ["60l60r"] = { + selection_box = { + type = "fixed", + fixed = { + {-1.000, -0.5000, -1.000, 1.000, -0.3750, 1.000} + } + } + }, + ["60r30r"] = { + selection_box = { + type = "fixed", + fixed = { + {1.000, -0.5000, -0.5000, -1.000, -0.3750, 0.5000}, + {1.000, -0.5000, -0.8750, -0.2500, -0.3750, -0.5000}, + {0.2500, -0.5000, 0.5000, -1.000, -0.3750, 0.8750}, + {1.375, -0.5000, -0.1250, 1.000, -0.3750, 0.1875} + } + } + }, + ["30l45r"] = { + selection_box = { + type = "fixed", + fixed = { + {-0.5000, -0.5000, -1.000, 0.5000, -0.3750, 1.000}, + {-0.8750, -0.5000, -1.000, -0.5000, -0.3750, 0.2500}, + {0.5000, -0.5000, -0.2500, 0.8750, -0.3750, 1.000}, + {-0.1250, -0.5000, -1.375, 0.1875, -0.3750, -1.000} + } + } + }, + ["60l45r"] = { + selection_box = { + type = "fixed", + fixed = { + {-1.000, -0.5000, -0.5000, 1.000, -0.3750, 0.5000}, + {-1.000, -0.5000, -0.8750, 0.2500, -0.3750, -0.5000}, + {-0.2500, -0.5000, 0.5000, 1.000, -0.3750, 0.8750}, + {-1.375, -0.5000, -0.1250, -1.000, -0.3750, 0.1875} + } + } + }, + ["60r45l"] = { + selection_box = { + type = "fixed", + fixed = { + {1.000, -0.5000, -0.5000, -1.000, -0.3750, 0.5000}, + {1.000, -0.5000, -0.8750, -0.2500, -0.3750, -0.5000}, + {0.2500, -0.5000, 0.5000, -1.000, -0.3750, 0.8750}, + {1.375, -0.5000, -0.1250, 1.000, -0.3750, 0.1875} + } + } + }, +} + +-- Diagonal +-- This set of rail crossings is named based on the angle of each intersecting +-- direction when facing east and param2=0. Rails with l/r swapped are mirror +-- images. For example, 30r45l is the mirror image of 30l45r. +advtrains.register_tracks("default", { + nodename_prefix="advtrains:dtrack_xingdiag", + texture_prefix="advtrains_dtrack_xingdiag", + models_prefix="advtrains_dtrack_xingdiag", + models_suffix=".obj", + shared_texture="advtrains_dtrack_shared.png", + description=attrans("Diagonal Diamond Crossing Track"), + formats = {}, + get_additional_definiton = function(def, preset, suffix, rotation) + return diagonal_boxen[suffix] or {} + end, +}, advtrains.ap.t_diagonalcrossing) +minetest.register_craft({ + output = 'advtrains:dtrack_xingdiag_placer 2', + recipe = { + {'advtrains:dtrack_placer', '', 'advtrains:dtrack_placer'}, + {'', 'advtrains:dtrack_placer', ''}, + {'advtrains:dtrack_placer', '', 'advtrains:dtrack_placer'} + } +}) +---- Not included: very shallow crossings like (30/60)+45. +---- At an angle of only 18.4 degrees, the models would not +---- translate well to a block game. +-- END crossings + +--slopes +advtrains.register_tracks("default", { + nodename_prefix="advtrains:dtrack", + texture_prefix="advtrains_dtrack", + models_prefix="advtrains_dtrack", + models_suffix=".obj", + shared_texture="advtrains_dtrack_shared.png", + second_texture="default_gravel.png", + description=attrans("Track"), + formats={vst1={true, false, true}, vst2={true, false, true}, vst31={true}, vst32={true}, vst33={true}}, +}, advtrains.ap.t_30deg_slope) + +minetest.register_craft({ + type = "shapeless", + output = 'advtrains:dtrack_slopeplacer 2', + recipe = { + "advtrains:dtrack_placer", + "advtrains:dtrack_placer", + "default:gravel", + }, +}) + + +--bumpers +advtrains.register_tracks("default", { + nodename_prefix="advtrains:dtrack_bumper", + texture_prefix="advtrains_dtrack_bumper", + models_prefix="advtrains_dtrack_bumper", + models_suffix=".b3d", + shared_texture="advtrains_dtrack_rail.png", + --bumpers still use the old texture until the models are redone. + description=attrans("Bumper"), + formats={}, +}, advtrains.ap.t_30deg_straightonly) +minetest.register_craft({ + output = 'advtrains:dtrack_bumper_placer 2', + recipe = { + {'group:wood', 'dye:red'}, + {'default:steel_ingot', 'default:steel_ingot'}, + {'advtrains:dtrack_placer', 'advtrains:dtrack_placer'}, + }, +}) +--legacy bumpers +for _,rot in ipairs({"", "_30", "_45", "_60"}) do + minetest.register_alias("advtrains:dtrack_bumper"..rot, "advtrains:dtrack_bumper_st"..rot) +end +-- atc track +advtrains.register_tracks("default", { + nodename_prefix="advtrains:dtrack_atc", + texture_prefix="advtrains_dtrack_atc", + models_prefix="advtrains_dtrack", + models_suffix=".b3d", + shared_texture="advtrains_dtrack_shared_atc.png", + description=attrans("ATC controller"), + formats={}, + get_additional_definiton = advtrains.atc_function +}, advtrains.trackpresets.t_30deg_straightonly) + + +-- Tracks for loading and unloading trains +-- Copyright (C) 2017 Gabriel Pérez-Cerezo + +local function get_far_node(pos) + local node = minetest.get_node(pos) + if node.name == "ignore" then + minetest.get_voxel_manip():read_from_map(pos, pos) + node = minetest.get_node(pos) + end + return node +end + + +local function show_fc_formspec(pos,player) + local pname = player:get_player_name() + if minetest.is_protected(pos,pname) then + minetest.chat_send_player(pname, "Position is protected!") + return + end + + local meta = minetest.get_meta(pos) + local fc = meta:get_string("fc") or "" + + local form = 'formspec_version[4]'.. + 'size[10,5]'.. + 'label[0.5,0.4;Advtrains Loading/Unloading Track]'.. + 'label[0.5,1.1;Set the code to match against the wagon\'s freight code]'.. + 'label[0.5,1.6;A blank field matches all wagons (default)]'.. + 'label[0.5,2.1;Use code # to disable the track section]'.. + 'field[0.5,3;5.5,1;fc;FC;'..minetest.formspec_escape(fc)..']'.. + 'button[6.5,3;3,1;save;Submit]' + minetest.show_formspec(pname, "at_load_unload_"..advtrains.encode_pos(pos), form) +end + +minetest.register_on_player_receive_fields(function(player, formname, fields) + local pname = player:get_player_name() + local pe = string.match(formname, "^at_load_unload_(............)$") + local pos = advtrains.decode_pos(pe) + if pos then + if minetest.is_protected(pos, pname) then + minetest.chat_send_player(pname, "Position is protected!") + return + end + + if fields.save then + minetest.get_meta(pos):set_string("fc",tostring(fields.fc)) + minetest.chat_send_player(pname,"Freight code set: "..tostring(fields.fc)) + show_fc_formspec(pos,player) + end + end +end) + + +local function train_load(pos, train_id, unload) + local train=advtrains.trains[train_id] + local below = get_far_node({x=pos.x, y=pos.y-1, z=pos.z}) + if not string.match(below.name, "chest") then + atprint("this is not a chest! at "..minetest.pos_to_string(pos)) + return + end + + local node_fc = minetest.get_meta(pos):get_string("fc") or "" + if node_fc == "#" then + --track section is disabled + return + end + + local inv = minetest.get_inventory({type="node", pos={x=pos.x, y=pos.y-1, z=pos.z}}) + if inv and train.velocity < 2 then + for k, v in ipairs(train.trainparts) do + local i=minetest.get_inventory({type="detached", name="advtrains_wgn_"..v}) + if i and i:get_list("box") then + + local wagon_data = advtrains.wagons[v] + local wagon_fc + if wagon_data.fc then + if not wagon_data.fcind then wagon_data.fcind = 1 end + wagon_fc = tostring(wagon_data.fc[wagon_data.fcind]) or "" + end + + if node_fc == "" or wagon_fc == node_fc then + if not unload then + for _, item in ipairs(inv:get_list("main")) do + if i:get_list("box") and i:room_for_item("box", item) then + i:add_item("box", item) + inv:remove_item("main", item) + end + end + else + for _, item in ipairs(i:get_list("box")) do + if inv:get_list("main") and inv:room_for_item("main", item) then + i:remove_item("box", item) + inv:add_item("main", item) + end + end + end + end + end + end + end +end + + + +advtrains.register_tracks("default", { + nodename_prefix="advtrains:dtrack_unload", + texture_prefix="advtrains_dtrack_unload", + models_prefix="advtrains_dtrack", + models_suffix=".b3d", + shared_texture="advtrains_dtrack_shared_unload.png", + description=attrans("Unloading Track"), + formats={}, + get_additional_definiton = function(def, preset, suffix, rotation) + return { + after_dig_node=function(pos) + advtrains.invalidate_all_paths() + advtrains.ndb.clear(pos) + end, + on_rightclick = function(pos, node, player) + show_fc_formspec(pos, player) + end, + advtrains = { + on_train_enter = function(pos, train_id) + train_load(pos, train_id, true) + end, + }, + } + end + }, advtrains.trackpresets.t_30deg_straightonly) +advtrains.register_tracks("default", { + nodename_prefix="advtrains:dtrack_load", + texture_prefix="advtrains_dtrack_load", + models_prefix="advtrains_dtrack", + models_suffix=".b3d", + shared_texture="advtrains_dtrack_shared_load.png", + description=attrans("Loading Track"), + formats={}, + get_additional_definiton = function(def, preset, suffix, rotation) + return { + after_dig_node=function(pos) + advtrains.invalidate_all_paths() + advtrains.ndb.clear(pos) + end, + on_rightclick = function(pos, node, player) + show_fc_formspec(pos, player) + end, + advtrains = { + on_train_enter = function(pos, train_id) + train_load(pos, train_id, false) + end, + }, + } + end + }, advtrains.trackpresets.t_30deg_straightonly) + +-- mod-dependent crafts +local loader_core = "default:mese_crystal" --fallback +if minetest.get_modpath("basic_materials") then + loader_core = "basic_materials:ic" +elseif minetest.get_modpath("technic") then + loader_core = "technic:control_logic_unit" +end +--print("Loader Core: "..loader_core) + +minetest.register_craft({ + type="shapeless", + output = 'advtrains:dtrack_load_placer', + recipe = { + "advtrains:dtrack_placer", + loader_core, + "default:chest" + }, +}) +loader_core = nil --nil the crafting variable + +--craft between load/unload tracks +minetest.register_craft({ + type="shapeless", + output = 'advtrains:dtrack_unload_placer', + recipe = { + "advtrains:dtrack_load_placer", + }, +}) +minetest.register_craft({ + type="shapeless", + output = 'advtrains:dtrack_load_placer', + recipe = { + "advtrains:dtrack_unload_placer", + }, +}) + + +if mesecon then + advtrains.register_tracks("default", { + nodename_prefix="advtrains:dtrack_detector_off", + texture_prefix="advtrains_dtrack_detector", + models_prefix="advtrains_dtrack", + models_suffix=".b3d", + shared_texture="advtrains_dtrack_shared_detector_off.png", + description=attrans("Detector Rail"), + formats={}, + get_additional_definiton = function(def, preset, suffix, rotation) + return { + mesecons = { + receptor = { + state = mesecon.state.off, + rules = advtrains.meseconrules + } + }, + advtrains = { + on_updated_from_nodedb = function(pos, node) + mesecon.receptor_off(pos, advtrains.meseconrules) + end, + on_train_enter=function(pos, train_id) + advtrains.ndb.swap_node(pos, {name="advtrains:dtrack_detector_on".."_"..suffix..rotation, param2=advtrains.ndb.get_node(pos).param2}) + if advtrains.is_node_loaded(pos) then + mesecon.receptor_on(pos, advtrains.meseconrules) + end + end + } + } + end + }, advtrains.ap.t_30deg_straightonly) + advtrains.register_tracks("default", { + nodename_prefix="advtrains:dtrack_detector_on", + texture_prefix="advtrains_dtrack", + models_prefix="advtrains_dtrack", + models_suffix=".b3d", + shared_texture="advtrains_dtrack_shared_detector_on.png", + description="Detector(on)(you hacker you)", + formats={}, + get_additional_definiton = function(def, preset, suffix, rotation) + return { + mesecons = { + receptor = { + state = mesecon.state.on, + rules = advtrains.meseconrules + } + }, + advtrains = { + on_updated_from_nodedb = function(pos, node) + mesecon.receptor_on(pos, advtrains.meseconrules) + end, + on_train_leave=function(pos, train_id) + advtrains.ndb.swap_node(pos, {name="advtrains:dtrack_detector_off".."_"..suffix..rotation, param2=advtrains.ndb.get_node(pos).param2}) + if advtrains.is_node_loaded(pos) then + mesecon.receptor_off(pos, advtrains.meseconrules) + end + end + } + } + end + }, advtrains.ap.t_30deg_straightonly_noplacer) +minetest.register_craft({ + type="shapeless", + output = 'advtrains:dtrack_detector_off_placer', + recipe = { + "advtrains:dtrack_placer", + "mesecons:wire_00000000_off" + }, +}) +end +--TODO legacy +--I know lbms are better for this purpose +for name,rep in pairs({swl_st="swlst", swr_st="swrst", swl_cr="swlcr", swr_cr="swrcr", }) do + minetest.register_abm({ + -- In the following two fields, also group:groupname will work. + nodenames = {"advtrains:track_"..name}, + interval = 1.0, -- Operation interval in seconds + chance = 1, -- Chance of trigger per-node per-interval is 1.0 / this + action = function(pos, node, active_object_count, active_object_count_wider) minetest.set_node(pos, {name="advtrains:track_"..rep, param2=node.param2}) end, + }) + minetest.register_abm({ + -- In the following two fields, also group:groupname will work. + nodenames = {"advtrains:track_"..name.."_45"}, + interval = 1.0, -- Operation interval in seconds + chance = 1, -- Chance of trigger per-node per-interval is 1.0 / this + action = function(pos, node, active_object_count, active_object_count_wider) minetest.set_node(pos, {name="advtrains:track_"..rep.."_45", param2=node.param2}) end, + }) +end + +if advtrains.register_replacement_lbms then +minetest.register_lbm({ + name = "advtrains:ramp_replacement_1", +-- In the following two fields, also group:groupname will work. + nodenames = {"advtrains:track_vert1"}, + action = function(pos, node, active_object_count, active_object_count_wider) minetest.set_node(pos, {name="advtrains:dtrack_vst1", param2=(node.param2+2)%4}) end, +}) +minetest.register_lbm({ + name = "advtrains:ramp_replacement_1", +-- -- In the following two fields, also group:groupname will work. + nodenames = {"advtrains:track_vert2"}, + action = function(pos, node, active_object_count, active_object_count_wider) minetest.set_node(pos, {name="advtrains:dtrack_vst2", param2=(node.param2+2)%4}) end, +}) + minetest.register_abm({ + name = "advtrains:st_rep_1", + -- In the following two fields, also group:groupname will work. + nodenames = {"advtrains:track_st"}, + interval=1, + chance=1, + action = function(pos, node, active_object_count, active_object_count_wider) minetest.set_node(pos, {name="advtrains:dtrack_st", param2=node.param2}) end, + }) + minetest.register_lbm({ + name = "advtrains:st_rep_1", + -- -- In the following two fields, also group:groupname will work. + nodenames = {"advtrains:track_st_45"}, + action = function(pos, node, active_object_count, active_object_count_wider) minetest.set_node(pos, {name="advtrains:dtrack_st_45", param2=node.param2}) end, + }) +end -- cgit v1.2.3 From ad82b9cd4e12e16a176a2af08d480fa939076515 Mon Sep 17 00:00:00 2001 From: orwell Date: Sun, 15 Oct 2023 15:06:20 +0200 Subject: Forget it, and use the old preset tables for track registration. Just adapt it to the new definition. --- advtrains/init.lua | 1 + advtrains/track_reg_helper.lua | 743 ++++++++++++++++++++++++ advtrains/trackplacer.lua | 36 +- advtrains/tracks.lua | 24 +- advtrains_interlocking/init.lua | 3 +- advtrains_line_automation/init.lua | 3 +- advtrains_train_track/init.lua | 1122 ++++++++++++++++++++++++++++++------ advtrains_train_track/oldinit.lua | 937 ------------------------------ 8 files changed, 1720 insertions(+), 1149 deletions(-) create mode 100644 advtrains/track_reg_helper.lua mode change 100755 => 100644 advtrains_train_track/init.lua delete mode 100644 advtrains_train_track/oldinit.lua (limited to 'advtrains_interlocking/init.lua') diff --git a/advtrains/init.lua b/advtrains/init.lua index 2213937..78126e5 100644 --- a/advtrains/init.lua +++ b/advtrains/init.lua @@ -206,6 +206,7 @@ dofile(advtrains.modpath.."/trainhud.lua") dofile(advtrains.modpath.."/trackplacer.lua") dofile(advtrains.modpath.."/copytool.lua") dofile(advtrains.modpath.."/tracks.lua") +dofile(advtrains.modpath.."/track_reg_helper.lua") dofile(advtrains.modpath.."/occupation.lua") dofile(advtrains.modpath.."/atc.lua") dofile(advtrains.modpath.."/wagons.lua") diff --git a/advtrains/track_reg_helper.lua b/advtrains/track_reg_helper.lua new file mode 100644 index 0000000..ad73a40 --- /dev/null +++ b/advtrains/track_reg_helper.lua @@ -0,0 +1,743 @@ +-- New track registration helper +-- Retains the old table-template-based definition format, but adapts it to the new (advtrains 2.5) +-- track definition system +-- Note to future: This is actually just a work-saver, avoiding me to port over all the crossing nodes as well as the linetrack tracks. +-- Future track mods should please directly use the appropriate advtrains.register_node_4rot() API and not rely on this! + +--definition preparation +local function conns(c1, c2, r1, r2) return {{c=c1, y=r1}, {c=c2, y=r2}} end +local function conns3(c1, c2, c3, r1, r2, r3) return {{c=c1, y=r1}, {c=c2, y=r2}, {c=c3, y=r3}} end + +advtrains.ap={} +advtrains.ap.t_30deg_flat={ + v25_format = true, + regstep=1, + variant={ + st={ + conns = conns(0,8), + desc = "straight", + tpdouble = true, + tpsingle = true, + trackworker = "cr", + }, + cr={ + conns = conns(0,7), + desc = "curve", + tpdouble = true, + trackworker = "swlst", + }, + swlst={ + conns = conns3(0,8,7), + desc = "left switch (straight)", + trackworker = "swrst", + switchalt = "cr", + switchmc = "on", + switchst = "st", + switchprefix = "swl", + conn_map = {2,1,1}, + stmref = "swl", + }, + swlcr={ + conns = conns3(0,8,7), + desc = "left switch (curve)", + trackworker = "swrcr", + switchalt = "st", + switchmc = "off", + switchst = "cr", + switchprefix = "swl", + conn_map = {3,1,1}, + stmref = "swl", + }, + swrst={ + conns = conns3(0,8,9), + desc = "right switch (straight)", + trackworker = "st", + switchalt = "cr", + switchmc = "on", + switchst = "st", + switchprefix = "swr", + conn_map = {2,1,1}, + stmref = "swr", + }, + swrcr={ + conns = conns3(0,8,9), + desc = "right switch (curve)", + trackworker = "st", + switchalt = "st", + switchmc = "off", + switchst = "cr", + switchprefix = "swr", + conn_map = {3,1,1}, + stmref = "swr", + }, + }, + regtp=true, + tpdefault="st", + trackworker={ + ["swrcr"]="st", + ["swrst"]="st", + ["cr"]="swlst", + ["swlcr"]="swrcr", + ["swlst"]="swrst", + }, + rotation={"", "_30", "_45", "_60"}, + statemaps = { + swl = { st = "swlst", cr = "swlcr"}, + swr = { st = "swrst", cr = "swrcr"} + } +} +advtrains.ap.t_yturnout={ + v25_format = true, + regstep=1, + variant={ + l={ + conns = conns3(0,7,9), + desc = "Y-turnout (left)", + switchalt = "r", + switchmc = "off", + switchst = "l", + switchprefix = "", + conn_map = {2,1,1}, + stmref = "sw", + tpsingle = true, + }, + r={ + conns = conns3(0,7,9), + desc = "Y-turnout (right)", + switchalt = "l", + switchmc = "on", + switchst = "r", + switchprefix = "", + conn_map = {3,1,1}, + stmref = "sw", + } + }, + regtp=true, + tpdefault="l", + rotation={"", "_30", "_45", "_60"}, + statemaps = { + sw = { l = "l", r = "r"} + } +} +advtrains.ap.t_s3way={ + v25_format = true, + regstep=1, + variant={ + l={ + conns = { {c=0}, {c=7}, {c=8}, {c=9}}, + desc = "3-way turnout (left)", + switchalt = "s", + switchst="l", + switchprefix = "", + conn_map = {2,1,1}, + stmref = "sw", + }, + s={ + conns = { {c=0}, {c=7}, {c=8}, {c=9}}, + desc = "3-way turnout (straight)", + switchalt ="r", + switchst = "s", + switchprefix = "", + conn_map = {3,1,1}, + stmref = "sw", + tpsingle = true, + }, + r={ + conns = { {c=0}, {c=7}, {c=8}, {c=9}}, + desc = "3-way turnout (right)", + switchalt = "l", + switchst="r", + switchprefix = "", + conn_map = {4,1,1}, + stmref = "sw", + } + }, + regtp=true, + tpdefault="l", + rotation={"", "_30", "_45", "_60"}, + statemaps = { + sw = { l = "l", s = "s", r = "r"} + } +} +advtrains.ap.t_30deg_slope={ + v25_format = true, + regstep=1, + variant={ + vst1={conns = conns(8,0,0,0.5), rail_y = 0.25, desc = "steep uphill 1/2", slope=true}, + vst2={conns = conns(8,0,0.5,1), rail_y = 0.75, desc = "steep uphill 2/2", slope=true}, + vst31={conns = conns(8,0,0,0.33), rail_y = 0.16, desc = "uphill 1/3", slope=true}, + vst32={conns = conns(8,0,0.33,0.66), rail_y = 0.5, desc = "uphill 2/3", slope=true}, + vst33={conns = conns(8,0,0.66,1), rail_y = 0.83, desc = "uphill 3/3", slope=true}, + }, + regsp=true, + slopeplacer={ + [2]={"vst1", "vst2"}, + [3]={"vst31", "vst32", "vst33"}, + max=3,--highest entry + }, + slopeplacer_45={ + [2]={"vst1_45", "vst2_45"}, + max=2, + }, + rotation={"", "_30", "_45", "_60"}, + trackworker={}, + increativeinv={}, +} +advtrains.ap.t_30deg_straightonly={ + v25_format = true, + regstep=1, + variant={ + st={ + conns = conns(0,8), + desc = "straight", + tpdouble = true, + tpsingle = true, + trackworker = "st", + }, + }, + regtp=true, + tpdefault="st", + rotation={"", "_30", "_45", "_60"}, +} +advtrains.ap.t_30deg_straightonly_noplacer={ + v25_format = true, + regstep=1, + variant={ + st={ + conns = conns(0,8), + desc = "straight", + tpdouble = true, + tpsingle = true, + trackworker = "st", + }, + }, + tpdefault="st", + rotation={"", "_30", "_45", "_60"}, +} +advtrains.ap.t_45deg={ + v25_format = true, + regstep=2, + variant={ + st={ + conns = conns(0,8), + desc = "straight", + tpdouble = true, + tpsingle = true, + trackworker = "cr", + }, + cr={ + conns = conns(0,6), + desc = "curve", + tpdouble = true, + trackworker = "swlst", + }, + swlst={ + conns = conns3(0,8,6), + desc = "left switch (straight)", + trackworker = "swrst", + switchalt = "cr", + switchmc = "on", + switchst = "st", + }, + swlcr={ + conns = conns3(0,6,8), + desc = "left switch (curve)", + trackworker = "swrcr", + switchalt = "st", + switchmc = "off", + switchst = "cr", + }, + swrst={ + conns = conns3(0,8,10), + desc = "right switch (straight)", + trackworker = "st", + switchalt = "cr", + switchmc = "on", + switchst = "st", + }, + swrcr={ + conns = conns3(0,10,8), + desc = "right switch (curve)", + trackworker = "st", + switchalt = "st", + switchmc = "off", + switchst = "cr", + }, + }, + regtp=true, + tpdefault="st", + trackworker={ + ["swrcr"]="st", + ["swrst"]="st", + ["cr"]="swlst", + ["swlcr"]="swrcr", + ["swlst"]="swrst", + }, + rotation={"", "_30", "_45", "_60"}, +} +advtrains.ap.t_perpcrossing={ + v25_format = true, + regstep = 1, + variant={ + st={ + conns = { {c=0}, {c=8}, {c=4}, {c=12} }, + desc = "perpendicular crossing", + tpdouble = true, + tpsingle = true, + trackworker = "st", + conn_map = {2,1,4,3}, + }, + }, + regtp=true, + tpdefault="st", + rotation={"", "_30", "_45", "_60"}, +} +advtrains.ap.t_90plusx_crossing={ + v25_format = true, + regstep = 1, + variant={ + ["30l"]={ + conns = { {c=0}, {c=8}, {c=1}, {c=9} }, + desc = "30/90 degree crossing (left)", + tpdouble = true, + tpsingle = true, + trackworker = "45l", + conn_map = {2,1,4,3}, + }, + ["45l"]={ + conns = { {c=0}, {c=8}, {c=2}, {c=10} }, + desc = "45/90 degree crossing (left)", + tpdouble = true, + tpsingle = true, + trackworker = "60l", + conn_map = {2,1,4,3}, + }, + ["60l"]={ + conns = { {c=0}, {c=8}, {c=3}, {c=11}}, + desc = "60/90 degree crossing (left)", + tpdouble = true, + tpsingle = true, + trackworker = "60r", + conn_map = {2,1,4,3}, + }, + ["60r"]={ + conns = { {c=0}, {c=8}, {c=5}, {c=13} }, + desc = "60/90 degree crossing (right)", + tpdouble = true, + tpsingle = true, + trackworker = "45r", + conn_map = {2,1,4,3}, + }, + ["45r"]={ + conns = { {c=0}, {c=8}, {c=6}, {c=14} }, + desc = "45/90 degree crossing (right)", + tpdouble = true, + tpsingle = true, + trackworker = "30r", + conn_map = {2,1,4,3}, + }, + ["30r"]={ + conns = { {c=0}, {c=8}, {c=7}, {c=15}}, + desc = "30/90 degree crossing (right)", + tpdouble = true, + tpsingle = true, + trackworker = "30l", + conn_map = {2,1,4,3}, + }, + }, + regtp=true, + tpdefault="30l", + rotation={""}, + trackworker = { + ["30l"] = "45l", + ["45l"] = "60l", + ["60l"] = "60r", + ["60r"] = "45r", + ["45r"] = "30r", + ["30r"] = "30l", + } +} + +advtrains.ap.t_diagonalcrossing = { + v25_format = true, + regstep=1, + variant={ + ["30l45r"]={ + conns = {{c=1}, {c=9}, {c=6}, {c=14}}, + desc = "30left-45right diagonal crossing", + tpdouble=true, + tpsingle=true, + trackworker="60l30l", + conn_map = {2,1,4,3}, + }, + ["60l30l"]={ + conns = {{c=3}, {c=11}, {c=1}, {c=9}}, + desc = "30left-60right diagonal crossing", + tpdouble=true, + tpsingle=true, + trackworker="60l45r", + conn_map = {2,1,4,3}, + }, + ["60l45r"]={ + conns = {{c=3}, {c=11}, {c=6}, {c=14}}, + desc = "60left-45right diagonal crossing", + tpdouble=true, + tpsingle=true, + trackworker="60l60r", + conn_map = {2,1,4,3}, + }, + ["60l60r"]={ + conns = {{c=3}, {c=11}, {c=5}, {c=13}}, + desc = "60left-60right diagonal crossing", + tpdouble=true, + tpsingle=true, + trackworker="60r45l", + conn_map = {2,1,4,3}, + }, + --If 60l60r had a mirror image, it would be here, but it's symmetric. + -- 60l60r is also equivalent to 30l30r but rotated 90 degrees. + ["60r45l"]={ + conns = {{c=5}, {c=13}, {c=2}, {c=10}}, + desc = "60right-45left diagonal crossing", + tpdouble=true, + tpsingle=true, + trackworker="60r30r", + conn_map = {2,1,4,3}, + }, + ["60r30r"]={ + conns = {{c=5}, {c=13}, {c=7}, {c=15}}, + desc = "60right-30right diagonal crossing", + tpdouble=true, + tpsingle=true, + trackworker="30r45l", + conn_map = {2,1,4,3}, + }, + ["30r45l"]={ + conns = {{c=7}, {c=15}, {c=2}, {c=10}}, + desc = "30right-45left diagonal crossing", + tpdouble=true, + tpsingle=true, + trackworker="30l45r", + conn_map = {2,1,4,3}, + }, + + }, + regtp=true, + tpdefault="30l45r", + rotation={""}, + trackworker = { + ["30l45r"] = "60l30l", + ["60l30l"] = "60l45r", + ["60l45r"] = "60l60r", + ["60l60r"] = "60r45l", + ["60r45l"] = "60r30r", + ["60r30r"] = "30r45l", + ["30r45l"] = "30l45r", + } +} + +advtrains.trackpresets = advtrains.ap + +--definition format: ([] optional) +--[[{ + nodename_prefix + texture_prefix + [shared_texture] + models_prefix + models_suffix (with dot) + [shared_model] + formats={ + st,cr,swlst,swlcr,swrst,swrcr,vst1,vst2 + (each a table with indices 0-3, for if to register a rail with this 'rotation' table entry. nil is assumed as 'all', set {} to not register at all) + } + common={} change something on common rail appearance +} +[18.12.17] Note on new connection system: +In order to support real rail crossing nodes and finally make the trackplacer respect switches, I changed the connection system. +There can be a variable number of connections available. These are specified as tuples {c=, y=} +The table "at_conns" consists of {, ...} +the "at_rail_y" property holds the value that was previously called "railheight" +Depending on the number of connections: +2 conns: regular rail +3 conns: switch: + - when train passes in at conn1, will move out of conn2 + - when train passes in at conn2 or conn3, will move out of conn1 +4 conns: cross (or cross switch, depending on arrangement of conns): + - conn1 <> conn2 + - conn3 <> conn4 +]] + +-- Notify the user if digging the rail is not allowed +local function can_dig_callback(pos, player) + local ok, reason = advtrains.can_dig_or_modify_track(pos) + if not ok and player then + minetest.chat_send_player(player:get_player_name(), attrans("This track can not be removed!") .. " " .. reason) + end + return ok +end + +local function append_statemap_suffix(state_map, nnpref, rot) + local t = {} + for state, nn in pairs(state_map) do + t[state] = nnpref .. "_" .. nn .. rot + end + return t +end + +function advtrains.register_tracks(tracktype, def, preset) + if not preset.v25_format then + error("advtrains.register_tracks(): A track preset for pre-v2.5 is used with advtrains 2.5+. Mod probably defines own track preset instead of using it from the advtrains.ap table! Please check track mod compatibility!") + end + + if preset.regtp then + local nnprefix = def.nodename_prefix + minetest.register_craftitem(":"..nnprefix.."_placer", { + description = def.description, + inventory_image = def.texture_prefix.."_placer.png", + wield_image = def.texture_prefix.."_placer.png", + groups={advtrains_trackplacer=1, digtron_on_place=1}, + liquids_pointable = false, + on_place = function(itemstack, placer, pointed_thing) + local name = placer:get_player_name() + if not name then + return itemstack, false + end + if pointed_thing.type=="node" then + local pos=pointed_thing.above + local upos=vector.subtract(pointed_thing.above, {x=0, y=1, z=0}) + if not advtrains.check_track_protection(pos, name) then + return itemstack, false + end + if minetest.registered_nodes[minetest.get_node(pos).name] and minetest.registered_nodes[minetest.get_node(pos).name].buildable_to then + local s = minetest.registered_nodes[minetest.get_node(upos).name] and minetest.registered_nodes[minetest.get_node(upos).name].walkable + if s then + -- minetest.chat_send_all(nnprefix) + local yaw = placer:get_look_horizontal() + advtrains.trackplacer.place_track(pos, nnprefix, name, yaw) + if not advtrains.is_creative(name) then + itemstack:take_item() + end + end + end + end + return itemstack, true + end, + }) + + advtrains.trackplacer.set_default_place_candidate(def.nodename_prefix, def.nodename_prefix.."_"..preset.tpdefault) + end + if preset.regsp then + advtrains.slope.register_placer(def, preset) + end + + for suffix, var in pairs(preset.variant) do + for rotid, rotation in ipairs(preset.rotation) do + if not def.formats[suffix] or def.formats[suffix][rotid] then + local img_suffix = suffix..rotation + local ndef = advtrains.merge_tables({ + description=def.description.."("..(var.desc or "any")..rotation..")", + drawtype = "mesh", + paramtype="light", + paramtype2="facedir", + walkable = false, + selection_box = { + type = "fixed", + fixed = {-1/2-1/16, -1/2, -1/2, 1/2+1/16, -1/2+2/16, 1/2}, + }, + + mesh = def.shared_model or (def.models_prefix.."_"..img_suffix..def.models_suffix), + tiles = {def.shared_texture or (def.texture_prefix.."_"..img_suffix..".png"), def.second_texture}, + + groups = { + attached_node = advtrains.IGNORE_WORLD and 0 or 1, + advtrains_track=1, + ["advtrains_track_"..tracktype]=1, + save_in_at_nodedb=1, + dig_immediate=2, + not_in_creative_inventory=1, + not_blocking_trains=1, + }, + + can_dig = advtrains.track_can_dig_callback, + after_dig_node = advtrains.track_update_callback, + after_place_node = advtrains.track_update_callback, + + at_rail_y = var.rail_y + }, def.common or {}) + + if preset.regtp then + ndef.drop = def.nodename_prefix.."_placer" + end + if preset.regsp and var.slope then + ndef.drop = def.nodename_prefix.."_slopeplacer" + end + + --connections + ndef.at_conns = advtrains.rotate_conn_by(var.conns, (rotid-1)*preset.regstep) + -- NEW since 2.5 + ndef.at_conn_map = var.conn_map + + local ndef_avt_table = {} + + if var.switchalt and var.switchst then + -- NEW since 2.5 + ndef.on_rightclick = advtrains.state_node_on_rightclick_callback + ndef_avt_table.node_state = var.switchst + ndef_avt_table.node_next_state = var.switchalt + -- obtain and build statemap + local state_map = preset.statemaps[var.stmref] + if not state_map then error("On registering "..def.nodename_prefix.."_"..suffix..rotation..", stmref of variant doesn't reference entry in preset.statemaps") end + ndef_avt_table.node_state_map = append_statemap_suffix(state_map, def.nodename_prefix, rotation) + + if var.switchmc then + local vswitchalt = var.switchalt + ndef.mesecons = {effector = { + ["action_"..var.switchmc] = function(pos, node) + advtrains.setstate(pos, vswitchalt, node) + end, + rules=advtrains.meseconrules + }} + end + end + + local adef={} + if def.get_additional_definiton then + adef=def.get_additional_definiton(def, preset, suffix, rotation) + end + ndef = advtrains.merge_tables(ndef, adef) + + -- insert getstate/setstate functions after merging the additional definitions + if ndef_avt_table then + ndef.advtrains = advtrains.merge_tables(ndef.advtrains or {}, ndef_avt_table) + end + + -- NEW since 2.5: add appropriate fields for trackworker rotation + -- get the next rotation step + local num_rots = #preset.rotation + if rotid >= num_rots then + ndef.advtrains.trackworker_next_rot = def.nodename_prefix.."_"..suffix..preset.rotation[1] + ndef.advtrains.trackworker_rot_incr_param2 = true + else + ndef.advtrains.trackworker_next_rot = def.nodename_prefix.."_"..suffix..preset.rotation[rotid+1] + end + if var.trackworker then + ndef.advtrains.trackworker_next_var = def.nodename_prefix.."_"..var.trackworker..rotation + end + + local the_node_name = def.nodename_prefix.."_"..suffix..rotation + + --trackplacer + if preset.regtp and (var.tpsingle or var.tpdouble) then + advtrains.trackplacer.register_candidate( + def.nodename_prefix, + the_node_name, + ndef, + var.tpsingle, + var.tpdouble, + true) + end + + -- All set, go ahead and register node! + --atdebug("track_reg_helper: Registering ",the_node_name," as",ndef) + minetest.register_node(":"..the_node_name, ndef) + + end + end + end +end + +-- slope placer. Defined in register_tracks. +--crafted with rail and gravel +local sl={} +function sl.register_placer(def, preset) + minetest.register_craftitem(":"..def.nodename_prefix.."_slopeplacer",{ + description = attrans("@1 Slope", def.description), + inventory_image = def.texture_prefix.."_slopeplacer.png", + wield_image = def.texture_prefix.."_slopeplacer.png", + groups={}, + on_place = sl.create_slopeplacer_on_place(def, preset) + }) +end +--(itemstack, placer, pointed_thing) +function sl.create_slopeplacer_on_place(def, preset) + return function(istack, player, pt) + if not pt.type=="node" then + minetest.chat_send_player(player:get_player_name(), attrans("Can't place: not pointing at node")) + return istack + end + local pos=pt.above + if not pos then + minetest.chat_send_player(player:get_player_name(), attrans("Can't place: not pointing at node")) + return istack + end + local node=minetest.get_node(pos) + if not minetest.registered_nodes[node.name] or not minetest.registered_nodes[node.name].buildable_to then + minetest.chat_send_player(player:get_player_name(), attrans("Can't place: space occupied!")) + return istack + end + if not advtrains.check_track_protection(pos, player:get_player_name()) then + minetest.record_protection_violation(pos, player:get_player_name()) + return istack + end + --determine player orientation (only horizontal component) + --get_look_horizontal may not be available + local yaw=player.get_look_horizontal and player:get_look_horizontal() or (player:get_look_yaw() - math.pi/2) + + --rounding unit vectors is a nice way for selecting 1 of 8 directions since sin(30°) is 0.5. + local dirvec={x=math.floor(math.sin(-yaw)+0.5), y=0, z=math.floor(math.cos(-yaw)+0.5)} + --translate to direction to look up inside the preset table + local param2, rot45=({ + [-1]={ + [-1]=2, + [0]=3, + [1]=3, + }, + [0]={ + [-1]=2, + [1]=0, + }, + [1]={ + [-1]=1, + [0]=1, + [1]=0, + }, + })[dirvec.x][dirvec.z], dirvec.x~=0 and dirvec.z~=0 + local lookup=preset.slopeplacer + if rot45 then lookup=preset.slopeplacer_45 end + + --go unitvector forward and look how far the next node is + local step=1 + while step<=lookup.max do + local node=minetest.get_node(vector.add(pos, dirvec)) + --next node solid? + if not minetest.registered_nodes[node.name] or not minetest.registered_nodes[node.name].buildable_to or advtrains.is_protected(pos, player:get_player_name()) then + --do slopes of this distance exist? + if lookup[step] then + if minetest.settings:get_bool("creative_mode") or istack:get_count()>=step then + --start placing + local placenodes=lookup[step] + while step>0 do + minetest.set_node(pos, {name=def.nodename_prefix.."_"..placenodes[step], param2=param2}) + if not minetest.settings:get_bool("creative_mode") then + istack:take_item() + end + step=step-1 + pos=vector.subtract(pos, dirvec) + end + else + minetest.chat_send_player(player:get_player_name(), attrans("Can't place: Not enough slope items left (@1 required)", step)) + end + else + minetest.chat_send_player(player:get_player_name(), attrans("Can't place: There's no slope of length @1",step)) + end + return istack + end + step=step+1 + pos=vector.add(pos, dirvec) + end + minetest.chat_send_player(player:get_player_name(), attrans("Can't place: no supporting node at upper end.")) + return itemstack + end +end + +advtrains.slope=sl diff --git a/advtrains/trackplacer.lua b/advtrains/trackplacer.lua index 71eb79c..e6111dc 100644 --- a/advtrains/trackplacer.lua +++ b/advtrains/trackplacer.lua @@ -27,21 +27,27 @@ end -- Register a track node as candidate -- tpg: the track place group to register the candidates for +-- NOTE: This value is automatically added to the node definition (ndef) in field ndef.advtrains.track_place_group! -- name, ndef: the node name and node definition table to register -- as_single: whether the rail should be considered as candidate for one-endpoint connection -- Typically only set for the straight rail variants -- as_double: whether the rail should be considered as candidate for two-endpoint connection -- Typically set for straights and curves -function tp.register_candidate(tpg, name, ndef, as_single, as_double) +-- ignore_2conn_for_legacy_xing: skips the 2-connection assertion - ONLY for compatibility with the legacy crossing nodes, DO NOT USE! +function tp.register_candidate(tpg, name, ndef, as_single, as_double, ignore_2conn_for_legacy_xing) + --atdebug("TP Register candidate:",tpg, name, as_single, as_double) --get or create TP group if not tp.groups[tpg] then tp.groups[tpg] = {double = {}, single1 = {}, single2 = {}, default = {name = name, param2 = 0} } -- note: this causes the first candidate to ever be registered to be the default (which is typically what you want) + -- But it can be overwritten using tp.set_default_place_candidate end local g = tp.groups[tpg] -- get conns - assert(#ndef.at_conns == 2) + if not ignore_2conn_for_legacy_xing then + assert(#ndef.at_conns == 2) + end local c1, c2 = ndef.at_conns[1].c, ndef.at_conns[2].c local is_symmetrical = (rotate(c1, 8) == c2) @@ -59,6 +65,21 @@ function tp.register_candidate(tpg, name, ndef, as_single, as_double) g.single2[rotate(c2,i*4)] = {name=name, param2=i} end end + + -- Set track place group on the node + if not ndef.advtrains then + ndef.advtrains = {} + end + ndef.advtrains.track_place_group = tpg +end + +-- Sets the node that is placed by the track placer when there is no track nearby. param2 defaults to 0 +function tp.set_default_place_candidate(tpg, name, param2) + if not tp.groups[tpg] then + tp.groups[tpg] = {double = {}, single1 = {}, single2 = {}, default = {name = name, param2 = param2 or 0} } + else + tp.groups[tpg].default = {name = name, param2 = param2 or 0} + end end local function check_or_bend_rail(origin, dir, pname, commit) @@ -87,7 +108,7 @@ local function check_or_bend_rail(origin, dir, pname, commit) return false end -- now the track must be two-conn, else it wouldn't be allowed to have track_place_group set. - assert(#conns == 2) + --assert(#conns == 2) -- cannot check here, because of legacy crossing hack -- Is player and game allowed to do this? if not advtrains.can_dig_or_modify_track(pos) then return false @@ -132,6 +153,7 @@ local function check_or_bend_rail(origin, dir, pname, commit) end local function track_place_node(pos, node, ndef) + --atdebug("track_place_node: ",pos, node) advtrains.ndb.swap_node(pos, node) local ndef = minetest.registered_nodes[node.name] if ndef and ndef.after_place_node then @@ -151,14 +173,20 @@ end -- The function returns true on success. function tp.place_track(pos, tpg, pname, yaw) -- 1. collect neighboring tracks and whether they can be connected + --atdebug("tp.place_track(",pos, tpg, pname, yaw,")") local cand = {} for i=0,15 do if check_or_bend_rail(pos, i, pname) then cand[#cand+1] = i end end + --atdebug("Candidates: ",cand) -- obtain the group table local g = tp.groups[tpg] + if not g then + error("tp.place_track: for tpg="..tpg.." couldn't find the group table") + end + --atdebug("Group table:",g) -- 2. try all possible two-endpoint connections for k1, conn1 in ipairs(cand) do for k2, conn2 in ipairs(cand) do @@ -167,6 +195,7 @@ function tp.place_track(pos, tpg, pname, yaw) -- the combination the other way round will be run through in a later loop iteration if advtrains.yawToDirection(yaw, conn1, conn2) == conn2 then -- does there exist a suitable double-connection rail? + --atdebug("Try double conn: ",conn1, conn2) local node = g.double[conn1.."_"..conn2] if node then check_or_bend_rail(pos, conn1, pname, true) @@ -187,6 +216,7 @@ function tp.place_track(pos, tpg, pname, yaw) else single = g.single2 end + --atdebug("Try single conn: ",conn1) local node = single[conn1] if node then check_or_bend_rail(pos, conn1, pname, true) diff --git a/advtrains/tracks.lua b/advtrains/tracks.lua index 46ceaf8..1abef59 100644 --- a/advtrains/tracks.lua +++ b/advtrains/tracks.lua @@ -222,19 +222,6 @@ function advtrains.register_node_4rot(ori_name, ori_ndef, definition_mangling_fu end end - --- Registers an item to place and automatically connect nearby tracks -function advtrains.register_track_placer(...) - -end - --- Registers an item to place and adjust slope tracks -function advtrains.register_slope_placer(...) - -end - - - -- track-related helper functions function advtrains.is_track(nodename) @@ -255,13 +242,10 @@ function advtrains.get_track_connections(name, param2) if not param2 then noderot=0 end if noderot > 3 then atprint(" get_track_connections: rail has invaild param2 of "..noderot) noderot=0 end - local tracktype - for k,_ in pairs(nodedef.groups) do - local tt=string.match(k, "^advtrains_track_(.+)$") - if tt then - tracktype=tt - end + if not nodedef.at_conns then + return nil end + --atdebug("Track connections of ",name,param2,":",nodedef.at_conns) return advtrains.rotate_conn_by(nodedef.at_conns, noderot*AT_CMAX/4), (nodedef.at_rail_y or 0), tracktype end @@ -283,4 +267,4 @@ function advtrains.can_dig_or_modify_track(pos) end end return true -end \ No newline at end of file +end diff --git a/advtrains_interlocking/init.lua b/advtrains_interlocking/init.lua index cc46b83..fe8b967 100644 --- a/advtrains_interlocking/init.lua +++ b/advtrains_interlocking/init.lua @@ -25,8 +25,7 @@ dofile(modpath.."tool.lua") dofile(modpath.."approach.lua") dofile(modpath.."ars.lua") ---TODO reenable tsr rail ---dofile(modpath.."tsr_rail.lua") +dofile(modpath.."tsr_rail.lua") minetest.register_privilege("interlocking", {description = "Can set up track sections, routes and signals.", give_to_singleplayer = true}) diff --git a/advtrains_line_automation/init.lua b/advtrains_line_automation/init.lua index cc8df3c..e7d0ea6 100644 --- a/advtrains_line_automation/init.lua +++ b/advtrains_line_automation/init.lua @@ -21,8 +21,7 @@ local modpath = minetest.get_modpath(minetest.get_current_modname()) .. DIR_DELI dofile(modpath.."railwaytime.lua") dofile(modpath.."scheduler.lua") ---TODO reenable stop rail ---dofile(modpath.."stoprail.lua") +dofile(modpath.."stoprail.lua") function advtrains.lines.load(data) diff --git a/advtrains_train_track/init.lua b/advtrains_train_track/init.lua old mode 100755 new mode 100644 index 6195b5b..5065155 --- a/advtrains_train_track/init.lua +++ b/advtrains_train_track/init.lua @@ -1,185 +1,937 @@ --- advtrains_train_track --- rewritten to work with advtrains 2.5 track system, but mimics the "old" template-based track registration --- Also, since 2.5, all tracks are moved here, even the ATC, LuaATC and Interlocking special tracks - -local function conns(c1, c2, r1, r2) return {{c=c1, y=r1}, {c=c2, y=r2}} end -local function conns3(c1, c2, c3, r1, r2, r3) return {{c=c1, y=r1}, {c=c2, y=r2}, {c=c3, y=r3}} end - - -local function register(reg) - for sgi, sgrp in ipairs(reg.sgroups) do - -- prepare the state map if we need it later - local state_map = {} - if sgrp.turnout then - for vn,var in pairs(sgrp.variants) do - local name = reg.base .. "_" .. vn - state_map[var.state] = name - end - end - -- iterate through each of the variants - for vn,var in pairs(sgrp.variants) do - local name = reg.base .. "_" .. vn - local ndef = { - description = reg.description .. " " .. vn, - drawtype = "mesh", - paramtype = "light", - paramtype2 = "facedir", - walkable = false, - selection_box = { - type = "fixed", - fixed = {-1/2-1/16, -1/2, -1/2, 1/2+1/16, -1/2+2/16, 1/2}, - }, - - mesh_prefix=reg.mprefix.."_"..vn, - mesh_suffix = ".b3d", - tiles = { "advtrains_dtrack_shared.png" }, - - groups = { - advtrains_track=1, - advtrains_track_default=1, - dig_immediate=2, - --not_in_creative_inventory=1, - }, - - at_conns = sgrp.conns, - at_conn_map = var.conn_map, - - can_dig = advtrains.track_can_dig_callback, - after_dig_node = advtrains.track_update_callback, - after_place_node = advtrains.track_update_callback, - - advtrains = { - trackworker_next_var = reg.base .. "_" .. var.next_var - } - } - -- drop field - if reg.register_placer then - ndef.drop = reg.base.."_placer" - else - ndef.drop = reg.drop - end - -- if variant is suitable for autoplacing (trackplacer) - if var.track_place then - ndef.advtrains.track_place_group = reg.base - ndef.advtrains.track_place_single = var.track_place_single - end - -- turnout handling - -- if the containing group was a turnout group, the containing state_map will be used - if sgrp.turnout then - ndef.on_rightclick = advtrains.state_node_on_rightclick_callback - ndef.advtrains.node_state = var.state - ndef.advtrains.node_next_state = var.next_state - ndef.advtrains.node_state_map = state_map - end - -- use advtrains-internal function to register the 4 rotations of the node, to make our life easier - --atdebug("Registering: ",name, ndef) -- for debugging it can be useful to output what is being registered - advtrains.register_node_4rot(name, ndef) - end - end - if reg.register_placer then - local tpgrp = reg.base - minetest.register_craftitem(":advtrains:dtrack_placer", { - description = reg.description, - inventory_image = reg.mprefix.."_placer.png", - wield_image = reg.mprefix.."_placer.png", - groups={advtrains_trackplacer=1, digtron_on_place=1}, - liquids_pointable = false, - on_place = function(itemstack, placer, pointed_thing) - local name = placer:get_player_name() - if not name then - return itemstack, false - end - if pointed_thing.type=="node" then - local pos=pointed_thing.above - local upos=vector.subtract(pointed_thing.above, {x=0, y=1, z=0}) - if not advtrains.check_track_protection(pos, name) then - return itemstack, false - end - if minetest.registered_nodes[minetest.get_node(pos).name] and minetest.registered_nodes[minetest.get_node(pos).name].buildable_to then - local s = minetest.registered_nodes[minetest.get_node(upos).name] and minetest.registered_nodes[minetest.get_node(upos).name].walkable - if s then - -- minetest.chat_send_all(nnprefix) - local yaw = placer:get_look_horizontal() - advtrains.trackplacer.place_track(pos, tpgrp, name, yaw) - if not advtrains.is_creative(name) then - itemstack:take_item() - end - end - end - end - return itemstack, true - end, - }) - end -end - - - --- normal dtrack -register({ - base = "advtrains:dtrack", - mprefix = "advtrains_dtrack", - description = attrans("Track"), - - sgroups = { -- integer-indexed table, we don't need a key here - -- inside are "variant" tables - { - variants = { - st = { - next_var = "cr", - track_place = true, - track_place_single = true, - }, - }, - conns = conns(0,8), - }, - { - variants = { - cr = { - next_var = "swlst", - track_place = true, - }, - }, - conns = conns(0,7), - }, - { - turnout = true, - variants = { - swlst = { - next_var = "swrst", - conn_map = {2,1,1}, - state = "st", - next_state = "cr", - }, - swlcr = { - next_var = "swrcr", - conn_map = {3,1,1}, - state = "cr", - next_state = "st", - }, - }, - conns = conns3(0,8,7), - }, - { - turnout = true, - variants = { - swrst = { - next_var = "st", - conn_map = {2,1,1}, - state = "st", - next_state = "cr", - }, - swrcr = { - next_var = "st", - conn_map = {3,1,1}, - state = "cr", - next_state = "st", - }, - }, - conns = conns3(0,8,9), - }, - }, - register_placer = true, -}) - ----------------------- \ No newline at end of file +-- Default tracks for advtrains +-- (c) orwell96 and contributors + +local default_boxen = { + ["st"] = { + [""] = { + selection_box = { + type = "fixed", + fixed = {-1/2-1/16, -1/2, -1/2, 1/2+1/16, -1/2+2/16, 1/2}, + } + }, + ["_30"] = { + selection_box = { + type = "fixed", + fixed = { + {-0.5000, -0.5000, -1.000, 0.5000, -0.3750, 1.000}, + {-0.8750, -0.5000, -1.000, -0.5000, -0.3750, 0.2500}, + {0.5000, -0.5000, -0.2500, 0.8750, -0.3750, 1.000}, + {-0.1250, -0.5000, -1.375, 0.1875, -0.3750, -1.000} + } + } + }, + ["_45"] = { + selection_box = { + type = "fixed", + fixed = { + {-0.5000, -0.5000, -0.8750, 0.5000, -0.3750, 0.8750}, + {0.5000, -0.5000, -0.5000, 0.8750, -0.3750, 0.5000}, + {-0.8750, -0.5000, -0.5000, -0.5000, -0.3750, 0.5000} + } + } + }, + ["_60"] = { + selection_box = { + type = "fixed", + fixed = { + {-1.000, -0.5000, -0.5000, 1.000, -0.3750, 0.5000}, + {-1.000, -0.5000, -0.8750, 0.2500, -0.3750, -0.5000}, + {-0.2500, -0.5000, 0.5000, 1.000, -0.3750, 0.8750}, + {-1.375, -0.5000, -0.1250, -1.000, -0.3750, 0.1875} + } + } + }, + }, + + ["cr"] = { + [""] = { + selection_box = { + type = "fixed", + fixed = { + {-0.5000, -0.5000, -0.5000, 0.6875, -0.3750, 0.5000}, + {-0.3750, -0.5000, -1.000, 1.000, -0.3750, 0.000} + } + } + }, + ["_30"] = { + selection_box = { + type = "fixed", + fixed = { + {-0.5000, -0.5000, -0.5000, 0.7500, -0.3750, 0.8750}, + {-0.3750, -0.5000, 0.8750, 0.2500, -0.3750, 1.188}, + {0.7500, -0.5000, 0.2500, 1.063, -0.3750, 0.8750} + } + } + }, + ["_45"] = { + selection_box = { + type = "fixed", + fixed = { + {-0.5000, -0.5000, -1.125, 0.5000, -0.3750, 0.6875}, + {-0.8750, -0.5000, -0.9375, -0.5000, -0.3750, 0.06250}, + {0.5000, -0.5000, -0.5000, 0.8750, -0.3750, 0.5000} + } + } + }, + ["_60"] = { + selection_box = { + type = "fixed", + fixed = { + {-0.8125, -0.5000, -0.5000, 1.188, -0.3750, 0.5000}, + {-0.1875, -0.5000, 0.5000, 0.8750, -0.3125, 0.8750}, + {-0.2500, -0.5000, -0.9375, 0.3125, -0.3125, -0.5000} + } + } + }, + }, + + ["swlst"] = { + [""] = { + selection_box = { + type = "fixed", + fixed = { + {-0.5000, -0.5000, -0.5000, 0.6250, -0.3750, 0.5000}, + {-0.3125, -0.5000, -1.000, 0.9375, -0.3125, -0.06250} + } + } + }, + ["_30"] = { + selection_box = { + type = "fixed", + fixed = { + {-0.5000, -0.5000, -1.000, 0.5000, -0.3750, 1.000}, + {-0.8750, -0.5000, -1.000, -0.5000, -0.3750, 0.2500}, + {0.5000, -0.5000, -0.2500, 0.8750, -0.3750, 1.000}, + {-0.1250, -0.5000, -1.375, 0.1875, -0.3750, -1.000} + } + } + }, + ["_45"] = { + selection_box = { + type = "fixed", + fixed = { + {-0.5000, -0.5000, -1.1875, 0.5000, -0.3750, 0.8750}, + {0.5000, -0.5000, -0.5000, 0.8750, -0.3750, 0.5000}, + {-0.8750, -0.5000, -0.8125, -0.5000, -0.3750, 0.5000} + } + } + }, + ["_60"] = { + selection_box = { + type = "fixed", + fixed = { + {-1.000, -0.5000, -0.5000, 1.000, -0.3750, 0.5000}, + {-1.000, -0.5000, -0.8750, 0.2500, -0.3750, -0.5000}, + {-0.2500, -0.5000, 0.5000, 1.000, -0.3750, 0.8750}, + {-1.375, -0.5000, -0.1250, -1.000, -0.3750, 0.1875} + } + } + }, + }, + + ["swrst"] = { + [""] = { + selection_box = { + type = "fixed", + fixed = { + {-0.5000, -0.5000, -0.5000, 0.6250, -0.3750, 0.5000}, + {-0.8125, -0.5000, -1.000, 0.4375, -0.3125, -0.06250} + } + } + }, + ["_30"] = { + selection_box = { + type = "fixed", + fixed = { + {-0.5000, -0.5000, -1.000, 0.5000, -0.3750, 1.000}, + {-0.8750, -0.5000, -1.000, -0.5000, -0.3750, 0.2500}, + {0.5000, -0.5000, -0.2500, 0.8750, -0.3750, 1.000}, + {-0.1250, -0.5000, -1.375, 0.1875, -0.3750, -1.000} + } + } + }, + ["_45"] = { + selection_box = { + type = "fixed", + fixed = { + {-1.1875, -0.5000, -0.5000, 0.8750, -0.3750, 0.5000}, + {-0.5000, -0.5000, 0.5000, 0.5000, -0.3750, 0.8750}, + {-0.8125, -0.5000, -0.8750, 0.5000, -0.3750, -0.5000} + } + } + }, + ["_60"] = { + selection_box = { + type = "fixed", + fixed = { + {-1.000, -0.5000, -0.5000, 1.000, -0.3750, 0.5000}, + {-1.000, -0.5000, -0.8750, 0.2500, -0.3750, -0.5000}, + {-0.2500, -0.5000, 0.5000, 1.000, -0.3750, 0.8750}, + {-1.375, -0.5000, -0.1250, -1.000, -0.3750, 0.1875} + } + } + }, + }, +} + +default_boxen["swlcr"] = default_boxen["swlst"] +default_boxen["swrcr"] = default_boxen["swrst"] + +--flat +advtrains.register_tracks("default", { + nodename_prefix="advtrains:dtrack", + texture_prefix="advtrains_dtrack", + models_prefix="advtrains_dtrack", + models_suffix=".b3d", + shared_texture="advtrains_dtrack_shared.png", + description=attrans("Track"), + formats={}, + + get_additional_definiton = function(def, preset, suffix, rotation) + if default_boxen[suffix] ~= nil and default_boxen[suffix][rotation] ~= nil then + return default_boxen[suffix][rotation] + else + return {} + end + end, +}, advtrains.ap.t_30deg_flat) + +minetest.register_craft({ + output = 'advtrains:dtrack_placer 50', + recipe = { + {'default:steel_ingot', 'group:stick', 'default:steel_ingot'}, + {'default:steel_ingot', 'group:stick', 'default:steel_ingot'}, + {'default:steel_ingot', 'group:stick', 'default:steel_ingot'}, + }, +}) + +local y3_boxen = { + [""] = { + selection_box = { + type = "fixed", + fixed = { + {-0.8750, -0.5000, -1.125, 0.8750, -0.3750, 0.4375} + } + } + }, + + ["_30"] = { + selection_box = { + type = "fixed", + fixed = { + {-0.5000, -0.5000, -0.875, 0.5000, -0.3750, 1.000}, + {-0.8750, -0.5000, -0.4375, -0.5000, -0.3750, 0.5625}, + {0.5000, -0.5000, -0.2500, 0.8125, -0.3750, 1.000}, + } + } + }, + + --UX FIXME: - 3way - have to place straight route before l and r or the + --nodebox overlaps too much and can't place the straight track node. + ["_45"] = { + selection_box = { + type = "fixed", + fixed = { + {-0.5000, -0.5000, -1.1250, 0.5000, -0.3750, 0.8750}, + {0.5000, -0.5000, -0.5000, 0.8750, -0.3750, 0.5000}, + {-1.1250, -0.5000, -0.9375, -0.5000, -0.3750, 0.5000} + } + } + }, + + ["_60"] = { + selection_box = { + type = "fixed", + fixed = { + --{-0.5000, -0.5000, -0.875, 0.5000, -0.3750, 1.000}, + {-0.875, -0.5000, -0.5, 1.0, -0.3750, 0.5}, + --{-0.8750, -0.5000, -0.4375, -0.5000, -0.3750, 0.5625}, + {-0.4375, -0.5000, -0.8750, 0.5625, -0.3750, -0.5000}, + --{0.5000, -0.5000, -0.2500, 0.8125, -0.3750, 1.000}, + {-0.2500, -0.5000, -0.2500, 1.0000, -0.3750, 0.8125}, + } + } + }, +} + + +local function y3_turnouts_addef(def, preset, suffix, rotation) + return y3_boxen[rotation] or {} +end +-- y-turnout +advtrains.register_tracks("default", { + nodename_prefix="advtrains:dtrack_sy", + texture_prefix="advtrains_dtrack_sy", + models_prefix="advtrains_dtrack_sy", + models_suffix=".obj", + shared_texture="advtrains_dtrack_shared.png", + description=attrans("Y-turnout"), + formats = {}, + get_additional_definiton = y3_turnouts_addef, +}, advtrains.ap.t_yturnout) +minetest.register_craft({ + output = 'advtrains:dtrack_sy_placer 2', + recipe = { + {'advtrains:dtrack_placer', '', 'advtrains:dtrack_placer'}, + {'', 'advtrains:dtrack_placer', ''}, + {'', 'advtrains:dtrack_placer', ''}, + }, +}) +--3-way turnout +advtrains.register_tracks("default", { + nodename_prefix="advtrains:dtrack_s3", + texture_prefix="advtrains_dtrack_s3", + models_prefix="advtrains_dtrack_s3", + models_suffix=".obj", + shared_texture="advtrains_dtrack_shared.png", + description=attrans("3-way turnout"), + formats = {}, + get_additional_definiton = y3_turnouts_addef, +}, advtrains.ap.t_s3way) +minetest.register_craft({ + output = 'advtrains:dtrack_s3_placer 1', + recipe = { + {'advtrains:dtrack_placer', 'advtrains:dtrack_placer', 'advtrains:dtrack_placer'}, + {'', 'advtrains:dtrack_placer', ''}, + {'', '', ''}, + }, +}) + +-- Diamond Crossings + +local perp_boxen = { + [""] = {}, --default size + ["_30"] = { + selection_box = { + type = "fixed", + fixed = { + {-1.000, -0.5000, -1.000, 1.000, -0.3750, 1.000} + } + } + }, + ["_45"] = { + selection_box = { + type = "fixed", + fixed = { + {-0.8125, -0.5000, -0.8125, 0.8125, -0.3750, 0.8125} + } + } + }, + ["_60"] = { + selection_box = { + type = "fixed", + fixed = { + {-1.000, -0.5000, -1.000, 1.000, -0.3750, 1.000} + } + } + }, +} + +-- perpendicular +advtrains.register_tracks("default", { + nodename_prefix="advtrains:dtrack_xing", + texture_prefix="advtrains_dtrack_xing", + models_prefix="advtrains_dtrack_xing", + models_suffix=".obj", + shared_texture="advtrains_dtrack_shared.png", + description=attrans("Perpendicular Diamond Crossing Track"), + formats = {}, + get_additional_definiton = function(def, preset, suffix, rotation) + return perp_boxen[rotation] or {} + end +}, advtrains.ap.t_perpcrossing) + +minetest.register_craft({ + output = 'advtrains:dtrack_xing_placer 3', + recipe = { + {'', 'advtrains:dtrack_placer', ''}, + {'advtrains:dtrack_placer', 'advtrains:dtrack_placer', 'advtrains:dtrack_placer'}, + {'', 'advtrains:dtrack_placer', ''} + } +}) + +local ninety_plus_boxen = { + ["30l"] = { + selection_box = { + type = "fixed", + fixed = { + {-0.5000, -0.5000, -1.000, 0.5000, -0.3750, 1.000}, + {-0.8750, -0.5000, -1.000, -0.5000, -0.3750, 0.2500}, + {0.5000, -0.5000, -0.2500, 0.8750, -0.3750, 1.000}, + {-0.1250, -0.5000, -1.375, 0.1875, -0.3750, -1.000} + } + } + }, + ["30r"] = { + selection_box = { + type = "fixed", + fixed = { + {0.5000, -0.5000, -1.000, -0.5000, -0.3750, 1.000}, + {0.8750, -0.5000, -1.000, 0.5000, -0.3750, 0.2500}, + {-0.5000, -0.5000, -0.2500, -0.8750, -0.3750, 1.000}, + {0.1250, -0.5000, -1.375, -0.1875, -0.3750, -1.000} + } + } + }, + ["45l"] = { + selection_box = { + type = "fixed", + fixed = { + {-0.5000, -0.5000, -0.8750, 0.5000, -0.3750, 0.8750}, + {0.5000, -0.5000, -0.5000, 0.8750, -0.3750, 0.5000}, + {-0.8750, -0.5000, -0.5000, -0.5000, -0.3750, 0.5000} + } + } + }, + ["45r"] = { + selection_box = { + type = "fixed", + fixed = { + {-0.5000, -0.5000, -0.8750, 0.5000, -0.3750, 0.8750}, + {0.5000, -0.5000, -0.5000, 0.8750, -0.3750, 0.5000}, + {-0.8750, -0.5000, -0.5000, -0.5000, -0.3750, 0.5000} + } + } + }, + ["60l"] = { + selection_box = { + type = "fixed", + fixed = { + {-1.000, -0.5000, -0.5000, 1.000, -0.3750, 0.5000}, + {-1.000, -0.5000, -0.8750, 0.2500, -0.3750, -0.5000}, + {-0.2500, -0.5000, 0.5000, 1.000, -0.3750, 0.8750}, + {-1.375, -0.5000, -0.1250, -1.000, -0.3750, 0.1875} + } + } + }, + ["60r"] = { + selection_box = { + type = "fixed", + fixed = { + {1.000, -0.5000, -0.5000, -1.000, -0.3750, 0.5000}, + {1.000, -0.5000, -0.8750, -0.2500, -0.3750, -0.5000}, + {0.2500, -0.5000, 0.5000, -1.000, -0.3750, 0.8750}, + {1.375, -0.5000, -0.1250, 1.000, -0.3750, 0.1875} + } + } + }, +} + +-- 90plusx +-- When you face east and param2=0, then this set of rails has a rail at 90 +-- degrees to the viewer, plus another rail crossing at 30, 45 or 60 degrees. +advtrains.register_tracks("default", { + nodename_prefix="advtrains:dtrack_xing90plusx", + texture_prefix="advtrains_dtrack_xing4590", + models_prefix="advtrains_dtrack_xing90plusx", + models_suffix=".obj", + shared_texture="advtrains_dtrack_shared.png", + description=attrans("90+Angle Diamond Crossing Track"), + formats = {}, + get_additional_definiton = function(def, preset, suffix, rotation) + return ninety_plus_boxen[suffix] or {} + end, +}, advtrains.ap.t_90plusx_crossing) +minetest.register_craft({ + output = 'advtrains:dtrack_xing90plusx_placer 2', + recipe = { + {'advtrains:dtrack_placer', '', ''}, + {'advtrains:dtrack_placer', 'advtrains:dtrack_placer', 'advtrains:dtrack_placer'}, + {'', '', 'advtrains:dtrack_placer'} + } +}) + +-- Deprecate any rails using the old name scheme +minetest.register_lbm({ + label = "Upgrade legacy 4590 rails", + name = "advtrains_train_track:replace_legacy_4590", + nodenames = {"advtrains:dtrack_xing4590_st"}, + run_at_every_load = true, + action = function(pos, node) + minetest.log("actionPos!: " .. pos.x .. "," .. pos.y .. "," .. pos.z) + minetest.log("node!: " .. node.name .. "," .. node.param1 .. "," .. node.param2) + advtrains.ndb.swap_node(pos, + { + name="advtrains:dtrack_xing90plusx_45l", + param1=node.param1, + param2=node.param2, + }) + end +}) +-- This will replace any items left in the inventory +minetest.register_alias("advtrains:dtrack_xing4590_placer", "advtrains:dtrack_xing90plusx_placer") + +local diagonal_boxen = { + ["30r45l"] = { + selection_box = { + type = "fixed", + fixed = { + {0.5000, -0.5000, -1.000, -0.5000, -0.3750, 1.000}, + {0.8750, -0.5000, -1.000, 0.5000, -0.3750, 0.2500}, + {-0.5000, -0.5000, -0.2500, -0.8750, -0.3750, 1.000}, + {0.1250, -0.5000, -1.375, -0.1875, -0.3750, -1.000} + } + } + }, + ["60l30l"] = { + selection_box = { + type = "fixed", + fixed = { + {-1.000, -0.5000, -0.5000, 1.000, -0.3750, 0.5000}, + {-1.000, -0.5000, -0.8750, 0.2500, -0.3750, -0.5000}, + {-0.2500, -0.5000, 0.5000, 1.000, -0.3750, 0.8750}, + {-1.375, -0.5000, -0.1250, -1.000, -0.3750, 0.1875} + } + } + }, + ["60l60r"] = { + selection_box = { + type = "fixed", + fixed = { + {-1.000, -0.5000, -1.000, 1.000, -0.3750, 1.000} + } + } + }, + ["60r30r"] = { + selection_box = { + type = "fixed", + fixed = { + {1.000, -0.5000, -0.5000, -1.000, -0.3750, 0.5000}, + {1.000, -0.5000, -0.8750, -0.2500, -0.3750, -0.5000}, + {0.2500, -0.5000, 0.5000, -1.000, -0.3750, 0.8750}, + {1.375, -0.5000, -0.1250, 1.000, -0.3750, 0.1875} + } + } + }, + ["30l45r"] = { + selection_box = { + type = "fixed", + fixed = { + {-0.5000, -0.5000, -1.000, 0.5000, -0.3750, 1.000}, + {-0.8750, -0.5000, -1.000, -0.5000, -0.3750, 0.2500}, + {0.5000, -0.5000, -0.2500, 0.8750, -0.3750, 1.000}, + {-0.1250, -0.5000, -1.375, 0.1875, -0.3750, -1.000} + } + } + }, + ["60l45r"] = { + selection_box = { + type = "fixed", + fixed = { + {-1.000, -0.5000, -0.5000, 1.000, -0.3750, 0.5000}, + {-1.000, -0.5000, -0.8750, 0.2500, -0.3750, -0.5000}, + {-0.2500, -0.5000, 0.5000, 1.000, -0.3750, 0.8750}, + {-1.375, -0.5000, -0.1250, -1.000, -0.3750, 0.1875} + } + } + }, + ["60r45l"] = { + selection_box = { + type = "fixed", + fixed = { + {1.000, -0.5000, -0.5000, -1.000, -0.3750, 0.5000}, + {1.000, -0.5000, -0.8750, -0.2500, -0.3750, -0.5000}, + {0.2500, -0.5000, 0.5000, -1.000, -0.3750, 0.8750}, + {1.375, -0.5000, -0.1250, 1.000, -0.3750, 0.1875} + } + } + }, +} + +-- Diagonal +-- This set of rail crossings is named based on the angle of each intersecting +-- direction when facing east and param2=0. Rails with l/r swapped are mirror +-- images. For example, 30r45l is the mirror image of 30l45r. +advtrains.register_tracks("default", { + nodename_prefix="advtrains:dtrack_xingdiag", + texture_prefix="advtrains_dtrack_xingdiag", + models_prefix="advtrains_dtrack_xingdiag", + models_suffix=".obj", + shared_texture="advtrains_dtrack_shared.png", + description=attrans("Diagonal Diamond Crossing Track"), + formats = {}, + get_additional_definiton = function(def, preset, suffix, rotation) + return diagonal_boxen[suffix] or {} + end, +}, advtrains.ap.t_diagonalcrossing) +minetest.register_craft({ + output = 'advtrains:dtrack_xingdiag_placer 2', + recipe = { + {'advtrains:dtrack_placer', '', 'advtrains:dtrack_placer'}, + {'', 'advtrains:dtrack_placer', ''}, + {'advtrains:dtrack_placer', '', 'advtrains:dtrack_placer'} + } +}) +---- Not included: very shallow crossings like (30/60)+45. +---- At an angle of only 18.4 degrees, the models would not +---- translate well to a block game. +-- END crossings + +--slopes +advtrains.register_tracks("default", { + nodename_prefix="advtrains:dtrack", + texture_prefix="advtrains_dtrack", + models_prefix="advtrains_dtrack", + models_suffix=".obj", + shared_texture="advtrains_dtrack_shared.png", + second_texture="default_gravel.png", + description=attrans("Track"), + formats={vst1={true, false, true}, vst2={true, false, true}, vst31={true}, vst32={true}, vst33={true}}, +}, advtrains.ap.t_30deg_slope) + +minetest.register_craft({ + type = "shapeless", + output = 'advtrains:dtrack_slopeplacer 2', + recipe = { + "advtrains:dtrack_placer", + "advtrains:dtrack_placer", + "default:gravel", + }, +}) + + +--bumpers +advtrains.register_tracks("default", { + nodename_prefix="advtrains:dtrack_bumper", + texture_prefix="advtrains_dtrack_bumper", + models_prefix="advtrains_dtrack_bumper", + models_suffix=".b3d", + shared_texture="advtrains_dtrack_rail.png", + --bumpers still use the old texture until the models are redone. + description=attrans("Bumper"), + formats={}, +}, advtrains.ap.t_30deg_straightonly) +minetest.register_craft({ + output = 'advtrains:dtrack_bumper_placer 2', + recipe = { + {'group:wood', 'dye:red'}, + {'default:steel_ingot', 'default:steel_ingot'}, + {'advtrains:dtrack_placer', 'advtrains:dtrack_placer'}, + }, +}) +--legacy bumpers +for _,rot in ipairs({"", "_30", "_45", "_60"}) do + minetest.register_alias("advtrains:dtrack_bumper"..rot, "advtrains:dtrack_bumper_st"..rot) +end +-- atc track +advtrains.register_tracks("default", { + nodename_prefix="advtrains:dtrack_atc", + texture_prefix="advtrains_dtrack_atc", + models_prefix="advtrains_dtrack", + models_suffix=".b3d", + shared_texture="advtrains_dtrack_shared_atc.png", + description=attrans("ATC controller"), + formats={}, + get_additional_definiton = advtrains.atc_function +}, advtrains.trackpresets.t_30deg_straightonly) + + +-- Tracks for loading and unloading trains +-- Copyright (C) 2017 Gabriel Pérez-Cerezo + +local function get_far_node(pos) + local node = minetest.get_node(pos) + if node.name == "ignore" then + minetest.get_voxel_manip():read_from_map(pos, pos) + node = minetest.get_node(pos) + end + return node +end + + +local function show_fc_formspec(pos,player) + local pname = player:get_player_name() + if minetest.is_protected(pos,pname) then + minetest.chat_send_player(pname, "Position is protected!") + return + end + + local meta = minetest.get_meta(pos) + local fc = meta:get_string("fc") or "" + + local form = 'formspec_version[4]'.. + 'size[10,5]'.. + 'label[0.5,0.4;Advtrains Loading/Unloading Track]'.. + 'label[0.5,1.1;Set the code to match against the wagon\'s freight code]'.. + 'label[0.5,1.6;A blank field matches all wagons (default)]'.. + 'label[0.5,2.1;Use code # to disable the track section]'.. + 'field[0.5,3;5.5,1;fc;FC;'..minetest.formspec_escape(fc)..']'.. + 'button[6.5,3;3,1;save;Submit]' + minetest.show_formspec(pname, "at_load_unload_"..advtrains.encode_pos(pos), form) +end + +minetest.register_on_player_receive_fields(function(player, formname, fields) + local pname = player:get_player_name() + local pe = string.match(formname, "^at_load_unload_(............)$") + local pos = advtrains.decode_pos(pe) + if pos then + if minetest.is_protected(pos, pname) then + minetest.chat_send_player(pname, "Position is protected!") + return + end + + if fields.save then + minetest.get_meta(pos):set_string("fc",tostring(fields.fc)) + minetest.chat_send_player(pname,"Freight code set: "..tostring(fields.fc)) + show_fc_formspec(pos,player) + end + end +end) + + +local function train_load(pos, train_id, unload) + local train=advtrains.trains[train_id] + local below = get_far_node({x=pos.x, y=pos.y-1, z=pos.z}) + if not string.match(below.name, "chest") then + atprint("this is not a chest! at "..minetest.pos_to_string(pos)) + return + end + + local node_fc = minetest.get_meta(pos):get_string("fc") or "" + if node_fc == "#" then + --track section is disabled + return + end + + local inv = minetest.get_inventory({type="node", pos={x=pos.x, y=pos.y-1, z=pos.z}}) + if inv and train.velocity < 2 then + for k, v in ipairs(train.trainparts) do + local i=minetest.get_inventory({type="detached", name="advtrains_wgn_"..v}) + if i and i:get_list("box") then + + local wagon_data = advtrains.wagons[v] + local wagon_fc + if wagon_data.fc then + if not wagon_data.fcind then wagon_data.fcind = 1 end + wagon_fc = tostring(wagon_data.fc[wagon_data.fcind]) or "" + end + + if node_fc == "" or wagon_fc == node_fc then + if not unload then + for _, item in ipairs(inv:get_list("main")) do + if i:get_list("box") and i:room_for_item("box", item) then + i:add_item("box", item) + inv:remove_item("main", item) + end + end + else + for _, item in ipairs(i:get_list("box")) do + if inv:get_list("main") and inv:room_for_item("main", item) then + i:remove_item("box", item) + inv:add_item("main", item) + end + end + end + end + end + end + end +end + + + +advtrains.register_tracks("default", { + nodename_prefix="advtrains:dtrack_unload", + texture_prefix="advtrains_dtrack_unload", + models_prefix="advtrains_dtrack", + models_suffix=".b3d", + shared_texture="advtrains_dtrack_shared_unload.png", + description=attrans("Unloading Track"), + formats={}, + get_additional_definiton = function(def, preset, suffix, rotation) + return { + after_dig_node=function(pos) + advtrains.invalidate_all_paths() + advtrains.ndb.clear(pos) + end, + on_rightclick = function(pos, node, player) + show_fc_formspec(pos, player) + end, + advtrains = { + on_train_enter = function(pos, train_id) + train_load(pos, train_id, true) + end, + }, + } + end + }, advtrains.trackpresets.t_30deg_straightonly) +advtrains.register_tracks("default", { + nodename_prefix="advtrains:dtrack_load", + texture_prefix="advtrains_dtrack_load", + models_prefix="advtrains_dtrack", + models_suffix=".b3d", + shared_texture="advtrains_dtrack_shared_load.png", + description=attrans("Loading Track"), + formats={}, + get_additional_definiton = function(def, preset, suffix, rotation) + return { + after_dig_node=function(pos) + advtrains.invalidate_all_paths() + advtrains.ndb.clear(pos) + end, + on_rightclick = function(pos, node, player) + show_fc_formspec(pos, player) + end, + advtrains = { + on_train_enter = function(pos, train_id) + train_load(pos, train_id, false) + end, + }, + } + end + }, advtrains.trackpresets.t_30deg_straightonly) + +-- mod-dependent crafts +local loader_core = "default:mese_crystal" --fallback +if minetest.get_modpath("basic_materials") then + loader_core = "basic_materials:ic" +elseif minetest.get_modpath("technic") then + loader_core = "technic:control_logic_unit" +end +--print("Loader Core: "..loader_core) + +minetest.register_craft({ + type="shapeless", + output = 'advtrains:dtrack_load_placer', + recipe = { + "advtrains:dtrack_placer", + loader_core, + "default:chest" + }, +}) +loader_core = nil --nil the crafting variable + +--craft between load/unload tracks +minetest.register_craft({ + type="shapeless", + output = 'advtrains:dtrack_unload_placer', + recipe = { + "advtrains:dtrack_load_placer", + }, +}) +minetest.register_craft({ + type="shapeless", + output = 'advtrains:dtrack_load_placer', + recipe = { + "advtrains:dtrack_unload_placer", + }, +}) + + +if mesecon then + advtrains.register_tracks("default", { + nodename_prefix="advtrains:dtrack_detector_off", + texture_prefix="advtrains_dtrack_detector", + models_prefix="advtrains_dtrack", + models_suffix=".b3d", + shared_texture="advtrains_dtrack_shared_detector_off.png", + description=attrans("Detector Rail"), + formats={}, + get_additional_definiton = function(def, preset, suffix, rotation) + return { + mesecons = { + receptor = { + state = mesecon.state.off, + rules = advtrains.meseconrules + } + }, + advtrains = { + on_updated_from_nodedb = function(pos, node) + mesecon.receptor_off(pos, advtrains.meseconrules) + end, + on_train_enter=function(pos, train_id) + advtrains.ndb.swap_node(pos, {name="advtrains:dtrack_detector_on".."_"..suffix..rotation, param2=advtrains.ndb.get_node(pos).param2}) + if advtrains.is_node_loaded(pos) then + mesecon.receptor_on(pos, advtrains.meseconrules) + end + end + } + } + end + }, advtrains.ap.t_30deg_straightonly) + advtrains.register_tracks("default", { + nodename_prefix="advtrains:dtrack_detector_on", + texture_prefix="advtrains_dtrack", + models_prefix="advtrains_dtrack", + models_suffix=".b3d", + shared_texture="advtrains_dtrack_shared_detector_on.png", + description="Detector(on)(you hacker you)", + formats={}, + get_additional_definiton = function(def, preset, suffix, rotation) + return { + mesecons = { + receptor = { + state = mesecon.state.on, + rules = advtrains.meseconrules + } + }, + advtrains = { + on_updated_from_nodedb = function(pos, node) + mesecon.receptor_on(pos, advtrains.meseconrules) + end, + on_train_leave=function(pos, train_id) + advtrains.ndb.swap_node(pos, {name="advtrains:dtrack_detector_off".."_"..suffix..rotation, param2=advtrains.ndb.get_node(pos).param2}) + if advtrains.is_node_loaded(pos) then + mesecon.receptor_off(pos, advtrains.meseconrules) + end + end + } + } + end + }, advtrains.ap.t_30deg_straightonly_noplacer) +minetest.register_craft({ + type="shapeless", + output = 'advtrains:dtrack_detector_off_placer', + recipe = { + "advtrains:dtrack_placer", + "mesecons:wire_00000000_off" + }, +}) +end +--TODO legacy +--I know lbms are better for this purpose +for name,rep in pairs({swl_st="swlst", swr_st="swrst", swl_cr="swlcr", swr_cr="swrcr", }) do + minetest.register_abm({ + -- In the following two fields, also group:groupname will work. + nodenames = {"advtrains:track_"..name}, + interval = 1.0, -- Operation interval in seconds + chance = 1, -- Chance of trigger per-node per-interval is 1.0 / this + action = function(pos, node, active_object_count, active_object_count_wider) minetest.set_node(pos, {name="advtrains:track_"..rep, param2=node.param2}) end, + }) + minetest.register_abm({ + -- In the following two fields, also group:groupname will work. + nodenames = {"advtrains:track_"..name.."_45"}, + interval = 1.0, -- Operation interval in seconds + chance = 1, -- Chance of trigger per-node per-interval is 1.0 / this + action = function(pos, node, active_object_count, active_object_count_wider) minetest.set_node(pos, {name="advtrains:track_"..rep.."_45", param2=node.param2}) end, + }) +end + +if advtrains.register_replacement_lbms then +minetest.register_lbm({ + name = "advtrains:ramp_replacement_1", +-- In the following two fields, also group:groupname will work. + nodenames = {"advtrains:track_vert1"}, + action = function(pos, node, active_object_count, active_object_count_wider) minetest.set_node(pos, {name="advtrains:dtrack_vst1", param2=(node.param2+2)%4}) end, +}) +minetest.register_lbm({ + name = "advtrains:ramp_replacement_1", +-- -- In the following two fields, also group:groupname will work. + nodenames = {"advtrains:track_vert2"}, + action = function(pos, node, active_object_count, active_object_count_wider) minetest.set_node(pos, {name="advtrains:dtrack_vst2", param2=(node.param2+2)%4}) end, +}) + minetest.register_abm({ + name = "advtrains:st_rep_1", + -- In the following two fields, also group:groupname will work. + nodenames = {"advtrains:track_st"}, + interval=1, + chance=1, + action = function(pos, node, active_object_count, active_object_count_wider) minetest.set_node(pos, {name="advtrains:dtrack_st", param2=node.param2}) end, + }) + minetest.register_lbm({ + name = "advtrains:st_rep_1", + -- -- In the following two fields, also group:groupname will work. + nodenames = {"advtrains:track_st_45"}, + action = function(pos, node, active_object_count, active_object_count_wider) minetest.set_node(pos, {name="advtrains:dtrack_st_45", param2=node.param2}) end, + }) +end diff --git a/advtrains_train_track/oldinit.lua b/advtrains_train_track/oldinit.lua deleted file mode 100644 index 5065155..0000000 --- a/advtrains_train_track/oldinit.lua +++ /dev/null @@ -1,937 +0,0 @@ --- Default tracks for advtrains --- (c) orwell96 and contributors - -local default_boxen = { - ["st"] = { - [""] = { - selection_box = { - type = "fixed", - fixed = {-1/2-1/16, -1/2, -1/2, 1/2+1/16, -1/2+2/16, 1/2}, - } - }, - ["_30"] = { - selection_box = { - type = "fixed", - fixed = { - {-0.5000, -0.5000, -1.000, 0.5000, -0.3750, 1.000}, - {-0.8750, -0.5000, -1.000, -0.5000, -0.3750, 0.2500}, - {0.5000, -0.5000, -0.2500, 0.8750, -0.3750, 1.000}, - {-0.1250, -0.5000, -1.375, 0.1875, -0.3750, -1.000} - } - } - }, - ["_45"] = { - selection_box = { - type = "fixed", - fixed = { - {-0.5000, -0.5000, -0.8750, 0.5000, -0.3750, 0.8750}, - {0.5000, -0.5000, -0.5000, 0.8750, -0.3750, 0.5000}, - {-0.8750, -0.5000, -0.5000, -0.5000, -0.3750, 0.5000} - } - } - }, - ["_60"] = { - selection_box = { - type = "fixed", - fixed = { - {-1.000, -0.5000, -0.5000, 1.000, -0.3750, 0.5000}, - {-1.000, -0.5000, -0.8750, 0.2500, -0.3750, -0.5000}, - {-0.2500, -0.5000, 0.5000, 1.000, -0.3750, 0.8750}, - {-1.375, -0.5000, -0.1250, -1.000, -0.3750, 0.1875} - } - } - }, - }, - - ["cr"] = { - [""] = { - selection_box = { - type = "fixed", - fixed = { - {-0.5000, -0.5000, -0.5000, 0.6875, -0.3750, 0.5000}, - {-0.3750, -0.5000, -1.000, 1.000, -0.3750, 0.000} - } - } - }, - ["_30"] = { - selection_box = { - type = "fixed", - fixed = { - {-0.5000, -0.5000, -0.5000, 0.7500, -0.3750, 0.8750}, - {-0.3750, -0.5000, 0.8750, 0.2500, -0.3750, 1.188}, - {0.7500, -0.5000, 0.2500, 1.063, -0.3750, 0.8750} - } - } - }, - ["_45"] = { - selection_box = { - type = "fixed", - fixed = { - {-0.5000, -0.5000, -1.125, 0.5000, -0.3750, 0.6875}, - {-0.8750, -0.5000, -0.9375, -0.5000, -0.3750, 0.06250}, - {0.5000, -0.5000, -0.5000, 0.8750, -0.3750, 0.5000} - } - } - }, - ["_60"] = { - selection_box = { - type = "fixed", - fixed = { - {-0.8125, -0.5000, -0.5000, 1.188, -0.3750, 0.5000}, - {-0.1875, -0.5000, 0.5000, 0.8750, -0.3125, 0.8750}, - {-0.2500, -0.5000, -0.9375, 0.3125, -0.3125, -0.5000} - } - } - }, - }, - - ["swlst"] = { - [""] = { - selection_box = { - type = "fixed", - fixed = { - {-0.5000, -0.5000, -0.5000, 0.6250, -0.3750, 0.5000}, - {-0.3125, -0.5000, -1.000, 0.9375, -0.3125, -0.06250} - } - } - }, - ["_30"] = { - selection_box = { - type = "fixed", - fixed = { - {-0.5000, -0.5000, -1.000, 0.5000, -0.3750, 1.000}, - {-0.8750, -0.5000, -1.000, -0.5000, -0.3750, 0.2500}, - {0.5000, -0.5000, -0.2500, 0.8750, -0.3750, 1.000}, - {-0.1250, -0.5000, -1.375, 0.1875, -0.3750, -1.000} - } - } - }, - ["_45"] = { - selection_box = { - type = "fixed", - fixed = { - {-0.5000, -0.5000, -1.1875, 0.5000, -0.3750, 0.8750}, - {0.5000, -0.5000, -0.5000, 0.8750, -0.3750, 0.5000}, - {-0.8750, -0.5000, -0.8125, -0.5000, -0.3750, 0.5000} - } - } - }, - ["_60"] = { - selection_box = { - type = "fixed", - fixed = { - {-1.000, -0.5000, -0.5000, 1.000, -0.3750, 0.5000}, - {-1.000, -0.5000, -0.8750, 0.2500, -0.3750, -0.5000}, - {-0.2500, -0.5000, 0.5000, 1.000, -0.3750, 0.8750}, - {-1.375, -0.5000, -0.1250, -1.000, -0.3750, 0.1875} - } - } - }, - }, - - ["swrst"] = { - [""] = { - selection_box = { - type = "fixed", - fixed = { - {-0.5000, -0.5000, -0.5000, 0.6250, -0.3750, 0.5000}, - {-0.8125, -0.5000, -1.000, 0.4375, -0.3125, -0.06250} - } - } - }, - ["_30"] = { - selection_box = { - type = "fixed", - fixed = { - {-0.5000, -0.5000, -1.000, 0.5000, -0.3750, 1.000}, - {-0.8750, -0.5000, -1.000, -0.5000, -0.3750, 0.2500}, - {0.5000, -0.5000, -0.2500, 0.8750, -0.3750, 1.000}, - {-0.1250, -0.5000, -1.375, 0.1875, -0.3750, -1.000} - } - } - }, - ["_45"] = { - selection_box = { - type = "fixed", - fixed = { - {-1.1875, -0.5000, -0.5000, 0.8750, -0.3750, 0.5000}, - {-0.5000, -0.5000, 0.5000, 0.5000, -0.3750, 0.8750}, - {-0.8125, -0.5000, -0.8750, 0.5000, -0.3750, -0.5000} - } - } - }, - ["_60"] = { - selection_box = { - type = "fixed", - fixed = { - {-1.000, -0.5000, -0.5000, 1.000, -0.3750, 0.5000}, - {-1.000, -0.5000, -0.8750, 0.2500, -0.3750, -0.5000}, - {-0.2500, -0.5000, 0.5000, 1.000, -0.3750, 0.8750}, - {-1.375, -0.5000, -0.1250, -1.000, -0.3750, 0.1875} - } - } - }, - }, -} - -default_boxen["swlcr"] = default_boxen["swlst"] -default_boxen["swrcr"] = default_boxen["swrst"] - ---flat -advtrains.register_tracks("default", { - nodename_prefix="advtrains:dtrack", - texture_prefix="advtrains_dtrack", - models_prefix="advtrains_dtrack", - models_suffix=".b3d", - shared_texture="advtrains_dtrack_shared.png", - description=attrans("Track"), - formats={}, - - get_additional_definiton = function(def, preset, suffix, rotation) - if default_boxen[suffix] ~= nil and default_boxen[suffix][rotation] ~= nil then - return default_boxen[suffix][rotation] - else - return {} - end - end, -}, advtrains.ap.t_30deg_flat) - -minetest.register_craft({ - output = 'advtrains:dtrack_placer 50', - recipe = { - {'default:steel_ingot', 'group:stick', 'default:steel_ingot'}, - {'default:steel_ingot', 'group:stick', 'default:steel_ingot'}, - {'default:steel_ingot', 'group:stick', 'default:steel_ingot'}, - }, -}) - -local y3_boxen = { - [""] = { - selection_box = { - type = "fixed", - fixed = { - {-0.8750, -0.5000, -1.125, 0.8750, -0.3750, 0.4375} - } - } - }, - - ["_30"] = { - selection_box = { - type = "fixed", - fixed = { - {-0.5000, -0.5000, -0.875, 0.5000, -0.3750, 1.000}, - {-0.8750, -0.5000, -0.4375, -0.5000, -0.3750, 0.5625}, - {0.5000, -0.5000, -0.2500, 0.8125, -0.3750, 1.000}, - } - } - }, - - --UX FIXME: - 3way - have to place straight route before l and r or the - --nodebox overlaps too much and can't place the straight track node. - ["_45"] = { - selection_box = { - type = "fixed", - fixed = { - {-0.5000, -0.5000, -1.1250, 0.5000, -0.3750, 0.8750}, - {0.5000, -0.5000, -0.5000, 0.8750, -0.3750, 0.5000}, - {-1.1250, -0.5000, -0.9375, -0.5000, -0.3750, 0.5000} - } - } - }, - - ["_60"] = { - selection_box = { - type = "fixed", - fixed = { - --{-0.5000, -0.5000, -0.875, 0.5000, -0.3750, 1.000}, - {-0.875, -0.5000, -0.5, 1.0, -0.3750, 0.5}, - --{-0.8750, -0.5000, -0.4375, -0.5000, -0.3750, 0.5625}, - {-0.4375, -0.5000, -0.8750, 0.5625, -0.3750, -0.5000}, - --{0.5000, -0.5000, -0.2500, 0.8125, -0.3750, 1.000}, - {-0.2500, -0.5000, -0.2500, 1.0000, -0.3750, 0.8125}, - } - } - }, -} - - -local function y3_turnouts_addef(def, preset, suffix, rotation) - return y3_boxen[rotation] or {} -end --- y-turnout -advtrains.register_tracks("default", { - nodename_prefix="advtrains:dtrack_sy", - texture_prefix="advtrains_dtrack_sy", - models_prefix="advtrains_dtrack_sy", - models_suffix=".obj", - shared_texture="advtrains_dtrack_shared.png", - description=attrans("Y-turnout"), - formats = {}, - get_additional_definiton = y3_turnouts_addef, -}, advtrains.ap.t_yturnout) -minetest.register_craft({ - output = 'advtrains:dtrack_sy_placer 2', - recipe = { - {'advtrains:dtrack_placer', '', 'advtrains:dtrack_placer'}, - {'', 'advtrains:dtrack_placer', ''}, - {'', 'advtrains:dtrack_placer', ''}, - }, -}) ---3-way turnout -advtrains.register_tracks("default", { - nodename_prefix="advtrains:dtrack_s3", - texture_prefix="advtrains_dtrack_s3", - models_prefix="advtrains_dtrack_s3", - models_suffix=".obj", - shared_texture="advtrains_dtrack_shared.png", - description=attrans("3-way turnout"), - formats = {}, - get_additional_definiton = y3_turnouts_addef, -}, advtrains.ap.t_s3way) -minetest.register_craft({ - output = 'advtrains:dtrack_s3_placer 1', - recipe = { - {'advtrains:dtrack_placer', 'advtrains:dtrack_placer', 'advtrains:dtrack_placer'}, - {'', 'advtrains:dtrack_placer', ''}, - {'', '', ''}, - }, -}) - --- Diamond Crossings - -local perp_boxen = { - [""] = {}, --default size - ["_30"] = { - selection_box = { - type = "fixed", - fixed = { - {-1.000, -0.5000, -1.000, 1.000, -0.3750, 1.000} - } - } - }, - ["_45"] = { - selection_box = { - type = "fixed", - fixed = { - {-0.8125, -0.5000, -0.8125, 0.8125, -0.3750, 0.8125} - } - } - }, - ["_60"] = { - selection_box = { - type = "fixed", - fixed = { - {-1.000, -0.5000, -1.000, 1.000, -0.3750, 1.000} - } - } - }, -} - --- perpendicular -advtrains.register_tracks("default", { - nodename_prefix="advtrains:dtrack_xing", - texture_prefix="advtrains_dtrack_xing", - models_prefix="advtrains_dtrack_xing", - models_suffix=".obj", - shared_texture="advtrains_dtrack_shared.png", - description=attrans("Perpendicular Diamond Crossing Track"), - formats = {}, - get_additional_definiton = function(def, preset, suffix, rotation) - return perp_boxen[rotation] or {} - end -}, advtrains.ap.t_perpcrossing) - -minetest.register_craft({ - output = 'advtrains:dtrack_xing_placer 3', - recipe = { - {'', 'advtrains:dtrack_placer', ''}, - {'advtrains:dtrack_placer', 'advtrains:dtrack_placer', 'advtrains:dtrack_placer'}, - {'', 'advtrains:dtrack_placer', ''} - } -}) - -local ninety_plus_boxen = { - ["30l"] = { - selection_box = { - type = "fixed", - fixed = { - {-0.5000, -0.5000, -1.000, 0.5000, -0.3750, 1.000}, - {-0.8750, -0.5000, -1.000, -0.5000, -0.3750, 0.2500}, - {0.5000, -0.5000, -0.2500, 0.8750, -0.3750, 1.000}, - {-0.1250, -0.5000, -1.375, 0.1875, -0.3750, -1.000} - } - } - }, - ["30r"] = { - selection_box = { - type = "fixed", - fixed = { - {0.5000, -0.5000, -1.000, -0.5000, -0.3750, 1.000}, - {0.8750, -0.5000, -1.000, 0.5000, -0.3750, 0.2500}, - {-0.5000, -0.5000, -0.2500, -0.8750, -0.3750, 1.000}, - {0.1250, -0.5000, -1.375, -0.1875, -0.3750, -1.000} - } - } - }, - ["45l"] = { - selection_box = { - type = "fixed", - fixed = { - {-0.5000, -0.5000, -0.8750, 0.5000, -0.3750, 0.8750}, - {0.5000, -0.5000, -0.5000, 0.8750, -0.3750, 0.5000}, - {-0.8750, -0.5000, -0.5000, -0.5000, -0.3750, 0.5000} - } - } - }, - ["45r"] = { - selection_box = { - type = "fixed", - fixed = { - {-0.5000, -0.5000, -0.8750, 0.5000, -0.3750, 0.8750}, - {0.5000, -0.5000, -0.5000, 0.8750, -0.3750, 0.5000}, - {-0.8750, -0.5000, -0.5000, -0.5000, -0.3750, 0.5000} - } - } - }, - ["60l"] = { - selection_box = { - type = "fixed", - fixed = { - {-1.000, -0.5000, -0.5000, 1.000, -0.3750, 0.5000}, - {-1.000, -0.5000, -0.8750, 0.2500, -0.3750, -0.5000}, - {-0.2500, -0.5000, 0.5000, 1.000, -0.3750, 0.8750}, - {-1.375, -0.5000, -0.1250, -1.000, -0.3750, 0.1875} - } - } - }, - ["60r"] = { - selection_box = { - type = "fixed", - fixed = { - {1.000, -0.5000, -0.5000, -1.000, -0.3750, 0.5000}, - {1.000, -0.5000, -0.8750, -0.2500, -0.3750, -0.5000}, - {0.2500, -0.5000, 0.5000, -1.000, -0.3750, 0.8750}, - {1.375, -0.5000, -0.1250, 1.000, -0.3750, 0.1875} - } - } - }, -} - --- 90plusx --- When you face east and param2=0, then this set of rails has a rail at 90 --- degrees to the viewer, plus another rail crossing at 30, 45 or 60 degrees. -advtrains.register_tracks("default", { - nodename_prefix="advtrains:dtrack_xing90plusx", - texture_prefix="advtrains_dtrack_xing4590", - models_prefix="advtrains_dtrack_xing90plusx", - models_suffix=".obj", - shared_texture="advtrains_dtrack_shared.png", - description=attrans("90+Angle Diamond Crossing Track"), - formats = {}, - get_additional_definiton = function(def, preset, suffix, rotation) - return ninety_plus_boxen[suffix] or {} - end, -}, advtrains.ap.t_90plusx_crossing) -minetest.register_craft({ - output = 'advtrains:dtrack_xing90plusx_placer 2', - recipe = { - {'advtrains:dtrack_placer', '', ''}, - {'advtrains:dtrack_placer', 'advtrains:dtrack_placer', 'advtrains:dtrack_placer'}, - {'', '', 'advtrains:dtrack_placer'} - } -}) - --- Deprecate any rails using the old name scheme -minetest.register_lbm({ - label = "Upgrade legacy 4590 rails", - name = "advtrains_train_track:replace_legacy_4590", - nodenames = {"advtrains:dtrack_xing4590_st"}, - run_at_every_load = true, - action = function(pos, node) - minetest.log("actionPos!: " .. pos.x .. "," .. pos.y .. "," .. pos.z) - minetest.log("node!: " .. node.name .. "," .. node.param1 .. "," .. node.param2) - advtrains.ndb.swap_node(pos, - { - name="advtrains:dtrack_xing90plusx_45l", - param1=node.param1, - param2=node.param2, - }) - end -}) --- This will replace any items left in the inventory -minetest.register_alias("advtrains:dtrack_xing4590_placer", "advtrains:dtrack_xing90plusx_placer") - -local diagonal_boxen = { - ["30r45l"] = { - selection_box = { - type = "fixed", - fixed = { - {0.5000, -0.5000, -1.000, -0.5000, -0.3750, 1.000}, - {0.8750, -0.5000, -1.000, 0.5000, -0.3750, 0.2500}, - {-0.5000, -0.5000, -0.2500, -0.8750, -0.3750, 1.000}, - {0.1250, -0.5000, -1.375, -0.1875, -0.3750, -1.000} - } - } - }, - ["60l30l"] = { - selection_box = { - type = "fixed", - fixed = { - {-1.000, -0.5000, -0.5000, 1.000, -0.3750, 0.5000}, - {-1.000, -0.5000, -0.8750, 0.2500, -0.3750, -0.5000}, - {-0.2500, -0.5000, 0.5000, 1.000, -0.3750, 0.8750}, - {-1.375, -0.5000, -0.1250, -1.000, -0.3750, 0.1875} - } - } - }, - ["60l60r"] = { - selection_box = { - type = "fixed", - fixed = { - {-1.000, -0.5000, -1.000, 1.000, -0.3750, 1.000} - } - } - }, - ["60r30r"] = { - selection_box = { - type = "fixed", - fixed = { - {1.000, -0.5000, -0.5000, -1.000, -0.3750, 0.5000}, - {1.000, -0.5000, -0.8750, -0.2500, -0.3750, -0.5000}, - {0.2500, -0.5000, 0.5000, -1.000, -0.3750, 0.8750}, - {1.375, -0.5000, -0.1250, 1.000, -0.3750, 0.1875} - } - } - }, - ["30l45r"] = { - selection_box = { - type = "fixed", - fixed = { - {-0.5000, -0.5000, -1.000, 0.5000, -0.3750, 1.000}, - {-0.8750, -0.5000, -1.000, -0.5000, -0.3750, 0.2500}, - {0.5000, -0.5000, -0.2500, 0.8750, -0.3750, 1.000}, - {-0.1250, -0.5000, -1.375, 0.1875, -0.3750, -1.000} - } - } - }, - ["60l45r"] = { - selection_box = { - type = "fixed", - fixed = { - {-1.000, -0.5000, -0.5000, 1.000, -0.3750, 0.5000}, - {-1.000, -0.5000, -0.8750, 0.2500, -0.3750, -0.5000}, - {-0.2500, -0.5000, 0.5000, 1.000, -0.3750, 0.8750}, - {-1.375, -0.5000, -0.1250, -1.000, -0.3750, 0.1875} - } - } - }, - ["60r45l"] = { - selection_box = { - type = "fixed", - fixed = { - {1.000, -0.5000, -0.5000, -1.000, -0.3750, 0.5000}, - {1.000, -0.5000, -0.8750, -0.2500, -0.3750, -0.5000}, - {0.2500, -0.5000, 0.5000, -1.000, -0.3750, 0.8750}, - {1.375, -0.5000, -0.1250, 1.000, -0.3750, 0.1875} - } - } - }, -} - --- Diagonal --- This set of rail crossings is named based on the angle of each intersecting --- direction when facing east and param2=0. Rails with l/r swapped are mirror --- images. For example, 30r45l is the mirror image of 30l45r. -advtrains.register_tracks("default", { - nodename_prefix="advtrains:dtrack_xingdiag", - texture_prefix="advtrains_dtrack_xingdiag", - models_prefix="advtrains_dtrack_xingdiag", - models_suffix=".obj", - shared_texture="advtrains_dtrack_shared.png", - description=attrans("Diagonal Diamond Crossing Track"), - formats = {}, - get_additional_definiton = function(def, preset, suffix, rotation) - return diagonal_boxen[suffix] or {} - end, -}, advtrains.ap.t_diagonalcrossing) -minetest.register_craft({ - output = 'advtrains:dtrack_xingdiag_placer 2', - recipe = { - {'advtrains:dtrack_placer', '', 'advtrains:dtrack_placer'}, - {'', 'advtrains:dtrack_placer', ''}, - {'advtrains:dtrack_placer', '', 'advtrains:dtrack_placer'} - } -}) ----- Not included: very shallow crossings like (30/60)+45. ----- At an angle of only 18.4 degrees, the models would not ----- translate well to a block game. --- END crossings - ---slopes -advtrains.register_tracks("default", { - nodename_prefix="advtrains:dtrack", - texture_prefix="advtrains_dtrack", - models_prefix="advtrains_dtrack", - models_suffix=".obj", - shared_texture="advtrains_dtrack_shared.png", - second_texture="default_gravel.png", - description=attrans("Track"), - formats={vst1={true, false, true}, vst2={true, false, true}, vst31={true}, vst32={true}, vst33={true}}, -}, advtrains.ap.t_30deg_slope) - -minetest.register_craft({ - type = "shapeless", - output = 'advtrains:dtrack_slopeplacer 2', - recipe = { - "advtrains:dtrack_placer", - "advtrains:dtrack_placer", - "default:gravel", - }, -}) - - ---bumpers -advtrains.register_tracks("default", { - nodename_prefix="advtrains:dtrack_bumper", - texture_prefix="advtrains_dtrack_bumper", - models_prefix="advtrains_dtrack_bumper", - models_suffix=".b3d", - shared_texture="advtrains_dtrack_rail.png", - --bumpers still use the old texture until the models are redone. - description=attrans("Bumper"), - formats={}, -}, advtrains.ap.t_30deg_straightonly) -minetest.register_craft({ - output = 'advtrains:dtrack_bumper_placer 2', - recipe = { - {'group:wood', 'dye:red'}, - {'default:steel_ingot', 'default:steel_ingot'}, - {'advtrains:dtrack_placer', 'advtrains:dtrack_placer'}, - }, -}) ---legacy bumpers -for _,rot in ipairs({"", "_30", "_45", "_60"}) do - minetest.register_alias("advtrains:dtrack_bumper"..rot, "advtrains:dtrack_bumper_st"..rot) -end --- atc track -advtrains.register_tracks("default", { - nodename_prefix="advtrains:dtrack_atc", - texture_prefix="advtrains_dtrack_atc", - models_prefix="advtrains_dtrack", - models_suffix=".b3d", - shared_texture="advtrains_dtrack_shared_atc.png", - description=attrans("ATC controller"), - formats={}, - get_additional_definiton = advtrains.atc_function -}, advtrains.trackpresets.t_30deg_straightonly) - - --- Tracks for loading and unloading trains --- Copyright (C) 2017 Gabriel Pérez-Cerezo - -local function get_far_node(pos) - local node = minetest.get_node(pos) - if node.name == "ignore" then - minetest.get_voxel_manip():read_from_map(pos, pos) - node = minetest.get_node(pos) - end - return node -end - - -local function show_fc_formspec(pos,player) - local pname = player:get_player_name() - if minetest.is_protected(pos,pname) then - minetest.chat_send_player(pname, "Position is protected!") - return - end - - local meta = minetest.get_meta(pos) - local fc = meta:get_string("fc") or "" - - local form = 'formspec_version[4]'.. - 'size[10,5]'.. - 'label[0.5,0.4;Advtrains Loading/Unloading Track]'.. - 'label[0.5,1.1;Set the code to match against the wagon\'s freight code]'.. - 'label[0.5,1.6;A blank field matches all wagons (default)]'.. - 'label[0.5,2.1;Use code # to disable the track section]'.. - 'field[0.5,3;5.5,1;fc;FC;'..minetest.formspec_escape(fc)..']'.. - 'button[6.5,3;3,1;save;Submit]' - minetest.show_formspec(pname, "at_load_unload_"..advtrains.encode_pos(pos), form) -end - -minetest.register_on_player_receive_fields(function(player, formname, fields) - local pname = player:get_player_name() - local pe = string.match(formname, "^at_load_unload_(............)$") - local pos = advtrains.decode_pos(pe) - if pos then - if minetest.is_protected(pos, pname) then - minetest.chat_send_player(pname, "Position is protected!") - return - end - - if fields.save then - minetest.get_meta(pos):set_string("fc",tostring(fields.fc)) - minetest.chat_send_player(pname,"Freight code set: "..tostring(fields.fc)) - show_fc_formspec(pos,player) - end - end -end) - - -local function train_load(pos, train_id, unload) - local train=advtrains.trains[train_id] - local below = get_far_node({x=pos.x, y=pos.y-1, z=pos.z}) - if not string.match(below.name, "chest") then - atprint("this is not a chest! at "..minetest.pos_to_string(pos)) - return - end - - local node_fc = minetest.get_meta(pos):get_string("fc") or "" - if node_fc == "#" then - --track section is disabled - return - end - - local inv = minetest.get_inventory({type="node", pos={x=pos.x, y=pos.y-1, z=pos.z}}) - if inv and train.velocity < 2 then - for k, v in ipairs(train.trainparts) do - local i=minetest.get_inventory({type="detached", name="advtrains_wgn_"..v}) - if i and i:get_list("box") then - - local wagon_data = advtrains.wagons[v] - local wagon_fc - if wagon_data.fc then - if not wagon_data.fcind then wagon_data.fcind = 1 end - wagon_fc = tostring(wagon_data.fc[wagon_data.fcind]) or "" - end - - if node_fc == "" or wagon_fc == node_fc then - if not unload then - for _, item in ipairs(inv:get_list("main")) do - if i:get_list("box") and i:room_for_item("box", item) then - i:add_item("box", item) - inv:remove_item("main", item) - end - end - else - for _, item in ipairs(i:get_list("box")) do - if inv:get_list("main") and inv:room_for_item("main", item) then - i:remove_item("box", item) - inv:add_item("main", item) - end - end - end - end - end - end - end -end - - - -advtrains.register_tracks("default", { - nodename_prefix="advtrains:dtrack_unload", - texture_prefix="advtrains_dtrack_unload", - models_prefix="advtrains_dtrack", - models_suffix=".b3d", - shared_texture="advtrains_dtrack_shared_unload.png", - description=attrans("Unloading Track"), - formats={}, - get_additional_definiton = function(def, preset, suffix, rotation) - return { - after_dig_node=function(pos) - advtrains.invalidate_all_paths() - advtrains.ndb.clear(pos) - end, - on_rightclick = function(pos, node, player) - show_fc_formspec(pos, player) - end, - advtrains = { - on_train_enter = function(pos, train_id) - train_load(pos, train_id, true) - end, - }, - } - end - }, advtrains.trackpresets.t_30deg_straightonly) -advtrains.register_tracks("default", { - nodename_prefix="advtrains:dtrack_load", - texture_prefix="advtrains_dtrack_load", - models_prefix="advtrains_dtrack", - models_suffix=".b3d", - shared_texture="advtrains_dtrack_shared_load.png", - description=attrans("Loading Track"), - formats={}, - get_additional_definiton = function(def, preset, suffix, rotation) - return { - after_dig_node=function(pos) - advtrains.invalidate_all_paths() - advtrains.ndb.clear(pos) - end, - on_rightclick = function(pos, node, player) - show_fc_formspec(pos, player) - end, - advtrains = { - on_train_enter = function(pos, train_id) - train_load(pos, train_id, false) - end, - }, - } - end - }, advtrains.trackpresets.t_30deg_straightonly) - --- mod-dependent crafts -local loader_core = "default:mese_crystal" --fallback -if minetest.get_modpath("basic_materials") then - loader_core = "basic_materials:ic" -elseif minetest.get_modpath("technic") then - loader_core = "technic:control_logic_unit" -end ---print("Loader Core: "..loader_core) - -minetest.register_craft({ - type="shapeless", - output = 'advtrains:dtrack_load_placer', - recipe = { - "advtrains:dtrack_placer", - loader_core, - "default:chest" - }, -}) -loader_core = nil --nil the crafting variable - ---craft between load/unload tracks -minetest.register_craft({ - type="shapeless", - output = 'advtrains:dtrack_unload_placer', - recipe = { - "advtrains:dtrack_load_placer", - }, -}) -minetest.register_craft({ - type="shapeless", - output = 'advtrains:dtrack_load_placer', - recipe = { - "advtrains:dtrack_unload_placer", - }, -}) - - -if mesecon then - advtrains.register_tracks("default", { - nodename_prefix="advtrains:dtrack_detector_off", - texture_prefix="advtrains_dtrack_detector", - models_prefix="advtrains_dtrack", - models_suffix=".b3d", - shared_texture="advtrains_dtrack_shared_detector_off.png", - description=attrans("Detector Rail"), - formats={}, - get_additional_definiton = function(def, preset, suffix, rotation) - return { - mesecons = { - receptor = { - state = mesecon.state.off, - rules = advtrains.meseconrules - } - }, - advtrains = { - on_updated_from_nodedb = function(pos, node) - mesecon.receptor_off(pos, advtrains.meseconrules) - end, - on_train_enter=function(pos, train_id) - advtrains.ndb.swap_node(pos, {name="advtrains:dtrack_detector_on".."_"..suffix..rotation, param2=advtrains.ndb.get_node(pos).param2}) - if advtrains.is_node_loaded(pos) then - mesecon.receptor_on(pos, advtrains.meseconrules) - end - end - } - } - end - }, advtrains.ap.t_30deg_straightonly) - advtrains.register_tracks("default", { - nodename_prefix="advtrains:dtrack_detector_on", - texture_prefix="advtrains_dtrack", - models_prefix="advtrains_dtrack", - models_suffix=".b3d", - shared_texture="advtrains_dtrack_shared_detector_on.png", - description="Detector(on)(you hacker you)", - formats={}, - get_additional_definiton = function(def, preset, suffix, rotation) - return { - mesecons = { - receptor = { - state = mesecon.state.on, - rules = advtrains.meseconrules - } - }, - advtrains = { - on_updated_from_nodedb = function(pos, node) - mesecon.receptor_on(pos, advtrains.meseconrules) - end, - on_train_leave=function(pos, train_id) - advtrains.ndb.swap_node(pos, {name="advtrains:dtrack_detector_off".."_"..suffix..rotation, param2=advtrains.ndb.get_node(pos).param2}) - if advtrains.is_node_loaded(pos) then - mesecon.receptor_off(pos, advtrains.meseconrules) - end - end - } - } - end - }, advtrains.ap.t_30deg_straightonly_noplacer) -minetest.register_craft({ - type="shapeless", - output = 'advtrains:dtrack_detector_off_placer', - recipe = { - "advtrains:dtrack_placer", - "mesecons:wire_00000000_off" - }, -}) -end ---TODO legacy ---I know lbms are better for this purpose -for name,rep in pairs({swl_st="swlst", swr_st="swrst", swl_cr="swlcr", swr_cr="swrcr", }) do - minetest.register_abm({ - -- In the following two fields, also group:groupname will work. - nodenames = {"advtrains:track_"..name}, - interval = 1.0, -- Operation interval in seconds - chance = 1, -- Chance of trigger per-node per-interval is 1.0 / this - action = function(pos, node, active_object_count, active_object_count_wider) minetest.set_node(pos, {name="advtrains:track_"..rep, param2=node.param2}) end, - }) - minetest.register_abm({ - -- In the following two fields, also group:groupname will work. - nodenames = {"advtrains:track_"..name.."_45"}, - interval = 1.0, -- Operation interval in seconds - chance = 1, -- Chance of trigger per-node per-interval is 1.0 / this - action = function(pos, node, active_object_count, active_object_count_wider) minetest.set_node(pos, {name="advtrains:track_"..rep.."_45", param2=node.param2}) end, - }) -end - -if advtrains.register_replacement_lbms then -minetest.register_lbm({ - name = "advtrains:ramp_replacement_1", --- In the following two fields, also group:groupname will work. - nodenames = {"advtrains:track_vert1"}, - action = function(pos, node, active_object_count, active_object_count_wider) minetest.set_node(pos, {name="advtrains:dtrack_vst1", param2=(node.param2+2)%4}) end, -}) -minetest.register_lbm({ - name = "advtrains:ramp_replacement_1", --- -- In the following two fields, also group:groupname will work. - nodenames = {"advtrains:track_vert2"}, - action = function(pos, node, active_object_count, active_object_count_wider) minetest.set_node(pos, {name="advtrains:dtrack_vst2", param2=(node.param2+2)%4}) end, -}) - minetest.register_abm({ - name = "advtrains:st_rep_1", - -- In the following two fields, also group:groupname will work. - nodenames = {"advtrains:track_st"}, - interval=1, - chance=1, - action = function(pos, node, active_object_count, active_object_count_wider) minetest.set_node(pos, {name="advtrains:dtrack_st", param2=node.param2}) end, - }) - minetest.register_lbm({ - name = "advtrains:st_rep_1", - -- -- In the following two fields, also group:groupname will work. - nodenames = {"advtrains:track_st_45"}, - action = function(pos, node, active_object_count, active_object_count_wider) minetest.set_node(pos, {name="advtrains:dtrack_st_45", param2=node.param2}) end, - }) -end -- cgit v1.2.3 From 2dab59f05572fe6cf73cde353446a5a501550b41 Mon Sep 17 00:00:00 2001 From: orwell Date: Tue, 6 Feb 2024 21:10:40 +0100 Subject: Start changing APIs and applying proof-of-concept to ks signals --- advtrains_interlocking/init.lua | 2 +- advtrains_interlocking/signal_api.lua | 105 +++++++++++++++++--------- advtrains_signals_ks/init.lua | 138 +++++++++++++++++++++------------- 3 files changed, 156 insertions(+), 89 deletions(-) (limited to 'advtrains_interlocking/init.lua') diff --git a/advtrains_interlocking/init.lua b/advtrains_interlocking/init.lua index 4d959cc..9aa0c06 100644 --- a/advtrains_interlocking/init.lua +++ b/advtrains_interlocking/init.lua @@ -12,7 +12,7 @@ end local modpath = minetest.get_modpath(minetest.get_current_modname()) .. DIR_DELIM -advtrains.interlocking.aspect = dofile(modpath.."aspect.lua") +--advtrains.interlocking.aspect = dofile(modpath.."aspect.lua") dofile(modpath.."database.lua") dofile(modpath.."distant.lua") diff --git a/advtrains_interlocking/signal_api.lua b/advtrains_interlocking/signal_api.lua index 743e8e1..c2cc08b 100644 --- a/advtrains_interlocking/signal_api.lua +++ b/advtrains_interlocking/signal_api.lua @@ -2,18 +2,19 @@ local F = advtrains.formspec -local DANGER = { - main = 0, - shunt = false, +local signal = {} + +signal.MASP_HALT = { + name = "halt" + halt = true, } -advtrains.interlocking.DANGER = DANGER -advtrains.interlocking.GENERIC_FREE = { - main = -1, +signal.ASPI_HALT = { + main = 0, shunt = false, - dst = false, } -advtrains.interlocking.FULL_FREE = { + +signal.ASPI_FREE = { main = -1, shunt = false, proceed_as_main = true, @@ -25,14 +26,12 @@ Most parts of ywang's implementation are fine, especially I like the formspecs. - Signal gets distant assigned via field in signal aspect table (instead of explicitly) - Signal speed/shunt are no longer free-text but rather they need to be predefined in the node definition To do this: Differentiation between: -== Aspect Group == +== Main Aspect == This is what a signal is assigned by either the route system or the user. It is a string key which has an appropriate entry in the node definition (where it has a description assigned) The signal mod defines a function to set a signal to the most appropriate aspect. This function gets -a) the aspect group name +a) the main aspect table (straight from node def) b) the distant signal's aspect group name & aspect table -EVERY signal must define the special aspect group "halt". This must always be the most restrictive aspect possible. -The "halt" aspect group should ignore any distant info, in most cases it is called without them anyway. == Aspect == One concrete combination of lights/shapes that a signal signal shows. Handling these is at the discretion of @@ -50,6 +49,11 @@ Note that once apply_aspect returns, there is no need for advtrains anymore to q When the signal, for any reason, wants to change its aspect by itself *without* going through the signal API then it should update the aspect info cache by calling advtrains.interlocking.signal.update_aspect_info(pos) +Note that the apply_aspect function MUST accept the following main aspect, even if it is not defined in the main_aspects table: +{ name = "halt", halt = true } +It should cause the signal to show its most restrictive aspect. Typically it is a halt aspect, but e.g. for distant-only +signals this would be "expect stop". + == Aspect Info == The actual signal aspect in the already-known format. This is what the trains use to determine halt/proceed and speed. In this, the dst field has to be resolved. asp = { @@ -62,7 +66,10 @@ asp = { Node definition of signals: - The signal needs some logic to figure out, for each combination of its own aspect group and the distant signal's aspect, what aspect info it can/will show. ndef.advtrains = { - aspect_groups = { [name] = { description = "Proceed at full speed", } } + main_aspects = { + { name = "proceed" description = "Proceed at full speed", } + { name = "proceed2" description = "Proceed at full speed", } + } -- The numerical order determines the layout of the list in the selection dialog. apply_aspect = function(pos, asp_group, dst_aspgrp, dst_aspinfo) -- set the node to show the desired aspect -- called by advtrains when this signal's aspect group or the distant signal's aspect changes @@ -72,28 +79,61 @@ ndef.advtrains = { } ]] -advtrains.interlocking.signal_convert_aspect_if_necessary = advtrains.interlocking.aspect +-- Set a signal's aspect. +-- Signal aspects should only be set through this function. It takes care of: +-- - Storing the main aspect and dst pos for this signal permanently (until next change) +-- - Assigning the distant signal for this signal +-- - Calling apply_aspect() in the signal's node definition to make the signal show the aspect +-- - Calling apply_aspect() again whenever the distant signal changes its aspect +-- - Notifying this signal's distant signals about changes to this signal (unless skip_dst_notify is specified) +function signal.set_aspect(pos, main_aspect, dst_pos, skip_dst_notify) + -- TODO +end + +-- Gets the stored main aspect and distant signal position for this signal +-- This information equals the information last passed to set_aspect +-- It does not take into consideration the actual speed signalling, please use +-- get_aspect_info() for this +-- returns: main_aspect, dst_pos +function signal.get_aspect(pos) + --TODO +end + +function signal.get_distant_signals_of(pos) + --TODO +end -function advtrains.interlocking.update_signal_aspect(tcbs, skipdst) +-- Called when either this signal has changed its main aspect +-- or when this distant signal's currently assigned main signal has changed its aspect +-- It retrieves the signal's main aspect and aspect info and calls apply_aspect of the node definition +-- to update the signal's appearance and aspect info +-- pts: The signal position to update as encoded_pos +function signal.reapply_aspect(pts, p_mainaspect) + --TODO +end + +-- Update this signal's aspect based on the set route +-- +function signal.update_route_aspect(tcbs, skip_dst_notify) if tcbs.signal then - local asp = tcbs.aspect or DANGER - advtrains.interlocking.signal_set_aspect(tcbs.signal, asp, skipdst) + local asp = tcbs.aspect or signal.MASP_HALT + signal.set_aspect(tcbs.signal, asp, skip_dst_notify) end end -function advtrains.interlocking.signal_can_dig(pos) +function signal.can_dig(pos) return not advtrains.interlocking.db.get_sigd_for_signal(pos) end function advtrains.interlocking.signal_after_dig(pos) -- clear influence point - advtrains.interlocking.signal_clear_aspect(pos) - advtrains.distant.unassign_all(pos, true) + advtrains.distant.unassign_all(pos, true) -- TODO end --- should be called when aspect has changed on this signal. -function advtrains.interlocking.signal_on_aspect_changed(pos) +-- Update waiting trains and distant signals about a changed signal aspect +function signal.notify_on_aspect_changed(pos, skip_dst_notify) + --TODO update distant? local ipts, iconn = advtrains.interlocking.db.get_ip_by_signalpos(pos) if not ipts then return end local ipos = minetest.string_to_pos(ipts) @@ -103,7 +143,7 @@ function advtrains.interlocking.signal_on_aspect_changed(pos) minetest.after(0, advtrains.invalidate_all_paths, ipos) end -function advtrains.interlocking.signal_rc_handler(pos, node, player, itemstack, pointed_thing) +function signal.on_rightclick(pos, node, player, itemstack, pointed_thing) local pname = player:get_player_name() local control = player:get_player_control() if control.aux1 then @@ -122,7 +162,7 @@ function advtrains.interlocking.show_signal_form(pos, node, pname) if ndef.advtrains and ndef.advtrains.set_aspect then -- permit to set aspect manually local function callback(pname, aspect) - advtrains.interlocking.signal_set_aspect(pos, aspect) + signal.set_aspect(pos, aspect) end local isasp = advtrains.interlocking.signal_get_aspect(pos, node) @@ -138,18 +178,6 @@ function advtrains.interlocking.show_signal_form(pos, node, pname) end end --- Returns the aspect the signal at pos is supposed to show -function advtrains.interlocking.signal_get_supposed_aspect(pos) - local sigd = advtrains.interlocking.db.get_sigd_for_signal(pos) - if sigd then - local tcbs = advtrains.interlocking.db.get_tcbs(sigd) - if tcbs.aspect then - return convert_aspect_if_necessary(tcbs.aspect) - end - end - return DANGER; -end - local players_assign_ip = {} local function ipmarker(ipos, connid) @@ -236,7 +264,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) end) -- inits the signal IP assignment process -function advtrains.interlocking.signal_init_ip_assign(pos, pname) +function signal.init_ip_assign(pos, pname) if not minetest.check_player_privs(pname, "interlocking") then minetest.chat_send_player(pname, "Insufficient privileges to use this!") return @@ -281,3 +309,6 @@ minetest.register_on_punchnode(function(pos, node, player, pointed_thing) players_assign_ip[pname] = nil end end) + + +advtrains.interlocking.signal = signal diff --git a/advtrains_signals_ks/init.lua b/advtrains_signals_ks/init.lua index 7e285ae..c91b4ec 100755 --- a/advtrains_signals_ks/init.lua +++ b/advtrains_signals_ks/init.lua @@ -11,10 +11,9 @@ local function asp_to_zs3type(asp) return math.min(16,4*math.floor(n/4)) end -local function setzs3(msp, lim, rot) +local function setzs3(msp, asp, rot) local pos = {x = msp.x, y = msp.y+1, z = msp.z} local node = advtrains.ndb.get_node(pos) - local asp = asp_to_zs3type(lim) if node.name:find("^advtrains_signals_ks:zs3_") then advtrains.ndb.swap_node(pos, {name="advtrains_signals_ks:zs3_"..asp.."_"..rot, param2 = node.param2}) end @@ -50,67 +49,106 @@ local function getzs3v(msp) end local setaspectf = function(rot) - return function(pos, node, asp) - setzs3(pos, asp.main, rot) - if asp.main == 0 then - if asp.shunt then - advtrains.ndb.swap_node(pos, {name="advtrains_signals_ks:hs_shunt_"..rot, param2 = node.param2}) - else - advtrains.ndb.swap_node(pos, {name="advtrains_signals_ks:hs_danger_"..rot, param2 = node.param2}) - end + return function(pos, node, main_aspect, dst_aspect, dst_aspect_info) + -- set zs3 signal to show speed according to main_aspect + setzs3(pos, asp.zs3, rot) + -- select appropriate lamps based on mainaspect and dst + if main_aspect.shunt then + advtrains.ndb.swap_node(pos, {name="advtrains_signals_ks:hs_shunt_"..rot, param2 = node.param2}) + setzs3v(pos, nil, rot) + elseif main_aspect.halt then + advtrains.ndb.swap_node(pos, {name="advtrains_signals_ks:hs_danger_"..rot, param2 = node.param2}) setzs3v(pos, nil, rot) else - if not asp.dst or asp.dst == -1 then + if not dst_aspect_info + or not dst_aspect_info.main + or dst_aspect_info.main == -1 then advtrains.ndb.swap_node(pos, {name="advtrains_signals_ks:hs_free_"..rot, param2 = node.param2}) - elseif asp.dst == 0 then + setzs3v(pos, nil, rot) + elseif dst_aspect_info.main == 0 then advtrains.ndb.swap_node(pos, {name="advtrains_signals_ks:hs_slow_"..rot, param2 = node.param2}) + setzs3v(pos, nil, rot) else advtrains.ndb.swap_node(pos, {name="advtrains_signals_ks:hs_nextslow_"..rot, param2 = node.param2}) + setzs3v(pos, dst_aspect_info.main, rot) end - setzs3v(pos, asp.dst, rot) end end end - -local suppasp = { - main = {0, 4, 6, 8, 12, 16, -1}, - dst = {0, 4, 6, 8, 12, 16, -1, false}, - shunt = nil, - proceed_as_main = true, - info = { - call_on = false, - dead_end = false, - w_speed = nil, - } +-- Main aspects main signal +-- These aspects tell only the speed signalization at this signal. +-- Actual signal aspect is chosen based on this and the Dst signal. +local mainaspects_main = { + { + name = "proceed" + description = "Proceed", + zs3 = "off" + }, + { + name = "shunt" + description = "Shunt", + zs3 = "off", + shunt = true, + }, + { + name = "proceed_16" + description = "Proceed (speed 16)", + zs3 = "16", + }, + { + name = "proceed_12" + description = "Proceed (speed 12)", + zs3 = "12", + }, + { + name = "proceed_8" + description = "Proceed (speed 8)", + zs3 = "8", + }, + { + name = "proceed_6" + description = "Proceed (speed 6)", + zs3 = "6", + }, + { + name = "proceed_4" + description = "Proceed (speed 4)", + zs3 = "4", + }, + { + name = "halt" + description = "Halt", + zs3 = "off", + halt = true, + }, } --Rangiersignal -local setaspectf_ra = function(rot) - return function(pos, node, asp) - if asp.shunt then +local applyaspectf_ra = function(rot) + -- we get here the full main_aspect table + return function(pos, node, main_aspect, dst_aspect, dst_aspect_info) + if main_aspect.shunt then advtrains.ndb.swap_node(pos, {name="advtrains_signals_ks:ra_shuntd_"..rot, param2 = node.param2}) else advtrains.ndb.swap_node(pos, {name="advtrains_signals_ks:ra_danger_"..rot, param2 = node.param2}) end - local meta = minetest.get_meta(pos) - if meta then - meta:set_string("infotext", minetest.serialize(asp)) - end end end -local suppasp_ra = { - main = { false }, - dst = { false }, - shunt = nil, - proceed_as_main = false, - - info = { - call_on = false, - dead_end = false, - w_speed = nil, - } +-- Main aspects shunt signal +-- Shunt signals have only two states, distant doesn't matter +local mainaspects_shunt = { + { + name = "shunt" + description = "Shunt", + shunt = true, + }, + { + name = "halt" + description = "Halt", + halt = true, + }, } advtrains.trackplacer.register_tracktype("advtrains_signals_ks:hs") @@ -192,9 +230,9 @@ for _, rtab in ipairs({ drop = "advtrains_signals_ks:hs_danger_0", inventory_image = "advtrains_signals_ks_hs_inv.png", advtrains = { - set_aspect = setaspectf(rot), - supported_aspects = suppasp, - get_aspect = afunc, + main_aspects = mainaspects_main + apply_aspect = applyaspectf_main(rot), + get_aspect_info = afunc, }, on_rightclick = advtrains.interlocking.signal_rc_handler, can_dig = advtrains.interlocking.signal_can_dig, @@ -235,11 +273,9 @@ for _, rtab in ipairs({ drop = "advtrains_signals_ks:ra_danger_0", inventory_image = "advtrains_signals_ks_ra_inv.png", advtrains = { - set_aspect = setaspectf_ra(rot), - supported_aspects = suppasp_ra, - get_aspect = function(pos, node) - return prts.asp - end, + main_aspects = mainaspects_ra, + apply_aspect = applyaspectf_ra(rot), + get_aspect_info = prts.asp, }, on_rightclick = advtrains.interlocking.signal_rc_handler, can_dig = advtrains.interlocking.signal_can_dig, @@ -276,7 +312,7 @@ for _, rtab in ipairs({ drop = "advtrains_signals_ks:"..prefix.."_"..dtyp.."_0", inventory_image = inv, advtrains = { - get_aspect = function() return asp end + get_aspect_info = asp }, on_rightclick = advtrains.interlocking.signal_rc_handler, can_dig = advtrains.interlocking.signal_can_dig, -- cgit v1.2.3 From 6fd845baec0f5aa8b7cdee1adf8d05061a643242 Mon Sep 17 00:00:00 2001 From: orwell Date: Thu, 23 May 2024 00:58:24 +0200 Subject: Connect the ropes, start on making the UI work --- advtrains/passive.lua | 2 +- advtrains_interlocking/database.lua | 2 +- advtrains_interlocking/distant.lua | 200 ----------- advtrains_interlocking/init.lua | 3 - advtrains_interlocking/route_ui.lua | 18 +- advtrains_interlocking/routesetting.lua | 15 +- advtrains_interlocking/signal_api.lua | 220 +++---------- advtrains_interlocking/signal_aspect_accessors.lua | 163 --------- advtrains_interlocking/signal_aspect_ui.lua | 366 ++++++++------------- advtrains_interlocking/tcb_ts_ui.lua | 11 +- advtrains_signals_japan/init.lua | 6 +- advtrains_signals_ks/init.lua | 23 +- 12 files changed, 220 insertions(+), 809 deletions(-) delete mode 100644 advtrains_interlocking/distant.lua delete mode 100644 advtrains_interlocking/signal_aspect_accessors.lua (limited to 'advtrains_interlocking/init.lua') diff --git a/advtrains/passive.lua b/advtrains/passive.lua index 231da82..37b79e4 100644 --- a/advtrains/passive.lua +++ b/advtrains/passive.lua @@ -57,7 +57,7 @@ function advtrains.setstate(parpos, newstate, pnode) return false, "train_here" end - if advtrains.interlocking and advtrains.interlocking.route.has_route_lock(minetest.encode_pos(pos)) then + if advtrains.interlocking and advtrains.interlocking.route.has_route_lock(advtrains.encode_pos(pos)) then return false, "route_lock_here" end diff --git a/advtrains_interlocking/database.lua b/advtrains_interlocking/database.lua index 4213c3d..e2df547 100644 --- a/advtrains_interlocking/database.lua +++ b/advtrains_interlocking/database.lua @@ -1006,7 +1006,7 @@ end function ildb.get_ip_signal_asp(pts, connid) local p = ildb.get_ip_signal(pts, connid) if p then - local asp = advtrains.interlocking.signal_get_aspect(p) + local asp = advtrains.interlocking.signal.get_aspect(p) if not asp then atlog("Clearing orphaned signal influence point", pts, "/", connid) ildb.clear_ip_signal(pts, connid) diff --git a/advtrains_interlocking/distant.lua b/advtrains_interlocking/distant.lua deleted file mode 100644 index 32ada82..0000000 --- a/advtrains_interlocking/distant.lua +++ /dev/null @@ -1,200 +0,0 @@ ---- Distant signaling. --- This module implements a database backend for distant signal assignments. --- The actual modifications to signal aspects are still done by signal aspect accessors. --- @module advtrains.interlocking.distant - -local db_distant = {} -local db_distant_of = {} - -local pts = advtrains.encode_pos -local stp = advtrains.decode_pos - ---- Replace the distant signal assignment database. --- @function load --- @param db The new database to load. -local function db_load(x) - if type(x) ~= "table" then - return - end - db_distant = x.distant - db_distant_of = x.distant_of -end - ---- Retrieve the current distant signal assignment database. --- @function save --- @return The current database. -local function db_save() - return { - distant = db_distant, - distant_of = db_distant_of, - } -end - -local update_signal, update_main, update_dst - ---- Unassign a distant signal. --- @function unassign_dst --- @param dst The position of the distant signal. --- @param[opt=false] force Whether to skip callbacks. -local function unassign_dst(dst, force) - local pts_dst = pts(dst) - local main = db_distant_of[pts_dst] - db_distant_of[pts_dst] = nil - if main then - local pts_main = main[1] - local t = db_distant[pts_main] - if t then - t[pts_dst] = nil - end - end - if not force then - update_dst(dst) - end -end - ---- Unassign a main signal. --- @function unassign_main --- @param main The position of the main signal. --- @param[opt=false] force Whether to skip callbacks. -local function unassign_main(main, force) - local pts_main = pts(main) - local t = db_distant[pts_main] - if not t then - return - end - for pts_dst in pairs(t) do - local realmain = db_distant_of[pts_dst] - if realmain and realmain[1] == pts_main then - db_distant_of[pts_dst] = nil - if not force then - local dst = stp(pts_dst) - update_dst(dst) - end - end - end - db_distant[pts_main] = nil -end - ---- Remove all (main and distant) signal assignments from a signal. --- @function unassign_all --- @param pos The position of the signal. --- @param[opt=false] force Whether to skip callbacks. -local function unassign_all(pos, force) - unassign_main(pos) - unassign_dst(pos, force) -end - ---- Check whether a signal is "appropriate" for the distant signal system. --- Currently, a signal is considered appropriate if its signal aspect can be set. --- @function appropriate_signal --- @param pos The position of the signal -local function appropriate_signal(pos) - local node = advtrains.ndb.get_node(pos) - local ndef = minetest.registered_nodes[node.name] or {} - if not ndef then - return false - end - local atdef = ndef.advtrains - if not atdef then - return false - end - return atdef.supported_aspects and atdef.set_aspect and true -end - ---- Assign a distant signal to a main signal. --- @function assign --- @param main The position of the main signal. --- @param dst The position of the distant signal. --- @param[opt="manual"] by The method of assignment. --- @param[opt=false] skip_update Whether to skip callbacks. -local function assign(main, dst, by, skip_update) - if not (appropriate_signal(main) and appropriate_signal(dst)) then - return - end - local pts_main = pts(main) - local pts_dst = pts(dst) - local t = db_distant[pts_main] - if not t then - t = {} - db_distant[pts_main] = t - end - if not by then - by = "manual" - end - unassign_dst(dst, true) - t[pts_dst] = by - db_distant_of[pts_dst] = {pts_main, by} - if not skip_update then - update_dst(dst) - end -end - ---- Get the distant signals assigned to a main signal. --- @function get_distant --- @param main The position of the main signal. --- @treturn {[pos]=by,...} A table of distant signals, with the positions encoded using `advtrains.encode_pos`. -local function get_distant(main) - local pts_main = pts(main) - return db_distant[pts_main] or {} -end - ---- Get the main signal assigned the a distant signal. --- @function get_main --- @param dst The position of the distant signal. --- @return The position of the main signal. --- @return The method of assignment. -local function get_main(dst) - local pts_dst = pts(dst) - local main = db_distant_of[pts_dst] - if not main then - return - end - if main[1] then - return stp(main[1]), unpack(main, 2) - else - return unpack(main) - end -end - ---- Update all distant signals assigned to a main signal. --- @function update_main --- @param main The position of the main signal. -update_main = function(main) - local pts_main = pts(main) - local t = get_distant(main) - for pts_dst in pairs(t) do - local dst = stp(pts_dst) - advtrains.interlocking.signal_readjust_aspect(dst) - end -end - ---- Update the aspect of a distant signal. --- @function update_dst --- @param dst The position of the distant signal. -update_dst = function(dst) - advtrains.interlocking.signal_readjust_aspect(dst) -end - ---- Update the aspect of a combined (main and distant) signal and all distant signals assigned to it. --- @function update_signal --- @param pos The position of the signal. -update_signal = function(pos) - update_main(pos) - update_dst(pos) -end - -advtrains.distant = { - load = db_load, - save = db_save, - assign = assign, - unassign_dst = unassign_dst, - unassign_main = unassign_main, - unassign_all = unassign_all, - get_distant = get_distant, - get_dst = get_distant, - get_main = get_main, - update_main = update_main, - update_dst = update_dst, - update_signal = update_signal, - appropriate_signal = appropriate_signal, -} diff --git a/advtrains_interlocking/init.lua b/advtrains_interlocking/init.lua index dd08b4a..c397aa6 100644 --- a/advtrains_interlocking/init.lua +++ b/advtrains_interlocking/init.lua @@ -15,9 +15,6 @@ local modpath = minetest.get_modpath(minetest.get_current_modname()) .. DIR_DELI --advtrains.interlocking.aspect = dofile(modpath.."aspect.lua") dofile(modpath.."database.lua") -dofile(modpath.."distant.lua") -dofile(modpath.."distant_ui.lua") -dofile(modpath.."signal_aspect_accessors.lua") dofile(modpath.."signal_api.lua") dofile(modpath.."signal_aspect_ui.lua") dofile(modpath.."demosignals.lua") diff --git a/advtrains_interlocking/route_ui.lua b/advtrains_interlocking/route_ui.lua index a1a331d..982c579 100644 --- a/advtrains_interlocking/route_ui.lua +++ b/advtrains_interlocking/route_ui.lua @@ -33,7 +33,7 @@ function atil.show_route_edit_form(pname, sigd, routeid) local function itab(t) tab[#tab+1] = minetest.formspec_escape(string.gsub(t, ",", " ")) end - itab("TCB "..sigd_to_string(sigd).." ("..(tcbs.signal_name or "")..") Route #"..routeid) + itab("("..(tcbs.signal_name or "+")..") Route #"..routeid) -- this code is partially copy-pasted from routesetting.lua -- we start at the tc designated by signal @@ -56,13 +56,14 @@ function atil.show_route_edit_form(pname, sigd, routeid) c_rseg = route[i] c_lckp = {} - itab(""..i.." Entry "..sigd_to_string(c_sigd).." -> Sec. "..(c_ts and c_ts.name or "-").." -> Exit "..(c_rseg.next and sigd_to_string(c_rseg.next) or "END")) + itab(""..i.." "..sigd_to_string(c_sigd)) + itab("= "..(c_ts and c_ts.name or "-").." =") if c_rseg.locks then for pts, state in pairs(c_rseg.locks) do local pos = minetest.string_to_pos(pts) - itab(" Lock: "..pts.." -> "..state) + itab("L "..pts.." -> "..state) if not advtrains.is_passive(pos) then itab("-!- No passive component at "..pts..". Please reconfigure route!") break @@ -75,16 +76,17 @@ function atil.show_route_edit_form(pname, sigd, routeid) end if c_sigd then local e_tcbs = ildb.get_tcbs(c_sigd) - itab("Route end: "..sigd_to_string(c_sigd).." ("..(e_tcbs and e_tcbs.signal_name or "-")..")") + local signame = "-" + if e_tcbs and e_tcbs.signal then signame = e_tcbs.signal_name or "+" end + itab("E "..sigd_to_string(c_sigd).." ("..signame..")") else - itab("Route ends on dead-end") + itab("E (none)") end - form = form.."textlist[0.5,2;7.75,3.9;rtelog;"..table.concat(tab, ",").."]" + form = form.."textlist[0.5,2;3,3.9;rtelog;"..table.concat(tab, ",").."]" form = form.."button[0.5,6;3,1;back;<<< Back to signal]" - form = form.."button[4.5,6;2,1;aspect;Signal Aspect]" - form = form.."button[6.5,6;2,1;delete;Delete Route]" + form = form.."button[5.5,6;3,1;delete;Delete Route]" --atdebug(route.ars) form = form.."style[ars;font=mono]" diff --git a/advtrains_interlocking/routesetting.lua b/advtrains_interlocking/routesetting.lua index 24b3199..a576139 100644 --- a/advtrains_interlocking/routesetting.lua +++ b/advtrains_interlocking/routesetting.lua @@ -138,7 +138,7 @@ function ilrs.set_route(signal, route, try) } if c_tcbs.signal then c_tcbs.route_committed = true - c_tcbs.aspect = route.aspect or advtrains.interlocking.FULL_FREE + c_tcbs.aspect = advtrains.interlocking.signal.MASP_FREE c_tcbs.route_origin = signal signals[#signals+1] = c_tcbs end @@ -166,7 +166,7 @@ function ilrs.set_route(signal, route, try) if (not nodst) and (not assigned_by or assigned_by == "routesetting") then advtrains.distant.assign(lastsig, pos, "routesetting", true) end - advtrains.interlocking.update_signal_aspect(tcbs, i ~= 1) + advtrains.interlocking.signal.update_route_aspect(tcbs, i ~= 1) end end @@ -278,14 +278,7 @@ function ilrs.cancel_route_from(sigd) c_tcbs.route_auto = nil c_tcbs.route_origin = nil - if c_tcbs.signal then - local pos = c_tcbs.signal - local _, assigned_by = advtrains.distant.get_main(pos) - if assigned_by == "routesetting" then - advtrains.distant.unassign_dst(pos, true) - end - end - advtrains.interlocking.update_signal_aspect(c_tcbs) + advtrains.interlocking.signal.update_route_aspect(c_tcbs) c_ts_id = c_tcbs.ts_id if not c_tcbs then @@ -370,7 +363,7 @@ function ilrs.update_route(sigd, tcbs, newrte, cancel) end if has_changed_aspect then -- FIX: prevent an minetest.after() loop caused by update_signal_aspect dispatching path invalidation, which in turn calls ARS again - advtrains.interlocking.update_signal_aspect(tcbs) + advtrains.interlocking.signal.update_route_aspect(tcbs) end advtrains.interlocking.update_player_forms(sigd) end diff --git a/advtrains_interlocking/signal_api.lua b/advtrains_interlocking/signal_api.lua index d27a045..5216594 100644 --- a/advtrains_interlocking/signal_api.lua +++ b/advtrains_interlocking/signal_api.lua @@ -5,9 +5,15 @@ local F = advtrains.formspec local signal = {} signal.MASP_HALT = { - name = "halt", - description = "HALT", - halt = true, + name = nil, + speed = nil, + remote = nil, +} + +signal.MASP_FREE = { + name = "_free", + speed = -1, + remote = nil, } signal.ASPI_HALT = { @@ -50,11 +56,12 @@ Note that once apply_aspect returns, there is no need for advtrains anymore to q When the signal, for any reason, wants to change its aspect by itself *without* going through the signal API then it should update the aspect info cache by calling advtrains.interlocking.signal.update_aspect_info(pos) -Note that the apply_aspect function MUST accept the following main aspect, even if it is not defined in the main_aspects table: -{ name = "halt", halt = true } -It should cause the signal to show its most restrictive aspect. Typically it is a halt aspect, but e.g. for distant-only +Apply_aspect may also receive nil as the main aspect. It usually means that the signal is not assigned to anything particular, +and it should cause the signal to show its most restrictive aspect. Typically it is a halt aspect, but e.g. for distant-only signals this would be "expect stop". +Main aspect names starting with underscore (e.g. "_default") are reserved and must not be used! + == Aspect Info == The actual signal aspect in the already-known format. This is what the trains use to determine halt/proceed and speed. asp = { @@ -152,7 +159,7 @@ end -- - Storing the main aspect and dst pos for this signal permanently (until next change) -- - Assigning the distant signal for this signal -- - Calling apply_aspect() in the signal's node definition to make the signal show the aspect --- - Calling apply_aspect() again whenever the distant signal changes its aspect +-- - Calling apply_aspect() again whenever the remote signal changes its aspect -- - Notifying this signal's distant signals about changes to this signal (unless skip_dst_notify is specified) function signal.set_aspect(pos, main_asp_name, main_asp_speed, rem_pos, skip_dst_notify) local main_pts = advtrains.encode_pos(pos) @@ -252,10 +259,7 @@ function signal.get_aspect(pos) end local function cache_mainaspects(ndefat) - ndefat.main_aspects_lookup = { - -- always define halt aspect - halt = signal.MASP_HALT - } + ndefat.main_aspects_lookup = {} for _,ma in ipairs(ndefat.main_aspects) do ndefat.main_aspects_lookup[ma.name] = ma end @@ -264,7 +268,7 @@ end function signal.get_aspect_internal(pos, aspt) if not aspt then -- oh, no main aspect, nevermind - return nil, aspt.remote, nil + return nil, nil, nil end atdebug("get_aspect_internal",pos,aspt) -- look aspect in nodedef @@ -277,6 +281,10 @@ function signal.get_aspect_internal(pos, aspt) cache_mainaspects(ndefat) end local masp = ndefat.main_aspects_lookup[aspt.name] + -- special handling for the default free aspect ("_free") + if aspt.name == "_free" then + masp = ndefat.main_aspects[1] + end if not masp then atwarn(pos,"invalid main aspect",aspt.name,"valid are",ndefat.main_aspects_lookup) return nil, aspt.remote, node, ndef @@ -355,10 +363,30 @@ end function signal.update_route_aspect(tcbs, skip_dst_notify) if tcbs.signal then local asp = tcbs.aspect or signal.MASP_HALT - signal.set_aspect(tcbs.signal, asp, skip_dst_notify) + signal.set_aspect(tcbs.signal, asp.name, asp.speed, asp.remote, skip_dst_notify) end end +-- Returns how capable the signal is with regards to aspect setting +-- 0: not a signal at all +-- 1: signal has get_aspect_info() but the aspect is not variable (e.g. a sign) +-- 2: signal has apply_aspect() but does not have main aspects (e.g. a pure distant signal) +-- 3: Full capabilities, signal has main aspects and can be used as main/shunt signal (can be start/endpoint of a route) +function signal.get_signal_cap_level(pos) + local node = advtrains.ndb.get_node_or_nil(pos) + local ndef = node and minetest.registered_nodes[node.name] + local ndefat = ndef and ndef.advtrains + if ndefat and ndefat.get_aspect_info then + if ndefat.apply_aspect then + if ndefat.main_aspects then + return 3 + end + return 2 + end + return 1 + end + return 0 +end ---------------- @@ -366,7 +394,7 @@ function signal.can_dig(pos) return not advtrains.interlocking.db.get_sigd_for_signal(pos) end -function advtrains.interlocking.signal_after_dig(pos) +function signal.after_dig(pos) -- TODO clear influence point advtrains.interlocking.signal.clear_aspect(pos) end @@ -374,169 +402,7 @@ end function signal.on_rightclick(pos, node, player, itemstack, pointed_thing) local pname = player:get_player_name() local control = player:get_player_control() - if control.aux1 then - advtrains.interlocking.show_ip_form(pos, pname) - return - end - advtrains.interlocking.show_signal_form(pos, node, pname) -end - -function advtrains.interlocking.show_signal_form(pos, node, pname) - local sigd = advtrains.interlocking.db.get_sigd_for_signal(pos) - if sigd then - advtrains.interlocking.show_signalling_form(sigd, pname) - else - local ndef = minetest.registered_nodes[node.name] - if ndef.advtrains and ndef.advtrains.set_aspect then - -- permit to set aspect manually - local function callback(pname, aspect) - signal.set_aspect(pos, aspect) - end - local isasp = advtrains.interlocking.signal_get_aspect(pos, node) - - advtrains.interlocking.show_signal_aspect_selector( - pname, - ndef.advtrains.supported_aspects, - pos, callback, - isasp) - else - --static signal - only IP - advtrains.interlocking.show_ip_form(pos, pname) - end - end -end - -local players_assign_ip = {} - -local function ipmarker(ipos, connid) - local node_ok, conns, rhe = advtrains.get_rail_info_at(ipos, advtrains.all_tracktypes) - if not node_ok then return end - local yaw = advtrains.dir_to_angle(conns[connid].c) - - -- using tcbmarker here - local obj = minetest.add_entity(vector.add(ipos, {x=0, y=0.2, z=0}), "advtrains_interlocking:tcbmarker") - if not obj then return end - obj:set_yaw(yaw) - obj:set_properties({ - textures = { "at_il_signal_ip.png" }, - }) -end - -function advtrains.interlocking.make_ip_formspec_component(pos, x, y, w) - advtrains.interlocking.db.check_for_duplicate_ip(pos) - local pts, connid = advtrains.interlocking.db.get_ip_by_signalpos(pos) - if pts then - return table.concat { - F.S_label(x, y, "Influence point is set at @1.", string.format("%s/%s", pts, connid)), - F.S_button_exit(x, y+0.5, w/2-0.125, "ip_set", "Modify"), - F.S_button_exit(x+w/2+0.125, y+0.5, w/2-0.125, "ip_clear", "Clear"), - }, pts, connid - else - return table.concat { - F.S_label(x, y, "Influence point is not set."), - F.S_button_exit(x, y+0.5, w, "ip_set", "Set influence point"), - } - end -end - --- shows small info form for signal properties --- This function is named show_ip_form because it was originally only intended --- for assigning/changing the influence point. --- only_notset: show only if it is not set yet (used by signal tcb assignment) -function advtrains.interlocking.show_ip_form(pos, pname, only_notset) - if not minetest.check_player_privs(pname, "interlocking") then - return - end - local ipform, pts, connid = advtrains.interlocking.make_ip_formspec_component(pos, 0.5, 0.5, 7) - local form = { - "formspec_version[4]", - "size[8,2.25]", - ipform, - } - if pts then - local ipos = minetest.string_to_pos(pts) - ipmarker(ipos, connid) - end - if advtrains.distant.appropriate_signal(pos) then - form[#form+1] = advtrains.interlocking.make_dst_formspec_component(pos, 0.5, 2, 7, 4.25) - form[2] = "size[8,6.75]" - end - form = table.concat(form) - if not only_notset or not pts then - minetest.show_formspec(pname, "at_il_propassign_"..minetest.pos_to_string(pos), form) - end + advtrains.interlocking.show_signal_form(pos, node, pname, control.aux1) end -function advtrains.interlocking.handle_ip_formspec_fields(pname, pos, fields) - if not (pos and minetest.check_player_privs(pname, {train_operator=true, interlocking=true})) then - return - end - if fields.ip_set then - advtrains.interlocking.signal_init_ip_assign(pos, pname) - elseif fields.ip_clear then - advtrains.interlocking.db.clear_ip_by_signalpos(pos) - end -end - -minetest.register_on_player_receive_fields(function(player, formname, fields) - local pname = player:get_player_name() - local pts = string.match(formname, "^at_il_propassign_([^_]+)$") - local pos - if pts then - pos = minetest.string_to_pos(pts) - end - if pos then - advtrains.interlocking.handle_ip_formspec_fields(pname, pos, fields) - advtrains.interlocking.handle_dst_formspec_fields(pname, pos, fields) - end -end) - --- inits the signal IP assignment process -function signal.init_ip_assign(pos, pname) - if not minetest.check_player_privs(pname, "interlocking") then - minetest.chat_send_player(pname, "Insufficient privileges to use this!") - return - end - --remove old IP - --advtrains.interlocking.db.clear_ip_by_signalpos(pos) - minetest.chat_send_player(pname, "Configuring Signal: Please look in train's driving direction and punch rail to set influence point.") - - players_assign_ip[pname] = pos -end - -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 - -- IP assignment - local signalpos = players_assign_ip[pname] - if signalpos then - if vector.distance(pos, signalpos)<=50 then - local node_ok, conns, rhe = advtrains.get_rail_info_at(pos, advtrains.all_tracktypes) - if node_ok and #conns == 2 then - - local yaw = player:get_look_horizontal() - local plconnid = advtrains.yawToClosestConn(yaw, conns) - - -- add assignment if not already present. - local pts = advtrains.roundfloorpts(pos) - if not advtrains.interlocking.db.get_ip_signal_asp(pts, plconnid) then - advtrains.interlocking.db.set_ip_signal(pts, plconnid, signalpos) - ipmarker(pos, plconnid) - minetest.chat_send_player(pname, "Configuring Signal: Successfully set influence point") - else - minetest.chat_send_player(pname, "Configuring Signal: Influence point of another signal is already present!") - end - else - minetest.chat_send_player(pname, "Configuring Signal: This is not a normal two-connection rail! Aborted.") - end - else - minetest.chat_send_player(pname, "Configuring Signal: Node is too far away. Aborted.") - end - players_assign_ip[pname] = nil - end -end) - - advtrains.interlocking.signal = signal diff --git a/advtrains_interlocking/signal_aspect_accessors.lua b/advtrains_interlocking/signal_aspect_accessors.lua deleted file mode 100644 index d91df31..0000000 --- a/advtrains_interlocking/signal_aspect_accessors.lua +++ /dev/null @@ -1,163 +0,0 @@ ---- Signal aspect accessors --- @module advtrains.interlocking - -local A = advtrains.interlocking.aspect -local D = advtrains.distant -local I = advtrains.interlocking -local N = advtrains.ndb -local pts = advtrains.roundfloorpts - -local get_aspect - -local supposed_aspects = {} - ---- Replace the signal aspect cache. --- @function load_supposed_aspects --- @param db The new database. -function I.load_supposed_aspects(tbl) - if tbl then - supposed_aspects = {} - for k, v in pairs(tbl) do - supposed_aspects[k] = A(v) - end - end -end - ---- Retrieve the signal aspect cache. --- @function save_supposed_aspects --- @return The current database in use. -function I.save_supposed_aspects() - local t = {} - for k, v in pairs(supposed_aspects) do - t[k] = v:plain(true) - end - return t -end - ---- Read the aspect of a signal strictly from cache. --- @param pos The position of the signal. --- @return[1] The aspect of the signal (if present in cache). --- @return[2] The nil constant (otherwise). -local function get_supposed_aspect(pos) - return supposed_aspects[pts(pos)] -end - ---- Update the signal aspect information in cache. --- @param pos The position of the signal. --- @param asp The new signal aspect -local function set_supposed_aspect(pos, asp) - supposed_aspects[pts(pos)] = asp -end - ---- Get the definition of a node. --- @param pos The position of the node. --- @return[1] The definition of the node (if present). --- @return[2] An empty table (otherwise). -local function get_ndef(pos) - local node = N.get_node(pos) - return (minetest.registered_nodes[node.name] or {}), node -end - ---- Get the aspects supported by a signal. --- @function signal_get_supported_aspects --- @param pos The position of the signal. --- @return[1] The table of supported aspects (if present). --- @return[2] The nil constant (otherwise). -local function get_supported_aspects(pos) - local ndef = get_ndef(pos) - if ndef.advtrains and ndef.advtrains.supported_aspects then - return ndef.advtrains.supported_aspects - end - return nil -end - ---- Adjust a new signal aspect to fit a signal. --- @param pos The position of the signal. --- @param asp The new signal aspect. --- @return The adjusted signal aspect. --- @return The information to pass to the `advtrains.set_aspect` field in the node definitions. -local function adjust_aspect(pos, asp) - local asp = A(asp) - - local mainpos = D.get_main(pos) - local nxtasp - if mainpos then - nxtasp = get_aspect(mainpos) - end - local suppasp = get_supported_aspects(pos) - if not suppasp then - return asp - end - return asp:adjust_distant(nxtasp, suppasp.dst_shift):to_group(suppasp.group) -end - ---- Get the aspect of a signal without accessing the cache. --- For most cases, `get_aspect` should be used instead. --- @function signal_get_real_aspect --- @param pos The position of the signal. --- @return[1] The signal aspect adjusted using `adjust_aspect` (if present). --- @return[2] The nil constant (otherwise). -local function get_real_aspect(pos) - local ndef, node = get_ndef(pos) - if ndef.advtrains and ndef.advtrains.get_aspect then - local asp = ndef.advtrains.get_aspect(pos, node) or I.DANGER - return adjust_aspect(pos, asp) - end - return nil -end - ---- Get the aspect of a signal. --- @function signal_get_aspect --- @param pos The position of the signal. --- @return[1] The aspect of the signal (if present). --- @return[2] The nil constant (otherwise). -get_aspect = function(pos) - local asp = get_supposed_aspect(pos) - if not asp then - asp = get_real_aspect(pos) - set_supposed_aspect(pos, asp) - end - return asp -end - ---- Set the aspect of a signal. --- @function signal_set_aspect --- @param pos The position of the signal. --- @param asp The new signal aspect. --- @param[opt=false] skipdst Whether to skip updating distant signals. -local function set_aspect(pos, asp, skipdst) - local node = N.get_node(pos) - local ndef = minetest.registered_nodes[node.name] - if ndef and ndef.advtrains and ndef.advtrains.set_aspect then - local oldasp = I.signal_get_aspect(pos) or DANGER - local newasp = adjust_aspect(pos, asp) - set_supposed_aspect(pos, newasp) - ndef.advtrains.set_aspect(pos, node, newasp) - I.signal_on_aspect_changed(pos) - local aspect_changed = oldasp ~= newasp - if (not skipdst) and aspect_changed then - D.update_main(pos) - end - end -end - ---- Remove a signal from cache. --- @function signal_clear_aspect --- @param pos The position of the signal. -local function clear_aspect(pos) - set_supposed_aspect(pos, nil) -end - ---- Readjust the aspect of a signal. --- @function signal_readjust_aspect --- @param pos The position of the signal. -local function readjust_aspect(pos) - set_aspect(pos, get_aspect(pos)) -end - -I.signal_get_supported_aspects = get_supported_aspects -I.signal_get_real_aspect = get_real_aspect -I.signal_get_aspect = get_aspect -I.signal_set_aspect = set_aspect -I.signal_clear_aspect = clear_aspect -I.signal_readjust_aspect = readjust_aspect diff --git a/advtrains_interlocking/signal_aspect_ui.lua b/advtrains_interlocking/signal_aspect_ui.lua index a81b7fe..e5d2003 100644 --- a/advtrains_interlocking/signal_aspect_ui.lua +++ b/advtrains_interlocking/signal_aspect_ui.lua @@ -1,262 +1,188 @@ local F = advtrains.formspec -local players_aspsel = {} -local function describe_main_aspect(spv) - if spv == 0 then - return attrans("Danger (halt)") - elseif spv == -1 then - return attrans("Continue at maximum speed") - elseif not spv then - return attrans("Continue with current speed limit") +function advtrains.interlocking.show_signal_form(pos, node, pname, aux_key) + local sigd = advtrains.interlocking.db.get_sigd_for_signal(pos) + if sigd and not aux_key then + advtrains.interlocking.show_signalling_form(sigd, pname) else - return attrans("Continue with the speed limit of @1", tostring(spv)) - end -end - -local function describe_shunt_aspect(shunt) - if shunt then - return attrans("Shunting allowed") - else - return attrans("No shunting") + if advtrains.interlocking.signal.get_signal_cap_level(pos) >= 2 then + advtrains.interlocking.show_ip_sa_form(pos, pname) + else + advtrains.interlocking.show_ip_form(pos, pname) + end end end -local function describe_distant_aspect(spv) - if spv == 0 then - return attrans("Expect to stop at the next signal") - elseif spv == -1 then - return attrans("Expect to continue at maximum speed") - elseif not spv then - return attrans("No distant signal information") - else - return attrans("Expect to continue with a speed limit of @1", tostring(spv)) - end +local players_assign_ip = {} +local players_assign_distant = {} + +local function ipmarker(ipos, connid) + local node_ok, conns, rhe = advtrains.get_rail_info_at(ipos, advtrains.all_tracktypes) + if not node_ok then return end + local yaw = advtrains.dir_to_angle(conns[connid].c) + + -- using tcbmarker here + local obj = minetest.add_entity(vector.add(ipos, {x=0, y=0.2, z=0}), "advtrains_interlocking:tcbmarker") + if not obj then return end + obj:set_yaw(yaw) + obj:set_properties({ + textures = { "at_il_signal_ip.png" }, + }) end -advtrains.interlocking.describe_main_aspect = describe_main_aspect -advtrains.interlocking.describe_shunt_aspect = describe_shunt_aspect -advtrains.interlocking.describe_distant_aspect = describe_distant_aspect - -local function dsel(p, q, x, y) - if p == nil then - if q then - return x - else - return y - end - elseif p then - return x +function advtrains.interlocking.make_ip_formspec_component(pos, x, y, w) + advtrains.interlocking.db.check_for_duplicate_ip(pos) + local pts, connid = advtrains.interlocking.db.get_ip_by_signalpos(pos) + if pts then + -- display marker + local ipos = minetest.string_to_pos(pts) + ipmarker(ipos, connid) + return table.concat { + F.S_label(x, y, "Influence point is set at @1.", string.format("%s/%s", pts, connid)), + F.S_button_exit(x, y+0.5, w/2-0.125, "ip_set", "Modify"), + F.S_button_exit(x+w/2+0.125, y+0.5, w/2-0.125, "ip_clear", "Clear"), + } else - return y + return table.concat { + F.S_label(x, y, "Influence point is not set."), + F.S_button_exit(x, y+0.5, w, "ip_set", "Set influence point"), + } end end -local function describe_supported_aspects(suppasp, isasp) - local t = {} - - local entries = {attrans("Use default value")} - local selid = 0 - local mainasps = suppasp.main - if type(mainasps) ~= "table" then - mainasps = {mainasps} - end - for idx, spv in ipairs(mainasps) do - if isasp and spv == rawget(isasp, "main") then - selid = idx - end - entries[idx+1] = describe_main_aspect(spv) - end - t.main = entries - t.main_current = selid+1 - t.main_string = tostring(isasp.main) - if t.main == nil then - t.main_string = "" +-- shows small formspec to set the signal influence point +-- only_notset: show only if it is not set yet (used by signal tcb assignment) +function advtrains.interlocking.show_ip_form(pos, pname, only_notset) + if not minetest.check_player_privs(pname, "interlocking") then + return end - - t.shunt = { - attrans("No shunting"), - attrans("Shunting allowed"), - attrans("Proceed as main"), + local ipform = advtrains.interlocking.make_ip_formspec_component(pos, 0.5, 0.5, 7) + local form = { + "formspec_version[4]", + "size[8,2.25]", + ipform, } - - t.shunt_current = dsel(suppasp.shunt, isasp.shunt, 2, 1) - if dsel(suppasp.proceed_as_main, isasp.proceed_as_main, t.shunt_current == 1) then - t.shunt_current = 3 - end - t.shunt_const = suppasp.shunt ~= nil - - if suppasp.group then - local gdef = advtrains.interlocking.aspect.get_group_definition(suppasp.group) - if gdef then - t.group = suppasp.group - t.groupdef = gdef - local entries = {} - local selid = 1 - for idx, name in ipairs(suppasp.name or {}) do - entries[idx] = gdef.aspects[name].label - if suppasp.group == isasp.group and name == isasp.name then - selid = idx - end - end - t.name = entries - t.name_current = selid - end + if not only_notset or not pts then + minetest.show_formspec(pname, "at_il_ipsaform_"..minetest.pos_to_string(pos), table.concat(form)) end - - return t end -advtrains.interlocking.describe_supported_aspects = describe_supported_aspects - -local function make_signal_aspect_selector(suppasp, purpose, isasp) - local t = describe_supported_aspects(suppasp, isasp) - local formmode = 1 - - local pos - if type(purpose) == "table" then - formmode = 2 - pos = purpose.pos +-- shows larger formspec to set the signal influence point, main aspect and distant signal pos +-- only_notset: show only if it is not set yet (used by signal tcb assignment) +function advtrains.interlocking.show_ip_sa_form(pos, pname) + if not minetest.check_player_privs(pname, "interlocking") then + return end - + local ipform = advtrains.interlocking.make_ip_formspec_component(pos, 0.5, 0.5, 7) + local ma, rpos = advtrains.interlocking.signal.get_aspect(pos) + local saform = F.S_button_exit(0, 2, 4, "sa_dst_assign", rpos and minetest.pos_to_string(rpos) or "") + .. F.S_button_exit(0, 3, 2, "sa_tmp_mainfree", "Main to free") .. F.S_button_exit(2, 3, 2, "sa_tmp_mainhalt", "Main to halt") local form = { "formspec_version[4]", - string.format("size[8,%f]", ({5.75, 10.75})[formmode]), - F.S_label(0.5, 0.5, "Select signal aspect"), + "size[8,4]", + ipform, + saform, } - local h0 = ({0, 1.5})[formmode] - form[#form+1] = F.S_label(0.5, 1.5+h0, "Main aspect") - form[#form+1] = F.S_label(0.5, 3+h0, "Shunt aspect") - form[#form+1] = F.S_button_exit(0.5, 4.5+h0, 7, "asp_save", "Save signal aspect") - if formmode == 1 then - form[#form+1] = F.label(0.5, 1, purpose) - form[#form+1] = F.field(0.5, 2, 7, "asp_mainval", "", t.main_string) - elseif formmode == 2 then - if t.group then - form[#form+1] = F.S_label(0.5, 1.5, "Signal aspect group: @1", t.groupdef.label) - form[#form+1] = F.dropdown(0.5, 2, 7, "asp_namesel", t.name, t.name_current, true) - else - form[#form+1] = F.S_label(0.5, 1.5, "This signal does not belong to a signal aspect group.") - form[#form+1] = F.S_label(0.5, 2, "You can not use a predefined signal aspect.") - end - form[#form+1] = F.S_label(0.5, 1, "Signal at @1", minetest.pos_to_string(pos)) - form[#form+1] = F.dropdown(0.5, 3.5, 7, "asp_mainsel", t.main, t.main_current, true) - form[#form+1] = advtrains.interlocking.make_ip_formspec_component(pos, 0.5, 7, 7) - form[#form+1] = advtrains.interlocking.make_short_dst_formspec_component(pos, 0.5, 8.5, 7) - end + minetest.show_formspec(pname, "at_il_ipsaform_"..minetest.pos_to_string(pos), table.concat(form)) +end - if formmode == 2 and t.shunt_const then - form[#form+1] = F.label(0.5, 3.5+h0, t.shunt[t.shunt_current]) - form[#form+1] = F.S_label(0.5, 4+h0, "The shunt aspect cannot be changed.") - else - form[#form+1] = F.dropdown(0.5, 3.5+h0, 7, "asp_shunt", t.shunt, t.shunt_current, true) +function advtrains.interlocking.handle_ip_sa_formspec_fields(pname, pos, fields) + if not (pos and minetest.check_player_privs(pname, {train_operator=true, interlocking=true})) then + return + end + if fields.ip_set then + advtrains.interlocking.init_ip_assign(pos, pname) + elseif fields.ip_clear then + advtrains.interlocking.db.clear_ip_by_signalpos(pos) + elseif fields.sa_dst_assign then + advtrains.interlocking.init_distant_assign(pos, pname) + elseif fields.sa_tmp_mainfree then + local ma, rpos = advtrains.interlocking.signal.get_aspect(pos) + advtrains.interlocking.signal.set_aspect(pos, "_free", -1, rpos) + elseif fields.sa_tmp_mainhalt then + local ma, rpos = advtrains.interlocking.signal.get_aspect(pos) + advtrains.interlocking.signal.set_aspect(pos, nil, nil, rpos) end - - return table.concat(form) end -function advtrains.interlocking.show_signal_aspect_selector(pname, p_suppasp, p_purpose, callback, isasp) - local suppasp = p_suppasp or { - main = {0, -1}, - dst = {false}, - shunt = false, - info = {}, - } - local purpose = p_purpose or "" +minetest.register_on_player_receive_fields(function(player, formname, fields) + local pname = player:get_player_name() + local pts = string.match(formname, "^at_il_ipsaform_([^_]+)$") local pos - if type(p_purpose) == "table" then - pos = p_purpose - purpose = {pname = pname, pos = pos} + if pts then + pos = minetest.string_to_pos(pts) end - - local form = make_signal_aspect_selector(suppasp, purpose, isasp) - if not form then - return + if pos then + advtrains.interlocking.handle_ip_sa_formspec_fields(pname, pos, fields) end +end) - local token = advtrains.random_id() - minetest.show_formspec(pname, "at_il_sigaspdia_"..token, form) - minetest.after(0, function() - players_aspsel[pname] = { - purpose = purpose, - suppasp = suppasp, - callback = callback, - token = token, - } - end) -end - -local function usebool(sup, val, free) - if sup == nil then - return val == free - else - return sup +-- inits the signal IP assignment process +function advtrains.interlocking.init_ip_assign(pos, pname) + if not minetest.check_player_privs(pname, "interlocking") then + minetest.chat_send_player(pname, "Insufficient privileges to use this!") + return end + --remove old IP + --advtrains.interlocking.db.clear_ip_by_signalpos(pos) + minetest.chat_send_player(pname, "Configuring Signal: Please look in train's driving direction and punch rail to set influence point.") + + players_assign_ip[pname] = pos end -local function get_aspect_from_formspec(suppasp, fields, psl) - local namei, group, name = tonumber(fields.asp_namesel), suppasp.group, nil - local gdef = advtrains.interlocking.aspect.get_group_definition(group) - if gdef then - local names = suppasp.name or {} - name = names[namei] or names[names] - else - group = nil - end - local maini = tonumber(fields.asp_mainsel) - local main = (suppasp.main or {})[(maini or 0)-1] - if not maini then - local mainval = fields.asp_mainval - if mainval == "-1" then - main = -1 - elseif mainval == "x" then - main = false - elseif string.match(mainval, "^%d+$") then - main = tonumber(mainval) - else - main = nil - end - elseif maini <= 1 then - main = nil - end - local shunti = tonumber(fields.asp_shunt) - local shunt = suppasp.shunt - if shunt == nil then - shunt = shunti == 2 - end - local proceed_as_main = suppasp.proceed_as_main - if proceed_as_main == nil then - proceed_as_main = shunti == 3 +-- inits the distant signal assignment process +function advtrains.interlocking.init_distant_assign(pos, pname) + if not minetest.check_player_privs(pname, "interlocking") then + minetest.chat_send_player(pname, "Insufficient privileges to use this!") + return end - return advtrains.interlocking.aspect { - main = main, - shunt = shunt, - proceed_as_main = proceed_as_main, - info = {}, - name = name, - group = group, - } + minetest.chat_send_player(pname, "Set distant signal: Punch the main signal to assign!") + + players_assign_distant[pname] = pos end -minetest.register_on_player_receive_fields(function(player, formname, fields) +minetest.register_on_punchnode(function(pos, node, player, pointed_thing) local pname = player:get_player_name() - local psl = players_aspsel[pname] - if psl then - if formname == "at_il_sigaspdia_"..psl.token then - local suppasp = psl.suppasp - if fields.asp_save then - local asp - asp = get_aspect_from_formspec(suppasp, fields, psl) - if asp then - psl.callback(pname, asp) + if not minetest.check_player_privs(pname, "interlocking") then + return + end + -- IP assignment + local signalpos = players_assign_ip[pname] + if signalpos then + if vector.distance(pos, signalpos)<=50 then + local node_ok, conns, rhe = advtrains.get_rail_info_at(pos, advtrains.all_tracktypes) + if node_ok and #conns == 2 then + + local yaw = player:get_look_horizontal() + local plconnid = advtrains.yawToClosestConn(yaw, conns) + + -- add assignment if not already present. + local pts = advtrains.roundfloorpts(pos) + if not advtrains.interlocking.db.get_ip_signal_asp(pts, plconnid) then + advtrains.interlocking.db.set_ip_signal(pts, plconnid, signalpos) + ipmarker(pos, plconnid) + minetest.chat_send_player(pname, "Configuring Signal: Successfully set influence point") + else + minetest.chat_send_player(pname, "Configuring Signal: Influence point of another signal is already present!") end - end - if type(psl.purpose) == "table" then - local pos = psl.purpose.pos - advtrains.interlocking.handle_ip_formspec_fields(pname, pos, fields) - advtrains.interlocking.handle_dst_formspec_fields(pname, pos, fields) + else + minetest.chat_send_player(pname, "Configuring Signal: This is not a normal two-connection rail! Aborted.") end else - players_aspsel[pname] = nil + minetest.chat_send_player(pname, "Configuring Signal: Node is too far away. Aborted.") end + players_assign_ip[pname] = nil + end + -- DST assignment + signalpos = players_assign_distant[pname] + if signalpos then + -- get current mainaspect + local ma, rpos = advtrains.interlocking.signal.get_aspect(signalpos) + -- if punched pos is valid signal then set it as the new remote, otherwise nil + local nrpos + if advtrains.interlocking.signal.get_signal_cap_level(pos) > 1 then nrpos = pos end + advtrains.interlocking.signal.set_aspect(signalpos, ma.name, ma.speed, nrpos) + players_assign_distant[pname] = nil end end) + diff --git a/advtrains_interlocking/tcb_ts_ui.lua b/advtrains_interlocking/tcb_ts_ui.lua index bfec648..caf22e3 100755 --- a/advtrains_interlocking/tcb_ts_ui.lua +++ b/advtrains_interlocking/tcb_ts_ui.lua @@ -186,7 +186,7 @@ minetest.register_on_punchnode(function(pos, node, player, pointed_thing) 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.set_aspect then + if ndef and ndef.advtrains and ndef.advtrains.apply_aspect then local tcbs = ildb.get_tcbs(sigd) if tcbs then tcbs.signal = pos @@ -464,7 +464,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) ts.route = nil for _, sigd in ipairs(ts.tc_breaks) do local tcbs = ildb.get_tcbs(sigd) - advtrains.interlocking.update_signal_aspect(tcbs) + advtrains.interlocking.signal.update_route_aspect(tcbs) end minetest.chat_send_player(pname, "Reset track section "..ts_id.."!") end @@ -642,7 +642,6 @@ function advtrains.interlocking.show_signalling_form(sigd, pname, sel_rte, calle form = form.."button[0.5,8;2.5,1;newroute;New Route]" form = form.."button[ 3,8;2.5,1;unassign;Unassign Signal]" form = form..string.format("checkbox[0.5,8.75;ars;Automatic routesetting;%s]", not tcbs.ars_disabled) - form = form..string.format("checkbox[0.5,9.25;dst;Distant signalling;%s]", not tcbs.nodst) end elseif sigd_equal(tcbs.route_origin, sigd) then -- something has gone wrong: tcbs.routeset should have been set... @@ -660,7 +659,7 @@ function advtrains.interlocking.show_signalling_form(sigd, pname, sel_rte, calle -- always a good idea to update the signal aspect if not called_from_form_update then -- FIX prevent a callback loop - advtrains.interlocking.update_signal_aspect(tcbs) + advtrains.interlocking.signal.update_route_aspect(tcbs) end end @@ -769,10 +768,6 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) if fields.ars then tcbs.ars_disabled = not minetest.is_yes(fields.ars) end - - if fields.dst then - tcbs.nodst = not minetest.is_yes(fields.dst) - end if fields.auto then tcbs.route_auto = true diff --git a/advtrains_signals_japan/init.lua b/advtrains_signals_japan/init.lua index eb39f85..d7cf035 100644 --- a/advtrains_signals_japan/init.lua +++ b/advtrains_signals_japan/init.lua @@ -374,10 +374,6 @@ for sigtype, sigdata in pairs { --sigdefs["rpt_"..sigtype] = process_signal(sigtype, sigdata, true) end -for k in pairs(sigdefs) do - advtrains.trackplacer.register_tracktype("advtrains_signals_japan:"..k) -end - for _, rtab in ipairs { {rot = "0", ici = true}, {rot = "30"}, @@ -455,7 +451,7 @@ for _, rtab in ipairs { can_dig = advtrains.interlocking.signal_can_dig, after_dig_node = advtrains.interlocking.signal_after_dig, }) - advtrains.trackplacer.add_worked("advtrains_signals_japan:"..sigtype, asp, "_"..rot) + --advtrains.trackplacer.add_worked("advtrains_signals_japan:"..sigtype, asp, "_"..rot) end end end diff --git a/advtrains_signals_ks/init.lua b/advtrains_signals_ks/init.lua index 67e0fec..abfb194 100755 --- a/advtrains_signals_ks/init.lua +++ b/advtrains_signals_ks/init.lua @@ -50,15 +50,18 @@ end local applyaspectf_main = function(rot) return function(pos, node, main_aspect, dst_aspect, dst_aspect_info) + if not main_aspect then + -- halt aspect, set red and don't do anything further + advtrains.ndb.swap_node(pos, {name="advtrains_signals_ks:hs_danger_"..rot, param2 = node.param2}) + setzs3v(pos, nil, rot) + return + end -- set zs3 signal to show speed according to main_aspect setzs3(pos, main_aspect.zs3, rot) -- select appropriate lamps based on mainaspect and dst if main_aspect.shunt then advtrains.ndb.swap_node(pos, {name="advtrains_signals_ks:hs_shunt_"..rot, param2 = node.param2}) setzs3v(pos, nil, rot) - elseif main_aspect.halt then - advtrains.ndb.swap_node(pos, {name="advtrains_signals_ks:hs_danger_"..rot, param2 = node.param2}) - setzs3v(pos, nil, rot) else if not dst_aspect_info or not dst_aspect_info.main @@ -128,7 +131,8 @@ local mainaspects_main = { local applyaspectf_ra = function(rot) -- we get here the full main_aspect table return function(pos, node, main_aspect, dst_aspect, dst_aspect_info) - if main_aspect.shunt then + if main_aspect then + -- any main aspect is fine, there's only one anyway advtrains.ndb.swap_node(pos, {name="advtrains_signals_ks:ra_shuntd_"..rot, param2 = node.param2}) else advtrains.ndb.swap_node(pos, {name="advtrains_signals_ks:ra_danger_"..rot, param2 = node.param2}) @@ -144,11 +148,6 @@ local mainaspects_shunt = { description = "Shunt", shunt = true, }, - { - name = "halt", - description = "Halt", - halt = true, - }, } for _, rtab in ipairs({ @@ -225,9 +224,9 @@ for _, rtab in ipairs({ apply_aspect = applyaspectf_main(rot), get_aspect_info = afunc, }, - on_rightclick = advtrains.interlocking.signal_rc_handler, - can_dig = advtrains.interlocking.signal_can_dig, - after_dig_node = advtrains.interlocking.signal_after_dig, + on_rightclick = advtrains.interlocking.signal.on_rightclick, + can_dig = advtrains.interlocking.signal.can_dig, + after_dig_node = advtrains.interlocking.signal.after_dig, }) -- rotatable by trackworker --TODO add rotation using trackworker -- cgit v1.2.3 From f52653209aecb2310c1fb9598391c86113296f27 Mon Sep 17 00:00:00 2001 From: orwell Date: Fri, 24 May 2024 00:00:12 +0200 Subject: Fix working of the legacy signals under new system --- advtrains/passive.lua | 6 ++ advtrains/signals.lua | 111 ++++++++++++--------------------- advtrains_interlocking/database.lua | 2 +- advtrains_interlocking/demosignals.lua | 97 ---------------------------- advtrains_interlocking/init.lua | 1 - advtrains_interlocking/signal_api.lua | 15 +++-- 6 files changed, 55 insertions(+), 177 deletions(-) delete mode 100644 advtrains_interlocking/demosignals.lua (limited to 'advtrains_interlocking/init.lua') diff --git a/advtrains/passive.lua b/advtrains/passive.lua index 37b79e4..aad309e 100644 --- a/advtrains/passive.lua +++ b/advtrains/passive.lua @@ -70,6 +70,12 @@ function advtrains.setstate(parpos, newstate, pnode) 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 diff --git a/advtrains/signals.lua b/advtrains/signals.lua index 58d28a5..0b874bf 100644 --- a/advtrains/signals.lua +++ b/advtrains/signals.lua @@ -5,13 +5,13 @@ local mrules_wallsignal = advtrains.meseconrules local function can_dig_func(pos) if advtrains.interlocking then - return advtrains.interlocking.signal_can_dig(pos) + return advtrains.interlocking.signal.can_dig(pos) end return true end local function after_dig_func(pos) if advtrains.interlocking then - return advtrains.interlocking.signal_after_dig(pos) + return advtrains.interlocking.signal.after_dig(pos) end return true end @@ -26,18 +26,20 @@ return { } end -local suppasp = { - main = {0, -1}, - dst = {false}, - shunt = nil, - proceed_as_main = true, - info = { - call_on = false, - dead_end = false, - w_speed = nil, - } +local main_aspects = { + { name = "free", description = "Free" } } +local function simple_apply_aspect(offname, onname) + return function(pos, node, main_aspect, rem_aspect, rem_aspinfo) + if main_aspect.halt then + advtrains.ndb.swap_node(pos, {name = offname, param2 = node.param2}) + else + advtrains.ndb.swap_node(pos, {name = onname, param2 = node.param2}) + end + end +end + for r,f in pairs({on={as="off", ls="green", als="red"}, off={as="on", ls="red", als="green"}}) do for rotid, rotation in ipairs({"", "_30", "_45", "_60"}) do @@ -71,7 +73,9 @@ for r,f in pairs({on={as="off", ls="green", als="red"}, off={as="on", ls="red", ["action_"..f.as] = function (pos, node) advtrains.ndb.swap_node(pos, {name = "advtrains:retrosignal_"..f.as..rotation, param2 = node.param2}, true) if advtrains.interlocking then - advtrains.interlocking.signal_on_aspect_changed(pos) + -- 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 end }}, @@ -85,23 +89,17 @@ for r,f in pairs({on={as="off", ls="green", als="red"}, off={as="on", ls="red", elseif advtrains.check_turnout_signal_protection(pos, player:get_player_name()) then advtrains.ndb.swap_node(pos, {name = "advtrains:retrosignal_"..f.as..rotation, param2 = node.param2}, true) if advtrains.interlocking then - advtrains.interlocking.signal_on_aspect_changed(pos) + -- 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 end end, - -- new signal API + -- very new signal API advtrains = { - set_aspect = function(pos, node, asp) - if asp.main ~= 0 then - advtrains.ndb.swap_node(pos, {name = "advtrains:retrosignal_on"..rotation, param2 = node.param2}, true) - else - advtrains.ndb.swap_node(pos, {name = "advtrains:retrosignal_off"..rotation, param2 = node.param2}, true) - end - end, - get_aspect = function(pos, node) - return aspect(r=="on") - end, - supported_aspects = suppasp, + main_aspects = main_aspects, + apply_aspect = simple_apply_aspect("advtrains:retrosignal_off"..rotation, "advtrains:retrosignal_on"..rotation), + get_aspect_info = function() return aspect(r=="on") end, }, can_dig = can_dig_func, after_dig_node = after_dig_func, @@ -136,7 +134,7 @@ for r,f in pairs({on={as="off", ls="green", als="red"}, off={as="on", ls="red", ["action_"..f.as] = function (pos, node) advtrains.setstate(pos, f.als, node) if advtrains.interlocking then - advtrains.interlocking.signal_on_aspect_changed(pos) + advtrains.interlocking.signal.notify_on_aspect_changed(pos) end end }}, @@ -149,30 +147,16 @@ for r,f in pairs({on={as="off", ls="green", als="red"}, off={as="on", ls="red", advtrains.interlocking.show_ip_form(pos, pname) elseif advtrains.check_turnout_signal_protection(pos, player:get_player_name()) then advtrains.setstate(pos, f.als, node) - if advtrains.interlocking then - advtrains.interlocking.signal_on_aspect_changed(pos) - end end end, - -- new signal API + -- very new signal API advtrains = { - set_aspect = function(pos, node, asp) - if asp.main ~= 0 then - advtrains.ndb.swap_node(pos, {name = "advtrains:signal_on"..rotation, param2 = node.param2}, true) - else - advtrains.ndb.swap_node(pos, {name = "advtrains:signal_off"..rotation, param2 = node.param2}, true) - end - end, - get_aspect = function(pos, node) - return aspect(r=="on") - end, - supported_aspects = suppasp, - getstate = f.ls, - setstate = function(pos, node, newstate) - if newstate == f.als then - advtrains.ndb.swap_node(pos, {name = "advtrains:signal_"..f.as..rotation, param2 = node.param2}, true) - end - end, + main_aspects = main_aspects, + apply_aspect = simple_apply_aspect("advtrains:signal_off"..rotation, "advtrains:signal_on"..rotation), + get_aspect_info = function() return aspect(r=="on") end, + node_state = f.ls, + node_state_map = { red = "advtrains:signal_off"..rotation, green = "advtrains:signal_on"..rotation}, + _is_passivenode_signal = true }, can_dig = can_dig_func, after_dig_node = after_dig_func, @@ -211,9 +195,6 @@ for r,f in pairs({on={as="off", ls="green", als="red"}, off={as="on", ls="red", rules = mrules_wallsignal, ["action_"..f.as] = function (pos, node) advtrains.setstate(pos, f.als, node) - if advtrains.interlocking then - advtrains.interlocking.signal_on_aspect_changed(pos) - end end }}, on_rightclick=function(pos, node, player) @@ -225,30 +206,16 @@ for r,f in pairs({on={as="off", ls="green", als="red"}, off={as="on", ls="red", advtrains.interlocking.show_ip_form(pos, pname) elseif advtrains.check_turnout_signal_protection(pos, player:get_player_name()) then advtrains.setstate(pos, f.als, node) - if advtrains.interlocking then - advtrains.interlocking.signal_on_aspect_changed(pos) - end end end, - -- new signal API + -- very new signal API advtrains = { - set_aspect = function(pos, node, asp) - if asp.main ~= 0 then - advtrains.ndb.swap_node(pos, {name = "advtrains:signal_wall_"..loc.."_on", param2 = node.param2}, true) - else - advtrains.ndb.swap_node(pos, {name = "advtrains:signal_wall_"..loc.."_off", param2 = node.param2}, true) - end - end, - get_aspect = function(pos, node) - return aspect(r=="on") - end, - supported_aspects = suppasp, - getstate = f.ls, - setstate = function(pos, node, newstate) - if newstate == f.als then - advtrains.ndb.swap_node(pos, {name = "advtrains:signal_wall_"..loc.."_"..f.as, param2 = node.param2}, true) - end - end, + main_aspects = main_aspects, + apply_aspect = simple_apply_aspect("advtrains:signal_wall_"..loc.."_off", "advtrains:signal_wall_"..loc.."_on"), + get_aspect_info = function() return aspect(r=="on") end, + node_state = f.ls, + node_state_map = { red = "advtrains:signal_wall_"..loc.."_off", green = "advtrains:signal_wall_"..loc.."_on" }, + _is_passivenode_signal = true }, can_dig = can_dig_func, after_dig_node = after_dig_func, diff --git a/advtrains_interlocking/database.lua b/advtrains_interlocking/database.lua index e2df547..49ca13d 100644 --- a/advtrains_interlocking/database.lua +++ b/advtrains_interlocking/database.lua @@ -1006,7 +1006,7 @@ end function ildb.get_ip_signal_asp(pts, connid) local p = ildb.get_ip_signal(pts, connid) if p then - local asp = advtrains.interlocking.signal.get_aspect(p) + local asp = advtrains.interlocking.signal.get_aspect_info(p) if not asp then atlog("Clearing orphaned signal influence point", pts, "/", connid) ildb.clear_ip_signal(pts, connid) diff --git a/advtrains_interlocking/demosignals.lua b/advtrains_interlocking/demosignals.lua deleted file mode 100644 index de6926a..0000000 --- a/advtrains_interlocking/demosignals.lua +++ /dev/null @@ -1,97 +0,0 @@ --- Demonstration signals --- Those can display the 3 main aspects of Ks signals - --- Note that the group value of advtrains_signal is 2, which means "step 2 of signal capabilities" --- advtrains_signal=1 is meant for signals that do not implement set_aspect. - - -local setaspect = function(pos, node, asp) - if asp.main == 0 then - advtrains.ndb.swap_node(pos, {name="advtrains_interlocking:ds_danger"}) - else - if asp.dst ~= 0 and asp.main == -1 then - advtrains.ndb.swap_node(pos, {name="advtrains_interlocking:ds_free"}) - else - advtrains.ndb.swap_node(pos, {name="advtrains_interlocking:ds_slow"}) - end - end - local meta = minetest.get_meta(pos) - if meta then - meta:set_string("infotext", minetest.serialize(asp)) - end -end - -local suppasp = { - main = {0, 6, -1}, - dst = {0, false}, - shunt = false, - proceed_as_main = true, - info = { - call_on = false, - dead_end = false, - w_speed = nil, - } -} - -minetest.register_node("advtrains_interlocking:ds_danger", { - description = "Demo signal at Danger", - tiles = {"at_il_signal_asp_danger.png"}, - groups = { - cracky = 3, - advtrains_signal = 2, - save_in_at_nodedb = 1, - }, - advtrains = { - set_aspect = setaspect, - supported_aspects = suppasp, - get_aspect = function(pos, node) - return advtrains.interlocking.DANGER - end, - }, - on_rightclick = advtrains.interlocking.signal_rc_handler, - can_dig = advtrains.interlocking.signal_can_dig, - after_destruct = advtrains.interlocking.signal_after_dig, -}) -minetest.register_node("advtrains_interlocking:ds_free", { - description = "Demo signal at Free", - tiles = {"at_il_signal_asp_free.png"}, - groups = { - cracky = 3, - advtrains_signal = 2, - save_in_at_nodedb = 1, - }, - advtrains = { - set_aspect = setaspect, - supported_aspects = suppasp, - get_aspect = function(pos, node) - return { - main = -1, - } - end, - }, - on_rightclick = advtrains.interlocking.signal_rc_handler, - can_dig = advtrains.interlocking.signal_can_dig, - after_destruct = advtrains.interlocking.signal_after_dig, -}) -minetest.register_node("advtrains_interlocking:ds_slow", { - description = "Demo signal at Slow", - tiles = {"at_il_signal_asp_slow.png"}, - groups = { - cracky = 3, - advtrains_signal = 2, - save_in_at_nodedb = 1, - }, - advtrains = { - set_aspect = setaspect, - supported_aspects = suppasp, - get_aspect = function(pos, node) - return { - main = 6, - } - end, - }, - on_rightclick = advtrains.interlocking.signal_rc_handler, - can_dig = advtrains.interlocking.signal_can_dig, - after_destruct = advtrains.interlocking.signal_after_dig, -}) - diff --git a/advtrains_interlocking/init.lua b/advtrains_interlocking/init.lua index c397aa6..a4ddbad 100644 --- a/advtrains_interlocking/init.lua +++ b/advtrains_interlocking/init.lua @@ -17,7 +17,6 @@ local modpath = minetest.get_modpath(minetest.get_current_modname()) .. DIR_DELI dofile(modpath.."database.lua") dofile(modpath.."signal_api.lua") dofile(modpath.."signal_aspect_ui.lua") -dofile(modpath.."demosignals.lua") dofile(modpath.."train_sections.lua") dofile(modpath.."route_prog.lua") dofile(modpath.."routesetting.lua") diff --git a/advtrains_interlocking/signal_api.lua b/advtrains_interlocking/signal_api.lua index 7826d30..6f52816 100644 --- a/advtrains_interlocking/signal_api.lua +++ b/advtrains_interlocking/signal_api.lua @@ -272,14 +272,14 @@ local function cache_mainaspects(ndefat) end function signal.get_aspect_internal(pos, aspt) - if not aspt then - -- oh, no main aspect, nevermind - return signal.MASP_HALT, nil, nil - end atdebug("get_aspect_internal",pos,aspt) -- look aspect in nodedef local node = advtrains.ndb.get_node_or_nil(pos) local ndef = node and minetest.registered_nodes[node.name] + if not aspt then + -- oh, no main aspect, nevermind + return signal.MASP_HALT, nil, node, ndef + end local ndefat = ndef and ndef.advtrains if ndefat and ndefat.apply_aspect then -- only if signal defines main aspect and its set in aspt @@ -318,7 +318,9 @@ function signal.get_aspect_info(pos) local masp, remote, node, ndef = signal.get_aspect_internal(pos, aspt) -- call into ndef if ndef.advtrains and ndef.advtrains.get_aspect_info then - return ndef.advtrains.get_aspect_info(pos, masp) + local ai = ndef.advtrains.get_aspect_info(pos, masp) + atdebug(pos,"aspectinfo",ai) + return ai end end @@ -333,11 +335,12 @@ function signal.reapply_aspect(pts) -- get aspt local aspt = signal.aspects[pts] atdebug("reapply_aspect",advtrains.decode_pos(pts),"aspt",aspt) + local pos = advtrains.decode_pos(pts) if not aspt then + signal.notify_trains(pos) return -- oop, nothing to do end -- resolve mainaspect table by name - local pos = advtrains.decode_pos(pts) local masp, remote, node, ndef = signal.get_aspect_internal(pos, aspt) -- if we have remote, resolve remote local rem_masp, rem_aspi -- cgit v1.2.3 From c145e5db7473a0baab6438d7c2ed9616948d8387 Mon Sep 17 00:00:00 2001 From: orwell Date: Sat, 22 Jun 2024 21:11:50 +0200 Subject: SmartRoute: Implement auto route search and first prototype --- advtrains/init.lua | 2 +- advtrains_interlocking/init.lua | 1 + advtrains_interlocking/route_ui.lua | 8 ++ advtrains_interlocking/signal_aspect_ui.lua | 1 + advtrains_interlocking/smartroute.lua | 149 ++++++++++++++++++++++++++++ advtrains_interlocking/tcb_ts_ui.lua | 3 + 6 files changed, 163 insertions(+), 1 deletion(-) create mode 100644 advtrains_interlocking/smartroute.lua (limited to 'advtrains_interlocking/init.lua') diff --git a/advtrains/init.lua b/advtrains/init.lua index 3bd8374..bca39df 100644 --- a/advtrains/init.lua +++ b/advtrains/init.lua @@ -33,7 +33,7 @@ advtrains = {trains={}, player_to_train_mapping={}} -- =======================Development/debugging settings===================== -- DO NOT USE FOR NORMAL OPERATION -local DUMP_DEBUG_SAVE = false +local DUMP_DEBUG_SAVE = true -- dump the save files in human-readable format into advtrains_DUMP local GENERATE_ATRICIFIAL_LAG = false diff --git a/advtrains_interlocking/init.lua b/advtrains_interlocking/init.lua index a4ddbad..5883ab3 100644 --- a/advtrains_interlocking/init.lua +++ b/advtrains_interlocking/init.lua @@ -22,6 +22,7 @@ dofile(modpath.."route_prog.lua") dofile(modpath.."routesetting.lua") dofile(modpath.."tcb_ts_ui.lua") dofile(modpath.."route_ui.lua") +dofile(modpath.."smartroute.lua") dofile(modpath.."tool.lua") dofile(modpath.."approach.lua") diff --git a/advtrains_interlocking/route_ui.lua b/advtrains_interlocking/route_ui.lua index 982c579..863fe11 100644 --- a/advtrains_interlocking/route_ui.lua +++ b/advtrains_interlocking/route_ui.lua @@ -86,6 +86,9 @@ function atil.show_route_edit_form(pname, sigd, routeid) form = form.."textlist[0.5,2;3,3.9;rtelog;"..table.concat(tab, ",").."]" form = form.."button[0.5,6;3,1;back;<<< Back to signal]" + if route.smartroute_generated then + form = form.."button[3.5,6;2,1;noautogen;Clr Autogen]" + end form = form.."button[5.5,6;3,1;delete;Delete Route]" --atdebug(route.ars) @@ -135,6 +138,11 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) advtrains.interlocking.show_signal_aspect_selector(pname, suppasp, route.name, callback, route.aspect or advtrains.interlocking.GENERIC_FREE) return end + + if fields.noautogen then + route.smartroute_generated = nil + end + if fields.delete then -- if something set the route in the meantime, make sure this doesn't break. atil.route.update_route(sigd, tcbs, nil, true) diff --git a/advtrains_interlocking/signal_aspect_ui.lua b/advtrains_interlocking/signal_aspect_ui.lua index 892c53b..39aab17 100644 --- a/advtrains_interlocking/signal_aspect_ui.lua +++ b/advtrains_interlocking/signal_aspect_ui.lua @@ -83,6 +83,7 @@ function advtrains.interlocking.show_ip_sa_form(pos, pname) -- Create Signal aspect formspec elements local ndef = advtrains.ndb.get_ndef(pos) if ndef and ndef.advtrains then + form[#form+1] = F.label(0.5, 2, "Signal Aspect:") -- main aspect list if ndef.advtrains.main_aspects then local entries = { "" } diff --git a/advtrains_interlocking/smartroute.lua b/advtrains_interlocking/smartroute.lua new file mode 100644 index 0000000..770c379 --- /dev/null +++ b/advtrains_interlocking/smartroute.lua @@ -0,0 +1,149 @@ +-- smartroute.lua +-- Implementation of the advtrains auto-route search + +local atil = advtrains.interlocking +local ildb = atil.db +local sr = {} + + +local function otherside(s) + if s==1 then return 2 else return 1 end +end + +--route search implementation +-- Note this is similar to recursively_find_routes in database.lua, there used for the rscache + +local function recursively_find_routes(s_pos, s_connid, searching_shunt, tcbseq, mark_pos, result_table, scan_limit) + --atdebug("Recursively restarting at ",s_pos, s_connid, "limit left", scan_limit) + local ti = advtrains.get_track_iterator(s_pos, s_connid, scan_limit, false) + local pos, connid, bconnid = ti:next_branch() + pos, connid, bconnid = ti:next_track()-- step once to get ahead of previous turnout + local last_pos + repeat + -- record position in mark_pos + local pts = advtrains.encode_pos(pos) + mark_pos[pts] = true + + local node = advtrains.ndb.get_node_or_nil(pos) + atdebug("(SmartRoute) Walk ",pos, "nodename", node.name, "entering at conn",bconnid) + local ndef = minetest.registered_nodes[node.name] + if ndef.advtrains and ndef.advtrains.node_state_map then + -- Stop, this is a switchable node. Find out which conns we can go at + atdebug("(SmartRoute) Found turnout ",pos, "nodename", node.name, "entering at conn",bconnid) + local out_conns = ildb.get_possible_out_connids(node.name, bconnid) + for oconnid, state in pairs(out_conns) do + --atdebug("Going in direction",oconnid,"state",state) + recursively_find_routes(pos, oconnid, searching_shunt, table.copy(tcbseq), table.copy(mark_pos), result_table, ti.limit) + end + return + end + --otherwise, this might be a tcb + local tcb = ildb.get_tcb(pos) + if tcb then + local fsigd = { p = pos, s = connid } + atdebug("(SmartRoute) Encounter TCB ",fsigd) + tcbseq[#tcbseq+1] = fsigd + -- check if this is a possible route endpoint + local tcbs = tcb[connid] + if tcbs.signal then + local ndef = advtrains.ndb.get_ndef(tcbs.signal) + if ndef and ndef.advtrains then + if ndef.advtrains.route_role == "main" or ndef.advtrains.route_role == "main_distant" + or ndef.advtrains.route_role == "end" or ndef.advtrains.route_role == "shunt" then + -- signal is suitable target + local is_mainsignal = ndef.advtrains.route_role ~= "shunt" + -- record the found route in the results + result_table[#result_table+1] = { + tcbseq = table.copy(tcbseq), + mark_pos = table.copy(mark_pos), + shunt_route = not is_mainsignal, + to_end_of_track = false, + name = tcbs.signal_name or atil.sigd_to_string(fsigd) + } + -- if this is a main signal and/or we are only searching shunt routes, stop the search here + if is_mainsignal or searching_shunt then + atdebug("(SmartRoute) Terminating here because it is main or only shunt routes searched") + return + end + end + end + end + end + -- Go forward + last_pos = pos + pos, connid, bconnid = ti:next_track() + until not pos -- this stops the loop when either the track end is reached or the limit is hit + --atdebug("recursively_find_routes: Reached track end or limit at", last_pos, ". This path is not saved, returning") +end + +local function build_route_from_foundroute(froute, name) + local route = { + name = froute.name, + use_rscache = true, + smartroute_generated = true, + } + for _, sigd in ipairs(froute.tcbseq) do + route[#route+1] = { next = sigd, locks = {} } + end + return route +end + +-- Maximum scan length for track iterator +local TS_MAX_SCAN = 1000 + +function sr.init(pname, sigd) + -- is start signal a shunt signal? + local is_startsignal_shunt = false + local tcbs = ildb.get_tcbs(sigd) + if tcbs.signal then + local ndef = advtrains.ndb.get_ndef(tcbs.signal) + if ndef and ndef.advtrains then + if ndef.advtrains.route_role == "shunt" then + is_startsignal_shunt = true + end + end + end + local result_table = {} + recursively_find_routes(sigd.p, sigd.s, is_startsignal_shunt, {}, {}, result_table, TS_MAX_SCAN) + + atdebug("Smartroute search finished:",result_table) + + -- Short-circuit logic right now for testing + -- go through and delete all routes that are autogenerated + local i = 1 + while i<=#tcbs.routes do + if tcbs.routes[i].smartroute_generated then + table.remove(tcbs.routes, i) + else + i=i+1 + end + end + -- just plainly create routes! + for idx, froute in ipairs(result_table) do + tcbs.routes[#tcbs.routes+1] = build_route_from_foundroute(froute) + end + atwarn("Smartroute done!") +end + + + +--[[ + player1 = { + origin = + found_routes = { + { tcbseq = {, , }, mark_pos = { table with keys being encoded_pos of rails constituting route }, to_end_of_track = false, shunt_route = false } + } + } +]]-- +local player_smartroute = {} + +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 + -- TODO +end) + + +advtrains.interlocking.smartroute = sr diff --git a/advtrains_interlocking/tcb_ts_ui.lua b/advtrains_interlocking/tcb_ts_ui.lua index bdb0a18..7f75bb9 100755 --- a/advtrains_interlocking/tcb_ts_ui.lua +++ b/advtrains_interlocking/tcb_ts_ui.lua @@ -616,6 +616,9 @@ function advtrains.interlocking.show_signalling_form(sigd, pname, sel_rte, calle local strtab = {} for idx, route in ipairs(tcbs.routes) do local clr = "" + if route.smartroute_generated then + clr = "#FFFF55" + end if route.ars then clr = "#FF5555" if route.ars.default then -- cgit v1.2.3 From fed637080a4dcfee889bdfa30ca4744018b92e00 Mon Sep 17 00:00:00 2001 From: orwell Date: Tue, 7 Jan 2025 01:18:23 +0100 Subject: Add facility to auto-name signals when they are assigned --- advtrains_interlocking/autonaming.lua | 69 +++++++++++++++++++++++++++++ advtrains_interlocking/init.lua | 1 + advtrains_interlocking/signal_aspect_ui.lua | 2 + advtrains_interlocking/tcb_ts_ui.lua | 6 +++ 4 files changed, 78 insertions(+) create mode 100644 advtrains_interlocking/autonaming.lua (limited to 'advtrains_interlocking/init.lua') diff --git a/advtrains_interlocking/autonaming.lua b/advtrains_interlocking/autonaming.lua new file mode 100644 index 0000000..f88ccbd --- /dev/null +++ b/advtrains_interlocking/autonaming.lua @@ -0,0 +1,69 @@ +-- autonaming.lua +-- Automatically set names of signals (and maybe track sections) based on numbering + + +local function find_highest_count(prefix) + local cnt = 0 + local pattern = "^"..prefix.."(%d+)$" + local alltcb = advtrains.interlocking.db.get_all_tcb() + for _,tcb in pairs(alltcb) do + for _,tcbs in pairs(tcb) do + if tcbs.signal_name then + local mnum = string.match(tcbs.signal_name, pattern) + if mnum then + local n = tonumber(mnum) + if n and n > cnt then + cnt = n + end + end + end + end + end + return cnt +end + +-- { pname = { prefix = "FOO", count = 7 } } +local player_prefix_info = {} + +function advtrains.interlocking.set_autoname_prefix(pname, prefix) + if prefix and #prefix>0 then + -- check that it is valid + if not string.match(prefix,"[A-Za-z_]+") then + return false, "Illegal prefix, only letters and _ allowed" + end + -- scan database for this prefix to find out the highest count + local count = find_highest_count(prefix) + player_prefix_info[pname] = { prefix = prefix, count = count} + return true, "Prefix set, next signal name will be: ".. advtrains.interlocking.get_next_autoname(pname, true) + else + player_prefix_info[pname] = nil + return true, "Prefix unset, signals are not auto-named for you!" + end +end + +function advtrains.interlocking.get_next_autoname(pname, no_increment) + local pi = player_prefix_info[pname] + if pi then + local name = pi.prefix..(pi.count+1) + if not no_increment then pi.count = pi.count+1 end + return name + else + return nil + end +end + +function advtrains.interlocking.add_autoname_to_tcbs(tcbs, pname) + if not tcbs or not pname then return end + if tcbs.signal_name then return end -- name already set + + tcbs.signal_name = advtrains.interlocking.get_next_autoname(pname) +end + +minetest.register_chatcommand("at_nameprefix", + { + params = "", + description = "Sets the current prefix for automatically naming interlocking components. Example: '/at_nameprefix TEST' - signals will be named TEST1, TEST2 and so on", + privs = {interlocking = true}, + func = advtrains.interlocking.set_autoname_prefix, +}) + diff --git a/advtrains_interlocking/init.lua b/advtrains_interlocking/init.lua index 5883ab3..c3b1119 100644 --- a/advtrains_interlocking/init.lua +++ b/advtrains_interlocking/init.lua @@ -23,6 +23,7 @@ dofile(modpath.."routesetting.lua") dofile(modpath.."tcb_ts_ui.lua") dofile(modpath.."route_ui.lua") dofile(modpath.."smartroute.lua") +dofile(modpath.."autonaming.lua") dofile(modpath.."tool.lua") dofile(modpath.."approach.lua") diff --git a/advtrains_interlocking/signal_aspect_ui.lua b/advtrains_interlocking/signal_aspect_ui.lua index 25abb2d..d67572c 100644 --- a/advtrains_interlocking/signal_aspect_ui.lua +++ b/advtrains_interlocking/signal_aspect_ui.lua @@ -201,6 +201,8 @@ local function try_auto_assign_to_tcb(signalpos, pos, connid, pname) -- go ahead and assign local sigd = { p=apos, s=aconnid } advtrains.interlocking.db.assign_signal_to_tcbs(signalpos, sigd) + -- use auto-naming + advtrains.interlocking.add_autoname_to_tcbs(tcb[aconnid], pname) minetest.chat_send_player(pname, "Assigned signal to the TCB at "..core.pos_to_string(apos)) advtrains.interlocking.show_tcb_marker(apos) advtrains.interlocking.show_signalling_form(sigd, pname) diff --git a/advtrains_interlocking/tcb_ts_ui.lua b/advtrains_interlocking/tcb_ts_ui.lua index edc6921..9abd4f7 100755 --- a/advtrains_interlocking/tcb_ts_ui.lua +++ b/advtrains_interlocking/tcb_ts_ui.lua @@ -192,6 +192,8 @@ minetest.register_on_punchnode(function(pos, node, player, pointed_thing) 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, "Configuring TCB: Successfully assigned signal.") advtrains.interlocking.show_ip_form(pos, pname, true) else @@ -265,6 +267,8 @@ function advtrains.interlocking.self_tcb_make_after_place_callback(fail_silently 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 @@ -350,6 +354,8 @@ function advtrains.interlocking.self_tcb_make_on_rightclick_callback(fail_silent 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 -- in any case open the signalling form nouw local control = player:get_player_control() -- cgit v1.2.3 From 49d177d82cb09d19e7a9f5731e316d1079546b28 Mon Sep 17 00:00:00 2001 From: orwell Date: Tue, 7 Jan 2025 23:29:53 +0100 Subject: Remove unused source files (parts of ywang's original distant signal implementation & old track registration) --- advtrains/init.lua | 1 - advtrains/oldtracks.lua | 751 ---------------------------------- advtrains/trackdb_legacy.lua | 27 -- advtrains_interlocking/aspect.lua | 296 -------------- advtrains_interlocking/distant_ui.lua | 141 ------- advtrains_interlocking/init.lua | 2 - 6 files changed, 1218 deletions(-) delete mode 100644 advtrains/oldtracks.lua delete mode 100644 advtrains/trackdb_legacy.lua delete mode 100644 advtrains_interlocking/aspect.lua delete mode 100644 advtrains_interlocking/distant_ui.lua (limited to 'advtrains_interlocking/init.lua') diff --git a/advtrains/init.lua b/advtrains/init.lua index 3918ab3..ad66200 100644 --- a/advtrains/init.lua +++ b/advtrains/init.lua @@ -225,7 +225,6 @@ dofile(advtrains.modpath.."/atc.lua") dofile(advtrains.modpath.."/wagons.lua") dofile(advtrains.modpath.."/protection.lua") -dofile(advtrains.modpath.."/trackdb_legacy.lua") dofile(advtrains.modpath.."/nodedb.lua") dofile(advtrains.modpath.."/couple.lua") diff --git a/advtrains/oldtracks.lua b/advtrains/oldtracks.lua deleted file mode 100644 index c415143..0000000 --- a/advtrains/oldtracks.lua +++ /dev/null @@ -1,751 +0,0 @@ ---advtrains by orwell96, see readme.txt - ---dev-time settings: ---EDIT HERE ---If the old non-model rails on straight tracks should be replaced by the new... ---false: no ---true: yes -advtrains.register_replacement_lbms=false - ---[[TracksDefinition -nodename_prefix -texture_prefix -description -common={} -straight={} -straight45={} -curve={} -curve45={} -lswitchst={} -lswitchst45={} -rswitchst={} -rswitchst45={} -lswitchcr={} -lswitchcr45={} -rswitchcr={} -rswitchcr45={} -vert1={ - --you'll probably want to override mesh here -} -vert2={ - --you'll probably want to override mesh here -} -]]-- -advtrains.all_tracktypes={} - ---definition preparation -local function conns(c1, c2, r1, r2) return {{c=c1, y=r1}, {c=c2, y=r2}} end -local function conns3(c1, c2, c3, r1, r2, r3) return {{c=c1, y=r1}, {c=c2, y=r2}, {c=c3, y=r3}} end - -advtrains.ap={} -advtrains.ap.t_30deg_flat={ - regstep=1, - variant={ - st={ - conns = conns(0,8), - desc = "straight", - tpdouble = true, - tpsingle = true, - trackworker = "cr", - }, - cr={ - conns = conns(0,7), - desc = "curve", - tpdouble = true, - trackworker = "swlst", - }, - swlst={ - conns = conns3(0,8,7), - desc = "left switch (straight)", - trackworker = "swrst", - switchalt = "cr", - switchmc = "on", - switchst = "st", - switchprefix = "swl", - }, - swlcr={ - conns = conns3(0,7,8), - desc = "left switch (curve)", - trackworker = "swrcr", - switchalt = "st", - switchmc = "off", - switchst = "cr", - switchprefix = "swl", - }, - swrst={ - conns = conns3(0,8,9), - desc = "right switch (straight)", - trackworker = "st", - switchalt = "cr", - switchmc = "on", - switchst = "st", - switchprefix = "swr", - }, - swrcr={ - conns = conns3(0,9,8), - desc = "right switch (curve)", - trackworker = "st", - switchalt = "st", - switchmc = "off", - switchst = "cr", - switchprefix = "swr", - }, - }, - regtp=true, - tpdefault="st", - trackworker={ - ["swrcr"]="st", - ["swrst"]="st", - ["cr"]="swlst", - ["swlcr"]="swrcr", - ["swlst"]="swrst", - }, - rotation={"", "_30", "_45", "_60"}, -} -advtrains.ap.t_yturnout={ - regstep=1, - variant={ - l={ - conns = conns3(0,7,9), - desc = "Y-turnout (left)", - switchalt = "r", - switchmc = "off", - switchst = "l", - switchprefix = "", - }, - r={ - conns = conns3(0,9,7), - desc = "Y-turnout (right)", - switchalt = "l", - switchmc = "on", - switchst = "r", - switchprefix = "", - } - }, - regtp=true, - tpdefault="l", - rotation={"", "_30", "_45", "_60"}, -} -advtrains.ap.t_s3way={ - regstep=1, - variant={ - l={ - conns = { {c=0}, {c=7}, {c=8}, {c=9}, {c=0} }, - desc = "3-way turnout (left)", - switchalt = "s", - switchst="l", - switchprefix = "", - }, - s={ - conns = { {c=0}, {c=8}, {c=7}, {c=9}, {c=0} }, - desc = "3-way turnout (straight)", - switchalt ="r", - switchst = "s", - switchprefix = "", - }, - r={ - conns = { {c=0}, {c=9}, {c=8}, {c=7}, {c=0} }, - desc = "3-way turnout (right)", - switchalt = "l", - switchst="r", - switchprefix = "", - } - }, - regtp=true, - tpdefault="l", - rotation={"", "_30", "_45", "_60"}, -} -advtrains.ap.t_30deg_slope={ - regstep=1, - variant={ - vst1={conns = conns(8,0,0,0.5), rail_y = 0.25, desc = "steep uphill 1/2", slope=true}, - vst2={conns = conns(8,0,0.5,1), rail_y = 0.75, desc = "steep uphill 2/2", slope=true}, - vst31={conns = conns(8,0,0,0.33), rail_y = 0.16, desc = "uphill 1/3", slope=true}, - vst32={conns = conns(8,0,0.33,0.66), rail_y = 0.5, desc = "uphill 2/3", slope=true}, - vst33={conns = conns(8,0,0.66,1), rail_y = 0.83, desc = "uphill 3/3", slope=true}, - }, - regsp=true, - slopeplacer={ - [2]={"vst1", "vst2"}, - [3]={"vst31", "vst32", "vst33"}, - max=3,--highest entry - }, - slopeplacer_45={ - [2]={"vst1_45", "vst2_45"}, - max=2, - }, - rotation={"", "_30", "_45", "_60"}, - trackworker={}, - increativeinv={}, -} -advtrains.ap.t_30deg_straightonly={ - regstep=1, - variant={ - st={ - conns = conns(0,8), - desc = "straight", - tpdouble = true, - tpsingle = true, - trackworker = "st", - }, - }, - regtp=true, - tpdefault="st", - rotation={"", "_30", "_45", "_60"}, -} -advtrains.ap.t_30deg_straightonly_noplacer={ - regstep=1, - variant={ - st={ - conns = conns(0,8), - desc = "straight", - tpdouble = true, - tpsingle = true, - trackworker = "st", - }, - }, - tpdefault="st", - rotation={"", "_30", "_45", "_60"}, -} -advtrains.ap.t_45deg={ - regstep=2, - variant={ - st={ - conns = conns(0,8), - desc = "straight", - tpdouble = true, - tpsingle = true, - trackworker = "cr", - }, - cr={ - conns = conns(0,6), - desc = "curve", - tpdouble = true, - trackworker = "swlst", - }, - swlst={ - conns = conns3(0,8,6), - desc = "left switch (straight)", - trackworker = "swrst", - switchalt = "cr", - switchmc = "on", - switchst = "st", - }, - swlcr={ - conns = conns3(0,6,8), - desc = "left switch (curve)", - trackworker = "swrcr", - switchalt = "st", - switchmc = "off", - switchst = "cr", - }, - swrst={ - conns = conns3(0,8,10), - desc = "right switch (straight)", - trackworker = "st", - switchalt = "cr", - switchmc = "on", - switchst = "st", - }, - swrcr={ - conns = conns3(0,10,8), - desc = "right switch (curve)", - trackworker = "st", - switchalt = "st", - switchmc = "off", - switchst = "cr", - }, - }, - regtp=true, - tpdefault="st", - trackworker={ - ["swrcr"]="st", - ["swrst"]="st", - ["cr"]="swlst", - ["swlcr"]="swrcr", - ["swlst"]="swrst", - }, - rotation={"", "_30", "_45", "_60"}, -} -advtrains.ap.t_perpcrossing={ - regstep = 1, - variant={ - st={ - conns = { {c=0}, {c=8}, {c=4}, {c=12} }, - desc = "perpendicular crossing", - tpdouble = true, - tpsingle = true, - trackworker = "st", - }, - }, - regtp=true, - tpdefault="st", - rotation={"", "_30", "_45", "_60"}, -} -advtrains.ap.t_90plusx_crossing={ - regstep = 1, - variant={ - ["30l"]={ - conns = { {c=0}, {c=8}, {c=1}, {c=9} }, - desc = "30/90 degree crossing (left)", - tpdouble = true, - tpsingle = true, - trackworker = "45l" - }, - ["45l"]={ - conns = { {c=0}, {c=8}, {c=2}, {c=10} }, - desc = "45/90 degree crossing (left)", - tpdouble = true, - tpsingle = true, - trackworker = "60l", - }, - ["60l"]={ - conns = { {c=0}, {c=8}, {c=3}, {c=11}}, - desc = "60/90 degree crossing (left)", - tpdouble = true, - tpsingle = true, - trackworker = "60r", - }, - ["60r"]={ - conns = { {c=0}, {c=8}, {c=5}, {c=13} }, - desc = "60/90 degree crossing (right)", - tpdouble = true, - tpsingle = true, - trackworker = "45r" - }, - ["45r"]={ - conns = { {c=0}, {c=8}, {c=6}, {c=14} }, - desc = "45/90 degree crossing (right)", - tpdouble = true, - tpsingle = true, - trackworker = "30r", - }, - ["30r"]={ - conns = { {c=0}, {c=8}, {c=7}, {c=15}}, - desc = "30/90 degree crossing (right)", - tpdouble = true, - tpsingle = true, - trackworker = "30l", - }, - }, - regtp=true, - tpdefault="30l", - rotation={""}, - trackworker = { - ["30l"] = "45l", - ["45l"] = "60l", - ["60l"] = "60r", - ["60r"] = "45r", - ["45r"] = "30r", - ["30r"] = "30l", - } -} - -advtrains.ap.t_diagonalcrossing = { - regstep=1, - variant={ - ["30l45r"]={ - conns = {{c=1}, {c=9}, {c=6}, {c=14}}, - desc = "30left-45right diagonal crossing", - tpdouble=true, - tpsingle=true, - trackworker="60l30l", - }, - ["60l30l"]={ - conns = {{c=3}, {c=11}, {c=1}, {c=9}}, - desc = "30left-60right diagonal crossing", - tpdouble=true, - tpsingle=true, - trackworker="60l45r" - }, - ["60l45r"]={ - conns = {{c=3}, {c=11}, {c=6}, {c=14}}, - desc = "60left-45right diagonal crossing", - tpdouble=true, - tpsingle=true, - trackworker="60l60r" - }, - ["60l60r"]={ - conns = {{c=3}, {c=11}, {c=5}, {c=13}}, - desc = "60left-60right diagonal crossing", - tpdouble=true, - tpsingle=true, - trackworker="60r45l", - }, - --If 60l60r had a mirror image, it would be here, but it's symmetric. - -- 60l60r is also equivalent to 30l30r but rotated 90 degrees. - ["60r45l"]={ - conns = {{c=5}, {c=13}, {c=2}, {c=10}}, - desc = "60right-45left diagonal crossing", - tpdouble=true, - tpsingle=true, - trackworker="60r30r", - }, - ["60r30r"]={ - conns = {{c=5}, {c=13}, {c=7}, {c=15}}, - desc = "60right-30right diagonal crossing", - tpdouble=true, - tpsingle=true, - trackworker="30r45l", - }, - ["30r45l"]={ - conns = {{c=7}, {c=15}, {c=2}, {c=10}}, - desc = "30right-45left diagonal crossing", - tpdouble=true, - tpsingle=true, - trackworker="30l45r", - }, - - }, - regtp=true, - tpdefault="30l45r", - rotation={""}, - trackworker = { - ["30l45r"] = "60l30l", - ["60l30l"] = "60l45r", - ["60l45r"] = "60l60r", - ["60l60r"] = "60r45l", - ["60r45l"] = "60r30r", - ["60r30r"] = "30r45l", - ["30r45l"] = "30l45r", - } -} - -advtrains.trackpresets = advtrains.ap - ---definition format: ([] optional) ---[[{ - nodename_prefix - texture_prefix - [shared_texture] - models_prefix - models_suffix (with dot) - [shared_model] - formats={ - st,cr,swlst,swlcr,swrst,swrcr,vst1,vst2 - (each a table with indices 0-3, for if to register a rail with this 'rotation' table entry. nil is assumed as 'all', set {} to not register at all) - } - common={} change something on common rail appearance -} -[18.12.17] Note on new connection system: -In order to support real rail crossing nodes and finally make the trackplacer respect switches, I changed the connection system. -There can be a variable number of connections available. These are specified as tuples {c=, y=} -The table "at_conns" consists of {, ...} -the "at_rail_y" property holds the value that was previously called "railheight" -Depending on the number of connections: -2 conns: regular rail -3 conns: switch: - - when train passes in at conn1, will move out of conn2 - - when train passes in at conn2 or conn3, will move out of conn1 -4 conns: cross (or cross switch, depending on arrangement of conns): - - conn1 <> conn2 - - conn3 <> conn4 -]] - --- Notify the user if digging the rail is not allowed -local function can_dig_callback(pos, player) - local ok, reason = advtrains.can_dig_or_modify_track(pos) - if not ok and player then - minetest.chat_send_player(player:get_player_name(), attrans("This track can not be removed!") .. " " .. reason) - end - return ok -end - -function advtrains.register_tracks(tracktype, def, preset) - advtrains.trackplacer.register_tracktype(def.nodename_prefix, preset.tpdefault) - if preset.regtp then - advtrains.trackplacer.register_track_placer(def.nodename_prefix, def.texture_prefix, def.description, def) - end - if preset.regsp then - advtrains.slope.register_placer(def, preset) - end - for suffix, var in pairs(preset.variant) do - for rotid, rotation in ipairs(preset.rotation) do - if not def.formats[suffix] or def.formats[suffix][rotid] then - local img_suffix = suffix..rotation - local ndef = advtrains.merge_tables({ - description=def.description.."("..(var.desc or "any")..rotation..")", - drawtype = "mesh", - paramtype="light", - paramtype2="facedir", - walkable = false, - selection_box = { - type = "fixed", - fixed = {-1/2-1/16, -1/2, -1/2, 1/2+1/16, -1/2+2/16, 1/2}, - }, - - mesh = def.shared_model or (def.models_prefix.."_"..img_suffix..def.models_suffix), - tiles = {def.shared_texture or (def.texture_prefix.."_"..img_suffix..".png"), def.second_texture}, - - groups = { - attached_node = advtrains.IGNORE_WORLD and 0 or 1, - advtrains_track=1, - ["advtrains_track_"..tracktype]=1, - save_in_at_nodedb=1, - dig_immediate=2, - not_in_creative_inventory=1, - not_blocking_trains=1, - }, - - can_dig = can_dig_callback, - after_dig_node=function(pos) - advtrains.ndb.update(pos) - end, - after_place_node=function(pos) - advtrains.ndb.update(pos) - end, - at_nnpref = def.nodename_prefix, - at_suffix = suffix, - at_rotation = rotation, - at_rail_y = var.rail_y - }, def.common or {}) - - if preset.regtp then - ndef.drop = def.nodename_prefix.."_placer" - end - if preset.regsp and var.slope then - ndef.drop = def.nodename_prefix.."_slopeplacer" - end - - --connections - ndef.at_conns = advtrains.rotate_conn_by(var.conns, (rotid-1)*preset.regstep) - - local ndef_avt_table - - if var.switchalt and var.switchst then - local switchfunc=function(pos, node, newstate) - newstate = newstate or var.switchalt -- support for 3 (or more) state switches - -- this code is only called from the internal setstate function, which - -- ensures that it is safe to switch the turnout - if newstate~=var.switchst then - advtrains.ndb.swap_node(pos, {name=def.nodename_prefix.."_"..(var.switchprefix or "")..newstate..rotation, param2=node.param2}) - advtrains.invalidate_all_paths(pos) - end - end - ndef.on_rightclick = function(pos, node, player) - if advtrains.check_turnout_signal_protection(pos, player:get_player_name()) then - advtrains.setstate(pos, nil, node) - advtrains.log("Switch", player:get_player_name(), pos) - end - end - if var.switchmc then - ndef.mesecons = {effector = { - ["action_"..var.switchmc] = function(pos, node) - advtrains.setstate(pos, nil, node) - end, - rules=advtrains.meseconrules - }} - end - ndef_avt_table = { - getstate = var.switchst, - setstate = switchfunc, - } - end - - local adef={} - if def.get_additional_definiton then - adef=def.get_additional_definiton(def, preset, suffix, rotation) - end - ndef = advtrains.merge_tables(ndef, adef) - - -- insert getstate/setstate functions after merging the additional definitions - if ndef_avt_table then - ndef.advtrains = advtrains.merge_tables(ndef.advtrains or {}, ndef_avt_table) - end - - minetest.register_node(":"..def.nodename_prefix.."_"..suffix..rotation, ndef) - --trackplacer - if preset.regtp then - local tpconns = {conn1=ndef.at_conns[1].c, conn2=ndef.at_conns[2].c} - if var.tpdouble then - advtrains.trackplacer.add_double_conn(def.nodename_prefix, suffix, rotation, tpconns) - end - if var.tpsingle then - advtrains.trackplacer.add_single_conn(def.nodename_prefix, suffix, rotation, tpconns) - end - end - advtrains.trackplacer.add_worked(def.nodename_prefix, suffix, rotation, var.trackworker) - end - end - end - advtrains.all_tracktypes[tracktype]=true -end - -function advtrains.is_track_and_drives_on(nodename, drives_on_p) - local drives_on = drives_on_p - if not drives_on then drives_on = advtrains.all_tracktypes end - local hasentry = false - for _,_ in pairs(drives_on) do - hasentry=true - end - if not hasentry then drives_on = advtrains.all_tracktypes end - - if not minetest.registered_nodes[nodename] then - return false - end - local nodedef=minetest.registered_nodes[nodename] - for k,v in pairs(drives_on) do - if nodedef.groups["advtrains_track_"..k] then - return true - end - end - return false -end - -function advtrains.get_track_connections(name, param2) - local nodedef=minetest.registered_nodes[name] - if not nodedef then atprint(" get_track_connections couldn't find nodedef for nodename "..(name or "nil")) return nil end - local noderot=param2 - if not param2 then noderot=0 end - if noderot > 3 then atprint(" get_track_connections: rail has invaild param2 of "..noderot) noderot=0 end - - local tracktype - for k,_ in pairs(nodedef.groups) do - local tt=string.match(k, "^advtrains_track_(.+)$") - if tt then - tracktype=tt - end - end - return advtrains.rotate_conn_by(nodedef.at_conns, noderot*AT_CMAX/4), (nodedef.at_rail_y or 0), tracktype -end - --- Function called when a track is about to be dug or modified by the trackworker --- Returns either true (ok) or false,"translated string describing reason why it isn't allowed" -function advtrains.can_dig_or_modify_track(pos) - if advtrains.get_train_at_pos(pos) then - return false, attrans("Position is occupied by a train.") - end - -- interlocking: tcb, signal IP a.s.o. - if advtrains.interlocking then - -- TCB? - if advtrains.interlocking.db.get_tcb(pos) then - return false, attrans("There's a Track Circuit Break here.") - end - -- signal ip? - if advtrains.interlocking.db.is_ip_at(pos, true) then -- is_ip_at with purge parameter - return false, attrans("There's a Signal Influence Point here.") - end - end - return true -end - --- slope placer. Defined in register_tracks. ---crafted with rail and gravel -local sl={} -function sl.register_placer(def, preset) - minetest.register_craftitem(":"..def.nodename_prefix.."_slopeplacer",{ - description = attrans("@1 Slope", def.description), - inventory_image = def.texture_prefix.."_slopeplacer.png", - wield_image = def.texture_prefix.."_slopeplacer.png", - groups={}, - on_place = sl.create_slopeplacer_on_place(def, preset) - }) -end ---(itemstack, placer, pointed_thing) -function sl.create_slopeplacer_on_place(def, preset) - return function(istack, player, pt) - if not pt.type=="node" then - minetest.chat_send_player(player:get_player_name(), attrans("Can't place: not pointing at node")) - return istack - end - local pos=pt.above - if not pos then - minetest.chat_send_player(player:get_player_name(), attrans("Can't place: not pointing at node")) - return istack - end - local node=minetest.get_node(pos) - if not minetest.registered_nodes[node.name] or not minetest.registered_nodes[node.name].buildable_to then - minetest.chat_send_player(player:get_player_name(), attrans("Can't place: space occupied!")) - return istack - end - if not advtrains.check_track_protection(pos, player:get_player_name()) then - minetest.record_protection_violation(pos, player:get_player_name()) - return istack - end - --determine player orientation (only horizontal component) - --get_look_horizontal may not be available - local yaw=player.get_look_horizontal and player:get_look_horizontal() or (player:get_look_yaw() - math.pi/2) - - --rounding unit vectors is a nice way for selecting 1 of 8 directions since sin(30°) is 0.5. - local dirvec={x=math.floor(math.sin(-yaw)+0.5), y=0, z=math.floor(math.cos(-yaw)+0.5)} - --translate to direction to look up inside the preset table - local param2, rot45=({ - [-1]={ - [-1]=2, - [0]=3, - [1]=3, - }, - [0]={ - [-1]=2, - [1]=0, - }, - [1]={ - [-1]=1, - [0]=1, - [1]=0, - }, - })[dirvec.x][dirvec.z], dirvec.x~=0 and dirvec.z~=0 - local lookup=preset.slopeplacer - if rot45 then lookup=preset.slopeplacer_45 end - - --go unitvector forward and look how far the next node is - local step=1 - while step<=lookup.max do - local node=minetest.get_node(vector.add(pos, dirvec)) - --next node solid? - if not minetest.registered_nodes[node.name] or not minetest.registered_nodes[node.name].buildable_to or advtrains.is_protected(pos, player:get_player_name()) then - --do slopes of this distance exist? - if lookup[step] then - if minetest.settings:get_bool("creative_mode") or istack:get_count()>=step then - --start placing - local placenodes=lookup[step] - while step>0 do - minetest.set_node(pos, {name=def.nodename_prefix.."_"..placenodes[step], param2=param2}) - if not minetest.settings:get_bool("creative_mode") then - istack:take_item() - end - step=step-1 - pos=vector.subtract(pos, dirvec) - end - else - minetest.chat_send_player(player:get_player_name(), attrans("Can't place: Not enough slope items left (@1 required)", step)) - end - else - minetest.chat_send_player(player:get_player_name(), attrans("Can't place: There's no slope of length @1",step)) - end - return istack - end - step=step+1 - pos=vector.add(pos, dirvec) - end - minetest.chat_send_player(player:get_player_name(), attrans("Can't place: no supporting node at upper end.")) - return itemstack - end -end - -advtrains.slope=sl - ---END code, BEGIN definition ---definition format: ([] optional) ---[[{ - nodename_prefix - texture_prefix - [shared_texture] - models_prefix - models_suffix (with dot) - [shared_model] - formats={ - st,cr,swlst,swlcr,swrst,swrcr,vst1,vst2 - (each a table with indices 0-3, for if to register a rail with this 'rotation' table entry. nil is assumed as 'all', set {} to not register at all) - } - common={} change something on common rail appearance -}]] - - - - - - - - - diff --git a/advtrains/trackdb_legacy.lua b/advtrains/trackdb_legacy.lua deleted file mode 100644 index 99349e8..0000000 --- a/advtrains/trackdb_legacy.lua +++ /dev/null @@ -1,27 +0,0 @@ ---trackdb_legacy.lua ---loads the (old) track database. the only use for this is to provide data for rails that haven't been written into the ndb database. ---nothing will be saved. ---if the user thinks that he has loaded every track in his world at least once, he can delete the track database. - ---trackdb[[y][x][z]={conn1, conn2, rely1, rely2, railheight} - - ---trackdb keeps its own save file. -advtrains.fpath_tdb=minetest.get_worldpath().."/advtrains_trackdb2" -local file, err = io.open(advtrains.fpath_tdb, "r") -if not file then - atprint("Not loading a trackdb file.") -else - local tbl = minetest.deserialize(file:read("*a")) - if type(tbl) == "table" then - advtrains.trackdb=tbl - atprint("Loaded trackdb file.") - end - file:close() -end - - - - - - diff --git a/advtrains_interlocking/aspect.lua b/advtrains_interlocking/aspect.lua deleted file mode 100644 index c7d5c81..0000000 --- a/advtrains_interlocking/aspect.lua +++ /dev/null @@ -1,296 +0,0 @@ ---- Signal aspect handling. --- @module advtrains.interlocking.aspect - -local registered_groups = {} - -local default_aspect = { - main = false, - dst = false, - shunt = true, - proceed_as_main = false, -} - -local signal_aspect = {} - -local signal_aspect_metatable = { - __eq = function(asp1, asp2) - for _, k in pairs {"main", "dst", "shunt", "proceed_as_main"} do - local v1, v2 = (asp1[k] or false), (asp2[k] or false) - if v1 ~= v2 then - return false - end - end - if asp1.group and asp1.group == asp2.group then - return asp1.name == asp2.name - end - return true - end, - __index = function(asp, field) - local val = signal_aspect[field] - if val then - return val - end - val = default_aspect[field] - if val == nil then - return nil - end - local group = registered_groups[rawget(asp, "group")] - if group then - local aspdef = group.aspects[rawget(asp, "name")] - if aspdef[field] ~= nil then - val = aspdef[field] - end - end - return val - end, - __tostring = function(asp) - local st = {} - if asp.group and asp.name then - table.insert(st, ("%q in %q"):format(asp.name, asp.group)) - end - if asp.main then - table.insert(st, ("current %d"):format(asp.main)) - end - if asp.main ~= 0 then - if asp.dst then - table.insert(st, string.format("next %d", asp.dst)) - end - end - if asp.main ~= 0 and asp.proceed_as_main then - table.insert(st, "proceed as main") - end - return ("[%s]"):format(table.concat(st, ", ")) - end, -} - -local function quicknew(t) - return setmetatable(t, signal_aspect_metatable) -end - ---- Signal aspect class. --- @type signal_aspect - ---- Return a plain version of the signal aspect. --- @param[opt=false] raw Bypass metamethods when fetching signal aspects --- @return A plain copy of the signal aspect object. -function signal_aspect:plain(raw) - local t = {} - for _, k in pairs {"main", "dst", "shunt", "proceed_as_main", "group", "name"} do - local v - if raw then - v = rawget(self, k) - else - v = self[k] - end - t[k] = v - end - return t -end - ---- Create (or copy) a signal aspect object. --- Note that signal aspect objects can also be created by calling the `advtrains.interlocking.aspect` table. --- @return The newly created signal aspect object. -function signal_aspect:new() - if type(self) ~= "table" then - return quicknew{} - end - local newasp = {} - for _, k in pairs {"main", "dst"} do - if type(self[k]) == "table" then - if self[k].free then - newasp[k] = self[k].speed - else - newasp[k] = 0 - end - else - newasp[k] = self[k] - end - end - if type(self.shunt) == "table" then - newasp.shunt = self.shunt.free - newasp.proceed_as_main = self.shunt.proceed_as_main - else - newasp.shunt = self.shunt - end - for _, k in pairs {"group", "name"} do - newasp[k] = self[k] - end - return quicknew(newasp) -end - ---- Modify the signal aspect in-place to fit in the specific signal group. --- @param group The signal group. The `nil` indicates a generic group. --- @return The (now modified) signal aspect itself. -function signal_aspect:to_group(group) - local cg = self.group - local gdef = registered_groups[group] - if type(self.name) ~= "string" then - self.name = nil - end - if not gdef then - for k in pairs(default_aspect) do - rawset(self, k, self[k]) - end - self.group = nil - self.name = nil - return self - elseif cg == group and gdef.aspects[self.name] then - return self - end - local newidx = 1 - if self.main == 0 then - newidx = #gdef.aspects - end - local cgdef = registered_groups[cg] - if cgdef then - local idx = (cgdef.aspects[self.name] or {}).index - if idx then - if idx >= #cgdef.aspects then - idx = #gdef.aspects - elseif idx >= #gdef.aspects then - idx = #gdef.aspects-1 - end - newidx = idx - end - end - self.group = group - self.name = gdef.aspects[newidx][1] - return self -end - ---- Modify the signal aspect in-place to indicate a specific distant aspect. --- @param dst The distant aspect --- @param[opt=1] shift The phase shift of the current signal. --- @return The (now modified) signal aspect itself. -function signal_aspect:adjust_distant(dst, shift) - if (shift or -1) < 0 then - shift = 1 - end - if not dst then - self.dst = nil - return self - end - if self.main ~= 0 then - self.dst = dst.main - else - self.dst = nil - return self - end - local dgdef = registered_groups[dst.group] - if dgdef then - if self.group == dst.group and shift == 0 then - self.name = dst.name - else - local idx = (dgdef.aspects[dst.name] or {}).index - if idx then - idx = math.max(idx-shift, 1) - self.group = dst.group - self.name = dgdef.aspects[idx][1] - end - end - end - return self -end - ---- Signal groups. --- @section signal_group - ---- Register a signal group. --- @function register_group --- @param def The definition table. -local function register_group(def) - local t = {} - local name = def.name - if type(name) ~= "string" then - return error("Expected signal group name to be a string, got " .. type(name)) - elseif registered_groups[name] then - return error(string.format("Attempt to redefine signal group %q, previously defined in %s", name, registered_groups[name].defined)) - end - t.name = name - - t.defined = debug.getinfo(2, "S").short_src or "[?]" - - local label = def.label or name - if type(label) ~= "string" then - return error("Label is not a string") - end - t.label = label - - local mainasps = {} - for idx, asp in pairs(def.aspects) do - local idxtp = type(idx) - if idxtp == "string" then - local t = {} - t.name = idx - - local label = asp.label or idx - if type(label) ~= "string" then - return error("Aspect label is not a string") - end - t.label = label - - for _, k in pairs{"main", "dst", "shunt"} do - t[k] = asp[k] - end - - mainasps[idx] = t - end - end - if #def.aspects < 2 then - return error("Insufficient entries in signal aspect list") - end - for idx, asplist in ipairs(def.aspects) do - if type(asplist) ~= "table" then - asplist = {asplist} - else - asplist = table.copy(asplist) - end - if #asplist < 1 then - error("Invalid entry in signal aspect list") - end - for _, k in ipairs(asplist) do - if type(k) ~= "string" then - return error("Invalid signal aspect ID") - end - local asp = mainasps[k] - if not asp then - return error("Invalid signal aspect ID") - end - if asp.index ~= nil then - return error("Attempt to assign a signal aspect to multiple numeric indices") - end - asp.index = idx - end - mainasps[idx] = asplist - end - t.aspects = mainasps - - registered_groups[name] = t -end - ---- Get the definition of a signal group. --- @function get_group_definition --- @param name The name of the signal group. --- @return[1] The definition for the signal group (if present). --- @return[2] The nil constant (otherwise). -local function get_group_definition(name) - local t = registered_groups[name] - if t then - return table.copy(t) - else - return nil - end -end - -local lib = { - register_group = register_group, - get_group_definition = get_group_definition, -} - -local libmt = { - __call = function(_, ...) - return signal_aspect.new(...) - end, -} - -return setmetatable(lib, libmt) diff --git a/advtrains_interlocking/distant_ui.lua b/advtrains_interlocking/distant_ui.lua deleted file mode 100644 index bb66dc4..0000000 --- a/advtrains_interlocking/distant_ui.lua +++ /dev/null @@ -1,141 +0,0 @@ -local F = advtrains.formspec -local D = advtrains.distant -local I = advtrains.interlocking - -function I.make_short_dst_formspec_component(pos, x, y, w) - local main, set_by = D.get_main(pos) - if main then - local pts_main = minetest.pos_to_string(main) - local desc = attrans("The assignment is made with an unknown method.") - if set_by == "manual" then - desc = attrans("The assignment is made manually.") - elseif set_by == "routesetting" then - desc = attrans("The assignment is made by the routesetting system.") - end - return table.concat { - F.S_label(x, y, "This signal is a distant signal of @1.", pts_main), - F.label(x, y+0.5, desc), - F.S_button_exit(x, y+1, w/2-0.125, "dst_assign", "Reassign"), - F.S_button_exit(x+w/2+0.125, y+1, w/2-0.125, "dst_unassign", "Unassign"), - } - else - return table.concat { - F.S_label(x, y, "This signal is not assigned to a main signal."), - F.S_label(x, y+0.5, "The distant aspect of the signal is not used."), - F.S_button_exit(x, y+1, w, "dst_assign", "Assign") - } - end -end - -function I.make_dst_list_formspec_component(pos, x, y, w, h) - local ymid = y+0.25+h/2 - local dstlist = {} - for pos, _ in pairs(D.get_dst(pos)) do - table.insert(dstlist, minetest.pos_to_string(advtrains.decode_pos(pos))) - end - return table.concat { - F.S_label(x, y, "Distant signals:"), - F.textlist(x, y+0.5, w-1, h-0.5, "dstlist", dstlist), - F.image_button_exit(x+w-0.75, ymid-0.875, 0.75, 0.75, "cdb_add.png", "dst_add", ""), - F.image_button_exit(x+w-0.75, ymid+0.125, 0.75, 0.75, "cdb_clear.png", "dst_del", ""), - } -end - -function I.make_dst_formspec_component(pos, x, y, w, h) - return I.make_short_dst_formspec_component(pos, x, y, w, h) - .. I.make_dst_list_formspec_component(pos, x, y+2, w, h-2) -end - -function I.show_distant_signal_form(pos, pname) - return I.show_ip_form(pos, pname) -end - -local signal_pos = {} -local function init_signal_assignment(pname, pos) - if not minetest.check_player_privs(pname, "interlocking") then - minetest.chat_send_player(pname, attrans("This operation is not allowed without the @1 privilege.", "interlocking")) - return - end - if not D.appropriate_signal(pos) then - minetest.chat_send_player(pname, attrans("Incompatible signal.")) - return - end - signal_pos[pname] = pos - minetest.chat_send_player(pname, attrans("Please punch the signal to use as the main signal.")) -end - -local distant_pos = {} -local function init_distant_assignment(pname, pos) - if not minetest.check_player_privs(pname, "interlocking") then - minetest.send_chat_player(pname, attrans("This operation is now allowed without the @1 privilege.", "interlocking")) - return - end - if not D.appropriate_signal(pos) then - minetest.chat_send_player(pname, attrans("Incompatible signal.")) - return - end - distant_pos[pname] = pos - minetest.chat_send_player(pname, attrans("Please punch the signal to use as the distant signal.")) -end - -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 - local spos = signal_pos[pname] - local distant = false - if not spos then - spos = distant_pos[pname] - if not spos then - return - end - distant = true - end - signal_pos[pname] = nil - distant_pos[pname] = nil - local is_signal = minetest.get_item_group(node.name, "advtrains_signal") >= 2 - if not (is_signal and D.appropriate_signal(pos)) then - minetest.chat_send_player(pname, attrans("Incompatible signal.")) - return - end - minetest.chat_send_player(pname, attrans("Successfully assigned signal.")) - if distant then - D.assign(spos, pos, "manual") - else - D.assign(pos, spos, "manual") - end -end) - -local dstsel = {} - -function advtrains.interlocking.handle_dst_formspec_fields(pname, pos, fields) - if not (pos and minetest.check_player_privs(pname, "interlocking")) then - return - end - if fields.dst_unassign then - D.unassign_dst(pos) - elseif fields.dst_assign then - init_signal_assignment(pname, pos) - elseif fields.dst_add then - init_distant_assignment(pname, pos) - elseif fields.dstlist then - dstsel[pname] = minetest.explode_textlist_event(fields.dstlist).index - elseif fields.dst_del then - local selid = dstsel[pname] - if selid then - local dsts = D.get_dst(pos) - local pos - for p, _ in pairs(dsts) do - selid = selid-1 - if selid <= 0 then - pos = p - break - end - end - if pos then - D.unassign_dst(advtrains.decode_pos(pos)) - end - end - end -end diff --git a/advtrains_interlocking/init.lua b/advtrains_interlocking/init.lua index c3b1119..ee08c30 100644 --- a/advtrains_interlocking/init.lua +++ b/advtrains_interlocking/init.lua @@ -12,8 +12,6 @@ end local modpath = minetest.get_modpath(minetest.get_current_modname()) .. DIR_DELIM ---advtrains.interlocking.aspect = dofile(modpath.."aspect.lua") - dofile(modpath.."database.lua") dofile(modpath.."signal_api.lua") dofile(modpath.."signal_aspect_ui.lua") -- cgit v1.2.3