Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Variables --
- local turtleState = {}
- local inputChest = {}
- local outputChest = {}
- local fuelChest = {}
- turtleStateFilename = "turtleState"
- -- Functions --
- findItem = function(itemName)
- for i=1,16,1 do
- itemData = turtle.getItemDetail(i)
- if itemData ~= nil then
- if itemData.name == itemName then
- return i
- end
- end
- end
- return false
- end
- turnRight = function()
- if not turtle.turnRight() then
- error('Unable to move.') -- Error will never be seen as turtle can never fail to turn left.
- else
- isFacing = isFacing + 1
- if isFacing > 3 then
- isFacing = 0
- end
- settings.set("isFacing", isFacing)
- settings.save(turtleStateFilename)
- end
- end
- turnLeft = function()
- if not turtle.turnLeft() then
- error('Unable to move.') -- Error will never be seen as turtle can never fail to turn left.
- else
- isFacing = isFacing - 1
- if isFacing < 0 then
- isFacing = 3
- end
- settings.set("isFacing", isFacing)
- settings.save(turtleStateFilename)
- end
- end
- turnToDir = function(direction)
- if direction == 0 then
- if isFacing == 1 then
- turnLeft()
- elseif isFacing == 2 then
- turnRight()
- turnRight()
- elseif isFacing == 3 then
- turnRight()
- end
- elseif direction == 1 then
- if isFacing == 0 then
- turnRight()
- elseif isFacing == 3 then
- turnLeft()
- turnLeft()
- elseif isFacing == 2 then
- turnLeft()
- end
- elseif direction == 2 then
- if isFacing == 1 then
- turnRight()
- elseif isFacing == 0 then
- turnLeft()
- turnLeft()
- elseif isFacing == 3 then
- turnLeft()
- end
- elseif direction == 3 then
- if isFacing == 0 then
- turnLeft()
- elseif isFacing == 1 then
- turnRight()
- turnRight()
- elseif isFacing == 2 then
- turnRight()
- end
- end
- if isFacing == direction then return true end
- end
- moveForward = function()
- turtle.dig()
- if not turtle.forward() then
- error("Unable to move.")
- else
- if isFacing == 0 then
- xPos = xPos + 1
- settings.set("xPos", xPos)
- elseif isFacing == 1 then
- yPos = yPos + 1
- settings.set("yPos", yPos)
- elseif isFacing == 2 then
- xPos = xPos - 1
- settings.set("xPos", xPos)
- elseif isFacing == 3 then
- yPos = yPos - 1
- settings.set("yPos", yPos)
- end
- settings.save(turtleStateFilename)
- end
- end
- moveBackward = function()
- if not turtle.back() then
- error("Unable to move.")
- else
- if isFacing == 0 then
- xPos = xPos - 1
- settings.set("xPos", xPos)
- elseif isFacing == 1 then
- yPos = yPos - 1
- settings.set("yPos", yPos)
- elseif isFacing == 2 then
- xPos = xPos + 1
- settings.set("xPos", xPos)
- elseif isFacing == 3 then
- yPos = yPos + 1
- settings.set("yPos", yPos)
- end
- settings.save(turtleStateFilename)
- end
- end
- moveUp = function()
- if not turtle.up() then
- error("Unable to move.")
- else
- zPos = zPos + 1
- settings.set("zPos", zPos)
- settings.save(turtleStateFilename)
- end
- end
- moveDown = function()
- if not turtle.down() then
- error("Unable to move.")
- else
- zPos = zPos - 1
- settings.set("zPos", zPos)
- settings.save(turtleStateFilename)
- end
- end
- returnToPos = function(x,y,z,f)
- while yPos ~= y do
- if x >= -1 then
- if xPos == -1 then
- if isFacing == 1 then
- turnLeft()
- elseif isFacing == 2 then
- turnRight()
- turnRight()
- elseif isFacing == 3 then
- turnRight()
- end
- moveForward()
- end
- else
- if isFacing == 1 then
- turnRight()
- elseif isFacing == 0 then
- turnRight()
- turnRight()
- elseif isFacing == 3 then
- turnLeft()
- end
- moveForward()
- end
- if yPos > y then
- if isFacing == 0 then
- turnLeft()
- elseif isFacing == 1 then
- turnRight()
- turnRight()
- elseif isFacing == 2 then
- turnRight()
- end
- moveForward()
- elseif yPos < y then
- if isFacing == 0 then
- turnRight()
- elseif isFacing == 3 then
- turnLeft()
- turnLeft()
- elseif isFacing == 2 then
- turnLeft()
- end
- moveForward()
- end
- end
- while xPos ~= x do
- if xPos > x then
- if isFacing == 1 then
- turnRight()
- elseif isFacing == 0 then
- turnLeft()
- turnLeft()
- elseif isFacing == 3 then
- turnLeft()
- end
- moveForward()
- elseif xPos < x then
- if isFacing == 1 then
- turnLeft()
- elseif isFacing == 2 then
- turnRight()
- turnRight()
- elseif isFacing == 3 then
- turnRight()
- end
- moveForward()
- end
- end
- while zPos ~= z do
- if zPos > z then
- moveDown()
- elseif zPos < z then
- moveUp()
- end
- end
- turnToDir(f)
- end
- selectgFillDownBlock = function()
- if xPos == -1 or xPos == xSize or yPos == -1 or yPos == ySize then
- fillDownTo("minecraft:stone", "minecraft:stone_bricks")
- else
- fillDownTo("minecraft:stone", "minecraft:dirt")
- if (math.fmod(xPos, 5) == 4 or math.fmod(xPos, 5) == -1) and (math.fmod(yPos, 5) == 4 or math.fmod(yPos, 5) == -1) then
- print("water here")
- turtle.digDown()
- if not findItem("minecraft:water_bucket") then
- local x = xPos
- local y = yPos
- local z = zPos
- local f = isFacing
- returnHome()
- while not findItem("minecraft:water_bucket") do
- print("Waiting for minecraft:water_bucket to be put in inventory.")
- sleep(2)
- end
- returnToPos(x,y,z,f)
- end
- turtle.select(findItem("minecraft:water_bucket"))
- turtle.placeDown()
- else
- print("dirt here")
- end
- end
- end
- returnHome = function()
- while zPos ~= 1 do
- if zPos > 1 then
- moveDown()
- elseif zPos < 1 then
- moveUp()
- end
- end
- while yPos ~= 0 do
- if xPos == -1 then
- if isFacing == 1 then
- turnLeft()
- elseif isFacing == 2 then
- turnRight()
- turnRight()
- elseif isFacing == 3 then
- turnRight()
- end
- moveForward()
- end
- if yPos > 0 then
- if isFacing == 0 then
- turnLeft()
- elseif isFacing == 1 then
- turnRight()
- turnRight()
- elseif isFacing == 2 then
- turnRight()
- end
- moveForward()
- elseif yPos < 0 then
- if isFacing == 0 then
- turnRight()
- elseif isFacing == 3 then
- turnLeft()
- turnLeft()
- elseif isFacing == 2 then
- turnLeft()
- end
- moveForward()
- end
- end
- while xPos ~= -1 do
- if xPos > -1 then
- if isFacing == 1 then
- turnRight()
- elseif isFacing == 0 then
- turnLeft()
- turnLeft()
- elseif isFacing == 3 then
- turnLeft()
- end
- moveForward()
- elseif xPos < -1 then
- if isFacing == 1 then
- turnLeft()
- elseif isFacing == 2 then
- turnRight()
- turnRight()
- elseif isFacing == 3 then
- turnRight()
- end
- moveForward()
- end
- end
- if xPos == -1 and yPos == 0 then
- if isFacing == 1 then
- turnLeft()
- elseif isFacing == 2 then
- turnRight()
- turnRight()
- elseif isFacing == 3 then
- turnRight()
- end
- end
- print("I AM HOME!")
- end
- restock = function(itemName, xPosPrev, yPosPrev, zPosPrev, isFacingPrev)
- print("xyz: ", xPosPrev, " ", yPosPrev, " ", zPosPrev, "is facing: ", isFacingPrev)
- returnHome()
- while not findItem(itemName) do
- print("Waiting for ", itemName, " to be put in inventory.")
- sleep(2)
- end
- returnToPos(xPosPrev, yPosPrev, zPosPrev, isFacingPrev)
- local selectItem = findItem(itemName)
- if selectItem ~= false then
- turtle.select(selectItem)
- else
- print("Did I restock?")
- end
- end
- if not settings.load(turtleStateFilename) then
- settings.get("xSize", 9)
- settings.get("ySize", 9)
- settings.set("xPos", -1)
- settings.set("yPos", 0)
- settings.set("zPos", 1)
- settings.set("isFacing", 0)
- settings.set("firstRun", true)
- settings.set("aboveGround", true)
- settings.set("currentStep", {})
- settings.save(turtleStateFilename)
- end
- xSize = settings.get("xSize", 9)
- ySize = settings.get("ySize", 9)
- xPos = settings.get("xPos", -1)
- yPos = settings.get("yPos", 0)
- zPos = settings.get("zPos", 1)
- firstRun = settings.get("firstRun", true)
- aboveGround = settings.get("aboveGround", true)
- isFacing = settings.get("isFacing", 0)
- if xPos == -1 and yPos == 0 then
- print("I am already home")
- else
- returnHome()
- end
- calculateWhatsNeeded = function()
- local wallBlocks = 0
- local dirtBlocks = 0
- local waterBlocks = 0
- for x=-1,xSize do
- for y=-1,ySize do
- if x == -1 or y == -1 or x == xSize or y == ySize then
- wallBlocks = wallBlocks + 1
- else
- if (math.fmod(x, 5) == 4 or math.fmod(x, 5) == -1) and (math.fmod(y, 5) == 4 or math.fmod(y, 5) == -1) then
- waterBlocks = waterBlocks + 1
- else
- dirtBlocks = dirtBlocks + 1
- end
- end
- end
- end
- print("Minimum blocks needed to create farm:")
- print("Wall Blocks: ", wallBlocks)
- print("Dirt Blocks: ", dirtBlocks)
- print("Water Blocks:", waterBlocks)
- print("These are only an estimate, may use a")
- print("significantly larger number of blocks.")
- end
- fillDownTo = function(blockA, blockB)
- while true do
- local blockBelow, blockData = turtle.inspectDown()
- local hasStone = findItem(blockB)
- if not hasStone then
- break
- else
- turtle.select(hasStone)
- end
- if blockBelow then
- if blockData.name == blockA then
- print("I reached ", blockA, ", Wooo!")
- break
- else
- turtle.digDown()
- moveDown()
- end
- end
- if not blockBelow then
- moveDown()
- end
- print("Current zCoord: ", zPos)
- end
- while zPos < 0 do
- if not findItem(blockB) then
- print("I have run out of stone")
- restock(blockB, xPos, yPos, zPos, isFacing)
- else
- turtle.select(findItem(blockB))
- end
- moveUp()
- turtle.placeDown()
- end
- end
- while true do
- local test = turtle.getFuelLevel()
- local solidBlockBelow = false
- print("Current fuel level: ", test)
- calculateWhatsNeeded()
- sleep(20)
- if firstRun then
- for i=1,6 do
- if i == 1 then
- returnToPos(-2, -1, 1, 0)
- print("Clearing a ", xSize, " x ", ySize, " area for the farm.")
- while true do
- if (xPos <= -1 or xPos >= xSize) and yPos >= ySize then
- returnHome()
- break
- end
- if xPos == xSize then
- turnRight()
- turtle.dig()
- moveForward()
- turtle.digUp()
- turtle.digDown()
- turnRight()
- end
- if xPos == -1 and yPos ~= -1 then
- turnLeft()
- turtle.dig()
- moveForward()
- turtle.digUp()
- turtle.digDown()
- turnLeft()
- end
- turtle.dig()
- moveForward()
- turtle.digUp()
- turtle.digDown()
- end
- elseif i == 2 then
- returnToPos(-2, -1, 0, 0)
- while true do
- if (xPos <= -1 or xPos >= xSize) and yPos >= ySize then
- returnHome()
- break
- end
- if xPos == xSize then
- turnRight()
- moveForward()
- selectgFillDownBlock()
- turnRight()
- end
- if xPos == -1 and yPos ~= -1 then
- turnLeft()
- moveForward()
- selectgFillDownBlock()
- turnLeft()
- end
- moveForward()
- selectgFillDownBlock()
- end
- elseif i == 3 then
- print("I am, will build walls and roof and place torches. ", i)
- sleep(10)
- elseif i == 4 then
- print("Reserved. ", i)
- sleep(1)
- elseif i == 5 then
- print("Reserved. ", i)
- sleep(1)
- elseif i == 6 then
- print("Reserved. ", i)
- sleep(1)
- end
- end
- else
- exit()
- if test <= 80 then
- returnHome()
- break
- end
- if xPos >= (xSize - 1) and yPos >= (ySize - 1) then
- returnHome()
- end
- if xPos == (xSize - 1) then
- turnRight()
- moveForward()
- turnRight()
- end
- if xPos == 0 and yPos ~= 0 then
- turnLeft()
- moveForward()
- turnLeft()
- end
- moveForward()
- if (math.fmod(xPos, 5) == 4 or math.fmod(xPos, 5) == -1) and (math.fmod(yPos, 5) == 4 or math.fmod(yPos, 5) == -1) then
- print("water here")
- turtle.digDown()
- else
- print("dirt here")
- end
- end
- print("Current xCoord: ", xPos)
- print("Current yCoord: ", yPos)
- sleep(1)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement