; Original script by Pennywise and Hudson
; Adapted (slightly) by Footmunch

_array = _this select 0
_plane = _array Select 0
_ammoname = _array Select 4

; Only work for the listed ammo
?(_ammoname != "BWC_kukri") : goto "Exit"

; MP check
?(!(local _plane)):goto "Exit"

; Get the missile object
_ammo = nearestobject [_plane,_ammoname]

_target = objNull

#ScanForTarget

~.03

?(!alive _ammo):goto "Exit"

_curPos = getPos _ammo 

_X = (_curPos select 0) + (sin (direction _ammo)) * 30
_Y = (_curPos select 1) + (cos (direction _ammo)) * 30

?(alive _ammo):_target = nearestobject [[_X, _Y, _curPos select 2],""]

; IFF
?(side _plane==side _target): goto "ScanForTarget"

; Check that the target has a crew (gets rid of 'objects')
?((count crew _target) == 0):goto "ScanForTarget"

; If we've got a plane or a helicopter
?("Plane" countType [_target] > 0):goto "CheckDistance"
?("Helicopter" countType [_target] > 0):goto "CheckDistance"

; Keep going till the missile explodes
?(alive _ammo):goto "ScanForTarget"

goto "Exit"

#CheckDistance
~.01
_dist = _target distance _ammo

; Wait until we're within 20 metres
?(_dist <= 20):goto "Detonate"

?(alive _ammo):goto "CheckDistance"

goto "Exit"

#Detonate
; Get a location 5 metres in front of the missile
_curPos = getPos _ammo
_X = (_curpos select 0) + ((Sin (direction _ammo)) * 5)
_y = (_curpos select 1) + ((Cos (direction _ammo)) * 5)

; Create the missile detonator
"BWCProxDetonator" createvehicle [_X, _Y, (_curPos select 2)]

#Exit
exit