From 913b5501b8eaa7793de54875d4992f23c211b6e0 Mon Sep 17 00:00:00 2001
From: terencehill <piuntn@gmail.com>
Date: Sun, 23 Sep 2018 12:49:27 +0200
Subject: [PATCH] Introduce some constants to indicate frozen states; improve a
 few frozen state checks in freezetag code

---
 .../gamemode/freezetag/sv_freezetag.qc        | 40 +++++++++----------
 qcsrc/common/monsters/sv_monsters.qc          |  4 +-
 qcsrc/common/mutators/mutator/nades/nades.qc  |  8 ++--
 qcsrc/common/turrets/sv_turrets.qc            |  2 +-
 qcsrc/common/vehicles/sv_vehicles.qc          |  2 +-
 qcsrc/server/client.qc                        |  4 +-
 qcsrc/server/defs.qh                          |  5 +++
 qcsrc/server/g_damage.qc                      |  7 ++--
 8 files changed, 37 insertions(+), 35 deletions(-)

diff --git a/qcsrc/common/gamemodes/gamemode/freezetag/sv_freezetag.qc b/qcsrc/common/gamemodes/gamemode/freezetag/sv_freezetag.qc
index 42b3b77f5..be6e92523 100644
--- a/qcsrc/common/gamemodes/gamemode/freezetag/sv_freezetag.qc
+++ b/qcsrc/common/gamemodes/gamemode/freezetag/sv_freezetag.qc
@@ -19,8 +19,7 @@ void freezetag_count_alive_players()
 	FOREACH_CLIENT(IS_PLAYER(it) && Entity_HasValidTeam(it),
 	{
 		++total_players;
-		if ((GetResourceAmount(it, RESOURCE_HEALTH) < 1) ||
-			(STAT(FROZEN, it) == 1))
+		if (GetResourceAmount(it, RESOURCE_HEALTH) < 1 || STAT(FROZEN, it) == FROZEN_NORMAL)
 		{
 			continue;
 		}
@@ -155,7 +154,7 @@ entity freezetag_LastPlayerForTeam(entity this)
 {
 	entity last_pl = NULL;
 	FOREACH_CLIENT(IS_PLAYER(it) && it != this && SAME_TEAM(it, this), {
-		if (!STAT(FROZEN, it) && GetResourceAmount(it, RESOURCE_HEALTH) >= 1)
+		if (STAT(FROZEN, it) != FROZEN_NORMAL && GetResourceAmount(it, RESOURCE_HEALTH) >= 1)
 		{
 			if (!last_pl)
 				last_pl = it;
@@ -213,7 +212,7 @@ void freezetag_Freeze(entity targ, entity attacker)
 
 bool freezetag_isEliminated(entity e)
 {
-	if(IS_PLAYER(e) && (STAT(FROZEN, e) == 1 || IS_DEAD(e)))
+	if(IS_PLAYER(e) && (STAT(FROZEN, e) == FROZEN_NORMAL || IS_DEAD(e)))
 		return true;
 	return false;
 }
@@ -231,7 +230,7 @@ void havocbot_goalrating_ft_freeplayers(entity this, float ratingscale, vector o
 	entity best_pl = NULL;
 	float best_dist2 = FLOAT_MAX;
 	FOREACH_CLIENT(IS_PLAYER(it) && it != this && SAME_TEAM(it, this), {
-		if (STAT(FROZEN, it) == 1)
+		if (STAT(FROZEN, it) == FROZEN_NORMAL)
 		{
 			if(vdist(it.origin - org, >, sradius))
 				continue;
@@ -267,10 +266,12 @@ void havocbot_role_ft_offense(entity this)
 
 	// Count how many players on team are unfrozen.
 	int unfrozen = 0;
-	FOREACH_CLIENT(IS_PLAYER(it) && SAME_TEAM(it, this) && !STAT(FROZEN, it), { unfrozen++; });
+	FOREACH_CLIENT(IS_PLAYER(it) && SAME_TEAM(it, this) && STAT(FROZEN, it) != FROZEN_NORMAL, {
+		unfrozen++;
+	});
 
 	// If only one left on team or if role has timed out then start trying to free players.
-	if ((unfrozen == 0 && !STAT(FROZEN, this)) || time > this.havocbot_role_timeout)
+	if ((!unfrozen && STAT(FROZEN, this) != FROZEN_NORMAL) || time > this.havocbot_role_timeout)
 	{
 		LOG_TRACE("changing role to freeing");
 		this.havocbot_role = havocbot_role_ft_freeing;
@@ -327,7 +328,7 @@ void havocbot_role_ft_freeing(entity this)
 
 void ft_RemovePlayer(entity this)
 {
-	if(!STAT(FROZEN, this))
+	if (STAT(FROZEN, this) != FROZEN_NORMAL)
 		freezetag_LastPlayerForTeam_Notify(this);
 	Unfreeze(this, false);
 
@@ -359,7 +360,7 @@ MUTATOR_HOOKFUNCTION(ft, PlayerDies)
 	if(round_handler_IsActive())
 	if(round_handler_CountdownRunning())
 	{
-		if(STAT(FROZEN, frag_target))
+		if (STAT(FROZEN, frag_target) == FROZEN_NORMAL)
 			Unfreeze(frag_target, true);
 		freezetag_count_alive_players();
 		return true; // let the player die so that he can respawn whenever he wants
@@ -371,7 +372,7 @@ MUTATOR_HOOKFUNCTION(ft, PlayerDies)
 		|| frag_deathtype == DEATH_TEAMCHANGE.m_id || frag_deathtype == DEATH_AUTOTEAMCHANGE.m_id)
 	{
 		// let the player die, he will be automatically frozen when he respawns
-		if(STAT(FROZEN, frag_target) != 1)
+		if (STAT(FROZEN, frag_target) != FROZEN_NORMAL)
 		{
 			freezetag_Add_Score(frag_target, frag_attacker);
 			freezetag_count_alive_players();
@@ -383,7 +384,7 @@ MUTATOR_HOOKFUNCTION(ft, PlayerDies)
 		return true;
 	}
 
-	if(STAT(FROZEN, frag_target))
+	if (STAT(FROZEN, frag_target) == FROZEN_NORMAL)
 		return true;
 
 	freezetag_Freeze(frag_target, frag_attacker);
@@ -465,7 +466,7 @@ MUTATOR_HOOKFUNCTION(ft, PlayerPreThink, CBC_ORDER_FIRST)
 	int n;
 	entity o = NULL;
 	entity player = M_ARGV(0, entity);
-	//if(STAT(FROZEN, player))
+	//if (STAT(FROZEN, player) == FROZEN_NORMAL)
 	//if(player.freezetag_frozen_timeout > 0 && time < player.freezetag_frozen_timeout)
 		//player.iceblock.alpha = ICE_MIN_ALPHA + (ICE_MAX_ALPHA - ICE_MIN_ALPHA) * (player.freezetag_frozen_timeout - time) / (player.freezetag_frozen_timeout - player.freezetag_frozen_time);
 
@@ -476,22 +477,19 @@ MUTATOR_HOOKFUNCTION(ft, PlayerPreThink, CBC_ORDER_FIRST)
 		vector revive_extra_size = '1 1 1' * autocvar_g_freezetag_revive_extra_size;
 		n = 0;
 		FOREACH_CLIENT(IS_PLAYER(it) && it != player, {
-			if(STAT(FROZEN, it) == 0)
-			if(!IS_DEAD(it))
-			if(SAME_TEAM(it, player))
+			if (!STAT(FROZEN, it) && !IS_DEAD(it) && SAME_TEAM(it, player))
 			if(boxesoverlap(player.absmin - revive_extra_size, player.absmax + revive_extra_size, it.absmin, it.absmax))
 			{
 				if(!o)
 					o = it;
-				if(STAT(FROZEN, player) == 1)
+				if (STAT(FROZEN, player) == FROZEN_NORMAL)
 					it.reviving = true;
 				++n;
 			}
 		});
-
 	}
 
-	if(n && STAT(FROZEN, player) == 1) // OK, there is at least one teammate reviving us
+	if (n && STAT(FROZEN, player) == FROZEN_NORMAL) // OK, there is at least one teammate reviving us
 	{
 		STAT(REVIVE_PROGRESS, player) = bound(0, STAT(REVIVE_PROGRESS, player) + frametime * max(1/60, autocvar_g_freezetag_revive_speed), 1);
 		SetResourceAmountExplicit(player, RESOURCE_HEALTH, max(1, STAT(REVIVE_PROGRESS, player) * ((warmup_stage) ? warmup_start_health : start_health)));
@@ -525,12 +523,12 @@ MUTATOR_HOOKFUNCTION(ft, PlayerPreThink, CBC_ORDER_FIRST)
 			it.reviving = false;
 		});
 	}
-	else if(!n && STAT(FROZEN, player) == 1) // only if no teammate is nearby will we reset
+	else if (!n && STAT(FROZEN, player) == FROZEN_NORMAL) // only if no teammate is nearby will we reset
 	{
 		STAT(REVIVE_PROGRESS, player) = bound(0, STAT(REVIVE_PROGRESS, player) - frametime * autocvar_g_freezetag_revive_clearspeed, 1);
 		SetResourceAmountExplicit(player, RESOURCE_HEALTH, max(1, STAT(REVIVE_PROGRESS, player) * ((warmup_stage) ? warmup_start_health : start_health)));
 	}
-	else if(!n && !STAT(FROZEN, player))
+	else if (!n && !STAT(FROZEN, player))
 	{
 		STAT(REVIVE_PROGRESS, player) = 0; // thawing nobody
 	}
@@ -591,7 +589,7 @@ MUTATOR_HOOKFUNCTION(ft, FragCenterMessage)
 	int kill_count_to_attacker = M_ARGV(3, int);
 	int kill_count_to_target = M_ARGV(4, int);
 
-	if(STAT(FROZEN, frag_target))
+	if(STAT(FROZEN, frag_target) == FROZEN_NORMAL)
 		return; // target was already frozen, so this is just pushing them off the cliff
 
 	Send_Notification(NOTIF_ONE, frag_attacker, MSG_CHOICE, CHOICE_FRAG_FREEZE, frag_target.netname, kill_count_to_attacker, (IS_BOT_CLIENT(frag_target) ? -1 : CS(frag_target).ping));
diff --git a/qcsrc/common/monsters/sv_monsters.qc b/qcsrc/common/monsters/sv_monsters.qc
index 011e14ca3..004fbf92b 100644
--- a/qcsrc/common/monsters/sv_monsters.qc
+++ b/qcsrc/common/monsters/sv_monsters.qc
@@ -1157,7 +1157,7 @@ void Monster_Anim(entity this)
 
 void Monster_Frozen_Think(entity this)
 {
-	if (STAT(FROZEN, this) == 2) // reviving in progress
+	if (STAT(FROZEN, this) == FROZEN_TEMP_REVIVING)
 	{
 		STAT(REVIVE_PROGRESS, this) = bound(0, STAT(REVIVE_PROGRESS, this) + this.ticrate * this.revive_speed, 1);
 		SetResourceAmountExplicit(this, RESOURCE_HEALTH, max(1, STAT(REVIVE_PROGRESS, this) * this.max_health));
@@ -1169,7 +1169,7 @@ void Monster_Frozen_Think(entity this)
 		if(STAT(REVIVE_PROGRESS, this) >= 1)
 			Unfreeze(this, false);
 	}
-	else if (STAT(FROZEN, this) == 3) // ice nade progress until explosion
+	else if (STAT(FROZEN, this) == FROZEN_TEMP_DYING)
 	{
 		STAT(REVIVE_PROGRESS, this) = bound(0, STAT(REVIVE_PROGRESS, this) - this.ticrate * this.revive_speed, 1);
 		SetResourceAmountExplicit(this, RESOURCE_HEALTH, max(0, autocvar_g_nades_ice_health + (this.max_health-autocvar_g_nades_ice_health) * STAT(REVIVE_PROGRESS, this)));
diff --git a/qcsrc/common/mutators/mutator/nades/nades.qc b/qcsrc/common/mutators/mutator/nades/nades.qc
index ef9be3459..b6dafe760 100644
--- a/qcsrc/common/mutators/mutator/nades/nades.qc
+++ b/qcsrc/common/mutators/mutator/nades/nades.qc
@@ -1021,7 +1021,7 @@ void nades_GiveBonus(entity player, float score)
 	if (autocvar_g_nades_bonus)
 	if (IS_REAL_CLIENT(player))
 	if (IS_PLAYER(player) && STAT(NADE_BONUS, player) < autocvar_g_nades_bonus_max)
-	if (STAT(FROZEN, player) == 0)
+	if (!STAT(FROZEN, player))
 	if (!IS_DEAD(player))
 	{
 		if ( STAT(NADE_BONUS_SCORE, player) < 1 )
@@ -1332,12 +1332,12 @@ MUTATOR_HOOKFUNCTION(nades, PlayerPreThink)
 	entity o = NULL;
 	if(player.freezetag_frozen_timeout > 0 && time >= player.freezetag_frozen_timeout)
 		n = -1;
-	else if(STAT(FROZEN, player) == 3)
+	else if (STAT(FROZEN, player) == FROZEN_TEMP_DYING)
 	{
 		vector revive_extra_size = '1 1 1' * autocvar_g_freezetag_revive_extra_size;
 		n = 0;
 		FOREACH_CLIENT(IS_PLAYER(it) && it != player, {
-			if(!IS_DEAD(it) && STAT(FROZEN, it) == 0 && SAME_TEAM(it, player))
+			if (!IS_DEAD(it) && !STAT(FROZEN, it) && SAME_TEAM(it, player))
 			if(boxesoverlap(player.absmin - revive_extra_size, player.absmax + revive_extra_size, it.absmin, it.absmax))
 			{
 				if(!o)
@@ -1348,7 +1348,7 @@ MUTATOR_HOOKFUNCTION(nades, PlayerPreThink)
 		});
 	}
 
-	if(n > 0 && STAT(FROZEN, player) == 3) // OK, there is at least one teammate reviving us
+	if (n > 0 && STAT(FROZEN, player) == FROZEN_TEMP_DYING) // OK, there is at least one teammate reviving us
 	{
 		STAT(REVIVE_PROGRESS, player) = bound(0, STAT(REVIVE_PROGRESS, player) + frametime * max(1/60, autocvar_g_freezetag_revive_speed), 1);
 		SetResourceAmount(player, RESOURCE_HEALTH, max(1, STAT(REVIVE_PROGRESS, player) * start_health));
diff --git a/qcsrc/common/turrets/sv_turrets.qc b/qcsrc/common/turrets/sv_turrets.qc
index 4962ddbe6..37fb5f0d5 100644
--- a/qcsrc/common/turrets/sv_turrets.qc
+++ b/qcsrc/common/turrets/sv_turrets.qc
@@ -733,7 +733,7 @@ float turret_validate_target(entity e_turret, entity e_target, float validate_fl
 	// Cant touch this
 	if (GetResourceAmount(e_target, RESOURCE_HEALTH) <= 0)
 		return -6;
-	else if(STAT(FROZEN, e_target) > 0)
+	else if (STAT(FROZEN, e_target))
 		return -6;
 
 	// vehicle
diff --git a/qcsrc/common/vehicles/sv_vehicles.qc b/qcsrc/common/vehicles/sv_vehicles.qc
index 2f5e8e46b..be8d468fa 100644
--- a/qcsrc/common/vehicles/sv_vehicles.qc
+++ b/qcsrc/common/vehicles/sv_vehicles.qc
@@ -966,7 +966,7 @@ bool vehicle_impulse(entity this, int imp)
 
 void vehicles_enter(entity pl, entity veh)
 {
-   // Remove this when bots know how to use vehicles
+	// Remove this when bots know how to use vehicles
 	if((IS_BOT_CLIENT(pl) && !autocvar_g_vehicles_allow_bots))
 		return;
 
diff --git a/qcsrc/server/client.qc b/qcsrc/server/client.qc
index d927e9c6f..489f2cc76 100644
--- a/qcsrc/server/client.qc
+++ b/qcsrc/server/client.qc
@@ -2404,7 +2404,7 @@ void PlayerPreThink (entity this)
 
 	if(IS_PLAYER(this))
 	{
-		if (STAT(FROZEN, this) == 2) // reviving in progress
+		if (STAT(FROZEN, this) == FROZEN_TEMP_REVIVING)
 		{
 			STAT(REVIVE_PROGRESS, this) = bound(0, STAT(REVIVE_PROGRESS, this) + frametime * this.revive_speed, 1);
 			SetResourceAmountExplicit(this, RESOURCE_HEALTH, max(1, STAT(REVIVE_PROGRESS, this) * start_health));
@@ -2413,7 +2413,7 @@ void PlayerPreThink (entity this)
 			if (STAT(REVIVE_PROGRESS, this) >= 1)
 				Unfreeze(this, false);
 		}
-		else if (STAT(FROZEN, this) == 3) // ice nade progress until explosion
+		else if (STAT(FROZEN, this) == FROZEN_TEMP_DYING)
 		{
 			STAT(REVIVE_PROGRESS, this) = bound(0, STAT(REVIVE_PROGRESS, this) - frametime * this.revive_speed, 1);
 			SetResourceAmountExplicit(this, RESOURCE_HEALTH, max(0, autocvar_g_nades_ice_health + (start_health-autocvar_g_nades_ice_health) * STAT(REVIVE_PROGRESS, this)));
diff --git a/qcsrc/server/defs.qh b/qcsrc/server/defs.qh
index 58111fb12..e81cf7228 100644
--- a/qcsrc/server/defs.qh
+++ b/qcsrc/server/defs.qh
@@ -340,6 +340,11 @@ string deathmessage;
 
 .float ballistics_density; // wall piercing factor, larger = bullet can pass through more
 
+//const int FROZEN_NOT				= 0;
+const int FROZEN_NORMAL				= 1;
+const int FROZEN_TEMP_REVIVING		= 2;
+const int FROZEN_TEMP_DYING			= 3;
+
 const int ACTIVE_NOT 		= 0;
 const int ACTIVE_ACTIVE 	= 1;
 const int ACTIVE_IDLE	 	= 2;
diff --git a/qcsrc/server/g_damage.qc b/qcsrc/server/g_damage.qc
index c2c2b85bc..c238b93f6 100644
--- a/qcsrc/server/g_damage.qc
+++ b/qcsrc/server/g_damage.qc
@@ -489,8 +489,8 @@ void Freeze(entity targ, float revivespeed, int frozen_type, bool show_waypoint)
 	float targ_maxhealth = ((IS_MONSTER(targ)) ? targ.max_health : start_health);
 
 	STAT(FROZEN, targ) = frozen_type;
-	STAT(REVIVE_PROGRESS, targ) = ((frozen_type == 3) ? 1 : 0);
-	SetResourceAmount(targ, RESOURCE_HEALTH, ((frozen_type == 3) ? targ_maxhealth : 1));
+	STAT(REVIVE_PROGRESS, targ) = ((frozen_type == FROZEN_TEMP_DYING) ? 1 : 0);
+	SetResourceAmount(targ, RESOURCE_HEALTH, ((frozen_type == FROZEN_TEMP_DYING) ? targ_maxhealth : 1));
 	targ.revive_speed = revivespeed;
 	if(targ.bot_attack)
 		IL_REMOVE(g_bot_targets, targ);
@@ -534,7 +534,7 @@ void Unfreeze(entity targ, bool reset_health)
 	if(!STAT(FROZEN, targ))
 		return;
 
-	if (reset_health && STAT(FROZEN, targ) != 3) // only reset health if target was frozen
+	if (reset_health && STAT(FROZEN, targ) != FROZEN_TEMP_DYING)
 		SetResourceAmount(targ, RESOURCE_HEALTH, ((IS_PLAYER(targ)) ? start_health : targ.max_health));
 
 	targ.pauseregen_finished = time + autocvar_g_balance_pause_health_regen;
@@ -1248,7 +1248,6 @@ void Fire_ApplyDamage(entity e)
 			}
 		});
 	}
-			
 }
 
 void Fire_ApplyEffect(entity e)
-- 
2.39.5