SHARE
TWEET

Untitled

a guest Nov 6th, 2016 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public Steering nearestEntity(AISteering aiSteering, float maxSearchRadius)
  2.     {
  3.         IntArray ignoredIndex = new IntArray();
  4.        
  5.         float compareX,compareY;
  6.         float compareSum;
  7.         float smallestSum = 0;
  8.         int index = 0;
  9.         for(int k = 0; k < EntityManager.getEntity().size; k ++)
  10.         {
  11.             if(EntityManager.getEntity(k) == aiSteering.getEntity())
  12.             {
  13.                 ignoredIndex.add(k); //index of the entity that is searching for the nearest entity. Ignore so it doesn't find itself
  14.             }
  15.             else
  16.             {
  17.                 if(!aiSteering.getHostilityList().contains(hostilityMap.get(EntityManager.getEntity(k)).hostile, false))
  18.                 {
  19.                     ignoredIndex.add(k);
  20.                 }
  21.             }
  22.         }
  23.         compareX = Math.abs(positionMap.get(EntityManager.getSteering(0).getEntity()).getX() - aiSteering.getPosition().x);
  24.         compareY = Math.abs(positionMap.get(EntityManager.getSteering(0).getEntity()).getY() - aiSteering.getPosition().y);
  25.        
  26.         compareSum = (float) Math.sqrt(Math.pow(compareX,2) + Math.pow(compareY,2));
  27.         if(compareSum > maxSearchRadius)
  28.         {
  29.             ignoredIndex.add(0);
  30.         }
  31.        
  32.         if(!ignoredIndex.contains(0))
  33.         {
  34.             smallestSum = compareSum;
  35.             index = 0;
  36.         }
  37.         else if(EntityManager.getEntity().size > 1) //to prevent out of bounds
  38.         {
  39.             int ignoree = 1;
  40.             if(!ignoredIndex.contains(ignoree))
  41.             {
  42.                 compareX = Math.abs(positionMap.get(EntityManager.getSteering(ignoree).getEntity()).getX() - aiSteering.getPosition().x);
  43.                 compareY = Math.abs(positionMap.get(EntityManager.getSteering(ignoree).getEntity()).getY() - aiSteering.getPosition().y);
  44.                
  45.                 compareSum = (float) Math.sqrt(Math.pow(compareX,2) + Math.pow(compareY,2));
  46.                 if(compareSum > maxSearchRadius)
  47.                 {
  48.                     ignoredIndex.add(ignoree);
  49.                 }
  50.                 else
  51.                 {
  52.                     smallestSum = compareSum;
  53.                     index = ignoree;
  54.                 }
  55.             }
  56.             while(ignoredIndex.contains(ignoree))
  57.             {
  58.                 ignoree++;
  59.                 if(ignoree >= EntityManager.getSteering().size)
  60.                 {
  61.                     ignoree = EntityManager.getSteering().size-1;
  62.                     break;
  63.                 }
  64.                 if(!ignoredIndex.contains(ignoree))
  65.                 {
  66.                     compareX = Math.abs(positionMap.get(EntityManager.getSteering(ignoree).getEntity()).getX() - aiSteering.getPosition().x);
  67.                     compareY = Math.abs(positionMap.get(EntityManager.getSteering(ignoree).getEntity()).getY() - aiSteering.getPosition().y);
  68.                    
  69.                     compareSum = (float) Math.sqrt(Math.pow(compareX,2) + Math.pow(compareY,2));
  70.                     if(compareSum > maxSearchRadius)
  71.                     {
  72.                         ignoredIndex.add(ignoree);
  73.                     }
  74.                     else
  75.                     {
  76.                         smallestSum = compareSum;
  77.                         index = ignoree;
  78.                     }
  79.                 }
  80.             }
  81.         }
  82.  
  83.         for(int k = 0; k < EntityManager.getEntity().size; k ++)
  84.         {
  85.             if(k == 0 && (ignoredIndex.contains(0) || ignoredIndex.contains(1)))
  86.             {
  87.                 k = 2;
  88.             }
  89.             if((!ignoredIndex.contains(0) && !ignoredIndex.contains(1)) && k == 0)
  90.             {
  91.                 k = 1;
  92.             }
  93.             while(ignoredIndex.contains(k))
  94.             {
  95.                 k++;
  96.             }
  97.  
  98.             if(k < EntityManager.getEntity().size)
  99.             {
  100.                 compareX = Math.abs(positionMap.get(EntityManager.getSteering(k).getEntity()).getX() - aiSteering.getPosition().x);
  101.                 compareY = Math.abs(positionMap.get(EntityManager.getSteering(k).getEntity()).getY() - aiSteering.getPosition().y);
  102.            
  103.                 compareSum = (float) Math.sqrt(Math.pow(compareX,2) + Math.pow(compareY,2));
  104.                 if(compareSum < smallestSum && compareSum <= maxSearchRadius)
  105.                 {
  106.                     smallestSum = compareSum;
  107.                     index = k;
  108.                 }    
  109.             }
  110.         }
  111.         if(ignoredIndex.contains(index))
  112.         {
  113.             return null;
  114.         }
  115.         return EntityManager.getSteering(index);
  116.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top