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