Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ----------------------------Definitions--------------------------
- homecoordinate={1,0}--home coordinate of the turtle
- fuelcoordinate={2,0}
- coordinate={homecoordinate[1],homecoordinate[2]}--sets current coordinate to home coordinate
- local rednetsleep=20--rednet timeout in seconds
- local rednetsleepcycles=6 --wait X number of rednet timeouts before sorting items despite turtle not full
- ---------------------------------Functions-------------------------------
- function moveto(ic,jc) --Function for moving to specified chest coordinate
- deltax=ic-coordinate[1] --calculate distance to target from current coordinate
- deltay=jc-coordinate[2]
- --print("Deltas: "..deltax..","..deltay)
- -- Move to first coordinate
- if deltay>0 then
- for i=1,deltay do
- while not turtle.forward() do
- sleep(1)
- end
- end
- elseif deltay<0 then
- for i=1,math.abs(deltay) do
- while not turtle.back() do
- sleep(1)
- end
- end
- end
- coordinate[2]=jc
- -- Move to second coordinate
- if deltax>0 then
- turtle.turnRight()
- for i=1,deltax do
- while not turtle.forward() do
- sleep(1)
- end
- end
- turtle.turnLeft()
- elseif deltax<0 then
- turtle.turnLeft()
- for i=1,math.abs(deltax) do
- while not turtle.forward() do
- sleep(1)
- end
- end
- turtle.turnRight()
- end
- coordinate[1]=ic
- end
- function resethome()
- for i=1,16 do
- while not turtle.forward() do
- local success,hometest=turtle.inspectDown()
- if success and hometest.name=="enderstorage:ender_storage" then
- turtle.turnRight()
- turtle.turnRight()
- return
- else
- turtle.turnLeft()
- break
- end
- end
- end
- end
- ------------------------Rednet prep-------------------------
- rednet.open("right")
- rednet.host("ItemSortRandom","random1")
- ------------------------Peripherals-------------------------
- inputChest=peripheral.wrap("bottom")
- ------------------------Execution---------------------------
- resethome()
- for i=1,16 do
- turtle.select(i)
- turtle.dropDown()
- end
- print("Starting with fuel level: "..turtle.getFuelLevel())
- while true do --main execution loop
- if turtle.getFuelLevel()<200 then
- moveto(fuelcoordinate[1],fuelcoordinate[2])
- turtle.select(1)
- while turtle.getFuelLevel()<200 do
- turtle.suckDown(64)
- turtle.refuel()
- sleep(1)
- end
- moveto(homecoordinate[1],homecoordinate[2])
- end
- --Receive sorting jobs from main computer:
- jobs={}
- jobtimer=0
- while #jobs<16 and jobtimer<rednetsleepcycles do --wait for a full inventory of jobs, or max 5 minutes
- local message=nil
- local senderId, message, protocol = rednet.receive(rednetsleep)
- if message~=nil then --If message was received
- print(textutils.serialize(message))
- for i=1,#message do
- if #jobs<16 then
- print(message[i][1].."/"..message[i][2].." x"..message[i][3].." to "..message[i][4]..","..message[i][5].." slot "..message[i][6])
- --append entry in message to jobs list and add turtle slot number for item:
- jobs[#jobs+1]={message[i][1],message[i][2],message[i][3],message[i][4],message[i][5],message[i][6],#jobs+1}
- for j=1,inputChest.size() do --find item for job in input chest
- inputItem=inputChest.getItemMeta(j)
- if inputItem~= nil then
- if inputItem.name == message[i][1] and inputItem.damage == message[i][2] then --check if item matches
- inputChest.pushItems("up",j,message[i][3],#jobs) --Insert item into turtle
- break
- end
- end
- end
- end
- end
- else
- jobtimer=jobtimer+1
- print("Rednet receive time-out # "..jobtimer)
- end
- end
- --Sort items to chests
- for i =1,#jobs do
- table.sort(jobs, function(a,b) --sort jobs table according to closest to current coordinate
- if (a[4]-coordinate[1])^2+(a[5]-coordinate[2])^2 < (b[4]-coordinate[1])^2+(b[5]-coordinate[2])^2 then return true end
- if (a[4]-coordinate[1])^2+(a[5]-coordinate[2])^2 > (b[4]-coordinate[1])^2+(b[5]-coordinate[2])^2 then return false end
- end)
- moveto(jobs[1][4],jobs[1][5])
- chest=peripheral.wrap("bottom")
- chest.pullItems("up",jobs[1][7],jobs[1][3],jobs[1][6])
- table.remove(jobs,1)
- end
- moveto(homecoordinate[1],homecoordinate[2])
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement