Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- pastebin get aK9V8ZyP turtlescript
- -- Assumptions
- -- +Z is towards the sky.
- -- +Y is the starting direction.
- -- The area is totally clear of blocks, mobs, and players.
- -- The Turtle starts above a chest or ender chest outside of the structure at (x,y,z) (0,0,+2)
- -- The Turtle always places downwards.
- -- Block orientation is as placed downwards.
- -- For consistency always face +y when placing a block.
- -- The Turtle inventory is preloaded with slots 1 through 16 and never runs out from the chest.
- -- Slot 1 is fuel.
- --XOO
- --XOO
- --TXX
- xHome = 0
- yHome = 0
- zHome = 2
- local xPos = xHome
- local yPos = yHome
- local zPos = zHome
- local xDir = 0
- local yDir = 1
- local blockToPlace = 0
- local debugFlag = 1
- --------- Stuff you need to edit ----------
- Array2d = {}
- Array2d[1] = {
- {"2","3","4","4"},
- {"5","0","7","7"},
- {"8","9","0","0"}
- }
- Array2d[2] = {
- {"3","4","5","5"},
- {"6","7","8","8"},
- {"9","0","0","0"}
- }
- Array3d = {1,2}
- local XSIZE = 4
- local YSIZE = 3
- local ZSIZE = table.getn(Array3d)
- --------- End stuff you need to edit ----------------
- --123456789abcdefg
- --0 denotes empty.
- function convertString(str)
- if (str == "0") then
- ret = 0
- elseif (str == "1") then
- ret = 1
- elseif (str == "2") then
- ret = 2
- elseif (str == "3") then
- ret = 3
- elseif (str == "4") then
- ret = 4
- elseif (str == "5") then
- ret = 5
- elseif (str == "6") then
- ret = 6
- elseif (str == "7") then
- ret = 7
- elseif (str == "8") then
- ret = 8
- elseif (str == "9") then
- ret = 9
- elseif (str == "a") then
- ret = 10
- elseif (str == "b") then
- ret = 11
- elseif (str == "c") then
- ret = 12
- elseif (str == "d") then
- ret = 13
- elseif (str == "e") then
- ret = 14
- elseif (str == "f") then
- ret = 15
- elseif (str == "g") then
- ret = 16
- else
- ret = 0
- end
- return ret
- end
- function debugPrint(str)
- if (debugFlag==1) then
- print(str)
- end
- end
- -- Dervied from: http://www.computercraft.info/wiki/Excavate
- local function turnLeft()
- turtle.turnLeft()
- xDir, yDir = -yDir, xDir
- end
- local function turnRight()
- turtle.turnRight()
- xDir, yDir = yDir, -xDir
- end
- function goTo(x,y,z)
- goToFacing(x,y,z,xDir,yDir)
- end
- function goToFacing( x, y, z, xd, yd )
- debugPrint("Starting at " .. xPos .. " " .. yPos .. " " .. zPos)
- debugPrint("Going to " .. x .. " " .. y .. " " .. z)
- -- This function moves on the z-axis first.
- while zPos > z do
- if turtle.down() then
- debugPrint("Went Down")
- zPos = zPos - 1
- else
- debugPrint("Can't go Down")
- sleep( 0.5 )
- end
- end
- while zPos < z do
- if turtle.up() then
- debugPrint("Went Up")
- zPos = zPos + 1
- else
- debugPrint("Can't go Up")
- sleep( 0.5 )
- end
- end
- if xPos > x then
- while xDir ~= -1 do
- if (xDir == 0 and yDir == -1) then
- debugPrint("Turning Right xPos > x")
- turnRight()
- else
- debugPrint("Turning Left xPos > x")
- turnLeft()
- end
- end
- while xPos > x do
- if turtle.forward() then
- debugPrint("Went Forward xPos > x")
- xPos = xPos - 1
- else
- debugPrint("Can't go Forward xPos > x")
- sleep( 0.5 )
- end
- end
- elseif xPos < x then
- while xDir ~= 1 do
- if (xDir == 0 and yDir == 1) then
- debugPrint("Turning Right xPos < x")
- turnRight()
- else
- debugPrint("Turning Left xPos < x")
- turnLeft()
- end
- end
- while xPos < x do
- if turtle.forward() then
- debugPrint("Went Forward xPos < x")
- xPos = xPos + 1
- else
- debugPrint("Can't go Forward xPos < x")
- sleep( 0.5 )
- end
- end
- end
- if yPos > y then
- while yDir ~= -1 do
- if ( xDir == 1 and yDir == 0) then
- debugPrint("Turning Right yPos > y")
- turnRight()
- else
- debugPrint("Turning Left yPos > y")
- turnLeft()
- end
- end
- while yPos > y do
- if turtle.forward() then
- debugPrint("Went Forward yPos > y")
- yPos = yPos - 1
- else
- debugPrint("Can't go Forward yPos > y")
- sleep( 0.5 )
- end
- end
- elseif yPos < y then
- while yDir ~= 1 do
- if ( xDir == -1 and yDir == 0) then
- debugPrint("Turning Right yPos < y")
- turnRight()
- else
- debugPrint("Turning Left yPos < y")
- turnLeft()
- end
- end
- while yPos < y do
- if turtle.forward() then
- debugPrint("Went Forward yPos < y")
- yPos = yPos + 1
- else
- debugPrint("Can't go Forward yPos < y")
- sleep( 0.5 )
- end
- end
- end
- while yDir ~= yd or xDir ~= xd do
- if (xd == yDir and yd == -xDir) then
- debugPrint("Returning Facing Right")
- turnRight()
- else
- debugPrint("Returning Facing Left")
- turnLeft()
- end
- end
- end
- -- Refill the inventory from the chest
- function refill()
- for i=1,16 do
- turtle.select(i)
- turtle.suckDown()
- end
- refuel()
- end
- function refuel()
- turtle.select(1)
- if(turtle.refuel(63)) then
- debugPrint("Refueled")
- else
- debugPrint("Could not Refuel")
- end
- fuel=turtle.getFuelLevel()
- debugPrint("Fuel is " .. fuel)
- end
- function returnHome()
- -- Goes to 0,0 first.
- goTo(xHome,yHome,zPos)
- goTo(xHome,yHome,zHome)
- end
- function returnHomeFacing01()
- -- Goes to 0,0 first.
- goTo(xHome,yHome,zPos)
- goToFacing(xHome,yHome,zHome,0,1)
- end
- debugPrint("XSIZE, YSIZE, ZSIZE" .. table.getn(Array2d[1][1]) .. " " .. table.getn(Array2d[1]) .. " " .. ZSIZE)
- refill()
- for zIndex =1,ZSIZE do
- for yIndex=1,YSIZE do
- for xIndex=1,XSIZE do
- blockToPlace = convertString(Array2d[Array3d[zIndex]][1+YSIZE - yIndex][xIndex]);
- fuel=turtle.getFuelLevel()
- debugPrint("Prepare to Place " .. blockToPlace .. " at " .. xIndex .. " " .. yIndex .. " " .. " " .. zIndex)
- -- If we don't have the material or are low on fuel, return home, refill, come back to the z-axis.
- if (blockToPlace ~= 0) then
- if (turtle.getItemCount(blockToPlace) < 2 or fuel < 200) then
- debugPrint("Returning Home")
- returnHome()
- debugPrint("Refilling")
- refill()
- end
- -- Go to and face +y
- debugPrint("goToFacing " .. xIndex .. " " .. yIndex .. " " .. zIndex + 1 .. " - - 0 1")
- goToFacing( xIndex, yIndex, zIndex+1, 0, 1 )
- debugPrint("Select Block")
- turtle.select(blockToPlace)
- turtle.placeDown()
- end
- end
- end
- end
- returnHomeFacing01()
- debugPrint("Done")
Add Comment
Please, Sign In to add comment