Advertisement
akihex

beanCounter

Aug 4th, 2014
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.61 KB | None | 0 0
  1. local args = {...}
  2. -- channel, lp, me, meDir, fur, furDir, wifi
  3.  
  4. local lp = #args > 1 and peripheral.wrap(args[2]) or peripheral.wrap("bottom")
  5. if not lp.getTurtleConnect() then lp.setTurtleConnect(true) end
  6.  
  7. local me = #args > 2 and peripheral.wrap(args[3]) or peripheral.wrap("right")
  8. local meDir = #args > 3 and args[4] or "east"
  9.  
  10. local fur = #args > 4 and peripheral.wrap(args[5]) or peripheral.wrap("front")
  11. local furDir = #args > 5 and args[6] or "north"
  12.  
  13. local wifi = #args > 6 and peripheral.wrap(args[7]) or peripheral.wrap("left")
  14. local channel = 42
  15. if #args > 0 then channel = tonumber(args[1]) end
  16. print("Opening wifi channel ",channel)
  17. wifi.open(channel)
  18.  
  19. local beans = {}
  20.  
  21. local function scanBeans()
  22.   local allItems = lp.getAvailableItems()
  23.   --print(#allItems, " items in lp system")
  24.   for i,item in pairs(allItems) do
  25.     if lp.getItemID(item[1]) == 25366 then
  26.       local nbt = lp.getNBTTagCompound(item[1])
  27.       if nbt then
  28.         local name = nbt.value.Aspects.value[1].value.key.value
  29.         if not beans[name] then beans[name] = {lpID=item[1], qty=item[2]}
  30.         else print("duplicate bean: ",name) end
  31.       end
  32.     end
  33.   end
  34. end
  35.  
  36. local function itemInSlot(slot)
  37.   --if turtle.getItemCount(slot) then return 1 else return 0 end
  38.   return turtle.getItemCount(slot)
  39. end
  40.  
  41. local function returnItemToME(slot,amount)
  42.   return me.pullItem(meDir,slot,amount)
  43. end
  44.  
  45. local function clearInventory()
  46.   for i=1,16 do
  47.     if itemInSlot(i) > 0 then returnItemToME(i,itemInSlot(i)) end
  48.   end
  49. end
  50.  
  51. local function requestBean(name, amount)
  52.   if amount > 64 then return 0 end
  53.   local slot = 1
  54.   while slot < 17 and itemInSlot(slot) > 0 do slot = slot + 1 end
  55.   if itemInSlot(slot) > 0 then print("turtle inventory full while trying to request ", name); return 0 end
  56.   --if itemInSlot(1) then if returnItemToME(1,itemInSlot(1)) end
  57.   local result = lp.makeRequest(beans[name].lpID, amount)
  58.   if result ~= "DONE" then
  59.     print("Failed to get ", amount, "name: ", result)
  60.     return 0
  61.   else
  62.     return slot
  63.   end
  64. end
  65.  
  66. local function loadFurnace(amount,slot)
  67.   local movedAmount = 0
  68.   local toMove = amount
  69.   repeat
  70.     movedAmount = movedAmount + fur.pullItem(furDir,slot,toMove)
  71.     if movedAmount then toMove = amount - movedAmount end
  72.     if not movedAmount or toMove > 0 then sleep(0.5) end
  73.   until movedAmount and toMove <= 0
  74.   return movedAmount
  75. end
  76.  
  77. local function loadBean(name, amount)
  78.   local beanInSlot = requestBean(name,amount)
  79.   local movedBeans = 0
  80.   if beanInSlot > 0 then movedBeans = loadFurnace(amount,beanInSlot) end
  81.   if itemInSlot(beanInSlot) == 0 then
  82.     wifi.transmit(channel,channel,textutils.serialize(movedBeans))
  83.   end
  84. end
  85.  
  86. local function initJars()
  87.   local numBeans = 0
  88.   --local lastBean = ""
  89.   clearInventory()
  90.   turtle.select(1)
  91.   for name,bean in pairs(beans) do
  92.     numBeans = numBeans + 1
  93.     --if numBeans < 16 then
  94. --    local beanInSlot = requestBean(name,1)
  95. --    if beanInSlot > 0 then loadFurnace(1,beanInSlot) end
  96.     loadBean(name,1)
  97.   end
  98.   print(numBeans," beans in lp system")
  99. end
  100.  
  101. if args[1] == "init" then
  102.   scanBeans()
  103.   initJars()
  104. else
  105.   scanBeans()
  106.   while true do
  107.     local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
  108.     local msg = textutils.unserialize(message)
  109.     print("Jars requesting ", msg[2], " ", msg[1])
  110.     if beans[msg[1]] then loadBean(msg[1],tonumber(msg[2]))
  111.     else
  112.       print(msg[1], " not in bean list")
  113.       sleep(10)
  114.       os.reboot()
  115.     end
  116.    -- if beans["gelum"] then print("gelum is in fact in the bean list") end
  117.   end
  118. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement