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