Skip to content

[GEN][ZH] Re-initialize pathfinding information cells into a known state in Pathfinder::reset() #1037

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Generals/Code/GameEngine/Include/GameLogic/AIPathfind.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ class PathfindCellInfo
{
friend class PathfindCell;
public:
static void initializeCellInfos(void);
static void allocateCellInfos(void);
static void releaseCellInfos(void);

Expand Down
27 changes: 19 additions & 8 deletions Generals/Code/GameEngine/Source/GameLogic/AI/AIPathfind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1086,21 +1086,15 @@ Real Path::computeFlightDistToGoal( const Coord3D *pos, Coord3D& goalPos )
enum { PATHFIND_CELLS_PER_FRAME=5000}; // Number of cells we will search pathfinding per frame.
enum {CELL_INFOS_TO_ALLOCATE = 30000};
PathfindCellInfo *PathfindCellInfo::s_infoArray = NULL;
PathfindCellInfo *PathfindCellInfo::s_firstFree = NULL;
PathfindCellInfo *PathfindCellInfo::s_firstFree = NULL;
/**
* Allocates a pool of pathfind cell infos.
*/
void PathfindCellInfo::allocateCellInfos(void)
{
releaseCellInfos();
s_infoArray = MSGNEW("PathfindCellInfo") PathfindCellInfo[CELL_INFOS_TO_ALLOCATE]; // pool[]ify
s_infoArray[CELL_INFOS_TO_ALLOCATE-1].m_pathParent = NULL;
s_infoArray[CELL_INFOS_TO_ALLOCATE-1].m_isFree = true;
s_firstFree = s_infoArray;
for (Int i=0; i<CELL_INFOS_TO_ALLOCATE-1; i++) {
s_infoArray[i].m_pathParent = &s_infoArray[i+1];
s_infoArray[i].m_isFree = true;
}
initializeCellInfos();
}

/**
Expand All @@ -1123,6 +1117,20 @@ void PathfindCellInfo::releaseCellInfos(void)
s_firstFree = NULL;
}

/**
* Initializes a pool of pathfind cell infos.
*/
void PathfindCellInfo::initializeCellInfos(void)
{
s_infoArray[CELL_INFOS_TO_ALLOCATE-1].m_pathParent = NULL;
s_infoArray[CELL_INFOS_TO_ALLOCATE-1].m_isFree = true;
s_firstFree = s_infoArray;
for (Int i=0; i<CELL_INFOS_TO_ALLOCATE-1; i++) {
s_infoArray[i].m_pathParent = &s_infoArray[i+1];
s_infoArray[i].m_isFree = true;
}
}

/**
* Gets a pathfindcellinfo.
*/
Expand Down Expand Up @@ -3460,6 +3468,9 @@ void Pathfinder::reset( void )
m_wallHeight = 0.0f;
}
m_zoneManager.reset();

// TheSuperHackers @fix Mauller 09/06/2025 Re-initialize pathfinding cell info objects so they are in a known clean state
PathfindCellInfo::initializeCellInfos();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain what exactly this achieves?

I see in debugger the main effect it has is putting the cell links into linear order again. But what is the effective consequence if the links were shuffled after one map run? Would there be a performance degradation?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i meant to put this up as a draft as i came across any other stuff.

This bit alone was to just put things into a known good state in case any pathfinding info cells were left dangling.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a counter to PathfindCellInfo::getACellInfo, PathfindCellInfo::releaseACellInfo and saw that its usage returned to 0 on map reset. I suggest to add this in debug builds and assert on that condition.

}

/**
Expand Down
1 change: 1 addition & 0 deletions GeneralsMD/Code/GameEngine/Include/GameLogic/AIPathfind.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ class PathfindCellInfo
{
friend class PathfindCell;
public:
static void initializeCellInfos(void);
static void allocateCellInfos(void);
static void releaseCellInfos(void);

Expand Down
27 changes: 19 additions & 8 deletions GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPathfind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1104,21 +1104,15 @@ Real Path::computeFlightDistToGoal( const Coord3D *pos, Coord3D& goalPos )
enum { PATHFIND_CELLS_PER_FRAME=5000}; // Number of cells we will search pathfinding per frame.
enum {CELL_INFOS_TO_ALLOCATE = 30000};
PathfindCellInfo *PathfindCellInfo::s_infoArray = NULL;
PathfindCellInfo *PathfindCellInfo::s_firstFree = NULL;
PathfindCellInfo *PathfindCellInfo::s_firstFree = NULL;
/**
* Allocates a pool of pathfind cell infos.
*/
void PathfindCellInfo::allocateCellInfos(void)
{
releaseCellInfos();
s_infoArray = MSGNEW("PathfindCellInfo") PathfindCellInfo[CELL_INFOS_TO_ALLOCATE]; // pool[]ify
s_infoArray[CELL_INFOS_TO_ALLOCATE-1].m_pathParent = NULL;
s_infoArray[CELL_INFOS_TO_ALLOCATE-1].m_isFree = true;
s_firstFree = s_infoArray;
for (Int i=0; i<CELL_INFOS_TO_ALLOCATE-1; i++) {
s_infoArray[i].m_pathParent = &s_infoArray[i+1];
s_infoArray[i].m_isFree = true;
}
initializeCellInfos();
}

/**
Expand All @@ -1141,6 +1135,20 @@ void PathfindCellInfo::releaseCellInfos(void)
s_firstFree = NULL;
}

/**
* Initializes a pool of pathfind cell infos.
*/
void PathfindCellInfo::initializeCellInfos(void)
{
s_infoArray[CELL_INFOS_TO_ALLOCATE-1].m_pathParent = NULL;
s_infoArray[CELL_INFOS_TO_ALLOCATE-1].m_isFree = true;
s_firstFree = s_infoArray;
for (Int i=0; i<CELL_INFOS_TO_ALLOCATE-1; i++) {
s_infoArray[i].m_pathParent = &s_infoArray[i+1];
s_infoArray[i].m_isFree = true;
}
}

/**
* Gets a pathfindcellinfo.
*/
Expand Down Expand Up @@ -3902,6 +3910,9 @@ void Pathfinder::reset( void )
m_wallHeight = 0.0f;
}
m_zoneManager.reset();

// TheSuperHackers @fix Mauller 09/06/2025 Re-initialize pathfinding cell info objects so they are in a known clean state
PathfindCellInfo::initializeCellInfos();
}

/**
Expand Down
Loading