fi5hii

Untitled

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