Advertisement
fi5hii

Untitled

May 21st, 2024
715
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.55 KB | None | 0 0
  1. local monitor = peripheral.find("monitor", function(name) return name == "monitor_0" end) -- name of the monitor connected to the network
  2. local maxRPM = 64 -- rpm that engines run at full power (for displaying green color)
  3. local CreateTarget = "create_target_0" -- name of the CC:C Bridge target block on the network
  4. local EngineNumber = 1 -- number of total engines connected to the target block
  5. local spinPos = {} -- table for all engines to log what sprite is rendered
  6. local buttonPos = {} -- table for button coords for all engines
  7. local maxW, maxH = monitor.getSize() -- gets screen dimensions
  8. local EnginesDisplayed = math.floor(maxW/18) -- number of engines that can fit on your monitor
  9. local firstPos = math.floor((maxW/EnginesDisplayed)-18) -- position of cursor for first engine
  10. local padding -- padding for centering
  11. local text = "ENGINE CONTROL" -- text that's on top of the engine monitor
  12. rednet.open("left") -- opens the side of the PC that has the wireless modem installed
  13. print("your monitor can display "..EnginesDisplayed.." engines in a row")
  14.  
  15.  
  16. for i = 1, EngineNumber do -- sets an array table for every engine to store button coords
  17.     buttonPos[i] = {}
  18.     for j = 1, 4 do -- 4 times for coordinates x1 x2 x3 x4
  19.         buttonPos[i][j] = 0
  20.     end
  21. end
  22.  
  23. local function TextColor(rpm) -- decides the color for the engine sprite
  24.     if rpm == 0 then
  25.         return "gray" -- gray color if engine is off
  26.     elseif rpm > 0 and rpm < maxRPM then
  27.         return "orange" -- orange color if engine is on but not at max speed
  28.     else
  29.         return "green" -- green color if engine is on and at max speed
  30.     end
  31. end
  32.  
  33. local function EngineStatus(rpm) -- returns the engine status in text
  34.     if rpm == 0 then
  35.         return "OFF"
  36.     else
  37.         return "ON"
  38.     end
  39. end
  40.  
  41. function isSpinning (rpm) -- rdetermines if the engine is spinning
  42.     if rpm == 0 then
  43.         return 0
  44.     else
  45.         return 1
  46.     end
  47. end
  48.  
  49. function lineBreak (line) -- returns the RPM for a motor
  50.     local stat = peripheral.call(CreateTarget, "getLine", line) -- separates the different lines in the target block
  51.     local number = tonumber(stat:match("^%d+")) -- removes "RPM" and only keeps the number
  52.     return number
  53. end
  54.  
  55. local function fillArrays(x, z, pos) -- fills arrays with ON & OFF button coords
  56.     buttonPos[pos][z+0] = x -- starting coord
  57.     buttonPos[pos][z+1] = x+3 -- ending coord
  58. end
  59.  
  60. local function Row(pos) -- calculates in which row the current engine has to be
  61.     if pos%EnginesDisplayed == 0 then
  62.         return(math.floor(((pos-1)/EnginesDisplayed)+1))
  63.     else
  64.         return(math.floor((pos/EnginesDisplayed)+1))
  65.     end
  66. end
  67.  
  68. local function DrawStill(rpm, pos) -- draws the still sprite of the engine
  69.     local y=4
  70.     row = Row(pos)
  71.     local x
  72.     if EnginesDisplayed == 1 and maxW < 20 then
  73.         y = y+(5*(row-1))
  74.         x = 2
  75.     elseif EnginesDisplayed == 1 then
  76.         y = y+(5*(row-1))
  77.         x = (math.floor(maxW/2)/2)
  78.     elseif (pos - (EnginesDisplayed * (row - 1))) == 1 then -- determines if the engine is far left (1st pos)
  79.      padding = ((maxW-(19*EnginesDisplayed))/2)+2
  80.     x = padding
  81.         y = y+(5*(row-1))
  82.     else -- running for engines in position 2 or higher from the left
  83.         x = (padding+(19*(pos - (EnginesDisplayed * (row - 1))-1)))
  84.         y = y+(5*(row-1))
  85.     end
  86.    
  87.     monitor.setTextColor(colors[TextColor(rpm)])
  88.     monitor.setCursorPos(x, y)
  89.     monitor.write(" ___")
  90.     y = y + 1
  91.     monitor.setCursorPos(x, y)
  92.     monitor.write("/ I \\ ")
  93.     y = y + 1
  94.     monitor.setCursorPos(x, y)
  95.     monitor.write("|-O-|")
  96.     y = y + 1
  97.     monitor.setCursorPos(x, y)
  98.     monitor.write("\\_I_/")
  99.     x = x + 6
  100.     y = y - 2
  101.     monitor.setTextColor(colors.white)
  102.     monitor.setCursorPos(x, y)
  103.     monitor.write("Engine #"..pos)
  104.     y = y + 1
  105.     monitor.setCursorPos(x, y)
  106.     monitor.setTextColor(colors.yellow)
  107.     monitor.write("Status: "..EngineStatus(rpm))
  108.     y = y + 1
  109.     x = x + 2
  110.     monitor.setCursorPos(x, y)
  111.     monitor.setTextColor(colors.green)
  112.     monitor.write("ON")
  113.     fillArrays(x, "1", pos)
  114.     x = x + 5
  115.     monitor.setCursorPos(x, y)
  116.     monitor.setTextColor(colors.red)
  117.     monitor.write("OFF")
  118.     fillArrays(x, "3", pos)
  119.     spinPos[pos] = 0 -- sets that the current sprite for the engine is still
  120. end
  121.  
  122. local function DrawMoving(rpm, pos) -- draws the moving sprite of the engine ADD ANOTHER PARAMETER TO FUNC
  123.     local y=4
  124.     row = Row(pos)
  125.     local x
  126.     if EnginesDisplayed == 1 and maxW < 20 then
  127.         y = y+(5*(row-1))
  128.         x = 2
  129.     elseif EnginesDisplayed == 1 then
  130.         y = y+(5*(row-1))
  131.         x = (math.floor(maxW/2)/2)
  132.     elseif (pos - (EnginesDisplayed * (row - 1))) == 1 then -- determines if the engine is far left (1st pos)
  133.         padding = ((maxW-(19*EnginesDisplayed))/2)+2
  134.         x = padding
  135.         y = y+(5*(row-1))
  136.     else -- running for engines in position 2 or higher from the left
  137.         x = (padding+(19*(pos - (EnginesDisplayed * (row - 1))-1)))
  138.         y = y+(5*(row-1))
  139.     end
  140.    
  141.     monitor.setTextColor(colors[TextColor(rpm)])
  142.     monitor.setCursorPos(x, y)
  143.     monitor.write(" ___")
  144.     y = y + 1
  145.     monitor.setCursorPos(x, y)
  146.     monitor.write("/\\ /\\ ")
  147.     y = y + 1
  148.     monitor.setCursorPos(x, y)
  149.     monitor.write("| O |")
  150.     y = y + 1
  151.     monitor.setCursorPos(x, y)
  152.     monitor.write("\\/_\\/")
  153.     x = x + 6
  154.     y = y - 2
  155.     monitor.setTextColor(colors.white)
  156.     monitor.setCursorPos(x, y)
  157.     monitor.write("Engine #"..pos)
  158.     y = y + 1
  159.     monitor.setCursorPos(x, y)
  160.     monitor.setTextColor(colors.yellow)
  161.     monitor.write("Status: "..EngineStatus(rpm))
  162.     y = y + 1
  163.     x = x + 2
  164.     monitor.setCursorPos(x, y)
  165.     monitor.setTextColor(colors.green)
  166.     -- IF STATEMENT TO WRINTE ON & OFF BUTTONS OR TUNRING ON AND OFF TEXT
  167.     monitor.write("ON")
  168.     fillArrays(x, "1", pos)
  169.     x = x + 5
  170.     monitor.setCursorPos(x, y)
  171.     monitor.setTextColor(colors.red)
  172.     monitor.write("OFF")
  173.     fillArrays(x, "3", pos)
  174.     spinPos[pos] = 1 -- sets that the current sprite for the engine is moving
  175. end
  176.  
  177. local arrayCount = 1
  178.  
  179. while arrayCount <= EngineNumber do -- sets the spin position of all engines to still
  180.     spinPos[arrayCount] = 0
  181.     arrayCount = arrayCount + 1
  182.     sleep(0.05) -- waits one ingame tick
  183. end
  184.  
  185. monitor.clear()
  186.  
  187. function Screen() -- calls all the functions to draw all the engines
  188.     while true do
  189.         local loop=1
  190.         while loop <= EngineNumber do
  191.             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
  192.                 DrawMoving(lineBreak(loop), loop)
  193.             else
  194.                 DrawStill(lineBreak(loop), loop)
  195.             end
  196.             loop = loop + 1
  197.         end
  198.         loop = 1
  199.         coroutine.yield() -- pauses the function until resumed
  200.     end
  201. end
  202.  
  203. function handleTouch(x, y) -- handles the touch on the monitor
  204.     local i = 1
  205.     local check_y = 7; -- line of the first buttons
  206.     if x and y then
  207.         for loop = 1, EngineNumber do
  208.             if Row(i) == 1 then -- checks if the engine is in first row to set button y coords
  209.                 check_y = 7
  210.             else
  211.                 check_y = 7+(5*(Row(i)-1)) -- sets button y coords for all other rows
  212.             end
  213.             if y == check_y and x >= buttonPos[i][1] and x <= buttonPos[i][2] then
  214.                 rednet.broadcast(i.. "ON")
  215.                 print("clicked ON ... engine ", i)
  216.             elseif y == check_y and x >= buttonPos[i][3] and x <= buttonPos[i][4] then
  217.                 rednet.broadcast(i.. "OFF")
  218.                 print("clicked OFF ... engine ", i)
  219.             end
  220.             i = i + 1
  221.         end
  222.     end
  223. end
  224.  
  225. local run=1
  226.  
  227. function wait (time)
  228.     local timer = os.startTimer(time)
  229.     while true do
  230.       local event = {os.pullEvent()}
  231.       if (event[1] == "timer" and event[2] == timer) then
  232.           break
  233.       elseif event[1] == "monitor_touch" then
  234.             handleTouch(event[3], event[4])
  235.       elseif event[1] == "modem_message" then
  236.           local mess = event[5]
  237.           print("Got modem message "..mess)
  238.           if mess == "shutdown_monitor" then
  239.               shutdownMonitor()
  240.               break
  241.           end
  242.       end
  243.     end
  244. end
  245.  
  246. screenCoroutine = coroutine.create(Screen)
  247.  
  248. while run == 1 do
  249.     monitor.setCursorPos((maxW-#text)/2+2,2)
  250.     monitor.setTextColor(colors.white)
  251.     monitor.write(text)
  252.     local success, screenResult = coroutine.resume(screenCoroutine)
  253.     if not success then
  254.         print("Error in screenCoroutine: " .. tostring(screenResult))
  255.     end
  256.     wait(1)
  257.     sleep(0.25)
  258.     monitor.clear()
  259. end -- by fi5hii and IceFire (plus a little help from MineNat69)
  260.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement