bool CvUnitAI::AI_patrolBorders()
{
PROFILE_FUNC();
CvPlot* pBestPlot;
int iValue;
int iBestValue;
iBestValue = 0;
pBestPlot = NULL;
int iDX, iDY;
int iSearchRange = baseMoves();
for (iDX = -(iSearchRange); iDX <= iSearchRange; iDX++)
{
for (iDY = -(iSearchRange); iDY <= iSearchRange; iDY++)
{
CvPlot* pLoopPlot = plotXY(getX_INLINE(), getY_INLINE(), iDX, iDY);
if (pLoopPlot != NULL)
{
if (canMoveInto(pLoopPlot))
{
DirectionTypes eNewDirection = estimateDirection(plot(), pLoopPlot);
iValue = GC.getGameINLINE().getSorenRandNum(1000, "AI Border Patrol");
if (pLoopPlot->isBorder(true))
{
iValue += GC.getGameINLINE().getSorenRandNum(1000, "AI Border Patrol");
}
else if (pLoopPlot->isBorder(false))
{
iValue += GC.getGameINLINE().getSorenRandNum(500, "AI Border Patrol");
}
//Avoid heading backwards, we want to circuit our borders, if possible.
if (eNewDirection == getOppositeDirection(getFacingDirection(false)))
{
iValue /= 25;
}
else if (isAdjacentDirection(getOppositeDirection(getFacingDirection(false)), eNewDirection))
{
iValue /= 10;
}
if (pLoopPlot->getOwnerINLINE() != getOwnerINLINE())
{
iValue /= 10;
}
if (iValue > iBestValue)
{
iBestValue = iValue;
pBestPlot = pLoopPlot;
}
}
}
}
}
if (pBestPlot != NULL)
{
FAssert(!atPlot(pBestPlot));
getGroup()->pushMission(MISSION_MOVE_TO, pBestPlot->getX_INLINE(), pBestPlot->getY_INLINE());
return true;
}
return false;
}