How to find the safest/best point to be on a 2D game map?

83 views Asked by At

I'm building a prototype stateless game AI, and am interested if there's a way to combine multiple data sources and make a decision about the "best" place to be within a 2D square grid game board

Example inputs:

  • An array of players/threats (x,y) coordinates
  • An array of possible move (x,y) coordinates within this turn
  • An array of traps
  • An array of terrain bonus/penalties
  • An array of proximity to friendly units/healers

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 - is there a better way to analyze data like I mentioned?

-(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>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 >maxDistance)
        {
            maxDistance = distanceToEnemy.intValue;
            bestTileIndex = key.intValue;
        }

    }

    if(bestTileIndex>tileCountTall*tileCountWide)
    {
        //out of bounds or some other error
        [self stationaryAction];
    }else
    {
        //move to safest tile
        [self.actor moveToTileIndex:bestTileIndex];
    }


}
1

There are 1 answers

1
Duncan C On

This isn't an Objective C question, it's an algorithms/AI question.

This is a deep and complex subject.

One approach would be to assign different weights to the different pro/con factors for each tile in the game, and then come up with a total desirability score for each tile based on the sums of those weighted values.

You might also solve a system of simultaneous equations to find the ideal solution.

I'd suggest buying a book on game AI. I've seen quite a few when I've looked int the tech sections of large bookstores (like the now-defunct Borders here in the US)