Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --
- -- KINDA WORKING DON'T TRY IT
- --
- iArg = { ... }
- side=tonumber(iArg[1]) or 10
- computerId = os.getComputerID()
- print("computer ID =", computerId)
- x = 0
- y = 0
- z = 0
- function refuel( amount )
- print("Refueling...")
- local fuelLevel = turtle.getFuelLevel()
- if fuelLevel == "unlimited" then
- return true
- end
- local needed = amount or (xPos + zPos + depth + 2)
- if turtle.getFuelLevel() < needed then
- local fueled = false
- for n=1,16 do
- if turtle.getItemCount(n) > 0 then
- turtle.select(n)
- if turtle.refuel(1) then
- while turtle.getItemCount(n) > 0 and turtle.getFuelLevel() < needed do
- turtle.refuel(1)
- end
- if turtle.getFuelLevel() >= needed then
- turtle.select(1)
- return true
- end
- end
- end
- end
- turtle.select(1)
- return false
- end
- return true
- end
- function fuel()
- if turtle.getFuelLevel() < 100 then
- refuel(120)
- end
- print("Fuel",turtle.getFuelLevel(),"/",turtle.getFuelLimit())
- end
- function placetorch()
- data = turtle.getItemDetail(16,true)
- if data == nil then return end
- if data.name.find(data.name,"torch") then
- turtle.back()
- turtle.turnRight()
- turtle.turnRight()
- previouslySelectedSlot = turtle.getSelectedSlot()
- turtle.select(16)
- turtle.select(previouslySelectedSlot)
- turtle.place()
- turtle.back()
- turtle.turnLeft()
- turtle.turnLeft()
- end
- end
- function adv(x,t)
- for y=1,x do
- tries = 0
- turtle.digUp()
- while turtle.forward() ~= true do
- turtle.digUp()
- turtle.dig()
- turtle.attack()
- sleep(0.2)
- tries = tries + 1
- if tries>500 then
- print("Error: can't move forward.")
- end
- end
- if x >= 10 and y%12 == 2 and t ~= 0 then placetorch() end
- fuel()
- end
- end
- local function openRednet()
- local listOfSides = rs.getSides()
- for i = 1,6 do
- if peripheral.isPresent(listOfSides[i]) and peripheral.getType(listOfSides[i]) == "modem" then
- rednet.open(listOfSides[i])
- return listOfSides[i]
- end
- end
- end
- function orientation() --get my bearings by comparing location to location+1
- openRednet()
- x1,y1,z1=gps.locate(computerId)
- if (x1 == nil) then
- print("Cannot find Rednet. Move closer or build another GPS system.")
- os.exit()
- end
- sleep(0.5)
- -----------
- while not turtle.forward() do turtle.dig() turtle.attack() end
- sleep(0.5)
- x2,y2,z2=gps.locate(computerId)
- print("Location:",x1,y1,z1)
- turtle.back() -- now move back to original location
- if x2>x1 then ori = "east" pos=1 end
- if x2<x1 then ori = "west" pos=2 end
- if z2<z1 then ori = "north" pos=3 end
- if z2>z1 then ori = "south" pos=4 end
- return ori, pos
- end
- function orient(pos,dir) -- turn until aligned to new direction
- while dir ~= pos do
- turtle.turnLeft()
- if pos==2 then pos=4
- elseif pos==4 then pos=1
- elseif pos==1 then pos=3
- elseif pos==3 then pos=2
- end
- end
- end
- function clear(sides) -- recursively move tighter and tighter
- if tonumber(sides) < 1 then -- return home
- moveto(ox,oy,oz)
- facing,from=orientation()
- orient(from,or1)
- print("orient(" .. from .. "," .. or1 .. ")")
- return
- end
- for i=1,3 do
- adv(sides,1)
- turtle.turnLeft()
- end
- clear(sides-1)
- end
- function avoid(orientation)
- local stuck = 0
- while not turtle.up() do
- if turtle.back() then
- if orientation==1 then x=x-1 print("x-1") end
- if orientation==2 then x=x+1 print("x+1") end
- if orientation==3 then z=z+1 print("z+1") end
- if orientation==4 then z=z-1 print("z-1") end
- else
- stuck = stuck + 1
- end
- if stuck == 20 then turtle.digUp() stuck = 0 end --attempt to free the turtle.
- end
- y=y+1 --moved up once
- end
- function reset()
- down=0
- while not turtle.detectDown() do
- turtle.down()
- down=down+1
- end
- return down
- end
- function move(dir)
- print("Orienting to dir =",dir)
- orient(dir)
- while not turtle.forward() do
- avoid(dir)
- end
- if dir=="up" then
- turtle.digUp() --?
- turtle.up()
- end
- if dir=="down" then
- turtle.digDown() --?
- turtle.down()
- end
- openRednet()
- rednet.broadcast(loc)
- rednet.close()
- end
- function moveto(x,y,z)
- pos=""
- fuel()
- dx=tonumber(x)
- dy=tonumber(y)
- dz=tonumber(z)
- x,y,z=gps.locate(computerId)
- ori,pos = orientation()
- print("position is " .. pos .. ". Orientation is " .. ori)
- print("I am at x=",x," y=",y," z=",z)
- print("I am going to x=",dx," y=",dy," z=",dz)
- lx = math.abs(dx-x)
- ly = math.abs(dy-y)
- lz = math.abs(dz-z)
- if (lx > 300) or (ly > 300) or (lz > 300) then
- print("Warning, distance beyond rednet range. Turtle will be unaccessible.")
- -- if not rednet.isOpen("right") then rednet.open("right") end
- openRednet()
- rednet.broadcast("Warning, distance beyond rednet range. Turtle will be unaccessible.")
- rednet.close("right")
- end
- while dx>x do print("x",dx,">",x," Need to move to 1 (east)") ori="east" x=x+1 move(pos,1) end --east
- while dx<x do print("x",dx,"<",x," Need to move to 2 (west)") ori="west" x=x-1 move(pos,2) end --west
- while dz>z do print("z",dz,">",z," Need to move to 4 (south)") ori="south" z=z+1 move(pos,4) end --south
- while dz<z do print("z",dz,"<",z," Need to move to 3 (north)") ori="north" z=z-1 move(pos,3) end --north
- reset()
- end
- openRednet()
- facing,or1=orientation()
- print("Facing " .. facing .. ". which is number " .. or1 ..".")
- ox,oy,oz=gps.locate(computerId) -- grab current location so we may return here
- print("Return location = " .. ox .. " " .. oy .. " " .. oz)
- fuel()
- print("Side = " .. side .. ".")
- for x=side,1,-1 do
- clear(x)
- turtle.digDown()
- turtle.down()
- end
- for d=8,1,-1 do
- turtle.digDown()
- turtle.down()
- for r=d,1,-1 do
- for t=r*4,1,-1 do
- adv(1,1)
- if t/r == math.floor(t/r) then
- turtle.turnLeft()
- end
- end
- end
- end
Add Comment
Please, Sign In to add comment