Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- term.clear()
- term.setCursorPos(1, 1)
- print("The control program has been launched...")
- local mon = peripheral.wrap("monitor_4")
- local relay = peripheral.wrap("redstone_relay_1")
- if not mon or not relay then
- error("Error: Check names monitor_4 and redstone_relay_1!")
- end
- mon.setBackgroundColor(colors.black)
- mon.clear()
- local RELAY_SIDE = "top"
- relay.setAnalogOutput("top", 15)
- mon.setTextScale(1)
- local currentPower = 15
- local ENGINES_COUNT = 1
- local SAILS_COUNT = 8
- local BASE_SAILS_COUNT = 8
- local MAX_AIRFLOW_PER_ENGINE = 31.68
- local MAX_LIFT_PER_ENGINE = 138.23
- local MAX_EFFECTIVE_REDUCTION = 14
- local btnMinus = {x1 = 2, y1 = 2, x2 = 5, y2 = 4}
- local btnPlus = {x1 = 14, y1 = 2, x2 = 17, y2 = 4}
- local function calculateStats(power)
- local effectivePower = math.max(power, 1)
- local airflowRatio = SAILS_COUNT / BASE_SAILS_COUNT
- local airflowPerEngine = (15 - effectivePower)
- * (MAX_AIRFLOW_PER_ENGINE / MAX_EFFECTIVE_REDUCTION)
- * airflowRatio
- local liftPerEngine = airflowPerEngine
- * (MAX_LIFT_PER_ENGINE / MAX_AIRFLOW_PER_ENGINE)
- return {
- airflow = airflowPerEngine,
- lift = liftPerEngine * ENGINES_COUNT,
- }
- end
- local function drawButton(x1, y1, x2, y2, color, text)
- mon.setBackgroundColor(color)
- mon.setTextColor(colors.white)
- for y = y1, y2 do
- mon.setCursorPos(x1, y)
- mon.write(string.rep(" ", x2 - x1 + 1))
- end
- mon.setCursorPos(
- x1 + math.floor((x2 - x1) / 2),
- y1 + math.floor((y2 - y1) / 2)
- )
- mon.write(text)
- end
- local function drawUI()
- local stats = calculateStats(currentPower)
- mon.setBackgroundColor(colors.black)
- mon.clear()
- drawButton(btnMinus.x1, btnMinus.y1, btnMinus.x2, btnMinus.y2, colors.red, "-")
- drawButton(btnPlus.x1, btnPlus.y1, btnPlus.x2, btnPlus.y2, colors.green, "+")
- mon.setBackgroundColor(colors.black)
- mon.setTextColor(colors.yellow)
- mon.setCursorPos(8, 3)
- mon.write(string.format("PW: %02d", currentPower))
- mon.setTextColor(colors.cyan)
- mon.setCursorPos(2, 6)
- mon.write(string.format("Engines: %d", ENGINES_COUNT))
- mon.setCursorPos(2, 7)
- mon.write(string.format("Sails: %d", SAILS_COUNT))
- mon.setCursorPos(2, 9)
- mon.write(string.format("Air: %.2f m/s", stats.airflow))
- mon.setCursorPos(2, 10)
- mon.write(string.format("Lift: %.2f kg", stats.lift))
- end
- local function updateSignal()
- relay.setAnalogOutput(RELAY_SIDE, currentPower)
- drawUI()
- end
- drawUI()
- updateSignal()
- while true do
- local _, side, x, y = os.pullEvent("monitor_touch")
- if side == "monitor_4" then
- if x >= btnMinus.x1 and x <= btnMinus.x2 and y >= btnMinus.y1 and y <= btnMinus.y2 then
- if currentPower > 0 then
- currentPower = currentPower - 1
- updateSignal()
- end
- elseif x >= btnPlus.x1 and x <= btnPlus.x2 and y >= btnPlus.y1 and y <= btnPlus.y2 then
- if currentPower < 15 then
- currentPower = currentPower + 1
- updateSignal()
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment