Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local monitor = peripheral.find("monitor", function(name) return name == "monitor_0" end) -- name of the monitor connected to the network
- local maxRPM = 64 -- rpm that engines run at full power (for displaying green color)
- local CreateTarget = "create_target_0" -- name of the CC:C Bridge target block on the network
- local EngineNumber = 1 -- number of total engines connected to the target block
- local spinPos = {} -- table for all engines to log what sprite is rendered
- local buttonPos = {} -- table for button coords for all engines
- local maxW, maxH = monitor.getSize() -- gets screen dimensions
- local EnginesDisplayed = math.floor(maxW/18) -- number of engines that can fit on your monitor
- local firstPos = math.floor((maxW/EnginesDisplayed)-18) -- position of cursor for first engine
- local padding -- padding for centering
- local text = "ENGINE CONTROL" -- text that's on top of the engine monitor
- rednet.open("left") -- opens the side of the PC that has the wireless modem installed
- print("your monitor can display "..EnginesDisplayed.." engines in a row")
- for i = 1, EngineNumber do -- sets an array table for every engine to store button coords
- buttonPos[i] = {}
- for j = 1, 4 do -- 4 times for coordinates x1 x2 x3 x4
- buttonPos[i][j] = 0
- end
- end
- local function TextColor(rpm) -- decides the color for the engine sprite
- if rpm == 0 then
- return "gray" -- gray color if engine is off
- elseif rpm > 0 and rpm < maxRPM then
- return "orange" -- orange color if engine is on but not at max speed
- else
- return "green" -- green color if engine is on and at max speed
- end
- end
- local function EngineStatus(rpm) -- returns the engine status in text
- if rpm == 0 then
- return "OFF"
- else
- return "ON"
- end
- end
- function isSpinning (rpm) -- rdetermines if the engine is spinning
- if rpm == 0 then
- return 0
- else
- return 1
- end
- end
- function lineBreak (line) -- returns the RPM for a motor
- local stat = peripheral.call(CreateTarget, "getLine", line) -- separates the different lines in the target block
- local number = tonumber(stat:match("^%d+")) -- removes "RPM" and only keeps the number
- return number
- end
- local function fillArrays(x, z, pos) -- fills arrays with ON & OFF button coords
- buttonPos[pos][z+0] = x -- starting coord
- buttonPos[pos][z+1] = x+3 -- ending coord
- end
- local function Row(pos) -- calculates in which row the current engine has to be
- 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) -- draws the still sprite of the engine
- local y=4
- row = Row(pos)
- local x
- if EnginesDisplayed == 1 and maxW < 20 then
- y = y+(5*(row-1))
- x = 2
- elseif EnginesDisplayed == 1 then
- y = y+(5*(row-1))
- x = (math.floor(maxW/2)/2)
- elseif (pos - (EnginesDisplayed * (row - 1))) == 1 then -- determines if the engine is far left (1st pos)
- padding = ((maxW-(19*EnginesDisplayed))/2)+2
- x = padding
- y = y+(5*(row-1))
- else -- running for engines in position 2 or higher from the left
- x = (padding+(19*(pos - (EnginesDisplayed * (row - 1))-1)))
- 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 -- sets that the current sprite for the engine is still
- end
- local function DrawMoving(rpm, pos) -- draws the moving sprite of the engine ADD ANOTHER PARAMETER TO FUNC
- local y=4
- row = Row(pos)
- local x
- if EnginesDisplayed == 1 and maxW < 20 then
- y = y+(5*(row-1))
- x = 2
- elseif EnginesDisplayed == 1 then
- y = y+(5*(row-1))
- x = (math.floor(maxW/2)/2)
- elseif (pos - (EnginesDisplayed * (row - 1))) == 1 then -- determines if the engine is far left (1st pos)
- padding = ((maxW-(19*EnginesDisplayed))/2)+2
- x = padding
- y = y+(5*(row-1))
- else -- running for engines in position 2 or higher from the left
- x = (padding+(19*(pos - (EnginesDisplayed * (row - 1))-1)))
- 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)
- -- IF STATEMENT TO WRINTE ON & OFF BUTTONS OR TUNRING ON AND OFF TEXT
- 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 -- sets that the current sprite for the engine is moving
- end
- local arrayCount = 1
- while arrayCount <= EngineNumber do -- sets the spin position of all engines to still
- spinPos[arrayCount] = 0
- arrayCount = arrayCount + 1
- sleep(0.05) -- waits one ingame tick
- end
- monitor.clear()
- function Screen() -- calls all the functions to draw all the engines
- while true do
- local loop=1
- while loop <= EngineNumber do
- if isSpinning(lineBreak(loop)) == 1 and spinPos[loop] == 0 then -- if the engine is spinning and the current sprite is still it will draw a moving one
- DrawMoving(lineBreak(loop), loop)
- else
- DrawStill(lineBreak(loop), loop)
- end
- loop = loop + 1
- end
- loop = 1
- coroutine.yield() -- pauses the function until resumed
- end
- end
- function handleTouch(x, y) -- handles the touch on the monitor
- local i = 1
- local check_y = 7; -- line of the first buttons
- if x and y then
- for loop = 1, EngineNumber do
- if Row(i) == 1 then -- checks if the engine is in first row to set button y coords
- check_y = 7
- else
- check_y = 7+(5*(Row(i)-1)) -- sets button y coords for all other rows
- 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 run=1
- 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)
- while run == 1 do
- monitor.setCursorPos((maxW-#text)/2+2,2)
- monitor.setTextColor(colors.white)
- monitor.write(text)
- local success, screenResult = coroutine.resume(screenCoroutine)
- if not success then
- print("Error in screenCoroutine: " .. tostring(screenResult))
- end
- wait(1)
- sleep(0.25)
- monitor.clear()
- end -- by fi5hii and IceFire (plus a little help from MineNat69)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement