smoke_detector.lua 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. local formspec_good =
  2. 'size[6,3]'..
  3. 'textarea[1,1;5,2;;;Battery voltage is in the acceptable range.]'
  4. local function formspec_bad(pos)
  5. local spos = pos.x ..','.. pos.y ..','.. pos.z
  6. local formspec =
  7. 'formspec_version[3]'..
  8. 'size[10.5,7]'..
  9. 'textarea[.5,.5;5,1;;;Battery is low, please replace.]' ..
  10. 'button_exit[3.5,1.5;3,1;gimme;Grab a part request form]'..
  11. 'list[nodemeta:'..spos..';part;8,1.5;1,1;]'..
  12. 'list[current_player;main;.5,3;8,2;]'..
  13. 'listring[current_player;main]'..
  14. 'listring[nodemeta:'..spos..';part]'..
  15. 'image[8,1.5;1,1;tasks_9v_battery_line.png]'
  16. return formspec
  17. end
  18. local box = {
  19. type = 'fixed',
  20. fixed = {
  21. {-.2, .4, -.2, .2, .5, .2}}}
  22. minetest.register_node('tasks:smoke_detector_on',{
  23. description = 'Smoke Detector',
  24. drawtype = 'mesh',
  25. mesh = 'tasks_smoke_alarm.obj',
  26. tiles = {name = 'tasks_smoke_alarm_on.png'},
  27. light_source = 2,
  28. paramtype = 'light',
  29. selection_box = box,
  30. collision_box = box,
  31. groups = {breakable = 1, tasks=1},
  32. on_construct = function(pos)
  33. tasks.on_construct(pos, 'Smoke Detector', 'Wall Art?')
  34. end,
  35. on_rightclick = function(pos, node, clicker)
  36. tasks.right_click_on(pos, node, clicker, formspec_good)
  37. end,
  38. on_timer = function(pos)
  39. local node = minetest.get_node(pos)
  40. local meta = minetest.get_meta(pos)
  41. local infotext = meta:get_string('info_repair')
  42. meta:set_string('infotext', infotext)
  43. minetest.swap_node(pos, {name = 'tasks:smoke_detector_off', param2 = node.param2})
  44. minetest.sound_play('tasks_smoke_alarm_chirp', {pos = pos, gain = 2, max_hear_distance = 14})
  45. end,
  46. })
  47. minetest.register_node('tasks:smoke_detector_off',{
  48. description = 'Smoke Detector',
  49. drawtype = 'mesh',
  50. mesh = 'tasks_smoke_alarm.obj',
  51. tiles = {name = 'tasks_smoke_alarm_off.png'},
  52. paramtype = 'light',
  53. selection_box = box,
  54. collision_box = box,
  55. groups = {breakable = 1, not_in_creative_inventory=1, tasks=1, plays_sound=1},
  56. _sound = 'tasks_smoke_alarm_chirp',
  57. drop = 'tasks:smoke_detector_on',
  58. req_form = 'tasks:9v_battery_req',
  59. req_count = 4,
  60. on_rightclick = function(pos, node, clicker)
  61. tasks.right_click_off(pos, node, clicker, formspec_bad(pos))
  62. end,
  63. allow_metadata_inventory_put = function(pos, listname, index, stack)
  64. if listname == 'part' then
  65. if stack:get_name() == 'tasks:9v_battery' then
  66. return 1
  67. else
  68. return 0
  69. end
  70. end
  71. end,
  72. on_metadata_inventory_put = function(pos, listname, index, stack, player)
  73. local name = player:get_player_name()
  74. local meta = minetest.get_meta(pos)
  75. local xp = meta:get_int('xp') or 1
  76. local inv = meta:get_inventory()
  77. local node = minetest.get_node(pos)
  78. local timer = minetest.get_node_timer(pos)
  79. local min = meta:get_int('time_min') or 30
  80. local max = meta:get_int('time_max') or 60
  81. local random_number = math.random(min,max)
  82. local infotext = meta:get_string('info_working')
  83. inv:set_stack('part', 1, '')
  84. minetest.swap_node(pos, {name = 'tasks:smoke_detector_on'})
  85. minetest.close_formspec(name, 'tasks:part_req_form')
  86. meta:set_string('infotext', infotext)
  87. tasks.only_add_xp(xp, name)
  88. timer:start(random_number)
  89. end,
  90. })