Merge branch 'z411/ca-prevent-observer' into 'master'
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / vehicles / cl_vehicles.qc
1 #include "cl_vehicles.qh"
2
3 void vehicle_alarm(entity e, int ch, Sound s0und)
4 {
5         TC(Sound, s0und);
6         if(!autocvar_cl_vehicles_alarm)
7                 return;
8
9         sound(e, ch, s0und, VOL_BASEVOICE, ATTEN_NONE);
10 }
11
12 void AuxiliaryXhair_Draw2D(entity this)
13 {
14         if (scoreboard_active)
15                 return;
16
17         vector pos = project_3d_to_2d(this.origin);
18
19         if (!(pos.z < 0 || pos.x < 0 || pos.y < 0 || pos.x > vid_conwidth || pos.y > vid_conheight))
20         {
21                 vector size = draw_getimagesize(this.axh_image) * autocvar_cl_vehicles_crosshair_size;
22                 pos.x -= 0.5 * size.x;
23                 pos.y -= 0.5 * size.y;
24                 pos.z = 0;
25                 drawpic(pos, this.axh_image, size, this.colormod, autocvar_crosshair_alpha * this.alpha, this.axh_drawflag);
26         }
27
28         if(time - this.cnt > this.axh_fadetime)
29                 this.draw2d = func_null;
30 }
31
32 NET_HANDLE(ENT_CLIENT_AUXILIARYXHAIR, bool isnew)
33 {
34         int sf = ReadByte();
35
36         int axh_id      = bound(0, ReadByte(), MAX_AXH);
37         entity axh = AuxiliaryXhair[axh_id];
38
39         if(axh == NULL || wasfreed(axh))
40         {
41                 axh                                     = new(auxiliary_crosshair);
42                 axh.draw2d                      = func_null;
43                 axh.drawmask            = MASK_NORMAL;
44                 axh.axh_drawflag        = DRAWFLAG_ADDITIVE;
45                 axh.axh_fadetime        = 0.1;
46                 axh.axh_image           = vCROSS_HINT;
47                 axh.alpha                       = 1;
48                 AuxiliaryXhair[axh_id] = axh;
49                 IL_PUSH(g_drawables_2d, axh);
50         }
51
52         if(sf & 2)
53         {
54                 axh.origin = ReadVector();
55         }
56
57         if(sf & 4)
58         {
59                 axh.colormod_x = ReadByte() / 255;
60                 axh.colormod_y = ReadByte() / 255;
61                 axh.colormod_z = ReadByte() / 255;
62         }
63
64         axh.cnt                         = time;
65         axh.draw2d                      = AuxiliaryXhair_Draw2D;
66         return true;
67 }
68
69 NET_HANDLE(TE_CSQC_VEHICLESETUP, bool isnew)
70 {
71         int hud_id = ReadByte();
72         return = true;
73
74         // hud_id == 0 means we exited a vehicle, so stop alarm sound/s
75         // note: HUD_NORMAL is set to 0 currently too, but we'll check both just in case
76         if(hud_id == 0 || hud_id == HUD_NORMAL)
77         {
78                 sound(this, CH_TRIGGER_SINGLE, SND_Null, VOL_BASEVOICE, ATTEN_NONE);
79                 sound(this, CH_PAIN_SINGLE, SND_Null, VOL_BASEVOICE, ATTEN_NONE);
80
81                 for(int i = 0; i < MAX_AXH; ++i)
82                 {
83                         entity axh = AuxiliaryXhair[i];
84
85                         if(axh != NULL && !wasfreed(axh))
86                         {
87                                 AuxiliaryXhair[i] = NULL;
88                                 delete(axh);
89                         }
90                 }
91                 return;
92         }
93
94         // Init auxiliary crosshairs
95         for(int i = 0; i < MAX_AXH; ++i)
96         {
97                 entity axh = AuxiliaryXhair[i];
98
99                 if(axh != NULL && !wasfreed(axh))  // MADNESS? THIS IS QQQQCCCCCCCCC (wasfreed, why do you exsist?)
100                         delete(axh);
101
102                 axh              = new(AuxiliaryXhair);
103                 axh.draw2d       = func_null;
104                 axh.drawmask     = MASK_NORMAL;
105                 axh.axh_drawflag = DRAWFLAG_NORMAL;
106                 axh.axh_fadetime = 0.1;
107                 axh.axh_image    = vCROSS_HINT;
108                 axh.alpha        = 1;
109                 AuxiliaryXhair[i] = axh;
110                 IL_PUSH(g_drawables_2d, axh);
111         }
112
113         if(hud_id == HUD_BUMBLEBEE_GUN)
114         {
115                 AuxiliaryXhair[0].axh_image = vCROSS_BURST; // Plasma cannons
116                 AuxiliaryXhair[1].axh_image = vCROSS_BURST; // Raygun
117         } else {
118                 Vehicle info = REGISTRY_GET(Vehicles, hud_id);
119         info.vr_setup(info, NULL);
120         }
121 }
122
123 void Vehicles_drawHUD(
124         string vehicle,
125         string vehicleWeapon1,
126         string vehicleWeapon2,
127         string iconAmmo1,
128         vector colorAmmo1,
129         string iconAmmo2,
130         vector colorAmmo2)
131 {
132         // Initialize
133         vector tmpSize = '0 0 0';
134         vector tmpPos  = '0 0 0';
135
136         float hudAlpha = autocvar_hud_panel_fg_alpha * hud_fade_alpha;
137         float barAlpha = autocvar_hud_progressbar_alpha * hudAlpha;
138         float blinkValue = 0.55 + sin(time * 7) * 0.45;
139
140         float health  = STAT(VEHICLESTAT_HEALTH)  * 0.01;
141         float shield  = STAT(VEHICLESTAT_SHIELD)  * 0.01;
142         float energy  = STAT(VEHICLESTAT_ENERGY)  * 0.01;
143         float ammo1   = STAT(VEHICLESTAT_AMMO1)   * 0.01;
144         float reload1 = STAT(VEHICLESTAT_RELOAD1) * 0.01;
145         float ammo2   = STAT(VEHICLESTAT_AMMO2)   * 0.01;
146         float reload2 = STAT(VEHICLESTAT_RELOAD2) * 0.01;
147
148         // HACK to deal with the inconsistent use of the vehicle stats
149         ammo1 = (ammo1) ? ammo1 : energy;
150
151         // Frame
152         string frame = strcat(hud_skin_path, "/vehicle_frame");
153         if (precache_pic(frame) == "")
154                 frame = "gfx/hud/default/vehicle_frame";
155
156         vehicleHud_Size  = draw_getimagesize(frame) * autocvar_cl_vehicles_hudscale;
157         vehicleHud_Pos.x = (vid_conwidth - vehicleHud_Size.x) / 2;
158         vehicleHud_Pos.y = vid_conheight - vehicleHud_Size.y;
159
160         if(teamplay && autocvar_hud_panel_bg_color_team)
161                 drawpic(vehicleHud_Pos, frame, vehicleHud_Size, myteamcolors * autocvar_hud_panel_bg_color_team, autocvar_hud_panel_bg_alpha * hud_fade_alpha, DRAWFLAG_NORMAL);
162         else
163                 drawpic(vehicleHud_Pos, frame, vehicleHud_Size, autocvar_hud_panel_bg_color, autocvar_hud_panel_bg_alpha * hud_fade_alpha, DRAWFLAG_NORMAL);
164
165         if(!autocvar__vehicles_shownchasemessage && time < vh_notice_time)
166         {
167                 float tmpblinkValue = 0.55 + sin(time * 3) * 0.45;
168                 tmpPos.x = vehicleHud_Pos.x + vehicleHud_Size.x * (96/256) - tmpSize.x;
169                 tmpPos.y = vehicleHud_Pos.y;
170                 tmpSize = '1 1 1' * hud_fontsize;
171                 drawstring(tmpPos, sprintf(_("Press %s"), getcommandkey(_("drop weapon"), "dropweapon")), tmpSize, '1 0 0' + '0 1 1' * tmpblinkValue, hudAlpha, DRAWFLAG_NORMAL);
172         }
173
174         // Model
175         tmpSize.x = vehicleHud_Size.x / 3;
176         tmpSize.y = vehicleHud_Size.y;
177         tmpPos.x  = vehicleHud_Pos.x + vehicleHud_Size.x / 3;
178         tmpPos.y  = vehicleHud_Pos.y;
179
180         if(health < 0.25)
181                 drawpic_skin(tmpPos, vehicle, tmpSize, '1 0 0' + '0 1 1' * blinkValue, hudAlpha, DRAWFLAG_NORMAL);
182         else
183                 drawpic_skin(tmpPos, vehicle, tmpSize, '1 1 1' * health  + '1 0 0' * (1 - health), hudAlpha, DRAWFLAG_NORMAL);
184
185         if(vehicleWeapon1)
186                 drawpic_skin(tmpPos, vehicleWeapon1, tmpSize, '1 1 1' * ammo1 + '1 0 0' * (1 - ammo1), hudAlpha, DRAWFLAG_NORMAL);
187         if(vehicleWeapon2)
188                 drawpic_skin(tmpPos, vehicleWeapon2, tmpSize, '1 1 1' * ammo2 + '1 0 0' * (1 - ammo2), hudAlpha, DRAWFLAG_NORMAL);
189
190         drawpic_skin(tmpPos, "vehicle_shield", tmpSize, '1 1 1' * shield + '1 0 0' * (1 - shield), hudAlpha * shield, DRAWFLAG_NORMAL);
191
192         // Health bar
193         tmpSize.y = vehicleHud_Size.y / 2;
194         tmpPos.x  = vehicleHud_Pos.x + vehicleHud_Size.x * (32/768);
195         tmpPos.y  = vehicleHud_Pos.y;
196
197         drawsetcliparea(tmpPos.x + (tmpSize.x * (1 - health)), tmpPos.y, tmpSize.x, tmpSize.y);
198         drawpic_skin(tmpPos, "vehicle_bar_northwest", tmpSize, autocvar_hud_progressbar_health_color, barAlpha, DRAWFLAG_NORMAL);
199
200         // Shield bar
201         tmpPos.y = vehicleHud_Pos.y + vehicleHud_Size.y / 2;
202
203         drawsetcliparea(tmpPos.x + (tmpSize.x * (1 - shield)), tmpPos.y, tmpSize.x, tmpSize.y);
204         drawpic_skin(tmpPos, "vehicle_bar_southwest", tmpSize, autocvar_hud_progressbar_armor_color, barAlpha, DRAWFLAG_NORMAL);
205
206         // Ammo1 bar
207         tmpPos.x = vehicleHud_Pos.x + vehicleHud_Size.x * (480/768);
208         tmpPos.y = vehicleHud_Pos.y;
209
210         if(ammo1)
211                 drawsetcliparea(tmpPos.x, tmpPos.y, tmpSize.x * ammo1, tmpSize.y);
212         else
213                 drawsetcliparea(tmpPos.x, tmpPos.y, tmpSize.x * reload1, tmpSize.y);
214
215         drawpic_skin(tmpPos, "vehicle_bar_northeast", tmpSize, colorAmmo1, barAlpha, DRAWFLAG_NORMAL);
216
217         // Ammo2 bar
218         tmpPos.y = vehicleHud_Pos.y + vehicleHud_Size.y / 2;
219
220         if(ammo2)
221                 drawsetcliparea(tmpPos.x, tmpPos.y, tmpSize.x * ammo2, tmpSize.y);
222         else
223                 drawsetcliparea(tmpPos.x, tmpPos.y, tmpSize.x * reload2, tmpSize.y);
224
225         drawpic_skin(tmpPos, "vehicle_bar_southeast", tmpSize, colorAmmo2, barAlpha, DRAWFLAG_NORMAL);
226         drawresetcliparea();
227
228         // Health icon
229         tmpSize.x = vehicleHud_Size.x * (80/768);
230         tmpSize.y = vehicleHud_Size.y * (80/256);
231         tmpPos.x  = vehicleHud_Pos.x + vehicleHud_Size.x * (64/768);
232         tmpPos.y  = vehicleHud_Pos.y + vehicleHud_Size.y * (48/256);
233
234         if(health < 0.25)
235         {
236                 if(alarm1time < time)
237                 {
238                         alarm1time = time + 2;
239                         vehicle_alarm(NULL, CH_PAIN_SINGLE, SND_VEH_ALARM);
240                 }
241                 drawpic_skin(tmpPos, "vehicle_icon_health", tmpSize, '1 1 1', hudAlpha * blinkValue, DRAWFLAG_NORMAL);
242         }
243         else
244         {
245                 if(alarm1time)
246                 {
247                         vehicle_alarm(NULL, CH_PAIN_SINGLE, SND_Null);
248                         alarm1time = 0;
249                 }
250                 drawpic_skin(tmpPos, "vehicle_icon_health", tmpSize, '1 1 1', hudAlpha, DRAWFLAG_NORMAL);
251         }
252
253         // Shield icon
254         tmpPos.y = vehicleHud_Pos.y + vehicleHud_Size.y / 2;
255
256         if(shield < 0.25)
257         {
258                 if(alarm2time < time)
259                 {
260                         alarm2time = time + 1;
261                         vehicle_alarm(NULL, CH_TRIGGER_SINGLE, SND_VEH_ALARM_SHIELD);
262                 }
263                 drawpic_skin(tmpPos, "vehicle_icon_shield", tmpSize, '1 1 1', hudAlpha * blinkValue, DRAWFLAG_NORMAL);
264         }
265         else
266         {
267                 if(alarm2time)
268                 {
269                         vehicle_alarm(NULL, CH_TRIGGER_SINGLE, SND_Null);
270                         alarm2time = 0;
271                 }
272                 drawpic_skin(tmpPos, "vehicle_icon_shield", tmpSize, '1 1 1', hudAlpha, DRAWFLAG_NORMAL);
273         }
274
275         // Ammo1 icon
276         tmpPos.x = vehicleHud_Pos.x + vehicleHud_Size.x * (624/768);
277         tmpPos.y = vehicleHud_Pos.y + vehicleHud_Size.y * (48/256);
278
279         if(iconAmmo1)
280         {
281                 if(ammo1)
282                         drawpic_skin(tmpPos, iconAmmo1, tmpSize, '1 1 1', hudAlpha, DRAWFLAG_NORMAL);
283                 else
284                         drawpic_skin(tmpPos, iconAmmo1, tmpSize, '1 1 1', hudAlpha * 0.2, DRAWFLAG_NORMAL);
285         }
286
287         // Ammo2 icon
288         tmpPos.y = vehicleHud_Pos.y + vehicleHud_Size.y / 2;
289
290         if(iconAmmo2)
291         {
292                 if(ammo2)
293                         drawpic_skin(tmpPos, iconAmmo2, tmpSize, '1 1 1', hudAlpha, DRAWFLAG_NORMAL);
294                 else
295                         drawpic_skin(tmpPos, iconAmmo2, tmpSize, '1 1 1', hudAlpha * 0.2, DRAWFLAG_NORMAL);
296         }
297 }
298
299 void Vehicles_drawCrosshair(string crosshair)
300 {
301         vector tmpSize = '0 0 0';
302         vector tmpPos  = '0 0 0';
303
304         // Crosshair
305         if(crosshair)
306         {
307                 tmpSize  = draw_getimagesize(crosshair) * autocvar_cl_vehicles_crosshair_size;
308                 tmpPos.x = (vid_conwidth - tmpSize.x) / 2;
309                 tmpPos.y = (vid_conheight - tmpSize.y) / 2;
310
311                 vector wcross_color = '1 1 1';
312                 if(autocvar_cl_vehicles_crosshair_colorize)
313                         wcross_color = crosshair_getcolor(NULL, STAT(VEHICLESTAT_HEALTH));
314
315                 drawpic(tmpPos, crosshair, tmpSize, wcross_color, autocvar_crosshair_alpha, DRAWFLAG_NORMAL);
316         }
317 }