Bazaar developer reference

CvDiplomacyAI::DoUpdateWarStates()

Defined in
CvDiplomacyAI.cpp, line 5866
Class
CvDiplomacyAI
Hook name
CivHooks::CvDiplomacyAI_DoUpdateWarStates

Hooks

static bool BeforeCvDiplomacyAI_DoUpdateWarStates(CvDiplomacyAI* self);static void AfterCvDiplomacyAI_DoUpdateWarStates(CvDiplomacyAI* self);static void ReplaceCvDiplomacyAI_DoUpdateWarStates(void (*original)(CvDiplomacyAI*), CvDiplomacyAI* self);CivRegisterBefore<CivHooks::CvDiplomacyAI_DoUpdateWarStates>(host, &BeforeCvDiplomacyAI_DoUpdateWarStates);CivRegisterAfter<CivHooks::CvDiplomacyAI_DoUpdateWarStates>(host, &AfterCvDiplomacyAI_DoUpdateWarStates);CivRegisterReplace<CivHooks::CvDiplomacyAI_DoUpdateWarStates>(host, &ReplaceCvDiplomacyAI_DoUpdateWarStates);

Code

5865/// Updates what the state of war is with all Players5866void CvDiplomacyAI::DoUpdateWarStates()5867{5868	WarStateTypes eWarState;58695870	int iMyLocalMilitaryStrength;5871	int iEnemyInHisLandsMilitaryStrength;58725873	int iMyForeignMilitaryStrength;5874	int iEnemyInMyLandsMilitaryStrength;58755876	int iMyPercentForeign;5877	int iEnemyPercentInHisLands;58785879	int iMyPercentLocal;5880	int iEnemyPercentInMyLands;58815882	int iLocalRatio;5883	int iForeignRatio;58845885	int iWarStateValue;58865887	CvUnit* pLoopUnit;5888	CvCity* pLoopCity;5889	int iUnitOrCityLoop;5890	int iPercentHealthLeft;58915892	// Reset overall war state5893	int iStateAllWars = 0;   // Used to assess overall war state in this function5894	SetStateAllWars(STATE_ALL_WARS_NEUTRAL);58955896	// Loop through all (known) Players5897	PlayerTypes eLoopPlayer;5898	for(int iPlayerLoop = 0; iPlayerLoop < MAX_CIV_PLAYERS; iPlayerLoop++)5899	{5900		eLoopPlayer = (PlayerTypes) iPlayerLoop;59015902		if(IsPlayerValid(eLoopPlayer))5903		{5904			// War?5905			if(IsAtWar(eLoopPlayer))5906			{5907				iMyLocalMilitaryStrength = 0;5908				iEnemyInHisLandsMilitaryStrength = 0;59095910				iMyForeignMilitaryStrength = 0;5911				iEnemyInMyLandsMilitaryStrength = 0;59125913				// Loop through our units5914				for(pLoopUnit = GetPlayer()->firstUnit(&iUnitOrCityLoop); pLoopUnit != NULL; pLoopUnit = GetPlayer()->nextUnit(&iUnitOrCityLoop))5915				{5916					// On our home front5917					if(pLoopUnit->plot()->IsHomeFrontForPlayer(GetPlayer()->GetID()))5918						iMyLocalMilitaryStrength += pLoopUnit->GetPower();59195920					// Enemy's home front5921					if(pLoopUnit->plot()->IsHomeFrontForPlayer(eLoopPlayer))5922						iMyForeignMilitaryStrength += pLoopUnit->GetPower();5923				}59245925				// Loop through our Enemy's units5926				for(pLoopUnit = GET_PLAYER(eLoopPlayer).firstUnit(&iUnitOrCityLoop); pLoopUnit != NULL; pLoopUnit = GET_PLAYER(eLoopPlayer).nextUnit(&iUnitOrCityLoop))5927				{5928					// On our home front5929					if(pLoopUnit->plot()->IsHomeFrontForPlayer(GetPlayer()->GetID()))5930					{5931						iEnemyInMyLandsMilitaryStrength += pLoopUnit->GetPower();5932					}5933					// Enemy's home front5934					if(pLoopUnit->plot()->IsHomeFrontForPlayer(eLoopPlayer))5935					{5936						iEnemyInHisLandsMilitaryStrength += pLoopUnit->GetPower();5937					}5938				}59395940				// Loop through our Cities5941				for(pLoopCity = GetPlayer()->firstCity(&iUnitOrCityLoop); pLoopCity != NULL; pLoopCity = GetPlayer()->nextCity(&iUnitOrCityLoop))5942				{5943					iPercentHealthLeft = (pLoopCity->GetMaxHitPoints() - pLoopCity->getDamage()) * 100 / pLoopCity->GetMaxHitPoints();5944					iMyLocalMilitaryStrength += (pLoopCity->GetPower() * iPercentHealthLeft / 100 / 100);5945				}59465947				// Loop through our Enemy's Cities5948				for(pLoopCity = GET_PLAYER(eLoopPlayer).firstCity(&iUnitOrCityLoop); pLoopCity != NULL; pLoopCity = GET_PLAYER(eLoopPlayer).nextCity(&iUnitOrCityLoop))5949				{5950					iPercentHealthLeft = (pLoopCity->GetMaxHitPoints() - pLoopCity->getDamage()) * 100 / pLoopCity->GetMaxHitPoints();5951					iEnemyInHisLandsMilitaryStrength += (pLoopCity->GetPower() * iPercentHealthLeft / 100 / 100);5952				}59535954				// Percentage of our forces in our locales & each other's locales59555956				// No Units!5957				if(iMyLocalMilitaryStrength + iMyForeignMilitaryStrength == 0)5958				{5959					iMyPercentLocal = 100;5960					iMyPercentForeign = 0;5961				}5962				else5963				{5964					iMyPercentLocal = iMyLocalMilitaryStrength * 100 / (iMyLocalMilitaryStrength + iMyForeignMilitaryStrength);5965					iMyPercentForeign = 100 - iMyPercentLocal;5966				}59675968				// No Units!5969				if(iEnemyInHisLandsMilitaryStrength + iEnemyInMyLandsMilitaryStrength == 0)5970				{5971					iEnemyPercentInHisLands = 100;5972					iEnemyPercentInMyLands = 0;5973				}5974				else5975				{5976					iEnemyPercentInHisLands = iEnemyInHisLandsMilitaryStrength * 100 / (iEnemyInHisLandsMilitaryStrength + iEnemyInMyLandsMilitaryStrength);5977					iEnemyPercentInMyLands = 100 - iEnemyPercentInHisLands;5978				}59795980				// Ratio of Me VS Him in our two locales5981				if(iMyLocalMilitaryStrength == 0)5982				{5983					iMyLocalMilitaryStrength = 1;5984				}5985				iLocalRatio = iMyLocalMilitaryStrength * 100 / (iMyLocalMilitaryStrength + iEnemyInMyLandsMilitaryStrength);59865987				if(iEnemyInHisLandsMilitaryStrength == 0)5988				{5989					iEnemyInHisLandsMilitaryStrength = 1;5990				}5991				iForeignRatio = iMyForeignMilitaryStrength * 100 / (iMyForeignMilitaryStrength + iEnemyInHisLandsMilitaryStrength);59925993				// Calm: Not much happening on either front5994				if(iForeignRatio < /*25*/ GC.getWAR_STATE_CALM_THRESHOLD_FOREIGN_FORCES() && iLocalRatio > 100 - /*25*/ GC.getWAR_STATE_CALM_THRESHOLD_FOREIGN_FORCES())5995				{5996					eWarState = WAR_STATE_CALM;5997				}5998				// SOMETHING is happening5999				else6000				{6001					iWarStateValue = 0;60026003					iWarStateValue += iLocalRatio;		// Will be between 0 and 100.  Anything less than 75 is bad news though!  We want a very high percentage in our own lands.6004					iWarStateValue += iForeignRatio;	// Will be between 0 and 100.  Will vary wildly though, depending on the status of an offensive.  A number of 50 is very good.60056006					iWarStateValue /= 2;60076008					// Some Example WarStateValues:6009					// Local		Foreign	WarStateValue6010					// 100%		70%			856011					// 100%		40%			706012					// 80% 		30% :		556013					// 100% 		0% :			506014					// 60% 		40% :		506015					// 60% 		10% :		356016					// 40% 		0% :			2060176018					if(iWarStateValue >= /*75*/ GC.getWAR_STATE_THRESHOLD_NEARLY_WON())6019					{6020						eWarState = WAR_STATE_NEARLY_WON;6021					}6022					else if(iWarStateValue >= /*57*/ GC.getWAR_STATE_THRESHOLD_OFFENSIVE())6023					{6024						eWarState = WAR_STATE_OFFENSIVE;6025					}6026					else if(iWarStateValue >= /*42*/ GC.getWAR_STATE_THRESHOLD_STALEMATE())6027					{6028						eWarState = WAR_STATE_STALEMATE;6029					}6030					else if(iWarStateValue >= /*25*/ GC.getWAR_STATE_THRESHOLD_DEFENSIVE())6031					{6032						eWarState = WAR_STATE_DEFENSIVE;6033					}6034					else6035					{6036						eWarState = WAR_STATE_NEARLY_DEFEATED;6037					}6038				}60396040//////////////////////////////////////////////////////////6041//				WAR STATE MODIFICATIONS - We crunched the numbers above, but are there any special cases to consider?6042//////////////////////////////////////////////////////////60436044				// If the war is calm but they're an easy target consider us on Offense6045				if(eWarState == WAR_STATE_CALM)6046				{6047					if(GetPlayerTargetValue(eLoopPlayer) >= TARGET_VALUE_FAVORABLE)6048					{6049						eWarState = WAR_STATE_OFFENSIVE;6050					}6051				}60526053				// If the other guy happens to have a guy or two near us but we vastly outnumber him overall, we're not really on the defensive6054				if(eWarState <= WAR_STATE_DEFENSIVE)6055				{6056					if(iMyLocalMilitaryStrength >= iEnemyInMyLandsMilitaryStrength + iEnemyInMyLandsMilitaryStrength)6057					{6058						eWarState = WAR_STATE_STALEMATE;6059					}6060				}60616062				// If this is a major power, determine what the impact of this war is on our global situation6063				if (!GET_PLAYER(eLoopPlayer).isMinorCiv())6064				{6065					if(eWarState == WAR_STATE_NEARLY_WON)6066					{6067						iStateAllWars += 2;6068					}6069					else if(eWarState == WAR_STATE_OFFENSIVE)6070					{6071						iStateAllWars += 1;6072					}6073					else if(eWarState == WAR_STATE_DEFENSIVE)6074					{6075						iStateAllWars -= 1;60766077						// If we are defensive in this war and our capital has been damaged, overall state should be defensive6078						CvCity *pCapital = m_pPlayer->getCapitalCity();6079						if (pCapital)6080						{6081							if (pCapital->getDamage() > 0)6082							{6083								SetStateAllWars(STATE_ALL_WARS_LOSING);6084							}6085						}6086					}6087					else if(eWarState == WAR_STATE_NEARLY_DEFEATED)6088					{6089						// If nearly defeated in any war, overall state should be defensive6090						SetStateAllWars(STATE_ALL_WARS_LOSING);6091					}6092				}6093			}6094			// Not at war6095			else6096				eWarState = NO_WAR_STATE_TYPE;60976098			SetWarState(eLoopPlayer, eWarState);6099		}6100	}61016102	// Finalize overall assessment6103	if(iStateAllWars < 0 || GetStateAllWars() == STATE_ALL_WARS_LOSING)6104		SetStateAllWars(STATE_ALL_WARS_LOSING);6105	else if(iStateAllWars > 0)6106		SetStateAllWars(STATE_ALL_WARS_WINNING);6107}

Calls

Called by