feat: refactor and finish functions
This commit is contained in:
parent
b60bb94e07
commit
a86cfed866
1 changed files with 79 additions and 31 deletions
|
|
@ -21,41 +21,89 @@ local FLOORS = {
|
||||||
|
|
||||||
local SLEEP_T = 5
|
local SLEEP_T = 5
|
||||||
|
|
||||||
local PKG_STACK = {
|
local PKG_QUEUE = {
|
||||||
transfer_all = function (self, src, dest)
|
insert = function (self, floor_i)
|
||||||
-- TODO
|
local is_in = function (item, list)
|
||||||
end,
|
for _, l_item in ipairs(list) do
|
||||||
transfer_matching = function (self, src, dest, match_f)
|
if l_item == item then return true end
|
||||||
-- TODO
|
end
|
||||||
end,
|
return false
|
||||||
get_next_floor = function (self)
|
|
||||||
-- TODO
|
|
||||||
end
|
|
||||||
stack = {},
|
|
||||||
}
|
|
||||||
|
|
||||||
function run (floors, stack)
|
|
||||||
local next_addr = nil
|
|
||||||
for _, floor in pairs(floors) do
|
|
||||||
if floor.detect() then
|
|
||||||
stack:transfer_all(floor.storage, floor.elevator_storage)
|
|
||||||
stack:transfer_matching(
|
|
||||||
floor.elevator_storage, floor.storage, floor.match
|
|
||||||
)
|
|
||||||
next_addr = stack:get_next_floor()
|
|
||||||
end
|
end
|
||||||
end
|
if not is_in(floor_i, self.queue) then
|
||||||
if next_addr ~= nil then
|
table.insert(self.queue, floor_i)
|
||||||
for _, floor in pairs(floors) do
|
end
|
||||||
if floor.match(next_addr) then
|
end,
|
||||||
floor.call()
|
check_floors = function (self, floors)
|
||||||
break
|
for i, floor in pairs(floors) do
|
||||||
|
if #(peripheral.call(floor.storage, "list")) > 0 then
|
||||||
|
self:insert(i)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end,
|
||||||
|
check_cargo = function (self, inv, floors)
|
||||||
|
for slot, item in pairs(peripheral.call(inv, "list")) do
|
||||||
|
local item_detail = peripheral.call(src, "getItemDetail", slot)
|
||||||
|
for i, floor in ipairs(floors) do
|
||||||
|
if item_detail.package ~= nil and
|
||||||
|
floor.match(item_detail.package.getAddress()) then
|
||||||
|
self:insert(i)
|
||||||
|
break
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
transfer_all = function (self, src, dest, floors)
|
||||||
|
-- assumption: this always transfers into the cargo box
|
||||||
|
for slot, item in pairs(peripheral.call(src, "list")) do
|
||||||
|
local item_detail = peripheral.call(src, "getItemDetail", slot)
|
||||||
|
for i, floor in ipairs(floors) do
|
||||||
|
if item_detail.package ~= nil and
|
||||||
|
floor.match(item_detail.package.getAddress()) then
|
||||||
|
self:insert(i)
|
||||||
|
peripheral.call(src, "pushItems", dest, slot)
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end,
|
||||||
|
transfer_matching = function (self, src, dest, match_f)
|
||||||
|
-- assumption: this always transfers out of the cargo box
|
||||||
|
for slot, item in pairs(peripheral.call(src, "list")) do
|
||||||
|
local item_detail = peripheral.call(src, "getItemDetail", slot)
|
||||||
|
if item_detail.package ~= nil and
|
||||||
|
match_f(item_detail.package.getAddress()) then
|
||||||
|
peripheral.call(src, "pushItems", dest, slot)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end,
|
||||||
|
get_next_floor = function (self)
|
||||||
|
return table.remove(self.queue, 1)
|
||||||
|
end
|
||||||
|
queue = {},
|
||||||
|
}
|
||||||
|
|
||||||
|
function run (floors, queue, first_run)
|
||||||
|
local next_floor = nil
|
||||||
|
queue:check_floors(floors)
|
||||||
|
for i, floor in pairs(floors) do
|
||||||
|
if floor.detect() then
|
||||||
|
if first_run then
|
||||||
|
queue:check_cargo(floor.elevator_storage, floors)
|
||||||
|
end
|
||||||
|
queue:transfer_all(floor.storage, floor.elevator_storage, floors)
|
||||||
|
queue:transfer_matching(
|
||||||
|
floor.elevator_storage, floor.storage, floor.match
|
||||||
|
)
|
||||||
|
next_floor = queue:get_next_floor()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
if next_floor ~= nil then
|
||||||
|
floors[next_floor].call()
|
||||||
|
end
|
||||||
|
os.sleep(SLEEP_T)
|
||||||
|
return run(floors, queue, false)
|
||||||
end
|
end
|
||||||
|
|
||||||
while true do
|
if arg ~= nil and arg[1] == "run" then
|
||||||
run(FLOORS)
|
run(FLOORS, queue, true)
|
||||||
os.sleep(SLEEP_T)
|
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue