Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local maxX,maxY = ...
- maxX= tonumber(maxX)
- maxY= tonumber(maxY)
- f=fs.open("log","a")
- f.writeLine("--------------------")
- f.write("maxX= ")
- f.write(maxX)
- f.write(" maxY= ")
- f.writeLine(maxY)
- noCoal=false
- refuel=function()
- success=false
- c=1
- while (not success) and c<16 do
- turtle.select(c)
- success=turtle.refuel()
- end
- if success then
- f.writeLine("Refueled")
- end
- return success
- end
- clearInventory = function()
- for c=1, 16 do
- turtle.select(c)
- item=turtle.getItemDetail()
- if item then
- if not check(item.name) then
- turtle.drop()
- end
- end
- end
- end
- check=function (item)
- if item=="minecraft:chest" or item:match "minecraft:.*stone.*" or item:match "minecraft:dirt.*" or item=="minecraft:gravel" or item:match "chisel:.*" or item=="minecraft:torch" then
- return false
- else
- return true
- end
- end
- find = function ()
- local success, item=turtle.inspectUp()
- if success then
- if check(item.name) then
- turtle.select(1)
- turtle.digUp()
- f.write("Found up: ")
- f.writeLine(item.name)
- end
- end
- success, item=turtle.inspectDown()
- if success then
- if check(item.name) then
- turtle.select(1)
- turtle.digDown()
- f.write("Found down: ")
- f.writeLine(item.name)
- end
- end
- turtle.turnLeft()
- success, item=turtle.inspect()
- if success then
- if check(item.name) then
- turtle.select(1)
- turtle.dig()
- f.write("Found left: ")
- f.writeLine(item.name)
- end
- end
- turtle.turnRight()
- turtle.turnRight()
- success, item=turtle.inspect()
- if success then
- if check(item.name) then
- turtle.select(1)
- turtle.dig()
- f.write("Found right: ")
- f.writeLine(item.name)
- end
- end
- turtle.turnLeft()
- success, item=turtle.inspect()
- if success then
- if check(item.name) then
- turtle.select(1)
- turtle.dig()
- f.write("Found forward: ")
- f.writeLine(item.name)
- end
- end
- end
- local x=0
- local y=0
- local state = "up"
- while x<maxX do --a
- while y<maxY do
- f.write("X=")
- f.write(x)
- f.write(" Y=")
- f.writeLine(y)
- local leftY=maxY-y
- local dist
- if state=="up" then
- dist=x+y
- else
- dist=x+leftY
- end
- local fuelLevel = turtle.getFuelLevel()
- if fuelLevel<= dist then --c
- local fuelFound=false
- local currSelected=1
- repeat
- turtle.select(currSelected)
- fuelFound = turtle.refuel()
- currSelected = currSelected+1
- until fuelFound == true or currSelected>16
- if fuelFound==false then
- f.writeLine("Fuel not found")
- else f.writeLine("Refueled")
- end
- end --c
- local nCoal=0
- freeSlots = 0
- for c=1,16 do --d
- local item=turtle.getItemDetail(c)
- if item then
- if item.name=="minecraft:coal" then
- nCoal = nCoal+item.count
- end
- if not check(item.name) then
- turtle.select(c)
- turtle.drop()
- freeSlots=freeSlots+1
- end
- else
- freeSlots=freeSlots+1
- end
- end --
- nCoal = nCoal*80+turtle.getFuelLevel()-4
- noCoal=true
- if nCoal<=dist or freeSlots <=1 then
- if nCoal<=dist then
- f.write("Not enough coal for the journey, returning home (left:")
- f.write(nCoal)
- f.write(", coord(")
- f.write(x)
- f.write(",")
- f.write(y)
- f.writeLine("))")
- end
- if freeSlots <=1 then f.writeLine("Not enough slots, returning home") end
- if state=="up" then
- turtle.turnLeft()
- turtle.turnLeft()
- else
- y=leftY
- end
- stopx=x
- stopy=y
- while y>0 do
- while turtle.detect() do
- turtle.select(1)
- turtle.dig()
- end
- clearInventory()
- if turtle.getFuelLevel()<2 then refuel() end
- turtle.forward()
- y=y-1
- f.write("X=")
- f.write(x)
- f.write(" Y=")
- f.writeLine(y)
- end
- turtle.turnRight()
- while x>0 do
- while turtle.detect() do
- turtle.select(1)
- turtle.dig()
- end
- clearInventory()
- if turtle.getFuelLevel()<2 then refuel() end
- turtle.forward()
- x=x-1
- f.write("X=")
- f.write(x)
- f.write(" Y=")
- f.writeLine(y)
- end
- success,storage=turtle.inspect()
- if success and storage.name=="minecraft:chest" then
- f.writeLine("Chest found")
- for c=1,16 do
- turtle.select(c)
- item=turtle.getItemDetail()
- if item and not item.name=="minecraft:coal" then
- dropSuccess=turtle.drop()
- if dropSuccess then
- f.write(item.name)
- f.writeLine(" dropped in chest")
- else
- f.writeLine("Chest full!! Shutting down...")
- f.close()
- os.shutdown()
- end
- end
- end
- if noCoal then
- success=turtle.suckUp()
- if not success then
- f.writeLine("no coal chest or coal, shutting down...")
- f.close()
- os.shutdown()
- else f.writeLine("Refueled from chest")
- end
- end
- f.write("Returning to the last point (")
- f.write(stopx)
- f.write(", ")
- f.write(stopy)
- f.writeLine(")")
- turtle.turnRight()
- turtle.turnRight()
- while x<stopx do
- find()
- if turtle.getFuelLevel()<2 then refuel() end
- while turtle.detect() do
- turtle.select(1)
- turtle.dig()
- end
- clearInventory()
- turtle.forward()
- x=x+1
- f.write("X=")
- f.write(x)
- f.write(" Y=")
- f.writeLine(y)
- end
- f.writeLine("Turning left for y")
- turtle.turnLeft()
- while y<stopy do
- find()
- if turtle.getFuelLevel()<2 then refuel() end
- while turtle.detect() do
- turtle.select(1)
- turtle.dig()
- end
- clearInventory()
- turtle.forward()
- y=y+1
- f.write("X=")
- f.write(x)
- f.write(" Y=")
- f.writeLinw(y)
- end
- if state==down then
- f.writeLine("Turning to the original state")
- turtle.turnLeft()
- turtle.turnLeft()
- y=maxY-leftY
- end
- else
- f.writeLine("No chest, shutting down...")
- f.close()
- os.shutdown()
- end
- end
- find()
- while turtle.detect() do
- turtle.select(1)
- turtle.dig()
- end
- turtle.forward()
- y=y+1
- end
- f.write("Ended x ")
- f.writeLine(x)
- if state=="up" then
- turtle.turnRight()
- else turtle.turnLeft()
- end
- for i=1, 3 do
- find()
- while turtle.detect() do
- turtle.select(1)
- turtle.dig()
- end
- turtle.forward()
- x=x+1
- end
- if state=="up" then
- turtle.turnRight()
- state="down"
- else
- turtle.turnLeft()
- state="up"
- end
- end --a
- f.writeLine("Returning home")
- if state=="up" then
- turtle.turnLeft()
- turtle.turnLeft()
- else
- y=maxY-y
- end
- while y>0 do
- find()
- if turtle.getFuelLevel()<2 then refuel() end
- while turtle.detect() do
- turtle.select(1)
- turtle.dig()
- end
- turtle.forward()
- y=y-1
- f.write("X=") f.write(x) f.write(" Y=") f.writeLine(y)
- clearInventory()
- end
- turtle.turnRight()
- while x>0 do
- if turtle.getFuelLevel()<2 then refuel() end
- find()
- while turtle.detect() do
- turtle.select(1)
- turtle.dig()
- end
- turtle.forward()
- x=x-1
- f.write("X=") f.write(x) f.write(" Y=") f.writeLine(y)
- clearInventory()
- end
- f.writeLine("Arrived! Dropping loot into chest")
- success, item = turtle.inspect()
- if item.name == "minecraft:chest" then
- f.writeLine("Chest found, beginning transfusion...")
- for c=1, 16 do
- turtle.select(c)
- item=turtle.getItemDetail()
- if item and item.name=="minecraft:coal" then
- success1=turtle.dropUp()
- t=1
- else
- if item then
- success2=turtle.drop()
- t=2
- end
- end
- if item and (success1 and t==1 or success2 and t==2) then
- f.write("Dropped ")
- f.write(item.count)
- f.write(" ")
- f.writeLine(item.name)
- else
- if item and t==1 then
- f.writeLine("Coal inventory full!!")
- else
- if item then
- f.writeLine("Item invetory full!!")
- end
- end
- end
- end
- end
- f.writeLine("Shutting down...")
- f.close()
- os.shutdown()
Add Comment
Please, Sign In to add comment