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