Guest User

killer

a guest
Dec 12th, 2012
433
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.65 KB | None | 0 0
  1. local pos = {
  2.   x = 0,
  3.   y = 0,
  4.   z = 0,
  5.   facing = 0
  6. }
  7.  
  8. local function setX(x)
  9.   pos.x = x
  10. end
  11. local function getX()
  12.   return pos.x
  13. end
  14. local function setY(y)
  15.   pos.y = y
  16. end
  17. local function getY()
  18.   return pos.y
  19. end
  20. local function setZ(z)
  21.   pox.z = z
  22. end
  23. local function getZ()
  24.   return pos.z
  25. end
  26. local function setFacing(f)
  27.   pos.facing = f
  28. end
  29. local function getFacing()
  30.   return pos.facing
  31. end
  32.  
  33. local function turnleft()
  34.   pos.facing = pos.facing - 1
  35.   if pos.facing == -1 then pos.facing = 3 end
  36.   turtle.turnLeft()
  37. end
  38.  
  39. local function moveforward(distance)
  40.    if turtle.forward() then
  41.       if pos.facing == 0 then
  42.         pos.z = pos.z + 1
  43.       elseif pos.facing == 2 then
  44.         pos.z = pos.z - 1
  45.       elseif pos.facing == 1 then
  46.         pos.x = pos.x - 1
  47.       elseif pos.facing == 3 then
  48.         pos.x = pos.x + 1
  49.       end
  50.       return true
  51.     else
  52.       return false
  53.     end
  54. end
  55.  
  56. local function turnright()
  57.   pos.facing = pos.facing + 1
  58.   if pos.facing == 4 then pos.facing = 0 end
  59.   turtle.turnRight()
  60. end
  61.  
  62. local function turnaround()
  63.   turnright()
  64.   turnright()
  65. end
  66.  
  67. local function face (faceto)
  68.   local dirdif = faceto-pos.facing
  69.   if dirdif == 1 then turnright()
  70.   elseif dirdif == -3 then turnright()
  71.   elseif dirdif == -1 then turnleft()
  72.   elseif dirdif == 3 then turnleft()
  73.   elseif dirdif == 2 then
  74.     turnaround()
  75.   elseif dirdif == -2 then
  76.     turnaround()
  77.   end
  78. end
  79.  
  80. local function movez(distance)
  81.   if distance ~= 0 then
  82.     if distance < 0 then
  83.       face(2)
  84.     elseif distance > 0 then
  85.       face(0)
  86.     end
  87.     return moveforward(math.abs(distance))
  88.   end
  89. end
  90.  
  91.  
  92. local function moveup(distance)
  93.     if turtle.up() then
  94.       pos.y = pos.y+1
  95.       return true
  96.     end
  97.     return false
  98. end
  99.  
  100. local function movedown(distance)
  101.     if turtle.down() then
  102.       pos.y = pos.y-1
  103.       return true
  104.     end
  105.     return false
  106. end
  107.  
  108. local function movex(distance)
  109.   if distance ~= 0 then
  110.     if distance < 0 then
  111.       face(1)
  112.     elseif distance > 0 then
  113.       face(3)
  114.     end
  115.     return moveforward(math.abs(distance))
  116.   end
  117. end
  118.  
  119. local function movey(distance)
  120.   if distance ~= 0 then
  121.     if distance < 0 then
  122.       return movedown(math.abs(distance))
  123.     elseif distance > 0 then
  124.       return moveup(distance)
  125.     end
  126.   end
  127. end
  128.  
  129. local function headTo(x, y, z)
  130.   if math.abs(x) > math.abs(y) then
  131.     if math.abs(x) > math.abs(z) then
  132.       if x > 0 then movex(1) else movex(-1) end
  133.     else
  134.       if math.abs(z) > math.abs(y) then
  135.         if z > 0 then movez(1) else movez(-1) end
  136.       else
  137.         if y > 0 then movey(1) else movey(-1) end
  138.       end
  139.     end
  140.   else
  141.     if math.abs(y) > math.abs(z) then
  142.         if y > 0 then movey(1) else movey(-1) end
  143.     else
  144.       if math.abs(x) > math.abs(z) then
  145.         if x > 0 then movex(1) else movex(-1) end
  146.       else
  147.         if z > 0 then movez(1) else movez(-1) end
  148.       end
  149.     end
  150.   end
  151. end
  152.  
  153. local function getdistance(entity)
  154.   return  math.abs(entity.Position.X) + math.abs(entity.Position.Y) + math.abs(entity.Position.Z)
  155. end
  156.  
  157. local sensor = peripheral.wrap("right")
  158. while true do
  159.   local targets = sensor.getTargets()
  160.   local closest
  161.   local closestDistance = 10000000
  162.   for k, btarget in pairs(targets) do
  163.     if getdistance(btarget) < closestDistance and btarget.type ~= "Player" then
  164.       closestDistance = getdistance(btarget)
  165.       closest = btarget
  166.     end
  167.   end
  168.   if closest ~= nil then
  169.       headTo(closest.Position.X, closest.Position.Y, closest.Position.Z)
  170.   else
  171.    sleep(0.1)  
  172.   end
  173.    print(closestDistance)
  174.   if closestDistance < 4 then
  175.    turtle.attack()
  176.   end
  177. end
Advertisement
Add Comment
Please, Sign In to add comment