Skip to content

Commit 92fdfb9

Browse files
Maullerxezon
authored andcommitted
refactor(pathfinder): Cleanup retail compatible insertion sort code (#2331)
1 parent a242967 commit 92fdfb9

2 files changed

Lines changed: 80 additions & 84 deletions

File tree

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

Lines changed: 40 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1668,62 +1668,60 @@ void PathfindCell::removeObstacle( Object *obstacle )
16681668
void PathfindCell::putOnSortedOpenList( PathfindCellList &list )
16691669
{
16701670
DEBUG_ASSERTCRASH(m_info, ("Has to have info."));
1671-
DEBUG_ASSERTCRASH(m_info->m_closed==FALSE && m_info->m_open==FALSE, ("Serious error - Invalid flags. jba"));
1671+
DEBUG_ASSERTCRASH(m_info->m_closed == FALSE && m_info->m_open == FALSE, ("Serious error - Invalid flags. jba"));
1672+
1673+
// mark the newCell as being on the open list
1674+
m_info->m_open = true;
1675+
m_info->m_closed = false;
1676+
16721677
if (list.m_head == nullptr)
16731678
{
16741679
list.m_head = this;
16751680
m_info->m_prevOpen = nullptr;
16761681
m_info->m_nextOpen = nullptr;
1682+
return;
16771683
}
1678-
else
1679-
{
1680-
// insertion sort
1681-
PathfindCell *c, *lastCell = nullptr;
1684+
1685+
// insertion sort
1686+
PathfindCell* currentCell = list.m_head;
1687+
PathfindCell* previousCell = nullptr;
16821688
#if RETAIL_COMPATIBLE_PATHFINDING
1683-
// TheSuperHackers @bugfix In the retail compatible pathfinding, on rare occasions, we get stuck in an infinite loop
1684-
// External code should pickup on the bad behaviour and cleanup properly, but we need to explicitly break out here
1685-
// The fixed pathfinding does not have this issue due to the proper cleanup of pathfindCells and their pathfindCellInfos
1686-
UnsignedInt cellCount = 0;
1687-
for (c = list.m_head; c && cellCount < PATHFIND_CELLS_PER_FRAME; c = c->getNextOpen())
1688-
{
1689-
cellCount++;
1689+
// TheSuperHackers @bugfix In the retail compatible pathfinding, on rare occasions, we get stuck in an infinite loop
1690+
// External code should pickup on the bad behaviour and cleanup properly, but we need to explicitly break out here
1691+
// The fixed pathfinding does not have this issue due to the proper cleanup of pathfindCells and their pathfindCellInfos
1692+
UnsignedInt cellCount = 0;
1693+
while (currentCell && cellCount < PATHFIND_CELLS_PER_FRAME && currentCell->m_info->m_totalCost <= m_info->m_totalCost)
1694+
{
1695+
cellCount++;
16901696
#else
1691-
for (c = list.m_head; c; c = c->getNextOpen())
1692-
{
1697+
while (currentCell && currentCell->m_info->m_totalCost <= m_info->m_totalCost)
1698+
{
16931699
#endif
1694-
if (c->m_info->m_totalCost > m_info->m_totalCost)
1695-
break;
1696-
1697-
lastCell = c;
1698-
}
1700+
previousCell = currentCell;
1701+
currentCell = currentCell->getNextOpen();
1702+
}
16991703

1700-
if (c)
1701-
{
1702-
// insert just before "c"
1703-
if (c->m_info->m_prevOpen)
1704-
c->m_info->m_prevOpen->m_nextOpen = this->m_info;
1705-
else
1706-
list.m_head = this;
1704+
if (currentCell)
1705+
{
1706+
// insert just before "currentCell"
1707+
if (currentCell->m_info->m_prevOpen)
1708+
currentCell->m_info->m_prevOpen->m_nextOpen = this->m_info;
1709+
else
1710+
list.m_head = this;
17071711

1708-
m_info->m_prevOpen = c->m_info->m_prevOpen;
1709-
c->m_info->m_prevOpen = this->m_info;
1712+
m_info->m_prevOpen = currentCell->m_info->m_prevOpen;
1713+
currentCell->m_info->m_prevOpen = this->m_info;
17101714

1711-
m_info->m_nextOpen = c->m_info;
1715+
m_info->m_nextOpen = currentCell->m_info;
17121716

1713-
}
1714-
else
1715-
{
1716-
// append after "lastCell" - end of list
1717-
lastCell->m_info->m_nextOpen = this->m_info;
1718-
m_info->m_prevOpen = lastCell->m_info;
1719-
m_info->m_nextOpen = nullptr;
1720-
}
17211717
}
1722-
1723-
// mark newCell as being on open list
1724-
m_info->m_open = true;
1725-
m_info->m_closed = false;
1726-
1718+
else
1719+
{
1720+
// append after "previousCell" - we are at the end of the list
1721+
previousCell->m_info->m_nextOpen = this->m_info;
1722+
m_info->m_prevOpen = previousCell->m_info;
1723+
m_info->m_nextOpen = nullptr;
1724+
}
17271725
}
17281726

17291727
/// remove self from "open" list

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

Lines changed: 40 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1685,62 +1685,60 @@ Bool PathfindCell::removeObstacle( Object *obstacle )
16851685
void PathfindCell::putOnSortedOpenList( PathfindCellList &list )
16861686
{
16871687
DEBUG_ASSERTCRASH(m_info, ("Has to have info."));
1688-
DEBUG_ASSERTCRASH(m_info->m_closed==FALSE && m_info->m_open==FALSE, ("Serious error - Invalid flags. jba"));
1688+
DEBUG_ASSERTCRASH(m_info->m_closed == FALSE && m_info->m_open == FALSE, ("Serious error - Invalid flags. jba"));
1689+
1690+
// mark the newCell as being on the open list
1691+
m_info->m_open = true;
1692+
m_info->m_closed = false;
1693+
16891694
if (list.m_head == nullptr)
16901695
{
16911696
list.m_head = this;
16921697
m_info->m_prevOpen = nullptr;
16931698
m_info->m_nextOpen = nullptr;
1699+
return;
16941700
}
1695-
else
1696-
{
1697-
// insertion sort
1698-
PathfindCell *c, *lastCell = nullptr;
1701+
1702+
// insertion sort
1703+
PathfindCell* currentCell = list.m_head;
1704+
PathfindCell* previousCell = nullptr;
16991705
#if RETAIL_COMPATIBLE_PATHFINDING
1700-
// TheSuperHackers @bugfix In the retail compatible pathfinding, on rare occasions, we get stuck in an infinite loop
1701-
// External code should pickup on the bad behaviour and cleanup properly, but we need to explicitly break out here
1702-
// The fixed pathfinding does not have this issue due to the proper cleanup of pathfindCells and their pathfindCellInfos
1703-
UnsignedInt cellCount = 0;
1704-
for (c = list.m_head; c && cellCount < PATHFIND_CELLS_PER_FRAME; c = c->getNextOpen())
1705-
{
1706-
cellCount++;
1706+
// TheSuperHackers @bugfix In the retail compatible pathfinding, on rare occasions, we get stuck in an infinite loop
1707+
// External code should pickup on the bad behaviour and cleanup properly, but we need to explicitly break out here
1708+
// The fixed pathfinding does not have this issue due to the proper cleanup of pathfindCells and their pathfindCellInfos
1709+
UnsignedInt cellCount = 0;
1710+
while (currentCell && cellCount < PATHFIND_CELLS_PER_FRAME && currentCell->m_info->m_totalCost <= m_info->m_totalCost)
1711+
{
1712+
cellCount++;
17071713
#else
1708-
for (c = list.m_head; c; c = c->getNextOpen())
1709-
{
1714+
while (currentCell && currentCell->m_info->m_totalCost <= m_info->m_totalCost)
1715+
{
17101716
#endif
1711-
if (c->m_info->m_totalCost > m_info->m_totalCost)
1712-
break;
1713-
1714-
lastCell = c;
1715-
}
1717+
previousCell = currentCell;
1718+
currentCell = currentCell->getNextOpen();
1719+
}
17161720

1717-
if (c)
1718-
{
1719-
// insert just before "c"
1720-
if (c->m_info->m_prevOpen)
1721-
c->m_info->m_prevOpen->m_nextOpen = this->m_info;
1722-
else
1723-
list.m_head = this;
1721+
if (currentCell)
1722+
{
1723+
// insert just before "currentCell"
1724+
if (currentCell->m_info->m_prevOpen)
1725+
currentCell->m_info->m_prevOpen->m_nextOpen = this->m_info;
1726+
else
1727+
list.m_head = this;
17241728

1725-
m_info->m_prevOpen = c->m_info->m_prevOpen;
1726-
c->m_info->m_prevOpen = this->m_info;
1729+
m_info->m_prevOpen = currentCell->m_info->m_prevOpen;
1730+
currentCell->m_info->m_prevOpen = this->m_info;
17271731

1728-
m_info->m_nextOpen = c->m_info;
1732+
m_info->m_nextOpen = currentCell->m_info;
17291733

1730-
}
1731-
else
1732-
{
1733-
// append after "lastCell" - end of list
1734-
lastCell->m_info->m_nextOpen = this->m_info;
1735-
m_info->m_prevOpen = lastCell->m_info;
1736-
m_info->m_nextOpen = nullptr;
1737-
}
17381734
}
1739-
1740-
// mark newCell as being on open list
1741-
m_info->m_open = true;
1742-
m_info->m_closed = false;
1743-
1735+
else
1736+
{
1737+
// append after "previousCell" - we are at the end of the list
1738+
previousCell->m_info->m_nextOpen = this->m_info;
1739+
m_info->m_prevOpen = previousCell->m_info;
1740+
m_info->m_nextOpen = nullptr;
1741+
}
17441742
}
17451743

17461744
/// remove self from "open" list

0 commit comments

Comments
 (0)