Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local x=0
- local y=0
- local dir=0
- local field=0
- local homeX=0
- local homeY=0
- local stop=false
- --Fields (H for home):
- -- 1 3
- -- H
- -- 2 4
- -- Only 1 and 2 are done for now
- local function harvest()
- if turtle.detectDown() then
- turtle.digDown()
- end
- end
- local function plant()
- if not turtle.detectDown() and turtle.getItemCount(1)>1 then
- turtle.select(1)
- turtle.placeDown()
- end
- end
- local function turn(direction)
- if direction == "left" then
- turtle.turnLeft()
- dir = (dir-1)%4
- elseif direction == "right" then
- turtle.turnRight()
- dir = (dir+1)%4
- end
- end
- local function forward()
- if dir == 0 then
- if turtle.forward() then
- x = x+1
- homeX = homeX+1
- end
- elseif dir == 2 then
- if turtle.forward() then
- x = x-1
- homeX = homeX-1
- end
- elseif dir == 1 then
- if turtle.forward() then
- y = y+1
- homeY = homeY+1
- end
- elseif dir == 3 then
- if turtle.forward() then
- y = y-1
- homeY = homeY-1
- end
- end
- end
- local function setDir(newDir)
- while not (newDir == dir) do
- turn("right")
- end
- end
- local function move()
- if x==0 and y~=0 then
- turn("left")
- elseif x==8 and y~=8 then
- turn("right")
- elseif x==8 and y==8 then
- return false
- end
- forward()
- return true
- end
- local function returnHome()
- if homeX > 0 then
- setDir(2)
- while homeX ~=0 do
- forward()
- end
- elseif homeX < 0 then
- setDir(0)
- while homeX ~=0 do
- forward()
- end
- end
- if homeY > 0 then
- setDir(3)
- while homeY ~=0 do
- forward()
- end
- elseif homeY < 0 then
- setDir(1)
- while homeY ~=0 do
- forward()
- end
- end
- setDir(0)
- print("Returned home")
- end
- local function gotoField()
- if homeX ~=0 or homeY ~=0 or dir~=0 then
- returnHome()
- end
- if field == 1 then
- turn("right")
- forward()
- turn("left")
- forward()
- elseif field == 2 then
- turn("left")
- for i=1, 9 do
- forward()
- end
- turn("right")
- forward()
- end
- -- Reset x and y
- x=0
- y=0
- write("Starting harvesting field ")
- write(field)
- print("...")
- end
- local function manageInventory()
- local fuelCount
- for i=3,15 do
- turtle.select(i)
- turtle.dropDown()
- end
- turtle.select(16)
- if turtle.getFuelLevel() < 100 and turtle.suckUp() then
- fuelCount = turtle.getItemCount()
- turtle.refuel(fuelCunt-1)
- end
- end
- local function harvestLoop(fieldNum)
- field = fieldNum
- stop=false
- turtle.select(1)
- gotoField()
- while not stop do
- harvest()
- plant()
- if not move() then
- stop = true
- end
- end
- returnHome()
- manageInventory()
- end
- local function mainLoop()
- harvestLoop(1)
- harvestLoop(2)
- end
- mainLoop()
Advertisement
Add Comment
Please, Sign In to add comment