/*
 FIND_ENEMIES © Pumpkin V1.02
 ------------

 trouve les ennemis dans un rayon donné et repositionne à une position sûre
 en fonction de 3 paramètres contenu dans le tableau "PUMPKINRespawnMove" :
 [rayon minimal de recherche, pas/incrément de recherche d'ennemis, distance maximale de respawn]

 Paramètres :
 ============
 unité execVM "plr_respawn\find_enemies.sqf"

*/

_man = _this;

if (isNil "PUMPKINRespawnMove") then { PUMPKINRespawnMove = [20,10,100] };

_safedist = PUMPKINRespawnMove select 0;
if (_safedist <= 0) then {_safedist = 20};
_pas = PUMPKINRespawnMove select 1;
if (_pas <= 0) then {_pas = _safedist / 2};
_maxdist = PUMPKINRespawnMove select 2;
if (_maxdist < _safedist ) then {_maxdist = _safedist * 5};

_mside = side _man;
_eside = east;
if (_mside == east) then { _eside = west };


_soldiers = (getpos _man) nearObjects ["Man",_safedist];
_vehs = (getpos _man) nearObjects ["LandVehicle",_safedist];
if ((_eside countSide (_soldiers + _vehs)) > 0) then {

	_ang = 0;
	_bestpos = getpos _man;
	_lessfoes = 999999;

	for [{_dist = 0},{_dist <= _maxdist},{_dist = _dist + _pas}] do {

		for [{_ang = 0},{_ang <= 360},{_ang = _ang + 20}] do {

			_pos = [(getpos _man select 0) + ((sin _ang) * _dist), (getpos _man select 1) + ((cos _ang) * _dist)];

			_soldiers = _pos nearObjects ["Man",_safedist];
			_vehs = _pos nearObjects ["LandVehicle",_safedist];
			_unknowns = _soldiers + _vehs;

			_nfoes = _eside countSide _unknowns;
			if (_nfoes < _lessfoes) then {
				if (not (surfaceIsWater [_pos select 0, _pos select 1])) then {
					_bestpos = _pos;
					_lessfoes = _nfoes;
				};
			};
			if (_lessfoes == 0) exitwith { _bestpos = _pos };
		};

		if (_lessfoes == 0) exitwith {};
	};

	if (not (surfaceIsWater [_bestpos select 0, _bestpos select 1])) then { _man setpos _bestpos };

};
