4 entity_state_t defaultstate =
6 0,//double time; // time this state was built
7 {0,0,0},//vec3_t origin;
8 {0,0,0},//vec3_t angles;
9 0,//int number; // entity number this state is for
10 0,//unsigned short active; // true if a valid state
11 0,//unsigned short modelindex;
12 0,//unsigned short frame;
13 0,//unsigned short effects;
14 0,//unsigned short tagentity;
15 0,//unsigned short specialvisibilityradius;
16 0,//unsigned short viewmodelforclient;
17 0,//unsigned short exteriormodelforclient;
18 0,//unsigned short nodrawtoclient;
19 0,//unsigned short drawonlytoclient;
20 {0,0,0,0},//short light[4];
22 0,//qbyte lightpflags;
28 254,//qbyte glowcolor;
31 {0,0,0,0,0,0}//qbyte unused[6];
34 void ClearStateToDefault(entity_state_t *s)
38 memset(s, 0, sizeof(*s));
45 void EntityState_Write(entity_state_t *ent, sizebuf_t *msg, entity_state_t *delta)
51 // if not active last frame, delta from defaults
53 delta = &defaultstate;
55 VectorCopy(ent->origin, org);
56 VectorCopy(delta->origin, deltaorg);
57 if (ent->flags & RENDER_LOWPRECISION)
60 org[0] = (int) (org[0] + 0.5f);
62 org[0] = (int) (org[0] - 0.5f);
64 org[1] = (int) (org[1] + 0.5f);
66 org[1] = (int) (org[1] - 0.5f);
68 org[2] = (int) (org[2] + 0.5f);
70 org[2] = (int) (org[2] - 0.5f);
72 if (delta->flags & RENDER_LOWPRECISION)
75 deltaorg[0] = (int) (deltaorg[0] + 0.5f);
77 deltaorg[0] = (int) (deltaorg[0] - 0.5f);
79 deltaorg[1] = (int) (deltaorg[1] + 0.5f);
81 deltaorg[1] = (int) (deltaorg[1] - 0.5f);
83 deltaorg[2] = (int) (deltaorg[2] + 0.5f);
85 deltaorg[2] = (int) (deltaorg[2] - 0.5f);
87 if (fabs(org[0] - deltaorg[0]) > 0.01f)
89 if (fabs(org[1] - deltaorg[1]) > 0.01f)
91 if (fabs(org[2] - deltaorg[2]) > 0.01f)
93 if ((qbyte) (ent->angles[0] * (256.0f / 360.0f)) != (qbyte) (delta->angles[0] * (256.0f / 360.0f)))
95 if ((qbyte) (ent->angles[1] * (256.0f / 360.0f)) != (qbyte) (delta->angles[1] * (256.0f / 360.0f)))
97 if ((qbyte) (ent->angles[2] * (256.0f / 360.0f)) != (qbyte) (delta->angles[2] * (256.0f / 360.0f)))
99 if ((ent->modelindex ^ delta->modelindex) & 0x00FF)
101 if ((ent->modelindex ^ delta->modelindex) & 0xFF00)
103 if ((ent->frame ^ delta->frame) & 0x00FF)
105 if ((ent->frame ^ delta->frame) & 0xFF00)
107 if ((ent->effects ^ delta->effects) & 0x00FF)
109 if ((ent->effects ^ delta->effects) & 0xFF00)
111 if (ent->colormap != delta->colormap)
113 if (ent->skin != delta->skin)
115 if (ent->alpha != delta->alpha)
117 if (ent->scale != delta->scale)
119 if (ent->glowsize != delta->glowsize)
121 if (ent->glowcolor != delta->glowcolor)
123 if (ent->flags != delta->flags)
125 if (ent->tagindex != delta->tagindex || ent->tagentity != delta->tagentity)
126 bits |= E_TAGATTACHMENT;
127 if (ent->light[0] != delta->light[0] || ent->light[1] != delta->light[1] || ent->light[2] != delta->light[2] || ent->light[3] != delta->light[3])
129 if (ent->lightstyle != delta->lightstyle)
130 bits |= E_LIGHTSTYLE;
131 if (ent->lightpflags != delta->lightpflags)
132 bits |= E_LIGHTPFLAGS;
134 if (bits) // don't send anything if it hasn't changed
136 if (bits & 0xFF000000)
138 if (bits & 0x00FF0000)
140 if (bits & 0x0000FF00)
143 MSG_WriteShort(msg, ent->number);
144 MSG_WriteByte(msg, bits & 0xFF);
145 if (bits & E_EXTEND1)
147 MSG_WriteByte(msg, (bits >> 8) & 0xFF);
148 if (bits & E_EXTEND2)
150 MSG_WriteByte(msg, (bits >> 16) & 0xFF);
151 if (bits & E_EXTEND3)
152 MSG_WriteByte(msg, (bits >> 24) & 0xFF);
155 // LordHavoc: have to write flags first, as they can modify protocol
157 MSG_WriteByte(msg, ent->flags);
158 if (ent->flags & RENDER_LOWPRECISION)
160 if (bits & E_ORIGIN1)
161 MSG_WriteShort(msg, org[0]);
162 if (bits & E_ORIGIN2)
163 MSG_WriteShort(msg, org[1]);
164 if (bits & E_ORIGIN3)
165 MSG_WriteShort(msg, org[2]);
169 if (bits & E_ORIGIN1)
170 MSG_WriteFloat(msg, org[0]);
171 if (bits & E_ORIGIN2)
172 MSG_WriteFloat(msg, org[1]);
173 if (bits & E_ORIGIN3)
174 MSG_WriteFloat(msg, org[2]);
177 MSG_WriteAngle(msg, ent->angles[0]);
179 MSG_WriteAngle(msg, ent->angles[1]);
181 MSG_WriteAngle(msg, ent->angles[2]);
183 MSG_WriteByte(msg, ent->modelindex & 0xFF);
185 MSG_WriteByte(msg, (ent->modelindex >> 8) & 0xFF);
187 MSG_WriteByte(msg, ent->frame & 0xFF);
189 MSG_WriteByte(msg, (ent->frame >> 8) & 0xFF);
190 if (bits & E_EFFECTS1)
191 MSG_WriteByte(msg, ent->effects & 0xFF);
192 if (bits & E_EFFECTS2)
193 MSG_WriteByte(msg, (ent->effects >> 8) & 0xFF);
194 if (bits & E_COLORMAP)
195 MSG_WriteByte(msg, ent->colormap);
197 MSG_WriteByte(msg, ent->skin);
199 MSG_WriteByte(msg, ent->alpha);
201 MSG_WriteByte(msg, ent->scale);
202 if (bits & E_GLOWSIZE)
203 MSG_WriteByte(msg, ent->glowsize);
204 if (bits & E_GLOWCOLOR)
205 MSG_WriteByte(msg, ent->glowcolor);
206 if (bits & E_TAGATTACHMENT)
208 MSG_WriteShort(msg, ent->tagentity);
209 MSG_WriteByte(msg, ent->tagindex);
213 MSG_WriteShort(msg, ent->light[0]);
214 MSG_WriteShort(msg, ent->light[1]);
215 MSG_WriteShort(msg, ent->light[2]);
216 MSG_WriteShort(msg, ent->light[3]);
218 if (bits & E_LIGHTSTYLE)
219 MSG_WriteByte(msg, ent->lightstyle);
220 if (bits & E_LIGHTPFLAGS)
221 MSG_WriteByte(msg, ent->lightpflags);
224 else if (delta->active)
225 MSG_WriteShort(msg, ent->number | 0x8000);
228 void EntityState_ReadUpdate(entity_state_t *e, int number)
231 cl_entities_active[number] = true;
233 e->time = cl.mtime[0];
236 bits = MSG_ReadByte();
237 if (bits & E_EXTEND1)
239 bits |= MSG_ReadByte() << 8;
240 if (bits & E_EXTEND2)
242 bits |= MSG_ReadByte() << 16;
243 if (bits & E_EXTEND3)
244 bits |= MSG_ReadByte() << 24;
248 if (cl.protocol == PROTOCOL_DARKPLACES2)
250 if (bits & E_ORIGIN1)
251 e->origin[0] = (signed short) MSG_ReadShort();
252 if (bits & E_ORIGIN2)
253 e->origin[1] = (signed short) MSG_ReadShort();
254 if (bits & E_ORIGIN3)
255 e->origin[2] = (signed short) MSG_ReadShort();
260 e->flags = MSG_ReadByte();
261 if (e->flags & RENDER_LOWPRECISION || cl.protocol == PROTOCOL_DARKPLACES2)
263 if (bits & E_ORIGIN1)
264 e->origin[0] = (signed short) MSG_ReadShort();
265 if (bits & E_ORIGIN2)
266 e->origin[1] = (signed short) MSG_ReadShort();
267 if (bits & E_ORIGIN3)
268 e->origin[2] = (signed short) MSG_ReadShort();
272 if (bits & E_ORIGIN1)
273 e->origin[0] = MSG_ReadFloat();
274 if (bits & E_ORIGIN2)
275 e->origin[1] = MSG_ReadFloat();
276 if (bits & E_ORIGIN3)
277 e->origin[2] = MSG_ReadFloat();
281 e->angles[0] = MSG_ReadAngle();
283 e->angles[1] = MSG_ReadAngle();
285 e->angles[2] = MSG_ReadAngle();
287 e->modelindex = (e->modelindex & 0xFF00) | (unsigned int) MSG_ReadByte();
289 e->modelindex = (e->modelindex & 0x00FF) | ((unsigned int) MSG_ReadByte() << 8);
291 e->frame = (e->frame & 0xFF00) | (unsigned int) MSG_ReadByte();
293 e->frame = (e->frame & 0x00FF) | ((unsigned int) MSG_ReadByte() << 8);
294 if (bits & E_EFFECTS1)
295 e->effects = (e->effects & 0xFF00) | (unsigned int) MSG_ReadByte();
296 if (bits & E_EFFECTS2)
297 e->effects = (e->effects & 0x00FF) | ((unsigned int) MSG_ReadByte() << 8);
298 if (bits & E_COLORMAP)
299 e->colormap = MSG_ReadByte();
301 e->skin = MSG_ReadByte();
303 e->alpha = MSG_ReadByte();
305 e->scale = MSG_ReadByte();
306 if (bits & E_GLOWSIZE)
307 e->glowsize = MSG_ReadByte();
308 if (bits & E_GLOWCOLOR)
309 e->glowcolor = MSG_ReadByte();
310 if (cl.protocol == PROTOCOL_DARKPLACES2)
312 e->flags = MSG_ReadByte();
313 if (bits & E_TAGATTACHMENT)
315 e->tagentity = MSG_ReadShort();
316 e->tagindex = MSG_ReadByte();
320 e->light[0] = MSG_ReadShort();
321 e->light[1] = MSG_ReadShort();
322 e->light[2] = MSG_ReadShort();
323 e->light[3] = MSG_ReadShort();
325 if (bits & E_LIGHTSTYLE)
326 e->lightstyle = MSG_ReadByte();
327 if (bits & E_LIGHTPFLAGS)
328 e->lightpflags = MSG_ReadByte();
330 if (developer_networkentities.integer >= 2)
332 Con_Printf("ReadUpdate e%i", number);
334 if (bits & E_ORIGIN1)
335 Con_Printf(" E_ORIGIN1 %f", e->origin[0]);
336 if (bits & E_ORIGIN2)
337 Con_Printf(" E_ORIGIN2 %f", e->origin[1]);
338 if (bits & E_ORIGIN3)
339 Con_Printf(" E_ORIGIN3 %f", e->origin[2]);
341 Con_Printf(" E_ANGLE1 %f", e->angles[0]);
343 Con_Printf(" E_ANGLE2 %f", e->angles[1]);
345 Con_Printf(" E_ANGLE3 %f", e->angles[2]);
346 if (bits & (E_MODEL1 | E_MODEL2))
347 Con_Printf(" E_MODEL %i", e->modelindex);
349 if (bits & (E_FRAME1 | E_FRAME2))
350 Con_Printf(" E_FRAME %i", e->frame);
351 if (bits & (E_EFFECTS1 | E_EFFECTS2))
352 Con_Printf(" E_EFFECTS %i", e->effects);
354 Con_Printf(" E_ALPHA %f", e->alpha / 255.0f);
356 Con_Printf(" E_SCALE %f", e->scale / 16.0f);
357 if (bits & E_COLORMAP)
358 Con_Printf(" E_COLORMAP %i", e->colormap);
360 Con_Printf(" E_SKIN %i", e->skin);
362 if (bits & E_GLOWSIZE)
363 Con_Printf(" E_GLOWSIZE %i", e->glowsize * 4);
364 if (bits & E_GLOWCOLOR)
365 Con_Printf(" E_GLOWCOLOR %i", e->glowcolor);
368 Con_Printf(" E_LIGHT %i:%i:%i:%i", e->light[0], e->light[1], e->light[2], e->light[3]);
369 if (bits & E_LIGHTPFLAGS)
370 Con_Printf(" E_LIGHTPFLAGS %i", e->lightpflags);
372 if (bits & E_TAGATTACHMENT)
373 Con_Printf(" E_TAGATTACHMENT e%i:%i", e->tagentity, e->tagindex);
374 if (bits & E_LIGHTSTYLE)
375 Con_Printf(" E_LIGHTSTYLE %i", e->lightstyle);
380 // (server) clears the database to contain no frames (thus delta compression compresses against nothing)
381 void EntityFrame_ClearDatabase(entity_database_t *d)
383 memset(d, 0, sizeof(*d));
386 // (server and client) removes frames older than 'frame' from database
387 void EntityFrame_AckFrame(entity_database_t *d, int frame)
390 if (d->ackframe < frame)
392 for (i = 0;i < d->numframes && d->frames[i].framenum < frame;i++);
393 // ignore outdated frame acks (out of order packets)
397 // if some queue is left, slide it down to beginning of array
399 memmove(&d->frames[0], &d->frames[i], sizeof(d->frames[0]) * d->numframes);
402 // (server) clears frame, to prepare for adding entities
403 void EntityFrame_Clear(entity_frame_t *f, vec3_t eye, int framenum)
406 f->framenum = framenum;
414 VectorCopy(eye, f->eye);
418 // (server) adds an entity to frame
419 void EntityFrame_AddEntity(entity_frame_t *f, entity_state_t *s)
421 if (f->numentities < MAX_ENTITY_DATABASE)
423 f->entitydata[f->numentities] = *s;
424 f->entitydata[f->numentities++].active = true;
428 // (server and client) reads a frame from the database
429 void EntityFrame_FetchFrame(entity_database_t *d, int framenum, entity_frame_t *f)
432 EntityFrame_Clear(f, NULL, -1);
433 for (i = 0;i < d->numframes && d->frames[i].framenum < framenum;i++);
434 if (i < d->numframes && framenum == d->frames[i].framenum)
436 f->framenum = framenum;
437 f->numentities = d->frames[i].endentity - d->frames[i].firstentity;
438 n = MAX_ENTITY_DATABASE - (d->frames[i].firstentity % MAX_ENTITY_DATABASE);
439 if (n > f->numentities)
441 memcpy(f->entitydata, d->entitydata + d->frames[i].firstentity % MAX_ENTITY_DATABASE, sizeof(*f->entitydata) * n);
442 if (f->numentities > n)
443 memcpy(f->entitydata + n, d->entitydata, sizeof(*f->entitydata) * (f->numentities - n));
444 VectorCopy(d->eye, f->eye);
448 // (server and client) adds a entity_frame to the database, for future reference
449 void EntityFrame_AddFrame(entity_database_t *d, entity_frame_t *f)
452 entity_frameinfo_t *info;
454 VectorCopy(f->eye, d->eye);
456 // figure out how many entity slots are used already
459 n = d->frames[d->numframes - 1].endentity - d->frames[0].firstentity;
460 if (n + f->numentities > MAX_ENTITY_DATABASE || d->numframes >= MAX_ENTITY_HISTORY)
462 // ran out of room, dump database
463 EntityFrame_ClearDatabase(d);
467 info = &d->frames[d->numframes];
468 info->framenum = f->framenum;
470 // make sure we check the newly added frame as well, but we haven't incremented numframes yet
471 for (n = 0;n <= d->numframes;n++)
473 if (e >= d->frames[n].framenum)
475 if (e == f->framenum)
476 Con_Printf("EntityFrame_AddFrame: tried to add out of sequence frame to database\n");
478 Con_Printf("EntityFrame_AddFrame: out of sequence frames in database\n");
481 e = d->frames[n].framenum;
483 // if database still has frames after that...
485 info->firstentity = d->frames[d->numframes - 1].endentity;
487 info->firstentity = 0;
488 info->endentity = info->firstentity + f->numentities;
491 n = info->firstentity % MAX_ENTITY_DATABASE;
492 e = MAX_ENTITY_DATABASE - n;
493 if (e > f->numentities)
495 memcpy(d->entitydata + n, f->entitydata, sizeof(entity_state_t) * e);
496 if (f->numentities > e)
497 memcpy(d->entitydata, f->entitydata + e, sizeof(entity_state_t) * (f->numentities - e));
500 // (server) writes a frame to network stream
501 static entity_frame_t deltaframe; // FIXME?
502 void EntityFrame_Write(entity_database_t *d, entity_frame_t *f, sizebuf_t *msg)
505 entity_frame_t *o = &deltaframe;
506 entity_state_t *ent, *delta;
508 EntityFrame_AddFrame(d, f);
510 EntityFrame_FetchFrame(d, d->ackframe > 0 ? d->ackframe : -1, o);
511 MSG_WriteByte (msg, svc_entities);
512 MSG_WriteLong (msg, o->framenum);
513 MSG_WriteLong (msg, f->framenum);
514 MSG_WriteFloat (msg, f->eye[0]);
515 MSG_WriteFloat (msg, f->eye[1]);
516 MSG_WriteFloat (msg, f->eye[2]);
519 for (i = 0;i < f->numentities;i++)
521 ent = f->entitydata + i;
522 number = ent->number;
523 for (;onum < o->numentities && o->entitydata[onum].number < number;onum++)
525 // write remove message
526 MSG_WriteShort(msg, o->entitydata[onum].number | 0x8000);
528 if (onum < o->numentities && (o->entitydata[onum].number == number))
530 // delta from previous frame
531 delta = o->entitydata + onum;
532 // advance to next entity in delta frame
537 // delta from defaults
538 delta = &defaultstate;
540 EntityState_Write(ent, msg, delta);
542 for (;onum < o->numentities;onum++)
544 // write remove message
545 MSG_WriteShort(msg, o->entitydata[onum].number | 0x8000);
547 MSG_WriteShort(msg, 0xFFFF);
550 // (client) reads a frame from network stream
551 static entity_frame_t framedata; // FIXME?
552 void EntityFrame_Read(entity_database_t *d)
555 entity_frame_t *f = &framedata, *delta = &deltaframe;
556 entity_state_t *e, *old, *oldend;
558 EntityFrame_Clear(f, NULL, -1);
560 // read the frame header info
561 f->time = cl.mtime[0];
562 number = MSG_ReadLong();
563 f->framenum = MSG_ReadLong();
564 f->eye[0] = MSG_ReadFloat();
565 f->eye[1] = MSG_ReadFloat();
566 f->eye[2] = MSG_ReadFloat();
567 EntityFrame_AckFrame(d, number);
568 EntityFrame_FetchFrame(d, number, delta);
569 old = delta->entitydata;
570 oldend = old + delta->numentities;
571 // read entities until we hit the magic 0xFFFF end tag
572 while ((number = (unsigned short) MSG_ReadShort()) != 0xFFFF)
575 Host_Error("EntityFrame_Read: read error\n");
576 removed = number & 0x8000;
578 if (number >= MAX_EDICTS)
579 Host_Error("EntityFrame_Read: number (%i) >= MAX_EDICTS (%i)\n", number, MAX_EDICTS);
581 // seek to entity, while copying any skipped entities (assume unchanged)
582 while (old < oldend && old->number < number)
584 if (f->numentities >= MAX_ENTITY_DATABASE)
585 Host_Error("EntityFrame_Read: entity list too big\n");
586 f->entitydata[f->numentities] = *old++;
587 f->entitydata[f->numentities++].time = cl.mtime[0];
591 if (old < oldend && old->number == number)
594 Con_Printf("EntityFrame_Read: REMOVE on unused entity %i\n", number);
598 if (f->numentities >= MAX_ENTITY_DATABASE)
599 Host_Error("EntityFrame_Read: entity list too big\n");
602 e = f->entitydata + f->numentities++;
604 if (old < oldend && old->number == number)
606 // delta from old entity
611 // delta from defaults
615 EntityState_ReadUpdate(e, number);
620 if (f->numentities >= MAX_ENTITY_DATABASE)
621 Host_Error("EntityFrame_Read: entity list too big\n");
622 f->entitydata[f->numentities] = *old++;
623 f->entitydata[f->numentities++].time = cl.mtime[0];
625 EntityFrame_AddFrame(d, f);
629 // (client) returns the frame number of the most recent frame recieved
630 int EntityFrame_MostRecentlyRecievedFrameNum(entity_database_t *d)
633 return d->frames[d->numframes - 1].framenum;
643 entity_state_t *EntityFrame4_GetReferenceEntity(entity_database4_t *d, int number)
645 if (d->maxreferenceentities <= number)
647 int oldmax = d->maxreferenceentities;
648 entity_state_t *oldentity = d->referenceentity;
649 d->maxreferenceentities = (number + 15) & ~7;
650 d->referenceentity = Mem_Alloc(d->mempool, d->maxreferenceentities * sizeof(*d->referenceentity));
653 memcpy(d->referenceentity, oldentity, oldmax * sizeof(*d->referenceentity));
656 // clear the newly created entities
657 for (;oldmax < d->maxreferenceentities;oldmax++)
659 d->referenceentity[oldmax] = defaultstate;
660 d->referenceentity[oldmax].number = oldmax;
663 return d->referenceentity + number;
666 void EntityFrame4_AddCommitEntity(entity_database4_t *d, entity_state_t *s)
668 // resize commit's entity list if full
669 if (d->currentcommit->maxentities <= d->currentcommit->numentities)
671 entity_state_t *oldentity = d->currentcommit->entity;
672 d->currentcommit->maxentities += 8;
673 d->currentcommit->entity = Mem_Alloc(d->mempool, d->currentcommit->maxentities * sizeof(*d->currentcommit->entity));
676 memcpy(d->currentcommit->entity, oldentity, d->currentcommit->numentities * sizeof(*d->currentcommit->entity));
680 d->currentcommit->entity[d->currentcommit->numentities++] = *s;
683 entity_database4_t *EntityFrame4_AllocDatabase(mempool_t *pool)
685 entity_database4_t *d;
686 d = Mem_Alloc(pool, sizeof(*d));
688 EntityFrame4_ResetDatabase(d);
693 void EntityFrame4_FreeDatabase(entity_database4_t *d)
696 for (i = 0;i < MAX_ENTITY_HISTORY;i++)
697 if (d->commit[i].entity)
698 Mem_Free(d->commit[i].entity);
699 if (d->referenceentity)
700 Mem_Free(d->referenceentity);
704 void EntityFrame4_ResetDatabase(entity_database4_t *d)
707 d->referenceframenum = -1;
708 for (i = 0;i < MAX_ENTITY_HISTORY;i++)
709 d->commit[i].numentities = 0;
710 for (i = 0;i < d->maxreferenceentities;i++)
711 d->referenceentity[i] = defaultstate;
714 int EntityFrame4_AckFrame(entity_database4_t *d, int framenum)
716 int i, j, found = false;
717 entity_database4_commit_t *commit;
718 if (d->referenceframenum == framenum)
722 EntityFrame4_ResetDatabase(d);
725 for (i = 0, commit = d->commit;i < MAX_ENTITY_HISTORY;i++, commit++)
727 if (commit->numentities && commit->framenum <= framenum)
729 if (commit->framenum == framenum)
732 d->referenceframenum = framenum;
733 if (developer_networkentities.integer >= 3)
735 for (j = 0;j < commit->numentities;j++)
737 entity_state_t *s = EntityFrame4_GetReferenceEntity(d, commit->entity[j].number);
738 if (commit->entity[j].active != s->active)
740 if (commit->entity[j].active)
741 Con_Printf("commit entity %i has become active (modelindex %i)\n", commit->entity[j].number, commit->entity[j].modelindex);
743 Con_Printf("commit entity %i has become inactive (modelindex %i)\n", commit->entity[j].number, commit->entity[j].modelindex);
745 *s = commit->entity[j];
749 for (j = 0;j < commit->numentities;j++)
750 *EntityFrame4_GetReferenceEntity(d, commit->entity[j].number) = commit->entity[j];
752 commit->numentities = 0;
758 int EntityFrame4_SV_WriteFrame_Entity(entity_database4_t *d, sizebuf_t *msg, int maxbytes, entity_state_t *s)
763 // prepare the buffer
764 memset(&buf, 0, sizeof(buf));
766 buf.maxsize = sizeof(data);
768 e = EntityFrame4_GetReferenceEntity(d, s->number);
769 // send an update (may update or remove the entity)
770 EntityState_Write(s, &buf, e);
771 // if the message is empty, skip out now
774 // if the commit is full, we're done
775 if (msg->cursize + buf.cursize + 2 >= min(msg->maxsize, maxbytes))
777 // add the entity to the commit
778 EntityFrame4_AddCommitEntity(d, s);
779 // write the message to the packet
780 SZ_Write(msg, buf.data, buf.cursize);
785 extern void CL_MoveLerpEntityStates(entity_t *ent);
786 void EntityFrame4_CL_ReadFrame(entity_database4_t *d)
788 int i, n, cnumber, referenceframenum, framenum, enumber, done, stopnumber, skip = false;
789 // read the number of the frame this refers to
790 referenceframenum = MSG_ReadLong();
791 // read the number of this frame
792 framenum = MSG_ReadLong();
793 // read the start number
794 enumber = MSG_ReadShort();
795 if (developer_networkentities.integer >= 1)
797 Con_Printf("recv svc_entities ref:%i num:%i (database: ref:%i commits:", referenceframenum, framenum, d->referenceframenum);
798 for (i = 0;i < MAX_ENTITY_HISTORY;i++)
799 if (d->commit[i].numentities)
800 Con_Printf(" %i", d->commit[i].framenum);
803 if (!EntityFrame4_AckFrame(d, referenceframenum))
805 Con_Printf("EntityFrame4_CL_ReadFrame: reference frame invalid (VERY BAD ERROR), this update will be skipped\n");
808 EntityFrame4_ResetDatabase(d);
810 d->currentcommit = NULL;
811 for (i = 0;i < MAX_ENTITY_HISTORY;i++)
813 if (!d->commit[i].numentities)
815 d->currentcommit = d->commit + i;
816 d->currentcommit->framenum = d->ackframenum = framenum;
817 d->currentcommit->numentities = 0;
820 if (d->currentcommit == NULL)
822 Con_Printf("EntityFrame4_CL_ReadFrame: error while decoding frame %i: database full, reading but not storing this update\n", framenum);
826 while (!done && !msg_badread)
828 // read the number of the modified entity
829 // (gaps will be copied unmodified)
830 n = (unsigned short)MSG_ReadShort();
833 // no more entities in this update, but we still need to copy the
834 // rest of the reference entities (final gap)
836 // read end of range number, then process normally
837 n = (unsigned short)MSG_ReadShort();
839 // high bit means it's a remove message
840 cnumber = n & 0x7FFF;
841 // add one (the changed one) if not done
842 stopnumber = cnumber + !done;
843 // process entities in range from the last one to the changed one
844 for (;enumber < stopnumber;enumber++)
848 if (enumber == cnumber && (n & 0x8000) == 0)
850 entity_state_t tempstate;
851 EntityState_ReadUpdate(&tempstate, enumber);
855 // slide the current into the previous slot
856 cl_entities[enumber].state_previous = cl_entities[enumber].state_current;
857 // copy a new current from reference database
858 cl_entities[enumber].state_current = *EntityFrame4_GetReferenceEntity(d, enumber);
859 // if this is the one to modify, read more data...
860 if (enumber == cnumber)
865 if (developer_networkentities.integer >= 2)
866 Con_Printf("entity %i: remove\n", enumber);
867 cl_entities[enumber].state_current = defaultstate;
872 if (developer_networkentities.integer >= 2)
873 Con_Printf("entity %i: update\n", enumber);
874 EntityState_ReadUpdate(&cl_entities[enumber].state_current, enumber);
877 else if (developer_networkentities.integer >= 4)
878 Con_Printf("entity %i: copy\n", enumber);
879 // fix the number (it gets wiped occasionally by copying from defaultstate)
880 cl_entities[enumber].state_current.number = enumber;
881 // check if we need to update the lerp stuff
882 if (cl_entities[enumber].state_current.active)
884 CL_MoveLerpEntityStates(&cl_entities[enumber]);
885 cl_entities_active[enumber] = true;
887 // add this to the commit entry whether it is modified or not
888 if (d->currentcommit)
889 EntityFrame4_AddCommitEntity(d, &cl_entities[enumber].state_current);
890 // print extra messages if desired
891 if (developer_networkentities.integer >= 2 && cl_entities[enumber].state_current.active != cl_entities[enumber].state_previous.active)
893 if (cl_entities[enumber].state_current.active)
894 Con_Printf("entity #%i has become active\n", enumber);
895 else if (cl_entities[enumber].state_previous.active)
896 Con_Printf("entity #%i has become inactive\n", enumber);
900 d->currentcommit = NULL;