src/game/Tactical/Soldier_Add.cc
UINT16 FindGridNoFromSweetSpot(const SOLDIERTYPE* const pSoldier, const INT16 sSweetGridNo, const INT8 ubRadius)
{
	INT16 sTop, sBottom;
	INT16 sLeft, sRight;
	INT16 cnt1, cnt2;
	INT16 sGridNo;
	INT32 uiRange, uiLowestRange = 999999;
	INT32 leftmost;
	SOLDIERTYPE soldier;
	UINT8 ubSaveNPCAPBudget;
	UINT8 ubSaveNPCDistLimit;

	//Save AI pathing vars.  changing the distlimit restricts how
	//far away the pathing will consider.
	ubSaveNPCAPBudget = gubNPCAPBudget;
	ubSaveNPCDistLimit = gubNPCDistLimit;
	gubNPCAPBudget = 0;
	gubNPCDistLimit = ubRadius;

	//create dummy soldier, and use the pathing to determine which nearby slots are
	//reachable.
	soldier = SOLDIERTYPE{};
	soldier.bLevel = 0;
	soldier.bTeam = 1;
	soldier.sGridNo = sSweetGridNo;

	sTop    = ubRadius;
	sBottom = -ubRadius;
	sLeft   = - ubRadius;
	sRight  = ubRadius;

	//clear the mapelements of potential residue MAPELEMENT_REACHABLE flags
	//in the square region.
	// ATE: CHECK FOR BOUNDARIES!!!!!!
	for( cnt1 = sBottom; cnt1 <= sTop; cnt1++ )
	{
		leftmost = ( ( sSweetGridNo + ( WORLD_COLS * cnt1 ) )/ WORLD_COLS ) * WORLD_COLS;

		for( cnt2 = sLeft; cnt2 <= sRight; cnt2++ )
		{
			sGridNo = sSweetGridNo + ( WORLD_COLS * cnt1 ) + cnt2;
			if( sGridNo >=0 && sGridNo < WORLD_MAX && sGridNo >= leftmost && sGridNo < ( leftmost + WORLD_COLS ) )
			{
				gpWorldLevelData[ sGridNo ].uiFlags &= (~MAPELEMENT_REACHABLE);
			}
		}
	}

	//Now, find out which of these gridnos are reachable
	//(use the fake soldier and the pathing settings)
	FindBestPath( &soldier, NOWHERE, 0, WALKING, COPYREACHABLE, ( PATH_IGNORE_PERSON_AT_DEST | PATH_THROUGH_PEOPLE ) );

	uiLowestRange = 999999;

	INT16 sLowestGridNo = NOWHERE;
	for( cnt1 = sBottom; cnt1 <= sTop; cnt1++ )
	{
		leftmost = ( ( sSweetGridNo + ( WORLD_COLS * cnt1 ) )/ WORLD_COLS ) * WORLD_COLS;

		for( cnt2 = sLeft; cnt2 <= sRight; cnt2++ )
		{
			sGridNo = sSweetGridNo + ( WORLD_COLS * cnt1 ) + cnt2;
			if( sGridNo >=0 && sGridNo < WORLD_MAX && sGridNo >= leftmost && sGridNo < ( leftmost + WORLD_COLS )
				&& gpWorldLevelData[ sGridNo ].uiFlags & MAPELEMENT_REACHABLE )
			{
				// Go on sweet stop
				if ( NewOKDestination( pSoldier, sGridNo, TRUE, pSoldier->bLevel ) )
				{
					// ATE: INstead of using absolute range, use the path cost!
					//uiRange = PlotPath(&soldier, sGridNo, NO_COPYROUTE, NO_PLOT, WALKING, 50);
					uiRange = CardinalSpacesAway( sSweetGridNo, sGridNo );

					//if ( uiRange == 0 )
					//{
					//	uiRange = 999999;
					//}

					if ( uiRange < uiLowestRange )
					{
						sLowestGridNo = sGridNo;
						uiLowestRange = uiRange;
					}
				}
			}
		}
	}
	gubNPCAPBudget = ubSaveNPCAPBudget;
	gubNPCDistLimit = ubSaveNPCDistLimit;
	return sLowestGridNo;
}
//Kris:  modified to actually path from sweetspot to gridno.  Previously, it only checked if the
//destination was sittable (though it was possible that that location would be trapped.
modified to actually path from sweetspot to gridno.  Previously, it only checked if the
destination was sittable (though it was possible that that location would be trapped.
modified to actually path from sweetspot to gridno . Previously it only checked if the destination was sittable ( though it was possible that that location would be trapped .
UINT16 FindGridNoFromSweetSpot(const SOLDIERTYPE* const pSoldier, const INT16 sSweetGridNo, const INT8 ubRadius)
{
	INT16 sTop, sBottom;
	INT16 sLeft, sRight;
	INT16 cnt1, cnt2;
	INT16 sGridNo;
	INT32 uiRange, uiLowestRange = 999999;
	INT32 leftmost;
	SOLDIERTYPE soldier;
	UINT8 ubSaveNPCAPBudget;
	UINT8 ubSaveNPCDistLimit;
	ubSaveNPCAPBudget = gubNPCAPBudget;
	ubSaveNPCDistLimit = gubNPCDistLimit;
	gubNPCAPBudget = 0;
	gubNPCDistLimit = ubRadius;
	soldier = SOLDIERTYPE{};
	soldier.bLevel = 0;
	soldier.bTeam = 1;
	soldier.sGridNo = sSweetGridNo;
	sTop    = ubRadius;
	sBottom = -ubRadius;
	sLeft   = - ubRadius;
	sRight  = ubRadius;
	for( cnt1 = sBottom; cnt1 <= sTop; cnt1++ )
	{
		leftmost = ( ( sSweetGridNo + ( WORLD_COLS * cnt1 ) )/ WORLD_COLS ) * WORLD_COLS;
		for( cnt2 = sLeft; cnt2 <= sRight; cnt2++ )
		{
			sGridNo = sSweetGridNo + ( WORLD_COLS * cnt1 ) + cnt2;
			if( sGridNo >=0 && sGridNo < WORLD_MAX && sGridNo >= leftmost && sGridNo < ( leftmost + WORLD_COLS ) )
			{
				gpWorldLevelData[ sGridNo ].uiFlags &= (~MAPELEMENT_REACHABLE);
			}
		}
	}
	FindBestPath( &soldier, NOWHERE, 0, WALKING, COPYREACHABLE, ( PATH_IGNORE_PERSON_AT_DEST | PATH_THROUGH_PEOPLE ) );
	uiLowestRange = 999999;
	INT16 sLowestGridNo = NOWHERE;
	for( cnt1 = sBottom; cnt1 <= sTop; cnt1++ )
	{
		leftmost = ( ( sSweetGridNo + ( WORLD_COLS * cnt1 ) )/ WORLD_COLS ) * WORLD_COLS;
		for( cnt2 = sLeft; cnt2 <= sRight; cnt2++ )
		{
			sGridNo = sSweetGridNo + ( WORLD_COLS * cnt1 ) + cnt2;
			if( sGridNo >=0 && sGridNo < WORLD_MAX && sGridNo >= leftmost && sGridNo < ( leftmost + WORLD_COLS )
				&& gpWorldLevelData[ sGridNo ].uiFlags & MAPELEMENT_REACHABLE )
			{
				if ( NewOKDestination( pSoldier, sGridNo, TRUE, pSoldier->bLevel ) )
				{
					uiRange = CardinalSpacesAway( sSweetGridNo, sGridNo );
					if ( uiRange < uiLowestRange )
					{
						sLowestGridNo = sGridNo;
						uiLowestRange = uiRange;
					}
				}
			}
		}
	}
	gubNPCAPBudget = ubSaveNPCAPBudget;
	gubNPCDistLimit = ubSaveNPCDistLimit;
	return sLowestGridNo;
}
UINT16 FindGridNoFromSweetSpot ( const SOLDIERTYPE * const pSoldier , const INT16 sSweetGridNo , const INT8 ubRadius ) { INT16 sTop , sBottom ; INT16 sLeft , sRight ; INT16 cnt1 , cnt2 ; INT16 sGridNo ; INT32 uiRange , uiLowestRange = 999999 ; INT32 leftmost ; SOLDIERTYPE soldier ; UINT8 ubSaveNPCAPBudget ; UINT8 ubSaveNPCDistLimit ; ubSaveNPCAPBudget = gubNPCAPBudget ; ubSaveNPCDistLimit = gubNPCDistLimit ; gubNPCAPBudget = 0 ; gubNPCDistLimit = ubRadius ; soldier = SOLDIERTYPE { } ; soldier . bLevel = 0 ; soldier . bTeam = 1 ; soldier . sGridNo = sSweetGridNo ; sTop = ubRadius ; sBottom = - ubRadius ; sLeft = - ubRadius ; sRight = ubRadius ; for ( cnt1 = sBottom ; cnt1 <= sTop ; cnt1 ++ ) { leftmost = ( ( sSweetGridNo + ( WORLD_COLS * cnt1 ) ) / WORLD_COLS ) * WORLD_COLS ; for ( cnt2 = sLeft ; cnt2 <= sRight ; cnt2 ++ ) { sGridNo = sSweetGridNo + ( WORLD_COLS * cnt1 ) + cnt2 ; if ( sGridNo >= 0 && sGridNo < WORLD_MAX && sGridNo >= leftmost && sGridNo < ( leftmost + WORLD_COLS ) ) { gpWorldLevelData [ sGridNo ] . uiFlags &= ( ~ MAPELEMENT_REACHABLE ) ; } } } FindBestPath ( & soldier , NOWHERE , 0 , WALKING , COPYREACHABLE , ( PATH_IGNORE_PERSON_AT_DEST | PATH_THROUGH_PEOPLE ) ) ; uiLowestRange = 999999 ; INT16 sLowestGridNo = NOWHERE ; for ( cnt1 = sBottom ; cnt1 <= sTop ; cnt1 ++ ) { leftmost = ( ( sSweetGridNo + ( WORLD_COLS * cnt1 ) ) / WORLD_COLS ) * WORLD_COLS ; for ( cnt2 = sLeft ; cnt2 <= sRight ; cnt2 ++ ) { sGridNo = sSweetGridNo + ( WORLD_COLS * cnt1 ) + cnt2 ; if ( sGridNo >= 0 && sGridNo < WORLD_MAX && sGridNo >= leftmost && sGridNo < ( leftmost + WORLD_COLS ) && gpWorldLevelData [ sGridNo ] . uiFlags & MAPELEMENT_REACHABLE ) { if ( NewOKDestination ( pSoldier , sGridNo , TRUE , pSoldier -> bLevel ) ) { uiRange = CardinalSpacesAway ( sSweetGridNo , sGridNo ) ; if ( uiRange < uiLowestRange ) { sLowestGridNo = sGridNo ; uiLowestRange = uiRange ; } } } } } gubNPCAPBudget = ubSaveNPCAPBudget ; gubNPCDistLimit = ubSaveNPCDistLimit ; return sLowestGridNo ; }
Kris:  modified to actually path from sweetspot to gridno.
Kris : modified to actually path from sweetspot to gridno .
//Save AI pathing vars.  changing the distlimit restricts how //far away the pathing will consider. //create dummy soldier, and use the pathing to determine which nearby slots are //reachable. //clear the mapelements of potential residue MAPELEMENT_REACHABLE flags //in the square region. // ATE: CHECK FOR BOUNDARIES!!!!!! //Now, find out which of these gridnos are reachable //(use the fake soldier and the pathing settings) // Go on sweet stop // ATE: INstead of using absolute range, use the path cost! //uiRange = PlotPath(&soldier, sGridNo, NO_COPYROUTE, NO_PLOT, WALKING, 50); //if ( uiRange == 0 ) //{ //	uiRange = 999999; //}