Advertisement
jille_Jr

CC: Grind stone display

May 8th, 2014
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.95 KB | None | 0 0
  1.  
  2. --(( Settings ))--
  3.  
  4. -- side relative to the computer
  5. -- or path if you are using cables
  6. local grindstone = peripheral.wrap("left")
  7. or error("Unable to wrap the grind stone!",0)
  8.  
  9. local monitor = peripheral.wrap("top")
  10. or error("Unable to wrap the monitor!",0)
  11.  
  12. -- colors
  13. local BG = colors.black
  14. local LOW = colors.red
  15. local MED = colors.orange
  16. local HIGH = colors.yellow
  17. local TOP = colors.lime
  18.  
  19. -- how many times to draw per second
  20. local updateRate = 5
  21.  
  22. --(( Variables ))--
  23.  
  24. local ores = 0
  25. local dusts = 0
  26. local total = 0
  27. local oresPercentage = 0
  28. local dustsPercentage = 0
  29.  
  30. local mW,mH = monitor.getSize()
  31. local tW,tH = term.getSize()
  32.  
  33. --(( Functions ))--
  34.  
  35. local function getAllStacks( inv )
  36.     local t = {}
  37.     local size = inv.getInventorySize()
  38.     for slot = 1,size do
  39.         local item = inv.getStackInSlot(slot) or {}
  40.  
  41.         item.rawName = item.rawName or ""
  42.         item.qty = item.qty or 0
  43.         item.dmg = item.dmg or 0
  44.         item.name = item.name or ""
  45.         item.id = item.id or 0
  46.  
  47.         t[slot] = item
  48.     end
  49.     return t
  50. end
  51.  
  52. local function updateItems()
  53.     local items = getAllStacks(grindstone)
  54.  
  55.     ores = 0
  56.     dusts = 0
  57.     total = 0
  58.     percentage = 0
  59.  
  60.     for slot,item in pairs(items) do
  61.         -- ores
  62.         if slot >= 1 and slot <= 3 then
  63.             ores = ores + item.qty
  64.         end
  65.  
  66.         -- dusts
  67.         if slot >= 4 and slot <= 6 then
  68.             dusts = dusts + item.qty
  69.         end
  70.     end
  71.  
  72.     total = ores + dusts
  73.     oresPercentage = ores / total
  74.     dustsPercentage = dusts / total
  75.  
  76.     if ores == 0 then oresPercentage = 0 end
  77.     if dusts == 0 then dustsPercentage = 0 end
  78. end
  79.  
  80. local function draw()
  81.     -- drawing a big bar over the monitor
  82.     -- drawing from left to right
  83.  
  84.     -- black area
  85.     monitor.setBackgroundColor(BG)
  86.     for column = math.ceil(dustsPercentage * mW),mW,1 do
  87.         for row = 1,mH,1 do
  88.             monitor.setCursorPos(column,row)
  89.             monitor.write(" ")
  90.         end
  91.     end
  92.  
  93.     -- colored area
  94.     if dustsPercentage > 0 then
  95.         -- 0% to 33%
  96.         if dustsPercentage < 1/3 then
  97.             monitor.setBackgroundColor(LOW)
  98.         end
  99.         -- 33% to 66%
  100.         if dustsPercentage >= 1/3
  101.         and dustsPercentage < 2/3 then
  102.             monitor.setBackgroundColor(MED)
  103.         end
  104.         -- 66% to 100%
  105.         if dustsPercentage >= 2/3
  106.         and dustsPercentage < 1 then
  107.             monitor.setBackgroundColor(HIGH)
  108.         end
  109.         -- 100%
  110.         if dustsPercentage == 1 then
  111.             monitor.setBackgroundColor(TOP)
  112.         end
  113.     end
  114.     for column = 1,math.floor(dustsPercentage * mW),1 do
  115.         for row = 1,mH,1 do
  116.             monitor.setCursorPos(column,row)
  117.             monitor.write(" ")
  118.         end
  119.     end
  120.  
  121.     -- on screen info
  122.     term.clear()
  123.  
  124.     local ore = "ore" if ores > 1 then ore = "ores" end
  125.     local dust = "dust" if dusts > 1 then dust = "dusts" end
  126.  
  127.     local text1 = "Currently "..ores.." "..ore.." and ".. dusts.." "..dust
  128.     local text2 = math.floor(dustsPercentage*100).."% done"
  129.  
  130.     term.setCursorPos(tW/2-#text1/2,tH/2-1)
  131.     term.write(text1)
  132.     term.setCursorPos(tW/2-#text2/2,tH/2+1)
  133.     term.write(text2)
  134. end
  135.  
  136. --(( Main program ))--
  137.  
  138. -- main loop
  139. while true do
  140.     updateItems()
  141.     draw()
  142.     sleep(1/updateRate)
  143. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement