Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.65 KB | None | 0 0
  1.     table.insert(texts, {numbersSize['startX'] + (margin*2), numbersSize['startY'], name, colors.white, colors.black})
  2.  
  3.     refresh = true
  4.     while refresh do
  5.         parallel.waitForAny(refreshSingleReactor,clickEvent)
  6.     end
  7. end
  8.  
  9. -- Makes and Handles the draw function for less lag in the visual
  10.  
  11. function refreshSingleReactor()
  12.     local rfPerTick = 0
  13.     local rfTotal = 0
  14.     local rfTotalMax = 10000000
  15.     local reactor = reactors[1]
  16.  
  17.     rfTotal = reactor.getEnergyStored()
  18.     rfPerTick = math.floor(reactor.getEnergyProducedLastTick())
  19.     rodLevel = math.floor(reactor.getControlRodLevel(0))
  20.     fuelPerTick = reactor.getFuelConsumedLastTick();
  21.  
  22.     local i = 0
  23.     local infotoAdd = 'RF PER TICK : '
  24.  
  25.     if currentRfTick ~= rfPerTick then
  26.         currentRfTick = rfPerTick
  27.         if rfPerTick > rfPerTickMax then
  28.             rfPerTickMax = rfPerTick
  29.         end
  30.  
  31.         table.insert(lines, {numbersSize['inX'] , numbersSize['inY'],numbersSize['inX'] + numbersSize['width'] , numbersSize['inY'], colors.black})
  32.         table.insert(texts, {numbersSize['inX'], numbersSize['inY'], infotoAdd .. rfPerTick .. " RF", colors.white, colors.black})
  33.         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})
  34.  
  35.         width = math.floor((infosSize['width'] / rfPerTickMax)*rfPerTick)
  36.         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})
  37.  
  38.     end
  39.  
  40.     i = 1
  41.     infotoAdd = 'ENERGY STORED : '
  42.     if currentRfTotal ~= rfTotal then
  43.         currentRfTotal = rfTotal
  44.  
  45.         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})
  46.  
  47.         width = math.floor((infosSize['width'] / rfTotalMax)*rfTotal)
  48.         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})
  49.         table.insert(lines, {numbersSize['inX'] , numbersSize['inY'] +2 ,numbersSize['inX'] + numbersSize['width'] , numbersSize['inY'] +2, colors.black})
  50.         table.insert(texts, {numbersSize['inX'], numbersSize['inY']+ 2 , infotoAdd .. rfTotal .. " RF", colors.white, colors.black})
  51.     end
  52.  
  53.     i = 2
  54.     infotoAdd = 'CONTROL ROD LEVEL : '
  55.     if currentRodLevel ~= rodLevel then
  56.         currentRodLevel = rodLevel
  57.  
  58.         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})
  59.  
  60.         width = math.floor((infosSize['width'] / 100)*rodLevel)
  61.         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})
  62.         table.insert(lines, {numbersSize['inX'] , numbersSize['inY']+4 ,numbersSize['inX'] + numbersSize['width'] , numbersSize['inY'] +4, colors.black})
  63.         table.insert(texts, {numbersSize['inX'], numbersSize['inY']+ 4 , infotoAdd .. rodLevel .. "%", colors.white, colors.black})
  64.     end
  65.  
  66.     i = 3
  67.     infotoAdd = 'FUEL USAGE : '
  68.     if currentFuelConsumedLastTick ~= fuelPerTick then
  69.         currentFuelConsumedLastTick = fuelPerTick
  70.  
  71.         table.insert(lines, {numbersSize['inX'] , numbersSize['inY']+6 ,numbersSize['inX'] + numbersSize['width'] , numbersSize['inY'] +6, colors.black})
  72.         table.insert(texts, {numbersSize['inX'], numbersSize['inY']+ 6 , infotoAdd .. format_num(tonumber(fuelPerTick),3) .. "mb/t", colors.white, colors.black})
  73.     end
  74.  
  75.     mon.setTextColor(colors.white)
  76.     adjustRodsLevel()
  77.  
  78.     draw()
  79.  
  80.     sleep(2)
  81. end
  82.  
  83. --
  84. -- ** Get the informations from the index file
  85. -- line 1 = min ROD
  86. -- line 2 = max ROD
  87. --
  88.  
  89. function getInfoFromFile()
  90.  
  91.      if (fs.exists(index..".txt") == false) then
  92.         file = io.open(index..".txt","w")
  93.         file:write("0")
  94.         file:write("\n")
  95.         file:write("100")
  96.         file:close()
  97.     else
  98.         file = fs.open(index..".txt","r")
  99.         minPowerRod = tonumber(file.readLine())
  100.         maxPowerRod = tonumber(file.readLine())
  101.         file.close()
  102.     end
  103. end
  104.  
  105. -- Save informations to the index file
  106.  
  107. function setInfoToFile()
  108.     file = io.open(index..".txt","w")
  109.     file:write(minPowerRod .. "\n" .. maxPowerRod)
  110.     file:flush()
  111.     file:close()
  112. end
  113.  
  114. ---============================================================
  115. -- add comma to separate thousands
  116. -- From Lua-users.org/wiki/FormattingNumbers
  117. --
  118. --
  119. function comma_value(amount)
  120.   local formatted = amount
  121.   while true do
  122.     formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
  123.     if (k==0) then
  124.       break
  125.     end
  126.   end
  127.   return formatted
  128. end
  129.  
  130. ---============================================================
  131. -- rounds a number to the nearest decimal places
  132. -- From Lua-users.org/wiki/FormattingNumbers
  133. --
  134. --
  135. function round(val, decimal)
  136.   if (decimal) then
  137.     return math.floor( (val * 10^decimal) + 0.5) / (10^decimal)
  138.   else
  139.     return math.floor(val+0.5)
  140.   end
  141. end
  142.  
  143. --===================================================================
  144. -- given a numeric value formats output with comma to separate thousands
  145. -- and rounded to given decimal places
  146. -- From Lua-users.org/wiki/FormattingNumbers
  147. --
  148. function format_num(amount, decimal, prefix, neg_prefix)
  149.   local str_amount,  formatted, famount, remain
  150.  
  151.   decimal = decimal or 2  -- default 2 decimal places
  152.   neg_prefix = neg_prefix or "-" -- default negative sign
  153.  
  154.   famount = math.abs(round(amount,decimal))
  155.   famount = math.floor(famount)
  156.  
  157.   remain = round(math.abs(amount) - famount, decimal)
  158.  
  159.         -- comma to separate the thousands
  160.   formatted = comma_value(famount)
  161.  
  162.         -- attach the decimal portion
  163.   if (decimal > 0) then
  164.     remain = string.sub(tostring(remain),3)
  165.     formatted = formatted .. "." .. remain ..
  166.                 string.rep("0", decimal - string.len(remain))
  167.   end
  168.  
  169.         -- attach prefix string e.g '$'
  170.   formatted = (prefix or "") .. formatted
  171.  
  172.         -- if value is negative then format accordingly
  173.   if (amount<0) then
  174.     if (neg_prefix=="()") then
  175.       formatted = "("..formatted ..")"
  176.     else
  177.       formatted = neg_prefix .. formatted
  178.     end
  179.   end
  180.  
  181.   return formatted
  182. end
  183.  
  184. -- Clear and make the pixel smaller because we are not blind
  185.  
  186. mon.setBackgroundColor(colors.black)
  187. mon.clear()
  188. mon.setTextScale(0.5)
  189.  
  190. -- Get the information from the index file
  191. getInfoFromFile()
  192.  
  193.  
  194. -- Add's the visual and starts the Loop
  195. addDrawBoxesSingleReactor()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement