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.

feat(gameengine): charge a turn what the hull takes to make it

Olcay Seygan1df5912acad0
diff --git a/GeneralsMD/Code/GameEngine/Include/Common/GlobalData.h b/GeneralsMD/Code/GameEngine/Include/Common/GlobalData.h
index c1a6504aa..7499f9865 100644
--- a/GeneralsMD/Code/GameEngine/Include/Common/GlobalData.h
+++ b/GeneralsMD/Code/GameEngine/Include/Common/GlobalData.h
@@ -380,6 +380,7 @@ public:
Int m_aiSliceFrames; ///< -aislice <n>: a unit's AI decides once every n logic frames, staggered by id (1 = every frame, the default)
Bool m_noFlowPath; ///< -noflowpath: price terrain only, the way retail does - no clearance, traffic or crossing cost
Bool m_noLanePath; ///< -nolanes: follow the centre of the route, the way retail does - no band, no lane, no passing
+ Bool m_noMomentumPath; ///< -nomomentum: charge a turn retail's flat 4/8/16 instead of what this hull's own turn rate costs
Bool m_showLanes; ///< -showlanes: draw every moving unit's route, the lane it was handed and the offset it kept
Bool m_crowdModel; ///< -crowd: the sandbox crowd model - corridor, absolute lanes, right of way, give way, fan out
Int m_groupDrill; ///< -groupdrill <n>: every n frames, order each player's army corner to corner as one group (0 = off)
diff --git a/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPathfind.h b/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPathfind.h
index 6dca01a82..47ad7f4a5 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPathfind.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPathfind.h
@@ -727,6 +727,21 @@ enum
PF_HEURISTIC_FLOW_NUM = 9,
PF_HEURISTIC_FLOW_DEN = 4,
+ /* Momentum. Retail charges a turn 4, 8 or 16 depending on how sharp it is, the same for a
+ motorcycle and a battleship, which is how a route comes to ask a tank for a turn it has to
+ stop and grind out. What a turn actually costs is the time to swing the hull, and in these
+ units that is exactly speed-per-frame over turn-rate-per-frame for every radian of it: a
+ unit that covers two cells while turning one radian pays two cells for one radian.
+
+ Two caps, and they are different on purpose. Mid-route the charge is held to STEP so the
+ estimate, which cannot see any of this, still has something to steer by - an A* whose real
+ costs run far ahead of its estimate stops walking towards the goal and expands the whole
+ corridor instead. The first step out of the start cell is capped at START instead, higher,
+ because that is the one turn the unit is definitely making right now and the one route
+ nobody wants: the one that begins by reversing out from under its own tracks. */
+ PF_TURN_CAP_STEP = 28, ///< 2 x COST_DIAGONAL
+ PF_TURN_CAP_START = 60,
+
/* Lanes. The three maps above decide where a route goes; these decide where across it a unit
drives. PROBE is how far sideways the ground is measured before the band is called wide
enough, in cells; TAPER is how close to the goal the band closes back onto the centre so
@@ -759,6 +774,15 @@ extern Int Pathfinder_trafficCost( Int baseCost, Int traffic );
/// How much extra a step into a cell somebody else wants at the same moment costs.
extern Int Pathfinder_crossingCost( Int baseCost, Int claimHalves );
+/** What swinging the hull through `radians` costs this chassis, capped at `cap`.
+
+ `chassis` is the unit's own speed per frame divided by its turn rate per frame, which in the
+ pathfinder's units is the cost of one radian directly: a hull that covers 40 world units in
+ the time it turns one radian pays 40 for that radian, the same as walking four cells. It is
+ zero for anything that turns on the spot, and the caller passes zero to switch the whole
+ charge off (`-nomomentum`, or a search with no object behind it). */
+extern Int Pathfinder_turnCost( Real radians, Int chassis, Int cap );
+
/** Where across a band a body sitting `lateral` world units left of the centre line rides.
0 is hard against the right edge, 1 hard against the left, 0.5 the centre; clamped just
inside both edges so a lane is never the wall itself. Half-widths of zero give 0.5. */
@@ -910,6 +934,10 @@ public:
static const char *getMatchProfileReport( void );
static void bumpNoPath( void ); ///< a path request came back empty
static void bumpBlockedFrame( Bool stuck ); ///< one unit-frame spent blocked by another unit
+ static void bumpWedgeFrame( Int consecutive ); ///< one unit-frame stopped with no collision to blame
+ static void bumpEscape( void ); ///< a unit started backing out of somewhere
+ static void bumpCrowdRepath( void ); ///< a unit gave up on its route rather than steer round
+ static void bumpDither( void ); ///< a unit was caught driving a long way and gaining nothing
void processPathfindQueue(void); ///< Process some or all of the queued pathfinds.
void forceMapRecalculation( ); ///< Force pathfind map recomputation. If region is given, only that area is recomputed
diff --git a/GeneralsMD/Code/GameEngine/Source/Common/CommandLine.cpp b/GeneralsMD/Code/GameEngine/Source/Common/CommandLine.cpp
index 6f865b418..efc1e4572 100644
--- a/GeneralsMD/Code/GameEngine/Source/Common/CommandLine.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/Common/CommandLine.cpp
@@ -1412,6 +1412,19 @@ Int parseNoLanePath(char *args[], int num)
return 1;
}
+/* -nomomentum prices a turn the way retail does: 4, 8 or 16 whatever is turning.
+
+ The search charges what the swing actually costs this hull instead - its own speed over its own
+ turn rate - and charges the first step against the direction the unit is already pointing, which
+ retail charged not at all. That is the difference between a route and a route a tank can drive.
+ Same reasoning as the two switches above: the baseline has to be the same binary. */
+Int parseNoMomentumPath(char *args[], int num)
+{
+ if (TheWritableGlobalData)
+ TheWritableGlobalData->m_noMomentumPath = TRUE;
+ return 1;
+}
+
/* -showlanes draws the band model on top of the world.
A movement change that measures well in a batch and cannot be seen in a game is a change nobody
@@ -1760,6 +1773,7 @@ static CommandLineParam params[] =
{ "-aislice", parseAISlice },
{ "-noflowpath", parseNoFlowPath },
{ "-nolanes", parseNoLanePath },
+ { "-nomomentum", parseNoMomentumPath },
{ "-showlanes", parseShowLanes },
{ "-crowd", parseCrowdModel },
{ "-groupdrill", parseGroupDrill },
diff --git a/GeneralsMD/Code/GameEngine/Source/Common/GlobalData.cpp b/GeneralsMD/Code/GameEngine/Source/Common/GlobalData.cpp
index bd1e0a3a9..77f1536f6 100644
--- a/GeneralsMD/Code/GameEngine/Source/Common/GlobalData.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/Common/GlobalData.cpp
@@ -685,6 +685,7 @@ GlobalData::GlobalData()
m_aiSliceFrames = 1; // every unit thinks every frame, which is what the game has always done
m_noFlowPath = FALSE; // the flow model is on; -noflowpath is the retail baseline to measure against
m_noLanePath = FALSE; // lanes are on; -nolanes is the retail baseline to measure against
+ m_noMomentumPath = FALSE; // a turn costs what the hull's own turn rate says; -nomomentum is retail's flat price
m_showLanes = FALSE; // the lane overlay is a diagnostic, off unless -showlanes asks for it
m_crowdModel = FALSE; // the crowd model is the change under test; without -crowd this is the old build
m_groupDrill = 0; // no group orders are faked; -groupdrill is a measuring tool and ruins the match it runs in
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPathfind.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPathfind.cpp
index 6235f6951..4ac402260 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPathfind.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPathfind.cpp
@@ -157,6 +157,17 @@ static Int thePFNoPath = 0; ///< a unit asked for a path and got nothing bac
static Int thePFBlockedFrames = 0; ///< unit-frames spent standing behind another unit
static Int thePFStuckFrames = 0; ///< of those, the ones where the blocker was not moving either
+/* The three above only ever see a collision, and the thing a player complains about is a unit that
+ has stopped without one: facing the wrong way, talked down to no speed by its own steering, or
+ wedged between two allies and a wall it never quite touches. The crowd model is the only part of
+ the game that measures that, and until these existed it measured it privately, one unit at a
+ time, where no batch could read it. */
+static Int thePFWedgeFrames = 0; ///< unit-frames a unit wanted to move, was not moving, and had hit nothing
+static Int thePFWedgeWorst = 0; ///< and the longest one unit was left that way in a row
+static Int thePFEscapes = 0; ///< backing-out manoeuvres started
+static Int thePFCrowdRepaths = 0; ///< routes given up on because nothing on them could get past
+static Int thePFDithers = 0; ///< units caught driving a long way and gaining nothing
+
inline void pfBump( Int slot ) { thePFCalls[ slot ]++; }
class PathProfile
@@ -2182,6 +2193,15 @@ const Int COST_DIAGONAL = 14;
at 3/2; it is only raised while the flow charge is on. */
Int thePathHeuristicNum = 3;
Int thePathHeuristicDen = 2;
+
+/* Momentum, one search's worth. Set by Pathfinder::beginFlowSearch and read by
+ PathfindCell::costSoFar, which has no object and no locomotor of its own to ask. Both are
+ functions of state every machine in a network game already agrees on - the unit's facing and
+ its locomotor's own numbers - so two machines charge the same turns. */
+Int thePathTurnChassis = 0; ///< cost of one radian for the unit being pathed; 0 = charge nothing
+Real thePathStartDirX = 0.0f; ///< which way it is pointing right now, as a unit vector
+Real thePathStartDirY = 0.0f;
+Bool thePathStartDirValid = FALSE;
const Real COST_TO_DISTANCE_FACTOR = 1.0f/10.0f;
const Real COST_TO_DISTANCE_FACTOR_SQR = COST_TO_DISTANCE_FACTOR*COST_TO_DISTANCE_FACTOR;
@@ -2276,17 +2296,53 @@ UnsignedInt PathfindCell::costSoFar( PathfindCell *parent )
ICoord2D dir;
dir.x = prevCell->getXIndex() - parent->getXIndex();
dir.y = prevCell->getYIndex() - parent->getYIndex();
-
+
// count number of direction changes
if (dir.x != prevDir.x || dir.y != prevDir.y)
{
Int dot = dir.x * prevDir.x + dir.y * prevDir.y;
+ Real radians;
if (dot > 0)
+ {
numTurns=4; // 45 degree turn
+ radians = PI/4;
+ }
else if (dot == 0)
+ {
numTurns = 8; // 90 degree turn
+ radians = PI/2;
+ }
else
+ {
numTurns = 16; // 135 degree turn
+ radians = 3*PI/4;
+ }
+
+ /* And what it costs this particular hull, which is the momentum part: retail's three
+ numbers are the same for a bike and a battleship. A floor rather than a replacement,
+ so a unit that turns on the spot still pays retail's price and nothing gets cheaper
+ than it was. */
+ const Int swing = Pathfinder_turnCost( radians, thePathTurnChassis, PF_TURN_CAP_STEP );
+ if (swing > numTurns)
+ numTurns = swing;
+ }
+ }
+ else if (thePathStartDirValid)
+ {
+ /* The first step out of the start cell, charged against where the hull is actually pointing.
+ Nothing used to charge it at all, so a route that began by reversing out from under the
+ unit's own tracks cost exactly the same as one that began by driving straight on, and the
+ search picked between them on length - which is how a column ordered forward sets off by
+ turning round. The angle is the real one here, not one of three: the facing is a
+ continuous number and this runs eight times a search, not millions. */
+ const Real len = (Real)sqrt( (Real)(prevDir.x*prevDir.x + prevDir.y*prevDir.y) );
+ if (len > 0.0f)
+ {
+ // prevDir points back at the parent, so the way we are travelling is the other way
+ Real dot = -((Real)prevDir.x * thePathStartDirX + (Real)prevDir.y * thePathStartDirY) / len;
+ if (dot < -1.0f) dot = -1.0f;
+ else if (dot > 1.0f) dot = 1.0f;
+ numTurns = Pathfinder_turnCost( ACos( dot ), thePathTurnChassis, PF_TURN_CAP_START );
}
}
@@ -4439,6 +4495,17 @@ Int Pathfinder_crossingCost( Int baseCost, Int claimHalves )
return (baseCost * PF_CROSSING_NUM * units) / PF_CROSSING_DEN;
}
+/* The turn charge. See PF_TURN_CAP_STEP for why this is a per-chassis number and not the flat
+ 4/8/16 retail pays, and why the two caps differ. */
+Int Pathfinder_turnCost( Real radians, Int chassis, Int cap )
+{
+ if (chassis <= 0 || radians <= 0.0f)
+ return 0;
+ Int cost = REAL_TO_INT_FLOOR( radians * (Real)chassis );
+ if (cost > cap) cost = cap;
+ return cost;
+}
+
/* Lanes across the band. Both of these are the whole of the lane arithmetic and neither of them
touches the map, so the tests can check the edges - a band with no width, a body already outside
it, a lane hard against a wall - without standing a Pathfinder up. */
@@ -4912,6 +4979,33 @@ void Pathfinder::beginFlowSearch( const Object *obj )
// and the estimate is raised to match what the charge adds, or the search stops steering
thePathHeuristicNum = m_flowCosts ? PF_HEURISTIC_FLOW_NUM : 3;
thePathHeuristicDen = m_flowCosts ? PF_HEURISTIC_FLOW_DEN : 2;
+
+ /* Momentum is a separate question from the three maps and is switched separately: it is about
+ the hull, not about the ground or the traffic on it. A search with no object behind it - is
+ there a way at all, how far apart are these two points, where can I shoot from - has no hull
+ and charges nothing. */
+ thePathTurnChassis = 0;
+ thePathStartDirValid = FALSE;
+ if (obj != NULL && !TheGlobalData->m_noMomentumPath)
+ {
+ const AIUpdateInterface *ai = obj->getAIUpdateInterface();
+ const Locomotor *loco = ai ? ai->getCurLocomotor() : NULL;
+ if (loco != NULL)
+ {
+ const BodyDamageType damage = obj->getBodyModule()->getDamageState();
+ const Real speed = loco->getMaxSpeedForCondition( damage );
+ const Real turn = loco->getMaxTurnRate( damage );
+ // per frame over per frame: world units of driving in the time it takes to turn a radian,
+ // which is one cost unit per world unit here
+ if (speed > 0.01f && turn > 0.0001f)
+ thePathTurnChassis = REAL_TO_INT_FLOOR( speed / turn );
+ }
+
+ const Real facing = obj->getOrientation();
+ thePathStartDirX = (Real)Cos( facing );
+ thePathStartDirY = (Real)Sin( facing );
+ thePathStartDirValid = (thePathTurnChassis > 0);
+ }
}
/* How wide the ground is beside a point, on one side. Half-cell steps, stopping at the first
@@ -4970,6 +5064,11 @@ void Pathfinder::resetMatchProfile( void )
thePFNoPath = 0;
thePFBlockedFrames = 0;
thePFStuckFrames = 0;
+ thePFWedgeFrames = 0;
+ thePFWedgeWorst = 0;
+ thePFEscapes = 0;
+ thePFCrowdRepaths = 0;
+ thePFDithers = 0;
}
void Pathfinder::bumpNoPath( void )
@@ -4984,6 +5083,31 @@ void Pathfinder::bumpBlockedFrame( Bool stuck )
thePFStuckFrames++;
}
+void Pathfinder::bumpWedgeFrame( Int consecutive )
+{
+ thePFWedgeFrames++;
+ /* And the longest any one unit was left like that, which is the number the rescue ladder is
+ promising something about: if nothing is ever ignored for longer than the ladder's own cycle,
+ this stays inside it, and if it does not, the promise is broken and the run says so. */
+ if( consecutive > thePFWedgeWorst )
+ thePFWedgeWorst = consecutive;
+}
+
+void Pathfinder::bumpEscape( void )
+{
+ thePFEscapes++;
+}
+
+void Pathfinder::bumpCrowdRepath( void )
+{
+ thePFCrowdRepaths++;
+}
+
+void Pathfinder::bumpDither( void )
+{
+ thePFDithers++;
+}
+
const char *Pathfinder::getMatchProfileReport( void )
{
__int64 freq = 0;
@@ -5006,9 +5130,11 @@ const char *Pathfinder::getMatchProfileReport( void )
strcpy( thePFMatchReport + len, one );
len += oneLen;
}
- if( len < (Int)sizeof( thePFMatchReport ) - 64 )
- sprintf( thePFMatchReport + len, "| nopath %d outofcells %d blocked %d stuck %d",
- thePFNoPath, thePFOutOfCells, thePFBlockedFrames, thePFStuckFrames );
+ if( len < (Int)sizeof( thePFMatchReport ) - 128 )
+ sprintf( thePFMatchReport + len,
+ "| nopath %d outofcells %d blocked %d stuck %d wedge %d worst %d escape %d repath %d dither %d",
+ thePFNoPath, thePFOutOfCells, thePFBlockedFrames, thePFStuckFrames,
+ thePFWedgeFrames, thePFWedgeWorst, thePFEscapes, thePFCrowdRepaths, thePFDithers );
return thePFMatchReport;
}