summaryrefslogtreecommitdiffstats
path: root/Server/Functions/Server_BuyUnit.sqf
blob: e028685a2f0aaf6243933ae5f3c9a663ed06eb27 (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
Private ["_building","_built","_config","_crew","_direction","_dir","_distance","_factoryType","_factoryPosition","_gbq","_id","_index","_isVehicle","_longest","_position","_queu","_queu2","_ret","_side","_sideText","_soldier","_team","_turrets","_type","_unitType","_vehicle","_waitTime"];
_id = _this select 0;
_building = _this select 1;
_unitType = _this select 2;
_side = _this select 3;
_team = _this select 4;
_isVehicle = _this select 5;

_sideText = str _side;

if (!(alive _building)||(isPlayer (leader _team))) exitWith {
	_gbq = (_team getVariable "queue") - _id;
	_team setVariable ["queue",_gbq];
	if !(alive _building) then {diag_log Format ["[WFBE (INFORMATION)][frameno:%2 | ticktime:%3] Server_BuyUnit: Canceled unit '%1', the factory is destroyed.",_unitType,diag_frameno,diag_tickTime]};
	if (isPlayer (leader _team)) then {diag_log Format ["[WFBE (INFORMATION)][frameno:%3 | ticktime:%4] Server_BuyUnit: Canceled unit '%1', Player %2 has replaced the AI Team leader.",_unitType, name (leader _team),diag_frameno,diag_tickTime]};
};

diag_log Format["[WFBE (INFORMATION)][frameno:%4 | ticktime:%5] Server_BuyUnit: %1 Team '%2' has purchased a %3",_side,_team,_unitType,diag_frameno,diag_tickTime];

_queu = _building getVariable "queu";
if (isNil "_queu") then {_queu = []};
_queu = _queu + [_id select 0];
_building setVariable ["queu",_queu,true];

_type = typeOf _building;
_index = (Format ["WFBE_%1STRUCTURENAMES",_sideText] Call GetNamespace) find _type;
_distance = (Format ["WFBE_%1STRUCTUREDISTANCES",_sideText] Call GetNamespace) select _index;
_direction = (Format ["WFBE_%1STRUCTUREDIRECTIONS",_sideText] Call GetNamespace) select _index;
_factoryType = (Format ["WFBE_%1STRUCTURES",_sideText] Call GetNamespace) select _index;
_waitTime = (_unitType Call GetNamespace) select QUERYUNITTIME;
_position = [getPos _building,_distance,getDir _building + _direction] Call GetPositionFrom;
_longest = Format ["WFBE_LONGEST%1BUILDTIME",_factoryType] Call GetNamespace;

_ret = 0;
_queu2 = [0];

if (count _queu > 0) then {
	_queu2 = _building getVariable "queu";
};

while {_id select 0 != _queu select 0} do {
	sleep 4;
	_ret = _ret + 4;
	_queu = _building getVariable "queu";
	
	if (!(alive _building)||(isNull _building)||(isPlayer (leader _team))) exitWith {
		_gbq = (_team getVariable "queue") - _id;
		_team setVariable ["queue",_gbq];
		_queu = _building getVariable "queu";
		_queu = _queu - [_queu select 0];
		_building setVariable ["queu",_queu,true];
		if !(alive _building) then {diag_log Format ["[WFBE (INFORMATION)][frameno:%2 | ticktime:%3] Server_BuyUnit: Canceled unit '%1', the factory is destroyed.",_unitType,diag_frameno,diag_tickTime]};
		if (isPlayer (leader _team)) then {diag_log Format ["[WFBE (INFORMATION)][frameno:%3 | ticktime:%4] Server_BuyUnit: Canceled unit '%1', Player %2 has replaced the AI Team leader.",_unitType, name (leader _team),diag_frameno,diag_tickTime]};
	};
	
	if (_queu select 0 == _queu2 select 0) then {
		if (_ret > _longest) then {
			if (count _queu > 0) then {
				_queu = _building getVariable "queu";
				_queu = _queu - [_queu select 0];
				_building setVariable ["queu",_queu,true];
			};	
		};
	};
	if (count _queu != count _queu2) then {
		_ret = 0;
		_queu2 = _building getVariable "queu";
	};
};

sleep _waitTime;

_queu = _building getVariable "queu";
_queu = _queu - [_id select 0];
_building setVariable ["queu",_queu,true];

if (!(alive _building)||(isPlayer (leader _team))) exitWith {
	_gbq = (_team getVariable "queue") - _id;
	_team setVariable ["queue",_gbq];
	if !(alive _building) then {diag_log Format ["[WFBE (INFORMATION)][frameno:%2 | ticktime:%3] Server_BuyUnit: Canceled unit '%1', the factory is destroyed.",_unitType,diag_frameno,diag_tickTime]};
	if (isPlayer (leader _team)) then {diag_log Format ["[WFBE (INFORMATION)][frameno:%3 | ticktime:%4] Server_BuyUnit: Canceled unit '%1', Player %2 has replaced the AI Team leader.",_unitType, name (leader _team),diag_frameno,diag_tickTime]};
};

if (_unitType isKindOf "Man") then {
	_soldier = [_unitType,_team,_position,_side] Call CreateMan;
	[_sideText,'UnitsCreated',1] Call UpdateStatistics;
} else {
	_crew = Format ["WFBE_%1SOLDIER",_sideText] Call GetNamespace;
	if (_unitType isKindOf "Tank") then {_crew = Format ["WFBE_%1CREW",_sideText] Call GetNamespace};
	if (_unitType isKindOf "Air") then {_crew = Format ["WFBE_%1PILOT",_sideText] Call GetNamespace};
	_vehicle = [_unitType,_position,_side,true] Call CreateVehi;
	_factoryPosition = getPos _building;
	_dir = -((((_position select 1) - (_factoryPosition select 1)) atan2 ((_position select 0) - (_factoryPosition select 0))) - 90);
	_vehicle setDir _dir;
	_vehicle setVelocity [0,0,-1];
	//--- AI Can fly.
	if (_vehicle isKindOf "Air") then {
		_vehicle flyInHeight 120;
		if (_vehicle isKindOf "Plane") then {
			_vehicle setPos [_position select 0,_position select 1,1500];
			_vehicle setVelocity [sin _dir * 250,cos _dir * 250,0];
		};
	};

	emptyQueu = emptyQueu + [_vehicle];
	_vehicle Spawn HandleEmptyVehicle;
	if (_vehicle distance (leader _team) < 200) then {(units _team) allowGetIn true;_team addVehicle _vehicle};
	
	/* Clear the vehicle */
	clearWeaponCargo _vehicle;
	clearMagazineCargo _vehicle;
	
	_soldier = [_crew,_team,_position,_side] Call CreateMan;
	[_soldier] allowGetIn true;
	[_soldier] orderGetIn true;
	if (_unitType in ('WFBE_BALANCEDUNITS' Call GetNamespace) && paramBalancing) then {[_vehicle] Spawn BalanceInit};
	_soldier assignAsDriver _vehicle;
	_soldier moveInDriver _vehicle;
	[_sideText,'VehiclesCreated',1] Call UpdateStatistics;
	_built = 1;
	if (_isVehicle select 1) then {
		_soldier = [_crew,_team,_position,_side] Call CreateMan;
		[_soldier] allowGetIn true;
		[_soldier] orderGetIn true;
		_soldier assignAsGunner _vehicle;
		_soldier moveInGunner _vehicle;
		_built = _built + 1;
	};
	if (_isVehicle select 2) then {
		if (vehicle leader _team == leader _team && leader _team distance _vehicle < 200 && alive leader _team) then {
			[leader _team] allowGetIn true;
			[leader _team] orderGetIn true;
			(leader _team) assignAsCommander _vehicle;
			(leader _team) moveInCommander _vehicle;
		} else {
			_soldier = [_crew,_team,_position,_side] Call CreateMan;
			[_soldier] allowGetIn true;
			[_soldier] orderGetIn true;
			_soldier assignAsCommander _vehicle;
			_soldier moveInCommander _vehicle;
		};
		_built = _built + 1;
	};
	
	if (_isVehicle select 3) then {
		Private ["_get","_turrets"];
		_get = _unitType Call GetNamespace;
		_turrets = _get select QUERYUNITTURRETS;
		
		{
			if (isNull (_vehicle turretUnit _x)) then {
				_soldier = [_crew,_team,_position,_side] Call CreateMan;
				[_soldier] allowGetIn true;
				_soldier moveInTurret [_vehicle, _x];
				_built = _built + 1;			
			};
		} forEach _turrets;
	};
	[_sideText,'UnitsCreated',_built] Call UpdateStatistics;
};

_gbq = (_team getVariable "queue") - _id;
_team setVariable ["queue",_gbq];