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