smoker.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. --
  2. -- Formspecs
  3. --
  4. function default.get_smoker_active_formspec(fuel_percent, item_percent)
  5. return "size[8,8.5]"..
  6. default.gui_bg..
  7. default.gui_bg_img..
  8. default.gui_slots..
  9. "list[context;src;2.75,0.5;1,1;]"..
  10. "list[context;fuel;2.75,2.5;1,1;]"..
  11. "image[2.75,1.5;1,1;bbq_smoker_fire_bg.png^[lowpart:"..
  12. (100-fuel_percent)..":bbq_smoker_fire_fg.png]"..
  13. "image[3.75,1.5;1,1;gui_smoker_arrow_bg.png^[lowpart:"..
  14. (item_percent)..":gui_smoker_arrow_fg.png^[transformR270]"..
  15. "list[context;dst;4.8,0.5;3,3;]"..
  16. "list[current_player;main;0,4.25;8,1;]"..
  17. "list[current_player;main;0,5.5;8,3;8]"..
  18. "listring[context;dst]"..
  19. "listring[current_player;main]"..
  20. "listring[context;src]"..
  21. "listring[current_player;main]"..
  22. "listring[context;fuel]"..
  23. "listring[current_player;main]"..
  24. default.get_hotbar_bg(0, 4.25)
  25. end
  26. function default.get_smoker_inactive_formspec()
  27. return "size[8,8.5]"..
  28. default.gui_bg..
  29. default.gui_bg_img..
  30. default.gui_slots..
  31. "list[context;src;2.75,0.5;1,1;]"..
  32. "list[context;fuel;2.75,2.5;1,1;]"..
  33. "image[2.75,1.5;1,1;bbq_smoker_fire_bg.png]"..
  34. "image[3.75,1.5;1,1;gui_smoker_arrow_bg.png^[transformR270]"..
  35. "list[context;dst;4.8,0.5;3,3;]"..
  36. "list[current_player;main;0,4.25;8,1;]"..
  37. "list[current_player;main;0,5.5;8,3;8]"..
  38. "listring[context;dst]"..
  39. "listring[current_player;main]"..
  40. "listring[context;src]"..
  41. "listring[current_player;main]"..
  42. "listring[context;fuel]"..
  43. "listring[current_player;main]"..
  44. default.get_hotbar_bg(0, 4.25)
  45. end
  46. --
  47. -- Node callback functions that are the same for active and inactive smoker
  48. --
  49. local function can_dig(pos, player)
  50. local meta = minetest.get_meta(pos);
  51. local inv = meta:get_inventory()
  52. return inv:is_empty("fuel") and inv:is_empty("dst") and inv:is_empty("src")
  53. end
  54. local function allow_metadata_inventory_put(pos, listname, index, stack, player)
  55. if minetest.is_protected(pos, player:get_player_name()) then
  56. return 0
  57. end
  58. local meta = minetest.get_meta(pos)
  59. local inv = meta:get_inventory()
  60. if listname == "fuel" then
  61. if minetest.get_craft_result({method="fuel", width=1, items={stack}}).time ~= 0 then
  62. if inv:is_empty("src") then
  63. meta:set_string("infotext", "smoker is empty")
  64. end
  65. return stack:get_count()
  66. else
  67. return 0
  68. end
  69. elseif listname == "src" then
  70. return stack:get_count()
  71. elseif listname == "dst" then
  72. return 0
  73. end
  74. end
  75. local function allow_metadata_inventory_move(pos, from_list, from_index, to_list, to_index, count, player)
  76. local meta = minetest.get_meta(pos)
  77. local inv = meta:get_inventory()
  78. local stack = inv:get_stack(from_list, from_index)
  79. return allow_metadata_inventory_put(pos, to_list, to_index, stack, player)
  80. end
  81. local function allow_metadata_inventory_take(pos, listname, index, stack, player)
  82. if minetest.is_protected(pos, player:get_player_name()) then
  83. return 0
  84. end
  85. return stack:get_count()
  86. end
  87. local function swap_node(pos, name)
  88. local node = minetest.get_node(pos)
  89. if node.name == name then
  90. return
  91. end
  92. node.name = name
  93. minetest.swap_node(pos, node)
  94. end
  95. local function smoker_node_timer(pos, elapsed)
  96. --
  97. -- Inizialize metadata
  98. --
  99. local meta = minetest.get_meta(pos)
  100. local fuel_time = meta:get_float("fuel_time") or 0
  101. local src_time = meta:get_float("src_time") or 0
  102. local fuel_totaltime = meta:get_float("fuel_totaltime") or 0
  103. local inv = meta:get_inventory()
  104. local srclist, fuellist
  105. local cookable, cooked
  106. local fuel
  107. local update = true
  108. while elapsed > 0 and update do
  109. update = false
  110. srclist = inv:get_list("src")
  111. fuellist = inv:get_list("fuel")
  112. --
  113. -- Cooking
  114. --
  115. -- Check if we have cookable content
  116. local aftercooked
  117. cooked, aftercooked = minetest.get_craft_result({method = "cooking", width = 1, items = srclist})
  118. cookable = cooked.time ~= 0
  119. local el = math.min(elapsed, fuel_totaltime - fuel_time)
  120. if cookable then -- fuel lasts long enough, adjust el to cooking duration
  121. el = math.min(el, cooked.time - src_time)
  122. end
  123. -- Check if we have enough fuel to burn
  124. if fuel_time < fuel_totaltime then
  125. -- The smoker is currently active and has enough fuel
  126. fuel_time = fuel_time + el
  127. -- If there is a cookable item then check if it is ready yet
  128. if cookable then
  129. src_time = src_time + el
  130. if src_time >= cooked.time then
  131. -- Place result in dst list if possible
  132. if inv:room_for_item("dst", cooked.item) then
  133. inv:add_item("dst", cooked.item)
  134. inv:set_stack("src", 1, aftercooked.items[1])
  135. src_time = src_time - cooked.time
  136. update = true
  137. end
  138. else
  139. -- Item could not be cooked: probably missing fuel
  140. update = true
  141. end
  142. end
  143. else
  144. -- smoker ran out of fuel
  145. if cookable then
  146. -- We need to get new fuel
  147. local afterfuel
  148. fuel, afterfuel = minetest.get_craft_result({method = "fuel", width = 1, items = fuellist})
  149. if fuel.time == 0 then
  150. -- No valid fuel in fuel list
  151. fuel_totaltime = 0
  152. src_time = 0
  153. else
  154. -- Take fuel from fuel list
  155. inv:set_stack("fuel", 1, afterfuel.items[1])
  156. update = true
  157. fuel_totaltime = fuel.time + (fuel_totaltime - fuel_time)
  158. end
  159. else
  160. -- We don't need to get new fuel since there is no cookable item
  161. fuel_totaltime = 0
  162. src_time = 0
  163. end
  164. fuel_time = 0
  165. end
  166. elapsed = elapsed - el
  167. end
  168. if fuel and fuel_totaltime > fuel.time then
  169. fuel_totaltime = fuel.time
  170. end
  171. if srclist[1]:is_empty() then
  172. src_time = 0
  173. end
  174. --
  175. -- Update formspec, infotext and node
  176. --
  177. local formspec
  178. local item_state
  179. local item_percent = 0
  180. if cookable then
  181. item_percent = math.floor(src_time / cooked.time * 100)
  182. if item_percent > 100 then
  183. item_state = "100% (output full)"
  184. else
  185. item_state = item_percent .. "%"
  186. end
  187. else
  188. if srclist[1]:is_empty() then
  189. item_state = "Empty"
  190. else
  191. item_state = "Not cookable"
  192. end
  193. end
  194. local fuel_state = "Empty"
  195. local active = "inactive"
  196. local result = false
  197. if fuel_totaltime ~= 0 then
  198. active = "active"
  199. local fuel_percent = math.floor(fuel_time / fuel_totaltime * 100)
  200. fuel_state = fuel_percent .. "%"
  201. formspec = default.get_smoker_active_formspec(fuel_percent, item_percent)
  202. swap_node(pos, "bbq:smoker_active")
  203. -- make sure timer restarts automatically
  204. result = true
  205. else
  206. if not fuellist[1]:is_empty() then
  207. fuel_state = "0%"
  208. end
  209. formspec = default.get_smoker_inactive_formspec()
  210. swap_node(pos, "bbq:smoker")
  211. -- stop timer on the inactive smoker
  212. minetest.get_node_timer(pos):stop()
  213. end
  214. local infotext = "smoker " .. active .. "\n(Item: " .. item_state ..
  215. "; Fuel: " .. fuel_state .. ")"
  216. --
  217. -- Set meta values
  218. --
  219. meta:set_float("fuel_totaltime", fuel_totaltime)
  220. meta:set_float("fuel_time", fuel_time)
  221. meta:set_float("src_time", src_time)
  222. meta:set_string("formspec", formspec)
  223. meta:set_string("infotext", infotext)
  224. return result
  225. end
  226. -------------------
  227. -- Node definitions
  228. -------------------
  229. minetest.register_node("bbq:smoker", {
  230. description = "Smoker",
  231. tiles = {
  232. "bbq_smoker_texture_bottom.png", --top
  233. "bbq_smoker_texture_bottom.png^[transformFY", --bottom
  234. "bbq_smoker_texture_side.png", --right side
  235. "bbq_smoker_texture_side.png^[transformFX", --left side
  236. "bbq_smoker_texture_back.png", --back
  237. "bbq_smoker_texture.png", --front
  238. },
  239. paramtype = "light",
  240. paramtype2 = "facedir",
  241. drawtype = "nodebox",
  242. node_box = {
  243. type = "fixed",
  244. fixed = {
  245. {-1.5, -0.03, -0.3, -1.0, 0.35, 0.093},-- smokebox
  246. {-1.0, -0.08, -0.5, 0.5, 0.6, 0.435 }, -- main body
  247. {-0.375, 0.095, -.52, -0.16, 0.155, -0.54}, -- main handle
  248. {-1.37, 0.095, -.32, -1.16, 0.155, -0.34}, -- smokebox handle
  249. {-0.345, 0.115, -.5, -0.325, 0.135, -0.55}, -- left handle bolt
  250. {-0.21, 0.115, -.5, -0.19, 0.135, -0.55}, -- right handle bolt
  251. {-1.32, 0.115, -.35, -1.34, 0.135, -0.3}, -- left smokebox handle bolt
  252. {-1.19, 0.115, -.35, -1.21, 0.135, -0.3}, -- right smokebox handle bolt
  253. {0.5, 0.18, .10, 0.99, 0.30, 0.22}, -- chimney x
  254. {0.87, 0.18, .10, 0.99, .62, 0.22}, -- chimney y
  255. -- {0.93, 0.62, .10, 0.93, .9, 0.22}, -- chimney smoke
  256. -- {0.87, 0.62, .16, 0.99, .9, 0.16}, -- chimney smoke
  257. {-0.8, -0.5, -0.3, -0.9, -0.08, -0.4}, -- front leftleg
  258. {0.3, -0.5, -0.3, 0.4, -0.08, -0.4}, -- front right leg
  259. {-0.8, -0.5, 0.3, -0.9, -0.08, 0.4}, -- front leftleg
  260. {0.3, -0.5, 0.3, 0.4, -0.08, 0.4}, -- front right leg
  261. },
  262. },
  263. sunlight_propagates = true,
  264. groups = {cracky=2},
  265. legacy_facedir_simple = true,
  266. is_ground_content = false,
  267. sounds = default.node_sound_stone_defaults(),
  268. can_dig = can_dig,
  269. on_timer = smoker_node_timer,
  270. on_construct = function(pos)
  271. local meta = minetest.get_meta(pos)
  272. meta:set_string("formspec", default.get_smoker_inactive_formspec())
  273. local inv = meta:get_inventory()
  274. inv:set_size('src', 1)
  275. inv:set_size('fuel', 1 )
  276. inv:set_size('dst', 9)
  277. end,
  278. on_metadata_inventory_move = function(pos)
  279. minetest.get_node_timer(pos):start(1.0)
  280. end,
  281. on_metadata_inventory_put = function(pos)
  282. -- start timer function, it will sort out whether smoker can burn or not.
  283. minetest.get_node_timer(pos):start(1.0)
  284. end,
  285. on_blast = function(pos)
  286. local drops = {}
  287. default.get_inventory_drops(pos, "src", drops)
  288. default.get_inventory_drops(pos, "fuel", drops)
  289. default.get_inventory_drops(pos, "dst", drops)
  290. drops[#drops+1] = "bbq:smoker"
  291. minetest.remove_node(pos)
  292. return drops
  293. end,
  294. allow_metadata_inventory_put = allow_metadata_inventory_put,
  295. allow_metadata_inventory_move = allow_metadata_inventory_move,
  296. allow_metadata_inventory_take = allow_metadata_inventory_take,
  297. })
  298. minetest.register_node("bbq:smoker_active", {
  299. description = "smoker",
  300. tiles = {
  301. "bbq_smoker_texture_bottom.png", --top
  302. "bbq_smoker_texture_bottom.png^[transformFY", --bottom
  303. {
  304. image = "bbq_smoker_texture_side_animated.png", --right side
  305. backface_culling = false,
  306. animation = {
  307. type = "vertical_frames",
  308. aspect_w = 16,
  309. aspect_h = 16,
  310. length = 1.5
  311. },
  312. },
  313. {
  314. image = "bbq_smoker_texture_side_animated.png^[transformFX", --left side
  315. backface_culling = false,
  316. animation = {
  317. type = "vertical_frames",
  318. aspect_w = 16,
  319. aspect_h = 16,
  320. length = 1.5
  321. },
  322. },
  323. {
  324. image = "bbq_smoker_texture_back_animated.png", --back
  325. backface_culling = false,
  326. animation = {
  327. type = "vertical_frames",
  328. aspect_w = 16,
  329. aspect_h = 16,
  330. length = 1.5
  331. },
  332. },
  333. {
  334. image = "bbq_smoker_texture_animated.png", --front
  335. backface_culling = false,
  336. animation = {
  337. type = "vertical_frames",
  338. aspect_w = 16,
  339. aspect_h = 16,
  340. length = 1.5
  341. },
  342. },
  343. },
  344. paramtype = "light",
  345. paramtype2 = "facedir",
  346. drawtype = "nodebox",
  347. node_box = {
  348. type = "fixed",
  349. fixed = {
  350. {-1.5, -0.03, -0.3, -1.0, 0.35, 0.093},-- smokebox
  351. {-1.0, -0.08, -0.5, 0.5, 0.6, 0.435 }, -- main body
  352. {-0.375, 0.095, -.52, -0.16, 0.155, -0.54}, -- main handle
  353. {-1.37, 0.095, -.32, -1.16, 0.155, -0.34}, -- smokebox handle
  354. {-0.345, 0.115, -.5, -0.325, 0.135, -0.55}, -- left handle bolt
  355. {-0.21, 0.115, -.5, -0.19, 0.135, -0.55}, -- right handle bolt
  356. {-1.32, 0.115, -.35, -1.34, 0.135, -0.3}, -- left smokebox handle bolt
  357. {-1.19, 0.115, -.35, -1.21, 0.135, -0.3}, -- right smokebox handle bolt
  358. {0.5, 0.18, .10, 0.99, 0.30, 0.22}, -- chimney x
  359. {0.87, 0.18, .10, 0.99, .62, 0.22}, -- chimney y
  360. {0.93, 0.62, .10, 0.93, .9, 0.22}, -- chimney smoke
  361. {0.87, 0.62, .16, 0.99, .9, 0.16}, -- chimney smoke
  362. {-0.8, -0.5, -0.3, -0.9, -0.08, -0.4}, -- front leftleg
  363. {0.3, -0.5, -0.3, 0.4, -0.08, -0.4}, -- front right leg
  364. {-0.8, -0.5, 0.3, -0.9, -0.08, 0.4}, -- front leftleg
  365. {0.3, -0.5, 0.3, 0.4, -0.08, 0.4}, -- front right leg
  366. },
  367. },
  368. sunlight_propagates = true,
  369. drop = "bbq:smoker",
  370. groups = {cracky=2, not_in_creative_inventory=1},
  371. legacy_facedir_simple = true,
  372. is_ground_content = false,
  373. sounds = default.node_sound_stone_defaults(),
  374. on_timer = smoker_node_timer,
  375. can_dig = can_dig,
  376. allow_metadata_inventory_put = allow_metadata_inventory_put,
  377. allow_metadata_inventory_move = allow_metadata_inventory_move,
  378. allow_metadata_inventory_take = allow_metadata_inventory_take,
  379. })