Advertisement
Guest User

mentlerd - BowTurtles

a guest
Jan 31st, 2013
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.58 KB | None | 0 0
  1.  
  2. os.loadAPI("ocs/apis/sensor")
  3.  
  4. SHOOT_AT = {
  5.     [ "Creeper" ]   = true,
  6.     [ "Skeleton" ]  = true,
  7.     [ "Spider"]     = true,
  8. }
  9.  
  10. local function isTarget( data )
  11.     return SHOOT_AT[ data.Name ]
  12. end
  13.  
  14. TRESHOLD = 45
  15. PITCH_OFF = { 0, 90, 180, -90 }
  16.  
  17. local bow  = peripheral.wrap( "left" )
  18. local prox = sensor.wrap("right")
  19.  
  20. local dir = 0
  21.  
  22. while true do
  23.     local targets = prox.getTargets()
  24.    
  25.     local voteLeft = 0
  26.     local voteRight = 0
  27.    
  28.     local hadTarget = false
  29.     local shot = false
  30.    
  31.     for key, data in pairs(targets) do 
  32.         if ( isTarget(data) ) then
  33.        
  34.             local off = data.Position
  35.             local aux = prox.getTargetDetails( key )
  36.            
  37.             if ( aux and aux.IsAlive ) then    
  38.                 local x = off.X
  39.                 local z = off.Z
  40.                
  41.                 local y = off.Y + 1
  42.                
  43.                 local len = math.sqrt( x * x + z * z )
  44.                
  45.                 local pitch = math.deg( -math.atan2( x/len, z/len ) ) - PITCH_OFF[dir +1]
  46.                 local yaw   = math.deg( math.atan( y/len ) )
  47.                
  48.                 if ( pitch < -180 ) then
  49.                     pitch = pitch + 360
  50.                 end
  51.                                    
  52.                 if ( math.abs( yaw ) < TRESHOLD ) then
  53.                     hadTarget = true
  54.                    
  55.                     if ( pitch < -TRESHOLD ) then
  56.                         voteLeft = voteLeft +1
  57.                     elseif ( pitch > TRESHOLD ) then
  58.                         voteRight = voteRight +1
  59.                     else
  60.                         bow.shoot( 1, pitch, yaw )
  61.                         sleep( 0.5 )
  62.    
  63.                         shot = true
  64.                         break
  65.                     end
  66.                 end
  67.  
  68.             end
  69.         end
  70.     end
  71.  
  72.     if ( hadTarget ) then
  73.         if ( not shot ) then
  74.             if ( voteRight > voteLeft ) then                               
  75.                 dir = (dir +1) % 4
  76.                 turtle.turnRight()
  77.             else
  78.                 dir = (dir +3) % 4
  79.                 turtle.turnLeft()
  80.             end
  81.         end
  82.     else
  83.         sleep( 5 )
  84.     end
  85.    
  86. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement