]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/hud/panel/weapons.qc
8fa22dd5e8afbce4d43f6d29c7740b6915b1e553
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / hud / panel / weapons.qc
1 #include "weapons.qh"
2 // Weapon icons (#0)
3
4 entity weaponorder[Weapons_MAX];
5 void weaponorder_swap(int i, int j, entity pass)
6 {
7     TC(int, i); TC(int, j);
8         entity h = weaponorder[i];
9         weaponorder[i] = weaponorder[j];
10         weaponorder[j] = h;
11 }
12
13 string weaponorder_cmp_str;
14 int weaponorder_cmp(int i, int j, entity pass)
15 {
16     TC(int, i); TC(int, j);
17         int ai = strstrofs(weaponorder_cmp_str, sprintf(" %d ", weaponorder[i].m_id), 0);
18         int aj = strstrofs(weaponorder_cmp_str, sprintf(" %d ", weaponorder[j].m_id), 0);
19         return aj - ai; // the string is in REVERSE order (higher prio at the right is what we want, but higher prio first is the string)
20 }
21
22 #define HUD_WEAPONS_GET_FULL_LAYOUT() MACRO_BEGIN { \
23         int nHidden = 0; \
24         FOREACH(Weapons, it != WEP_Null, { \
25                 if (weapons_stat & WepSet_FromWeapon(it)) continue; \
26                 if (it.spawnflags & WEP_FLAG_MUTATORBLOCKED) nHidden += 1; \
27         }); \
28         vector table_size = HUD_GetTableSize_BestItemAR((Weapons_COUNT - 1) - nHidden, panel_size, aspect); \
29         columns = table_size.x; \
30         rows = table_size.y; \
31         weapon_size.x = panel_size.x / columns; \
32         weapon_size.y = panel_size.y / rows; \
33 } MACRO_END
34
35 void HUD_Weapons()
36 {
37         // declarations
38         WepSet weapons_stat = WepSet_GetFromStat();
39         int i;
40         float f, a;
41         float screen_ar;
42         vector center = '0 0 0';
43         int weapon_count, weapon_id;
44         int row, column, rows = 0, columns = 0;
45         bool vertical_order = true;
46         float aspect = autocvar_hud_panel_weapons_aspect;
47
48         float timeout = autocvar_hud_panel_weapons_timeout;
49         float timein_effect_length = autocvar_hud_panel_weapons_timeout_speed_in; //? 0.375 : 0);
50         float timeout_effect_length = autocvar_hud_panel_weapons_timeout_speed_out; //? 0.75 : 0);
51
52         vector barsize = '0 0 0', baroffset = '0 0 0';
53         vector ammo_color = '1 0 1';
54         float ammo_alpha = 1;
55
56         float when = max(1, autocvar_hud_panel_weapons_complainbubble_time);
57         float fadetime = max(0, autocvar_hud_panel_weapons_complainbubble_fadetime);
58
59         vector weapon_pos, weapon_size = '0 0 0';
60         vector color;
61
62         // check to see if we want to continue
63         if(hud != HUD_NORMAL) return;
64
65         if(!autocvar__hud_configure)
66         {
67                 if((!autocvar_hud_panel_weapons) || (spectatee_status == -1))
68                         return;
69                 if(timeout && time >= weapontime + timeout + timeout_effect_length)
70                 if(autocvar_hud_panel_weapons_timeout_effect == 3 || (autocvar_hud_panel_weapons_timeout_effect == 1 && !(autocvar_hud_panel_weapons_timeout_fadebgmin + autocvar_hud_panel_weapons_timeout_fadefgmin)))
71                 {
72                         weaponprevtime = time;
73                         return;
74                 }
75         }
76
77         // update generic hud functions
78         HUD_Panel_UpdateCvars();
79
80         // figure out weapon order (how the weapons are sorted) // TODO make this configurable
81         if(weaponorder_bypriority != autocvar_cl_weaponpriority || !weaponorder[0])
82         {
83                 int weapon_cnt;
84                 if(weaponorder_bypriority)
85                         strunzone(weaponorder_bypriority);
86                 if(weaponorder_byimpulse)
87                         strunzone(weaponorder_byimpulse);
88
89                 weaponorder_bypriority = strzone(autocvar_cl_weaponpriority);
90                 weaponorder_byimpulse = strzone(W_FixWeaponOrder_BuildImpulseList(W_FixWeaponOrder_ForceComplete(W_NumberWeaponOrder(weaponorder_bypriority))));
91                 weaponorder_cmp_str = strcat(" ", weaponorder_byimpulse, " ");
92
93                 weapon_cnt = 0;
94                 FOREACH(Weapons, it != WEP_Null && it.impulse >= 0, weaponorder[weapon_cnt++] = it);
95                 for(i = weapon_cnt; i < Weapons_MAX; ++i)
96                         weaponorder[i] = NULL;
97                 heapsort(weapon_cnt, weaponorder_swap, weaponorder_cmp, NULL);
98
99                 weaponorder_cmp_str = string_null;
100         }
101
102         if(!autocvar_hud_panel_weapons_complainbubble || autocvar__hud_configure || time - complain_weapon_time >= when + fadetime)
103                 complain_weapon = 0;
104
105         if(autocvar__hud_configure)
106         {
107                 if(!weapons_stat)
108                 {
109                         int j = 0;
110                         FOREACH(Weapons, it != WEP_Null && it.impulse >= 0 && (it.impulse % 3 != 0) && j < 6, {
111                                 if(!(it.spawnflags & WEP_FLAG_MUTATORBLOCKED))
112                                 {
113                                         weapons_stat |= it.m_wepset;
114                                         ++j;
115                                 }
116                         });
117                 }
118
119                 #if 0
120                 /// debug code
121                 if(cvar("wep_add"))
122                 {
123                         weapons_stat = '0 0 0';
124                         float countw = 1 + floor((floor(time * cvar("wep_add"))) % (Weapons_COUNT - 1));
125                         for(i = WEP_FIRST; i <= countw; ++i)
126                                 weapons_stat |= WepSet_FromWeapon(Weapons_from(i));
127                 }
128                 #endif
129         }
130
131         // determine which weapons are going to be shown
132         if (autocvar_hud_panel_weapons_onlyowned)
133         {
134                 if(autocvar__hud_configure)
135                 {
136                         if(menu_enabled != 2)
137                                 HUD_Panel_DrawBg(1); // also draw the bg of the entire panel
138                 }
139
140                 // do we own this weapon?
141                 weapon_count = 0;
142                 for(i = 0; i <= WEP_LAST-WEP_FIRST; ++i)
143                         if((weapons_stat & WepSet_FromWeapon(weaponorder[i])) || (weaponorder[i].m_id == complain_weapon))
144                                 ++weapon_count;
145
146
147                 // might as well commit suicide now, no reason to live ;)
148                 if (weapon_count == 0)
149                         return;
150
151                 vector old_panel_size = panel_size;
152                 panel_size -= '2 2 0' * panel_bg_padding;
153
154                 HUD_WEAPONS_GET_FULL_LAYOUT();
155
156                 // NOTE: although weapons should aways look the same even if onlyowned is enabled,
157                 // we enlarge them a bit when possible to better match the desired aspect ratio
158                 if(panel_size.x / panel_size.y < aspect)
159                 {
160                         // maximum number of rows that allows to display items with the desired aspect ratio
161                         int max_rows = floor(panel_size.y / (weapon_size.x / aspect));
162                         columns = min(columns, ceil(weapon_count / max_rows));
163                         rows = ceil(weapon_count / columns);
164                         weapon_size.y = min(panel_size.y / rows, weapon_size.x / aspect);
165                         weapon_size.x = min(panel_size.x / columns, aspect * weapon_size.y);
166                         vertical_order = false;
167                 }
168                 else
169                 {
170                         int max_columns = floor(panel_size.x / (weapon_size.y * aspect));
171                         rows = min(rows, ceil(weapon_count / max_columns));
172                         columns = ceil(weapon_count / rows);
173                         weapon_size.x = min(panel_size.x / columns, aspect * weapon_size.y);
174                         weapon_size.y = min(panel_size.y / rows, weapon_size.x / aspect);
175                         vertical_order = true;
176                 }
177
178                 // reduce size of the panel
179                 panel_size.x = columns * weapon_size.x;
180                 panel_size.y = rows * weapon_size.y;
181                 panel_size += '2 2 0' * panel_bg_padding;
182
183                 // center the resized panel, or snap it to the screen edge when close enough
184                 if(panel_pos.x > vid_conwidth * 0.001)
185                 {
186                         if(panel_pos.x + old_panel_size.x > vid_conwidth * 0.999)
187                                 panel_pos.x += old_panel_size.x - panel_size.x;
188                         else
189                                 panel_pos.x += (old_panel_size.x - panel_size.x) / 2;
190                 }
191                 else if(old_panel_size.x > vid_conwidth * 0.999)
192                         panel_pos.x += (old_panel_size.x - panel_size.x) / 2;
193
194                 if(panel_pos.y > vid_conheight * 0.001)
195                 {
196                         if(panel_pos.y + old_panel_size.y > vid_conheight * 0.999)
197                                 panel_pos.y += old_panel_size.y - panel_size.y;
198                         else
199                                 panel_pos.y += (old_panel_size.y - panel_size.y) / 2;
200                 }
201                 else if(old_panel_size.y > vid_conheight * 0.999)
202                         panel_pos.y += (old_panel_size.y - panel_size.y) / 2;
203         }
204         else
205                 weapon_count = (Weapons_COUNT - 1);
206
207         // animation for fading in/out the panel respectively when not in use
208         if(!autocvar__hud_configure)
209         {
210                 if (timeout && time >= weapontime + timeout) // apply timeout effect if needed
211                 {
212                         f = bound(0, (time - (weapontime + timeout)) / timeout_effect_length, 1);
213
214                         // fade the panel alpha
215                         if(autocvar_hud_panel_weapons_timeout_effect == 1)
216                         {
217                                 panel_bg_alpha *= (autocvar_hud_panel_weapons_timeout_fadebgmin * f + (1 - f));
218                                 panel_fg_alpha *= (autocvar_hud_panel_weapons_timeout_fadefgmin * f + (1 - f));
219                         }
220                         else if(autocvar_hud_panel_weapons_timeout_effect == 3)
221                         {
222                                 panel_bg_alpha *= (1 - f);
223                                 panel_fg_alpha *= (1 - f);
224                         }
225
226                         // move the panel off the screen
227                         if (autocvar_hud_panel_weapons_timeout_effect == 2 || autocvar_hud_panel_weapons_timeout_effect == 3)
228                         {
229                                 f *= f; // for a cooler movement
230                                 center.x = panel_pos.x + panel_size.x/2;
231                                 center.y = panel_pos.y + panel_size.y/2;
232                                 screen_ar = vid_conwidth/vid_conheight;
233                                 if (center.x/center.y < screen_ar) //bottom left
234                                 {
235                                         if ((vid_conwidth - center.x)/center.y < screen_ar) //bottom
236                                                 panel_pos.y += f * (vid_conheight - panel_pos.y);
237                                         else //left
238                                                 panel_pos.x -= f * (panel_pos.x + panel_size.x);
239                                 }
240                                 else //top right
241                                 {
242                                         if ((vid_conwidth - center.x)/center.y < screen_ar) //right
243                                                 panel_pos.x += f * (vid_conwidth - panel_pos.x);
244                                         else //top
245                                                 panel_pos.y -= f * (panel_pos.y + panel_size.y);
246                                 }
247                                 if(f == 1)
248                                         center.x = -1; // mark the panel as off screen
249                         }
250                         weaponprevtime = time - (1 - f) * timein_effect_length;
251                 }
252                 else if (timeout && time < weaponprevtime + timein_effect_length) // apply timein effect if needed
253                 {
254                         f = bound(0, (time - weaponprevtime) / timein_effect_length, 1);
255
256                         // fade the panel alpha
257                         if(autocvar_hud_panel_weapons_timeout_effect == 1)
258                         {
259                                 panel_bg_alpha *= (autocvar_hud_panel_weapons_timeout_fadebgmin * (1 - f) + f);
260                                 panel_fg_alpha *= (autocvar_hud_panel_weapons_timeout_fadefgmin * (1 - f) + f);
261                         }
262                         else if(autocvar_hud_panel_weapons_timeout_effect == 3)
263                         {
264                                 panel_bg_alpha *= (f);
265                                 panel_fg_alpha *= (f);
266                         }
267
268                         // move the panel back on screen
269                         if (autocvar_hud_panel_weapons_timeout_effect == 2 || autocvar_hud_panel_weapons_timeout_effect == 3)
270                         {
271                                 f *= f; // for a cooler movement
272                                 f = 1 - f;
273                                 center.x = panel_pos.x + panel_size.x/2;
274                                 center.y = panel_pos.y + panel_size.y/2;
275                                 screen_ar = vid_conwidth/vid_conheight;
276                                 if (center.x/center.y < screen_ar) //bottom left
277                                 {
278                                         if ((vid_conwidth - center.x)/center.y < screen_ar) //bottom
279                                                 panel_pos.y += f * (vid_conheight - panel_pos.y);
280                                         else //left
281                                                 panel_pos.x -= f * (panel_pos.x + panel_size.x);
282                                 }
283                                 else //top right
284                                 {
285                                         if ((vid_conwidth - center.x)/center.y < screen_ar) //right
286                                                 panel_pos.x += f * (vid_conwidth - panel_pos.x);
287                                         else //top
288                                                 panel_pos.y -= f * (panel_pos.y + panel_size.y);
289                                 }
290                         }
291                 }
292         }
293
294         // draw the background, then change the virtual size of it to better fit other items inside
295         HUD_Panel_DrawBg(1);
296
297         if(center.x == -1)
298                 return; // panel has gone off screen
299
300         if(panel_bg_padding)
301         {
302                 panel_pos += '1 1 0' * panel_bg_padding;
303                 panel_size -= '2 2 0' * panel_bg_padding;
304         }
305
306         // after the sizing and animations are done, update the other values
307
308         if(!rows) // if rows is > 0 onlyowned code has already updated these vars
309         {
310                 HUD_WEAPONS_GET_FULL_LAYOUT();
311                 vertical_order = (panel_size.x / panel_size.y >= aspect);
312         }
313
314         // calculate position/size for visual bar displaying ammount of ammo status
315         if (autocvar_hud_panel_weapons_ammo)
316         {
317                 ammo_color = stov(autocvar_hud_panel_weapons_ammo_color);
318                 ammo_alpha = panel_fg_alpha * autocvar_hud_panel_weapons_ammo_alpha;
319
320                 if(weapon_size.x/weapon_size.y > aspect)
321                 {
322                         barsize.x = aspect * weapon_size.y;
323                         barsize.y = weapon_size.y;
324                         baroffset.x = (weapon_size.x - barsize.x) / 2;
325                 }
326                 else
327                 {
328                         barsize.y = 1/aspect * weapon_size.x;
329                         barsize.x = weapon_size.x;
330                         baroffset.y = (weapon_size.y - barsize.y) / 2;
331                 }
332         }
333         if(autocvar_hud_panel_weapons_accuracy)
334                 Accuracy_LoadColors();
335
336         // draw items
337         row = column = 0;
338         vector label_size = '1 1 0' * min(weapon_size.x, weapon_size.y) * bound(0, autocvar_hud_panel_weapons_label_scale, 1);
339         vector noncurrent_pos = '0 0 0';
340         vector noncurrent_size = weapon_size * bound(0, autocvar_hud_panel_weapons_noncurrent_scale, 1);
341         float noncurrent_alpha = panel_fg_alpha * bound(0, autocvar_hud_panel_weapons_noncurrent_alpha, 1);
342         bool isCurrent;
343
344         for(i = 0; i <= WEP_LAST-WEP_FIRST; ++i)
345         {
346                 // retrieve information about the current weapon to be drawn
347                 entity it = weaponorder[i];
348                 weapon_id = it.impulse;
349
350                 // skip if this weapon doesn't exist
351                 if(!it || weapon_id < 0) { continue; }
352
353                 // skip this weapon if we don't own it (and onlyowned is enabled)-- or if weapons_complainbubble is showing for this weapon
354                 if(autocvar_hud_panel_weapons_onlyowned)
355                 {
356                         if (!((weapons_stat & WepSet_FromWeapon(it)) || (it.m_id == complain_weapon)))
357                                 continue;
358                 }
359                 else
360                 {
361                         if (it.spawnflags & WEP_FLAG_MUTATORBLOCKED && !(weapons_stat & WepSet_FromWeapon(it)))
362                                 continue;
363                 }
364
365                 // figure out the drawing position of weapon
366                 weapon_pos = (panel_pos + eX * column * weapon_size.x + eY * row * weapon_size.y);
367                 noncurrent_pos.x = weapon_pos.x + (weapon_size.x - noncurrent_size.x) / 2;
368                 noncurrent_pos.y = weapon_pos.y + (weapon_size.y - noncurrent_size.y) / 2;
369
370                 // draw background behind currently selected weapon
371                 isCurrent = (it == switchweapon);
372                 if(isCurrent)
373                         drawpic_aspect_skin(weapon_pos, "weapon_current_bg", weapon_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
374
375                 // draw the weapon accuracy
376                 if(autocvar_hud_panel_weapons_accuracy)
377                 {
378                         float panel_weapon_accuracy = weapon_accuracy[it.m_id-WEP_FIRST];
379                         if(panel_weapon_accuracy >= 0)
380                         {
381                                 color = Accuracy_GetColor(panel_weapon_accuracy);
382                                 drawpic_aspect_skin(weapon_pos, "weapon_accuracy", weapon_size, color, panel_fg_alpha, DRAWFLAG_NORMAL);
383                         }
384                 }
385
386                 // drawing all the weapon items
387                 if(weapons_stat & WepSet_FromWeapon(it))
388                 {
389                         // draw the weapon image
390                         if(isCurrent)
391                                 drawpic_aspect_skin(weapon_pos, it.model2, weapon_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
392                         else
393                                 drawpic_aspect_skin(noncurrent_pos, it.model2, noncurrent_size, '1 1 1', noncurrent_alpha, DRAWFLAG_NORMAL);
394
395                         // draw weapon label string
396                         switch(autocvar_hud_panel_weapons_label)
397                         {
398                                 case 1: // weapon number
399                                         drawstring(weapon_pos, ftos(weapon_id), label_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
400                                         break;
401
402                                 case 2: // bind
403                                         drawstring(weapon_pos, getcommandkey(ftos(weapon_id), strcat("weapon_group_", ftos(weapon_id))), label_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
404                                         break;
405
406                                 case 3: // weapon name
407                                         drawstring(weapon_pos, strtolower(it.m_name), label_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
408                                         break;
409
410                                 default: // nothing
411                                         break;
412                         }
413
414                         // draw ammo status bar
415                         if(autocvar_hud_panel_weapons_ammo && (it.ammo_field != ammo_none))
416                         {
417                                 float ammo_full;
418                                 a = getstati(GetAmmoStat(it.ammo_field)); // how much ammo do we have?
419
420                                 if(a > 0)
421                                 {
422                                         switch(it.ammo_field)
423                                         {
424                                                 case ammo_shells:  ammo_full = autocvar_hud_panel_weapons_ammo_full_shells;  break;
425                                                 case ammo_nails:   ammo_full = autocvar_hud_panel_weapons_ammo_full_nails;   break;
426                                                 case ammo_rockets: ammo_full = autocvar_hud_panel_weapons_ammo_full_rockets; break;
427                                                 case ammo_cells:   ammo_full = autocvar_hud_panel_weapons_ammo_full_cells;   break;
428                                                 case ammo_plasma:  ammo_full = autocvar_hud_panel_weapons_ammo_full_plasma;  break;
429                                                 case ammo_fuel:    ammo_full = autocvar_hud_panel_weapons_ammo_full_fuel;    break;
430                                                 default: ammo_full = 60;
431                                         }
432
433                                         drawsetcliparea(
434                                                 weapon_pos.x + baroffset.x,
435                                                 weapon_pos.y + baroffset.y,
436                                                 barsize.x * bound(0, a/ammo_full, 1),
437                                                 barsize.y
438                                         );
439
440                                         drawpic_aspect_skin(
441                                                 weapon_pos,
442                                                 "weapon_ammo",
443                                                 weapon_size,
444                                                 ammo_color,
445                                                 ammo_alpha,
446                                                 DRAWFLAG_NORMAL
447                                         );
448
449                                         drawresetcliparea();
450                                 }
451                         }
452                 }
453                 else // draw a "ghost weapon icon" if you don't have the weapon
454                 {
455                         drawpic_aspect_skin(noncurrent_pos, it.model2, noncurrent_size, '0.2 0.2 0.2', panel_fg_alpha * 0.5, DRAWFLAG_NORMAL);
456                 }
457
458                 // draw the complain message
459                 if(it.m_id == complain_weapon)
460                 {
461                         if(fadetime)
462                                 a = ((complain_weapon_time + when > time) ? 1 : bound(0, (complain_weapon_time + when + fadetime - time) / fadetime, 1));
463                         else
464                                 a = ((complain_weapon_time + when > time) ? 1 : 0);
465
466                         string s;
467                         if(complain_weapon_type == 0) {
468                                 s = _("Out of ammo");
469                                 color = stov(autocvar_hud_panel_weapons_complainbubble_color_outofammo);
470                         }
471                         else if(complain_weapon_type == 1) {
472                                 s = _("Don't have");
473                                 color = stov(autocvar_hud_panel_weapons_complainbubble_color_donthave);
474                         }
475                         else {
476                                 s = _("Unavailable");
477                                 color = stov(autocvar_hud_panel_weapons_complainbubble_color_unavailable);
478                         }
479                         float padding = autocvar_hud_panel_weapons_complainbubble_padding;
480                         drawpic_aspect_skin(weapon_pos + '1 1 0' * padding, "weapon_complainbubble", weapon_size - '2 2 0' * padding, color, a * panel_fg_alpha, DRAWFLAG_NORMAL);
481                         drawstring_aspect(weapon_pos + '1 1 0' * padding, s, weapon_size - '2 2 0' * padding, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
482                 }
483
484                 #if 0
485                 /// debug code
486                 if(!autocvar_hud_panel_weapons_onlyowned)
487                 {
488                         drawfill(weapon_pos + '1 1 0', weapon_size - '2 2 0', '1 1 1', panel_fg_alpha * 0.2, DRAWFLAG_NORMAL);
489                         drawstring(weapon_pos, ftos(i + 1), label_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
490                 }
491                 #endif
492
493                 // continue with new position for the next weapon
494                 if(vertical_order)
495                 {
496                         ++column;
497                         if(column >= columns)
498                         {
499                                 column = 0;
500                                 ++row;
501                         }
502                 }
503                 else
504                 {
505                         ++row;
506                         if(row >= rows)
507                         {
508                                 row = 0;
509                                 ++column;
510                         }
511                 }
512         }
513 }