tool.lua 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. -- All weapons can use this single function, the last three variables should be provided by the weapon def.
  2. -- Sound is the sound the weapon makes when used.
  3. -- Dist is the max_hear_distance for the sound.
  4. lobby.on_weapon_use = function(itemstack, player, pointed_thing)
  5. local physics_table = player:get_physics_override()
  6. if physics_table.speed ~= 0 then
  7. local traitor = player:get_player_name()
  8. local obj = pointed_thing.ref
  9. if obj and pointed_thing.type == "object" then
  10. local player_name = obj:get_player_name()
  11. if player_name ~= '' then
  12. local map_id = lobby.game[traitor]
  13. local count = lobby.map[map_id]
  14. if count > 0 then
  15. local weapon = itemstack:get_name()
  16. local def = minetest.registered_nodes[weapon]
  17. local sound = def._sound or ''
  18. local max_dist = def._hear_dist or 16
  19. local cooldown = 20
  20. local chance = 100
  21. local data = lobby.savedata.data[map_id]
  22. if data then
  23. if data.cooldown then
  24. cooldown = data.cooldown
  25. end
  26. if data.chance then
  27. chance = data.chance
  28. end
  29. end
  30. if sound ~= '' then
  31. minetest.sound_play(sound, {pos = player:get_pos(), gain = 1, max_hear_distance = max_dist})
  32. end
  33. if math.random(100) <= chance then
  34. local victim = minetest.get_player_by_name(player_name)
  35. local death_pos = victim:get_pos()
  36. minetest.sound_play('lobby_kill', {pos = death_pos, gain = 1, max_hear_distance = 16})
  37. victim:set_hp(0)
  38. else
  39. minetest.chat_send_player(traitor, 'You missed!')
  40. cooldown = math.floor(cooldown / 4)
  41. end
  42. local player_inv = player:get_inventory()
  43. minetest.chat_send_player(traitor, 'Weapon Cooldown for '..cooldown..' seconds.')
  44. minetest.after(cooldown, function()
  45. local map_id = lobby.game[traitor]
  46. if map_id ~= 'lobby' then
  47. player_inv:add_item('main', weapon)
  48. end
  49. end)
  50. elseif count == 0 then
  51. lobby.traitor_win(traitor, map_id)
  52. end
  53. itemstack:take_item()
  54. return itemstack
  55. end
  56. --else
  57. --The weapon could do stuff if not pointing at a player, make noise, or even trigger the cooldown. :P
  58. end
  59. end
  60. end
  61. minetest.register_node('lobby:sword', {
  62. description = 'Broadsword\nRange: 7',
  63. drawtype = 'mesh',
  64. mesh = 'lobby_sword.obj',
  65. tiles = {'lobby_sword.png'},
  66. inventory_image = 'lobby_sword_inv.png',
  67. range = 7,
  68. _sound = 'lobby_swoosh1',
  69. _hear_dist = 8,
  70. node_placement_prediction = '',
  71. on_use = lobby.on_weapon_use,
  72. on_place = function(pos)
  73. return nil
  74. end,
  75. on_drop = lobby.no_drop,
  76. groups = {not_in_creative_inventory=1, weapon=1},
  77. })
  78. minetest.register_node('lobby:shank', {
  79. description = 'Goblin Shank\nRange: 5',
  80. drawtype = 'mesh',
  81. mesh = 'lobby_shank.obj',
  82. tiles = {'lobby_shank.png'},
  83. inventory_image = 'lobby_shank_inv.png',
  84. range = 5,
  85. groups = {not_in_creative_inventory=1, weapon=1},
  86. on_use = lobby.on_weapon_use,
  87. on_place = function(pos)
  88. return nil
  89. end,
  90. on_drop = lobby.no_drop
  91. })
  92. minetest.register_node('lobby:yoyo', {
  93. description = 'Killer Yo-yo\nRange: 6',
  94. drawtype = 'mesh',
  95. mesh = 'lobby_yoyo.obj',
  96. tiles = {'lobby_yoyo.png'},
  97. inventory_image = 'lobby_yoyo_inv.png',
  98. range = 6,
  99. _sound = 'lobby_swoosh1',
  100. _hear_dist = 8,
  101. groups = {not_in_creative_inventory=1, weapon=1},
  102. on_use = lobby.on_weapon_use,
  103. on_place = function(pos)
  104. return nil
  105. end,
  106. on_drop = lobby.no_drop
  107. })
  108. minetest.register_node('lobby:battleaxe', {
  109. description = 'Battleaxe\nRange: 8',
  110. drawtype = 'mesh',
  111. mesh = 'lobby_battleaxe.obj',
  112. tiles = {'lobby_battleaxe_uv.png'},
  113. inventory_image = 'lobby_battleaxe_inv.png',
  114. range = 8,
  115. _sound = 'lobby_swoosh1',
  116. _hear_dist = 8,
  117. node_placement_prediction = '',
  118. on_use = lobby.on_weapon_use,
  119. on_place = function(pos)
  120. return nil
  121. end,
  122. on_drop = lobby.no_drop,
  123. groups = {not_in_creative_inventory=1, weapon=1},
  124. })
  125. minetest.create_detached_inventory('weapons_list', {
  126. allow_put = function(inv, listname, index, stack, player2)
  127. return -1
  128. end,
  129. allow_take = function(inv, listname, index, stack, player2)
  130. local name = player2 and player2:get_player_name() or ''
  131. if not creative.is_enabled_for(name) then
  132. return 0
  133. end
  134. return -1
  135. end
  136. })
  137. local stashable = {}
  138. minetest.register_on_mods_loaded(function()
  139. for name, def in pairs(minetest.registered_items) do
  140. local group = def.groups or {}
  141. if group.weapon then
  142. stashable[name] = def
  143. end
  144. end
  145. local list = {}
  146. for name, def in pairs(stashable) do
  147. list[#list+1] = name
  148. end
  149. local stash_inv = minetest.get_inventory({type = 'detached', name = 'weapons_list'})
  150. stash_inv:set_size('main', #list)
  151. stash_inv:set_list('main', list)
  152. end)