fi5hii

Untitled

May 13th, 2024
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.05 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. if pos <= EnginesDisplayed then
  71. local x = (pos+(math.floor(maxW/EnginesDisplayed)*pos)-18)
  72. else
  73. local x = (pos-(EnginesDisplayed*(row-1)))+(math.floor(maxW/EnginesDisplayed)*pos)-18 -- check math
  74. y = y+(5*(row-1))
  75. end
  76.  
  77. --[[if ( pos%2==1) then
  78. x=2
  79. else
  80. x=22
  81. end
  82. if pos==1 or pos==2 then
  83. y = 4
  84. elseif pos==3 or pos==4 then
  85. y = 9
  86. else
  87. y = 14
  88. end]]--
  89.  
  90. monitor.setTextColor(colors[TextColor(rpm)])
  91. monitor.setCursorPos(x, y)
  92. monitor.write(" ___")
  93. y = y + 1
  94. monitor.setCursorPos(x, y)
  95. monitor.write("/ I \\ ")
  96. y = y + 1
  97. monitor.setCursorPos(x, y)
  98. monitor.write("|-O-|")
  99. y = y + 1
  100. monitor.setCursorPos(x, y)
  101. monitor.write("\\_I_/")
  102. x = x + 6
  103. y = y - 2
  104. monitor.setTextColor(colors.white)
  105. monitor.setCursorPos(x, y)
  106. monitor.write("Engine #"..pos)
  107. y = y + 1
  108. monitor.setCursorPos(x, y)
  109. monitor.setTextColor(colors.yellow)
  110. monitor.write("Status: "..EngineStatus(rpm))
  111. y = y + 1
  112. x = x + 2
  113. monitor.setCursorPos(x, y)
  114. monitor.setTextColor(colors.green)
  115. monitor.write("ON")
  116. fillArrays(x, "1", pos)
  117. x = x + 5
  118. monitor.setCursorPos(x, y)
  119. monitor.setTextColor(colors.red)
  120. monitor.write("OFF")
  121. fillArrays(x, "3", pos)
  122. spinPos[pos] = 0 -- sets that the current sprite for the engine is still
  123. end
  124.  
  125. local function DrawMoving(rpm, pos) -- draws the moving sprite of the engine
  126. local y=4
  127. row = Row(pos)
  128. if pos <= EnginesDisplayed then
  129. local x = (pos+(math.floor(maxW/EnginesDisplayed)*pos)-18)
  130. else
  131. local x = (pos-(EnginesDisplayed*(row-1)))+(math.floor(maxW/EnginesDisplayed)*pos)-18 -- check math
  132. y = y+(5*(row-1))
  133. end
  134.  
  135. --[[if ( pos%2==1) then
  136. x=2
  137. else
  138. x=22
  139. end
  140. if pos==1 or pos==2 then
  141. y = 4
  142. elseif pos==3 or pos==4 then
  143. y = 9
  144. else
  145. y = 14
  146. end]]--
  147.  
  148. monitor.setTextColor(colors[TextColor(rpm)])
  149. monitor.setCursorPos(x, y)
  150. monitor.write(" ___")
  151. y = y + 1
  152. monitor.setCursorPos(x, y)
  153. monitor.write("/\\ /\\ ")
  154. y = y + 1
  155. monitor.setCursorPos(x, y)
  156. monitor.write("| O |")
  157. y = y + 1
  158. monitor.setCursorPos(x, y)
  159. monitor.write("\\/_\\/")
  160. x = x + 6
  161. y = y - 2
  162. monitor.setTextColor(colors.white)
  163. monitor.setCursorPos(x, y)
  164. monitor.write("Engine #"..pos)
  165. y = y + 1
  166. monitor.setCursorPos(x, y)
  167. monitor.setTextColor(colors.yellow)
  168. monitor.write("Status: "..EngineStatus(rpm))
  169. y = y + 1
  170. x = x + 2
  171. monitor.setCursorPos(x, y)
  172. monitor.setTextColor(colors.green)
  173. monitor.write("ON")
  174. fillArrays(x, "1", pos)
  175. x = x + 5
  176. monitor.setCursorPos(x, y)
  177. monitor.setTextColor(colors.red)
  178. monitor.write("OFF")
  179. fillArrays(x, "3", pos)
  180. spinPos[pos] = 1 -- sets that the current sprite for the engine is moving
  181. end
  182.  
  183. while arrayCount <= EngineNumber do -- sets the spin position of all engines to still
  184. spinPos[arrayCount] = 0
  185. arrayCount = arrayCount + 1
  186. sleep(0.05) -- waits one ingame tick
  187. end
  188.  
  189. monitor.clear()
  190. arrayCount = 1
  191.  
  192. function Screen() -- calls all the functions to draw all the engines
  193. while true do
  194. local loop=1
  195. while loop <= EngineNumber do
  196. 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
  197. DrawMoving(lineBreak(loop), loop)
  198. else
  199. DrawStill(lineBreak(loop), loop)
  200. end
  201. loop = loop + 1
  202. end
  203. loop = 1
  204. coroutine.yield() -- pauses the function until resumed
  205. end
  206. end
  207.  
  208. function handleTouch(x, y) -- handles the touch on the monitor
  209. local i = 1
  210. local check_y = 7; -- line of the first buttons
  211. if x and y then
  212. for loop = 1, EngineNumber do
  213. if i == 1 or i == 2 then
  214. check_y = 7
  215. elseif i == 3 or i == 4 then
  216. check_y = 7 + 5
  217. elseif i == 5 or i == 6 then
  218. check_y = 7 + 10
  219. end
  220. if y == check_y and x >= buttonPos[i][1] and x <= buttonPos[i][2] then
  221. rednet.broadcast(i, "ON")
  222. print("clicked ON ... engine ", i)
  223. elseif y == check_y and x >= buttonPos[i][3] and x <= buttonPos[i][4] then
  224. rednet.broadcast(i, "OFF")
  225. print("clicked OFF ... engine ", i)
  226. end
  227. i = i + 1
  228. end
  229. end
  230. end
  231.  
  232. local run=1
  233.  
  234. function wait ( time )
  235. local timer = os.startTimer(time)
  236. while true do
  237. local event = {os.pullEvent()}
  238. if (event[1] == "timer" and event[2] == timer) then
  239. break
  240. elseif event[1] == "monitor_touch" then
  241. handleTouch(event[3], event[4])
  242. elseif event[1] == "modem_message" then
  243. local mess = event[5]
  244. print("Got modem message "..mess)
  245. if mess == "shutdown_monitor" then
  246. shutdownMonitor()
  247. break
  248. end
  249. end
  250. end
  251. end
  252.  
  253. screenCoroutine = coroutine.create(Screen)
  254.  
  255. while run == 1 do
  256. monitor.setCursorPos(14, 2)
  257. monitor.setTextColor(colors.white)
  258. monitor.write("ENGINE CONTROL")
  259. local success, screenResult = coroutine.resume(screenCoroutine)
  260. if not success then
  261. print("Error in screenCoroutine: " .. tostring(screenResult))
  262. end
  263. wait(1)
  264. sleep(0.25)
  265. monitor.clear()
  266. end -- by fi5hii and IceFire (plus a little help from MineNat69)
Advertisement
Add Comment
Please, Sign In to add comment