3 #include <client/autocvars.qh>
4 #include <client/defs.qh>
5 #include <client/miscfunctions.qh>
6 #include <client/view.qh>
7 #include <common/wepent.qh>
11 entity weaponorder[Weapons_MAX];
12 void weaponorder_swap(int i, int j, entity pass)
14 TC(int, i); TC(int, j);
15 entity h = weaponorder[i];
16 weaponorder[i] = weaponorder[j];
20 string weaponorder_cmp_str;
21 int weaponorder_cmp(int i, int j, entity pass)
23 TC(int, i); TC(int, j);
24 int ai = strstrofs(weaponorder_cmp_str, sprintf(" %d ", weaponorder[i].m_id), 0);
25 int aj = strstrofs(weaponorder_cmp_str, sprintf(" %d ", weaponorder[j].m_id), 0);
26 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)
29 #define HUD_WEAPONS_GET_FULL_LAYOUT() MACRO_BEGIN \
31 FOREACH(Weapons, it != WEP_Null, { \
32 if (weapons_stat & WepSet_FromWeapon(it)) continue; \
33 if (it.spawnflags & (WEP_FLAG_HIDDEN | WEP_FLAG_MUTATORBLOCKED | WEP_FLAG_SPECIALATTACK)) nHidden += 1; \
35 vector table_size = HUD_GetTableSize_BestItemAR((Weapons_COUNT - 1) - nHidden, panel_size, aspect); \
36 columns = table_size.x; \
37 rows = table_size.y; \
38 weapon_size.x = panel_size.x / columns; \
39 weapon_size.y = panel_size.y / rows; \
42 string cl_weaponpriority_old;
43 bool weapons_orderbyimpulse_old;
47 WepSet weapons_stat = WepSet_GetFromStat();
51 vector center = '0 0 0';
52 int weapon_count, weapon_id;
53 int row, column, rows = 0, columns = 0;
54 bool vertical_order = true;
55 float aspect = autocvar_hud_panel_weapons_aspect;
57 float timeout = autocvar_hud_panel_weapons_timeout;
58 float timein_effect_length = autocvar_hud_panel_weapons_timeout_speed_in; //? 0.375 : 0);
59 float timeout_effect_length = autocvar_hud_panel_weapons_timeout_speed_out; //? 0.75 : 0);
61 vector barsize = '0 0 0', baroffset = '0 0 0';
62 vector ammo_color = '1 0 1';
65 float when = max(1, autocvar_hud_panel_weapons_complainbubble_time);
66 float fadetime = max(0, autocvar_hud_panel_weapons_complainbubble_fadetime);
68 bool infinite_ammo = (STAT(ITEMS) & IT_UNLIMITED_AMMO);
70 vector weapon_pos, weapon_size = '0 0 0';
73 entity panel_switchweapon = NULL;
75 // check to see if we want to continue
76 if(hud != HUD_NORMAL) return;
78 if(!autocvar__hud_configure)
80 if((!autocvar_hud_panel_weapons) || (spectatee_status == -1))
82 if(STAT(HEALTH) <= 0 && autocvar_hud_panel_weapons_hide_ondeath)
84 if(timeout && time >= weapontime + timeout + timeout_effect_length)
85 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)))
87 weaponprevtime = time;
92 // update generic hud functions
93 HUD_Panel_LoadCvars();
95 if(cl_weaponpriority_old != autocvar_cl_weaponpriority || weapons_orderbyimpulse_old != autocvar_hud_panel_weapons_orderbyimpulse || weaponorder[0] == NULL)
97 weapons_orderbyimpulse_old = autocvar_hud_panel_weapons_orderbyimpulse;
98 strcpy(cl_weaponpriority_old, autocvar_cl_weaponpriority);
99 string weporder = W_FixWeaponOrder_ForceComplete(W_NumberWeaponOrder(cl_weaponpriority_old));
100 if(autocvar_hud_panel_weapons_orderbyimpulse)
102 weporder = W_FixWeaponOrder_BuildImpulseList(weporder);
105 weaponorder_cmp_str = strcat(" ", weporder, " ");
108 FOREACH(Weapons, it != WEP_Null && it.impulse >= 0, weaponorder[weapon_cnt++] = it);
109 for(i = weapon_cnt; i < Weapons_MAX; ++i)
110 weaponorder[i] = NULL;
111 heapsort(weapon_cnt, weaponorder_swap, weaponorder_cmp, NULL);
113 weaponorder_cmp_str = string_null;
116 if(!autocvar_hud_panel_weapons_complainbubble || autocvar__hud_configure || time - complain_weapon_time >= when + fadetime)
117 complain_weapon = NULL;
119 entity wepent = viewmodels[0]; // TODO: unhardcode
121 if (wepent.switchweapon == WEP_Null)
122 panel_switchweapon = NULL;
123 else if (!panel_switchweapon)
124 panel_switchweapon = wepent.switchweapon;
126 if(autocvar__hud_configure)
131 FOREACH(Weapons, it != WEP_Null && it.impulse >= 0 && (it.impulse % 3 != 0) && j < 6, {
132 if(!(it.spawnflags & WEP_FLAG_MUTATORBLOCKED) && !(it.spawnflags & WEP_FLAG_SPECIALATTACK))
134 if(!panel_switchweapon || j < 4)
135 panel_switchweapon = it;
136 weapons_stat |= it.m_wepset;
148 FOREACH(Weapons, it != WEP_Null, {
149 if (it.spawnflags & WEP_FLAG_MUTATORBLOCKED) nHidden += 1;
151 weapons_stat = '0 0 0';
152 float countw = 1 + floor((floor(time * cvar("wep_add"))) % ((Weapons_COUNT - 1) - nHidden));
153 for(i = 0, j = 0; i <= (Weapons_COUNT - 1) && j < countw; ++i)
155 if(weaponorder[i].spawnflags & WEP_FLAG_MUTATORBLOCKED)
157 weapons_stat |= weaponorder[i].m_wepset;
164 // determine which weapons are going to be shown
165 if (autocvar_hud_panel_weapons_onlyowned)
167 if(autocvar__hud_configure)
169 if(hud_configure_menu_open != 2)
170 HUD_Panel_DrawBg(); // also draw the bg of the entire panel
173 // do we own this weapon?
175 if (autocvar_hud_panel_weapons_onlyowned >= 2) // only current
177 for (i = 0; i <= WEP_LAST-WEP_FIRST; ++i)
178 if (weaponorder[i] == panel_switchweapon || weaponorder[i] == complain_weapon)
183 for (i = 0; i <= WEP_LAST-WEP_FIRST; ++i)
184 if ((weapons_stat & WepSet_FromWeapon(weaponorder[i])) || weaponorder[i] == complain_weapon)
188 // might as well commit suicide now, no reason to live ;)
189 if (weapon_count == 0)
192 vector old_panel_size = panel_size;
193 panel_size -= '2 2 0' * panel_bg_padding;
195 HUD_WEAPONS_GET_FULL_LAYOUT();
197 // NOTE: although weapons should aways look the same even if onlyowned is enabled,
198 // we enlarge them a bit when possible to better match the desired aspect ratio
199 if(panel_size.x / panel_size.y < aspect)
201 // maximum number of rows that allows to display items with the desired aspect ratio
202 int max_rows = floor(panel_size.y / (weapon_size.x / aspect));
203 columns = min(columns, ceil(weapon_count / max_rows));
204 rows = ceil(weapon_count / columns);
205 weapon_size.y = min(panel_size.y / rows, weapon_size.x / aspect);
206 weapon_size.x = min(panel_size.x / columns, aspect * weapon_size.y);
207 vertical_order = false;
211 int max_columns = floor(panel_size.x / (weapon_size.y * aspect));
212 rows = min(rows, ceil(weapon_count / max_columns));
213 columns = ceil(weapon_count / rows);
214 weapon_size.x = min(panel_size.x / columns, aspect * weapon_size.y);
215 weapon_size.y = min(panel_size.y / rows, weapon_size.x / aspect);
216 vertical_order = true;
219 // reduce size of the panel
220 panel_size.x = columns * weapon_size.x;
221 panel_size.y = rows * weapon_size.y;
222 panel_size += '2 2 0' * panel_bg_padding;
224 // center the resized panel, or snap it to the screen edge when close enough
225 if(panel_pos.x > vid_conwidth * 0.001)
227 if(panel_pos.x + old_panel_size.x > vid_conwidth * 0.999)
228 panel_pos.x += old_panel_size.x - panel_size.x;
230 panel_pos.x += (old_panel_size.x - panel_size.x) / 2;
232 else if(old_panel_size.x > vid_conwidth * 0.999)
233 panel_pos.x += (old_panel_size.x - panel_size.x) / 2;
235 if(panel_pos.y > vid_conheight * 0.001)
237 if(panel_pos.y + old_panel_size.y > vid_conheight * 0.999)
238 panel_pos.y += old_panel_size.y - panel_size.y;
240 panel_pos.y += (old_panel_size.y - panel_size.y) / 2;
242 else if(old_panel_size.y > vid_conheight * 0.999)
243 panel_pos.y += (old_panel_size.y - panel_size.y) / 2;
246 weapon_count = (Weapons_COUNT - 1);
248 // animation for fading in/out the panel respectively when not in use
249 if(!autocvar__hud_configure)
251 if (timeout && time >= weapontime + timeout) // apply timeout effect if needed
253 f = bound(0, (time - (weapontime + timeout)) / timeout_effect_length, 1);
255 // fade the panel alpha
256 if(autocvar_hud_panel_weapons_timeout_effect == 1)
258 panel_bg_alpha *= (autocvar_hud_panel_weapons_timeout_fadebgmin * f + (1 - f));
259 panel_fg_alpha *= (autocvar_hud_panel_weapons_timeout_fadefgmin * f + (1 - f));
261 else if(autocvar_hud_panel_weapons_timeout_effect == 3)
263 panel_bg_alpha *= (1 - f);
264 panel_fg_alpha *= (1 - f);
267 // move the panel off the screen
268 if (autocvar_hud_panel_weapons_timeout_effect == 2 || autocvar_hud_panel_weapons_timeout_effect == 3)
270 f *= f; // for a cooler movement
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
276 if ((vid_conwidth - center.x)/center.y < screen_ar) //bottom
277 panel_pos.y += f * (vid_conheight - panel_pos.y);
279 panel_pos.x -= f * (panel_pos.x + panel_size.x);
283 if ((vid_conwidth - center.x)/center.y < screen_ar) //right
284 panel_pos.x += f * (vid_conwidth - panel_pos.x);
286 panel_pos.y -= f * (panel_pos.y + panel_size.y);
289 center.x = -1; // mark the panel as off screen
291 weaponprevtime = time - (1 - f) * timein_effect_length;
293 else if (timeout && time < weaponprevtime + timein_effect_length) // apply timein effect if needed
295 f = bound(0, (time - weaponprevtime) / timein_effect_length, 1);
297 // fade the panel alpha
298 if(autocvar_hud_panel_weapons_timeout_effect == 1)
300 panel_bg_alpha *= (autocvar_hud_panel_weapons_timeout_fadebgmin * (1 - f) + f);
301 panel_fg_alpha *= (autocvar_hud_panel_weapons_timeout_fadefgmin * (1 - f) + f);
303 else if(autocvar_hud_panel_weapons_timeout_effect == 3)
305 panel_bg_alpha *= (f);
306 panel_fg_alpha *= (f);
309 // move the panel back on screen
310 if (autocvar_hud_panel_weapons_timeout_effect == 2 || autocvar_hud_panel_weapons_timeout_effect == 3)
312 f *= f; // for a cooler movement
314 center.x = panel_pos.x + panel_size.x/2;
315 center.y = panel_pos.y + panel_size.y/2;
316 screen_ar = vid_conwidth/vid_conheight;
317 if (center.x/center.y < screen_ar) //bottom left
319 if ((vid_conwidth - center.x)/center.y < screen_ar) //bottom
320 panel_pos.y += f * (vid_conheight - panel_pos.y);
322 panel_pos.x -= f * (panel_pos.x + panel_size.x);
326 if ((vid_conwidth - center.x)/center.y < screen_ar) //right
327 panel_pos.x += f * (vid_conwidth - panel_pos.x);
329 panel_pos.y -= f * (panel_pos.y + panel_size.y);
335 // draw the background, then change the virtual size of it to better fit other items inside
336 if (autocvar_hud_panel_weapons_dynamichud)
343 return; // panel has gone off screen
347 panel_pos += '1 1 0' * panel_bg_padding;
348 panel_size -= '2 2 0' * panel_bg_padding;
351 // after the sizing and animations are done, update the other values
353 if(!rows) // if rows is > 0 onlyowned code has already updated these vars
355 HUD_WEAPONS_GET_FULL_LAYOUT();
356 vertical_order = (panel_size.x / panel_size.y >= aspect);
359 // calculate position/size for visual bar displaying ammount of ammo status
360 if (!infinite_ammo && autocvar_hud_panel_weapons_ammo)
362 ammo_color = stov(autocvar_hud_panel_weapons_ammo_color);
363 ammo_alpha = panel_fg_alpha * autocvar_hud_panel_weapons_ammo_alpha;
365 if(weapon_size.x/weapon_size.y > aspect)
367 barsize.x = aspect * weapon_size.y;
368 barsize.y = weapon_size.y;
369 baroffset.x = (weapon_size.x - barsize.x) / 2;
373 barsize.y = 1/aspect * weapon_size.x;
374 barsize.x = weapon_size.x;
375 baroffset.y = (weapon_size.y - barsize.y) / 2;
378 if(autocvar_hud_panel_weapons_accuracy)
379 Accuracy_LoadColors();
383 vector label_size = '1 1 0' * min(weapon_size.x, weapon_size.y) * bound(0, autocvar_hud_panel_weapons_label_scale, 1);
384 vector noncurrent_size = weapon_size * bound(0.01, autocvar_hud_panel_weapons_noncurrent_scale, 1);
385 float noncurrent_alpha = panel_fg_alpha * bound(0, autocvar_hud_panel_weapons_noncurrent_alpha, 1);
386 static vector weapon_pos_current = '-1 0 0';
387 if(weapon_pos_current.x == -1)
388 weapon_pos_current = panel_pos;
391 if(autocvar_hud_panel_weapons_selection_speed <= 0 || autocvar__hud_configure)
394 switch_speed = frametime * autocvar_hud_panel_weapons_selection_speed;
395 vector radius_size = weapon_size * (autocvar_hud_panel_weapons_selection_radius + 1);
397 // draw background behind currently selected weapon
398 // do it earlier to make sure bg is drawn behind every weapon icons while it's moving
399 if(panel_switchweapon)
400 drawpic_aspect_skin(weapon_pos_current, "weapon_current_bg", weapon_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
402 for(i = 0; i <= WEP_LAST-WEP_FIRST; ++i)
404 // retrieve information about the current weapon to be drawn
405 entity it = weaponorder[i];
406 weapon_id = it.impulse;
408 // skip if this weapon doesn't exist
409 if(!it || weapon_id < 0) { continue; }
411 // skip this weapon if we don't own it (and onlyowned is enabled)-- or if weapons_complainbubble is showing for this weapon
412 if (autocvar_hud_panel_weapons_onlyowned)
414 if (autocvar_hud_panel_weapons_onlyowned >= 2) // only current
416 if (!(it == panel_switchweapon || it == complain_weapon))
421 if (!((weapons_stat & WepSet_FromWeapon(it)) || (it == complain_weapon)))
427 if (it.spawnflags & (WEP_FLAG_HIDDEN | WEP_FLAG_MUTATORBLOCKED | WEP_FLAG_SPECIALATTACK)
428 && !(weapons_stat & WepSet_FromWeapon(it)))
434 // figure out the drawing position of weapon
435 weapon_pos = panel_pos + vec2(column * weapon_size.x, row * weapon_size.y);
437 // update position of the currently selected weapon
438 if(it == panel_switchweapon)
440 if(weapon_pos_current.y > weapon_pos.y)
441 weapon_pos_current.y = max(weapon_pos.y, weapon_pos_current.y - switch_speed * (weapon_pos_current.y - weapon_pos.y));
442 else if(weapon_pos_current.y < weapon_pos.y)
443 weapon_pos_current.y = min(weapon_pos.y, weapon_pos_current.y + switch_speed * (weapon_pos.y - weapon_pos_current.y));
444 if(weapon_pos_current.x > weapon_pos.x)
445 weapon_pos_current.x = max(weapon_pos.x, weapon_pos_current.x - switch_speed * (weapon_pos_current.x - weapon_pos.x));
446 else if(weapon_pos_current.x < weapon_pos.x)
447 weapon_pos_current.x = min(weapon_pos.x, weapon_pos_current.x + switch_speed * (weapon_pos.x - weapon_pos_current.x));
450 // draw the weapon accuracy
451 if(autocvar_hud_panel_weapons_accuracy)
453 float panel_weapon_accuracy = weapon_accuracy[it.m_id-WEP_FIRST];
454 if(panel_weapon_accuracy >= 0)
456 color = Accuracy_GetColor(panel_weapon_accuracy);
457 drawpic_aspect_skin(weapon_pos, "weapon_accuracy", weapon_size, color, panel_fg_alpha, DRAWFLAG_NORMAL);
461 vector weapon_size_real = noncurrent_size;
462 float weapon_alpha_real = noncurrent_alpha;
463 float radius_factor_x = 1 - bound(0, fabs(weapon_pos.x - weapon_pos_current.x) / radius_size.x, 1);
464 float radius_factor_y = 1 - bound(0, fabs(weapon_pos.y - weapon_pos_current.y) / radius_size.y, 1);
465 if(radius_factor_x || radius_factor_y)
467 weapon_size_real.x += (weapon_size.x - noncurrent_size.x) * radius_factor_x;
468 weapon_size_real.y += (weapon_size.y - noncurrent_size.y) * radius_factor_y;
469 weapon_alpha_real += (panel_fg_alpha - noncurrent_alpha) * min(radius_factor_x, radius_factor_y);
472 vector weapon_pos_real = weapon_pos;
473 weapon_pos_real.x = weapon_pos.x + (weapon_size.x - weapon_size_real.x) / 2;
474 weapon_pos_real.y = weapon_pos.y + (weapon_size.y - weapon_size_real.y) / 2;
476 // drawing all the weapon items
477 if(weapons_stat & WepSet_FromWeapon(it))
479 // draw the weapon image
480 drawpic_aspect_skin(weapon_pos_real, it.model2, weapon_size_real, '1 1 1', weapon_alpha_real, DRAWFLAG_NORMAL);
482 // draw weapon label string
483 switch(autocvar_hud_panel_weapons_label)
485 case 1: // weapon number
486 drawstring(weapon_pos, ftos(weapon_id), label_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
490 drawstring(weapon_pos, getcommandkey(ftos(weapon_id), strcat("weapon_group_", ftos(weapon_id))), label_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
493 case 3: // weapon name
494 drawstring(weapon_pos, strtolower(it.m_name), label_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
501 // draw ammo status bar
502 if(!infinite_ammo && autocvar_hud_panel_weapons_ammo && (it.ammo_type != RES_NONE))
505 a = getstati(GetAmmoStat(it.ammo_type)); // how much ammo do we have?
509 switch (it.ammo_type)
511 case RES_SHELLS: ammo_full = autocvar_hud_panel_weapons_ammo_full_shells; break;
512 case RES_BULLETS: ammo_full = autocvar_hud_panel_weapons_ammo_full_nails; break;
513 case RES_ROCKETS: ammo_full = autocvar_hud_panel_weapons_ammo_full_rockets; break;
514 case RES_CELLS: ammo_full = autocvar_hud_panel_weapons_ammo_full_cells; break;
515 case RES_PLASMA: ammo_full = autocvar_hud_panel_weapons_ammo_full_plasma; break;
516 case RES_FUEL: ammo_full = autocvar_hud_panel_weapons_ammo_full_fuel; break;
517 default: ammo_full = 60;
521 weapon_pos.x + baroffset.x,
522 weapon_pos.y + baroffset.y,
523 barsize.x * bound(0, a/ammo_full, 1),
540 else // draw a "ghost weapon icon" if you don't have the weapon
542 drawpic_aspect_skin(weapon_pos_real, it.model2, weapon_size_real, '0.2 0.2 0.2', weapon_alpha_real * 0.5, DRAWFLAG_NORMAL);
545 // draw the complain message
546 if(it == complain_weapon)
549 a = ((complain_weapon_time + when > time) ? 1 : bound(0, (complain_weapon_time + when + fadetime - time) / fadetime, 1));
551 a = ((complain_weapon_time + when > time) ? 1 : 0);
554 if(complain_weapon_type == 0) {
555 s = _("Out of ammo");
556 color = stov(autocvar_hud_panel_weapons_complainbubble_color_outofammo);
558 else if(complain_weapon_type == 1) {
560 color = stov(autocvar_hud_panel_weapons_complainbubble_color_donthave);
563 s = _("Unavailable");
564 color = stov(autocvar_hud_panel_weapons_complainbubble_color_unavailable);
566 float padding = autocvar_hud_panel_weapons_complainbubble_padding;
567 drawpic_aspect_skin(weapon_pos + '1 1 0' * padding, "weapon_complainbubble", weapon_size - '2 2 0' * padding, color, a * panel_fg_alpha, DRAWFLAG_NORMAL);
568 drawstring_aspect(weapon_pos + '1 1 0' * padding, s, weapon_size - '2 2 0' * padding, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
573 if(!autocvar_hud_panel_weapons_onlyowned)
575 drawfill(weapon_pos + '1 1 0', weapon_size - '2 2 0', '1 1 1', panel_fg_alpha * 0.2, DRAWFLAG_NORMAL);
576 drawstring(weapon_pos, ftos(i + 1), label_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
580 // continue with new position for the next weapon
584 if(column >= columns)