Unify globalstep, load and save of all components into single functions
parent
337db2a573
commit
331db7ba5f
|
@ -94,56 +94,58 @@ dofile(advtrains.modpath.."/craft_items.lua")
|
||||||
--load/save
|
--load/save
|
||||||
|
|
||||||
advtrains.fpath=minetest.get_worldpath().."/advtrains"
|
advtrains.fpath=minetest.get_worldpath().."/advtrains"
|
||||||
local file, err = io.open(advtrains.fpath, "r")
|
function advtrains.avt_load()
|
||||||
if not file then
|
local file, err = io.open(advtrains.fpath, "r")
|
||||||
minetest.log("error", " Failed to read advtrains save data from file "..advtrains.fpath..": "..(err or "Unknown Error"))
|
if not file then
|
||||||
else
|
minetest.log("error", " Failed to read advtrains save data from file "..advtrains.fpath..": "..(err or "Unknown Error"))
|
||||||
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)
|
|
||||||
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
|
else
|
||||||
minetest.log("error", " Failed to deserialize advtrains save data: Not a table!")
|
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)
|
||||||
|
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
|
end
|
||||||
file:close()
|
|
||||||
end
|
end
|
||||||
|
|
||||||
advtrains.save = function()
|
advtrains.avt_save = function()
|
||||||
--atprint("saving")
|
--atprint("saving")
|
||||||
advtrains.invalidate_all_paths()
|
advtrains.invalidate_all_paths()
|
||||||
|
|
||||||
|
@ -192,4 +194,72 @@ advtrains.save = function()
|
||||||
file:write(datastr)
|
file:write(datastr)
|
||||||
file:close()
|
file:close()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--## MAIN LOOP ##--
|
||||||
|
--Calls all subsequent main tasks of both advtrains and atlatc
|
||||||
|
local init_load
|
||||||
|
local save_interval=20
|
||||||
|
local save_timer=save_interval
|
||||||
|
|
||||||
|
minetest.register_globalstep(function(dtime_mt)
|
||||||
|
--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)
|
||||||
|
|
||||||
|
--## 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
|
||||||
|
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
|
||||||
|
end
|
||||||
minetest.register_on_shutdown(advtrains.save)
|
minetest.register_on_shutdown(advtrains.save)
|
||||||
|
|
|
@ -36,25 +36,9 @@ advtrains.train_brake_force=3--per second, not divided by number of wagons
|
||||||
advtrains.train_roll_force=0.5--per second, not divided by number of wagons, acceleration when rolling without brake
|
advtrains.train_roll_force=0.5--per second, not divided by number of wagons, acceleration when rolling without brake
|
||||||
advtrains.train_emerg_force=10--for emergency brakes(when going off track)
|
advtrains.train_emerg_force=10--for emergency brakes(when going off track)
|
||||||
|
|
||||||
advtrains.save_interval=10
|
|
||||||
advtrains.save_timer=advtrains.save_interval
|
|
||||||
|
|
||||||
minetest.register_globalstep(function(dtime_mt)
|
advtrains.mainloop_trainlogic(function(dtime)
|
||||||
--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.save_timer=advtrains.save_timer-dtime
|
|
||||||
if advtrains.save_timer<=0 then
|
|
||||||
local t=os.clock()
|
|
||||||
--save
|
|
||||||
advtrains.save()
|
|
||||||
advtrains.save_timer=advtrains.save_interval
|
|
||||||
atprintbm("saving", t)
|
|
||||||
end
|
|
||||||
--build a table of all players indexed by pts. used by damage and door system.
|
--build a table of all players indexed by pts. used by damage and door system.
|
||||||
advtrains.playersbypts={}
|
advtrains.playersbypts={}
|
||||||
for _, player in pairs(minetest.get_connected_players()) do
|
for _, player in pairs(minetest.get_connected_players()) do
|
||||||
|
@ -80,7 +64,7 @@ minetest.register_globalstep(function(dtime_mt)
|
||||||
|
|
||||||
atprintbm("trainsteps", t)
|
atprintbm("trainsteps", t)
|
||||||
endstep()
|
endstep()
|
||||||
end)
|
end
|
||||||
|
|
||||||
minetest.register_on_joinplayer(function(player)
|
minetest.register_on_joinplayer(function(player)
|
||||||
local pname=player:get_player_name()
|
local pname=player:get_player_name()
|
||||||
|
|
|
@ -128,7 +128,7 @@ minetest.register_chatcommand("itm_cache_ndb", {
|
||||||
})
|
})
|
||||||
|
|
||||||
local timer=0
|
local timer=0
|
||||||
minetest.register_globalstep(function(dtime)
|
function advtrains_itm_mainloop(dtime)
|
||||||
timer=timer-math.min(dtime, 0.1)
|
timer=timer-math.min(dtime, 0.1)
|
||||||
if timer<=0 then
|
if timer<=0 then
|
||||||
for pname,d in pairs(itm_pdata) do
|
for pname,d in pairs(itm_pdata) do
|
||||||
|
@ -143,5 +143,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
--automatically run itm_cache_ndb
|
function advtrains_itm_init()
|
||||||
minetest.after(2, cache_ndb)
|
--automatically run itm_cache_ndb
|
||||||
|
minetest.after(2, cache_ndb)
|
||||||
|
end
|
||||||
|
|
|
@ -40,25 +40,28 @@ dofile(mp.."/chatcmds.lua")
|
||||||
|
|
||||||
|
|
||||||
local filename=minetest.get_worldpath().."/advtrains_luaautomation"
|
local filename=minetest.get_worldpath().."/advtrains_luaautomation"
|
||||||
local file, err = io.open(filename, "r")
|
|
||||||
if not file then
|
function atlatc.load()
|
||||||
minetest.log("error", " Failed to read advtrains_luaautomation save data from file "..filename..": "..(err or "Unknown Error"))
|
local file, err = io.open(filename, "r")
|
||||||
else
|
if not file then
|
||||||
atprint("luaautomation reading file:",filename)
|
minetest.log("error", " Failed to read advtrains_luaautomation save data from file "..filename..": "..(err or "Unknown Error"))
|
||||||
local tbl = minetest.deserialize(file:read("*a"))
|
|
||||||
if type(tbl) == "table" then
|
|
||||||
if tbl.version==1 then
|
|
||||||
for envname, data in pairs(tbl.envs) do
|
|
||||||
atlatc.envs[envname]=atlatc.env_load(envname, data)
|
|
||||||
end
|
|
||||||
atlatc.active.load(tbl.active)
|
|
||||||
atlatc.interrupt.load(tbl.interrupt)
|
|
||||||
atlatc.pcnaming.load(tbl.pcnaming)
|
|
||||||
end
|
|
||||||
else
|
else
|
||||||
minetest.log("error", " Failed to read advtrains_luaautomation save data from file "..filename..": Not a table!")
|
atprint("luaautomation reading file:",filename)
|
||||||
|
local tbl = minetest.deserialize(file:read("*a"))
|
||||||
|
if type(tbl) == "table" then
|
||||||
|
if tbl.version==1 then
|
||||||
|
for envname, data in pairs(tbl.envs) do
|
||||||
|
atlatc.envs[envname]=atlatc.env_load(envname, data)
|
||||||
|
end
|
||||||
|
atlatc.active.load(tbl.active)
|
||||||
|
atlatc.interrupt.load(tbl.interrupt)
|
||||||
|
atlatc.pcnaming.load(tbl.pcnaming)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
minetest.log("error", " Failed to read advtrains_luaautomation save data from file "..filename..": Not a table!")
|
||||||
|
end
|
||||||
|
file:close()
|
||||||
end
|
end
|
||||||
file:close()
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- run init code of all environments
|
-- run init code of all environments
|
||||||
|
@ -94,21 +97,14 @@ atlatc.save = function()
|
||||||
file:close()
|
file:close()
|
||||||
end
|
end
|
||||||
|
|
||||||
minetest.register_on_shutdown(atlatc.save)
|
|
||||||
|
|
||||||
-- globalstep for step code
|
-- globalstep for step code
|
||||||
local timer, step_int=0, 2
|
local timer, step_int=0, 2
|
||||||
local stimer, sstep_int=0, 10
|
|
||||||
|
|
||||||
minetest.register_globalstep(function(dtime)
|
function atlatc.mainloop_stepcode(dtime)
|
||||||
timer=timer+dtime
|
timer=timer+dtime
|
||||||
if timer>step_int then
|
if timer>step_int then
|
||||||
timer=0
|
timer=0
|
||||||
atlatc.run_stepcode()
|
atlatc.run_stepcode()
|
||||||
end
|
end
|
||||||
stimer=stimer+dtime
|
|
||||||
if stimer>sstep_int then
|
|
||||||
stimer=0
|
|
||||||
atlatc.save()
|
|
||||||
end
|
|
||||||
end)
|
end)
|
||||||
|
|
|
@ -21,7 +21,7 @@ function iq.add(t, pos, evtdata)
|
||||||
run=true
|
run=true
|
||||||
end
|
end
|
||||||
|
|
||||||
minetest.register_globalstep(function(dtime)
|
function iq.mainloop(dtime)
|
||||||
if run then
|
if run then
|
||||||
timer=timer + math.min(dtime, 0.2)
|
timer=timer + math.min(dtime, 0.2)
|
||||||
for i=1,#queue do
|
for i=1,#queue do
|
||||||
|
|
Loading…
Reference in New Issue