Bazaar developer reference

CvHomelandAI::PlotMovesToSafety()

Defined in
CvHomelandAI.cpp, line 1020
Class
CvHomelandAI
Hook name
CivHooks::CvHomelandAI_PlotMovesToSafety

Hooks

static bool BeforeCvHomelandAI_PlotMovesToSafety(CvHomelandAI* self);static void AfterCvHomelandAI_PlotMovesToSafety(CvHomelandAI* self);static void ReplaceCvHomelandAI_PlotMovesToSafety(void (*original)(CvHomelandAI*), CvHomelandAI* self);CivRegisterBefore<CivHooks::CvHomelandAI_PlotMovesToSafety>(host, &BeforeCvHomelandAI_PlotMovesToSafety);CivRegisterAfter<CivHooks::CvHomelandAI_PlotMovesToSafety>(host, &AfterCvHomelandAI_PlotMovesToSafety);CivRegisterReplace<CivHooks::CvHomelandAI_PlotMovesToSafety>(host, &ReplaceCvHomelandAI_PlotMovesToSafety);

Code

1019/// Moved endangered units to safe hexes1020void CvHomelandAI::PlotMovesToSafety()1021{1022	ClearCurrentMoveUnits();10231024	// Loop through all recruited units1025	for(list<int>::iterator it = m_CurrentTurnUnits.begin(); it != m_CurrentTurnUnits.end(); ++it)1026	{1027		UnitHandle pUnit = m_pPlayer->getUnit(*it);1028		if(pUnit)1029		{1030			// Danger value of plot must be greater than 01031			CvPlot* pPlot = pUnit->plot();10321033			int iDangerLevel = m_pPlayer->GetPlotDanger(*pPlot);1034			if(iDangerLevel > 0)1035			{1036				bool bAddUnit = false;10371038				// If civilian (or embarked unit) always ready to flee1039				// slewis - 4.18.2013 - Problem here is that a combat unit that is a boat can get stuck in a city hiding from barbarians on the land1040				if(!pUnit->IsCanDefend())1041				{1042					if (pUnit->IsAutomated() && pUnit->GetBaseCombatStrength() > 0)1043					{1044						// then this is our special case1045					}1046					else1047					{1048						bAddUnit = true;1049					}1050				}10511052				// Also may be true if a damaged combat unit1053				else if(pUnit->GetCurrHitPoints() < pUnit->GetMaxHitPoints())1054				{1055#if defined(MOD_AI_SMART_V3)1056					bool bFlee;1057					if (MOD_AI_SMART_V3)1058						bFlee = ((pUnit->getDamage()*100)/pUnit->GetMaxHitPoints())>70;1059					else1060						bFlee = pUnit->IsUnderEnemyRangedAttack() || pUnit->GetBaseCombatStrengthConsideringDamage() * 2 <= pUnit->GetBaseCombatStrength();1061#endif10621063					if(pUnit->isBarbarian())1064					{1065						// Barbarian combat units - only naval units flee (but they flee if have taken ANY damage)1066						if(pUnit->getDomainType() == DOMAIN_SEA)1067						{1068							bAddUnit = true;1069						}1070					}10711072#if defined(MOD_AI_SMART_V3)1073					// Everyone else flees at more than 70% damage1074					else if(bFlee)1075#else1076					// Everyone else flees at less than or equal to 50% combat strength1077					else if(pUnit->IsUnderEnemyRangedAttack() || pUnit->GetBaseCombatStrengthConsideringDamage() * 2 <= pUnit->GetBaseCombatStrength())1078#endif1079					{1080						bAddUnit = true;1081					}1082				}10831084				// Also flee if danger is really high in current plot (but not if we're barbarian)1085				else if(!pUnit->isBarbarian())1086				{1087					int iAcceptableDanger;1088					iAcceptableDanger = pUnit->GetBaseCombatStrengthConsideringDamage() * 100;1089					if(iDangerLevel > iAcceptableDanger)1090					{1091						bAddUnit = true;1092					}1093				}10941095				if(bAddUnit)1096				{1097					// Just one unit involved in this move to execute1098					CvHomelandUnit unit;1099					unit.SetID(pUnit->GetID());1100					m_CurrentMoveUnits.push_back(unit);1101				}1102			}1103		}1104	}11051106	if(m_CurrentMoveUnits.size() > 0)1107	{1108		ExecuteMovesToSafestPlot();1109	}1110}

Calls

Called by