beehive.lua 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. local box = {
  2. type = 'fixed',
  3. fixed = {
  4. {-.5, -.5, -.5, .5, .375, .5}}}
  5. local formspec_good =
  6. 'formspec_version[3]'..
  7. 'size[12,6]'..
  8. 'image[0,0;12,6;tasks_beehive_ui_bg.png]'
  9. local function math_clamp(val, lower, upper)
  10. return math.max(lower, math.min(upper, val))
  11. end
  12. beehive = {}
  13. local function beehive_formspec(name, wx, wy, b1x, b1y, b2x, b2y)
  14. wx = math_clamp((wx + math.random(-4,4)/10), 0, 11)
  15. wy = math_clamp((wy + math.random(-4,4)/10), 0, 5)
  16. b1x = math_clamp((b1x + math.random(-4,4)/10), 0, 11)
  17. b1y = math_clamp((b1y + math.random(-4,4)/10), 0, 5)
  18. b2x = math_clamp((b2x + math.random(-4,4)/10), 0, 11)
  19. b2y = math_clamp((b2y + math.random(-4,4)/10), 0, 5)
  20. local formspec =
  21. 'formspec_version[3]'..
  22. 'size[12,6]'..
  23. 'image[0,0;12,6;tasks_beehive_ui_bg.png]'..
  24. 'image_button['..wx..','..wy..';1,1;tasks_beehive_ui_wasp.png;thief;;true;false]'..
  25. 'image_button['..b1x..','..b1y..';1,1;tasks_beehive_ui_bee.png;bee;;true;false]'..
  26. 'image_button['..b2x..','..b2y..';1,1;tasks_beehive_ui_bee.png;bee;;true;false]'
  27. minetest.show_formspec(name, 'tasks:beehive_bad', formspec)
  28. minetest.after(.25, function()
  29. if beehive[name] == 'true' then
  30. beehive_formspec(name, wx, wy, b1x, b1y, b2x, b2y)
  31. end
  32. end)
  33. end
  34. minetest.register_node('tasks:beehive',{
  35. description = 'Beehive',
  36. drawtype = 'mesh',
  37. mesh = 'tasks_beehive.obj',
  38. tiles = {'tasks_beehive.png'},
  39. paramtype = 'light',
  40. paramtype2 = 'facedir',
  41. selection_box = box,
  42. collision_box = box,
  43. groups = {breakable = 1, tasks=1},
  44. on_construct = function(pos)
  45. local meta = minetest.get_meta(pos)
  46. meta:set_string('infotext', 'Beehive')
  47. meta:set_string('info_working', 'Beehive')
  48. meta:set_string('info_repair', 'Distressed Beehive')
  49. meta:set_string('status', 'good')
  50. meta:set_int('time_min', 30)
  51. meta:set_int('time_max', 90)
  52. meta:set_int('xp', 5)
  53. meta:set_int('level', 0)
  54. end,
  55. on_rightclick = function(pos, node, clicker)
  56. local name = clicker:get_player_name()
  57. local timer = minetest.get_node_timer(pos)
  58. local meta = minetest.get_meta(pos)
  59. local min = meta:get_int('time_min') or 30
  60. local max = meta:get_int('time_max') or 60
  61. local status = meta:get_string('status')
  62. local random_number = math.random(min,max)
  63. timer:start(random_number)
  64. local map_id = lobby.game[name]
  65. local sabotage_level = lobby.sabotage_level[map_id] or 5
  66. local level = meta:get_int('level') or 0
  67. if level < sabotage_level then
  68. if status == 'good' then
  69. minetest.show_formspec(name, 'tasks:beehive_good', formspec_good)
  70. else
  71. tasks.player_config[name] = pos
  72. beehive[name] = 'true'
  73. beehive_formspec(name, 6, 3, 2, 4, 9, 1)
  74. end
  75. else
  76. minetest.chat_send_player(name, 'level is currently sabotaged, and you can\'t do this now.')
  77. end
  78. end,
  79. on_timer = function(pos)
  80. local node = minetest.get_node(pos)
  81. local meta = minetest.get_meta(pos)
  82. local infotext = meta:get_string('info_repair')
  83. meta:set_string('infotext', infotext)
  84. meta:set_string('status', 'bad')
  85. end,
  86. })
  87. minetest.register_on_player_receive_fields(function(player, formname, fields)
  88. local name = player:get_player_name()
  89. if formname == 'tasks:beehive_bad'then
  90. if fields.thief then
  91. beehive[name] = 'false'
  92. local pos = tasks.player_config[name]
  93. local node = minetest.get_node(pos)
  94. local meta = minetest.get_meta(pos)
  95. local infotext = meta:get_string('info_working')
  96. local xp = meta:get_int('xp')
  97. meta:set_string('status', 'good')
  98. meta:set_string('infotext', infotext)
  99. tasks.only_add_xp(xp, name)
  100. minetest.close_formspec(name, 'tasks:beehive_bad')
  101. minetest.chat_send_player(name, 'Nice going, the queen thanks you!')
  102. elseif fields.bee then
  103. beehive[name] = 'false'
  104. minetest.close_formspec(name, 'tasks:beehive_bad')
  105. minetest.chat_send_player(name, 'Careful, you just killed a honeybee.')
  106. elseif fields.quit then
  107. beehive[name] = 'false'
  108. end
  109. end
  110. end)