diff options
-rw-r--r-- | advtrains_interlocking/autonaming.lua | 69 | ||||
-rw-r--r-- | advtrains_interlocking/init.lua | 1 | ||||
-rw-r--r-- | advtrains_interlocking/signal_aspect_ui.lua | 2 | ||||
-rwxr-xr-x | advtrains_interlocking/tcb_ts_ui.lua | 6 |
4 files changed, 78 insertions, 0 deletions
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 = "<prefix>", + 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() |