oven.lua 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. local oven_fs = "size[8,7]"
  2. .."image[3.5,1.5;1,1;default_furnace_fire_bg.png]"
  3. .."list[current_player;main;0,3;8,4;]"
  4. .."list[context;input;2,1.5;1,1;]"
  5. .."list[context;output;5,1.5;1,1;]"
  6. .."label[3,0.5;Oven]"
  7. .."label[1.5,1;Uncooked Food]"
  8. .."label[4.5,1;Cooked Food]"
  9. -- possibly add "fire" image?
  10. local function get_active_oven_fs(item_percent)
  11. return "size[8,7]"
  12. .."image[3.5,1.5;1,1;default_furnace_fire_bg.png^[lowpart:"
  13. ..(item_percent)..":default_furnace_fire_fg.png]"
  14. .."list[current_player;main;0,3;8,4;]"
  15. .."list[context;input;2,1.5;1,1;]"
  16. .."list[context;output;5,1.5;1,1;]"
  17. .."label[3,0.5;Oven]"
  18. .."label[1.5,1;Uncooked Food]"
  19. .."label[4.5,1;Cooked Food]"
  20. -- possibly add "fire" image?
  21. end
  22. --x,y;w,h
  23. -- Adding recipe API so we don't end up hardcoding items
  24. ma_pops_furniture.oven = {}
  25. local oven = ma_pops_furniture.oven
  26. oven.recipes = {}
  27. function oven.register_recipe(input, output) oven.recipes[input] = output end
  28. local function update_formspec(progress, goal, meta)
  29. local formspec
  30. if progress > 0 and progress <= goal then
  31. local item_percent = math.floor(progress / goal * 100)
  32. formspec = get_active_oven_fs(item_percent)
  33. else
  34. formspec = oven_fs
  35. end
  36. meta:set_string("formspec", formspec)
  37. end
  38. local function recalculate(pos)
  39. local meta, timer = minetest.get_meta(pos), minetest.get_node_timer(pos)
  40. local inv = meta:get_inventory()
  41. local stack = inv:get_stack("input", 1)
  42. local k = oven.recipes[stack:get_name()]
  43. if not k then return end
  44. timer:stop()
  45. update_formspec(0, 3, meta)
  46. timer:start(1)
  47. end
  48. local function do_cook_single(pos)
  49. local meta = minetest.get_meta(pos)
  50. local inv = meta:get_inventory()
  51. local food_uncooked = inv:get_stack("input", 1)
  52. food_uncooked:set_count(1)
  53. --If the uncooked food wasn't removed mid-cooking, then cook it.
  54. if not oven.recipes[food_uncooked:get_name()] then
  55. minetest.chat_send_all("Oven cooked nothing because there was nothing to cook.")
  56. minetest.get_node_timer(pos):stop()
  57. update_formspec(0, 3, meta)
  58. else
  59. inv:remove_item("input", food_uncooked) -- Clear the slot
  60. local food_cooked = oven.recipes[food_uncooked:get_name()] -- Get the cooked food
  61. inv:add_item("output", food_cooked) -- Put the cooked food in the slot
  62. end
  63. end
  64. minetest.register_node("ma_pops_furniture:oven", {
  65. description = "Oven",
  66. tiles = {
  67. "mp_oven_top.png",
  68. "mp_oven_bottom.png",
  69. "mp_oven_right.png",
  70. "mp_oven_left.png",
  71. "mp_oven_back.png",
  72. "mp_oven_front.png"
  73. },
  74. paramtype2 = "facedir",
  75. groups = {cracky = 2, tubedevice = 1, tubedevice_receiver = 1},
  76. legacy_facedir_simple = true,
  77. is_ground_content = false,
  78. sounds = default.node_sound_stone_defaults(),
  79. drawtype = "nodebox",
  80. node_box = {
  81. type = "fixed",
  82. fixed = {
  83. {-0.5, 0.3125, -0.5, 0.5, 0.5, 0.5},
  84. {-0.5, -0.5, -0.375, 0.5, 0.3125, 0.5},
  85. {-0.4375, -0.4375, -0.4375, 0.4375, 0.25, -0.375},
  86. {-0.375, 0.125, -0.5, 0.375, 0.1875, -0.375},
  87. },
  88. },
  89. can_dig = function(pos, player)
  90. local meta = minetest.get_meta(pos)
  91. local inv = meta:get_inventory()
  92. return inv:is_empty("input") and inv:is_empty("output")
  93. end,
  94. on_timer = function(pos, elapsed)
  95. local meta = minetest.get_meta(pos)
  96. local stack = meta:get_inventory():get_stack("input", 1)
  97. local cooking_time = meta:get_int("cooking_time") or 0
  98. cooking_time = cooking_time + 1
  99. if cooking_time % 3 == 0 then
  100. do_cook_single(pos)
  101. end
  102. update_formspec(cooking_time % 3, 3, meta)
  103. meta:set_int("cooking_time", cooking_time)
  104. --Keep cooking until there is nothing left to cook.
  105. if not stack:is_empty() then
  106. return true
  107. else
  108. meta:set_int("cooking_time", 0)
  109. update_formspec(0, 3, meta)
  110. return false
  111. end
  112. end,
  113. on_metadata_inventory_put = recalculate,
  114. on_metadata_inventory_take = recalculate,
  115. on_construct = function(pos)
  116. local meta = minetest.get_meta(pos)
  117. meta:set_string("formspec", oven_fs)
  118. local inv = meta:get_inventory()
  119. inv:set_size("input", 1)
  120. inv:set_size("output", 1)
  121. end,
  122. on_blast = function(pos)
  123. local drops = {}
  124. default.get_inventory_drops(pos, "input", drops)
  125. default.get_inventory_drops(pos, "output", drops)
  126. table.insert(drops, "ma_pops_furniture:oven")
  127. minetest.remove_node(pos)
  128. return drops
  129. end,
  130. allow_metadata_inventory_put = function(pos, list, index, stack, player)
  131. return oven.recipes[stack:get_name()] and stack:get_count() or 0
  132. end,
  133. })
  134. -- Recipe Registration
  135. oven.register_recipe("default:ice", "default:water_source")
  136. -- No milk bucket as this doesn't support substitutes for now
  137. oven.register_recipe("mobs_mc:chicken_raw", "test:chicken_cooked")
  138. --[[ We don't need to check mod existance when registering recipe
  139. Recipe won't even be executed if there is no raw chicken in input ]]--
  140. oven.register_recipe("mobs_mc:beef_raw", "test:beef_cooked")
  141. oven.register_recipe("farming:coffee_cup", "farming:coffee_cup_hot") -- What a crutch there was...
  142. -- Add needed recipes as you go, note that other mods can add more recipes too