init.lua 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. the_end_game_master = {}
  2. local modpath = minetest.get_modpath("the_end_game_master")
  3. the_end_game_master.intramod = {}
  4. local storage = minetest.get_mod_storage()
  5. local stage = storage:get_string("stage")
  6. the_end_game_master.intramod.storage = storage
  7. dofile(modpath .. "/game_watcher.lua")
  8. dofile(modpath .. "/pregame.lua")
  9. dofile(modpath .. "/game.lua")
  10. the_end_game_master.intramod = nil
  11. function the_end_game_master.clear_mobs()
  12. minetest.clear_objects({mode = "full"})
  13. end
  14. function the_end_game_master.set_stage(s)
  15. stage = s
  16. storage:set_string("stage", s)
  17. end
  18. function the_end_game_master.get_stage()
  19. return stage
  20. end
  21. if stage == ""
  22. then
  23. the_end_game_master.set_pregame()
  24. elseif stage == "pregame" or stage == "loading"
  25. then
  26. the_end_game_master.set_pregame()
  27. elseif stage == "game"
  28. then
  29. in_game_guide.set_root_page("the_end_gameplay_guide_master:home")
  30. mob_spawning_director.enable()
  31. sfinv.enabled = true
  32. end
  33. minetest.register_on_player_hpchange(function(player, change, reason)
  34. if stage ~= "game" and reason.type ~= "respawn"
  35. then
  36. return 0
  37. else
  38. return change
  39. end
  40. end, true)
  41. --each game is given an id that is used to detect when a player is new
  42. local game_id = storage:get_int("game_id")
  43. function the_end_game_master.get_game_id()
  44. return game_id
  45. end
  46. function the_end_game_master.increase_game_id()
  47. game_id = game_id + 1
  48. storage:set_int("game_id", game_id)
  49. end
  50. local function store_game_id_in_playermeta(player)
  51. local meta = player:get_meta()
  52. meta:set_int("last_game_played", game_id)
  53. end
  54. minetest.register_on_leaveplayer(store_game_id_in_playermeta)
  55. minetest.register_on_shutdown(function()
  56. for _, p in ipairs(minetest.get_connected_players())
  57. do
  58. store_game_id_in_playermeta(p)
  59. end
  60. end)
  61. minetest.register_on_joinplayer(function(player)
  62. local meta = player:get_meta()
  63. local gid = meta:get_int("last_game_played")
  64. if gid ~= game_id
  65. then
  66. limbo_respawning.return_from_limbo(player)
  67. if stage == "game"
  68. then
  69. give_initial_stuff.give(player)
  70. end
  71. end
  72. end)
  73. --removing mobs that weren't loaded when a game ended
  74. defense_mob_api.register_on_default_get_staticdata(function(self)
  75. return "the_end_game", game_id
  76. end)
  77. defense_mob_api.register_on_default_activate(function(self, staticdata)
  78. local data = minetest.deserialize(staticdata)
  79. if data and data.the_end_game and data.the_end_game ~= game_id
  80. then
  81. self:remove()
  82. end
  83. end)