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 ((int) ent->origin[0] != (int) delta->origin[0])
180 if ((int) ent->origin[1] != (int) delta->origin[1])
182 if ((int) ent->origin[2] != (int) delta->origin[2])
184 if ((byte) (ent->angles[0] * (256.0f / 360.0f)) != (byte) (delta->angles[0] * (256.0f / 360.0f)))
186 if ((byte) (ent->angles[1] * (256.0f / 360.0f)) != (byte) (delta->angles[1] * (256.0f / 360.0f)))
188 if ((byte) (ent->angles[2] * (256.0f / 360.0f)) != (byte) (delta->angles[2] * (256.0f / 360.0f)))
190 if ((ent->modelindex ^ delta->modelindex) & 0x00FF)
192 if ((ent->modelindex ^ delta->modelindex) & 0xFF00)
194 if ((ent->frame ^ delta->frame) & 0x00FF)
196 if ((ent->frame ^ delta->frame) & 0xFF00)
198 if ((ent->effects ^ delta->effects) & 0x00FF)
200 if ((ent->effects ^ delta->effects) & 0xFF00)
202 if (ent->colormap != delta->colormap)
204 if (ent->skin != delta->skin)
206 if (ent->alpha != delta->alpha)
208 if (ent->scale != delta->scale)
210 if (ent->glowsize != delta->glowsize)
212 if (ent->glowcolor != delta->glowcolor)
214 if (ent->flags != delta->flags)
217 if (bits) // don't send anything if it hasn't changed
219 if (bits & 0xFF000000)
221 if (bits & 0x00FF0000)
223 if (bits & 0x0000FF00)
226 MSG_WriteShort(msg, number);
227 MSG_WriteByte(msg, bits & 0xFF);
228 if (bits & E_EXTEND1)
230 MSG_WriteByte(msg, (bits >> 8) & 0xFF);
231 if (bits & E_EXTEND2)
233 MSG_WriteByte(msg, (bits >> 16) & 0xFF);
234 if (bits & E_EXTEND3)
235 MSG_WriteByte(msg, (bits >> 24) & 0xFF);
238 if (bits & E_ORIGIN1)
239 MSG_WriteShort(msg, ent->origin[0]);
240 if (bits & E_ORIGIN2)
241 MSG_WriteShort(msg, ent->origin[1]);
242 if (bits & E_ORIGIN3)
243 MSG_WriteShort(msg, ent->origin[2]);
245 MSG_WriteAngle(msg, ent->angles[0]);
247 MSG_WriteAngle(msg, ent->angles[1]);
249 MSG_WriteAngle(msg, ent->angles[2]);
251 MSG_WriteByte(msg, ent->modelindex & 0x00FF);
253 MSG_WriteByte(msg, ent->modelindex & 0xFF00);
255 MSG_WriteByte(msg, ent->frame & 0x00FF);
257 MSG_WriteByte(msg, ent->frame & 0xFF00);
258 if (bits & E_EFFECTS1)
259 MSG_WriteByte(msg, ent->effects & 0x00FF);
260 if (bits & E_EFFECTS2)
261 MSG_WriteByte(msg, ent->effects & 0xFF00);
262 if (bits & E_COLORMAP)
263 MSG_WriteByte(msg, ent->colormap);
265 MSG_WriteByte(msg, ent->skin);
267 MSG_WriteByte(msg, ent->alpha);
269 MSG_WriteByte(msg, ent->scale);
270 if (bits & E_GLOWSIZE)
271 MSG_WriteByte(msg, ent->glowsize);
272 if (bits & E_GLOWCOLOR)
273 MSG_WriteByte(msg, ent->glowcolor);
275 MSG_WriteByte(msg, ent->flags);
278 for (;onum < o->numentities;onum++)
280 // write remove message
281 MSG_WriteShort(msg, o->entitydata[onum].number | 0x8000);
283 MSG_WriteShort(msg, 0xFFFF);
286 // (client) reads a frame from network stream
287 void EntityFrame_Read(entity_database_t *d)
289 int number, removed, bits;
290 entity_frame_t framedata, *f = &framedata, deltaframedata, *delta = &deltaframedata;
291 entity_state_t *e, baseline, *old, *oldend;
293 ClearStateToDefault(&baseline);
294 memset(f, 0, sizeof(*f));
295 // read the frame header info
296 f->time = cl.mtime[0];
297 number = MSG_ReadLong();
298 f->framenum = MSG_ReadLong();
299 f->eye[0] = MSG_ReadFloat();
300 f->eye[1] = MSG_ReadFloat();
301 f->eye[2] = MSG_ReadFloat();
302 EntityFrame_AckFrame(d, number);
303 EntityFrame_FetchFrame(d, number, delta);
304 old = delta->entitydata;
305 oldend = old + delta->numentities;
306 // read entities until we hit the magic 0xFFFF end tag
307 while ((number = (unsigned short) MSG_ReadShort()) != 0xFFFF)
310 Host_Error("EntityFrame_Read: read error\n");
311 removed = number & 0x8000;
313 if (number >= MAX_EDICTS)
314 Host_Error("EntityFrame_Read: number (%i) >= MAX_EDICTS (%i)\n", number, MAX_EDICTS);
316 // seek to entity, while copying any skipped entities (assume unchanged)
317 while (old < oldend && old->number < number)
319 if (f->numentities >= MAX_ENTITY_DATABASE)
320 Host_Error("EntityFrame_Read: entity list too big\n");
321 memcpy(f->entitydata + f->numentities, old, sizeof(entity_state_t));
322 f->entitydata[f->numentities].time = cl.mtime[0];
328 if (old < oldend && old->number == number)
331 Con_Printf("EntityFrame_Read: REMOVE on unused entity %i\n", number);
335 if (f->numentities >= MAX_ENTITY_DATABASE)
336 Host_Error("EntityFrame_Read: entity list too big\n");
339 e = f->entitydata + f->numentities++;
341 if (old < oldend && old->number == number)
343 // delta from old entity
344 memcpy(e, old++, sizeof(*e));
348 // delta from baseline
349 memcpy(e, &baseline, sizeof(*e));
353 e->time = cl.mtime[0];
356 bits = MSG_ReadByte();
357 if (bits & E_EXTEND1)
359 bits |= MSG_ReadByte() << 8;
360 if (bits & E_EXTEND2)
362 bits |= MSG_ReadByte() << 16;
363 if (bits & E_EXTEND3)
364 bits |= MSG_ReadByte() << 24;
368 if (bits & E_ORIGIN1)
369 e->origin[0] = (signed short) MSG_ReadShort();
370 if (bits & E_ORIGIN2)
371 e->origin[1] = (signed short) MSG_ReadShort();
372 if (bits & E_ORIGIN3)
373 e->origin[2] = (signed short) MSG_ReadShort();
375 e->angles[0] = MSG_ReadAngle();
377 e->angles[1] = MSG_ReadAngle();
379 e->angles[2] = MSG_ReadAngle();
381 e->modelindex = (e->modelindex & 0xFF00) | MSG_ReadByte();
383 e->modelindex = (e->modelindex & 0x00FF) | (MSG_ReadByte() << 8);
385 e->frame = (e->frame & 0xFF00) | MSG_ReadByte();
387 e->frame = (e->frame & 0x00FF) | (MSG_ReadByte() << 8);
388 if (bits & E_EFFECTS1)
389 e->effects = (e->effects & 0xFF00) | MSG_ReadByte();
390 if (bits & E_EFFECTS2)
391 e->effects = (e->effects & 0x00FF) | (MSG_ReadByte() << 8);
392 if (bits & E_COLORMAP)
393 e->colormap = MSG_ReadByte();
395 e->skin = MSG_ReadByte();
397 e->alpha = MSG_ReadByte();
399 e->scale = MSG_ReadByte();
400 if (bits & E_GLOWSIZE)
401 e->glowsize = MSG_ReadByte();
402 if (bits & E_GLOWCOLOR)
403 e->glowcolor = MSG_ReadByte();
405 e->flags = MSG_ReadByte();
410 if (f->numentities >= MAX_ENTITY_DATABASE)
411 Host_Error("EntityFrame_Read: entity list too big\n");
412 memcpy(f->entitydata + f->numentities, old, sizeof(entity_state_t));
413 f->entitydata[f->numentities].time = cl.mtime[0];
417 EntityFrame_AddFrame(d, f);
421 // (client) reads (and interpolates) the eye location from the database,
422 // given a current time
423 int EntityFrame_FetchEye(entity_database_t *d, vec3_t eye, double time)
426 if (d->numframes == 0)
428 // Host_Error("EntityFrame_FetchEye: no frames\n");
429 if (d->numframes > 1 && d->frames[d->numframes - 2].time != d->frames[d->numframes - 1].time)
431 frac = (time - d->frames[d->numframes - 2].time) / (d->frames[d->numframes - 1].time - d->frames[d->numframes - 2].time);
432 frac = bound(0, frac, 1);
433 VectorSubtract(d->frames[d->numframes - 2].eye, d->frames[d->numframes - 1].eye, eye);
434 VectorMA(d->frames[d->numframes - 2].eye, frac, eye, eye);
437 VectorCopy(d->frames[0].eye, eye);
441 // (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
442 int EntityFrame_FetchEntityByIndex(entity_frame_t *f, entity_state_t *e, int index)
444 if (index < 0 || index >= f->numentities)
446 memcpy(e, f->entitydata + index, sizeof(*e));
450 int EntityFrame_FetchEntityByNumber(entity_frame_t *f, entity_state_t *e, int number)
453 for (i = 0;i < f->numentities;i++)
455 if (f->entitydata[i].number == number)
457 memcpy(e, f->entitydata + i, sizeof(*e));
461 ClearStateToDefault(e);
466 // (client) returns the frame number of the most recent frame recieved
467 int EntityFrame_MostRecentlyRecievedFrameNum(entity_database_t *d)
470 return d->frames[d->numframes - 1].framenum;