Bazaar developer reference

CvGrandStrategyAI::DoTurn()

Defined in
CvGrandStrategyAI.cpp, line 251
Class
CvGrandStrategyAI
Hook name
CivHooks::CvGrandStrategyAI_DoTurn

Hooks

static bool BeforeCvGrandStrategyAI_DoTurn(CvGrandStrategyAI* self);static void AfterCvGrandStrategyAI_DoTurn(CvGrandStrategyAI* self);static void ReplaceCvGrandStrategyAI_DoTurn(void (*original)(CvGrandStrategyAI*), CvGrandStrategyAI* self);CivRegisterBefore<CivHooks::CvGrandStrategyAI_DoTurn>(host, &BeforeCvGrandStrategyAI_DoTurn);CivRegisterAfter<CivHooks::CvGrandStrategyAI_DoTurn>(host, &AfterCvGrandStrategyAI_DoTurn);CivRegisterReplace<CivHooks::CvGrandStrategyAI_DoTurn>(host, &ReplaceCvGrandStrategyAI_DoTurn);

Code

250/// Runs every turn to determine what the player's Active Grand Strategy is and to change Priority Levels as necessary251void CvGrandStrategyAI::DoTurn()252{253	DoGuessOtherPlayersActiveGrandStrategy();254255	int iGrandStrategiesLoop;256	AIGrandStrategyTypes eGrandStrategy;257	CvAIGrandStrategyXMLEntry* pGrandStrategy;258	CvString strGrandStrategyName;259260	// Loop through all GrandStrategies to set their Priorities261	for(iGrandStrategiesLoop = 0; iGrandStrategiesLoop < GetAIGrandStrategies()->GetNumAIGrandStrategies(); iGrandStrategiesLoop++)262	{263		eGrandStrategy = (AIGrandStrategyTypes) iGrandStrategiesLoop;264		pGrandStrategy = GetAIGrandStrategies()->GetEntry(iGrandStrategiesLoop);265		strGrandStrategyName = (CvString) pGrandStrategy->GetType();266267		// Base Priority looks at Personality Flavors (0 - 10) and multiplies * the Flavors attached to a Grand Strategy (0-10),268		// so expect a number between 0 and 100 back from this269		int iPriority = GetBaseGrandStrategyPriority(eGrandStrategy);270271		if(strGrandStrategyName == "AIGRANDSTRATEGY_CONQUEST")272		{273			iPriority += GetConquestPriority();274		}275		else if(strGrandStrategyName == "AIGRANDSTRATEGY_CULTURE")276		{277			iPriority += GetCulturePriority();278		}279		else if(strGrandStrategyName == "AIGRANDSTRATEGY_UNITED_NATIONS")280		{281			iPriority += GetUnitedNationsPriority();282		}283		else if(strGrandStrategyName == "AIGRANDSTRATEGY_SPACESHIP")284		{285			iPriority += GetSpaceshipPriority();286		}287288		// Random element289		iPriority += GC.getGame().getJonRandNum(/*50*/ GC.getAI_GS_RAND_ROLL(), "Grand Strategy AI: GS rand roll.");290291		// Give a boost to the current strategy so that small fluctuation doesn't cause a big change292		if(GetActiveGrandStrategy() == eGrandStrategy && GetActiveGrandStrategy() != NO_AIGRANDSTRATEGY)293		{294			iPriority += /*50*/ GC.getAI_GRAND_STRATEGY_CURRENT_STRATEGY_WEIGHT();295		}296297		SetGrandStrategyPriority(eGrandStrategy, iPriority);298	}299300	// Now look at what we think the other players in the game are up to - we might have an opportunity to capitalize somewhere301	int iNumPlayersAliveAndMet = 0;302303	int iMajorLoop;304305	for(iMajorLoop = 0; iMajorLoop < MAX_MAJOR_CIVS; iMajorLoop++)306	{307		if(GET_PLAYER((PlayerTypes) iMajorLoop).isAlive())308		{309			if(GET_TEAM(GetPlayer()->getTeam()).isHasMet(GET_PLAYER((PlayerTypes) iMajorLoop).getTeam()))310			{311				iNumPlayersAliveAndMet++;312			}313		}314	}315316	FStaticVector< int, 5, true, c_eCiv5GameplayDLL > viNumGrandStrategiesAdopted;317	int iNumPlayers;318319	// Init vector320	for(iGrandStrategiesLoop = 0; iGrandStrategiesLoop < GetAIGrandStrategies()->GetNumAIGrandStrategies(); iGrandStrategiesLoop++)321	{322		iNumPlayers = 0;323324		// Tally up how many players we think are pusuing each Grand Strategy325		for(iMajorLoop = 0; iMajorLoop < MAX_MAJOR_CIVS; iMajorLoop++)326		{327			if(GetGuessOtherPlayerActiveGrandStrategy((PlayerTypes) iMajorLoop) == (AIGrandStrategyTypes) iGrandStrategiesLoop)328			{329				iNumPlayers++;330			}331		}332333		viNumGrandStrategiesAdopted.push_back(iNumPlayers);334	}335336	FStaticVector< int, 5, true, c_eCiv5GameplayDLL > viGrandStrategyChangeForLogging;337338	int iChange;339340	// Now modify our preferences based on how many people are going for stuff341	for(iGrandStrategiesLoop = 0; iGrandStrategiesLoop < GetAIGrandStrategies()->GetNumAIGrandStrategies(); iGrandStrategiesLoop++)342	{343		eGrandStrategy = (AIGrandStrategyTypes) iGrandStrategiesLoop;344		// If EVERYONE else we know is also going for this Grand Strategy, reduce our Priority by 50%345		iChange = GetGrandStrategyPriority(eGrandStrategy) * /*50*/ GC.getAI_GRAND_STRATEGY_OTHER_PLAYERS_GS_MULTIPLIER();346		iChange = iChange * viNumGrandStrategiesAdopted[eGrandStrategy] / iNumPlayersAliveAndMet;347		iChange /= 100;348349		ChangeGrandStrategyPriority(eGrandStrategy, -iChange);350351		viGrandStrategyChangeForLogging.push_back(-iChange);352	}353354	ChangeNumTurnsSinceActiveSet(1);355356	// Now see which Grand Strategy should be active, based on who has the highest Priority right now357	// Grand Strategy must be run for at least 10 turns358	if(GetActiveGrandStrategy() == NO_AIGRANDSTRATEGY || GetNumTurnsSinceActiveSet() >= /*10*/ GC.getAI_GRAND_STRATEGY_NUM_TURNS_STRATEGY_MUST_BE_ACTIVE())359	{360		int iBestPriority = -1;361		int iPriority;362363		AIGrandStrategyTypes eBestGrandStrategy = NO_AIGRANDSTRATEGY;364365		for(iGrandStrategiesLoop = 0; iGrandStrategiesLoop < GetAIGrandStrategies()->GetNumAIGrandStrategies(); iGrandStrategiesLoop++)366		{367			eGrandStrategy = (AIGrandStrategyTypes) iGrandStrategiesLoop;368369			iPriority = GetGrandStrategyPriority(eGrandStrategy);370371			if(iPriority > iBestPriority)372			{373				iBestPriority = iPriority;374				eBestGrandStrategy = eGrandStrategy;375			}376		}377378		if(eBestGrandStrategy != GetActiveGrandStrategy())379		{380			SetActiveGrandStrategy(eBestGrandStrategy);381			m_pPlayer->GetCitySpecializationAI()->SetSpecializationsDirty(SPECIALIZATION_UPDATE_NEW_GRAND_STRATEGY);382		}383	}384385	LogGrandStrategies(viGrandStrategyChangeForLogging);386}

Calls

Called by