; ********************************************************************************
; ** Operation Flashpoint Script File
; ********************************************************************************
; ** Sintaxis
; ** [heliW2,[grpW1],[helipad1,0, true], [helipad2,true], 2,"accion.sqs", [pos1, pos2]] exec "Ghelicopter.sqs"
; ********************************************************************************
; ** Elemento Nº 1 de cuarto parametro es:
; **             nill: Para que el heli recoja en la posicion del grupo
; **    nombre_objeto: Para que el heli recoja en la posicion del objeto
; ********************************************************************************

;//--- Vehiculo transportador
_heli = _this select 0

;//--- Vector de grupo a transportar
_grps = _this select 1

;//--- Objeto/referencia coordenedas de recogida y tiempo de espera (0: ilimitado)
_intro = _this select 2

;//--- Objeto/referencia coordenedas de insercion de grupos
_out = _this select 3

;//--- Modalidad de insercion (0: desembarcar, 1:Rapel, 2: Paracaidas)
_modo = _this select 4

;//--- Script de accion/grupo insertado
_acc  = _this select 5

;//--- Secuencia de posiciones intermedias para ruta de aproximación
_ruta  = _this select 6

;//--- Datos para posicionar cuerda/rapel
_ang = -90.00
_ang2= 90.00
_distR = 1.20
_lngSoporte = 0.00
_abajo = 1.5
_heli setskill 1
_altrapel = 12

#inicio
? not(local server): goto "salida"

_primera = true
_altdrop = 100

_in = _intro select 0
_wait = _intro select 1
_cover = false
? count _intro == 3: _cover = _intro select 2

_cover2 = _out select 1
_out = _out select 0

? _in distance _out < 100: goto "salida"

;----------------------------------
;--- Determinar engine
;----------------------------------
_simulador = nill

_OFP  = "scalar bool array string 0xfcffffef"
_ArmA = "scalar bool array string 0xe0ffffef"

_param = _ArmA
? format["%1",_simulador] == _OFP  : _param = _OFP
? format["%1",_simulador] == _ArmA : _param = _ArmA

_orden = 0
_trip = count(crew _heli)

_posfin = [getpos _heli select 0, getpos _heli select 1, 0]
_dest = "HeliHempty" createvehicle _posfin
_alt = 30

#ciclo
_grp = _grps select _orden
? _primera×·ˆ and ((getpos _heli) select 2) > 10: goto "cargado"
? _heli distance (leader _grp) > 100: goto "recoge"

#cargado
{_x assignascargo _heli} foreach units _grp
{_x moveincargo _heli} foreach units _grp
goto "a_destino"

;-----------------------------------------------------
#recoge
? not alive(_heli): goto "salida"
~1
? format["%1",_in] == _param :_dest setpos getpos (leader _grp); goto "recoge_1"
_dest setpos getpos _in

#recoge_1
_heli domove getpos _dest
? _heli distance _dest < 100: _heli flyinheight 0
? _heli distance _dest > 100: goto "recoge"
_heli setspeedmode "LIMITED"
_heli land "get in"

#desciende1
? not alive(_heli): goto "salida"
? ((getpos _heli) select 2) < 10: goto "carga"
~1
_heli flyinheight ((getpos _heli) select 2) - 0.5
goto "desciende1"

;-----------------------------------------------------
#carga
_heli flyinheight -1
{_x assignascargo _heli} foreach units _grp
@(getpos _heli select 2) <= 0.5
(units _grp) ordergetin true
_tiempo = _wait
_salto = "carga_01"
? _cover : goto "descarga_0"

#carga_01
? not alive(_heli): goto "salida"
? count(crew _heli) == (count(units _grp) + _trip): goto "a_destino"
~1
_viv = 0
{if (alive(_x)) then {_viv = _viv + 1}} forEach units _grp
? _viv == 0 : goto "retoma_1"
? _wait == 0: goto "carga_01"
_tiempo = _tiempo - 1
? (_tiempo > 0): goto "carga_01"
? (count(crew _heli) > _trip): goto "carga_01"
? _heli distance (leader _grp) < 100: goto "carga_01"

;_heli flyinheight _alt
{UnassignVehicle _x} foreach units _grp
goto "retoma_1"

;-----------------------------------------------------
#a_destino
_heli flyinheight _alt
_heli setspeedmode "NORMAL"
_wpmax = count _ruta
_wpact = 0

#a_destino_0
? _wpmax > 0:  _heli domove getpos (_ruta select _wpact)

#a_destino_1
? not alive(_heli): goto "salida"
~1
? _wpmax == 0: _heli domove getpos _out; goto "a_destino_2"
? (_heli distance (_ruta select _wpact)) > 150: goto "a_destino_1"
_wpact = _wpact + 1
? _wpact < _wpmax: goto "a_destino_0"

_wpmax = 0
_heli domove getpos _out

#a_destino_2
? (_modo in [0,3]) and (_heli distance _out) < 150: goto "despliega"
? (_modo == 1) and (_heli distance _out) < 150: _heli flyinheight _altrapel
? (_modo == 1) and (_heli distance _out) <  50: goto "rapel"
? (_modo == 2) and (_heli distance _out) < 400: goto "paraca"
goto "a_destino_1"

;-----------------------------------------------------
#paraca
_alt = ((getpos _heli) select 2)

#paraca_1
? not alive(_heli): goto "salida"
_heli flyinheight _altdrop
~1
? ((getpos _heli) select 2) < _altdrop: goto "paraca_1"
~3
_max = (count units _grp) - 1

#salto
~1
_quien = ((units _grp) select _max)
_quien action ["eject", _heli]
_quien setvelocity velocity(_heli)
unassignvehicle _quien
_max = _max - 1
? _max >= 0: goto "salto"
~5
_heli flyinheight _alt
goto "retoma"

;-----------------------------------------------------
#rapel
_heli setspeedmode "LIMITED"

#rapel_ini
? not alive(_heli): goto "salida"
_heli domove getpos _out
~0.5
_heli flyinheight _altrapel
? _heli distance _out > 100: goto "rapel_ini"
? getpos _heli select 2 > _altrapel or (speed _heli > 5): goto "rapel_ini"
? speed _heli <= 5 : goto "rapel_1"

_vx = ((velocity _heli) select 0) * 0.55
_vy = ((velocity _heli) select 1) * 0.55
_vz = (velocity _heli) select 2
_heli setvelocity [_vx, _vy, _vz]
goto "rapel_ini"

#rapel_1
_vec = units _grp
_can = count _vec
_pos = 0
_InitCuerda = false
~3

#rapel_2
_man  = _vec select _pos
_donde = getpos _heli
_dx    = _donde select 0
_dy    = _donde select 1
_alt   = _donde select 2
? not(_InitCuerda): _cuerda = "FSM_FastRope" createvehicle [0,0,0]
_InitCuerda = true

#rapel_2_1
_dir = direction _heli
_donde = getpos _heli
_dx    = _donde select 0
_dy    = _donde select 1
_dz    = _donde select 2

;############################################
_ang = 320.00
_ang2= 40.00
_distR = 2.00
_lngSoporte = 0.00
_abajo = 1.50
;############################################

_dif = 1
? _ang < 0 : _dif = -1
_dirC = (_dir + _ang)
_dirC2= (_dir + _ang2)
_pxC = _dx + (_distR * sin(_dirC))
_pyC = _dy + (_distR * cos(_dirC))

_cuerda setdir (_dirC)
_cuerda setpos [_pxC, _pyC, _dz + _abajo]
_cuerda setvectorUP [0,0,1]

#rapel_3
unassignvehicle _man
_man setdir random(360)
_man setpos [ _dx, _dy, _alt]

#rapel_4
_heli flyinheight _altrapel
_man switchmove "Crew"

_dir = direction _heli
_donde = getpos _heli
_dx    = _donde select 0
_dy    = _donde select 1
_dz    = _donde select 2
if (_dz > _altrapel) then {_heli setpos [_dx, _dy, _altrapel]}

_dirC = (_dir + _ang)
_pxC = _dx + (_distR * sin(_dirC))
_pyC = _dy + (_distR * cos(_dirC))
_cuerda setdir (_dirC2 + 90)
_cuerda setpos [_pxC, _pyC, _dz  + _abajo]
_cuerda setvectorUP [0,0,1]

_height = (getpos _man select 2)
_height = _height - 0.13

_dirC = (_dir + _ang)
_Ndir = ((getdir _man) + (_height * 0.2))
_pxM = _dx + ((_distR + _lngSoporte) * sin(_dirC))
_pyM = _dy + ((_distR + _lngSoporte) * cos(_dirC))
_man setdir _Ndir
_man setpos [ _pxM, _pyM, _height]

_cuerda2 setdir _Ndir
_cuerda2 setpos [_pxM, _pyM, _height]
_cuerda2 setvectorUP [0,0,1]

_man setvelocity [0,0,0.25]
~0.0035

? not(alive _man) : _man switchmove ""; goto "rapel_99"
? _height > 1.7 : goto "rapel_4"

_nx = getpos _man select 0
_ny = getpos _man select 1
_man switchMove "AcrgPknlMstpSnonWnonDnon_AmovPercMstpSnonWnonDnon_getOutHigh"

_man setpos [_pxM, _pyM, 0]
_man move [_nx+1+random(30)-15, _ny+1+random(30)-15]

#rapel_99
_can = _can - 1
_pos = _pos + 1
? _can > 0: goto "rapel_2"

#rapel_99_1
~1
_cuerda setvelocity [random 1, random 1, 1]
_cuerda setvectorUP [random 1, random 1, random 1]

;####################################### Suelta cuerdas
#rapel_99_2
_donde = getpos _heli
_dx1   = _donde select 0
_dy1   = _donde select 1
_dz1   = _donde select 2
if (_dz1 > _altrapel) then {_heli setpos [_dx1, _dy1, _altrapel]}
_cuerda setpos [_pxC, _pyC, _dz - 0.2]
_vec = vectorUP _cuerda
_dz = (position _cuerda) select 2
~0.0000010
? _dz > 0 : goto "rapel_99_2"
deletevehicle _cuerda
deletevehicle _cuerda2

;####################################### Avance momentaneo
_heli flyinheight 30
_avX = _dx + (500 * sin(getdir _heli))
_avY = _dy + (500 * cos(getdir _heli))
_heli domove [_avX, _avY, 0]
@unitready _heli
goto "retoma"

;-----------------------------------------------------
#despliega
_heli setspeedmode "LIMITED"
? _modo == 0 : _heli land "get out"
_salto = "descarga_2"

#desciende2
? not alive(_heli): goto "salida"
if (_modo == 0) then {_heli flyinheight ((getpos _heli) select 2) - 0.5} else {_heli flyinheight 3}
? _modo == 0 : _heli flyinheight ((getpos _heli) select 2) - 0.5
? _modo == 0 and ((position _heli) select 2) <= 1 : goto "descarga"
? _modo == 3 and ((position _heli) select 2) <= 3 : goto "descarga"
~0.5
goto "desciende2"

#descarga
if (_modo == 0) then {_heli flyinheight 0} else {_heli flyinheight 2}
? not _cover2 or _modo == 3 : goto "descarga_2"

#descarga_0
_heli flyinheight 0
_cansmoke = 2

#descarga_1
_sep = 15

? daytime < 5.5 or daytime > 18.00: goto "descarga_2"

_px = getpos _heli select 0
_py = getpos _heli select 1
_pz = getpos _heli select 2
_dir = random(360)
_px = _px + (_sep * sin(_dir))
_py = _py + (_sep * cos(_dir))
_humo = "smokeshell" createvehicle [_px, _py, _pz]
_cansmoke = _cansmoke - 1
? _cansmoke > 0: goto "descarga_1"
goto _salto

#descarga_2
_pos = 0
_can = count(units _grp)
? _modo == 0 : _heli domove position _heli; goto "descarga_3"

_desX = (position _heli select 0) + (150 * sin(direction _heli))
_desY = (position _heli select 1) + (150 * cos(direction _heli))
_lado = -1

#descarga_3
? _modo == 0 : goto "descarga_31"
? speed _heli <= 20 : goto "descarga_31"
_vx = ((velocity _heli) select 0) * 0.9
_vy = ((velocity _heli) select 1) * 0.9
_vz = (velocity _heli) select 2
_heli setvelocity [_vx, _vy, _vz]
_heli flyinheight 3
goto "descarga_32"

#descarga_31
_lado = _lado * (-1)
if (_modo == 0) then {_heli flyinheight 0} else {_heli move [_desX, _desY]}
_xdir = (direction _heli) + 50
? _lado < 0 : _xdir = (direction _heli) - 50

_Lx = (position _heli select 0) + (2 * sin(_xdir))
_Ly = (position _heli select 1) + (2 * cos(_xdir))
_Lz = (position _heli select 2) - 0.5

_man = (units _grp) select _pos
_man action ["EJECT", _heli]
unassignvehicle _man
_man setunitpos "middle"

_man setpos [_Lx, _Ly, _Lz]
_man setdir _xdir

_pos = _pos + 1
_can = _can - 1

#descarga_32
~0.40
? _can >= 0: goto "descarga_3"

_desX = (position _heli select 0) + (150 * sin(direction _heli))
_desY = (position _heli select 1) + (150 * cos(direction _heli))
_heli move [_desX, _desY]
_heli flyinheight _alt
~3

#retoma
? _acc != "": [_grp] exec _acc

#retoma_1
_heli flyinheight _alt
_heli setspeedmode "NORMAL"
_primera = false
_orden = _orden + 1
? _orden < (count _grps): goto "ciclo"

;-----------------------------------------------------
#retorna
? not alive(_heli): goto "salida"
_dest setpos _posfin
_heli domove getpos _dest
~1
? _heli distance _dest > 100: goto "retorna"
_heli land "land"

;-----------------------------------------------------
#salida
deletevehicle _dest
exit