Advertisement
natie3

Aspects

Oct 2nd, 2014
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.04 KB | None | 0 0
  1. m1 = peripheral.wrap("monitor_37")
  2. m2 = peripheral.wrap("monitor_38")
  3.  
  4. function getList()
  5.   lijst = {}
  6.   jars = peripheral.getNames()
  7.   for k, v in pairs(jars) do
  8.     if (v ~= "right" and v ~= "left" and v ~= "container_chest_0" and v ~= "monitor_38" and v ~= "monitor_37") then
  9.       aspects = peripheral.call(v,"getAspects")
  10.       aspect = aspects[1]
  11.       if #lijst == 0 then
  12.         table.insert(lijst,aspect)
  13.       else
  14.         found = false
  15.         foundIndex = 0
  16.         for i = 1, #lijst do
  17.           if lijst[i].name == aspect.name then
  18.             found = true
  19.             foundIndex = i
  20.           end
  21.         end
  22.         if found then
  23.           lijst[foundIndex].quantity = lijst[foundIndex].quantity + aspect.quantity
  24.         else
  25.           table.insert(lijst,aspect)
  26.         end
  27.       end
  28.     end
  29.   end
  30.   sort_func = function( a,b ) return a.name < b.name end
  31.   table.sort( lijst, sort_func )
  32.   return lijst
  33. end
  34.  
  35. function draw(m,y,aspect)
  36.   mx, my = m.getSize()
  37.   m.setBackgroundColor(colors.black)
  38.   ypos = y
  39.   xpos = 2
  40.   if y > 12 then
  41.     ypos = y - 12
  42.     xpos = (mx/2)
  43.   end  
  44.   m.setCursorPos(xpos,(ypos*2))
  45.   m.write(aspect.name)
  46.   m.setCursorPos(xpos+13,(ypos*2))
  47.   m.setBackgroundColor(colors.orange)
  48.   amount = math.ceil((aspect.quantity/(5*64))*((mx/2)-16))
  49.   for i = 1, amount do
  50.     m.write(" ")
  51.   end
  52.   m.setCursorPos(xpos+14,(ypos*2))
  53.   m.write(tostring(aspect.quantity))
  54. end
  55.  
  56. os.startTimer(5)
  57. oldlist = getList()
  58. newlist = oldlist
  59. while true do
  60.   os.pullEvent("timer")
  61.   oldlist = newlist
  62.   newlist = getList()
  63.   m1.setBackgroundColor(colors.black)
  64.   m2.setBackgroundColor(colors.black)
  65.   m1.clear()
  66.   m2.clear()
  67.   changed = false
  68.   for i = 1, #newlist do
  69.     if newlist[i].quantity ~= oldlist[i].quantity then
  70.       changed = true
  71.     end
  72.     if i < 25 then
  73.       draw(m1,i,newlist[i])
  74.     else
  75.       draw(m2,i-24,newlist[i])
  76.     end
  77.   end
  78.   if changed then
  79.     print("Something changed")
  80.     os.startTimer(5)
  81.   else
  82.     print("Nothing changed")
  83.     os.startTimer(30)  
  84.   end
  85. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement