Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local args = {...}
- -- channel, lp, me, meDir, fur, furDir, wifi
- local lp = #args > 1 and peripheral.wrap(args[2]) or peripheral.wrap("bottom")
- if not lp.getTurtleConnect() then lp.setTurtleConnect(true) end
- local me = #args > 2 and peripheral.wrap(args[3]) or peripheral.wrap("right")
- local meDir = #args > 3 and args[4] or "east"
- local fur = #args > 4 and peripheral.wrap(args[5]) or peripheral.wrap("front")
- local furDir = #args > 5 and args[6] or "north"
- local wifi = #args > 6 and peripheral.wrap(args[7]) or peripheral.wrap("left")
- local channel = 42
- if #args > 0 then channel = tonumber(args[1]) end
- print("Opening wifi channel ",channel)
- wifi.open(channel)
- local beans = {}
- local function scanBeans()
- local allItems = lp.getAvailableItems()
- --print(#allItems, " items in lp system")
- for i,item in pairs(allItems) do
- if lp.getItemID(item[1]) == 25366 then
- local nbt = lp.getNBTTagCompound(item[1])
- if nbt then
- local name = nbt.value.Aspects.value[1].value.key.value
- if not beans[name] then beans[name] = {lpID=item[1], qty=item[2]}
- else print("duplicate bean: ",name) end
- end
- end
- end
- end
- local function itemInSlot(slot)
- --if turtle.getItemCount(slot) then return 1 else return 0 end
- return turtle.getItemCount(slot)
- end
- local function returnItemToME(slot,amount)
- return me.pullItem(meDir,slot,amount)
- end
- local function clearInventory()
- for i=1,16 do
- if itemInSlot(i) > 0 then returnItemToME(i,itemInSlot(i)) end
- end
- end
- local function requestBean(name, amount)
- if amount > 64 then return 0 end
- local slot = 1
- while slot < 17 and itemInSlot(slot) > 0 do slot = slot + 1 end
- if itemInSlot(slot) > 0 then print("turtle inventory full while trying to request ", name); return 0 end
- --if itemInSlot(1) then if returnItemToME(1,itemInSlot(1)) end
- local result = lp.makeRequest(beans[name].lpID, amount)
- if result ~= "DONE" then
- print("Failed to get ", amount, "name: ", result)
- return 0
- else
- return slot
- end
- end
- local function loadFurnace(amount,slot)
- local movedAmount = 0
- local toMove = amount
- repeat
- movedAmount = movedAmount + fur.pullItem(furDir,slot,toMove)
- if movedAmount then toMove = amount - movedAmount end
- if not movedAmount or toMove > 0 then sleep(0.5) end
- until movedAmount and toMove <= 0
- return movedAmount
- end
- local function loadBean(name, amount)
- local beanInSlot = requestBean(name,amount)
- local movedBeans = 0
- if beanInSlot > 0 then movedBeans = loadFurnace(amount,beanInSlot) end
- if itemInSlot(beanInSlot) == 0 then
- wifi.transmit(channel,channel,textutils.serialize(movedBeans))
- end
- end
- local function initJars()
- local numBeans = 0
- --local lastBean = ""
- clearInventory()
- turtle.select(1)
- for name,bean in pairs(beans) do
- numBeans = numBeans + 1
- --if numBeans < 16 then
- -- local beanInSlot = requestBean(name,1)
- -- if beanInSlot > 0 then loadFurnace(1,beanInSlot) end
- loadBean(name,1)
- end
- print(numBeans," beans in lp system")
- end
- if args[1] == "init" then
- scanBeans()
- initJars()
- else
- scanBeans()
- while true do
- local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
- local msg = textutils.unserialize(message)
- print("Jars requesting ", msg[2], " ", msg[1])
- if beans[msg[1]] then loadBean(msg[1],tonumber(msg[2]))
- else
- print(msg[1], " not in bean list")
- sleep(10)
- os.reboot()
- end
- -- if beans["gelum"] then print("gelum is in fact in the bean list") end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement