Bazaar developer reference

CvEconomicAI::DoTurn()

Defined in
CvEconomicAI.cpp, line 540
Class
CvEconomicAI
Hook name
CivHooks::CvEconomicAI_DoTurn
Lua events
GameEvents.EconomicStrategyCanActivate

Hooks

static bool BeforeCvEconomicAI_DoTurn(CvEconomicAI* self);static void AfterCvEconomicAI_DoTurn(CvEconomicAI* self);static void ReplaceCvEconomicAI_DoTurn(void (*original)(CvEconomicAI*), CvEconomicAI* self);CivRegisterBefore<CivHooks::CvEconomicAI_DoTurn>(host, &BeforeCvEconomicAI_DoTurn);CivRegisterAfter<CivHooks::CvEconomicAI_DoTurn>(host, &AfterCvEconomicAI_DoTurn);CivRegisterReplace<CivHooks::CvEconomicAI_DoTurn>(host, &ReplaceCvEconomicAI_DoTurn);

Code

539/// Called every turn to see what Strategies this player should using (or not)540void CvEconomicAI::DoTurn()541{542	AI_PERF_FORMAT("AI-perf.csv", ("CvEconomicAI::DoTurn, Turn %03d, %s", GC.getGame().getElapsedGameTurns(), GetPlayer()->getCivilizationShortDescription()) );543544	UpdatePlots();545	LogMonitor();546	LogCityMonitor();547548	// Functions that need to run before we look at strategies549	DoReconState();550	DoAntiquitySites();551552	int iStrategiesLoop = 0;553554	// Loop through all strategies555	for(iStrategiesLoop = 0; iStrategiesLoop < GetEconomicAIStrategies()->GetNumEconomicAIStrategies(); iStrategiesLoop++)556	{557		EconomicAIStrategyTypes eStrategy = (EconomicAIStrategyTypes) iStrategiesLoop;558		CvEconomicAIStrategyXMLEntry* pStrategy = GetEconomicAIStrategies()->GetEntry(iStrategiesLoop);559560		// Minor Civs can't run some Strategies561		if(m_pPlayer->isMinorCiv() && pStrategy->IsNoMinorCivs())562		{563			continue;564		}565566		bool bTestStrategyStart = true;567568		// Do we already have this Strategy adopted?569		if(IsUsingStrategy(eStrategy))570		{571			bTestStrategyStart = false;572		}573		else574		{575			// Has the prereq Tech necessary?576			if(pStrategy->GetTechPrereq() != NO_TECH && !GET_TEAM(GetPlayer()->getTeam()).GetTeamTechs()->HasTech((TechTypes) pStrategy->GetTechPrereq()))577			{578				bTestStrategyStart = false;579			}580581			// Has the Tech which obsoletes this Strategy?582			if(bTestStrategyStart && pStrategy->GetTechObsolete() != NO_TECH && GET_TEAM(GetPlayer()->getTeam()).GetTeamTechs()->HasTech((TechTypes) pStrategy->GetTechObsolete()))583			{584				bTestStrategyStart = false;585			}586587			// Not time to check this yet?588			if(GC.getGame().getGameTurn() < pStrategy->GetFirstTurnExecuted())589			{590				bTestStrategyStart = false;591			}592		}593594		bool bTestStrategyEnd = false;595596		// Strategy is active, check to see if we should even try to disable it597		if(IsUsingStrategy(eStrategy))598		{599			if(pStrategy->GetCheckTriggerTurnCount() > 0)600			{601				// Is it a turn where we want to check to see if this Strategy is maintained?602				if((GC.getGame().getGameTurn() - GetTurnStrategyAdopted(eStrategy)) % pStrategy->GetCheckTriggerTurnCount() == 0)603				{604					bTestStrategyEnd = true;605				}606			}607608			if(bTestStrategyEnd && pStrategy->GetMinimumNumTurnsExecuted() > 0)609			{610				// Has the minimum # of turns passed for this Strategy?611				if(GC.getGame().getGameTurn() < GetTurnStrategyAdopted(eStrategy) + pStrategy->GetMinimumNumTurnsExecuted())612				{613					bTestStrategyEnd = false;614				}615			}616		}617618		// Check Strategy Triggers619		// Functionality and existence of specific Strategies is hardcoded here, but data is stored in XML so it's easier to modify620621		if(bTestStrategyStart || bTestStrategyEnd)622		{623			bool bStrategyShouldBeActive = false;624625			// Has the Tech which obsoletes this Strategy? If so, Strategy should be deactivated regardless of other factors626			if(pStrategy->GetTechObsolete() != NO_TECH && GET_TEAM(GetPlayer()->getTeam()).GetTeamTechs()->HasTech((TechTypes) pStrategy->GetTechObsolete()))627			{628				bStrategyShouldBeActive = false;629			}630			// Strategy isn't obsolete, so test triggers as normal631			else632			{633				CvString strStrategyName = (CvString) pStrategy->GetType();634635				// Check all of the Strategy Triggers636				if(strStrategyName == "ECONOMICAISTRATEGY_NEED_RECON")637					bStrategyShouldBeActive = EconomicAIHelpers::IsTestStrategy_NeedRecon(m_pPlayer);638				else if(strStrategyName == "ECONOMICAISTRATEGY_ENOUGH_RECON")639					bStrategyShouldBeActive = EconomicAIHelpers::IsTestStrategy_EnoughRecon(m_pPlayer);640				else if(strStrategyName == "ECONOMICAISTRATEGY_REALLY_NEED_RECON_SEA")641					bStrategyShouldBeActive = EconomicAIHelpers::IsTestStrategy_ReallyNeedReconSea(m_pPlayer);642				else if(strStrategyName == "ECONOMICAISTRATEGY_NEED_RECON_SEA")643					bStrategyShouldBeActive = EconomicAIHelpers::IsTestStrategy_NeedReconSea(m_pPlayer);644				else if(strStrategyName == "ECONOMICAISTRATEGY_ENOUGH_RECON_SEA")645					bStrategyShouldBeActive = EconomicAIHelpers::IsTestStrategy_EnoughReconSea(m_pPlayer);646				else if(strStrategyName == "ECONOMICAISTRATEGY_EARLY_EXPANSION")647					bStrategyShouldBeActive = EconomicAIHelpers::IsTestStrategy_EarlyExpansion(m_pPlayer);648				else if(strStrategyName == "ECONOMICAISTRATEGY_ENOUGH_EXPANSION")649					bStrategyShouldBeActive = EconomicAIHelpers::IsTestStrategy_EnoughExpansion(eStrategy, m_pPlayer);650				else if(strStrategyName == "ECONOMICAISTRATEGY_NEED_HAPPINESS")651					bStrategyShouldBeActive = EconomicAIHelpers::IsTestStrategy_NeedHappiness(eStrategy, m_pPlayer);652				else if(strStrategyName == "ECONOMICAISTRATEGY_NEED_HAPPINESS_CRITICAL")653					bStrategyShouldBeActive = EconomicAIHelpers::IsTestStrategy_NeedHappinessCritical(eStrategy, m_pPlayer);654				else if(strStrategyName == "ECONOMICAISTRATEGY_CITIES_NEED_NAVAL_GROWTH")655					bStrategyShouldBeActive = EconomicAIHelpers::IsTestStrategy_CitiesNeedNavalGrowth(eStrategy, m_pPlayer);656				else if(strStrategyName == "ECONOMICAISTRATEGY_CITIES_NEED_NAVAL_TILE_IMPROVEMENT")657					bStrategyShouldBeActive = EconomicAIHelpers::IsTestStrategy_CitiesNeedNavalTileImprovement(eStrategy, m_pPlayer);658				else if(strStrategyName == "ECONOMICAISTRATEGY_FOUND_CITY")659					bStrategyShouldBeActive = EconomicAIHelpers::IsTestStrategy_FoundCity(eStrategy, m_pPlayer);660				else if(strStrategyName == "ECONOMICAISTRATEGY_TRADE_WITH_CITY_STATE")661					bStrategyShouldBeActive = EconomicAIHelpers::IsTestStrategy_TradeWithCityState(eStrategy, m_pPlayer);662				else if(strStrategyName == "ECONOMICAISTRATEGY_NEED_IMPROVEMENT_FOOD")663					bStrategyShouldBeActive = EconomicAIHelpers::IsTestStrategy_NeedImprovement(m_pPlayer, YIELD_FOOD);664				else if(strStrategyName == "ECONOMICAISTRATEGY_NEED_IMPROVEMENT_PRODUCTION")665					bStrategyShouldBeActive = EconomicAIHelpers::IsTestStrategy_NeedImprovement(m_pPlayer, YIELD_PRODUCTION);666				else if(strStrategyName == "ECONOMICAISTRATEGY_ONE_OR_FEWER_COASTAL_CITIES")667					bStrategyShouldBeActive = EconomicAIHelpers::IsTestStrategy_OneOrFewerCoastalCities(m_pPlayer);668				else if(strStrategyName == "ECONOMICAISTRATEGY_LOSING_MONEY")669					bStrategyShouldBeActive = EconomicAIHelpers::IsTestStrategy_LosingMoney(eStrategy, m_pPlayer);670				else if(strStrategyName == "ECONOMICAISTRATEGY_HALT_GROWTH_BUILDINGS")671					bStrategyShouldBeActive = EconomicAIHelpers::IsTestStrategy_HaltGrowthBuildings(m_pPlayer);672				else if(strStrategyName == "ECONOMICAISTRATEGY_TOO_MANY_UNITS")673					bStrategyShouldBeActive = EconomicAIHelpers::IsTestStrategy_TooManyUnits(m_pPlayer);674				else if(strStrategyName == "ECONOMICAISTRATEGY_ISLAND_START")675					bStrategyShouldBeActive = EconomicAIHelpers::IsTestStrategy_IslandStart(eStrategy, m_pPlayer);676				else if(strStrategyName == "ECONOMICAISTRATEGY_EXPAND_TO_OTHER_CONTINENTS")677					bStrategyShouldBeActive = EconomicAIHelpers::IsTestStrategy_ExpandToOtherContinents(m_pPlayer);678				else if(strStrategyName == "ECONOMICAISTRATEGY_REALLY_EXPAND_TO_OTHER_CONTINENTS")679					bStrategyShouldBeActive = EconomicAIHelpers::IsTestStrategy_ReallyExpandToOtherContinents(m_pPlayer);				680				else if(strStrategyName == "ECONOMICAISTRATEGY_MOSTLY_ON_THE_COAST")681					bStrategyShouldBeActive = EconomicAIHelpers::IsTestStrategy_MostlyOnTheCoast(m_pPlayer);682				else if(strStrategyName == "ECONOMICAISTRATEGY_EXPAND_LIKE_CRAZY")683					bStrategyShouldBeActive = EconomicAIHelpers::IsTestStrategy_ExpandLikeCrazy(eStrategy, m_pPlayer);684				else if(strStrategyName == "ECONOMICAISTRATEGY_GROW_LIKE_CRAZY")685					bStrategyShouldBeActive = EconomicAIHelpers::IsTestStrategy_GrowLikeCrazy(eStrategy, m_pPlayer);686				else if(strStrategyName == "ECONOMICAISTRATEGY_GS_CULTURE")687					bStrategyShouldBeActive = EconomicAIHelpers::IsTestStrategy_GS_Culture(m_pPlayer);688				else if(strStrategyName == "ECONOMICAISTRATEGY_GS_CONQUEST")689					bStrategyShouldBeActive = EconomicAIHelpers::IsTestStrategy_GS_Conquest(m_pPlayer);690				else if(strStrategyName == "ECONOMICAISTRATEGY_GS_DIPLOMACY")691					bStrategyShouldBeActive = EconomicAIHelpers::IsTestStrategy_GS_Diplomacy(m_pPlayer);692				else if(strStrategyName == "ECONOMICAISTRATEGY_GS_SPACESHIP")693					bStrategyShouldBeActive = EconomicAIHelpers::IsTestStrategy_GS_Spaceship(m_pPlayer);694				else if(strStrategyName == "ECONOMICAISTRATEGY_GS_SPACESHIP_HOMESTRETCH")695					bStrategyShouldBeActive = EconomicAIHelpers::IsTestStrategy_GS_SpaceshipHomestretch(m_pPlayer);696				else if(strStrategyName == "ECONOMICAISTRATEGY_NAVAL_MAP")697					bStrategyShouldBeActive = EconomicAIHelpers::IsTestStrategy_NavalMap(m_pPlayer);698				else if(strStrategyName == "ECONOMICAISTRATEGY_OFFSHORE_EXPANSION_MAP")699					bStrategyShouldBeActive = EconomicAIHelpers::IsTestStrategy_OffshoreExpansionMap(m_pPlayer);700				else if(strStrategyName == "ECONOMICAISTRATEGY_DEVELOPING_RELIGION")701					bStrategyShouldBeActive = EconomicAIHelpers::IsTestStrategy_DevelopingReligion(m_pPlayer);702				else if(strStrategyName == "ECONOMICAISTRATEGY_TECH_LEADER")703					bStrategyShouldBeActive = EconomicAIHelpers::IsTestStrategy_TechLeader(m_pPlayer);704				else if(strStrategyName == "ECONOMICAISTRATEGY_NEED_ARCHAEOLOGISTS")705					bStrategyShouldBeActive = EconomicAIHelpers::IsTestStrategy_NeedArchaeologists(m_pPlayer);706				else if(strStrategyName == "ECONOMICAISTRATEGY_ENOUGH_ARCHAEOLOGISTS")707					bStrategyShouldBeActive = EconomicAIHelpers::IsTestStrategy_EnoughArchaeologists(m_pPlayer);708				else if(strStrategyName == "ECONOMICAISTRATEGY_NEED_MUSEUMS")709					bStrategyShouldBeActive = EconomicAIHelpers::IsTestStrategy_NeedMuseums(m_pPlayer);710				else if(strStrategyName == "ECONOMICAISTRATEGY_NEED_GUILDS")711					bStrategyShouldBeActive = EconomicAIHelpers::IsTestStrategy_NeedGuilds(m_pPlayer);712				else if(strStrategyName == "ECONOMICAISTRATEGY_CONCERT_TOUR")713					bStrategyShouldBeActive = EconomicAIHelpers::IsTestStrategy_ConcertTour(eStrategy, m_pPlayer);714				else if(strStrategyName == "ECONOMICAISTRATEGY_STARTED_PIETY")715					bStrategyShouldBeActive = EconomicAIHelpers::IsTestStrategy_StartedPiety(m_pPlayer);716717				// Never found it?  Assume it is scenario-specific and should be on unless disabled by Lua hook718				else719				{720					bStrategyShouldBeActive = true;721				}722723				// Check Lua hook724				ICvEngineScriptSystem1* pkScriptSystem = gDLL->GetScriptSystem();725				if(pkScriptSystem && bStrategyShouldBeActive)726				{727					CvLuaArgsHandle args;728					args->Push(iStrategiesLoop);729					args->Push(m_pPlayer->GetID());730731					// Attempt to execute the game events.732					// Will return false if there are no registered listeners.733					bool bResult = false;734					if(LuaSupport::CallTestAll(pkScriptSystem, "EconomicStrategyCanActivate", args.get(), bResult))735					{736						// Check the result.737						if(bResult == false)738						{739							bStrategyShouldBeActive = false;740						}741					}742				}743			}744745			// 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)746			bool bAdoptOrEndStrategy = false;747748			// Strategy should be on, and if it's not, turn it on749			if(bStrategyShouldBeActive)750			{751				if(bTestStrategyStart)752				{753					bAdoptOrEndStrategy = true;754				}755				else if(bTestStrategyEnd)756				{757					bAdoptOrEndStrategy = false;758				}759			}760			// Strategy should be off, and if it's not, turn it off761			else762			{763				if(bTestStrategyStart)764				{765					bAdoptOrEndStrategy = false;766				}767				else if(bTestStrategyEnd)768				{769					bAdoptOrEndStrategy = true;770				}771			}772773			// Flavor propagation774			if(bAdoptOrEndStrategy)775			{776#if !defined(MOD_API_EXTENSIONS)777				int iFlavorLoop;778#endif779780				// We should adopt this Strategy781				if(bTestStrategyStart)782				{783#if defined(MOD_API_EXTENSIONS)784					UseStrategy(eStrategy, true);785#else786					SetUsingStrategy(eStrategy, true);787788					for(iFlavorLoop = 0; iFlavorLoop < GC.getNumFlavorTypes(); iFlavorLoop++)789					{790						m_aiTempFlavors[iFlavorLoop] = pStrategy->GetPlayerFlavorValue(iFlavorLoop);791					}792793					GetPlayer()->GetFlavorManager()->ChangeFlavors(m_aiTempFlavors, true);794795					for(iFlavorLoop = 0; iFlavorLoop < GC.getNumFlavorTypes(); iFlavorLoop++)796					{797						m_aiTempFlavors[iFlavorLoop] = pStrategy->GetCityFlavorValue(iFlavorLoop);798					}799800					GetPlayer()->GetFlavorManager()->ChangeFlavors(m_aiTempFlavors, false);801#endif802				}803				// End the Strategy804				else if(bTestStrategyEnd)805				{806#if defined(MOD_API_EXTENSIONS)807					UseStrategy(eStrategy, false);808#else809					SetUsingStrategy(eStrategy, false);810811					for(iFlavorLoop = 0; iFlavorLoop < GC.getNumFlavorTypes(); iFlavorLoop++)812					{813						m_aiTempFlavors[iFlavorLoop] = -pStrategy->GetPlayerFlavorValue(iFlavorLoop);814					}815816					GetPlayer()->GetFlavorManager()->ChangeFlavors(m_aiTempFlavors, true);817818					for(iFlavorLoop = 0; iFlavorLoop < GC.getNumFlavorTypes(); iFlavorLoop++)819					{820						m_aiTempFlavors[iFlavorLoop] = -pStrategy->GetCityFlavorValue(iFlavorLoop);821					}822823					GetPlayer()->GetFlavorManager()->ChangeFlavors(m_aiTempFlavors, false);824#endif825				}826			}827		}828	}829830	if(!m_pPlayer->isHuman())831	{832		DoHurry();833		DoPlotPurchases();834		DisbandExtraWorkers();835		DisbandExtraArchaeologists();836#if defined(MOD_AI_SMART_V3)837		if (MOD_AI_SMART_V3)838		{839			DisbandLongObsoleteUnits();840		}841#endif842843#if defined(MOD_GLOBAL_GREATWORK_YIELDTYPES)844		YieldTypes eFocusYield = NO_YIELD;845		if (EconomicAIHelpers::IsTestStrategy_GS_Spaceship(m_pPlayer)) {846			eFocusYield = YIELD_SCIENCE;847		} else if (EconomicAIHelpers::IsTestStrategy_DevelopingReligion(m_pPlayer)) {848			eFocusYield = YIELD_FAITH;849		} else if (EconomicAIHelpers::IsTestStrategy_LosingMoney((EconomicAIStrategyTypes) GC.getInfoTypeForString("ECONOMICAISTRATEGY_LOSING_MONEY", true), m_pPlayer)) {850			eFocusYield = YIELD_GOLD;851		} else {852			eFocusYield = YIELD_CULTURE;853		}854		855		m_pPlayer->GetCulture()->DoSwapGreatWorks(eFocusYield);856#else857		m_pPlayer->GetCulture()->DoSwapGreatWorks();858#endif859	}860}

Calls

Called by