0

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];
    }


}
Till
  • 27,559
  • 13
  • 88
  • 122
Alex Stone
  • 46,408
  • 55
  • 231
  • 407
  • Found that the distance to enemy like above is called "Manhattan Distance" - the number of steps required to get there on a 2d board – Alex Stone Dec 17 '13 at 02:25

1 Answers1

3

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)

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • Borders was awesome... wish they were still funct. but +1 game AI is a good place to start, a lot of them have very practical examples (but they are often tied to some sort of framework, so read the forward.) – Grady Player Dec 17 '13 at 01:05