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