Daniel_I_Am

Draconic Evolution Reactor Automation Script (DERAS)

Nov 11th, 2017
1,839
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.18 KB | None | 0 0
  1. --#################################################################
  2. --USEFUL FUNCTIONS
  3. --reactor functions:
  4.  
  5. --getReactorInfo():
  6. --failsafe
  7. --fieldDrainRate
  8. --maxFieldStrength
  9. --fieldStrength
  10. --energySaturation
  11. --temperature
  12. --maxEnergySaturation
  13. --fuelConversion
  14. --generationRate
  15. --status
  16. --maxFuelConversion
  17. --fuelConversionRate
  18.  
  19. --stopReactor()
  20.  
  21. --flux gate functions:
  22. --getFlow
  23. --getSignalLowFlow
  24. --setSignalLowFlow
  25. --#################################################################
  26.  
  27. os = require("os")
  28. io = require("io")
  29. term = require("term")
  30. component = require("component")
  31.  
  32. if not component.isAvailable("draconic_reactor") or not component.isAvailable("flux_gate") or not component.isAvailable("gpu") then
  33.     print("Make sure a draconic reactor, flux gate and gpu are connected to the computer before running this script.")
  34.     os.exit()
  35. end
  36.  
  37. gpu = component.gpu
  38. reactor = component.draconic_reactor.getReactorInfo()
  39. gate = component.flux_gate
  40. event = require("event")
  41.  
  42. args = {...}
  43.  
  44. isAutoStart = true
  45. if args then
  46.     if args[1] == "false" then
  47.         isAutoStart = false
  48.     end
  49. end
  50.  
  51. --[[Colors Table]]--
  52. colors = { black = 0x000000, white = 0xf8f8ff, blue = 0x0000ff, lightGray = 0xd9d9d9, red = 0xff0000,
  53. purple = 0x9b30ff, carrot = 0xffa500, magenta = 0xcd00cd, lightBlue = 0x87cefa, yellow = 0xffff00,
  54. lime = 0x32cd32, pink = 0xffc0cb, gray = 0x696969, brown = 0x8b4500, green = 0x006400, cyan = 0x008b8b,
  55. olive = 0x6b8e23, gold = 0x8b6914, orangered = 0xdb4e02, diamond = 0x0fa7c7,crimson = 0xaf002a,fuchsia = 0xfd3f92,
  56. folly = 0xff004f, frenchBlue = 0x0072bb, lilac = 0x86608e, flax = 0xeedc82, darkGray = 0x563c5c,
  57. englishGreen = 0x1b4d3e, eggplant = 0x614051, deepPink  = 0xff1493, ruby = 0x843f5b, orange = 0xf5c71a,
  58. lemon = 0xffd300, darkBlue = 0x002e63, bitterLime = 0xbfff00 }
  59.  
  60. width = 100 --df: 160
  61. height = 50 --df: 50
  62. barTextCol = colors.black
  63. BGCol = colors.gray
  64. maxTemp = 8000
  65.  
  66. function start()
  67.   reactor = component.draconic_reactor.getReactorInfo()
  68.   maxSat = reactor.maxEnergySaturation
  69.   maxConv = reactor.maxFuelConversion
  70.   maxShield = reactor.maxFieldStrength
  71.   gate.setSignalLowFlow(0)
  72.   if isAutoStart then
  73.     component.draconic_reactor.chargeReactor()
  74.   end
  75.   component.draconic_reactor.setFailSafe(true)
  76.   flow = reactor.generationRate
  77.   gate.setSignalLowFlow(flow)
  78. end
  79.  
  80. function shutdown()
  81.   component.draconic_reactor.stopReactor()
  82.   throwError("Reactor was shut down by program.")
  83. end
  84.  
  85. function update()
  86.   reactor = component.draconic_reactor.getReactorInfo()
  87.   sat = reactor.energySaturation
  88.   satp = math.floor(100*sat/maxSat+0.5)
  89.   gen = reactor.generationRate
  90.   conv = reactor.fuelConversion
  91.   convp = math.floor(10000*conv/maxConv+0.5)/100
  92.   flow = gate.getFlow()
  93.   temp = reactor.temperature
  94.   tempp = math.floor(100*temp/maxTemp+0.5)
  95.   shield = reactor.fieldStrength
  96.   shieldp = math.floor(100*shield/maxShield+0.5)
  97.   status = reactor.status
  98. end
  99.  
  100. function throwError(msg)
  101.   gpu.setBackground(colors.black)
  102.   gpu.setForeground(colors.white)
  103.   gpu.setResolution(gpu.maxResolution())
  104.   term.clear()
  105.   print("An error was thrown during execution of this program:")
  106.   print(tostring(msg))
  107.   os.sleep(5)
  108.   os.execute("/home/reactor.lua false")
  109. end
  110.  
  111. function addRect(x1,y1,x2,y2,color)
  112.   gpu.setBackground(color)
  113.   gpu.fill(x1,y1,x2-x1+1,y2-y1+1," ")
  114. end
  115.  
  116. function addBar(x1,y1,x2,y2,borderSize,barColFilled,barColEmpty,borderCol,textCol,filledp,text)
  117.   addRect(x1,y1,x2,y2,borderCol)
  118.   addRect(x1+borderSize,y1+borderSize,x2-borderSize,y2-borderSize,barColEmpty)
  119.   barHeight = y2-y1-2*borderSize
  120.   filledPixels = math.floor((barHeight*filledp)/100+0.5)
  121.   addRect(x1+borderSize,y2-borderSize-filledPixels,x2-borderSize,y2-borderSize,barColFilled)
  122.   gpu.setBackground(borderCol);gpu.setForeground(textCol);gpu.set(x1+borderSize,y1+borderSize-1,tostring(text))
  123. end
  124.  
  125. function GUI()
  126.   gpu.setBackground(BGCol)
  127.   gpu.setResolution(width,height)
  128.   term.clear()
  129.   addTextCentered(2,"DRACONIC REACTOR AUTOMATION SCRIPT",colors.red,colors.gray)
  130.   addTextCentered(3,"By Daniel_I_Am",colors.black,colors.gray)
  131. end
  132.  
  133. function addText(x,y,text,FGCol,BGCol)
  134.   gpu.setForeground(FGCol)
  135.   gpu.setBackground(BGCol)
  136.   gpu.set(x,y,text)
  137. end
  138.  
  139. function comma_value(n) -- credit http://richard.warburton.it
  140.   local left,num,right = string.match(n,'^([^%d]*%d)(%d*)(.-)$')
  141.   return left..(num:reverse():gsub('(%d%d%d)','%1.'):reverse())..right
  142. end
  143.  
  144. function center(y,text)
  145.   width, height = gpu.getResolution()
  146.   term.setCursor((width-string.len(text))/2+1, y)
  147.   term.write(text)
  148. end
  149.  
  150. function addTextCentered(y,str,FGCol,BGCol)
  151.   gpu.setForeground(FGCol)
  152.   gpu.setBackground(BGCol)
  153.   center(y,tostring(str))
  154. end
  155.  
  156. function screen()
  157. --addBar(x1,x2,y1,y2,borderSize,barColFilled,barColEmpty,borderCol,textCol,filledp,text)
  158.   addBar(2,5,15,height-1,2,colors.green,colors.black,colors.white,barTextCol,satp,"Saturation")
  159.   addBar(width-33,5,width-19,height-1,2,colors.red,colors.black,colors.white,barTextCol,tempp,"Temperature")
  160.   addBar(20,5,34,height-1,2,colors.blue,colors.black,colors.white,barTextCol,shieldp,"Containment")
  161.   addBar(width-14,5,width-1,height-1,2,colors.orange,colors.black,colors.white,barTextCol,100-convp,"Conversion")
  162.   addTextCentered(6,"  " .. tostring(comma_value(tostring(gen)).." RF/t Generated  "),colors.white,colors.gray)
  163.   addTextCentered(7,"  " .. tostring(comma_value(tostring(flow)).." RF/t Flowrate  "),colors.white,colors.gray)
  164. end
  165.  
  166. GUI()
  167. start()
  168.  
  169. repeat
  170.   update()
  171.   screen()
  172.   if status == "warming_up" then
  173.     component.draconic_reactor.activateReactor()
  174.   elseif status == "stopping" or status == "cooling" or status == "cold" then
  175.     throwError("Reactor shut down due to outside sources.")
  176.   else
  177.     if sat and maxSat then
  178.       if gen+5000>flow and satp>25 then
  179.         flow = flow+10000
  180.       elseif gen<flow and satp<10 then
  181.         flow = math.floor((gen-5000)/10000)*10000
  182.       end
  183.       gate.setSignalLowFlow(flow)
  184.     end
  185.     if convp>95.00 and status == "running" then
  186.       shutdown()
  187.     end
  188.     if temp > 7500 and status == "running" then
  189.       shutdown()
  190.     end
  191.     if shield < 10 and status == "running" then
  192.       shutdown()
  193.     end
  194.   end
  195.   os.sleep(1)  
  196. until false
Advertisement
Add Comment
Please, Sign In to add comment