fi5hii

Untitled

May 13th, 2024
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.34 KB | None | 0 0
  1. local monitor = peripheral.find("monitor", function(name) return name == "monitor_4" end)
  2. local maxRPM = 64
  3. local CreateTarget = "create_target_4"
  4. local EngineNumber = 6
  5. local spinPos = {}
  6. local buttonPos = {}
  7. local maxW, maxH = monitor.getSize()
  8. local EnginesDisplayed = math.floor(maxW / 18)
  9. local firstPos = math.floor((maxW / EnginesDisplayed) - 18)
  10. rednet.open("left")
  11.  
  12. print("Your monitor can display " .. EnginesDisplayed .. " engines in a row")
  13.  
  14. for i = 1, EngineNumber do
  15. buttonPos[i] = {}
  16. for j = 1, 4 do
  17. buttonPos[i][j] = 0
  18. end
  19. end
  20.  
  21. local function TextColor(rpm)
  22. if rpm == 0 then
  23. return "gray"
  24. elseif rpm > 0 and rpm < maxRPM then
  25. return "orange"
  26. else
  27. return "green"
  28. end
  29. end
  30.  
  31. local function EngineStatus(rpm)
  32. if rpm == 0 then
  33. return "OFF"
  34. else
  35. return "ON"
  36. end
  37. end
  38.  
  39. function isSpinning(rpm)
  40. if rpm == 0 then
  41. return 0
  42. else
  43. return 1
  44. end
  45. end
  46.  
  47. function lineBreak(line)
  48. local stat = peripheral.call(CreateTarget, "getLine", line)
  49. local number = tonumber(stat:match("^%d+"))
  50. return number
  51. end
  52.  
  53. local function fillArrays(x, z, pos)
  54. buttonPos[pos][z + 0] = x
  55. buttonPos[pos][z + 1] = x + 3
  56. end
  57.  
  58. local function Row(pos)
  59. if pos % EnginesDisplayed == 0 then
  60. return(math.floor(((pos - 1) / EnginesDisplayed) + 1))
  61. else
  62. return(math.floor((pos / EnginesDisplayed) + 1))
  63. end
  64. end
  65.  
  66. local function DrawStill(rpm, pos)
  67. local y = 4
  68. local row = Row(pos)
  69. local x
  70. if pos <= EnginesDisplayed then
  71. x = (pos + (math.floor(maxW / EnginesDisplayed) * pos) - 18)
  72. else
  73. x = (pos - (EnginesDisplayed * (row - 1))) + (math.floor(maxW / EnginesDisplayed) * pos) - 18
  74. y = y + (5 * (row - 1))
  75. end
  76.  
  77. monitor.setTextColor(colors[TextColor(rpm)])
  78. monitor.setCursorPos(x, y)
  79. monitor.write(" ___")
  80. y = y + 1
  81. monitor.setCursorPos(x, y)
  82. monitor.write("/ I \\ ")
  83. y = y + 1
  84. monitor.setCursorPos(x, y)
  85. monitor.write("|-O-|")
  86. y = y + 1
  87. monitor.setCursorPos(x, y)
  88. monitor.write("\\_I_/")
  89. x = x + 6
  90. y = y - 2
  91. monitor.setTextColor(colors.white)
  92. monitor.setCursorPos(x, y)
  93. monitor.write("Engine #" .. pos)
  94. y = y + 1
  95. monitor.setCursorPos(x, y)
  96. monitor.setTextColor(colors.yellow)
  97. monitor.write("Status: " .. EngineStatus(rpm))
  98. y = y + 1
  99. x = x + 2
  100. monitor.setCursorPos(x, y)
  101. monitor.setTextColor(colors.green)
  102. monitor.write("ON")
  103. fillArrays(x, "1", pos)
  104. x = x + 5
  105. monitor.setCursorPos(x, y)
  106. monitor.setTextColor(colors.red)
  107. monitor.write("OFF")
  108. fillArrays(x, "3", pos)
  109. spinPos[pos] = 0
  110. end
  111.  
  112. local function DrawMoving(rpm, pos)
  113. local y = 4
  114. local row = Row(pos)
  115. local x
  116. if pos <= EnginesDisplayed then
  117. x = (pos + (math.floor(maxW / EnginesDisplayed) * pos) - 18)
  118. else
  119. x = (pos - (EnginesDisplayed * (row - 1))) + (math.floor(maxW / EnginesDisplayed) * pos) - 18
  120. y = y + (5 * (row - 1))
  121. end
  122.  
  123. monitor.setTextColor(colors[TextColor(rpm)])
  124. monitor.setCursorPos(x, y)
  125. monitor.write(" ___")
  126. y = y + 1
  127. monitor.setCursorPos(x, y)
  128. monitor.write("/\\ /\\ ")
  129. y = y + 1
  130. monitor.setCursorPos(x, y)
  131. monitor.write("| O |")
  132. y = y + 1
  133. monitor.setCursorPos(x, y)
  134. monitor.write("\\/_\\/")
  135.  
  136. x = x + 6
  137. y = y - 2
  138. monitor.setTextColor(colors.white)
  139. monitor.setCursorPos(x, y)
  140. monitor.write("Engine #" .. pos)
  141. y = y + 1
  142. monitor.setCursorPos(x, y)
  143. monitor.setTextColor(colors.yellow)
  144. monitor.write("Status: " .. EngineStatus(rpm))
  145. y = y + 1
  146. x = x + 2
  147. monitor.setCursorPos(x, y)
  148. monitor.setTextColor(colors.green)
  149. monitor.write("ON")
  150. fillArrays(x, "1", pos)
  151. x = x + 5
  152. monitor.setCursorPos(x, y)
  153. monitor.setTextColor(colors.red)
  154. monitor.write("OFF")
  155. fillArrays(x, "3", pos)
  156. spinPos[pos] = 1
  157. end
  158.  
  159. arrayCount = 1
  160.  
  161. while arrayCount <= EngineNumber do
  162. spinPos[arrayCount] = 0
  163. arrayCount = arrayCount + 1
  164. sleep(0.05)
  165. end
  166.  
  167. monitor.clear()
  168.  
  169. function Screen()
  170. while true do
  171. local loop = 1
  172. while loop <= EngineNumber do
  173. if isSpinning(lineBreak(loop)) == 1 and spinPos[loop] == 0 then
  174. DrawMoving(lineBreak(loop), loop)
  175. else
  176. DrawStill(lineBreak(loop), loop)
  177. end
  178. loop = loop + 1
  179. end
  180. loop = 1
  181. coroutine.yield()
  182. end
  183. end
  184.  
  185. function handleTouch(x, y)
  186. local i = 1
  187. local check_y = 7
  188. if x and y then
  189. for loop = 1, EngineNumber do
  190. if i == 1 or i == 2 then
  191. check_y = 7
  192. elseif i == 3 or i == 4 then
  193. check_y = 7 + 5
  194. elseif i == 5 or i == 6 then
  195. check_y = 7 + 10
  196. end
  197. if y == check_y and x >= buttonPos[i][1] and x <= buttonPos[i][2] then
  198. rednet.broadcast(i, "ON")
  199. print("Clicked ON ... Engine ", i)
  200. elseif y == check_y and x >= buttonPos[i][3] and x <= buttonPos[i][4] then
  201. rednet.broadcast(i, "OFF")
  202. print("Clicked OFF ... Engine ", i)
  203. end
  204. i = i + 1
  205. end
  206. end
  207. end
  208.  
  209. local function wait(time)
  210. local timer = os.startTimer(time)
  211. while true do
  212. local event = {os.pullEvent()}
  213. if event[1] == "timer" and event[2] == timer then
  214. break
  215. elseif event[1] == "monitor_touch" then
  216. handleTouch(event[3], event[4])
  217. elseif event[1] == "modem_message" then
  218. local mess = event[5]
  219. print("Got modem message " .. mess)
  220. if mess == "shutdown_monitor" then
  221. shutdownMonitor()
  222. break
  223. end
  224. end
  225. end
  226. end
  227.  
  228. screenCoroutine = coroutine.create(Screen)
  229.  
  230. local run = 1
  231.  
  232. while run == 1 do
  233. monitor.setCursorPos(14, 2)
  234. monitor.setTextColor(colors.white)
  235. monitor.write("ENGINE CONTROL")
  236. local success, screenResult = coroutine.resume(screenCoroutine)
  237. if not success then
  238. print("Error in screenCoroutine: " .. tostring(screenResult))
  239. break
  240. end
  241. wait(1)
  242. sleep(0.25)
  243. monitor.clear()
  244. end
  245.  
Advertisement
Add Comment
Please, Sign In to add comment