/*
	FILE: fnc_tow.sqf

	Name: tbd_m119_m119_fnc_tow

	Author(s):
		ilbinek

	Description:
		Start towing the M119 howitzer

	Parameters:
		_arty	- object	- M119 to be towed

	Returns:
		Nothing
	
	Examples:
		> [_arty] call tbd_m119_m119_fnc_tow;

	Public:
		No
*/

#include "..\script_component.hpp"

params ["_arty"];

if (!([_arty] call FUNC(canBeTowed))) exitWith {};

// Find the vehicle I need to attach to
// Get pos of the towing point
private _pos = _arty modelToWorld (_arty selectionPosition ["towing_point", "Memory"]);
private _radius = TOW_RADIUS;
private _vehicles = _pos nearEntities _radius;

private _canBeTowed = false;
{
	private _veh = _x;
	private _class = typeOf _veh;
	{
		if (_class regexMatch _x#0) then {
			private _p = _veh modelToWorldVisual [_x#1#0, _x#1#1, _x#1#2];
			if (_p distance _pos < ATTACH_RADIUS) exitWith {
				_canBeTowed = true;
				[QGVAR(tow), [_arty, _veh, _x#1#0, _x#1#1, _x#1#2, _x#2]] call CBA_fnc_serverEvent;
			};
		};
	} forEach GVAR(towingList);
	if (_canBeTowed) exitWith {};
} forEach _vehicles;

_arty setVariable [QGVAR(towed), true, true];
[QGVAR(towed), [_arty], QGVAR(towJIP)] call CBA_fnc_globalEventJIP;
