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