So I am programming a RTS with DirectX and I have managed to get all the troops, buildings, FX, and terrain on the screen and still never drop below 50 FPS. However, when I start running the AI code, the game drops to 20 FPS, and then eventually to 13 FPS.
Upon checking the CPU Usage data I have determined that my A* path function is creating an issue:
std::vector<INTPOINT> TERRAIN::GetPath(INTPOINT start, INTPOINT goal)
{
try
{
//Check that the two points are within the bounds of the map
MAPTILE *startTile = GetTile(start);
MAPTILE *goalTile = GetTile(goal);
if (!Within(start) || !Within(goal) || start == goal || startTile == NULL || goalTile == NULL)
return std::vector<INTPOINT>();
//Check if a path exists
if (!startTile->m_walkable || !goalTile->m_walkable || startTile->m_set != goalTile->m_set)
return std::vector<INTPOINT>();
//Init Search
long numTiles = m_size.x * m_size.y;
for (long l = 0; l < numTiles; l++)
{
m_pMapTiles[l].f = m_pMapTiles[l].g = FLT_MAX; //Clear F,G
m_pMapTiles[l].open = m_pMapTiles[l].closed = false; //Reset Open and Closed
}
std::vector<MAPTILE*> open; //Create Our Open list
startTile->g = 0; //Init our starting point (SP)
startTile->f = H(start, goal);
startTile->open = true;
open.push_back(startTile); //Add SP to the Open list
bool found = false; // Search as long as a path hasnt been found,
while (!found && !open.empty()) // or there is no more tiles to search
{
MAPTILE * best = open[0]; // Find the best tile (i.e. the lowest F value)
int bestPlace = 0;
for (int i = 1; i < (int)open.size(); i++)
if (open[i]->f < best->f)
{
best = open[i];
bestPlace = i;
}
if (best == NULL)break; //No path found
open[bestPlace]->open = false;
// Take the best node out of the Open list
open.erase(open.begin() + bestPlace);
if (best->m_mappos == goal) //If the goal has been found
{
std::vector<INTPOINT> p, p2;
MAPTILE *point = best;
while (point->m_mappos != start) // Generate path
{
p.push_back(point->m_mappos);
point = point->m_pParent;
}
for (int i = (int)p.size() - 1; i != 0; i--) // Reverse path
p2.push_back(p[i]);
p2.push_back(goal);
return p2;
}
else
{
for (int i = 0; i < 8; i++) // otherwise, check the neighbors of the
if (best->m_pNeighbors[i] != NULL) // best tile
{
bool inList = false; // Generate new G and F value
float newG = best->g + 1.0f;
float d = H(best->m_mappos, best->m_pNeighbors[i]->m_mappos);
float newF = newG + H(best->m_pNeighbors[i]->m_mappos, goal) + best->m_pNeighbors[i]->m_cost * 5.0f * d;
if (best->m_pNeighbors[i]->open || best->m_pNeighbors[i]->closed)
{
if (newF < best->m_pNeighbors[i]->f) // If the new F value is lower
{
best->m_pNeighbors[i]->g = newG; // update the values of this tile
best->m_pNeighbors[i]->f = newF;
best->m_pNeighbors[i]->m_pParent = best;
}
inList = true;
}
if (!inList) // If the neighbor tile isn't in the Open or Closed list
{
best->m_pNeighbors[i]->f = newF; //Set the values
best->m_pNeighbors[i]->g = newG;
best->m_pNeighbors[i]->m_pParent = best;
best->m_pNeighbors[i]->open = true;
open.push_back(best->m_pNeighbors[i]); //Add it to the open list
}
}
best->closed = true; //The best tile has now been searched, add it to the Closed list
}
}
return std::vector<INTPOINT>(); //No path found, return an empty path
}
catch (...)
{
debug.Print("Error in TERRAIN::GetPath()");
return std::vector<INTPOINT>();
}
}
How were games like Empire Earth and Age of Empires able to make calculations like this one without the tax on the computer that I am suffering? Only around 5 units are making this calculation, while Age and Empire Earth are capable of doing hundreds at the time without a significant drop in frame rate.
What could be the issue here?
-rydog
Edit: Sorry about the crappy title name. I did not double check it before I clicked “post," and it does not appear that I am able to change it.