hearth.lua 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. --------------------------------------------------
  2. --HEARTH
  3. ------------------------------------------------
  4. --Functions
  5. ----------
  6. -- On construct
  7. -- set form
  8. local hearth_on_construct = function(pos)
  9. local inv = minetest.get_meta(pos):get_inventory()
  10. inv:set_size("fuel", 1)
  11. local meta = minetest.get_meta(pos)
  12. meta:set_string("formspec",
  13. "size[8,5.3]" ..
  14. default.gui_bg ..
  15. default.gui_bg_img ..
  16. default.gui_slots ..
  17. "list[current_name;fuel;3.5,0;1,1;]" ..
  18. "list[current_player;main;0,1.15;8,1;]" ..
  19. "list[current_player;main;0,2.38;8,3;8]" ..
  20. "listring[current_name;main]" ..
  21. "listring[current_player;main]" ..
  22. default.get_hotbar_bg(0,1.15)
  23. )
  24. end
  25. ----------
  26. --Digging only if no fuel
  27. local hearth_can_dig = function(pos, player)
  28. local inv = minetest.get_meta(pos):get_inventory()
  29. return inv:is_empty("fuel")
  30. end
  31. -------------
  32. -- Only fuel items allowed
  33. local hearth_allow_metadata_inventory_put = function(pos, listname, index, stack, player)
  34. if listname == "fuel" then
  35. if minetest.get_craft_result({method="fuel", width=1, items={stack}}).time ~= 0 then
  36. return stack:get_count()
  37. else
  38. return 0
  39. end
  40. end
  41. return 0
  42. end
  43. --------------
  44. --Particle Effects
  45. local function hearth_fire_on(pos, time)
  46. local meta = minetest.get_meta(pos)
  47. --flames
  48. minetest.add_particlespawner({
  49. amount = 4,
  50. time = time,
  51. minpos = {x = pos.x - 0.1, y = pos.y + 0.6, z = pos.z - 0.1},
  52. maxpos = {x = pos.x + 0.1, y = pos.y + 1, z = pos.z + 0.1},
  53. minvel = {x= 0, y= 0, z= 0},
  54. maxvel = {x= 0.001, y= 0.001, z= 0.001},
  55. minacc = {x= 0, y= 0, z= 0},
  56. maxacc = {x= 0.001, y= 0.001, z= 0.001},
  57. minexptime = 3,
  58. maxexptime = 5,
  59. minsize = 4,
  60. maxsize = 8,
  61. collisiondetection = false,
  62. vertical = true,
  63. texture = "fire_basic_flame_animated.png",
  64. animation = {
  65. type = "vertical_frames",
  66. aspect_w = 16,
  67. aspect_h = 16,
  68. length = 1
  69. },
  70. glow = 13,
  71. })
  72. --Smoke
  73. minetest.add_particlespawner({
  74. amount = 4,
  75. time = time,
  76. minpos = {x = pos.x - 0.1, y = pos.y + 0.8, z = pos.z - 0.1},
  77. maxpos = {x = pos.x + 0.1, y = pos.y + 1, z = pos.z + 0.1},
  78. minvel = {x= 0, y= 0, z= 0},
  79. maxvel = {x= 0.01, y= 0.07, z= 0.01},
  80. minacc = {x= 0, y= 0, z= 0},
  81. maxacc = {x= 0.01, y= 0.2, z= 0.01},
  82. minexptime = 5,
  83. maxexptime = 20,
  84. minsize = 1,
  85. maxsize = 8,
  86. collisiondetection = true,
  87. vertical = true,
  88. texture = "default_item_smoke.png",
  89. --animation = {type="vertical_frames", aspect_w=16, aspect_h=16, length = 0.7,},
  90. })
  91. end
  92. ----------
  93. --Burn
  94. --add flames and burn fuel
  95. local hearth_burn = function(pos, elapsed)
  96. local pos_above = {x=pos.x, y=pos.y+1, z=pos.z}
  97. local node_above = minetest.get_node(pos_above)
  98. local timer = minetest.get_node_timer(pos)
  99. local inv = minetest.get_inventory({type="node", pos=pos})
  100. local item = inv:get_stack("fuel", 1)
  101. local fuel_burned = minetest.get_craft_result({method="fuel", width=1, items={item:peek_item(1)}}).time
  102. --remove light...
  103. -- so it doesn't stay on permanently
  104. if inv:is_empty("fuel") and node_above.name == "earthbuild:hearth_air" then
  105. minetest.set_node(pos_above, {name = "air"})
  106. end
  107. --burn item and add smoke and flames
  108. if fuel_burned > 0 and (node_above.name == "air" or node_above.name == "earthbuild:hearth_air") then
  109. --randomly remove an item of fuel based on it's burn time.
  110. --this is so it can get through the fuel slowly, but..
  111. -- still do frequent flames
  112. --i.e. every X sec it will have a 1/X chance of burning
  113. local burn_time = fuel_burned * 30
  114. local burn_it = math.random(1, burn_time)
  115. --it's luck was up, it got burnt away.
  116. if burn_it == 1 then
  117. item:set_count(item:get_count() - 1)
  118. inv:set_stack("fuel", 1, item)
  119. end
  120. --if has no light
  121. if node_above.name == "air" then
  122. minetest.set_node(pos_above, {name = "earthbuild:hearth_air"})
  123. end
  124. --do fire effects and restart timer
  125. timer:start(5)
  126. hearth_fire_on(pos, 5)
  127. minetest.sound_play("fire_small",{pos=pos, max_hear_distance = 15, loop=false, gain=0.2})
  128. end
  129. end
  130. --Removed node..make sure to remove light
  131. local no_hearth = function(pos, oldnode)
  132. local pos_above = {x=pos.x, y=pos.y+1, z=pos.z}
  133. local node_above = minetest.get_node(pos_above)
  134. if node_above.name == "earthbuild:hearth_air" then
  135. minetest.set_node(pos_above, {name = "air"})
  136. end
  137. end
  138. -------------------------------------------------
  139. --Node
  140. -- adds hearth
  141. minetest.register_node('earthbuild:hearth', {
  142. description = 'Hearth',
  143. drawtype = "normal",
  144. tiles = {
  145. "earthbuild_hearth_top.png",
  146. "earthbuild_cob.png",
  147. "earthbuild_hearth_side.png",
  148. "earthbuild_hearth_side.png",
  149. "earthbuild_hearth_side.png",
  150. "earthbuild_hearth_side.png"
  151. },
  152. paramtype = "light",
  153. drop = "earthbuild:hearth",
  154. groups = {crumbly = 1, cracky = 1, oddly_breakable_by_hand = 2},
  155. damage_per_second = 1,
  156. sounds = default.node_sound_dirt_defaults(),
  157. on_construct = hearth_on_construct,
  158. can_dig = hearth_can_dig,
  159. allow_metadata_inventory_put = hearth_allow_metadata_inventory_put,
  160. on_metadata_inventory_put = hearth_burn,
  161. on_timer = hearth_burn,
  162. after_destruct = no_hearth,
  163. })
  164. -----------------------------------------
  165. --Hearth Air
  166. --this is to add a light source
  167. -- an invisible glowing block added above the fire when burning
  168. --this is needed because particles give no light
  169. minetest.register_node("earthbuild:hearth_air", {
  170. description = "Hearth Air",
  171. tiles = {"earthbuild_hearth_top.png"},
  172. drawtype = "airlike",
  173. paramtype = "light",
  174. sunlight_propagates = true,
  175. walkable = false,
  176. pointable = false,
  177. diggable = false,
  178. buildable_to = true,
  179. protected = true,
  180. groups = {not_in_creative_inventory = 1},
  181. light_source= 9,
  182. on_blast = function(pos)
  183. end,
  184. })
  185. ----------------------------------------------
  186. --Crafts
  187. -- adds hearth recipes
  188. minetest.register_craft({
  189. output = 'earthbuild:hearth',
  190. recipe = {
  191. {'', 'group:tree', ''},
  192. {'group:stick', 'group:stick', 'group:stick'},
  193. {'', 'earthbuild:rammed_earth', ''},
  194. }
  195. })
  196. minetest.register_craft({
  197. output = 'earthbuild:hearth',
  198. recipe = {
  199. {'', 'group:tree', ''},
  200. {'group:stick', 'group:stick', 'group:stick'},
  201. {'', 'earthbuild:cob', ''},
  202. }
  203. })
  204. minetest.register_craft({
  205. output = 'earthbuild:hearth',
  206. recipe = {
  207. {'', 'group:tree', ''},
  208. {'group:stick', 'group:stick', 'group:stick'},
  209. {'default:dirt', 'default:dirt', 'default:dirt'},
  210. }
  211. })