player_callbacks.lua 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. local function not_builder(player)
  2. local name = player:get_player_name()
  3. return not minetest.check_player_privs(name, { creative = true })
  4. end
  5. local function pro_player(player)
  6. local name = player:get_player_name()
  7. return minetest.check_player_privs(name, { pro_player = true })
  8. end
  9. minetest.register_on_joinplayer(function(player)
  10. local name = player:get_player_name()
  11. local player_attributes = player:get_meta()
  12. local max_hp = player_attributes:get_int('hp')
  13. if max_hp < 20 then
  14. player_attributes:set_int('hp', 20)
  15. else
  16. player:set_properties({hp_max = max_hp})
  17. player:set_hp(max_hp)
  18. end
  19. local privs = minetest.get_player_privs(name)
  20. privs.worldedit = nil
  21. minetest.set_player_privs(name, privs)
  22. player_attributes:set_string('mode', 'builder')
  23. lobby.suspect[name] = 0
  24. lobby.game[name] = 'lobby'
  25. lobby.voted[name] = true
  26. if not_builder(player) then
  27. local player_inv = player:get_inventory()
  28. player_inv:set_list('main', {})
  29. player:set_pos(lobby.spawn_pos)
  30. player_attributes:set_string('mode', 'solo')
  31. end
  32. if not pro_player(player) then
  33. player:set_pos(lobby.spawn_pos)
  34. minetest.show_formspec(name, 'lobby:help_1', lobby_help_1)
  35. end
  36. lobby.check_xp_queue(name)
  37. end)
  38. minetest.register_on_dieplayer(function(player)
  39. local name = player:get_player_name()
  40. local player_attributes = player:get_meta()
  41. local map_id = lobby.game[name]
  42. local mode = player_attributes:get_string('mode')
  43. local pos = vector.round(player:get_pos())
  44. player_attributes:set_string('spawn_pos', minetest.pos_to_string(pos))
  45. player:set_physics_override({speed=1})
  46. if map_id ~= 'lobby' and (mode == 'player' or mode == 'traitor') then
  47. local player_inv = player:get_inventory()
  48. player_inv:set_list('main', {})
  49. local traitor = lobby.traitors[map_id]
  50. local param2 = minetest.dir_to_facedir(player:get_look_dir())
  51. local pos_node = minetest.get_node(pos)
  52. local tone = player_attributes:get_int('tone')
  53. if tone == 0 then
  54. tone = 4
  55. end
  56. local corpse_col = 'lobby:corpse_'..tone
  57. if pos_node.name ~= 'air' then
  58. if minetest.get_item_group(pos_node.name, 'liquid') > 0 then
  59. minetest.set_node(pos, {name = corpse_col, param2 = param2})
  60. lobby.corpse_entry(pos, map_id)
  61. else
  62. local air = minetest.find_node_near(pos, 1, 'air')
  63. if air then
  64. minetest.set_node(air, {name = corpse_col, param2 = param2})
  65. lobby.corpse_entry(air, map_id)
  66. pos = air
  67. end
  68. end
  69. else
  70. minetest.set_node(pos, {name = corpse_col, param2 = param2})
  71. lobby.corpse_entry(pos, map_id)
  72. end
  73. local meta = minetest.get_meta(pos)
  74. meta:set_string('infotext', name..'\'s Corpse.')
  75. player:set_nametag_attributes({
  76. color = {a = 0, r = 255, g = 255, b = 255}
  77. })
  78. player:set_properties({visual_size = {x = 0, y = 0}, collisionbox = {-0.3, 0.0, -0.3, 0.3, .2, 0.3}})
  79. player_attributes:set_string('mode', 'ghost')
  80. minetest.chat_send_player(name, 'You\'re now a ghost. Return to the lobby with the /lobby chat command.')
  81. lobby.game[name] = map_id..'_ghost'
  82. if name == traitor then
  83. minetest.log('action', 'Traitor ('..name..') just died, resulting in team win.')
  84. lobby.team_win(map_id)
  85. else
  86. lobby.update_maps(map_id)
  87. end
  88. elseif mode == 'solo' or mode == 'ghost' then
  89. local player_inv = player:get_inventory()
  90. player_inv:set_list('main', {})
  91. end
  92. end)
  93. minetest.register_on_respawnplayer(function(player)
  94. local name = player:get_player_name()
  95. local player_attributes = player:get_meta()
  96. local pos = minetest.string_to_pos(player_attributes:get_string('spawn_pos'))
  97. local map_id = lobby.game[name]
  98. minetest.log('action', name..' just respawned on '..map_id)
  99. local mode = player_attributes:get_string('mode')
  100. player:set_armor_groups({immortal=1})
  101. minetest.chat_send_player(name, 'You are immortal for 10 seconds.')
  102. minetest.after(10, function()
  103. player:set_armor_groups({})
  104. minetest.chat_send_player(name, 'Immortallity has ended.')
  105. end)
  106. if map_id ~= 'lobby' and mode == 'ghost' then
  107. local real_id = string.sub(map_id, 0, -7)
  108. if lobby.map[real_id] and lobby.map[real_id] > 1 then
  109. player:set_pos(pos)
  110. return true
  111. else
  112. player:set_pos(lobby.spawn_pos)
  113. end
  114. elseif mode == 'builder' then
  115. player:set_pos(pos)
  116. return true
  117. else
  118. local player_attributes = player:get_meta()
  119. player:set_nametag_attributes({
  120. color = {a = 255, r = 255, g = 255, b = 255}
  121. })
  122. player:set_properties({visual_size = {x = 10, y = 10}, collisionbox = {-0.3, 0.0, -0.3, 0.3, 1.7, 0.3}})
  123. player:set_pos(lobby.spawn_pos)
  124. player_attributes:set_string('mode', 'solo')
  125. return true
  126. end
  127. end)
  128. minetest.register_on_leaveplayer(function(player, timed_out)
  129. local name = player:get_player_name()
  130. local map_id = lobby.game[name]
  131. local player_attributes = player:get_meta()
  132. local mode = player_attributes:get_string('mode')
  133. if map_id ~= 'lobby' and (mode == 'player' or mode == 'traitor') then
  134. local game_data = lobby.savedata.data[map_id]
  135. local map_name = game_data['map_name'] or map_id
  136. local traitor = lobby.traitors[map_id]
  137. minetest.chat_send_all('somebody quit the '..map_name..' level')
  138. local player_inv = player:get_inventory()
  139. player_inv:set_list('main', {})
  140. lobby.update_maps(map_id)
  141. if traitor == name then
  142. lobby.team_win(map_id)
  143. end
  144. end
  145. end)