asteroidsteam

Extreme Reactors Remote Control

Jul 12th, 2022 (edited)
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.00 KB | None | 0 0
  1. rednet.open("back")
  2.  
  3. buttons = {}
  4.  
  5. local cur_power_gen = 0
  6. local target_power = 5000 -- RF/Tick
  7. local control_rod_level = 50
  8. local reactor_enabled = false
  9.  
  10. local mw, mh = term.getSize()
  11.  
  12. function drawBoxWithCenteredText(x,y,w,h,text,color)
  13.     local ox,oy = term.getCursorPos()
  14.     local bgColor = term.getBackgroundColor()
  15.  
  16.     term.setBackgroundColor(color)
  17.     for cy=1,h,1 do
  18.         for cx=1,w,1 do
  19.             term.setCursorPos(x+cx,y+cy)
  20.             term.write(" ")
  21.         end
  22.     end
  23.  
  24.     local cx = math.floor(w / 2 + x - string.len(text) / 2 + 1)
  25.     local cy = math.floor(h / 2 + y + 1)
  26.  
  27.     term.setCursorPos(cx, cy)
  28.     term.write(text)
  29.  
  30.     term.setBackgroundColor(bgColor)
  31.     term.setCursorPos(ox, oy)
  32. end
  33.  
  34. function clamp(val, min, max)
  35.     if val > max then
  36.         return max
  37.     elseif val < min then
  38.         return min
  39.     else
  40.         return val
  41.     end
  42. end
  43.  
  44. function drawBar(x, y, size, perc)
  45.     local bgColor = term.getBackgroundColor()
  46.     local ox, oy = term.getCursorPos()
  47.  
  48.     term.setCursorPos(x, y)
  49.     term.setBackgroundColor(colors.green)
  50.  
  51.     local percGreen = clamp(math.floor(perc * size), 0, size)
  52.     local percGray = clamp(size - percGreen, 0, size - percGreen)
  53.     if percGray < 0 then
  54.         percGray = 0
  55.     end
  56.  
  57.     for i = percGreen,1,-1 do
  58.         term.write(" ")
  59.     end
  60.     term.setBackgroundColor(colors.lightGray)
  61.     for i = percGray,1,-1 do
  62.         term.write(" ")
  63.     end
  64.  
  65.     term.setBackgroundColor(bgColor)
  66.     term.setCursorPos(ox, oy)
  67. end
  68.  
  69. function getControlRodDiff(RFDiff)
  70.     if RFDiff > 10000 then
  71.         return 5
  72.     elseif RFDiff > 5000 then
  73.         return 3
  74.     elseif RFDiff > 1000 then
  75.         return 1
  76.     elseif RFDiff > 500 then
  77.         return 0.5
  78.     elseif RFDiff > 100 then
  79.         return 0.1
  80.     else
  81.         return 0.05
  82.     end
  83. end
  84.  
  85. function formatRF(rf)
  86.     if rf >= 1000000 then
  87.         return round(rf/1000000, 2) .. " MiRF"
  88.     elseif rf >= 1000 then
  89.         return round(rf/1000, 2) .. " KiRF"
  90.     else
  91.         return math.floor(rf) .. " RF"
  92.     end
  93. end
  94.  
  95. function round(num, numDecimalPlaces)
  96.     local mult = 10^(numDecimalPlaces or 0)
  97.     return math.floor(num * mult + 0.5) / mult
  98. end
  99.  
  100. function renderButtons()
  101.     for i = 1, #buttons do
  102.         local button = buttons[i]
  103.         if not reactor_enabled then
  104.             if button.drod then
  105.                 drawBoxWithCenteredText(button.x,button.y,button.w,button.h,button.text,button.color)
  106.             end
  107.         else
  108.             drawBoxWithCenteredText(button.x,button.y,button.w,button.h,button.text,button.color)
  109.         end
  110.     end
  111. end
  112.  
  113. function addButton(x,y,w,h,text,color,doRenderOnDisable, executor)
  114.     local button = {
  115.         x = x,
  116.         y = y,
  117.         w = w,
  118.         h = h,
  119.         text = text,
  120.         color = color,
  121.         drod = doRenderOnDisable,
  122.         executor = executor
  123.     }
  124.     buttons[#buttons+1] = button
  125. end
  126.  
  127. function inBounds(n, min, max)
  128.     return n <= max and n >= min
  129. end
  130.  
  131. function sendBTNCommand(btn)
  132.     sendCommand({button = btn.text})
  133. end
  134.  
  135. addButton(1,mh - 4,11,3,"SHUT DOWN",colours.red, true, sendBTNCommand)
  136. addButton(14,mh - 4,11,3,"START",colours.green, true, sendBTNCommand)
  137.  
  138. addButton(1, 9, 3, 1, "-10", colours.red, false, sendBTNCommand)
  139. addButton(5, 9, 4, 1, "-100", colours.red, false, sendBTNCommand)
  140. addButton(10, 9, 5, 1, "-1000", colours.red, false, sendBTNCommand)
  141.  
  142. addButton(1, 11, 3, 1, "+10", colours.green, false, sendBTNCommand)
  143. addButton(5, 11, 4, 1, "+100", colours.green, false, sendBTNCommand)
  144. addButton(10, 11, 5, 1, "+1000", colours.green, false, sendBTNCommand)
  145.  
  146. function sendCommand(command)
  147.     rednet.send(0, textutils.serialize(command), "reactor_control")
  148.     local id, object = rednet.receive("reactor_response")
  149.     return textutils.unserialize(object)
  150. end
  151.  
  152. function getReactorStatus()
  153.     return sendCommand("_QUERY_")
  154. end
  155.  
  156. term.clear()
  157.  
  158. function render()
  159.     while true do
  160.         local reactor_status = getReactorStatus()
  161.  
  162.         cur_power_gen = reactor_status.cur_power_gen
  163.         reactor_enabled = reactor_status.reactor_enabled
  164.         target_power = reactor_status.target_power
  165.         control_rod_level = reactor_status.control_rod_level
  166.  
  167.         if cur_power_gen ~= 0 and reactor_enabled then
  168.             term.setCursorPos(1,1)
  169.             term.write("Control Rod Level: ")
  170.             drawBar(1,2,20,control_rod_level/100)
  171.             term.setCursorPos(22,2)
  172.             term.write(math.floor(control_rod_level) .. "\%  ")
  173.  
  174.             term.setCursorPos(1,4)
  175.             term.write("Power Generation: ")
  176.             drawBar(1,5,13,cur_power_gen/target_power)
  177.             term.setCursorPos(15,5)
  178.             term.write(formatRF(cur_power_gen) .. "/t    ")
  179.  
  180.             term.setCursorPos(1,7)
  181.             term.write("Target power generation: ")
  182.             term.setCursorPos(2,8)
  183.             term.clearLine()
  184.             term.setBackgroundColor(colors.blue)
  185.             term.write(formatRF(target_power) .. "/t")
  186.             term.setBackgroundColor(colors.black)
  187.         end
  188.  
  189.         if not reactor_enabled then
  190.             term.clear()
  191.             term.setCursorPos(2,2)
  192.             term.write("Reactor is offline")
  193.         end
  194.  
  195.         renderButtons()
  196.         sleep(0.1)
  197.     end
  198. end
  199.  
  200. function pullEvents()
  201.     while true do
  202.         event, button, x, y = os.pullEvent("mouse_click")
  203.         for i=1, #buttons do
  204.             local button = buttons[i]
  205.             if inBounds(x, button.x, button.x + button.w) and inBounds(y, button.y, button.y + button.h) then
  206.                 button.executor(button)
  207.                 break
  208.             end
  209.         end
  210.     end
  211. end
  212.  
  213. parallel.waitForAny(pullEvents, render)
Add Comment
Please, Sign In to add comment