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): validate build and rally orders, and unstick the docks

Olcay Seygan7a3c7854f913
diff --git a/GeneralsMD/Code/GameEngine/Source/Common/RTS/Player.cpp b/GeneralsMD/Code/GameEngine/Source/Common/RTS/Player.cpp
index e2ff27fa3..cf75bd433 100644
--- a/GeneralsMD/Code/GameEngine/Source/Common/RTS/Player.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/Common/RTS/Player.cpp
@@ -3141,6 +3141,10 @@ void Player::removeUpgrade( const UpgradeTemplate *upgradeTemplate )
TheControlBar->markUIDirty();
}
+ // it is off the list and nothing else holds it: free it. This was missing, so every
+ // cancelled upgrade leaked one pool object for the rest of the game.
+ upgrade->deleteInstance();
+
} // end if
} // end removeUpgrade
diff --git a/GeneralsMD/Code/GameEngine/Source/Common/RTS/ScoreKeeper.cpp b/GeneralsMD/Code/GameEngine/Source/Common/RTS/ScoreKeeper.cpp
index 7fcb1079f..6e4e419d6 100644
--- a/GeneralsMD/Code/GameEngine/Source/Common/RTS/ScoreKeeper.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/Common/RTS/ScoreKeeper.cpp
@@ -209,11 +209,19 @@ void ScoreKeeper::removeObjectBuilt( const Object *o)
if (removeFromCount)
{
- Int existingCount = 0;
+ //
+ // Floor everything at zero. removeObjectBuilt is also reached for objects this player
+ // never built (a structure captured and then lost), which used to insert a -1 entry into
+ // the map and push the totals negative in the score screen.
+ //
+ if (m_totalBuildingsBuilt < 0)
+ m_totalBuildingsBuilt = 0;
+ if (m_totalUnitsBuilt < 0)
+ m_totalUnitsBuilt = 0;
+
ObjectCountMapIt it = m_objectsBuilt.find(o->getTemplate());
- if (it != m_objectsBuilt.end())
- existingCount = it->second;
- m_objectsBuilt[o->getTemplate()] = existingCount - 1;
+ if (it != m_objectsBuilt.end() && it->second > 0)
+ it->second = it->second - 1;
}
}
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AutoDepositUpdate.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AutoDepositUpdate.cpp
index 95236b6b3..d6896ce2f 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AutoDepositUpdate.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AutoDepositUpdate.cpp
@@ -169,7 +169,9 @@ UpdateSleepTime AutoDepositUpdate::update( void )
if( modData->m_isActualMoney )
{
getObject()->getControllingPlayer()->getMoney()->deposit( moneyAmount );
- getObject()->getControllingPlayer()->getScoreKeeper()->addMoneyEarned( modData->m_depositAmount);
+ // score what we actually deposited: this used to score m_depositAmount only, so the
+ // upgraded supply boost never reached the end-game income total.
+ getObject()->getControllingPlayer()->getScoreKeeper()->addMoneyEarned( moneyAmount );
}
Bool displayMoney = moneyAmount > 0 ? TRUE : FALSE;
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/DockUpdate/DockUpdate.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/DockUpdate/DockUpdate.cpp
index 432d756cd..cd18cc8e8 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/DockUpdate/DockUpdate.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/DockUpdate/DockUpdate.cpp
@@ -416,14 +416,32 @@ void DockUpdate::setDockCrippled( Bool setting )
UpdateSleepTime DockUpdate::update()
{
+ //
+ // Reap dockers that died on us. cancelDock()/onExit() are only ever called by the docker's own
+ // AI states, so a docker killed while it held the dock (or an approach slot) left its ID here
+ // forever and this dock never granted entry to anyone again - the classic "supply center stops
+ // accepting trucks" stall.
+ //
+ if( m_activeDocker != INVALID_ID && TheGameLogic->findObjectByID( m_activeDocker ) == NULL )
+ m_activeDocker = INVALID_ID;
+
if( m_activeDocker == INVALID_ID && !m_dockCrippled )
{
// if setDockCrippled has been called, I will never give enterance permission.
for( Int positionIndex = 0; positionIndex < m_approachPositionReached.size(); ++positionIndex )
{
+ ObjectID ownerID = m_approachPositionOwners[positionIndex];
+ if( ownerID != INVALID_ID && TheGameLogic->findObjectByID( ownerID ) == NULL )
+ {
+ // the unit queued in this slot is gone; free it instead of blocking the queue
+ m_approachPositionOwners[positionIndex] = INVALID_ID;
+ m_approachPositionReached[positionIndex] = FALSE;
+ continue;
+ }
+
if( m_approachPositionReached[positionIndex] )
{
- m_activeDocker = m_approachPositionOwners[positionIndex];
+ m_activeDocker = ownerID;
return UPDATE_SLEEP_NONE;
}
}
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/DockUpdate/RepairDockUpdate.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/DockUpdate/RepairDockUpdate.cpp
index db1834d43..8e9892eb3 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/DockUpdate/RepairDockUpdate.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/DockUpdate/RepairDockUpdate.cpp
@@ -108,7 +108,13 @@ Bool RepairDockUpdate::action( Object *docker, Object *drone )
// to this docked object each frame so that it is fully healed after the correct amount
// of time has passed
//
- if( m_lastRepair == 0 )
+ //
+ // keyed on the docker, not on "is the slot empty": m_lastRepair is only reset when a docker
+ // reaches full health, so one that died or was ordered away mid-repair left its rate behind
+ // and the NEXT docker healed at the previous unit's rate - an almost-dead heavy tank followed
+ // by a lightly damaged one repaired it nearly instantly.
+ //
+ if( m_lastRepair != docker->getID() )
{
// save ID of this docker as the last docker
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/DockUpdate/SupplyWarehouseDockUpdate.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/DockUpdate/SupplyWarehouseDockUpdate.cpp
index a4401324c..40989f29d 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/DockUpdate/SupplyWarehouseDockUpdate.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/DockUpdate/SupplyWarehouseDockUpdate.cpp
@@ -112,7 +112,10 @@ Bool SupplyWarehouseDockUpdate::action( Object* docker, Object *drone )
--m_boxesStored;// so the docker sees that I am shy by one box (or empty) from within his gainOneBox()
- SupplyTruckAIInterface *ai = docker->getAIUpdateInterface()->getSupplyTruckAIInterface();
+ // getAIUpdateInterface() can be NULL here and was dereferenced blind (the sibling
+ // SupplyCenterDockUpdate does check). The ai==NULL path below already puts the box back.
+ AIUpdateInterface *dockerAI = docker->getAIUpdateInterface();
+ SupplyTruckAIInterface *ai = dockerAI ? dockerAI->getSupplyTruckAIInterface() : NULL;
if( ai && ai->gainOneBox( m_boxesStored ) )
{
if( m_boxesStored == 0 && getSupplyWarehouseDockUpdateModuleData()->m_deleteWhenEmpty )
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/ProductionUpdate.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/ProductionUpdate.cpp
index 49b43c16f..f8d3c342c 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/ProductionUpdate.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/ProductionUpdate.cpp
@@ -340,9 +340,15 @@ void ProductionUpdate::cancelUpgrade( const UpgradeTemplate *upgrade )
// get the player
Player *player = getObject()->getControllingPlayer();
- // sanity, you can't cancel it if the player isn't actually building one
+ //
+ // You can't refund it if the player isn't actually building one - but the queue entry still
+ // has to go. Returning here without removing it is what made cancelAndRefundAllProduction
+ // spin: the same head entry was retried 100 times and then abandoned, so selling or losing a
+ // research building neither refunded the upgrade nor cleared it from the queue.
+ //
+ Bool refundIt = TRUE;
if( upgrade->getUpgradeType() == UPGRADE_TYPE_PLAYER && player->hasUpgradeInProduction( upgrade ) == FALSE )
- return;
+ refundIt = FALSE;
//
// find the production entry for this upgrade in the queue here, there can only be one
@@ -363,8 +369,11 @@ void ProductionUpdate::cancelUpgrade( const UpgradeTemplate *upgrade )
return;
// refund money back to the player
- Money *money = player->getMoney();
- money->deposit( production->m_upgradeToResearch->calcCostToBuild( player ) );
+ if( refundIt )
+ {
+ Money *money = player->getMoney();
+ money->deposit( production->m_upgradeToResearch->calcCostToBuild( player ) );
+ }
// remove this production from the queue
removeFromProductionQueue( production );
@@ -473,8 +482,11 @@ void ProductionUpdate::cancelUnitCreate( ProductionID productionID )
for( production = m_productionQueue; production; production = production->m_next )
{
- // are we at the one we want get rid of it
- if( production->m_productionID == productionID )
+ // are we at the one we want get rid of it.
+ // The type test matters: m_objectToProduce and m_upgradeToResearch share a union, and an
+ // upgrade entry carries PRODUCTIONID_INVALID - so a cancel arriving with that id used to
+ // match an upgrade here and hand an UpgradeTemplate* to ThingTemplate::calcCostToBuild.
+ if( production->m_type == PRODUCTION_UNIT && production->m_productionID == productionID )
{
// give the player the cost of the object back
@@ -1143,25 +1155,33 @@ void ProductionUpdate::onDie( const DamageInfo *damageInfo )
// ------------------------------------------------------------------------------------------------
void ProductionUpdate::cancelAndRefundAllProduction( void )
{
- // Empirically, in release the code can loop forever. So we limit to 100 passes. jba. [8/31/2003]
- const Int productionLimit = 100;// With luck, we never queue up 100 units. [8/31/2003]
- Int i;
- for (i=0; i<productionLimit; i++)
+ //
+ // The old loop ran a fixed 100 passes whether or not the queue was empty, because
+ // cancelUpgrade could return without removing its entry (fixed above) and the head would
+ // then never change. Now: stop as soon as the queue is empty, and if an entry somehow
+ // survives its own cancel, stop rather than spin.
+ //
+ const Int productionLimit = 100;// a sanity backstop, not the termination condition
+ for( Int i = 0; i < productionLimit && m_productionQueue; i++ )
{
- // iterate through our production queue
- if( m_productionQueue )
+ ProductionEntry *head = m_productionQueue;
+
+ if( head->getProductionType() == PRODUCTION_UNIT )
+ cancelUnitCreate( head->getProductionID() );
+ else if( head->getProductionType() == PRODUCTION_UPGRADE )
+ cancelUpgrade( head->getProductionUpgrade() );
+ else
{
- if( m_productionQueue->getProductionType() == PRODUCTION_UNIT )
- cancelUnitCreate( m_productionQueue->getProductionID() );
- else if( m_productionQueue->getProductionType() == PRODUCTION_UPGRADE )
- cancelUpgrade( m_productionQueue->getProductionUpgrade() );
- else
- {
- // unknown production type
- DEBUG_CRASH(( "ProductionUpdate::cancelAndRefundAllProduction - Unknown production type '%d'\n", m_productionQueue->getProductionType() ));
- return;
- } // end else
- } // end if
+ // unknown production type
+ DEBUG_CRASH(( "ProductionUpdate::cancelAndRefundAllProduction - Unknown production type '%d'\n", head->getProductionType() ));
+ return;
+ } // end else
+
+ if( m_productionQueue == head )
+ {
+ DEBUG_CRASH(( "ProductionUpdate::cancelAndRefundAllProduction - head entry would not cancel\n" ));
+ return;
+ }
}
} // end cancelAndRefundAllProduction
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogicDispatch.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogicDispatch.cpp
index cc34a1373..02b70873d 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogicDispatch.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogicDispatch.cpp
@@ -555,7 +555,10 @@ void GameLogic::logicMessageDispatcher( GameMessage *msg, void *userData )
{
Object *obj = TheGameLogic->findObjectByID( msg->getArgument( 0 )->objectID );
Coord3D dest = msg->getArgument( 1 )->location;
- if (obj)
+ // sanity, the player must control the object. The object id comes straight off the
+ // message, and every sibling case here (MSG_CANCEL_UNIT_CREATE, MSG_DOZER_CANCEL_CONSTRUCT)
+ // checks it - without it a doctored client could set rally points on enemy factories.
+ if (obj && obj->getControllingPlayer() == thisPlayer)
{
doSetRallyPoint( obj, dest );
}
@@ -1496,6 +1499,18 @@ void GameLogic::logicMessageDispatcher( GameMessage *msg, void *userData )
if( place == NULL || constructorObject == NULL )
break; //These are not crashes, as the object may have died before this message came in
+ // sanity, the player must control the builder
+ if( constructorObject->getControllingPlayer() != thisPlayer )
+ break;
+
+ //
+ // Re-check prerequisites and money on the logic side. This used to go straight to
+ // buildObjectNow, whose Money::withdraw clamps to the balance - so a client that
+ // skipped the UI's own check could put up structures it had not paid for.
+ //
+ if( TheBuildAssistant->canMakeUnit( constructorObject, place ) != CANMAKE_OK )
+ break;
+
if( msg->getType() == GameMessage::MSG_DOZER_CONSTRUCT )
{