Compare commits
No commits in common. "compacting-storage/v2" and "main" have entirely different histories.
compacting
...
main
1 changed files with 96 additions and 126 deletions
|
|
@ -6,42 +6,11 @@
|
||||||
|
|
||||||
-- constants
|
-- constants
|
||||||
|
|
||||||
local MAIN_TICKER = ""
|
local INVENTORY = "left"
|
||||||
local REQUEST_TICKER = ""
|
|
||||||
local CRAFTING_REQUESTER = ""
|
|
||||||
local STORAGE_ADDR = ""
|
|
||||||
local COMPACTING_ADDR = ""
|
|
||||||
local UNPACKING_ADDR = ""
|
|
||||||
local SLEEP_TIME = 30
|
local SLEEP_TIME = 30
|
||||||
local NUGGET_RATIO = 63 -- amount of nuggets per craft
|
local NUGGET_RATIO = 63 -- amount of nuggets per craft
|
||||||
local BLOCK_RATIO = 7 -- amount of blocks per craft
|
local BLOCK_RATIO = 7 -- amount of blocks per craft
|
||||||
local ITEM_TYPES = {
|
local ITEM_TYPES = { "iron", "copper", "zinc", "gold", "electrum" }
|
||||||
iron = {
|
|
||||||
nugget = "minecraft",
|
|
||||||
ingot = "minecraft",
|
|
||||||
block = "minecraft",
|
|
||||||
},
|
|
||||||
copper = {
|
|
||||||
nugget = "create",
|
|
||||||
ingot = "minecraft",
|
|
||||||
block = "minecraft",
|
|
||||||
},
|
|
||||||
zinc = {
|
|
||||||
nugget = "create",
|
|
||||||
ingot = "create",
|
|
||||||
block = "create",
|
|
||||||
},
|
|
||||||
gold = {
|
|
||||||
nugget = "minecraft",
|
|
||||||
ingot = "minecraft",
|
|
||||||
block = "minecraft",
|
|
||||||
},
|
|
||||||
electrum = {
|
|
||||||
nugget = "createaddition",
|
|
||||||
ingot = "createaddition",
|
|
||||||
block = "createaddition",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
local STACKS = 64
|
local STACKS = 64
|
||||||
local NUGGETS = 1
|
local NUGGETS = 1
|
||||||
|
|
@ -52,90 +21,100 @@ local BLOCKS = 81
|
||||||
local MIN_NUM = (4 * STACKS * NUGGETS) + (4 * STACKS * INGOTS)
|
local MIN_NUM = (4 * STACKS * NUGGETS) + (4 * STACKS * INGOTS)
|
||||||
local MAX_NUM = (8 * STACKS * NUGGETS) + (8 * STACKS * INGOTS)
|
local MAX_NUM = (8 * STACKS * NUGGETS) + (8 * STACKS * INGOTS)
|
||||||
|
|
||||||
local getName = {
|
-- TODO map targets to specific relays and faces
|
||||||
nugget = function (item_type)
|
-- Key - 1: nuggets_to_ingots, 2: ingots_to_blocks, 3: blocks_to_ingots,
|
||||||
return ("%s:%s_nugget"):format(
|
-- 4: ingots_to_nuggets, 5: enable/disable production
|
||||||
ITEM_TYPES[item_type].nugget,
|
local target = {}
|
||||||
item_type
|
target.iron = {}
|
||||||
)
|
target.iron[1] = "0:front"
|
||||||
end,
|
target.iron[2] = "0:top"
|
||||||
ingot = function (item_type)
|
target.iron[3] = "0:back"
|
||||||
return ("%s:%s_ingot"):format(
|
target.iron[4] = "16:front"
|
||||||
ITEM_TYPES[item_type].ingot,
|
target.iron[5] = "17:back"
|
||||||
item_type
|
target.copper = {}
|
||||||
)
|
target.copper[1] = "1:front"
|
||||||
end,
|
target.copper[2] = "1:top"
|
||||||
block = function (item_type)
|
target.copper[3] = "1:back"
|
||||||
return ("%s:%s_block"):format(
|
target.copper[4] = "16:top"
|
||||||
ITEM_TYPES[item_type].block,
|
target.copper[5] = "18:front"
|
||||||
item_type
|
target.zinc = {}
|
||||||
)
|
target.zinc[1] = "2:front"
|
||||||
end,
|
target.zinc[2] = "2:top"
|
||||||
}
|
target.zinc[3] = "2:back"
|
||||||
|
target.zinc[4] = "16:back"
|
||||||
|
target.zinc[5] = "18:top"
|
||||||
|
target.gold = {}
|
||||||
|
target.gold[1] = "3:front"
|
||||||
|
target.gold[2] = "3:top"
|
||||||
|
target.gold[3] = "3:back"
|
||||||
|
target.gold[4] = "17:front"
|
||||||
|
target.gold[5] = "18:back"
|
||||||
|
target.electrum = {}
|
||||||
|
target.electrum[1] = "4:front"
|
||||||
|
target.electrum[2] = "4:top"
|
||||||
|
target.electrum[3] = "4:back"
|
||||||
|
target.electrum[4] = "17:top"
|
||||||
|
|
||||||
-- basic actions
|
-- basic actions
|
||||||
function craft (requester, item_type, conversion_type)
|
function craft (item_type, conversion_type)
|
||||||
-- conversion_type key: 1, nuggets to ingots; 2, ingots to blocks;
|
-- TODO double check this
|
||||||
-- 3, blocks to ingots; 4, ingots to nuggets
|
local relay_num, face = string.match(
|
||||||
local convert = {
|
target[item_type][conversion_type],
|
||||||
getName.nugget,
|
"(%d+):(%l+)"
|
||||||
getName.ingot,
|
)
|
||||||
getName.block,
|
local periph = string.format("redstone_relay_%d", relay_num)
|
||||||
getName.ingot,
|
peripheral.call(periph, "setOutput", face, true)
|
||||||
}
|
os.sleep(0.1)
|
||||||
local name = convert[conversion_type](item_type)
|
peripheral.call(periph, "setOutput", face, false)
|
||||||
local compacting = conversion_type <= 2
|
|
||||||
local count, address
|
|
||||||
if compacting then
|
|
||||||
count = NUGGET_RATIO
|
|
||||||
address = COMPACTING_ADDR
|
|
||||||
else
|
|
||||||
count = BLOCK_RATIO
|
|
||||||
address = UNPACKING_ADDR
|
|
||||||
end
|
|
||||||
requester.setAddress(address)
|
|
||||||
requester.setRequest({ name = name, count = count })
|
|
||||||
requester.request()
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function craft_multiple (requester, item_type, conversion_type, count)
|
function craft_multiple (item_type, conversion_type, count)
|
||||||
if count < 1 then
|
if count < 1 then
|
||||||
return
|
return
|
||||||
else
|
else
|
||||||
craft(requester, item_type, conversion_type)
|
craft(item_type, conversion_type)
|
||||||
os.sleep(1)
|
os.sleep(1)
|
||||||
return craft_multiple(requester, item_type, conversion_type, count - 1)
|
return craft_multiple(item_type, conversion_type, count - 1)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function execute_crafts (requester, item_type, crafts)
|
function execute_crafts (item_type, crafts)
|
||||||
local num_crafts = crafts.nugget
|
local num_crafts = crafts.nuggets
|
||||||
if num_crafts > 0 then
|
if num_crafts > 0 then
|
||||||
print(("Crafting %s nuggets"):format(item_type))
|
print(("Crafting %s nuggets"):format(item_type))
|
||||||
craft_multiple(requester, item_type, 4, num_crafts)
|
craft_multiple(item_type, 4, num_crafts)
|
||||||
elseif num_crafts < 0 then
|
elseif num_crafts < 0 then
|
||||||
print(("Crafting %s ingots"):format(item_type))
|
print(("Crafting %s ingots"):format(item_type))
|
||||||
craft_multiple(requester, item_type, 1, -num_crafts)
|
craft_multiple(item_type, 1, -num_crafts)
|
||||||
end
|
end
|
||||||
num_crafts = crafts.block
|
num_crafts = crafts.blocks
|
||||||
if num_crafts > 0 then
|
if num_crafts > 0 then
|
||||||
print(("Crafting %s blocks"):format(item_type))
|
print(("Crafting %s blocks"):format(item_type))
|
||||||
craft_multiple(requester, item_type, 2, num_crafts)
|
craft_multiple(item_type, 2, num_crafts)
|
||||||
elseif num_crafts < 0 then
|
elseif num_crafts < 0 then
|
||||||
print(("Crafting %s ingots"):format(item_type))
|
print(("Crafting %s ingots"):format(item_type))
|
||||||
craft_multiple(requester, item_type, 3, -num_crafts)
|
craft_multiple(item_type, 3, -num_crafts)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function request_more (ticker, item_type)
|
function set_production (item_type, produce)
|
||||||
ticker.requestFiltered(STORAGE_ADDR, { name = getName.nugget(item_type) })
|
local target_string = target[item_type][5]
|
||||||
|
if target_string == nil then return end
|
||||||
|
local relay_num, face = string.match(
|
||||||
|
target_string,
|
||||||
|
"(%d+):(%l+)"
|
||||||
|
)
|
||||||
|
local periph = string.format("redstone_relay_%d", relay_num)
|
||||||
|
-- ON is disable production
|
||||||
|
if produce then print(("Producing %s"):format(item_type)) end
|
||||||
|
peripheral.call(periph, "setOutput", face, not produce)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- logic
|
-- logic
|
||||||
|
|
||||||
function sum_items (inv_f)
|
function sum_items (inv)
|
||||||
local result = {}
|
local result = {}
|
||||||
for _, item in pairs(inv_f()) do
|
for _, item in pairs(inv.list()) do
|
||||||
if result[item.name] == nil then result[item.name] = 0 end
|
if result[item.name] == nil then result[item.name] = 0 end
|
||||||
result[item.name] = result[item.name] + item.count
|
result[item.name] = result[item.name] + item.count
|
||||||
end
|
end
|
||||||
|
|
@ -143,13 +122,14 @@ function sum_items (inv_f)
|
||||||
end
|
end
|
||||||
|
|
||||||
function dist_to_num (dist)
|
function dist_to_num (dist)
|
||||||
return dist.nugget + (dist.ingot * INGOTS) + (dist.block * BLOCKS)
|
return dist.nuggets + dist.ingots * INGOTS + dist.blocks * BLOCKS
|
||||||
end
|
end
|
||||||
|
|
||||||
function get_dist (item_type, items)
|
function get_dist (item_type, items)
|
||||||
local dist = { nugget = 0, ingot = 0, block = 0 }
|
local dist = { nuggets = 0, ingots = 0, blocks = 0 }
|
||||||
for item, count in pairs(items) do
|
for item, count in pairs(items) do
|
||||||
local type, form = item:match":(%l-)_(%l*)"
|
local type, form = item:match":(%l-)_(%l*)"
|
||||||
|
form = string.format("%ss", form)
|
||||||
if type == item_type and dist[form] ~= nil then
|
if type == item_type and dist[form] ~= nil then
|
||||||
dist[form] = dist[form] + count
|
dist[form] = dist[form] + count
|
||||||
end
|
end
|
||||||
|
|
@ -158,51 +138,52 @@ function get_dist (item_type, items)
|
||||||
end
|
end
|
||||||
|
|
||||||
function decide_dist (num)
|
function decide_dist (num)
|
||||||
local dist = { nugget = 0, ingot = 0, block = 0 }
|
local dist = { nuggets = 0, ingots = 0, blocks = 0 }
|
||||||
if num > MAX_NUM then
|
if num > MAX_NUM then
|
||||||
local excess = num - MAX_NUM
|
local excess = num - MAX_NUM
|
||||||
dist.block = math.floor(excess / BLOCKS)
|
dist.blocks = math.floor(excess / BLOCKS)
|
||||||
num = num - (dist.block * BLOCKS)
|
num = num - (dist.blocks * BLOCKS)
|
||||||
end
|
end
|
||||||
local balance = math.floor(num / 10)
|
local balance = math.floor(num / 10)
|
||||||
dist.nugget, dist.ingot = balance, balance
|
dist.nuggets, dist.ingots = balance, balance
|
||||||
num = num - (balance * 10)
|
num = num - (balance * 10)
|
||||||
dist.nugget = dist.nugget + num
|
dist.nuggets = dist.nuggets + num
|
||||||
return dist
|
return dist
|
||||||
end
|
end
|
||||||
|
|
||||||
function get_diff (dist1, dist2)
|
function get_diff (dist1, dist2)
|
||||||
return {
|
return {
|
||||||
nugget = dist1.nugget - dist2.nugget,
|
nuggets = dist1.nuggets - dist2.nuggets,
|
||||||
ingot = dist1.ingot - dist2.ingot,
|
ingots = dist1.ingots - dist2.ingots,
|
||||||
block = dist1.block - dist2.block
|
blocks = dist1.blocks - dist2.blocks
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
function filter_clamp (diff, current_dist)
|
function filter_clamp (diff, current_dist)
|
||||||
-- reduce diff numbers to what can be crafted using current resources
|
-- reduce diff numbers to what can be crafted using current resources
|
||||||
if diff.block > 0 then
|
if diff.blocks > 0 then
|
||||||
diff.block = math.min(
|
diff.blocks = math.min(
|
||||||
diff.block - (diff.block % BLOCK_RATIO),
|
diff.blocks - (diff.blocks % BLOCK_RATIO),
|
||||||
math.floor(current_dist.ingot / 9)
|
math.floor(current_dist.ingots / 9)
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
if diff.nugget > 0 then
|
if diff.nuggets > 0 then
|
||||||
diff.nugget = math.min(
|
diff.nuggets = math.min(
|
||||||
diff.nugget - (diff.nugget % NUGGET_RATIO),
|
diff.nuggets - (diff.nuggets % NUGGET_RATIO),
|
||||||
current_dist.ingot * 9
|
current_dist.ingots * 9
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
diff.ingot = (-diff.nugget / 9) + (-diff.block * 9)
|
diff.ingots = (-diff.nuggets / 9) + (-diff.blocks * 9)
|
||||||
return diff
|
return diff
|
||||||
end
|
end
|
||||||
|
|
||||||
function diff_to_crafts (diff)
|
function diff_to_crafts (diff)
|
||||||
|
-- TODO double-check this one
|
||||||
-- go from one end e.g. nuggets to the other e.g. blocks, removing
|
-- go from one end e.g. nuggets to the other e.g. blocks, removing
|
||||||
-- from the diff to create crafts until the diff is empty
|
-- from the diff to create crafts until the diff is empty
|
||||||
local crafts = {}
|
local crafts = {}
|
||||||
crafts.nugget = math.floor(diff.nugget / NUGGET_RATIO)
|
crafts.nuggets = math.floor(diff.nuggets / NUGGET_RATIO)
|
||||||
crafts.block = math.floor(diff.block / BLOCK_RATIO)
|
crafts.blocks = math.floor(diff.blocks / BLOCK_RATIO)
|
||||||
return crafts
|
return crafts
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -215,35 +196,24 @@ function print_counts (items)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local main_t = peripheral.wrap(MAIN_TICKER)
|
|
||||||
local request_t = peripheral.wrap(REQUEST_TICKER)
|
|
||||||
local craft_requester = peripheral.wrap(CRAFTING_REQUESTER)
|
|
||||||
craft_requester.setConfiguration("strict")
|
|
||||||
local requesting = {}
|
|
||||||
while true do
|
while true do
|
||||||
-- 1. Sum items in inventory and organize by item type
|
-- 1. Sum items in inventory and organize by item type
|
||||||
local items = sum_items(main_t.stock)
|
local items = sum_items(peripheral.wrap(INVENTORY))
|
||||||
-- print_counts(items)
|
-- print_counts(items)
|
||||||
|
|
||||||
-- 2. either work towards a desired distribution or create more resources
|
-- 2. either work towards a desired distribution or create more resources
|
||||||
for item_type, _ in pairs(ITEM_TYPES) do
|
for _, item_type in ipairs(ITEM_TYPES) do
|
||||||
local current_dist = get_dist(item_type, items)
|
local current_dist = get_dist(item_type, items)
|
||||||
local num = dist_to_num(current_dist)
|
local num = dist_to_num(current_dist)
|
||||||
|
if num > MAX_NUM then set_production(item_type, false) end
|
||||||
if num > MIN_NUM then
|
if num > MIN_NUM then
|
||||||
local desired_dist = decide_dist(num)
|
local desired_dist = decide_dist(num)
|
||||||
local diff = get_diff(desired_dist, current_dist)
|
local diff = get_diff(desired_dist, current_dist)
|
||||||
diff = filter_clamp(diff, current_dist)
|
diff = filter_clamp(diff, current_dist)
|
||||||
local crafts = diff_to_crafts(diff)
|
local crafts = diff_to_crafts(diff)
|
||||||
execute_crafts(craft_requester, item_type, crafts)
|
execute_crafts(item_type, crafts)
|
||||||
else
|
else
|
||||||
requesting[item_type] = true
|
set_production(item_type, true)
|
||||||
end
|
|
||||||
if num > MAX_NUM then
|
|
||||||
-- keep requesting until above MAX_NUM
|
|
||||||
requesting[item_type] = false
|
|
||||||
end
|
|
||||||
if requesting[item_type] then
|
|
||||||
request_more(request_t, item_type)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue