Decrease the amount of garbage saved in save files
parent
e3b7046968
commit
07442cfe8f
|
@ -150,6 +150,13 @@ function advtrains.merge_tables(a, ...)
|
||||||
end
|
end
|
||||||
return new
|
return new
|
||||||
end
|
end
|
||||||
|
function advtrains.save_keys(tbl, keys)
|
||||||
|
local new={}
|
||||||
|
for _,key in ipairs(keys) do
|
||||||
|
new[key] = tbl[key]
|
||||||
|
end
|
||||||
|
return new
|
||||||
|
end
|
||||||
function advtrains.yaw_from_3_positions(prev, curr, next)
|
function advtrains.yaw_from_3_positions(prev, curr, next)
|
||||||
local pts=minetest.pos_to_string
|
local pts=minetest.pos_to_string
|
||||||
--atprint("p3 "..pts(prev)..pts(curr)..pts(next))
|
--atprint("p3 "..pts(prev)..pts(curr)..pts(next))
|
||||||
|
@ -315,3 +322,11 @@ function advtrains.get_matching_conn(conn, nconns)
|
||||||
return connlku[nconns][conn]
|
return connlku[nconns][conn]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function advtrains.random_id()
|
||||||
|
local idst=""
|
||||||
|
for i=0,5 do
|
||||||
|
idst=idst..(math.random(0,9))
|
||||||
|
end
|
||||||
|
return idst
|
||||||
|
end
|
||||||
|
|
||||||
|
|
|
@ -184,6 +184,17 @@ function advtrains.avt_load()
|
||||||
advtrains.player_to_train_mapping = tbl.ptmap or {}
|
advtrains.player_to_train_mapping = tbl.ptmap or {}
|
||||||
advtrains.ndb.load_data(tbl.ndb)
|
advtrains.ndb.load_data(tbl.ndb)
|
||||||
advtrains.atc.load_data(tbl.atc)
|
advtrains.atc.load_data(tbl.atc)
|
||||||
|
--remove wagon_save entries that are not part of a train
|
||||||
|
local todel=advtrains.merge_tables(advtrains.wagon_save)
|
||||||
|
for tid, train in pairs(advtrains.trains) do
|
||||||
|
for _, wid in ipairs(train.trainparts) do
|
||||||
|
todel[wid]=nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
for wid, _ in pairs(todel) do
|
||||||
|
atwarn("Removing unused wagon", wid, "from wagon_save table.")
|
||||||
|
advtrains.wagon_save[wid]=nil
|
||||||
|
end
|
||||||
else
|
else
|
||||||
--oh no, its the old one...
|
--oh no, its the old one...
|
||||||
advtrains.trains=tbl
|
advtrains.trains=tbl
|
||||||
|
@ -257,23 +268,15 @@ advtrains.avt_save = function(remove_players_from_wagons)
|
||||||
local tmp_trains={}
|
local tmp_trains={}
|
||||||
for id, train in pairs(advtrains.trains) do
|
for id, train in pairs(advtrains.trains) do
|
||||||
--first, deep_copy the train
|
--first, deep_copy the train
|
||||||
local v=advtrains.merge_tables(train)
|
local v=advtrains.save_keys(train, {
|
||||||
|
"last_pos", "last_pos_prev", "movedir", "velocity", "tarvelocity",
|
||||||
|
"trainparts", "savedpos_off_track_index_offset", "recently_collided_with_env",
|
||||||
|
"atc_brake_target", "atc_wait_finish", "atc_command", "atc_delay", "door_state"
|
||||||
|
})
|
||||||
--then invalidate
|
--then invalidate
|
||||||
if v.index then
|
if train.index then
|
||||||
v.restore_add_index=v.index-math.floor(v.index+1)
|
v.restore_add_index=train.index-math.floor(train.index+1)
|
||||||
end
|
end
|
||||||
v.path=nil
|
|
||||||
v.path_dist=nil
|
|
||||||
v.index=nil
|
|
||||||
v.end_index=nil
|
|
||||||
v.min_index_on_track=nil
|
|
||||||
v.max_index_on_track=nil
|
|
||||||
v.path_extent_min=nil
|
|
||||||
v.path_extent_max=nil
|
|
||||||
|
|
||||||
v.detector_old_index=nil
|
|
||||||
v.detector_old_end_index=nil
|
|
||||||
|
|
||||||
--then save it
|
--then save it
|
||||||
tmp_trains[id]=v
|
tmp_trains[id]=v
|
||||||
end
|
end
|
||||||
|
|
|
@ -139,7 +139,7 @@ function advtrains.train_step_a(id, train, dtime)
|
||||||
if train.min_index_on_track then
|
if train.min_index_on_track then
|
||||||
assert(math.floor(train.min_index_on_track)==train.min_index_on_track)
|
assert(math.floor(train.min_index_on_track)==train.min_index_on_track)
|
||||||
end
|
end
|
||||||
--- 1. LEGACY STUFF ---
|
--- 1. not exactly legacy. required now because of saving ---
|
||||||
if not train.drives_on or not train.max_speed then
|
if not train.drives_on or not train.max_speed then
|
||||||
advtrains.update_trainpart_properties(id)
|
advtrains.update_trainpart_properties(id)
|
||||||
end
|
end
|
||||||
|
@ -230,7 +230,7 @@ function advtrains.train_step_a(id, train, dtime)
|
||||||
if train.recently_collided_with_env then
|
if train.recently_collided_with_env then
|
||||||
train.tarvelocity=0
|
train.tarvelocity=0
|
||||||
if not train_moves then
|
if not train_moves then
|
||||||
train.recently_collided_with_env=false--reset status when stopped
|
train.recently_collided_with_env=nil--reset status when stopped
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if train.locomotives_in_train==0 then
|
if train.locomotives_in_train==0 then
|
||||||
|
@ -558,8 +558,8 @@ end
|
||||||
|
|
||||||
--returns new id
|
--returns new id
|
||||||
function advtrains.create_new_train_at(pos, pos_prev)
|
function advtrains.create_new_train_at(pos, pos_prev)
|
||||||
local newtrain_id=os.time()..os.clock()
|
local newtrain_id=advtrains.random_id()
|
||||||
while advtrains.trains[newtrain_id] do newtrain_id=os.time()..os.clock() end--ensure uniqueness(will be unneccessary)
|
while advtrains.trains[newtrain_id] do newtrain_id=advtrains.random_id() end--ensure uniqueness
|
||||||
|
|
||||||
advtrains.trains[newtrain_id]={}
|
advtrains.trains[newtrain_id]={}
|
||||||
advtrains.trains[newtrain_id].last_pos=pos
|
advtrains.trains[newtrain_id].last_pos=pos
|
||||||
|
@ -903,24 +903,29 @@ function advtrains.invalidate_all_paths(pos)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if exec then
|
if exec then
|
||||||
--TODO duplicate code in init.lua avt_save()!
|
advtrains.invalidate_path(k)
|
||||||
if v.index then
|
|
||||||
v.restore_add_index=v.index-math.floor(v.index+1)
|
|
||||||
end
|
|
||||||
v.path=nil
|
|
||||||
v.path_dist=nil
|
|
||||||
v.index=nil
|
|
||||||
v.end_index=nil
|
|
||||||
v.min_index_on_track=nil
|
|
||||||
v.max_index_on_track=nil
|
|
||||||
v.path_extent_min=nil
|
|
||||||
v.path_extent_max=nil
|
|
||||||
|
|
||||||
v.detector_old_index=nil
|
|
||||||
v.detector_old_end_index=nil
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
function advtrains.invalidate_path(id)
|
||||||
|
local v=advtrains.trains[id]
|
||||||
|
if not v then return end
|
||||||
|
--TODO duplicate code in init.lua avt_save()!
|
||||||
|
if v.index then
|
||||||
|
v.restore_add_index=v.index-math.floor(v.index+1)
|
||||||
|
end
|
||||||
|
v.path=nil
|
||||||
|
v.path_dist=nil
|
||||||
|
v.index=nil
|
||||||
|
v.end_index=nil
|
||||||
|
v.min_index_on_track=nil
|
||||||
|
v.max_index_on_track=nil
|
||||||
|
v.path_extent_min=nil
|
||||||
|
v.path_extent_max=nil
|
||||||
|
|
||||||
|
v.detector_old_index=nil
|
||||||
|
v.detector_old_end_index=nil
|
||||||
|
end
|
||||||
|
|
||||||
--not blocking trains group
|
--not blocking trains group
|
||||||
function advtrains.train_collides(node)
|
function advtrains.train_collides(node)
|
||||||
|
|
|
@ -59,16 +59,18 @@ function wagon:get_staticdata()
|
||||||
self.ser_inv=advtrains.serialize_inventory(inv)
|
self.ser_inv=advtrains.serialize_inventory(inv)
|
||||||
end
|
end
|
||||||
--save to table before being unloaded
|
--save to table before being unloaded
|
||||||
advtrains.wagon_save[self.unique_id]=advtrains.merge_tables(self)
|
advtrains.wagon_save[self.unique_id]=advtrains.save_keys(self, {
|
||||||
|
"seatp", "owner", "ser_inv", "wagon_flipped", "train_id"
|
||||||
|
})
|
||||||
advtrains.wagon_save[self.unique_id].entity_name=self.name
|
advtrains.wagon_save[self.unique_id].entity_name=self.name
|
||||||
advtrains.wagon_save[self.unique_id].name=nil
|
|
||||||
advtrains.wagon_save[self.unique_id].object=nil
|
|
||||||
return self.unique_id
|
return self.unique_id
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
--returns: uid of wagon
|
--returns: uid of wagon
|
||||||
function wagon:init_new_instance(train_id, properties)
|
function wagon:init_new_instance(train_id, properties)
|
||||||
self.unique_id=os.time()..os.clock()
|
local new_id=advtrains.random_id()
|
||||||
|
while advtrains.wagon_save[new_id] do new_id=advtrains.random_id() end--ensure uniqueness
|
||||||
|
self.unique_id=new_id
|
||||||
self.train_id=train_id
|
self.train_id=train_id
|
||||||
for k,v in pairs(properties) do
|
for k,v in pairs(properties) do
|
||||||
if k~="name" and k~="object" then
|
if k~="name" and k~="object" then
|
||||||
|
@ -361,7 +363,7 @@ function wagon:on_step(dtime)
|
||||||
|
|
||||||
--DisCouple
|
--DisCouple
|
||||||
if self.pos_in_trainparts and self.pos_in_trainparts>1 then
|
if self.pos_in_trainparts and self.pos_in_trainparts>1 then
|
||||||
if gp.velocity==0 and not self.lock_couples then
|
if gp.velocity==0 then
|
||||||
if not self.discouple or not self.discouple.object:getyaw() then
|
if not self.discouple or not self.discouple.object:getyaw() then
|
||||||
local object=minetest.add_entity(pos, "advtrains:discouple")
|
local object=minetest.add_entity(pos, "advtrains:discouple")
|
||||||
if object then
|
if object then
|
||||||
|
@ -501,24 +503,27 @@ function wagon:on_step(dtime)
|
||||||
self.object:setacceleration(accelerationvec)
|
self.object:setacceleration(accelerationvec)
|
||||||
|
|
||||||
if #self.seats > 0 and self.old_yaw ~= yaw then
|
if #self.seats > 0 and self.old_yaw ~= yaw then
|
||||||
if not self.player_yaw then
|
if not self.player_yaw then
|
||||||
self.player_yaw = {}
|
self.player_yaw = {}
|
||||||
end
|
end
|
||||||
for _,name in pairs(self.seatp) do
|
if not self.old_yaw then
|
||||||
local p = minetest.get_player_by_name(name)
|
self.old_yaw=yaw
|
||||||
if p then
|
end
|
||||||
if not self.turning then
|
for _,name in pairs(self.seatp) do
|
||||||
-- save player looking direction offset
|
local p = minetest.get_player_by_name(name)
|
||||||
self.player_yaw[name] = p:get_look_horizontal()-self.old_yaw
|
if p then
|
||||||
end
|
if not self.turning then
|
||||||
-- set player looking direction using calculated offset
|
-- save player looking direction offset
|
||||||
p:set_look_horizontal(self.player_yaw[name]+yaw)
|
self.player_yaw[name] = p:get_look_horizontal()-self.old_yaw
|
||||||
end
|
end
|
||||||
end
|
-- set player looking direction using calculated offset
|
||||||
self.turning = true
|
p:set_look_horizontal(self.player_yaw[name]+yaw)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
self.turning = true
|
||||||
elseif self.old_yaw == yaw then
|
elseif self.old_yaw == yaw then
|
||||||
-- train is no longer turning
|
-- train is no longer turning
|
||||||
self.turning = false
|
self.turning = false
|
||||||
end
|
end
|
||||||
|
|
||||||
self.object:setyaw(yaw)
|
self.object:setyaw(yaw)
|
||||||
|
|
Loading…
Reference in New Issue