if(_debug) then { diag_log format ["[OCCUPATION]:: Testing location name: %1 position: %2",_locationName,_pos];};
while{_okToSpawn} do
{
// Percentage chance to spawn (roll 80 or more to spawn AI)
_spawnChance = round (random 100);
if(_spawnChance < 80) exitWith { _okToSpawn = false; if(_debug) then { diag_log format ["[OCCUPATION]:: Rolled %1 so not spawning AI this time",_spawnChance,_locationName];};};
if (!isNil "_nearBase") exitwith { _okToSpawn = false; if(_debug) then { diag_log format ["[OCCUPATION]:: %1 is too close to player base",_locationName];};};
// Don't spawn AI near traders and spawn zones
_nearestMarker = [allMapMarkers, _pos] call BIS_fnc_nearestPosition; // Nearest Marker to the Location
_posNearestMarker = getMarkerPos _nearestMarker;
if(_pos distance _posNearestMarker < 500) exitwith { _okToSpawn = false; if(_debug) then { diag_log format ["[OCCUPATION]:: %1 is too close to a %2",_locationName,_nearestMarker];}; };
// Don't spawn additional AI if there are already AI in range
if(_aiNear > 0) exitwith { _okToSpawn = false; if(_debug) then { diag_log format ["[OCCUPATION]:: %1 already has %2 active AI patrolling",_locationName,_aiNear];}; };