morington

RedstoneRelay

Apr 26th, 2026 (edited)
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.16 KB | None | 0 0
  1. term.clear()
  2. term.setCursorPos(1, 1)
  3. print("The control program has been launched...")
  4.  
  5. local mon = peripheral.wrap("monitor_4")
  6. local relay = peripheral.wrap("redstone_relay_1")
  7.  
  8. if not mon or not relay then
  9.     error("Error: Check names monitor_4 and redstone_relay_1!")
  10. end
  11.  
  12. mon.setBackgroundColor(colors.black)
  13. mon.clear()
  14.  
  15. local RELAY_SIDE = "top"
  16. relay.setAnalogOutput("top", 15)
  17.  
  18. mon.setTextScale(1)
  19.  
  20. local currentPower = 15
  21.  
  22. local ENGINES_COUNT = 1
  23. local SAILS_COUNT = 8
  24.  
  25. local BASE_SAILS_COUNT = 8
  26. local MAX_AIRFLOW_PER_ENGINE = 31.68
  27. local MAX_LIFT_PER_ENGINE = 138.23
  28. local MAX_EFFECTIVE_REDUCTION = 14
  29.  
  30. local btnMinus = {x1 = 2, y1 = 2, x2 = 5, y2 = 4}
  31. local btnPlus = {x1 = 14, y1 = 2, x2 = 17, y2 = 4}
  32.  
  33. local function calculateStats(power)
  34.     local effectivePower = math.max(power, 1)
  35.     local airflowRatio = SAILS_COUNT / BASE_SAILS_COUNT
  36.  
  37.     local airflowPerEngine = (15 - effectivePower)
  38.         * (MAX_AIRFLOW_PER_ENGINE / MAX_EFFECTIVE_REDUCTION)
  39.         * airflowRatio
  40.  
  41.     local liftPerEngine = airflowPerEngine
  42.         * (MAX_LIFT_PER_ENGINE / MAX_AIRFLOW_PER_ENGINE)
  43.  
  44.     return {
  45.         airflow = airflowPerEngine,
  46.         lift = liftPerEngine * ENGINES_COUNT,
  47.     }
  48. end
  49.  
  50. local function drawButton(x1, y1, x2, y2, color, text)
  51.     mon.setBackgroundColor(color)
  52.     mon.setTextColor(colors.white)
  53.  
  54.     for y = y1, y2 do
  55.         mon.setCursorPos(x1, y)
  56.         mon.write(string.rep(" ", x2 - x1 + 1))
  57.     end
  58.  
  59.     mon.setCursorPos(
  60.         x1 + math.floor((x2 - x1) / 2),
  61.         y1 + math.floor((y2 - y1) / 2)
  62.     )
  63.     mon.write(text)
  64. end
  65.  
  66. local function drawUI()
  67.     local stats = calculateStats(currentPower)
  68.  
  69.     mon.setBackgroundColor(colors.black)
  70.     mon.clear()
  71.  
  72.     drawButton(btnMinus.x1, btnMinus.y1, btnMinus.x2, btnMinus.y2, colors.red, "-")
  73.     drawButton(btnPlus.x1, btnPlus.y1, btnPlus.x2, btnPlus.y2, colors.green, "+")
  74.  
  75.     mon.setBackgroundColor(colors.black)
  76.  
  77.     mon.setTextColor(colors.yellow)
  78.     mon.setCursorPos(8, 3)
  79.     mon.write(string.format("PW: %02d", currentPower))
  80.  
  81.     mon.setTextColor(colors.cyan)
  82.     mon.setCursorPos(2, 6)
  83.     mon.write(string.format("Engines: %d", ENGINES_COUNT))
  84.  
  85.     mon.setCursorPos(2, 7)
  86.     mon.write(string.format("Sails: %d", SAILS_COUNT))
  87.  
  88.     mon.setCursorPos(2, 9)
  89.     mon.write(string.format("Air: %.2f m/s", stats.airflow))
  90.  
  91.     mon.setCursorPos(2, 10)
  92.     mon.write(string.format("Lift: %.2f kg", stats.lift))
  93. end
  94.  
  95. local function updateSignal()
  96.     relay.setAnalogOutput(RELAY_SIDE, currentPower)
  97.     drawUI()
  98. end
  99.  
  100. drawUI()
  101. updateSignal()
  102.  
  103. while true do
  104.     local _, side, x, y = os.pullEvent("monitor_touch")
  105.  
  106.     if side == "monitor_4" then
  107.         if x >= btnMinus.x1 and x <= btnMinus.x2 and y >= btnMinus.y1 and y <= btnMinus.y2 then
  108.             if currentPower > 0 then
  109.                 currentPower = currentPower - 1
  110.                 updateSignal()
  111.             end
  112.         elseif x >= btnPlus.x1 and x <= btnPlus.x2 and y >= btnPlus.y1 and y <= btnPlus.y2 then
  113.             if currentPower < 15 then
  114.                 currentPower = currentPower + 1
  115.                 updateSignal()
  116.             end
  117.         end
  118.     end
  119. end
Advertisement
Add Comment
Please, Sign In to add comment