Advertisement
whitaleedee

essentia main

Sep 28th, 2014
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.29 KB | None | 0 0
  1. wired = peripheral.wrap("front") -- Wired modem
  2. wireless = peripheral.wrap("bottom") -- Wireless modem
  3. levelMon = peripheral.wrap("top")
  4. statusMon = peripheral.wrap("back")
  5. levelColors = { 2048, 512, 8, 16, 16384 }
  6. levelLimits = { 60, 45, 32, 15, 5 }
  7.  
  8. aspects = {}
  9. filename = "MissingAspects"
  10.  
  11. channel = 300 -- This is the channel all the messageing traffic goes on
  12. wireless.open(channel)
  13.  
  14. function getCons()
  15.     return wired.getNamesRemote()
  16. end
  17.  
  18. function split(inputstr, sep) -- splits a string into a table
  19.     if sep == nil then
  20.         sep = "%s"
  21.     end
  22.     t={}
  23.     i=1
  24.     for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
  25.         t[i] = str
  26.         i = i + 1
  27.     end
  28.     return t
  29. end
  30.  
  31. function splitStr(pStr, pChar) -- splits string into two variables
  32.     return string.match(pStr, "([^" .. pChar .. "]+)" .. pChar .. "([^" .. pChar .. "]+)")
  33. end
  34.  
  35. function send(msg)
  36.     print("sending: " .. msg)
  37.     wireless.transmit(channel, 1, msg)
  38. end
  39.  
  40. function receive()
  41.     e = {os.pullEvent("modem_message")}
  42.     print("received: " .. e[5])
  43.     return e[5]
  44. end
  45.  
  46. function getColor(count)
  47.     for k,v in pairs(levelLimits) do
  48.         if tonumber(count) <= tonumber(v) then
  49.             levelMon.setTextColor(levelColors[k])
  50.         end
  51.     end
  52. end
  53.  
  54. function updateJarLevelMonitor()
  55.     levelMon.clear()
  56.     local xmax, ymax = levelMon.getSize()
  57.     levelMon.setCursorPos(2,1)
  58.     for k,v in pairs(aspects) do
  59.         local x, y = levelMon.getCursorPos()
  60.         if tonumber(x+5) > tonumber(xmax) then
  61.             levelMon.setCursorPos(2,y+1)
  62.             x, y = levelMon.getCursorPos()
  63.         end
  64.         getColor(v.quantity)
  65.         levelMon.write(string.sub(v.name, 1, 5))
  66.         levelMon.setCursorPos(x+6,y)
  67.         levelMon.write(tostring(v.quantity))
  68.         levelMon.setCursorPos(x+10,y)
  69.     end
  70.    
  71.     levelMon.setTextColor(colors.purple)
  72.     levelMon.write("    Hit Screen to Refresh Jar Values")
  73. end
  74.  
  75. function length(pTable)
  76.     local count = 0
  77.     for k,v in pairs(pTable) do count = count + 1 end
  78.     return count
  79. end
  80.  
  81. local btnColors = {colors.purple, colors.blue}
  82. function displayRefillButton(col)
  83.     statusMon.setTextColor(btnColors[col])
  84.     statusMon.setCursorPos(1,3) statusMon.write("+-------------+")
  85.     statusMon.setCursorPos(1,4) statusMon.write("|   HIT HERE  |")
  86.     statusMon.setCursorPos(1,5) statusMon.write("|      TO     |")
  87.     statusMon.setCursorPos(1,6) statusMon.write("| REFILL JARS |")
  88.     statusMon.setCursorPos(1,7) statusMon.write("+-------------+")
  89. end
  90.  
  91. function updateStatusMonitor(msg)
  92.     statusMon.clear()
  93.     statusMon.setTextColor(colors.purple)
  94.     statusMon.setTextScale(.5)
  95.  
  96.     local msgTable = split(msg, " ")
  97.     local l = tonumber(length(msgTable))
  98.     local y = 5 - math.ceil(l/2)
  99.     for k,v in pairs(msgTable) do
  100.         local x = 8 - math.ceil(string.len(v)/2)
  101.         statusMon.setCursorPos(x,y) statusMon.write(v)
  102.         y = y+1
  103.     end
  104. end
  105.  
  106. function updateAspects()
  107.     cons = getCons()
  108.     aspects = {}
  109.    
  110.     local i = 0
  111.     for k,v in pairs(cons) do
  112.         updateStatusMonitor("Checking Aspect Jars")
  113.         local jar = peripheral.wrap(v)
  114.         if jar then
  115.             local aspectTbl = jar.getAspects()
  116.            
  117.             if aspectTbl then table.insert(aspects, aspectTbl[1]) end
  118.         end
  119.         i = (i+1)%5
  120.     end
  121.    
  122.     updateStatusMonitor("Sorting Aspect Table")
  123.     local sort_func = function( a,b ) return a.name < b.name end
  124.     table.sort( aspects, sort_func )
  125. end
  126.  
  127. function addToFile(str)
  128.     local writeType = ""
  129.     if fs.exists(filename) then
  130.         writeType = "a"
  131.     else
  132.         writeType = "w"
  133.     end
  134.    
  135.     local file = fs.open(filename, writeType)
  136.     file.writeLine(str)
  137.     file.close()
  138. end
  139.  
  140. function refillAll()
  141.     updateAspects()
  142.     send(textutils.serialize(aspects))
  143.    
  144.     for i=1,3 do
  145.         updateStatusMonitor("Waiting for Turtles " .. to string(4-tonumber(i)))
  146.         local missingData = textutils.unserialize(receive())
  147.         for k,v in pairs(missingData) do
  148.             addToFile(v.name .. "," .. v.quantity)
  149.         end
  150.     end
  151. end
  152.  
  153. while true do
  154.     updateAspects()
  155.     updateJarLevelMonitor()
  156.     displayRefillButton(1)
  157.  
  158.     local e = {os.pullEvent("monitor_touch")}
  159.     if e and e[1] == "monitor_touch" then
  160.         if e[2] == "top" then
  161.             updateJarLevelMonitor()
  162.         elseif e[2] == "back" then
  163.             displayRefillButton(2)
  164.             sleep(.1)
  165.             displayRefillButton(1)
  166.                        
  167.             refillAll()
  168.             send("check if done")
  169.             updateStatusMonitor("Processing Essentia")
  170.             local msg = receive()
  171.             if msg and msg == "done" then
  172.                 updateStatusMonitor("Done!")
  173.             end
  174.            
  175.             updateJarLevelMonitor()
  176.             sleep(1)
  177.         end
  178.     end
  179. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement