Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local t = turtle
- local FUELBUFFER = 30
- comingHome = false
- local NORTH = 1
- local EAST = 2
- local SOUTH = 3
- local WEST = 4
- local COAL_SLOT = 1
- local TORCH_SLOT = 2
- local GOOD_ITEMS = {"minecraft:gold_ore","minecraft:iron_ore","minecraft:torch","minecraft:coal","minecraft:diamond","minecraft:redstone","minecraft:emerald"}
- local PLACEABLE_ITEMS = {"minecraft:cobblestone","minecraft:stone",}
- local TORCH_NAME = "minecraft:torch"
- local COAL_NAME = "minecraft:coal"
- local COAL_DIRECTION = SOUTH
- local TORCH_DIRECTION = EAST
- --POINT CLASS-----------------------------------------------
- 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.z=self.z+1
- end
- function Point:down()
- self.z=self.z-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
- --POINT CLASS END -----------------------------------------------------
- relativePos=Point:new(0,0,0,NORTH)
- function onTurnon()
- fuel = getFuelLevel()
- end
- function isGoodItem(name)
- for good in GOOD_ITEMS do
- if good == name then
- return true
- end
- end
- return false
- end
- --Returns if the inventory is full. Will try to dump "bad" items before saying its full
- function isFull()
- local BUFFER_SPACES = 2 --Spaces needing to be cleared after being full to be considered good to continue
- --Check if every spot has an item in it
- for i = 1, 16 do
- if t.getItemCount(i)==0 then
- return false
- end
- end
- --If the inventory was full try to drop undesirable items
- local slotsDropped=0
- for i=0,15 do
- if not isGoodItem(getItemName(i)) then
- t.select(i)
- t.dropDown()
- slotsDropped=slotsDropped+1
- end
- end
- if slotsDropped<=BUFFER_SPACES or slotsDropped==0 then
- return true
- else
- return false
- end
- end
- function getItemName(i)
- if t.getItemCount(i)==0 then
- return "nil"
- else
- return t.getItemDetail(i,false).name
- end
- end
- function dumpInventory()
- for i = 1, 16 do
- t.select(i)
- if getItemName(i)==COAL_NAME then
- fFace(COAL_DIRECTION)
- t.drop()
- elseif i==TORCH_SLOT and getItemName(i)~=TORCH_NAME then
- t.dropDown()
- end
- end
- end
- function restockFuel()
- fFace(COAL_DIRECTION)
- t.select(COAL_SLOT)
- t.suck()
- t.refuel()
- end
- function restockTorches()
- fFace(TORCH_DIRECTION)
- t.select(TORCH_SLOT)
- t.suck()
- if outOfTorches() then
- print("Not enough torches to continue")
- shell.exit()
- end
- end
- function outOfTorches()
- if getItemName(TORCH_SLOT)~=TORCH_NAME or t.getItemCount(TORCH_SLOT)==0 then
- return true
- else
- return false
- end
- end
- --Return home, restock, and come back
- function restockReturn(previousPoint)
- comingHome=true
- print("Coming Home")
- --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
- print("At home")
- --At home base do everythign we need to
- dumpInventory()
- restockFuel()
- restockTorches()
- print("Going back to war")
- local currentFuel = t.getFuelLevel()
- local distanceOut = previousPoint.x + previousPoint.y + previousPoint.y
- if currentFuel<(distanceOut*3) then
- print("Not enough fuel to continue the mission")
- shell.exit()
- end
- goBackTo(previousPoint)
- comingHome=false
- end
- function goBackTo(pointA)
- --Fix y
- if pointA.y>relativePos.y then
- fFace(NORTH)
- elseif pointA.y<relativePos.y then
- fFace(SOUTH)
- end
- while pointA.y~=relativePos.y do
- fForward()
- end
- --Fix x
- if pointA.x>relativePos.x then
- fFace(EAST)
- elseif pointA.x<relativePos.x then
- fFace(WEST)
- end
- while pointA.x~=relativePos.x do
- fForward()
- end
- --Fix Z
- while pointA.z<relativePos.z do
- fDown()
- end
- while pointA.z>relativePos.z do
- fUp()
- 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 comingHome 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
- local returnPoint = Point:new(relativePos.x,relativePos.y,relativePos.z,relativePos.dir)
- restockReturn(returnPoint)
- 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)
- local temp = 0
- print("turning: "..d)
- if d == relativePos.dir then
- return
- elseif d>relativePos.dir then
- temp = d-relativePos.dir
- if temp==1 then
- fRight()
- elseif temp==2 then
- fTurnAround()
- elseif temp==3 then
- fLeft()
- end
- elseif d<relativePos.dir then
- temp = relativePos.dir-d
- if temp==1 then
- fLeft()
- elseif temp==2 then
- fTurnAround()
- elseif temp==3 then
- fRight()
- end
- end
- end
- function goToZ(a)
- while relativePos.z<a do
- fUp()
- end
- while relativePos.z>a do
- fDown()
- end
- end
- function placeTorch()
- if outOfTorches then
- local returnPoint = Point:new(relativePos.x,relativePos.y,relativePos.z,relativePos.dir)
- restockReturn(returnPoint)
- end
- local previousZ = relativePos.z
- goToZ(1)
- t.select(TORCH_SLOT)
- t.placeDown()
- goToZ(previousZ)
- 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()
- if i==5 then
- placeTorch()
- end
- --Checking inventory is slow only do it once per 3by3
- if isFull() then
- restockFuel()
- end
- 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