bool CvDangerPlots::UpdateDangerSingleUnit(CvUnit* pLoopUnit, bool bIgnoreVisibility, bool bRemember) { if(ShouldIgnoreUnit(pLoopUnit, bIgnoreVisibility)) return false; if(IsKnownAttacker(pLoopUnit->getOwner(),pLoopUnit->GetID())) return false; //for ranged every plot we can enter with movement left is a base for attack int iMinMovesLeft = pLoopUnit->IsCanAttackRanged() ? 1 : 0; if (pLoopUnit->isMustSetUpToRangedAttack()) iMinMovesLeft += GC.getMOVE_DENOMINATOR(); //specialty for barbarian who won't leave camp if (pLoopUnit->isBarbarian() && pLoopUnit->plot()->getImprovementType()==(ImprovementTypes)GC.getBARBARIAN_CAMP_IMPROVEMENT()) iMinMovesLeft = pLoopUnit->getMoves(); //use the worst case assumption here, no ZOC (all intervening units have been killed) ReachablePlots reachablePlots; TacticalAIHelpers::GetAllPlotsInReachThisTurn(pLoopUnit,pLoopUnit->plot(),reachablePlots,true,false,false,iMinMovesLeft); if (pLoopUnit->IsCanAttackRanged()) { //for ranged every tile we can enter with movement left is a base for attack std::set<int> attackableTiles; TacticalAIHelpers::GetPlotsUnderRangedAttackFrom(pLoopUnit,reachablePlots,attackableTiles); for (std::set<int>::iterator attackTile=attackableTiles.begin(); attackTile!=attackableTiles.end(); ++attackTile) { CvPlot* pAttackTile = GC.getMap().plotByIndexUnchecked(*attackTile); AssignUnitDangerValue(pLoopUnit, pAttackTile); } } else { //for melee every tile we can move into can be attacked for (ReachablePlots::iterator moveTile=reachablePlots.begin(); moveTile!=reachablePlots.end(); ++moveTile) { CvPlot* pMoveTile = GC.getMap().plotByIndexUnchecked(moveTile->iPlotIndex); AssignUnitDangerValue(pLoopUnit, pMoveTile); } } if (bRemember) AddKnownAttacker(pLoopUnit->getOwner(),pLoopUnit->GetID()); return true; }
/// Updates the danger plots values to reflect threats across the map void CvDangerPlots::UpdateDanger (bool bPretendWarWithAllCivs, bool bIgnoreVisibility) { // danger plots have not been initialized yet, so no need to update if (!m_bArrayAllocated) { return; } // wipe out values int iGridSize = GC.getMap().numPlots(); CvAssertMsg(iGridSize == m_DangerPlots.size(), "iGridSize does not match number of DangerPlots"); for (int i = 0; i < iGridSize; i++) { m_DangerPlots[i] = 0; } CvPlayer& thisPlayer = GET_PLAYER(m_ePlayer); TeamTypes thisTeam = thisPlayer.getTeam(); // for each opposing civ for (int iPlayer = 0; iPlayer < MAX_PLAYERS; iPlayer++) { PlayerTypes ePlayer = (PlayerTypes)iPlayer; CvPlayer& loopPlayer = GET_PLAYER(ePlayer); TeamTypes eTeam = loopPlayer.getTeam(); if (!loopPlayer.isAlive()) { continue; } if (eTeam == thisTeam) { continue; } if (ShouldIgnorePlayer(ePlayer) && !bPretendWarWithAllCivs) { continue; } //for each unit int iLoop; CvUnit* pLoopUnit = NULL; for (pLoopUnit = loopPlayer.firstUnit(&iLoop); pLoopUnit != NULL; pLoopUnit = loopPlayer.nextUnit(&iLoop)) { if (ShouldIgnoreUnit(pLoopUnit, bIgnoreVisibility)) { continue; } int iRange = pLoopUnit->baseMoves(); if (pLoopUnit->canRangeStrike()) { iRange += pLoopUnit->GetRange(); } CvPlot* pUnitPlot = pLoopUnit->plot(); AssignUnitDangerValue(pLoopUnit, pUnitPlot); CvPlot* pLoopPlot = NULL; for (int iDX = -(iRange); iDX <= iRange; iDX++) { for (int iDY = -(iRange); iDY <= iRange; iDY++) { pLoopPlot = plotXYWithRangeCheck(pUnitPlot->getX(), pUnitPlot->getY(), iDX, iDY, iRange); if (!pLoopPlot || pLoopPlot == pUnitPlot) { continue; } if (!pLoopUnit->canMoveOrAttackInto(*pLoopPlot) && !pLoopUnit->canRangeStrikeAt(pLoopPlot->getX(),pLoopPlot->getY())) { continue; } AssignUnitDangerValue(pLoopUnit, pLoopPlot); } } } // for each city CvCity* pLoopCity; for (pLoopCity = loopPlayer.firstCity(&iLoop); pLoopCity != NULL; pLoopCity = loopPlayer.nextCity(&iLoop)) { if (ShouldIgnoreCity(pLoopCity, bIgnoreVisibility)) { continue; } int iRange = GC.getCITY_ATTACK_RANGE(); CvPlot* pCityPlot = pLoopCity->plot(); AssignCityDangerValue(pLoopCity, pCityPlot); CvPlot* pLoopPlot = NULL; for (int iDX = -(iRange); iDX <= iRange; iDX++) { for (int iDY = -(iRange); iDY <= iRange; iDY++) { pLoopPlot = plotXYWithRangeCheck(pCityPlot->getX(), pCityPlot->getY(), iDX, iDY, iRange); if (!pLoopPlot) { continue; } AssignCityDangerValue(pLoopCity, pLoopPlot); } } } } // Citadels int iCitadelValue = GetDangerValueOfCitadel(); int iPlotLoop; CvPlot *pPlot, *pAdjacentPlot; for (iPlotLoop = 0; iPlotLoop < GC.getMap().numPlots(); iPlotLoop++) { pPlot = GC.getMap().plotByIndexUnchecked(iPlotLoop); if (pPlot->isRevealed(thisTeam)) { ImprovementTypes eImprovement = pPlot->getRevealedImprovementType(thisTeam); if (eImprovement != NO_IMPROVEMENT && GC.getImprovementInfo(eImprovement)->GetNearbyEnemyDamage() > 0) { if (!ShouldIgnoreCitadel(pPlot, bIgnoreVisibility)) { for (int iI = 0; iI < NUM_DIRECTION_TYPES; iI++) { pAdjacentPlot = plotDirection(pPlot->getX(), pPlot->getY(), ((DirectionTypes)iI)); if (pAdjacentPlot != NULL) { AddDanger(pAdjacentPlot->getX(), pAdjacentPlot->getY(), iCitadelValue); } } } } } } // testing city danger values CvCity* pLoopCity; int iLoopCity = 0; for (pLoopCity = thisPlayer.firstCity(&iLoopCity); pLoopCity != NULL; pLoopCity = thisPlayer.nextCity(&iLoopCity)) { int iThreatValue = GetCityDanger(pLoopCity); pLoopCity->SetThreatValue(iThreatValue); } m_bDirty = false; }