Advertisement
chronicrv

Reactor Control ComputerCraft

Sep 17th, 2020 (edited)
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 23.45 KB | None | 0 0
  1. -- ****************************************************** --
  2. -- ****************************************************** --
  3. -- **             Reactor Madness Program              ** --
  4. -- **                 for Big Reactor                  ** --
  5. -- **                Written by krakaen                ** --
  6. -- **                  Video Tutorial                  ** --
  7. -- **   https://www.youtube.com/watch?v=SbbT7ncyS2M    ** --
  8. -- ****************************************************** --
  9. -- ****************************************************** --
  10.  
  11. -- ******** Version 0.1 Changelog (02/16/2016) ********
  12. -- Changed currentRFTotal to 1 (makes power show at start)
  13.  
  14. -- ******** Version 0.2 Changelog (02/16/2016) ********
  15. -- Added fuel usage (mb/t)
  16. -- Added function round
  17. -- Added function comma_value
  18. -- Added function format_num
  19.  
  20. -- ******** Version 0.3 Changelog (02/17/2016) ********
  21. -- Change Rod looking for 0 instead of 1
  22.  
  23. -- ******** Version 0.4 Changelog (10/18/2016) ********
  24. -- Change rodLevel to do a Math.ceil instead of Math.floor
  25.  
  26. -- ******** Version 0.5 Changelog (03/01/2017) ********
  27. -- Change drawBoxed and drawFilledBox for drawLines with for loop
  28. -- to get compatibility with 1.60+
  29.  
  30. -- ******** Version 0.6 Changelog (03/22/2017) ********
  31. -- Added Custom error handling
  32. -- fixed typo in contorlsSize for controlsSize
  33.  
  34. -- ******** Version 0.7 Changelog (05/15/2018) ********
  35. -- Added new functions for exrteme reactor handling. Will work on both new and old versions
  36.  
  37. local VERISON = "NEW"
  38.  
  39. function checkVersionTooOld()
  40.   local monitorsCheck = {peripheral.find("monitor")}
  41. end
  42.  
  43. function checkVersionNEW()
  44.   local reactorsArray = {peripheral.find("BigReactors-Reactor")}
  45.   reactorsArray[0].getEnergyStats();
  46. end
  47.  
  48. function checkErrors()
  49.   if pcall(checkVersionTooOld) then
  50.  
  51.   else
  52.     error("The version of ComputerCraft is too old to use this program, sorry", 0)
  53.   end
  54.   local monitorsCheck = {peripheral.find("monitor")}
  55.   local reactorsCheck = {peripheral.find("BigReactors-Reactor")}
  56.  
  57.   if monitorsCheck[1] == nil then
  58.     error("The Monitor is not being detected, make sure the connections(modem) are activated", 0)
  59.   end
  60.  
  61.   if reactorsCheck[1] == nil then
  62.     error("The Reactor is not being detected, make sure the connections(modem) are activated. The problem could also be related to chunk protection on some public servers, ask an admin about it.", 0)
  63.   end
  64.  
  65.   if pcall(checkVersionNEW) then
  66.   VERISON = "NEW"
  67.   else
  68.   VERISON = "OLD"
  69.   end
  70. end
  71.  
  72. -- you need to give the index to be able to use the program
  73. -- ex : NameOfProgram Index   (reactor Krakaen)
  74.  
  75. local args = { ... }
  76.  
  77. local button = {}
  78. local filleds = {}
  79. local boxes = {}
  80. local lines = {}
  81. local texts = {}
  82.  
  83. local refresh = true
  84.  
  85. local infosSize = {}
  86. local controlsSize = {}
  87. local numbersSize = {}
  88. local currentRfTotal = 1
  89. local currentRfTick = 1
  90. local currentFuelConsumedLastTick = 0.00001
  91.  
  92.  
  93. local rfPerTickMax = 1
  94. local minPowerRod = 0
  95. local maxPowerRod = 100
  96. local currentRodLevel = 1
  97.  
  98. local index = ""
  99.  
  100. checkErrors() -- verify that everything is okay to start the program
  101.  
  102. if (#args == 0) then
  103.     error("No index Given, Make sure to start the 'start' program and not the 'reactor' program", 0)
  104. end
  105.  
  106. if (#args == 1) then
  107.   index = args[1]
  108. end
  109.  
  110. local monitors = {peripheral.find("monitor")}
  111. local mon = monitors[1]
  112. local reactors = {peripheral.find("BigReactors-Reactor")}
  113. local energyCore = peripheral.find("draconic_rf_storage")
  114.  
  115. -- use the black thingy sponge to clear the chalkboard
  116.  
  117. term.redirect(mon)
  118. mon.clear()
  119. mon.setTextColor(colors.white)
  120. mon.setBackgroundColor(colors.black)
  121.  
  122.  
  123. function clearTable()
  124.   button = {}
  125. end
  126.  
  127.  
  128.  
  129. -- All the things that make my buttons work
  130.  
  131. function setButton(name, title, func, xmin, ymin, xmax, ymax, elem, elem2, color)
  132.   button[name] = {}
  133.   button[name]["title"] = title
  134.     button[name]["func"] = func
  135.     button[name]["active"] = false
  136.     button[name]["xmin"] = xmin
  137.     button[name]["ymin"] = ymin
  138.     button[name]["xmax"] = xmax
  139.     button[name]["ymax"] = ymax
  140.     button[name]["color"] = color
  141.     button[name]["elem"] = elem
  142.     button[name]["elem2"] = elem2
  143. end
  144.  
  145. -- stuff and things for buttons
  146.  
  147. function fill(text, color, bData)
  148.    mon.setBackgroundColor(color)
  149.    mon.setTextColor(colors.white)
  150.    local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
  151.    local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(bData["title"])) /2) +1
  152.    for j = bData["ymin"], bData["ymax"] do
  153.       mon.setCursorPos(bData["xmin"], j)
  154.       if j == yspot then
  155.          for k = 0, bData["xmax"] - bData["xmin"] - string.len(bData["title"]) +1 do
  156.             if k == xspot then
  157.                mon.write(bData["title"])
  158.             else
  159.                mon.write(" ")
  160.             end
  161.          end
  162.       else
  163.          for i = bData["xmin"], bData["xmax"] do
  164.             mon.write(" ")
  165.          end
  166.       end
  167.    end
  168.    mon.setBackgroundColor(colors.black)
  169. end
  170.  
  171. -- stuff and things for buttons
  172.  
  173. function screen()
  174.    local currColor
  175.    for name,data in pairs(button) do
  176.       local on = data["active"]
  177.       currColor = data["color"]
  178.       fill(name, currColor, data)
  179.    end
  180. end
  181.  
  182. -- stuff and things for buttons
  183.  
  184. function flash(name)
  185.  
  186.   screen()
  187. end
  188.  
  189. -- magical handler for clicky clicks
  190.  
  191. function checkxy(x, y)
  192.    for name, data in pairs(button) do
  193.       if y>=data["ymin"] and  y <= data["ymax"] then
  194.          if x>=data["xmin"] and x<= data["xmax"] then
  195.             data["func"](data["elem"], data["elem2"])
  196.             flash(data['name'])
  197.             return true
  198.             --data["active"] = not data["active"]
  199.             --print(name)
  200.          end
  201.       end
  202.    end
  203.    return false
  204. end
  205.  
  206. -- Don't question my code, it works on my machine...
  207.  
  208. function label(w, h, text)
  209.    mon.setCursorPos(w, h)
  210.    mon.write(text)
  211. end
  212.  
  213. -- Draw function : put's all the beautiful magic in the screen
  214.  
  215. function draw()
  216.  
  217.   for key,value in pairs(filleds) do
  218.     local counter = 1
  219.     for i=value[2],value[4],1 do
  220.       paintutils.drawLine(value[1] , value[2]+counter, value[3], value[2]+counter, value[5])
  221.       counter = counter + 1
  222.     end
  223.   end
  224.  
  225.   for key,value in pairs(boxes) do
  226.     paintutils.drawLine(value[1] , value[2], value[1], value[4], value[5])
  227.     paintutils.drawLine(value[1] , value[2], value[3], value[2], value[5])
  228.     paintutils.drawLine(value[1] , value[4], value[3], value[4], value[5])
  229.     paintutils.drawLine(value[3] , value[2], value[3], value[4], value[5])
  230.   end
  231.  
  232.   for key,value in pairs(lines) do
  233.     paintutils.drawLine(value[1] , value[2], value[3], value[4], value[5])
  234.   end
  235.  
  236.   for key,value in pairs(texts) do
  237.     mon.setCursorPos(value[1], value[2])
  238.     mon.setTextColor(value[4])
  239.     mon.setBackgroundColor(value[5])
  240.     mon.write(value[3])
  241.   end
  242.   screen()
  243.   resetDraw()
  244. end
  245.  
  246. -- Resets the elements to draw to only draw the neccessity
  247.  
  248. function resetDraw()
  249.   filleds = {}
  250.   boxes = {}
  251.   lines = {}
  252.   texts = {}
  253. end
  254.  
  255. -- Handles all the clicks for the buttons
  256.  
  257. function clickEvent()
  258.   local myEvent={os.pullEvent("monitor_touch")}
  259.   checkxy(myEvent[3], myEvent[4])
  260. end
  261.  
  262. -- Power up the reactor (M&N are a good source of food right?)
  263.  
  264. function powerUp(m,n)
  265.   local reactor = reactors[1]
  266.   reactor.setActive(true)
  267. end
  268.  
  269. -- Power down the reactor (M&N are a good source of food right?)
  270.  
  271. function powerDown(m,n)
  272.   local reactor = reactors[1]
  273.   reactor.setActive(false)
  274. end
  275.  
  276. -- Handles and calculate the Min and Max of the power limitation
  277.  
  278. function modifyRods(limit, number)
  279.   local tempLevel = 0
  280.  
  281.   if limit == "min" then
  282.     tempLevel = minPowerRod + number
  283.     if tempLevel <= 0 then
  284.       minPowerRod = 0
  285.     end
  286.  
  287.     if tempLevel >= maxPowerRod then
  288.       minPowerRod = maxPowerRod -10
  289.     end
  290.  
  291.     if tempLevel < maxPowerRod and tempLevel > 0 then
  292.       minPowerRod = tempLevel
  293.     end
  294.   else
  295.     tempLevel = maxPowerRod + number
  296.     if tempLevel <= minPowerRod then
  297.       maxPowerRod = minPowerRod +10
  298.     end
  299.  
  300.     if tempLevel >= 100 then
  301.       maxPowerRod = 100
  302.     end
  303.  
  304.     if tempLevel > minPowerRod and tempLevel < 100 then
  305.       maxPowerRod = tempLevel
  306.     end
  307.   end
  308.  
  309.   table.insert(lines, {controlsSize['inX'], controlsSize['inY'] +(controlsSize['sectionHeight']*1)+4, controlsSize['inX'] + controlsSize['width'], controlsSize['inY']+(controlsSize['sectionHeight']*1)+4, colors.black})
  310.  
  311.   table.insert(texts, {controlsSize['inX']+5, controlsSize['inY'] +(controlsSize['sectionHeight']*1)+4, minPowerRod .. '%', colors.lightBlue, colors.black})
  312.   table.insert(texts, {controlsSize['inX']+13, controlsSize['inY'] +(controlsSize['sectionHeight']*1)+4, '--', colors.white, colors.black})
  313.   table.insert(texts, {controlsSize['inX']+20, controlsSize['inY'] +(controlsSize['sectionHeight']*1)+4, maxPowerRod .. '%', colors.purple, colors.black})
  314.  
  315.   setInfoToFile()
  316.   adjustRodsLevel()
  317. end
  318.  
  319. -- Calculate and adjusts the level of the rods
  320.  
  321. function adjustRodsLevel()
  322.   local reactor = reactors[1]
  323.   local rfTotalMax = energyCore.getMaxEnergyStored()
  324.  
  325.   local allStats = getAllStats()
  326.   local currentRfTotal = allStats["rfTotal"]
  327.   local reactorRodsLevel = allStats["reactorRodsLevel"]
  328.  
  329.   differenceMinMax = maxPowerRod - minPowerRod
  330.  
  331.   maxPower = (rfTotalMax/100) * maxPowerRod
  332.   minPower = (rfTotalMax/100) * minPowerRod
  333.  
  334.   if currentRfTotal >= maxPower then
  335.     currentRfTotal = maxPower
  336.   end
  337.  
  338.   if currentRfTotal <= minPower then
  339.     currentRfTotal = minPower
  340.   end
  341.  
  342.   currentRfTotal = currentRfTotal - (rfTotalMax/100) * minPowerRod
  343.  
  344.   local rfInBetween = (rfTotalMax/100) * differenceMinMax
  345.   local rodLevel = math.ceil((currentRfTotal/rfInBetween)*100)
  346.  
  347.   if VERSION == "NEW" then
  348.     if reactor.mbIsConnected() == true and reactor.mbIsAssembled() == true then
  349.       for key,value in pairs(reactorRodsLevel) do
  350.         reactorRodsLevel[key] = rodLevel
  351.       end
  352.       reactor.setControlRodsLevels(reactorRodsLevel)
  353.     end
  354.   else
  355.     reactor.setAllControlRodLevels(rodLevel)
  356.   end
  357. end
  358.  
  359. -- Creates the frame and the basic of the visual
  360. -- Also adds the variables informations for placement of stuff and things
  361.  
  362. function addDrawBoxesSingleReactor()
  363.   local w, h = mon.getSize()
  364.   local margin = math.floor((w/100)*2)
  365.  
  366.   infosSize['startX'] = margin + 1
  367.   infosSize['startY'] =  margin + 1
  368.   infosSize['endX'] = (((w-(margin*2))/3)*2)-margin
  369.   infosSize['endY'] = h - margin
  370.   infosSize['height'] = infosSize['endY']-infosSize['startY']-(margin*2)-2
  371.   infosSize['width'] = infosSize['endX']-infosSize['startX']-(margin*2)-2
  372.   infosSize['inX'] = infosSize['startX'] + margin +1
  373.   infosSize['inY'] = infosSize['startY'] + margin +1
  374.   infosSize['sectionHeight'] = math.floor(infosSize['height']/3)
  375.  
  376.   table.insert(boxes, {infosSize['startX'] , infosSize['startY'], infosSize['endX'], infosSize['endY'], colors.gray})
  377.   local name = "INFOS"
  378.   table.insert(lines, {infosSize['startX'] + margin , infosSize['startY'], infosSize['startX'] + (margin*2) + #name+1, infosSize['startY'], colors.black})
  379.   table.insert(texts, {infosSize['startX'] + (margin*2), infosSize['startY'], name, colors.white, colors.black})
  380.  
  381.   local names = {}
  382.   names[1] = 'ENERGY LAST TICK'
  383.   names[2] = 'ENERGY STORED'
  384.   names[3] = 'CONTROL ROD LEVEL'
  385.  
  386.   for i=0,2,1 do
  387.     table.insert(texts, {infosSize['inX'] + margin, infosSize['inY'] + (infosSize['sectionHeight']*i) +i, names[i+1], colors.white, colors.black})
  388.     table.insert(filleds, {infosSize['inX'] , infosSize['inY'] + 2 + (infosSize['sectionHeight']*i) +i, infosSize['inX'] + infosSize['width'], infosSize['inY'] + (infosSize['sectionHeight']*(i+1))-2 +i, colors.lightGray})
  389.   end
  390.  
  391.  
  392.   -- Controls
  393.  
  394.   controlsSize['startX'] = infosSize['endX'] + margin + 1
  395.   controlsSize['startY'] =  margin + 1
  396.   controlsSize['endX'] = w-margin
  397.   controlsSize['endY'] = (((h - (margin*2))/3)*2) +1
  398.   controlsSize['height'] = controlsSize['endY']-controlsSize['startY']-(margin)-1
  399.   controlsSize['width'] = controlsSize['endX']-controlsSize['startX']-(margin*2)-2
  400.   controlsSize['inX'] = controlsSize['startX'] + margin +1
  401.   controlsSize['inY'] = controlsSize['startY'] + margin
  402.  
  403.   table.insert(boxes, {controlsSize['startX'] , controlsSize['startY'], controlsSize['endX'], controlsSize['endY'], colors.gray})
  404.   name = "CONTROLS"
  405.   table.insert(lines, {controlsSize['startX'] + margin , controlsSize['startY'], controlsSize['startX'] + (margin*2) + #name+1, controlsSize['startY'], colors.black})
  406.   table.insert(texts, {controlsSize['startX'] + (margin*2), controlsSize['startY'], name, colors.white, colors.black})
  407.  
  408.   controlsSize['sectionHeight'] = math.floor(controlsSize['height']/4)
  409.  
  410.   reactor = reactors[1]
  411.  
  412.   mon.setTextColor(colors.white)
  413.   setButton("ON","ON", powerUp, controlsSize['inX'], controlsSize['inY']+1, controlsSize['inX'] + math.floor(controlsSize['width']/2) -1, controlsSize['inY'] +3, 0, 0, colors.green)
  414.   setButton("OFF","OFF", powerDown, controlsSize['inX'] + math.floor(controlsSize['width']/2) +2, controlsSize['inY']+1, controlsSize['inX'] + controlsSize['width'], controlsSize['inY'] +3,0, 0, colors.red)
  415.  
  416.   table.insert(texts, {controlsSize['inX']+6, controlsSize['inY'] +(controlsSize['sectionHeight']*1)+1, 'AUTO-CONTROL', colors.white, colors.black})
  417.  
  418.   table.insert(texts, {controlsSize['inX']+4, controlsSize['inY'] +(controlsSize['sectionHeight']*1)+3, 'MIN', colors.lightBlue, colors.black})
  419.   table.insert(texts, {controlsSize['inX']+4, controlsSize['inY'] +(controlsSize['sectionHeight']*1)+4, minPowerRod..'%', colors.lightBlue, colors.black})
  420.  
  421.   table.insert(texts, {controlsSize['inX']+10, controlsSize['inY'] +(controlsSize['sectionHeight']*1)+4, '--', colors.white, colors.black})
  422.   table.insert(texts, {controlsSize['inX']+15, controlsSize['inY'] +(controlsSize['sectionHeight']*1)+3, 'MAX', colors.purple, colors.black})
  423.   table.insert(texts, {controlsSize['inX']+15, controlsSize['inY'] +(controlsSize['sectionHeight']*1)+4, maxPowerRod..'%', colors.purple, colors.black})
  424.   mon.setTextColor(colors.white)
  425.  
  426.   setButton("low-10","-10", modifyRods, controlsSize['inX'], controlsSize['inY'] +(controlsSize['sectionHeight']*2)+2, controlsSize['inX'] + math.floor(controlsSize['width']/2) -1, controlsSize['inY'] +(controlsSize['sectionHeight']*2)+4, "min", -10, colors.lightBlue)
  427.   setButton("high-10","-10", modifyRods, controlsSize['inX'] + math.floor(controlsSize['width']/2) +2, controlsSize['inY'] +(controlsSize['sectionHeight']*2)+2, controlsSize['inX'] + controlsSize['width'], controlsSize['inY'] +(controlsSize['sectionHeight']*2)+4, "max", -10, colors.purple)
  428.  
  429.   setButton("low+10","+10", modifyRods, controlsSize['inX'], controlsSize['inY'] +(controlsSize['sectionHeight']*3)+2, controlsSize['inX'] + math.floor(controlsSize['width']/2) -1, controlsSize['inY'] +(controlsSize['sectionHeight']*3)+4, "min", 10, colors.lightBlue)
  430.   setButton("high+10","+10", modifyRods, controlsSize['inX'] + math.floor(controlsSize['width']/2) +2, controlsSize['inY'] +(controlsSize['sectionHeight']*3)+2, controlsSize['inX'] + controlsSize['width'], controlsSize['inY'] +(controlsSize['sectionHeight']*3)+4, "max", 10, colors.purple)
  431.  
  432.   -- Numbers
  433.  
  434.   numbersSize['startX'] = infosSize['endX'] + margin + 1
  435.   numbersSize['startY'] = controlsSize['endY'] + margin + 1
  436.   numbersSize['endX'] = w-margin
  437.   numbersSize['endY'] = h-margin
  438.   numbersSize['height'] = numbersSize['endY']-numbersSize['startY']-(margin)-1
  439.   numbersSize['width'] = numbersSize['endX']-numbersSize['startX']-(margin*2)-2
  440.   numbersSize['inX'] = numbersSize['startX'] + margin +1
  441.   numbersSize['inY'] = numbersSize['startY'] + margin +1
  442.  
  443.   table.insert(boxes, {numbersSize['startX'] , numbersSize['startY'], numbersSize['endX'], numbersSize['endY'], colors.gray})
  444.   name = "NUMBERS"
  445.   table.insert(lines, {numbersSize['startX'] + margin , numbersSize['startY'], numbersSize['startX'] + (margin*2) + #name+1, numbersSize['startY'], colors.black})
  446.   table.insert(texts, {numbersSize['startX'] + (margin*2), numbersSize['startY'], name, colors.white, colors.black})
  447.  
  448.   refresh = true
  449.   while refresh do
  450.     parallel.waitForAny(refreshSingleReactor,clickEvent)
  451.   end
  452. end
  453.  
  454. -- Makes and Handles the draw function for less lag in the visual
  455.  
  456. function getAllStats()
  457.   local stats = {}
  458.   local reactor = reactors[1]
  459.  
  460.   if VERSION == "NEW" then
  461.     if reactor.mbIsConnected() == true and reactor.mbIsAssembled() == true then
  462.       local reactorEnergyStats = reactor.getEnergyStats()
  463.       local reactorFuelStats = reactor.getFuelStats()
  464.       stats["reactorRodsLevel"] = reactor.getControlRodsLevels()
  465.  
  466.       stats["rfTotal"] = energyCore.getEnergyStored()
  467.       stats["rfPerTick"] = math.ceil(reactorEnergyStats["energyProducedLastTick"])
  468.       stats["rodLevel"] = stats["reactorRodsLevel"][0]
  469.       stats["fuelPerTick"] = round(reactorFuelStats["fuelConsumedLastTick"], 2)
  470.     end
  471.   else
  472.     stats["rfTotal"] = energyCore.getEnergyStored()
  473.     stats["rfPerTick"] = math.floor(reactor.getEnergyProducedLastTick())
  474.     stats["rodLevel"] = math.floor(reactor.getControlRodLevel(0))
  475.     stats["fuelPerTick"] = reactor.getFuelConsumedLastTick()
  476.   end
  477.  
  478.   return stats
  479. end
  480.  
  481. function refreshSingleReactor()
  482.   local rfPerTick = 0
  483.   local rfTotal = 0
  484.   local rfTotalMax = energyCore.getMaxEnergyStored()
  485.   local reactor = reactors[1]
  486.  
  487.   local allStats = getAllStats()
  488.   rfTotal = allStats["rfTotal"]
  489.   rfPerTick = allStats["rfPerTick"]
  490.   rodLevel = allStats["rodLevel"]
  491.   fuelPerTick = allStats["fuelPerTick"]
  492.  
  493.   local i = 0
  494.   local infotoAdd = 'RF PER TICK : '
  495.  
  496.   if currentRfTick ~= rfPerTick then
  497.     currentRfTick = rfPerTick
  498.     if rfPerTick > rfPerTickMax then
  499.       rfPerTickMax = rfPerTick
  500.     end
  501.  
  502.     table.insert(lines, {numbersSize['inX'] , numbersSize['inY'],numbersSize['inX'] + numbersSize['width'] , numbersSize['inY'], colors.black})
  503.     table.insert(texts, {numbersSize['inX'], numbersSize['inY'], infotoAdd .. rfPerTick .. " RF", colors.white, colors.black})
  504.     table.insert(filleds, {infosSize['inX'] , infosSize['inY'] + 1 + (infosSize['sectionHeight']*i) +i, infosSize['inX'] + infosSize['width'], infosSize['inY'] + (infosSize['sectionHeight']*(i+1))-2 +i, colors.lightGray})
  505.     width = math.floor((infosSize['width'] / rfPerTickMax)*rfPerTick)
  506.     table.insert(filleds, {infosSize['inX'] , infosSize['inY'] + 1 + (infosSize['sectionHeight']*i) +i, infosSize['inX'] + width, infosSize['inY'] + (infosSize['sectionHeight']*(i+1))-2 +i, colors.green})
  507.  
  508.   end
  509.  
  510.   i = 1
  511.   infotoAdd = 'RF : '
  512.   if currentRfTotal ~= rfTotal then
  513.     currentRfTotal = rfTotal
  514.  
  515.     table.insert(filleds, {infosSize['inX'] , infosSize['inY'] + 1 + (infosSize['sectionHeight']*i) +i, infosSize['inX'] + infosSize['width'], infosSize['inY'] + (infosSize['sectionHeight']*(i+1))-2 +i, colors.lightGray})
  516.  
  517.     width = math.floor((infosSize['width'] / rfTotalMax)*rfTotal)
  518.     table.insert(filleds, {infosSize['inX'] , infosSize['inY'] + 1 + (infosSize['sectionHeight']*i) +i, infosSize['inX'] + width, infosSize['inY'] + (infosSize['sectionHeight']*(i+1))-2 +i, colors.green})
  519.     table.insert(lines, {numbersSize['inX'] , numbersSize['inY'] +2 ,numbersSize['inX'] + numbersSize['width'] , numbersSize['inY'] +2, colors.black})
  520.     table.insert(texts, {numbersSize['inX'], numbersSize['inY']+ 2 , infotoAdd .. rfTotal .. " RF", colors.white, colors.black})
  521.   end
  522.  
  523.   i = 2
  524.   infotoAdd = 'CONTROL ROD LEVEL : '
  525.   if currentRodLevel ~= rodLevel then
  526.     currentRodLevel = rodLevel
  527.  
  528.     table.insert(filleds, {infosSize['inX'] , infosSize['inY'] + 1 + (infosSize['sectionHeight']*i) +i, infosSize['inX'] + infosSize['width'], infosSize['inY'] + (infosSize['sectionHeight']*(i+1))-2 +i, colors.lightGray})
  529.  
  530.     width = math.floor((infosSize['width'] / 100)*rodLevel)
  531.     table.insert(filleds, {infosSize['inX'] , infosSize['inY'] + 1 + (infosSize['sectionHeight']*i) +i, infosSize['inX'] + width, infosSize['inY'] + (infosSize['sectionHeight']*(i+1))-2 +i, colors.green})
  532.     table.insert(lines, {numbersSize['inX'] , numbersSize['inY']+4 ,numbersSize['inX'] + numbersSize['width'] , numbersSize['inY'] +4, colors.black})
  533.     table.insert(texts, {numbersSize['inX'], numbersSize['inY']+ 4 , infotoAdd .. rodLevel .. "%", colors.white, colors.black})
  534.   end
  535.  
  536.   i = 3
  537.   infotoAdd = 'FUEL USAGE : '
  538.   if currentFuelConsumedLastTick ~= fuelPerTick then
  539.     currentFuelConsumedLastTick = fuelPerTick
  540.  
  541.     table.insert(lines, {numbersSize['inX'] , numbersSize['inY']+6 ,numbersSize['inX'] + numbersSize['width'] , numbersSize['inY'] +6, colors.black})
  542.     table.insert(texts, {numbersSize['inX'], numbersSize['inY']+ 6 , infotoAdd .. format_num(tonumber(fuelPerTick),3) .. "mb/t", colors.white, colors.black})
  543.   end
  544.  
  545.   mon.setTextColor(colors.white)
  546.   adjustRodsLevel()
  547.  
  548.   draw()
  549.  
  550.   sleep(2)
  551. end
  552.  
  553. --
  554. -- ** Get the informations from the index file
  555. -- line 1 = min ROD
  556. -- line 2 = max ROD
  557. --
  558.  
  559. function getInfoFromFile()
  560.  
  561.    if (fs.exists(index..".txt") == false) then
  562.     file = io.open(index..".txt","w")
  563.       file:write("0")
  564.       file:write("\n")
  565.       file:write("100")
  566.       file:close()
  567.   else
  568.     file = fs.open(index..".txt","r")
  569.     minPowerRod = tonumber(file.readLine())
  570.     maxPowerRod = tonumber(file.readLine())
  571.       file.close()
  572.   end
  573. end
  574.  
  575. -- Save informations to the index file
  576.  
  577. function setInfoToFile()
  578.   file = io.open(index..".txt","w")
  579.   file:write(minPowerRod .. "\n" .. maxPowerRod)
  580.     file:flush()
  581.     file:close()
  582. end
  583.  
  584. ---============================================================
  585. -- add comma to separate thousands
  586. -- From Lua-users.org/wiki/FormattingNumbers
  587. --
  588. --
  589. function comma_value(amount)
  590.   local formatted = amount
  591.   while true do
  592.     formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
  593.     if (k==0) then
  594.       break
  595.     end
  596.   end
  597.   return formatted
  598. end
  599.  
  600. ---============================================================
  601. -- rounds a number to the nearest decimal places
  602. -- From Lua-users.org/wiki/FormattingNumbers
  603. --
  604. --
  605. function round(val, decimal)
  606.   if (decimal) then
  607.     return math.floor( (val * 10^decimal) + 0.5) / (10^decimal)
  608.   else
  609.     return math.floor(val+0.5)
  610.   end
  611. end
  612.  
  613. --===================================================================
  614. -- given a numeric value formats output with comma to separate thousands
  615. -- and rounded to given decimal places
  616. -- From Lua-users.org/wiki/FormattingNumbers
  617. --
  618. function format_num(amount, decimal, prefix, neg_prefix)
  619.   local str_amount,  formatted, famount, remain
  620.  
  621.   decimal = decimal or 2  -- default 2 decimal places
  622.   neg_prefix = neg_prefix or "-" -- default negative sign
  623.  
  624.   famount = math.abs(round(amount,decimal))
  625.   famount = math.floor(famount)
  626.  
  627.   remain = round(math.abs(amount) - famount, decimal)
  628.  
  629.         -- comma to separate the thousands
  630.   formatted = comma_value(famount)
  631.  
  632.         -- attach the decimal portion
  633.   if (decimal > 0) then
  634.     remain = string.sub(tostring(remain),3)
  635.     formatted = formatted .. "." .. remain ..
  636.                 string.rep("0", decimal - string.len(remain))
  637.   end
  638.  
  639.         -- attach prefix string e.g '$'
  640.   formatted = (prefix or "") .. formatted
  641.  
  642.         -- if value is negative then format accordingly
  643.   if (amount<0) then
  644.     if (neg_prefix=="()") then
  645.       formatted = "("..formatted ..")"
  646.     else
  647.       formatted = neg_prefix .. formatted
  648.     end
  649.   end
  650.  
  651.   return formatted
  652. end
  653.  
  654. -- Clear and make the pixel smaller because we are not blind
  655.  
  656. mon.setBackgroundColor(colors.black)
  657. mon.clear()
  658. mon.setTextScale(0.5)
  659.  
  660. -- Get the information from the index file
  661. getInfoFromFile()
  662.  
  663.  
  664. -- Add's the visual and starts the Loop
  665. addDrawBoxesSingleReactor()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement