Advertisement
Xyzzy

ess2

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