CvPlotGroup* CvPlotGroup::colorRegionInternal(CvPlot* pPlot, PlayerTypes eOwner, CvPlotGroup* pPlotGroup, bool bRecalculateBonuses)
{
PROFILE_FUNC();
if (pPlot->isTradeNetwork(GET_PLAYER(eOwner).getTeam()))
{
std::vector<CvPlot*> queue;
queue.reserve(100);
queue.push_back(pPlot);
int iWaterMark = 0;
while( iWaterMark < (int) queue.size() )
{
CvPlot* pLoopPlot = queue[iWaterMark++];
if ( pLoopPlot->getPlotGroup(eOwner) == NULL )
{
if ( pPlotGroup == NULL )
{
pPlotGroup = GET_PLAYER(eOwner).initPlotGroup(pLoopPlot, bRecalculateBonuses);
}
else
{
pPlotGroup->addPlot(pLoopPlot, bRecalculateBonuses);
}
for (int iI = 0; iI < NUM_DIRECTION_TYPES; ++iI)
{
CvPlot* pAdjacentPlot = plotDirection(pLoopPlot->getX_INLINE(), pLoopPlot->getY_INLINE(), ((DirectionTypes)iI));
if (pAdjacentPlot != NULL)
{
if (pLoopPlot->isTradeNetworkConnected(pAdjacentPlot, GET_PLAYER(eOwner).getTeam()))
{
CvPlotGroup* pAdjacentPlotGroup = pAdjacentPlot->getPlotGroup(eOwner);
if (pAdjacentPlotGroup != NULL)
{
if ( pAdjacentPlotGroup != pPlotGroup )
{
if ( pPlotGroup->getLengthPlots() > pAdjacentPlotGroup->getLengthPlots() )
{
pPlotGroup->mergeIn(pAdjacentPlotGroup, bRecalculateBonuses);
}
else
{
pAdjacentPlotGroup->mergeIn(pPlotGroup, bRecalculateBonuses);
pPlotGroup = pAdjacentPlotGroup;
}
}
}
else
{
queue.push_back(pAdjacentPlot);
}
}
}
}
}
}
}
return pPlotGroup;
}