MMOAtlantian

Big Reactors + EnderIO capacitors + storage

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