Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local localChannel=666
- if fs.exists("event")==false then shell.run("pastebin get L7KYC10V event") end
- os.loadAPI("event")
- if fs.exists("utils")==false then shell.run("pastebin get c4CBkgKV utils") end
- os.loadAPI("utils")
- local taskQue={
- }
- local turtles={
- }
- local chests={
- }
- for _,name in pairs(peripheral.getNames()) do
- local per=peripheral.wrap(name)
- if per.pushItem~=nil then
- chests[#chests+1]={chest=per}
- end
- end
- local items={}
- local modems={}
- local drop={x=468,y=48,z=1072}
- function init()
- modem=peripheral.wrap("back")
- modem.open(localChannel)
- modems["back"]=modem
- modems["wired"]=modem
- wireless=peripheral.wrap("top")
- wireless.open(localChannel)
- modems["top"]=wireless
- modems["wireless"]=wireless
- end
- init()
- function indexItems()
- items={}
- for chestID,chest in pairs(chests) do
- for i,stack in pairs(chest.chest.getAllStacks()) do
- local data=stack.all()
- if items[string.lower(data.display_name)]==nil then
- items[string.lower(data.display_name)]={[chestID]=data.qty}
- else
- if items[string.lower(data.display_name)][chestID]==nil then
- items[string.lower(data.display_name)][chestID]=data.qty
- else
- items[string.lower(data.display_name)][chestID]=items[string.lower(data.display_name)][chestID]+data.qty
- end
- end
- end
- end
- end
- indexItems()
- function getItemCount(name)
- local item=items[string.lower(name)]
- if item==nil then return 0 end
- local count=0
- for chestID,itemCount in pairs(item) do
- count=count+itemCount
- end
- return count
- end
- function getItemPosition(name,count)
- local item=items[string.lower(name)]
- if item==nil then return end
- local positions={}
- if count==0 then
- count=1
- end
- local remainder=count
- for chestID,itemCount in pairs(item) do
- remainder=remainder-itemCount
- positions[#positions+1]={pos=chests[chestID].pos,count=itemCount}
- if remainder<=0 then
- return positions
- end
- end
- return positions
- end
- function getIdleTurtle()
- for id,turtle in pairs(turtles) do
- if turtle.status=="idle" then
- return turtle
- end
- end
- return nil
- end
- function statusUpdate(channel,turtle,previous,new)
- print("Que:\n"..textutils.serialise(taskQue))
- if new=="idle" then
- turtle.task=nil
- if #taskQue==0 then return end
- modems["wireless"].transmit(channel,localChannel,taskQue[1])
- for id,data in ipairs(taskQue) do
- taskQue[id-1]=data
- end
- taskQue[0]=nil
- taskQue[#taskQue]=nil
- end
- end
- event.addHandler("onTurtleStatusChange",statusUpdate)
- local messageHandles={
- ["ping"]=function(replyChannel,message)
- if turtles[replyChannel]==nil then return end
- return "pong"
- end,
- ["getItemCount"]=function(replyChannel,message)
- indexItems()
- local itemName=utils.split(message,"|",2)
- return "returnItemCount|"..getItemCount(itemName)
- end,
- ["requestItemList"]=function(replyChannel,message)
- indexItems()
- local itemString=""
- for itemname,chests in pairs(items) do
- itemString=itemString..itemname..","..getItemCount(itemname)..";"
- end
- return "returnItemList|"..itemString
- end,
- ["requestTurtles"]=function(replyChannel,message)
- local turtleString=""
- for channel,turtle in pairs(turtles) do
- turtleString=turtleString..channel..","..turtle.status..","..tostring(turtle.task)..";"
- end
- return "returnTurtles|"..turtleString
- end,
- ["requestQue"]=function(replyChannel,message)
- local queString=""
- for _,queCommand in pairs(taskQue) do
- queString=queString..string.gsub(queCommand,"|",";").."|"
- end
- return "returnQue|"..queString
- end,
- ["requestItem"]=function(replyChannel,message)
- indexItems()
- local itemName=utils.split(message,"|",2)
- local count=tonumber(utils.split(message,"|",3))
- print("Item request: "..itemName.."x"..count)
- local remainder=count
- for chestID,chest in pairs(chests) do
- for slot,stack in pairs(chest.chest.getAllStacks()) do
- local data=stack.all()
- if string.lower(data.display_name)==itemName then
- print(slot..":"..count)
- local count=data.qty
- if count>remainder then
- chest.chest.pushItem("up",slot,remainder)
- remainder=0
- else
- chest.chest.pushItem("up",slot,count)
- remainder=remainder-count
- end
- if remainder<=0 then
- return
- end
- end
- end
- end
- end,
- ["getItemPosition"]=function(replyChannel,message)
- indexItems()
- local itemName=utils.split(message,"|",2)
- local posString=""
- local positions=getItemPosition(itemName)
- if positions==nil then return end
- for _,pos in pairs(positions) do
- posString=posString..x..","..y..","..z.."|"
- end
- return posString
- end,
- ["sendToTurtles"]=function(replyChannel,message)
- local turtleChannel=utils.split(message,"|",2)
- local message=utils.split(message,"|",3)
- local turtle=turtles[replyChannel]
- turtle.task=message
- turtle.status="working"
- end,
- ["indexItems"]=function(replyChannel,message)
- indexItems()
- return "confirmed"
- end,
- ["assign"]=function(replyChannel)
- if turtles[replyChannel]==nil then
- turtles[replyChannel]={
- channel=replyChannel,
- status="idle",
- }
- event.trigger("onTurtleAssign",replyChannel,turtles[replyChannel],"confirmed")
- event.trigger("onTurtleStatusChange",replyChannel,turtles[replyChannel],false,"idle")
- return "confirmed"
- else
- event.trigger("onTurtleAssign",replyChannel,turtles[replyChannel],"denied")
- return "denied"
- end
- end,
- ["status"]=function(replyChannel,message)
- if turtles[replyChannel]==nil then
- return "denied"
- end
- local previous=turtles[replyChannel].status
- local status=utils.split(message,"|",2)
- turtles[replyChannel].status=status
- event.trigger("onTurtleStatusChange",replyChannel,turtles[replyChannel],previous,status)
- end,
- ["requestDropoff"]=function(replyChannel,message)
- return "returnDropoff|"..drop.x..","..drop.y..","..drop.z
- end,
- ["requestRestPos"]=function(replyChannel,message)
- return "returnHomePos|"..rest.x..","..rest.y..","..rest.z
- end,
- }
- function handleMessages(side,channel,replyChannel,message)
- print(replyChannel.." : "..message)
- local messageType=utils.split(message,"|",1)
- if messageHandles[messageType]==nil then return end
- local returnMessage=messageHandles[messageType](replyChannel,message)
- if returnMessage==nil then return end
- modems[side].transmit(replyChannel,localChannel,returnMessage)
- end
- event.addHandler("modem_message",handleMessages)
- while true do
- event.handleCCEvents()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement