Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local layerCount = nil
- local currentPosition = {
- x = 0;
- y = 0;
- z = 0;
- }
- local currentDirection = 3
- local persisted = false
- function readData(name)
- if fs.exists('/data/'..name) then
- local handle = fs.open('/data/'..name, 'r')
- local stuff = handle.readAll()
- handle.close()
- return textutils.unserialize(stuff)
- end
- return
- end
- function writeData(name, data)
- if not fs.exists('/data') then
- fs.makeDir('/data')
- end
- local handle = fs.open('/data/'..name, 'w')
- handle.write(textutils.serialize(data))
- handle.close()
- end
- function updatePosition(deltaX, deltaY, deltaZ)
- currentPosition.x = currentPosition.x + deltaX
- currentPosition.y = currentPosition.y + deltaY
- currentPosition.z = currentPosition.z + deltaZ
- writeData('position', currentPosition)
- end
- function updateDirection(deltaD)
- currentDirection = (currentDirection + deltaD - 1) % 4 + 1
- writeData('direction', currentDirection)
- end
- function updateLayerCount(layers)
- layerCount = layers
- writeData('layerCount', layerCount)
- end
- function loadData()
- local savedLayerCount = readData('layerCount')
- local savedPosition = readData('position')
- local savedDirection = readData('direction')
- if (savedLayerCount ~= nil) then
- layerCount = savedLayerCount or layerCount
- currentPosition = savedPosition or currentPosition
- currentDirection = savedDirection or currentDirection
- persisted = true
- end
- end
- function persist()
- loadData()
- if persisted then
- if currentDirection == 1 then
- turnRight()
- elseif currentDirection == 3 then
- turnLeft()
- elseif currentDirection == 4 then
- turnRight()
- turnRight()
- end
- if currentPosition.x > 0 then
- moveForward(currentPosition.x)
- elseif currentPosition.x < 0 then
- moveBack(-currentPosition.x)
- end
- turnLeft()
- if currentPosition.z > 0 then
- moveForward(currentPosition.z)
- elseif currentPosition.z < 0 then
- moveBack(-currentPosition.z)
- end
- if currentPosition.y > 0 then
- moveDown(currentPosition.y)
- elseif currentPosition.y < 0 then
- moveUp(-currentPosition.y)
- end
- storeItems()
- else
- turnRight()
- turtle.suck(64)
- turtle.suck(16)
- turnLeft()
- end
- end
- function moveForward(n)
- n = n or 1
- for i = 1, n do
- while not turtle.forward() do
- term.clear()
- print('turtle is stuck, please remove the block in front.')
- sleep(1)
- end
- term.clear()
- local dX = (currentDirection - 3) * ((currentDirection + 1) % 2)
- local dZ = (currentDirection - 2) * (currentDirection % 2)
- updatePosition(dX, 0, dZ)
- end
- end
- function moveBack(n)
- n = n or 1
- for i = 1, n do
- while not turtle.back() do
- term.clear()
- print('turtle is stuck, please remove the block behind.')
- sleep(1)
- end
- term.clear()
- local dX = -(currentDirection - 3) * ((currentDirection + 1) % 2)
- local dZ = -(currentDirection - 2) * (currentDirection % 2)
- updatePosition(dX, 0, dZ)
- end
- end
- function moveUp(n)
- n = n or 1
- for i = 1, n do
- while not turtle.up() do
- term.clear()
- print('turtle is stuck, please remove the block above.')
- sleep(1)
- end
- term.clear()
- updatePosition(0, 1, 0)
- end
- end
- function moveDown(n)
- n = n or 1
- for i = 1, n do
- while not turtle.down() do
- term.clear()
- print('turtle is stuck, please remove the block underneath.')
- sleep(1)
- end
- term.clear()
- updatePosition(0, -1, 0)
- end
- end
- function turnLeft()
- turtle.turnLeft()
- updateDirection(-1)
- end
- function turnRight()
- turtle.turnRight()
- updateDirection(1)
- end
- function refuel(level, overrideMoveCount)
- local moveCount = nil
- if overrideMoveCount~= nil then
- moveCount = overrideMoveCount
- else
- moveCount = 98 + level*6
- end
- fuelCount = math.ceil((moveCount - turtle.getFuelLevel()) / 80)
- if fuelCount <= 0 then
- return
- end
- turnLeft()
- turtle.select(16)
- while fuelCount > 0 do
- turtle.suck(fuelCount)
- fuelCount = fuelCount - turtle.getItemCount(16)
- turtle.refuel()
- if fuelCount ~= 0 then
- term.clear()
- print('missing '..fuelCount..' coal, please put coal in the chest')
- sleep(1)
- else
- term.clear()
- break
- end
- end
- turtle.select(1)
- turnRight()
- end
- function dropByName(name, dropCallback)
- dropCallback = dropCallback or turtle.drop
- for i = 1, 16 do
- local data = turtle.getItemDetail(i)
- if (data ~= nil) and (data.name == 'minecraft:'..name) then
- turtle.select(i)
- while not dropCallback() do
- term.clear()
- print('chest is full, please empty it.')
- end
- end
- end
- end
- function storeItems()
- dropByName('wheat', turtle.dropDown)
- turnLeft()
- dropByName('wheat_seeds', turtle.drop)
- turtle.select(1)
- turtle.suck(64)
- turtle.suck(16)
- turnLeft()
- end
- function selectSeeds()
- for i = 1, 16 do
- local data = turtle.getItemDetail(i)
- if (data ~= nil) and data.name == 'minecraft:wheat_seeds' then
- turtle.select(i)
- return
- end
- end
- end
- function replantOne()
- local _, data = turtle.inspectDown()
- if (data.state == nil) or (data.state.age == nil) then
- selectSeeds()
- turtle.placeDown()
- elseif data.state.age == 7 then
- turtle.digDown()
- selectSeeds()
- turtle.placeDown()
- end
- end
- function replantLayer(layer)
- refuel(layer)
- local y = (layer-1) * 3
- moveUp(y)
- moveForward()
- for x = 1, 9 do
- for z = 1, 8 do
- replantOne()
- moveForward()
- end
- replantOne()
- if x ~= 9 then
- if x % 2 == 1 then
- turnRight()
- moveForward()
- turnRight()
- else
- turnLeft()
- moveForward()
- turnLeft()
- end
- end
- end
- turnLeft()
- moveForward(8)
- turnLeft()
- moveForward(9)
- moveDown(y)
- storeItems()
- end
- function replantAll()
- for i = 1, layerCount do
- replantLayer(i)
- end
- end
- function waitToGrow()
- repeat
- local _, data = turtle.inspectDown()
- sleep(1)
- until (data.state == nil) or (data.state.age == 7) or (data.state.age == nil)
- end
- function watchCrops()
- refuel(nil, 2)
- moveForward()
- waitToGrow()
- moveBack()
- turtle.select(1)
- end
- function main()
- term.clear()
- persist()
- if not persisted then
- print('Enter layer count: ')
- local input = nil
- while true do
- input = tonumber(read())
- if (input == nil) or (math.floor(input) ~= input) then
- print('input should be an integer.')
- else
- updateLayerCount(input)
- term.clear()
- break
- end
- end
- end
- while true do
- watchCrops()
- replantAll()
- end
- end
- main()
Add Comment
Please, Sign In to add comment