Advertisement
Guest User

aspects

a guest
Feb 23rd, 2014
10,588
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.14 KB | None | 0 0
  1. rednet.open("left")
  2. os.loadAPI("button")
  3. local turtleID = 40
  4. local essentia = {}
  5. local jars = peripheral.getNames()
  6. local m = peripheral.wrap("top")
  7. local monCoord = {}
  8. local currEssentia
  9. local fillAmt = 0
  10. local rowsActive = true
  11.  
  12. function sortEss(t)
  13.    local keys = {}
  14.    for k in pairs(t) do keys[#keys+1] = k end
  15.    table.sort(keys)
  16.    
  17.    local i = 0
  18.    return function()
  19.       i = i+1
  20.       if keys[i] then
  21.          return keys[i], t[keys[i]]
  22.       end
  23.    end
  24. end
  25.  
  26. function scanEssentia()
  27.   for i,j in ipairs(jars) do
  28.      if peripheral.getType(j) == "tt_aspectContainer" then
  29.        asp = peripheral.call(j, "getAspects")
  30.        countasp = peripheral.call(j, "getAspectCount", asp)
  31.        if countasp > 0 then
  32.           essentia[asp] = math.floor(countasp)
  33.        end
  34. --       print(countasp)
  35. --     print(asp..":"..countasp)
  36. --     print(peripheral.getType(j))
  37.      end
  38.   end
  39. end
  40.  
  41. function printEssentia()
  42.   m.setTextColor(colors.white)
  43.   local x = 1
  44.   local y = 1
  45.   monCoord[x] = {}
  46. --  for a = 1,17 do
  47.   for i,j in sortEss(essentia) do
  48.      if j<=20 then m.setTextColor(colors.red) end
  49.      if j<40 and j>20 then m.setTextColor(colors.yellow) end
  50.      if j>=40 then m.setTextColor(colors.green) end
  51.      
  52.      m.setCursorPos(x,y)
  53.      m.write(i)
  54.      m.setCursorPos(x+14,y)
  55.      m.write(tostring(j))
  56. --     print(j)
  57.      monCoord[x][y] = i
  58.      if y < 17 then
  59.         y = y+1
  60.      else
  61.         y = 1
  62.         x = x+17
  63.         monCoord[x] = {}  
  64.      end
  65.   end
  66. -- end
  67.  m.setTextColor(colors.white)
  68. end
  69.  
  70. function getClick()
  71.    local event,side,x,y = os.pullEvent()
  72.    if event=="monitor_touch" then
  73.      if button.checkxy(x,y) then
  74.         print("button")
  75.      else
  76.         if rowsActive then
  77.           fillAmt = 0
  78.           print(x..":"..x-(x%17)+1)
  79.           print(monCoord[x-(x%17)+1][y])
  80.           currEssentia = monCoord[x-(x%17)+1][y]
  81.           if currEssentia ~= nil then
  82.           if essentia[currEssentia] < 64 then
  83.              fillTable2()
  84.           else
  85.              m.clear()
  86.              button.label(1,10, currEssentia.." is already full.  Please choose another.")
  87.              sleep(3)
  88.              refresh()
  89.           end
  90.           end
  91.         end
  92.      end
  93.    end
  94. end
  95.  
  96. function refresh()
  97.    button.flash("Refresh")
  98.    m.clear()
  99.    scanEssentia()
  100.    printEssentia()
  101.    print("Refreshed")
  102.    button.screen()
  103. end
  104.  
  105. function fillTable()
  106.    rowsActive = true
  107.    button.clearTable()
  108.    button.setTable("Refresh", refresh, "", 15, 35, 19, 19)
  109.    button.screen()
  110. end
  111.  
  112. function addEss(num)
  113.    fillAmt = fillAmt + num
  114.    if fillAmt < 0 then fillAmt = 0 end
  115.    if fillAmt > 64-essentia[currEssentia] then fillAmt = 64-essentia[currEssentia] end
  116.    m.clear()
  117.    fillTable2()
  118. end
  119.  
  120. function fillEss()
  121.    local essData = {}
  122.    essData[1] = currEssentia
  123.    essData[2] = fillAmt
  124.    local sendData = ""
  125.    sendData = textutils.serialize(essData)
  126.    rednet.send(turtleID, sendData)
  127.    m.clear()
  128.    button.label(7, 10, "Waiting for aspects to finish cooking....")
  129.    button.label(7, 12, "Don't forget to refresh the screen after")
  130.    button.label(7, 13, "the golem is done moving the essentia.")
  131.    rednet.receive()
  132.    m.clear()
  133.    fillTable()
  134.    refresh()
  135. end
  136.  
  137. function cancel()
  138.    m.clear()
  139.    fillTable()
  140.    refresh()
  141. end  
  142.  
  143. function fillTable2()
  144.    rowsActive = false
  145.    button.clearTable()
  146.    m.clear()
  147.    button.label(7, 1, "Essentia: "..currEssentia.." contains "..essentia[currEssentia])
  148.    button.setTable("+1", addEss, 1, 8, 18, 6,6)
  149.    button.setTable("+5", addEss, 5, 20, 30, 6, 6)
  150.    button.setTable("+10", addEss, 10, 32, 42, 6, 6)
  151.    button.setTable("-1", addEss, -1, 8, 18, 8, 8)
  152.    button.setTable("-5", addEss, -5, 20, 30, 8, 8)
  153.    button.setTable("-10", addEss, -10, 32, 42, 8 ,8)
  154.    button.setTable("Refill Jar", addEss, 64-essentia[currEssentia], 8, 42, 10, 10)
  155.    button.setTable("Execute Fill Request", fillEss, "", 8, 42, 16, 18)
  156.    button.setTable("Cancel", cancel, "", 20, 30, 12, 14)
  157.    button.label(7, 4, "Currently Adding "..fillAmt.." "..currEssentia.." essentia.")
  158.    button.screen()
  159. end
  160.  
  161. fillTable()
  162. refresh()
  163. while true do getClick() end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement