? !(local player):exit

_rc = _this
MAP_resp_end = false

_i = random 999999
_i = (_i+.5) - (_i+.5)%1

MAP_endRespCam = false
_MA = []
_find = {private["_c","_r", "_v", "_a"]; _a=_this select 0; _v = _this select 1; _r = -1; if (_v in _a) then {_c = count _a - 1; while "_c>=0" do {if (_v == _a select _c) then {_r = _c; _c = -0.5} else {_c = _c - 1}}}; _r}

_mags = {private["_c", "_i","_u", "_w", "_a"]; _c = 0; _u = []; _w = ""; while "_c < count _this" do {_a = false; _i =  _this select _c; _w = _i; if (_i in MAP_respRLAddon1) then {_w = MAP_respRLAddon1 select 0; _a = true; if (_N ammo _w > 0) then {_MA=_MA+[_i]} else {if ("_x in _u" count MAP_respRLAddon1 == 0) then {_u = _u + [_i]} else {_MA=_MA+[_i]}}}; if (_i in MAP_respRLAddon2) then {_w = MAP_respRLAddon2 select 0; _a = true; if (_N ammo _w > 0) then {_MA=_MA+[_i]} else {if ("_x in _u" count MAP_respRLAddon2 == 0) then {_u = _u + [_i]} else {_MA=_MA+[_i]}}};  if (_i in MAP_respRLAddons) then {_w = MAP_respRLAddons select (([MAP_respRLAddons, _i] call _find) - 1); _a = true}; if (!_a) then {_w = _i}; if (!(_i in MAP_respRocketLauncherAmmo || _i in MAP_respRLAddons) || _N ammo _w > 0) then {_MA=_MA+[_i]} else {if !(_i in _u) then {_u = _u + [_i]} else {_MA=_MA+[_i]}}; _c=_c+1}; _MA}

#start
_N = Player
Player SetCaptive false

#l1
~0.2
? ! (player in MAP_resp): call format ["MAP_respUnit_%1 = player; publicVariable {MAP_respUnit_%1}", _i]; call format [{"Civilian" createUnit [position MAP_respDummy, group MAP_respDummy, "MAP_respDummy_%1 = this; MAP_resp = MAP_resp + [MAP_respUnit_%1]"]}, _i]; _i exec "MAP_respawn\MAP_respawnDelete.sqs"
? alive Player: goTo "l1"

_rc=_rc - 1
~(Random 1.5)
call format ["MAP_respUnit_%1 = player; publicVariable {MAP_respUnit_%1}", _i]
call format [{"Civilian" createUnit [position MAP_respDummy, group MAP_respDummy, "MAP_respDummy_%1 = this; MAP_resp = MAP_resp - [MAP_respUnit_%1]"]}, _i]
_i exec "MAP_respawn\MAP_respawnDelete.sqs"

titleCut["","black out",3]

#l2
~0.2
? !alive Player: goTo "l2"

player setCaptive True

? _rc < 0 || (side player) countSide MAP_resp < 1 || "typeOf _x in MAP_respMedics" count MAP_resp == 0 && count MAP_respMedics > 0 && !MAP_respMedicsAutoHeal: goTo "end"

_N exec "MAP_respawn\MAP_respawnCamera.sqs"

titleCut["","black in",3]
_W = direction _N
_t = time
_u = 9

#l3
_p = position _N
_u = _u + 1
_t2 = MAP_respHealMaxTime +_t - time
? MAP_respHealMaxTime > 0 && _u == 10: titleText [format ["Call for help or you are dead in %1 seconds!", (_t2+.5) - (_t2+.5)%1], "plain"]; _u = 0
? MAP_respHealMaxTime > 0 && (time - MAP_respHealMaxTime > _t) || count MAP_respMedics > 0 && "typeOf _x in MAP_respMedics" count MAP_resp == 0 && !MAP_respMedicsAutoHeal || position _N select 2 < -0.8: removeAllWeapons player; goTo "end"
? vehicle player != player: player action ["eject", vehicle player]
~0.5
_M = nearestObject [(_p select 0) + (sin _W * 2) , (_p select 1) + (cos _W * 2), 0]
_W = _W + 45
? !alive player: titlecut["","black out",3] ; goto "l2"
? !(typeOf player in MAP_respMedics && count MAP_respMedics > 0 && MAP_respMedicsAutoHeal) && ((!(typeOf _M in MAP_respMedics) && count MAP_respMedics > 0) || "man" counttype [_M] != 1 || _M == Player || Side _M != Side Player || damage _M > 0.85): goTo "l3"

? typeOf player in MAP_respMedics && MAP_respMedicsAutoHeal: goTo "w"

call format ["MAP_respUnit_%1 = player; MAP_respHealer_%1 = _m; publicVariable {MAP_respUnit_%1}; publicVariable {MAP_respHealer_%1}", _i]
call format [{"Civilian" createUnit [position MAP_respDummy, group MAP_respDummy, "MAP_respDummy_%1 = this; MAP_respUnit_%1 switchMove {LyingToTreatedLying}; MAP_respUnit_%1 playMove {LyingToTreatedLying}; MAP_respHealer_%1 switchMove {medic}; MAP_respHealer_%1 playMove {medic}; MAP_resp = MAP_resp + [MAP_respUnit_%1]"]}, _i]
_i exec "MAP_respawn\MAP_respawnDelete.sqs"
goTo "w2"

#w
call format ["MAP_respUnit_%1 = player; publicVariable {MAP_respUnit_%1}", _i]
call format [{"Civilian" createUnit [position MAP_respDummy, group MAP_respDummy, "MAP_respDummy_%1 = this; MAP_respUnit_%1 switchMove {LyingToTreatedLying}; MAP_respUnit_%1 playMove {LyingToTreatedLying}; MAP_resp = MAP_resp + [MAP_respUnit_%1]"]}, _i]
_i exec "MAP_respawn\MAP_respawnDelete.sqs"

#w2
_GW = weapons _N
_MAall = magazines _N
_primW = primaryWeapon _N

_MA = []
_MA = _MAall call _mags

removeAllWeapons player

~0.1

"player addMagazine _x" forEach _MA
~random 1
"player addWeapon _x" forEach _GW
~0.1
? _primW != "": player selectWeapon _primW

~0.2

player setPos position _N
deleteVehicle _N

MAP_endRespCam = true

? _rc > 1: titleText [format ["You still have %1 respawns", _rc], "plain"]
? _rc == 1: titleText ["This was your last respawn!", "plain"]

~2.7
goto "start"

#end
MAP_respBody = _N
call format ["MAP_respUnit_%1 = player; publicVariable {MAP_respUnit_%1}", _i]
call format [{"Civilian" createUnit [position MAP_respDummy, group MAP_respDummy, "MAP_respDummy_%1 = this; [MAP_respUnit_%1] join grpNull; MAP_respUnit_%1 setPos position MAP_respDummy"]}, _i]
_i exec "MAP_respawn\MAP_respawnDelete.sqs"
MAP_endRespCam = true
? MAP_noKEG: _N exec "MAP_respawn\MAP_respawnCamera.sqs"; goTo "noKEG"
[0,0,"SeaGull" createVehicle [getpos _N select 0,getpos _N select 1,10]] exec "MAP_respawn\KEG_spectScript.sqs"

#noKEG
~ 6 + random 2
_N = Player
? ! (player in MAP_resp): goTo "endloop"; 
call format ["MAP_respUnit_%1 = player; publicVariable {MAP_respUnit_%1}", _i]
call format [{"Civilian" createUnit [position MAP_respDummy, group MAP_respDummy, "MAP_respDummy_%1 = this; MAP_Resp = MAP_Resp - [MAP_respUnit_%1]"]}, _i]
(_i) exec "MAP_respawn\MAP_respawnDelete.sqs"

#endLoop
~5
player setPos position MAP_respDummy
? MAP_resp_end: exit
? alive Player: goTo "endLoop"

@alive Player
removeAllWeapons player
player switchMove "LyingDying"
deleteVehicle _N
player setCaptive true
~1
_N = Player
goTo "endLoop"