void CvTacticalAnalysisMap::EstablishZoneNeighborhood()
{
	//walk over the map and see which zones are adjacent
	int iW = GC.getMap().getGridWidth();
	int iH = GC.getMap().getGridHeight();

	for(unsigned int iI = 0; iI < m_DominanceZones.size(); iI++)
	{
		m_DominanceZones[iI].ClearNeighboringZones();
	}

	for (int i=0; i<iW; i++)
	{
		for (int j=0; j<iH; j++)
		{
			CvPlot* pA = GC.getMap().plot(i,j);
			CvPlot* pB = GC.getMap().plot(i,j+1);
			CvPlot* pC = GC.getMap().plot(i+1,j);

			CvTacticalAnalysisCell* cA = pA ? GetCell( pA->GetPlotIndex() ) : NULL;
			CvTacticalAnalysisCell* cB = pB ? GetCell( pB->GetPlotIndex() ) : NULL;
			CvTacticalAnalysisCell* cC = pC ? GetCell( pC->GetPlotIndex() ) : NULL;

			if (cA && cB)
			{
				int iA = cA->GetDominanceZone();
				int iB = cB->GetDominanceZone();
				if (iA!=-1 && iB!=-1 && 
					GetZoneByID(iA)->GetTerritoryType()!=TACTICAL_TERRITORY_NO_OWNER &&
					GetZoneByID(iB)->GetTerritoryType()!=TACTICAL_TERRITORY_NO_OWNER
					)
				{
					GetZoneByID(iA)->AddNeighboringZone(iB);
					GetZoneByID(iB)->AddNeighboringZone(iA);
				}
			}
			if (cA && cC)
			{
				int iA = cA->GetDominanceZone();
				int iC = cC->GetDominanceZone();
				if (iA!=-1 && iC!=-1 &&
					GetZoneByID(iA)->GetTerritoryType()!=TACTICAL_TERRITORY_NO_OWNER &&
					GetZoneByID(iC)->GetTerritoryType()!=TACTICAL_TERRITORY_NO_OWNER
					)
				{
					GetZoneByID(iA)->AddNeighboringZone(iC);
					GetZoneByID(iC)->AddNeighboringZone(iA);
				}
			}
		}
	}
}
//	---------------------------------------------------------------------------
bool CvBarbarians::IsPlotValidForBarbCamp(CvPlot* pPlot)
{
	int iRange = 4;
	int iDY;

	int iPlotX = pPlot->getX();
	int iPlotY = pPlot->getY();

	CvMap& kMap = GC.getMap();
	for (int iDX = -(iRange); iDX <= iRange; iDX++)
	{
		for (iDY = -(iRange); iDY <= iRange; iDY++)
		{
			int iLoopPlotX = iPlotX + iDX;
			int iLoopPlotY = iPlotY + iDY;

			// Cut off corners
			if (plotDistance(iPlotX, iPlotY, iLoopPlotX, iLoopPlotY) > iRange)
				continue;

			// If the counter is below -1 that means a camp was cleared recently
			CvPlot* pLoopPlot = kMap.plot(iLoopPlotX, iLoopPlotY);
			if (pLoopPlot)
			{
				if (m_aiPlotBarbCampSpawnCounter[pLoopPlot->GetPlotIndex()] < -1)
					return false;
			}
		}
	}

	return true;
}
//	---------------------------------------------------------------------------
bool CvBarbarians::IsPlotValidForBarbCamp(CvPlot* pPlot)
{
	int iRange = 4;
	int iDY;

	int iPlotX = pPlot->getX();
	int iPlotY = pPlot->getY();

	CvMap& kMap = GC.getMap();
	for (int iDX = -(iRange); iDX <= iRange; iDX++)
	{
		for (iDY = -(iRange); iDY <= iRange; iDY++)
		{
			int iLoopPlotX = iPlotX + iDX;
			int iLoopPlotY = iPlotY + iDY;

			// Cut off corners
			if (plotDistance(iPlotX, iPlotY, iLoopPlotX, iLoopPlotY) > iRange)
				continue;

			// If the counter is below -1 that means a camp was cleared recently
			CvPlot* pLoopPlot = kMap.plot(iLoopPlotX, iLoopPlotY);
			if (pLoopPlot)
			{
				if (m_aiPlotBarbCampSpawnCounter[pLoopPlot->GetPlotIndex()] < -1)
					return false;
			}
		}
	}

#if defined(MOD_EVENTS_BARBARIANS)
	if (MOD_EVENTS_BARBARIANS) {
		if (GAMEEVENTINVOKE_TESTALL(GAMEEVENT_BarbariansCanFoundCamp, iPlotX, iPlotY) == GAMEEVENTRETURN_FALSE) {
			return false;
		}
	}
#endif
	
	return true;
}
// Indicate the plots we might want to move to that the enemy can attack
void CvTacticalAnalysisMap::MarkCellsNearEnemy()
{
	for(unsigned int iUnitIndex = 0;  iUnitIndex < m_EnemyUnits.size(); iUnitIndex++)
	{
		CvUnit* pUnit = getUnit(m_EnemyUnits[iUnitIndex]);
		
		//for ranged every plot we can enter with movement left is a base for attack
		int iMinMovesLeft = pUnit->IsCanAttackRanged() ? 1 : 0;

		//be a bit conservative here, use ZOC - if one of our units is killed, this is not correct anymore
		//therefore we later do a dilation filter on the cells
		ReachablePlots tiles = pUnit->GetAllPlotsInReachThisTurn(false,true,false,iMinMovesLeft);

		for (ReachablePlots::iterator moveTile=tiles.begin(); moveTile!=tiles.end(); ++moveTile)
		{
			CvPlot* pMoveTile = GC.getMap().plotByIndexUnchecked(moveTile->iPlotIndex);
			int iPlotIndex = GC.getMap().plotNum(pMoveTile->getX(),pMoveTile->getY());

			if (pUnit->IsCanAttackRanged())
			{
				std::set<int> rangedPlots;
				//this generates some overlap, but preventing that is about as bad as ignoring it
				TacticalAIHelpers::GetPlotsUnderRangedAttackFrom(pUnit,pMoveTile,rangedPlots,false,false);
				for (std::set<int>::iterator attackTile=rangedPlots.begin(); attackTile!=rangedPlots.end(); ++attackTile)
				{
					m_pCells[*attackTile].SetSubjectToAttack(true);
				}
			}
			else
			{
				//for melee every tile he can move into can be attacked
				m_pCells[iPlotIndex].SetSubjectToAttack(true);
			}
		}
	}

	//do the dilation
	std::vector<int> vCellsToMark;
	for (int iPlotLoop = 0; iPlotLoop < GC.getMap().numPlots(); iPlotLoop++)
	{
		//nothing to do
		if (m_pCells[iPlotLoop].IsSubjectToAttack())
			continue;

		CvPlot* pPlot = GC.getMap().plotByIndexUnchecked(iPlotLoop);

		//danger plots are calculated without ZOC
		if (GET_PLAYER(m_ePlayer).GetPossibleAttackers(*pPlot).size()>0)
		{
			//check whether neighbors are subject to attack with ZOC
			CvPlot** aPlotsToCheck = GC.getMap().getNeighborsUnchecked(pPlot);
			for (int iI = 0; iI < NUM_DIRECTION_TYPES; iI++)
			{
				CvPlot* pAdjacentPlot = aPlotsToCheck[iI];
				if (pAdjacentPlot)
				{
					if (m_pCells[pAdjacentPlot->GetPlotIndex()].IsSubjectToAttack())
					{
						vCellsToMark.push_back(iPlotLoop);
						break;
					}
				}
			}
		}
	}
	//this should give a nice compromise
	for (size_t iI = 0; iI < vCellsToMark.size(); iI++)
		m_pCells[vCellsToMark[iI]].SetSubjectToAttack(true);

	// Look at every cell on the map
	for(int iI = 0; iI < GC.getMap().numPlots(); iI++)
	{
		CvPlot* pPlot = GC.getMap().plotByIndexUnchecked(iI);
		if(m_pCells[iI].IsRevealed() && !m_pCells[iI].IsImpassableTerrain() && !m_pCells[iI].IsImpassableTerritory())
		{
			// Friendly cities always safe
			if(!m_pCells[iI].IsFriendlyCity())
			{
				if(!pPlot->isVisibleToEnemy(m_ePlayer))
				{
					m_pCells[iI].SetNotVisibleToEnemy(true);
				}
				else
				{
					// Check adjacent plots for enemy citadels
					if(!m_pCells[iI].IsSubjectToAttack())
					{
						if ( pPlot->IsNearEnemyCitadel( m_ePlayer ) )
								m_pCells[iI].SetSubjectToAttack(true);

						for(unsigned int iCityIndex = 0;  iCityIndex < m_EnemyCities.size(); iCityIndex++)
						{
							CvCity* pCity = getCity( m_EnemyCities[iCityIndex] );
							if (pCity->canRangeStrikeAt( pPlot->getX(), pPlot->getY() ))
								m_pCells[iI].SetSubjectToAttack(true);
						}
					}
				}
			}
		}
	}
}