Conserve left-over microblocks (#207)

This commit is contained in:
The4codeblocks 2025-04-16 09:37:25 -05:00 committed by GitHub
parent 20689b1f18
commit 768225a990
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 9 deletions

View File

@ -20,6 +20,9 @@ circular_saw.known_stairs = setmetatable({}, {
-- This is populated by stairsplus:register_all: -- This is populated by stairsplus:register_all:
circular_saw.known_nodes = {} circular_saw.known_nodes = {}
-- This is populated by stairsplus:register_micro:
circular_saw.microblocks = {}
-- How many microblocks does this shape at the output inventory cost: -- How many microblocks does this shape at the output inventory cost:
-- It may cause slight loss, but no gain. -- It may cause slight loss, but no gain.
circular_saw.cost_in_microblocks = { circular_saw.cost_in_microblocks = {
@ -136,11 +139,14 @@ function circular_saw:reset(pos)
end end
inv:set_list("input", {}) inv:set_list("input", {})
inv:set_list("micro", {}) --inv:set_list("micro", {})
inv:set_list("output", {})
meta:set_int("anz", 0) local microblockcount = inv:get_stack("micro",1):get_count()
meta:set_int("anz", microblockcount)
if microblockcount == 0 then
meta:set_string("infotext", S("Circular Saw is empty") .. owned_by) meta:set_string("infotext", S("Circular Saw is empty") .. owned_by)
inv:set_list("output", {})
end
end end
@ -161,19 +167,22 @@ function circular_saw:update_inventory(pos, amount)
end end
local stack = inv:get_stack("input", 1) local stack = inv:get_stack("input", 1)
local microstack = inv:get_stack("micro",1)
-- At least one "normal" block is necessary to see what kind of stairs are requested. -- At least one "normal" block is necessary to see what kind of stairs are requested.
if stack:is_empty() then if stack:is_empty() and microstack:is_empty() then
-- Any microblocks not taken out yet are now lost. -- Any microblocks not taken out yet are now lost.
-- (covers material loss in the machine) -- (covers material loss in the machine)
self:reset(pos) self:reset(pos)
return return
end end
local node_name = stack:get_name() or ""
local node_name = stack:get_name() or "" -- planned to extract from microblock TODO
local node_def = stack:get_definition() local node_def = stack:get_definition()
local name_parts = circular_saw.known_nodes[node_name] or "" local name_parts = circular_saw.known_nodes[node_name] or ""
local modname = name_parts[1] or "" local modname = name_parts[1] or circular_saw.microblocks[microstack:get_name()][1]
local material = name_parts[2] or "" local material = name_parts[2] or circular_saw.microblocks[microstack:get_name()][2]
local owned_by = meta:get_string("owner") local owned_by = meta:get_string("owner")
if owned_by and owned_by ~= "" then if owned_by and owned_by ~= "" then
@ -197,6 +206,7 @@ function circular_saw:update_inventory(pos, amount)
inv:set_list("micro", { inv:set_list("micro", {
modname .. ":micro_" .. material .. " " .. (amount % 8) modname .. ":micro_" .. material .. " " .. (amount % 8)
}) })
-- Display: -- Display:
inv:set_list("output", inv:set_list("output",
self:get_output_inv(modname, material, amount, self:get_output_inv(modname, material, amount,
@ -354,7 +364,6 @@ function circular_saw.on_metadata_inventory_take(
-- We do know how much each block at each position costs: -- We do know how much each block at each position costs:
local cost = circular_saw.cost_in_microblocks[index] local cost = circular_saw.cost_in_microblocks[index]
* stack:get_count() * stack:get_count()
circular_saw:update_inventory(pos, -cost) circular_saw:update_inventory(pos, -cost)
elseif listname == "micro" then elseif listname == "micro" then
-- Each microblock costs 1 microblock: -- Each microblock costs 1 microblock:

View File

@ -40,4 +40,5 @@ function stairsplus:register_micro(modname, subname, recipeitem, fields)
end end
circular_saw.known_nodes[recipeitem] = {modname, subname} circular_saw.known_nodes[recipeitem] = {modname, subname}
circular_saw.microblocks[modname.. ":micro_" .. subname] = {modname, subname}
end end