4 int EntityState_ReadExtendBits(void)
7 bits = MSG_ReadByte(&cl_message);
10 bits |= MSG_ReadByte(&cl_message) << 8;
11 if (bits & 0x00008000)
13 bits |= MSG_ReadByte(&cl_message) << 16;
14 if (bits & 0x00800000)
15 bits |= MSG_ReadByte(&cl_message) << 24;
21 void EntityState_ReadFields(entity_state_t *e, unsigned int bits)
23 if (cls.protocol == PROTOCOL_DARKPLACES2)
26 e->origin[0] = MSG_ReadCoord16i(&cl_message);
28 e->origin[1] = MSG_ReadCoord16i(&cl_message);
30 e->origin[2] = MSG_ReadCoord16i(&cl_message);
35 e->flags = MSG_ReadByte(&cl_message);
36 if (e->flags & RENDER_LOWPRECISION)
39 e->origin[0] = MSG_ReadCoord16i(&cl_message);
41 e->origin[1] = MSG_ReadCoord16i(&cl_message);
43 e->origin[2] = MSG_ReadCoord16i(&cl_message);
48 e->origin[0] = MSG_ReadCoord32f(&cl_message);
50 e->origin[1] = MSG_ReadCoord32f(&cl_message);
52 e->origin[2] = MSG_ReadCoord32f(&cl_message);
55 if ((cls.protocol == PROTOCOL_DARKPLACES5 || cls.protocol == PROTOCOL_DARKPLACES6) && !(e->flags & RENDER_LOWPRECISION))
58 e->angles[0] = MSG_ReadAngle16i(&cl_message);
60 e->angles[1] = MSG_ReadAngle16i(&cl_message);
62 e->angles[2] = MSG_ReadAngle16i(&cl_message);
67 e->angles[0] = MSG_ReadAngle8i(&cl_message);
69 e->angles[1] = MSG_ReadAngle8i(&cl_message);
71 e->angles[2] = MSG_ReadAngle8i(&cl_message);
74 e->modelindex = (e->modelindex & 0xFF00) | (unsigned int) MSG_ReadByte(&cl_message);
76 e->modelindex = (e->modelindex & 0x00FF) | ((unsigned int) MSG_ReadByte(&cl_message) << 8);
78 e->frame = (e->frame & 0xFF00) | (unsigned int) MSG_ReadByte(&cl_message);
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)
86 e->colormap = MSG_ReadByte(&cl_message);
88 e->skin = MSG_ReadByte(&cl_message);
90 e->alpha = MSG_ReadByte(&cl_message);
92 e->scale = MSG_ReadByte(&cl_message);
93 if (bits & E_GLOWSIZE)
94 e->glowsize = MSG_ReadByte(&cl_message);
95 if (bits & E_GLOWCOLOR)
96 e->glowcolor = MSG_ReadByte(&cl_message);
97 if (cls.protocol == PROTOCOL_DARKPLACES2)
99 e->flags = MSG_ReadByte(&cl_message);
100 if (bits & E_TAGATTACHMENT)
102 e->tagentity = (unsigned short) MSG_ReadShort(&cl_message);
103 e->tagindex = MSG_ReadByte(&cl_message);
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);
112 if (bits & E_LIGHTSTYLE)
113 e->lightstyle = MSG_ReadByte(&cl_message);
114 if (bits & E_LIGHTPFLAGS)
115 e->lightpflags = MSG_ReadByte(&cl_message);
117 if (developer_networkentities.integer >= 2)
119 Con_Printf("ReadFields e%i", e->number);
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]);
128 Con_Printf(" E_ANGLE1 %f", e->angles[0]);
130 Con_Printf(" E_ANGLE2 %f", e->angles[1]);
132 Con_Printf(" E_ANGLE3 %f", e->angles[2]);
133 if (bits & (E_MODEL1 | E_MODEL2))
134 Con_Printf(" E_MODEL %i", e->modelindex);
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);
141 Con_Printf(" E_ALPHA %f", e->alpha / 255.0f);
143 Con_Printf(" E_SCALE %f", e->scale / 16.0f);
144 if (bits & E_COLORMAP)
145 Con_Printf(" E_COLORMAP %i", e->colormap);
147 Con_Printf(" E_SKIN %i", e->skin);
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);
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);
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);
167 // (client) adds a entity_frame to the database, for future reference
168 void EntityFrame_AddFrame_Client(entityframe_database_t *d, vec3_t eye, int framenum, int numentities, const entity_state_t *entitydata)
171 entity_frameinfo_t *info;
173 VectorCopy(eye, d->eye);
175 // figure out how many entity slots are used already
178 n = d->frames[d->numframes - 1].endentity - d->frames[0].firstentity;
179 if (n + numentities > MAX_ENTITY_DATABASE || d->numframes >= MAX_ENTITY_HISTORY)
181 // ran out of room, dump database
182 EntityFrame_ClearDatabase(d);
186 info = &d->frames[d->numframes];
187 info->framenum = framenum;
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++)
192 if (e >= d->frames[n].framenum)
195 Con_Print("EntityFrame_AddFrame: tried to add out of sequence frame to database\n");
197 Con_Print("EntityFrame_AddFrame: out of sequence frames in database\n");
200 e = d->frames[n].framenum;
202 // if database still has frames after that...
204 info->firstentity = d->frames[d->numframes - 1].endentity;
206 info->firstentity = 0;
207 info->endentity = info->firstentity + numentities;
210 n = info->firstentity % MAX_ENTITY_DATABASE;
211 e = MAX_ENTITY_DATABASE - n;
214 memcpy(d->entitydata + n, entitydata, sizeof(entity_state_t) * e);
216 memcpy(d->entitydata, entitydata + e, sizeof(entity_state_t) * (numentities - e));
219 // (client) reads a frame from network stream
220 void EntityFrame_CL_ReadFrame(void)
222 int i, number, removed;
223 entity_frame_t *f, *delta;
224 entity_state_t *e, *old, *oldend;
226 entityframe_database_t *d;
227 if (!cl.entitydatabase)
228 cl.entitydatabase = EntityFrame_AllocDatabase(cls.levelmempool);
229 d = cl.entitydatabase;
231 delta = &d->deltaframe;
233 EntityFrame_Clear(f, NULL, -1);
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)
250 if (cl_message.badread)
251 Host_Error("EntityFrame_Read: read error");
252 removed = number & 0x8000;
254 if (number >= MAX_EDICTS)
255 Host_Error("EntityFrame_Read: number (%i) >= MAX_EDICTS (%i)", number, MAX_EDICTS);
257 // seek to entity, while copying any skipped entities (assume unchanged)
258 while (old < oldend && old->number < number)
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];
267 if (old < oldend && old->number == number)
270 Con_Printf("EntityFrame_Read: REMOVE on unused entity %i\n", number);
274 if (f->numentities >= MAX_ENTITY_DATABASE)
275 Host_Error("EntityFrame_Read: entity list too big");
278 e = f->entitydata + f->numentities++;
280 if (old < oldend && old->number == number)
282 // delta from old entity
287 // delta from defaults
291 if (cl.num_entities <= number)
293 cl.num_entities = number + 1;
294 if (number >= cl.max_entities)
295 CL_ExpandEntities(number);
297 cl.entities_active[number] = true;
298 e->active = ACTIVE_NETWORK;
299 e->time = cl.mtime[0];
301 EntityState_ReadFields(e, EntityState_ReadExtendBits());
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];
311 EntityFrame_AddFrame_Client(d, f->eye, f->framenum, f->numentities, f->entitydata);
313 memset(cl.entities_active, 0, cl.num_entities * sizeof(unsigned char));
315 for (i = 0;i < f->numentities;i++)
317 for (;number < f->entitydata[i].number && number < cl.num_entities;number++)
319 if (cl.entities_active[number])
321 cl.entities_active[number] = false;
322 cl.entities[number].state_current.active = ACTIVE_NOT;
325 if (number >= cl.num_entities)
328 ent = &cl.entities[number];
329 ent->state_previous = ent->state_current;
330 ent->state_current = f->entitydata[i];
331 CL_MoveLerpEntityStates(ent);
332 // the entity lives again...
333 cl.entities_active[number] = true;
336 for (;number < cl.num_entities;number++)
338 if (cl.entities_active[number])
340 cl.entities_active[number] = false;
341 cl.entities[number].state_current.active = ACTIVE_NOT;
347 // (client) returns the frame number of the most recent frame recieved
348 int EntityFrame_MostRecentlyRecievedFrameNum(entityframe_database_t *d)
351 return d->frames[d->numframes - 1].framenum;