2018-01-07 00:41:55 +00:00
local NAME = minetest.get_current_modname ( )
2019-10-04 03:13:42 +00:00
local MAX_ENERGY = 300
2018-01-06 22:52:25 +00:00
local modifier = 10 -- Cooking cost modifier
local time_modifier = 1.5 -- Cooking time modifier
2019-02-24 11:57:30 +00:00
local formspec = sparktech.add_inventory ( 6 , 1.5 ,
" list[current_name;source;0,0;2,1;] " ..
2019-06-28 17:39:22 +00:00
" list[current_name;product;7.5,0;2,1;] " )
2017-08-20 14:00:56 +00:00
2017-12-31 18:06:29 +00:00
local function is_smeltable ( items )
2018-01-03 22:20:30 +00:00
local cooked
local aftercooked
cooked , aftercooked = minetest.get_craft_result ( { method = " cooking " , width = 1 , items = items } )
return cooked.time ~= 0
2017-12-31 18:06:29 +00:00
end
local function is_item_allowed ( pos , target , _ , stack )
2019-02-25 17:21:48 +00:00
if target ~= " source " then return 0 end --so no items get moved into the produce inventory by the user
2018-01-03 22:20:30 +00:00
if stack == nil or not is_smeltable ( { stack } ) then --invalid or unsmeltable items dont get in
return 0
else
return stack : get_count ( )
end
2017-12-31 18:06:29 +00:00
end
local function is_item_allowed_move ( pos , src , sidx , target , tidx , c , p )
2018-01-03 22:20:30 +00:00
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 )
2017-12-31 18:06:29 +00:00
end
2017-01-13 20:18:45 +00:00
local function ongetitem ( pos )
2018-01-03 22:20:30 +00:00
local meta = minetest.get_meta ( pos )
2018-09-20 22:39:48 +00:00
if meta : get_int ( " energy " ) < modifier then return end
2018-01-03 22:20:30 +00:00
local timer = minetest.get_node_timer ( pos )
timer : start ( 1.0 )
2017-01-11 23:10:52 +00:00
end
2018-01-06 22:52:25 +00:00
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 ..
2019-06-28 17:39:22 +00:00
sparktech.makebar ( " energy2.png " , 0 , 1.125 , 9.75 , 0.25 ,
2019-10-04 03:13:42 +00:00
energy , minetest.get_item_group ( MAX_ENERGY )
2018-01-06 22:52:25 +00:00
, 0 )
2019-06-28 17:39:22 +00:00
for item = 0 , 1 do
2018-01-06 22:52:25 +00:00
local cooked
local aftercooked
2019-06-28 17:39:22 +00:00
local progress = meta : get_int ( " progress_ " .. ( item + 1 ) )
cooked , aftercooked = minetest.get_craft_result ( { method = " cooking " , width = 1 , items = { inventory : get_stack ( " source " , ( item + 1 ) ) } } )
2018-01-06 22:52:25 +00:00
cfmsp = cfmsp ..
2019-06-28 17:39:22 +00:00
sparktech.makebar ( " progress2.png " , item * 1.25 , 0 , 1 , 1 ,
2018-01-06 22:52:25 +00:00
progress , math.ceil ( cooked.time * time_modifier ) , 0 )
end
meta : set_string ( " formspec " , cfmsp )
end
2017-01-03 02:39:13 +00:00
local function mytime ( pos , elapsed )
2018-01-03 22:20:30 +00:00
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 " )
2018-09-20 22:44:47 +00:00
2018-01-04 19:37:29 +00:00
for item = 1 , 2 do
2018-09-20 22:44:47 +00:00
if energy >= modifier then
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 )
end
2018-01-04 19:37:29 +00:00
else
2018-09-20 22:44:47 +00:00
meta : set_int ( " progress_ " .. item , progress + 1 )
energy = energy - modifier
2018-01-04 19:37:29 +00:00
end
end
end
2018-01-06 22:52:25 +00:00
2018-09-20 22:44:47 +00:00
meta : set_int ( " energy " , energy )
2018-01-07 14:05:17 +00:00
update_formspec ( pos ) -- Later this should only be done when a player looks into the block
2018-01-06 22:52:25 +00:00
2018-01-04 19:37:29 +00:00
--TODO: for the formspec code, if it is marked as active update it here
2018-09-20 22:44:47 +00:00
local has_items = false -- Keep updating if there are still items
for _ , e in pairs ( meta : get_inventory ( ) : get_list ( " source " ) ) do
has_items = has_items or not e : is_empty ( )
end
if has_items then
2018-01-03 22:20:30 +00:00
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
2017-01-13 20:18:45 +00:00
end
2017-01-13 20:25:20 +00:00
local function maytake ( pos , listname , index , stack , player )
2018-01-03 22:20:30 +00:00
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
2017-01-13 20:18:45 +00:00
end
2018-01-07 00:41:55 +00:00
minetest.register_node ( NAME .. " :lv_furnace " , {
2018-01-03 22:20:30 +00:00
description = " Electric furnace " ,
tiles = {
2019-10-04 00:41:29 +00:00
NAME .. " _steel_sideplate.png " ,
NAME .. " _steel_sideplate.png " ,
NAME .. " _steel_sideplate.png " ,
NAME .. " _steel_sideplate.png " ,
NAME .. " _steel_backplate.png " ,
NAME .. " _furnace_frontplate.png "
2018-01-03 22:20:30 +00:00
} ,
paramtype2 = " facedir " ,
groups = {
2019-10-04 01:18:27 +00:00
sparktech_techy = WRENCHABLE ,
sparktech_energy_type = ENERGY_CONSUMER ,
sparktech_net_trigger = TRUE ,
2019-10-04 03:13:42 +00:00
sparktech_energy_max = MAX_ENERGY ,
2019-10-04 02:01:06 +00:00
sparktech_energy_wakeup = 10
2018-01-03 22:20:30 +00:00
} ,
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 )
2018-01-06 22:52:25 +00:00
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
return false
2018-01-03 22:20:30 +00:00
end ,
allow_metadata_inventory_put = is_item_allowed ,
allow_metadata_inventory_move = is_item_allowed_move ,
2018-01-07 14:57:42 +00:00
allow_metadata_inventory_take = function ( _ , _ , _ , stack ) return stack : get_count ( ) end , -- Might want to improve this
2018-01-03 22:20:30 +00:00
--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
2018-01-06 22:52:25 +00:00
} )