coke.lua 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. local function swap_node(pos, name)
  2. local node = minetest.get_node(pos)
  3. if node.name == name then
  4. return
  5. end
  6. node.name = name
  7. minetest.swap_node(pos, node)
  8. end
  9. minetest.register_node("forge:coke", {
  10. description = "Coke",
  11. tiles = { "default_coal_block.png^[colorize:black:180" },
  12. is_ground_content = true,
  13. groups = {cracky=2, level=2, refractory=3},
  14. sounds = default.node_sound_stone_defaults(),
  15. })
  16. -- -------------------------------------------------
  17. -- enclosure determination
  18. local function pushpos(t, v, p)
  19. local h = minetest.hash_node_position(p)
  20. if v[h] == nil then
  21. table.insert(t, p)
  22. end
  23. end
  24. local function find_blob_extent(startpos)
  25. local blob = {}
  26. local stack = {}
  27. local visited = {}
  28. local shell = {}
  29. local node = minetest.get_node(startpos)
  30. if node.name == "air" then
  31. return nil
  32. end
  33. local bname = node.name
  34. table.insert(stack, startpos)
  35. while #stack > 0 do
  36. local p = table.remove(stack)
  37. local ph = minetest.hash_node_position(p)
  38. print("visiting "..minetest.pos_to_string(p))
  39. if visited[ph] == nil then
  40. visited[ph] = 1
  41. local pn = minetest.get_node(p)
  42. if pn then
  43. if pn.name == bname then
  44. blob[ph] = {x=p.x, y=p.y, z=p.z}
  45. pushpos(stack, visited, {x=p.x+1, y=p.y, z=p.z})
  46. pushpos(stack, visited, {x=p.x-1, y=p.y, z=p.z})
  47. pushpos(stack, visited, {x=p.x, y=p.y+1, z=p.z})
  48. pushpos(stack, visited, {x=p.x, y=p.y-1, z=p.z})
  49. pushpos(stack, visited, {x=p.x, y=p.y, z=p.z+1})
  50. pushpos(stack, visited, {x=p.x, y=p.y, z=p.z-1})
  51. else
  52. shell[pn.name] = (shell[pn.name] or 0) + 1
  53. end
  54. end
  55. end
  56. end
  57. for _,p in pairs(blob) do
  58. print("blob "..minetest.pos_to_string(p))
  59. end
  60. for n,v in pairs(shell) do
  61. print("shell "..n.." - ".. v)
  62. end
  63. return blob, shell
  64. end
  65. -- ---------------------------------------------
  66. local function get_af_active_formspec(fuel_percent, item_percent)
  67. return "size[8,8.5]"..
  68. default.gui_bg..
  69. default.gui_bg_img..
  70. default.gui_slots..
  71. -- "list[context;src;2.75,0.5;1,1;]"..
  72. "list[context;fuel;.75,.5;2,4;]"..
  73. "image[2.75,1.5;1,1;default_furnace_fire_bg.png^[lowpart:"..
  74. (100-fuel_percent)..":default_furnace_fire_fg.png]"..
  75. "image[3.75,1.5;1,1;gui_furnace_arrow_bg.png^[lowpart:"..
  76. (item_percent)..":gui_furnace_arrow_fg.png^[transformR270]"..
  77. -- "list[context;dst;4.75,0.96;2,2;]"..
  78. "list[current_player;main;0,4.25;8,1;]"..
  79. "list[current_player;main;0,5.5;8,3;8]"..
  80. -- "listring[context;dst]"..
  81. -- "listring[current_player;main]"..
  82. -- "listring[context;src]"..
  83. -- "listring[current_player;main]"..
  84. -- "listring[context;fuel]"..
  85. -- "listring[current_player;main]"..
  86. default.get_hotbar_bg(0, 4.25)
  87. end
  88. function get_af_inactive_formspec()
  89. return "size[8,8.5]"..
  90. default.gui_bg..
  91. default.gui_bg_img..
  92. default.gui_slots..
  93. -- "list[context;src;2.75,0.5;1,1;]"..
  94. "list[context;fuel;2.75,2.5;2,2;]"..
  95. "image[2.75,1.5;1,1;default_furnace_fire_bg.png]"..
  96. -- "image[3.75,1.5;1,1;gui_furnace_arrow_bg.png^[transformR270]"..
  97. -- "list[context;dst;4.75,0.96;2,2;]"..
  98. "list[current_player;main;0,4.25;8,1;]"..
  99. "list[current_player;main;0,5.5;8,3;8]"..
  100. "listring[context;dst]"..
  101. "listring[current_player;main]"..
  102. "listring[context;src]"..
  103. "listring[current_player;main]"..
  104. "listring[context;fuel]"..
  105. "listring[current_player;main]"..
  106. default.get_hotbar_bg(0, 4.25)
  107. end
  108. local function grab_fuel(inv)
  109. local list = inv:get_list("fuel")
  110. for i,st in ipairs(list) do
  111. print(st:get_name())
  112. local fuel, remains
  113. fuel, remains = minetest.get_craft_result({
  114. method = "fuel",
  115. width = 1,
  116. items = {
  117. ItemStack(st:get_name())
  118. },
  119. })
  120. if fuel.time > 0 then
  121. -- Take fuel from fuel list
  122. st:take_item()
  123. inv:set_stack("fuel", i, st)
  124. return fuel.time
  125. end
  126. end
  127. return 0 -- no fuel found
  128. end
  129. local function af_on_timer(pos, elapsed)
  130. local meta = minetest.get_meta(pos)
  131. local fuel_time = meta:get_float("fuel_time") or 0
  132. local fuel_burned = meta:get_float("fuel_burned") or 0
  133. local cook_time_remaining = meta:get_float("cook_time_remaining") or 10
  134. local inv = meta:get_inventory()
  135. local burned = elapsed
  136. local turn_off = false
  137. print("\n\naf timer")
  138. print("fuel_burned: " .. fuel_burned)
  139. print("fuel_time: " .. fuel_time)
  140. if fuel_time > 0 and fuel_burned + elapsed < fuel_time then
  141. fuel_burned = fuel_burned + elapsed
  142. meta:set_float("fuel_burned", fuel_burned + elapsed)
  143. else
  144. local t = grab_fuel(inv)
  145. if t <= 0 then -- out of fuel
  146. print("out of fuel")
  147. meta:set_float("fuel_time", 0)
  148. meta:set_float("fuel_burned", 0)
  149. burned = fuel_time - fuel_burned
  150. turn_off = true
  151. else
  152. -- roll into the next period
  153. fuel_burned = elapsed - (fuel_time - fuel_burned)
  154. fuel_time = t
  155. print("fuel remaining: " .. (fuel_time - fuel_burned))
  156. meta:set_float("fuel_time", fuel_time)
  157. meta:set_float("fuel_burned", fuel_burned)
  158. end
  159. end
  160. if burned > 0 then
  161. local remain = cook_time_remaining - burned
  162. print("remain: ".. remain);
  163. if remain > 0 then
  164. meta:set_float("cook_time_remaining", remain)
  165. else
  166. print("finished")
  167. -- convert coal to coke
  168. local blobs = meta:get_string("blob") or ""
  169. local blob = minetest.deserialize(blobs)
  170. local p
  171. for i,pp in pairs(blob) do
  172. p = pp
  173. blob[i] = nil
  174. break
  175. end
  176. minetest.set_node(p, {name="forge:coke"})
  177. meta:set_string("blob", minetest.serialize(blob))
  178. meta:set_float("cook_time_remaining", 5)
  179. end
  180. end
  181. if turn_off then
  182. swap_node(pos, "forge:coke_furnace")
  183. return
  184. end
  185. fuel_pct = math.floor((fuel_burned * 100) / fuel_time)
  186. -- item_pct = math.floor((fuel_burned * 100) / fuel_time)
  187. meta:set_string("formspec", get_af_active_formspec(fuel_pct, 0))
  188. meta:set_string("infotext", "Fuel: " .. fuel_pct)
  189. --minetest.get_node_timer(pos):start(1.0)
  190. return true
  191. end
  192. local shell_nodes = {
  193. ["forge:coke_furnace"] = 1,
  194. ["forge:coke_furnace_on"] = 1,
  195. ["default:brick"] = 1,
  196. ["forge:refractory_brick"] = 1,
  197. }
  198. local function check_coke_oven(furnacepos)
  199. local fnode = minetest.get_node(furnacepos)
  200. local fdir = minetest.facedir_to_dir(fnode.param2)
  201. local seedp = vector.add(furnacepos, fdir)
  202. local seedn = minetest.get_node(seedp)
  203. if seedn == nil or seedn.name ~= "default:coalblock" then
  204. print("not coal")
  205. return false
  206. end
  207. local fuel, oven = find_blob_extent(seedp)
  208. local oven_intact = 1
  209. for name,_ in pairs(oven) do
  210. if shell_nodes[name] == nil then
  211. print("failing for ".. name)
  212. oven_intact = 0
  213. break
  214. end
  215. end
  216. if oven_intact == 0 then
  217. print("not intact")
  218. return false
  219. end
  220. local meta = minetest.get_meta(furnacepos)
  221. meta:set_string("blob", minetest.serialize(fuel))
  222. return true
  223. end
  224. minetest.register_node("forge:coke_furnace_on", {
  225. description = "Coke furnace (active)",
  226. tiles = {
  227. "default_steel_block.png", "default_steel_block.png",
  228. "default_steel_block.png", "default_steel_block.png",
  229. "default_steel_block.png",
  230. {
  231. image = "default_furnace_front_active.png",
  232. backface_culling = false,
  233. animation = {
  234. type = "vertical_frames",
  235. aspect_w = 16,
  236. aspect_h = 16,
  237. length = 1.5
  238. },
  239. }
  240. },
  241. paramtype2 = "facedir",
  242. groups = {cracky=2},
  243. legacy_facedir_simple = true,
  244. is_ground_content = false,
  245. sounds = default.node_sound_stone_defaults(),
  246. stack_max = 1,
  247. can_dig = can_dig,
  248. on_timer = af_on_timer,
  249. on_construct = function(pos)
  250. local meta = minetest.get_meta(pos)
  251. meta:set_string("formspec", get_af_inactive_formspec())
  252. local inv = meta:get_inventory()
  253. inv:set_size('fuel', 4)
  254. minetest.get_node_timer(pos):start(1.0)
  255. end,
  256. on_metadata_inventory_move = function(pos)
  257. minetest.get_node_timer(pos):start(1.0)
  258. end,
  259. on_metadata_inventory_put = function(pos)
  260. -- start timer function, it will sort out whether furnace can burn or not.
  261. minetest.get_node_timer(pos):start(1.0)
  262. end,
  263. on_punch = function(pos)
  264. swap_node(pos, "forge:coke_furnace")
  265. end,
  266. -- on_blast = function(pos)
  267. -- local drops = {}
  268. -- default.get_inventory_drops(pos, "src", drops)
  269. -- default.get_inventory_drops(pos, "fuel", drops)
  270. -- default.get_inventory_drops(pos, "dst", drops)
  271. -- drops[#drops+1] = "machines:machine"
  272. -- minetest.remove_node(pos)
  273. -- return drops
  274. -- end,
  275. allow_metadata_inventory_put = allow_metadata_inventory_put,
  276. allow_metadata_inventory_move = allow_metadata_inventory_move,
  277. allow_metadata_inventory_take = allow_metadata_inventory_take,
  278. })
  279. minetest.register_node("forge:coke_furnace", {
  280. description = "Coke Furnace",
  281. tiles = {
  282. "default_steel_block.png", "default_steel_block.png",
  283. "default_steel_block.png", "default_steel_block.png",
  284. "default_steel_block.png", "default_furnace_front.png"
  285. },
  286. paramtype2 = "facedir",
  287. groups = {cracky=2},
  288. legacy_facedir_simple = true,
  289. is_ground_content = false,
  290. sounds = default.node_sound_stone_defaults(),
  291. stack_max = 1,
  292. can_dig = can_dig,
  293. --on_timer = af_node_timer,
  294. on_construct = function(pos)
  295. local meta = minetest.get_meta(pos)
  296. meta:set_string("formspec", get_af_inactive_formspec())
  297. local inv = meta:get_inventory()
  298. inv:set_size('fuel', 4)
  299. end,
  300. on_metadata_inventory_move = function(pos)
  301. --minetest.get_node_timer(pos):start(1.0)
  302. end,
  303. on_metadata_inventory_put = function(pos)
  304. -- start timer function, it will sort out whether furnace can burn or not.
  305. --minetest.get_node_timer(pos):start(1.0)
  306. end,
  307. on_punch = function(pos, node, player)
  308. if check_coke_oven(pos) then
  309. swap_node(pos, "forge:coke_furnace_on")
  310. minetest.get_node_timer(pos):start(1.0)
  311. else
  312. minetest.chat_send_player(player:get_player_name(), "Coke oven is incomplete.")
  313. end
  314. end,
  315. -- on_blast = function(pos)
  316. -- local drops = {}
  317. -- default.get_inventory_drops(pos, "src", drops)
  318. -- default.get_inventory_drops(pos, "fuel", drops)
  319. -- default.get_inventory_drops(pos, "dst", drops)
  320. -- drops[#drops+1] = "machines:machine"
  321. -- minetest.remove_node(pos)
  322. -- return drops
  323. -- end,
  324. allow_metadata_inventory_put = allow_metadata_inventory_put,
  325. allow_metadata_inventory_move = allow_metadata_inventory_move,
  326. allow_metadata_inventory_take = allow_metadata_inventory_take,
  327. })
  328. minetest.register_craft({
  329. output = 'forge:coke_furnace',
  330. recipe = {
  331. {'forge:refractory_clay_brick', 'forge:refractory_clay_brick', 'forge:refractory_clay_brick'},
  332. {'forge:refractory_clay_brick', '', 'forge:refractory_clay_brick'},
  333. {'forge:refractory_clay_brick', 'forge:refractory_clay_brick', 'forge:refractory_clay_brick'},
  334. }
  335. })
  336. minetest.register_craft({
  337. type = "fuel",
  338. recipe = "forge:coke",
  339. burntime = 120,
  340. })