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                 for(i = WEP_FIRST; i <= WEP_LAST; ++i)
81                 {
82                         setself(get_weaponinfo(i));
83                         if(self.impulse >= 0)
84                         {
85                                 weaponorder[weapon_cnt] = self;
86                                 ++weapon_cnt;
87                         }
88                 }
89                 for(i = weapon_cnt; i < Weapons_MAX; ++i)
90                         weaponorder[i] = world;
91                 heapsort(weapon_cnt, weaponorder_swap, weaponorder_cmp, world);
92
93                 weaponorder_cmp_str = string_null;
94         }
95
96         if(!autocvar_hud_panel_weapons_complainbubble || autocvar__hud_configure || time - complain_weapon_time >= when + fadetime)
97                 complain_weapon = 0;
98
99         if(autocvar__hud_configure)
100         {
101                 if(!weapons_stat)
102                         for(i = WEP_FIRST; i <= WEP_LAST; i += floor((WEP_LAST-WEP_FIRST)/5))
103                                 weapons_stat |= WepSet_FromWeapon(i);
104
105                 #if 0
106                 /// debug code
107                 if(cvar("wep_add"))
108                 {
109                         weapons_stat = '0 0 0';
110                         float countw = 1 + floor((floor(time * cvar("wep_add"))) % (Weapons_COUNT - 1));
111                         for(i = WEP_FIRST; i <= countw; ++i)
112                                 weapons_stat |= WepSet_FromWeapon(i);
113                 }
114                 #endif
115         }
116
117         // determine which weapons are going to be shown
118         if (autocvar_hud_panel_weapons_onlyowned)
119         {
120                 if(autocvar__hud_configure)
121                 {
122                         if(menu_enabled != 2)
123                                 HUD_Panel_DrawBg(1); // also draw the bg of the entire panel
124                 }
125
126                 // do we own this weapon?
127                 weapon_count = 0;
128                 for(i = 0; i <= WEP_LAST-WEP_FIRST; ++i)
129                         if((weapons_stat & WepSet_FromWeapon(weaponorder[i].weapon)) || (weaponorder[i].weapon == complain_weapon))
130                                 ++weapon_count;
131
132
133                 // might as well commit suicide now, no reason to live ;)
134                 if (weapon_count == 0)
135                         return;
136
137                 vector old_panel_size = panel_size;
138                 vector padded_panel_size = panel_size - '2 2 0' * panel_bg_padding;
139
140                 // get the all-weapons layout
141                 int nHidden = 0;
142                 WepSet weapons_stat = WepSet_GetFromStat();
143                 for (int i = WEP_FIRST; i <= WEP_LAST; ++i) {
144                         Weapon w = get_weaponinfo(i);
145                         if (weapons_stat & w.m_wepset) continue;
146                         if (w.spawnflags & WEP_FLAG_MUTATORBLOCKED) nHidden += 1;
147                 }
148                 vector table_size = HUD_GetTableSize_BestItemAR((Weapons_COUNT - 1) - nHidden, padded_panel_size, aspect);
149                 columns = table_size.x;
150                 rows = table_size.y;
151                 weapon_size.x = padded_panel_size.x / columns;
152                 weapon_size.y = padded_panel_size.y / rows;
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(padded_panel_size.x / padded_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(padded_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(padded_panel_size.y / rows, weapon_size.x / aspect);
163                         weapon_size.x = min(padded_panel_size.x / columns, aspect * weapon_size.y);
164                         vertical_order = false;
165                 }
166                 else
167                 {
168                         int max_columns = floor(padded_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(padded_panel_size.x / columns, aspect * weapon_size.y);
172                         weapon_size.y = min(padded_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;
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                 vector table_size = HUD_GetTableSize_BestItemAR((Weapons_COUNT - 1), panel_size, aspect);
309                 columns = table_size.x;
310                 rows = table_size.y;
311                 weapon_size.x = panel_size.x / columns;
312                 weapon_size.y = panel_size.y / rows;
313                 vertical_order = (panel_size.x / panel_size.y >= aspect);
314         }
315
316         // calculate position/size for visual bar displaying ammount of ammo status
317         if (autocvar_hud_panel_weapons_ammo)
318         {
319                 ammo_color = stov(autocvar_hud_panel_weapons_ammo_color);
320                 ammo_alpha = panel_fg_alpha * autocvar_hud_panel_weapons_ammo_alpha;
321
322                 if(weapon_size.x/weapon_size.y > aspect)
323                 {
324                         barsize.x = aspect * weapon_size.y;
325                         barsize.y = weapon_size.y;
326                         baroffset.x = (weapon_size.x - barsize.x) / 2;
327                 }
328                 else
329                 {
330                         barsize.y = 1/aspect * weapon_size.x;
331                         barsize.x = weapon_size.x;
332                         baroffset.y = (weapon_size.y - barsize.y) / 2;
333                 }
334         }
335         if(autocvar_hud_panel_weapons_accuracy)
336                 Accuracy_LoadColors();
337
338         // draw items
339         row = column = 0;
340         vector label_size = '1 1 0' * min(weapon_size.x, weapon_size.y) * bound(0, autocvar_hud_panel_weapons_label_scale, 1);
341         vector noncurrent_pos = '0 0 0';
342         vector noncurrent_size = weapon_size * bound(0, autocvar_hud_panel_weapons_noncurrent_scale, 1);
343         float noncurrent_alpha = panel_fg_alpha * bound(0, autocvar_hud_panel_weapons_noncurrent_alpha, 1);
344         bool isCurrent;
345
346         for(i = 0; i <= WEP_LAST-WEP_FIRST; ++i)
347         {
348                 // retrieve information about the current weapon to be drawn
349                 setself(weaponorder[i]);
350                 weapon_id = self.impulse;
351                 isCurrent = (self.weapon == switchweapon);
352
353                 // skip if this weapon doesn't exist
354                 if(!self || weapon_id < 0) { continue; }
355
356                 // skip this weapon if we don't own it (and onlyowned is enabled)-- or if weapons_complainbubble is showing for this weapon
357                 if(autocvar_hud_panel_weapons_onlyowned)
358                 if (!((weapons_stat & WepSet_FromWeapon(self.weapon)) || (self.weapon == complain_weapon)))
359                         continue;
360
361                 // figure out the drawing position of weapon
362                 weapon_pos = (panel_pos + eX * column * weapon_size.x + eY * row * weapon_size.y);
363                 noncurrent_pos.x = weapon_pos.x + (weapon_size.x - noncurrent_size.x) / 2;
364                 noncurrent_pos.y = weapon_pos.y + (weapon_size.y - noncurrent_size.y) / 2;
365
366                 // draw background behind currently selected weapon
367                 if(isCurrent)
368                         drawpic_aspect_skin(weapon_pos, "weapon_current_bg", weapon_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
369
370                 // draw the weapon accuracy
371                 if(autocvar_hud_panel_weapons_accuracy)
372                 {
373                         float panel_weapon_accuracy = weapon_accuracy[self.weapon-WEP_FIRST];
374                         if(panel_weapon_accuracy >= 0)
375                         {
376                                 color = Accuracy_GetColor(panel_weapon_accuracy);
377                                 drawpic_aspect_skin(weapon_pos, "weapon_accuracy", weapon_size, color, panel_fg_alpha, DRAWFLAG_NORMAL);
378                         }
379                 }
380
381                 // drawing all the weapon items
382                 if(weapons_stat & WepSet_FromWeapon(self.weapon))
383                 {
384                         // draw the weapon image
385                         if(isCurrent)
386                                 drawpic_aspect_skin(weapon_pos, self.model2, weapon_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
387                         else
388                                 drawpic_aspect_skin(noncurrent_pos, self.model2, noncurrent_size, '1 1 1', noncurrent_alpha, DRAWFLAG_NORMAL);
389
390                         // draw weapon label string
391                         switch(autocvar_hud_panel_weapons_label)
392                         {
393                                 case 1: // weapon number
394                                         drawstring(weapon_pos, ftos(weapon_id), label_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
395                                         break;
396
397                                 case 2: // bind
398                                         drawstring(weapon_pos, getcommandkey(ftos(weapon_id), strcat("weapon_group_", ftos(weapon_id))), label_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
399                                         break;
400
401                                 case 3: // weapon name
402                                         drawstring(weapon_pos, strtolower(self.m_name), label_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
403                                         break;
404
405                                 default: // nothing
406                                         break;
407                         }
408
409                         // draw ammo status bar
410                         if(autocvar_hud_panel_weapons_ammo && (self.ammo_field != ammo_none))
411                         {
412                                 float ammo_full;
413                                 a = getstati(GetAmmoStat(self.ammo_field)); // how much ammo do we have?
414
415                                 if(a > 0)
416                                 {
417                                         switch(self.ammo_field)
418                                         {
419                                                 case ammo_shells:  ammo_full = autocvar_hud_panel_weapons_ammo_full_shells;  break;
420                                                 case ammo_nails:   ammo_full = autocvar_hud_panel_weapons_ammo_full_nails;   break;
421                                                 case ammo_rockets: ammo_full = autocvar_hud_panel_weapons_ammo_full_rockets; break;
422                                                 case ammo_cells:   ammo_full = autocvar_hud_panel_weapons_ammo_full_cells;   break;
423                                                 case ammo_plasma:  ammo_full = autocvar_hud_panel_weapons_ammo_full_plasma;  break;
424                                                 case ammo_fuel:    ammo_full = autocvar_hud_panel_weapons_ammo_full_fuel;    break;
425                                                 default: ammo_full = 60;
426                                         }
427
428                                         drawsetcliparea(
429                                                 weapon_pos.x + baroffset.x,
430                                                 weapon_pos.y + baroffset.y,
431                                                 barsize.x * bound(0, a/ammo_full, 1),
432                                                 barsize.y
433                                         );
434
435                                         drawpic_aspect_skin(
436                                                 weapon_pos,
437                                                 "weapon_ammo",
438                                                 weapon_size,
439                                                 ammo_color,
440                                                 ammo_alpha,
441                                                 DRAWFLAG_NORMAL
442                                         );
443
444                                         drawresetcliparea();
445                                 }
446                         }
447                 }
448                 else // draw a "ghost weapon icon" if you don't have the weapon
449                 {
450                         drawpic_aspect_skin(noncurrent_pos, self.model2, noncurrent_size, '0.2 0.2 0.2', panel_fg_alpha * 0.5, DRAWFLAG_NORMAL);
451                 }
452
453                 // draw the complain message
454                 if(self.weapon == complain_weapon)
455                 {
456                         if(fadetime)
457                                 a = ((complain_weapon_time + when > time) ? 1 : bound(0, (complain_weapon_time + when + fadetime - time) / fadetime, 1));
458                         else
459                                 a = ((complain_weapon_time + when > time) ? 1 : 0);
460
461                         string s;
462                         if(complain_weapon_type == 0) {
463                                 s = _("Out of ammo");
464                                 color = stov(autocvar_hud_panel_weapons_complainbubble_color_outofammo);
465                         }
466                         else if(complain_weapon_type == 1) {
467                                 s = _("Don't have");
468                                 color = stov(autocvar_hud_panel_weapons_complainbubble_color_donthave);
469                         }
470                         else {
471                                 s = _("Unavailable");
472                                 color = stov(autocvar_hud_panel_weapons_complainbubble_color_unavailable);
473                         }
474                         float padding = autocvar_hud_panel_weapons_complainbubble_padding;
475                         drawpic_aspect_skin(weapon_pos + '1 1 0' * padding, "weapon_complainbubble", weapon_size - '2 2 0' * padding, color, a * panel_fg_alpha, DRAWFLAG_NORMAL);
476                         drawstring_aspect(weapon_pos + '1 1 0' * padding, s, weapon_size - '2 2 0' * padding, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
477                 }
478
479                 #if 0
480                 /// debug code
481                 if(!autocvar_hud_panel_weapons_onlyowned)
482                 {
483                         drawfill(weapon_pos + '1 1 0', weapon_size - '2 2 0', '1 1 1', panel_fg_alpha * 0.2, DRAWFLAG_NORMAL);
484                         drawstring(weapon_pos, ftos(i + 1), label_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
485                 }
486                 #endif
487
488                 // continue with new position for the next weapon
489                 if(vertical_order)
490                 {
491                         ++column;
492                         if(column >= columns)
493                         {
494                                 column = 0;
495                                 ++row;
496                         }
497                 }
498                 else
499                 {
500                         ++row;
501                         if(row >= rows)
502                         {
503                                 row = 0;
504                                 ++column;
505                         }
506                 }
507         }
508 }