nodes_active.lua 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. local esc = minetest.formspec_escape
  2. local function button_create(id, name, player_count, xp, pos, desc)
  3. local formspec =
  4. 'formspec_version[3]'..
  5. 'size[12,12]'..
  6. 'field[0.5,1;5,.5;ID;ID Must not contain spaces or special characters;'..esc(id)..']'..
  7. 'field[6,1;5,.5;name;Map Name;'..esc(name)..']'..
  8. 'field[0.5,2;3,.5;player_count;Required # of Players;'..esc(player_count)..']'..
  9. 'field[4,2;3,.5;xp;Required XP;'..esc(xp)..']'..
  10. 'field[7.5,2;3,.5;pos;Spawn POS (x,y,z);'..esc(pos)..']'..
  11. 'textarea[0.5,3;10,3;desc;Description of map;'..esc(desc)..']'..
  12. 'button_exit[3,7;2,1;save;Submit]'
  13. return formspec
  14. end
  15. local function button_display(id, name, player_count, xp, pos, desc)
  16. local formspec =
  17. 'formspec_version[3]'..
  18. 'size[12,6]'..
  19. 'textarea[0.5,1;5,2;;Map Name;'..esc(name)..']'..
  20. 'textarea[0.5,2;5,2;;Required # of Players;'..esc(player_count)..']'..
  21. 'textarea[0.5,3;5,2;;Required XP;'..esc(xp)..']'..
  22. 'textarea[0.5,4;10,3;;Description of map;'..esc(desc)..']'
  23. return formspec
  24. end
  25. minetest.register_node('lobby:button_0', {
  26. description = 'Unconfigured Button',
  27. tiles = {'lobby_button_0.png'},
  28. groups = {breakable=1},
  29. light_source = 5,
  30. after_place_node = function(pos, placer)
  31. local meta = minetest.get_meta(pos)
  32. meta:set_string('owner', placer:get_player_name())
  33. meta:set_string('infotext', 'Unconfigured button')
  34. meta:set_string('id', '')
  35. meta:set_string('name', '')
  36. meta:set_string('player_count', '')
  37. meta:set_string('xp', '')
  38. meta:set_string('pos', '0,0,0')
  39. meta:set_string('desc', '')
  40. end,
  41. on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
  42. local player_name = clicker:get_player_name()
  43. local meta = minetest.get_meta(pos)
  44. local owner = meta:get_string('owner')
  45. local id = meta:get_string('id')
  46. local name = meta:get_string('name')
  47. local player_count = meta:get_string('player_count')
  48. local xp = meta:get_string('xp')
  49. local pos = meta:get_string('pos')
  50. local desc = meta:get_string('desc')
  51. meta:set_string('formspec', button_create(id, name, player_count, xp, pos, desc))
  52. end,
  53. on_receive_fields = function(pos, formname, fields, sender)
  54. local meta = minetest.get_meta(pos)
  55. local owner = meta:get_string('owner')
  56. if fields ['save'] then
  57. local player_name = sender:get_player_name()
  58. meta:set_string('infotext', fields.name)
  59. meta:set_string('id', fields.ID)
  60. meta:set_string('name', fields.name)
  61. meta:set_string('player_count', fields.player_count)
  62. meta:set_string('xp', fields.xp)
  63. meta:set_string('pos', fields.pos)
  64. meta:set_string('desc', fields.desc)
  65. local map_id = fields.ID
  66. local dist = tonumber(minetest.settings:get("map_generation_limit") or 31000)
  67. local x, y, z = string.match(fields.pos, "^(-?%d+),(-?%d+),(-?%d+)$")
  68. local pos_x = tonumber(x) or 50000
  69. local pos_y = tonumber(y) or 50000
  70. local pos_z = tonumber(z) or 50000
  71. local saveable = 0
  72. if pos_x > dist or pos_x < -dist or pos_y > dist or pos_y < -dist or pos_z > dist or pos_z < -dist then
  73. minetest.chat_send_player(player_name, 'Double check your spawn pos, values seem to be invalid.')
  74. saveable = 1
  75. end
  76. if lobby.savedata.IDs[map_id] then
  77. minetest.chat_send_player(player_name, 'That ID is already in use, you MUST change the ID.')
  78. saveable = 1
  79. end
  80. if saveable == 0 then
  81. local data = {}
  82. data.level_pos = {x = pos_x, y = pos_y+2, z = pos_z}
  83. data.xp = tonumber(fields.xp)
  84. lobby.savedata.IDs[map_id] = true
  85. lobby.savedata.data[map_id] = data
  86. minetest.swap_node(pos, {name = 'lobby:button_1'})
  87. end
  88. end
  89. end,
  90. })
  91. minetest.register_node('lobby:button_1', {
  92. description = 'Configured Button',
  93. tiles = {'lobby_button_1.png'},
  94. groups = {not_in_creative_inventory=1},
  95. drop = 'lobby:button_0',
  96. light_source = 14,
  97. on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
  98. local meta = minetest.get_meta(pos)
  99. local id = meta:get_string('id')
  100. local item = itemstack:get_name()
  101. if item ~= 'creative:tool_breaking' then
  102. local player_name = clicker:get_player_name()
  103. local owner = meta:get_string('owner')
  104. local name = meta:get_string('name')
  105. local player_count = meta:get_string('player_count')
  106. local xp = meta:get_string('xp')
  107. local pos = meta:get_string('pos')
  108. local desc = meta:get_string('desc')
  109. meta:set_string('formspec', button_display(id, name, player_count, xp, pos, desc))
  110. else
  111. minetest.swap_node(pos, {name = 'lobby:button_0'})
  112. lobby.savedata.IDs[id] = false
  113. end
  114. end,
  115. on_punch = function(pos, node, puncher, pointed_thing)
  116. local meta = minetest.get_meta(pos)
  117. local map_id = meta:get_string('id')
  118. if minetest.check_player_privs(puncher:get_player_name(), { creative = true }) then
  119. local game_data = lobby.savedata.data[map_id]
  120. local game_pos = game_data['level_pos']
  121. puncher:setpos({x=game_pos.x+(math.random(-2,2)),y=game_pos.y,z=game_pos.z+(math.random(-2,2))})
  122. else
  123. local needed_players = tonumber(meta:get_string('player_count'))
  124. local objs = minetest.get_objects_inside_radius(pos, 4)
  125. local player_count = 0
  126. local map_players = {}
  127. for _, obj in pairs(objs) do
  128. if obj:is_player() then
  129. player_count = player_count + 1
  130. local player_name = obj:get_player_name()
  131. table.insert(map_players, player_name)
  132. end
  133. end
  134. if player_count >= needed_players then
  135. if lobby.map[map_id] == 0 or lobby.map[map_id] == nil then
  136. minetest.chat_send_player(puncher:get_player_name(), 'you can start the game!')
  137. lobby.map[map_id] = player_count
  138. local traitor = math.random(1,player_count)
  139. lobby.xp[map_id] = 0
  140. lobby.votes[map_id] = 0
  141. lobby.corpses[map_id] = {}
  142. for i=1,player_count do
  143. minetest.chat_send_player(map_players[i], 'Hold tight, loading the level.')
  144. lobby.voted[map_players[i]] = true
  145. if i == traitor then
  146. minetest.chat_send_player(map_players[i], 'You\'re the imposter. Try to kill all the other players.')
  147. minetest.chat_send_player(map_players[i], 'You\'ll get a weapon in a few seconds.')
  148. lobby.traitors[map_id] = map_players[i]
  149. local player = minetest.get_player_by_name(map_players[i])
  150. local player_inv = player:get_inventory()
  151. minetest.after(10, function()
  152. player_inv:add_item('main', 'lobby:shank')
  153. end)
  154. end
  155. local player = minetest.get_player_by_name(map_players[i])
  156. local game_data = lobby.savedata.data[map_id]
  157. local game_pos = game_data['level_pos']
  158. local privs = {}
  159. lobby.game[map_players[i]] = map_id
  160. player:setpos({x=game_pos.x+(math.random(-2,2)),y=game_pos.y,z=game_pos.z+(math.random(-2,2))})
  161. privs.interact = true
  162. privs.shout = true
  163. minetest.set_player_privs(map_players[i], privs)
  164. end
  165. else
  166. minetest.chat_send_player(puncher:get_player_name(), 'You can\'t join an in progress game.')
  167. end
  168. else
  169. minetest.chat_send_player(puncher:get_player_name(), 'Maybe not enough players? Right click the button for more information.')
  170. end
  171. end
  172. end,
  173. })