From e83b67be533ddb7349ab2346601caf6cf3a537c5 Mon Sep 17 00:00:00 2001
From: otta8634 <k9wolf@pm.me>
Date: Mon, 23 Dec 2024 19:19:07 +0800
Subject: [PATCH] Refactor and reformat weapons hud code a bit

Having almost all code in the one function made it a bit difficult to understand.
---
 qcsrc/client/hud/panel/weapons.qc | 394 ++++++++++++++++--------------
 qcsrc/client/hud/panel/weapons.qh |   6 +-
 2 files changed, 209 insertions(+), 191 deletions(-)

diff --git a/qcsrc/client/hud/panel/weapons.qc b/qcsrc/client/hud/panel/weapons.qc
index fe0b84f702..ff0753f34c 100644
--- a/qcsrc/client/hud/panel/weapons.qc
+++ b/qcsrc/client/hud/panel/weapons.qc
@@ -37,28 +37,26 @@ void HUD_Weapons_Export(int fh)
 
 void Accuracy_LoadLevels()
 {
-	if(autocvar_accuracy_color_levels != acc_color_levels)
+	if (autocvar_accuracy_color_levels != acc_color_levels)
 	{
 		strcpy(acc_color_levels, autocvar_accuracy_color_levels);
 		acc_levels = tokenize_console(acc_color_levels);
-		if(acc_levels > MAX_ACCURACY_LEVELS)
+		if (acc_levels > MAX_ACCURACY_LEVELS)
 			acc_levels = MAX_ACCURACY_LEVELS;
-		if(acc_levels < 2)
+		if (acc_levels < 2)
 			LOG_INFO("Warning: accuracy_color_levels must contain at least 2 values");
 
-		int i;
-		for(i = 0; i < acc_levels; ++i)
+		for (int i = 0; i < acc_levels; ++i)
 			acc_lev[i] = stof(argv(i)) / 100.0;
 	}
 }
 
 void Accuracy_LoadColors()
 {
-	if(time > acc_col_loadtime)
-	if(acc_levels >= 2)
+	if (time > acc_col_loadtime)
+	if (acc_levels >= 2)
 	{
-		int i;
-		for(i = 0; i < acc_levels; ++i)
+		for (int i = 0; i < acc_levels; ++i)
 			acc_col[i] = stov(cvar_string(strcat("accuracy_color", ftos(i))));
 		acc_col_loadtime = time + 2;
 	}
@@ -66,19 +64,17 @@ void Accuracy_LoadColors()
 
 vector Accuracy_GetColor(float accuracy)
 {
-	float factor;
-	vector color;
-	if(acc_levels < 2)
+	if (acc_levels < 2)
 		return '0 0 0'; // return black, can't determine the right color
 
 	// find the max level lower than acc
 	int j = acc_levels-1;
-	while(j && accuracy < acc_lev[j])
+	while (j && accuracy < acc_lev[j])
 		--j;
 
 	// inject color j+1 in color j, how much depending on how much accuracy is higher than level j
-	factor = (accuracy - acc_lev[j]) / (acc_lev[j+1] - acc_lev[j]);
-	color = acc_col[j];
+	float factor = (accuracy - acc_lev[j]) / (acc_lev[j+1] - acc_lev[j]);
+	vector color = acc_col[j];
 	color = color + factor * (acc_col[j+1] - color);
 	return color;
 }
@@ -118,77 +114,61 @@ string cl_weaponpriority_old;
 bool weapons_orderbyimpulse_old;
 void HUD_Weapons()
 {
-	// declarations
-	WepSet weapons_stat = WepSet_GetFromStat();
-	int i;
-	float f, a;
-	float screen_ar;
-	vector center = '0 0 0';
-	int weapon_count, weapon_id;
-	int row, column, rows = 0, columns = 0;
-	bool vertical_order = true;
-	float aspect = max(0.001, autocvar_hud_panel_weapons_aspect);
-
-	float timeout = autocvar_hud_panel_weapons_timeout;
-	float timein_effect_length = autocvar_hud_panel_weapons_timeout_speed_in; //? 0.375 : 0);
-	float timeout_effect_length = autocvar_hud_panel_weapons_timeout_speed_out; //? 0.75 : 0);
-
-	vector barsize = '0 0 0', baroffset = '0 0 0';
-	vector ammo_color = '1 0 1';
-	float ammo_alpha = 1;
-
-	float when = max(1, autocvar_hud_panel_weapons_complainbubble_time);
-	float fadetime = max(0, autocvar_hud_panel_weapons_complainbubble_fadetime);
-
-	bool infinite_ammo = (STAT(ITEMS) & IT_UNLIMITED_AMMO);
-
-	vector weapon_pos, weapon_size = '0 0 0';
-	vector color;
-
-	entity panel_switchweapon = NULL;
-
 	// check to see if we want to continue
-	if(hud != HUD_NORMAL) return;
+	if (hud != HUD_NORMAL) return;
 
-	if(!autocvar__hud_configure)
+	if (!autocvar__hud_configure)
 	{
-		if((!autocvar_hud_panel_weapons) || (spectatee_status == -1))
+		if ((!autocvar_hud_panel_weapons) || (spectatee_status == -1))
 			return;
-		if(STAT(HEALTH) <= 0 && autocvar_hud_panel_weapons_hide_ondeath)
+		if (STAT(HEALTH) <= 0 && autocvar_hud_panel_weapons_hide_ondeath)
 			return;
-		if(timeout && time >= weapontime + timeout + timeout_effect_length)
-		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)))
+
+		float timeout = autocvar_hud_panel_weapons_timeout;
+		float timeout_effect_length = autocvar_hud_panel_weapons_timeout_speed_out; //? 0.75 : 0);
+
+		if (timeout && time >= weapontime + timeout + timeout_effect_length)
+		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)))
 		{
 			weaponprevtime = time;
 			return;
 		}
 	}
 
+	// declarations
+	WepSet weapons_stat = WepSet_GetFromStat();
+	entity panel_switchweapon = NULL;
+	vector center = '0 0 0';
+
+	float when = max(1, autocvar_hud_panel_weapons_complainbubble_time);
+	float fadetime = max(0, autocvar_hud_panel_weapons_complainbubble_fadetime);
+
+	vector weapon_size = '0 0 0';
+
 	// update generic hud functions
 	HUD_Panel_LoadCvars();
 
-	if(cl_weaponpriority_old != autocvar_cl_weaponpriority || weapons_orderbyimpulse_old != autocvar_hud_panel_weapons_orderbyimpulse || weaponorder[0] == NULL)
+	if (cl_weaponpriority_old != autocvar_cl_weaponpriority || weapons_orderbyimpulse_old != autocvar_hud_panel_weapons_orderbyimpulse || weaponorder[0] == NULL)
 	{
 		weapons_orderbyimpulse_old = autocvar_hud_panel_weapons_orderbyimpulse;
 		strcpy(cl_weaponpriority_old, autocvar_cl_weaponpriority);
 		string weporder = W_FixWeaponOrder_ForceComplete(W_NumberWeaponOrder(cl_weaponpriority_old));
-		if(autocvar_hud_panel_weapons_orderbyimpulse)
-		{
+		if (autocvar_hud_panel_weapons_orderbyimpulse)
 			weporder = W_FixWeaponOrder_BuildImpulseList(weporder);
-		}
 
 		weaponorder_cmp_str = strcat(" ", weporder, " ");
 
 		int weapon_cnt = 0;
 		FOREACH(Weapons, it != WEP_Null && it.impulse >= 0, weaponorder[weapon_cnt++] = it);
-		for(i = weapon_cnt; i < REGISTRY_MAX(Weapons); ++i)
+		for (int i = weapon_cnt; i < REGISTRY_MAX(Weapons); ++i)
 			weaponorder[i] = NULL;
 		heapsort(weapon_cnt, weaponorder_swap, weaponorder_cmp, NULL);
 
 		weaponorder_cmp_str = string_null;
 	}
 
-	if(!autocvar_hud_panel_weapons_complainbubble || autocvar__hud_configure || time - complain_weapon_time >= when + fadetime)
+	if (!autocvar_hud_panel_weapons_complainbubble || autocvar__hud_configure || time - complain_weapon_time >= when + fadetime)
 		complain_weapon = NULL;
 
 	entity wepent = viewmodels[0]; // TODO: unhardcode
@@ -198,15 +178,15 @@ void HUD_Weapons()
 	else if (!panel_switchweapon)
 		panel_switchweapon = wepent.switchweapon;
 
-	if(autocvar__hud_configure)
+	if (autocvar__hud_configure)
 	{
-		if(!weapons_stat)
+		if (!weapons_stat)
 		{
 			int j = 0;
 			FOREACH(Weapons, it != WEP_Null && it.impulse >= 0 && (it.impulse % 3 != 0) && j < 6, {
-				if(!(it.spawnflags & WEP_FLAG_MUTATORBLOCKED) && !(it.spawnflags & WEP_FLAG_SPECIALATTACK))
+				if (!(it.spawnflags & WEP_FLAG_MUTATORBLOCKED) && !(it.spawnflags & WEP_FLAG_SPECIALATTACK))
 				{
-					if(!panel_switchweapon || j < 4)
+					if (!panel_switchweapon || j < 4)
 						panel_switchweapon = it;
 					weapons_stat |= it.m_wepset;
 					++j;
@@ -216,18 +196,17 @@ void HUD_Weapons()
 
 		#if 0
 		/// debug code
-		if(cvar("wep_add"))
+		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"))) % ((REGISTRY_COUNT(Weapons) - 1) - nHidden));
-			for(i = 0, j = 0; i <= (REGISTRY_COUNT(Weapons) - 1) && j < countw; ++i)
+			for (int i = 0, j = 0; i <= (REGISTRY_COUNT(Weapons) - 1) && j < countw; ++i)
 			{
-				if(weaponorder[i].spawnflags & WEP_FLAG_MUTATORBLOCKED)
+				if (weaponorder[i].spawnflags & WEP_FLAG_MUTATORBLOCKED)
 					continue;
 				weapons_stat |= weaponorder[i].m_wepset;
 				++j;
@@ -237,11 +216,15 @@ void HUD_Weapons()
 	}
 
 	// determine which weapons are going to be shown
+	int weapon_count;
+	int rows = 0, columns = 0;
+	bool vertical_order = true;
+	float aspect = max(0.001, autocvar_hud_panel_weapons_aspect);
 	if (autocvar_hud_panel_weapons_onlyowned)
 	{
-		if(autocvar__hud_configure)
+		if (autocvar__hud_configure)
 		{
-			if(hud_configure_menu_open != 2)
+			if (hud_configure_menu_open != 2)
 				HUD_Panel_DrawBg(); // also draw the bg of the entire panel
 		}
 
@@ -249,13 +232,13 @@ void HUD_Weapons()
 		weapon_count = 0;
 		if (autocvar_hud_panel_weapons_onlyowned >= 2) // only current
 		{
-			for (i = 0; i <= WEP_LAST-WEP_FIRST; ++i)
+			for (int i = 0; i <= WEP_LAST-WEP_FIRST; ++i)
 				if (weaponorder[i] == panel_switchweapon || weaponorder[i] == complain_weapon)
 					++weapon_count;
 		}
 		else
 		{
-			for (i = 0; i <= WEP_LAST-WEP_FIRST; ++i)
+			for (int i = 0; i <= WEP_LAST-WEP_FIRST; ++i)
 				if ((weapons_stat & WepSet_FromWeapon(weaponorder[i])) || weaponorder[i] == complain_weapon)
 					++weapon_count;
 		}
@@ -271,24 +254,23 @@ void HUD_Weapons()
 
 		// 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(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(panel_size.y / (weapon_size.x / aspect));
-			columns = min(columns, ceil(weapon_count / max_rows));
-			rows = ceil(weapon_count / columns);
-			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
+		vertical_order = panel_size.x / panel_size.y >= aspect;
+		if (vertical_order)
 		{
+			// maximum number of columns that allows to display items with the desired aspect ratio
 			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(panel_size.x / columns, aspect * weapon_size.y);
 			weapon_size.y = min(panel_size.y / rows, weapon_size.x / aspect);
-			vertical_order = true;
+		}
+		else
+		{
+			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(panel_size.y / rows, weapon_size.x / aspect);
+			weapon_size.x = min(panel_size.x / columns, aspect * weapon_size.y);
 		}
 
 		// reduce size of the panel
@@ -297,116 +279,137 @@ void HUD_Weapons()
 		panel_size += '2 2 0' * panel_bg_padding;
 
 		// center the resized panel, or snap it to the screen edge when close enough
-		if(panel_pos.x > vid_conwidth * 0.001)
+		if (panel_pos.x > vid_conwidth * 0.001)
 		{
 			if(panel_pos.x + old_panel_size.x > vid_conwidth * 0.999)
 				panel_pos.x += old_panel_size.x - panel_size.x;
 			else
 				panel_pos.x += (old_panel_size.x - panel_size.x) / 2;
 		}
-		else if(old_panel_size.x > vid_conwidth * 0.999)
+		else if (old_panel_size.x > vid_conwidth * 0.999)
 			panel_pos.x += (old_panel_size.x - panel_size.x) / 2;
 
-		if(panel_pos.y > vid_conheight * 0.001)
+		if (panel_pos.y > vid_conheight * 0.001)
 		{
-			if(panel_pos.y + old_panel_size.y > vid_conheight * 0.999)
+			if (panel_pos.y + old_panel_size.y > vid_conheight * 0.999)
 				panel_pos.y += old_panel_size.y - panel_size.y;
 			else
 				panel_pos.y += (old_panel_size.y - panel_size.y) / 2;
 		}
-		else if(old_panel_size.y > vid_conheight * 0.999)
+		else if (old_panel_size.y > vid_conheight * 0.999)
 			panel_pos.y += (old_panel_size.y - panel_size.y) / 2;
 	}
 	else
-		weapon_count = (REGISTRY_COUNT(Weapons) - 1);
+		weapon_count = (REGISTRY_COUNT(Weapons) - 1); // unused value
+
+
+	Weapons_Fade(center);
+	Weapons_Draw(panel_switchweapon, weapons_stat, center, weapon_size, aspect, when, fadetime, rows, columns, vertical_order);
+}
+
 
-	// animation for fading in/out the panel respectively when not in use
-	if(!autocvar__hud_configure)
+// animation for fading in/out the panel respectively when not in use
+void Weapons_Fade(vector center)
+{
+	if (autocvar__hud_configure)
+		return;
+
+	float timeout = autocvar_hud_panel_weapons_timeout;
+	float timein_effect_length  = autocvar_hud_panel_weapons_timeout_speed_in; //? 0.375 : 0);
+	float timeout_effect_length = autocvar_hud_panel_weapons_timeout_speed_out; //? 0.75 : 0);
+
+	if (timeout && time >= weapontime + timeout) // apply timeout effect if needed
 	{
-		if (timeout && time >= weapontime + timeout) // apply timeout effect if needed
+		float f = bound(0, (time - (weapontime + timeout)) / timeout_effect_length, 1);
+
+		// fade the panel alpha
+		if (autocvar_hud_panel_weapons_timeout_effect == 1)
 		{
-			f = bound(0, (time - (weapontime + timeout)) / timeout_effect_length, 1);
+			panel_bg_alpha *= (autocvar_hud_panel_weapons_timeout_fadebgmin * f + (1 - f));
+			panel_fg_alpha *= (autocvar_hud_panel_weapons_timeout_fadefgmin * f + (1 - f));
+		}
+		else if (autocvar_hud_panel_weapons_timeout_effect == 3)
+		{
+			panel_bg_alpha *= (1 - f);
+			panel_fg_alpha *= (1 - f);
+		}
 
-			// fade the panel alpha
-			if(autocvar_hud_panel_weapons_timeout_effect == 1)
+		// move the panel off the screen
+		if (autocvar_hud_panel_weapons_timeout_effect == 2
+		||  autocvar_hud_panel_weapons_timeout_effect == 3)
+		{
+			f *= f; // for a cooler movement
+			center.x = panel_pos.x + panel_size.x/2;
+			center.y = panel_pos.y + panel_size.y/2;
+			float screen_ar = vid_conwidth/vid_conheight;
+			if (center.x/center.y < screen_ar) //bottom left
 			{
-				panel_bg_alpha *= (autocvar_hud_panel_weapons_timeout_fadebgmin * f + (1 - f));
-				panel_fg_alpha *= (autocvar_hud_panel_weapons_timeout_fadefgmin * f + (1 - f));
+				if ((vid_conwidth - center.x)/center.y < screen_ar) //bottom
+					panel_pos.y += f * (vid_conheight - panel_pos.y);
+				else //left
+					panel_pos.x -= f * (panel_pos.x + panel_size.x);
 			}
-			else if(autocvar_hud_panel_weapons_timeout_effect == 3)
+			else //top right
 			{
-				panel_bg_alpha *= (1 - f);
-				panel_fg_alpha *= (1 - f);
+				if ((vid_conwidth - center.x)/center.y < screen_ar) //right
+					panel_pos.x += f * (vid_conwidth - panel_pos.x);
+				else //top
+					panel_pos.y -= f * (panel_pos.y + panel_size.y);
 			}
+			if (f == 1)
+				center.x = -1; // mark the panel as off screen
+		}
+		weaponprevtime = time - (1 - f) * timein_effect_length;
+	}
+	else if (timeout && time < weaponprevtime + timein_effect_length) // apply timein effect if needed
+	{
+		float f = bound(0, (time - weaponprevtime) / timein_effect_length, 1);
 
-			// move the panel off the screen
-			if (autocvar_hud_panel_weapons_timeout_effect == 2 || autocvar_hud_panel_weapons_timeout_effect == 3)
-			{
-				f *= f; // for a cooler movement
-				center.x = panel_pos.x + panel_size.x/2;
-				center.y = panel_pos.y + panel_size.y/2;
-				screen_ar = vid_conwidth/vid_conheight;
-				if (center.x/center.y < screen_ar) //bottom left
-				{
-					if ((vid_conwidth - center.x)/center.y < screen_ar) //bottom
-						panel_pos.y += f * (vid_conheight - panel_pos.y);
-					else //left
-						panel_pos.x -= f * (panel_pos.x + panel_size.x);
-				}
-				else //top right
-				{
-					if ((vid_conwidth - center.x)/center.y < screen_ar) //right
-						panel_pos.x += f * (vid_conwidth - panel_pos.x);
-					else //top
-						panel_pos.y -= f * (panel_pos.y + panel_size.y);
-				}
-				if(f == 1)
-					center.x = -1; // mark the panel as off screen
-			}
-			weaponprevtime = time - (1 - f) * timein_effect_length;
+		// fade the panel alpha
+		if (autocvar_hud_panel_weapons_timeout_effect == 1)
+		{
+			panel_bg_alpha *= (autocvar_hud_panel_weapons_timeout_fadebgmin * (1 - f) + f);
+			panel_fg_alpha *= (autocvar_hud_panel_weapons_timeout_fadefgmin * (1 - f) + f);
 		}
-		else if (timeout && time < weaponprevtime + timein_effect_length) // apply timein effect if needed
+		else if (autocvar_hud_panel_weapons_timeout_effect == 3)
 		{
-			f = bound(0, (time - weaponprevtime) / timein_effect_length, 1);
+			panel_bg_alpha *= (f);
+			panel_fg_alpha *= (f);
+		}
 
-			// fade the panel alpha
-			if(autocvar_hud_panel_weapons_timeout_effect == 1)
+		// move the panel back on screen
+		if (autocvar_hud_panel_weapons_timeout_effect == 2
+		||  autocvar_hud_panel_weapons_timeout_effect == 3)
+		{
+			f *= f; // for a cooler movement
+			f = 1 - f;
+			center.x = panel_pos.x + panel_size.x/2;
+			center.y = panel_pos.y + panel_size.y/2;
+			float screen_ar = vid_conwidth/vid_conheight;
+			if (center.x/center.y < screen_ar) //bottom left
 			{
-				panel_bg_alpha *= (autocvar_hud_panel_weapons_timeout_fadebgmin * (1 - f) + f);
-				panel_fg_alpha *= (autocvar_hud_panel_weapons_timeout_fadefgmin * (1 - f) + f);
+				if ((vid_conwidth - center.x)/center.y < screen_ar) //bottom
+					panel_pos.y += f * (vid_conheight - panel_pos.y);
+				else //left
+					panel_pos.x -= f * (panel_pos.x + panel_size.x);
 			}
-			else if(autocvar_hud_panel_weapons_timeout_effect == 3)
+			else //top right
 			{
-				panel_bg_alpha *= (f);
-				panel_fg_alpha *= (f);
-			}
-
-			// move the panel back on screen
-			if (autocvar_hud_panel_weapons_timeout_effect == 2 || autocvar_hud_panel_weapons_timeout_effect == 3)
-			{
-				f *= f; // for a cooler movement
-				f = 1 - f;
-				center.x = panel_pos.x + panel_size.x/2;
-				center.y = panel_pos.y + panel_size.y/2;
-				screen_ar = vid_conwidth/vid_conheight;
-				if (center.x/center.y < screen_ar) //bottom left
-				{
-					if ((vid_conwidth - center.x)/center.y < screen_ar) //bottom
-						panel_pos.y += f * (vid_conheight - panel_pos.y);
-					else //left
-						panel_pos.x -= f * (panel_pos.x + panel_size.x);
-				}
-				else //top right
-				{
-					if ((vid_conwidth - center.x)/center.y < screen_ar) //right
-						panel_pos.x += f * (vid_conwidth - panel_pos.x);
-					else //top
-						panel_pos.y -= f * (panel_pos.y + panel_size.y);
-				}
+				if ((vid_conwidth - center.x)/center.y < screen_ar) //right
+					panel_pos.x += f * (vid_conwidth - panel_pos.x);
+				else //top
+					panel_pos.y -= f * (panel_pos.y + panel_size.y);
 			}
 		}
 	}
+}
 
+void Weapons_Draw(
+	entity panel_switchweapon, WepSet weapons_stat,
+	vector center, vector weapon_size, float aspect,
+	float when, float fadetime,
+	int rows, int columns, bool vertical_order)
+{
 	// 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();
@@ -414,10 +417,19 @@ void HUD_Weapons()
 		HUD_Scale_Disable();
 	HUD_Panel_DrawBg();
 
-	if(center.x == -1)
+	if (center.x == -1)
 		return; // panel has gone off screen
 
-	if(panel_bg_padding)
+	vector color;
+	vector barsize = '0 0 0', baroffset = '0 0 0';
+
+	vector ammo_color = '1 0 1';
+	float ammo_alpha = 1;
+	bool infinite_ammo = (STAT(ITEMS) & IT_UNLIMITED_AMMO);
+
+	int row = 0, column = 0;
+
+	if (panel_bg_padding)
 	{
 		panel_pos += '1 1 0' * panel_bg_padding;
 		panel_size -= '2 2 0' * panel_bg_padding;
@@ -425,7 +437,7 @@ void HUD_Weapons()
 
 	// after the sizing and animations are done, update the other values
 
-	if(!rows) // if rows is > 0 onlyowned code has already updated these vars
+	if (!rows) // if rows is > 0 onlyowned code has already updated these vars
 	{
 		HUD_WEAPONS_GET_FULL_LAYOUT();
 		vertical_order = (panel_size.x / panel_size.y >= aspect);
@@ -437,7 +449,7 @@ void HUD_Weapons()
 		ammo_color = stov(autocvar_hud_panel_weapons_ammo_color);
 		ammo_alpha = panel_fg_alpha * autocvar_hud_panel_weapons_ammo_alpha;
 
-		if(weapon_size.x/weapon_size.y > aspect)
+		if (weapon_size.x/weapon_size.y > aspect)
 		{
 			barsize.x = aspect * weapon_size.y;
 			barsize.y = weapon_size.y;
@@ -450,20 +462,19 @@ void HUD_Weapons()
 			baroffset.y = (weapon_size.y - barsize.y) / 2;
 		}
 	}
-	if(autocvar_hud_panel_weapons_accuracy)
+	if (autocvar_hud_panel_weapons_accuracy)
 		Accuracy_LoadColors();
 
 	// draw items
-	row = column = 0;
 	vector label_size = '1 1 0' * min(weapon_size.x, weapon_size.y) * bound(0, autocvar_hud_panel_weapons_label_scale, 1);
 	vector noncurrent_size = weapon_size * bound(0.01, autocvar_hud_panel_weapons_noncurrent_scale, 1);
 	float noncurrent_alpha = panel_fg_alpha * bound(0, autocvar_hud_panel_weapons_noncurrent_alpha, 1);
 	static vector weapon_pos_current = '-1 0 0';
-	if(weapon_pos_current.x == -1)
+	if (weapon_pos_current.x == -1)
 		weapon_pos_current = panel_pos;
 
 	float switch_speed;
-	if(autocvar_hud_panel_weapons_selection_speed <= 0 || autocvar__hud_configure)
+	if (autocvar_hud_panel_weapons_selection_speed <= 0 || autocvar__hud_configure)
 		switch_speed = 999;
 	else
 		switch_speed = frametime * autocvar_hud_panel_weapons_selection_speed;
@@ -471,17 +482,18 @@ void HUD_Weapons()
 
 	// draw background behind currently selected weapon
 	// do it earlier to make sure bg is drawn behind every weapon icons while it's moving
-	if(panel_switchweapon)
+	if (panel_switchweapon)
 		drawpic_aspect_skin(weapon_pos_current, "weapon_current_bg", weapon_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
 
-	for(i = 0; i <= WEP_LAST-WEP_FIRST; ++i)
+	for (int i = 0; i <= WEP_LAST-WEP_FIRST; ++i)
 	{
 		// retrieve information about the current weapon to be drawn
 		entity it = weaponorder[i];
-		weapon_id = it.impulse;
+		int weapon_id = it.impulse;
 
 		// skip if this weapon doesn't exist
-		if(!it || 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)
@@ -505,26 +517,26 @@ void HUD_Weapons()
 		}
 
 		// figure out the drawing position of weapon
-		weapon_pos = panel_pos + vec2(column * weapon_size.x, row * weapon_size.y);
+		vector weapon_pos = panel_pos + vec2(column * weapon_size.x, row * weapon_size.y);
 
 		// update position of the currently selected weapon
-		if(it == panel_switchweapon)
+		if (it == panel_switchweapon)
 		{
-			if(weapon_pos_current.y > weapon_pos.y)
+			if (weapon_pos_current.y > weapon_pos.y)
 				weapon_pos_current.y = max(weapon_pos.y, weapon_pos_current.y - switch_speed * (weapon_pos_current.y - weapon_pos.y));
-			else if(weapon_pos_current.y < weapon_pos.y)
+			else if (weapon_pos_current.y < weapon_pos.y)
 				weapon_pos_current.y = min(weapon_pos.y, weapon_pos_current.y + switch_speed * (weapon_pos.y - weapon_pos_current.y));
-			if(weapon_pos_current.x > weapon_pos.x)
+			if (weapon_pos_current.x > weapon_pos.x)
 				weapon_pos_current.x = max(weapon_pos.x, weapon_pos_current.x - switch_speed * (weapon_pos_current.x - weapon_pos.x));
-			else if(weapon_pos_current.x < weapon_pos.x)
+			else if (weapon_pos_current.x < weapon_pos.x)
 				weapon_pos_current.x = min(weapon_pos.x, weapon_pos_current.x + switch_speed * (weapon_pos.x - weapon_pos_current.x));
 		}
 
 		// draw the weapon accuracy
-		if(autocvar_hud_panel_weapons_accuracy)
+		if (autocvar_hud_panel_weapons_accuracy)
 		{
 			float panel_weapon_accuracy = weapon_accuracy[it.m_id-WEP_FIRST];
-			if(panel_weapon_accuracy >= 0)
+			if (panel_weapon_accuracy >= 0)
 			{
 				color = Accuracy_GetColor(panel_weapon_accuracy);
 				drawpic_aspect_skin(weapon_pos, "weapon_accuracy", weapon_size, color, panel_fg_alpha, DRAWFLAG_NORMAL);
@@ -535,7 +547,7 @@ void HUD_Weapons()
 		float weapon_alpha_real = noncurrent_alpha;
 		float radius_factor_x = 1 - bound(0, fabs(weapon_pos.x - weapon_pos_current.x) / radius_size.x, 1);
 		float radius_factor_y = 1 - bound(0, fabs(weapon_pos.y - weapon_pos_current.y) / radius_size.y, 1);
-		if(radius_factor_x || radius_factor_y)
+		if (radius_factor_x || radius_factor_y)
 		{
 			weapon_size_real.x += (weapon_size.x - noncurrent_size.x) * radius_factor_x;
 			weapon_size_real.y += (weapon_size.y - noncurrent_size.y) * radius_factor_y;
@@ -547,13 +559,13 @@ void HUD_Weapons()
 		weapon_pos_real.y = weapon_pos.y + (weapon_size.y - weapon_size_real.y) / 2;
 
 		// drawing all the weapon items
-		if(weapons_stat & WepSet_FromWeapon(it))
+		if (weapons_stat & WepSet_FromWeapon(it))
 		{
 			// draw the weapon image
 			drawpic_aspect_skin(weapon_pos_real, it.model2, weapon_size_real, '1 1 1', weapon_alpha_real, DRAWFLAG_NORMAL);
 
 			// draw weapon label string
-			switch(autocvar_hud_panel_weapons_label)
+			switch (autocvar_hud_panel_weapons_label)
 			{
 				case 1: // weapon number
 					drawstring(weapon_pos, ftos(weapon_id), label_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
@@ -572,12 +584,12 @@ void HUD_Weapons()
 			}
 
 			// draw ammo status bar
-			if(!infinite_ammo && autocvar_hud_panel_weapons_ammo && (it.ammo_type != RES_NONE))
+			if (!infinite_ammo && autocvar_hud_panel_weapons_ammo && (it.ammo_type != RES_NONE))
 			{
 				float ammo_full;
-				a = getstati(GetAmmoStat(it.ammo_type)); // how much ammo do we have?
+				float a = getstati(GetAmmoStat(it.ammo_type)); // how much ammo do we have?
 
-				if(a > 0)
+				if (a > 0)
 				{
 					// TODO: registry handles
 					switch (it.ammo_type)
@@ -616,23 +628,27 @@ void HUD_Weapons()
 		}
 
 		// draw the complain message
-		if(it == complain_weapon)
+		if (it == complain_weapon)
 		{
-			if(fadetime)
+			float a;
+			if (fadetime)
 				a = ((complain_weapon_time + when > time) ? 1 : bound(0, (complain_weapon_time + when + fadetime - time) / fadetime, 1));
 			else
 				a = ((complain_weapon_time + when > time) ? 1 : 0);
 
 			string s;
-			if(complain_weapon_type == 0) {
+			if (complain_weapon_type == 0)
+			{
 				s = _("Out of ammo");
 				color = stov(autocvar_hud_panel_weapons_complainbubble_color_outofammo);
 			}
-			else if(complain_weapon_type == 1) {
+			else if (complain_weapon_type == 1)
+			{
 				s = _("Don't have");
 				color = stov(autocvar_hud_panel_weapons_complainbubble_color_donthave);
 			}
-			else {
+			else
+			{
 				s = _("Unavailable");
 				color = stov(autocvar_hud_panel_weapons_complainbubble_color_unavailable);
 			}
@@ -651,10 +667,10 @@ void HUD_Weapons()
 		#endif
 
 		// continue with new position for the next weapon
-		if(vertical_order)
+		if (vertical_order)
 		{
 			++column;
-			if(column >= columns)
+			if (column >= columns)
 			{
 				column = 0;
 				++row;
@@ -663,7 +679,7 @@ void HUD_Weapons()
 		else
 		{
 			++row;
-			if(row >= rows)
+			if (row >= rows)
 			{
 				row = 0;
 				++column;
diff --git a/qcsrc/client/hud/panel/weapons.qh b/qcsrc/client/hud/panel/weapons.qh
index 3a7182d5ed..bdcbfb2e3e 100644
--- a/qcsrc/client/hud/panel/weapons.qh
+++ b/qcsrc/client/hud/panel/weapons.qh
@@ -45,8 +45,10 @@ vector acc_col[MAX_ACCURACY_LEVELS];
 float acc_col_loadtime;
 int acc_levels;
 string acc_color_levels;
-void Accuracy_LoadLevels();
 
+void Accuracy_LoadLevels();
 void Accuracy_LoadColors();
-
 vector Accuracy_GetColor(float accuracy);
+
+void Weapons_Fade(vector);
+void Weapons_Draw(entity, WepSet, vector, vector, float, float, int, int, float, bool);
-- 
2.39.5