aboutsummaryrefslogtreecommitdiff
path: root/advtrains
diff options
context:
space:
mode:
authororwell96 <mono96.mml@gmail.com>2017-01-12 17:33:43 +0100
committerorwell96 <mono96.mml@gmail.com>2017-01-12 17:33:43 +0100
commitdc8b47a066ff8b3251ad09d4b490647e3f057d10 (patch)
treeb0953a17adf450e8bff02395f71538472f85e393 /advtrains
parent04d14dc1bf4d7527635652e48493114254750dd3 (diff)
downloadadvtrains-dc8b47a066ff8b3251ad09d4b490647e3f057d10.tar.gz
advtrains-dc8b47a066ff8b3251ad09d4b490647e3f057d10.tar.bz2
advtrains-dc8b47a066ff8b3251ad09d4b490647e3f057d10.zip
Unite all save data (except the track database) into a single save file
Diffstat (limited to 'advtrains')
-rw-r--r--advtrains/advtrains/atc.lua33
-rw-r--r--advtrains/advtrains/init.lua112
-rw-r--r--advtrains/advtrains/pseudoload.lua3
-rw-r--r--advtrains/advtrains/trainlogic.lua85
4 files changed, 115 insertions, 118 deletions
diff --git a/advtrains/advtrains/atc.lua b/advtrains/advtrains/atc.lua
index bd02425..e015d47 100644
--- a/advtrains/advtrains/atc.lua
+++ b/advtrains/advtrains/atc.lua
@@ -2,37 +2,10 @@
--registers and controls the ATC system
local atc={}
--- ATC persistence table
-atc.controllers = {}
+-- ATC persistence table. advtrains.atc is created by init.lua when it loads the save file.
+atc.controllers = advtrains.atc.controllers
--contents: {command="...", arrowconn=0-15 where arrow points}
-advtrains.fpath_atc=minetest.get_worldpath().."/advtrains_atc"
-local file, err = io.open(advtrains.fpath_atc, "r")
-if not file then
- local er=err or "Unknown Error"
- atprint("Failed loading advtrains atc save file "..er)
-else
- local tbl = minetest.deserialize(file:read("*a"))
- if type(tbl) == "table" then
- atc.controllers=tbl.controllers
- end
- file:close()
-end
-function atc.save()
- --leave space for more save data.
- local datastr = minetest.serialize({controllers = atc.controllers})
- if not datastr then
- minetest.log("error", " Failed to serialize trackdb data!")
- return
- end
- local file, err = io.open(advtrains.fpath_atc, "w")
- if err then
- return err
- end
- file:write(datastr)
- file:close()
-end
-
--call from advtrains.detector subprogram
function atc.trigger_controller_train_enter(pos, train_id)
@@ -290,5 +263,7 @@ function atc.execute_atc_command(id, train)
atc.train_reset_command(id)
end
+
+
--move table to desired place
advtrains.atc=atc
diff --git a/advtrains/advtrains/init.lua b/advtrains/advtrains/init.lua
index a415857..74201b5 100644
--- a/advtrains/advtrains/init.lua
+++ b/advtrains/advtrains/init.lua
@@ -1,6 +1,7 @@
--advtrains
-advtrains={}
+--create the base table structure.
+advtrains={atc={controllers={}}, detector={}, trackplacer={}, trains={}, trackdb={}, wagon_save={}}
advtrains.modpath = minetest.get_modpath("advtrains")
@@ -34,8 +35,8 @@ local function print_concat_table(a)
end
return str
end
-atprint=function() end
---atprint=function(t, ...) minetest.log("action", "[advtrains]"..print_concat_table({t, ...})) minetest.chat_send_all("[advtrains]"..print_concat_table({t, ...})) end
+--atprint=function() end
+atprint=function(t, ...) minetest.log("action", "[advtrains]"..print_concat_table({t, ...})) minetest.chat_send_all("[advtrains]"..print_concat_table({t, ...})) end
sid=function(id) return string.sub(id, -4) end
dofile(advtrains.modpath.."/helpers.lua");
@@ -55,6 +56,110 @@ advtrains.meseconrules =
{x=0, y=1, z=-1},
{x=0, y=-1, z=-1},
{x=0, y=-2, z=0}}
+
+
+
+--load/save
+
+advtrains.fpath=minetest.get_worldpath().."/advtrains"
+local file, err = io.open(advtrains.fpath, "r")
+if not file then
+ minetest.log("error", " Failed to read advtrains save data from file "..advtrains.fpath..": "..(err or "Unknown Error"))
+else
+ local tbl = minetest.deserialize(file:read("*a"))
+ if type(tbl) == "table" then
+ if tbl.version then
+ --congrats, we have the new save format.
+ advtrains.atc.controllers = tbl.atc_controllers
+ advtrains.trains = tbl.trains
+ advtrains.wagon_save = tbl.wagon_save
+ else
+ --oh no, its the old one...
+ advtrains.trains=tbl
+ --load ATC
+ advtrains.fpath_atc=minetest.get_worldpath().."/advtrains_atc"
+ local file, err = io.open(advtrains.fpath_atc, "r")
+ if not file then
+ local er=err or "Unknown Error"
+ atprint("Failed loading advtrains atc save file "..er)
+ else
+ local tbl = minetest.deserialize(file:read("*a"))
+ if type(tbl) == "table" then
+ advtrains.atc.controllers=tbl.controllers
+ end
+ file:close()
+ end
+ --load wagon saves
+ advtrains.fpath_ws=minetest.get_worldpath().."/advtrains_wagon_save"
+ local file, err = io.open(advtrains.fpath_ws, "r")
+ if not file then
+ local er=err or "Unknown Error"
+ atprint("Failed loading advtrains save file "..er)
+ else
+ local tbl = minetest.deserialize(file:read("*a"))
+ if type(tbl) == "table" then
+ advtrains.wagon_save=tbl
+ end
+ file:close()
+ end
+ end
+ else
+ minetest.log("error", " Failed to deserialize advtrains save data: Not a table!")
+ end
+ file:close()
+end
+
+advtrains.save = function()
+ atprint("saving")
+ advtrains.invalidate_all_paths()
+
+ -- update wagon saves
+ for _,wagon in pairs(minetest.luaentities) do
+ if wagon.is_wagon and wagon.initialized then
+ wagon:get_staticdata()
+ end
+ end
+ --cross out userdata
+ for w_id, data in pairs(advtrains.wagon_save) do
+ data.name=nil
+ data.object=nil
+ if data.driver then
+ data.driver_name=data.driver:get_player_name()
+ data.driver=nil
+ else
+ data.driver_name=nil
+ end
+ if data.discouple then
+ data.discouple.object:remove()
+ data.discouple=nil
+ end
+ end
+ --atprint(dump(advtrains.wagon_save))
+
+ --versions:
+ -- 1 - Initial new save format.
+ local save_tbl={
+ trains = advtrains.trains,
+ wagon_save = advtrains.wagon_save,
+ atc_controllers = advtrains.atc.controllers,
+ version = 1,
+ }
+ local datastr = minetest.serialize(save_tbl)
+ if not datastr then
+ minetest.log("error", " Failed to serialize advtrains save data!")
+ return
+ end
+ local file, err = io.open(advtrains.fpath, "w")
+ if err then
+ minetest.log("error", " Failed to write advtrains save data to file "..advtrains.fpath..": "..(err or "Unknown Error"))
+ return
+ end
+ file:write(datastr)
+ file:close()
+end
+minetest.register_on_shutdown(advtrains.save)
+
+
dofile(advtrains.modpath.."/trainlogic.lua")
dofile(advtrains.modpath.."/trainhud.lua")
dofile(advtrains.modpath.."/trackplacer.lua")
@@ -69,3 +174,4 @@ dofile(advtrains.modpath.."/damage.lua")
dofile(advtrains.modpath.."/signals.lua")
dofile(advtrains.modpath.."/misc_nodes.lua")
dofile(advtrains.modpath.."/crafting.lua")
+
diff --git a/advtrains/advtrains/pseudoload.lua b/advtrains/advtrains/pseudoload.lua
index 5cbd2c6..818808d 100644
--- a/advtrains/advtrains/pseudoload.lua
+++ b/advtrains/advtrains/pseudoload.lua
@@ -98,7 +98,8 @@ function advtrains.save_trackdb()
end
end
]]--end temp outcomment
-advtrains.trackdb={}
+
+--trackdb keeps its own save file.
advtrains.fpath_tdb=minetest.get_worldpath().."/advtrains_trackdb2"
local file, err = io.open(advtrains.fpath_tdb, "r")
if not file then
diff --git a/advtrains/advtrains/trainlogic.lua b/advtrains/advtrains/trainlogic.lua
index 07a48cc..c9c7403 100644
--- a/advtrains/advtrains/trainlogic.lua
+++ b/advtrains/advtrains/trainlogic.lua
@@ -39,96 +39,11 @@ advtrains.train_emerg_force=10--for emergency brakes(when going off track)
advtrains.audit_interval=10
-advtrains.trains={}
-advtrains.wagon_save={}
-
---load initially
-advtrains.fpath=minetest.get_worldpath().."/advtrains"
-local file, err = io.open(advtrains.fpath, "r")
-if not file then
- local er=err or "Unknown Error"
- atprint("Failed loading advtrains save file "..er)
-else
- local tbl = minetest.deserialize(file:read("*a"))
- if type(tbl) == "table" then
- advtrains.trains=tbl
- end
- file:close()
-end
-advtrains.fpath_ws=minetest.get_worldpath().."/advtrains_wagon_save"
-local file, err = io.open(advtrains.fpath_ws, "r")
-if not file then
- local er=err or "Unknown Error"
- atprint("Failed loading advtrains save file "..er)
-else
- local tbl = minetest.deserialize(file:read("*a"))
- if type(tbl) == "table" then
- advtrains.wagon_save=tbl
- end
- file:close()
-end
-
-
-advtrains.save = function()
- atprint("saving")
- advtrains.invalidate_all_paths()
- local datastr = minetest.serialize(advtrains.trains)
- if not datastr then
- minetest.log("error", " Failed to serialize train data!")
- return
- end
- local file, err = io.open(advtrains.fpath, "w")
- if err then
- return err
- end
- file:write(datastr)
- file:close()
-
- -- update wagon saves
- for _,wagon in pairs(minetest.luaentities) do
- if wagon.is_wagon and wagon.initialized then
- wagon:get_staticdata()
- end
- end
- --cross out userdata
- for w_id, data in pairs(advtrains.wagon_save) do
- data.name=nil
- data.object=nil
- if data.driver then
- data.driver_name=data.driver:get_player_name()
- data.driver=nil
- else
- data.driver_name=nil
- end
- if data.discouple then
- data.discouple.object:remove()
- data.discouple=nil
- end
- end
- --atprint(dump(advtrains.wagon_save))
- datastr = minetest.serialize(advtrains.wagon_save)
- if not datastr then
- minetest.log("error", " Failed to serialize train data!")
- return
- end
- file, err = io.open(advtrains.fpath_ws, "w")
- if err then
- return err
- end
- file:write(datastr)
- file:close()
-
- advtrains.save_trackdb()
- advtrains.atc.save()
-end
-minetest.register_on_shutdown(advtrains.save)
-
advtrains.save_and_audit_timer=advtrains.audit_interval
minetest.register_globalstep(function(dtime)
advtrains.save_and_audit_timer=advtrains.save_and_audit_timer-dtime
if advtrains.save_and_audit_timer<=0 then
local t=os.clock()
-
--save
advtrains.save()
advtrains.save_and_audit_timer=advtrains.audit_interval