Bazaar developer reference

CvDangerPlots::AssignUnitDangerValue(CvUnit *, CvPlot *)

Defined in
CvDangerPlots.cpp, line 605
Class
CvDangerPlots
Hook name
CivHooks::CvDangerPlots_AssignUnitDangerValue

Hooks

static bool BeforeCvDangerPlots_AssignUnitDangerValue(CvDangerPlots* self, CvUnit* pUnit, CvPlot* pPlot);static void AfterCvDangerPlots_AssignUnitDangerValue(CvDangerPlots* self, CvUnit* pUnit, CvPlot* pPlot);static void ReplaceCvDangerPlots_AssignUnitDangerValue(void (*original)(CvDangerPlots*, CvUnit*, CvPlot*), CvDangerPlots* self, CvUnit* pUnit, CvPlot* pPlot);CivRegisterBefore<CivHooks::CvDangerPlots_AssignUnitDangerValue>(host, &BeforeCvDangerPlots_AssignUnitDangerValue);CivRegisterAfter<CivHooks::CvDangerPlots_AssignUnitDangerValue>(host, &AfterCvDangerPlots_AssignUnitDangerValue);CivRegisterReplace<CivHooks::CvDangerPlots_AssignUnitDangerValue>(host, &ReplaceCvDangerPlots_AssignUnitDangerValue);

Code

604/// Contains the calculations to do the danger value for the plot according to the unit605void CvDangerPlots::AssignUnitDangerValue(CvUnit* pUnit, CvPlot* pPlot)606{607	// MAJIK NUMBARS TO MOVE TO XML608#if defined(MOD_AI_SMART_V3)609	int iTurnsAway = 0;610#endif611612	int iCombatValueCalc = 100;613	int iBaseUnitCombatValue = pUnit->GetBaseCombatStrengthConsideringDamage() * iCombatValueCalc;614	// Combat capable?  If not, the calculations will always result in 0, so just skip it.615	if(iBaseUnitCombatValue > 0)616	{617		// Will any danger be zero'ed out?618		if(!IsDangerByRelationshipZero(pUnit->getOwner(), pPlot))619		{620			//int iDistance = plotDistance(pUnitPlot->getX(), pUnitPlot->getY(), pPlot->getX(), pPlot->getY());621			//int iRange = pUnit->baseMoves();622			//FAssertMsg(iRange > 0, "0 range? Uh oh");623624			CvIgnoreUnitsPathFinder& kPathFinder = GC.getIgnoreUnitsPathFinder();625			kPathFinder.SetData(pUnit);626627			int iPlotX = pPlot->getX();628			int iPlotY = pPlot->getY();629#if defined(MOD_AI_SMART_V3)630			if (MOD_AI_SMART_V3)631			{632				int pDistance = plotDistance(pUnit->getX(), pUnit->getY(), iPlotX, iPlotY);633				// Lest substract distance, will not greatly affect danger but will give distant = safer position.634				iBaseUnitCombatValue -= pDistance;635				//AMS: Is a ranged unit?636				if (pUnit->canRangeStrike())637				{638					// Plot is in range and can strike from current position639					if( pDistance <= pUnit->GetRange() && pUnit->canRangeStrikeAt(pPlot->getX(),pPlot->getY()))640					{641						iTurnsAway = 1;642					}643					else if(pDistance < pUnit->GetRangePlusMoveToshot())644					{645						if (kPathFinder.GeneratePath(pUnit->getX(), pUnit->getY(), iPlotX, iPlotY, 0, true /*bReuse*/))646						{647							iTurnsAway = 2;648						}649					}650					if (iTurnsAway == 0)651					{652						return;653					}654				}655			}656#endif657658#if defined(MOD_AI_SMART_V3)659			if (!MOD_AI_SMART_V3 || iTurnsAway == 0)660			{661#endif662			// can the unit actually walk there663			if(!kPathFinder.GeneratePath(pUnit->getX(), pUnit->getY(), iPlotX, iPlotY, 0, true /*bReuse*/))664			{665				return;666			}667668			CvAStarNode* pNode = kPathFinder.GetLastNode();669#if defined(MOD_AI_SMART_V3)670			if (MOD_AI_SMART_V3)671			{672				iTurnsAway = pNode->m_iData2;673				iTurnsAway = max(iTurnsAway, 1);674			}675#endif676#if defined(MOD_AI_SMART_V3)677			}678#endif679680#if defined(MOD_AI_SMART_V3)681			if (!MOD_AI_SMART_V3)682			{683				CvAStarNode* pNode = kPathFinder.GetLastNode();684				iTurnsAway = pNode->m_iData2;685				iTurnsAway = max(iTurnsAway, 1);686			}687#else688			int iTurnsAway = pNode->m_iData2;689			iTurnsAway = max(iTurnsAway, 1);690#endif691692			int iUnitCombatValue = iBaseUnitCombatValue / iTurnsAway;693			iUnitCombatValue = ModifyDangerByRelationship(pUnit->getOwner(), pPlot, iUnitCombatValue);694			AddDanger(iPlotX, iPlotY, iUnitCombatValue, iTurnsAway <= 1);695		}696	}697}

Calls

Called by