Page 1 of 1

[Script] AI group hunt

Posted: Mon Aug 26, 2019 5:43 pm
by laverniusregalis
This little script takes advantage of the relatively new BIS_fnc_stalk; when called, the given unit's group will locate the nearest hostile group within the chosen radius and begin stalking them. fnc_stalk remains unchanged, and will cause the group to move to/near the target's location, updating every so often according to the refresh parameter.

Code: Select all

// hunt: nul = [unit, radius] execVM "hunt.sqf";
// Selected unit's group will hunt down the nearest enemy group

params [
	["_unit", objNull, [objNull]] // unit to act as hunter
	,["_radius", 2000, [0]] // radius to search for enemy group
	,["_refresh", 15, [0]] // hunt refresh rate in seconds, minimum 5
	,["_accuracy", 0, [0]] // hunt precision in meters; >0 to make AI patrol around enemy location
	,["_cond", {false}] // hunt stop condition
	,["_dest", 0] // hunter ending destination
];

private _closest = objNull;

private _enemySides = [side group _unit] call BIS_fnc_enemySides;

private _nearEnemies = allUnits select { (_x distance _unit) < _radius && (side _x) in _enemySides};

private _closestdist = _radius+1;
{
	if (_x distance _unit < _closestdist) then {
		_closest = _x;
		_closestdist = _x distance _unit;
	};
} forEach _nearEnemies;

_closest;

[group _unit, group _closest, _refresh, _accuracy, _cond, _dest] call BIS_fnc_stalk;