From b60bb94e0716b76a111c21ff261218051c381666 Mon Sep 17 00:00:00 2001 From: Emerson Rosen-Jones Date: Thu, 25 Dec 2025 15:33:14 -0500 Subject: [PATCH 1/8] feat(cargo-elevator): implement high-level functions Still need the smaller details. --- cargo-elevator.lua | 61 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 cargo-elevator.lua diff --git a/cargo-elevator.lua b/cargo-elevator.lua new file mode 100644 index 0000000..1717d56 --- /dev/null +++ b/cargo-elevator.lua @@ -0,0 +1,61 @@ +-- program to manage a cargo elevator +-- yes, i am aware i could just use the computer to transfer packages at this +-- point, but where's the fun in that? + +local FLOORS = { + { + match = function (input) + local first_char = string.sub(input, 1, 1) + return first_char == "A" or first_char == "W" + end, + detect = function () + -- TODO + end, + call = function () + -- TODO + end + storage = "", + elevator_storage = "", + } +} + +local SLEEP_T = 5 + +local PKG_STACK = { + transfer_all = function (self, src, dest) + -- TODO + end, + transfer_matching = function (self, src, dest, match_f) + -- TODO + end, + 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 + if next_addr ~= nil then + for _, floor in pairs(floors) do + if floor.match(next_addr) then + floor.call() + break + end + end + end +end + +while true do + run(FLOORS) + os.sleep(SLEEP_T) +end From a86cfed866b48a27377e4536ee8f0f1f462dfebb Mon Sep 17 00:00:00 2001 From: Emerson Rosen-Jones Date: Fri, 26 Dec 2025 12:54:27 -0500 Subject: [PATCH 2/8] feat: refactor and finish functions --- cargo-elevator.lua | 110 ++++++++++++++++++++++++++++++++------------- 1 file changed, 79 insertions(+), 31 deletions(-) diff --git a/cargo-elevator.lua b/cargo-elevator.lua index 1717d56..3a850ea 100644 --- a/cargo-elevator.lua +++ b/cargo-elevator.lua @@ -21,41 +21,89 @@ local FLOORS = { local SLEEP_T = 5 -local PKG_STACK = { - transfer_all = function (self, src, dest) - -- TODO - end, - transfer_matching = function (self, src, dest, match_f) - -- TODO - end, - 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() +local PKG_QUEUE = { + insert = function (self, floor_i) + local is_in = function (item, list) + for _, l_item in ipairs(list) do + if l_item == item then return true end + end + return false end - end - if next_addr ~= nil then - for _, floor in pairs(floors) do - if floor.match(next_addr) then - floor.call() - break + if not is_in(floor_i, self.queue) then + table.insert(self.queue, floor_i) + end + end, + check_floors = function (self, floors) + 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 + 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 -while true do - run(FLOORS) - os.sleep(SLEEP_T) +if arg ~= nil and arg[1] == "run" then + run(FLOORS, queue, true) end From 5acc3ab0f7045767dfb89eec93e9ad0f35fa4785 Mon Sep 17 00:00:00 2001 From: Emerson Rosen-Jones Date: Fri, 26 Dec 2025 12:54:45 -0500 Subject: [PATCH 3/8] feat: add default configuration --- cargo-elevator.lua | 66 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 61 insertions(+), 5 deletions(-) diff --git a/cargo-elevator.lua b/cargo-elevator.lua index 3a850ea..4e674c7 100644 --- a/cargo-elevator.lua +++ b/cargo-elevator.lua @@ -9,14 +9,70 @@ local FLOORS = { return first_char == "A" or first_char == "W" end, detect = function () - -- TODO + return redstone.getOutput("top") end, call = function () - -- TODO + redstone.setOutput("top", true) + os.sleep(0.5) + redstone.setOutput("top", false) end - storage = "", - elevator_storage = "", - } + storage = "create:item_vault_17", + elevator_storage = "bottom", + }, + { + match = function (input) + local first_char = string.sub(input, 1, 1) + return first_char == "F" + end, + detect = function () + local relay = "redstone_relay_21" + peripheral.call(relay, "getOutput", "top") + end, + call = function () + local relay = "redstone_relay_21" + peripheral.call(relay, "setOutput", "top", true) + os.sleep(0.5) + peripheral.call(relay, "setOutput", "top", false) + end + storage = "create:item_vault_18", + elevator_storage = "create:portable_storage_interface_8", + }, + { + match = function (input) + local first_char = string.sub(input, 1, 1) + return first_char == "L" + end, + detect = function () + local relay = "redstone_relay_22" + peripheral.call(relay, "getOutput", "top") + end, + call = function () + local relay = "redstone_relay_22" + peripheral.call(relay, "setOutput", "top", true) + os.sleep(0.5) + peripheral.call(relay, "setOutput", "top", false) + end + storage = "create:item_vault_19", + elevator_storage = "create:portable_storage_interface_9", + }, + { + match = function (input) + local first_char = string.sub(input, 1, 1) + return first_char == "E" + end, + detect = function () + local relay = "redstone_relay_23" + peripheral.call(relay, "getOutput", "top") + end, + call = function () + local relay = "redstone_relay_23" + peripheral.call(relay, "setOutput", "top", true) + os.sleep(0.5) + peripheral.call(relay, "setOutput", "top", false) + end + storage = "create:item_vault_20", + elevator_storage = "create:portable_storage_interface_10", + }, } local SLEEP_T = 5 From f9302e2b3d3cd89ea2e7b596f04819474660ecdb Mon Sep 17 00:00:00 2001 From: Emerson Rosen-Jones Date: Fri, 26 Dec 2025 13:39:27 -0500 Subject: [PATCH 4/8] fix: remove several syntax errors --- cargo-elevator.lua | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/cargo-elevator.lua b/cargo-elevator.lua index 4e674c7..58d8002 100644 --- a/cargo-elevator.lua +++ b/cargo-elevator.lua @@ -15,7 +15,7 @@ local FLOORS = { redstone.setOutput("top", true) os.sleep(0.5) redstone.setOutput("top", false) - end + end, storage = "create:item_vault_17", elevator_storage = "bottom", }, @@ -33,7 +33,7 @@ local FLOORS = { peripheral.call(relay, "setOutput", "top", true) os.sleep(0.5) peripheral.call(relay, "setOutput", "top", false) - end + end, storage = "create:item_vault_18", elevator_storage = "create:portable_storage_interface_8", }, @@ -51,7 +51,7 @@ local FLOORS = { peripheral.call(relay, "setOutput", "top", true) os.sleep(0.5) peripheral.call(relay, "setOutput", "top", false) - end + end, storage = "create:item_vault_19", elevator_storage = "create:portable_storage_interface_9", }, @@ -69,7 +69,7 @@ local FLOORS = { peripheral.call(relay, "setOutput", "top", true) os.sleep(0.5) peripheral.call(relay, "setOutput", "top", false) - end + end, storage = "create:item_vault_20", elevator_storage = "create:portable_storage_interface_10", }, @@ -107,7 +107,7 @@ local PKG_QUEUE = { 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 @@ -134,7 +134,7 @@ local PKG_QUEUE = { end, get_next_floor = function (self) return table.remove(self.queue, 1) - end + end, queue = {}, } @@ -161,5 +161,5 @@ function run (floors, queue, first_run) end if arg ~= nil and arg[1] == "run" then - run(FLOORS, queue, true) + run(FLOORS, PKG_QUEUE, true) end From d9f85131005e68339ccd6f333ae6277ec00ae2b0 Mon Sep 17 00:00:00 2001 From: Emerson Rosen-Jones Date: Fri, 26 Dec 2025 13:43:14 -0500 Subject: [PATCH 5/8] fix: corrects floor detection --- cargo-elevator.lua | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cargo-elevator.lua b/cargo-elevator.lua index 58d8002..fba2027 100644 --- a/cargo-elevator.lua +++ b/cargo-elevator.lua @@ -9,7 +9,7 @@ local FLOORS = { return first_char == "A" or first_char == "W" end, detect = function () - return redstone.getOutput("top") + return redstone.getInput("top") end, call = function () redstone.setOutput("top", true) @@ -26,7 +26,7 @@ local FLOORS = { end, detect = function () local relay = "redstone_relay_21" - peripheral.call(relay, "getOutput", "top") + peripheral.call(relay, "getInput", "top") end, call = function () local relay = "redstone_relay_21" @@ -44,7 +44,7 @@ local FLOORS = { end, detect = function () local relay = "redstone_relay_22" - peripheral.call(relay, "getOutput", "top") + peripheral.call(relay, "getInput", "top") end, call = function () local relay = "redstone_relay_22" @@ -62,7 +62,7 @@ local FLOORS = { end, detect = function () local relay = "redstone_relay_23" - peripheral.call(relay, "getOutput", "top") + peripheral.call(relay, "getInput", "top") end, call = function () local relay = "redstone_relay_23" From dac0874232be9cd60462b67d11abe4f40b0d8492 Mon Sep 17 00:00:00 2001 From: Emerson Rosen-Jones Date: Fri, 26 Dec 2025 13:46:47 -0500 Subject: [PATCH 6/8] fix: remove error in default config --- cargo-elevator.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cargo-elevator.lua b/cargo-elevator.lua index fba2027..376a305 100644 --- a/cargo-elevator.lua +++ b/cargo-elevator.lua @@ -17,7 +17,7 @@ local FLOORS = { redstone.setOutput("top", false) end, storage = "create:item_vault_17", - elevator_storage = "bottom", + elevator_storage = "create:portable_storage_interface_11", }, { match = function (input) From 3ce4f9579ddb085f5f71556fe633949333212c4a Mon Sep 17 00:00:00 2001 From: Emerson Rosen-Jones Date: Fri, 26 Dec 2025 14:06:48 -0500 Subject: [PATCH 7/8] fix: corrects floor detection part 2 --- cargo-elevator.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cargo-elevator.lua b/cargo-elevator.lua index 376a305..66749bb 100644 --- a/cargo-elevator.lua +++ b/cargo-elevator.lua @@ -26,7 +26,7 @@ local FLOORS = { end, detect = function () local relay = "redstone_relay_21" - peripheral.call(relay, "getInput", "top") + return peripheral.call(relay, "getInput", "top") end, call = function () local relay = "redstone_relay_21" @@ -44,7 +44,7 @@ local FLOORS = { end, detect = function () local relay = "redstone_relay_22" - peripheral.call(relay, "getInput", "top") + return peripheral.call(relay, "getInput", "top") end, call = function () local relay = "redstone_relay_22" @@ -62,7 +62,7 @@ local FLOORS = { end, detect = function () local relay = "redstone_relay_23" - peripheral.call(relay, "getInput", "top") + return peripheral.call(relay, "getInput", "top") end, call = function () local relay = "redstone_relay_23" From 6672c75d1a50784273d77f9cd78a0bcb8c1adf36 Mon Sep 17 00:00:00 2001 From: Emerson Rosen-Jones Date: Fri, 26 Dec 2025 14:06:55 -0500 Subject: [PATCH 8/8] fix: removes syntax error --- cargo-elevator.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cargo-elevator.lua b/cargo-elevator.lua index 66749bb..6aadb4b 100644 --- a/cargo-elevator.lua +++ b/cargo-elevator.lua @@ -98,7 +98,7 @@ local PKG_QUEUE = { 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) + local item_detail = peripheral.call(inv, "getItemDetail", slot) for i, floor in ipairs(floors) do if item_detail.package ~= nil and floor.match(item_detail.package.getAddress()) then