diff --git a/advtrains/init.lua b/advtrains/init.lua index 28e104e..84cfa38 100644 --- a/advtrains/init.lua +++ b/advtrains/init.lua @@ -39,6 +39,12 @@ AT_CMAX = 16 advtrains = {trains={}, player_to_train_mapping={}} +-- get wagon loading range +advtrains.wagon_load_range = tonumber(minetest.settings:get("advtrains_wagon_load_range")) +if not advtrains.wagon_load_range then + advtrains.wagon_load_range = tonumber(minetest.settings:get("active_block_range"))*16 +end + --pcall local no_action=false diff --git a/advtrains/settingtypes.txt b/advtrains/settingtypes.txt index 26c6d5a..d8eb44a 100644 --- a/advtrains/settingtypes.txt +++ b/advtrains/settingtypes.txt @@ -38,4 +38,10 @@ advtrains_prot_range_down (Track protection range [down]) int 1 0 10 # none: No damage is dealt at all. # drop: Player is killed, all items are dropped as items on the tracks. # normal: Player is killed, game-defined behavior is applied as if the player died by other means. -advtrains_overrun_mode (Overrun mode) enum drop none,drop,normal \ No newline at end of file +advtrains_overrun_mode (Overrun mode) enum drop none,drop,normal + +# Wagon entity loading/unloading range, in nodes +# When a wagon is within this range to a player, it is loaded +# When a wagon leaves this range + 32 nodes, it is unloaded +# If unset, defaults to active_block_range*16 +advtrains_wagon_load_range (Wagon Entity Load/Unload Range) int 96 32 512 diff --git a/advtrains/trainlogic.lua b/advtrains/trainlogic.lua index 6155768..3e57db2 100644 --- a/advtrains/trainlogic.lua +++ b/advtrains/trainlogic.lua @@ -851,7 +851,7 @@ function advtrains.update_trainpart_properties(train_id, invert_flipstate) end -local ablkrng = minetest.settings:get("active_block_range")*16 +local ablkrng = advtrains.wagon_load_range -- This function checks whether entities need to be spawned for certain wagons, and spawns them. -- Called from train_step_*(), not required to check init. function advtrains.spawn_wagons(train_id) @@ -862,7 +862,7 @@ function advtrains.spawn_wagons(train_id) local data = advtrains.wagons[w_id] if data then if data.train_id ~= train_id then - atwarn("Train",train_id,"Wagon #",1,": Saved train ID",data.train_id,"did not match!") + atwarn("Train",train_id,"Wagon #",i,": Saved train ID",data.train_id,"did not match!") data.train_id = train_id end if not advtrains.wagon_objects[w_id] or not advtrains.wagon_objects[w_id]:getyaw() then @@ -878,6 +878,7 @@ function advtrains.spawn_wagons(train_id) end if spawn 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) diff --git a/advtrains/wagons.lua b/advtrains/wagons.lua index 2b0833d..28a471a 100644 --- a/advtrains/wagons.lua +++ b/advtrains/wagons.lua @@ -14,6 +14,8 @@ advtrains.wagons = {} advtrains.wagon_prototypes = {} advtrains.wagon_objects = {} +local unload_wgn_range = advtrains.wagon_load_range + 32 + local setting_show_ids = minetest.settings:get_bool("advtrains_show_ids") -- @@ -473,8 +475,29 @@ function wagon:on_step(dtime) yaw=yaw+math.pi end + -- this timer runs off every 2 seconds. self.updatepct_timer=(self.updatepct_timer or 0)-dtime local updatepct_timer_elapsed = self.updatepct_timer<=0 + + if updatepct_timer_elapsed then + --restart timer + self.updatepct_timer=2 + -- perform checks that are not frequently needed + + -- unload entity if out of range (because relevant pr won't be merged in engine) + -- This is a WORKAROUND! + local outofrange = false + for _,p in pairs(minetest.get_connected_players()) do + if vector.distance(p:get_pos(),pos)>=unload_wgn_range then + outofrange = true + end + end + if outofrange then + --atdebug("wagon",self.id,"unloading (too far away)") + self.object:remove() + end + end + if not self.old_velocity_vector or not vector.equals(velocityvec, self.old_velocity_vector) or not self.old_acceleration_vector @@ -520,7 +543,6 @@ function wagon:on_step(dtime) self.object:setyaw(yaw) end - self.updatepct_timer=2 if self.update_animation then self:update_animation(train.velocity, self.old_velocity) end