Bazaar developer reference

CvMilitaryAI::UpdateMilitaryStrategies()

Defined in
CvMilitaryAI.cpp, line 2472
Class
CvMilitaryAI
Hook name
CivHooks::CvMilitaryAI_UpdateMilitaryStrategies
Lua events
GameEvents.MilitaryStrategyCanActivate

Hooks

static bool BeforeCvMilitaryAI_UpdateMilitaryStrategies(CvMilitaryAI* self);static void AfterCvMilitaryAI_UpdateMilitaryStrategies(CvMilitaryAI* self);static void ReplaceCvMilitaryAI_UpdateMilitaryStrategies(void (*original)(CvMilitaryAI*), CvMilitaryAI* self);CivRegisterBefore<CivHooks::CvMilitaryAI_UpdateMilitaryStrategies>(host, &BeforeCvMilitaryAI_UpdateMilitaryStrategies);CivRegisterAfter<CivHooks::CvMilitaryAI_UpdateMilitaryStrategies>(host, &AfterCvMilitaryAI_UpdateMilitaryStrategies);CivRegisterReplace<CivHooks::CvMilitaryAI_UpdateMilitaryStrategies>(host, &ReplaceCvMilitaryAI_UpdateMilitaryStrategies);

Code

2471/// Start or stop military strategies to get flavors set properly2472void CvMilitaryAI::UpdateMilitaryStrategies()2473{2474	AI_PERF_FORMAT("Military-AI-perf.csv", ("UpdateMilitaryStrategies, Turn %03d, %s", GC.getGame().getElapsedGameTurns(), m_pPlayer->getCivilizationShortDescription()) );24752476	int iStrategiesLoop = 0;24772478	// Loop through all strategies2479	for(iStrategiesLoop = 0; iStrategiesLoop < GetMilitaryAIStrategies()->GetNumMilitaryAIStrategies(); iStrategiesLoop++)2480	{2481		MilitaryAIStrategyTypes eStrategy = (MilitaryAIStrategyTypes) iStrategiesLoop;2482		CvMilitaryAIStrategyXMLEntry* pStrategy = GetMilitaryAIStrategies()->GetEntry(iStrategiesLoop);24832484		if(pStrategy == NULL)	// Can have holes in the list2485			continue;24862487		// Minor Civs can't run some Strategies2488		if(GetPlayer()->isMinorCiv() && pStrategy->IsNoMinorCivs())2489			continue;24902491		// Some strategies ONLY for Minor Civs2492		if(!GetPlayer()->isMinorCiv() && pStrategy->IsOnlyMinorCivs())2493			continue;24942495		bool bTestStrategyStart = true;24962497		// Do we already have this Strategy adopted?2498		if(IsUsingStrategy(eStrategy))2499			bTestStrategyStart = false;25002501		else2502		{2503			// Has the prereq Tech necessary?2504			if(pStrategy->GetTechPrereq() != NO_TECH && !GET_TEAM(GetPlayer()->getTeam()).GetTeamTechs()->HasTech((TechTypes) pStrategy->GetTechPrereq()))2505			{2506				bTestStrategyStart = false;2507			}25082509			// Has the Tech which obsoletes this Strategy?2510			if(bTestStrategyStart && pStrategy->GetTechObsolete() != NO_TECH && GET_TEAM(GetPlayer()->getTeam()).GetTeamTechs()->HasTech((TechTypes) pStrategy->GetTechObsolete()))2511			{2512				bTestStrategyStart = false;2513			}25142515			// Not time to check this yet?2516			if(GC.getGame().getGameTurn() < pStrategy->GetFirstTurnExecuted())2517			{2518				bTestStrategyStart = false;2519			}2520		}25212522		bool bTestStrategyEnd = false;25232524		// Strategy is active, check to see if we should even try to disable it2525		if(IsUsingStrategy(eStrategy))2526		{2527			if(pStrategy->GetCheckTriggerTurnCount() > 0)2528			{2529				// Is it a turn where we want to check to see if this Strategy is maintained?2530				if((GC.getGame().getGameTurn() - GetTurnStrategyAdopted(eStrategy)) % pStrategy->GetCheckTriggerTurnCount() == 0)2531					bTestStrategyEnd = true;2532			}25332534			if(bTestStrategyEnd && pStrategy->GetMinimumNumTurnsExecuted() > 0)2535			{2536				// Has the minimum # of turns passed for this Strategy?2537				if(GC.getGame().getGameTurn() < GetTurnStrategyAdopted(eStrategy) + pStrategy->GetMinimumNumTurnsExecuted())2538					bTestStrategyEnd = false;2539			}2540		}25412542		// Check Strategy Triggers2543		// Functionality and existence of specific Strategies is hardcoded here, but data is stored in XML so it's easier to modify25442545		if(bTestStrategyStart || bTestStrategyEnd)2546		{2547			bool bStrategyShouldBeActive = false;25482549			// Has the Tech which obsoletes this Strategy? If so, Strategy should be deactivated regardless of other factors2550			if(pStrategy->GetTechObsolete() != NO_TECH && GET_TEAM(GetPlayer()->getTeam()).GetTeamTechs()->HasTech((TechTypes) pStrategy->GetTechObsolete()))2551			{2552				bStrategyShouldBeActive = false;2553			}25542555			// Strategy isn't obsolete, so test triggers as normal2556			else2557			{2558				CvString strStrategyName = (CvString) pStrategy->GetType();25592560				// Check all of the Strategy Triggers2561				if(strStrategyName == "MILITARYAISTRATEGY_ENOUGH_MILITARY_UNITS")2562					bStrategyShouldBeActive = MilitaryAIHelpers::IsTestStrategy_EnoughMilitaryUnits(m_pPlayer);2563				else if(strStrategyName == "MILITARYAISTRATEGY_EMPIRE_DEFENSE")2564					bStrategyShouldBeActive = MilitaryAIHelpers::IsTestStrategy_EmpireDefense(m_pPlayer);2565				else if(strStrategyName == "MILITARYAISTRATEGY_EMPIRE_DEFENSE_CRITICAL")2566					bStrategyShouldBeActive = MilitaryAIHelpers::IsTestStrategy_EmpireDefenseCritical(m_pPlayer);2567				else if(strStrategyName == "MILITARYAISTRATEGY_ENOUGH_NAVAL_UNITS")2568					bStrategyShouldBeActive = MilitaryAIHelpers::IsTestStrategy_EnoughNavalUnits(m_pPlayer);2569				else if(strStrategyName == "MILITARYAISTRATEGY_NEED_NAVAL_UNITS")2570					bStrategyShouldBeActive = MilitaryAIHelpers::IsTestStrategy_NeedNavalUnits(m_pPlayer);2571				else if(strStrategyName == "MILITARYAISTRATEGY_NEED_NAVAL_UNITS_CRITICAL")2572					bStrategyShouldBeActive = MilitaryAIHelpers::IsTestStrategy_NeedNavalUnitsCritical(m_pPlayer);2573				else if(strStrategyName == "MILITARYAISTRATEGY_WAR_MOBILIZATION")2574					bStrategyShouldBeActive = MilitaryAIHelpers::IsTestStrategy_WarMobilization(eStrategy, m_pPlayer);2575				else if(strStrategyName == "MILITARYAISTRATEGY_AT_WAR")2576					bStrategyShouldBeActive = MilitaryAIHelpers::IsTestStrategy_AtWar(m_pPlayer);2577				else if(strStrategyName == "MILITARYAISTRATEGY_MINOR_CIV_GENERAL_DEFENSE")2578					bStrategyShouldBeActive = MilitaryAIHelpers::IsTestStrategy_MinorCivGeneralDefense();2579				else if(strStrategyName == "MILITARYAISTRATEGY_MINOR_CIV_THREAT_ELEVATED")2580					bStrategyShouldBeActive = MilitaryAIHelpers::IsTestStrategy_MinorCivThreatElevated(m_pPlayer);2581				else if(strStrategyName == "MILITARYAISTRATEGY_MINOR_CIV_THREAT_CRITICAL")2582					bStrategyShouldBeActive = MilitaryAIHelpers::IsTestStrategy_MinorCivThreatCritical(m_pPlayer);2583				else if(strStrategyName == "MILITARYAISTRATEGY_ERADICATE_BARBARIANS")2584					bStrategyShouldBeActive = MilitaryAIHelpers::IsTestStrategy_EradicateBarbarians(eStrategy, m_pPlayer, m_iBarbarianCampCount, m_iVisibleBarbarianCount);2585				else if(strStrategyName == "MILITARYAISTRATEGY_WINNING_WARS")2586					bStrategyShouldBeActive = MilitaryAIHelpers::IsTestStrategy_WinningWars(m_pPlayer);2587				else if(strStrategyName == "MILITARYAISTRATEGY_LOSING_WARS")2588					bStrategyShouldBeActive = MilitaryAIHelpers::IsTestStrategy_LosingWars(m_pPlayer);2589				else if(strStrategyName == "MILITARYAISTRATEGY_NEED_RANGED")2590					bStrategyShouldBeActive = MilitaryAIHelpers::IsTestStrategy_NeedRangedUnits(m_pPlayer, m_iNumRangedLandUnits, m_iNumMeleeLandUnits);2591				else if(strStrategyName == "MILITARYAISTRATEGY_ENOUGH_RANGED")2592					bStrategyShouldBeActive = MilitaryAIHelpers::IsTestStrategy_EnoughRangedUnits(m_pPlayer, m_iNumRangedLandUnits, m_iNumMeleeLandUnits);2593				else if(strStrategyName == "MILITARYAISTRATEGY_NEED_RANGED_EARLY")2594					bStrategyShouldBeActive = MilitaryAIHelpers::IsTestStrategy_NeedRangedDueToEarlySneakAttack(m_pPlayer);2595				else if(strStrategyName == "MILITARYAISTRATEGY_NEED_MOBILE")2596					bStrategyShouldBeActive = MilitaryAIHelpers::IsTestStrategy_NeedMobileUnits(m_pPlayer, m_iNumMobileLandUnits, m_iNumMeleeLandUnits);2597				else if(strStrategyName == "MILITARYAISTRATEGY_ENOUGH_MOBILE")2598					bStrategyShouldBeActive = MilitaryAIHelpers::IsTestStrategy_EnoughMobileUnits(m_pPlayer, m_iNumMobileLandUnits, m_iNumMeleeLandUnits);2599				else if(strStrategyName == "MILITARYAISTRATEGY_NEED_AIR")2600					bStrategyShouldBeActive = MilitaryAIHelpers::IsTestStrategy_NeedAirUnits(m_pPlayer, m_iNumAirUnits, m_iNumMeleeLandUnits);2601				else if(strStrategyName == "MILITARYAISTRATEGY_ENOUGH_AIR")2602					bStrategyShouldBeActive = MilitaryAIHelpers::IsTestStrategy_EnoughAirUnits(m_pPlayer, m_iNumAirUnits, m_iNumMeleeLandUnits);2603				else if(strStrategyName == "MILITARYAISTRATEGY_NEED_NUKE")2604					bStrategyShouldBeActive = MilitaryAIHelpers::IsTestStrategy_NeedANuke(m_pPlayer);2605				else if(strStrategyName == "MILITARYAISTRATEGY_NEED_ANTIAIR")2606					bStrategyShouldBeActive = MilitaryAIHelpers::IsTestStrategy_NeedAntiAirUnits(m_pPlayer, m_iNumAntiAirUnits, m_iNumMeleeLandUnits);2607				else if(strStrategyName == "MILITARYAISTRATEGY_ENOUGH_ANTIAIR")2608					bStrategyShouldBeActive = MilitaryAIHelpers::IsTestStrategy_EnoughAntiAirUnits(m_pPlayer, m_iNumAntiAirUnits, m_iNumMeleeLandUnits);2609				else if(strStrategyName == "MILITARYAISTRATEGY_NEED_AIR_CARRIER")2610					bStrategyShouldBeActive = MilitaryAIHelpers::IsTestStrategy_NeedAirCarriers(m_pPlayer);26112612				// Never found it?  Assume it is scenario-specific and should be on unless disabled by Lua hook2613				else2614				{2615					bStrategyShouldBeActive = true;2616				}26172618				// Check Lua hook2619				ICvEngineScriptSystem1* pkScriptSystem = gDLL->GetScriptSystem();2620				if(pkScriptSystem && bStrategyShouldBeActive)2621				{2622					CvLuaArgsHandle args;2623					args->Push(iStrategiesLoop);2624					args->Push(m_pPlayer->GetID());26252626					// Attempt to execute the game events.2627					// Will return false if there are no registered listeners.2628					bool bResult = false;2629					if(LuaSupport::CallTestAll(pkScriptSystem, "MilitaryStrategyCanActivate", args.get(), bResult))2630					{2631						// Check the result.2632						if(bResult == false)2633						{2634							bStrategyShouldBeActive = false;2635						}2636					}2637				}2638			}26392640			// This variable keeps track of whether or not we should be doing something (i.e. Strategy is active now but should be turned off, OR Strategy is inactive and should be enabled)2641			bool bAdoptOrEndStrategy = false;26422643			// Strategy should be on, and if it's not, turn it on2644			if(bStrategyShouldBeActive)2645			{2646				if(bTestStrategyStart)2647					bAdoptOrEndStrategy = true;2648				else if(bTestStrategyEnd)2649					bAdoptOrEndStrategy = false;2650			}2651			// Strategy should be off, and if it's not, turn it off2652			else2653			{2654				if(bTestStrategyStart)2655					bAdoptOrEndStrategy = false;2656				else if(bTestStrategyEnd)2657					bAdoptOrEndStrategy = true;2658			}26592660			// Flavor propagation2661			if(bAdoptOrEndStrategy)2662			{2663#if !defined(MOD_API_EXTENSIONS)2664				int iFlavorLoop;2665#endif26662667				// We should adopt this Strategy2668				if(bTestStrategyStart)2669				{2670#if defined(MOD_API_EXTENSIONS)2671					UseStrategy(eStrategy, true);2672#else2673					SetUsingStrategy(eStrategy, true);26742675					for(iFlavorLoop = 0; iFlavorLoop < GC.getNumFlavorTypes(); iFlavorLoop++)2676					{2677						m_aiTempFlavors[iFlavorLoop] = pStrategy->GetPlayerFlavorValue(iFlavorLoop);2678					}26792680					GetPlayer()->GetFlavorManager()->ChangeFlavors(m_aiTempFlavors, true);26812682					for(iFlavorLoop = 0; iFlavorLoop < GC.getNumFlavorTypes(); iFlavorLoop++)2683					{2684						m_aiTempFlavors[iFlavorLoop] = pStrategy->GetCityFlavorValue(iFlavorLoop);2685					}26862687					GetPlayer()->GetFlavorManager()->ChangeFlavors(m_aiTempFlavors, false);26882689					if(pStrategy->RequiresCitySpecializationUpdate())2690						GetPlayer()->GetCitySpecializationAI()->SetSpecializationsDirty(SPECIALIZATION_UPDATE_STRATEGY_NOW_ON);2691#endif2692				}2693				// End the Strategy2694				else if(bTestStrategyEnd)2695				{2696#if defined(MOD_API_EXTENSIONS)2697					UseStrategy(eStrategy, false);2698#else2699					SetUsingStrategy(eStrategy, false);27002701					for(iFlavorLoop = 0; iFlavorLoop < GC.getNumFlavorTypes(); iFlavorLoop++)2702					{2703						m_aiTempFlavors[iFlavorLoop] = -pStrategy->GetPlayerFlavorValue(iFlavorLoop);2704					}27052706					GetPlayer()->GetFlavorManager()->ChangeFlavors(m_aiTempFlavors, true);27072708					for(iFlavorLoop = 0; iFlavorLoop < GC.getNumFlavorTypes(); iFlavorLoop++)2709					{2710						m_aiTempFlavors[iFlavorLoop] = -pStrategy->GetCityFlavorValue(iFlavorLoop);2711					}27122713					GetPlayer()->GetFlavorManager()->ChangeFlavors(m_aiTempFlavors, false);27142715					if(pStrategy->RequiresCitySpecializationUpdate())2716						GetPlayer()->GetCitySpecializationAI()->SetSpecializationsDirty(SPECIALIZATION_UPDATE_STRATEGY_NOW_OFF);2717#endif2718				}2719			}2720		}2721	}2722}

Calls

Called by