From f284d395d700ffb0fad29c18a9cfe466ed92961b Mon Sep 17 00:00:00 2001 From: orwell96 Date: Thu, 13 Jan 2022 21:03:55 +0100 Subject: Add TrackIterator interface as a common framework for walking along tracks (also for third-party libs) This will replace the interlocking traverser and will be used in the new itrainmap implementation --- advtrains/helpers.lua | 116 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) (limited to 'advtrains/helpers.lua') diff --git a/advtrains/helpers.lua b/advtrains/helpers.lua index cf890ca..bef4903 100644 --- a/advtrains/helpers.lua +++ b/advtrains/helpers.lua @@ -470,3 +470,119 @@ else end end end + + +-- TrackIterator interface -- + +-- Metatable: +local trackiter_mt = { + -- get whether there are still unprocessed branches + has_next_branch = function(self) + return #self.branches > 0 + end, + -- go to the next unprocessed branch + -- returns track_pos, track_connid of the switch/crossing node where the track branches off + next_branch = function(self) + local br = table.remove(self.branches, 1) + -- Advance internal state + local adj_pos, adj_connid, _, _, adj_conns = advtrains.get_adjacent_rail(br.pos, nil, br.connid) + self.pos = adj_pos + self.bconnid = adj_connid + self.tconns = adj_conns + self.limit = br.limit - 1 + self.visited[advtrains.encode_pos(br.pos)] = true + return br.pos, br.connid + end, + -- get the next track along the current branch, + -- potentially adding branching tracks to the unprocessed branches list + -- returns status, track_pos, track_connid + -- status is true(ok), false(track has ended), nil(traversing limit has been reached) (when status~=true, track_pos and track_connid are nil) + next_track = function(self) + local pos = self.pos + if not pos then + -- last run found track end. Return false + return false, "track_end" + end + -- if limit hit, return nil to signal this + if self.limit <= 0 then + return nil, "limit_hit" + end + if self.visited[advtrains.encode_pos(pos)] then + -- node was already seen. do not continue + return nil, "already_visited" + end + -- select next conn (main conn to follow is the associated connection) + local mconnid = advtrains.get_matching_conn(self.bconnid, #self.tconns) + -- If there are more connections, add these to branches + for nconnid,_ in ipairs(self.tconns) do + if nconnid~=mconnid and nconnid~=self.bconnid then + table.insert(self.branches, {pos = self.pos, connid = nconnid, limit=self.limit}) + end + end + -- Advance internal state + local adj_pos, adj_connid, _, _, adj_conns = advtrains.get_adjacent_rail(pos, self.tconns, mconnid) + self.pos = adj_pos + self.bconnid = adj_connid + self.tconns = adj_conns + self.limit = self.limit - 1 + self.visited[advtrains.encode_pos(pos)] = true + return pos, mconnid + end, +} + +-- Returns a new TrackIterator object + +-- Parameters: +-- initial_pos: the initial track position of the track iterator +-- initial_connid: the connection index in which to traverse. If nil, adds a "branch" for every connection of the track (traverse in all directions) +-- limit: maximum distance from the start point after which the traverser stops +-- follow_all: if true, follows all branches at multi-connection tracks, even the ones pointing backwards or the crossing track on crossings. If false, follows only switches in driving direction. + +-- Functions of the returned TrackIterator can be called via the Lua : notation, such as ti:next_track() +-- If only the main track needs to be followed, use only the ti:next_track() function and do not call ti:next_branch(). +function advtrains.get_track_iterator(initial_pos, initial_connid, limit, follow_all) + local ti = { + visited = {} + } + if initial_connid then + ti.branches = { {pos = initial_pos, connid = initial_connid, limit=limit} } + else + -- get track info here + local node_ok, conns, rail_y=advtrains.get_rail_info_at(initial_pos) + assert(node_ok, "get_track_iterator called with non-track node!") + ti.branches = {} + for coni, _ in pairs(conns) do + table.insert(ti.branches, {pos = initial_pos, connid = coni, limit=limit}) + end + end + setmetatable(ti, {__index=trackiter_mt}) + return ti +end + +--[[ +Example TrackIterator usage structure: + +local ti, pos, connid, ok +ti = advtrains.get_track_iterator(initial_pos, initial_connid, 500, true) +while ti:has_next_branch() do + pos, connid = ti:next_branch() -- in first iteration, this will be the node at initial_pos. In subsequent iterations this will be the switch node from which we are branching off + repeat + + if then break end --for example, when traversing should stop at TCBs this can check if there is a tcb here + ok, pos, connid = ti:next_track() + until not ok -- this stops the loop when either the track end is reached or the limit is hit + -- while loop continues with the next branch ( diverging branch of one of the switches/crossings) until no more are left +end + +Example for walking only a single track (without branching): + +local ti, pos, connid, ok +ti = advtrains.get_track_iterator(initial_pos, initial_connid, 500, true) + +pos, connid = ti:next_branch() -- this always needs to be done at least one time, and gets the track at initial_pos +repeat + + if then break end --for example, when traversing should stop at TCBs this can check if there is a tcb here + ok, pos, connid = ti:next_track() +until not ok -- this stops the loop when either the track end is reached or the limit is hit +]] -- cgit v1.2.3 From 6a5540878f334e97b78ef1430698a8bf8b3faa99 Mon Sep 17 00:00:00 2001 From: orwell96 Date: Sun, 19 Mar 2023 15:20:03 +0100 Subject: Auto-Repair Track Sections/TCBs (automatically when adding/removing or triggered by interlocking tool) --- advtrains/helpers.lua | 50 +- advtrains_interlocking/database.lua | 567 +++++++++++++-------- advtrains_interlocking/tcb_ts_ui.lua | 182 +++---- .../textures/at_il_ts_highlight_particle.png | Bin 0 -> 7164 bytes advtrains_interlocking/tool.lua | 90 +++- 5 files changed, 509 insertions(+), 380 deletions(-) create mode 100644 advtrains_interlocking/textures/at_il_ts_highlight_particle.png (limited to 'advtrains/helpers.lua') diff --git a/advtrains/helpers.lua b/advtrains/helpers.lua index bef4903..6d22bc5 100644 --- a/advtrains/helpers.lua +++ b/advtrains/helpers.lua @@ -342,9 +342,10 @@ function advtrains.get_matching_conn(conn, nconns) return connlku[nconns][conn] end -function advtrains.random_id() +function advtrains.random_id(lenp) local idst="" - for i=0,5 do + local len = lenp or 6 + for i=1,len do idst=idst..(math.random(0,9)) end return idst @@ -476,6 +477,14 @@ end -- Metatable: local trackiter_mt = { + -- Internal State: + -- branches: A list of {pos, connid, limit} for where to restart + -- pos: The *next* position that the track iterator will return + -- bconnid: The connid of the connection of the rail at pos that points backward + -- tconns: The connections of the rail at pos + -- limit: the current limit + -- visited: a key-boolean table of already visited rails + -- get whether there are still unprocessed branches has_next_branch = function(self) return #self.branches > 0 @@ -491,13 +500,18 @@ local trackiter_mt = { self.tconns = adj_conns self.limit = br.limit - 1 self.visited[advtrains.encode_pos(br.pos)] = true + self.last_track_already_visited = false return br.pos, br.connid end, -- get the next track along the current branch, -- potentially adding branching tracks to the unprocessed branches list - -- returns status, track_pos, track_connid - -- status is true(ok), false(track has ended), nil(traversing limit has been reached) (when status~=true, track_pos and track_connid are nil) + -- returns track_pos, track_connid, track_backwards_connid + -- On error, returns nil, reason; reason is one of "track_end", "limit_hit", "already_visited" next_track = function(self) + if self.last_track_already_visited then + -- see comment below + return nil, "already_visited" + end local pos = self.pos if not pos then -- last run found track end. Return false @@ -507,12 +521,17 @@ local trackiter_mt = { if self.limit <= 0 then return nil, "limit_hit" end - if self.visited[advtrains.encode_pos(pos)] then - -- node was already seen. do not continue - return nil, "already_visited" - end -- select next conn (main conn to follow is the associated connection) + local old_bconnid = self.bconnid local mconnid = advtrains.get_matching_conn(self.bconnid, #self.tconns) + if self.visited[advtrains.encode_pos(pos)] then + -- node was already seen + -- Due to special requirements for the track section updater, return this first already visited track once + -- but do not process any further rails on this branch + -- The next call will then throw already_visited error + self.last_track_already_visited = true + return pos, mconnid, old_bconnid + end -- If there are more connections, add these to branches for nconnid,_ in ipairs(self.tconns) do if nconnid~=mconnid and nconnid~=self.bconnid then @@ -526,7 +545,16 @@ local trackiter_mt = { self.tconns = adj_conns self.limit = self.limit - 1 self.visited[advtrains.encode_pos(pos)] = true - return pos, mconnid + self.last_track_already_visited = false + return pos, mconnid, old_bconnid + end, + + add_branch = function(self, pos, connid) + table.insert(self.branches, {pos = pos, connid = connid, limit=self.limit}) + end, + + is_visited = function(self, pos) + return self.visited[advtrains.encode_pos(pos)] end, } @@ -569,8 +597,8 @@ while ti:has_next_branch() do repeat if then break end --for example, when traversing should stop at TCBs this can check if there is a tcb here - ok, pos, connid = ti:next_track() - until not ok -- this stops the loop when either the track end is reached or the limit is hit + pos, connid = ti:next_track() + until not pos -- this stops the loop when either the track end is reached or the limit is hit -- while loop continues with the next branch ( diverging branch of one of the switches/crossings) until no more are left end diff --git a/advtrains_interlocking/database.lua b/advtrains_interlocking/database.lua index 5f163e4..38fad77 100644 --- a/advtrains_interlocking/database.lua +++ b/advtrains_interlocking/database.lua @@ -37,6 +37,22 @@ Another case of this: The / here is a non-interlocked turnout (to a non-frequently used siding). For some reason, there is no exit node there, so the route is set to the signal at the right end. The train is taking the exit to the siding and frees the TC, without ever having touched the right TC. + + +== Terminology / Variable Names == + +"tcb" : A TCB table (as in track_circuit_breaks) +"tcbs" : One side of a tcb (that is tcb == {[1] = tcbs, [2] = tcbs}) +"sigd" : A table of format {p=, s=} by which a "tcbs" is uniqely identified. + +== Section Autorepair & Turnout Cache == + +As fundamental part of reworked route programming mechanism, Track Section objects become weak now. They are created and destroyed on demand. +ildb.repair_tcb automatically checks all nearby sections for issues and repairs them automatically. + +Also the database now holds a cache of the turnouts in the section and their position for all possible driving paths. +Every time a repair operation takes place, and on every track edit operation, the affected sections need to have their cache updated. + ]]-- local TRAVERSER_LIMIT = 1000 @@ -59,7 +75,22 @@ advtrains.interlocking.npr_rails = {} function ildb.load(data) if not data then return end if data.tcbs then - track_circuit_breaks = data.tcbs + if data.tcbpts_conversion_applied then + track_circuit_breaks = data.tcbs + else + -- Convert legacy pos_to_string tcbs to new advtrains.encode_pos position strings + for pts, tcb in pairs(data.tcbs) do + local pos = minetest.string_to_pos(pts) + if pos then + -- that was a pos_to_string + local epos = advtrains.encode_pos(pos) + track_circuit_breaks[epos] = tcb + else + -- keep entry, it is already new + track_circuit_breaks[pts] = tcb + end + end + end end if data.ts then track_sections = data.ts @@ -121,6 +152,7 @@ function ildb.save() rs_callbacks = advtrains.interlocking.route.rte_callbacks, influence_points = influence_points, npr_rails = advtrains.interlocking.npr_rails, + tcbpts_conversion_applied = true, -- remark that legacy pos conversion has taken place } end @@ -147,8 +179,6 @@ TCB data structure -- This is the "B" side of the TCB [2] = { -- Variant: end of track-circuited area (initial state of TC) ts_id = nil, -- this is the indication for end_of_interlocking - section_free = , --this can be set by an exit node via mesecons or atlatc, - -- or from the tc formspec. } } @@ -156,7 +186,11 @@ Track section [id] = { name = "Some human-readable name" tc_breaks = { ,... } -- Bounding TC's (signal specifiers) - -- Can be direct ends (auto-detected), conflicting routes or TCBs that are too far away from each other + rs_cache = { [-] = { [] = "state" } } + -- Saves the turnout states that need to be locked when a route is set from tcb#x to tcb#y + -- e.g. "1-2" = { "800080008000" = "st" } + -- Recalculated on every change via update_ts_cache + route = { origin = , -- route origin entry = , -- supposed train entry point @@ -172,7 +206,9 @@ Track section -- first says whether to clear the routesetting status from the origin signal. -- locks contains the positions where locks are held by this ts. -- 'route' is cleared when train enters the section, while 'route_post' cleared when train leaves section. + trains = {, ...} -- Set whenever a train (or more) reside in this TC + -- Note: The same train ID may be contained in this mapping multiple times, when it has entered the section in two different places. } @@ -185,24 +221,13 @@ signal_assignments = { } ]] +-- Maximum scan length for track iterator +local TS_MAX_SCAN = 1000 --- -function ildb.create_tcb(pos) - local new_tcb = { - [1] = {}, - [2] = {}, - } - local pts = advtrains.roundfloorpts(pos) - if not track_circuit_breaks[pts] then - track_circuit_breaks[pts] = new_tcb - return true - else - return false - end -end +-- basic functions function ildb.get_tcb(pos) - local pts = advtrains.roundfloorpts(pos) + local pts = advtrains.encode_pos(pos) return track_circuit_breaks[pts] end @@ -212,227 +237,347 @@ function ildb.get_tcbs(sigd) return tcb[sigd.s] end - -function ildb.create_ts(sigd) - local tcbs = ildb.get_tcbs(sigd) - local id = advtrains.random_id() - - while track_sections[id] do - id = advtrains.random_id() - end - - track_sections[id] = { - name = "Section "..id, - tc_breaks = { sigd } - } - tcbs.ts_id = id -end - function ildb.get_ts(id) return track_sections[id] end +-- retrieve full tables. Please use only read-only! +function ildb.get_all_tcb() + return track_circuit_breaks +end +function ildb.get_all_ts() + return track_sections +end - --- various helper functions handling sigd's -local sigd_equal = advtrains.interlocking.sigd_equal -local function insert_sigd_nodouble(list, sigd) - for idx, cmp in pairs(list) do - if sigd_equal(sigd, cmp) then - return +-- Checks the consistency of the track section at the given position +-- This method attempts to autorepair track sections if they are inconsistent +-- @param pos: the position to start from +-- Returns: +-- ts_id - the track section that was found +-- nil - No track section exists +function ildb.check_and_repair_ts_at_pos(pos) + atdebug("check_and_repair_ts_at_pos", pos) + -- STEP 1: Ensure that only one section is at this place + -- get all TCBs adjacent to this + local all_tcbs = ildb.get_all_tcbs_adjacent(pos, nil) + local first_ts = true + local ts_id + for _,sigd in ipairs(all_tcbs) do + ildb.tcbs_ensure_ts_ref_exists(sigd) + local tcbs_ts_id = sigd.tcbs.ts_id + if first_ts then + -- this one determines + ts_id = tcbs_ts_id + first_ts = false + else + -- these must be the same as the first + if ts_id ~= tcbs_ts_id then + -- inconsistency is found, repair it + atdebug("check_and_repair_ts_at_pos: Inconsistency is found!") + return ildb.repair_ts_merge_all(all_tcbs) + -- Step2 check is no longer necessary since we just created that new section + end end end - table.insert(list, sigd) -end - - --- This function will actually handle the node that is in connid direction from the node at pos --- so, this needs the conns of the node at pos, since these are already calculated -local function traverser(found_tcbs, pos, conns, connid, count, brk_when_found_n) - local adj_pos, adj_connid, conn_idx, nextrail_y, next_conns = advtrains.get_adjacent_rail(pos, conns, connid, advtrains.all_tracktypes) - if not adj_pos then - --atdebug("Traverser found end-of-track at",pos, connid) + -- only one found (it is either nil or a ts id) + atdebug("check_and_repair_ts_at_pos: TS consistent id=",ts_id,"") + if not ts_id then return + -- All TCBs agreed that there is no section here. end - -- look whether there is a TCB here - if #next_conns == 2 then --if not, don't even try! - local tcb = ildb.get_tcb(adj_pos) - if tcb then - -- done with this branch - --atdebug("Traverser found tcb at",adj_pos, adj_connid) - insert_sigd_nodouble(found_tcbs, {p=adj_pos, s=adj_connid}) - return + + local ts = ildb.get_ts(ts_id) + if not ts then + -- This branch may never be reached, because ildb.tcbs_ensure_ts_ref_exists(sigd) is already supposed to clear out missing sections + error("check_and_repair_ts_at_pos: Resolved to nonexisting section although ildb.tcbs_ensure_ts_ref_exists(sigd) was supposed to prevent this. Panic!") + end + ildb.purge_ts_tcb_refs(ts_id) + -- STEP 2: Ensure that all_tcbs is equal to the track section's TCB list. If there are extra TCBs then the section should be split + -- ildb.tcbs_ensure_ts_ref_exists(sigd) has already make sure that all tcbs are found in the ts's tc_breaks list + -- That means it is sufficient to compare the LENGTHS of both lists, if one is longer then it is inconsistent + if #ts.tc_breaks ~= #all_tcbs then + atdebug("check_and_repair_ts_at_pos: Partition is found!") + return ildb.repair_ts_merge_all(all_tcbs) + end + return ts_id +end + +-- Helper function to prevent duplicates +local function insert_sigd_if_not_present(tab, sigd) + local found = false + for _, ssigd in ipairs(tab) do + if vector.equals(sigd.p, ssigd.p) and sigd.s==ssigd.s then + found = true end end - -- recursion abort condition - if count > TRAVERSER_LIMIT then - --atdebug("Traverser hit counter at",adj_pos, adj_connid) - return true + if not found then + table.insert(tab, sigd) end - -- continue traversing - local counter_hit = false - for nconnid, nconn in ipairs(next_conns) do - if adj_connid ~= nconnid then - counter_hit = counter_hit or traverser(found_tcbs, adj_pos, next_conns, nconnid, count + 1, brk_when_found_n) - if brk_when_found_n and #found_tcbs>=brk_when_found_n then - break + return not found +end + +-- Starting from a position, search all TCBs that can be reached from this position. +-- In a non-faulty setup, all of these should have the same track section assigned. +-- This function does not trigger a repair. +-- @param inipos: the initial position +-- @param inidir: the initial direction, or nil to search in all directions +-- Returns: a list of sigd's describing the TCBs found (sigd's point inward): +-- {p=, s=, tcbs=} +function ildb.get_all_tcbs_adjacent(inipos, inidir) + atdebug("get_all_tcbs_adjacent: inipos",inipos,"inidir",inidir,"") + local found_sigd = {} + local ti = advtrains.get_track_iterator(inipos, inidir, TS_MAX_SCAN, true) + local pos, connid, bconnid, tcb + while ti:has_next_branch() do + pos, connid = ti:next_branch() + --atdebug("get_all_tcbs_adjacent: BRANCH: ",pos, connid) + bconnid = nil + repeat + tcb = ildb.get_tcb(pos) + if tcb then + -- found a tcb + if not bconnid then + -- A branch start point cannot be a TCB, as that would imply that it was a turnout/crossing (illegal) + -- Only exception where this can happen is if the start point is a TCB, then we'd need to add the forward side of it to our list + if pos.x==inipos.x and pos.y==inipos.y and pos.z==inipos.z then + -- Note "connid" instead of "bconnid" + atdebug("get_all_tcbs_adjacent: Found Startpoint TCB: ",pos, connid, "ts=", tcb[connid].ts_id) + insert_sigd_if_not_present(found_sigd, {p=pos, s=connid, tcbs=tcb[connid]}) + else + -- this may not happend + error("Found TCB at TrackIterator new branch which is not the start point, this is illegal! pos="..minetest.pos_to_string(pos)) + end + + else + -- add the sigd of this tcb and a reference to the tcb table in it + atdebug("get_all_tcbs_adjacent: Found TCB: ",pos, bconnid, "ts=", tcb[bconnid].ts_id) + insert_sigd_if_not_present(found_sigd, {p=pos, s=bconnid, tcbs=tcb[bconnid]}) + break + end end - end + pos, connid, bconnid = ti:next_track() + --atdebug("get_all_tcbs_adjacent: TRACK: ",pos, connid, bconnid) + until not pos end - return counter_hit + return found_sigd end - - --- Merges the TS with merge_id into root_id and then deletes merge_id -local function merge_ts(root_id, merge_id) - local rts = ildb.get_ts(root_id) - local mts = ildb.get_ts(merge_id) - if not mts then return end -- This may be the case when sync_tcb_neighbors - -- inserts the same id twice. do nothing. - - if not ildb.may_modify_ts(rts) then return false end - if not ildb.may_modify_ts(mts) then return false end - - -- cobble together the list of TCBs - for _, msigd in ipairs(mts.tc_breaks) do - local tcbs = ildb.get_tcbs(msigd) - if tcbs then - insert_sigd_nodouble(rts.tc_breaks, msigd) - tcbs.ts_id = root_id +-- Called by frontend functions when multiple tcbs's that logically belong to one section have been determined to have different sections +-- Parameter is the output of ildb.get_all_tcbs_adjacent(pos) +-- Returns the ID of the track section that results after the merge +function ildb.repair_ts_merge_all(all_tcbs, force_create) + atdebug("repair_ts_merge_all: Instructed to merge sections of following TCBs:") + -- The first loop does the following for each TCBS: + -- a) Store the TS ID in the set of TS to update + -- b) Set the TS ID to nil, so that the TCBS gets removed from the section + local ts_to_update = {} + local ts_name_repo = {} + local any_ts = false + for _,sigd in ipairs(all_tcbs) do + local ts_id = sigd.tcbs.ts_id + atdebug(sigd, "ts=", ts_id) + if ts_id then + local ts = track_sections[ts_id] + if ts then + any_ts = true + ts_to_update[ts_id] = true + -- if nonstandard name, store this + if ts.name and not string.match(ts.name, "^Section") then + ts_name_repo[#ts_name_repo+1] = ts.name + end + end end - advtrains.interlocking.show_tcb_marker(msigd.p) + sigd.tcbs.ts_id = nil end - -- done - track_sections[merge_id] = nil -end - -local lntrans = { "A", "B" } -local function sigd_to_string(sigd) - return minetest.pos_to_string(sigd.p).." / "..lntrans[sigd.s] + if not any_ts and not force_create then + -- nothing to do at all, just no interlocking. Why were we even called + atdebug("repair_ts_merge_all: No track section present, will not create a new one") + return nil + end + -- Purge every TS in turn. TS's that are now empty will be deleted. TS's that still have TCBs will be kept + for ts_id, _ in pairs(ts_to_update) do + local remain_ts = ildb.purge_ts_tcb_refs(ts_id) + end + -- Create a new fresh track section with all the TCBs we have in our collection + local new_ts_id, new_ts = ildb.create_ts_from_tcb_list(all_tcbs) + return new_ts_id end --- Check for near TCBs and connect to their TS if they have one, and syncs their data. -function ildb.sync_tcb_neighbors(pos, connid) - local found_tcbs = { {p = pos, s = connid} } - local node_ok, conns, rhe = advtrains.get_rail_info_at(pos, advtrains.all_tracktypes) - if not node_ok then - atwarn("update_tcb_neighbors but node is NOK: "..minetest.pos_to_string(pos)) - return +-- For the specified TS, go through the list of TCBs and purge all TCBs that have no corresponding backreference in their TCBS table. +-- If the track section ends up empty, it is deleted in this process. +-- Should the track section still exist after the purge operation, it is returned. +function ildb.purge_ts_tcb_refs(ts_id) + local ts = track_sections[ts_id] + if not ts then + return nil end - - --atdebug("Traversing from ",pos, connid) - local counter_hit = traverser(found_tcbs, pos, conns, connid, 0) - - local ts_id - local list_eoi = {} - local list_ok = {} - local list_mismatch = {} - local ts_to_merge = {} - - for idx, sigd in pairs(found_tcbs) do + local has_changed = false + local i = 1 + while ts.tc_breaks[i] do + -- get TCBS + local sigd = ts.tc_breaks[i] local tcbs = ildb.get_tcbs(sigd) - if not tcbs.ts_id then - --atdebug("Sync: put",sigd_to_string(sigd),"into list_eoi") - table.insert(list_eoi, sigd) - elseif not ts_id and tcbs.ts_id then - if not ildb.get_ts(tcbs.ts_id) then - atwarn("Track section database is inconsistent, there's no TS with ID=",tcbs.ts_id) - tcbs.ts_id = nil - table.insert(list_eoi, sigd) + if tcbs then + if tcbs.ts_id == ts_id then + -- this one is legit + i = i+1 else - --atdebug("Sync: put",sigd_to_string(sigd),"into list_ok") - ts_id = tcbs.ts_id - table.insert(list_ok, sigd) + -- this one is to be purged + atdebug("purge_ts_tcb_refs(",ts_id,"): purging",sigd,"(backreference = ",tcbs.ts_id,")") + table.remove(ts.tc_breaks, i) + has_changed = true end - elseif ts_id and tcbs.ts_id and tcbs.ts_id ~= ts_id then - atwarn("Track section database is inconsistent, sections share track!") - atwarn("Merging",tcbs.ts_id,"into",ts_id,".") - table.insert(list_mismatch, sigd) - table.insert(ts_to_merge, tcbs.ts_id) + else + -- if not tcbs: was anyway an orphan, remove it + atdebug("purge_ts_tcb_refs(",ts_id,"): purging",sigd,"(referred nonexisting TCB)") + table.remove(ts.tc_breaks, i) + has_changed = true end end - if ts_id then - local ts = ildb.get_ts(ts_id) - for _, sigd in ipairs(list_eoi) do - local tcbs = ildb.get_tcbs(sigd) - tcbs.ts_id = ts_id - table.insert(ts.tc_breaks, sigd) - advtrains.interlocking.show_tcb_marker(sigd.p) - end - for _, mts in ipairs(ts_to_merge) do - merge_ts(ts_id, mts) + if #ts.tc_breaks == 0 then + -- remove the section completely + atdebug("purge_ts_tcb_refs(",ts_id,"): after purging, the section is empty, is being deleted") + track_sections[ts_id] = nil + return nil + else + if has_changed then + -- needs to update route cache + ildb.update_ts_cache(ts_id) end + return ts end end -function ildb.link_track_sections(merge_id, root_id) - if merge_id == root_id then +-- For the specified TCBS, make sure that the track section referenced by it +-- (a) exists and +-- (b) has a backreference to this TCBS stored in its tc_breaks list +function ildb.tcbs_ensure_ts_ref_exists(sigd) + local tcbs = sigd.tcbs or ildb.get_tcbs(sigd) + if not tcbs or not tcbs.ts_id then return end + local ts = ildb.get_ts(tcbs.ts_id) + if not ts then + atdebug("tcbs_ensure_ts_ref_exists(",sigd,"): TS does not exist, setting to nil") + -- TS is deleted, clear own ts id + tcbs.ts_id = nil return end - merge_ts(root_id, merge_id) + local did_insert = insert_sigd_if_not_present(ts.tc_breaks, {p=sigd.p, s=sigd.s}) + if did_insert then + atdebug("tcbs_ensure_ts_ref_exists(",sigd,"): TCBS was missing reference in TS",tcbs.ts_id) + ildb.update_ts_cache(ts_id) + end end -function ildb.remove_from_interlocking(sigd) - local tcbs = ildb.get_tcbs(sigd) - if not ildb.may_modify_tcbs(tcbs) then return false end +function ildb.create_ts_from_tcb_list(sigd_list) + local id = advtrains.random_id(8) - if tcbs.ts_id then - local tsid = tcbs.ts_id - local ts = ildb.get_ts(tsid) - if not ts then - tcbs.ts_id = nil - return true - end - - -- remove entry from the list - local idx = 1 - while idx <= #ts.tc_breaks do - local cmp = ts.tc_breaks[idx] - if sigd_equal(sigd, cmp) then - table.remove(ts.tc_breaks, idx) - else - idx = idx + 1 - end - end - tcbs.ts_id = nil - - --ildb.sync_tcb_neighbors(sigd.p, sigd.s) - - if #ts.tc_breaks == 0 then - track_sections[tsid] = nil + while track_sections[id] do + id = advtrains.random_id(8) + end + atdebug("create_ts_from_tcb_list: sigd_list=",sigd_list, "new ID will be ",id) + + local tcbr = {} + -- makes a copy of the sigd list, for use in repair mechanisms where sigd may contain a tcbs field which we dont want + for _, sigd in ipairs(sigd_list) do + table.insert(tcbr, {p=sigd.p, s=sigd.s}) + local tcbs = sigd.tcbs or ildb.get_tcbs(sigd) + if tcbs.ts_id then + error("Trying to create TS with TCBS that is already assigned to other section") end + tcbs.ts_id = id end - advtrains.interlocking.show_tcb_marker(sigd.p) - if tcbs.signal then - return false + + local new_ts = { + tc_breaks = tcbr + } + track_sections[id] = new_ts + -- update the TCB markers + for _, sigd in ipairs(sigd_list) do + advtrains.interlocking.show_tcb_marker(sigd.p) end - return true + + + ildb.update_ts_cache(id) + return id, new_ts end -function ildb.remove_tcb(pos) - local pts = advtrains.roundfloorpts(pos) - if not track_circuit_breaks[pts] then - return true --FIX: not an error, because tcb is already removed - end - for connid=1,2 do - if not ildb.remove_from_interlocking({p=pos, s=connid}) then - return false - end + +-- Updates the turnout cache of the given track section +function ildb.update_ts_cache(ts_id) + local ts = ildb.get_ts(ts_id) + if not ts then + error("Update TS Cache called with nonexisting ts_id "..(ts_id or "nil")) end + local rscache = {} + -- start on every of the TS's TCBs, walk the track forward and store locks along the way + -- TODO: Need change in handling of switches + atdebug("update_ts_cache",ts_id,"TODO: implement") +end + +local lntrans = { "A", "B" } +local function sigd_to_string(sigd) + return minetest.pos_to_string(sigd.p).." / "..lntrans[sigd.s] +end + +-- Create a new TCB at the position and update/repair the adjoining sections +function ildb.create_tcb_at(pos) + atdebug("create_tcb_at",pos) + local pts = advtrains.encode_pos(pos) + track_circuit_breaks[pts] = {[1] = {}, [2] = {}} + local all_tcbs_1 = ildb.get_all_tcbs_adjacent(pos, 1) + atdebug("TCBs on A side",all_tcbs_1) + local all_tcbs_2 = ildb.get_all_tcbs_adjacent(pos, 2) + atdebug("TCBs on B side",all_tcbs_2) + -- perform TS repair + ildb.repair_ts_merge_all(all_tcbs_1) + ildb.repair_ts_merge_all(all_tcbs_2) +end + +-- Create a new TCB at the position and update/repair the now joined section +function ildb.remove_tcb_at(pos) + atdebug("remove_tcb_at",pos) + local pts = advtrains.encode_pos(pos) + local old_tcb = track_circuit_breaks[pts] track_circuit_breaks[pts] = nil + -- purge the track sections adjacent + if old_tcb[1].ts_id then + ildb.purge_ts_tcb_refs(old_tcb[1].ts_id) + end + if old_tcb[2].ts_id then + ildb.purge_ts_tcb_refs(old_tcb[2].ts_id) + end + advtrains.interlocking.remove_tcb_marker(pos) + -- If needed, merge the track sections here + ildb.check_and_repair_ts_at_pos(pos) return true end -function ildb.dissolve_ts(ts_id) - local ts = ildb.get_ts(ts_id) - if not ildb.may_modify_ts(ts) then return false end - local tcbr = advtrains.merge_tables(ts.tc_breaks) - for _,sigd in ipairs(tcbr) do - ildb.remove_from_interlocking(sigd) +function ildb.create_ts_from_tcbs(sigd) + atdebug("create_ts_from_tcbs",sigd) + local all_tcbs = ildb.get_all_tcbs_adjacent(sigd.p, sigd.s) + ildb.repair_ts_merge_all(all_tcbs, true) +end + +-- Remove the given track section, leaving its TCBs with no section assigned +function ildb.remove_ts(ts_id) + atdebug("remove_ts",ts_id) + local ts = track_sections[ts_id] + if not ts then + error("remove_ts: "..ts_id.." doesn't exist") end - -- Note: ts gets removed in the moment of the removal of the last TCB. - return true + while ts.tc_breaks[i] do + -- get TCBS + local sigd = ts.tc_breaks[i] + local tcbs = ildb.get_tcbs(sigd) + if tcbs then + atdebug("cleared TCB",sigd) + tcbs.ts_id = nil + else + atdebug("orphan TCB",sigd) + end + i = i+1 + end + track_sections[ts_id] = nil end -- Returns true if it is allowed to modify any property of a track section, such as @@ -457,38 +602,8 @@ function ildb.may_modify_tcbs(tcbs) return true end --- Utilize the traverser to find the track section at the specified position --- Returns: --- ts_id, origin - the first found ts and the sigd of the found tcb --- nil - there were no TCBs in TRAVERSER_MAX range of the position --- false - the first found TCB stated End-Of-Interlocking, or track ends were reached -function ildb.get_ts_at_pos(pos) - local node_ok, conns, rhe = advtrains.get_rail_info_at(pos, advtrains.all_tracktypes) - if not node_ok then - error("get_ts_at_pos but node is NOK: "..minetest.pos_to_string(pos)) - end - local limit_hit = false - local found_tcbs = {} - for connid, conn in ipairs(conns) do -- Note: a breadth-first-search would be better for performance - limit_hit = limit_hit or traverser(found_tcbs, pos, conns, connid, 0, 1) - if #found_tcbs >= 1 then - local tcbs = ildb.get_tcbs(found_tcbs[1]) - local ts - if tcbs.ts_id then - return tcbs.ts_id, found_tcbs[1] - else - return false - end - end - end - if limit_hit then - -- there was at least one limit hit - return nil - else - -- all traverser ends were track ends - return false - end -end + +-- Signals/IP -- -- returns the sigd the signal at pos belongs to, if this is known diff --git a/advtrains_interlocking/tcb_ts_ui.lua b/advtrains_interlocking/tcb_ts_ui.lua index 0cc10da..97c28a8 100755 --- a/advtrains_interlocking/tcb_ts_ui.lua +++ b/advtrains_interlocking/tcb_ts_ui.lua @@ -88,12 +88,8 @@ minetest.register_node("advtrains_interlocking:tcb_node", { local tcb = ildb.get_tcb(tcbpos) if not tcb then return true end for connid=1,2 do - if tcb[connid].ts_id or tcb[connid].signal then - minetest.chat_send_player(pname, "Can't remove TCB: Both sides must have no track section and no signal assigned!") - return false - end - if not ildb.may_modify_tcbs(tcb[connid]) then - minetest.chat_send_player(pname, "Can't remove TCB: Side "..connid.." forbids modification (shouldn't happen).") + if tcb[connid].signal then + minetest.chat_send_player(pname, "Can't remove TCB: Both sides must have no signal assigned!") return false end end @@ -105,15 +101,7 @@ minetest.register_node("advtrains_interlocking:tcb_node", { local tcbpts = oldmetadata.fields.tcb_pos if tcbpts and tcbpts ~= "" then local tcbpos = minetest.string_to_pos(tcbpts) - local success = ildb.remove_tcb(tcbpos) - if success and player then - minetest.chat_send_player(player:get_player_name(), "TCB has been removed.") - else - minetest.chat_send_player(player:get_player_name(), "Failed to remove TCB!") - minetest.set_node(pos, oldnode) - local meta = minetest.get_meta(pos) - meta:set_string("tcb_pos", minetest.pos_to_string(tcbpos)) - end + ildb.remove_tcb_at(tcbpos) end end, }) @@ -167,15 +155,13 @@ minetest.register_on_punchnode(function(pos, node, player, pointed_thing) if vector.distance(pos, tcbnpos)<=20 then local node_ok, conns, rhe = advtrains.get_rail_info_at(pos, advtrains.all_tracktypes) if node_ok and #conns == 2 then - local ok = ildb.create_tcb(pos) - - if not ok then - minetest.chat_send_player(pname, "Configuring TCB: TCB already exists at this position! It has now been re-assigned.") + -- if there is already a tcb here, reassign it + if ildb.get_tcb(pos) then + minetest.chat_send_player(pname, "Configuring TCB: Already existed at this position, it is now linked to this TCB marker") + else + ildb.create_tcb_at(pos) end - - ildb.sync_tcb_neighbors(pos, 1) - ildb.sync_tcb_neighbors(pos, 2) - + local meta = minetest.get_meta(tcbnpos) meta:set_string("tcb_pos", minetest.pos_to_string(pos)) meta:set_string("infotext", "TCB assigned to "..minetest.pos_to_string(pos)) @@ -234,22 +220,12 @@ local function mktcbformspec(tcbs, btnpref, offset, pname) ts = ildb.get_ts(tcbs.ts_id) end if ts then - form = form.."label[0.5,"..offset..";Side "..btnpref..": "..minetest.formspec_escape(ts.name).."]" + form = form.."label[0.5,"..offset..";Side "..btnpref..": "..minetest.formspec_escape(ts.name or tcbs.ts_id).."]" form = form.."button[0.5,"..(offset+0.5)..";5,1;"..btnpref.."_gotots;Show track section]" - if ildb.may_modify_tcbs(tcbs) then - -- Note: the security check to prohibit those actions is located in database.lua in the corresponding functions. - form = form.."button[0.5,"..(offset+1.5)..";2.5,1;"..btnpref.."_update;Update near TCBs]" - form = form.."button[3 ,"..(offset+1.5)..";2.5,1;"..btnpref.."_remove;Remove from section]" - end else tcbs.ts_id = nil form = form.."label[0.5,"..offset..";Side "..btnpref..": ".."End of interlocking]" form = form.."button[0.5,"..(offset+0.5)..";5,1;"..btnpref.."_makeil;Create Interlocked Track Section]" - --if tcbs.section_free then - --form = form.."button[0.5,"..(offset+1.5)..";5,1;"..btnpref.."_setlocked;Section is free]" - --else - --form = form.."button[0.5,"..(offset+1.5)..";5,1;"..btnpref.."_setfree;Section is blocked]" - --end end if tcbs.signal then form = form.."button[0.5,"..(offset+2.5)..";5,1;"..btnpref.."_sigdia;Signalling]" @@ -297,11 +273,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) local tcb = ildb.get_tcb(pos) if not tcb then return end local f_gotots = {fields.A_gotots, fields.B_gotots} - local f_update = {fields.A_update, fields.B_update} - local f_remove = {fields.A_remove, fields.B_remove} local f_makeil = {fields.A_makeil, fields.B_makeil} - local f_setlocked = {fields.A_setlocked, fields.B_setlocked} - local f_setfree = {fields.A_setfree, fields.B_setfree} local f_asnsig = {fields.A_asnsig, fields.B_asnsig} local f_sigdia = {fields.A_sigdia, fields.B_sigdia} @@ -312,29 +284,12 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) advtrains.interlocking.show_ts_form(tcbs.ts_id, pname) return end - if f_update[connid] then - ildb.sync_tcb_neighbors(pos, connid) - end - if f_remove[connid] then - ildb.remove_from_interlocking({p=pos, s=connid}) - end else if f_makeil[connid] then - -- try sinc_tcb_neighbors first - ildb.sync_tcb_neighbors(pos, connid) - -- if that didn't work, create new section if not tcbs.ts_id then - ildb.create_ts({p=pos, s=connid}) - ildb.sync_tcb_neighbors(pos, connid) + ildb.create_ts_from_tcbs({p=pos, s=connid}) end end - -- non-interlocked - if f_setfree[connid] then - tcbs.section_free = true - end - if f_setlocked[connid] then - tcbs.section_free = nil - end end if f_asnsig[connid] and not tcbs.signal then minetest.chat_send_player(pname, "Configuring TCB: Please punch the signal to assign.") @@ -357,10 +312,7 @@ end) -- TS Formspec --- textlist selection temporary storage -local ts_pselidx = {} - -function advtrains.interlocking.show_ts_form(ts_id, pname, sel_tcb) +function advtrains.interlocking.show_ts_form(ts_id, pname) if not minetest.check_player_privs(pname, "interlocking") then minetest.chat_send_player(pname, "Insufficient privileges to use this!") return @@ -369,7 +321,7 @@ function advtrains.interlocking.show_ts_form(ts_id, pname, sel_tcb) if not ts_id then return end local form = "size[10,10]label[0.5,0.5;Track Section Detail - "..ts_id.."]" - form = form.."field[0.8,2;5.2,1;name;Section name;"..minetest.formspec_escape(ts.name).."]" + form = form.."field[0.8,2;5.2,1;name;Section name;"..minetest.formspec_escape(ts.name or "").."]" form = form.."button[5.5,1.7;1,1;setname;Set]" local hint @@ -382,26 +334,8 @@ function advtrains.interlocking.show_ts_form(ts_id, pname, sel_tcb) form = form.."textlist[0.5,3;5,3;tcblist;"..table.concat(strtab, ",").."]" if ildb.may_modify_ts(ts) then - - if players_link_ts[pname] then - local other_id = players_link_ts[pname] - local other_ts = ildb.get_ts(other_id) - if other_ts then - if ildb.may_modify_ts(other_ts) then - form = form.."button[5.5,3;3.5,1;mklink;Join with "..minetest.formspec_escape(other_ts.name).."]" - form = form.."button[9 ,3;0.5,1;cancellink;X]" - end - end - else - form = form.."button[5.5,3;4,1;link;Join into other section]" - hint = 1 - end - form = form.."button[5.5,4;4,1;dissolve;Dissolve Section]" + form = form.."button[5.5,4;4,1;remove;Remove Section]" form = form.."tooltip[dissolve;This will remove the track section and set all its end points to End Of Interlocking]" - if sel_tcb then - form = form.."button[5.5,5;4,1;del_tcb;Unlink selected TCB]" - hint = 2 - end else hint=3 end @@ -420,17 +354,12 @@ function advtrains.interlocking.show_ts_form(ts_id, pname, sel_tcb) end form = form.."button[5.5,7;4,1;reset;Reset section state]" - - if hint == 1 then - form = form.."label[0.5,0.75;Use the 'Join' button to designate rail crosses and link not listed far-away TCBs]" - elseif hint == 2 then - form = form.."label[0.5,0.75;Unlinking a TCB will set it to non-interlocked mode.]" - elseif hint == 3 then + + if hint == 3 then form = form.."label[0.5,0.75;You cannot modify track sections when a route is set or a train is on the section.]" --form = form.."label[0.5,1;Trying to unlink a TCB directly connected to this track will not work.]" end - ts_pselidx[pname]=sel_tcb minetest.show_formspec(pname, "at_il_tsconfig_"..ts_id, form) end @@ -442,45 +371,15 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) return end -- independent of the formspec, clear this whenever some formspec event happens - local tpsi = ts_pselidx[pname] - ts_pselidx[pname] = nil local ts_id = string.match(formname, "^at_il_tsconfig_(.+)$") if ts_id and not fields.quit then local ts = ildb.get_ts(ts_id) if not ts then return end - local sel_tcb - if fields.tcblist then - local tev = minetest.explode_textlist_event(fields.tcblist) - sel_tcb = tev.index - ts_pselidx[pname] = sel_tcb - elseif tpsi then - sel_tcb = tpsi - end - if ildb.may_modify_ts(ts) then - if players_link_ts[pname] then - if fields.cancellink then - players_link_ts[pname] = nil - elseif fields.mklink then - ildb.link_track_sections(players_link_ts[pname], ts_id) - players_link_ts[pname] = nil - end - end - - if fields.del_tcb and sel_tcb and sel_tcb > 0 and sel_tcb <= #ts.tc_breaks then - if not ildb.remove_from_interlocking(ts.tc_breaks[sel_tcb]) then - minetest.chat_send_player(pname, "Please unassign signal first!") - end - sel_tcb = nil - end - - if fields.link then - players_link_ts[pname] = ts_id - end - if fields.dissolve then - ildb.dissolve_ts(ts_id) + if fields.remove then + ildb.remove_ts(ts_id) minetest.close_formspec(pname, formname) return end @@ -489,7 +388,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) if fields.setname then ts.name = fields.name if ts.name == "" then - ts.name = "Section "..ts_id + ts.name = nil end end @@ -566,7 +465,7 @@ function advtrains.interlocking.show_tcb_marker(pos) ts = ildb.get_ts(tcbs.ts_id) end if ts then - itex[connid] = ts.name + itex[connid] = ts.name or tcbs.ts_id or "???" else itex[connid] = "--EOI--" end @@ -589,6 +488,47 @@ function advtrains.interlocking.show_tcb_marker(pos) markerent[pts] = obj end +function advtrains.interlocking.remove_tcb_marker(pos) + local pts = advtrains.roundfloorpts(pos) + if markerent[pts] then + markerent[pts]:remove() + end + markerent[pts] = nil +end + +-- Spawns particles to highlight the clicked track section +-- TODO: Adapt behavior to not dumb-walk anymore +function advtrains.interlocking.highlight_track_section(pos) + local ti = advtrains.get_track_iterator(pos, nil, 100, true) + local pos, connid, bconnid, tcb + while ti:has_next_branch() do + pos, connid = ti:next_branch() + --atdebug("highlight_track_section: BRANCH: ",pos, connid) + bconnid = nil + repeat + -- spawn particles + minetest.add_particle({ + pos = pos, + velocity = {x=0, y=0, z=0}, + acceleration = {x=0, y=0, z=0}, + expirationtime = 10, + size = 7, + vertical = true, + texture = "at_il_ts_highlight_particle.png", + glow = 6, + }) + -- abort if TCB is found + tcb = ildb.get_tcb(pos) + if tcb then + advtrains.interlocking.show_tcb_marker(pos) + break + end + pos, connid, bconnid = ti:next_track() + --atdebug("highlight_track_section: TRACK: ",pos, connid, bconnid) + until not pos + end +end + -- Signalling formspec - set routes a.s.o -- textlist selection temporary storage diff --git a/advtrains_interlocking/textures/at_il_ts_highlight_particle.png b/advtrains_interlocking/textures/at_il_ts_highlight_particle.png new file mode 100644 index 0000000..4ba3622 Binary files /dev/null and b/advtrains_interlocking/textures/at_il_ts_highlight_particle.png differ diff --git a/advtrains_interlocking/tool.lua b/advtrains_interlocking/tool.lua index 5d38b3a..4b701b4 100644 --- a/advtrains_interlocking/tool.lua +++ b/advtrains_interlocking/tool.lua @@ -3,14 +3,64 @@ local ilrs = advtrains.interlocking.route +local function node_right_click(pos, pname) + if advtrains.is_passive(pos) then + local form = "size[7,5]label[0.5,0.5;Route lock inspector]" + local pts = minetest.pos_to_string(pos) + + local rtl = ilrs.has_route_lock(pts) + + if rtl then + form = form.."label[0.5,1;Route locks currently put:\n"..rtl.."]" + form = form.."button_exit[0.5,3.5; 5,1;clear;Clear]" + else + form = form.."label[0.5,1;No route locks set]" + form = form.."button_exit[0.5,3.5; 5,1;emplace;Emplace manual lock]" + end + + minetest.show_formspec(pname, "at_il_rtool_"..pts, form) + return + end + + -- If not a turnout, check the track section and show a form + local node_ok, conns, rail_y=advtrains.get_rail_info_at(pos) + if not node_ok then + minetest.chat_send_player(pname, "Node is not a track!") + return + end + + local ts_id = advtrains.interlocking.db.check_and_repair_ts_at_pos(pos) + if ts_id then + advtrains.interlocking.show_ts_form(ts_id, pname) + else + minetest.chat_send_player(pname, "No track section at this location!") + end +end + +local function node_left_click(pos, pname) + local node_ok, conns, rail_y=advtrains.get_rail_info_at(pos) + if not node_ok then + minetest.chat_send_player(pname, "Node is not a track!") + return + end + + local ts_id = advtrains.interlocking.db.check_and_repair_ts_at_pos(pos) + if ts_id then + advtrains.interlocking.highlight_track_section(pos) + else + minetest.chat_send_player(pname, "No track section at this location!") + end +end + + minetest.register_craftitem("advtrains_interlocking:tool",{ - description = "Interlocking tool\nright-click turnouts to inspect route locks", + description = "Interlocking tool\nPunch: Highlight track section\nPlace: check route locks/show track section info", groups = {cracky=1}, -- key=name, value=rating; rating=1..3. inventory_image = "at_il_tool.png", wield_image = "at_il_tool.png", stack_max = 1, - on_place = function(itemstack, placer, pointed_thing) - local pname = placer:get_player_name() + on_place = function(itemstack, player, pointed_thing) + local pname = player:get_player_name() if not pname then return end @@ -20,27 +70,23 @@ minetest.register_craftitem("advtrains_interlocking:tool",{ end if pointed_thing.type=="node" then local pos=pointed_thing.under - if advtrains.is_passive(pos) then - local form = "size[7,5]label[0.5,0.5;Route lock inspector]" - local pts = minetest.pos_to_string(pos) - - local rtl = ilrs.has_route_lock(pts) - - if rtl then - form = form.."label[0.5,1;Route locks currently put:\n"..rtl.."]" - form = form.."button_exit[0.5,3.5; 5,1;clear;Clear]" - else - form = form.."label[0.5,1;No route locks set]" - form = form.."button_exit[0.5,3.5; 5,1;emplace;Emplace manual lock]" - end - - minetest.show_formspec(pname, "at_il_rtool_"..pts, form) - else - minetest.chat_send_player(pname, "Cannot use this here.") - return - end + node_right_click(pos, pname) end end, + on_use = function(itemstack, player, pointed_thing) + local pname = player:get_player_name() + if not pname then + return + end + if not minetest.check_player_privs(pname, {interlocking=true}) then + minetest.chat_send_player(pname, "Insufficient privileges to use this!") + return + end + if pointed_thing.type=="node" then + local pos=pointed_thing.under + node_left_click(pos, pname) + end + end }) minetest.register_on_player_receive_fields(function(player, formname, fields) -- cgit v1.2.3 From a14eb7fe737ffc4df4077a01f4859d74f62d0af2 Mon Sep 17 00:00:00 2001 From: orwell96 Date: Sun, 19 Mar 2023 17:19:40 +0100 Subject: TCB Xlinking added, to make nonconnected crossings possible --- advtrains/helpers.lua | 2 + advtrains_interlocking/database.lua | 168 ++++++++++++++++++++++++++++++----- advtrains_interlocking/tcb_ts_ui.lua | 95 +++++++++++++------- advtrains_interlocking/tool.lua | 13 ++- 4 files changed, 222 insertions(+), 56 deletions(-) (limited to 'advtrains/helpers.lua') diff --git a/advtrains/helpers.lua b/advtrains/helpers.lua index 6d22bc5..7e078fb 100644 --- a/advtrains/helpers.lua +++ b/advtrains/helpers.lua @@ -297,6 +297,7 @@ end function advtrains.get_adjacent_rail(this_posnr, this_conns_p, conn_idx, drives_on) local this_pos = advtrains.round_vector_floor_y(this_posnr) local this_conns = this_conns_p + local _ if not this_conns then _, this_conns = advtrains.get_rail_info_at(this_pos) end @@ -583,6 +584,7 @@ function advtrains.get_track_iterator(initial_pos, initial_connid, limit, follow table.insert(ti.branches, {pos = initial_pos, connid = coni, limit=limit}) end end + ti.limit = limit -- safeguard if someone adds a branch before calling anything setmetatable(ti, {__index=trackiter_mt}) return ti end diff --git a/advtrains_interlocking/database.lua b/advtrains_interlocking/database.lua index 38fad77..35afc8d 100644 --- a/advtrains_interlocking/database.lua +++ b/advtrains_interlocking/database.lua @@ -163,6 +163,9 @@ TCB data structure -- This is the "A" side of the TCB [1] = { -- Variant: with adjacent TCs. ts_id = -- ID of the assigned track section + xlink = -- If two sections of track are not physically joined but must function as one TS (e.g. knights move crossing), a bidirectional link can be added with exactly one other TCB. + -- TS search will behave as if these two TCBs were physically connected. + signal = -- optional: when set, routes can be set from this tcb/direction and signal -- aspect will be set accordingly. routeset = -- Route set from this signal. This is the entry that is cleared once @@ -249,17 +252,26 @@ function ildb.get_all_ts() return track_sections end --- Checks the consistency of the track section at the given position --- This method attempts to autorepair track sections if they are inconsistent +-- Checks the consistency of the track section at the given position, attempts to autorepair track sections if they are inconsistent +-- There are 2 operation modes: +-- 1: pos is NOT a TCB, tcb_connid MUST be nil +-- 2: pos is a TCB, tcb_connid MUST be given -- @param pos: the position to start from +-- @param tcb_connid: If provided node is a TCB, -- Returns: -- ts_id - the track section that was found -- nil - No track section exists -function ildb.check_and_repair_ts_at_pos(pos) - atdebug("check_and_repair_ts_at_pos", pos) +function ildb.check_and_repair_ts_at_pos(pos, tcb_connid) + atdebug("check_and_repair_ts_at_pos", pos, tcb_connid) + -- check prereqs + if ildb.get_tcb(pos) then + if not tcb_connid then error("check_and_repair_ts_at_pos: Startpoint is TCB, must provide tcb_connid!") end + else + if tcb_connid then error("check_and_repair_ts_at_pos: Startpoint is not TCB, must not provide tcb_connid!") end + end -- STEP 1: Ensure that only one section is at this place -- get all TCBs adjacent to this - local all_tcbs = ildb.get_all_tcbs_adjacent(pos, nil) + local all_tcbs = ildb.get_all_tcbs_adjacent(pos, tcb_connid) local first_ts = true local ts_id for _,sigd in ipairs(all_tcbs) do @@ -321,41 +333,63 @@ end -- This function does not trigger a repair. -- @param inipos: the initial position -- @param inidir: the initial direction, or nil to search in all directions +-- @param per_track_callback: A callback function called with signature (pos, connid, bconnid) for every track encountered -- Returns: a list of sigd's describing the TCBs found (sigd's point inward): -- {p=, s=, tcbs=} -function ildb.get_all_tcbs_adjacent(inipos, inidir) +function ildb.get_all_tcbs_adjacent(inipos, inidir, per_track_callback) atdebug("get_all_tcbs_adjacent: inipos",inipos,"inidir",inidir,"") local found_sigd = {} local ti = advtrains.get_track_iterator(inipos, inidir, TS_MAX_SCAN, true) + -- if initial start is TCBS and has xlink, need to add that to the TI + local inisi = {p=inipos, s=inidir}; + local initcbs = ildb.get_tcbs(inisi) + if initcbs then + ildb.validate_tcb_xlink(inisi, true) + if initcbs.xlink then + -- adding the tcb will happen when this branch is retrieved again using ti:next_branch() + atdebug("get_all_tcbs_adjacent: Putting xlink Branch for initial node",initcbs.xlink) + ti:add_branch(initcbs.xlink.p, initcbs.xlink.s) + end + end local pos, connid, bconnid, tcb while ti:has_next_branch() do pos, connid = ti:next_branch() --atdebug("get_all_tcbs_adjacent: BRANCH: ",pos, connid) bconnid = nil + is_branch_start = true repeat + -- callback + if per_track_callback then + per_track_callback(pos, connid, bconnid) + end tcb = ildb.get_tcb(pos) if tcb then + local using_connid = bconnid -- found a tcb - if not bconnid then - -- A branch start point cannot be a TCB, as that would imply that it was a turnout/crossing (illegal) - -- Only exception where this can happen is if the start point is a TCB, then we'd need to add the forward side of it to our list - if pos.x==inipos.x and pos.y==inipos.y and pos.z==inipos.z then - -- Note "connid" instead of "bconnid" - atdebug("get_all_tcbs_adjacent: Found Startpoint TCB: ",pos, connid, "ts=", tcb[connid].ts_id) - insert_sigd_if_not_present(found_sigd, {p=pos, s=connid, tcbs=tcb[connid]}) - else - -- this may not happend - error("Found TCB at TrackIterator new branch which is not the start point, this is illegal! pos="..minetest.pos_to_string(pos)) - end - - else - -- add the sigd of this tcb and a reference to the tcb table in it - atdebug("get_all_tcbs_adjacent: Found TCB: ",pos, bconnid, "ts=", tcb[bconnid].ts_id) - insert_sigd_if_not_present(found_sigd, {p=pos, s=bconnid, tcbs=tcb[bconnid]}) + if is_branch_start then + -- A branch cannot be a TCB, as that would imply that it was a turnout/crossing (illegal) + -- UNLESS: (a) it is the start point or (b) it was added via xlink + -- Then the correct conn to use is connid (pointing forward) + atdebug("get_all_tcbs_adjacent: Inserting TCB at branch start",pos, connid) + using_connid = connid + end + -- add the sigd of this tcb and a reference to the tcb table in it + atdebug("get_all_tcbs_adjacent: Found TCB: ",pos, using_connid, "ts=", tcb[using_connid].ts_id) + local si = {p=pos, s=using_connid, tcbs=tcb[using_connid]} + -- if xlink exists, add it now (only if we are not in branch start) + ildb.validate_tcb_xlink(si, true) + if not is_branch_start and si.tcbs.xlink then + -- adding the tcb will happen when this branch is retrieved again using ti:next_branch() + atdebug("get_all_tcbs_adjacent: Putting xlink Branch",si.tcbs.xlink) + ti:add_branch(si.tcbs.xlink.p, si.tcbs.xlink.s) + end + insert_sigd_if_not_present(found_sigd, si) + if not is_branch_start then break end end pos, connid, bconnid = ti:next_track() + is_branch_start = false --atdebug("get_all_tcbs_adjacent: TRACK: ",pos, connid, bconnid) until not pos end @@ -464,7 +498,7 @@ function ildb.tcbs_ensure_ts_ref_exists(sigd) local did_insert = insert_sigd_if_not_present(ts.tc_breaks, {p=sigd.p, s=sigd.s}) if did_insert then atdebug("tcbs_ensure_ts_ref_exists(",sigd,"): TCBS was missing reference in TS",tcbs.ts_id) - ildb.update_ts_cache(ts_id) + ildb.update_ts_cache(tcbs.ts_id) end end @@ -515,7 +549,7 @@ function ildb.update_ts_cache(ts_id) end local lntrans = { "A", "B" } -local function sigd_to_string(sigd) +function ildb.sigd_to_string(sigd) return minetest.pos_to_string(sigd.p).." / "..lntrans[sigd.s] end @@ -546,12 +580,98 @@ function ildb.remove_tcb_at(pos) if old_tcb[2].ts_id then ildb.purge_ts_tcb_refs(old_tcb[2].ts_id) end + -- update xlink partners + if old_tcb[1].xlink then + ildb.validate_tcb_xlink(old_tcb[1].xlink) + end + if old_tcb[2].xlink then + ildb.validate_tcb_xlink(old_tcb[2].xlink) + end advtrains.interlocking.remove_tcb_marker(pos) -- If needed, merge the track sections here ildb.check_and_repair_ts_at_pos(pos) return true end +-- Xlink: Connecting not-physically-connected sections handling + +-- Ensures that the xlink of this tcbs is bidirectional +function ildb.validate_tcb_xlink(sigd, suppress_repairs) + local tcbs = sigd.tcbs or ildb.get_tcbs(sigd) + local osigd = tcbs.xlink + if not osigd then return end + local otcbs = ildb.get_tcbs(tcbs.xlink) + if not otcbs then + atdebug("validate_tcb_xlink",sigd,": Link partner ",osigd,"orphaned") + tcbs.xlink = nil + if not suppress_repairs then + ildb.check_and_repair_ts_at_pos(sigd.p, sigd.s) + end + return + end + if otcbs.xlink then + if not vector.equals(otcbs.xlink.p, sigd.p) or otcbs.xlink.s~=sigd.s then + atdebug("validate_tcb_xlink",sigd,": Link partner ",osigd,"backreferencing to someone else (namely",otcbs.xlink,") clearing it") + tcbs.xlink = nil + if not suppress_repairs then + ildb.check_and_repair_ts_at_pos(sigd.p, sigd.s) + atdebug("validate_tcb_xlink",sigd,": Link partner ",osigd," was backreferencing to someone else, now updating that") + ildb.validate_tcb_xlink(osigd) + end + end + else + atdebug("validate_tcb_xlink",sigd,": Link partner ",osigd,"wasn't backreferencing, clearing it") + tcbs.xlink = nil + if not suppress_repairs then + ildb.check_and_repair_ts_at_pos(sigd.p, sigd.s) + end + end +end + +function ildb.add_tcb_xlink(sigd1, sigd2) + atdebug("add_tcb_xlink",sigd1, sigd2) + local tcbs1 = sigd1.tcbs or ildb.get_tcbs(sigd1) + local tcbs2 = sigd2.tcbs or ildb.get_tcbs(sigd2) + if vector.equals(sigd1.p, sigd2.p) then + atdebug("add_tcb_xlink Cannot xlink with same TCB") + return + end + if not tcbs1 or not tcbs2 then + atdebug("add_tcb_xlink TCBS doesnt exist") + return + end + if tcbs1.xlink or tcbs2.xlink then + atdebug("add_tcb_xlink One already linked") + return + end + -- apply link + tcbs1.xlink = {p=sigd2.p, s=sigd2.s} + tcbs2.xlink = {p=sigd1.p, s=sigd1.s} + -- update section. It should be sufficient to call update only once because the TCBs are linked anyway now + ildb.check_and_repair_ts_at_pos(sigd1.p, sigd1.s) +end + +function ildb.remove_tcb_xlink(sigd) + atdebug("remove_tcb_xlink",sigd) + -- Validate first. If Xlink is gone already then, nothing to do + ildb.validate_tcb_xlink(sigd) + -- Checking all of these already done by validate + local tcbs = sigd.tcbs or ildb.get_tcbs(sigd) + local osigd = tcbs.xlink + if not osigd then + -- validate already cleared us + atdebug("remove_tcb_xlink: Already gone by validate") + return + end + local otcbs = ildb.get_tcbs(tcbs.xlink) + -- clear it + otcbs.xlink = nil + tcbs.xlink = nil + -- Update section for ourself and the other one + ildb.check_and_repair_ts_at_pos(sigd.p, sigd.s) + ildb.check_and_repair_ts_at_pos(osigd.p, osigd.s) +end + function ildb.create_ts_from_tcbs(sigd) atdebug("create_ts_from_tcbs",sigd) local all_tcbs = ildb.get_all_tcbs_adjacent(sigd.p, sigd.s) diff --git a/advtrains_interlocking/tcb_ts_ui.lua b/advtrains_interlocking/tcb_ts_ui.lua index 97c28a8..08d1c32 100755 --- a/advtrains_interlocking/tcb_ts_ui.lua +++ b/advtrains_interlocking/tcb_ts_ui.lua @@ -2,6 +2,7 @@ local players_assign_tcb = {} local players_assign_signal = {} +local players_assign_xlink = {} local players_link_ts = {} local ildb = advtrains.interlocking.db @@ -166,6 +167,7 @@ minetest.register_on_punchnode(function(pos, node, player, pointed_thing) meta:set_string("tcb_pos", minetest.pos_to_string(pos)) meta:set_string("infotext", "TCB assigned to "..minetest.pos_to_string(pos)) minetest.chat_send_player(pname, "Configuring TCB: Successfully configured TCB") + advtrains.interlocking.show_tcb_marker(pos) else minetest.chat_send_player(pname, "Configuring TCB: This is not a normal two-connection rail! Aborted.") end @@ -213,9 +215,14 @@ end) -- TCB Form -local function mktcbformspec(tcbs, btnpref, offset, pname) +local function mktcbformspec(pos, side, tcbs, offset, pname) local form = "" + local btnpref = side==1 and "A" or "B" local ts + -- ensure that mapping and xlink are up to date + ildb.tcbs_ensure_ts_ref_exists({p=pos, s=side, tcbs=tcbs}) + ildb.validate_tcb_xlink({p=pos, s=side, tcbs=tcbs}) + -- Note: repair operations may have been triggered by this if tcbs.ts_id then ts = ildb.get_ts(tcbs.ts_id) end @@ -227,6 +234,19 @@ local function mktcbformspec(tcbs, btnpref, offset, pname) form = form.."label[0.5,"..offset..";Side "..btnpref..": ".."End of interlocking]" form = form.."button[0.5,"..(offset+0.5)..";5,1;"..btnpref.."_makeil;Create Interlocked Track Section]" end + -- xlink + if tcbs.xlink then + form = form.."label[0.5,"..(offset+1.5)..";Link:"..ildb.sigd_to_string(tcbs.xlink).."]" + form = form.."button[4.5,"..(offset+1.5)..";1,1;"..btnpref.."_xlinkdel;X]" + else + if players_assign_xlink[pname] then + form = form.."button[0.5,"..(offset+1.5)..";4,1;"..btnpref.."_xlinklink;Link "..ildb.sigd_to_string(players_assign_xlink[pname]).."]" + form = form.."button[4.5,"..(offset+1.5)..";1,1;"..btnpref.."_xlinkabrt;X]" + else + form = form.."label[0.5,"..(offset+1.5)..";No Link]" + form = form.."button[4.5,"..(offset+1.5)..";1,1;"..btnpref.."_xlinkadd;+]" + end + end if tcbs.signal then form = form.."button[0.5,"..(offset+2.5)..";5,1;"..btnpref.."_sigdia;Signalling]" else @@ -245,8 +265,8 @@ function advtrains.interlocking.show_tcb_form(pos, pname) if not tcb then return end local form = "size[6,9] label[0.5,0.5;Track Circuit Break Configuration]" - form = form .. mktcbformspec(tcb[1], "A", 1, pname) - form = form .. mktcbformspec(tcb[2], "B", 5, pname) + form = form .. mktcbformspec(pos, 1, tcb[1], 1, pname) + form = form .. mktcbformspec(pos, 2, tcb[2], 5, pname) minetest.show_formspec(pname, "at_il_tcbconfig_"..minetest.pos_to_string(pos), form) advtrains.interlocking.show_tcb_marker(pos) @@ -276,6 +296,10 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) local f_makeil = {fields.A_makeil, fields.B_makeil} local f_asnsig = {fields.A_asnsig, fields.B_asnsig} local f_sigdia = {fields.A_sigdia, fields.B_sigdia} + local f_xlinkadd = {fields.A_xlinkadd, fields.B_xlinkadd} + local f_xlinkdel = {fields.A_xlinkdel, fields.B_xlinkdel} + local f_xlinklink = {fields.A_xlinklink, fields.B_xlinklink} + local f_xlinkabrt = {fields.A_xlinkabrt, fields.B_xlinkabrt} for connid=1,2 do local tcbs = tcb[connid] @@ -291,6 +315,28 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) end end end + if tcbs.xlink then + if f_xlinkdel[connid] then + ildb.remove_tcb_xlink({p=pos, s=connid}) + end + else + local osigd = players_assign_xlink[pname] + if osigd then + if f_xlinklink[connid] then + ildb.add_tcb_xlink({p=pos, s=connid}, osigd) + players_assign_xlink[pname] = nil + elseif f_xlinkabrt[connid] then + players_assign_xlink[pname] = nil + end + else + if f_xlinkadd[connid] then + players_assign_xlink[pname] = {p=pos, s=connid} + minetest.chat_send_player(pname, "TCB Link: Select linked TCB now!") + minetest.close_formspec(pname, formname) + return -- to not reopen form + end + end + end if f_asnsig[connid] and not tcbs.signal then minetest.chat_send_player(pname, "Configuring TCB: Please punch the signal to assign.") players_assign_signal[pname] = {p=pos, s=connid} @@ -496,36 +542,25 @@ function advtrains.interlocking.remove_tcb_marker(pos) markerent[pts] = nil end +local ts_showparticles_callback = function(pos, connid, bconnid) + minetest.add_particle({ + pos = pos, + velocity = {x=0, y=0, z=0}, + acceleration = {x=0, y=0, z=0}, + expirationtime = 10, + size = 7, + vertical = true, + texture = "at_il_ts_highlight_particle.png", + glow = 6, + }) +end + -- Spawns particles to highlight the clicked track section -- TODO: Adapt behavior to not dumb-walk anymore function advtrains.interlocking.highlight_track_section(pos) - local ti = advtrains.get_track_iterator(pos, nil, 100, true) - local pos, connid, bconnid, tcb - while ti:has_next_branch() do - pos, connid = ti:next_branch() - --atdebug("highlight_track_section: BRANCH: ",pos, connid) - bconnid = nil - repeat - -- spawn particles - minetest.add_particle({ - pos = pos, - velocity = {x=0, y=0, z=0}, - acceleration = {x=0, y=0, z=0}, - expirationtime = 10, - size = 7, - vertical = true, - texture = "at_il_ts_highlight_particle.png", - glow = 6, - }) - -- abort if TCB is found - tcb = ildb.get_tcb(pos) - if tcb then - advtrains.interlocking.show_tcb_marker(pos) - break - end - pos, connid, bconnid = ti:next_track() - --atdebug("highlight_track_section: TRACK: ",pos, connid, bconnid) - until not pos + local all_tcbs = ildb.get_all_tcbs_adjacent(pos, nil, ts_showparticles_callback) + for _,sigd in ipairs(all_tcbs) do + advtrains.interlocking.show_tcb_marker(sigd.p) end end diff --git a/advtrains_interlocking/tool.lua b/advtrains_interlocking/tool.lua index 4b701b4..6723f88 100644 --- a/advtrains_interlocking/tool.lua +++ b/advtrains_interlocking/tool.lua @@ -26,7 +26,11 @@ local function node_right_click(pos, pname) local node_ok, conns, rail_y=advtrains.get_rail_info_at(pos) if not node_ok then minetest.chat_send_player(pname, "Node is not a track!") - return + return + end + if advtrains.interlocking.db.get_tcb(pos) then + advtrains.interlocking.show_tcb_form(pos, pname) + return end local ts_id = advtrains.interlocking.db.check_and_repair_ts_at_pos(pos) @@ -41,7 +45,12 @@ local function node_left_click(pos, pname) local node_ok, conns, rail_y=advtrains.get_rail_info_at(pos) if not node_ok then minetest.chat_send_player(pname, "Node is not a track!") - return + return + end + + if advtrains.interlocking.db.get_tcb(pos) then + advtrains.interlocking.show_tcb_marker(pos) + return end local ts_id = advtrains.interlocking.db.check_and_repair_ts_at_pos(pos) -- 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/helpers.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 2067ab0a90550b2fc513e032d4c4e54dd11a2290 Mon Sep 17 00:00:00 2001 From: orwell Date: Sun, 15 Oct 2023 15:48:59 +0200 Subject: Use conn_map in the path calculation --- advtrains/debugitems.lua | 19 +++++++++++++++++++ advtrains/helpers.lua | 41 +++++++++++++++++++++++++++-------------- advtrains/nodedb.lua | 4 ++-- advtrains/passive.lua | 2 ++ advtrains/path.lua | 28 +++++++++++----------------- advtrains/track_reg_helper.lua | 6 +++--- advtrains/tracks.lua | 4 +++- 7 files changed, 67 insertions(+), 37 deletions(-) (limited to 'advtrains/helpers.lua') diff --git a/advtrains/debugitems.lua b/advtrains/debugitems.lua index a59efc7..2236cba 100644 --- a/advtrains/debugitems.lua +++ b/advtrains/debugitems.lua @@ -118,3 +118,22 @@ minetest.register_tool("advtrains:trackitest", end, } ) + +minetest.register_chatcommand("at_trackdef_audit", + { + params = "", + description = "Performs an audit of all track definitions currently loaded and checks for potential problems", + func = function(name, param) + for name, ndef in pairs(minetest.registered_nodes) do + --TODO finish this! + if ndef.at_conns then + -- check if conn_map is there and if it has enough entries + if #ndef.at_conns > 2 then + if #ndef.at_conn_map < #ndef.at_conns then + atwarn("AUDIT: Node",name,"- Not enough connmap entries! Check ndef:",ndef) + end + end + end + end + end, +}) diff --git a/advtrains/helpers.lua b/advtrains/helpers.lua index 7a774d9..d7e691d 100644 --- a/advtrains/helpers.lua +++ b/advtrains/helpers.lua @@ -292,7 +292,7 @@ function advtrains.conn_matches_to(conn, other_conns) 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: , , , +-- returns: , , , , (adjacent conns table), (adjacent connmap table) -- 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) local this_pos = advtrains.round_vector_floor_y(this_posnr) @@ -303,8 +303,8 @@ function advtrains.get_adjacent_rail(this_posnr, this_conns_p, conn_idx) end if not conn_idx then for coni, _ in ipairs(this_conns) do - local adj_pos, adj_conn_idx, _, nry, nco = advtrains.get_adjacent_rail(this_pos, this_conns, coni) - if adj_pos then return adj_pos,adj_conn_idx,coni,nry, nco end + local adj_pos, adj_conn_idx, _, nry, nco, ncm = advtrains.get_adjacent_rail(this_pos, this_conns, coni) + if adj_pos then return adj_pos,adj_conn_idx,coni,nry, nco, ncm end end return nil end @@ -318,29 +318,40 @@ function advtrains.get_adjacent_rail(this_posnr, this_conns_p, conn_idx) adj_pos.y = adj_pos.y + 1 end - local nextnode_ok, nextconns, nextrail_y=advtrains.get_rail_info_at(adj_pos) + local nextnode_ok, nextconns, nextrail_y, nextconnmap=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) + nextnode_ok, nextconns, nextrail_y, nextconnmap=advtrains.get_rail_info_at(adj_pos) if not nextnode_ok then return nil end end local adj_connid = advtrains.conn_matches_to({c=conn.c, y=conn_y}, nextconns) if adj_connid then - return adj_pos, adj_connid, conn_idx, nextrail_y, nextconns + return adj_pos, adj_connid, conn_idx, nextrail_y, nextconns, nextconnmap end return nil end -- when a train enters a rail on connid 'conn', which connid will it go out? --- nconns: number of connections in connection table: --- 2 = straight rail; 3 = turnout, 4 = crossing, 5 = three-way turnout (5th entry is a stub) +-- Since 2.5: This mapping is contained in the conn_map table in the node definition! -- returns: connid_out -local connlku={[2]={2,1}, [3]={2,1,1}, [4]={2,1,4,3}, [5]={2,1,1,1}} -function advtrains.get_matching_conn(conn, nconns) - return connlku[nconns][conn] +function advtrains.get_matching_conn(conn, conn_map) + if tonumber(conn_map) then + error("Legacy call to get_matching_conn! Instead of nconns, conn_map needs to be provided!") + end + if not conn_map then + --OK for two-conn rails, just return the other + if conn==1 then return 2 end + if conn==2 then return 1 end + error("get_matching_conn: For connid >=3, conn_map must not be nil!") + end + local cout = conn_map[conn] + if not cout then + error("get_matching_conn: Connid "..conn.." not found in conn_map which is "..atdump(conn_map)) + end + return cout end function advtrains.random_id(lenp) @@ -495,10 +506,11 @@ local trackiter_mt = { next_branch = function(self) local br = table.remove(self.branches, 1) -- Advance internal state - local adj_pos, adj_connid, _, _, adj_conns = advtrains.get_adjacent_rail(br.pos, nil, br.connid) + local adj_pos, adj_connid, _, _, adj_conns, adj_connmap = advtrains.get_adjacent_rail(br.pos, nil, br.connid) self.pos = adj_pos self.bconnid = adj_connid self.tconns = adj_conns + self.tconnmap = adj_connmap self.limit = br.limit - 1 self.visited[advtrains.encode_pos(br.pos)] = true self.last_track_already_visited = false @@ -524,7 +536,7 @@ local trackiter_mt = { end -- select next conn (main conn to follow is the associated connection) local old_bconnid = self.bconnid - local mconnid = advtrains.get_matching_conn(self.bconnid, #self.tconns) + local mconnid = advtrains.get_matching_conn(self.bconnid, self.tconnmap) if self.visited[advtrains.encode_pos(pos)] then -- node was already seen -- Due to special requirements for the track section updater, return this first already visited track once @@ -540,10 +552,11 @@ local trackiter_mt = { end end -- Advance internal state - local adj_pos, adj_connid, _, _, adj_conns = advtrains.get_adjacent_rail(pos, self.tconns, mconnid) + local adj_pos, adj_connid, _, _, adj_conns, adj_connmap = advtrains.get_adjacent_rail(pos, self.tconns, mconnid) self.pos = adj_pos self.bconnid = adj_connid self.tconns = adj_conns + self.tconnmap = adj_connmap self.limit = self.limit - 1 self.visited[advtrains.encode_pos(pos)] = true self.last_track_already_visited = false diff --git a/advtrains/nodedb.lua b/advtrains/nodedb.lua index 39106b2..d903be7 100644 --- a/advtrains/nodedb.lua +++ b/advtrains/nodedb.lua @@ -278,9 +278,9 @@ function advtrains.get_rail_info_at(pos) if(not advtrains.is_track(nodename)) then return false end - local conns, railheight = advtrains.get_track_connections(node.name, node.param2) + local conns, railheight, connmap = advtrains.get_track_connections(node.name, node.param2) - return true, conns, railheight + return true, conns, railheight, connmap end local IGNORE_WORLD = advtrains.IGNORE_WORLD diff --git a/advtrains/passive.lua b/advtrains/passive.lua index 76da720..741d7df 100644 --- a/advtrains/passive.lua +++ b/advtrains/passive.lua @@ -68,6 +68,8 @@ function advtrains.setstate(parpos, newstate, pnode) if ndef.advtrains.node_on_switch_state then ndef.advtrains.node_on_switch_state(pos, new_node, old_state, newstate) end + -- invalidate paths (only relevant if this is a track) + advtrains.invalidate_all_paths(pos) return true end diff --git a/advtrains/path.lua b/advtrains/path.lua index 72ee05d..588140d 100644 --- a/advtrains/path.lua +++ b/advtrains/path.lua @@ -38,11 +38,11 @@ -- 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) + local node_ok, conns, rhe, connmap = advtrains.get_rail_info_at(pos) if not node_ok then return node_ok end - local mconnid = advtrains.get_matching_conn(connid, #conns) + local mconnid = advtrains.get_matching_conn(connid, connmap) train.index = rel_index train.path = { [0] = { x=posr.x, y=posr.y+rhe, z=posr.z } } train.path_cn = { [0] = connid } @@ -206,22 +206,19 @@ function advtrains.path_get(train, index) while index > pef do local pos = train.path[pef] local connid = train.path_cn[pef] - local node_ok, this_conns, adj_pos, adj_connid, conn_idx, nextrail_y, next_conns + local node_ok, this_conns, adj_pos, adj_connid, conn_idx, nextrail_y, next_conns, next_connmap 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) + adj_pos, adj_connid, conn_idx, nextrail_y, next_conns, next_connmap = advtrains.get_adjacent_rail(pos, this_conns, connid) end pef = pef + 1 if adj_pos then advtrains.occ.set_item(train.id, adj_pos, pef) - + + local mconnid = advtrains.get_matching_conn(adj_connid, next_connmap) -- If we have split points, notify accordingly - local mconnid = advtrains.get_matching_conn(adj_connid, #next_conns) - if #next_conns==3 and adj_connid==1 and train.points_split and train.points_split[advtrains.encode_pos(adj_pos)] then - --atdebug(id,"has split points restored at",adj_pos) - mconnid = 3 - end + -- TODO readd support for split points (remember the cp and cn of points) adj_pos.y = adj_pos.y + nextrail_y train.path_cp[pef] = adj_connid @@ -245,22 +242,19 @@ function advtrains.path_get(train, index) while index < peb do local pos = train.path[peb] local connid = train.path_cp[peb] - local node_ok, this_conns, adj_pos, adj_connid, conn_idx, nextrail_y, next_conns + local node_ok, this_conns, adj_pos, adj_connid, conn_idx, nextrail_y, next_conns, next_connmap 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) + adj_pos, adj_connid, conn_idx, nextrail_y, next_conns, next_connmap = advtrains.get_adjacent_rail(pos, this_conns, connid) end peb = peb - 1 if adj_pos then advtrains.occ.set_item(train.id, adj_pos, peb) + local mconnid = advtrains.get_matching_conn(adj_connid, next_connmap) -- If we have split points, notify accordingly - local mconnid = advtrains.get_matching_conn(adj_connid, #next_conns) - if #next_conns==3 and adj_connid==1 and train.points_split and train.points_split[advtrains.encode_pos(adj_pos)] then - -- atdebug(id,"has split points restored at",adj_pos) - mconnid = 3 - end + -- TODO readd support for split points (remember the cp and cn of points) adj_pos.y = adj_pos.y + nextrail_y train.path_cn[peb] = adj_connid diff --git a/advtrains/track_reg_helper.lua b/advtrains/track_reg_helper.lua index ad73a40..dbb2d53 100644 --- a/advtrains/track_reg_helper.lua +++ b/advtrains/track_reg_helper.lua @@ -129,7 +129,7 @@ advtrains.ap.t_s3way={ switchalt = "s", switchst="l", switchprefix = "", - conn_map = {2,1,1}, + conn_map = {2,1,1,1}, stmref = "sw", }, s={ @@ -138,7 +138,7 @@ advtrains.ap.t_s3way={ switchalt ="r", switchst = "s", switchprefix = "", - conn_map = {3,1,1}, + conn_map = {3,1,1,1}, stmref = "sw", tpsingle = true, }, @@ -148,7 +148,7 @@ advtrains.ap.t_s3way={ switchalt = "l", switchst="r", switchprefix = "", - conn_map = {4,1,1}, + conn_map = {4,1,1,1}, stmref = "sw", } }, diff --git a/advtrains/tracks.lua b/advtrains/tracks.lua index 1abef59..661da8a 100644 --- a/advtrains/tracks.lua +++ b/advtrains/tracks.lua @@ -235,6 +235,8 @@ function advtrains.is_track(nodename) return false end +-- returns the connection tables of the track with given node details +-- returns: conns table, railheight, conn_map table 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 @@ -246,7 +248,7 @@ function advtrains.get_track_connections(name, param2) 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 + return advtrains.rotate_conn_by(nodedef.at_conns, noderot*AT_CMAX/4), (nodedef.at_rail_y or 0), nodedef.at_conn_map end -- Function called when a track is about to be dug or modified by the trackworker -- cgit v1.2.3 From 2bfc24ef27ea4098a93cc025b513afe1b8c4ee1f Mon Sep 17 00:00:00 2001 From: orwell Date: Mon, 25 Nov 2024 23:39:56 +0100 Subject: Auto-assign signal to TCB if it finds one ahead of the influence point --- advtrains/helpers.lua | 2 +- advtrains_interlocking/signal_aspect_ui.lua | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) (limited to 'advtrains/helpers.lua') diff --git a/advtrains/helpers.lua b/advtrains/helpers.lua index d7e691d..172cf07 100644 --- a/advtrains/helpers.lua +++ b/advtrains/helpers.lua @@ -578,7 +578,7 @@ local trackiter_mt = { -- initial_pos: the initial track position of the track iterator -- initial_connid: the connection index in which to traverse. If nil, adds a "branch" for every connection of the track (traverse in all directions) -- limit: maximum distance from the start point after which the traverser stops --- follow_all: if true, follows all branches at multi-connection tracks, even the ones pointing backwards or the crossing track on crossings. If false, follows only switches in driving direction. +-- follow_all: NOT IMPLEMENTED (supposed: if true, follows all branches at multi-connection tracks, even the ones pointing backwards or the crossing track on crossings. If false, follows only switches in driving direction.) -- Functions of the returned TrackIterator can be called via the Lua : notation, such as ti:next_track() -- If only the main track needs to be followed, use only the ti:next_track() function and do not call ti:next_branch(). diff --git a/advtrains_interlocking/signal_aspect_ui.lua b/advtrains_interlocking/signal_aspect_ui.lua index 39aab17..9f2675f 100644 --- a/advtrains_interlocking/signal_aspect_ui.lua +++ b/advtrains_interlocking/signal_aspect_ui.lua @@ -186,6 +186,30 @@ function advtrains.interlocking.init_distant_assign(pos, pname) players_assign_distant[pname] = pos end +local function try_auto_assign_to_tcb(signalpos, pos, connid, pname) + local ti = advtrains.get_track_iterator(pos, connid, 6, false) -- maximum 6 track nodes ahead + local apos, aconnid = ti:next_branch() + while apos do + -- check for presence of a tcb + local tcb = advtrains.interlocking.db.get_tcb(apos) + if tcb then + -- check on the pointing connid whether it has a signal already + if not tcb[aconnid].signal then + -- go ahead and assign + local sigd = { p=apos, s=aconnid } + advtrains.interlocking.db.assign_signal_to_tcbs(signalpos, sigd) + 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) + end + -- in all cases return + return + end + apos, aconnid = ti:next_track() + end + -- if we end up here limit is up +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 @@ -207,6 +231,8 @@ minetest.register_on_punchnode(function(pos, node, player, pointed_thing) advtrains.interlocking.db.set_ip_signal(pts, plconnid, signalpos) ipmarker(pos, plconnid) minetest.chat_send_player(pname, "Configuring Signal: Successfully set influence point") + -- Try to find a TCB ahead and auto assign this signal there + try_auto_assign_to_tcb(signalpos, pos, plconnid, pname) else minetest.chat_send_player(pname, "Configuring Signal: Influence point of another signal is already present!") end -- cgit v1.2.3 From 5546dca7825127152558b2982ce47a219456eb34 Mon Sep 17 00:00:00 2001 From: orwell Date: Mon, 24 Mar 2025 22:00:17 +0100 Subject: is_node_loaded: use new function compare_block_status() instead of removed is_block_active() --- advtrains/helpers.lua | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'advtrains/helpers.lua') diff --git a/advtrains/helpers.lua b/advtrains/helpers.lua index 172cf07..e58625f 100644 --- a/advtrains/helpers.lua +++ b/advtrains/helpers.lua @@ -474,7 +474,13 @@ end local active_node_range = tonumber(minetest.settings:get("active_block_range"))*16 + 16 -- Function to check whether node at position(pos) is "loaded"/"active" -- That is, whether it is within the active_block_range to a player -if minetest.is_block_active then -- define function differently whether minetest.is_block_active is available or not +if core.compare_block_status then + -- latest API + function advtrains.is_node_loaded(pos) + return core.compare_block_status(pos, "active") + end +elseif minetest.is_block_active then -- define function differently whether minetest.is_block_active is available or not + -- API added by my PR but later superseded by the above and now removed advtrains.is_node_loaded = minetest.is_block_active else function advtrains.is_node_loaded(pos) -- cgit v1.2.3