Hello, I'm working on an AI for Starcraft 1. I'm implementing a two dimensional array which, on an average sized map, will be 128 by 128. A few operations are performed on each square (16384). My problem is that the function crashes roughly half way through. Here is the code:
double **tPFMap;
void buildTPFMap() {
int mapHeight = Broodwar->mapHeight();
int mapWidth = Broodwar->mapWidth();
int percentDone = 0;
// Constructing tPFMap
tPFMap = newdouble*[mapWidth];
for(int i = 0; i < mapWidth; i++) {
tPFMap[i] = newdouble[mapHeight];
}
TilePosition currentTile;
Position currentPos;
// Goal is located 1/4 in from X = 0 and Y = 0 from the top left.
TilePosition goalTile = TilePosition(mapWidth/4, mapHeight/4);
// Note that point 0,0 is the top left
for(int i = 0; i < mapWidth; i++) {
for(int j = 0; j < mapHeight; j++) {
currentTile = TilePosition(i, j);
currentPos = Position((i * 32) + 16, (j * 32) + 16);
TilePosition nearest = TilePosition(BWTA::getNearestUnwalkablePosition(currentPos));
// Sets a positive charge for each tile relative to its distance to the goal.
tPFMap[i][j] += (10/BWTA::getGroundDistance(goalTile, currentTile));
// Sets a negative charge for tiles near unwalkable areas
if(BWTA::getGroundDistance(nearest, currentTile) <= 48) {
Broodwar->drawDot(CoordinateType::Map, currentPos.x(), currentPos.y(), Colors::Red);
tPFMap[i][j] -= 10;
}
}
printf("%d out of %d done\n", i, mapWidth);
}
}
This code will only be called once. Obviously I don't expect you to know the Starcraft related methods and I cant make them any more efficient anyways. I simply need to know if I can make use of the array more efficiently. Thanks for any help!
Run it in a debugger - which line does it fail at and what is the error?
One thing to watch out for is, Line 12 is insufficient for initializing POD (plain-old-data).
In C++, new[] called on an Object will call the default constructor, but new[] called on POD does nothing to initialize.
You need to turn on debugging (-g for gcc, but I don't know what compiler you are using).
Otherwise, I recommend printing tPFMap[i][j] before AND after Line 31.
You should also print mapHeight, mapWidth, and tPFMap (should be a non-null pointer) as a sanity check.
I suspect that you will get garbage due to no initialization.
One other note - since you have a rectangular matrix, I would actually just allocate
double *tPFMap = newdouble[mapWidth * mapHeight];
You have less fragmentation of memory this way (but you will have to update your code a little).