Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local monitor = peripheral.find("monitor", function(name) return name == "monitor_4" end)
- local maxRPM = 64
- local CreateTarget = "create_target_4"
- local EngineNumber = 6
- local spinPos = {}
- local buttonPos = {}
- local maxW, maxH = monitor.getSize()
- local EnginesDisplayed = math.floor(maxW / 18)
- local firstPos = math.floor((maxW / EnginesDisplayed) - 18)
- rednet.open("left")
- print("Your monitor can display " .. EnginesDisplayed .. " engines in a row")
- for i = 1, EngineNumber do
- buttonPos[i] = {}
- for j = 1, 4 do
- buttonPos[i][j] = 0
- end
- end
- local function TextColor(rpm)
- if rpm == 0 then
- return "gray"
- elseif rpm > 0 and rpm < maxRPM then
- return "orange"
- else
- return "green"
- end
- end
- local function EngineStatus(rpm)
- if rpm == 0 then
- return "OFF"
- else
- return "ON"
- end
- end
- function isSpinning(rpm)
- if rpm == 0 then
- return 0
- else
- return 1
- end
- end
- function lineBreak(line)
- local stat = peripheral.call(CreateTarget, "getLine", line)
- local number = tonumber(stat:match("^%d+"))
- return number
- end
- local function fillArrays(x, z, pos)
- buttonPos[pos][z + 0] = x
- buttonPos[pos][z + 1] = x + 3
- end
- local function Row(pos)
- if pos % EnginesDisplayed == 0 then
- return(math.floor(((pos - 1) / EnginesDisplayed) + 1))
- else
- return(math.floor((pos / EnginesDisplayed) + 1))
- end
- end
- local function DrawStill(rpm, pos)
- local y = 4
- local row = Row(pos)
- local x
- if pos <= EnginesDisplayed then
- x = (pos + (math.floor(maxW / EnginesDisplayed) * pos) - 18)
- else
- x = (pos - (EnginesDisplayed * (row - 1))) + (math.floor(maxW / EnginesDisplayed) * pos) - 18
- y = y + (5 * (row - 1))
- end
- monitor.setTextColor(colors[TextColor(rpm)])
- monitor.setCursorPos(x, y)
- monitor.write(" ___")
- y = y + 1
- monitor.setCursorPos(x, y)
- monitor.write("/ I \\ ")
- y = y + 1
- monitor.setCursorPos(x, y)
- monitor.write("|-O-|")
- y = y + 1
- monitor.setCursorPos(x, y)
- monitor.write("\\_I_/")
- x = x + 6
- y = y - 2
- monitor.setTextColor(colors.white)
- monitor.setCursorPos(x, y)
- monitor.write("Engine #" .. pos)
- y = y + 1
- monitor.setCursorPos(x, y)
- monitor.setTextColor(colors.yellow)
- monitor.write("Status: " .. EngineStatus(rpm))
- y = y + 1
- x = x + 2
- monitor.setCursorPos(x, y)
- monitor.setTextColor(colors.green)
- monitor.write("ON")
- fillArrays(x, "1", pos)
- x = x + 5
- monitor.setCursorPos(x, y)
- monitor.setTextColor(colors.red)
- monitor.write("OFF")
- fillArrays(x, "3", pos)
- spinPos[pos] = 0
- end
- local function DrawMoving(rpm, pos)
- local y = 4
- local row = Row(pos)
- local x
- if pos <= EnginesDisplayed then
- x = (pos + (math.floor(maxW / EnginesDisplayed) * pos) - 18)
- else
- x = (pos - (EnginesDisplayed * (row - 1))) + (math.floor(maxW / EnginesDisplayed) * pos) - 18
- y = y + (5 * (row - 1))
- end
- monitor.setTextColor(colors[TextColor(rpm)])
- monitor.setCursorPos(x, y)
- monitor.write(" ___")
- y = y + 1
- monitor.setCursorPos(x, y)
- monitor.write("/\\ /\\ ")
- y = y + 1
- monitor.setCursorPos(x, y)
- monitor.write("| O |")
- y = y + 1
- monitor.setCursorPos(x, y)
- monitor.write("\\/_\\/")
- x = x + 6
- y = y - 2
- monitor.setTextColor(colors.white)
- monitor.setCursorPos(x, y)
- monitor.write("Engine #" .. pos)
- y = y + 1
- monitor.setCursorPos(x, y)
- monitor.setTextColor(colors.yellow)
- monitor.write("Status: " .. EngineStatus(rpm))
- y = y + 1
- x = x + 2
- monitor.setCursorPos(x, y)
- monitor.setTextColor(colors.green)
- monitor.write("ON")
- fillArrays(x, "1", pos)
- x = x + 5
- monitor.setCursorPos(x, y)
- monitor.setTextColor(colors.red)
- monitor.write("OFF")
- fillArrays(x, "3", pos)
- spinPos[pos] = 1
- end
- arrayCount = 1
- while arrayCount <= EngineNumber do
- spinPos[arrayCount] = 0
- arrayCount = arrayCount + 1
- sleep(0.05)
- end
- monitor.clear()
- function Screen()
- while true do
- local loop = 1
- while loop <= EngineNumber do
- if isSpinning(lineBreak(loop)) == 1 and spinPos[loop] == 0 then
- DrawMoving(lineBreak(loop), loop)
- else
- DrawStill(lineBreak(loop), loop)
- end
- loop = loop + 1
- end
- loop = 1
- coroutine.yield()
- end
- end
- function handleTouch(x, y)
- local i = 1
- local check_y = 7
- if x and y then
- for loop = 1, EngineNumber do
- if i == 1 or i == 2 then
- check_y = 7
- elseif i == 3 or i == 4 then
- check_y = 7 + 5
- elseif i == 5 or i == 6 then
- check_y = 7 + 10
- end
- if y == check_y and x >= buttonPos[i][1] and x <= buttonPos[i][2] then
- rednet.broadcast(i, "ON")
- print("Clicked ON ... Engine ", i)
- elseif y == check_y and x >= buttonPos[i][3] and x <= buttonPos[i][4] then
- rednet.broadcast(i, "OFF")
- print("Clicked OFF ... Engine ", i)
- end
- i = i + 1
- end
- end
- end
- local function wait(time)
- local timer = os.startTimer(time)
- while true do
- local event = {os.pullEvent()}
- if event[1] == "timer" and event[2] == timer then
- break
- elseif event[1] == "monitor_touch" then
- handleTouch(event[3], event[4])
- elseif event[1] == "modem_message" then
- local mess = event[5]
- print("Got modem message " .. mess)
- if mess == "shutdown_monitor" then
- shutdownMonitor()
- break
- end
- end
- end
- end
- screenCoroutine = coroutine.create(Screen)
- local run = 1
- while run == 1 do
- monitor.setCursorPos(14, 2)
- monitor.setTextColor(colors.white)
- monitor.write("ENGINE CONTROL")
- local success, screenResult = coroutine.resume(screenCoroutine)
- if not success then
- print("Error in screenCoroutine: " .. tostring(screenResult))
- break
- end
- wait(1)
- sleep(0.25)
- monitor.clear()
- end
Advertisement
Add Comment
Please, Sign In to add comment