2018-01-07 01:41:55 +01:00

176 lines
5.8 KiB
Lua

local NAME = minetest.get_current_modname()
local modifier = 10 -- Cooking cost modifier
local time_modifier = 1.5 -- Cooking time modifier
local formspec =
"size[8,6.5]"..
"list[current_name;source;0,0;1,2;]" ..
"list[current_name;product;7,0;1,2;]" ..
"list[current_player;main;0,2.5;8,3;8]" ..
"list[current_player;main;0,5.7;8,1;]"..
"bgcolor[#FC05E344;false]" ..
"listcolors[#fc059db0;#fc059dd0]"
local function is_smeltable(items)
local cooked
local aftercooked
cooked, aftercooked = minetest.get_craft_result({method = "cooking", width = 1, items = items})
return cooked.time ~= 0
end
local function is_item_allowed(pos, target, _, stack)
if target == "product" then return 0 end --so no items get moved into the souce inventory by the user
if stack == nil or not is_smeltable({stack}) then --invalid or unsmeltable items dont get in
return 0
else
return stack:get_count()
end
end
local function is_item_allowed_move(pos, src, sidx, target, tidx, c, p)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
local stack = inv:get_stack(src, sidx)
return is_item_allowed(pos, target, tidx, stack, p)
end
local function ongetitem(pos)
local meta = minetest.get_meta(pos)
if meta:get_int("energy") == 0 then return end
local timer = minetest.get_node_timer(pos)
timer:start(1.0)
end
local function update_formspec(pos)
local meta = minetest.get_meta(pos)
local inventory = meta:get_inventory()
local cfmsp = formspec
local energy = meta:get_int("energy")
cfmsp = cfmsp ..
sparktech.makebar("energy2.png", 0, 2.1, 9.75, 0.25,
energy, minetest.get_item_group(minetest.get_node(pos).name, "sparktech_energy_max")
, 0)
for item=1, 2 do -- Later this should only be done when a player looks into the block
local cooked
local aftercooked
local progress = meta:get_int("progress_" .. item)
cooked, aftercooked = minetest.get_craft_result({method = "cooking", width = 1, items = {inventory:get_stack("source", item)}})
cfmsp = cfmsp ..
sparktech.makebar("progress2.png", 0, -1 + item, 1, 1,
progress, math.ceil(cooked.time * time_modifier), 0)
end
meta:set_string("formspec", cfmsp)
end
local function mytime(pos, elapsed)
local meta = minetest.get_meta(pos)
local energy = meta:get_int("energy")
local inventory = meta:get_inventory()
local source = inventory:get_list("source")
local product = inventory:get_list("product")
local counter = 0
local newcycle = true -- set to false if not new cycle is required
for item=1,2 do
if energy == 0 then return end --if no energy is available return, rest of the function doesnt matter
local cooked
local aftercooked
local progress = meta:get_int("progress_" .. item)
cooked, aftercooked = minetest.get_craft_result({method = "cooking", width = 1, items = {inventory:get_stack("source", item)}})
--source and product assigned
if progress >= math.ceil(cooked.time * time_modifier) then
if inventory:room_for_item("product", cooked.item) then
inventory:add_item("product", cooked.item)
inventory:set_stack("source", item, aftercooked.items[1])
meta:set_int("progress_" .. item, 0)
else
counter = counter +1
end
else
meta:set_int("progress_" .. item, progress + 1)
energy = energy - 1 * modifier
meta:set_int("energy", energy)
end
-- minetest.debug(dump(pos) .. " : " .. dump(item))
end
update_formspec(pos)
if counter == 2 then
newcycle = false -- this meens that both finished cooking and the other inventory is full
end
--TODO: for the formspec code, if it is marked as active update it here
if (newcycle) then
minetest.get_node_timer(pos):start(1.0) -- it was determined that a new cycle is required, this is the case if energy and material to cook is available
end
return
end
local function maytake(pos, listname, index, stack, player)
local meta = minetest.get_meta(pos)
if listname == "product" then
return tonumber(stack:get_count()) --sure, take the stuff that the furnace has cooked for you :D
end
-- TODO: code to stop people from taking halfcooked items, they'd burn their fingers
end
minetest.register_node( NAME .. ":lv_furnace", {
description = "Electric furnace",
tiles = {
"furnace2.png",
"furnace2.png",
"furnace2.png",
"furnace2.png",
"furnace2.png",
"furnace.png"
},
paramtype2 = "facedir",
groups = {
sparktech_techy = 1,
sparktech_energy_type = 4,
sparktech_net_trigger = 1,
sparktech_energy_max = 300,
spark_energy_timer = 2
},
on_timer = mytime, -- add a mytimer function
allow_metadata_inventory_take = maytake,
--on_righclick = function(pos, node, player, itemstack, pointed_thing)
--TODO show formspec here, and store that a formspec is open in meta, the mytimer function schould update the formspec if it is open, recievefields schould mark it as closed
--end,
on_construct = function(pos, placer)
local meta = minetest.get_meta(pos)
meta:set_string("formspec",formspec) --gonna replace this with crafting the formspec on rightclick instead
local inventory = meta:get_inventory()
inventory:set_size('source',2)
inventory:set_size('product',2)
end,
on_rightclick = function(pos, clicker) -- Requires Version >0.4.16 to trigger because a custom formspec is set
minetest.debug("Ping!")
return false
end,
allow_metadata_inventory_put = is_item_allowed,
allow_metadata_inventory_move = is_item_allowed_move,
--on_metadata_inventory_move = ongetitem, --doesnt seem necesarry to me, if the item moves in the inventory schould not change the cooking process
on_metadata_inventory_put = ongetitem
})
minetest.register_craft({
output = NAME .. ":lv_furnace",
recipe = { { 'group:steelplate', 'group:steelplate', 'group:steelplate' },
{ 'group:steelplate', 'default:copper_ingot', 'group:steelplate' },
{ 'group:steelplate', 'group:steelplate', 'group:steelplate' }}
})