Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local pos = {
- x = 0,
- y = 0,
- z = 0,
- facing = 0
- }
- local function setX(x)
- pos.x = x
- end
- local function getX()
- return pos.x
- end
- local function setY(y)
- pos.y = y
- end
- local function getY()
- return pos.y
- end
- local function setZ(z)
- pox.z = z
- end
- local function getZ()
- return pos.z
- end
- local function setFacing(f)
- pos.facing = f
- end
- local function getFacing()
- return pos.facing
- end
- local function turnleft()
- pos.facing = pos.facing - 1
- if pos.facing == -1 then pos.facing = 3 end
- turtle.turnLeft()
- end
- local function moveforward(distance)
- if turtle.forward() then
- if pos.facing == 0 then
- pos.z = pos.z + 1
- elseif pos.facing == 2 then
- pos.z = pos.z - 1
- elseif pos.facing == 1 then
- pos.x = pos.x - 1
- elseif pos.facing == 3 then
- pos.x = pos.x + 1
- end
- return true
- else
- return false
- end
- end
- local function turnright()
- pos.facing = pos.facing + 1
- if pos.facing == 4 then pos.facing = 0 end
- turtle.turnRight()
- end
- local function turnaround()
- turnright()
- turnright()
- end
- local function face (faceto)
- local dirdif = faceto-pos.facing
- if dirdif == 1 then turnright()
- elseif dirdif == -3 then turnright()
- elseif dirdif == -1 then turnleft()
- elseif dirdif == 3 then turnleft()
- elseif dirdif == 2 then
- turnaround()
- elseif dirdif == -2 then
- turnaround()
- end
- end
- local function movez(distance)
- if distance ~= 0 then
- if distance < 0 then
- face(2)
- elseif distance > 0 then
- face(0)
- end
- return moveforward(math.abs(distance))
- end
- end
- local function moveup(distance)
- if turtle.up() then
- pos.y = pos.y+1
- return true
- end
- return false
- end
- local function movedown(distance)
- if turtle.down() then
- pos.y = pos.y-1
- return true
- end
- return false
- end
- local function movex(distance)
- if distance ~= 0 then
- if distance < 0 then
- face(1)
- elseif distance > 0 then
- face(3)
- end
- return moveforward(math.abs(distance))
- end
- end
- local function movey(distance)
- if distance ~= 0 then
- if distance < 0 then
- return movedown(math.abs(distance))
- elseif distance > 0 then
- return moveup(distance)
- end
- end
- end
- local function headTo(x, y, z)
- if math.abs(x) > math.abs(y) then
- if math.abs(x) > math.abs(z) then
- if x > 0 then movex(1) else movex(-1) end
- else
- if math.abs(z) > math.abs(y) then
- if z > 0 then movez(1) else movez(-1) end
- else
- if y > 0 then movey(1) else movey(-1) end
- end
- end
- else
- if math.abs(y) > math.abs(z) then
- if y > 0 then movey(1) else movey(-1) end
- else
- if math.abs(x) > math.abs(z) then
- if x > 0 then movex(1) else movex(-1) end
- else
- if z > 0 then movez(1) else movez(-1) end
- end
- end
- end
- end
- local function getdistance(entity)
- return math.abs(entity.Position.X) + math.abs(entity.Position.Y) + math.abs(entity.Position.Z)
- end
- local sensor = peripheral.wrap("right")
- while true do
- local targets = sensor.getTargets()
- local closest
- local closestDistance = 10000000
- for k, btarget in pairs(targets) do
- if getdistance(btarget) < closestDistance and btarget.type ~= "Player" then
- closestDistance = getdistance(btarget)
- closest = btarget
- end
- end
- if closest ~= nil then
- headTo(closest.Position.X, closest.Position.Y, closest.Position.Z)
- else
- sleep(0.1)
- end
- print(closestDistance)
- if closestDistance < 4 then
- turtle.attack()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment