Advertisement
Guest User

aspects code for dw20's LP essentia display system

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