akihex

jarHead

Aug 5th, 2014
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.53 KB | None | 0 0
  1. local args = {...}
  2. local jarReScanDelay = 30
  3.  
  4. local jars = {}
  5. local jarMaxAmount = 64
  6. local wifi = nil
  7. local channel = 42
  8. if #args > 0 then channel = tonumber(args[1]) end
  9. local guiChannel = #args > 1 and tonumber(args[2]) or 46
  10.  
  11. local function scanConnections()
  12.   local numJars = 0
  13.   local devList = peripheral.getNames()
  14.   for _,dev in pairs(devList) do
  15.     if peripheral.getType(dev) == "tilejarvoid" then
  16.       local jar = peripheral.wrap(dev)
  17.       if jar then jars[#jars+1] = {dev=jar, name=dev}; numJars = numJars + 1 end
  18.     elseif peripheral.getType(dev) == "modem" and peripheral.call(dev,"isWireless") then wifi = peripheral.wrap(dev)
  19.     end
  20.   end
  21.   return {njars=numJars}
  22. end
  23.  
  24. local function scanJars()
  25.   for _,jar in pairs(jars) do
  26.     if jar.dev and jar.dev.getAspects() and jar.dev.getAspects()[1] then
  27.       if jar.dev.getAspects()[1].quantity < jarMaxAmount then
  28.         print(jar.dev.getAspects()[1].quantity," ",string.lower(jar.dev.getAspects()[1].name))
  29.         --call turtle
  30.         if wifi then
  31.           local event, modemSide, senderChannel, replyChannel, message, senderDistance = nil, nil, nil, nil, nil, nil
  32.           local again = true
  33.           local timeOut
  34.           repeat
  35.             if again then
  36.               timeOut = os.startTimer(15)
  37.               wifi.transmit(channel,channel,textutils.serialize({string.lower(jar.dev.getAspects()[1].name),jarMaxAmount-jar.dev.getAspects()[1].quantity}))
  38.             end
  39.             again = false
  40.             event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent()
  41.             if event == "modem_message" and senderChannel == channel then
  42.               local msg = textutils.unserialize(message)
  43.               print(msg, " of ", jarMaxAmount-jar.dev.getAspects()[1].quantity, " requested beans moved")
  44.             elseif event == "modem_message" and senderChannel == guiChannel then
  45.               --os.queueEvent(event, side, senderChannel, replyChannel, message, senderDistance)
  46.             elseif event == "timer" and modemSide == timeOut then again = true
  47.             end
  48.           until event == "modem_message" and senderChannel == channel
  49.         else print("no wireless modem found")
  50.         end
  51.       end
  52.     end
  53.   end
  54. end
  55.  
  56. local function getJars()
  57.   local myJars = {}
  58.   for _,jar in pairs(jars) do
  59.     if jar.dev and jar.dev.getAspects() and jar.dev.getAspects()[1] then
  60.       myJars[string.lower(jar.dev.getAspects()[1].name)] = jar.dev.getAspects()[1].quantity      
  61.     end
  62.   end
  63.   return myJars
  64. end
  65.  
  66. print(scanConnections().njars, " connected jars")
  67.  
  68. if wifi then
  69.   print("Opening wifi channel ",channel)
  70.   wifi.open(channel)
  71.   print("Opening wifi channel ",guiChannel)
  72.   wifi.open(guiChannel)
  73. else return 0
  74. end
  75.  
  76. local timer
  77.  
  78. local function scanLoop()
  79.   scanJars()
  80.   timer = os.startTimer(jarReScanDelay)
  81.   while true do  
  82.     local event, side, senderChannel, replyChannel, message, senderDistance = os.pullEvent()
  83.     if event == "timer" and side == timer then
  84.       scanJars()
  85.       timer = os.startTimer(jarReScanDelay)
  86.     --elseif event == "modem_message" then
  87.     end
  88.   end
  89. end
  90.  
  91. local function answerGUI()
  92.   while true do
  93.     local event, side, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
  94.     if senderChannel == guiChannel then
  95.       wifi.transmit(guiChannel,guiChannel,getJars())
  96.     else
  97.       --os.queueEvent(event, side, senderChannel, replyChannel, message, senderDistance)
  98.     end
  99.   end
  100. end
  101.  
  102. parallel.waitForAny(scanLoop,answerGUI)
Advertisement
Add Comment
Please, Sign In to add comment