Advertisement
GravityCube

ReactorControl for Tekkit

Jan 23rd, 2018
1,030
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- ****************************************************** --
  2. -- ****************************************************** --
  3. -- **             Reactor Madness Program              ** --
  4. -- **                 for Big Reactor                  ** --
  5. -- **                Written by krakaen                ** --
  6. -- **          Modified by GravityCube (Tekkit)        ** --
  7. -- **                  Video Tutorial                  ** --
  8. -- **   https://www.youtube.com/watch?v=SbbT7ncyS2M    ** --
  9. -- ****************************************************** --
  10. -- ****************************************************** --
  11.  
  12. -- ******** Version 0.1 Changelog (02/16/2016) ********
  13. -- Changed currentRFTotal to 1 (makes power show at start)
  14.  
  15. -- ******** Version 0.2 Changelog (02/16/2016) ********
  16. -- Added fuel usage (mb/t)
  17. -- Added function round
  18. -- Added function comma_value
  19. -- Added function format_num
  20.  
  21. -- ******** Version 0.3 Changelog (02/17/2016) ********
  22. -- Change Rod looking for 0 instead of 1
  23.  
  24. -- ******** Version 0.4 Changelog (10/18/2016) ********
  25. -- Change rodLevel to do a Math.ceil instead of Math.floor
  26.  
  27. -- ******** Version 0.5 Changelog (03/01/2017) ********
  28. -- Change drawBoxed and drawFilledBox for drawLines with for loop
  29. -- to get compatibility with 1.60+
  30.  
  31. -- ******** Version 0.6 Changelog (03/22/2017) ********
  32. -- Added Custom error handling
  33. -- fixed typo in contorlsSize for controlsSize
  34. function peripheral.find(pType)
  35.     for _,pName in pairs(peripheral.getNames()) do
  36.         if peripheral.getType(pName) == pType then
  37.             return peripheral.wrap(pName)
  38.         end
  39.     end
  40.     return nil
  41. end
  42. function checkErrors()
  43.     local monitorsCheck = {peripheral.find("monitor")}
  44.    
  45.     local reactorsCheck = {peripheral.find("BigReactors-Reactor")}
  46.  
  47.   if monitorsCheck[1] == nil then
  48.     error("The Monitor is not being detected, make sure the connections(modem) are activated", 0)
  49.   end
  50.  
  51.   if reactorsCheck[1] == nil then
  52.     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)
  53.   end
  54. end
  55.  
  56. -- you need to give the index to be able to use the program
  57. -- ex : NameOfProgram Index   (reactor Krakaen)
  58.  
  59. local args = { ... }
  60.  
  61. local button = {}
  62. local filleds = {}
  63. local boxes = {}
  64. local lines = {}
  65. local texts = {}
  66.  
  67. local refresh = true
  68.  
  69. local infosSize = {}
  70. local controlsSize = {}
  71. local numbersSize = {}
  72. local currentRfTotal = 1
  73. local currentRfTick = 1
  74. local currentFuelConsumedLastTick = 0.00001
  75.  
  76.  
  77. local rfPerTickMax = 1
  78. local minPowerRod = 0
  79. local maxPowerRod = 100
  80. local currentRodLevel = 1
  81.  
  82. local index = ""
  83.  
  84. checkErrors() -- verify that everything is okay to start the program
  85.  
  86. if (#args == 0) then
  87.       error("No index Given, Make sure to start the 'start' program and not the 'reactor' program", 0)
  88. end
  89.  
  90. if (#args == 1) then
  91.     index = args[1]
  92. end
  93.  
  94. local monitors = {peripheral.find("monitor")}
  95. local mon = monitors[1]
  96. local reactors = {peripheral.find("BigReactors-Reactor")}
  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.     local currentRfTotal = reactor.getEnergyStored()
  307.  
  308.     differenceMinMax = maxPowerRod - minPowerRod
  309.  
  310.     maxPower = (rfTotalMax/100) * maxPowerRod
  311.     minPower = (rfTotalMax/100) * minPowerRod
  312.  
  313.     if currentRfTotal >= maxPower then
  314.         currentRfTotal = maxPower
  315.     end
  316.  
  317.     if currentRfTotal <= minPower then
  318.         currentRfTotal = minPower
  319.     end
  320.  
  321.     currentRfTotal = currentRfTotal - (rfTotalMax/100) * minPowerRod
  322.  
  323.     local rfInBetween = (rfTotalMax/100) * differenceMinMax
  324.     local rodLevel = math.ceil((currentRfTotal/rfInBetween)*100)
  325.  
  326.     reactor.setAllControlRodLevels(rodLevel)
  327. end
  328.  
  329. -- Creates the frame and the basic of the visual
  330. -- Also adds the variables informations for placement of stuff and things
  331.  
  332. function addDrawBoxesSingleReactor()
  333.     local w, h = mon.getSize()
  334.     local margin = math.floor((w/100)*2)
  335.  
  336.     infosSize['startX'] = margin + 1
  337.     infosSize['startY'] =  margin + 1
  338.     infosSize['endX'] = (((w-(margin*2))/3)*2)-margin
  339.     infosSize['endY'] = h - margin
  340.     infosSize['height'] = infosSize['endY']-infosSize['startY']-(margin*2)-2
  341.     infosSize['width'] = infosSize['endX']-infosSize['startX']-(margin*2)-2
  342.     infosSize['inX'] = infosSize['startX'] + margin +1
  343.     infosSize['inY'] = infosSize['startY'] + margin +1
  344.     infosSize['sectionHeight'] = math.floor(infosSize['height']/3)
  345.  
  346.     table.insert(boxes, {infosSize['startX'] , infosSize['startY'], infosSize['endX'], infosSize['endY'], colors.gray})
  347.     local name = "INFOS"
  348.     table.insert(lines, {infosSize['startX'] + margin , infosSize['startY'], infosSize['startX'] + (margin*2) + #name+1, infosSize['startY'], colors.black})
  349.     table.insert(texts, {infosSize['startX'] + (margin*2), infosSize['startY'], name, colors.white, colors.black})
  350.  
  351.     local names = {}
  352.     names[1] = 'ENERGY LAST TICK'
  353.     names[2] = 'ENERGY STORED'
  354.     names[3] = 'CONTROL ROD LEVEL'
  355.  
  356.     for i=0,2,1 do
  357.         table.insert(texts, {infosSize['inX'] + margin, infosSize['inY'] + (infosSize['sectionHeight']*i) +i, names[i+1], colors.white, colors.black})
  358.         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})
  359.     end
  360.  
  361.  
  362.     -- Controls
  363.  
  364.     controlsSize['startX'] = infosSize['endX'] + margin + 1
  365.     controlsSize['startY'] =  margin + 1
  366.     controlsSize['endX'] = w-margin
  367.     controlsSize['endY'] = (((h - (margin*2))/3)*2) +1
  368.     controlsSize['height'] = controlsSize['endY']-controlsSize['startY']-(margin)-1
  369.     controlsSize['width'] = controlsSize['endX']-controlsSize['startX']-(margin*2)-2
  370.     controlsSize['inX'] = controlsSize['startX'] + margin +1
  371.     controlsSize['inY'] = controlsSize['startY'] + margin
  372.  
  373.     table.insert(boxes, {controlsSize['startX'] , controlsSize['startY'], controlsSize['endX'], controlsSize['endY'], colors.gray})
  374.     name = "CONTROLS"
  375.     table.insert(lines, {controlsSize['startX'] + margin , controlsSize['startY'], controlsSize['startX'] + (margin*2) + #name+1, controlsSize['startY'], colors.black})
  376.     table.insert(texts, {controlsSize['startX'] + (margin*2), controlsSize['startY'], name, colors.white, colors.black})
  377.  
  378.     controlsSize['sectionHeight'] = math.floor(controlsSize['height']/4)
  379.  
  380.     reactor = reactors[1]
  381.  
  382.     mon.setTextColor(colors.white)
  383.     setButton("ON","ON", powerUp, controlsSize['inX'], controlsSize['inY'], controlsSize['inX'] + math.floor(controlsSize['width']/2) -1, controlsSize['inY'] +2, 0, 0, colors.green)
  384.     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)
  385.  
  386.     table.insert(texts, {controlsSize['inX']+8, controlsSize['inY'] +(controlsSize['sectionHeight']*1)+1, 'AUTO-CONTROL', colors.white, colors.black})
  387.  
  388.     table.insert(texts, {controlsSize['inX']+5, controlsSize['inY'] +(controlsSize['sectionHeight']*1)+3, 'MIN', colors.lightBlue, colors.black})
  389.     table.insert(texts, {controlsSize['inX']+5, controlsSize['inY'] +(controlsSize['sectionHeight']*1)+4, minPowerRod..'%', colors.lightBlue, colors.black})
  390.  
  391.     table.insert(texts, {controlsSize['inX']+13, controlsSize['inY'] +(controlsSize['sectionHeight']*1)+4, '--', colors.white, colors.black})
  392.     table.insert(texts, {controlsSize['inX']+20, controlsSize['inY'] +(controlsSize['sectionHeight']*1)+3, 'MAX', colors.purple, colors.black})
  393.     table.insert(texts, {controlsSize['inX']+20, controlsSize['inY'] +(controlsSize['sectionHeight']*1)+4, maxPowerRod..'%', colors.purple, colors.black})
  394.     mon.setTextColor(colors.white)
  395.  
  396.     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)
  397.     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)
  398.  
  399.     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)
  400.     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)
  401.  
  402.     -- Numbers
  403.  
  404.     numbersSize['startX'] = infosSize['endX'] + margin + 1
  405.     numbersSize['startY'] = controlsSize['endY'] + margin + 1
  406.     numbersSize['endX'] = w-margin
  407.     numbersSize['endY'] = h-margin
  408.     numbersSize['height'] = numbersSize['endY']-numbersSize['startY']-(margin)-1
  409.     numbersSize['width'] = numbersSize['endX']-numbersSize['startX']-(margin*2)-2
  410.     numbersSize['inX'] = numbersSize['startX'] + margin +1
  411.     numbersSize['inY'] = numbersSize['startY'] + margin
  412.  
  413.     table.insert(boxes, {numbersSize['startX'] , numbersSize['startY'], numbersSize['endX'], numbersSize['endY'], colors.gray})
  414.     name = "NUMBERS"
  415.     table.insert(lines, {numbersSize['startX'] + margin , numbersSize['startY'], numbersSize['startX'] + (margin*2) + #name+1, numbersSize['startY'], colors.black})
  416.     table.insert(texts, {numbersSize['startX'] + (margin*2), numbersSize['startY'], name, colors.white, colors.black})
  417.  
  418.     refresh = true
  419.     while refresh do
  420.         parallel.waitForAny(refreshSingleReactor,clickEvent)
  421.     end
  422. end
  423.  
  424. -- Makes and Handles the draw function for less lag in the visual
  425.  
  426. function refreshSingleReactor()
  427.     local rfPerTick = 0
  428.     local rfTotal = 0
  429.     local rfTotalMax = 10000000
  430.     local reactor = reactors[1]
  431.  
  432.     rfTotal = reactor.getEnergyStored()
  433.     rfPerTick = math.floor(reactor.getEnergyProducedLastTick())
  434.     rodLevel = math.floor(reactor.getControlRodLevel(0))
  435.     fuelPerTick = reactor.getFuelConsumedLastTick();
  436.  
  437.     local i = 0
  438.     local infotoAdd = 'RF PER TICK : '
  439.  
  440.     if currentRfTick ~= rfPerTick then
  441.         currentRfTick = rfPerTick
  442.         if rfPerTick > rfPerTickMax then
  443.             rfPerTickMax = rfPerTick
  444.         end
  445.  
  446.         table.insert(lines, {numbersSize['inX'] , numbersSize['inY'],numbersSize['inX'] + numbersSize['width'] , numbersSize['inY'], colors.black})
  447.         table.insert(texts, {numbersSize['inX'], numbersSize['inY'], infotoAdd .. rfPerTick .. " RF", colors.white, colors.black})
  448.         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})
  449.  
  450.         width = math.floor((infosSize['width'] / rfPerTickMax)*rfPerTick)
  451.         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})
  452.  
  453.     end
  454.  
  455.     i = 1
  456.     infotoAdd = 'ENERGY STORED : '
  457.     if currentRfTotal ~= rfTotal then
  458.         currentRfTotal = rfTotal
  459.  
  460.         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})
  461.  
  462.         width = math.floor((infosSize['width'] / rfTotalMax)*rfTotal)
  463.         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})
  464.         table.insert(lines, {numbersSize['inX'] , numbersSize['inY'] +2 ,numbersSize['inX'] + numbersSize['width'] , numbersSize['inY'] +2, colors.black})
  465.         table.insert(texts, {numbersSize['inX'], numbersSize['inY']+ 2 , infotoAdd .. rfTotal .. " RF", colors.white, colors.black})
  466.     end
  467.  
  468.     i = 2
  469.     infotoAdd = 'CONTROL ROD LEVEL : '
  470.     if currentRodLevel ~= rodLevel then
  471.         currentRodLevel = rodLevel
  472.  
  473.         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})
  474.  
  475.         width = math.floor((infosSize['width'] / 100)*rodLevel)
  476.         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})
  477.         table.insert(lines, {numbersSize['inX'] , numbersSize['inY']+4 ,numbersSize['inX'] + numbersSize['width'] , numbersSize['inY'] +4, colors.black})
  478.         table.insert(texts, {numbersSize['inX'], numbersSize['inY']+ 4 , infotoAdd .. rodLevel .. "%", colors.white, colors.black})
  479.     end
  480.  
  481.     i = 3
  482.     infotoAdd = 'FUEL USAGE : '
  483.     if currentFuelConsumedLastTick ~= fuelPerTick then
  484.         currentFuelConsumedLastTick = fuelPerTick
  485.  
  486.         table.insert(lines, {numbersSize['inX'] , numbersSize['inY']+6 ,numbersSize['inX'] + numbersSize['width'] , numbersSize['inY'] +6, colors.black})
  487.         table.insert(texts, {numbersSize['inX'], numbersSize['inY']+ 6 , infotoAdd .. format_num(tonumber(fuelPerTick),3) .. "mb/t", colors.white, colors.black})
  488.     end
  489.  
  490.     mon.setTextColor(colors.white)
  491.     adjustRodsLevel()
  492.  
  493.     draw()
  494.  
  495.     sleep(2)
  496. end
  497.  
  498. --
  499. -- ** Get the informations from the index file
  500. -- line 1 = min ROD
  501. -- line 2 = max ROD
  502. --
  503.  
  504. function getInfoFromFile()
  505.  
  506.      if (fs.exists(index..".txt") == false) then
  507.         file = io.open(index..".txt","w")
  508.         file:write("0")
  509.         file:write("\n")
  510.         file:write("100")
  511.         file:close()
  512.     else
  513.         file = fs.open(index..".txt","r")
  514.         minPowerRod = tonumber(file.readLine())
  515.         maxPowerRod = tonumber(file.readLine())
  516.         file.close()
  517.     end
  518. end
  519.  
  520. -- Save informations to the index file
  521.  
  522. function setInfoToFile()
  523.     file = io.open(index..".txt","w")
  524.     file:write(minPowerRod .. "\n" .. maxPowerRod)
  525.     file:flush()
  526.     file:close()
  527. end
  528.  
  529. ---============================================================
  530. -- add comma to separate thousands
  531. -- From Lua-users.org/wiki/FormattingNumbers
  532. --
  533. --
  534. function comma_value(amount)
  535.   local formatted = amount
  536.   while true do
  537.     formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
  538.     if (k==0) then
  539.       break
  540.     end
  541.   end
  542.   return formatted
  543. end
  544.  
  545. ---============================================================
  546. -- rounds a number to the nearest decimal places
  547. -- From Lua-users.org/wiki/FormattingNumbers
  548. --
  549. --
  550. function round(val, decimal)
  551.   if (decimal) then
  552.     return math.floor( (val * 10^decimal) + 0.5) / (10^decimal)
  553.   else
  554.     return math.floor(val+0.5)
  555.   end
  556. end
  557.  
  558. --===================================================================
  559. -- given a numeric value formats output with comma to separate thousands
  560. -- and rounded to given decimal places
  561. -- From Lua-users.org/wiki/FormattingNumbers
  562. --
  563. function format_num(amount, decimal, prefix, neg_prefix)
  564.   local str_amount,  formatted, famount, remain
  565.  
  566.   decimal = decimal or 2  -- default 2 decimal places
  567.   neg_prefix = neg_prefix or "-" -- default negative sign
  568.  
  569.   famount = math.abs(round(amount,decimal))
  570.   famount = math.floor(famount)
  571.  
  572.   remain = round(math.abs(amount) - famount, decimal)
  573.  
  574.         -- comma to separate the thousands
  575.   formatted = comma_value(famount)
  576.  
  577.         -- attach the decimal portion
  578.   if (decimal > 0) then
  579.     remain = string.sub(tostring(remain),3)
  580.     formatted = formatted .. "." .. remain ..
  581.                 string.rep("0", decimal - string.len(remain))
  582.   end
  583.  
  584.         -- attach prefix string e.g '$'
  585.   formatted = (prefix or "") .. formatted
  586.  
  587.         -- if value is negative then format accordingly
  588.   if (amount<0) then
  589.     if (neg_prefix=="()") then
  590.       formatted = "("..formatted ..")"
  591.     else
  592.       formatted = neg_prefix .. formatted
  593.     end
  594.   end
  595.  
  596.   return formatted
  597. end
  598.  
  599. -- Clear and make the pixel smaller because we are not blind
  600.  
  601. mon.setBackgroundColor(colors.black)
  602. mon.clear()
  603. mon.setTextScale(0.5)
  604.  
  605. -- Get the information from the index file
  606. getInfoFromFile()
  607.  
  608.  
  609. -- Add's the visual and starts the Loop
  610. addDrawBoxesSingleReactor()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement