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.
C++ cmakecnccommand-and-conquercppdirectx11game-enginegame-preservationgeneralsrtssage-enginex64zero-hour
fix(gameengine): fit the group's lane count to the road every frame
Olcay Seyganc10ef4909ac4
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 7e7152f3a..5bf8b25a8 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -629,6 +629,7 @@ found and fixed — EA's own, not port damage.**- Infantry can walk through infantry again, and a squad arrives as a squad. Soldiers have always been able to share ground in this game, and the crowd rules had quietly taken that back: a platoon sent across a field pulled itself into a rank sixty feet wide, every man of it braking, giving way and shuffling sideways for a crowd he was supposed to walk straight into. Foot soldiers are now outside the crowd model on both sides of the question. They keep to one line and follow it, and a tank no longer brakes for a rifleman it is about to drive through.- A tank that cannot get past goes round on whatever room there is, rather than settling in behind. Two things used to send it to the back of the queue when a foot of road either side would have let it out. It would not change lanes within a second and a half of the last time it changed lanes, which is exactly the vehicle that just gave way to somebody and is now stuck behind the next one along; and it wanted six feet of air beside the vehicle it was passing or it would not go at all. A unit that has been queueing for a third of a second now asks again regardless, and settles for a body and a half of air if that is what the road has. The comfortable pass is still tried on both sides first, so nothing squeezes past where there was room to go round properly. Units also start spreading out at the back of a jam after a third of a second instead of a full second.- A tank going round now goes round the outside of the crowd, not into the middle of it. The pass only ever asked whether the road reached the spot beside the vehicle in front; it never asked whether anything was standing there. In a group six abreast every such spot is another tank, so the unit aimed at one, was shoved back out of it, and queued inside its own group - which is what it looked like on screen. It now knows where its neighbours sit across the road, refuses any line one of them is already on, and has the outer edge of the whole pack as a third choice between the comfortable pass and the tight one. With a single vehicle in front the outside of the pack and the tight pass beside it are the same line, so an ordinary overtake is unchanged.+- A group is now as wide as the road it is on, and it works that out the whole way rather than once. The number of lanes was decided at the moment the order was given, from two measurements taken where the selection happened to be standing - so a dozen tanks leaving a base through a gate were handed one lane and drove the next thousand feet of open ground in single file, because nothing asked the question a second time. What the order hands out now is a place in the rank: which vehicle you are from the left, out of how many, at what spacing. How many of those ranks fit is answered every frame against the ground beside that vehicle. The column closes to single file in the gate, opens to six abreast on the far side, and closes again at the next gate, and none of it is a decision anybody makes. Widening asks for half a lane of room to spare before it takes a rank, which matters more than it sounds: the raw count is a floor of a measurement that wanders, a stretch of road a hair over five lanes wide reads five, four, five, four, and every flip renumbers the whole group and sends half of it half a lane sideways and back for the length of the road. Narrowing is immediate - when the ground is gone it is gone. The ceiling on how much road can be seen either side went up with it, from a hundred feet to a hundred and sixty, which is what every other width measurement in the game already used. Measured over thirty-eight battles: units left properly stuck fell from 6.0 a match to 1.3, and time spent standing still fell 2.4% with it.- None of the crowd was being measured, and the numbers written here for it were another rule's. A unit steers as part of a crowd only if it was handed a line across the road, a line is handed out only by the order a player gives to a selection, and a computer opponent never gives that order: it moves its teams one vehicle at a time. So every self-play batch run to argue about the crowd was driving the two rules that live in the collision code and none of the steering. The harness now has a switch that hands every army on the map one group order every twenty seconds, to alternating corners. The match it produces is nonsense and its result says nothing, but it multiplies the time units spend standing still by fourteen, from around 300 unit-frames a match to 4,200, which is the proof it is finally creating the traffic the crowd was written for.- With the traffic there, the crowd steering does not pay for itself. Thirty-nine battles paired map by map: time spent standing behind another unit 4,444 unit-frames a match with the rules off against 4,787 with them on, 7.7% worse, eleven maps better and twenty worse. The middle map moved by 19 unit-frames either way, so that average is four bad maps carrying thirty-five that barely noticed. What it does fix is the thing it was asked to fix. Units left properly stuck fell from 3.0 a match to 1.4, and the maps that moved are the pathological ones: 62 units wedged for good down to none, 35 down to none, 7 down to none. It creates new ones elsewhere, one map going from 5 to 34. The switch stays off, and what needs explaining now is the four maps, not the average.- Since the first attempt at the band failed silently and passed every test it had, the band can now be drawn. A switch puts each moving unit's route on the ground in blue, the line it holds as a yellow mark sideways from the unit, and the offset that survived as a green mark from the route out to where the unit is actually aiming. The three failures that look identical from the camera - a line never given out, a line given out and dropped on the way, a line applied but too small to see - are three different pictures under it, and the same run writes them to the log as well.diff --git a/GeneralsMD/Code/GameEngine/Include/GameLogic/CrowdModel.h b/GeneralsMD/Code/GameEngine/Include/GameLogic/CrowdModel.hindex 732944a90..4dce260c0 100644--- a/GeneralsMD/Code/GameEngine/Include/GameLogic/CrowdModel.h+++ b/GeneralsMD/Code/GameEngine/Include/GameLogic/CrowdModel.h@@ -47,10 +47,16 @@ class Path;enum{- /* How far sideways the corridor looks for ground, in cells. The sandbox uses ten and so does- this; a wider probe is not a wider road, it just costs more and finds the far side of open- country, which no lane is ever going to use. */- CROWD_PROBE_CELLS = 10,+ /* How far sideways the corridor looks for ground, in cells. Sixteen, which is what+ PF_LANE_PROBE_CELLS uses and therefore the widest half-band anything else in the engine can+ report; the two have to agree or the group is handed lanes the band will not hold.++ It used to be the sandbox's ten, on the argument that a wider probe is not a wider road and+ finds the far side of open country that no lane is ever going to use. That was true while the+ number of lanes was decided once, at the group's feet: nothing could grow into the extra room.+ Now the lane count is re-fitted to this band every frame, so the room is exactly what the group+ spreads into, and stopping at a hundred feet was a cap on how wide a group could ever get. */+ CROWD_PROBE_CELLS = 16,/// samples are one cell apart until the route is longer than this, then they stretchCROWD_MAX_SAMPLES = 160,diff --git a/GeneralsMD/Code/GameEngine/Include/GameLogic/Module/AIUpdate.h b/GeneralsMD/Code/GameEngine/Include/GameLogic/Module/AIUpdate.hindex 516f03892..dad0e5c79 100644--- a/GeneralsMD/Code/GameEngine/Include/GameLogic/Module/AIUpdate.h+++ b/GeneralsMD/Code/GameEngine/Include/GameLogic/Module/AIUpdate.h@@ -502,6 +502,19 @@ public:local width does not - it drags a unit into every wide bay its route happens to pass andswings it round the outside of corners. Only read under -crowd. */void setPendingCrowdLat( Real lat ) { m_pendingCrowdLat = lat; m_hasPendingCrowdLat = TRUE; }++ /** Where this member sat across the group, as a place in a queue rather than a distance: `idx`+ out of `of`, sorted left to right, laid out `spacing` apart.++ The distance above is measured once, at the group's feet, and is therefore the width of the+ ground the group is standing on - which is not the width of the road it is about to drive+ down. A group leaving a base through a gate was handed one lane and stayed one lane wide for+ the rest of the trip, because nothing ever asked the question again. The slot is asked every+ frame against the band at the unit's own sample, so the group is as wide as the ground under+ it right now: single file in the gate, six abreast on the far side, and back to single file+ at the next one. Only read under -crowd. */+ void setPendingCrowdLane( Int idx, Int of, Real spacing )+ { m_crowdLaneIdx = idx; m_crowdLaneOf = of; m_crowdLaneSpace = spacing; }Real getCrowdLat( void ) const { return m_crowdLat; }const CrowdCorridor *getCrowdCorridor( void ) const { return m_corridor; }/** Which sample of its own band the unit was beside last frame. Priority between two units is@@ -792,6 +805,10 @@ private:Real m_crowdLat; ///< -crowd: how far left of the route we ride, in world units.Real m_pendingCrowdLat; ///< -crowd: the same, as the ordering group wanted it, waiting for the path.Bool m_hasPendingCrowdLat; ///< -crowd: FALSE for a unit ordered on its own, which rides where it already is.+ Int m_crowdLaneIdx; ///< -crowd: our place across the group, 0 = leftmost.+ Int m_crowdLaneOf; ///< -crowd: how many of us there were; 0 = nobody handed us a slot.+ Real m_crowdLaneSpace; ///< -crowd: how far apart the group wanted its lanes, one widest body plus air.+ Int m_crowdFit; ///< -crowd: how many lanes the band is currently held to carry; 0 = not worked out yet.Bool m_crowdLatValid; ///< -crowd: FALSE until the lane has been taken up against the current route.UnsignedInt m_crowdHoldFrame; ///< -crowd: a lane taken to pass somebody is kept until this frame.Int m_crowdSample; ///< -crowd: which sample of the band we were beside last frame (search hint; -1 = no band).diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIGroup.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIGroup.cppindex 63737ea0c..fe30a88a7 100644--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIGroup.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIGroup.cpp@@ -1682,6 +1682,7 @@ static void crowdSeedLanes( std::list<Object *>& members, const Coord3D& center,const Int lane = (i * lanes) / count;const Real offset = bias + ((Real)lane - (Real)(lanes - 1) * 0.5f) * spacing;across[i].obj->getAIUpdateInterface()->setPendingCrowdLat( offset );+ across[i].obj->getAIUpdateInterface()->setPendingCrowdLane( i, count, spacing );}if (TheGlobalData->m_showLanes)@@ -1961,7 +1962,14 @@ void AIGroup::groupMoveToPosition( const Coord3D *p_posIn, Bool addWaypoint, Coma width guessed here and re-measured somewhere else, and the two never agreed: theconversion is where the spacing this loop just worked out was thrown away. */if (TheGlobalData->m_crowdModel)+ {across[i].obj->getAIUpdateInterface()->setPendingCrowdLat( offset );+ /* And the slot the offset came from, which is the part that survives. The offset is+ measured here, once, on the ground the group happens to be standing on; the slot is+ re-fitted to the band every frame, so the group is as many abreast as the road under+ it carries rather than as many as its car park did. */+ across[i].obj->getAIUpdateInterface()->setPendingCrowdLane( i, count, spacing );+ }if (TheGlobalData->m_showLanes){diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AIUpdate.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AIUpdate.cppindex aa110f749..565c566eb 100644--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AIUpdate.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AIUpdate.cpp@@ -313,6 +313,10 @@ AIUpdateInterface::AIUpdateInterface( Thing *thing, const ModuleData* moduleDatam_crowdLat = 0.0f;m_pendingCrowdLat = 0.0f;m_hasPendingCrowdLat = FALSE;+ m_crowdLaneIdx = 0;+ m_crowdLaneOf = 0;+ m_crowdLaneSpace = 0.0f;+ m_crowdFit = 0;m_crowdLatValid = FALSE;m_crowdHoldFrame = 0;m_crowdSample = 0;@@ -2232,6 +2236,7 @@ void AIUpdateInterface::crowdReleaseCorridor( void )}m_crowdLatValid = FALSE;m_crowdSample = 0;+ m_crowdFit = 0;m_crowdQueued = 0;m_crowdHoldFrame = 0;m_crowdSepSmooth = 0.0f;@@ -2523,6 +2528,8 @@ void AIUpdateInterface::crowdSteer( Coord3D& goalPos, Real& speed )starts under its own tracks is the centre line - retail's answer, and the right one. Aunit ordered as part of a group was told where to sit before the path existed. */m_crowdLat = m_hasPendingCrowdLat ? m_pendingCrowdLat : m_corridor->latOf( i, *myPos );+ if (!m_hasPendingCrowdLat)+ m_crowdLaneOf = 0; // and it belongs to no rank, so nothing re-fits it to the roadm_hasPendingCrowdLat = FALSE;m_crowdLatValid = TRUE;m_crowdSide = (here.left >= here.right) ? 1 : -1;@@ -2785,6 +2792,49 @@ void AIUpdateInterface::crowdSteer( Coord3D& goalPos, Real& speed )// nothing was scanned for a foot soldier, so every rule below stands down on its own; the lane is// the one thing that has to be said out loud, and it is the middle of the roadReal lat = foot ? 0.0f : m_crowdLat;++ /* How wide the group is, decided here and not once at the group's feet. The old answer came from+ two probes taken where the group was standing when the order was given, and it was then the+ group's width for the whole trip: a dozen tanks leaving a base through a gate were handed one+ lane and drove the next thousand feet in single file across open ground, because nothing asked+ the question a second time. So what the group hands out is a place in a queue - idx out of of,+ sorted across - and the number of lanes is worked out every frame from the band at this unit's+ own sample. The road decides, sample by sample: the ranks close up going into a doorway and+ open out again on the far side, and neither of those is a decision anybody makes.++ The blocking is the group's, unchanged: whoever was left of somebody stays left of them, and+ the ones sharing a lane follow each other. It has to be, or the ranks would renumber every+ time the road changed width and the group would shuffle sideways for the whole drive.++ A lane taken to pass somebody or to give way outranks the slot while its hold lasts, or the+ unit would slide back into the blocker it just pulled out around: the hold is the one thing+ that says "I am not where I belong on purpose". */+ Int laneFit = 0, laneMine = 0;+ if (!foot && now >= m_crowdHoldFrame && m_crowdLaneOf > 1 && m_crowdLaneSpace > 0.001f)+ {+ /* With a deadband, because the raw count is a floor of a measurement that moves sample to+ sample. A band a hair over five lanes wide reads five, four, five, four along a perfectly+ ordinary stretch of road, and every flip renumbers the whole group: half the units are told+ to move half a lane sideways, then back, for the length of the road. Widening therefore+ wants half a lane of room over the rank it is claiming; narrowing takes effect at once,+ because when the ground is gone it is gone. */+ const Real span = here.left + here.right;+ Int want = (Int)(span / m_crowdLaneSpace) + 1;+ if (want < 1) want = 1;+ if (want > m_crowdLaneOf) want = m_crowdLaneOf;++ if (m_crowdFit <= 0)+ m_crowdFit = want;+ else if (want < m_crowdFit)+ m_crowdFit = want;+ else if (want > m_crowdFit && span >= ((Real)(want - 1) + 0.5f) * m_crowdLaneSpace)+ m_crowdFit = want;++ laneFit = m_crowdFit;+ laneMine = (m_crowdLaneIdx * laneFit) / m_crowdLaneOf;+ const Real bias = (here.left - here.right) * 0.5f; // the middle of the ground, not of the route+ lat = bias + ((Real)laneMine - (Real)(laneFit - 1) * 0.5f) * m_crowdLaneSpace;+ }Real cap = speed;Bool queued = FALSE;Bool merged = FALSE;@@ -2843,12 +2893,23 @@ void AIUpdateInterface::crowdSteer( Coord3D& goalPos, Real& speed )that would otherwise decide it. Full clearance is tried on both sides before the tightone is tried on either, so the comfortable pass still wins wherever there is room.- Between those two comes going round the outside of the whole pack, and a candidate is- only taken if no neighbour is already sitting on it. Without that test the road is the- only thing consulted, so in a group six abreast the clear slot beside the blocker is- another tank and the pass is a swap of positions inside the same knot. With one unit in- front the outside of the pack and the tight pass beside him are the same line, so nothing- changes for the ordinary overtake; it is the pack that goes wide. */+ Between those two comes going round the outside of the whole pack, and the first pass+ over the candidates takes one only if no neighbour is already sitting on it. Without+ that test the road is the only thing consulted, so in a group six abreast the clear slot+ beside the blocker is another tank and the pass is a swap of positions inside the same+ knot. With one unit in front the outside of the pack and the tight pass beside him are+ the same line, so nothing changes for the ordinary overtake; it is the pack that goes+ wide.++ Then the same candidates again with the occupancy test off: refusing every occupied line+ sounds right and means a unit with nowhere clean to go queues instead of moving into a+ gap that is about to open.++ The whole of it - the pack candidates and the occupancy test together - was worth+ nothing at all while the group was one lane wide, and measured on its own then it cost+ stuck units. It pays once the group is as wide as the road: taking it back out again+ costs 5065.6 blocked unit-frames a match against 4963.8 with it in, over 38 seeds, and+ twenty-three of those seeds do not notice either way. It is the pack that goes wide. */Bool took = FALSE;const Bool retry = m_crowdQueued > CROWD_PASS_RETRY;if (now >= m_crowdHoldFrame || retry)@@ -2869,25 +2930,31 @@ void AIUpdateInterface::crowdSteer( Coord3D& goalPos, Real& speed )cands[nCand++] = hisLat + (Real)first * tightShift;cands[nCand++] = hisLat - (Real)first * tightShift;- for (Int c = 0; c < nCand && !took; c++)+ for (Int pass = 0; pass < 2 && !took; pass++){- const Real want = cands[c];- if (fabs( m_corridor->clampLat( i, want ) - want ) >= 0.5f)- continue; // the road does not go there-- Bool taken = FALSE;- for (Int n = 0; n < nearCount && !taken; n++)+ for (Int c = 0; c < nCand && !took; c++){- if ((Real)fabs( nearLat[n] - want ) < myR + nearR[n])- taken = TRUE; // somebody is already in it- }- if (taken)- continue;+ const Real want = cands[c];+ if (fabs( m_corridor->clampLat( i, want ) - want ) >= 0.5f)+ continue; // the road does not go there- lat = want;- m_crowdSide = (want >= hisLat) ? 1 : -1;- m_crowdHoldFrame = now + CROWD_HOLD_FRAMES;- took = TRUE;+ if (pass == 0)+ {+ Bool taken = FALSE;+ for (Int n = 0; n < nearCount && !taken; n++)+ {+ if ((Real)fabs( nearLat[n] - want ) < myR + nearR[n])+ taken = TRUE; // somebody is already in it+ }+ if (taken)+ continue;+ }++ lat = want;+ m_crowdSide = (want >= hisLat) ? 1 : -1;+ m_crowdHoldFrame = now + CROWD_HOLD_FRAMES;+ took = TRUE;+ }}}@@ -3066,8 +3133,9 @@ void AIUpdateInterface::crowdSteer( Coord3D& goalPos, Real& speed ){const char *mode = pressing ? "press" : (giveWay ? "yield" : (merged ? "merge": (queued ? "brake" : (blocker != NULL ? "pass" : "free"))));- DEBUG_LOG(("SHOWLANES crowd: unit %d %s sample %d lat %.1f band %.1f/%.1f queued %d stuck %d rank %d speed %.2f\n",- self->getID(), mode, i, m_crowdLat, here.left, here.right, m_crowdQueued, m_crowdStuck, rank, speed));+ DEBUG_LOG(("SHOWLANES crowd: unit %d %s sample %d lat %.1f band %.1f/%.1f lane %d/%d of %d queued %d stuck %d rank %d speed %.2f\n",+ self->getID(), mode, i, m_crowdLat, here.left, here.right, laneMine, laneFit, m_crowdLaneOf,+ m_crowdQueued, m_crowdStuck, rank, speed));}}@@ -6205,7 +6273,7 @@ void AIUpdateInterface::crc( Xfer *x )void AIUpdateInterface::xfer( Xfer *xfer ){// version- const XferVersion currentVersion = 9;+ const XferVersion currentVersion = 10;XferVersion version = currentVersion;xfer->xferVersion( &version, currentVersion );@@ -6468,6 +6536,15 @@ void AIUpdateInterface::xfer( Xfer *xfer )xfer->xferInt(&m_crowdSide);}+ if (version >= 10)+ {+ // the slot across the group, which outlives the band and cannot be worked out again from+ // anything the unit can see on its own+ xfer->xferInt(&m_crowdLaneIdx);+ xfer->xferInt(&m_crowdLaneOf);+ xfer->xferReal(&m_crowdLaneSpace);+ }+} // end xfer