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)
56 VectorCopy(eye, f->eye);
60 // (server) allocates an entity slot in frame, returns NULL if full
61 entity_state_t *EntityFrame_NewEntity(entity_frame_t *f, int number)
64 if (f->numentities >= MAX_ENTITY_DATABASE)
66 e = &f->entitydata[f->numentities++];
72 // (server and client) reads a frame from the database
73 void EntityFrame_FetchFrame(entity_database_t *d, int framenum, entity_frame_t *f)
76 EntityFrame_Clear(f, NULL);
77 for (i = 0;i < d->numframes && d->frames[i].framenum < framenum;i++);
78 if (i < d->numframes && framenum == d->frames[i].framenum)
80 f->framenum = framenum;
81 f->numentities = d->frames[i].endentity - d->frames[i].firstentity;
82 n = MAX_ENTITY_DATABASE - (d->frames[i].firstentity % MAX_ENTITY_DATABASE);
83 if (n > f->numentities)
85 memcpy(f->entitydata, d->entitydata + d->frames[i].firstentity % MAX_ENTITY_DATABASE, sizeof(*f->entitydata) * n);
86 if (f->numentities > n)
87 memcpy(f->entitydata + n, d->entitydata, sizeof(*f->entitydata) * (f->numentities - n));
88 VectorCopy(d->eye, f->eye);
94 // (server and client) adds a entity_frame to the database, for future reference
95 void EntityFrame_AddFrame(entity_database_t *d, entity_frame_t *f)
98 entity_frameinfo_t *info;
100 VectorCopy(f->eye, d->eye);
102 // figure out how many entity slots are used already
105 n = d->frames[d->numframes - 1].endentity - d->frames[0].firstentity;
106 if (n + f->numentities > MAX_ENTITY_DATABASE || d->numframes >= MAX_ENTITY_HISTORY)
108 // ran out of room, dump database
109 EntityFrame_ClearDatabase(d);
113 info = &d->frames[d->numframes];
114 info->framenum = f->framenum;
116 // make sure we check the newly added frame as well, but we haven't incremented numframes yet
117 for (n = 0;n <= d->numframes;n++)
119 if (e >= d->frames[n].framenum)
121 if (e == f->framenum)
122 Con_Printf("EntityFrame_AddFrame: tried to add out of sequence frame to database\n");
124 Con_Printf("EntityFrame_AddFrame: out of sequence frames in database\n");
127 e = d->frames[n].framenum;
129 // if database still has frames after that...
131 info->firstentity = d->frames[d->numframes - 1].endentity;
133 info->firstentity = 0;
134 info->endentity = info->firstentity + f->numentities;
137 n = info->firstentity % MAX_ENTITY_DATABASE;
138 e = MAX_ENTITY_DATABASE - n;
139 if (e > f->numentities)
141 memcpy(d->entitydata + n, f->entitydata, sizeof(entity_state_t) * e);
142 if (f->numentities > e)
143 memcpy(d->entitydata, f->entitydata + e, sizeof(entity_state_t) * (f->numentities - e));
146 // (server) writes a frame to network stream
147 static entity_frame_t deltaframe; // FIXME?
148 void EntityFrame_Write(entity_database_t *d, entity_frame_t *f, sizebuf_t *msg)
150 int i, onum, bits, number;
151 entity_frame_t *o = &deltaframe;
152 entity_state_t *ent, *delta, baseline;
154 EntityFrame_AddFrame(d, f);
156 ClearStateToDefault(&baseline);
157 EntityFrame_FetchFrame(d, d->ackframe > 0 ? d->ackframe : -1, o);
158 MSG_WriteByte (msg, svc_entities);
159 MSG_WriteLong (msg, o->framenum);
160 MSG_WriteLong (msg, f->framenum);
161 MSG_WriteFloat (msg, f->eye[0]);
162 MSG_WriteFloat (msg, f->eye[1]);
163 MSG_WriteFloat (msg, f->eye[2]);
166 for (i = 0;i < f->numentities;i++)
168 ent = f->entitydata + i;
169 number = ent->number;
170 for (;onum < o->numentities && o->entitydata[onum].number < number;onum++)
172 // write remove message
173 MSG_WriteShort(msg, o->entitydata[onum].number | 0x8000);
175 if (onum < o->numentities && (o->entitydata[onum].number == number))
177 // delta from previous frame
178 delta = o->entitydata + onum;
179 // advance to next entity in delta frame
184 // delta from baseline
188 if (ent->flags & RENDER_LOWPRECISION)
190 if ((int) ent->origin[0] != (int) delta->origin[0])
192 if ((int) ent->origin[1] != (int) delta->origin[1])
194 if ((int) ent->origin[2] != (int) delta->origin[2])
199 if (fabs(ent->origin[0] - delta->origin[0]) > 0.01f)
201 if (fabs(ent->origin[1] - delta->origin[1]) > 0.01f)
203 if (fabs(ent->origin[2] - delta->origin[2]) > 0.01f)
206 if ((qbyte) (ent->angles[0] * (256.0f / 360.0f)) != (qbyte) (delta->angles[0] * (256.0f / 360.0f)))
208 if ((qbyte) (ent->angles[1] * (256.0f / 360.0f)) != (qbyte) (delta->angles[1] * (256.0f / 360.0f)))
210 if ((qbyte) (ent->angles[2] * (256.0f / 360.0f)) != (qbyte) (delta->angles[2] * (256.0f / 360.0f)))
212 if ((ent->modelindex ^ delta->modelindex) & 0x00FF)
214 if ((ent->modelindex ^ delta->modelindex) & 0xFF00)
216 if ((ent->frame ^ delta->frame) & 0x00FF)
218 if ((ent->frame ^ delta->frame) & 0xFF00)
220 if ((ent->effects ^ delta->effects) & 0x00FF)
222 if ((ent->effects ^ delta->effects) & 0xFF00)
224 if (ent->colormap != delta->colormap)
226 if (ent->skin != delta->skin)
228 if (ent->alpha != delta->alpha)
230 if (ent->scale != delta->scale)
232 if (ent->glowsize != delta->glowsize)
234 if (ent->glowcolor != delta->glowcolor)
236 if (ent->flags != delta->flags)
239 if (bits) // don't send anything if it hasn't changed
241 if (bits & 0xFF000000)
243 if (bits & 0x00FF0000)
245 if (bits & 0x0000FF00)
248 MSG_WriteShort(msg, number);
249 MSG_WriteByte(msg, bits & 0xFF);
250 if (bits & E_EXTEND1)
252 MSG_WriteByte(msg, (bits >> 8) & 0xFF);
253 if (bits & E_EXTEND2)
255 MSG_WriteByte(msg, (bits >> 16) & 0xFF);
256 if (bits & E_EXTEND3)
257 MSG_WriteByte(msg, (bits >> 24) & 0xFF);
260 // LordHavoc: have to write flags first, as they can modify protocol
262 MSG_WriteByte(msg, ent->flags);
263 if (ent->flags & RENDER_LOWPRECISION)
265 if (bits & E_ORIGIN1)
266 MSG_WriteShort(msg, ent->origin[0]);
267 if (bits & E_ORIGIN2)
268 MSG_WriteShort(msg, ent->origin[1]);
269 if (bits & E_ORIGIN3)
270 MSG_WriteShort(msg, ent->origin[2]);
274 if (bits & E_ORIGIN1)
275 MSG_WriteFloat(msg, ent->origin[0]);
276 if (bits & E_ORIGIN2)
277 MSG_WriteFloat(msg, ent->origin[1]);
278 if (bits & E_ORIGIN3)
279 MSG_WriteFloat(msg, ent->origin[2]);
282 MSG_WriteAngle(msg, ent->angles[0]);
284 MSG_WriteAngle(msg, ent->angles[1]);
286 MSG_WriteAngle(msg, ent->angles[2]);
288 MSG_WriteByte(msg, ent->modelindex & 0xFF);
290 MSG_WriteByte(msg, (ent->modelindex >> 8) & 0xFF);
292 MSG_WriteByte(msg, ent->frame & 0xFF);
294 MSG_WriteByte(msg, (ent->frame >> 8) & 0xFF);
295 if (bits & E_EFFECTS1)
296 MSG_WriteByte(msg, ent->effects & 0xFF);
297 if (bits & E_EFFECTS2)
298 MSG_WriteByte(msg, (ent->effects >> 8) & 0xFF);
299 if (bits & E_COLORMAP)
300 MSG_WriteByte(msg, ent->colormap);
302 MSG_WriteByte(msg, ent->skin);
304 MSG_WriteByte(msg, ent->alpha);
306 MSG_WriteByte(msg, ent->scale);
307 if (bits & E_GLOWSIZE)
308 MSG_WriteByte(msg, ent->glowsize);
309 if (bits & E_GLOWCOLOR)
310 MSG_WriteByte(msg, ent->glowcolor);
313 for (;onum < o->numentities;onum++)
315 // write remove message
316 MSG_WriteShort(msg, o->entitydata[onum].number | 0x8000);
318 MSG_WriteShort(msg, 0xFFFF);
321 // (client) reads a frame from network stream
322 static entity_frame_t framedata; // FIXME?
323 void EntityFrame_Read(entity_database_t *d)
325 int number, removed, bits;
326 entity_frame_t *f = &framedata, *delta = &deltaframe;
327 entity_state_t *e, baseline, *old, *oldend;
329 ClearStateToDefault(&baseline);
331 EntityFrame_Clear(f, NULL);
333 // read the frame header info
334 f->time = cl.mtime[0];
335 number = MSG_ReadLong();
336 f->framenum = MSG_ReadLong();
337 f->eye[0] = MSG_ReadFloat();
338 f->eye[1] = MSG_ReadFloat();
339 f->eye[2] = MSG_ReadFloat();
340 EntityFrame_AckFrame(d, number);
341 EntityFrame_FetchFrame(d, number, delta);
342 old = delta->entitydata;
343 oldend = old + delta->numentities;
344 // read entities until we hit the magic 0xFFFF end tag
345 while ((number = (unsigned short) MSG_ReadShort()) != 0xFFFF)
348 Host_Error("EntityFrame_Read: read error\n");
349 removed = number & 0x8000;
351 if (number >= MAX_EDICTS)
352 Host_Error("EntityFrame_Read: number (%i) >= MAX_EDICTS (%i)\n", number, MAX_EDICTS);
354 // seek to entity, while copying any skipped entities (assume unchanged)
355 while (old < oldend && old->number < number)
357 if (f->numentities >= MAX_ENTITY_DATABASE)
358 Host_Error("EntityFrame_Read: entity list too big\n");
359 memcpy(f->entitydata + f->numentities, old, sizeof(entity_state_t));
360 f->entitydata[f->numentities].time = cl.mtime[0];
366 if (old < oldend && old->number == number)
369 Con_Printf("EntityFrame_Read: REMOVE on unused entity %i\n", number);
373 if (f->numentities >= MAX_ENTITY_DATABASE)
374 Host_Error("EntityFrame_Read: entity list too big\n");
377 e = f->entitydata + f->numentities++;
379 if (old < oldend && old->number == number)
381 // delta from old entity
382 memcpy(e, old++, sizeof(*e));
386 // delta from baseline
387 memcpy(e, &baseline, sizeof(*e));
391 e->time = cl.mtime[0];
394 bits = MSG_ReadByte();
395 if (bits & E_EXTEND1)
397 bits |= MSG_ReadByte() << 8;
398 if (bits & E_EXTEND2)
400 bits |= MSG_ReadByte() << 16;
401 if (bits & E_EXTEND3)
402 bits |= MSG_ReadByte() << 24;
406 if (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();
418 e->flags = MSG_ReadByte();
419 if (e->flags & RENDER_LOWPRECISION || dpprotocol == DPPROTOCOL_VERSION2)
421 if (bits & E_ORIGIN1)
422 e->origin[0] = (signed short) MSG_ReadShort();
423 if (bits & E_ORIGIN2)
424 e->origin[1] = (signed short) MSG_ReadShort();
425 if (bits & E_ORIGIN3)
426 e->origin[2] = (signed short) MSG_ReadShort();
430 if (bits & E_ORIGIN1)
431 e->origin[0] = MSG_ReadFloat();
432 if (bits & E_ORIGIN2)
433 e->origin[1] = MSG_ReadFloat();
434 if (bits & E_ORIGIN3)
435 e->origin[2] = MSG_ReadFloat();
439 e->angles[0] = MSG_ReadAngle();
441 e->angles[1] = MSG_ReadAngle();
443 e->angles[2] = MSG_ReadAngle();
445 e->modelindex = (e->modelindex & 0xFF00) | (unsigned int) MSG_ReadByte();
447 e->modelindex = (e->modelindex & 0x00FF) | ((unsigned int) MSG_ReadByte() << 8);
449 e->frame = (e->frame & 0xFF00) | (unsigned int) MSG_ReadByte();
451 e->frame = (e->frame & 0x00FF) | ((unsigned int) MSG_ReadByte() << 8);
452 if (bits & E_EFFECTS1)
453 e->effects = (e->effects & 0xFF00) | (unsigned int) MSG_ReadByte();
454 if (bits & E_EFFECTS2)
455 e->effects = (e->effects & 0x00FF) | ((unsigned int) MSG_ReadByte() << 8);
456 if (bits & E_COLORMAP)
457 e->colormap = MSG_ReadByte();
459 e->skin = MSG_ReadByte();
461 e->alpha = MSG_ReadByte();
463 e->scale = MSG_ReadByte();
464 if (bits & E_GLOWSIZE)
465 e->glowsize = MSG_ReadByte();
466 if (bits & E_GLOWCOLOR)
467 e->glowcolor = MSG_ReadByte();
468 if (dpprotocol == DPPROTOCOL_VERSION2)
470 e->flags = MSG_ReadByte();
475 if (f->numentities >= MAX_ENTITY_DATABASE)
476 Host_Error("EntityFrame_Read: entity list too big\n");
477 memcpy(f->entitydata + f->numentities, old, sizeof(entity_state_t));
478 f->entitydata[f->numentities].time = cl.mtime[0];
482 EntityFrame_AddFrame(d, f);
486 // (client) returns the frame number of the most recent frame recieved
487 int EntityFrame_MostRecentlyRecievedFrameNum(entity_database_t *d)
490 return d->frames[d->numframes - 1].framenum;