Hooks
static bool BeforeCvDangerPlots_UpdateDanger(CvDangerPlots* self, bool bPretendWarWithAllCivs, bool bIgnoreVisibility);static void AfterCvDangerPlots_UpdateDanger(CvDangerPlots* self, bool bPretendWarWithAllCivs, bool bIgnoreVisibility);static void ReplaceCvDangerPlots_UpdateDanger(void (*original)(CvDangerPlots*, bool, bool), CvDangerPlots* self, bool bPretendWarWithAllCivs, bool bIgnoreVisibility);CivRegisterBefore<CivHooks::CvDangerPlots_UpdateDanger>(host, &BeforeCvDangerPlots_UpdateDanger);CivRegisterAfter<CivHooks::CvDangerPlots_UpdateDanger>(host, &AfterCvDangerPlots_UpdateDanger);CivRegisterReplace<CivHooks::CvDangerPlots_UpdateDanger>(host, &ReplaceCvDangerPlots_UpdateDanger);
Code
79/// Updates the danger plots values to reflect threats across the map80void CvDangerPlots::UpdateDanger(bool bPretendWarWithAllCivs, bool bIgnoreVisibility)81{82 // danger plots have not been initialized yet, so no need to update83 if(!m_bArrayAllocated)84 {85 return;86 }8788 // wipe out values89 int iGridSize = GC.getMap().numPlots();90 CvAssertMsg(iGridSize == m_DangerPlots.size(), "iGridSize does not match number of DangerPlots");91 for(int i = 0; i < iGridSize; i++)92 {93 m_DangerPlots[i] = 0;94 }9596 CvPlayer& thisPlayer = GET_PLAYER(m_ePlayer);97 TeamTypes thisTeam = thisPlayer.getTeam();9899 // for each opposing civ100 for(int iPlayer = 0; iPlayer < MAX_PLAYERS; iPlayer++)101 {102 PlayerTypes ePlayer = (PlayerTypes)iPlayer;103 CvPlayer& loopPlayer = GET_PLAYER(ePlayer);104 TeamTypes eTeam = loopPlayer.getTeam();105106 if(!loopPlayer.isAlive())107 {108 continue;109 }110111 if(eTeam == thisTeam)112 {113 continue;114 }115116 if(ShouldIgnorePlayer(ePlayer) && !bPretendWarWithAllCivs)117 {118 continue;119 }120121 //for each unit122 int iLoop;123 CvUnit* pLoopUnit = NULL;124 for(pLoopUnit = loopPlayer.firstUnit(&iLoop); pLoopUnit != NULL; pLoopUnit = loopPlayer.nextUnit(&iLoop))125 {126 if(ShouldIgnoreUnit(pLoopUnit, bIgnoreVisibility))127 {128 continue;129 }130131 int iRange = pLoopUnit->baseMoves();132 if(pLoopUnit->canRangeStrike())133 {134 iRange += pLoopUnit->GetRange();135 }136137 CvPlot* pUnitPlot = pLoopUnit->plot();138 AssignUnitDangerValue(pLoopUnit, pUnitPlot);139 CvPlot* pLoopPlot = NULL;140141 for(int iDX = -(iRange); iDX <= iRange; iDX++)142 {143 for(int iDY = -(iRange); iDY <= iRange; iDY++)144 {145 pLoopPlot = plotXYWithRangeCheck(pUnitPlot->getX(), pUnitPlot->getY(), iDX, iDY, iRange);146 if(!pLoopPlot || pLoopPlot == pUnitPlot)147 {148 continue;149 }150151#if defined(MOD_AI_SMART_V3)152 bool bContinue = !pLoopUnit->canMoveOrAttackInto(*pLoopPlot) && !pLoopUnit->canRangeStrikeAt(pLoopPlot->getX(),pLoopPlot->getY());153 if (MOD_AI_SMART_V3) bContinue = !pLoopUnit->canMoveOrAttackInto(*pLoopPlot) && !pLoopUnit->canRangeStrike();154 if(bContinue)155#else156 if(!pLoopUnit->canMoveOrAttackInto(*pLoopPlot) && !pLoopUnit->canRangeStrikeAt(pLoopPlot->getX(),pLoopPlot->getY()))157#endif158 {159 continue;160 }161162 AssignUnitDangerValue(pLoopUnit, pLoopPlot);163 }164 }165 }166167 // for each city168 CvCity* pLoopCity;169 for(pLoopCity = loopPlayer.firstCity(&iLoop); pLoopCity != NULL; pLoopCity = loopPlayer.nextCity(&iLoop))170 {171 if(ShouldIgnoreCity(pLoopCity, bIgnoreVisibility))172 {173 continue;174 }175176#if defined(MOD_EVENTS_CITY_BOMBARD)177 int iRange = pLoopCity->getBombardRange();178 179 if (iRange == 0) continue;180#else181 int iRange = GC.getCITY_ATTACK_RANGE();182#endif183 CvPlot* pCityPlot = pLoopCity->plot();184 AssignCityDangerValue(pLoopCity, pCityPlot);185 CvPlot* pLoopPlot = NULL;186187 for(int iDX = -(iRange); iDX <= iRange; iDX++)188 {189 for(int iDY = -(iRange); iDY <= iRange; iDY++)190 {191 pLoopPlot = plotXYWithRangeCheck(pCityPlot->getX(), pCityPlot->getY(), iDX, iDY, iRange);192 if(!pLoopPlot)193 {194 continue;195 }196197 AssignCityDangerValue(pLoopCity, pLoopPlot);198 }199 }200 }201 }202203 // Citadels204 int iCitadelValue = GetDangerValueOfCitadel();205 int iPlotLoop;206 CvPlot* pPlot, *pAdjacentPlot;207 for(iPlotLoop = 0; iPlotLoop < GC.getMap().numPlots(); iPlotLoop++)208 {209 pPlot = GC.getMap().plotByIndexUnchecked(iPlotLoop);210211 if(pPlot->isRevealed(thisTeam))212 {213 ImprovementTypes eImprovement = pPlot->getRevealedImprovementType(thisTeam);214 if(eImprovement != NO_IMPROVEMENT && GC.getImprovementInfo(eImprovement)->GetNearbyEnemyDamage() > 0)215 {216 if(!ShouldIgnoreCitadel(pPlot, bIgnoreVisibility))217 {218 for(int iI = 0; iI < NUM_DIRECTION_TYPES; iI++)219 {220 pAdjacentPlot = plotDirection(pPlot->getX(), pPlot->getY(), ((DirectionTypes)iI));221222 if(pAdjacentPlot != NULL)223 {224 AddDanger(pAdjacentPlot->getX(), pAdjacentPlot->getY(), iCitadelValue, true);225 }226 }227 }228 }229 }230 }231232 // testing city danger values233 CvCity* pLoopCity;234 int iLoopCity = 0;235 for(pLoopCity = thisPlayer.firstCity(&iLoopCity); pLoopCity != NULL; pLoopCity = thisPlayer.nextCity(&iLoopCity))236 {237 int iThreatValue = GetCityDanger(pLoopCity);238 pLoopCity->SetThreatValue(iThreatValue);239 }240241 m_bDirty = false;242}