4 void ClearStateToDefault(entity_state_t *s)
8 VectorClear(s->origin);
9 VectorClear(s->angles);
22 // (server) clears the database to contain no frames (thus delta compression compresses against nothing)
23 void EntityFrame_ClearDatabase(entity_database_t *d)
25 memset(d, 0, sizeof(*d));
28 // (server and client) removes frames older than 'frame' from database
29 void EntityFrame_AckFrame(entity_database_t *d, int frame)
32 if (d->ackframe < frame)
34 for (i = 0;i < d->numframes && d->frames[i].framenum < frame;i++);
35 // ignore outdated frame acks (out of order packets)
39 // if some queue is left, slide it down to beginning of array
41 memmove(&d->frames[0], &d->frames[i], sizeof(d->frames[0]) * d->numframes);
44 // (server) clears frame, to prepare for adding entities
45 void EntityFrame_Clear(entity_frame_t *f, vec3_t eye)
47 memset(f, 0, sizeof(*f));
48 VectorCopy(eye, f->eye);
51 // (server) allocates an entity slot in frame, returns NULL if full
52 entity_state_t *EntityFrame_NewEntity(entity_frame_t *f, int number)
55 if (f->numentities >= MAX_ENTITY_DATABASE)
57 e = &f->entitydata[f->numentities++];
63 // (server and client) reads a frame from the database
64 void EntityFrame_FetchFrame(entity_database_t *d, int framenum, entity_frame_t *f)
67 memset(f, 0, sizeof(*f));
68 for (i = 0;i < d->numframes && d->frames[i].framenum < framenum;i++);
69 if (i < d->numframes && framenum == d->frames[i].framenum)
71 f->framenum = framenum;
72 f->numentities = d->frames[i].endentity - d->frames[i].firstentity;
73 n = MAX_ENTITY_DATABASE - (d->frames[i].firstentity % MAX_ENTITY_DATABASE);
74 if (n > f->numentities)
76 memcpy(f->entitydata, d->entitydata + d->frames[i].firstentity % MAX_ENTITY_DATABASE, sizeof(*f->entitydata) * n);
77 if (f->numentities > n)
78 memcpy(f->entitydata + n, d->entitydata, sizeof(*f->entitydata) * (f->numentities - n));
79 VectorCopy(d->eye, f->eye);
85 // (server and client) adds a entity_frame to the database, for future reference
86 void EntityFrame_AddFrame(entity_database_t *d, entity_frame_t *f)
89 entity_frameinfo_t *info;
91 VectorCopy(f->eye, d->eye);
93 // figure out how many entity slots are used already
96 n = d->frames[d->numframes - 1].endentity - d->frames[0].firstentity;
97 if (n + f->numentities > MAX_ENTITY_DATABASE || d->numframes >= MAX_ENTITY_HISTORY)
99 // ran out of room, dump database
100 EntityFrame_ClearDatabase(d);
104 info = &d->frames[d->numframes];
105 info->framenum = f->framenum;
107 // make sure we check the newly added frame as well, but we haven't incremented numframes yet
108 for (n = 0;n <= d->numframes;n++)
110 if (e >= d->frames[n].framenum)
112 if (e == f->framenum)
113 Con_Printf("EntityFrame_AddFrame: tried to add out of sequence frame to database\n");
115 Con_Printf("EntityFrame_AddFrame: out of sequence frames in database\n");
118 e = d->frames[n].framenum;
120 // if database still has frames after that...
122 info->firstentity = d->frames[d->numframes - 1].endentity;
124 info->firstentity = 0;
125 info->endentity = info->firstentity + f->numentities;
128 n = info->firstentity % MAX_ENTITY_DATABASE;
129 e = MAX_ENTITY_DATABASE - n;
130 if (e > f->numentities)
132 memcpy(d->entitydata + n, f->entitydata, sizeof(entity_state_t) * e);
133 if (f->numentities > e)
134 memcpy(d->entitydata, f->entitydata + e, sizeof(entity_state_t) * (f->numentities - e));
137 // (server) writes a frame to network stream
138 void EntityFrame_Write(entity_database_t *d, entity_frame_t *f, sizebuf_t *msg)
140 int i, onum, bits, number;
141 entity_frame_t deltaframe, *o = &deltaframe;
142 entity_state_t *ent, *delta, baseline;
144 EntityFrame_AddFrame(d, f);
146 ClearStateToDefault(&baseline);
147 EntityFrame_FetchFrame(d, d->ackframe > 0 ? d->ackframe : -1, o);
148 MSG_WriteByte (msg, svc_entities);
149 MSG_WriteLong (msg, o->framenum);
150 MSG_WriteLong (msg, f->framenum);
151 MSG_WriteFloat (msg, f->eye[0]);
152 MSG_WriteFloat (msg, f->eye[1]);
153 MSG_WriteFloat (msg, f->eye[2]);
156 for (i = 0;i < f->numentities;i++)
158 ent = f->entitydata + i;
159 number = ent->number;
160 for (;onum < o->numentities && o->entitydata[onum].number < number;onum++)
162 // write remove message
163 MSG_WriteShort(msg, o->entitydata[onum].number | 0x8000);
165 if (onum < o->numentities && (o->entitydata[onum].number == number))
167 // delta from previous frame
168 delta = o->entitydata + onum;
169 // advance to next entity in delta frame
174 // delta from baseline
178 if (ent->flags & RENDER_LOWPRECISION)
180 if ((int) ent->origin[0] != (int) delta->origin[0])
182 if ((int) ent->origin[1] != (int) delta->origin[1])
184 if ((int) ent->origin[2] != (int) delta->origin[2])
189 if (fabs(ent->origin[0] - delta->origin[0]) > 0.01f)
191 if (fabs(ent->origin[1] - delta->origin[1]) > 0.01f)
193 if (fabs(ent->origin[2] - delta->origin[2]) > 0.01f)
196 if ((qbyte) (ent->angles[0] * (256.0f / 360.0f)) != (qbyte) (delta->angles[0] * (256.0f / 360.0f)))
198 if ((qbyte) (ent->angles[1] * (256.0f / 360.0f)) != (qbyte) (delta->angles[1] * (256.0f / 360.0f)))
200 if ((qbyte) (ent->angles[2] * (256.0f / 360.0f)) != (qbyte) (delta->angles[2] * (256.0f / 360.0f)))
202 if ((ent->modelindex ^ delta->modelindex) & 0x00FF)
204 if ((ent->modelindex ^ delta->modelindex) & 0xFF00)
206 if ((ent->frame ^ delta->frame) & 0x00FF)
208 if ((ent->frame ^ delta->frame) & 0xFF00)
210 if ((ent->effects ^ delta->effects) & 0x00FF)
212 if ((ent->effects ^ delta->effects) & 0xFF00)
214 if (ent->colormap != delta->colormap)
216 if (ent->skin != delta->skin)
218 if (ent->alpha != delta->alpha)
220 if (ent->scale != delta->scale)
222 if (ent->glowsize != delta->glowsize)
224 if (ent->glowcolor != delta->glowcolor)
226 if (ent->flags != delta->flags)
229 if (bits) // don't send anything if it hasn't changed
231 if (bits & 0xFF000000)
233 if (bits & 0x00FF0000)
235 if (bits & 0x0000FF00)
238 MSG_WriteShort(msg, number);
239 MSG_WriteByte(msg, bits & 0xFF);
240 if (bits & E_EXTEND1)
242 MSG_WriteByte(msg, (bits >> 8) & 0xFF);
243 if (bits & E_EXTEND2)
245 MSG_WriteByte(msg, (bits >> 16) & 0xFF);
246 if (bits & E_EXTEND3)
247 MSG_WriteByte(msg, (bits >> 24) & 0xFF);
250 // LordHavoc: have to write flags first, as they can modify protocol
252 MSG_WriteByte(msg, ent->flags);
253 if (ent->flags & RENDER_LOWPRECISION)
255 if (bits & E_ORIGIN1)
256 MSG_WriteShort(msg, ent->origin[0]);
257 if (bits & E_ORIGIN2)
258 MSG_WriteShort(msg, ent->origin[1]);
259 if (bits & E_ORIGIN3)
260 MSG_WriteShort(msg, ent->origin[2]);
264 if (bits & E_ORIGIN1)
265 MSG_WriteFloat(msg, ent->origin[0]);
266 if (bits & E_ORIGIN2)
267 MSG_WriteFloat(msg, ent->origin[1]);
268 if (bits & E_ORIGIN3)
269 MSG_WriteFloat(msg, ent->origin[2]);
272 MSG_WriteAngle(msg, ent->angles[0]);
274 MSG_WriteAngle(msg, ent->angles[1]);
276 MSG_WriteAngle(msg, ent->angles[2]);
278 MSG_WriteByte(msg, ent->modelindex & 0xFF);
280 MSG_WriteByte(msg, (ent->modelindex >> 8) & 0xFF);
282 MSG_WriteByte(msg, ent->frame & 0xFF);
284 MSG_WriteByte(msg, (ent->frame >> 8) & 0xFF);
285 if (bits & E_EFFECTS1)
286 MSG_WriteByte(msg, ent->effects & 0xFF);
287 if (bits & E_EFFECTS2)
288 MSG_WriteByte(msg, (ent->effects >> 8) & 0xFF);
289 if (bits & E_COLORMAP)
290 MSG_WriteByte(msg, ent->colormap);
292 MSG_WriteByte(msg, ent->skin);
294 MSG_WriteByte(msg, ent->alpha);
296 MSG_WriteByte(msg, ent->scale);
297 if (bits & E_GLOWSIZE)
298 MSG_WriteByte(msg, ent->glowsize);
299 if (bits & E_GLOWCOLOR)
300 MSG_WriteByte(msg, ent->glowcolor);
303 for (;onum < o->numentities;onum++)
305 // write remove message
306 MSG_WriteShort(msg, o->entitydata[onum].number | 0x8000);
308 MSG_WriteShort(msg, 0xFFFF);
311 // (client) reads a frame from network stream
312 void EntityFrame_Read(entity_database_t *d)
314 int number, removed, bits;
315 entity_frame_t framedata, *f = &framedata, deltaframedata, *delta = &deltaframedata;
316 entity_state_t *e, baseline, *old, *oldend;
318 ClearStateToDefault(&baseline);
319 memset(f, 0, sizeof(*f));
320 // read the frame header info
321 f->time = cl.mtime[0];
322 number = MSG_ReadLong();
323 f->framenum = MSG_ReadLong();
324 f->eye[0] = MSG_ReadFloat();
325 f->eye[1] = MSG_ReadFloat();
326 f->eye[2] = MSG_ReadFloat();
327 EntityFrame_AckFrame(d, number);
328 EntityFrame_FetchFrame(d, number, delta);
329 old = delta->entitydata;
330 oldend = old + delta->numentities;
331 // read entities until we hit the magic 0xFFFF end tag
332 while ((number = (unsigned short) MSG_ReadShort()) != 0xFFFF)
335 Host_Error("EntityFrame_Read: read error\n");
336 removed = number & 0x8000;
338 if (number >= MAX_EDICTS)
339 Host_Error("EntityFrame_Read: number (%i) >= MAX_EDICTS (%i)\n", number, MAX_EDICTS);
341 // seek to entity, while copying any skipped entities (assume unchanged)
342 while (old < oldend && old->number < number)
344 if (f->numentities >= MAX_ENTITY_DATABASE)
345 Host_Error("EntityFrame_Read: entity list too big\n");
346 memcpy(f->entitydata + f->numentities, old, sizeof(entity_state_t));
347 f->entitydata[f->numentities].time = cl.mtime[0];
353 if (old < oldend && old->number == number)
356 Con_Printf("EntityFrame_Read: REMOVE on unused entity %i\n", number);
360 if (f->numentities >= MAX_ENTITY_DATABASE)
361 Host_Error("EntityFrame_Read: entity list too big\n");
364 e = f->entitydata + f->numentities++;
366 if (old < oldend && old->number == number)
368 // delta from old entity
369 memcpy(e, old++, sizeof(*e));
373 // delta from baseline
374 memcpy(e, &baseline, sizeof(*e));
378 e->time = cl.mtime[0];
381 bits = MSG_ReadByte();
382 if (bits & E_EXTEND1)
384 bits |= MSG_ReadByte() << 8;
385 if (bits & E_EXTEND2)
387 bits |= MSG_ReadByte() << 16;
388 if (bits & E_EXTEND3)
389 bits |= MSG_ReadByte() << 24;
393 if (dpprotocol == DPPROTOCOL_VERSION2)
395 if (bits & E_ORIGIN1)
396 e->origin[0] = (signed short) MSG_ReadShort();
397 if (bits & E_ORIGIN2)
398 e->origin[1] = (signed short) MSG_ReadShort();
399 if (bits & E_ORIGIN3)
400 e->origin[2] = (signed short) MSG_ReadShort();
405 e->flags = MSG_ReadByte();
406 if (e->flags & RENDER_LOWPRECISION || dpprotocol == DPPROTOCOL_VERSION2)
408 if (bits & E_ORIGIN1)
409 e->origin[0] = (signed short) MSG_ReadShort();
410 if (bits & E_ORIGIN2)
411 e->origin[1] = (signed short) MSG_ReadShort();
412 if (bits & E_ORIGIN3)
413 e->origin[2] = (signed short) MSG_ReadShort();
417 if (bits & E_ORIGIN1)
418 e->origin[0] = MSG_ReadFloat();
419 if (bits & E_ORIGIN2)
420 e->origin[1] = MSG_ReadFloat();
421 if (bits & E_ORIGIN3)
422 e->origin[2] = MSG_ReadFloat();
426 e->angles[0] = MSG_ReadAngle();
428 e->angles[1] = MSG_ReadAngle();
430 e->angles[2] = MSG_ReadAngle();
432 e->modelindex = (e->modelindex & 0xFF00) | (unsigned int) MSG_ReadByte();
434 e->modelindex = (e->modelindex & 0x00FF) | ((unsigned int) MSG_ReadByte() << 8);
436 e->frame = (e->frame & 0xFF00) | (unsigned int) MSG_ReadByte();
438 e->frame = (e->frame & 0x00FF) | ((unsigned int) MSG_ReadByte() << 8);
439 if (bits & E_EFFECTS1)
440 e->effects = (e->effects & 0xFF00) | (unsigned int) MSG_ReadByte();
441 if (bits & E_EFFECTS2)
442 e->effects = (e->effects & 0x00FF) | ((unsigned int) MSG_ReadByte() << 8);
443 if (bits & E_COLORMAP)
444 e->colormap = MSG_ReadByte();
446 e->skin = MSG_ReadByte();
448 e->alpha = MSG_ReadByte();
450 e->scale = MSG_ReadByte();
451 if (bits & E_GLOWSIZE)
452 e->glowsize = MSG_ReadByte();
453 if (bits & E_GLOWCOLOR)
454 e->glowcolor = MSG_ReadByte();
455 if (dpprotocol == DPPROTOCOL_VERSION2)
457 e->flags = MSG_ReadByte();
462 if (f->numentities >= MAX_ENTITY_DATABASE)
463 Host_Error("EntityFrame_Read: entity list too big\n");
464 memcpy(f->entitydata + f->numentities, old, sizeof(entity_state_t));
465 f->entitydata[f->numentities].time = cl.mtime[0];
469 EntityFrame_AddFrame(d, f);
473 // (client) reads (and interpolates) the eye location from the database,
474 // given a current time
475 int EntityFrame_FetchEye(entity_database_t *d, vec3_t eye, double time)
478 if (d->numframes == 0)
480 // Host_Error("EntityFrame_FetchEye: no frames\n");
481 if (d->numframes > 1 && d->frames[d->numframes - 2].time != d->frames[d->numframes - 1].time)
483 frac = (time - d->frames[d->numframes - 2].time) / (d->frames[d->numframes - 1].time - d->frames[d->numframes - 2].time);
484 frac = bound(0, frac, 1);
485 VectorSubtract(d->frames[d->numframes - 2].eye, d->frames[d->numframes - 1].eye, eye);
486 VectorMA(d->frames[d->numframes - 2].eye, frac, eye, eye);
489 VectorCopy(d->frames[0].eye, eye);
493 // (client) fetchs an entity from a frame, index is the index into the frame's entity list, returns false if index is out of bounds
494 int EntityFrame_FetchEntityByIndex(entity_frame_t *f, entity_state_t *e, int index)
496 if (index < 0 || index >= f->numentities)
498 memcpy(e, f->entitydata + index, sizeof(*e));
502 int EntityFrame_FetchEntityByNumber(entity_frame_t *f, entity_state_t *e, int number)
505 for (i = 0;i < f->numentities;i++)
507 if (f->entitydata[i].number == number)
509 memcpy(e, f->entitydata + i, sizeof(*e));
513 ClearStateToDefault(e);
518 // (client) returns the frame number of the most recent frame recieved
519 int EntityFrame_MostRecentlyRecievedFrameNum(entity_database_t *d)
522 return d->frames[d->numframes - 1].framenum;