Advertisement
TheModerGuy

gpsplayerfinder

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