From bc4be8c518a62d8ae09227114c32a90133d0fde8 Mon Sep 17 00:00:00 2001
From: Mario <mario.mario@y7mail.com>
Date: Fri, 31 Jul 2020 20:48:34 +1000
Subject: [PATCH] Add Read/WriteAngleVector macros to simplify the networking
 of angles

---
 qcsrc/common/effects/qc/modeleffects.qc          | 16 ++++------------
 .../gamemodes/gamemode/onslaught/onslaught.qc    |  4 +---
 .../gamemodes/gamemode/onslaught/sv_onslaught.qc |  5 ++---
 qcsrc/common/mapobjects/func/plat.qc             |  8 ++------
 qcsrc/common/mapobjects/func/train.qc            |  8 ++------
 qcsrc/common/mapobjects/misc/laser.qc            |  6 ++----
 qcsrc/common/mapobjects/misc/teleport_dest.qc    | 10 ++--------
 qcsrc/common/mapobjects/models.qc                | 12 ++----------
 qcsrc/common/mapobjects/trigger/jumppads.qc      |  8 ++------
 qcsrc/common/mapobjects/trigger/viewloc.qc       |  8 ++------
 qcsrc/common/t_items.qc                          |  8 ++------
 qcsrc/common/turrets/cl_turrets.qc               |  3 +--
 qcsrc/common/turrets/sv_turrets.qc               |  4 +---
 qcsrc/common/vehicles/sv_vehicles.qc             |  2 ++
 qcsrc/common/vehicles/vehicle/bumblebee.qc       |  2 ++
 qcsrc/common/weapons/weapon/arc.qc               |  8 ++------
 qcsrc/common/wepent.qc                           |  4 ++--
 qcsrc/lib/net.qh                                 |  4 ++++
 18 files changed, 37 insertions(+), 83 deletions(-)

diff --git a/qcsrc/common/effects/qc/modeleffects.qc b/qcsrc/common/effects/qc/modeleffects.qc
index 9849b5be7..41eee445c 100644
--- a/qcsrc/common/effects/qc/modeleffects.qc
+++ b/qcsrc/common/effects/qc/modeleffects.qc
@@ -30,15 +30,11 @@ bool modeleffect_SendEntity(entity this, entity to, int sf)
 	}
 	if(f & 2)
 	{
-		WriteCoord(MSG_ENTITY, this.angles.x);
-		WriteCoord(MSG_ENTITY, this.angles.y);
-		WriteCoord(MSG_ENTITY, this.angles.z);
+		WriteAngleVector(MSG_ENTITY, this.angles);
 	}
 	if(f & 4)
 	{
-		WriteCoord(MSG_ENTITY, this.avelocity.x);
-		WriteCoord(MSG_ENTITY, this.avelocity.y);
-		WriteCoord(MSG_ENTITY, this.avelocity.z);
+		WriteAngleVector(MSG_ENTITY, this.avelocity);
 	}
 	WriteShort(MSG_ENTITY, this.scale * 256.0);
 	WriteShort(MSG_ENTITY, this.scale2 * 256.0);
@@ -128,15 +124,11 @@ NET_HANDLE(ENT_CLIENT_MODELEFFECT, bool isnew)
 	}
 	if(f & 2)
 	{
-		e.angles_x = ReadAngle();
-		e.angles_y = ReadAngle();
-		e.angles_z = ReadAngle();
+		e.angles = ReadAngleVector();
 	}
 	if(f & 4)
 	{
-		e.avelocity_x = ReadAngle();
-		e.avelocity_y = ReadAngle();
-		e.avelocity_z = ReadAngle();
+		e.avelocity = ReadAngleVector();
 	}
 	e.scale1 = ReadShort() / 256.0;
 	e.scale2 = ReadShort() / 256.0;
diff --git a/qcsrc/common/gamemodes/gamemode/onslaught/onslaught.qc b/qcsrc/common/gamemodes/gamemode/onslaught/onslaught.qc
index 66ae14db4..f0a0046d8 100644
--- a/qcsrc/common/gamemodes/gamemode/onslaught/onslaught.qc
+++ b/qcsrc/common/gamemodes/gamemode/onslaught/onslaught.qc
@@ -12,9 +12,7 @@ NET_HANDLE(ENT_ONSCAMERA, bool isnew)
 	this.origin = ReadVector();
 	setorigin(this, this.origin);
 
-	this.angles_x = ReadAngle();
-	this.angles_y = ReadAngle();
-	this.angles_z = ReadAngle();
+	this.angles = ReadAngleVector();
 
 	this.drawmask  = MASK_NORMAL;
 	setmodel(this, MDL_Null); // give it a size for clientcamera
diff --git a/qcsrc/common/gamemodes/gamemode/onslaught/sv_onslaught.qc b/qcsrc/common/gamemodes/gamemode/onslaught/sv_onslaught.qc
index 1f3e43378..170845298 100644
--- a/qcsrc/common/gamemodes/gamemode/onslaught/sv_onslaught.qc
+++ b/qcsrc/common/gamemodes/gamemode/onslaught/sv_onslaught.qc
@@ -39,9 +39,7 @@ bool clientcamera_send(entity this, entity to, int sf)
 
 	WriteVector(MSG_ENTITY, this.origin);
 
-	WriteAngle(MSG_ENTITY, this.angles_x);
-	WriteAngle(MSG_ENTITY, this.angles_y);
-	WriteAngle(MSG_ENTITY, this.angles_z);
+	WriteAngleVector(MSG_ENTITY, this.angles);
 
 	return true;
 }
@@ -876,6 +874,7 @@ void ons_camSetup(entity this)
 
 	FOREACH_CLIENT(true, it.clientcamera = cam;);
 
+	// NOTE: engine networked
 	WriteByte(MSG_ALL, SVC_SETVIEWANGLES);
 	WriteAngle(MSG_ALL, cam.angles_x);
 	WriteAngle(MSG_ALL, cam.angles_y);
diff --git a/qcsrc/common/mapobjects/func/plat.qc b/qcsrc/common/mapobjects/func/plat.qc
index 53dbed02f..64275a357 100644
--- a/qcsrc/common/mapobjects/func/plat.qc
+++ b/qcsrc/common/mapobjects/func/plat.qc
@@ -31,9 +31,7 @@ float plat_send(entity this, entity to, float sf)
 
 		WriteVector(MSG_ENTITY, this.size);
 
-		WriteAngle(MSG_ENTITY, this.mangle_x);
-		WriteAngle(MSG_ENTITY, this.mangle_y);
-		WriteAngle(MSG_ENTITY, this.mangle_z);
+		WriteAngleVector(MSG_ENTITY, this.mangle);
 
 		WriteShort(MSG_ENTITY, this.speed);
 		WriteShort(MSG_ENTITY, this.height);
@@ -152,9 +150,7 @@ NET_HANDLE(ENT_CLIENT_PLAT, bool isnew)
 
 		this.size = ReadVector();
 
-		this.mangle_x = ReadAngle();
-		this.mangle_y = ReadAngle();
-		this.mangle_z = ReadAngle();
+		this.mangle = ReadAngleVector();
 
 		this.speed = ReadShort();
 		this.height = ReadShort();
diff --git a/qcsrc/common/mapobjects/func/train.qc b/qcsrc/common/mapobjects/func/train.qc
index 4e9c33456..4ee741d58 100644
--- a/qcsrc/common/mapobjects/func/train.qc
+++ b/qcsrc/common/mapobjects/func/train.qc
@@ -164,9 +164,7 @@ float train_send(entity this, entity to, float sf)
 
 		WriteVector(MSG_ENTITY, this.view_ofs);
 
-		WriteAngle(MSG_ENTITY, this.mangle_x);
-		WriteAngle(MSG_ENTITY, this.mangle_y);
-		WriteAngle(MSG_ENTITY, this.mangle_z);
+		WriteAngleVector(MSG_ENTITY, this.mangle);
 
 		WriteShort(MSG_ENTITY, this.speed);
 		WriteShort(MSG_ENTITY, this.height);
@@ -303,9 +301,7 @@ NET_HANDLE(ENT_CLIENT_TRAIN, bool isnew)
 
 		this.view_ofs = ReadVector();
 
-		this.mangle_x = ReadAngle();
-		this.mangle_y = ReadAngle();
-		this.mangle_z = ReadAngle();
+		this.mangle = ReadAngleVector();
 
 		this.speed = ReadShort();
 		this.height = ReadShort();
diff --git a/qcsrc/common/mapobjects/misc/laser.qc b/qcsrc/common/mapobjects/misc/laser.qc
index 812dce76b..77435e938 100644
--- a/qcsrc/common/mapobjects/misc/laser.qc
+++ b/qcsrc/common/mapobjects/misc/laser.qc
@@ -161,8 +161,7 @@ bool laser_SendEntity(entity this, entity to, float sendflags)
 		}
 		else
 		{
-			WriteAngle(MSG_ENTITY, this.mangle_x);
-			WriteAngle(MSG_ENTITY, this.mangle_y);
+			WriteAngleVector2D(MSG_ENTITY, this.mangle);
 		}
 	}
 	if(sendflags & SF_LASER_UPDATE_ACTIVE)
@@ -388,8 +387,7 @@ NET_HANDLE(ENT_CLIENT_LASER, bool isnew)
 		}
 		else
 		{
-			this.angles_x = ReadAngle();
-			this.angles_y = ReadAngle();
+			this.angles = ReadAngleVector2D();
 		}
 	}
 	if(sendflags & SF_LASER_UPDATE_ACTIVE)
diff --git a/qcsrc/common/mapobjects/misc/teleport_dest.qc b/qcsrc/common/mapobjects/misc/teleport_dest.qc
index 7402726d4..6bf95f24f 100644
--- a/qcsrc/common/mapobjects/misc/teleport_dest.qc
+++ b/qcsrc/common/mapobjects/misc/teleport_dest.qc
@@ -14,10 +14,7 @@ bool teleport_dest_send(entity this, entity to, int sendflags)
 		WriteCoord(MSG_ENTITY, this.speed);
 		WriteString(MSG_ENTITY, this.targetname);
 		WriteVector(MSG_ENTITY, this.origin);
-
-		WriteAngle(MSG_ENTITY, this.mangle_x);
-		WriteAngle(MSG_ENTITY, this.mangle_y);
-		WriteAngle(MSG_ENTITY, this.mangle_z);
+		WriteAngleVector(MSG_ENTITY, this.mangle);
 	}
 
 	return true;
@@ -72,10 +69,7 @@ NET_HANDLE(ENT_CLIENT_TELEPORT_DEST, bool isnew)
 		this.speed = ReadCoord();
 		this.targetname = strzone(ReadString());
 		this.origin = ReadVector();
-
-		this.mangle_x = ReadAngle();
-		this.mangle_y = ReadAngle();
-		this.mangle_z = ReadAngle();
+		this.mangle = ReadAngleVector();
 
 		setorigin(this, this.origin);
 
diff --git a/qcsrc/common/mapobjects/models.qc b/qcsrc/common/mapobjects/models.qc
index bcaca193f..c70effedb 100644
--- a/qcsrc/common/mapobjects/models.qc
+++ b/qcsrc/common/mapobjects/models.qc
@@ -109,11 +109,7 @@ bool g_clientmodel_genericsendentity(entity this, entity to, int sf)
 	if(sf & BIT(2))
 	{
 		if(sf & 0x10)
-		{
-			WriteAngle(MSG_ENTITY, this.angles.x);
-			WriteAngle(MSG_ENTITY, this.angles.y);
-			WriteAngle(MSG_ENTITY, this.angles.z);
-		}
+			WriteAngleVector(MSG_ENTITY, this.angles);
 	}
 
 	if(sf & BIT(3))
@@ -334,11 +330,7 @@ NET_HANDLE(ENT_CLIENT_WALL, bool isnew)
 	if(f & 4)
 	{
 		if(f & 0x10)
-		{
-			this.angles_x = ReadAngle();
-			this.angles_y = ReadAngle();
-			this.angles_z = ReadAngle();
-		}
+			this.angles = ReadAngleVector();
 		else
 			this.angles = '0 0 0';
 	}
diff --git a/qcsrc/common/mapobjects/trigger/jumppads.qc b/qcsrc/common/mapobjects/trigger/jumppads.qc
index 5138419d1..b016dde9a 100644
--- a/qcsrc/common/mapobjects/trigger/jumppads.qc
+++ b/qcsrc/common/mapobjects/trigger/jumppads.qc
@@ -634,9 +634,7 @@ bool target_push_send(entity this, entity to, float sf)
 	WriteString(MSG_ENTITY, this.targetname);
 	WriteVector(MSG_ENTITY, this.origin);
 
-	WriteAngle(MSG_ENTITY, this.angles_x);
-	WriteAngle(MSG_ENTITY, this.angles_y);
-	WriteAngle(MSG_ENTITY, this.angles_z);
+	WriteAngleVector(MSG_ENTITY, this.angles);
 
 	return true;
 }
@@ -726,9 +724,7 @@ NET_HANDLE(ENT_CLIENT_TARGET_PUSH, bool isnew)
 	this.targetname = strzone(ReadString());
 	this.origin = ReadVector();
 
-	this.angles_x = ReadAngle();
-	this.angles_y = ReadAngle();
-	this.angles_z = ReadAngle();
+	this.angles = ReadAngleVector();
 
 	return = true;
 
diff --git a/qcsrc/common/mapobjects/trigger/viewloc.qc b/qcsrc/common/mapobjects/trigger/viewloc.qc
index ba5dcbe44..354d36cb7 100644
--- a/qcsrc/common/mapobjects/trigger/viewloc.qc
+++ b/qcsrc/common/mapobjects/trigger/viewloc.qc
@@ -125,9 +125,7 @@ bool viewloc_send(entity this, entity to, int sf)
 
 	WriteVector(MSG_ENTITY, this.origin);
 
-	WriteAngle(MSG_ENTITY, this.angles_x);
-	WriteAngle(MSG_ENTITY, this.angles_y);
-	WriteAngle(MSG_ENTITY, this.angles_z);
+	WriteAngleVector(MSG_ENTITY, this.angles);
 
 	return true;
 }
@@ -200,9 +198,7 @@ NET_HANDLE(ENT_CLIENT_VIEWLOC, bool isnew)
 	this.origin = ReadVector();
 	setorigin(this, this.origin);
 
-	this.movedir_x = ReadAngle();
-	this.movedir_y = ReadAngle();
-	this.movedir_z = ReadAngle();
+	this.movedir = ReadAngleVector();
 
 	return = true;
 
diff --git a/qcsrc/common/t_items.qc b/qcsrc/common/t_items.qc
index ef2923b86..3b550f93e 100644
--- a/qcsrc/common/t_items.qc
+++ b/qcsrc/common/t_items.qc
@@ -168,9 +168,7 @@ NET_HANDLE(ENT_CLIENT_ITEM, bool isnew)
 
     if(sf & ISF_ANGLES)
     {
-        this.angles_x = ReadAngle();
-        this.angles_y = ReadAngle();
-        this.angles_z = ReadAngle();
+        this.angles = ReadAngleVector();
     }
 
     if(sf & ISF_SIZE)
@@ -321,9 +319,7 @@ bool ItemSend(entity this, entity to, int sf)
 
 	if(sf & ISF_ANGLES)
 	{
-		WriteAngle(MSG_ENTITY, this.angles_x);
-		WriteAngle(MSG_ENTITY, this.angles_y);
-		WriteAngle(MSG_ENTITY, this.angles_z);
+		WriteAngleVector(MSG_ENTITY, this.angles);
 	}
 
 	// sets size on the client, unused on server
diff --git a/qcsrc/common/turrets/cl_turrets.qc b/qcsrc/common/turrets/cl_turrets.qc
index df20e1ad2..fca9f7f23 100644
--- a/qcsrc/common/turrets/cl_turrets.qc
+++ b/qcsrc/common/turrets/cl_turrets.qc
@@ -376,8 +376,7 @@ NET_HANDLE(ENT_CLIENT_TURRET, bool isnew)
 		this.origin = ReadVector();
 		setorigin(this, this.origin);
 
-		this.angles_x = ReadAngle();
-		this.angles_y = ReadAngle();
+		this.angles = ReadAngleVector2D();
 
 		turret_construct(this, isnew);
 		this.colormap = 1024;
diff --git a/qcsrc/common/turrets/sv_turrets.qc b/qcsrc/common/turrets/sv_turrets.qc
index d8587b14a..ceaae8ad5 100644
--- a/qcsrc/common/turrets/sv_turrets.qc
+++ b/qcsrc/common/turrets/sv_turrets.qc
@@ -320,7 +320,6 @@ void turrets_setframe(entity this, float _frame, float client_only)
 
 bool turret_send(entity this, entity to, float sf)
 {
-
 	WriteHeader(MSG_ENTITY, ENT_CLIENT_TURRET);
 	WriteByte(MSG_ENTITY, sf);
 	if(sf & TNSF_SETUP)
@@ -329,8 +328,7 @@ bool turret_send(entity this, entity to, float sf)
 
 		WriteVector(MSG_ENTITY, this.origin);
 
-		WriteAngle(MSG_ENTITY, this.angles_x);
-		WriteAngle(MSG_ENTITY, this.angles_y);
+		WriteAngleVector2D(MSG_ENTITY, this.angles);
 	}
 
 	if(sf & TNSF_ANG)
diff --git a/qcsrc/common/vehicles/sv_vehicles.qc b/qcsrc/common/vehicles/sv_vehicles.qc
index 7273f5a64..5d864b1b5 100644
--- a/qcsrc/common/vehicles/sv_vehicles.qc
+++ b/qcsrc/common/vehicles/sv_vehicles.qc
@@ -839,6 +839,7 @@ void vehicles_exit(entity vehic, bool eject)
 			WriteByte (MSG_ONE, SVC_SETVIEWPORT);
 			WriteEntity( MSG_ONE, player);
 
+			// NOTE: engine networked
 			WriteByte (MSG_ONE, SVC_SETVIEWANGLES);
 			WriteAngle(MSG_ONE, 0);
 			WriteAngle(MSG_ONE, vehic.angles_y);
@@ -1081,6 +1082,7 @@ void vehicles_enter(entity pl, entity veh)
 		WriteByte (MSG_ONE, SVC_SETVIEWPORT);
 		WriteEntity(MSG_ONE, veh.vehicle_viewport);
 
+		// NOTE: engine networked
 		WriteByte (MSG_ONE, SVC_SETVIEWANGLES);
 		if(veh.tur_head)
 		{
diff --git a/qcsrc/common/vehicles/vehicle/bumblebee.qc b/qcsrc/common/vehicles/vehicle/bumblebee.qc
index 224f96564..0a8875b48 100644
--- a/qcsrc/common/vehicles/vehicle/bumblebee.qc
+++ b/qcsrc/common/vehicles/vehicle/bumblebee.qc
@@ -230,6 +230,7 @@ void bumblebee_gunner_exit(entity this, int _exitflag)
 		WriteByte(MSG_ONE, SVC_SETVIEWPORT);
 		WriteEntity(MSG_ONE, player);
 
+		// NOTE: engine networked
 		WriteByte(MSG_ONE, SVC_SETVIEWANGLES);
 		WriteAngle(MSG_ONE, 0);
 		WriteAngle(MSG_ONE, vehic.angles.y);
@@ -340,6 +341,7 @@ bool bumblebee_gunner_enter(entity this, entity player)
 		WriteByte(MSG_ONE, 		SVC_SETVIEWPORT);
 		WriteEntity(MSG_ONE, 	gunner.vehicle_viewport);
 
+		// NOTE: engine networked
 		WriteByte(MSG_ONE, 		SVC_SETVIEWANGLES);
 		WriteAngle(MSG_ONE, 	gunner.angles_x + vehic.angles_x); // tilt
 		WriteAngle(MSG_ONE, 	gunner.angles_y + vehic.angles_y); // yaw
diff --git a/qcsrc/common/weapons/weapon/arc.qc b/qcsrc/common/weapons/weapon/arc.qc
index 387bb910f..5f79c8ccf 100644
--- a/qcsrc/common/weapons/weapon/arc.qc
+++ b/qcsrc/common/weapons/weapon/arc.qc
@@ -40,9 +40,7 @@ bool W_Arc_Beam_Send(entity this, entity to, int sf)
 	}
 	if(sf & ARC_SF_BEAMDIR) // beam direction
 	{
-		WriteAngle(MSG_ENTITY, this.beam_dir.x);
-		WriteAngle(MSG_ENTITY, this.beam_dir.y);
-		WriteAngle(MSG_ENTITY, this.beam_dir.z);
+		WriteAngleVector(MSG_ENTITY, this.beam_dir);
 	}
 	if(sf & ARC_SF_BEAMTYPE) // beam type
 	{
@@ -1274,9 +1272,7 @@ NET_HANDLE(ENT_CLIENT_ARC_BEAM, bool isnew)
 
 	if(sf & ARC_SF_BEAMDIR) // beam direction
 	{
-		this.angles_x = ReadAngle();
-		this.angles_y = ReadAngle();
-		this.angles_z = ReadAngle();
+		this.angles = ReadAngleVector();
 	}
 
 	if(sf & ARC_SF_BEAMTYPE) // beam type
diff --git a/qcsrc/common/wepent.qc b/qcsrc/common/wepent.qc
index 5e78207d4..3d751035b 100644
--- a/qcsrc/common/wepent.qc
+++ b/qcsrc/common/wepent.qc
@@ -36,10 +36,10 @@ MACRO_END
     \
     PROP(false, porto_v_angle_held, WEPENT_SET_NORMAL, \
 	{ WriteByte(chan, this.porto_v_angle_held); if(this.porto_v_angle_held) { \
-		 WriteAngle(chan, this.owner.porto_v_angle.x); WriteAngle(chan, this.owner.porto_v_angle.y); \
+		 WriteAngleVector2D(chan, this.owner.porto_v_angle); \
 		} }, \
 	{ (viewmodels[this.m_wepent_slot]).angles_held_status = ReadByte(); if((viewmodels[this.m_wepent_slot]).angles_held_status) { \
-		(viewmodels[this.m_wepent_slot]).angles_held = vec2(ReadAngle(), ReadAngle()); } \
+		(viewmodels[this.m_wepent_slot]).angles_held = ReadAngleVector2D(); } \
 		else { (viewmodels[this.m_wepent_slot]).angles_held = '0 0 0'; } }) \
     \
     PROP(false, tuba_instrument, WEPENT_SET_NORMAL, \
diff --git a/qcsrc/lib/net.qh b/qcsrc/lib/net.qh
index 3cec9fd9b..522b4ff23 100644
--- a/qcsrc/lib/net.qh
+++ b/qcsrc/lib/net.qh
@@ -357,6 +357,8 @@ MACRO_END
 		#define ReadFloat() ReadCoord()
 		#define ReadVector() vec3(ReadFloat(), ReadFloat(), ReadFloat())
 		#define ReadVector2D() vec2(ReadFloat(), ReadFloat())
+		#define ReadAngleVector() vec3(ReadAngle(), ReadAngle(), ReadAngle())
+		#define ReadAngleVector2D() vec2(ReadAngle(), ReadAngle())
 
 		int Readbits(int num)
 		{
@@ -397,6 +399,8 @@ MACRO_END
 		#define WriteFloat(to, f) WriteCoord(to, f)
 		#define WriteVector(to, v) MACRO_BEGIN WriteFloat(to, v.x); WriteFloat(to, v.y); WriteFloat(to, v.z); MACRO_END
 		#define WriteVector2D(to, v) MACRO_BEGIN WriteFloat(to, v.x); WriteFloat(to, v.y); MACRO_END
+		#define WriteAngleVector(to, v) MACRO_BEGIN WriteAngle(to, v.x); WriteAngle(to, v.y); WriteAngle(to, v.z); MACRO_END
+		#define WriteAngleVector2D(to, v) MACRO_BEGIN WriteAngle(to, v.x); WriteAngle(to, v.y); MACRO_END
 
 		void Writebits(float dst, float val, int num)
 		{
-- 
2.39.5