functions.lua 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. minetest.register_on_player_receive_fields(function(player, formname, fields)
  2. if formname == 'sabotage:traitor' then
  3. local name = player:get_player_name()
  4. local map_id = lobby.game[name]
  5. local pos = sabotage.player_pos[name]
  6. local meta = minetest.get_meta(pos)
  7. if fields.sabotage then
  8. local sabotaged = lobby.sabotage[map_id]
  9. if sabotaged == true then
  10. minetest.chat_send_player(name, 'Sorry you can only sabotage one item at a time currently.')
  11. else
  12. local chance = meta:get_int('chance') or 1
  13. local damage = meta:get_int('damage') or 1
  14. local level = meta:get_int('level') or 1
  15. local player_attributes = player:get_meta()
  16. local luck = player_attributes:get_int('luck')
  17. if math.random(10) + luck >= chance then
  18. local hurt = math.random(damage)
  19. local health = player:get_hp()
  20. local new_health = health - hurt
  21. player:set_hp(new_health)
  22. end
  23. if math.random(10) + luck >= chance then
  24. local node = minetest.get_node(pos)
  25. local node_name = node.name
  26. local def = minetest.registered_nodes[node_name]
  27. local desc = def.description
  28. local flip = def.sabotage_switch
  29. if flip then
  30. minetest.swap_node(pos, {name = flip, param2 = node.param2})
  31. end
  32. meta:set_string('traitor', name)
  33. meta:set_string('map_id', map_id)
  34. lobby.sabotage[map_id] = true
  35. local time = meta:get_int('timer')
  36. if time > 0 then
  37. sabotage.show_hud_timed(map_id, time, desc)
  38. sabotage.timer[map_id] = time
  39. elseif time == 0 then
  40. sabotage.show_hud(map_id, desc)
  41. end
  42. local alert = meta:get_string('alert')
  43. local level = meta:get_int('level') or 0
  44. local terminate = meta:get_string('terminate')
  45. lobby.message_to_level(map_id, 'Level has been sabotaged, proceed with caution.')
  46. lobby.message_to_level(map_id, alert)
  47. minetest.chat_send_player(name, 'You successfully sabotaged this node')
  48. lobby.sabotage_level[map_id] = level
  49. local data = {}
  50. data.pos = pos
  51. data.traitor = name
  52. data.terminate = terminate
  53. sabotage.data[map_id] = data
  54. end
  55. end
  56. end
  57. elseif formname == 'sabotage:builder' then
  58. local name = player:get_player_name()
  59. local map_id = lobby.game[name]
  60. local pos = sabotage.player_pos[name]
  61. local meta = minetest.get_meta(pos)
  62. if fields.save then
  63. local timer = math.max((tonumber(fields.timer_input) or 0), 0)
  64. local chance = math.min((tonumber(fields.chance) or 2), 10)
  65. local damage = tonumber(fields.damage) or 1
  66. local alert = fields.alert or ''
  67. local level = math.min((tonumber(fields.level) or 1), 4)
  68. local xp = fields.xp or 0
  69. meta:set_int('timer', timer)
  70. meta:set_int('chance', chance)
  71. meta:set_int('damage', damage)
  72. meta:set_int('level', level)
  73. meta:set_int('xp', xp)
  74. meta:set_string('alert', alert)
  75. elseif fields.terminate then
  76. meta:set_string('terminate', fields.terminate)
  77. end
  78. elseif formname == 'sabotage:player' then
  79. local name = player:get_player_name()
  80. local map_id = lobby.game[name]
  81. if fields.repair then
  82. local pos = sabotage.player_pos[name]
  83. local meta = minetest.get_meta(pos)
  84. local chance = meta:get_int('chance') or 1
  85. local damage = meta:get_int('damage') or 1
  86. local terminate = meta:get_string('terminate')
  87. local player_attributes = player:get_meta()
  88. local luck = player_attributes:get_int('luck')
  89. if math.random(10) + luck >= chance then
  90. local hurt = math.random(damage)
  91. local health = player:get_hp()
  92. local new_health = health - hurt
  93. player:set_hp(new_health)
  94. end
  95. if math.random(10) + luck >= chance then
  96. sabotage.timer[map_id] = 0
  97. sabotage.finish(map_id, name, 'team')
  98. end
  99. end
  100. end
  101. end)
  102. function sabotage.finish(map_id, name, winner)
  103. print 'sabotage ended'
  104. if lobby.map[map_id] then
  105. local data = sabotage.data[map_id]
  106. local pos = data.pos
  107. local traitor = data.traitor
  108. local terminate = data.terminate
  109. local node = minetest.get_node(pos)
  110. local node_name = node.name
  111. local def = minetest.registered_nodes[node_name]
  112. local flip = def.sabotage_switch
  113. if flip then
  114. minetest.swap_node(pos, {name = flip, param2 = node.param2})
  115. end
  116. if lobby.map[map_id] > 1 and lobby.traitors[map_id] == traitor then
  117. lobby.sabotage_level[map_id] = 5
  118. lobby.sabotage[map_id] = false
  119. if terminate == 'true' then
  120. if winner == 'team' then
  121. lobby.team_win(map_id)
  122. else
  123. lobby.traitor_win(traitor, map_id)
  124. end
  125. elseif name then
  126. local meta = minetest.get_meta(pos)
  127. local desc = def.description
  128. local xp = meta:get_int('xp')
  129. tasks.only_add_xp(xp, name)
  130. lobby.message_to_level(map_id, desc.. ' was just fixed.')
  131. end
  132. end
  133. end
  134. end