Skip to content

Commit 674ba9e

Browse files
authored
unify(pathfinder): Merge local variable optimizations in PathfindZoneManager::calculateZones() from Zero Hour (TheSuperHackers#2367)
1 parent 83a51f8 commit 674ba9e

File tree

1 file changed

+59
-46
lines changed

1 file changed

+59
-46
lines changed

Generals/Code/GameEngine/Source/GameLogic/AI/AIPathfind.cpp

Lines changed: 59 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2606,30 +2606,35 @@ void PathfindZoneManager::calculateZones( PathfindCell **map, PathfindLayer laye
26062606
#endif
26072607
#endif
26082608
// Now map the zones in the map back into the collapsed zones.
2609-
for( j=globalBounds.lo.y; j<=globalBounds.hi.y; j++ ) {
2610-
for( i=globalBounds.lo.x; i<=globalBounds.hi.x; i++ ) {
2611-
map[i][j].setZone(collapsedZones[map[i][j].getZone()]);
2609+
for( j=globalBounds.lo.y; j<=globalBounds.hi.y; j++ ) {
2610+
for( i=globalBounds.lo.x; i<=globalBounds.hi.x; i++ ) {
2611+
PathfindCell &cell = map[i][j];
2612+
cell.setZone(collapsedZones[cell.getZone()]);
26122613
if (map[i][j].getZone()==0) {
26132614
DEBUG_CRASH(("Zone not set cell %d, %d", i, j));
26142615
}
26152616
}
26162617
}
26172618
for (i=0; i<=LAYER_LAST; i++) {
2618-
Int zone = collapsedZones[layers[i].getZone()];
2619+
PathfindLayer &r_thisLayer = layers[i];
2620+
2621+
Int zone = collapsedZones[r_thisLayer.getZone()];
26192622
if (zone == 0) {
26202623
zone = m_maxZone;
26212624
m_maxZone++;
26222625
}
2623-
layers[i].setZone( zone );
2626+
2627+
r_thisLayer.setZone( zone );
26242628
if (!layers[i].isUnused() && !layers[i].isDestroyed() && layers[i].getZone()==0) {
26252629
DEBUG_CRASH(("Zone not set Layer %d", i));
26262630
}
2627-
layers[i].applyZone();
2628-
if (!layers[i].isUnused() && !layers[i].isDestroyed()) {
2631+
r_thisLayer.applyZone();
2632+
2633+
if (!r_thisLayer.isUnused() && !r_thisLayer.isDestroyed()) {
26292634
ICoord2D ndx;
2630-
layers[i].getStartCellIndex(&ndx);
2635+
r_thisLayer.getStartCellIndex(&ndx);
26312636
setBridge(ndx.x, ndx.y, true);
2632-
layers[i].getEndCellIndex(&ndx);
2637+
r_thisLayer.getEndCellIndex(&ndx);
26332638
setBridge(ndx.x, ndx.y, true);
26342639
}
26352640
}
@@ -2675,64 +2680,72 @@ void PathfindZoneManager::calculateZones( PathfindCell **map, PathfindLayer laye
26752680
m_hierarchicalZones[i] = i;
26762681
}
26772682

2678-
for( j=globalBounds.lo.y; j<=globalBounds.hi.y; j++ ) {
2679-
for( i=globalBounds.lo.x; i<=globalBounds.hi.x; i++ ) {
2680-
if ( (map[i][j].getConnectLayer() > LAYER_GROUND) &&
2681-
(map[i][j].getType() == PathfindCell::CELL_CLEAR) ) {
2682-
PathfindLayer *layer = layers + map[i][j].getConnectLayer();
2683-
resolveZones(map[i][j].getZone(), layer->getZone(), m_hierarchicalZones, m_maxZone);
2683+
for( j=globalBounds.lo.y; j<=globalBounds.hi.y; j++ ) {
2684+
for( i=globalBounds.lo.x; i<=globalBounds.hi.x; i++ ) {
2685+
PathfindCell &r_thisCell = map[i][j];
2686+
2687+
if ( (r_thisCell.getConnectLayer() > LAYER_GROUND) &&
2688+
(r_thisCell.getType() == PathfindCell::CELL_CLEAR) ) {
2689+
PathfindLayer *layer = layers + r_thisCell.getConnectLayer();
2690+
resolveZones(r_thisCell.getZone(), layer->getZone(), m_hierarchicalZones, m_maxZone);
26842691
}
2685-
if (i>globalBounds.lo.x && map[i][j].getZone()!=map[i-1][j].getZone()) {
2686-
if (map[i][j].getType() == map[i-1][j].getType()) {
2687-
applyZone(map[i][j], map[i-1][j], m_hierarchicalZones, m_maxZone);
2692+
2693+
if ( i > globalBounds.lo.x && r_thisCell.getZone() != map[i-1][j].getZone() ) {
2694+
const PathfindCell &r_leftCell = map[i-1][j];
2695+
2696+
if (r_thisCell.getType() == r_leftCell.getType()) {
2697+
applyZone(r_thisCell, r_leftCell, m_hierarchicalZones, m_maxZone);
26882698
}
2689-
if (waterGround(map[i][j], map[i-1][j])) {
2690-
applyZone(map[i][j], map[i-1][j], m_groundWaterZones, m_maxZone);
2699+
if (waterGround(r_thisCell, r_leftCell)) {
2700+
applyZone(r_thisCell, r_leftCell, m_groundWaterZones, m_maxZone);
26912701
}
2692-
if (groundRubble(map[i][j], map[i-1][j])) {
2693-
Int zone1 = map[i][j].getZone();
2694-
Int zone2 = map[i-1][j].getZone();
2702+
if (groundRubble(r_thisCell, r_leftCell)) {
2703+
Int zone1 = r_thisCell.getZone();
2704+
Int zone2 = r_leftCell.getZone();
26952705
if (m_terrainZones[zone1] != m_terrainZones[zone2]) {
26962706
//DEBUG_LOG(("Matching terrain zone %d to %d.", zone1, zone2));
26972707
}
2698-
applyZone(map[i][j], map[i-1][j], m_groundRubbleZones, m_maxZone);
2708+
applyZone(r_thisCell, r_leftCell, m_groundRubbleZones, m_maxZone);
26992709
}
2700-
if (groundCliff(map[i][j], map[i-1][j])) {
2701-
applyZone(map[i][j], map[i-1][j], m_groundCliffZones, m_maxZone);
2710+
if (groundCliff(r_thisCell, r_leftCell)) {
2711+
applyZone(r_thisCell, r_leftCell, m_groundCliffZones, m_maxZone);
27022712
}
2703-
if (terrain(map[i][j], map[i-1][j])) {
2704-
applyZone(map[i][j], map[i-1][j], m_terrainZones, m_maxZone);
2713+
if (terrain(r_thisCell, r_leftCell)) {
2714+
applyZone(r_thisCell, r_leftCell, m_terrainZones, m_maxZone);
27052715
}
2706-
if (crusherGround(map[i][j], map[i-1][j])) {
2707-
applyZone(map[i][j], map[i-1][j], m_crusherZones, m_maxZone);
2716+
if (crusherGround(r_thisCell, r_leftCell)) {
2717+
applyZone(r_thisCell, r_leftCell, m_crusherZones, m_maxZone);
27082718
}
27092719
}
2710-
if (j>globalBounds.lo.y && map[i][j].getZone()!=map[i][j-1].getZone()) {
2711-
if (map[i][j].getType() == map[i][j-1].getType()) {
2712-
applyZone(map[i][j], map[i][j-1], m_hierarchicalZones, m_maxZone);
2720+
2721+
if (j>globalBounds.lo.y && r_thisCell.getZone()!=map[i][j-1].getZone()) {
2722+
const PathfindCell &r_topCell = map[i][j-1];
2723+
2724+
if (r_thisCell.getType() == r_topCell.getType()) {
2725+
applyZone(r_thisCell, r_topCell, m_hierarchicalZones, m_maxZone);
27132726
}
2714-
if (waterGround(map[i][j],map[i][j-1])) {
2715-
applyZone(map[i][j], map[i][j-1], m_groundWaterZones, m_maxZone);
2727+
if (waterGround(r_thisCell, r_topCell)) {
2728+
applyZone(r_thisCell, r_topCell, m_groundWaterZones, m_maxZone);
27162729
}
2717-
if (groundRubble(map[i][j], map[i][j-1])) {
2718-
Int zone1 = map[i][j].getZone();
2719-
Int zone2 = map[i][j-1].getZone();
2730+
if (groundRubble(r_thisCell, r_topCell)) {
2731+
Int zone1 = r_thisCell.getZone();
2732+
Int zone2 = r_topCell.getZone();
27202733
if (m_terrainZones[zone1] != m_terrainZones[zone2]) {
27212734
//DEBUG_LOG(("Matching terrain zone %d to %d.", zone1, zone2));
27222735
}
2723-
applyZone(map[i][j], map[i][j-1], m_groundRubbleZones, m_maxZone);
2736+
applyZone(r_thisCell, r_topCell, m_groundRubbleZones, m_maxZone);
27242737
}
2725-
if (groundCliff(map[i][j],map[i][j-1])) {
2726-
applyZone(map[i][j], map[i][j-1], m_groundCliffZones, m_maxZone);
2738+
if (groundCliff(r_thisCell, r_topCell)) {
2739+
applyZone(r_thisCell, r_topCell, m_groundCliffZones, m_maxZone);
27272740
}
2728-
if (terrain(map[i][j], map[i][j-1])) {
2729-
applyZone(map[i][j], map[i][j-1], m_terrainZones, m_maxZone);
2741+
if (terrain(r_thisCell, r_topCell)) {
2742+
applyZone(r_thisCell, r_topCell, m_terrainZones, m_maxZone);
27302743
}
2731-
if (crusherGround(map[i][j], map[i][j-1])) {
2732-
applyZone(map[i][j], map[i][j-1], m_crusherZones, m_maxZone);
2744+
if (crusherGround(r_thisCell, r_topCell)) {
2745+
applyZone(r_thisCell, r_topCell, m_crusherZones, m_maxZone);
27332746
}
27342747
}
2735-
DEBUG_ASSERTCRASH(map[i][j].getZone() != 0, ("Cleared the zone."));
2748+
DEBUG_ASSERTCRASH(r_thisCell.getZone() != 0, ("Cleared the zone."));
27362749
}
27372750
}
27382751

0 commit comments

Comments
 (0)