campfire.lua 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. minetest.register_node('tasks:campfire_0', {
  2. description = 'Campfire Smoldering',
  3. drawtype = 'mesh',
  4. mesh = 'tasks_campfire.obj',
  5. tiles = {'tasks_campfire_material.png', 'tasks_campfire_base.png', 'blank.png'},
  6. use_texture_alpha = 'clip',
  7. paramtype2 = 'facedir',
  8. light_source = 3,
  9. groups = {breakable=1, tasks=1, not_in_creative_inventory=1},
  10. drop = 'tasks:campfire_1',
  11. selection_box = {
  12. type = 'fixed',
  13. fixed = {{-.4, -.5, -.4, .4, 0, .4}}
  14. },
  15. collision_box = {
  16. type = 'fixed',
  17. fixed = {{-.4, -.5, -.4, .4, 0, .4}}
  18. },
  19. on_rightclick = function(pos, node, clicker, itemstack)
  20. local name = clicker:get_player_name()
  21. local item = itemstack:get_name()
  22. if item == 'tasks:log' then
  23. minetest.swap_node(pos, {name = 'tasks:campfire_1', param2 = node.param2})
  24. local timer = minetest.get_node_timer(pos)
  25. local meta = minetest.get_meta(pos)
  26. local xp = meta:get_int('xp') or 1
  27. local min = meta:get_int('time_min') or 240
  28. local max = meta:get_int('time_max') or 600
  29. local random_number = math.random(min,max)
  30. timer:start(random_number)
  31. tasks.only_add_xp(xp, name)
  32. return ''
  33. else
  34. minetest.chat_send_player(name, 'Try with another item.')
  35. end
  36. end,
  37. on_punch = function(pos, node, puncher)
  38. local name = puncher:get_player_name()
  39. local wield = puncher:get_wielded_item()
  40. local wield_name = wield:get_name()
  41. if wield_name == 'tasks:log' then
  42. minetest.swap_node(pos, {name = 'tasks:campfire_1', param2 = node.param2})
  43. local timer = minetest.get_node_timer(pos)
  44. local meta = minetest.get_meta(pos)
  45. local xp = meta:get_int('xp') or 1
  46. local min = meta:get_int('time_min') or 240
  47. local max = meta:get_int('time_max') or 600
  48. local random_number = math.random(min,max)
  49. timer:start(random_number)
  50. tasks.only_add_xp(xp, name)
  51. puncher:set_wielded_item('')
  52. else
  53. minetest.chat_send_player(name, 'Try with another item.')
  54. end
  55. end,
  56. })
  57. minetest.register_node('tasks:campfire_1', {
  58. description = 'Campfire',
  59. drawtype = 'mesh',
  60. mesh = 'tasks_campfire.obj',
  61. tiles = {'tasks_campfire_material.png', 'tasks_campfire_base.png', {name='tasks_campfire_flames.png', animation = {type = 'vertical_frames', aspect_w = 32, aspect_h = 32, length = 1}}},
  62. use_texture_alpha = 'clip',
  63. paramtype2 = 'facedir',
  64. light_source = 14,
  65. groups = {breakable=1, tasks=1},
  66. selection_box = {
  67. type = 'fixed',
  68. fixed = {{-.4, -.5, -.4, .4, .2, .4}}
  69. },
  70. collision_box = {
  71. type = 'fixed',
  72. fixed = {{-.4, -.5, -.4, .4, .2, .4}}
  73. },
  74. on_construct = function(pos)
  75. local meta = minetest.get_meta(pos)
  76. meta:set_int('time_min', 240)
  77. meta:set_int('time_max', 600)
  78. meta:set_int('xp', 1)
  79. meta:set_int('level', 0)
  80. end,
  81. on_rightclick = tasks.right_click,
  82. on_punch = function(pose, node, puncher)
  83. if math.random(30) == 26 then
  84. local player_inv = puncher:get_inventory()
  85. if not player_inv:contains_item('main', {name='lights:torch_1', count = 1}) then
  86. player_inv:add_item('main', 'lights:torch_1')
  87. end
  88. end
  89. if math.random(20) == 4 then
  90. local hurt = math.random(5)
  91. local health = puncher:get_hp()
  92. local new_health = health - hurt
  93. puncher:set_hp(new_health)
  94. end
  95. end,
  96. on_timer = function(pos)
  97. local node = minetest.get_node(pos)
  98. minetest.swap_node(pos, {name = 'tasks:campfire_0', param2 = node.param2})
  99. end,
  100. })
  101. -- Return true if `index` is the first free slot in the player's main inventory
  102. local function is_first_free_slot(player, index)
  103. local player_inv = player:get_inventory()
  104. local first_free = index+1
  105. for i = 1,index,1 do
  106. local curstk = player_inv:get_stack('main', i)
  107. --minetest.debug('main(' .. tostring(i) .. ') = ' .. curstk:get_name())
  108. if curstk:is_empty() and i < first_free then
  109. first_free = i
  110. break
  111. end
  112. end
  113. return (first_free == index)
  114. end
  115. minetest.register_node('tasks:wood_pile', {
  116. description = 'Wood Pile',
  117. drawtype = 'mesh',
  118. mesh = 'tasks_wood_pile.obj',
  119. tiles = {'tasks_wood_pile.png'},
  120. paramtype2 = 'facedir',
  121. groups = {breakable=1},
  122. selection_box = {
  123. type = 'fixed',
  124. fixed = {{-.5, -.5, -.5, 1.5, .5, .5}}
  125. },
  126. collision_box = {
  127. type = 'fixed',
  128. fixed = {{-.5, -.5, -.5, 1.5, .5, .5}}
  129. },
  130. on_punch = function(pose, node, puncher)
  131. local player_inv = puncher:get_inventory()
  132. if not player_inv:contains_item('main', {name='tasks:log', count = 4}) then
  133. player_inv:add_item('main', 'tasks:log')
  134. else
  135. minetest.chat_send_player(puncher:get_player_name(), 'You have too many of those already.')
  136. end
  137. end,
  138. on_rightclick = function(pose, node, clicker, wielded)
  139. local player_inv = clicker:get_inventory()
  140. if not player_inv:contains_item('main', {name='tasks:log', count = 4}) then
  141. local wield_log = is_first_free_slot(clicker, clicker:get_wield_index())
  142. player_inv:add_item('main', 'tasks:log')
  143. if wielded:is_empty() and wield_log then
  144. -- In addition to adding the log to the inventory, we have to give
  145. -- the player a log in their hand, if the selected hotbar item was
  146. -- the first empty slot. Otherwise, the bare hand seemingly overrides
  147. -- the log we just added to the hotbar.
  148. return ItemStack('tasks:log')
  149. end
  150. else
  151. minetest.chat_send_player(clicker:get_player_name(), 'You have too many of those already.')
  152. end
  153. end,
  154. })