Advertisement
Plazter

OpenComputers Draconic Reactor

Apr 29th, 2017
1,062
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.31 KB | None | 0 0
  1. --[[
  2. //      MADE BY...: Plazter
  3. //      Date......: 29. April 2017
  4. //      Edited....: 18 April 2018
  5. //      Mod:......: OpenComputers
  6. //    Credit to.: Brain_Corbec for color table ( Note u can allways add more colors (hex code))
  7. //      Note: This program contains a debug tool, thats intented for you to use
  8. //            to figure out where to get your clicks, incase a button or something
  9. //            is not working correctly. if you use this program, i hope you will
  10. //            Enjoy it.
  11. --]]
  12. ------------------------------------------------------------------------------------------------------------------
  13.                                                  --[[ WRAPPINGS ]]--
  14. ------------------------------------------------------------------------------------------------------------------
  15. component = require("component")
  16. reactor = component.draconic_reactor
  17. FluxGateOutput = component.proxy(component.get("7a7511a5-e91d-40cf-ae46-ee387377ae71")) -- Output Flux gate
  18. FluxGateShield = component.proxy(component.get("86ff77aa-b209-4e95-8b3e-cd33e21289a8")) -- Input flux gate
  19. term = require("term")
  20. gpu = component.gpu
  21. event = require("event")
  22. keyboard = require("keyboard")
  23. screen = component.screen
  24. ------------------------------------------------------------------------------------------------------------------
  25.                                                  --[[  TABLES  ]]--
  26. ------------------------------------------------------------------------------------------------------------------
  27.  
  28. args = {...}
  29.  
  30. colors = { black = 0x000000, white = 0xf8f8ff, blue = 0x0000ff, lightGray = 0xd9d9d9, red = 0xff0000,
  31. purple = 0x9b30ff, carrot = 0xffa500, magenta = 0xcd00cd, lightBlue = 0x87cefa, yellow = 0xffff00,
  32. lime = 0x32cd32, pink = 0xffc0cb, gray = 0x696969, brown = 0x8b4500, green = 0x006400, cyan = 0x008b8b,
  33. olive = 0x6b8e23, gold = 0x8b6914, orangered = 0xdb4e02, diamond = 0x0fa7c7,crimson = 0xaf002a,fuchsia = 0xfd3f92,
  34. folly = 0xff004f, frenchBlue = 0x0072bb, lilac = 0x86608e, flax = 0xeedc82, darkGray = 0x563c5c,
  35. englishGreen = 0x1b4d3e, eggplant = 0x614051, deepPink  = 0xff1493, ruby = 0x843f5b, orange = 0xf5c71a,
  36. lemon = 0xffd300, darkBlue = 0x002e63, bitterLime = 0xbfff00 }
  37. ------------------------------------------------------------------------------------------------------------------
  38.                                                  --[[ VARIABLES ]]--
  39. ------------------------------------------------------------------------------------------------------------------
  40. Border_bg = colors.white
  41. Default_bg = colors.gray
  42. text_col = colors.white
  43. status_col = colors.black
  44. failsafe = true
  45. failSafeTemp = 6400
  46. auto = failSafeTemp
  47. startt = nil
  48. ------------------------------------------------------------------------------------------------------------------
  49.                                                  --[[ FUNCTIONS ]]--
  50. ------------------------------------------------------------------------------------------------------------------
  51. function guiBorders(x,y,len,height,str) -- BORDER FUNC FOR GUI
  52.   gpu.setBackground(Border_bg)
  53.   gpu.fill(x,y,len,height,str)
  54.   gpu.setBackground(Default_bg)
  55. end
  56.  
  57. function GUI() -- SETS THE GUI LAYOUT (GRAPHICAL USER INTERFACE)
  58.   gpu.setBackground(Default_bg)
  59.   term.clear()
  60.   w,  h = gpu.getResolution()
  61.   guiBorders(1,1,w,1," ")
  62.     for i = 1,h do
  63.       --guiBorders(1,i,1,1," ")
  64.       --guiBorders(w,i,1,1," ")
  65.     end
  66.   guiBorders(1,h,w,1," ")
  67.   gpu.setForeground(text_col)
  68. end
  69.  
  70. function Center(y,text) -- CENTERS TEXT  
  71.   w, h = gpu.getResolution()
  72.   term.setCursor((w-string.len(text))/2+1, y)
  73.   term.write(text)
  74. end
  75.  
  76. function info(title,x,y) -- Rewriting of gpu.set
  77.   gpu.set(x,y,title)
  78. end
  79.  
  80. function InfoUpdate(y, text) -- Text for function UPDATE
  81.   w, h = gpu.getResolution()
  82.       place = (w-string.len(text))-2
  83.     gpu.set(place, y, text)
  84. end
  85.  
  86. function display() -- Text to load onto screen on launch
  87.   -- TITLE
  88.   Center(2, "{{  Draconic Reactor  }}")
  89.   -- Info Title
  90.   info("Temperature.....................................:",2,4)
  91.   info("Shield..........................................:",2,6)
  92.   info("Generation Rate.................................:", 2,8)
  93.   info("RF Output.......................................:", 2, 10)
  94.   info("RF Net gain.....................................:", 2,12)
  95.   info("Energy Saturation...............................:", 2, 14)
  96.   info("Fuel Conversion Rate............................:", 2,16)
  97.   -- Button Panel
  98.   Center(18, "Set Output")
  99.   Center(19, "<<< << < = > >> >>>")
  100.   Center(21, "Set State")
  101.   Center(22, "Charge - Online - Offline")
  102.   gpu.set(2,21,"Toggle Auto")
  103.   gpu.set(2,22,"On - Off")
  104. end
  105.  
  106. function com(n) -- credit http://richard.warburton.it
  107.   local left,num,right = string.match(n,'^([^%d]*%d)(%d*)(.-)$')
  108.   return left..(num:reverse():gsub('(%d%d%d)','%1.'):reverse())..right
  109. end
  110.  
  111. function UPDATE() -- The Information we want to keep update
  112.   --if reactorInfo.
  113.   InfoUpdate(5, "  "..string.format("%.2f",tostring(temperature)).." C") -- Temperature
  114.   InfoUpdate(7,"  "..string.format("%2.f",perc*100).." %   ") -- Shield %
  115.   InfoUpdate(9,"  "..com(tostring(gen)).." RF/T") -- Generating
  116.   InfoUpdate(11, "  "..com(tostring(FluxGateOutput.getFlow())).." RF/T") -- What the Current output is set to.
  117.   InfoUpdate(13,"  ".. com(tostring( gen  - FluxGateShield.getFlow())).." RF/T") -- The total gain in rf
  118.   if reactorInfo.energySaturation > 0 then
  119.   InfoUpdate(15, "  "..string.format("%.3f",tostring(reactorInfo.energySaturation / reactorInfo.maxEnergySaturation * 100)).." %   ") -- The    
  120.   end  
  121. --energy Saturation
  122.   InfoUpdate(17, tostring(reactorInfo.fuelConversionRate).." nB/T")  -- Fuel use
  123.     --if temperature >= 6000 then
  124.       --startt = true
  125.       --gpu.set(2,19,"Auto On")
  126.     --end
  127. end
  128.  
  129.  
  130.  
  131. function getPress(line) -- Reads where we press, and set it as we wishes it
  132.   if x ~= nil and y ~= nil then
  133.     if x >= 32 and x <= 37 and y == 22 then -- Online
  134.       reactor.activateReactor()
  135.       gpu.setBackground(colors.green)
  136.       gpu.set(32,22, "Online")
  137.       gpu.setBackground(Default_bg)
  138.       gpu.set(23,22, "Charge")
  139.       gpu.set(41,22,"Offline")
  140.     elseif x >= 23 and x <= 28 and y == 22 then -- Charge
  141.       reactor.chargeReactor()
  142.       gpu.setBackground(colors.blue)
  143.       gpu.set(23,22, "Charge")
  144.       gpu.setBackground(Default_bg)
  145.       gpu.set(32,22, "Online")
  146.       gpu.set(41,22, "Offline")
  147.     elseif x >= 41 and x <= 47 and y == 22 then --Ofline
  148.       reactor.stopReactor()
  149.       gpu.setBackground(colors.red)
  150.       gpu.set(41,22, "Offline")
  151.       gpu.setBackground(Default_bg)
  152.       gpu.set(23,22, "Charge")
  153.       gpu.set(32,22,"Online")
  154.     end
  155.  
  156.     if x >= 23 and x <= 29 and y == line then -- <<<
  157.       FluxGateOutput.setFlowOverride(currOut - 10000)
  158.     elseif x >= 30 and x <= 32 and y == line then -- <<
  159.       FluxGateOutput.setFlowOverride(currOut - 1000)
  160.     elseif x >= 33 and x <= 34 and y == line then -- <
  161.       FluxGateOutput.setFlowOverride(currOut - 100)
  162.     end
  163.  
  164.     if x >= 36 and x <= 37 and y == line then -- >
  165.       FluxGateOutput.setFlowOverride(currOut + 100)
  166.     elseif x >= 38 and x <= 41 and y == line then -- >>
  167.       FluxGateOutput.setFlowOverride(currOut + 1000)
  168.     elseif x >= 42 and x <= 46 and y == line then -- >>>
  169.       FluxGateOutput.setFlowOverride(currOut + 10000)
  170.     end
  171.   end
  172. end
  173.  
  174. function tog()
  175.   if x ~= nil and y ~= nil then    
  176.     if x >= 2 and x <= 3  and y == 22 then
  177.         startt = true
  178.         gpu.setBackground(Default_bg)
  179.         gpu.set(7,22,"Off")
  180.         gpu.setBackground(colors.green)
  181.         gpu.set(2,22,"On")
  182.         gpu.setBackground(Default_bg)
  183.      elseif x >= 6 and x <= 8 and y == 22 then
  184.         startt = false
  185.         gpu.setBackground(colors.red)
  186.         gpu.set(7,22,"Off")
  187.         gpu.setBackground(Default_bg)
  188.         gpu.set(2,22,"On")
  189.     end -- if x >=
  190.   end -- If x ~=
  191. end -- Function
  192.  
  193.  
  194. function FailSafe()
  195.   if failsafe then
  196.     if temperature > failSafeTemp then
  197.           reactor.stopReactor()
  198.           gpu.setBackground(colors.red)
  199.           gpu.set(41,22, "Offline")
  200.           gpu.setBackground(Default_bg)
  201.           gpu.set(23,22, "Charge")
  202.           gpu.set(32,22,"Online")
  203.           gpu.setBackground(colors.red)
  204.           Center(11,"FAIL SAFE MODE!")
  205.           gpu.setBackground(Default_bg)
  206.           fail = true
  207.           startt = false
  208.         --  failCount = failCount + 1
  209.     elseif fail and temperature < 5800 then
  210.           reactor.activateReactor()
  211.           gpu.setBackground(colors.lime)
  212.           gpu.set(32,22, "Online")
  213.           gpu.setBackground(Default_bg)
  214.           gpu.set(41,22,"Offline")
  215.           gpu.set(23,22,"Charge")
  216.           Center(11,"               ")
  217.           fail = false
  218.             if failCount == 50 then
  219.               failCount = 0
  220.             end
  221.     end
  222.   end
  223. end
  224.  
  225. -- TESTING
  226. function autoo() -- THIS FUNCTION NEEDS TO BE UPDATED FOR SAFE STARTUP
  227.   if startt == true then
  228.    if math.floor((reactorInfo.energySaturation/reactorInfo.maxEnergySaturation*100)) >= 25 and math.floor((reactorInfo.energySaturation / reactorInfo.maxEnergySaturation * 100)) <= 50 and  temperature <= 6000 then
  229.       FluxGateOutput.setFlowOverride(FluxGateOutput.getFlow()+150)
  230.    end
  231.   elseif startt == false then
  232.     --
  233.   end
  234. end
  235. ------------------------------------------------------------------------------------------------------------------
  236.                                               --[[ PROGRAM INITATION ]]--
  237. ------------------------------------------------------------------------------------------------------------------
  238.  
  239. gpu.setResolution(70,23)
  240. FluxGateShield.setOverrideEnabled(true)
  241. FluxGateShield.setFlowOverride(230000)
  242. FluxGateOutput.setOverrideEnabled(true)
  243. if FluxGateOutput.getFlow() >= 430000 then
  244.     --
  245.     else
  246.     FluxGateOutput.setFlowOverride(430000)
  247. end
  248.  
  249. screen.setTouchModeInverted(true)
  250. GUI() -- Loads screen layout
  251. display() -- Loads Text Layout
  252. ------------------------------------------------------------------------------------------------------------------
  253.                                                  --[[ MAIN LOOP ]]--
  254. ------------------------------------------------------------------------------------------------------------------
  255.  
  256. while true do
  257.     --UPDATING VARS THAT NEEDS TO BE HERE --
  258.     reactorInfo = reactor.getReactorInfo()  
  259.     target = 0.30
  260.     currShield = reactorInfo.fieldStrength
  261.     maxShield = reactorInfo.maxFieldStrength
  262.     perc = (currShield/maxShield)
  263.     shieldDrain = reactorInfo.fieldDrainRate
  264.     shieldStr = reactorInfo.fieldStrength
  265.     temperature = reactorInfo.temperature
  266.     currOut = FluxGateOutput.getFlow()
  267.     gen = reactorInfo.generationRate
  268.     -- FUNCTIONS TO KEEP LOOKING UP --    
  269.     UPDATE()
  270.     getPress(19)
  271.     FailSafe()
  272.     autoo()
  273.     --tog()
  274.     -- EVENT LISTENER
  275.     _,_,x,y = event.pull(1, "touch")    
  276.       tog()
  277.       -- DEBUG TOOL
  278.       if args[1] == "debug" then
  279.         if x ~= nil and y~= nil then
  280.           Center(16,"X: ".. x .." Y: "..y)
  281.         end
  282.       elseif args[1] == "test" then
  283.         startt = true
  284.       end
  285.       -- TERMINATE PROGRAM AND RESTORE RESOLUTION
  286.       if keyboard.isKeyDown(keyboard.keys.w) and keyboard.isControlDown() then
  287.           term.clear()
  288.           w,h = gpu.maxResolution()
  289.           gpu.setResolution(w,h)
  290.           screen.setTouchModeInverted(false)
  291.           os.exit()
  292.     end
  293.  
  294. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement