4 void ClearStateToDefault(entity_state_t *s)
6 memset(s, 0, sizeof(*s));
12 // (server) clears the database to contain no frames (thus delta compression compresses against nothing)
13 void EntityFrame_ClearDatabase(entity_database_t *d)
15 memset(d, 0, sizeof(*d));
18 // (server and client) removes frames older than 'frame' from database
19 void EntityFrame_AckFrame(entity_database_t *d, int frame)
22 if (d->ackframe < frame)
24 for (i = 0;i < d->numframes && d->frames[i].framenum < frame;i++);
25 // ignore outdated frame acks (out of order packets)
29 // if some queue is left, slide it down to beginning of array
31 memmove(&d->frames[0], &d->frames[i], sizeof(d->frames[0]) * d->numframes);
34 // (server) clears frame, to prepare for adding entities
35 void EntityFrame_Clear(entity_frame_t *f, vec3_t eye, int framenum)
38 f->framenum = framenum;
46 VectorCopy(eye, f->eye);
50 // (server) adds an entity to frame
51 void EntityFrame_AddEntity(entity_frame_t *f, entity_state_t *s)
53 if (f->numentities < MAX_ENTITY_DATABASE)
55 f->entitydata[f->numentities] = *s;
56 f->entitydata[f->numentities++].active = true;
60 // (server and client) reads a frame from the database
61 void EntityFrame_FetchFrame(entity_database_t *d, int framenum, entity_frame_t *f)
64 EntityFrame_Clear(f, NULL, -1);
65 for (i = 0;i < d->numframes && d->frames[i].framenum < framenum;i++);
66 if (i < d->numframes && framenum == d->frames[i].framenum)
68 f->framenum = framenum;
69 f->numentities = d->frames[i].endentity - d->frames[i].firstentity;
70 n = MAX_ENTITY_DATABASE - (d->frames[i].firstentity % MAX_ENTITY_DATABASE);
71 if (n > f->numentities)
73 memcpy(f->entitydata, d->entitydata + d->frames[i].firstentity % MAX_ENTITY_DATABASE, sizeof(*f->entitydata) * n);
74 if (f->numentities > n)
75 memcpy(f->entitydata + n, d->entitydata, sizeof(*f->entitydata) * (f->numentities - n));
76 VectorCopy(d->eye, f->eye);
80 // (server and client) adds a entity_frame to the database, for future reference
81 void EntityFrame_AddFrame(entity_database_t *d, entity_frame_t *f)
84 entity_frameinfo_t *info;
86 VectorCopy(f->eye, d->eye);
88 // figure out how many entity slots are used already
91 n = d->frames[d->numframes - 1].endentity - d->frames[0].firstentity;
92 if (n + f->numentities > MAX_ENTITY_DATABASE || d->numframes >= MAX_ENTITY_HISTORY)
94 // ran out of room, dump database
95 EntityFrame_ClearDatabase(d);
99 info = &d->frames[d->numframes];
100 info->framenum = f->framenum;
102 // make sure we check the newly added frame as well, but we haven't incremented numframes yet
103 for (n = 0;n <= d->numframes;n++)
105 if (e >= d->frames[n].framenum)
107 if (e == f->framenum)
108 Con_Printf("EntityFrame_AddFrame: tried to add out of sequence frame to database\n");
110 Con_Printf("EntityFrame_AddFrame: out of sequence frames in database\n");
113 e = d->frames[n].framenum;
115 // if database still has frames after that...
117 info->firstentity = d->frames[d->numframes - 1].endentity;
119 info->firstentity = 0;
120 info->endentity = info->firstentity + f->numentities;
123 n = info->firstentity % MAX_ENTITY_DATABASE;
124 e = MAX_ENTITY_DATABASE - n;
125 if (e > f->numentities)
127 memcpy(d->entitydata + n, f->entitydata, sizeof(entity_state_t) * e);
128 if (f->numentities > e)
129 memcpy(d->entitydata, f->entitydata + e, sizeof(entity_state_t) * (f->numentities - e));
132 // (server) writes a frame to network stream
133 static entity_frame_t deltaframe; // FIXME?
134 void EntityFrame_Write(entity_database_t *d, entity_frame_t *f, sizebuf_t *msg)
136 int i, onum, bits, number;
137 float org[3], deltaorg[3];
138 entity_frame_t *o = &deltaframe;
139 entity_state_t *ent, *delta, baseline;
141 EntityFrame_AddFrame(d, f);
143 ClearStateToDefault(&baseline);
144 EntityFrame_FetchFrame(d, d->ackframe > 0 ? d->ackframe : -1, o);
145 MSG_WriteByte (msg, svc_entities);
146 MSG_WriteLong (msg, o->framenum);
147 MSG_WriteLong (msg, f->framenum);
148 MSG_WriteFloat (msg, f->eye[0]);
149 MSG_WriteFloat (msg, f->eye[1]);
150 MSG_WriteFloat (msg, f->eye[2]);
153 for (i = 0;i < f->numentities;i++)
155 ent = f->entitydata + i;
156 number = ent->number;
157 for (;onum < o->numentities && o->entitydata[onum].number < number;onum++)
159 // write remove message
160 MSG_WriteShort(msg, o->entitydata[onum].number | 0x8000);
162 if (onum < o->numentities && (o->entitydata[onum].number == number))
164 // delta from previous frame
165 delta = o->entitydata + onum;
166 // advance to next entity in delta frame
171 // delta from baseline
175 VectorCopy(ent->origin, org);
176 VectorCopy(delta->origin, deltaorg);
177 if (ent->flags & RENDER_LOWPRECISION)
180 org[0] = (int) (org[0] + 0.5f);
182 org[0] = (int) (org[0] - 0.5f);
184 org[1] = (int) (org[1] + 0.5f);
186 org[1] = (int) (org[1] - 0.5f);
188 org[2] = (int) (org[2] + 0.5f);
190 org[2] = (int) (org[2] - 0.5f);
192 if (delta->flags & RENDER_LOWPRECISION)
195 deltaorg[0] = (int) (deltaorg[0] + 0.5f);
197 deltaorg[0] = (int) (deltaorg[0] - 0.5f);
199 deltaorg[1] = (int) (deltaorg[1] + 0.5f);
201 deltaorg[1] = (int) (deltaorg[1] - 0.5f);
203 deltaorg[2] = (int) (deltaorg[2] + 0.5f);
205 deltaorg[2] = (int) (deltaorg[2] - 0.5f);
207 if (fabs(org[0] - deltaorg[0]) > 0.01f)
209 if (fabs(org[1] - deltaorg[1]) > 0.01f)
211 if (fabs(org[2] - deltaorg[2]) > 0.01f)
213 if ((qbyte) (ent->angles[0] * (256.0f / 360.0f)) != (qbyte) (delta->angles[0] * (256.0f / 360.0f)))
215 if ((qbyte) (ent->angles[1] * (256.0f / 360.0f)) != (qbyte) (delta->angles[1] * (256.0f / 360.0f)))
217 if ((qbyte) (ent->angles[2] * (256.0f / 360.0f)) != (qbyte) (delta->angles[2] * (256.0f / 360.0f)))
219 if ((ent->modelindex ^ delta->modelindex) & 0x00FF)
221 if ((ent->modelindex ^ delta->modelindex) & 0xFF00)
223 if ((ent->frame ^ delta->frame) & 0x00FF)
225 if ((ent->frame ^ delta->frame) & 0xFF00)
227 if ((ent->effects ^ delta->effects) & 0x00FF)
229 if ((ent->effects ^ delta->effects) & 0xFF00)
231 if (ent->colormap != delta->colormap)
233 if (ent->skin != delta->skin)
235 if (ent->alpha != delta->alpha)
237 if (ent->scale != delta->scale)
239 if (ent->glowsize != delta->glowsize)
241 if (ent->glowcolor != delta->glowcolor)
243 if (ent->flags != delta->flags)
245 if (ent->tagindex != delta->tagindex || ent->tagentity != delta->tagentity)
246 bits |= E_TAGATTACHMENT;
248 if (bits) // don't send anything if it hasn't changed
250 if (bits & 0xFF000000)
252 if (bits & 0x00FF0000)
254 if (bits & 0x0000FF00)
257 MSG_WriteShort(msg, number);
258 MSG_WriteByte(msg, bits & 0xFF);
259 if (bits & E_EXTEND1)
261 MSG_WriteByte(msg, (bits >> 8) & 0xFF);
262 if (bits & E_EXTEND2)
264 MSG_WriteByte(msg, (bits >> 16) & 0xFF);
265 if (bits & E_EXTEND3)
266 MSG_WriteByte(msg, (bits >> 24) & 0xFF);
269 // LordHavoc: have to write flags first, as they can modify protocol
271 MSG_WriteByte(msg, ent->flags);
272 if (ent->flags & RENDER_LOWPRECISION)
274 if (bits & E_ORIGIN1)
275 MSG_WriteShort(msg, org[0]);
276 if (bits & E_ORIGIN2)
277 MSG_WriteShort(msg, org[1]);
278 if (bits & E_ORIGIN3)
279 MSG_WriteShort(msg, org[2]);
283 if (bits & E_ORIGIN1)
284 MSG_WriteFloat(msg, org[0]);
285 if (bits & E_ORIGIN2)
286 MSG_WriteFloat(msg, org[1]);
287 if (bits & E_ORIGIN3)
288 MSG_WriteFloat(msg, org[2]);
291 MSG_WriteAngle(msg, ent->angles[0]);
293 MSG_WriteAngle(msg, ent->angles[1]);
295 MSG_WriteAngle(msg, ent->angles[2]);
297 MSG_WriteByte(msg, ent->modelindex & 0xFF);
299 MSG_WriteByte(msg, (ent->modelindex >> 8) & 0xFF);
301 MSG_WriteByte(msg, ent->frame & 0xFF);
303 MSG_WriteByte(msg, (ent->frame >> 8) & 0xFF);
304 if (bits & E_EFFECTS1)
305 MSG_WriteByte(msg, ent->effects & 0xFF);
306 if (bits & E_EFFECTS2)
307 MSG_WriteByte(msg, (ent->effects >> 8) & 0xFF);
308 if (bits & E_COLORMAP)
309 MSG_WriteByte(msg, ent->colormap);
311 MSG_WriteByte(msg, ent->skin);
313 MSG_WriteByte(msg, ent->alpha);
315 MSG_WriteByte(msg, ent->scale);
316 if (bits & E_GLOWSIZE)
317 MSG_WriteByte(msg, ent->glowsize);
318 if (bits & E_GLOWCOLOR)
319 MSG_WriteByte(msg, ent->glowcolor);
320 if (bits & E_TAGATTACHMENT)
322 MSG_WriteShort(msg, ent->tagentity);
323 MSG_WriteByte(msg, ent->tagindex);
327 for (;onum < o->numentities;onum++)
329 // write remove message
330 MSG_WriteShort(msg, o->entitydata[onum].number | 0x8000);
332 MSG_WriteShort(msg, 0xFFFF);
335 // (client) reads a frame from network stream
336 static entity_frame_t framedata; // FIXME?
337 void EntityFrame_Read(entity_database_t *d)
339 int number, removed, bits;
340 entity_frame_t *f = &framedata, *delta = &deltaframe;
341 entity_state_t *e, baseline, *old, *oldend;
343 ClearStateToDefault(&baseline);
345 EntityFrame_Clear(f, NULL, -1);
347 // read the frame header info
348 f->time = cl.mtime[0];
349 number = MSG_ReadLong();
350 f->framenum = MSG_ReadLong();
351 f->eye[0] = MSG_ReadFloat();
352 f->eye[1] = MSG_ReadFloat();
353 f->eye[2] = MSG_ReadFloat();
354 EntityFrame_AckFrame(d, number);
355 EntityFrame_FetchFrame(d, number, delta);
356 old = delta->entitydata;
357 oldend = old + delta->numentities;
358 // read entities until we hit the magic 0xFFFF end tag
359 while ((number = (unsigned short) MSG_ReadShort()) != 0xFFFF)
362 Host_Error("EntityFrame_Read: read error\n");
363 removed = number & 0x8000;
365 if (number >= MAX_EDICTS)
366 Host_Error("EntityFrame_Read: number (%i) >= MAX_EDICTS (%i)\n", number, MAX_EDICTS);
368 // seek to entity, while copying any skipped entities (assume unchanged)
369 while (old < oldend && old->number < number)
371 if (f->numentities >= MAX_ENTITY_DATABASE)
372 Host_Error("EntityFrame_Read: entity list too big\n");
373 memcpy(f->entitydata + f->numentities, old, sizeof(entity_state_t));
374 f->entitydata[f->numentities].time = cl.mtime[0];
380 if (old < oldend && old->number == number)
383 Con_Printf("EntityFrame_Read: REMOVE on unused entity %i\n", number);
387 if (f->numentities >= MAX_ENTITY_DATABASE)
388 Host_Error("EntityFrame_Read: entity list too big\n");
391 e = f->entitydata + f->numentities++;
393 if (old < oldend && old->number == number)
395 // delta from old entity
396 memcpy(e, old++, sizeof(*e));
400 // delta from baseline
401 memcpy(e, &baseline, sizeof(*e));
405 e->time = cl.mtime[0];
408 bits = MSG_ReadByte();
409 if (bits & E_EXTEND1)
411 bits |= MSG_ReadByte() << 8;
412 if (bits & E_EXTEND2)
414 bits |= MSG_ReadByte() << 16;
415 if (bits & E_EXTEND3)
416 bits |= MSG_ReadByte() << 24;
420 if (dpprotocol == DPPROTOCOL_VERSION2)
422 if (bits & E_ORIGIN1)
423 e->origin[0] = (signed short) MSG_ReadShort();
424 if (bits & E_ORIGIN2)
425 e->origin[1] = (signed short) MSG_ReadShort();
426 if (bits & E_ORIGIN3)
427 e->origin[2] = (signed short) MSG_ReadShort();
432 e->flags = MSG_ReadByte();
433 if (e->flags & RENDER_LOWPRECISION || dpprotocol == DPPROTOCOL_VERSION2)
435 if (bits & E_ORIGIN1)
436 e->origin[0] = (signed short) MSG_ReadShort();
437 if (bits & E_ORIGIN2)
438 e->origin[1] = (signed short) MSG_ReadShort();
439 if (bits & E_ORIGIN3)
440 e->origin[2] = (signed short) MSG_ReadShort();
444 if (bits & E_ORIGIN1)
445 e->origin[0] = MSG_ReadFloat();
446 if (bits & E_ORIGIN2)
447 e->origin[1] = MSG_ReadFloat();
448 if (bits & E_ORIGIN3)
449 e->origin[2] = MSG_ReadFloat();
453 e->angles[0] = MSG_ReadAngle();
455 e->angles[1] = MSG_ReadAngle();
457 e->angles[2] = MSG_ReadAngle();
459 e->modelindex = (e->modelindex & 0xFF00) | (unsigned int) MSG_ReadByte();
461 e->modelindex = (e->modelindex & 0x00FF) | ((unsigned int) MSG_ReadByte() << 8);
463 e->frame = (e->frame & 0xFF00) | (unsigned int) MSG_ReadByte();
465 e->frame = (e->frame & 0x00FF) | ((unsigned int) MSG_ReadByte() << 8);
466 if (bits & E_EFFECTS1)
467 e->effects = (e->effects & 0xFF00) | (unsigned int) MSG_ReadByte();
468 if (bits & E_EFFECTS2)
469 e->effects = (e->effects & 0x00FF) | ((unsigned int) MSG_ReadByte() << 8);
470 if (bits & E_COLORMAP)
471 e->colormap = MSG_ReadByte();
473 e->skin = MSG_ReadByte();
475 e->alpha = MSG_ReadByte();
477 e->scale = MSG_ReadByte();
478 if (bits & E_GLOWSIZE)
479 e->glowsize = MSG_ReadByte();
480 if (bits & E_GLOWCOLOR)
481 e->glowcolor = MSG_ReadByte();
482 if (dpprotocol == DPPROTOCOL_VERSION2)
484 e->flags = MSG_ReadByte();
485 if (bits & E_TAGATTACHMENT)
487 e->tagentity = MSG_ReadShort();
488 e->tagindex = MSG_ReadByte();
494 if (f->numentities >= MAX_ENTITY_DATABASE)
495 Host_Error("EntityFrame_Read: entity list too big\n");
496 memcpy(f->entitydata + f->numentities, old, sizeof(entity_state_t));
497 f->entitydata[f->numentities].time = cl.mtime[0];
501 EntityFrame_AddFrame(d, f);
505 // (client) returns the frame number of the most recent frame recieved
506 int EntityFrame_MostRecentlyRecievedFrameNum(entity_database_t *d)
509 return d->frames[d->numframes - 1].framenum;