Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local t = turtle
- local fuel = 0
- local fuelBuffer = 30
- local returning = false
- local NORTH = 1
- local EAST = 2
- local SOUTH = 3
- local WEST = 4
- Point = {}
- Point.__index = Point
- function Point:new(x,y,z,dir)
- local obj = {x=x,y=y,z=z,dir=dir}
- setmetatable(obj,Point)
- return obj
- end
- function Point:forward()
- if self.dir == NORTH then
- self.y=self.y+1
- elseif self.dir == South then
- self.y=self.y-1
- elseif self.dir == EAST then
- self.x=self.x+1
- elseif self.dir == WEST then
- self.x=self.x-1
- end
- end
- function Point:back()
- if self.dir == NORTH then
- self.y=self.y-1
- elseif self.dir == South then
- self.y=self.y+1
- elseif self.dir == EAST then
- self.x=self.x-1
- elseif self.dir == WEST then
- self.x=self.x+1
- end
- end
- function Point:up()
- self.y=self.y+1
- end
- function Point:down()
- self.y=self.y-1
- end
- function Point:right()
- self.dir=self.dir+1
- if self.dir>WEST then
- self.dir = NORTH
- end
- end
- function Point:left()
- self.dir=self.dir-1
- if self.dir<NORTH then
- self.dir = WEST
- end
- end
- function Point:print()
- local dirText=""
- if self.dir == NORTH then
- dirText="NORTH"
- elseif self.dir == South then
- dirText="SOUTH"
- elseif self.dir == EAST then
- dirText="EAST"
- elseif self.dir == WEST then
- dirText="WEST"
- end
- print("Point: ("..self.x..","..self.y..","..self.z..","..dirText..")")
- end
- local relativePos=Point:new(0,0,0,NORTH)
- function onTurnon()
- fuel = getFuelLevel()
- end
- function dumpInventory()
- for i = 0, 15 do
- t.select(i)
- t.dropDown()
- end
- end
- function restockFuel()
- fFace(SOUTH)
- t.select(0)
- t.suck()
- t.refuel()
- end
- function refuelReturn()
- --Fix Z
- while relativePos.z>0 do
- fDown()
- end
- while relativePos.z<0 do
- fUp()
- end
- --Fix x
- if relativePos.x<0 then
- fFace(EAST)
- elseif relativePos.x>0 then
- fFace(WEST)
- end
- while relativePos.x~=0 do
- fForward()
- end
- --Fix y
- if relativePos.y<0 then
- fFace(NORTH)
- elseif relativePos.y>0 then
- fFace(SOUTH)
- end
- while relativePos.y~=0 do
- fForward()
- end
- end
- function goBackTO(pointA)
- --Fix Z
- while pointA.z>0 do
- fDown()
- end
- while pointA.z<0 do
- fUp()
- end
- --Fix y
- if pointA.y<0 then
- fFace(NORTH)
- elseif pointA.y>0 then
- fFace(SOUTH)
- end
- while pointA.y~=0 do
- fForward()
- end
- --Fix x
- if pointA.x<0 then
- fFace(EAST)
- elseif pointA.x>0 then
- fFace(WEST)
- end
- while pointA.x~=0 do
- fForward()
- end
- --Fix dir
- fFace(pointA.dir)
- end
- --Get the current fuel level and return home if too close to empty
- --If we refuel we come back to the same posisiton/orientation
- function updateFuel()
- if returning then
- return
- end
- fuel = t.getFuelLevel()
- --Fuel to get back home is calculated
- local blocksBack = relativePos.x+relativePos.y+relativePos.z
- if fuel <= blocksBack + fuelBuffer then
- returning=true
- local returnPoint = Point:new(relativePos.x,relativePos.y,relativePos.z,relativePos.dir)
- refuelReturn()
- restockFuel()
- goBackTo(returnPoint)
- returning=false
- end
- end
- function fDig()
- while t.detect() do
- updateFuel()
- t.dig()
- end
- end
- function fDigDown()
- while t.detectDown() do
- updateFuel()
- t.digDown()
- end
- end
- function fDigUp()
- while t.detectUp() do
- updateFuel()
- t.digUp()
- end
- end
- function fForward(x)
- x = x or 1
- for i=1,x do
- while not t.forward() do
- updateFuel()
- fDig()
- end
- relativePos:forward()
- updateFuel()
- end
- end
- function fDown()
- while not t.down() do
- updateFuel()
- fDigDown()
- end
- relativePos:down()
- updateFuel()
- end
- function fUp()
- while not t.up() do
- updateFuel()
- fDigUp()
- end
- relativePos:up()
- updateFuel()
- end
- function fRight()
- t.turnRight()
- relativePos:right()
- updateFuel()
- end
- function fLeft()
- t.turnLeft()
- relativePos:left()
- updateFuel()
- end
- function fTurnAround()
- fRight()
- fRight()
- end
- function fBack()
- if not t.back() then
- fTurnAround()
- fDig()
- fTurnAround()
- end
- relativePos:back()
- updateFuel()
- end
- function fFace(d)
- if d == relativePos.dir then
- return
- elseif d-relativePos.dir==1 then
- fLeft()
- elseif relativePos.dir-d==1 then
- fRight()
- else
- fTurnAround()
- end
- end
- --Assuming middle start
- function mine3By3(x)
- for i=0,x do
- --dig middle part
- fDig()
- fForward()
- fDigUp()
- fDigDown()
- --dig left part
- fLeft()
- fDig()
- fForward()
- fDigUp()
- fDigDown()
- --dig right part
- fTurnAround()
- fForward()
- fDig()
- fForward()
- fDigUp()
- fDigDown()
- --return
- fBack()
- fLeft()
- end
- end
- function mine()
- fUp()
- fFace(NORTH)
- while true do
- mine3By3(5)
- fLeft()
- fForward()
- mine3By3(5)
- fTurnAround()
- fForward(5+3)
- mine3By3(5)
- fTurnAround()
- fForward(5+2)
- fRight()
- end
- end
- mine()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement