Merge branch 'master' into Mario/vehicles
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / vehicles / cl_vehicles.qc
1 const string vCROSS_BURST = "gfx/vehicles/crosshair_burst.tga";
2 const string vCROSS_DROP  = "gfx/vehicles/crosshair_drop.tga";
3 const string vCROSS_GUIDE = "gfx/vehicles/crosshair_guide.tga";
4 const string vCROSS_HEAL  = "gfx/vehicles/crosshair_heal.tga";
5 const string vCROSS_HINT  = "gfx/vehicles/crosshair_hint.tga";
6 const string vCROSS_LOCK  = "gfx/vehicles/crosshair_lock.tga";
7 const string vCROSS_RAIN  = "gfx/vehicles/crosshair_rain.tga";
8 const string vCROSS_TANK  = "gfx/vehicles/crosshair_tank.tga";
9 const string vCROSS_TANK2 = "gfx/vehicles/crosshair_tank2.tga";
10
11 entity dropmark;
12
13 const int MAX_AXH = 4;
14 entity AuxiliaryXhair[MAX_AXH];
15
16 .string axh_image;
17 .float  axh_fadetime;
18 .int    axh_drawflag;
19
20 float alarm1time;
21 float alarm2time;
22
23 void vehicle_alarm(entity e, int ch, string s0und)
24 {
25         if(!autocvar_cl_vehicles_alarm)
26                 return;
27
28         sound(e, ch, s0und, VOL_BASEVOICE, ATTEN_NONE);
29 }
30
31 void AuxiliaryXhair_Draw2D()
32 {
33         if (scoreboard_showscores)
34                 return;
35
36         vector size = draw_getimagesize(self.axh_image) * autocvar_cl_vehicles_crosshair_size;
37         vector pos = project_3d_to_2d(self.move_origin) - 0.5 * size;
38
39         if (!(pos.z < 0 || pos.x < 0 || pos.y < 0 || pos.x > vid_conwidth || pos.y > vid_conheight))
40         {
41                 pos.z = 0;
42                 size.z = 0;
43                 drawpic(pos, self.axh_image, size, self.colormod, autocvar_crosshair_alpha * self.alpha, self.axh_drawflag);
44         }
45
46         if(time - self.cnt > self.axh_fadetime)
47                 self.draw2d = func_null;
48 }
49
50 void Net_AuXair2(bool bIsNew)
51 {
52         int axh_id      = bound(0, ReadByte(), MAX_AXH);
53         entity axh              = AuxiliaryXhair[axh_id];
54
55         if(axh == world || wasfreed(axh))  // MADNESS? THIS IS QQQQCCCCCCCCC (wasfreed, why do you exsist?)
56         {
57                 axh                                     = spawn();
58                 axh.draw2d                      = func_null;
59                 axh.drawmask            = MASK_NORMAL;
60                 axh.axh_drawflag        = DRAWFLAG_ADDITIVE;
61                 axh.axh_fadetime        = 0.1;
62                 axh.axh_image           = vCROSS_HINT;
63                 axh.alpha                       = 1;
64                 AuxiliaryXhair[axh_id] = axh;
65         }
66
67         axh.move_origin_x       = ReadCoord();
68         axh.move_origin_y       = ReadCoord();
69         axh.move_origin_z       = ReadCoord();
70         axh.colormod_x          = ReadByte() / 255;
71         axh.colormod_y          = ReadByte() / 255;
72         axh.colormod_z          = ReadByte() / 255;
73         axh.cnt                         = time;
74         axh.draw2d                      = AuxiliaryXhair_Draw2D;
75 }
76
77 void Net_VehicleSetup()
78 {
79         int hud_id = ReadByte();
80
81         // hud_id == 0 means we exited a vehicle, so stop alarm sound/s
82         if(hud_id == 0)
83         {
84                 sound(self, CH_TRIGGER_SINGLE, "misc/null.wav", VOL_BASEVOICE, ATTEN_NONE);
85                 sound(self, CH_PAIN_SINGLE, "misc/null.wav", VOL_BASEVOICE, ATTEN_NONE);
86                 return;
87         }
88
89         // Init auxiliary crosshairs
90         for(int i = 0; i < MAX_AXH; ++i)
91         {
92                 entity axh = AuxiliaryXhair[i];
93
94                 if(axh != world && !wasfreed(axh))  // MADNESS? THIS IS QQQQCCCCCCCCC (wasfreed, why do you exsist?)
95                         remove(axh);
96
97                 axh              = spawn();
98                 axh.draw2d       = func_null;
99                 axh.drawmask     = MASK_NORMAL;
100                 axh.axh_drawflag = DRAWFLAG_NORMAL;
101                 axh.axh_fadetime = 0.1;
102                 axh.axh_image    = vCROSS_HINT;
103                 axh.alpha        = 1;
104                 AuxiliaryXhair[i] = axh;
105         }
106
107         if(hud_id == HUD_BUMBLEBEE_GUN)
108         {
109                 AuxiliaryXhair[0].axh_image = vCROSS_BURST; // Plasma cannons
110                 AuxiliaryXhair[1].axh_image = vCROSS_BURST; // Raygun
111         }
112         else { VEH_ACTION(hud_id, VR_SETUP); }
113 }
114
115 void Vehicles_drawHUD(
116         string vehicle,
117         string vehicleWeapon1,
118         string vehicleWeapon2,
119         string iconAmmo1,
120         vector colorAmmo1,
121         string iconAmmo2,
122         vector colorAmmo2,
123         string crosshair)
124 {
125         if(autocvar_r_letterbox)
126                 return;
127
128         if(scoreboard_showscores)
129                 return;
130
131         // Initialize
132         vector hudSize = '0 0 0';
133         vector hudPos  = '0 0 0';
134         vector tmpSize = '0 0 0';
135         vector tmpPos  = '0 0 0';
136
137         float hudAlpha = autocvar_hud_panel_fg_alpha;
138         float barAlpha = autocvar_hud_progressbar_alpha * hudAlpha;
139         float blinkValue = 0.55 + sin(time * 7) * 0.45;
140
141         float health  = getstati(STAT_VEHICLESTAT_HEALTH)  * 0.01;
142         float shield  = getstati(STAT_VEHICLESTAT_SHIELD)  * 0.01;
143         float energy  = getstati(STAT_VEHICLESTAT_ENERGY)  * 0.01;
144         float ammo1   = getstati(STAT_VEHICLESTAT_AMMO1)   * 0.01;
145         float reload1 = getstati(STAT_VEHICLESTAT_RELOAD1) * 0.01;
146         float ammo2   = getstati(STAT_VEHICLESTAT_AMMO2)   * 0.01;
147         float reload2 = getstati(STAT_VEHICLESTAT_RELOAD2) * 0.01;
148
149         // HACK to deal with the inconsistent use of the vehicle stats
150         ammo1 = (ammo1) ? ammo1 : energy;
151
152         // Frame
153         string frame = strcat(hud_skin_path, "/vehicle_frame");
154         if (precache_pic(frame) == "")
155                 frame = "gfx/hud/default/vehicle_frame";
156
157         hudSize  = draw_getimagesize(frame) * autocvar_cl_vehicles_hudscale;
158         hudPos.x = (vid_conwidth - hudSize.x) / 2;
159         hudPos.y = vid_conheight - hudSize.y;
160
161         if(teamplay && autocvar_hud_panel_bg_color_team)
162                 drawpic(hudPos, frame, hudSize, myteamcolors * autocvar_hud_panel_bg_color_team, autocvar_hud_panel_bg_alpha, DRAWFLAG_NORMAL);
163         else
164                 drawpic(hudPos, frame, hudSize, autocvar_hud_panel_bg_color, autocvar_hud_panel_bg_alpha, DRAWFLAG_NORMAL);
165
166         if(!autocvar__vehicles_shownchasemessage && time < vh_notice_time)
167         {
168                 float tmpblinkValue = 0.55 + sin(time * 3) * 0.45;
169                 tmpPos.x = hudPos.x + hudSize.x * (96/256) - tmpSize.x;
170                 tmpPos.y = hudPos.y;
171                 tmpSize = '1 1 1' * hud_fontsize;
172                 drawstring(tmpPos, sprintf(_("Press %s"), getcommandkey("dropweapon", "dropweapon")), tmpSize, '1 0 0' + '0 1 1' * tmpblinkValue, hudAlpha, DRAWFLAG_NORMAL);
173         }
174
175         // Model
176         tmpSize.x = hudSize.x / 3;
177         tmpSize.y = hudSize.y;
178         tmpPos.x  = hudPos.x + hudSize.x / 3;
179         tmpPos.y  = hudPos.y;
180
181         if(health < 0.25)
182                 drawpic_skin(tmpPos, vehicle, tmpSize, '1 0 0' + '0 1 1' * blinkValue, hudAlpha, DRAWFLAG_NORMAL);
183         else
184                 drawpic_skin(tmpPos, vehicle, tmpSize, '1 1 1' * health  + '1 0 0' * (1 - health), hudAlpha, DRAWFLAG_NORMAL);
185
186         if(vehicleWeapon1)
187                 drawpic_skin(tmpPos, vehicleWeapon1, tmpSize, '1 1 1' * ammo1 + '1 0 0' * (1 - ammo1), hudAlpha, DRAWFLAG_NORMAL);
188         if(vehicleWeapon2)
189                 drawpic_skin(tmpPos, vehicleWeapon2, tmpSize, '1 1 1' * ammo2 + '1 0 0' * (1 - ammo2), hudAlpha, DRAWFLAG_NORMAL);
190
191         drawpic_skin(tmpPos, "vehicle_shield", tmpSize, '1 1 1' * shield + '1 0 0' * (1 - shield), hudAlpha * shield, DRAWFLAG_NORMAL);
192
193         // Health bar
194         tmpSize.y = hudSize.y / 2;
195         tmpPos.x  = hudPos.x + hudSize.x * (32/768);
196         tmpPos.y  = hudPos.y;
197
198         drawsetcliparea(tmpPos.x + (tmpSize.x * (1 - health)), tmpPos.y, tmpSize.x, tmpSize.y);
199         drawpic_skin(tmpPos, "vehicle_bar_northwest", tmpSize, autocvar_hud_progressbar_health_color, barAlpha, DRAWFLAG_NORMAL);
200
201         // Shield bar
202         tmpPos.y = hudPos.y + hudSize.y / 2;
203
204         drawsetcliparea(tmpPos.x + (tmpSize.x * (1 - shield)), tmpPos.y, tmpSize.x, tmpSize.y);
205         drawpic_skin(tmpPos, "vehicle_bar_southwest", tmpSize, autocvar_hud_progressbar_armor_color, barAlpha, DRAWFLAG_NORMAL);
206
207         // Ammo1 bar
208         tmpPos.x = hudPos.x + hudSize.x * (480/768);
209         tmpPos.y = hudPos.y;
210
211         if(ammo1)
212                 drawsetcliparea(tmpPos.x, tmpPos.y, tmpSize.x * ammo1, tmpSize.y);
213         else
214                 drawsetcliparea(tmpPos.x, tmpPos.y, tmpSize.x * reload1, tmpSize.y);
215
216         drawpic_skin(tmpPos, "vehicle_bar_northeast", tmpSize, colorAmmo1, barAlpha, DRAWFLAG_NORMAL);
217
218         // Ammo2 bar
219         tmpPos.y = hudPos.y + hudSize.y / 2;
220
221         if(ammo2)
222                 drawsetcliparea(tmpPos.x, tmpPos.y, tmpSize.x * ammo2, tmpSize.y);
223         else
224                 drawsetcliparea(tmpPos.x, tmpPos.y, tmpSize.x * reload2, tmpSize.y);
225
226         drawpic_skin(tmpPos, "vehicle_bar_southeast", tmpSize, colorAmmo2, barAlpha, DRAWFLAG_NORMAL);
227         drawresetcliparea();
228
229         // Health icon
230         tmpSize.x = hudSize.x * (80/768);
231         tmpSize.y = hudSize.y * (80/256);
232         tmpPos.x  = hudPos.x + hudSize.x * (64/768);
233         tmpPos.y  = hudPos.y + hudSize.y * (48/256);
234
235         if(health < 0.25)
236         {
237                 if(alarm1time < time)
238                 {
239                         alarm1time = time + 2;
240                         vehicle_alarm(self, CH_PAIN_SINGLE, "vehicles/alarm.wav");
241                 }
242                 drawpic_skin(tmpPos, "vehicle_icon_health", tmpSize, '1 1 1', hudAlpha * blinkValue, DRAWFLAG_NORMAL);
243         }
244         else
245         {
246                 if(alarm1time)
247                 {
248                         vehicle_alarm(self, CH_PAIN_SINGLE, "misc/null.wav");
249                         alarm1time = 0;
250                 }
251                 drawpic_skin(tmpPos, "vehicle_icon_health", tmpSize, '1 1 1', hudAlpha, DRAWFLAG_NORMAL);
252         }
253
254         // Shield icon
255         tmpPos.y = hudPos.y + hudSize.y / 2;
256
257         if(shield < 0.25)
258         {
259                 if(alarm2time < time)
260                 {
261                         alarm2time = time + 1;
262                         vehicle_alarm(self, CH_TRIGGER_SINGLE, "vehicles/alarm_shield.wav");
263                 }
264                 drawpic_skin(tmpPos, "vehicle_icon_shield", tmpSize, '1 1 1', hudAlpha * blinkValue, DRAWFLAG_NORMAL);
265         }
266         else
267         {
268                 if(alarm2time)
269                 {
270                         vehicle_alarm(self, CH_TRIGGER_SINGLE, "misc/null.wav");
271                         alarm2time = 0;
272                 }
273                 drawpic_skin(tmpPos, "vehicle_icon_shield", tmpSize, '1 1 1', hudAlpha, DRAWFLAG_NORMAL);
274         }
275
276         // Ammo1 icon
277         tmpPos.x = hudPos.x + hudSize.x * (624/768);
278         tmpPos.y = hudPos.y + hudSize.y * (48/256);
279
280         if(iconAmmo1)
281         {
282                 if(ammo1)
283                         drawpic_skin(tmpPos, iconAmmo1, tmpSize, '1 1 1', hudAlpha, DRAWFLAG_NORMAL);
284                 else
285                         drawpic_skin(tmpPos, iconAmmo1, tmpSize, '1 1 1', hudAlpha * 0.2, DRAWFLAG_NORMAL);
286         }
287
288         // Ammo2 icon
289         tmpPos.y = hudPos.y + hudSize.y / 2;
290
291         if(iconAmmo2)
292         {
293                 if(ammo2)
294                         drawpic_skin(tmpPos, iconAmmo2, tmpSize, '1 1 1', hudAlpha, DRAWFLAG_NORMAL);
295                 else
296                         drawpic_skin(tmpPos, iconAmmo2, tmpSize, '1 1 1', hudAlpha * 0.2, DRAWFLAG_NORMAL);
297         }
298
299         // Bumblebee gunner crosshairs
300         if(hud == VEH_BUMBLEBEE)
301         {
302                 tmpSize = '1 1 1' * hud_fontsize;
303                 tmpPos.x = hudPos.x + hudSize.x * (520/768);
304
305                 if(!AuxiliaryXhair[1].draw2d)
306                 {
307                         tmpPos.y = hudPos.y + hudSize.y * (96/256) - tmpSize.y;
308                         drawstring(tmpPos, _("No right gunner!"), tmpSize, '1 1 1', hudAlpha * blinkValue, DRAWFLAG_NORMAL);
309                 }
310
311                 if(!AuxiliaryXhair[2].draw2d)
312                 {
313                         tmpPos.y = hudPos.y + hudSize.y * (160/256);
314                         drawstring(tmpPos, _("No left gunner!"), tmpSize, '1 1 1', hudAlpha * blinkValue, DRAWFLAG_NORMAL);
315                 }
316         }
317
318         // Raptor bomb crosshair
319         if(hud == VEH_RAPTOR && weapon2mode != RSM_FLARE)
320         {
321                 vector where;
322
323                 if(!dropmark)
324                 {
325                         dropmark = spawn();
326                         dropmark.owner = self;
327                         dropmark.gravity = 1;
328                 }
329
330                 if(reload2 == 1)
331                 {
332                         setorigin(dropmark, pmove_org);
333                         dropmark.velocity = pmove_vel;
334                         tracetoss(dropmark, self);
335
336                         where = project_3d_to_2d(trace_endpos);
337
338                         setorigin(dropmark, trace_endpos);
339                         tmpSize = draw_getimagesize(vCROSS_DROP) * autocvar_cl_vehicles_crosshair_size;
340
341                         if (!(where.z < 0 || where.x < 0 || where.y < 0 || where.x > vid_conwidth || where.y > vid_conheight))
342                         {
343                                 where.x -= tmpSize.x * 0.5;
344                                 where.y -= tmpSize.y * 0.5;
345                                 where.z = 0;
346                                 drawpic(where, vCROSS_DROP, tmpSize, '0 1 0', autocvar_crosshair_alpha * 0.9, DRAWFLAG_ADDITIVE);
347                                 drawpic(where, vCROSS_DROP, tmpSize, '0 1 0', autocvar_crosshair_alpha * 0.6, DRAWFLAG_NORMAL); // Ensure visibility against bright bg
348                         }
349                         dropmark.cnt = time + 5;
350                 }
351                 else
352                 {
353                         if(dropmark.cnt > time)
354                         {
355                                 where = project_3d_to_2d(dropmark.origin);
356                                 tmpSize = draw_getimagesize(vCROSS_DROP) * autocvar_cl_vehicles_crosshair_size * 1.25;
357
358                                 if (!(where.z < 0 || where.x < 0 || where.y < 0 || where.x > vid_conwidth || where.y > vid_conheight))
359                                 {
360                                         where.x -= tmpSize.x * 0.5;
361                                         where.y -= tmpSize.y * 0.5;
362                                         where.z = 0;
363                                         drawpic(where, vCROSS_DROP, tmpSize, '1 0 0', autocvar_crosshair_alpha * 0.9, DRAWFLAG_ADDITIVE);
364                                         drawpic(where, vCROSS_DROP, tmpSize, '1 0 0', autocvar_crosshair_alpha * 0.6, DRAWFLAG_NORMAL); // Ensure visibility against bright bg
365                                 }
366                         }
367                 }
368         }
369
370         // Crosshair
371         if(crosshair)
372         {
373                 tmpSize  = draw_getimagesize(crosshair) * autocvar_cl_vehicles_crosshair_size;
374                 tmpPos.x = (vid_conwidth - tmpSize.x) / 2;
375                 tmpPos.y = (vid_conheight - tmpSize.y) / 2;
376
377                 drawpic(tmpPos, crosshair, tmpSize, '1 1 1', autocvar_crosshair_alpha, DRAWFLAG_NORMAL);
378         }
379 }