Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- t = {}
- t.x = 0
- t.z = 0
- t.y = 0
- t.dir = 0
- t.cal = false
- --In Minecraft axis work like this
- --0 = SOUTH, z axis goes up
- --1 = WEST, x axis goes down
- --2 = NORTH, z axis goes down
- --3 = EAST, x axis goes up
- --math functions to determine length and distance
- function math.len(x,y)
- return math.sqrt(x*x + y*y)
- end
- function math.dist(x1,y1, x2,y2)
- return math.len(x1-x2, y1-y2)
- end
- function t.forward()
- if t.cal then
- if turtle.forward() then
- if t.dir == 0 then
- t.z = t.z + 1
- elseif t.dir == 1 then
- t.x = t.x - 1
- elseif t.dir == 2 then
- t.z = t.z - 1
- elseif t.dir == 3 then
- t.x = t.x + 1
- end
- end
- else
- print("not calibrated")
- end
- end
- function t.back()
- if t.cal then
- if turtle.back() then
- if t.dir == 0 then
- t.z = t.z - 1
- elseif t.dir == 1 then
- t.x = t.x + 1
- elseif t.dir == 2 then
- t.z = t.z + 1
- elseif t.dir == 3 then
- t.x = t.x - 1
- end
- end
- else
- print("not calibrated")
- end
- end
- function t.turnLeft()
- if t.cal then
- turtle.turnLeft()
- t.dir = t.dir + 1
- if t.dir > 3 then t.dir = 0 end
- else
- print("not calibrated")
- end
- end
- function t.turnRight()
- if t.cal then
- turtle.turnRight()
- t.dir = t.dir - 1
- if t.dir < 0 then t.dir = 3 end
- else
- print("not calibrated")
- end
- end
- function t.up()
- if t.cal then
- if turtle.up() then
- t.y=t.y+1
- end
- else
- print("not calibrated")
- end
- end
- function t.down()
- if t.cal then
- if turtle.down() then
- t.y=t.y-1
- end
- else
- print("not calibrated")
- end
- end
- function t.west()
- while t.dir ~= 1 do
- t.turnLeft()
- end
- t.forward()
- end
- function t.east()
- while t.dir ~= 3 do
- t.turnLeft()
- end
- t.forward()
- end
- function t.north()
- while t.dir ~= 2 do
- t.turnLeft()
- end
- t.forward()
- end
- function t.south()
- while t.dir ~= 0 do
- t.turnLeft()
- end
- t.forward()
- end
- function t.calibrate()
- local result = true
- local calibrationStones = {}
- t.dir = -1
- while t.dir < 0 do
- turtle.turnLeft()
- if not turtle.detect() then
- --start position is correct
- t.x=0
- t.y=0
- t.z=0
- t.dir=0
- end
- end
- local fuel = turtle.getFuelLevel()
- while fuel<8000 and turtle.refuel(10) do
- sleep(0.2)
- end
- if turtle.getFuelLevel() == 0 then
- result = false
- print("ERROR: No Energy or Fuel")
- end
- if result then
- print("Calibration Successful")
- else
- print("Calibration Failed")
- end
- while t.dir ~= 0 do
- t.turnLeft()
- sleep(0.2)
- end
- t.cal = result
- end
- function t.flyHome()
- print("returning home from "..t.x..","..t.z)
- local returnHeight = math.random(12,18)
- if t.x+t.y+t.z ~= 0 then
- while t.y < returnHeight do
- t.up()
- sleep(0.2)
- end
- while t.x > 0 do
- t.west()
- sleep(0.2)
- end
- while t.x < 0 do
- t.east()
- sleep(0.2)
- end
- while t.z > 0 do
- t.north()
- sleep(0.2)
- end
- while t.z < 0 do
- t.south()
- sleep(0.2)
- end
- while t.y > 0 do
- t.down()
- sleep(0.2)
- end
- while t.dir ~= 0 do
- t.turnLeft()
- sleep(0.2)
- end
- end
- end
- function t.sayPlease()
- redstone.setOutput("front",true)
- sleep(2)
- turtle.suckDown(1)
- redstone.setOutput("front",false)
- t.flyHome()
- end
- function t.checkDistance()
- local xDist
- local zDist
- local yDist = 35
- if t.x > 0 then
- xDist = t.x
- else
- xDist = t.x * -1
- end
- if t.z > 0 then
- zDist = t.z
- else
- zDist = t.z * -1
- end
- local distanceHome = xDist+zDist+yDist
- local fuelLevel = turtle.getFuelLevel()
- print(distanceHome.." "..fuelLevel)
- if fuelLevel < distanceHome then
- print("almost out of fuel, returning home")
- return false
- end
- return true
- end
- --[[t.calibrate()
- if t.cal then
- t.forward()
- t.forward()
- t.forward()
- t.forward()
- t.turnLeft()
- t.forward()
- t.forward()
- t.forward()
- t.forward()
- t.forward()
- t.turnRight()
- t.turnRight()
- t.back()
- t.back()
- t.back()
- t.back()
- t.flyHome()
- end]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement