DarkPlaces
Game engine based on the Quake 1 engine by id Software, developed by LadyHavoc
 
cl_ents.c
Go to the documentation of this file.
1#include "quakedef.h"
2#include "protocol.h"
3
5{
6 unsigned int bits;
8 if (bits & 0x00000080)
9 {
10 bits |= MSG_ReadByte(&cl_message) << 8;
11 if (bits & 0x00008000)
12 {
13 bits |= MSG_ReadByte(&cl_message) << 16;
14 if (bits & 0x00800000)
15 bits |= MSG_ReadByte(&cl_message) << 24;
16 }
17 }
18 return bits;
19}
20
21void EntityState_ReadFields(entity_state_t *e, unsigned int bits)
22{
24 {
25 if (bits & E_ORIGIN1)
27 if (bits & E_ORIGIN2)
29 if (bits & E_ORIGIN3)
31 }
32 else
33 {
34 if (bits & E_FLAGS)
37 {
38 if (bits & E_ORIGIN1)
40 if (bits & E_ORIGIN2)
42 if (bits & E_ORIGIN3)
44 }
45 else
46 {
47 if (bits & E_ORIGIN1)
49 if (bits & E_ORIGIN2)
51 if (bits & E_ORIGIN3)
53 }
54 }
56 {
57 if (bits & E_ANGLE1)
59 if (bits & E_ANGLE2)
61 if (bits & E_ANGLE3)
63 }
64 else
65 {
66 if (bits & E_ANGLE1)
68 if (bits & E_ANGLE2)
70 if (bits & E_ANGLE3)
72 }
73 if (bits & E_MODEL1)
74 e->modelindex = (e->modelindex & 0xFF00) | (unsigned int) MSG_ReadByte(&cl_message);
75 if (bits & E_MODEL2)
76 e->modelindex = (e->modelindex & 0x00FF) | ((unsigned int) MSG_ReadByte(&cl_message) << 8);
77 if (bits & E_FRAME1)
78 e->frame = (e->frame & 0xFF00) | (unsigned int) MSG_ReadByte(&cl_message);
79 if (bits & E_FRAME2)
80 e->frame = (e->frame & 0x00FF) | ((unsigned int) MSG_ReadByte(&cl_message) << 8);
81 if (bits & E_EFFECTS1)
82 e->effects = (e->effects & 0xFF00) | (unsigned int) MSG_ReadByte(&cl_message);
83 if (bits & E_EFFECTS2)
84 e->effects = (e->effects & 0x00FF) | ((unsigned int) MSG_ReadByte(&cl_message) << 8);
85 if (bits & E_COLORMAP)
87 if (bits & E_SKIN)
89 if (bits & E_ALPHA)
91 if (bits & E_SCALE)
93 if (bits & E_GLOWSIZE)
95 if (bits & E_GLOWCOLOR)
98 if (bits & E_FLAGS)
100 if (bits & E_TAGATTACHMENT)
101 {
102 e->tagentity = (unsigned short) MSG_ReadShort(&cl_message);
104 }
105 if (bits & E_LIGHT)
106 {
107 e->light[0] = (unsigned short) MSG_ReadShort(&cl_message);
108 e->light[1] = (unsigned short) MSG_ReadShort(&cl_message);
109 e->light[2] = (unsigned short) MSG_ReadShort(&cl_message);
110 e->light[3] = (unsigned short) MSG_ReadShort(&cl_message);
111 }
112 if (bits & E_LIGHTSTYLE)
114 if (bits & E_LIGHTPFLAGS)
116
118 {
119 Con_Printf("ReadFields e%i", e->number);
120
121 if (bits & E_ORIGIN1)
122 Con_Printf(" E_ORIGIN1 %f", e->origin[0]);
123 if (bits & E_ORIGIN2)
124 Con_Printf(" E_ORIGIN2 %f", e->origin[1]);
125 if (bits & E_ORIGIN3)
126 Con_Printf(" E_ORIGIN3 %f", e->origin[2]);
127 if (bits & E_ANGLE1)
128 Con_Printf(" E_ANGLE1 %f", e->angles[0]);
129 if (bits & E_ANGLE2)
130 Con_Printf(" E_ANGLE2 %f", e->angles[1]);
131 if (bits & E_ANGLE3)
132 Con_Printf(" E_ANGLE3 %f", e->angles[2]);
133 if (bits & (E_MODEL1 | E_MODEL2))
134 Con_Printf(" E_MODEL %i", e->modelindex);
135
136 if (bits & (E_FRAME1 | E_FRAME2))
137 Con_Printf(" E_FRAME %i", e->frame);
138 if (bits & (E_EFFECTS1 | E_EFFECTS2))
139 Con_Printf(" E_EFFECTS %i", e->effects);
140 if (bits & E_ALPHA)
141 Con_Printf(" E_ALPHA %f", e->alpha / 255.0f);
142 if (bits & E_SCALE)
143 Con_Printf(" E_SCALE %f", e->scale / 16.0f);
144 if (bits & E_COLORMAP)
145 Con_Printf(" E_COLORMAP %i", e->colormap);
146 if (bits & E_SKIN)
147 Con_Printf(" E_SKIN %i", e->skin);
148
149 if (bits & E_GLOWSIZE)
150 Con_Printf(" E_GLOWSIZE %i", e->glowsize * 4);
151 if (bits & E_GLOWCOLOR)
152 Con_Printf(" E_GLOWCOLOR %i", e->glowcolor);
153
154 if (bits & E_LIGHT)
155 Con_Printf(" E_LIGHT %i:%i:%i:%i", e->light[0], e->light[1], e->light[2], e->light[3]);
156 if (bits & E_LIGHTPFLAGS)
157 Con_Printf(" E_LIGHTPFLAGS %i", e->lightpflags);
158
159 if (bits & E_TAGATTACHMENT)
160 Con_Printf(" E_TAGATTACHMENT e%i:%i", e->tagentity, e->tagindex);
161 if (bits & E_LIGHTSTYLE)
162 Con_Printf(" E_LIGHTSTYLE %i", e->lightstyle);
163 Con_Print("\n");
164 }
165}
166
167// (client) adds a entity_frame to the database, for future reference
168void EntityFrame_AddFrame_Client(entityframe_database_t *d, vec3_t eye, int framenum, int numentities, const entity_state_t *entitydata)
169{
170 int n, e;
171 entity_frameinfo_t *info;
172
173 VectorCopy(eye, d->eye);
174
175 // figure out how many entity slots are used already
176 if (d->numframes)
177 {
178 n = d->frames[d->numframes - 1].endentity - d->frames[0].firstentity;
179 if (n + numentities > MAX_ENTITY_DATABASE || d->numframes >= MAX_ENTITY_HISTORY)
180 {
181 // ran out of room, dump database
183 }
184 }
185
186 info = &d->frames[d->numframes];
187 info->framenum = framenum;
188 e = -1000;
189 // make sure we check the newly added frame as well, but we haven't incremented numframes yet
190 for (n = 0;n <= d->numframes;n++)
191 {
192 if (e >= d->frames[n].framenum)
193 {
194 if (e == framenum)
195 Con_Print("EntityFrame_AddFrame: tried to add out of sequence frame to database\n");
196 else
197 Con_Print("EntityFrame_AddFrame: out of sequence frames in database\n");
198 return;
199 }
200 e = d->frames[n].framenum;
201 }
202 // if database still has frames after that...
203 if (d->numframes)
204 info->firstentity = d->frames[d->numframes - 1].endentity;
205 else
206 info->firstentity = 0;
207 info->endentity = info->firstentity + numentities;
208 d->numframes++;
209
212 if (e > numentities)
213 e = numentities;
214 memcpy(d->entitydata + n, entitydata, sizeof(entity_state_t) * e);
215 if (numentities > e)
216 memcpy(d->entitydata, entitydata + e, sizeof(entity_state_t) * (numentities - e));
217}
218
219// (client) reads a frame from network stream
221{
222 int i, number, removed;
223 entity_frame_t *f, *delta;
224 entity_state_t *e, *old, *oldend;
225 entity_t *ent;
227 if (!cl.entitydatabase)
229 d = cl.entitydatabase;
230 f = &d->framedata;
231 delta = &d->deltaframe;
232
234
235 // read the frame header info
236 f->time = cl.mtime[0];
237 number = MSG_ReadLong(&cl_message);
238 f->framenum = MSG_ReadLong(&cl_message);
239 CL_NewFrameReceived(f->framenum);
240 f->eye[0] = MSG_ReadFloat(&cl_message);
241 f->eye[1] = MSG_ReadFloat(&cl_message);
242 f->eye[2] = MSG_ReadFloat(&cl_message);
243 EntityFrame_AckFrame(d, number);
244 EntityFrame_FetchFrame(d, number, delta);
245 old = delta->entitydata;
246 oldend = old + delta->numentities;
247 // read entities until we hit the magic 0xFFFF end tag
248 while ((number = (unsigned short) MSG_ReadShort(&cl_message)) != 0xFFFF && !cl_message.badread)
249 {
251 Host_Error("EntityFrame_Read: read error");
252 removed = number & 0x8000;
253 number &= 0x7FFF;
254 if (number >= MAX_EDICTS)
255 Host_Error("EntityFrame_Read: number (%i) >= MAX_EDICTS (%i)", number, MAX_EDICTS);
256
257 // seek to entity, while copying any skipped entities (assume unchanged)
258 while (old < oldend && old->number < number)
259 {
260 if (f->numentities >= MAX_ENTITY_DATABASE)
261 Host_Error("EntityFrame_Read: entity list too big");
262 f->entitydata[f->numentities] = *old++;
263 f->entitydata[f->numentities++].time = cl.mtime[0];
264 }
265 if (removed)
266 {
267 if (old < oldend && old->number == number)
268 old++;
269 else
270 Con_Printf("EntityFrame_Read: REMOVE on unused entity %i\n", number);
271 }
272 else
273 {
274 if (f->numentities >= MAX_ENTITY_DATABASE)
275 Host_Error("EntityFrame_Read: entity list too big");
276
277 // reserve this slot
278 e = f->entitydata + f->numentities++;
279
280 if (old < oldend && old->number == number)
281 {
282 // delta from old entity
283 *e = *old++;
284 }
285 else
286 {
287 // delta from defaults
288 *e = defaultstate;
289 }
290
291 if (cl.num_entities <= number)
292 {
293 cl.num_entities = number + 1;
294 if (number >= cl.max_entities)
295 CL_ExpandEntities(number);
296 }
297 cl.entities_active[number] = true;
299 e->time = cl.mtime[0];
300 e->number = number;
302 }
303 }
304 while (old < oldend)
305 {
306 if (f->numentities >= MAX_ENTITY_DATABASE)
307 Host_Error("EntityFrame_Read: entity list too big");
308 f->entitydata[f->numentities] = *old++;
309 f->entitydata[f->numentities++].time = cl.mtime[0];
310 }
311 EntityFrame_AddFrame_Client(d, f->eye, f->framenum, f->numentities, f->entitydata);
312
313 memset(cl.entities_active, 0, cl.num_entities * sizeof(unsigned char));
314 number = 1;
315 for (i = 0;i < f->numentities;i++)
316 {
317 for (;number < f->entitydata[i].number && number < cl.num_entities;number++)
318 {
319 if (cl.entities_active[number])
320 {
321 cl.entities_active[number] = false;
323 }
324 }
325 if (number >= cl.num_entities)
326 break;
327 // update the entity
328 ent = &cl.entities[number];
329 ent->state_previous = ent->state_current;
330 ent->state_current = f->entitydata[i];
332 // the entity lives again...
333 cl.entities_active[number] = true;
334 number++;
335 }
336 for (;number < cl.num_entities;number++)
337 {
338 if (cl.entities_active[number])
339 {
340 cl.entities_active[number] = false;
342 }
343 }
344}
345
346
347// (client) returns the frame number of the most recent frame recieved
349{
350 if (d->numframes)
351 return d->frames[d->numframes - 1].framenum;
352 else
353 return -1;
354}
void EntityFrame_AddFrame_Client(entityframe_database_t *d, vec3_t eye, int framenum, int numentities, const entity_state_t *entitydata)
Definition cl_ents.c:168
void EntityState_ReadFields(entity_state_t *e, unsigned int bits)
Definition cl_ents.c:21
int EntityState_ReadExtendBits(void)
Definition cl_ents.c:4
int EntityFrame_MostRecentlyRecievedFrameNum(entityframe_database_t *d)
Definition cl_ents.c:348
void EntityFrame_CL_ReadFrame(void)
Definition cl_ents.c:220
void CL_NewFrameReceived(int num)
Definition cl_input.c:1737
client_state_t cl
Definition cl_main.c:117
client_static_t cls
Definition cl_main.c:116
void CL_ExpandEntities(int num)
Definition cl_main.c:300
void CL_MoveLerpEntityStates(entity_t *ent)
Definition cl_parse.c:1991
cvar_t developer_networkentities
Definition cl_parse.c:173
void EntityFrame_Clear(entity_frame_t *f, vec3_t eye, int framenum)
Definition com_ents.c:23
void EntityFrame_FetchFrame(entityframe_database_t *d, int framenum, entity_frame_t *f)
Definition com_ents.c:50
entityframe_database_t * EntityFrame_AllocDatabase(mempool_t *mempool)
Definition com_ents.c:5
void EntityFrame_AckFrame(entityframe_database_t *d, int frame)
Definition com_ents.c:35
void EntityFrame_ClearDatabase(entityframe_database_t *d)
Definition com_ents.c:17
float MSG_ReadCoord32f(sizebuf_t *sb)
Definition com_msg.c:384
float MSG_ReadAngle8i(sizebuf_t *sb)
Definition com_msg.c:409
float MSG_ReadAngle16i(sizebuf_t *sb)
Definition com_msg.c:414
float MSG_ReadCoord16i(sizebuf_t *sb)
Definition com_msg.c:379
#define MSG_ReadShort
Definition common.h:191
#define MSG_ReadFloat
Definition common.h:193
@ PROTOCOL_DARKPLACES2
various changes
Definition common.h:140
@ PROTOCOL_DARKPLACES5
uses EntityFrame5 entity snapshot encoder/decoder which is based on a Tribes networking article at ht...
Definition common.h:137
@ PROTOCOL_DARKPLACES6
various changes
Definition common.h:136
#define MSG_ReadLong
Definition common.h:192
#define MSG_ReadByte(sb)
Definition common.h:188
void Con_Print(const char *msg)
Prints to all appropriate console targets, and adds timestamps.
Definition console.c:1504
void Con_Printf(const char *fmt,...)
Prints to all appropriate console targets.
Definition console.c:1514
#define n(x, y)
void Host_Error(const char *error,...)
Definition host.c:85
#define VectorCopy(in, out)
Definition mathlib.h:101
sizebuf_t cl_message
Definition netconn.c:71
entity_state_t defaultstate
Definition protocol.c:4
#define E_TAGATTACHMENT
Definition protocol.h:645
#define E_ANGLE2
Definition protocol.h:617
#define MAX_ENTITY_DATABASE
Definition protocol.h:554
#define E_ORIGIN2
Definition protocol.h:614
#define E_ORIGIN3
Definition protocol.h:615
#define E_LIGHTSTYLE
Definition protocol.h:646
#define E_SCALE
Definition protocol.h:626
#define MAX_ENTITY_HISTORY
Definition protocol.h:553
#define E_EFFECTS1
Definition protocol.h:624
#define E_FLAGS
Definition protocol.h:629
#define E_COLORMAP
Definition protocol.h:627
#define E_MODEL2
Definition protocol.h:634
#define E_SKIN
Definition protocol.h:628
#define E_FRAME2
Definition protocol.h:633
#define E_LIGHT
Definition protocol.h:638
#define E_GLOWSIZE
Definition protocol.h:636
#define E_EFFECTS2
Definition protocol.h:635
#define E_GLOWCOLOR
Definition protocol.h:637
#define E_MODEL1
Definition protocol.h:619
#define E_LIGHTPFLAGS
Definition protocol.h:639
#define E_ANGLE1
Definition protocol.h:616
#define RENDER_LOWPRECISION
Definition protocol.h:360
#define E_ORIGIN1
Definition protocol.h:613
@ ACTIVE_NETWORK
Definition protocol.h:433
@ ACTIVE_NOT
Definition protocol.h:432
#define E_FRAME1
Definition protocol.h:623
#define E_ALPHA
Definition protocol.h:625
#define E_ANGLE3
Definition protocol.h:618
int i
#define MAX_EDICTS
max number of objects in game world at once (32768 protocol limit)
Definition qdefs.h:105
#define NULL
Definition qtypes.h:12
vec_t vec3_t[3]
Definition qtypes.h:71
float f
unsigned char * entities_active
Definition client.h:993
entity_t * entities
Definition client.h:991
int max_entities
Definition client.h:980
double mtime[2]
Definition client.h:861
entityframe_database_t * entitydatabase
Definition client.h:963
mempool_t * levelmempool
Definition client.h:571
protocolversion_t protocol
Definition client.h:617
int integer
Definition cvar.h:73
entity_state_t entitydata[MAX_ENTITY_DATABASE]
Definition protocol.h:565
unsigned char flags
Definition protocol.h:468
unsigned char skin
Definition protocol.h:463
unsigned char glowcolor
Definition protocol.h:467
unsigned char glowsize
Definition protocol.h:466
float origin[3]
Definition protocol.h:444
unsigned short tagentity
Definition protocol.h:451
unsigned char lightstyle
Definition protocol.h:460
unsigned char colormap
Definition protocol.h:462
unsigned short modelindex
Definition protocol.h:449
unsigned char alpha
Definition protocol.h:464
unsigned short light[4]
Definition protocol.h:458
unsigned char tagindex
Definition protocol.h:470
float angles[3]
Definition protocol.h:445
unsigned char active
Definition protocol.h:459
unsigned short frame
Definition protocol.h:450
unsigned short number
Definition protocol.h:448
unsigned char scale
Definition protocol.h:465
unsigned char lightpflags
Definition protocol.h:461
entity_state_t state_current
Definition client.h:471
entity_state_t state_previous
Definition client.h:469
entity_frame_t framedata
Definition protocol.h:602
entity_frameinfo_t frames[MAX_ENTITY_HISTORY]
Definition protocol.h:596
entity_state_t entitydata[MAX_ENTITY_DATABASE]
Definition protocol.h:598
entity_frame_t deltaframe
Definition protocol.h:601
qbool badread
Definition common.h:56