;============================================================================
; detect.sqs
; ["INIT", [arrayOfFriendlyUnits, arrayOfEnemyUnits], UserScript]
; alimag - 07/2009
;=============================================================================

if (count _this > 3) then {goto "HIDE"}
goto (_this select 0)

;----------------
#INIT
;----------------
_friends = (_this select 1) select 0
_enemies = (_this select 1) select 1
_script = _this select 2
_delay = 2
_distance = 2

{_x setBehaviour "CARELESS";_x setSpeedMode "LIMITED"} forEach _enemies
{_x addEventHandler ["killed", {["KILLED", _this] exec "detect.sqs"}]} forEach _enemies
{_x addEventHandler ["hit", {["HIT", _this] exec "detect.sqs"}]} forEach _enemies
{_x addEventHandler ["fired", {["FIRED", _this] exec "detect.sqs"}]} forEach _friends

; init global var
detectSQS = [false, _friends, _enemies, [], _delay, [], _distance]

; start general detection script
["DETECT"] exec "detect.sqs"

; wait for exit script flag
@ (detectSQS select 0)
[_enemies] exec _script

exit

;----------------
#DETECT
;----------------
_friends = detectSQS select 1
_enemies = detectSQS select 2

#DETECT_LOOP1
	_i = 0
	_n = count _friends
	#DETECT_LOOP2
		_friend = _friends select _i
		{if ((_x knowsAbout _friend > 0.5) && (_x knowsAbout _friend != 1.5) && !(_x in (detectSQS select 5))) then {["COMBAT",_x,"Unit"] exec "detect.sqs"}} forEach _enemies
		_i = _i + 1
	if (_i < _n) then {goto "DETECT_LOOP2"}
	~1
if !(detectSQS select 0) then {goto "DETECT_LOOP1"}
exit

;----------------
#COMBAT
;----------------
_unit = _this select 1
_from = _this select 2
_delay = detectSQS select 4

detectSQS set [5, (detectSQS select 5) + [_unit]]
if !(alive _unit) then {exit}

player globalChat format ["%1 detected by (%2)", _from, _unit]

_unit addEventHandler ["fired", {["COMBAT_FIRED",_unit] exec "detect.sqs"}]
_unit setBehaviour "COMBAT"
_unit lockWp true
_unit setCombatMode "RED"
~_delay
if (alive _unit) then {detectSQS set [0, true]}
exit

;----------------
#COMBAT_FIRED
;----------------
if (detectSQS select 0) then {exit}

_unit = _this select 1
_enemies = detectSQS select 2
_dist = detectSQS select 6
{if (_x distance _unit < _dist) then {["COMBAT",_x,"Friendly gunshot"] exec "detect.sqs"}} forEach _enemies
exit

;----------------
#HIT
;----------------
if (detectSQS select 0) then {exit}

_unit = (_this select 1) select 0
if !(_unit in (detectSQS select 5)) then {["COMBAT", _unit, "Shooter"] exec "detect.sqs"}
exit

;----------------
#KILLED
;----------------
if (detectSQS select 0) then {exit}

_unit = (_this select 1) select 0
_killer = (_this select 1) select 1

; detectable body tag - visibility value seems to low - another object ?
_a = "SkeetDisk" createVehicle [0,0,0]
_a attachTo [_unit, [0.5, 0, 0]]

detectSQS set [3, (detectSQS select 3) + [[_unit, _a,false]]]
_n = count (detectSQS select 3)
if ((_unit in (detectSQS select 5)) && !(detectSQS select 0)) then {player globalChat "Detector neutralized"}
if (_n == 1) then {["BODY"] exec "detect.sqs"}
exit

;----------------
#BODY
;----------------
_enemies = detectSQS select 2

#BODY_LOOP1
	_i = 0
	_n = count (detectSQS select 3)
	#BODY_LOOP2
		_body = ((detectSQS select 3) select _i) select 0
		_flag = ((detectSQS select 3) select _i) select 2
		if (isNull _body) then {if !(_flag) then {((detectSQS select 3) select _i) set [2,true];titleText ["Body hidden","plain down"]};goto "BODY_SKIP"}
		_tag = ((detectSQS select 3) select _i) select 1
		{if ((_x knowsAbout _tag > 0) && !(_x in (detectSQS select 5))) then {_x lockWp true;_x move (getPos _tag);["COMBAT",_x,"Body"] exec "detect.sqs"}} forEach _enemies
		#BODY_SKIP
		_i = _i + 1
	if (_i < _n) then {goto "BODY_LOOP2"}
	~1
if !(detectSQS select 0) then {goto "BODY_LOOP1"}
exit

;----------------
#FIRED
;----------------
if (detectSQS select 0) then {exit}

_grenade = (getPos ((_this select 1) select 0)) nearestObject ((_this select 1) select 4)

; if not grenade exit
if !(((_this select 1) select 1) == "Throw") then {exit}

; determine grenade explosion position
#FIRED_LOOP
	_pos = getPos _grenade
	~0.1
if (alive _grenade) then {goto "FIRED_LOOP"}

_enemies = detectSQS select 2
_impact = "SkeetDisk" createVehicle [0,0,0]
_impact setPos _pos
_dist = detectSQS select 6
{if (_x distance _impact < _dist) then {["COMBAT",_x,"Explosion"] exec "detect.sqs"}} forEach _enemies
deleteVehicle _impact
exit
