]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/client/hud/panel/weapons.qc
Merge branch 'master' into terencehill/dynamic_hud
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / hud / panel / weapons.qc
index 8970332cd105def4f719790ddfabd38f217e44ec..7aac7a39e3d418929620db41e6beab51681b3840 100644 (file)
@@ -1,8 +1,10 @@
+#include "weapons.qh"
 // Weapon icons (#0)
 
 entity weaponorder[Weapons_MAX];
 void weaponorder_swap(int i, int j, entity pass)
 {
+    TC(int, i); TC(int, j);
        entity h = weaponorder[i];
        weaponorder[i] = weaponorder[j];
        weaponorder[j] = h;
@@ -11,15 +13,27 @@ void weaponorder_swap(int i, int j, entity pass)
 string weaponorder_cmp_str;
 int weaponorder_cmp(int i, int j, entity pass)
 {
-       int ai, aj;
-       ai = strstrofs(weaponorder_cmp_str, sprintf(" %d ", weaponorder[i].weapon), 0);
-       aj = strstrofs(weaponorder_cmp_str, sprintf(" %d ", weaponorder[j].weapon), 0);
+    TC(int, i); TC(int, j);
+       int ai = strstrofs(weaponorder_cmp_str, sprintf(" %d ", weaponorder[i].m_id), 0);
+       int aj = strstrofs(weaponorder_cmp_str, sprintf(" %d ", weaponorder[j].m_id), 0);
        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)
 }
 
+#define HUD_WEAPONS_GET_FULL_LAYOUT() MACRO_BEGIN { \
+       int nHidden = 0; \
+       FOREACH(Weapons, it != WEP_Null, { \
+               if (weapons_stat & WepSet_FromWeapon(it)) continue; \
+               if (it.spawnflags & WEP_FLAG_MUTATORBLOCKED) nHidden += 1; \
+       }); \
+       vector table_size = HUD_GetTableSize_BestItemAR((Weapons_COUNT - 1) - nHidden, panel_size, aspect); \
+       columns = table_size.x; \
+       rows = table_size.y; \
+       weapon_size.x = panel_size.x / columns; \
+       weapon_size.y = panel_size.y / rows; \
+} MACRO_END
+
 void HUD_Weapons()
 {
-       SELFPARAM();
        // declarations
        WepSet weapons_stat = WepSet_GetFromStat();
        int i;
@@ -77,7 +91,7 @@ void HUD_Weapons()
                weaponorder_cmp_str = strcat(" ", weaponorder_byimpulse, " ");
 
                weapon_cnt = 0;
-               FOREACH(Weapons, it != WEP_Null && it.impulse >= 0, LAMBDA(weaponorder[weapon_cnt++] = it));
+               FOREACH(Weapons, it != WEP_Null && it.impulse >= 0, weaponorder[weapon_cnt++] = it);
                for(i = weapon_cnt; i < Weapons_MAX; ++i)
                        weaponorder[i] = NULL;
                heapsort(weapon_cnt, weaponorder_swap, weaponorder_cmp, NULL);
@@ -91,17 +105,35 @@ void HUD_Weapons()
        if(autocvar__hud_configure)
        {
                if(!weapons_stat)
-                       for(i = WEP_FIRST; i <= WEP_LAST; i += floor((WEP_LAST-WEP_FIRST)/5))
-                               weapons_stat |= WepSet_FromWeapon(Weapons_from(i));
+               {
+                       int j = 0;
+                       FOREACH(Weapons, it != WEP_Null && it.impulse >= 0 && (it.impulse % 3 != 0) && j < 6, {
+                               if(!(it.spawnflags & WEP_FLAG_MUTATORBLOCKED))
+                               {
+                                       weapons_stat |= it.m_wepset;
+                                       ++j;
+                               }
+                       });
+               }
 
                #if 0
                /// debug code
                if(cvar("wep_add"))
                {
+                       int j;
+                       int nHidden = 0;
+                       FOREACH(Weapons, it != WEP_Null, {
+                               if (it.spawnflags & WEP_FLAG_MUTATORBLOCKED) nHidden += 1;
+                       });
                        weapons_stat = '0 0 0';
-                       float countw = 1 + floor((floor(time * cvar("wep_add"))) % (Weapons_COUNT - 1));
-                       for(i = WEP_FIRST; i <= countw; ++i)
-                               weapons_stat |= WepSet_FromWeapon(Weapons_from(i));
+                       float countw = 1 + floor((floor(time * cvar("wep_add"))) % ((Weapons_COUNT - 1) - nHidden));
+                       for(i = 0, j = 0; i <= (Weapons_COUNT - 1) && j < countw; ++i)
+                       {
+                               if(weaponorder[i].spawnflags & WEP_FLAG_MUTATORBLOCKED)
+                                       continue;
+                               weapons_stat |= weaponorder[i].m_wepset;
+                               ++j;
+                       }
                }
                #endif
        }
@@ -118,7 +150,7 @@ void HUD_Weapons()
                // do we own this weapon?
                weapon_count = 0;
                for(i = 0; i <= WEP_LAST-WEP_FIRST; ++i)
-                       if((weapons_stat & WepSet_FromWeapon(Weapons_from(weaponorder[i].weapon))) || (weaponorder[i].weapon == complain_weapon))
+                       if((weapons_stat & WepSet_FromWeapon(weaponorder[i])) || (weaponorder[i].m_id == complain_weapon))
                                ++weapon_count;
 
 
@@ -127,40 +159,29 @@ void HUD_Weapons()
                        return;
 
                vector old_panel_size = panel_size;
-               vector padded_panel_size = panel_size - '2 2 0' * panel_bg_padding;
-
-               // get the all-weapons layout
-               int nHidden = 0;
-               WepSet weapons_stat = WepSet_GetFromStat();
-               FOREACH(Weapons, it != WEP_Null, LAMBDA(
-                       if (weapons_stat & it.m_wepset) continue;
-                       if (it.spawnflags & WEP_FLAG_MUTATORBLOCKED) nHidden += 1;
-               ));
-               vector table_size = HUD_GetTableSize_BestItemAR((Weapons_COUNT - 1) - nHidden, padded_panel_size, aspect);
-               columns = table_size.x;
-               rows = table_size.y;
-               weapon_size.x = padded_panel_size.x / columns;
-               weapon_size.y = padded_panel_size.y / rows;
+               panel_size -= '2 2 0' * panel_bg_padding;
+
+               HUD_WEAPONS_GET_FULL_LAYOUT();
 
                // NOTE: although weapons should aways look the same even if onlyowned is enabled,
                // we enlarge them a bit when possible to better match the desired aspect ratio
-               if(padded_panel_size.x / padded_panel_size.y < aspect)
+               if(panel_size.x / panel_size.y < aspect)
                {
                        // maximum number of rows that allows to display items with the desired aspect ratio
-                       int max_rows = floor(padded_panel_size.y / (weapon_size.x / aspect));
+                       int max_rows = floor(panel_size.y / (weapon_size.x / aspect));
                        columns = min(columns, ceil(weapon_count / max_rows));
                        rows = ceil(weapon_count / columns);
-                       weapon_size.y = min(padded_panel_size.y / rows, weapon_size.x / aspect);
-                       weapon_size.x = min(padded_panel_size.x / columns, aspect * weapon_size.y);
+                       weapon_size.y = min(panel_size.y / rows, weapon_size.x / aspect);
+                       weapon_size.x = min(panel_size.x / columns, aspect * weapon_size.y);
                        vertical_order = false;
                }
                else
                {
-                       int max_columns = floor(padded_panel_size.x / (weapon_size.y * aspect));
+                       int max_columns = floor(panel_size.x / (weapon_size.y * aspect));
                        rows = min(rows, ceil(weapon_count / max_columns));
                        columns = ceil(weapon_count / rows);
-                       weapon_size.x = min(padded_panel_size.x / columns, aspect * weapon_size.y);
-                       weapon_size.y = min(padded_panel_size.y / rows, weapon_size.x / aspect);
+                       weapon_size.x = min(panel_size.x / columns, aspect * weapon_size.y);
+                       weapon_size.y = min(panel_size.y / rows, weapon_size.x / aspect);
                        vertical_order = true;
                }
 
@@ -281,10 +302,14 @@ void HUD_Weapons()
        }
 
        // draw the background, then change the virtual size of it to better fit other items inside
+       if (autocvar_hud_panel_weapons_dynamichud)
+               HUD_Scale_Enable();
+       else
+               HUD_Scale_Disable();
        HUD_Panel_DrawBg(1);
 
        if(center.x == -1)
-               return;
+               return; // panel has gone off screen
 
        if(panel_bg_padding)
        {
@@ -296,11 +321,7 @@ void HUD_Weapons()
 
        if(!rows) // if rows is > 0 onlyowned code has already updated these vars
        {
-               vector table_size = HUD_GetTableSize_BestItemAR((Weapons_COUNT - 1), panel_size, aspect);
-               columns = table_size.x;
-               rows = table_size.y;
-               weapon_size.x = panel_size.x / columns;
-               weapon_size.y = panel_size.y / rows;
+               HUD_WEAPONS_GET_FULL_LAYOUT();
                vertical_order = (panel_size.x / panel_size.y >= aspect);
        }
 
@@ -337,17 +358,23 @@ void HUD_Weapons()
        for(i = 0; i <= WEP_LAST-WEP_FIRST; ++i)
        {
                // retrieve information about the current weapon to be drawn
-               setself(weaponorder[i]);
-               weapon_id = self.impulse;
-               isCurrent = (self.weapon == switchweapon.m_id);
+               entity it = weaponorder[i];
+               weapon_id = it.impulse;
 
                // skip if this weapon doesn't exist
-               if(!self || weapon_id < 0) { continue; }
+               if(!it || weapon_id < 0) { continue; }
 
                // skip this weapon if we don't own it (and onlyowned is enabled)-- or if weapons_complainbubble is showing for this weapon
                if(autocvar_hud_panel_weapons_onlyowned)
-               if (!((weapons_stat & WepSet_FromWeapon(Weapons_from(self.weapon))) || (self.weapon == complain_weapon)))
-                       continue;
+               {
+                       if (!((weapons_stat & WepSet_FromWeapon(it)) || (it.m_id == complain_weapon)))
+                               continue;
+               }
+               else
+               {
+                       if (it.spawnflags & WEP_FLAG_MUTATORBLOCKED && !(weapons_stat & WepSet_FromWeapon(it)))
+                               continue;
+               }
 
                // figure out the drawing position of weapon
                weapon_pos = (panel_pos + eX * column * weapon_size.x + eY * row * weapon_size.y);
@@ -355,13 +382,14 @@ void HUD_Weapons()
                noncurrent_pos.y = weapon_pos.y + (weapon_size.y - noncurrent_size.y) / 2;
 
                // draw background behind currently selected weapon
+               isCurrent = (it == switchweapon);
                if(isCurrent)
                        drawpic_aspect_skin(weapon_pos, "weapon_current_bg", weapon_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
 
                // draw the weapon accuracy
                if(autocvar_hud_panel_weapons_accuracy)
                {
-                       float panel_weapon_accuracy = weapon_accuracy[self.weapon-WEP_FIRST];
+                       float panel_weapon_accuracy = weapon_accuracy[it.m_id-WEP_FIRST];
                        if(panel_weapon_accuracy >= 0)
                        {
                                color = Accuracy_GetColor(panel_weapon_accuracy);
@@ -370,13 +398,13 @@ void HUD_Weapons()
                }
 
                // drawing all the weapon items
-               if(weapons_stat & WepSet_FromWeapon(Weapons_from(self.weapon)))
+               if(weapons_stat & WepSet_FromWeapon(it))
                {
                        // draw the weapon image
                        if(isCurrent)
-                               drawpic_aspect_skin(weapon_pos, self.model2, weapon_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
+                               drawpic_aspect_skin(weapon_pos, it.model2, weapon_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
                        else
-                               drawpic_aspect_skin(noncurrent_pos, self.model2, noncurrent_size, '1 1 1', noncurrent_alpha, DRAWFLAG_NORMAL);
+                               drawpic_aspect_skin(noncurrent_pos, it.model2, noncurrent_size, '1 1 1', noncurrent_alpha, DRAWFLAG_NORMAL);
 
                        // draw weapon label string
                        switch(autocvar_hud_panel_weapons_label)
@@ -390,7 +418,7 @@ void HUD_Weapons()
                                        break;
 
                                case 3: // weapon name
-                                       drawstring(weapon_pos, strtolower(self.m_name), label_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
+                                       drawstring(weapon_pos, strtolower(it.m_name), label_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
                                        break;
 
                                default: // nothing
@@ -398,14 +426,14 @@ void HUD_Weapons()
                        }
 
                        // draw ammo status bar
-                       if(autocvar_hud_panel_weapons_ammo && (self.ammo_field != ammo_none))
+                       if(autocvar_hud_panel_weapons_ammo && (it.ammo_field != ammo_none))
                        {
                                float ammo_full;
-                               a = getstati(GetAmmoStat(self.ammo_field)); // how much ammo do we have?
+                               a = getstati(GetAmmoStat(it.ammo_field)); // how much ammo do we have?
 
                                if(a > 0)
                                {
-                                       switch(self.ammo_field)
+                                       switch(it.ammo_field)
                                        {
                                                case ammo_shells:  ammo_full = autocvar_hud_panel_weapons_ammo_full_shells;  break;
                                                case ammo_nails:   ammo_full = autocvar_hud_panel_weapons_ammo_full_nails;   break;
@@ -438,11 +466,11 @@ void HUD_Weapons()
                }
                else // draw a "ghost weapon icon" if you don't have the weapon
                {
-                       drawpic_aspect_skin(noncurrent_pos, self.model2, noncurrent_size, '0.2 0.2 0.2', panel_fg_alpha * 0.5, DRAWFLAG_NORMAL);
+                       drawpic_aspect_skin(noncurrent_pos, it.model2, noncurrent_size, '0.2 0.2 0.2', panel_fg_alpha * 0.5, DRAWFLAG_NORMAL);
                }
 
                // draw the complain message
-               if(self.weapon == complain_weapon)
+               if(it.m_id == complain_weapon)
                {
                        if(fadetime)
                                a = ((complain_weapon_time + when > time) ? 1 : bound(0, (complain_weapon_time + when + fadetime - time) / fadetime, 1));