Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --by morganamilo
- local x = 0 -- increases as turtle goes forward relative to direction it faced when first placed
- local y = 0 -- increases as turtle goes left relative to direction it faced when first placed, decreases when turtle goes right
- local z = 0 -- increases as turtle goes up, decreases as turtle does down
- local xDiff = {1, 0, -1, 0} -- what to add to 'x' depending on the orientation
- local yDiff = {0, 1, 0, -1}
- local orient = 1
- function get(var) -- returns respective varible
- if var == 'x' then return x
- elseif var == 'y' then return y
- elseif var == 'z' then return z
- elseif var == 'orient' then return orient end
- end
- function refuelIfEmpty() -- refuels if fuel level is 0
- if turtle.getFuelLevel() == 0 then
- print('Out of fuel\nWaiting for fuel in slot ' .. turtle.getSelectedSlot() .. '...')
- while not turtle.refuel do os.pullEvent('turtle_inventory') end
- print('Fuel level is: ' .. turtle.getFuelLevel())
- end
- end
- function refuelAtIfNeeded(fuelX, fuelY, fuelZ) -- refuels using chest at specified location
- local currentX, currentY, currentZ, currentO = x, y, z, orient
- if turtle.getFuelLevel() == getDistanceFrom(fuelX, fuelY, fuelZ) then
- print('looking for fuel in current slot...')
- if turtle.refuel() then
- print('fuel found, continuing')
- else
- print('heading back to refuel...')
- turtle.drop()
- if x < fuelX then goTo(fuelX + 1, fuelY, fuelZ, 'z' 'y') end
- elseif x > fuelX then goTo(fuelX - 1, fuelY, fuelZ, 'z' 'y') end
- turtle.suck()
- if not turtle.refuel() then
- print('fuel not found, waiting for fuel in current slot...')
- while not turtle.refuel() do os.pullEvent('turtle_inventory') end
- end
- print('fuel found, continuing')
- end
- goTo(currentX, currentY, currentZ, 'z' 'y')
- faceDir(currentO)
- end
- end
- function forward(limit) -- moves forward, digs up/down if respective blocks need to be dug
- x = x + xDiff[orient]
- y = y + yDiff[orient]
- while not turtle.forward() do
- refuelIfEmpty()
- if turtle.detect() then turtle.dig() end
- end
- if z > -limit + 1 and turtle.detectDown() then turtle.digDown() end
- if turtle.detectUp() and z < limit -1 then turtle.digUp() end
- end
- function left() -- turns left, takes 1 from orient
- orient = (orient + 2) % 4 + 1
- turtle.turnLeft()
- end
- function right() -- turns right, adds 1 to orient
- orient = orient % 4 + 1
- turtle.turnRight()
- end
- function faceDir(direction) -- turns to face specified direction, using any number other than '1, 2, 3, 4' will cause a u-turn
- if direction ~= orient then
- if direction == (orient + 2) % 4 + 1 then left()
- elseif direction == orient % 4 + 1 then right()
- else left() left() end
- end
- end
- function up(limit) -- moves up, adds 1 to z
- repeat
- if turtle.detectUp() then turtle.digUp() end
- until turtle.up()
- z = z + 1
- if turtle.detectUp() and z < limit - 1 then turtle.digUp() end
- end
- function down(limit) -- moves down, takes 1 from z
- repeat
- if turtle.detectDown() then turtle.digDown() end
- until turtle.down()
- z = z - 1
- if turtle.detectDown() and z > -limit + 1 then turtle.digDown() end
- end
- function goTo(newX, newY, newZ, prior1, prior2) -- goes to given coordinate
- local functs = {x = {goToX, newX}, y = {goToY, newY}, z = {goToZ, newZ}}
- local prior3
- for k, v in pairs({'x', 'y', 'z'}) do
- if not (prior1 == v or prior2 == v) then prior3 = v end
- end
- for k, v in pairs({prior1, prior2, prior3}) do
- functs[v][1](functs[v][2])
- end
- end
- function goToX(newX)
- if newX > x then faceDir(1) end
- if newX < x then faceDir(3) end
- while newX ~= x do forward(1) end
- end
- function goToY(newY)
- if newY > y then faceDir(2) end
- if newY < y then faceDir(4) end
- while newY ~= y do forward(1) end
- end
- function goToZ(newZ)
- while newZ > z do up(1) end
- while newZ < z do down(1) end
- end
- function round(n) -- rounds numbers
- return math.floor(n + 0.5)
- end
- function inRad(x, y, rad) -- checks if given x and y are in radius
- return round(math.sqrt(x*x+y*y)) < rad
- end
- function nextInRad(rad)
- nextX = x + xDiff[orient]
- nextY = y + yDiff[orient]
- return inRad(nextX, nextY, rad)
- end
- function isEven(n) -- true if n is even
- if n % 2 == 0 then return true
- else return false end
- end
- function turn(n) -- turns left if n is even otherwise right, used for alternating turning in for loops
- if isEven(n) then left()
- else right() end
- end
Advertisement
Add Comment
Please, Sign In to add comment