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