ratismal

dw - aspects

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