Add workaround to unload wagons that are too far away from players

This commit is contained in:
orwell96 2020-07-12 20:37:51 +02:00
parent 74bf177cc8
commit 19509acf56
4 changed files with 39 additions and 4 deletions

View File

@ -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

View File

@ -39,3 +39,9 @@ advtrains_prot_range_down (Track protection range [down]) int 1 0 10
# 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
# 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

View File

@ -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)

View File

@ -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