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): send the salvage collector back to its post
Olcay Seygane124f7ee0a18
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 0435be2df..195aa6024 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -356,7 +356,8 @@ found and fixed — EA's own, not port damage.**## One crate, one collector-- Salvage gets collected. A wreck leaves money and a free upgrade lying on the ground, and the game asked you to spot it in the middle of the fight that made it, work out which of your units was allowed to take it, and drive that one over it by hand. Nobody does that, so most salvage on most maps timed out where it fell. Whoever is standing nearest with nothing to do goes and gets it now: a unit that can still be upgraded off it goes first, however far back it is standing, and failing that the nearest idle unit takes the cash. Only idle units, so nothing is ever pulled out of a fight or off an order, and dozers and harvesters are left to the job they are already earning at.+- Salvage gets collected. A wreck leaves money and a free upgrade lying on the ground, and the game asked you to spot it in the middle of the fight that made it, work out which of your units was allowed to take it, and drive that one over it by hand. Nobody does that, so most salvage on most maps timed out where it fell. Whoever is standing nearest with nothing to do goes and gets it now: a unit that can still be upgraded off it goes first, however far back it is standing, and failing that the nearest idle unit takes the cash. Dozers and harvesters are left to the job they are already earning at.+- And the collector comes back. A line dug in across a road is idle, every man of it, so the first version of this walked the line out to the wrecks in front and left it standing there - the position lost to the crates it had just earned. A unit sent for a crate now walks back to the spot it left. Nobody with an enemy in sight goes at all, and nothing is called further than a few seconds away. Anything that dies when it fires stays where it is: a Terrorist is worth more than every crate on the map.- And anybody can take the money. A salvage crate could only be picked up by a unit that salvages, so one dropped among troops who could not use it was money that belonged to nobody. The parts still only go to a salvager - the weapon, the armour, the promotion - but the cash goes to whoever drives over it.- A crate pays once per frame, not once per soldier who touched it.- A unit that kills something carrying a crate now goes for the crate. The game noted the crate, then forgot which one before it looked, so the "pick up what I just dropped" order at seven places in the code never once fired.diff --git a/GeneralsMD/Code/GameEngine/Include/GameLogic/Module/AIUpdate.h b/GeneralsMD/Code/GameEngine/Include/GameLogic/Module/AIUpdate.hindex f468ca392..b261181f3 100644--- a/GeneralsMD/Code/GameEngine/Include/GameLogic/Module/AIUpdate.h+++ b/GeneralsMD/Code/GameEngine/Include/GameLogic/Module/AIUpdate.h@@ -720,6 +720,9 @@ public:// this is intended for use ONLY by the production exit modules.void friend_setExitProductionRallyPoint( const Coord3D *pos );++ // this is intended for use ONLY by the salvage collection pass in GameLogic.+ void friend_setSalvageReturnPosition( const Coord3D *pos );#if defined(_DEBUG) || defined(_INTERNAL)inline const Coord3D *friend_getRequestedDestination() const { return &m_requestedDestination; }inline const Coord3D *friend_getRequestedDestination2() const { return &m_requestedDestination2; }@@ -897,6 +900,8 @@ private:ObjectID m_moveOutOfWay2;Coord3D m_exitProductionRallyPoint; ///< Rally point to attack-move to once we are clear of the producer that just built us.Bool m_hasExitProductionRallyPoint; ///< True while m_exitProductionRallyPoint is still waiting to be ordered.+ Coord3D m_salvageReturnPosition; ///< Where we stood when we were sent to fetch a salvage crate.+ Bool m_hasSalvageReturnPosition; ///< True while the walk back to m_salvageReturnPosition is still owed.// Locomotors -------------------------------------------------------------------------------------------------enum LocoGoalType // Note - written out in save/load xfer, don't change these numbers. jba.diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AIUpdate.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AIUpdate.cppindex b0dc29a3f..25911b41b 100644--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AIUpdate.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AIUpdate.cpp@@ -351,6 +351,8 @@ AIUpdateInterface::AIUpdateInterface( Thing *thing, const ModuleData* moduleDatam_moveOutOfWay2 = INVALID_ID;m_exitProductionRallyPoint.zero();m_hasExitProductionRallyPoint = FALSE;+ m_salvageReturnPosition.zero();+ m_hasSalvageReturnPosition = FALSE;m_locomotorSet.clear();m_curLocomotor = NULL;m_curLocomotorSet = LOCOMOTORSET_INVALID;@@ -1188,6 +1190,18 @@ UpdateSleepTime AIUpdateInterface::update( void )stRet = STATE_CONTINUE;}+ // A unit that was sent to pick up a salvage crate walks back to the spot it was standing on, so+ // the line it was part of closes up again instead of drifting to wherever the wrecks fell. The+ // crate is reached, and the trip ends, by dropping into idle, which is the same signal the rally+ // point above waits for.+ if (m_hasSalvageReturnPosition && getAIStateType() == AI_IDLE)+ {+ Coord3D returnPosition = m_salvageReturnPosition;+ m_hasSalvageReturnPosition = FALSE;+ privateMoveToPosition( &returnPosition, CMD_FROM_AI );+ stRet = STATE_CONTINUE;+ }+if (IS_STATE_SLEEP(stRet)){Int frames = GET_STATE_SLEEP_FRAMES(stRet);@@ -4308,6 +4322,11 @@ void AIUpdateInterface::aiDoCommand(const AICommandParms* parms)if (parms->m_cmd != AICMD_FOLLOW_EXITPRODUCTION_PATH)m_hasExitProductionRallyPoint = FALSE;+ // Likewise the walk back from a salvage crate: the player moving the unit somewhere means the+ // spot it left is no longer where it belongs. The order that sends it to the crate is given+ // first and the return position recorded after, so this does not eat its own trip.+ m_hasSalvageReturnPosition = FALSE;+#ifdef ALLOW_SURRENDER// surrendered items have very limited options, and only via AI cmdsif (isSurrendered())@@ -5083,6 +5102,16 @@ void AIUpdateInterface::friend_setExitProductionRallyPoint( const Coord3D *pos )m_hasExitProductionRallyPoint = TRUE;}+//----------------------------------------------------------------------------------------+/**+ * Remember the spot to come back to once the salvage crate we were just sent to is collected.+ */+void AIUpdateInterface::friend_setSalvageReturnPosition( const Coord3D *pos )+{+ m_salvageReturnPosition = *pos;+ m_hasSalvageReturnPosition = TRUE;+}+//----------------------------------------------------------------------------------------/*** Follow the path defined by the given array of points@@ -6922,12 +6951,13 @@ void AIUpdateInterface::crc( Xfer *x )* 6: the out-of-bounds xfer of m_guardTargetType is fixed* 11: m_isMoving, which the duplicated m_isSafePath used to stand in place of* 12: m_allowedToChase- * 13: m_pathfindFoundNothing */+ * 13: m_pathfindFoundNothing+ * 14: the salvage return position and its flag */// ------------------------------------------------------------------------------------------------void AIUpdateInterface::xfer( Xfer *xfer ){// version- const XferVersion currentVersion = 13;+ const XferVersion currentVersion = 14;XferVersion version = currentVersion;xfer->xferVersion( &version, currentVersion );@@ -7222,6 +7252,13 @@ void AIUpdateInterface::xfer( Xfer *xfer )xfer->xferBool(&m_pathfindFoundNothing);}+ if (version >= 14)+ {+ // a save made while the unit is on its way to a salvage crate owes it the walk back+ xfer->xferCoord3D(&m_salvageReturnPosition);+ xfer->xferBool(&m_hasSalvageReturnPosition);+ }+} // end xfer// ------------------------------------------------------------------------------------------------diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogic.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogic.cppindex 33c1aa038..981b569d3 100644--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogic.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogic.cpp@@ -4104,6 +4104,30 @@ static Bool wantsSalvageUpgrade( const Object *obj )return FALSE;}+/** A unit that blows itself up with its own weapon spends its whole life on one target, and a crate+ is not that target. */+static Bool diesUsingItsOwnWeapon( const Object *obj )+{+ for( Int slot = PRIMARY_WEAPON; slot < WEAPONSLOT_COUNT; ++slot )+ {+ const Weapon *weapon = obj->getWeaponInWeaponSlot( (WeaponSlotType)slot );+ if( weapon != NULL && ( weapon->getTemplate()->getAffectsMask() & WEAPON_KILLS_SELF ) != 0 )+ return TRUE;+ }++ return FALSE;+}++/** Is anything hostile close enough to this unit to be its problem right now? */+static Bool hasEnemyInSight( Object *obj )+{+ PartitionFilterAlive alive;+ PartitionFilterRelationship enemies( obj, PartitionFilterRelationship::ALLOW_ENEMIES );+ PartitionFilter *filters[] = { &alive, &enemies, NULL };++ return ThePartitionManager->getClosestObject( obj, obj->getVisionRange(), FROM_CENTER_2D, filters ) != NULL;+}+/* Somebody goes and gets the salvage. A crate dropped by a wreck is money and a free upgrade lyingon the ground, and the game asked you to notice it, work out which of your units was allowed totake it, and drive that one over it by hand, in the middle of the fight that made it. Nobody@@ -4112,18 +4136,26 @@ static Bool wantsSalvageUpgrade( const Object *obj )Whoever is standing nearest and has nothing else to do goes and takes it, once a second. A unitthat can still be upgraded off it wins over one that cannot, however far back it is standing,because the upgrade is worth more than the walk; failing that the nearest idle unit takes the- cash. Only idle units, so this never pulls a unit out of a fight or off an order, and the trip- itself makes the unit busy, which is what stops it being ordered again on the next pass.+ cash. Only idle units, and the trip itself makes the unit busy, which is what stops it being+ ordered again on the next pass.++ Idle is not the same as free, though, which is what the first version of this got wrong. A line+ of infantry dug in across a road is idle, every one of them, and a fight in front of it makes+ wrecks: the line walked off to collect them and never came back, and the position was lost to+ the crates it had just earned. So the collector goes and then walks back to the spot it left,+ nobody with an enemy in sight leaves at all, and the call radius is short enough that the walk+ costs seconds rather than half a minute.A dozer and a harvester are left alone: both have a job of their own that earns more than the- crate does. An aircraft is left alone too, because a crate cannot be claimed from the air. */+ crate does. An aircraft is left alone too, because a crate cannot be claimed from the air, and+ so is anything that dies when it fires - a terrorist is worth more than every crate on the map. */static void salvageCrateTick( void ){const UnsignedInt now = TheGameLogic->getFrame();if( now % LOGICFRAMES_PER_SECOND != 0 )return;- const Real SALVAGE_CALL_RADIUS = 250.0f;+ const Real SALVAGE_CALL_RADIUS = 150.0f;for( Object *crate = TheGameLogic->getFirstObject(); crate != NULL; crate = crate->getNextObject() ){@@ -4148,10 +4180,11 @@ static void salvageCrateTick( void )continue;if( them->isContained() || them->isEffectivelyDead() || them->isNeutralControlled() )continue;-AIUpdateInterface *ai = them->getAI();if( ai == NULL || !ai->isIdle() )continue;+ if( diesUsingItsOwnWeapon( them ) )+ continue;const Player *owner = them->getControllingPlayer();if( owner == NULL || crate->getShroudedStatus( owner->getPlayerIndex() ) >= OBJECTSHROUD_FOGGED )@@ -4169,8 +4202,12 @@ static void salvageCrateTick( void )break;}- if( collector )- collector->getAI()->aiMoveToPosition( crate->getPosition(), CMD_FROM_AI );+ if( collector == NULL || hasEnemyInSight( collector ) )+ continue;++ const Coord3D post = *collector->getPosition();+ collector->getAI()->aiMoveToPosition( crate->getPosition(), CMD_FROM_AI );+ collector->getAI()->friend_setSalvageReturnPosition( &post );}}