diff --git a/elevator-controller.lua b/elevator/elevator-controller.lua similarity index 89% rename from elevator-controller.lua rename to elevator/elevator-controller.lua index 0a96d11..337b6ef 100644 --- a/elevator-controller.lua +++ b/elevator/elevator-controller.lua @@ -5,41 +5,12 @@ A script for an all-in-one elevator with floor selection, sounds, doors, and more. ]] -local SYSTEM_NAME = "Test Elevator" - --- Floors, in order from bottom to top. -local FLOORS = { - { - label = "-1", - name = "Basement", - speaker = "speaker_2", - doorRedstone = "redstoneIntegrator_17", - contactRedstone = "redstoneIntegrator_18", - monitor = "monitor_4", - callMonitor = "monitor_5", - height = 25 - }, - { - label = "0", - name = "Ground Floor", - speaker = "speaker_1", - doorRedstone = "redstoneIntegrator_7", - contactRedstone = "redstoneIntegrator_6", - monitor = "monitor_1", - callMonitor = "monitor_3", - height = 58 - }, - { - label = "1", - name = "Mechanical Room", - speaker = "speaker_0", - doorRedstone = "redstoneIntegrator_4", - contactRedstone = "redstoneIntegrator_5", - monitor = "monitor_0", - callMonitor = "monitor_2", - height = 68 - } -} +-- Load floors from elevator settings. +local FLOORS = {} +local floorsFile = io.open("floors.tbl", "r") or error("Missing floors.tbl") +FLOORS = textutils.unserialize(floorsFile:read("a")) +floorsFile:close() +table.sort(FLOORS, function(fA, fB) return fA.height < fB.height end) local FLOORS_BY_LABEL = {} for _, floor in pairs(FLOORS) do @@ -52,13 +23,18 @@ for _, floor in pairs(FLOORS) do end table.sort(FLOOR_LABELS_ORDERED, function(lblA, lblB) return FLOORS_BY_LABEL[lblA].height < FLOORS_BY_LABEL[lblB].height end) -local CONTROL_BASE_RPM = 16 -local CONTROL_MAX_RPM = 256 -local CONTROL_ANALOG_LEVEL_PER_RPM = 2 +local settingsFile = io.open("settings.tbl", "r") or error("Missing settings.tbl") +local settings = textutils.unserialize(settingsFile:read("a")) -local CONTROL_DIRECTION_UP = true -local CONTROL_DIRECTION_DOWN = false -local CONTROL_REDSTONE = "redstoneIntegrator_13" +local SYSTEM_NAME = settings.systemName + +local CONTROL_BASE_RPM = settings.control.baseRpm +local CONTROL_MAX_RPM = settings.control.maxRpm +local CONTROL_ANALOG_LEVEL_PER_RPM = settings.control.analogLevelPerRpm + +local CONTROL_DIRECTION_UP = settings.control.directionUp +local CONTROL_DIRECTION_DOWN = settings.control.directionDown +local CONTROL_REDSTONE = settings.control.redstone local CURRENT_STATE = { rpm = nil, @@ -66,11 +42,11 @@ local CURRENT_STATE = { } local function openDoor(floor) - peripheral.call(floor.doorRedstone, "setOutput", "back", true) + peripheral.call(floor.redstone, "setOutput", "left", true) end local function closeDoor(floor) - peripheral.call(floor.doorRedstone, "setOutput", "back", false) + peripheral.call(floor.redstone, "setOutput", "left", false) end local function playChime(floor) @@ -133,7 +109,7 @@ local function setSpeed(rpm) end local function isFloorContactActive(floor) - return peripheral.call(floor.contactRedstone, "getInput", "back") + return peripheral.call(floor.redstone, "getInput", "back") end -- Determines the label of the floor we're currently on. @@ -141,7 +117,7 @@ end -- If that fails, the elevator is at an unknown position, so we move it as soon as possible to top. local function determineCurrentFloorLabel() for _, floor in pairs(FLOORS) do - local status = peripheral.call(floor.contactRedstone, "getInput", "back") + local status = isFloorContactActive(floor) if status then return floor.label end end -- No floor found. Move the elevator to the top. diff --git a/elevator/floors.lua b/elevator/floors.lua new file mode 100644 index 0000000..52657f8 --- /dev/null +++ b/elevator/floors.lua @@ -0,0 +1,65 @@ +{ + { + label = "1", + name = "First Floor", + speaker = "speaker_0", + redstone = "redstoneIntegrator_4", + monitor = "monitor_1", + callMonitor = "monitor_0", + height = 126 + }, + { + label = "2", + name = "Second Floor", + speaker = "speaker_1", + redstone = "redstoneIntegrator_6", + monitor = "monitor_3", + callMonitor = "monitor_2", + height = 140 + }, + { + label = "3", + name = "Third Floor", + speaker = "speaker_2", + redstone = "redstoneIntegrator_7", + monitor = "monitor_5", + callMonitor = "monitor_4", + height = 147 + }, + { + label = "4", + name = "Fourth Floor", + speaker = "speaker_3", + redstone = "redstoneIntegrator_8", + monitor = "monitor_6", + callMonitor = "monitor_7", + height = 154 + }, + { + label = "5", + name = "Fifth Floor", + speaker = "speaker_4", + redstone = "redstoneIntegrator_9", + monitor = "monitor_8", + callMonitor = "monitor_9", + height = 161 + }, + { + label = "6", + name = "Sixth Floor", + speaker = "speaker_5", + redstone = "redstoneIntegrator_10", + monitor = "monitor_10", + callMonitor = "monitor_11", + height = 168 + }, + { + label = "7", + name = "Seventh Floor", + speaker = "speaker_6", + redstone = "redstoneIntegrator_11", + monitor = "monitor_12", + callMonitor = "monitor_13", + height = 175 + } +} \ No newline at end of file diff --git a/elevator/settings.lua b/elevator/settings.lua new file mode 100644 index 0000000..ac0a9f7 --- /dev/null +++ b/elevator/settings.lua @@ -0,0 +1,11 @@ +{ + systemName = "HandieVale Tower", + control = { + baseRpm = 16, + maxRpm = 256, + analogLevelPerRpm = 2, + directionUp = true, + directionDown = false, + redstone = "redstoneIntegrator_5" + } +} \ No newline at end of file