From 2d7640d424c3d7d558ed0b81b8d98fd306562d11 Mon Sep 17 00:00:00 2001 From: orwell96 Date: Sat, 24 Jun 2023 14:37:52 +0200 Subject: Occupation system: store multiple indices for the same train, introduce reverse_lookup_sel() to select appropriate index out of multiple based on a heuristic --- advtrains/trainlogic.lua | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'advtrains/trainlogic.lua') diff --git a/advtrains/trainlogic.lua b/advtrains/trainlogic.lua index 288e224..f136577 100644 --- a/advtrains/trainlogic.lua +++ b/advtrains/trainlogic.lua @@ -619,7 +619,7 @@ function advtrains.train_step_b(id, train, dtime) local base_cn = train.path_cn[base_idx] --atdebug(id,"Begin Checking for on-track collisions new_idx=",new_index_curr_tv,"base_idx=",base_idx,"base_pos=",base_pos,"base_cn=",base_cn) -- query occupation - local occ = advtrains.occ.get_trains_over(base_pos) + local occ = advtrains.occ.reverse_lookup_sel(base_pos, "close_proximity") -- iterate other trains for otid, ob_idx in pairs(occ) do if otid ~= id then @@ -649,7 +649,7 @@ function advtrains.train_step_b(id, train, dtime) -- Phase 2 - project ref_index back onto our path and check again (necessary because there might be a turnout on the way and we are driving into the flank if target_is_inside then - local our_index = advtrains.path_project(otrn, ref_index, id) + local our_index = advtrains.path_project(otrn, ref_index, id, "before_end") --atdebug("Backprojected our_index",our_index) if our_index and our_index <= new_index_curr_tv and our_index >= train.index then --FIX: If train was already past the collision point in the previous step, there is no collision! Fixes bug with split_at_index @@ -1218,7 +1218,6 @@ function advtrains.invert_train(train_id) advtrains.update_trainpart_properties(train_id, true) -- recalculate path - advtrains.train_ensure_init(train_id, train) -- If interlocking present, check whether this train is in a section and then set as shunt move after reversion if advtrains.interlocking and train.il_sections and #train.il_sections > 0 then @@ -1244,7 +1243,7 @@ function advtrains.invalidate_all_paths(pos) local tab if pos then -- if position given, check occupation system - tab = advtrains.occ.get_trains_over(pos) + tab = advtrains.occ.reverse_lookup_quick(pos) else tab = advtrains.trains end @@ -1257,7 +1256,7 @@ end -- Calls invalidate_path_ahead on all trains occupying (having paths over) this node -- Can be called during train step. function advtrains.invalidate_all_paths_ahead(pos) - local tab = advtrains.occ.get_trains_over(pos) + local tab = advtrains.occ.reverse_lookup_sel(pos, "first_ahead") for id,index in pairs(tab) do local train = advtrains.trains[id] -- cgit v1.2.3 From 6d3c5a5f38d66a74c7b2bc219ee16f258ba50a2b Mon Sep 17 00:00:00 2001 From: 1F616EMO Date: Tue, 22 Oct 2024 06:59:16 +0800 Subject: Wagon iterator, lookup by id, and use them in code --- advtrains/trainlogic.lua | 19 +++------ advtrains/wagons.lua | 108 ++++++++++++++++++++++++++++++----------------- 2 files changed, 75 insertions(+), 52 deletions(-) (limited to 'advtrains/trainlogic.lua') diff --git a/advtrains/trainlogic.lua b/advtrains/trainlogic.lua index f136577..ce646da 100644 --- a/advtrains/trainlogic.lua +++ b/advtrains/trainlogic.lua @@ -142,13 +142,8 @@ minetest.register_on_joinplayer(function(player) local pname = player:get_player_name() local id=advtrains.player_to_train_mapping[pname] if id then - for _,wagon in pairs(minetest.luaentities) do - if wagon.is_wagon and wagon.initialized and wagon.id then - local wdata = advtrains.wagons[wagon.id] - if wdata and wdata.train_id == id then - wagon:reattach_all() - end - end + for _, wagon in advtrains.wagon_entity_pairs_in_train(id) do + wagon:reattach_all() end end end) @@ -160,12 +155,10 @@ minetest.register_on_dieplayer(function(player) if id then local train=advtrains.trains[id] if not train then advtrains.player_to_train_mapping[pname]=nil return end - for _,wagon in pairs(minetest.luaentities) do - if wagon.is_wagon and wagon.initialized and wagon.train_id==id then - --when player dies, detach him from the train - --call get_off_plr on every wagon since we don't know which one he's on. - wagon:get_off_plr(pname) - end + for _, wagon in advtrains.wagon_entity_pairs_in_train(id) do + --when player dies, detach him from the train + --call get_off_plr on every wagon since we don't know which one he's on. + wagon:get_off_plr(pname) end -- just in case no wagon felt responsible for this player: clear train mapping advtrains.player_to_train_mapping[pname] = nil diff --git a/advtrains/wagons.lua b/advtrains/wagons.lua index 993d7d3..ddff2f5 100644 --- a/advtrains/wagons.lua +++ b/advtrains/wagons.lua @@ -1103,12 +1103,11 @@ function wagon:handle_bordcom_fields(pname, formname, fields) for i, tpid in ipairs(train.trainparts) do if fields["dcpl_"..i] then advtrains.safe_decouple_wagon(tpid, pname) - elseif fields["wgprp"..i] then - for _,wagon in pairs(minetest.luaentities) do - if wagon.is_wagon and wagon.initialized and wagon.id==tpid and data.owner==pname then - wagon:show_wagon_properties(pname) - return - end + elseif fields["wgprp"..i] and data.owner==pname then + local wagon = advtrains.get_wagon_entity(tpid) + if wagon then + wagon:show_wagon_properties(pname) + return end end end @@ -1154,44 +1153,48 @@ end minetest.register_on_player_receive_fields(function(player, formname, fields) local uid=string.match(formname, "^advtrains_geton_(.+)$") if uid then - for _,wagon in pairs(minetest.luaentities) do - if wagon.is_wagon and wagon.initialized and wagon.id==uid then - local data = advtrains.wagons[wagon.id] - if fields.inv then - if wagon.has_inventory and wagon.get_inventory_formspec then - minetest.show_formspec(player:get_player_name(), "advtrains_inv_"..uid, wagon:get_inventory_formspec(player:get_player_name(), make_inv_name(uid))) - end - elseif fields.seat then - local val=minetest.explode_textlist_event(fields.seat) - if val and val.type~="INV" and not data.seatp[player:get_player_name()] then - --get on - wagon:get_on(player, val.index) - --will work with the new close_formspec functionality. close exactly this formspec. - minetest.show_formspec(player:get_player_name(), formname, "") - end + local wagon = advtrains.get_wagon_entity(uid) + if wagon then + local data = advtrains.wagons[wagon.id] + if fields.inv then + if wagon.has_inventory and wagon.get_inventory_formspec then + minetest.show_formspec(player:get_player_name(), "advtrains_inv_"..uid, wagon:get_inventory_formspec(player:get_player_name(), make_inv_name(uid))) + end + elseif fields.seat then + local val=minetest.explode_textlist_event(fields.seat) + if val and val.type~="INV" and not data.seatp[player:get_player_name()] then + --get on + wagon:get_on(player, val.index) + --will work with the new close_formspec functionality. close exactly this formspec. + minetest.show_formspec(player:get_player_name(), formname, "") end end end + return true end + uid=string.match(formname, "^advtrains_seating_(.+)$") if uid then - for _,wagon in pairs(minetest.luaentities) do - if wagon.is_wagon and wagon.initialized and wagon.id==uid then - local pname=player:get_player_name() - local no=wagon:get_seatno(pname) - if no then - if wagon.seat_groups then - wagon:seating_from_key_helper(pname, fields, no) - end + local wagon = advtrains.get_wagon_entity(uid) + if wagon then + local pname=player:get_player_name() + local no=wagon:get_seatno(pname) + if no then + if wagon.seat_groups then + wagon:seating_from_key_helper(pname, fields, no) end end end + return true end + uid=string.match(formname, "^advtrains_prop_(.+)$") if uid then local pname=player:get_player_name() local data = advtrains.wagons[uid] - if pname~=data.owner and not minetest.check_player_privs(pname, {train_admin = true}) then + if not data then + return true + elseif pname~=data.owner and not minetest.check_player_privs(pname, {train_admin = true}) then return true end if fields.save or not fields.quit then @@ -1213,29 +1216,32 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) wagon.show_wagon_properties({id=uid}, pname) end end + return true end uid=string.match(formname, "^advtrains_bordcom_(.+)$") if uid then - for _,wagon in pairs(minetest.luaentities) do - if wagon.is_wagon and wagon.initialized and wagon.id==uid then - wagon:handle_bordcom_fields(player:get_player_name(), formname, fields) - end + local wagon = advtrains.get_wagon_entity(uid) + if wagon then + wagon:handle_bordcom_fields(player:get_player_name(), formname, fields) end + return true end + uid=string.match(formname, "^advtrains_inv_(.+)$") if uid then local pname=player:get_player_name() local data = advtrains.wagons[uid] if fields.prop and data.owner==pname then - for _,wagon in pairs(minetest.luaentities) do - if wagon.is_wagon and wagon.initialized and wagon.id==uid and data.owner==pname then - wagon:show_wagon_properties(pname) - --wagon:handle_bordcom_fields(player:get_player_name(), formname, fields) - end + local wagon = advtrains.get_wagon_entity(uid) + if wagon then + wagon:show_wagon_properties(pname) + --wagon:handle_bordcom_fields(player:get_player_name(), formname, fields) end end + return true end end) + function wagon:seating_from_key_helper(pname, fields, no) local data = advtrains.wagons[self.id] local sgr=self.seats[no].group @@ -1507,3 +1513,27 @@ function advtrains.get_wagon_at_index(train_id, w_index) -- nothing found, dist must be further back return nil end + +function advtrains.get_wagon_entity(wagon_id) + if not advtrains.wagons[wagon_id] then return end + local object = advtrains.wagon_objects[wagon_id] + if object then + return object:get_luaentity() + end +end + +function advtrains.next_wagon_entity_in_train(train, i) + local wagon_id = train.trainparts[i + 1] + if wagon_id then + local wagon = advtrains.get_wagon_entity(wagon_id) + if wagon then + return i + 1, wagon + end + end +end + +function advtrains.wagon_entity_pairs_in_train(train_id) + local train = advtrains.trains[train_id] + if not train then return function() end end + return advtrains.next_wagon_entity_in_train, train, 0 +end -- cgit v1.2.3 From 86e80e4cfb0d7017ff962e40ab99a4ab6addf8f2 Mon Sep 17 00:00:00 2001 From: 1F616EMO Date: Sat, 14 Sep 2024 07:36:45 +0800 Subject: Fix crashing when train hitting objects without armor group --- advtrains/trainlogic.lua | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'advtrains/trainlogic.lua') diff --git a/advtrains/trainlogic.lua b/advtrains/trainlogic.lua index ce646da..796aae9 100644 --- a/advtrains/trainlogic.lua +++ b/advtrains/trainlogic.lua @@ -796,9 +796,12 @@ function advtrains.train_step_c(id, train, dtime) if is_loaded_area then local objs = minetest.get_objects_inside_radius(rcollpos, 2) for _,obj in ipairs(objs) do - if not obj:is_player() and obj:get_armor_groups().fleshy and obj:get_armor_groups().fleshy > 0 - and obj:get_luaentity() and obj:get_luaentity().name~="signs_lib:text" then - obj:punch(obj, 1, { full_punch_interval = 1.0, damage_groups = {fleshy = 1000}, }, nil) + if not obj:is_player() then + local armor = obj:get_armor_groups() + local luaentity = obj:get_luaentity() + if armor.fleshy and armor.fleshy > 0 and luaentity and luaentity.name ~= "signs_lib:text" then + obj:punch(obj, 1, { full_punch_interval = 1.0, damage_groups = {fleshy = 1000}, }, nil) + end end end end -- cgit v1.2.3 From 66868e2eefe97b7a6d35fa3fecc895b9c7bbc4cc Mon Sep 17 00:00:00 2001 From: "Y. Wang" Date: Thu, 19 Sep 2024 18:39:19 +0000 Subject: Address wagon aliasing issues As it turns out, not fully testing new features is not necessarily a good idea ... This patch follows up 1F616EMO's patch by * Making get_wagon_prototype return the resolved alias, * Handling recursive wagon alises (in particular, loops), and * Adding (partial) unittest for the wagon aliasing system. [v2]: The testcases are complemented a bit more to cover situations where the alias resolution system should return nil. [v2]: This patch should hopefully also warn about not spawning wagons. Note that this only warns about the missing wagon entity and does _not_ actually fix the issue. How to test: * In a world with both advtrains_train_subway and advtrains_train_japan enabled, place a subway wagon, a Japanese engine, and a regular Japanese wagon. * Add the test mod to the world; do NOT remove advtrains_train_japan. * Restart the world. Notice that the Japanese wagons still appear as Japanese wagons despite being aliased to subway wagons. * Restart the world without the advtrains_train_japan mod. Notice that the engine appears as the subway wagon while the regular Japanese wagon appears as the wagon placeholder. [v2]: Also note that the warning message about the missing wagon prototype still mentions the regular Japanese wagon. * Restart the world again with the advtrains_train_japan mod. Notice that both type of Japanese wagons reappear as Japanese wagons. * Observe that unittests work. Test mod: advtrains.register_wagon_alias("advtrains:engine_japan", "advtrains:subway_wagon") advtrains.register_wagon_alias("advtrains:wagon_japan", "advtrains:wagon_japan") --- advtrains/spec/wagons_spec.lua | 40 ++++++++++++++++++++++++++++++++++++++++ advtrains/trainlogic.lua | 13 +++++++++++-- advtrains/wagons.lua | 35 +++++++++++++++++++++++------------ 3 files changed, 74 insertions(+), 14 deletions(-) create mode 100644 advtrains/spec/wagons_spec.lua (limited to 'advtrains/trainlogic.lua') diff --git a/advtrains/spec/wagons_spec.lua b/advtrains/spec/wagons_spec.lua new file mode 100644 index 0000000..df0687b --- /dev/null +++ b/advtrains/spec/wagons_spec.lua @@ -0,0 +1,40 @@ +require "mineunit" +mineunit "core" + +_G.advtrains = { + wagon_load_range = 32 +} +sourcefile "wagons" + +local myproto = {_test = true} +advtrains.register_wagon(":mywagon", myproto, "My wagon", "", false) +advtrains.register_wagon_alias(":myalias", ":mywagon") +advtrains.register_wagon_alias(":myotheralias", ":myalias") + +local myotherproto = {_other = true} +advtrains.register_wagon(":noalias", myotherproto, "Not aliased wagon", "", false) +advtrains.register_wagon_alias(":noalias", ":mywagon") + +advtrains.register_wagon_alias(":nilalias", ":nil") + +advtrains.register_wagon_alias(":R1", ":R2") +advtrains.register_wagon_alias(":R2", ":R3") +advtrains.register_wagon_alias(":R3", ":R1") + +describe("wagon alias system", function() + it("should work", function() + assert.same({":mywagon", myproto}, {advtrains.resolve_wagon_alias(":myalias")}) + assert.equal(myproto, advtrains.wagon_prototypes[":myalias"]) + assert.same({":mywagon", myproto}, {advtrains.resolve_wagon_alias(":myotheralias")}) + end) + it("should respect wagon registration", function() + assert.same({":noalias", myotherproto}, {advtrains.resolve_wagon_alias(":noalias")}) + end) + it("should handle recursive loops", function() + assert.same({}, {advtrains.resolve_wagon_alias(":R1")}) + end) + it("should return nil for missing entries", function() + assert.same({}, {advtrains.resolve_wagon_alias(":what")}) + assert.same({}, {advtrains.resolve_wagon_alias(":nilalias")}) + end) +end) diff --git a/advtrains/trainlogic.lua b/advtrains/trainlogic.lua index 796aae9..3b006d2 100644 --- a/advtrains/trainlogic.lua +++ b/advtrains/trainlogic.lua @@ -1113,8 +1113,17 @@ function advtrains.spawn_wagons(train_id) if advtrains.position_in_range(pos, ablkrng) then --atdebug("wagon",w_id,"spawning") local wt = advtrains.get_wagon_prototype(data) - local wagon = minetest.add_entity(pos, wt):get_luaentity() - wagon:set_id(w_id) + local wobj = minetest.add_entity(pos, wt) + if not wobj then + atwarn("Failed to spawn wagon", w_id, "of type", wt) + else + local wagon = wobj:get_luaentity() + if not wagon then + atwarn("Wagon", w_id, "of type", wt, "spawned with nil luaentity") + else + wagon:set_id(w_id) + end + end end end else diff --git a/advtrains/wagons.lua b/advtrains/wagons.lua index 50ce4ab..8bb12ee 100644 --- a/advtrains/wagons.lua +++ b/advtrains/wagons.lua @@ -16,15 +16,8 @@ advtrains.wagons = {} advtrains.wagon_alias = {} advtrains.wagon_prototypes = setmetatable({}, { __index = function(t, k) - local rtn_val = rawget(t, k) - if rtn_val ~= nil then - return rtn_val - end - local alias = advtrains.wagon_alias[k] - if alias then - return rawget(t, alias) - end - return nil + local _, proto = advtrains.resolve_wagon_alias(k) + return proto end }) advtrains.wagon_objects = {} @@ -1338,17 +1331,35 @@ function advtrains.get_wagon_prototype(data) data.type = data.entity_name data.entity_name = nil end - if not wt or not advtrains.wagon_prototypes[wt] then + local rt, proto = advtrains.resolve_wagon_alias(wt) + if not rt then atwarn("Unable to load wagon type",wt,", using placeholder") - wt="advtrains:wagon_placeholder" + rt = "advtrains:wagon_placeholder" + proto = advtrains.wagon_prototypes[rt] end - return wt, advtrains.wagon_prototypes[wt] + return rt, proto end function advtrains.register_wagon_alias(src, dst) advtrains.wagon_alias[src] = dst end +local function recursive_resolve_alias(name, seen) + local prototype = rawget(advtrains.wagon_prototypes, name) + if prototype then + return name, prototype + end + local resolved = advtrains.wagon_alias[name] + if resolved and not seen[resolved] then + seen[name] = true + return recursive_resolve_alias(resolved, seen) + end +end + +function advtrains.resolve_wagon_alias(name) + return recursive_resolve_alias(name, {}) +end + function advtrains.standard_inventory_formspec(self, pname, invname) --[[minetest.chat_send_player(pname, string.format("self=%s, pname=%s, invname=%s", self, pname, invname)) for k,v in pairs(self) do -- cgit v1.2.3 From b075d8e3336dfb2755dd1b9dc29c5320a155013b Mon Sep 17 00:00:00 2001 From: "Y. Wang" Date: Fri, 20 Sep 2024 12:45:20 +0000 Subject: Implement staticdata for trains This patch exposes train.staticdata that can be used by other modders to save data in trains across restarts. It additionally exposes two new APIs for modders where this is relevant: * advtrains.te_register_on_couple(function(init_train, stat_train)): registers a callback for train coupling, where stat_train is couple into init_train and is subsequently removed. This callback is run before the actual coupling takes place; in particular, it is run before stat_train is removed. * advtrains.te_register_on_decouple(function(train, newtrain, index)): registers a callback for train decoupling, where newtrain is created by splitting the train at the given index (the wagon at the index is part of the new train). This callback is run after decoupling takes place. advtrains.te_register_on_decouple(function --- advtrains/couple.lua | 16 ++++++++++++++++ advtrains/init.lua | 1 + advtrains/trainlogic.lua | 6 ++++++ 3 files changed, 23 insertions(+) (limited to 'advtrains/trainlogic.lua') diff --git a/advtrains/couple.lua b/advtrains/couple.lua index 9474dcf..cbeb661 100644 --- a/advtrains/couple.lua +++ b/advtrains/couple.lua @@ -28,6 +28,20 @@ end advtrains.register_coupler_type("chain", attrans("Buffer and Chain Coupler")) advtrains.register_coupler_type("scharfenberg", attrans("Scharfenberg Coupler")) +for _, name in pairs {"couple", "decouple"} do + local t = {} + local function reg(f) + table.insert(t, f) + end + local function cb(...) + for _, f in ipairs(t) do + f(...) + end + end + advtrains["te_registered_on_" .. name] = t + advtrains["te_register_on_" .. name] = reg + advtrains["te_run_callbacks_on_" .. name] = cb +end local function create_couple_entity(pos, train1, t1_is_front, train2, t2_is_front) local id1 = train1.id @@ -235,6 +249,8 @@ function advtrains.couple_trains(init_train, invert_init_train, stat_train, stat return end + advtrains.te_run_callbacks_on_couple(init_train, stat_train) + if stat_train_opposite then -- insert wagons in inverse order and set their wagon_flipped state for i=1,stat_wagoncnt do diff --git a/advtrains/init.lua b/advtrains/init.lua index cb2214d..4a0b928 100644 --- a/advtrains/init.lua +++ b/advtrains/init.lua @@ -476,6 +476,7 @@ advtrains.avt_save = function(remove_players_from_wagons) "text_outside", "text_inside", "line", "routingcode", "il_sections", "speed_restriction", "speed_restrictions_t", "is_shunt", "points_split", "autocouple", "atc_wait_autocouple", "ars_disable", + "staticdata", }) --then save it tmp_trains[id]=v diff --git a/advtrains/trainlogic.lua b/advtrains/trainlogic.lua index 3b006d2..ed49a4c 100644 --- a/advtrains/trainlogic.lua +++ b/advtrains/trainlogic.lua @@ -263,6 +263,10 @@ function advtrains.train_ensure_init(id, train) atwarn(debug.traceback()) return nil end + + if not train.staticdata then + train.staticdata = {} + end train.dirty = true if train.no_step then @@ -1190,6 +1194,8 @@ function advtrains.split_train_at_index(train, index) newtrain.points_split = advtrains.merge_tables(train.points_split) newtrain.autocouple = train.autocouple + advtrains.te_run_callbacks_on_decouple(train, newtrain, index) + return newtrain_id -- return new train ID, so new train can be manipulated end -- cgit v1.2.3