fi5hii

Untitled

May 13th, 2024
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.64 KB | None | 0 0
  1. local monitor = peripheral.find("monitor", function(name) return name == "monitor_4" end)
  2. local maxRPM = 64 -- rpm that engines run at full power (for displaying green color)
  3. local CreateTarget = "create_target_4" -- name of the CC:C Bridge target block on the network
  4. local EngineNumber = 6 -- number of total engines connected to the target block
  5. local spinPos = {}
  6. local buttonPos = {}
  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. rednet.open("left") -- opens the side of the PC that has the wireless modem installed
  11.  
  12.  
  13. print("your monitor can display "..EnginesDisplayed.." engines in a row")
  14.  
  15. for i = 1, EngineNumber do -- sets an array table for every engine to store button coords
  16. buttonPos[i] = {}
  17. for j = 1, 4 do -- 4 times for coordinates x1 x2 x3 x4
  18. buttonPos[i][j] = 0
  19. end
  20. end
  21.  
  22. local function TextColor(rpm) -- decides the color for the engine sprite
  23. if rpm == 0 then
  24. return "gray" -- gray color if engine is off
  25. elseif rpm > 0 and rpm < maxRPM then
  26. return "orange" -- orange color if engine is on but not at max speed
  27. else
  28. return "green" -- green color if engine is on and at max speed
  29. end
  30. end
  31.  
  32. local function EngineStatus(rpm) -- returns if the engine is on or off in text
  33. if rpm == 0 then
  34. return "OFF"
  35. else
  36. return "ON"
  37. end
  38. end
  39.  
  40. function isSpinning (rpm) -- returns if the engine is spinning or not in int
  41. if rpm == 0 then
  42. return 0
  43. else
  44. return 1
  45. end
  46. end
  47.  
  48. function lineBreak (line) -- returns the RPM for a motor
  49. local stat = peripheral.call(CreateTarget, "getLine", line) -- separates the different lines in the target block
  50. local number = tonumber(stat:match("^%d+")) -- removes "RPM" and only keeps the number
  51. return number
  52. end
  53.  
  54. local function fillArrays(x, z, pos) -- fills arrays with ON & OFF button coords
  55. buttonPos[pos][z+0] = x
  56. buttonPos[pos][z+1] = x+3
  57. end
  58.  
  59. local function Row(pos) -- calculates in which row the current engine has to be
  60. if pos%EnginesDisplayed == 0 then
  61. return(math.floor(((pos-1)/EnginesDisplayed)+1))
  62. else
  63. return(math.floor((pos/EnginesDisplayed)+1))
  64. end
  65. end
  66.  
  67. local function DrawStill(rpm, pos) -- draws the still sprite of the engine
  68. local y=4
  69. row = Row(pos)
  70. local x
  71. if pos <= EnginesDisplayed then
  72. x = (pos+(math.floor(maxW/EnginesDisplayed)*pos)-18)
  73. else
  74. x = (pos-(EnginesDisplayed*(row-1)))+(math.floor(maxW/EnginesDisplayed)*pos)-18 -- check math
  75. y = y+(5*(row-1))
  76. end
  77.  
  78. monitor.setTextColor(colors[TextColor(rpm)])
  79. monitor.setCursorPos(x, y)
  80. monitor.write(" ___")
  81. y = y + 1
  82. monitor.setCursorPos(x, y)
  83. monitor.write("/ I \\ ")
  84. y = y + 1
  85. monitor.setCursorPos(x, y)
  86. monitor.write("|-O-|")
  87. y = y + 1
  88. monitor.setCursorPos(x, y)
  89. monitor.write("\\_I_/")
  90. x = x + 6
  91. y = y - 2
  92. monitor.setTextColor(colors.white)
  93. monitor.setCursorPos(x, y)
  94. monitor.write("Engine #"..pos)
  95. y = y + 1
  96. monitor.setCursorPos(x, y)
  97. monitor.setTextColor(colors.yellow)
  98. monitor.write("Status: "..EngineStatus(rpm))
  99. y = y + 1
  100. x = x + 2
  101. monitor.setCursorPos(x, y)
  102. monitor.setTextColor(colors.green)
  103. monitor.write("ON")
  104. fillArrays(x, "1", pos)
  105. x = x + 5
  106. monitor.setCursorPos(x, y)
  107. monitor.setTextColor(colors.red)
  108. monitor.write("OFF")
  109. fillArrays(x, "3", pos)
  110. spinPos[pos] = 0 -- sets that the current sprite for the engine is still
  111. end
  112.  
  113. local function DrawMoving(rpm, pos) -- draws the moving sprite of the engine
  114. local y=4
  115. row = Row(pos)
  116. local x
  117. if pos <= EnginesDisplayed then
  118. x = (pos+(math.floor(maxW/EnginesDisplayed)*pos)-18)
  119. else
  120. x = (pos-(EnginesDisplayed*(row-1)))+(math.floor(maxW/EnginesDisplayed)*pos)-18 -- check math
  121. y = y+(5*(row-1))
  122. end
  123.  
  124. monitor.setTextColor(colors[TextColor(rpm)])
  125. monitor.setCursorPos(x, y)
  126. monitor.write(" ___")
  127. y = y + 1
  128. monitor.setCursorPos(x, y)
  129. monitor.write("/\\ /\\ ")
  130. y = y + 1
  131. monitor.setCursorPos(x, y)
  132. monitor.write("| O |")
  133. y = y + 1
  134. monitor.setCursorPos(x, y)
  135. monitor.write("\\/_\\/")
  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 -- sets that the current sprite for the engine is moving
  157. end
  158.  
  159. local arrayCount = 1
  160.  
  161. while arrayCount <= EngineNumber do -- sets the spin position of all engines to still
  162. spinPos[arrayCount] = 0
  163. arrayCount = arrayCount + 1
  164. sleep(0.05) -- waits one ingame tick
  165. end
  166.  
  167. monitor.clear()
  168.  
  169. function Screen() -- calls all the functions to draw all the engines
  170. while true do
  171. local loop=1
  172. while loop <= EngineNumber do
  173. 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
  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() -- pauses the function until resumed
  182. end
  183. end
  184.  
  185. function handleTouch(x, y) -- handles the touch on the monitor
  186. local i = 1
  187. local check_y = 7; -- line of the first buttons
  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 run=1
  210.  
  211. function wait ( time )
  212. local timer = os.startTimer(time)
  213. while true do
  214. local event = {os.pullEvent()}
  215. if (event[1] == "timer" and event[2] == timer) then
  216. break
  217. elseif event[1] == "monitor_touch" then
  218. handleTouch(event[3], event[4])
  219. elseif event[1] == "modem_message" then
  220. local mess = event[5]
  221. print("Got modem message "..mess)
  222. if mess == "shutdown_monitor" then
  223. shutdownMonitor()
  224. break
  225. end
  226. end
  227. end
  228. end
  229.  
  230. screenCoroutine = coroutine.create(Screen)
  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. end
  240. wait(1)
  241. sleep(0.25)
  242. monitor.clear()
  243. end -- by fi5hii and IceFire (plus a little help from MineNat69)
Advertisement
Add Comment
Please, Sign In to add comment