title: How to find the safest/best point to be on a 2D game map?
body: <p>I'm building a prototype stateless game AI, and am interested if there's a way to <strong>combine multiple data sources</strong> and make a decision about the <strong>"best" place to be within a 2D square grid game board</strong></p>
  
  <p>Example inputs:</p>
  
  <ul>
  <li>An array of players/threats (x,y) coordinates</li>
  <li>An array of possible move (x,y) coordinates within this turn</li>
  <li>An array of traps</li>
  <li>An array of terrain bonus/penalties</li>
  <li>An array of proximity to friendly units/healers</li>
  </ul>
  
  <p>For example, here's my method that calculates the furthest distance from a group of players and moves the monster there (if critically injured). I would like to improve this by including data from arrays like above. I'm afraid this will really bloat such methods - <strong>is there a better way to analyze data like I mentioned?</strong></p>
  
  <pre><code>-(void)runAway
  {
      [self debugMessage];
  
  
      int bestTileIndex = 0;
      int maxDistance = 0;
  
      NSMutableArray* playerLocations = [[MapOfTiles sharedInstance] playerLocations];
  
      NSMutableDictionary* validMoveDistanceToEnemy =[[NSMutableDictionary alloc] initWithCapacity:112];
  
      float sum = 0;
  
      //valid move arrays is an array of arrays of tiles at 1, 2, 3,etc moves away from the origin
      if(validMoveArrays.count&gt;0)
      {
  
          for(NSArray* reachableTiles in validMoveArrays)
          {
              for(NSNumber* tileNumber in reachableTiles)
              {
                  sum = 0;
  
                  for(NSNumber* playerLocation in playerLocations)
                  {
                      sum += [self distanceFromTileIndex:tileNumber.intValue toTileIndex:playerLocation.intValue];
                  }
  
                  [validMoveDistanceToEnemy setObject:@(sum) forKey:tileNumber];
              }
          }
      }
  
      DLog(@"validMoveDistanceToEnemy: %@",validMoveDistanceToEnemy);
  
      NSNumber* distanceToEnemy = nil;
      for (NSNumber* key in [validMoveDistanceToEnemy allKeys])
      {
          distanceToEnemy = [validMoveDistanceToEnemy objectForKey:key];
  
          if(distanceToEnemy.intValue &gt;maxDistance)
          {
              maxDistance = distanceToEnemy.intValue;
              bestTileIndex = key.intValue;
          }
  
      }
  
      if(bestTileIndex&gt;tileCountTall*tileCountWide)
      {
          //out of bounds or some other error
          [self stationaryAction];
      }else
      {
          //move to safest tile
          [self.actor moveToTileIndex:bestTileIndex];
      }
  
  
  }
  </code></pre>