Bazaar developer reference

CvHomelandAI::ExecuteAdmiralMoves()

Defined in
CvHomelandAI.cpp, line 4260
Class
CvHomelandAI
Hook name
CivHooks::CvHomelandAI_ExecuteAdmiralMoves

Hooks

static bool BeforeCvHomelandAI_ExecuteAdmiralMoves(CvHomelandAI* self);static void AfterCvHomelandAI_ExecuteAdmiralMoves(CvHomelandAI* self);static void ReplaceCvHomelandAI_ExecuteAdmiralMoves(void (*original)(CvHomelandAI*), CvHomelandAI* self);CivRegisterBefore<CivHooks::CvHomelandAI_ExecuteAdmiralMoves>(host, &BeforeCvHomelandAI_ExecuteAdmiralMoves);CivRegisterAfter<CivHooks::CvHomelandAI_ExecuteAdmiralMoves>(host, &AfterCvHomelandAI_ExecuteAdmiralMoves);CivRegisterReplace<CivHooks::CvHomelandAI_ExecuteAdmiralMoves>(host, &ReplaceCvHomelandAI_ExecuteAdmiralMoves);

Code

4259/// Moves a great admiral into an important coastal city to aid its defense4260void CvHomelandAI::ExecuteAdmiralMoves()4261{4262	FStaticVector< CvHomelandUnit, 64, true, c_eCiv5GameplayDLL >::iterator it;4263	CvPlot* pHolyCityPlot = NULL;4264	CvCity* pHolyCity = NULL;42654266	// Do we have an Apollo program to stay clear of?4267	bool bHaveApolloInCapital = false;4268	ProjectTypes eApolloProgram = (ProjectTypes) GC.getSPACE_RACE_TRIGGER_PROJECT();4269	if(eApolloProgram != NO_PROJECT)4270	{4271		if(GET_TEAM(m_pPlayer->getTeam()).getProjectCount(eApolloProgram) > 0)4272		{4273			bHaveApolloInCapital = true;4274		}4275	}42764277	// Do we have a holy city to stay clear of?4278	bool bKeepHolyCityClear = false;4279	CvGameReligions* pReligions = GC.getGame().GetGameReligions();4280	ReligionTypes eMyReligion = pReligions->GetReligionCreatedByPlayer(m_pPlayer->GetID());4281	const CvReligion* pMyReligion = pReligions->GetReligion(eMyReligion, m_pPlayer->GetID());4282	if(pMyReligion)4283	{4284		pHolyCityPlot = GC.getMap().plot(pMyReligion->m_iHolyCityX, pMyReligion->m_iHolyCityY);4285		pHolyCity = pHolyCityPlot->getPlotCity();4286		if(pHolyCity && pHolyCity->isCoastal() && pHolyCity->getOwner() == m_pPlayer->GetID())4287		{4288			bKeepHolyCityClear = true;4289		}4290	}42914292	for(it = m_CurrentMoveUnits.begin(); it != m_CurrentMoveUnits.end(); ++it)4293	{4294		UnitHandle pUnit = m_pPlayer->getUnit(it->GetID());4295		if(!pUnit)4296		{4297			continue;4298		}42994300		if(pUnit->GetGreatPeopleDirective() == GREAT_PEOPLE_DIRECTIVE_GOLDEN_AGE)4301		{4302			ExecuteGoldenAgeMove(pUnit.pointer());4303			continue;4304		}43054306		bool bNotAtFriendlyCity = !pUnit->plot()->isCity() || pUnit->plot()->getOwner() != pUnit->getOwner();43074308			// Score cities to move to4309		CvCity* pLoopCity;4310		int iLoopCity = 0;4311		CvWeightedVector<CvCity *, SAFE_ESTIMATE_NUM_CITIES, true> weightedCityList;4312		for(pLoopCity = m_pPlayer->firstCity(&iLoopCity); pLoopCity != NULL; pLoopCity = m_pPlayer->nextCity(&iLoopCity))4313		{4314			if (bHaveApolloInCapital && pLoopCity->isCapital())4315			{4316				continue;4317			}43184319			if (bKeepHolyCityClear && pLoopCity == pHolyCity)4320			{4321				continue;4322			}43234324			if (!pLoopCity->isCoastal())4325			{4326				continue;4327			}43284329			// Don't go here if a different general or admiral is already present4330			bool bSkipCity = false;4331			CvPlot* pTarget = pLoopCity->plot();4332			for(int iUnitLoop = 0; iUnitLoop < pTarget->getNumUnits(); iUnitLoop++)4333			{4334				CvUnit *pLoopUnit = pTarget->getUnitByIndex(iUnitLoop);4335				if(pLoopUnit->AI_getUnitAIType() == UNITAI_GENERAL && pLoopUnit->GetID() != pUnit->GetID())4336				{4337					bSkipCity = true;4338					break;4339				}4340				else if(pLoopUnit->AI_getUnitAIType() == UNITAI_ADMIRAL && pLoopUnit->GetID() != pUnit->GetID())4341				{4342					bSkipCity = true;4343					break;4344				}4345			}4346			if(bSkipCity)4347			{4348				continue;4349			}43504351			int iTurns = TurnsToReachTarget(pUnit, pLoopCity->plot());43524353			// Don't go here if I'm not in a city currently and this city is not reachable by normal movement4354			if (bNotAtFriendlyCity)4355			{4356				if (iTurns == MAX_INT)4357				{4358					continue;4359				}4360			}43614362			// Weight is size of largest adjacent ocean4363			int iWeight = pTarget->GetSizeLargestAdjacentWater();43644365			// If this city is damaged, divide weight by the damage level4366			if (pLoopCity->getDamage() > 0)4367			{4368				iWeight /= pLoopCity->getDamage();4369			}43704371			// Subtract off turns to reach4372			if (iTurns != MAX_INT)4373			{4374				iWeight -= iTurns;4375			}43764377			weightedCityList.push_back(pLoopCity, iWeight);4378		}43794380		weightedCityList.SortItems();4381		if (weightedCityList.size() > 0)4382		{4383			CvCity *pChosenCity = weightedCityList.GetElement(0);43844385			// Am I already in chosen city?4386			if (pUnit->plot() == pChosenCity->plot())4387			{4388				pUnit->finishMoves();4389				UnitProcessed(pUnit->GetID());43904391				if(GC.getLogging() && GC.getAILogging())4392				{4393					CvString strLogString;4394					strLogString.Format("Great Admiral still in most favored city of %s, X: %d, Y: %d", pChosenCity->getName().GetCString(), pChosenCity->getX(), pChosenCity->getY());4395					LogHomelandMessage(strLogString);4396				}4397			}4398			4399			// Am I currently in a different friendly city?4400			else if (!bNotAtFriendlyCity)4401			{4402				pUnit->PushMission(CvTypes::getMISSION_CHANGE_ADMIRAL_PORT(), pChosenCity->getX(), pChosenCity->getY());4403				pUnit->finishMoves();4404				UnitProcessed(pUnit->GetID());44054406				if(GC.getLogging() && GC.getAILogging())4407				{4408					CvString strLogString;4409					strLogString.Format("Transferring Great Admiral to city of %s, X: %d, Y: %d", pChosenCity->getName().GetCString(), pChosenCity->getX(), pChosenCity->getY());4410					LogHomelandMessage(strLogString);4411				}4412			}44134414			// Move normally to this city4415			else4416			{4417				pUnit->PushMission(CvTypes::getMISSION_MOVE_TO(), pChosenCity->getX(), pChosenCity->getY());4418				pUnit->finishMoves();4419				UnitProcessed(pUnit->GetID());44204421				if(GC.getLogging() && GC.getAILogging())4422				{4423					CvString strLogString;4424					strLogString.Format("Moving Great Admiral normally to city of %s, X: %d, Y: %d", pChosenCity->getName().GetCString(), pChosenCity->getX(), pChosenCity->getY());4425					LogHomelandMessage(strLogString);4426				}4427			}4428		}4429		else4430		{4431			pUnit->finishMoves();4432			UnitProcessed(pUnit->GetID());44334434			if(GC.getLogging() && GC.getAILogging())4435			{4436				CvString strLogString;4437				strLogString.Format("No place to move Great Admiral at, X: %d, Y: %d", pUnit->getX(), pUnit->getY());4438				LogHomelandMessage(strLogString);4439			}4440		}4441	}4442}

Calls

Called by