Advertisement
meuced

Display_Energy.lua

Sep 11th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.59 KB | None | 0 0
  1. -- ****************************************************** --
  2. -- ****************************************************** --
  3. -- **             ENERGY BIG SCREEN DISPLAY            ** --
  4. -- **                     by cedmeu                    ** --
  5. -- **            Based on program by krakaen           ** --
  6. -- **                  cf. :                           ** --
  7. -- **   https://www.youtube.com/watch?v=SbbT7ncyS2M    ** --
  8. -- ****************************************************** --
  9. -- ****************************************************** --
  10.  
  11.  
  12. function checkVersion()
  13.     local monitorsCheck = {peripheral.find("monitor")}
  14. end
  15.  
  16. function checkErrors()
  17.     if pcall(checkVersion) then
  18.  
  19.     else
  20.         error("The version of ComputerCraft is too old to use this program, sorry", 0)
  21.     end
  22.     local monitorsCheck = {peripheral.find("monitor")}
  23.  
  24.   if monitorsCheck[1] == nil then
  25.     error("The Monitor is not being detected, make sure the connections(modem) are activated", 0)
  26.   end
  27. end
  28.  
  29.  
  30. local args = { ... }
  31.  
  32. local button = {}
  33. local filleds = {}
  34. local boxes = {}
  35. local lines = {}
  36. local texts = {}
  37.  
  38. local refresh = true
  39.  
  40. local infosSize = {}
  41. local controlsSize = {}
  42. local numbersSize = {}
  43. local currentRfTotal = 1
  44.  
  45. checkErrors() -- verify that everything is okay to start the program
  46.  
  47. local monitors = {peripheral.find("monitor")}
  48. local mon = monitors[1]
  49. local cells = {peripheral.find("tile_thermalexpansion_cell_basic_name")}
  50. local genSolar = {peripheral.find("extrautils_generatorsolar")}
  51.  
  52. -- use the black thingy sponge to clear the chalkboard
  53.  
  54. term.redirect(mon)
  55. mon.clear()
  56. mon.setTextColor(colors.white)
  57. mon.setBackgroundColor(colors.black)
  58.  
  59. function clearTable()
  60.     button = {}
  61. end
  62.  
  63. -- ****************************************************** --
  64. -- ****************************************************** --
  65. --   some functions for drawing boxes, buttons...         --
  66. -- ****************************************************** --
  67. -- ****************************************************** --
  68.  
  69. -- All the things that make my buttons work
  70.  
  71. function setButton(name, title, func, xmin, ymin, xmax, ymax, elem, elem2, color)
  72.     button[name] = {}
  73.     button[name]["title"] = title
  74.     button[name]["func"] = func
  75.     button[name]["active"] = false
  76.     button[name]["xmin"] = xmin
  77.     button[name]["ymin"] = ymin
  78.     button[name]["xmax"] = xmax
  79.     button[name]["ymax"] = ymax
  80.     button[name]["color"] = color
  81.     button[name]["elem"] = elem
  82.     button[name]["elem2"] = elem2
  83. end
  84.  
  85. -- stuff and things for buttons
  86.  
  87. function fill(text, color, bData)
  88.    mon.setBackgroundColor(color)
  89.    mon.setTextColor(colors.white)
  90.    local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
  91.    local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(bData["title"])) /2) +1
  92.    for j = bData["ymin"], bData["ymax"] do
  93.       mon.setCursorPos(bData["xmin"], j)
  94.       if j == yspot then
  95.          for k = 0, bData["xmax"] - bData["xmin"] - string.len(bData["title"]) +1 do
  96.             if k == xspot then
  97.                mon.write(bData["title"])
  98.             else
  99.                mon.write(" ")
  100.             end
  101.          end
  102.       else
  103.          for i = bData["xmin"], bData["xmax"] do
  104.             mon.write(" ")
  105.          end
  106.       end
  107.    end
  108.    mon.setBackgroundColor(colors.black)
  109. end
  110.  
  111. -- stuff and things for buttons
  112.  
  113. function screen()
  114.    local currColor
  115.    for name,data in pairs(button) do
  116.       local on = data["active"]
  117.       currColor = data["color"]
  118.       fill(name, currColor, data)
  119.    end
  120. end
  121.  
  122. -- stuff and things for buttons
  123.  
  124. function flash(name)
  125.  
  126.     screen()
  127. end
  128.  
  129. -- magical handler for clicky clicks
  130.  
  131. function checkxy(x, y)
  132.    for name, data in pairs(button) do
  133.       if y>=data["ymin"] and  y <= data["ymax"] then
  134.          if x>=data["xmin"] and x<= data["xmax"] then
  135.             data["func"](data["elem"], data["elem2"])
  136.             flash(data['name'])
  137.             return true
  138.             --data["active"] = not data["active"]
  139.             --print(name)
  140.          end
  141.       end
  142.    end
  143.    return false
  144. end
  145.  
  146. -- Don't question my code, it works on my machine...
  147.  
  148. function label(w, h, text)
  149.    mon.setCursorPos(w, h)
  150.    mon.write(text)
  151. end
  152.  
  153. -- Draw function : put's all the beautiful magic in the screen
  154.  
  155. function draw()
  156.  
  157.     for key,value in pairs(filleds) do
  158.         local counter = 1
  159.         for i=value[2],value[4],1 do
  160.             paintutils.drawLine(value[1] , value[2]+counter, value[3], value[2]+counter, value[5])
  161.             counter = counter + 1
  162.         end
  163.     end
  164.  
  165.     for key,value in pairs(boxes) do
  166.         paintutils.drawLine(value[1] , value[2], value[1], value[4], value[5])
  167.         paintutils.drawLine(value[1] , value[2], value[3], value[2], value[5])
  168.         paintutils.drawLine(value[1] , value[4], value[3], value[4], value[5])
  169.         paintutils.drawLine(value[3] , value[2], value[3], value[4], value[5])
  170.     end
  171.  
  172.     for key,value in pairs(lines) do
  173.         paintutils.drawLine(value[1] , value[2], value[3], value[4], value[5])
  174.     end
  175.  
  176.     for key,value in pairs(texts) do
  177.         mon.setCursorPos(value[1], value[2])
  178.         mon.setTextColor(value[4])
  179.         mon.setBackgroundColor(value[5])
  180.         mon.write(value[3])
  181.     end
  182.     screen()
  183.     resetDraw()
  184. end
  185.  
  186. -- Resets the elements to draw to only draw the neccessity
  187.  
  188. function resetDraw()
  189.     filleds = {}
  190.     boxes = {}
  191.     lines = {}
  192.     texts = {}
  193. end
  194.  
  195. -- Handles all the clicks for the buttons
  196.  
  197. function clickEvent()
  198.     local myEvent={os.pullEvent("monitor_touch")}
  199.     checkxy(myEvent[3], myEvent[4])
  200. end
  201.  
  202.  
  203. -- ****************************************************** --
  204. -- ****************************************************** --
  205. -- Creates the frame and the basic of the visual          --
  206. -- Also adds the variables informations for placement of  --
  207. -- stuff and things                                       --
  208. -- ****************************************************** --
  209. -- ****************************************************** --
  210.  
  211.  
  212. function addDrawBoxesEnergy()
  213.     local w, h = mon.getSize()
  214.     local margin = math.floor((w/100)*2)
  215.  
  216.     infosSize['startX'] = margin + 1
  217.     infosSize['startY'] =  margin + 1
  218.     --infosSize['endX'] = (((w-(margin*2))/3)*2)-margin
  219.     infosSize['endX'] = w - (margin + 1)
  220.     --infosSize['endY'] = h - margin
  221.     infosSize['endY'] = h - ((h*1/3)- (margin + 1))
  222.     infosSize['height'] = infosSize['endY']-infosSize['startY']-(margin*2)-2
  223.     infosSize['width'] = infosSize['endX']-infosSize['startX']-(margin*2)-2
  224.     infosSize['inX'] = infosSize['startX'] + margin +1
  225.     infosSize['inY'] = infosSize['startY'] + margin +1
  226.     --infosSize['sectionHeight'] = math.floor(infosSize['height']/3)
  227.     infosSize['sectionHeight'] = 5
  228.  
  229. -- Boxe INFOS
  230.     table.insert(boxes, {infosSize['startX'] , infosSize['startY'], infosSize['endX'], infosSize['endY'], colors.gray})
  231.     local name = "ENERGY"
  232.     table.insert(lines, {infosSize['startX'] + margin , infosSize['startY'], infosSize['startX'] + (margin*2) + #name+1, infosSize['startY'], colors.black})
  233.     table.insert(texts, {infosSize['startX'] + (margin*2), infosSize['startY'], name, colors.white, colors.black})
  234.  
  235.     -- Les 3 jauges
  236.     local names = {}
  237.     names[1] = 'TOTAL ENERGY STORED'
  238.     names[2] = 'ENERGY FROM BASIC CELLS'
  239.     names[3] = 'ENERGY FROM SOLAR GENERATOR'
  240.  
  241.     for i=0,2,1 do
  242.         table.insert(texts, {infosSize['inX'] + margin, infosSize['inY'] + (infosSize['sectionHeight']*i) +i, names[i+1], colors.white, colors.black})
  243.         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})
  244.     end
  245.  
  246.     -- Numbers
  247.  
  248.     numbersSize['startX'] = infosSize['endX'] + margin + 1
  249.     numbersSize['startY'] = infosSize['startY'] + margin + 1 +35
  250.     numbersSize['endX'] = w-margin
  251.     numbersSize['endY'] = h-margin
  252.     numbersSize['height'] = numbersSize['endY']-numbersSize['startY']-(margin)-1
  253.     numbersSize['width'] = numbersSize['endX']-numbersSize['startX']-(margin*2)-2
  254.     numbersSize['inX'] = numbersSize['startX'] + margin +1
  255.     numbersSize['inY'] = numbersSize['startY'] + margin
  256.  
  257.     --table.insert(boxes, {numbersSize['startX'] , numbersSize['startY'], numbersSize['endX'], numbersSize['endY'], colors.gray})
  258.     name = "NUMBERS"
  259.     --table.insert(lines, {numbersSize['startX'] + margin , numbersSize['startY'], numbersSize['startX'] + (margin*2) + #name+1, numbersSize['startY'], colors.black})
  260.     --table.insert(texts, {numbersSize['startX'] + (margin*2), numbersSize['startY'], name, colors.white, colors.black})
  261.     --draw()
  262.     refresh = true
  263.     while refresh do
  264.         parallel.waitForAny(refreshEnergy,clickEvent)
  265.     end
  266. end
  267.  
  268. -- Makes and Handles the draw function for less lag in the visual
  269.  
  270. function refreshEnergy()
  271.  
  272.     local rfTotal = 0
  273.     local rfTotalMax = 0
  274.  
  275.     local rfTotalCells = 0
  276.     local rfTotalMaxCells = 0
  277.  
  278.     local rfTotalGenSolar = 0
  279.     local rfTotalMaxGenSolar = 0
  280.    
  281.     local j=0
  282.  
  283.     for j = 1, #cells do
  284.         rfTotalCells = rfTotalCells + cells[j].getEnergyStored()
  285.         rfTotalMaxCells = rfTotalMaxCells + cells[j].getMaxEnergyStored()
  286.     end
  287.  
  288.     for j = 1, #genSolar do
  289.         rfTotalGenSolar = rfTotalGenSolar + genSolar[j].getEnergyStored()
  290.         rfTotalMaxGenSolar = rfTotalMaxGenSolar + genSolar[j].getMaxEnergyStored()
  291.     end
  292.  
  293.     rfTotal = rfTotal + rfTotalCells + rfTotalGenSolar
  294.     rfTotalMax = rfTotalMax + rfTotalMaxCells + rfTotalMaxGenSolar
  295.  
  296.     local i = 0
  297.     local infotoAdd = 'TOTAL ENERGY STORED : '
  298.  
  299.         table.insert(lines, {numbersSize['inX'] , numbersSize['inY'],numbersSize['inX'] + numbersSize['width'] , numbersSize['inY'], colors.black})
  300.         --table.insert(texts, {numbersSize['inX'], numbersSize['inY'], infotoAdd .. rfTotal .. " RF", colors.white, colors.black})
  301.         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})
  302.  
  303.         width = math.floor((infosSize['width'] / rfTotalMax)*rfTotal)
  304.         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})
  305.  
  306.     i = 1
  307.     infotoAdd = 'ENERGY FROM BASIC CELLS : '
  308.     if currentRfTotal ~= rfTotalCells then
  309.         currentRfTotal = rfTotalCells
  310.  
  311.         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})
  312.  
  313.         width = math.floor((infosSize['width'] / rfTotalMaxCells)*rfTotalCells)
  314.         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})
  315.         table.insert(lines, {numbersSize['inX'] , numbersSize['inY'] +2 ,numbersSize['inX'] + numbersSize['width'] , numbersSize['inY'] +2, colors.black})
  316.         --table.insert(texts, {numbersSize['inX'], numbersSize['inY']+ 2 , infotoAdd .. rfTotalCells .. " RF", colors.white, colors.black})
  317.     end
  318.  
  319.     i = 2
  320.     infotoAdd = 'ENERGY FROM GENERATOR SOLAR : '
  321.     if currentRfTotal ~= rfTotalGenSolar then
  322.         currentRfTotal = rfTotalGenSolar
  323.  
  324.         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})
  325.  
  326.         width = math.floor((infosSize['width'] / rfTotalMaxGenSolar)*rfTotalGenSolar)
  327.         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})
  328.         table.insert(lines, {numbersSize['inX'] , numbersSize['inY'] +3 ,numbersSize['inX'] + numbersSize['width'] , numbersSize['inY'] +2, colors.black})
  329.         --table.insert(texts, {numbersSize['inX'], numbersSize['inY']+ 3 , infotoAdd .. rfTotalGenSolar .. " RF", colors.white, colors.black})
  330.     end
  331.  
  332.     mon.setTextColor(colors.white)
  333.  
  334.     draw()
  335.  
  336.     sleep(10)
  337. end
  338.  
  339.  
  340. -- ****************************************************** --
  341. -- ****************************************************** --
  342. -- few others utilities functions                         --
  343. -- ****************************************************** --
  344. -- ****************************************************** --
  345.  
  346. ---============================================================
  347. -- add comma to separate thousands
  348. -- From Lua-users.org/wiki/FormattingNumbers
  349. --
  350. --
  351. function comma_value(amount)
  352.   local formatted = amount
  353.   while true do
  354.     formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
  355.     if (k==0) then
  356.       break
  357.     end
  358.   end
  359.   return formatted
  360. end
  361.  
  362. ---============================================================
  363. -- rounds a number to the nearest decimal places
  364. -- From Lua-users.org/wiki/FormattingNumbers
  365. --
  366. --
  367. function round(val, decimal)
  368.   if (decimal) then
  369.     return math.floor( (val * 10^decimal) + 0.5) / (10^decimal)
  370.   else
  371.     return math.floor(val+0.5)
  372.   end
  373. end
  374.  
  375. --===================================================================
  376. -- given a numeric value formats output with comma to separate thousands
  377. -- and rounded to given decimal places
  378. -- From Lua-users.org/wiki/FormattingNumbers
  379. --
  380. function format_num(amount, decimal, prefix, neg_prefix)
  381.   local str_amount,  formatted, famount, remain
  382.  
  383.   decimal = decimal or 2  -- default 2 decimal places
  384.   neg_prefix = neg_prefix or "-" -- default negative sign
  385.  
  386.   famount = math.abs(round(amount,decimal))
  387.   famount = math.floor(famount)
  388.  
  389.   remain = round(math.abs(amount) - famount, decimal)
  390.  
  391.         -- comma to separate the thousands
  392.   formatted = comma_value(famount)
  393.  
  394.         -- attach the decimal portion
  395.   if (decimal > 0) then
  396.     remain = string.sub(tostring(remain),3)
  397.     formatted = formatted .. "." .. remain ..
  398.                 string.rep("0", decimal - string.len(remain))
  399.   end
  400.  
  401.         -- attach prefix string e.g '$'
  402.   formatted = (prefix or "") .. formatted
  403.  
  404.         -- if value is negative then format accordingly
  405.   if (amount<0) then
  406.     if (neg_prefix=="()") then
  407.       formatted = "("..formatted ..")"
  408.     else
  409.       formatted = neg_prefix .. formatted
  410.     end
  411.   end
  412.  
  413.   return formatted
  414. end
  415.  
  416.  
  417. -- ****************************************************** --
  418. -- ****************************************************** --
  419. -- aaaand here is the "main"...                           --
  420. -- ****************************************************** --
  421. -- ****************************************************** --
  422.  
  423.  
  424. -- Clear and make the pixel smaller because we are not blind
  425.  
  426. mon.setBackgroundColor(colors.black)
  427. mon.clear()
  428. mon.setTextScale(0.5)
  429.  
  430. -- Add's the visual and starts the Loop
  431. addDrawBoxesEnergy()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement