Skip to content

Zero Hour rebuilt from EA's source: x64, Direct3D 11, VS2022/CMake, ~60 original bugs fixed, an AI that builds a base. Bring your own game.

fix(gameengine): let a joining reinforcement pick up its team's waypoint

Olcay Seygan0b2b3c065682
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8e47a2b14..6b4325a0d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -167,6 +167,7 @@ found and fixed — EA's own, not port damage.**
- A guarding unit no longer fights itself between returning to post and shooting back.
- Engineers can clear mines and booby traps they cannot see.
- A helicopter no longer boards a transport from the air.
+- A computer player's reinforcement catches up with the team it joined. A unit sent to a team already walking a waypoint path took the team's orders without the path itself, which crashed the match; the crash was stopped by leaving the newcomer standing where it spawned, and now it picks up the route from wherever the team has got to. Found and fixed by T0T0W.
- Select a big army, right click once, and all of it goes. In a crowd, units step aside to let a neighbour past, and a unit that stepped aside at the moment it was working out its route forgot the order and stood where it was. Out of 120 units sent across a small map, 7 were left behind, and the bigger the selection the more of them stayed. Now 200 out of 200 get there. Clicking on a building did the same thing to the whole group: there was no open ground right under the cursor, so nobody was given a place to stand. The group now stands around the building.
- The whole unit limit moves on one click. The lobby allows 840 units in a match, and the game could only keep 512 of them waiting for a route at once: sent across the map together, 329 of 841 never took a step, then or ever. All 840 go now, whether one player owns them or two armies of 420 are crossing through each other, and the game never falls behind: the click itself costs 13 milliseconds, and no moment of the march took longer than 33, the time the game has for each step of the battle. With all 840 on screen the picture averages 30 frames a second on the new renderer and 37 on the old one, 2.7 milliseconds quicker than before because a crowd of infantry now redraws its shadows' poses in turns instead of every soldier every frame.
- A tank in a column no longer parks behind the one in front while that one drives away. Touching the tank ahead cut its speed a little more every moment and never let it come back, so once the leader had paused, the follower sat at a dead stop until their hulls came apart and then crawled back up to speed. Every pause at the head of a column rippled down it as a line of tanks standing still. The follower now picks its speed up again as soon as the one in front moves off. Across eight maps, stops of a second or more behind the unit ahead fell from 27 to 19 in a 30-unit squad and from 1169 to 781 in a 200-unit army, the longest single stop in that army went from 14.6 seconds to 9, and the last tank of the squad arrives 6.6 seconds sooner. In computer-against-computer matches, the time units spent jammed so hard that the game counted them stuck fell from about 1.3 seconds a match to half a second.
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIStates.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIStates.cpp
index 2e38d29ec..d2eed066f 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIStates.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIStates.cpp
@@ -4538,6 +4538,15 @@ void AIFollowWaypointPathState::loadPostProcess( void )
AIInternalMoveToState::loadPostProcess();
} // end loadPostProcess
+//----------------------------------------------------------------------------------------------------------
+/** Pick the waypoint a unit joining a group should start from. A freshly joined unit has no
+ waypoint of its own, while the rest of its team may already be part-way along the path. */
+const Waypoint *AIFollowWaypointPath_groupInitialWaypoint( const Waypoint *goalWaypoint,
+ const Waypoint *teamWaypoint )
+{
+ return goalWaypoint ? goalWaypoint : teamWaypoint;
+}
+
//----------------------------------------------------------------------------------------------------------
StateReturnType AIFollowWaypointPathState::onEnter()
{
@@ -4545,18 +4554,24 @@ StateReturnType AIFollowWaypointPathState::onEnter()
m_priorWaypoint = NULL;
m_currentWaypoint = ((AIStateMachine *)getMachine())->getGoalWaypoint();
AIUpdateInterface *ai = getMachineOwner()->getAI();
+ Object *obj = getMachineOwner();
// EA let a group move through with no waypoint and read its location on the next line: a unit
// joining an AI team copied a teammate's path state but not its waypoint (joinTeam, from
- // AIPlayer::checkReadyTeams), and took the game down here
+ // AIPlayer::checkReadyTeams), and took the game down here. The reinforcement takes up the path
+ // where its team already is rather than failing out of it, which is what refusing the state did
+ // and which left it standing while the rest of the team walked off. Found by T0T0W.
+ if (m_moveAsGroup)
+ {
+ m_currentWaypoint = AIFollowWaypointPath_groupInitialWaypoint(
+ m_currentWaypoint, obj->getTeam()->getCurrentWaypoint());
+ }
if (m_currentWaypoint == NULL) return STATE_FAILURE;
getMachine()->setGoalPosition(m_currentWaypoint->getLocation());
m_framesSleeping = 0;
m_groupOffset.x = m_groupOffset.y = 0;
-
- Object *obj = getMachineOwner();
/* Interesting thought experiment. Didn't work well. jba
Real distSqrLimit = 9*obj->getGeometryInfo().getMajorRadius()*obj->getGeometryInfo().getMajorRadius();
const Waypoint *way = m_currentWaypoint;
@@ -4591,9 +4606,6 @@ StateReturnType AIFollowWaypointPathState::onEnter()
m_groupOffset.y = obj->getPosition()->y - center.y;
}
}
- if (m_currentWaypoint==NULL && m_moveAsGroup) {
- m_currentWaypoint = obj->getTeam()->getCurrentWaypoint();
- }
// set initial movement goal
computeGoal(m_moveAsGroup);
StateReturnType ret = AIInternalMoveToState::onEnter();
diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cpp
index e563ae715..b8ca48e5d 100644
--- a/GeneralsMD/Code/Tests/test_gameengine.cpp
+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp
@@ -1346,6 +1346,27 @@ TEST(attack_move_turns_on_whoever_is_shooting_it)
CHECK( !AIAttackMove_shouldRetaliate( 0xffffffff, 0xfffffff0, WINDOW, false, true ) );
}
+/* AIStates.cpp: joinTeam clears a reinforcement's own waypoint before copying the state of a unit
+ already in its team. If that state follows a waypoint path as a group, the reinforcement has to
+ pick up the team's current waypoint before the state reads its location. Found by T0T0W. */
+extern const Waypoint *AIFollowWaypointPath_groupInitialWaypoint( const Waypoint *goalWaypoint,
+ const Waypoint *teamWaypoint );
+
+TEST(a_reinforcement_joins_the_waypoint_path_its_team_is_following)
+{
+ const Waypoint *ownWaypoint = (const Waypoint *)0x100;
+ const Waypoint *teamWaypoint = (const Waypoint *)0x200;
+
+ /* An explicit order stays authoritative; joining must not rewind the unit to the team's point. */
+ CHECK_EQ( AIFollowWaypointPath_groupInitialWaypoint( ownWaypoint, teamWaypoint ), ownWaypoint );
+
+ /* This is the crash case: joinTeam left no local goal, but the active team has one. */
+ CHECK_EQ( AIFollowWaypointPath_groupInitialWaypoint( NULL, teamWaypoint ), teamWaypoint );
+
+ /* A team whose path has ended has nothing safe to dereference; the state must fail instead. */
+ CHECK( AIFollowWaypointPath_groupInitialWaypoint( NULL, NULL ) == NULL );
+}
+
/* BitFlags used to be templated on the bit count alone, and its name table is a static
member of the instantiation - so two flag sets with the same count were the SAME type
and shared one s_bitNameList. The linker folded the two definitions and kept whichever