Hydrotronics

Extreme Reactors Code (MC 1.10.2)

Aug 15th, 2018
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. local VERISON = "NEW"
  4.  
  5. function checkVersionTooOld()
  6.     local monitorsCheck = {peripheral.find("monitor")}
  7. end
  8.  
  9. function checkVersionNEW()
  10.     local reactorsArray = {peripheral.find("BigReactors-Reactor")}
  11.     reactorsArray[0].getEnergyStats();
  12. end
  13.  
  14. function checkErrors()
  15.     if pcall(checkVersionTooOld) then
  16.  
  17.     else
  18.         error("The version of ComputerCraft is too old to use this program, sorry", 0)
  19.     end
  20.     local monitorsCheck = {peripheral.find("monitor")}
  21.     local reactorsCheck = {peripheral.find("BigReactors-Reactor")}
  22.  
  23.   if monitorsCheck[1] == nil then
  24.     error("The Monitor is not being detected, make sure the connections(modem) are activated", 0)
  25.   end
  26.  
  27.   if reactorsCheck[1] == nil then
  28.     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)
  29.   end
  30.  
  31.   if pcall(checkVersionNEW) then
  32.     VERISON = "NEW"
  33.   else
  34.     VERISON = "OLD"
  35.   end
  36. end
  37.  
  38. -- you need to give the index to be able to use the program
  39. -- ex : Reactor Data
  40.  
  41. local args = { ... }
  42.  
  43. local button = {}
  44. local filleds = {}
  45. local boxes = {}
  46. local lines = {}
  47. local texts = {}
  48.  
  49. local refresh = true
  50.  
  51. local infosSize = {}
  52. local controlsSize = {}
  53. local numbersSize = {}
  54. local currentRfTotal = 1
  55. local currentRfTick = 1
  56. local currentFuelConsumedLastTick = 0.00001
  57.  
  58.  
  59. local rfPerTickMax = 1
  60. local minPowerRod = 0
  61. local maxPowerRod = 100
  62. local currentRodLevel = 1
  63.  
  64. local index = ""
  65.  
  66. checkErrors()
  67.  
  68. if (#args == 0) then
  69.       error("No index Given, Make sure to start the 'start' program and not the 'reactor' program", 0)
  70. end
  71.  
  72. if (#args == 1) then
  73.     index = args[1]
  74. end
  75.  
  76. local monitors = {peripheral.find("monitor")}
  77. local mon = monitors[1]
  78. local reactors = {peripheral.find("BigReactors-Reactor")}
  79.  
  80.  
  81. term.redirect(mon)
  82. mon.clear()
  83. mon.setTextColor(colors.white)
  84. mon.setBackgroundColor(colors.black)
  85.  
  86. function clearTable()
  87.     button = {}
  88. end
  89.  
  90.  
  91.  
  92.  
  93. function setButton(name, title, func, xmin, ymin, xmax, ymax, elem, elem2, color)
  94.     button[name] = {}
  95.     button[name]["title"] = title
  96.     button[name]["func"] = func
  97.     button[name]["active"] = false
  98.     button[name]["xmin"] = xmin
  99.     button[name]["ymin"] = ymin
  100.     button[name]["xmax"] = xmax
  101.     button[name]["ymax"] = ymax
  102.     button[name]["color"] = color
  103.     button[name]["elem"] = elem
  104.     button[name]["elem2"] = elem2
  105. end
  106.  
  107. function fill(text, color, bData)
  108.    mon.setBackgroundColor(color)
  109.    mon.setTextColor(colors.white)
  110.    local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
  111.    local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(bData["title"])) /2) +1
  112.    for j = bData["ymin"], bData["ymax"] do
  113.       mon.setCursorPos(bData["xmin"], j)
  114.       if j == yspot then
  115.          for k = 0, bData["xmax"] - bData["xmin"] - string.len(bData["title"]) +1 do
  116.             if k == xspot then
  117.                mon.write(bData["title"])
  118.             else
  119.                mon.write(" ")
  120.             end
  121.          end
  122.       else
  123.          for i = bData["xmin"], bData["xmax"] do
  124.             mon.write(" ")
  125.          end
  126.       end
  127.    end
  128.    mon.setBackgroundColor(colors.black)
  129. end
  130.  
  131.  
  132. function screen()
  133.    local currColor
  134.    for name,data in pairs(button) do
  135.       local on = data["active"]
  136.       currColor = data["color"]
  137.       fill(name, currColor, data)
  138.    end
  139. end
  140.  
  141.  
  142. function flash(name)
  143.  
  144.     screen()
  145. end
  146.  
  147.  
  148. function checkxy(x, y)
  149.    for name, data in pairs(button) do
  150.       if y>=data["ymin"] and  y <= data["ymax"] then
  151.          if x>=data["xmin"] and x<= data["xmax"] then
  152.             data["func"](data["elem"], data["elem2"])
  153.             flash(data['name'])
  154.             return true
  155.             --data["active"] = not data["active"]
  156.             --print(name)
  157.          end
  158.       end
  159.    end
  160.    return false
  161. end
  162.  
  163. function label(w, h, text)
  164.    mon.setCursorPos(w, h)
  165.    mon.write(text)
  166. end
  167.  
  168. function draw()
  169.  
  170.     for key,value in pairs(filleds) do
  171.         local counter = 1
  172.         for i=value[2],value[4],1 do
  173.             paintutils.drawLine(value[1] , value[2]+counter, value[3], value[2]+counter, value[5])
  174.             counter = counter + 1
  175.         end
  176.     end
  177.  
  178.     for key,value in pairs(boxes) do
  179.         paintutils.drawLine(value[1] , value[2], value[1], value[4], value[5])
  180.         paintutils.drawLine(value[1] , value[2], value[3], value[2], value[5])
  181.         paintutils.drawLine(value[1] , value[4], value[3], value[4], value[5])
  182.         paintutils.drawLine(value[3] , value[2], value[3], value[4], value[5])
  183.     end
  184.  
  185.     for key,value in pairs(lines) do
  186.         paintutils.drawLine(value[1] , value[2], value[3], value[4], value[5])
  187.     end
  188.  
  189.     for key,value in pairs(texts) do
  190.         mon.setCursorPos(value[1], value[2])
  191.         mon.setTextColor(value[4])
  192.         mon.setBackgroundColor(value[5])
  193.         mon.write(value[3])
  194.     end
  195.     screen()
  196.     resetDraw()
  197. end
  198.  
  199.  
  200. function resetDraw()
  201.     filleds = {}
  202.     boxes = {}
  203.     lines = {}
  204.     texts = {}
  205. end
  206.  
  207. function clickEvent()
  208.     local myEvent={os.pullEvent("monitor_touch")}
  209.     checkxy(myEvent[3], myEvent[4])
  210. end
  211.  
  212.  
  213. function powerUp(m,n)
  214.     local reactor = reactors[1]
  215.     reactor.setActive(true)
  216. end
  217. --M & Ns are a good source of food, right?
  218. function powerDown(m,n)
  219.     local reactor = reactors[1]
  220.     reactor.setActive(false)
  221. end
  222.  
  223.  
  224. function modifyRods(limit, number)
  225.     local tempLevel = 0
  226.  
  227.     if limit == "min" then
  228.         tempLevel = minPowerRod + number
  229.         if tempLevel <= 0 then
  230.             minPowerRod = 0
  231.         end
  232.  
  233.         if tempLevel >= maxPowerRod then
  234.             minPowerRod = maxPowerRod -10
  235.         end
  236.  
  237.         if tempLevel < maxPowerRod and tempLevel > 0 then
  238.             minPowerRod = tempLevel
  239.         end
  240.     else
  241.         tempLevel = maxPowerRod + number
  242.         if tempLevel <= minPowerRod then
  243.             maxPowerRod = minPowerRod +10
  244.         end
  245.  
  246.         if tempLevel >= 100 then
  247.             maxPowerRod = 100
  248.         end
  249.  
  250.         if tempLevel > minPowerRod and tempLevel < 100 then
  251.             maxPowerRod = tempLevel
  252.         end
  253.     end
  254.  
  255.     table.insert(lines, {controlsSize['inX'], controlsSize['inY'] +(controlsSize['sectionHeight']*1)+4, controlsSize['inX'] + controlsSize['width'], controlsSize['inY']+(controlsSize['sectionHeight']*1)+4, colors.black})
  256.  
  257.     table.insert(texts, {controlsSize['inX']+5, controlsSize['inY'] +(controlsSize['sectionHeight']*1)+4, minPowerRod .. '%', colors.lightBlue, colors.black})
  258.     table.insert(texts, {controlsSize['inX']+13, controlsSize['inY'] +(controlsSize['sectionHeight']*1)+4, '--', colors.white, colors.black})
  259.     table.insert(texts, {controlsSize['inX']+20, controlsSize['inY'] +(controlsSize['sectionHeight']*1)+4, maxPowerRod .. '%', colors.purple, colors.black})
  260.  
  261.     setInfoToFile()
  262.     adjustRodsLevel()
  263. end
  264.  
  265. function adjustRodsLevel()
  266.     local reactor = reactors[1]
  267.     local rfTotalMax = 10000000
  268.  
  269.     local allStats = getAllStats()
  270.     local currentRfTotal = allStats["rfTotal"]
  271.     local reactorRodsLevel = allStats["reactorRodsLevel"]
  272.  
  273.     differenceMinMax = maxPowerRod - minPowerRod
  274.  
  275.     maxPower = (rfTotalMax/100) * maxPowerRod
  276.     minPower = (rfTotalMax/100) * minPowerRod
  277.  
  278.     if currentRfTotal >= maxPower then
  279.         currentRfTotal = maxPower
  280.     end
  281.  
  282.     if currentRfTotal <= minPower then
  283.         currentRfTotal = minPower
  284.     end
  285.  
  286.     currentRfTotal = currentRfTotal - (rfTotalMax/100) * minPowerRod
  287.  
  288.     local rfInBetween = (rfTotalMax/100) * differenceMinMax
  289.     local rodLevel = math.ceil((currentRfTotal/rfInBetween)*100)
  290.  
  291.     if VERSION == "NEW" then
  292.         if reactor.mbIsConnected() == true and reactor.mbIsAssembled() == true then
  293.             for key,value in pairs(reactorRodsLevel) do
  294.                 reactorRodsLevel[key] = rodLevel
  295.             end
  296.             reactor.setControlRodsLevels(reactorRodsLevel)
  297.         end
  298.     else
  299.         reactor.setAllControlRodLevels(rodLevel)
  300.     end
  301. end
  302.  
  303. function addDrawBoxesSingleReactor()
  304.     local w, h = mon.getSize()
  305.     local margin = math.floor((w/100)*2)
  306.  
  307.     infosSize['startX'] = margin + 1
  308.     infosSize['startY'] =  margin + 1
  309.     infosSize['endX'] = (((w-(margin*2))/3)*2)-margin
  310.     infosSize['endY'] = h - margin
  311.     infosSize['height'] = infosSize['endY']-infosSize['startY']-(margin*2)-2
  312.     infosSize['width'] = infosSize['endX']-infosSize['startX']-(margin*2)-2
  313.     infosSize['inX'] = infosSize['startX'] + margin +1
  314.     infosSize['inY'] = infosSize['startY'] + margin +1
  315.     infosSize['sectionHeight'] = math.floor(infosSize['height']/3)
  316.  
  317.     table.insert(boxes, {infosSize['startX'] , infosSize['startY'], infosSize['endX'], infosSize['endY'], colors.gray})
  318.     local name = "INFOS"
  319.     table.insert(lines, {infosSize['startX'] + margin , infosSize['startY'], infosSize['startX'] + (margin*2) + #name+1, infosSize['startY'], colors.black})
  320.     table.insert(texts, {infosSize['startX'] + (margin*2), infosSize['startY'], name, colors.white, colors.black})
  321.  
  322.     local names = {}
  323.     names[1] = 'ENERGY LAST TICK'
  324.     names[2] = 'ENERGY STORED'
  325.     names[3] = 'CONTROL ROD LEVEL'
  326.  
  327.     for i=0,2,1 do
  328.         table.insert(texts, {infosSize['inX'] + margin, infosSize['inY'] + (infosSize['sectionHeight']*i) +i, names[i+1], colors.white, colors.black})
  329.         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})
  330.     end
  331.  
  332.  
  333.     controlsSize['startX'] = infosSize['endX'] + margin + 1
  334.     controlsSize['startY'] =  margin + 1
  335.     controlsSize['endX'] = w-margin
  336.     controlsSize['endY'] = (((h - (margin*2))/3)*2) +1
  337.     controlsSize['height'] = controlsSize['endY']-controlsSize['startY']-(margin)-1
  338.     controlsSize['width'] = controlsSize['endX']-controlsSize['startX']-(margin*2)-2
  339.     controlsSize['inX'] = controlsSize['startX'] + margin +1
  340.     controlsSize['inY'] = controlsSize['startY'] + margin
  341.  
  342.     table.insert(boxes, {controlsSize['startX'] , controlsSize['startY'], controlsSize['endX'], controlsSize['endY'], colors.gray})
  343.     name = "CONTROLS"
  344.     table.insert(lines, {controlsSize['startX'] + margin , controlsSize['startY'], controlsSize['startX'] + (margin*2) + #name+1, controlsSize['startY'], colors.black})
  345.     table.insert(texts, {controlsSize['startX'] + (margin*2), controlsSize['startY'], name, colors.white, colors.black})
  346.  
  347.     controlsSize['sectionHeight'] = math.floor(controlsSize['height']/4)
  348.  
  349.     reactor = reactors[1]
  350.  
  351.     mon.setTextColor(colors.white)
  352.     setButton("ON","ON", powerUp, controlsSize['inX'], controlsSize['inY'], controlsSize['inX'] + math.floor(controlsSize['width']/2) -1, controlsSize['inY'] +2, 0, 0, colors.green)
  353.     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)
  354.  
  355.     table.insert(texts, {controlsSize['inX']+8, controlsSize['inY'] +(controlsSize['sectionHeight']*1)+1, 'AUTO-CONTROL', colors.white, colors.black})
  356.  
  357.     table.insert(texts, {controlsSize['inX']+5, controlsSize['inY'] +(controlsSize['sectionHeight']*1)+3, 'MIN', colors.lightBlue, colors.black})
  358.     table.insert(texts, {controlsSize['inX']+5, controlsSize['inY'] +(controlsSize['sectionHeight']*1)+4, minPowerRod..'%', colors.lightBlue, colors.black})
  359.  
  360.     table.insert(texts, {controlsSize['inX']+13, controlsSize['inY'] +(controlsSize['sectionHeight']*1)+4, '--', colors.white, colors.black})
  361.     table.insert(texts, {controlsSize['inX']+20, controlsSize['inY'] +(controlsSize['sectionHeight']*1)+3, 'MAX', colors.purple, colors.black})
  362.     table.insert(texts, {controlsSize['inX']+20, controlsSize['inY'] +(controlsSize['sectionHeight']*1)+4, maxPowerRod..'%', colors.purple, colors.black})
  363.     mon.setTextColor(colors.white)
  364.  
  365.     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)
  366.     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)
  367.  
  368.     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)
  369.     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)
  370.  
  371.  
  372.     numbersSize['startX'] = infosSize['endX'] + margin + 1
  373.     numbersSize['startY'] = controlsSize['endY'] + margin + 1
  374.     numbersSize['endX'] = w-margin
  375.     numbersSize['endY'] = h-margin
  376.     numbersSize['height'] = numbersSize['endY']-numbersSize['startY']-(margin)-1
  377.     numbersSize['width'] = numbersSize['endX']-numbersSize['startX']-(margin*2)-2
  378.     numbersSize['inX'] = numbersSize['startX'] + margin +1
  379.     numbersSize['inY'] = numbersSize['startY'] + margin
  380.  
  381.     table.insert(boxes, {numbersSize['startX'] , numbersSize['startY'], numbersSize['endX'], numbersSize['endY'], colors.gray})
  382.     name = "NUMBERS"
  383.     table.insert(lines, {numbersSize['startX'] + margin , numbersSize['startY'], numbersSize['startX'] + (margin*2) + #name+1, numbersSize['startY'], colors.black})
  384.     table.insert(texts, {numbersSize['startX'] + (margin*2), numbersSize['startY'], name, colors.white, colors.black})
  385.  
  386.     refresh = true
  387.     while refresh do
  388.         parallel.waitForAny(refreshSingleReactor,clickEvent)
  389.     end
  390. end
  391.  
  392. function getAllStats()
  393.     local stats = {}
  394.     local reactor = reactors[1]
  395.  
  396.     if VERSION == "NEW" then
  397.         if reactor.mbIsConnected() == true and reactor.mbIsAssembled() == true then
  398.             local reactorEnergyStats = reactor.getEnergyStats()
  399.             local reactorFuelStats = reactor.getFuelStats()
  400.             stats["reactorRodsLevel"] = reactor.getControlRodsLevels()
  401.  
  402.             stats["rfTotal"] = reactorEnergyStats["energyStored"]
  403.             stats["rfPerTick"] = math.ceil(reactorEnergyStats["energyProducedLastTick"])
  404.             stats["rodLevel"] = stats["reactorRodsLevel"][0]
  405.             stats["fuelPerTick"] = round(reactorFuelStats["fuelConsumedLastTick"], 2)
  406.         end
  407.     else
  408.         stats["rfTotal"] = reactor.getEnergyStored()
  409.         stats["rfPerTick"] = math.floor(reactor.getEnergyProducedLastTick())
  410.         stats["rodLevel"] = math.floor(reactor.getControlRodLevel(0))
  411.         stats["fuelPerTick"] = reactor.getFuelConsumedLastTick()
  412.     end
  413.    
  414.     return stats
  415. end
  416.  
  417. function refreshSingleReactor()
  418.     local rfPerTick = 0
  419.     local rfTotal = 0
  420.     local rfTotalMax = 10000000
  421.     local reactor = reactors[1]
  422.  
  423.     local allStats = getAllStats()
  424.     rfTotal = allStats["rfTotal"]
  425.     rfPerTick = allStats["rfPerTick"]
  426.     rodLevel = allStats["rodLevel"]
  427.     fuelPerTick = allStats["fuelPerTick"]
  428.  
  429.     local i = 0
  430.     local infotoAdd = 'RF PER TICK : '
  431.  
  432.     if currentRfTick ~= rfPerTick then
  433.         currentRfTick = rfPerTick
  434.         if rfPerTick > rfPerTickMax then
  435.             rfPerTickMax = rfPerTick
  436.         end
  437.  
  438.         table.insert(lines, {numbersSize['inX'] , numbersSize['inY'],numbersSize['inX'] + numbersSize['width'] , numbersSize['inY'], colors.black})
  439.         table.insert(texts, {numbersSize['inX'], numbersSize['inY'], infotoAdd .. rfPerTick .. " RF", colors.white, colors.black})
  440.         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})
  441.  
  442.         width = math.floor((infosSize['width'] / rfPerTickMax)*rfPerTick)
  443.         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})
  444.  
  445.     end
  446.  
  447.     i = 1
  448.     infotoAdd = 'ENERGY STORED : '
  449.     if currentRfTotal ~= rfTotal then
  450.         currentRfTotal = rfTotal
  451.  
  452.         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})
  453.  
  454.         width = math.floor((infosSize['width'] / rfTotalMax)*rfTotal)
  455.         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})
  456.         table.insert(lines, {numbersSize['inX'] , numbersSize['inY'] +2 ,numbersSize['inX'] + numbersSize['width'] , numbersSize['inY'] +2, colors.black})
  457.         table.insert(texts, {numbersSize['inX'], numbersSize['inY']+ 2 , infotoAdd .. rfTotal .. " RF", colors.white, colors.black})
  458.     end
  459.  
  460.     i = 2
  461.     infotoAdd = 'CONTROL ROD LEVEL : '
  462.     if currentRodLevel ~= rodLevel then
  463.         currentRodLevel = rodLevel
  464.  
  465.         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})
  466.  
  467.         width = math.floor((infosSize['width'] / 100)*rodLevel)
  468.         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})
  469.         table.insert(lines, {numbersSize['inX'] , numbersSize['inY']+4 ,numbersSize['inX'] + numbersSize['width'] , numbersSize['inY'] +4, colors.black})
  470.         table.insert(texts, {numbersSize['inX'], numbersSize['inY']+ 4 , infotoAdd .. rodLevel .. "%", colors.white, colors.black})
  471.     end
  472.  
  473.     i = 3
  474.     infotoAdd = 'FUEL USAGE : '
  475.     if currentFuelConsumedLastTick ~= fuelPerTick then
  476.         currentFuelConsumedLastTick = fuelPerTick
  477.  
  478.         table.insert(lines, {numbersSize['inX'] , numbersSize['inY']+6 ,numbersSize['inX'] + numbersSize['width'] , numbersSize['inY'] +6, colors.black})
  479.         table.insert(texts, {numbersSize['inX'], numbersSize['inY']+ 6 , infotoAdd .. format_num(tonumber(fuelPerTick),3) .. "mb/t", colors.white, colors.black})
  480.     end
  481.  
  482.     mon.setTextColor(colors.white)
  483.     adjustRodsLevel()
  484.  
  485.     draw()
  486.  
  487.     sleep(2)
  488. end
  489.  
  490.  
  491. function getInfoFromFile()
  492.  
  493.      if (fs.exists(index..".txt") == false) then
  494.         file = io.open(index..".txt","w")
  495.         file:write("0")
  496.         file:write("\n")
  497.         file:write("100")
  498.         file:close()
  499.     else
  500.         file = fs.open(index..".txt","r")
  501.         minPowerRod = tonumber(file.readLine())
  502.         maxPowerRod = tonumber(file.readLine())
  503.         file.close()
  504.     end
  505. end
  506.  
  507. function setInfoToFile()
  508.     file = io.open(index..".txt","w")
  509.     file:write(minPowerRod .. "\n" .. maxPowerRod)
  510.     file:flush()
  511.     file:close()
  512. end
  513.  
  514. function comma_value(amount)
  515.   local formatted = amount
  516.   while true do
  517.     formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
  518.     if (k==0) then
  519.       break
  520.     end
  521.   end
  522.   return formatted
  523. end
  524.  
  525.  
  526. function round(val, decimal)
  527.   if (decimal) then
  528.     return math.floor( (val * 10^decimal) + 0.5) / (10^decimal)
  529.   else
  530.     return math.floor(val+0.5)
  531.   end
  532. end
  533.  
  534. function format_num(amount, decimal, prefix, neg_prefix)
  535.   local str_amount,  formatted, famount, remain
  536.  
  537.   decimal = decimal or 2  -- default 2 decimal places
  538.   neg_prefix = neg_prefix or "-" -- default negative sign
  539.  
  540.   famount = math.abs(round(amount,decimal))
  541.   famount = math.floor(famount)
  542.  
  543.   remain = round(math.abs(amount) - famount, decimal)
  544.  
  545.   formatted = comma_value(famount)
  546.  
  547.   if (decimal > 0) then
  548.     remain = string.sub(tostring(remain),3)
  549.     formatted = formatted .. "." .. remain ..
  550.                 string.rep("0", decimal - string.len(remain))
  551.   end
  552.  
  553.   formatted = (prefix or "") .. formatted
  554.  
  555.   if (amount<0) then
  556.     if (neg_prefix=="()") then
  557.       formatted = "("..formatted ..")"
  558.     else
  559.       formatted = neg_prefix .. formatted
  560.     end
  561.   end
  562.  
  563.   return formatted
  564. end
  565.  
  566. mon.setBackgroundColor(colors.black)
  567. mon.clear()
  568. mon.setTextScale(0.5)
  569.  
  570. getInfoFromFile()
  571.  
  572.  
  573. addDrawBoxesSingleReactor()
Advertisement
Add Comment
Please, Sign In to add comment