forked from VoxeLibre/VoxeLibre
Furnaces can skip the night and use in-game time when possible
This commit is contained in:
parent
14027b96d1
commit
f1c01e2785
|
@ -1,5 +1,6 @@
|
||||||
|
|
||||||
local S = minetest.get_translator("mcl_furnaces")
|
local S = minetest.get_translator("mcl_furnaces")
|
||||||
|
local time_multiplier = 24*60*60 / (minetest.settings:get('time_speed') or 72)
|
||||||
|
|
||||||
--
|
--
|
||||||
-- Formspecs
|
-- Formspecs
|
||||||
|
@ -163,45 +164,91 @@ local function furnace_node_timer(pos, elapsed)
|
||||||
local src_item = meta:get_string("src_item") or ""
|
local src_item = meta:get_string("src_item") or ""
|
||||||
local fuel_totaltime = meta:get_float("fuel_totaltime") or 0
|
local fuel_totaltime = meta:get_float("fuel_totaltime") or 0
|
||||||
|
|
||||||
|
local current_game_time = .0 + ((minetest.get_day_count() + minetest.get_timeofday()) * time_multiplier)
|
||||||
|
|
||||||
|
local last_game_time = meta:get_string("last_gametime") -- FIXME: In Windows s(g)et_float() works OK but under Linux it returns rounded 2-byte values like 449540.000000000 which are unusable
|
||||||
|
if last_game_time then
|
||||||
|
last_game_time = tonumber(last_game_time)
|
||||||
|
end
|
||||||
|
if not last_game_time or last_game_time < 1 then
|
||||||
|
last_game_time = current_game_time
|
||||||
|
elseif last_game_time == current_game_time then
|
||||||
|
current_game_time = current_game_time + 1.0
|
||||||
|
end
|
||||||
|
local elapsed_game_time = .0 + current_game_time - last_game_time
|
||||||
|
|
||||||
local inv = meta:get_inventory()
|
local inv = meta:get_inventory()
|
||||||
local srclist, fuellist
|
local srclist, fuellist
|
||||||
|
|
||||||
local cookable, cooked
|
local cookable, cooked
|
||||||
|
local active
|
||||||
local fuel
|
local fuel
|
||||||
|
|
||||||
local update = true
|
|
||||||
while update do
|
|
||||||
update = false
|
|
||||||
|
|
||||||
srclist = inv:get_list("src")
|
srclist = inv:get_list("src")
|
||||||
fuellist = inv:get_list("fuel")
|
fuellist = inv:get_list("fuel")
|
||||||
|
|
||||||
--
|
|
||||||
-- Cooking
|
|
||||||
--
|
|
||||||
|
|
||||||
-- Check if we have cookable content
|
|
||||||
local aftercooked
|
|
||||||
cooked, aftercooked = minetest.get_craft_result({method = "cooking", width = 1, items = srclist})
|
|
||||||
cookable = cooked.time ~= 0
|
|
||||||
|
|
||||||
-- Check if src item has been changed
|
-- Check if src item has been changed
|
||||||
if srclist[1]:get_name() ~= src_item then
|
if srclist[1]:get_name() ~= src_item then
|
||||||
-- Reset cooking progress in this case
|
-- Reset cooking progress in this case
|
||||||
src_time = 0
|
src_time = 0
|
||||||
src_item = srclist[1]:get_name()
|
src_item = srclist[1]:get_name()
|
||||||
update = true
|
elapsed_game_time = 1 -- ?FIXME? Can break mechanics if it loads furnaces during 'skip the night'
|
||||||
|
-- added to prevent instant cooking when we didn't touch the furnace for a long time and then load it
|
||||||
|
end
|
||||||
|
|
||||||
-- Check if we have enough fuel to burn
|
local update = true
|
||||||
elseif fuel_time < fuel_totaltime then
|
while elapsed_game_time > 0.00001 and update do
|
||||||
-- The furnace is currently active and has enough fuel
|
--
|
||||||
fuel_time = fuel_time + elapsed
|
-- Cooking
|
||||||
-- If there is a cookable item then check if it is ready yet
|
--
|
||||||
|
|
||||||
|
local el = elapsed_game_time
|
||||||
|
|
||||||
|
-- Check if we have cookable content: cookable
|
||||||
|
local aftercooked
|
||||||
|
cooked, aftercooked = minetest.get_craft_result({method = "cooking", width = 1, items = srclist})
|
||||||
|
cookable = cooked.time ~= 0
|
||||||
if cookable then
|
if cookable then
|
||||||
-- Successful cooking requires space in dst slot and time
|
-- Successful cooking requires space in dst slot and time
|
||||||
if inv:room_for_item("dst", cooked.item) then
|
if not inv:room_for_item("dst", cooked.item) then
|
||||||
src_time = src_time + elapsed
|
cookable = false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if cookable then -- fuel lasts long enough, adjust el to cooking duration
|
||||||
|
el = math.min(el, cooked.time - src_time)
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Check if we have enough fuel to burn
|
||||||
|
active = fuel_time < fuel_totaltime
|
||||||
|
if cookable and not active then
|
||||||
|
-- We need to get new fuel
|
||||||
|
local afterfuel
|
||||||
|
fuel, afterfuel = minetest.get_craft_result({method = "fuel", width = 1, items = fuellist})
|
||||||
|
|
||||||
|
if fuel.time == 0 then
|
||||||
|
-- No valid fuel in fuel list -- stop
|
||||||
|
fuel_totaltime = 0
|
||||||
|
src_time = 0
|
||||||
|
update = false
|
||||||
|
else
|
||||||
|
-- Take fuel from fuel list
|
||||||
|
inv:set_stack("fuel", 1, afterfuel.items[1])
|
||||||
|
fuel_time = 0
|
||||||
|
fuel_totaltime = fuel.time
|
||||||
|
el = math.min(el, fuel_totaltime)
|
||||||
|
active = true
|
||||||
|
fuellist = inv:get_list("fuel")
|
||||||
|
end
|
||||||
|
elseif active then
|
||||||
|
el = math.min(el, fuel_totaltime - fuel_time)
|
||||||
|
-- The furnace is currently active and has enough fuel
|
||||||
|
fuel_time = fuel_time + el
|
||||||
|
end
|
||||||
|
|
||||||
|
-- If there is a cookable item then check if it is ready yet
|
||||||
|
if cookable and active then
|
||||||
|
src_time = src_time + el
|
||||||
-- Place result in dst list if done
|
-- Place result in dst list if done
|
||||||
if src_time >= cooked.time then
|
if src_time >= cooked.time then
|
||||||
inv:add_item("dst", cooked.item)
|
inv:add_item("dst", cooked.item)
|
||||||
|
@ -211,54 +258,26 @@ local function furnace_node_timer(pos, elapsed)
|
||||||
if inv:get_stack("fuel", 1):get_name() == "mcl_buckets:bucket_empty" then
|
if inv:get_stack("fuel", 1):get_name() == "mcl_buckets:bucket_empty" then
|
||||||
if srclist[1]:get_name() == "mcl_sponges:sponge_wet" then
|
if srclist[1]:get_name() == "mcl_sponges:sponge_wet" then
|
||||||
inv:set_stack("fuel", 1, "mcl_buckets:bucket_water")
|
inv:set_stack("fuel", 1, "mcl_buckets:bucket_water")
|
||||||
|
fuellist = inv:get_list("fuel")
|
||||||
-- Also for river water
|
-- Also for river water
|
||||||
elseif srclist[1]:get_name() == "mcl_sponges:sponge_wet_river_water" then
|
elseif srclist[1]:get_name() == "mcl_sponges:sponge_wet_river_water" then
|
||||||
inv:set_stack("fuel", 1, "mcl_buckets:bucket_river_water")
|
inv:set_stack("fuel", 1, "mcl_buckets:bucket_river_water")
|
||||||
|
fuellist = inv:get_list("fuel")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
src_time = 0
|
srclist = inv:get_list("src")
|
||||||
update = true
|
|
||||||
end
|
|
||||||
elseif src_time ~= 0 then
|
|
||||||
-- If output slot is occupied, stop cooking
|
|
||||||
src_time = 0
|
|
||||||
update = true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
else
|
|
||||||
-- Furnace ran out of fuel
|
|
||||||
if cookable then
|
|
||||||
-- We need to get new fuel
|
|
||||||
local afterfuel
|
|
||||||
fuel, afterfuel = minetest.get_craft_result({method = "fuel", width = 1, items = fuellist})
|
|
||||||
|
|
||||||
if fuel.time == 0 then
|
|
||||||
-- No valid fuel in fuel list
|
|
||||||
fuel_totaltime = 0
|
|
||||||
src_time = 0
|
|
||||||
else
|
|
||||||
-- Take fuel from fuel list
|
|
||||||
inv:set_stack("fuel", 1, afterfuel.items[1])
|
|
||||||
update = true
|
|
||||||
fuel_totaltime = fuel.time + (fuel_time - fuel_totaltime)
|
|
||||||
src_time = src_time + elapsed
|
|
||||||
end
|
|
||||||
else
|
|
||||||
-- We don't need to get new fuel since there is no cookable item
|
|
||||||
fuel_totaltime = 0
|
|
||||||
src_time = 0
|
src_time = 0
|
||||||
end
|
end
|
||||||
fuel_time = 0
|
|
||||||
end
|
end
|
||||||
|
|
||||||
elapsed = 0
|
elapsed_game_time = elapsed_game_time - el
|
||||||
end
|
end
|
||||||
|
|
||||||
if fuel and fuel_totaltime > fuel.time then
|
if fuel and fuel_totaltime > fuel.time then
|
||||||
fuel_totaltime = fuel.time
|
fuel_totaltime = fuel.time
|
||||||
end
|
end
|
||||||
if srclist[1]:is_empty() then
|
if srclist and srclist[1]:is_empty() then
|
||||||
src_time = 0
|
src_time = 0
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -274,7 +293,7 @@ local function furnace_node_timer(pos, elapsed)
|
||||||
|
|
||||||
local result = false
|
local result = false
|
||||||
|
|
||||||
if fuel_totaltime ~= 0 then
|
if active then
|
||||||
local fuel_percent = math.floor(fuel_time / fuel_totaltime * 100)
|
local fuel_percent = math.floor(fuel_time / fuel_totaltime * 100)
|
||||||
formspec = active_formspec(fuel_percent, item_percent)
|
formspec = active_formspec(fuel_percent, item_percent)
|
||||||
swap_node(pos, "mcl_furnaces:furnace_active")
|
swap_node(pos, "mcl_furnaces:furnace_active")
|
||||||
|
@ -292,8 +311,13 @@ local function furnace_node_timer(pos, elapsed)
|
||||||
meta:set_float("fuel_totaltime", fuel_totaltime)
|
meta:set_float("fuel_totaltime", fuel_totaltime)
|
||||||
meta:set_float("fuel_time", fuel_time)
|
meta:set_float("fuel_time", fuel_time)
|
||||||
meta:set_float("src_time", src_time)
|
meta:set_float("src_time", src_time)
|
||||||
|
if srclist then
|
||||||
meta:set_string("src_item", srclist[1]:get_name())
|
meta:set_string("src_item", srclist[1]:get_name())
|
||||||
|
else
|
||||||
|
meta:set_string("src_item", "")
|
||||||
|
end
|
||||||
meta:set_string("formspec", formspec)
|
meta:set_string("formspec", formspec)
|
||||||
|
meta:set_string("last_gametime", tostring(current_game_time)) -- FIXME: In Windows s(g)et_float() works OK but under Linux it returns rounded 2-byte values like 449540.000000000 which are unusable
|
||||||
|
|
||||||
return result
|
return result
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue