init.lua 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. -- advtrains_luaautomation/init.lua
  2. -- Lua automation features for advtrains
  3. -- Uses global table 'atlatc' (AdvTrains_LuaATC)
  4. atltrans = attrans
  5. local S = atltrans
  6. --Privilege
  7. --Only trusted players should be enabled to build stuff which can break the server.
  8. atlatc = { envs = {}}
  9. minetest.register_privilege("atlatc", { description = S("Can place and configure LuaATC components, including execute potentially harmful Lua code"), give_to_singleplayer = false, default= false })
  10. --Size of code input forms in X,Y notation. Must be at least 10x10
  11. atlatc.CODE_FORM_SIZE = "15,12"
  12. --Position of Error Label in Code Form
  13. atlatc.CODE_FORM_ERRLABELPOS = "0,12"
  14. --assertt helper. error if a variable is not of a type
  15. function assertt(var, typ)
  16. if type(var)~=typ then
  17. error("Assertion failed, variable has to be of type "..typ)
  18. end
  19. end
  20. local mp=minetest.get_modpath("advtrains_luaautomation")
  21. if not mp then
  22. error("Mod name error: Mod folder is not named 'advtrains_luaautomation'!")
  23. end
  24. dofile(mp.."/environment.lua")
  25. dofile(mp.."/interrupt.lua")
  26. dofile(mp.."/active_common.lua")
  27. dofile(mp.."/atc_rail.lua")
  28. dofile(mp.."/operation_panel.lua")
  29. if mesecon then
  30. dofile(mp.."/mesecon_controller.lua")
  31. end
  32. dofile(mp.."/pcnaming.lua")
  33. dofile(mp.."/chatcmds.lua")
  34. local filename=minetest.get_worldpath().."/advtrains_luaautomation"
  35. function atlatc.load(tbl)
  36. if tbl.version==1 then
  37. for envname, data in pairs(tbl.envs) do
  38. atlatc.envs[envname]=atlatc.env_load(envname, data)
  39. end
  40. atlatc.active.load(tbl.active)
  41. atlatc.interrupt.load(tbl.interrupt)
  42. atlatc.pcnaming.load(tbl.pcnaming)
  43. end
  44. -- run init code of all environments
  45. atlatc.run_initcode()
  46. end
  47. function atlatc.load_pre_v4()
  48. minetest.log("action", "[atlatc] Loading pre-v4 save file")
  49. local file, err = io.open(filename, "r")
  50. if not file then
  51. minetest.log("warning", " Failed to read advtrains_luaautomation save data from file "..filename..": "..(err or "Unknown Error"))
  52. minetest.log("warning", " (this is normal when first enabling advtrains on this world)")
  53. else
  54. atprint("luaautomation reading file:",filename)
  55. local tbl = minetest.deserialize(file:read("*a"))
  56. if type(tbl) == "table" then
  57. if tbl.version==1 then
  58. for envname, data in pairs(tbl.envs) do
  59. atlatc.envs[envname]=atlatc.env_load(envname, data)
  60. end
  61. atlatc.active.load(tbl.active)
  62. atlatc.interrupt.load(tbl.interrupt)
  63. atlatc.pcnaming.load(tbl.pcnaming)
  64. end
  65. else
  66. minetest.log("error", " Failed to read advtrains_luaautomation save data from file "..filename..": Not a table!")
  67. end
  68. file:close()
  69. end
  70. -- run init code of all environments
  71. atlatc.run_initcode()
  72. end
  73. atlatc.save = function()
  74. --versions:
  75. -- 1 - Initial save format.
  76. local envdata={}
  77. for envname, env in pairs(atlatc.envs) do
  78. envdata[envname]=env:save()
  79. end
  80. local save_tbl={
  81. version = 1,
  82. envs=envdata,
  83. active = atlatc.active.save(),
  84. interrupt = atlatc.interrupt.save(),
  85. pcnaming = atlatc.pcnaming.save(),
  86. }
  87. return save_tbl
  88. end
  89. --[[
  90. -- globalstep for step code
  91. local timer, step_int=0, 2
  92. function atlatc.mainloop_stepcode(dtime)
  93. timer=timer+dtime
  94. if timer>step_int then
  95. timer=0
  96. atlatc.run_stepcode()
  97. end
  98. end
  99. ]]