Advertisement
Henness

killer

Jan 8th, 2013
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.01 KB | None | 0 0
  1. -- note, when first placing the turtle, make sure he's facing south
  2.  
  3. local loc = {}
  4. loc = {
  5.  
  6.   x = 0,
  7.   y = 0,
  8.   z = 0,
  9.   facing = 0,
  10.  
  11.  
  12.   facingToAxis = {
  13.     [0] = { axis = 'z', unit = 1 },
  14.     [1] = { axis = 'x', unit = -1 },
  15.     [2] = { axis = 'z', unit = -1 },
  16.     [3] = { axis = 'x', unit = 1 }
  17.   },
  18.  
  19.   getAxisForFacing = function()
  20.     return loc.facingToAxis[loc.facing]['axis'], loc.facingToAxis[loc.facing]['unit']
  21.   end,
  22.  
  23.   turnleft = function()
  24.     loc.facing = (loc.facing - 1) % 4
  25.     turtle.turnLeft()
  26.   end,
  27.  
  28.   turnright = function()
  29.     loc.facing = (loc.facing + 1) % 4
  30.     turtle.turnRight()
  31.   end,
  32.  
  33.   turnaround = function()
  34.     loc.turnright()
  35.     loc.turnright()
  36.   end,
  37.  
  38.   moveforward = function()
  39.     if turtle.forward() then
  40.       local axis, unit = loc.getAxisForFacing()
  41.       loc[axis] = loc[axis] + unit
  42.       return true
  43.      else
  44.       return false
  45.      end
  46.   end,
  47.  
  48.   face = function(faceto)
  49.     local dirdif = (faceto - loc.facing) % 4
  50.     if dirdif == 1 then
  51.       loc.turnright()
  52.     elseif dirdif == 2 then
  53.       loc.turnaround()
  54.     elseif dirdif == 3 then
  55.       loc.turnleft()
  56.     end
  57.   end,
  58.  
  59.   movez = function(d)
  60.     if d < 0 then loc.face(2) elseif d > 0 then loc.face(0) end
  61.     return loc.moveforward()
  62.   end,
  63.  
  64.   movex = function(d)
  65.     if d < 0 then loc.face(1) elseif d > 0 then loc.face(3) end
  66.     return loc.moveforward()
  67.   end,
  68.  
  69.   movey = function(d)
  70.     if d < 0 then
  71.       return loc.movedown()
  72.     else
  73.       return loc.moveup()
  74.     end
  75.   end,
  76.  
  77.   moveup = function()
  78.     if turtle.up() then
  79.       loc.y = loc.y + 1
  80.     end
  81.     return false
  82.   end,
  83.  
  84.   movedown = function()
  85.     if turtle.down() then
  86.       loc.y = loc.y - 1
  87.     end
  88.     return false
  89.   end,
  90.  
  91.   distanceto = function(x, y, z)
  92.     return math.abs(x) + math.abs(y) + math.abs(z)
  93.   end,
  94.  
  95.   distancetopos = function(pos)
  96.     return loc.distanceto(pos.X, pos.Y, pos.Z)
  97.   end,
  98.  
  99.   sortpos = function(a, b)
  100.       return math.abs(a.pos) > math.abs(b.pos)
  101.   end,
  102.  
  103.   headtowards = function (x, y, z)
  104.     local d = {
  105.       { axis = 'x', pos = x },
  106.       { axis = 'y', pos = y },
  107.       { axis = 'z', pos = z }
  108.     }
  109.    
  110.     table.sort(d, loc.sortpos)
  111.  
  112.     for k, v in ipairs(d) do
  113.       print('move'..v.axis)
  114.       if loc['move'..v.axis](v.pos) then
  115.         break
  116.       end
  117.     end
  118.    
  119.   end
  120. }
  121.  
  122. os.loadAPI("ocs/apis/sensor")
  123. local sensor = sensor.wrap("right")
  124. while true do
  125.   local targets = sensor.getTargets()
  126.   local closest
  127.   local closestDistance = 10000000
  128.   for k, btarget in pairs(targets) do
  129.     local dist = loc.distancetopos(btarget.Position)
  130.     if dist < closestDistance and btarget.type == "Player" then
  131.       closestDistance = dist
  132.       closest = btarget
  133.     end
  134.   end
  135.   if closest ~= nil then
  136.     loc.headtowards(closest.Position.X, closest.Position.Y, closest.Position.Z)
  137.   else
  138.     sleep(0.1)  
  139.   end
  140.   print(closestDistance)
  141.   if closestDistance < 4 then
  142.    turtle.attack()
  143.   end
  144. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement