shithub: qk1

Download patch

ref: 1d16b811ef7e5322c31ad1586cf9e471ea959317
parent: da3a52a4ad18fa6deb154e0e4b71b755699d5b3b
author: Sigrid Solveig Haflínudóttir <sigrid@ftrv.se>
date: Thu Nov 2 14:42:06 EDT 2023

cl: put protocol into client instead of allocating

--- a/cl_input.c
+++ b/cl_input.c
@@ -330,7 +330,7 @@
 	MSG_WriteFloat (&buf, cl.mtime[0]);	// so server can get ping times
 
 	for (i=0 ; i<3 ; i++)
-		cl.protocol->MSG_WriteAngle (&buf, cl.viewangles[i]);
+		cl.protocol.MSG_WriteAngle(&buf, cl.viewangles[i]);
 	
     MSG_WriteShort (&buf, cmd->forwardmove);
     MSG_WriteShort (&buf, cmd->sidemove);
--- a/cl_main.c
+++ b/cl_main.c
@@ -50,7 +50,7 @@
 // wipe the entire cl structure
 	memset(&cl, 0, sizeof cl);
 
-	//if(cls.state == ca_connected)
+	if(cls.state == ca_connected)
 		stopallsfx();
 
 	SZ_Clear (&cls.message);
--- a/cl_parse.c
+++ b/cl_parse.c
@@ -97,7 +97,7 @@
     field_mask = MSG_ReadByte(); 
     volume = (field_mask & SND_VOLUME) ? MSG_ReadByte() : Spktvol;
 	attenuation = (field_mask & SND_ATTENUATION) ? MSG_ReadByte()/64.0 : Spktatt;
-	if(field_mask & (cl.protocol->fl_large_entity | cl.protocol->fl_large_channel)){
+	if(field_mask & (cl.protocol.fl_large_entity | cl.protocol.fl_large_channel)){
 		ent = MSG_ReadShort();
 		channel = MSG_ReadByte();
 	}else{
@@ -105,7 +105,7 @@
 		ent = channel >> 3;
 		channel &= 7;
 	}
-	sound_num = (field_mask & cl.protocol->fl_large_sound) ? MSG_ReadShort() : MSG_ReadByte();
+	sound_num = (field_mask & cl.protocol.fl_large_sound) ? MSG_ReadShort() : MSG_ReadByte();
 
 	if(ent > MAX_EDICTS)
 		Host_Error("CL_ParseStartSoundPacket: ent = %d", ent);
@@ -197,7 +197,6 @@
 
 // parse protocol version number
 	i = MSG_ReadLong ();
-	free(cl.protocol);
 	p = nil;
 	for(n = 0; n < nelem(protos); n++){
 		if(protos[n].version == i){
@@ -209,9 +208,8 @@
 		Con_Printf ("Server returned unknown protocol version %d", i);
 		return;
 	}
-	cl.protocol = malloc(sizeof(*cl.protocol));
-	memmove(cl.protocol, p, sizeof(*cl.protocol));
-	cl.protocol->MSG_ReadProtocolInfo(cl.protocol);
+	memmove(&cl.protocol, p, sizeof(cl.protocol));
+	cl.protocol.MSG_ReadProtocolInfo(&cl.protocol);
 
 // parse maxclients
 	cl.maxclients = MSG_ReadByte ();
@@ -372,21 +370,21 @@
 		ent->skinnum = skin;
 	}
 
-	ent->msg_origins[0][0] = (bits & U_ORIGIN1) ? cl.protocol->MSG_ReadCoord() : ent->baseline.origin[0];
-	ent->msg_angles[0][0] = (bits & U_ANGLE1) ? cl.protocol->MSG_ReadAngle() : ent->baseline.angles[0];
-	ent->msg_origins[0][1] = (bits & U_ORIGIN2) ? cl.protocol->MSG_ReadCoord() : ent->baseline.origin[1];
-	ent->msg_angles[0][1] = (bits & U_ANGLE2) ? cl.protocol->MSG_ReadAngle() : ent->baseline.angles[1];
-	ent->msg_origins[0][2] = (bits & U_ORIGIN3) ? cl.protocol->MSG_ReadCoord() : ent->baseline.origin[2];
-	ent->msg_angles[0][2] = (bits & U_ANGLE3) ? cl.protocol->MSG_ReadAngle() : ent->baseline.angles[2];
+	ent->msg_origins[0][0] = (bits & U_ORIGIN1) ? cl.protocol.MSG_ReadCoord() : ent->baseline.origin[0];
+	ent->msg_angles[0][0] = (bits & U_ANGLE1) ? cl.protocol.MSG_ReadAngle() : ent->baseline.angles[0];
+	ent->msg_origins[0][1] = (bits & U_ORIGIN2) ? cl.protocol.MSG_ReadCoord() : ent->baseline.origin[1];
+	ent->msg_angles[0][1] = (bits & U_ANGLE2) ? cl.protocol.MSG_ReadAngle() : ent->baseline.angles[1];
+	ent->msg_origins[0][2] = (bits & U_ORIGIN3) ? cl.protocol.MSG_ReadCoord() : ent->baseline.origin[2];
+	ent->msg_angles[0][2] = (bits & U_ANGLE3) ? cl.protocol.MSG_ReadAngle() : ent->baseline.angles[2];
 
-	ent->alpha = (bits & cl.protocol->fl_alpha) ? MSG_ReadByte() : ent->baseline.alpha;
+	ent->alpha = (bits & cl.protocol.fl_alpha) ? MSG_ReadByte() : ent->baseline.alpha;
 
 	if(bits & U_NOLERP)
 		ent->forcelink = true;
 
-	if(bits & cl.protocol->fl_large_frame)
+	if(bits & cl.protocol.fl_large_frame)
 		ent->frame |= MSG_ReadByte()<<8;
-	if(bits & cl.protocol->fl_large_model)
+	if(bits & cl.protocol.fl_large_model)
 		modnum |= MSG_ReadByte()<<8;
 
 	if(modnum < 0 || modnum >= MAX_MODELS)
@@ -428,16 +426,16 @@
 	int i, bits;
 
 	bits = withbits ? MSG_ReadByte() : 0;
-	ent->baseline.modelindex = (bits & cl.protocol->fl_large_baseline_model) ? MSG_ReadShort() : MSG_ReadByte();
-	ent->baseline.frame = (bits & cl.protocol->fl_large_baseline_frame) ? MSG_ReadShort() : MSG_ReadByte();
+	ent->baseline.modelindex = (bits & cl.protocol.fl_large_baseline_model) ? MSG_ReadShort() : MSG_ReadByte();
+	ent->baseline.frame = (bits & cl.protocol.fl_large_baseline_frame) ? MSG_ReadShort() : MSG_ReadByte();
 	ent->baseline.colormap = MSG_ReadByte();
 	ent->baseline.skin = MSG_ReadByte();
 	for (i=0 ; i<3 ; i++)
 	{
-		ent->baseline.origin[i] = cl.protocol->MSG_ReadCoord ();
-		ent->baseline.angles[i] = cl.protocol->MSG_ReadAngle ();
+		ent->baseline.origin[i] = cl.protocol.MSG_ReadCoord ();
+		ent->baseline.angles[i] = cl.protocol.MSG_ReadAngle ();
 	}
-	ent->baseline.alpha = (bits & cl.protocol->fl_baseline_alpha) ? MSG_ReadByte() : DEFAULT_ALPHA;
+	ent->baseline.alpha = (bits & cl.protocol.fl_baseline_alpha) ? MSG_ReadByte() : DEFAULT_ALPHA;
 }
 
 
@@ -520,7 +518,7 @@
 		Sbar_Changed();
 	}
 
-	if(bits & cl.protocol->fl_large_weaponmodel)
+	if(bits & cl.protocol.fl_large_weaponmodel)
 		weaponmodel |= MSG_ReadByte() << 8;
 	if(cl.stats[STAT_WEAPON] != weaponmodel){
 		cl.stats[STAT_WEAPON] = weaponmodel;
@@ -527,9 +525,9 @@
 		Sbar_Changed();
 	}
 
-	if(bits & cl.protocol->fl_large_weaponframe)
+	if(bits & cl.protocol.fl_large_weaponframe)
 		cl.stats[STAT_WEAPONFRAME] |= MSG_ReadByte() << 8;
-	cl.viewent.alpha = (bits & cl.protocol->fl_weapon_alpha) ? MSG_ReadByte() : DEFAULT_ALPHA;
+	cl.viewent.alpha = (bits & cl.protocol.fl_weapon_alpha) ? MSG_ReadByte() : DEFAULT_ALPHA;
 
 	if(cl.viewent.model != cl.model_precache[cl.stats[STAT_WEAPON]]){
 		// FIXME(sigrid) - reset lerp
@@ -699,7 +697,7 @@
 			}
 			if(p == nil)
 				Host_Error("CL_ParseServerMessage: server uses unknown protocol %d\n", i);
-			if(p->version != cl.protocol->version)
+			if(p->version != cl.protocol.version)
 				Host_Error("CL_ParseServerMessage: server decided to switch protocol to %d mid-game?\n", i);
 			break;
 			
@@ -729,7 +727,7 @@
 			
 		case svc_setangle:
 			for (i=0 ; i<3 ; i++)
-				cl.viewangles[i] = cl.protocol->MSG_ReadAngle ();
+				cl.viewangles[i] = cl.protocol.MSG_ReadAngle ();
 			break;
 			
 		case svc_setview:
--- a/client.h
+++ b/client.h
@@ -119,7 +119,7 @@
 //
 typedef struct
 {
-	protocol_t *protocol;
+	protocol_t  protocol;
 	int			movemessages;	// since connecting to this server
 								// throw out the first couple, so the player
 								// doesn't accidentally do something the 
@@ -290,6 +290,7 @@
 
 void CL_ParseTEnt (void);
 void CL_UpdateTEnts (void);
+void CL_ResetTEnts (void);
 
 void CL_ClearState (void);
 
--- a/common.h
+++ b/common.h
@@ -30,11 +30,6 @@
 void InsertLinkBefore (link_t *l, link_t *before);
 void InsertLinkAfter (link_t *l, link_t *after);
 
-// (type *)STRUCT_FROM_LINK(link_t *link, type, member)
-// ent = STRUCT_FROM_LINK(link,entity_t,order)
-// FIXME: remove this mess!
-#define	STRUCT_FROM_LINK(l,t,m) ((t *)((byte *)l - (uintptr)&(((t *)0)->m)))
-
 //============================================================================
 
 #define max(a,b) ((a)>(b)?(a):(b))
@@ -100,15 +95,15 @@
 float MSG_ReadAngleInt16 (void);
 
 #define MSG_ReadVec(proto, d) do{ \
-	(d)[0] = (proto)->MSG_ReadCoord(); \
-	(d)[1] = (proto)->MSG_ReadCoord(); \
-	(d)[2] = (proto)->MSG_ReadCoord(); \
+	(d)[0] = (proto).MSG_ReadCoord(); \
+	(d)[1] = (proto).MSG_ReadCoord(); \
+	(d)[2] = (proto).MSG_ReadCoord(); \
 }while(0)
 
 #define MSG_WriteVec(proto, sb, s) do{ \
-	(proto)->MSG_WriteCoord(sb, (s)[0]); \
-	(proto)->MSG_WriteCoord(sb, (s)[1]); \
-	(proto)->MSG_WriteCoord(sb, (s)[2]); \
+	(proto).MSG_WriteCoord(sb, (s)[0]); \
+	(proto).MSG_WriteCoord(sb, (s)[1]); \
+	(proto).MSG_WriteCoord(sb, (s)[2]); \
 }while(0)
 
 //============================================================================
--- a/progs.h
+++ b/progs.h
@@ -27,7 +27,7 @@
 	entvars_t	v;					// C exported fields from progs
 // other fields from progs come immediately after
 } edict_t;
-#define	EDICT_FROM_AREA(l) STRUCT_FROM_LINK(l,edict_t,area)
+#define	EDICT_FROM_AREA(l) (edict_t*)((byte*)l - offsetof(edict_t, area))
 
 //============================================================================
 
--- a/sv_main.c
+++ b/sv_main.c
@@ -99,8 +99,8 @@
 
 	if (sv.datagram.cursize > MAX_DATAGRAM-16)
 		return;	
-	MSG_WriteByte (&sv.datagram, svc_particle);
-	MSG_WriteVec(sv.protocol, &sv.datagram, org);
+	MSG_WriteByte(&sv.datagram, svc_particle);
+	MSG_WriteVec(*sv.protocol, &sv.datagram, org);
 	for (i=0 ; i<3 ; i++)
 	{
 		v = dir[i]*16;