2017-02-02 16:40:51 +01:00
-- advtrains_luaautomation/init.lua
-- Lua automation features for advtrains
-- Uses global table 'atlatc' (AdvTrains_LuaATC)
-- Boilerplate to support localized strings if intllib mod is installed.
2019-11-06 21:20:26 +01:00
if intllib then
2017-02-02 16:40:51 +01:00
atltrans = intllib.Getter ( )
else
atltrans = function ( s , a , ... ) a = { a , ... } return s : gsub ( " @(%d+) " , function ( n ) return a [ tonumber ( n ) ] end ) end
end
--Privilege
--Only trusted players should be enabled to build stuff which can break the server.
atlatc = { envs = { } }
minetest.register_privilege ( " atlatc " , { description = " Player can place and modify LUA ATC components. Grant with care! Allows to execute bad LUA code. " , give_to_singleplayer = false , default = false } )
2017-03-30 21:59:30 +02:00
--assertt helper. error if a variable is not of a type
function assertt ( var , typ )
if type ( var ) ~= typ then
error ( " Assertion failed, variable has to be of type " .. typ )
end
end
2017-02-02 16:40:51 +01:00
local mp = minetest.get_modpath ( " advtrains_luaautomation " )
if not mp then
error ( " Mod name error: Mod folder is not named 'advtrains_luaautomation'! " )
end
dofile ( mp .. " /environment.lua " )
dofile ( mp .. " /interrupt.lua " )
dofile ( mp .. " /active_common.lua " )
dofile ( mp .. " /atc_rail.lua " )
dofile ( mp .. " /operation_panel.lua " )
2017-02-28 14:38:59 +01:00
dofile ( mp .. " /pcnaming.lua " )
2020-07-07 14:25:47 +02:00
2017-02-02 21:14:20 +01:00
dofile ( mp .. " /chatcmds.lua " )
2017-02-02 16:40:51 +01:00
local filename = minetest.get_worldpath ( ) .. " /advtrains_luaautomation "
2017-04-29 19:13:15 +02:00
function atlatc . load ( )
local file , err = io.open ( filename , " r " )
if not file then
2019-12-18 10:01:47 +01:00
minetest.log ( " warning " , " Failed to read advtrains_luaautomation save data from file " .. filename .. " : " .. ( err or " Unknown Error " ) )
minetest.log ( " warning " , " (this is normal when first enabling advtrains on this world) " )
2017-04-29 19:13:15 +02:00
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 )
2017-02-02 16:40:51 +01:00
end
2017-04-29 19:13:15 +02:00
else
minetest.log ( " error " , " Failed to read advtrains_luaautomation save data from file " .. filename .. " : Not a table! " )
2017-02-02 16:40:51 +01:00
end
2017-04-29 19:13:15 +02:00
file : close ( )
2017-02-02 16:40:51 +01:00
end
2017-04-29 19:44:43 +02:00
-- run init code of all environments
atlatc.run_initcode ( )
2017-02-02 16:40:51 +01:00
end
atlatc.save = function ( )
--versions:
-- 1 - Initial save format.
local envdata = { }
for envname , env in pairs ( atlatc.envs ) do
envdata [ envname ] = env : save ( )
end
local save_tbl = {
version = 1 ,
envs = envdata ,
active = atlatc.active . save ( ) ,
interrupt = atlatc.interrupt . save ( ) ,
2017-02-28 14:38:59 +01:00
pcnaming = atlatc.pcnaming . save ( ) ,
2017-02-02 16:40:51 +01:00
}
local datastr = minetest.serialize ( save_tbl )
if not datastr then
minetest.log ( " error " , " Failed to save advtrains_luaautomation save data to file " .. filename .. " : Can't serialize! " )
return
end
local file , err = io.open ( filename , " w " )
if err then
minetest.log ( " error " , " Failed to save advtrains_luaautomation save data to file " .. filename .. " : " .. ( err or " Unknown Error " ) )
return
end
file : write ( datastr )
file : close ( )
end
2017-02-04 18:35:34 +01:00
2017-02-02 16:40:51 +01:00
-- globalstep for step code
local timer , step_int = 0 , 2
2017-04-29 19:13:15 +02:00
function atlatc . mainloop_stepcode ( dtime )
2017-02-02 16:40:51 +01:00
timer = timer + dtime
if timer > step_int then
timer = 0
atlatc.run_stepcode ( )
end
2017-04-29 19:44:43 +02:00
end