aboutsummaryrefslogtreecommitdiff
path: root/advtrains
diff options
context:
space:
mode:
authororwell96 <mono96.mml@gmail.com>2017-04-29 19:13:15 +0200
committerorwell96 <mono96.mml@gmail.com>2017-04-29 19:13:15 +0200
commit331db7ba5f4e4b308c1f8efa93c2b40c5139766d (patch)
treec9dc5d02d6c1986ef420004f94154c61be0cc1a9 /advtrains
parent337db2a573ccaa1672e713855022e96106679803 (diff)
downloadadvtrains-331db7ba5f4e4b308c1f8efa93c2b40c5139766d.tar.gz
advtrains-331db7ba5f4e4b308c1f8efa93c2b40c5139766d.tar.bz2
advtrains-331db7ba5f4e4b308c1f8efa93c2b40c5139766d.zip
Unify globalstep, load and save of all components into single functions
Diffstat (limited to 'advtrains')
-rw-r--r--advtrains/advtrains/init.lua154
-rw-r--r--advtrains/advtrains/nocrash.lua0
-rw-r--r--advtrains/advtrains/trainlogic.lua20
-rw-r--r--advtrains/advtrains_itrainmap/init.lua8
-rw-r--r--advtrains/advtrains_luaautomation/init.lua44
-rw-r--r--advtrains/advtrains_luaautomation/interrupt.lua2
6 files changed, 140 insertions, 88 deletions
diff --git a/advtrains/advtrains/init.lua b/advtrains/advtrains/init.lua
index 970bbb6..4098729 100644
--- a/advtrains/advtrains/init.lua
+++ b/advtrains/advtrains/init.lua
@@ -94,56 +94,58 @@ dofile(advtrains.modpath.."/craft_items.lua")
--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.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
-advtrains.save = function()
+advtrains.avt_save = function()
--atprint("saving")
advtrains.invalidate_all_paths()
@@ -192,4 +194,72 @@ advtrains.save = function()
file:write(datastr)
file:close()
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)
diff --git a/advtrains/advtrains/nocrash.lua b/advtrains/advtrains/nocrash.lua
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/advtrains/advtrains/nocrash.lua
diff --git a/advtrains/advtrains/trainlogic.lua b/advtrains/advtrains/trainlogic.lua
index d9edd6d..98a0e2b 100644
--- a/advtrains/advtrains/trainlogic.lua
+++ b/advtrains/advtrains/trainlogic.lua
@@ -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_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)
- --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(function(dtime)
- 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.
advtrains.playersbypts={}
for _, player in pairs(minetest.get_connected_players()) do
@@ -80,7 +64,7 @@ minetest.register_globalstep(function(dtime_mt)
atprintbm("trainsteps", t)
endstep()
-end)
+end
minetest.register_on_joinplayer(function(player)
local pname=player:get_player_name()
diff --git a/advtrains/advtrains_itrainmap/init.lua b/advtrains/advtrains_itrainmap/init.lua
index 96e30c5..85a3709 100644
--- a/advtrains/advtrains_itrainmap/init.lua
+++ b/advtrains/advtrains_itrainmap/init.lua
@@ -128,7 +128,7 @@ minetest.register_chatcommand("itm_cache_ndb", {
})
local timer=0
-minetest.register_globalstep(function(dtime)
+function advtrains_itm_mainloop(dtime)
timer=timer-math.min(dtime, 0.1)
if timer<=0 then
for pname,d in pairs(itm_pdata) do
@@ -143,5 +143,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
end
end)
---automatically run itm_cache_ndb
-minetest.after(2, cache_ndb)
+function advtrains_itm_init()
+ --automatically run itm_cache_ndb
+ minetest.after(2, cache_ndb)
+end
diff --git a/advtrains/advtrains_luaautomation/init.lua b/advtrains/advtrains_luaautomation/init.lua
index 71808e7..b24bc01 100644
--- a/advtrains/advtrains_luaautomation/init.lua
+++ b/advtrains/advtrains_luaautomation/init.lua
@@ -40,25 +40,28 @@ dofile(mp.."/chatcmds.lua")
local filename=minetest.get_worldpath().."/advtrains_luaautomation"
-local file, err = io.open(filename, "r")
-if not file then
- minetest.log("error", " Failed to read advtrains_luaautomation save data from file "..filename..": "..(err or "Unknown Error"))
-else
- 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)
+
+function atlatc.load()
+ local file, err = io.open(filename, "r")
+ if not file then
+ minetest.log("error", " Failed to read advtrains_luaautomation save data from file "..filename..": "..(err or "Unknown Error"))
+ else
+ 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
- atlatc.active.load(tbl.active)
- atlatc.interrupt.load(tbl.interrupt)
- atlatc.pcnaming.load(tbl.pcnaming)
+ else
+ minetest.log("error", " Failed to read advtrains_luaautomation save data from file "..filename..": Not a table!")
end
- else
- minetest.log("error", " Failed to read advtrains_luaautomation save data from file "..filename..": Not a table!")
+ file:close()
end
- file:close()
end
-- run init code of all environments
@@ -94,21 +97,14 @@ atlatc.save = function()
file:close()
end
-minetest.register_on_shutdown(atlatc.save)
-- globalstep for step code
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
if timer>step_int then
timer=0
atlatc.run_stepcode()
end
- stimer=stimer+dtime
- if stimer>sstep_int then
- stimer=0
- atlatc.save()
- end
end)
diff --git a/advtrains/advtrains_luaautomation/interrupt.lua b/advtrains/advtrains_luaautomation/interrupt.lua
index 4d59db5..fa340b8 100644
--- a/advtrains/advtrains_luaautomation/interrupt.lua
+++ b/advtrains/advtrains_luaautomation/interrupt.lua
@@ -21,7 +21,7 @@ function iq.add(t, pos, evtdata)
run=true
end
-minetest.register_globalstep(function(dtime)
+function iq.mainloop(dtime)
if run then
timer=timer + math.min(dtime, 0.2)
for i=1,#queue do