functions.lua 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. function tasks.check_xp(map_id, needed_xp)
  2. if needed_xp <= 0 then
  3. lobby.team_win(map_id)
  4. end
  5. end
  6. function tasks.add_xp(pos, node, puncher, swap_to) -- This function adds xp and swaps the task node.
  7. local player_attributes = puncher:get_meta()
  8. local mode = player_attributes:get_string('mode')
  9. local meta = minetest.get_meta(pos)
  10. local earned_xp = meta:get_int('xp') or 1
  11. local wield = puncher:get_wielded_item()
  12. local wield_name = wield:get_name()
  13. if wield_name ~= 'creative:tool_breaking' then
  14. local name = puncher:get_player_name()
  15. local timer = minetest.get_node_timer(pos)
  16. local min = math.max(meta:get_int('time_min'), 5)
  17. local max = math.max(meta:get_int('time_max'), 20)
  18. local random_number = math.random(min,max)
  19. timer:start(random_number)
  20. local map_id = lobby.game[name]
  21. local traitor = lobby.traitors[map_id]
  22. minetest.swap_node(pos, {name = swap_to, param2 = node.param2})
  23. meta:set_string('formspec', '')
  24. if map_id ~= 'lobby' then
  25. if name ~= traitor then
  26. if mode == 'ghost' then
  27. earned_xp = math.floor(earned_xp/2)
  28. map_id = string.sub(map_id, 0, -7)
  29. end
  30. local game_data = lobby.savedata.data[map_id]
  31. if lobby.xp[map_id] and mode ~= 'solo' then
  32. lobby.xp[map_id] = lobby.xp[map_id] + earned_xp
  33. local needed_xp = game_data['xp'] - lobby.xp[map_id]
  34. minetest.chat_send_player(name, 'You just earned '..earned_xp..' XP for your team.\nYou need '..needed_xp..' more XP to defeat the imposter.')
  35. minetest.log('action', map_id..': Gained '..earned_xp..' xp, still '..needed_xp..' xp required to complete level.')
  36. tasks.check_xp(map_id, needed_xp)
  37. else
  38. lobby.give_xp(puncher, 1)
  39. end
  40. else
  41. minetest.chat_send_player(name, 'You\'re the traitor, you can do tasks, but you won\'t earn XP. Try killing the other players.')
  42. end
  43. elseif map_id == 'lobby' and not minetest.check_player_privs(puncher:get_player_name(), {creative = true}) then
  44. lobby.give_xp(puncher, 1)
  45. end
  46. end
  47. end
  48. function tasks.only_add_xp(xp, name) -- This function only adds XP, you are responsible for modifying the task node yourself.
  49. local player = minetest.get_player_by_name(name)
  50. local player_attributes = player:get_meta()
  51. local mode = player_attributes:get_string('mode')
  52. local map_id = lobby.game[name] or 'lobby'
  53. local traitor = lobby.traitors[map_id]
  54. if map_id ~= 'lobby' then
  55. if name ~= traitor then
  56. if mode == 'ghost' then
  57. xp = math.floor(xp/2)
  58. map_id = string.sub(map_id, 0, -7)
  59. end
  60. local game_data = lobby.savedata.data[map_id]
  61. if lobby.xp[map_id] and mode ~= 'solo' then
  62. lobby.xp[map_id] = lobby.xp[map_id] + xp
  63. local needed_xp = game_data['xp'] - lobby.xp[map_id]
  64. minetest.chat_send_player(name, 'You just earned '..xp..' XP for your team.\nYou need '..needed_xp..' more XP to defeat the imposter.')
  65. minetest.log('action', map_id..': Gained '..xp..' xp, still '..needed_xp..' xp required to complete level.')
  66. tasks.check_xp(map_id, needed_xp)
  67. else
  68. lobby.give_xp(player, 1)
  69. end
  70. else
  71. minetest.chat_send_player(name, 'You\'re the traitor, you can do tasks, but you won\'t earn XP. Try killing the other players.')
  72. end
  73. elseif map_id == 'lobby' and not minetest.check_player_privs(name, {creative = true}) then
  74. lobby.give_xp(player, 1)
  75. end
  76. end
  77. function tasks.right_click_on(pos, node, clicker, formspec)
  78. local name = clicker:get_player_name()
  79. local timer = minetest.get_node_timer(pos)
  80. local meta = minetest.get_meta(pos)
  81. local min = meta:get_int('time_min') or 30
  82. local max = meta:get_int('time_max') or 60
  83. local random_number = math.random(min,max)
  84. timer:start(random_number)
  85. local map_id = lobby.game[name]
  86. local sabotage_level = lobby.sabotage_level[map_id] or 5
  87. local level = meta:get_int('level') or 0
  88. if level < sabotage_level then
  89. minetest.show_formspec(name, 'tasks:good', formspec)
  90. else
  91. minetest.chat_send_player(name, 'level is currently sabotaged, and you can\'t do this now.')
  92. end
  93. end
  94. function tasks.right_click_off(pos, node, clicker, formspec)
  95. local name = clicker:get_player_name()
  96. local map_id = lobby.game[name]
  97. local sabotage_level = lobby.sabotage_level[map_id] or 5
  98. local meta = minetest.get_meta(pos)
  99. local level = meta:get_int('level') or 0
  100. if level < sabotage_level then
  101. tasks.player_config[name] = pos
  102. minetest.show_formspec(name, 'tasks:part_req_form', formspec)
  103. else
  104. minetest.chat_send_player(name, 'level is currently sabotaged, and you can\'t do this now.')
  105. end
  106. end
  107. function tasks.on_construct(pos, info_working, info_repair)
  108. local meta = minetest.get_meta(pos)
  109. local inv = meta:get_inventory()
  110. inv:set_size('part', 1)
  111. meta:set_string('infotext', info_working)
  112. meta:set_string('info_working', info_working)
  113. meta:set_string('info_repair', info_repair)
  114. meta:set_int('time_min', 30)
  115. meta:set_int('time_max', 90)
  116. meta:set_int('xp', 5)
  117. meta:set_int('level', 0)
  118. end
  119. function tasks.is_integer(input)
  120. local number = tonumber(input)
  121. if number then
  122. if math.floor(number) == number then
  123. return true
  124. end
  125. end
  126. end
  127. function tasks.valid_input(string)
  128. local variables = string:split(', ')
  129. local xp = variables[1]
  130. local timer = variables[2]
  131. if tasks.is_integer(xp) and tasks.is_integer(timer) then
  132. return true
  133. end
  134. end
  135. minetest.register_on_player_receive_fields(function(player, formname, fields)
  136. local name = player:get_player_name()
  137. if formname == 'tasks:part_req_form'then
  138. if fields.gimme then
  139. local pos = tasks.player_config[name]
  140. local node = minetest.get_node(pos).name
  141. local def = minetest.registered_nodes[node]
  142. local item = def.req_form
  143. local max = def.req_count
  144. local player_inv = player:get_inventory()
  145. if not player_inv:contains_item('main', {name = item, count = max}) then
  146. player_inv:add_item('main', item)
  147. else
  148. minetest.chat_send_player(name, 'You have too many of these already.')
  149. end
  150. end
  151. end
  152. end)
  153. minetest.register_lbm({ --This can be removed in a few months. 08/27/22
  154. label = 'Tasks infotext update',
  155. name = 'tasks:info_update',
  156. run_at_every_load = false,
  157. nodenames = {'group:tasks'},
  158. action = function(pos, node)
  159. local meta = minetest.get_meta(pos)
  160. local info = meta:get_string('infotext')
  161. local info_working = meta:get_string('info_working')
  162. local info_repair = meta:get_string('info_repair')
  163. if info_repair == '' then
  164. local i, j = string.find(info, "%(")
  165. if i then
  166. info_repair = info
  167. info_working = string.sub(info, 1, (i-2))
  168. else
  169. info_working = info
  170. info_repair = info..' (Needs Repair)'
  171. end
  172. end
  173. meta:set_string('info_repair', info_repair)
  174. meta:set_string('info_working', info_working)
  175. end
  176. })