From dd3776b0e72e2400cfa1a7686a06a6ff80a9e640 Mon Sep 17 00:00:00 2001
From: Samual Lenks <samual@xonotic.org>
Date: Fri, 28 Jun 2013 23:32:26 -0400
Subject: [PATCH] Move hitplot stuff into a separate file

---
 qcsrc/server/cl_client.qc       | 14 +----
 qcsrc/server/defs.qh            |  1 -
 qcsrc/server/progs.src          |  2 +
 qcsrc/server/weapons/hitplot.qc | 91 +++++++++++++++++++++++++++++++++
 qcsrc/server/weapons/hitplot.qh |  5 ++
 qcsrc/server/weapons/main.qc    | 73 --------------------------
 6 files changed, 100 insertions(+), 86 deletions(-)
 create mode 100644 qcsrc/server/weapons/hitplot.qc
 create mode 100644 qcsrc/server/weapons/hitplot.qh

diff --git a/qcsrc/server/cl_client.qc b/qcsrc/server/cl_client.qc
index 9014145f08..e5133ad886 100644
--- a/qcsrc/server/cl_client.qc
+++ b/qcsrc/server/cl_client.qc
@@ -1188,13 +1188,7 @@ void ClientConnect (void)
 	if(!sv_foginterval && world.fog != "")
 		stuffcmd(self, strcat("\nfog ", world.fog, "\nr_fog_exp2 0\nr_drawfog 1\n"));
 
-	if(autocvar_g_hitplots || strstrofs(strcat(" ", autocvar_g_hitplots_individuals, " "), strcat(" ", self.netaddress, " "), 0) >= 0)
-	{
-		self.hitplotfh = fopen(strcat("hits-", matchid, "-", self.netaddress, "-", ftos(self.playerid), ".plot"), FILE_WRITE);
-		fputs(self.hitplotfh, strcat("#name ", self.netname, "\n"));
-	}
-	else
-		self.hitplotfh = -1;
+	W_HitPlotOpen(self);
 
 	if(g_race || g_cts) {
 		string rr;
@@ -1254,11 +1248,7 @@ void ClientDisconnect (void)
 
 	CheatShutdownClient();
 
-	if(self.hitplotfh >= 0)
-	{
-		fclose(self.hitplotfh);
-		self.hitplotfh = -1;
-	}
+	W_HitPlotClose(self);
 
 	anticheat_report();
 	anticheat_shutdown();
diff --git a/qcsrc/server/defs.qh b/qcsrc/server/defs.qh
index d2b9d87ad3..fde29caf77 100644
--- a/qcsrc/server/defs.qh
+++ b/qcsrc/server/defs.qh
@@ -511,7 +511,6 @@ float servertime, serverprevtime, serverframetime;
 .float stat_shotorg; // networked stat for trueaim HUD
 
 string matchid;
-.float hitplotfh;
 
 .float last_pickup;
 
diff --git a/qcsrc/server/progs.src b/qcsrc/server/progs.src
index 50b3b0b81b..0014d32ffd 100644
--- a/qcsrc/server/progs.src
+++ b/qcsrc/server/progs.src
@@ -63,6 +63,7 @@ command/getreplies.qh
 command/cmd.qh
 command/sv_cmd.qh
 
+weapons/hitplot.qh
 weapons/accuracy.qh
 weapons/csqcprojectile.qh // TODO
 ../common/csqcmodel_settings.qh
@@ -137,6 +138,7 @@ g_models.qc
 item_key.qc
 secret.qc
 
+weapons/hitplot.qc
 weapons/main.qc
 weapons/common.qc
 weapons/csqcprojectile.qc // TODO
diff --git a/qcsrc/server/weapons/hitplot.qc b/qcsrc/server/weapons/hitplot.qc
new file mode 100644
index 0000000000..5ef9f1287d
--- /dev/null
+++ b/qcsrc/server/weapons/hitplot.qc
@@ -0,0 +1,91 @@
+vector W_HitPlotUnnormalizedUntransform(vector screenforward, vector screenright, vector screenup, vector v)
+{
+	vector ret;
+	ret_x = screenright * v;
+	ret_y = screenup * v;
+	ret_z = screenforward * v;
+	return ret;
+}
+
+vector W_HitPlotNormalizedUntransform(vector org, entity targ, vector screenforward, vector screenright, vector screenup, vector v)
+{
+	float i, j, k;
+	vector mi, ma, thisv, myv, ret;
+
+	myv = W_HitPlotUnnormalizedUntransform(screenforward, screenright, screenup, org);
+
+	// x = 0..1 relative to hitbox; y = 0..1 relative to hitbox; z = distance
+
+	mi = ma = targ.origin + 0.5 * (targ.mins + targ.maxs);
+	for(i = 0; i < 2; ++i) for(j = 0; j < 2; ++j) for(k = 0; k < 2; ++k)
+	{
+		thisv = targ.origin;
+		if(i) thisv_x += targ.maxs_x; else thisv_x += targ.mins_x;
+		if(j) thisv_y += targ.maxs_y; else thisv_y += targ.mins_y;
+		if(k) thisv_z += targ.maxs_z; else thisv_z += targ.mins_z;
+		thisv = W_HitPlotUnnormalizedUntransform(screenforward, screenright, screenup, thisv);
+		if(i || j || k)
+		{
+			if(mi_x > thisv_x) mi_x = thisv_x; if(ma_x < thisv_x) ma_x = thisv_x;
+			if(mi_y > thisv_y) mi_y = thisv_y; if(ma_y < thisv_y) ma_y = thisv_y;
+			//if(mi_z > thisv_z) mi_z = thisv_z; if(ma_z < thisv_z) ma_y = thisv_z;
+		}
+		else
+		{
+			// first run
+			mi = ma = thisv;
+		}
+	}
+
+	thisv = W_HitPlotUnnormalizedUntransform(screenforward, screenright, screenup, v);
+	ret_x = (thisv_x - mi_x) / (ma_x - mi_x);
+	ret_y = (thisv_y - mi_y) / (ma_y - mi_y);
+	ret_z = thisv_z - myv_z;
+	return ret;
+}
+
+void W_HitPlotAnalysis(entity player, vector screenforward, vector screenright, vector screenup)
+{
+	vector hitplot;
+	vector org;
+	float lag;
+
+	if(player.hitplotfh >= 0)
+	{
+		lag = ANTILAG_LATENCY(player);
+		if(lag < 0.001)
+			lag = 0;
+		if not(IS_REAL_CLIENT(player))
+			lag = 0; // only antilag for clients
+
+		org = player.origin + player.view_ofs;
+		traceline_antilag_force(player, org, org + screenforward * MAX_SHOT_DISTANCE, MOVE_NORMAL, player, lag);
+		if(IS_CLIENT(trace_ent))
+		{
+			antilag_takeback(trace_ent, time - lag);
+			hitplot = W_HitPlotNormalizedUntransform(org, trace_ent, screenforward, screenright, screenup, trace_endpos);
+			antilag_restore(trace_ent);
+			fputs(player.hitplotfh, strcat(ftos(hitplot_x), " ", ftos(hitplot_y), " ", ftos(hitplot_z), " ", ftos(player.switchweapon), "\n"));
+			//print(strcat(ftos(hitplot_x), " ", ftos(hitplot_y), " ", ftos(hitplot_z), "\n"));
+		}
+	}
+}
+
+void W_HitPlotOpen(entity player)
+{
+	if(autocvar_g_hitplots || strstrofs(strcat(" ", autocvar_g_hitplots_individuals, " "), strcat(" ", player.netaddress, " "), 0) >= 0)
+	{
+		player.hitplotfh = fopen(strcat("hits-", matchid, "-", player.netaddress, "-", ftos(player.playerid), ".plot"), FILE_WRITE);
+		fputs(player.hitplotfh, strcat("#name ", player.netname, "\n"));
+	}
+	else { player.hitplotfh = -1; }
+}
+
+void W_HitPlotClose(entity player)
+{
+	if(player.hitplotfh >= 0)
+	{
+		fclose(player.hitplotfh);
+		player.hitplotfh = -1;
+	}
+}
diff --git a/qcsrc/server/weapons/hitplot.qh b/qcsrc/server/weapons/hitplot.qh
new file mode 100644
index 0000000000..9bc8ff3039
--- /dev/null
+++ b/qcsrc/server/weapons/hitplot.qh
@@ -0,0 +1,5 @@
+.float hitplotfh;
+
+void W_HitPlotAnalysis(entity player, vector screenforward, vector screenright, vector screenup);
+void W_HitPlotOpen(entity player);
+void W_HitPlotClose(entity player);
diff --git a/qcsrc/server/weapons/main.qc b/qcsrc/server/weapons/main.qc
index 1d684c7fb5..c7b47ef31d 100644
--- a/qcsrc/server/weapons/main.qc
+++ b/qcsrc/server/weapons/main.qc
@@ -36,79 +36,6 @@ float WFRAME_RELOAD = 3;
 
 void(float fr, float t, void() func) weapon_thinkf;
 
-vector W_HitPlotUnnormalizedUntransform(vector screenforward, vector screenright, vector screenup, vector v)
-{
-	vector ret;
-	ret_x = screenright * v;
-	ret_y = screenup * v;
-	ret_z = screenforward * v;
-	return ret;
-}
-
-vector W_HitPlotNormalizedUntransform(vector org, entity targ, vector screenforward, vector screenright, vector screenup, vector v)
-{
-	float i, j, k;
-	vector mi, ma, thisv, myv, ret;
-
-	myv = W_HitPlotUnnormalizedUntransform(screenforward, screenright, screenup, org);
-
-	// x = 0..1 relative to hitbox; y = 0..1 relative to hitbox; z = distance
-
-	mi = ma = targ.origin + 0.5 * (targ.mins + targ.maxs);
-	for(i = 0; i < 2; ++i) for(j = 0; j < 2; ++j) for(k = 0; k < 2; ++k)
-	{
-		thisv = targ.origin;
-		if(i) thisv_x += targ.maxs_x; else thisv_x += targ.mins_x;
-		if(j) thisv_y += targ.maxs_y; else thisv_y += targ.mins_y;
-		if(k) thisv_z += targ.maxs_z; else thisv_z += targ.mins_z;
-		thisv = W_HitPlotUnnormalizedUntransform(screenforward, screenright, screenup, thisv);
-		if(i || j || k)
-		{
-			if(mi_x > thisv_x) mi_x = thisv_x; if(ma_x < thisv_x) ma_x = thisv_x;
-			if(mi_y > thisv_y) mi_y = thisv_y; if(ma_y < thisv_y) ma_y = thisv_y;
-			//if(mi_z > thisv_z) mi_z = thisv_z; if(ma_z < thisv_z) ma_y = thisv_z;
-		}
-		else
-		{
-			// first run
-			mi = ma = thisv;
-		}
-	}
-
-	thisv = W_HitPlotUnnormalizedUntransform(screenforward, screenright, screenup, v);
-	ret_x = (thisv_x - mi_x) / (ma_x - mi_x);
-	ret_y = (thisv_y - mi_y) / (ma_y - mi_y);
-	ret_z = thisv_z - myv_z;
-	return ret;
-}
-
-void W_HitPlotAnalysis(entity player, vector screenforward, vector screenright, vector screenup)
-{
-	vector hitplot;
-	vector org;
-	float lag;
-
-	if(player.hitplotfh >= 0)
-	{
-		lag = ANTILAG_LATENCY(player);
-		if(lag < 0.001)
-			lag = 0;
-		if not(IS_REAL_CLIENT(player))
-			lag = 0; // only antilag for clients
-
-		org = player.origin + player.view_ofs;
-		traceline_antilag_force(player, org, org + screenforward * MAX_SHOT_DISTANCE, MOVE_NORMAL, player, lag);
-		if(IS_CLIENT(trace_ent))
-		{
-			antilag_takeback(trace_ent, time - lag);
-			hitplot = W_HitPlotNormalizedUntransform(org, trace_ent, screenforward, screenright, screenup, trace_endpos);
-			antilag_restore(trace_ent);
-			fputs(player.hitplotfh, strcat(ftos(hitplot_x), " ", ftos(hitplot_y), " ", ftos(hitplot_z), " ", ftos(player.switchweapon), "\n"));
-			//print(strcat(ftos(hitplot_x), " ", ftos(hitplot_y), " ", ftos(hitplot_z), "\n"));
-		}
-	}
-}
-
 vector w_shotorg;
 vector w_shotdir;
 vector w_shotend;
-- 
2.39.5