microscope.lua 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. local formspec_idle =
  2. 'size[6,3]'..
  3. 'textarea[1,1;5,2;;;No new specimens identify at this time.]'
  4. local specimen_table = {
  5. 'Onion', 'Algae', 'Pumpkin', 'Fungus', 'Snow', 'Yeast', 'Tree Rings' , 'Orange', 'Oak Flower'
  6. }
  7. local function microscope_identify(pos, ran, count)
  8. local formspec =
  9. 'formspec_version[3]'..
  10. 'size[11,7]'..
  11. 'bgcolor[#171717]'..
  12. 'textarea[7,.5;3.5,3;;;There are currently '..count..' specimens waiting to be identified!]'..
  13. 'image[.5,.5;6,6;tasks_microscope_specimen_'..ran..'.png]'..--specimen images should be cropped to a circle, 192px wide & tall
  14. 'dropdown[7,4;3.5,.5;specimen;'..table.concat(specimen_table, ',')..';0]'..
  15. 'button_exit[8,5.5;2,1;save;Save]'
  16. return formspec
  17. end
  18. local box = {
  19. type = 'fixed',
  20. fixed = {
  21. {-.25, -.5, -.25, .25, .4375, .25}}}
  22. minetest.register_node('tasks:microscope',{
  23. description = 'Microscope',
  24. drawtype = 'mesh',
  25. mesh = 'tasks_microscope.obj',
  26. tiles = {name = 'tasks_microscope.png'},
  27. paramtype = 'light',
  28. paramtype2 = 'facedir',
  29. selection_box = box,
  30. collision_box = box,
  31. groups = {breakable = 1, tasks=1},
  32. on_construct = function(pos)
  33. tasks.on_construct(pos, 'Microscope', 'Microscope with Specimens')
  34. end,
  35. on_rightclick = function(pos, node, clicker)
  36. local name = clicker:get_player_name()
  37. local map_id = lobby.game[name]
  38. local sabotage_level = lobby.sabotage_level[map_id] or 5
  39. local meta = minetest.get_meta(pos)
  40. local count = meta:get_int('count') or 0
  41. local level = meta:get_int('level') or 0
  42. if level < sabotage_level then
  43. tasks.player_config[name] = pos
  44. if count == 0 then
  45. tasks.right_click_on(pos, node, clicker, formspec_idle)
  46. else
  47. local name = clicker:get_player_name()
  48. local ran = math.random(2,9)
  49. meta:set_int('answer', ran)
  50. minetest.show_formspec(name, 'tasks:microscope', microscope_identify(pos, ran, count))
  51. end
  52. else
  53. minetest.chat_send_player(name, 'level is currently sabotaged, and you can\'t do this now.')
  54. end
  55. end,
  56. on_timer = function(pos)
  57. local node = minetest.get_node(pos)
  58. local meta = minetest.get_meta(pos)
  59. local info_repair = meta:get_string('info_repair')
  60. meta:set_string('infotext', info_repair)
  61. local min = meta:get_int('time_min') or 240
  62. local max = meta:get_int('time_max') or 600
  63. local count = meta:get_int('count') or 0
  64. local random_number = math.random(min,max)
  65. local timer = minetest.get_node_timer(pos)
  66. if count < 20 then
  67. meta:set_int('count', count+1)
  68. end
  69. timer:start(random_number)
  70. end,
  71. })
  72. minetest.register_on_player_receive_fields(function(player, formname, fields)
  73. local name = player:get_player_name()
  74. if formname == 'tasks:microscope' then
  75. if fields.save then
  76. local pos = tasks.player_config[name]
  77. local meta = minetest.get_meta(pos)
  78. local count = meta:get_int('count')
  79. local answer = meta:get_int('answer')
  80. for i, index in ipairs(specimen_table) do
  81. if index == fields.specimen then
  82. if i == answer then
  83. local xp = meta:get_int('xp')
  84. tasks.only_add_xp(xp, name)
  85. minetest.chat_send_player(name, 'You got it.')
  86. else
  87. minetest.chat_send_player(name, 'That ain\'t it chief.') --This shouldn't be sent if the player closes the formspec
  88. end
  89. end
  90. end
  91. local ran = math.random(2,9)
  92. meta:set_int('count', count - 1)
  93. meta:set_int('answer', ran)
  94. if count > 1 then
  95. minetest.show_formspec(name, 'tasks:microscope', microscope_identify(pos, ran, count-1))
  96. else
  97. minetest.close_formspec(name, 'tasks:microscope')
  98. local info_working = meta:get_string('info_working')
  99. meta:set_string('infotext', info_working)
  100. end
  101. end
  102. end
  103. end)