summaryrefslogtreecommitdiffstats
path: root/Server/Missions/M_PLAYERS_Attack_Air/m_init.sqf
blob: d874c2e5c52a38a6e54e765cf810b3abec38bb76 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* Players Attack Script (Hybrid) */
Private ["_boundaries","_chopper","_group","_jipHandler","_list","_pilot","_players","_ran","_ranDir","_runFor","_ranPos","_target","_targets","_turrets","_uniqueIndex","_uniqueLabel","_vehicle"];

_uniqueLabel = _this select 0;
_uniqueIndex = _this select 1;
_runFor = _this select 2;

//--- Retrieve the players on a side.
_players = [];
_list = Format ["WFBE_%1TEAMS",_runFor] Call GetNamespace;
{
	if !(isNil '_x') then {
		if (side _x == _runFor) then {
			if (isPlayer(leader _x) && alive(leader _x)) then {_players = _players + [leader _x]};
		};
	};
} forEach _list;

//--- Ensure that the team already has a town.
if (count _players == 0) exitWith {
	[Format['_WFBE_M_RUNNINGMISSIONS_%1',_runFor],((Format['_WFBE_M_RUNNINGMISSIONS_%1',_runFor]) Call GetNamespace) - 1, true] Call SetNamespace;
};

//--- Radio the designated team that a new mission is available.
['NewMissionAvailable','',_runFor] Spawn SideMessage;

//--- Define the chopper spawn position.
_boundaries = "WFBE_BOUNDARIESXY" Call GetNamespace;

_ranPos = [];
_ranDir = [];

_bd = 'WFBE_BOUNDARIESXY' Call GetNamespace;
if !(isNil '_bd') then {
	_ranPos = [
		[0+random(200),0+random(200),400+random(200)],
		[0+random(200),_bd-random(200),400+random(200)],
		[_bd-random(200),_bd-random(200),400+random(200)],
		[_bd-random(200),0+random(200),400+random(200)]
	];
	_ranDir = [45,145,225,315];
} else {
	_ranPos = [[0+random(200),0+random(200),400+random(200)],[10000+random(200),0+random(200),400+random(200)]];
	_ranDir = [45,315];
};

_ran = round(random((count _ranPos)-1));

//--- This script use PMC units.
_chopper = "Ka60_PMC";
_group = createGroup resistance;
_vehicle = createVehicle [_chopper,(_ranPos select _ran), [], (_ranDir select _ran), "FLY"];
_vehicle addEventHandler ['Killed',{[_this select 0,_this select 1,resistance] Spawn UnitKilled}];

//--- Man the chopper.
_pilot = ["Soldier_Pilot_PMC",_group,[0,0,0],resistance] Call CreateMan;
_pilot moveInDriver _vehicle;
_pilot = ["Soldier_Pilot_PMC",_group,[0,0,0],resistance] Call CreateMan;
_pilot moveInGunner _vehicle;

//--- Man the extra turrets.
_config = configFile >> "CfgVehicles" >> _chopper >> "Turrets";
_turrets = [_config] call BIS_fnc_returnVehicleTurrets;
if (count _turrets > 0) then {[_turrets, [], _vehicle, "Soldier_Pilot_PMC", _group] call SpawnTurrets};

//--- Warn the client.
WFBE_M_PLAYERS_Attack_Air = [_runFor,'CLTFNCM_PLAYERS_ATTACK_AIR',[_vehicle,_uniqueLabel,_uniqueIndex]];
publicVariable 'WFBE_M_PLAYERS_Attack_Air';
if (isHostedServer || local player) then {[_runFor,'CLTFNCM_PLAYERS_ATTACK_AIR',[_vehicle,_uniqueLabel,_uniqueIndex]] Spawn HandlePVF};
_jipHandler = [_vehicle,_uniqueLabel,_uniqueIndex] Spawn {
	Private ['_chopper','_oldPlayerList','_runFor','_uniqueLabel','_uniqueIndex','_update'];
	_chopper = _this select 0;
	_uniqueLabel = _this select 1;
	_uniqueIndex = _this select 1;
	
	while {true} do {
		_oldPlayerList = playableUnits;
		sleep 15;
		_update = false;
		{
			if !(_x in _oldPlayerList) then {_update = true};
		} forEach playableUnits;
		
		if (_update) then {
			WFBE_M_PLAYERS_Attack_Air = [_runFor,'CLTFNCM_PLAYERS_ATTACK_AIR',[_chopper,_uniqueLabel,_uniqueIndex]];
			publicVariable 'WFBE_M_PLAYERS_Attack_Air';
			if (isHostedServer || local player) then {[_runFor,'CLTFNCM_PLAYERS_ATTACK_AIR',[_chopper,_uniqueLabel,_uniqueIndex]] Spawn HandlePVF};
		};
	};
};

//--- Move the chopper toward the target.
while {true} do {
	sleep 20;
	
	_players = [];
	{
		if !(isNil '_x') then {
			if (side _x == _runFor) then {
				if (isPlayer(leader _x) && alive(leader _x)) then {_players = _players + [leader _x]};
			};
		};
	} forEach _list;
	
	if (count _players == 0 || !alive _vehicle || !alive(driver _vehicle) || !canMove _vehicle) exitWith {};
	
	_targets = [_vehicle, _players] Call SortByDistance;
	_target = _targets select 0;
	{
		if (vehicle _x == _x && !((vehicle _x) isKindOf "Air")) exitWith {_target = _x};
	} forEach _targets;
	
	if !(someAmmo _vehicle) then {[_vehicle,resistance] Call RearmVehicle};
	
	_vehicle doMove (getPos _target);
	{_group reveal _x} forEach (vehicle _target nearEntities 200);
};

if (alive _vehicle || count _players == 0) then {_vehicle setDammage 1};
{if (alive _x || count _players == 0) then {_x setDammage 1}} forEach (units _group);

//--- Kill the spawned thread if it still runs.
if !(scriptDone _jipHandler) then {terminate _jipHandler};

//--- Wait for the client to end up his sync.
sleep 35;

[Format['_WFBE_M_RUNNINGMISSIONS_%1',_runFor],((Format['_WFBE_M_RUNNINGMISSIONS_%1',_runFor]) Call GetNamespace) - 1, true] Call SetNamespace;