diff options
Diffstat (limited to 'advtrains/advtrains/init.lua')
-rw-r--r-- | advtrains/advtrains/init.lua | 168 |
1 files changed, 119 insertions, 49 deletions
diff --git a/advtrains/advtrains/init.lua b/advtrains/advtrains/init.lua index 9b7f31f..022fcee 100644 --- a/advtrains/advtrains/init.lua +++ b/advtrains/advtrains/init.lua @@ -10,7 +10,7 @@ end advtrains = {trains={}, wagon_save={}, player_to_train_mapping={}} --pcall -local no_action=true +local no_action=false function advtrains.pcall(fun) if no_action then return end @@ -118,62 +118,58 @@ dofile(advtrains.modpath.."/craft_items.lua") advtrains.fpath=minetest.get_worldpath().."/advtrains" -function advtrains.load() - -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.trains = tbl.trains - advtrains.wagon_save = tbl.wagon_save - advtrains.player_to_train_mapping = tbl.ptmap or {} - advtrains.ndb.load_data(tbl.ndb) - advtrains.atc.load_data(tbl.atc) - 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) +function advtrains.avt_load() + 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.trains = tbl.trains + advtrains.wagon_save = tbl.wagon_save + advtrains.player_to_train_mapping = tbl.ptmap or {} + advtrains.ndb.load_data(tbl.ndb) + advtrains.atc.load_data(tbl.atc) else - local tbl = minetest.deserialize(file:read("*a")) - if type(tbl) == "table" then - advtrains.atc.controllers=tbl.controllers + --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 - 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 + --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 - file:close() end + else + minetest.log("error", " Failed to deserialize advtrains save data: Not a table!") end - else - minetest.log("error", " Failed to deserialize advtrains save data: Not a table!") + file:close() end - file:close() end -no_action=false -end -advtrains.load() - -advtrains.save = function() +advtrains.avt_save = function() --atprint("saving") advtrains.invalidate_all_paths() @@ -222,4 +218,78 @@ advtrains.save = function() file:write(datastr) file:close() end + +--## MAIN LOOP ##-- +--Calls all subsequent main tasks of both advtrains and atlatc +local init_load=false +local save_interval=20 +local save_timer=save_interval + +minetest.register_globalstep(function(dtime_mt) + return advtrains.pcall(function() + --call load once. see advtrains.load() comment + if not init_load then + advtrains.load() + end + --limit dtime: if trains move too far in one step, automation may cause stuck and wrongly braking trains + local dtime=dtime_mt + if dtime>0.2 then + atprint("Limiting dtime to 0.2!") + dtime=0.2 + end + + advtrains.mainloop_trainlogic(dtime) + if advtrains_itm_mainloop then + advtrains_itm_mainloop(dtime) + end + if atlatc then + atlatc.mainloop_stepcode(dtime) + atlatc.interrupt.mainloop(dtime) + end + + + --trigger a save when necessary + save_timer=save_timer-dtime + if save_timer<=0 then + local t=os.clock() + --save + advtrains.save() + save_timer=save_interval + atprintbm("saving", t) + end + end) +end) + +--if something goes wrong in these functions, there is no help. no pcall here. + +--## MAIN LOAD ROUTINE ## +-- Causes the loading of everything +-- first time called in main loop (after the init phase) because luaautomation has to initialize first. +function advtrains.load() + advtrains.avt_load() --loading advtrains. includes ndb at advtrains.ndb.load_data() + if atlatc then + atlatc.load() --includes interrupts + end + if advtrains_itm_init then + advtrains_itm_init() + end + init_load=true + no_action=false + atprint("[load_all]Loaded advtrains save files") +end + +--## MAIN SAVE ROUTINE ## +-- Causes the saving of everything +function advtrains.save() + if not init_load then + --wait... we haven't loaded yet?! + atwarn("Instructed to save() but load() was never called!") + return + end + advtrains.avt_save() --saving advtrains. includes ndb at advtrains.ndb.save_data() + if atlatc then + atlatc.save() + end + atprint("[save_all]Saved advtrains save files") +end minetest.register_on_shutdown(advtrains.save) |