Cypher121

storage

Jun 28th, 2014
370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. rednet.open("top")
  2. m = peripheral.wrap("monitor_0")
  3. os.loadAPI("button")
  4.  
  5. prodID = 22
  6. outID = 51
  7. expID = 26
  8.  
  9. jarType = "tilejar"
  10. voidType = "tilejarvoid"
  11.  
  12. essentia = {}
  13. monCoord = {}
  14.  
  15. add = {}
  16. jars = {}
  17.  
  18. aspList = {
  19.     "Aer", "Alienis", "Aqua", "Arbor", "Auram", "Bestia", "Cognitio", "Corpus",
  20.     "Exanimis", "Fabrico", "Fames", "Gelum", "Granum", "Herba", "Humanus", "Ignis",
  21.     "Instrumentum", "Iter", "Limus", "Lucrum", "Lux", "Machina", "Messis", "Metallum",
  22.     "Meto", "Mortuus", "Motus", "Ordo", "Pannus", "Perditio", "Perfodio", "Permutatio",
  23.     "Potentia", "Praecantatio", "Sano", "Saxum", "Sensus", "Spiritus", "Telum", "Tempestas",
  24.     "Tempus", "Tenebrae", "Terra", "Tutamen", "Vacuos", "Venenum", "Victus", "Vinculum",
  25.     "Vitium", "Vitreus", "Volatus"
  26. }
  27. color = {
  28.     Aer = colors.yellow,
  29.     Alienis = colors.purple,
  30.     Aqua = colors.blue,
  31.     Arbor = colors.brown,
  32.     Auram = colors.pink,
  33.     Bestia = colors.brown,
  34.     Cognitio = colors.pink,
  35.     Corpus = colors.magenta,
  36.     Exanimis = colors.green,
  37.     Fabrico = colors.gray,
  38.     Fames = colors.red,
  39.     Gelum = colors.lightGray,
  40.     Granum = colors.brown,
  41.     Herba = colors.green,
  42.     Humanus = colors.lightGray,
  43.     Ignis = colors.red,
  44.     Instrumentum = colors.blue,
  45.     Iter = colors.red,
  46.     Limus = colors.lime,
  47.     Lucrum = colors.yellow,
  48.     Lux = colors.yellow,
  49.     Machina = colors.gray,
  50.     Messis = colors.brown,
  51.     Metallum = colors.gray,
  52.     Meto = colors.brown,
  53.     Mortuus = colors.gray,
  54.     Motus = colors.lightGray,
  55.     Ordo = colors.lightGray,
  56.     Pannus = colors.lightGray,
  57.     Perditio = colors.gray,
  58.     Perfodio = colors.lightGray,
  59.     Permutatio = colors.green,
  60.     Potentia = colors.lightGray,
  61.     Praecantatio = colors.purple,
  62.     Sano = colors.red,
  63.     Saxum = colors.gray,
  64.     Sensus = colors.lightBlue,
  65.     Spiritus = colors.lightGray,
  66.     Telum = colors.red,
  67.     Tempestas = colors.lightGray,
  68.     Tempus = colors.lightBlue,
  69.     Tenebrae = colors.gray,
  70.     Terra = colors.green,
  71.     Tutamen = colors.lightBlue,
  72.     Vacuos = colors.lightGray,
  73.     Venenum = colors.lime,
  74.     Victus = colors.red,
  75.     Vinculum = colors.gray,
  76.     Vitium = colors.purple,
  77.     Vitreus = colors.lightBlue,
  78.     Volatus = colors.lightGray
  79. }
  80.  
  81. reqAmount = 0
  82. curEssentia = ""
  83. curAmount = 0
  84.  
  85. rowsActive = true
  86.  
  87. function initFunctions()
  88.   for i = 0, 9 do
  89.     add[i] = function()
  90.       reqAmount = tonumber(reqAmount .. i);
  91.       if (reqAmount > 64) then
  92.         reqAmount = 64
  93.       end
  94.       if (reqAmount > (curAmount + essentia[curEssentia])) then
  95.         reqAmount = curAmount + essentia[curEssentia]
  96.       end
  97.       drawJarGUI()
  98.     end
  99.   end
  100. end
  101.  
  102. function remove()
  103.   reqAmount = string.sub(reqAmount, 1, -2)
  104.   if (reqAmount == "") then
  105.     reqAmount = 0
  106.   end
  107.   reqAmount = tonumber(reqAmount)
  108.   drawJarGUI()
  109. end
  110.  
  111. function export()
  112.   if (reqAmount <= curAmount) then
  113.     reqAmount = curAmount
  114.     drawJarGUI()
  115.   else
  116.     data = {}
  117.     data.ess = curEssentia
  118.     data.amount = reqAmount - curAmount
  119.     rednet.send(expID, textutils.serialize(data))
  120.     print("Pushing " .. data.amount .. " " .. data.ess)
  121.     progress()
  122.   end
  123. end
  124.  
  125. function progress()
  126.   m.clear()
  127.   button.clearTable()
  128.   drawJar()
  129.   m.setCursorPos(30, 5)
  130.   m.write("Filling jar: ")
  131.  
  132.   targetJar = peripheral.wrap(jarType .. "_" .. outID)
  133.   if (targetJar == nil) then
  134.     targetJar = peripheral.wrap(voidType .. "_" .. outID)
  135.   end
  136.  
  137.   while (expID ~= rednet.receive(0.5)) do
  138.     curAmount = getFill(targetJar)
  139.     m.setCursorPos(43, 5)
  140.     m.write(curAmount)
  141.     drawEssentia()
  142.   end
  143.  
  144.   m.clear()
  145.   drawJar()
  146.   curAmount = getFill(targetJar)
  147.   drawEssentia()
  148.   m.setCursorPos(35, 5)
  149.   m.write("Done")
  150.   m.setCursorPos(32, 6)
  151.   m.write("Remove jar")
  152. end
  153.      
  154. function sortEss(t)
  155.   local keys = {}
  156.   for k in pairs(t) do table.insert(keys, k) end
  157.   table.sort(keys)
  158.    
  159.   local i = 0
  160.   return function()
  161.     i = i+1
  162.     if keys[i] then
  163.       return keys[i], t[keys[i]]
  164.     end
  165.   end
  166. end
  167.  
  168. function mapJars()
  169.   pers = peripheral.getNames()  
  170.   for i, v in pairs(pers) do
  171.     if (peripheral.getType(v) == voidType) then
  172.       if (string.sub(v, string.find(v, "_[^_]*$") + 1, -1) ~= outID) then
  173.         table.insert(jars, peripheral.wrap(v))
  174.       end
  175.     end
  176.   end
  177. end
  178.  
  179. function scanEssentia()
  180.   for i, v in pairs(aspList) do
  181.     essentia[v] = 0
  182.   end
  183.   for i,j in pairs(jars) do
  184.     aspects = j.getAspects()
  185.     if (aspects[1] ~= nil) then
  186.       essentia[aspects[1].name] = aspects[1].quantity
  187.     end
  188. --  print(countasp)
  189. --  print(asp..":"..countasp)
  190. --  print(peripheral.getType(j))
  191.   end
  192. end
  193.  
  194. function printEssentia()
  195.   m.setTextColor(colors.white)
  196.   local x = 1
  197.   local y = 1
  198.   monCoord[x] = {}
  199. --for a = 1,17 do
  200.   for i,j in sortEss(essentia) do
  201.     if j<=20 then m.setTextColor(colors.red) end
  202.     if j<40 and j>20 then m.setTextColor(colors.yellow) end
  203.     if j>=40 then m.setTextColor(colors.green) end
  204.  
  205.     m.setCursorPos(x,y)
  206.     m.write(i)
  207.     m.setCursorPos(x+14,y)
  208.     m.write(tostring(j))
  209. --  print(j)
  210.     monCoord[x][y] = i
  211.     if y < 17 then
  212.       y = y+1
  213.     else
  214.       y = 1
  215.       x = x+17
  216.       monCoord[x] = {}  
  217.     end
  218.   end
  219.   m.setTextColor(colors.white)
  220. end
  221.  
  222. function drawEssentia()
  223.   m.setBackgroundColor(color[curEssentia])
  224.   for i = (19 - math.floor(curAmount / 4)), 18 do
  225.     for j = 2, 19 do
  226.       m.setCursorPos(j, i)
  227.       m.write(" ")
  228.     end
  229.   end
  230.   m.setBackgroundColor(colors.black)
  231. end
  232.  
  233. function getFill(per)
  234.   aspInfo = per.getAspects()[1]
  235.   if (aspInfo == nil) then
  236.     return 0
  237.   end
  238.   return aspInfo.quantity
  239. end
  240.  
  241. function outline(x1, x2, y1, y2)
  242.   m.setBackgroundColor(colors.white)
  243.   for i = x1, x2 do
  244.     m.setCursorPos(i, y1)
  245.     m.write(" ")
  246.     m.setCursorPos(i, y2)
  247.     m.write(" ")
  248.   end
  249.   for i = (y1 + 1), (y2 - 1) do
  250.     m.setCursorPos(x1, i)
  251.     m.write(" ")
  252.     m.setCursorPos(x2, i)
  253.     m.write(" ")
  254.   end
  255.   m.setBackgroundColor(colors.black)
  256. end
  257.  
  258. function drawJar()
  259.   outline(1, 20, 2, 19)
  260.   m.setBackgroundColor(colors.brown)
  261.   for i = 4, 17 do
  262.     m.setCursorPos(i, 1)
  263.     m.write(" ")
  264.   end
  265.   for i = 6, 15 do
  266.     m.setCursorPos(i, 2)
  267.     m.write(" ")
  268.   end
  269.   m.setBackgroundColor(colors.black)
  270. end
  271.  
  272. function drawJarGUI()
  273.   rowsActive = false
  274.   button.clearTable()
  275.   m.clear()
  276.  
  277.   for i = 0, 8 do
  278.     button.setTable(tostring(i + 1),
  279.                     add[i + 1], false,
  280.                     34 + ((i % 3) * 5),
  281.                     38 + ((i % 3) * 5),
  282.                     6 + math.floor(i / 3) * 3,
  283.                     8 + math.floor(i / 3) * 3)
  284.   end
  285.   button.setTable("0", add[0], false, 39, 43, 15, 17)
  286.   button.setTable("<", remove, false, 34, 38, 15, 17)
  287.   button.setTable("Start", export, false, 44, 48, 15, 17)
  288.  
  289.   button.screen()
  290.  
  291.   drawJar()
  292.   req_x = 47
  293.   if (reqAmount > 9) then
  294.     req_x = req_x - 1
  295.   end
  296.   m.setCursorPos(req_x, 4)
  297.   m.write(reqAmount)
  298.   outline(34, 48, 3, 5)
  299.   m.setTextColor(colors.black)
  300.   m.setCursorPos(49, 4)
  301.   m.write(" ")
  302.   m.setTextColor(colors.white)
  303.   drawEssentia()
  304.   m.setCursorPos(math.floor((54 - string.len(curEssentia)) / 2), 1)
  305.   m.write(curEssentia)
  306. end
  307.  
  308. function getClickOrJar()
  309.   local event, side, x, y = os.pullEvent()
  310.   print(event)
  311.   if (event == "monitor_touch") then
  312.     if (not button.checkxy(x,y)) then
  313.       if (peripheral.isPresent(jarType .. "_" .. outID) or peripheral.isPresent(voidType .. "_" .. outID)) then
  314.         curEssentia = monCoord[x-(x%17)+1][y]
  315.         targetJar = peripheral.wrap(jarType .. "_" .. outID) or peripheral.wrap(voidType .. "_" .. outID)
  316.         if (targetJar.getAspects()[1] ~= nil) then
  317.           return
  318.         end
  319.         curAmount = getFill(targetJar)
  320.         initFunctions()
  321.         reqAmount = 0
  322.         scanEssentia()
  323.         drawJarGUI()
  324.       end
  325.     end
  326.   elseif (event == "peripheral") then
  327.     if ((side == (jarType .. "_" .. outID)) or (side == (voidType .. "_" .. outID))) then
  328.       t = peripheral.call(side, "getAspects")[1]
  329.       if (t ~= nil) then
  330.         curEssentia = t.name
  331.         curAmount = t.quantity
  332.         initFunctions()
  333.         reqAmount = 0
  334.         scanEssentia()
  335.         drawJarGUI()
  336.       end
  337.     end
  338.   elseif ((event == "peripheral_detach") and not rowsActive) then
  339.     fillTable()
  340.     refresh()
  341.   end
  342. end
  343.  
  344. function refClick()
  345.   return refresh(true)
  346. end
  347.  
  348. function refresh(flash)
  349.   if flash then
  350.     button.flash("Refresh")
  351.   end
  352.   m.clear()
  353.   scanEssentia()
  354.   printEssentia()
  355.   print("Refreshed")
  356.   button.screen()
  357.   button.label(24, 19, "Idle")
  358. end
  359.  
  360. function refill()
  361.   button.flash("Refill")
  362.   scanEssentia()
  363.   for asp, qty in sortEss(essentia) do
  364.     if (qty < 64) then
  365.       fillEss(asp, 64 - qty)
  366.     end
  367.   end
  368.   refresh(false)
  369. end
  370.  
  371. function fillTable()
  372.   rowsActive = true
  373.   button.clearTable()
  374.   button.setTable("Refresh", refClick, false, 5, 20, 19, 19)
  375.   button.setTable("Refill", refill, false, 30, 45, 19, 19)
  376.   button.screen()
  377. end
  378.  
  379. function fillEss(asp, amount)
  380.   local essData = {}
  381.   essData.asp = string.lower(asp)
  382.   essData.amount = amount
  383.  
  384.   sendData = textutils.serialize(essData)
  385.   rednet.send(prodID, sendData)
  386.   button.label(24, 19, "Busy")
  387.   print("Sent request for " .. amount .. " " .. asp .. " to #" .. prodID)
  388.   rednet.receive()
  389.   fillTable()
  390.   refresh(false)
  391. end
  392.  
  393. mapJars()
  394. print(table.getn(jars))
  395. fillTable()
  396. refresh(false)
  397. while true do getClickOrJar() end
Advertisement
Add Comment
Please, Sign In to add comment