pregame.lua 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. local S = minetest.get_translator("the_end_game_master")
  2. local intramod = the_end_game_master.intramod
  3. intramod.huds = {}
  4. local function set_pregame_hud(player)
  5. local name = player:get_player_name()
  6. local hud = intramod.huds[name]
  7. if not hud
  8. then
  9. hud = {}
  10. intramod.huds = hud
  11. end
  12. table.insert(hud,
  13. player:hud_add(
  14. {
  15. hud_elem_type = "image",
  16. position = {x = 0.5, y = 0.5},
  17. scale = {x = -100, y = -100},
  18. name = "pregame_background",
  19. text = "the_end_game_master_pregame_background.png",
  20. })
  21. )
  22. table.insert(hud,
  23. player:hud_add(
  24. {
  25. hud_elem_type = "image",
  26. position = {x = 0.5, y = 0.15},
  27. scale = {x = -40, y = -15},
  28. name = "pregame_logo",
  29. text = "the_end_game_master_header.png",
  30. })
  31. )
  32. table.insert(hud,
  33. player:hud_add(
  34. {
  35. hud_elem_type = "text",
  36. position = {x = 0.5, y = 0.75},
  37. name = "pregame_inv_hint",
  38. text = S("Open your inventory to start"),
  39. number = 0xFFFFFF
  40. })
  41. )
  42. intramod.huds[name] = hud
  43. player:hud_set_flags(
  44. {
  45. hotbar = false,
  46. healthbar = false,
  47. crosshair = false,
  48. wielditem = false,
  49. breathbar = false,
  50. })
  51. end
  52. minetest.register_on_leaveplayer(function(player)
  53. intramod.huds[player:get_player_name()] = nil
  54. end)
  55. local ready_players = {}
  56. local function set_pregame_inv(player)
  57. local playername = player:get_player_name()
  58. local ready_affecting_button = ready_players[playername] and
  59. "button[4.5,5;3,1;vote_not_ready;" ..
  60. minetest.formspec_escape(S("No longer Ready")) .. "]" or
  61. "button[4.5,5;3,1;vote_ready;" ..
  62. minetest.formspec_escape(S("Ready")) .. "]"
  63. local formspec = "size[12,8]" ..
  64. "real_coordinates[true]" ..
  65. ready_affecting_button..
  66. --"button[1,6;3,1;choose_appearance;Choose Appearance]" ..
  67. --"button[4.5,6;3,1;choose_beard;Choose Beard]" ..
  68. "button[4.5,3;3,1;guide;" .. S("Guide") .. "]"
  69. player:set_inventory_formspec(formspec)
  70. return formspec
  71. end
  72. local function show_404_page(player)
  73. local formspec = "size[12,12]" ..
  74. "real_coordinates[true]" ..
  75. "image[0,0.5;12,2;the_end_game_master_header.png]" ..
  76. "label[2,6;404 Error: This page does not exist and is propably under construction]"
  77. minetest.show_formspec(player:get_player_name(),
  78. "the_end_game_master:404_page",
  79. formspec)
  80. end
  81. local music = {}
  82. local function start_pregame_music(player)
  83. local name = player:get_player_name()
  84. music[name] = minetest.sound_play("mood_music_level1",
  85. {
  86. to_player = name,
  87. loop = true,
  88. })
  89. end
  90. function the_end_game_master.stop_pregame_music(player)
  91. local name = player:get_player_name()
  92. if music[name]
  93. then
  94. minetest.sound_stop(music[name])
  95. music[name] = nil
  96. end
  97. end
  98. minetest.register_on_leaveplayer(function(player)
  99. music[player:get_player_name()] = nil
  100. end)
  101. local function vote_ready(player)
  102. local name = player:get_player_name()
  103. ready_players[name] = true
  104. local player_count = #minetest.get_connected_players()
  105. local ready_count = 0
  106. for _, _ in pairs(ready_players)
  107. do
  108. ready_count = ready_count + 1
  109. end
  110. local ready_string = S("@1 is ready!\n"..
  111. "@2 / @3 players are ready.", name, ready_count, player_count)
  112. if ready_count >= player_count * 0.75 --start game
  113. then
  114. minetest.chat_send_all(ready_string .. S("\nLoading map..."))
  115. the_end_game_master.start_game()
  116. return
  117. end
  118. minetest.chat_send_all(ready_string)
  119. set_pregame_inv(player)
  120. end
  121. local function vote_not_ready(player)
  122. local name = player:get_player_name()
  123. ready_players[name] = nil
  124. local player_count = #minetest.get_connected_players()
  125. local ready_count = 0
  126. for _, _ in pairs(ready_players)
  127. do
  128. ready_count = ready_count + 1
  129. end
  130. minetest.chat_send_all(S("@1 is no longer ready.\n@2 / @3 players are ready.",
  131. name, ready_count, player_count))
  132. set_pregame_inv(player)
  133. end
  134. minetest.register_on_leaveplayer(vote_not_ready)
  135. minetest.register_on_player_receive_fields(function(player, formname, fields)
  136. local stage = the_end_game_master.get_stage()
  137. if formname ~= "" or sfinv.enabled or not (stage == "pregame" or stage == "loading")
  138. then
  139. return false
  140. end
  141. if fields.vote_ready
  142. then
  143. vote_ready(player)
  144. elseif fields.vote_not_ready
  145. then
  146. vote_not_ready(player)
  147. elseif fields.choose_appearance
  148. then
  149. show_404_page(player)
  150. elseif fields.choose_beard
  151. then
  152. show_404_page(player)
  153. elseif fields.guide
  154. then
  155. in_game_guide.show_root_page(player:get_player_name(), "return_to_pregame_inv")
  156. end
  157. return false
  158. end)
  159. minetest.register_on_player_receive_fields(function(player, formname, fields)
  160. local stage = the_end_game_master.get_stage()
  161. if fields.return_to_pregame_inv and (stage == "pregame" or stage == "loading")
  162. then
  163. minetest.show_formspec(player:get_player_name(),
  164. "", set_pregame_inv(player))
  165. return true
  166. end
  167. return false
  168. end)
  169. local function clear_inv(player)
  170. local inv = player:get_inventory()
  171. if inv
  172. then
  173. inv:set_list("hand", {ItemStack("limbo_respawning:limbohand")})
  174. inv:set_list("main", {})
  175. end
  176. end
  177. local function set_pregame_physics(player)
  178. player:set_physics_override(
  179. {
  180. speed = 0,
  181. jump = 0,
  182. gravity = 0,
  183. sneak = false,
  184. })
  185. end
  186. local function set_player_to_pregame(player)
  187. set_pregame_hud(player)
  188. set_pregame_inv(player)
  189. clear_inv(player)
  190. set_pregame_physics(player)
  191. start_pregame_music(player)
  192. end
  193. function the_end_game_master.set_pregame()
  194. the_end_game_master.set_stage("pregame")
  195. ready_players = {}
  196. --move players to lobby box
  197. movable_spawnpoint.set_spawnpoint({x = 0, y = -24, z = 0})
  198. in_game_guide.set_root_page("the_end_gameplay_guide_master:home")
  199. in_game_guide.set_root_page_back_button_name("return_to_pregame_inv")
  200. --TODO: give options like appearance editor and beard/kit chooser
  201. --remove status effects
  202. mob_spawning_director.disable()
  203. sfinv.enabled = false
  204. end
  205. minetest.register_on_joinplayer(function(player)
  206. local stage = the_end_game_master.get_stage()
  207. if stage == "pregame" or stage == "loading"
  208. then
  209. movable_spawnpoint.spawn_player(player)
  210. set_player_to_pregame(player)
  211. end
  212. end)
  213. limbo_respawning.register_on_respawn(function(playername)
  214. local player = minetest.get_player_by_name(playername)
  215. local stage = the_end_game_master.get_stage()
  216. if player
  217. then
  218. movable_spawnpoint.spawn_player(player)
  219. if stage ~= "game"
  220. then
  221. set_player_to_pregame(player)
  222. end
  223. end
  224. end)