From 07442cfe8ff1a23053446c18e2ada854eeed18d8 Mon Sep 17 00:00:00 2001 From: orwell96 Date: Sun, 7 Jan 2018 19:00:43 +0100 Subject: Decrease the amount of garbage saved in save files --- advtrains/helpers.lua | 15 +++++++++++++++ advtrains/init.lua | 33 +++++++++++++++++--------------- advtrains/trainlogic.lua | 43 +++++++++++++++++++++++------------------- advtrains/wagons.lua | 49 ++++++++++++++++++++++++++---------------------- 4 files changed, 84 insertions(+), 56 deletions(-) (limited to 'advtrains') diff --git a/advtrains/helpers.lua b/advtrains/helpers.lua index 3864d81..5f5521f 100644 --- a/advtrains/helpers.lua +++ b/advtrains/helpers.lua @@ -150,6 +150,13 @@ function advtrains.merge_tables(a, ...) end return new 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) local pts=minetest.pos_to_string --atprint("p3 "..pts(prev)..pts(curr)..pts(next)) @@ -315,3 +322,11 @@ function advtrains.get_matching_conn(conn, nconns) return connlku[nconns][conn] end +function advtrains.random_id() + local idst="" + for i=0,5 do + idst=idst..(math.random(0,9)) + end + return idst +end + diff --git a/advtrains/init.lua b/advtrains/init.lua index e3a19e4..99849cb 100644 --- a/advtrains/init.lua +++ b/advtrains/init.lua @@ -184,6 +184,17 @@ function advtrains.avt_load() advtrains.player_to_train_mapping = tbl.ptmap or {} advtrains.ndb.load_data(tbl.ndb) 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 --oh no, its the old one... advtrains.trains=tbl @@ -257,23 +268,15 @@ advtrains.avt_save = function(remove_players_from_wagons) local tmp_trains={} for id, train in pairs(advtrains.trains) do --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 - if v.index then - v.restore_add_index=v.index-math.floor(v.index+1) + if train.index then + v.restore_add_index=train.index-math.floor(train.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 - --then save it tmp_trains[id]=v end diff --git a/advtrains/trainlogic.lua b/advtrains/trainlogic.lua index a72d246..4a0b26c 100644 --- a/advtrains/trainlogic.lua +++ b/advtrains/trainlogic.lua @@ -139,7 +139,7 @@ function advtrains.train_step_a(id, train, dtime) if train.min_index_on_track then assert(math.floor(train.min_index_on_track)==train.min_index_on_track) end - --- 1. LEGACY STUFF --- + --- 1. not exactly legacy. required now because of saving --- if not train.drives_on or not train.max_speed then advtrains.update_trainpart_properties(id) end @@ -230,7 +230,7 @@ function advtrains.train_step_a(id, train, dtime) if train.recently_collided_with_env then train.tarvelocity=0 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 if train.locomotives_in_train==0 then @@ -558,8 +558,8 @@ end --returns new id function advtrains.create_new_train_at(pos, pos_prev) - local newtrain_id=os.time()..os.clock() - while advtrains.trains[newtrain_id] do newtrain_id=os.time()..os.clock() end--ensure uniqueness(will be unneccessary) + local newtrain_id=advtrains.random_id() + while advtrains.trains[newtrain_id] do newtrain_id=advtrains.random_id() end--ensure uniqueness advtrains.trains[newtrain_id]={} advtrains.trains[newtrain_id].last_pos=pos @@ -903,24 +903,29 @@ function advtrains.invalidate_all_paths(pos) end end if exec then - --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 + advtrains.invalidate_path(k) 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 function advtrains.train_collides(node) diff --git a/advtrains/wagons.lua b/advtrains/wagons.lua index b75ca50..a367487 100644 --- a/advtrains/wagons.lua +++ b/advtrains/wagons.lua @@ -59,16 +59,18 @@ function wagon:get_staticdata() self.ser_inv=advtrains.serialize_inventory(inv) end --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].name=nil - advtrains.wagon_save[self.unique_id].object=nil return self.unique_id end) end --returns: uid of wagon 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 for k,v in pairs(properties) do if k~="name" and k~="object" then @@ -361,7 +363,7 @@ function wagon:on_step(dtime) --DisCouple 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 local object=minetest.add_entity(pos, "advtrains:discouple") if object then @@ -501,24 +503,27 @@ function wagon:on_step(dtime) self.object:setacceleration(accelerationvec) if #self.seats > 0 and self.old_yaw ~= yaw then - if not self.player_yaw then - self.player_yaw = {} - end - for _,name in pairs(self.seatp) do - local p = minetest.get_player_by_name(name) - if p then - if not self.turning then - -- save player looking direction offset - self.player_yaw[name] = p:get_look_horizontal()-self.old_yaw - end - -- set player looking direction using calculated offset - p:set_look_horizontal(self.player_yaw[name]+yaw) - end - end - self.turning = true + if not self.player_yaw then + self.player_yaw = {} + end + if not self.old_yaw then + self.old_yaw=yaw + end + for _,name in pairs(self.seatp) do + local p = minetest.get_player_by_name(name) + if p then + if not self.turning then + -- save player looking direction offset + self.player_yaw[name] = p:get_look_horizontal()-self.old_yaw + end + -- set player looking direction using calculated offset + p:set_look_horizontal(self.player_yaw[name]+yaw) + end + end + self.turning = true elseif self.old_yaw == yaw then - -- train is no longer turning - self.turning = false + -- train is no longer turning + self.turning = false end self.object:setyaw(yaw) -- cgit v1.2.3