4 extern cvar_t developer_networkentities;
6 entity_state_t defaultstate =
8 0,//double time; // time this state was built
9 {0,0,0},//vec3_t origin;
10 {0,0,0},//vec3_t angles;
11 0,//int number; // entity number this state is for
12 0,//unsigned short active; // true if a valid state
13 0,//unsigned short modelindex;
14 0,//unsigned short frame;
15 0,//unsigned short effects;
16 0,//unsigned short tagentity;
17 0,//unsigned short specialvisibilityradius;
18 0,//unsigned short viewmodelforclient;
19 0,//unsigned short exteriormodelforclient;
20 0,//unsigned short nodrawtoclient;
21 0,//unsigned short drawonlytoclient;
27 254,//qbyte glowcolor;
32 void ClearStateToDefault(entity_state_t *s)
36 memset(s, 0, sizeof(*s));
43 void EntityState_Write(entity_state_t *ent, sizebuf_t *msg, entity_state_t *delta)
49 // if not active last frame, delta from defaults
51 delta = &defaultstate;
53 VectorCopy(ent->origin, org);
54 VectorCopy(delta->origin, deltaorg);
55 if (ent->flags & RENDER_LOWPRECISION)
58 org[0] = (int) (org[0] + 0.5f);
60 org[0] = (int) (org[0] - 0.5f);
62 org[1] = (int) (org[1] + 0.5f);
64 org[1] = (int) (org[1] - 0.5f);
66 org[2] = (int) (org[2] + 0.5f);
68 org[2] = (int) (org[2] - 0.5f);
70 if (delta->flags & RENDER_LOWPRECISION)
73 deltaorg[0] = (int) (deltaorg[0] + 0.5f);
75 deltaorg[0] = (int) (deltaorg[0] - 0.5f);
77 deltaorg[1] = (int) (deltaorg[1] + 0.5f);
79 deltaorg[1] = (int) (deltaorg[1] - 0.5f);
81 deltaorg[2] = (int) (deltaorg[2] + 0.5f);
83 deltaorg[2] = (int) (deltaorg[2] - 0.5f);
85 if (fabs(org[0] - deltaorg[0]) > 0.01f)
87 if (fabs(org[1] - deltaorg[1]) > 0.01f)
89 if (fabs(org[2] - deltaorg[2]) > 0.01f)
91 if ((qbyte) (ent->angles[0] * (256.0f / 360.0f)) != (qbyte) (delta->angles[0] * (256.0f / 360.0f)))
93 if ((qbyte) (ent->angles[1] * (256.0f / 360.0f)) != (qbyte) (delta->angles[1] * (256.0f / 360.0f)))
95 if ((qbyte) (ent->angles[2] * (256.0f / 360.0f)) != (qbyte) (delta->angles[2] * (256.0f / 360.0f)))
97 if ((ent->modelindex ^ delta->modelindex) & 0x00FF)
99 if ((ent->modelindex ^ delta->modelindex) & 0xFF00)
101 if ((ent->frame ^ delta->frame) & 0x00FF)
103 if ((ent->frame ^ delta->frame) & 0xFF00)
105 if ((ent->effects ^ delta->effects) & 0x00FF)
107 if ((ent->effects ^ delta->effects) & 0xFF00)
109 if (ent->colormap != delta->colormap)
111 if (ent->skin != delta->skin)
113 if (ent->alpha != delta->alpha)
115 if (ent->scale != delta->scale)
117 if (ent->glowsize != delta->glowsize)
119 if (ent->glowcolor != delta->glowcolor)
121 if (ent->flags != delta->flags)
123 if (ent->tagindex != delta->tagindex || ent->tagentity != delta->tagentity)
124 bits |= E_TAGATTACHMENT;
126 if (bits) // don't send anything if it hasn't changed
128 if (bits & 0xFF000000)
130 if (bits & 0x00FF0000)
132 if (bits & 0x0000FF00)
135 MSG_WriteShort(msg, ent->number);
136 MSG_WriteByte(msg, bits & 0xFF);
137 if (bits & E_EXTEND1)
139 MSG_WriteByte(msg, (bits >> 8) & 0xFF);
140 if (bits & E_EXTEND2)
142 MSG_WriteByte(msg, (bits >> 16) & 0xFF);
143 if (bits & E_EXTEND3)
144 MSG_WriteByte(msg, (bits >> 24) & 0xFF);
147 // LordHavoc: have to write flags first, as they can modify protocol
149 MSG_WriteByte(msg, ent->flags);
150 if (ent->flags & RENDER_LOWPRECISION)
152 if (bits & E_ORIGIN1)
153 MSG_WriteShort(msg, org[0]);
154 if (bits & E_ORIGIN2)
155 MSG_WriteShort(msg, org[1]);
156 if (bits & E_ORIGIN3)
157 MSG_WriteShort(msg, org[2]);
161 if (bits & E_ORIGIN1)
162 MSG_WriteFloat(msg, org[0]);
163 if (bits & E_ORIGIN2)
164 MSG_WriteFloat(msg, org[1]);
165 if (bits & E_ORIGIN3)
166 MSG_WriteFloat(msg, org[2]);
169 MSG_WriteAngle(msg, ent->angles[0]);
171 MSG_WriteAngle(msg, ent->angles[1]);
173 MSG_WriteAngle(msg, ent->angles[2]);
175 MSG_WriteByte(msg, ent->modelindex & 0xFF);
177 MSG_WriteByte(msg, (ent->modelindex >> 8) & 0xFF);
179 MSG_WriteByte(msg, ent->frame & 0xFF);
181 MSG_WriteByte(msg, (ent->frame >> 8) & 0xFF);
182 if (bits & E_EFFECTS1)
183 MSG_WriteByte(msg, ent->effects & 0xFF);
184 if (bits & E_EFFECTS2)
185 MSG_WriteByte(msg, (ent->effects >> 8) & 0xFF);
186 if (bits & E_COLORMAP)
187 MSG_WriteByte(msg, ent->colormap);
189 MSG_WriteByte(msg, ent->skin);
191 MSG_WriteByte(msg, ent->alpha);
193 MSG_WriteByte(msg, ent->scale);
194 if (bits & E_GLOWSIZE)
195 MSG_WriteByte(msg, ent->glowsize);
196 if (bits & E_GLOWCOLOR)
197 MSG_WriteByte(msg, ent->glowcolor);
198 if (bits & E_TAGATTACHMENT)
200 MSG_WriteShort(msg, ent->tagentity);
201 MSG_WriteByte(msg, ent->tagindex);
205 else if (delta->active)
206 MSG_WriteShort(msg, ent->number | 0x8000);
209 void EntityState_ReadUpdate(entity_state_t *e, int number)
212 cl_entities_active[number] = true;
214 e->time = cl.mtime[0];
217 bits = MSG_ReadByte();
218 if (bits & E_EXTEND1)
220 bits |= MSG_ReadByte() << 8;
221 if (bits & E_EXTEND2)
223 bits |= MSG_ReadByte() << 16;
224 if (bits & E_EXTEND3)
225 bits |= MSG_ReadByte() << 24;
229 if (cl.protocol == PROTOCOL_DARKPLACES2)
231 if (bits & E_ORIGIN1)
232 e->origin[0] = (signed short) MSG_ReadShort();
233 if (bits & E_ORIGIN2)
234 e->origin[1] = (signed short) MSG_ReadShort();
235 if (bits & E_ORIGIN3)
236 e->origin[2] = (signed short) MSG_ReadShort();
241 e->flags = MSG_ReadByte();
242 if (e->flags & RENDER_LOWPRECISION || cl.protocol == PROTOCOL_DARKPLACES2)
244 if (bits & E_ORIGIN1)
245 e->origin[0] = (signed short) MSG_ReadShort();
246 if (bits & E_ORIGIN2)
247 e->origin[1] = (signed short) MSG_ReadShort();
248 if (bits & E_ORIGIN3)
249 e->origin[2] = (signed short) MSG_ReadShort();
253 if (bits & E_ORIGIN1)
254 e->origin[0] = MSG_ReadFloat();
255 if (bits & E_ORIGIN2)
256 e->origin[1] = MSG_ReadFloat();
257 if (bits & E_ORIGIN3)
258 e->origin[2] = MSG_ReadFloat();
262 e->angles[0] = MSG_ReadAngle();
264 e->angles[1] = MSG_ReadAngle();
266 e->angles[2] = MSG_ReadAngle();
268 e->modelindex = (e->modelindex & 0xFF00) | (unsigned int) MSG_ReadByte();
270 e->modelindex = (e->modelindex & 0x00FF) | ((unsigned int) MSG_ReadByte() << 8);
272 e->frame = (e->frame & 0xFF00) | (unsigned int) MSG_ReadByte();
274 e->frame = (e->frame & 0x00FF) | ((unsigned int) MSG_ReadByte() << 8);
275 if (bits & E_EFFECTS1)
276 e->effects = (e->effects & 0xFF00) | (unsigned int) MSG_ReadByte();
277 if (bits & E_EFFECTS2)
278 e->effects = (e->effects & 0x00FF) | ((unsigned int) MSG_ReadByte() << 8);
279 if (bits & E_COLORMAP)
280 e->colormap = MSG_ReadByte();
282 e->skin = MSG_ReadByte();
284 e->alpha = MSG_ReadByte();
286 e->scale = MSG_ReadByte();
287 if (bits & E_GLOWSIZE)
288 e->glowsize = MSG_ReadByte();
289 if (bits & E_GLOWCOLOR)
290 e->glowcolor = MSG_ReadByte();
291 if (cl.protocol == PROTOCOL_DARKPLACES2)
293 e->flags = MSG_ReadByte();
294 if (bits & E_TAGATTACHMENT)
296 e->tagentity = MSG_ReadShort();
297 e->tagindex = MSG_ReadByte();
300 if (developer_networkentities.integer)
302 Con_Printf("ReadUpdate e%i", number);
304 if (bits & E_ORIGIN1)
305 Con_Printf(" E_ORIGIN1 %f", e->origin[0]);
306 if (bits & E_ORIGIN2)
307 Con_Printf(" E_ORIGIN2 %f", e->origin[1]);
308 if (bits & E_ORIGIN3)
309 Con_Printf(" E_ORIGIN3 %f", e->origin[2]);
311 Con_Printf(" E_ANGLE1 %f", e->angles[0]);
313 Con_Printf(" E_ANGLE2 %f", e->angles[1]);
315 Con_Printf(" E_ANGLE3 %f", e->angles[2]);
316 if (bits & (E_MODEL1 | E_MODEL2))
317 Con_Printf(" E_MODEL %i", e->modelindex);
319 if (bits & (E_FRAME1 | E_FRAME2))
320 Con_Printf(" E_FRAME %i", e->frame);
321 if (bits & (E_EFFECTS1 | E_EFFECTS2))
322 Con_Printf(" E_EFFECTS %i", e->effects);
324 Con_Printf(" E_ALPHA %f", e->alpha / 255.0f);
326 Con_Printf(" E_SCALE %f", e->scale / 16.0f);
327 if (bits & E_COLORMAP)
328 Con_Printf(" E_COLORMAP %i", e->colormap);
330 Con_Printf(" E_SKIN %i", e->skin);
332 if (bits & E_GLOWSIZE)
333 Con_Printf(" E_GLOWSIZE %i", e->glowsize * 8);
334 if (bits & E_GLOWCOLOR)
335 Con_Printf(" E_GLOWCOLOR %i", e->glowcolor);
337 if (bits & E_TAGATTACHMENT)
338 Con_Printf(" E_TAGATTACHMENT e%i:%i", e->tagentity, e->tagindex);
343 // (server) clears the database to contain no frames (thus delta compression compresses against nothing)
344 void EntityFrame_ClearDatabase(entity_database_t *d)
346 memset(d, 0, sizeof(*d));
349 // (server and client) removes frames older than 'frame' from database
350 void EntityFrame_AckFrame(entity_database_t *d, int frame)
353 if (d->ackframe < frame)
355 for (i = 0;i < d->numframes && d->frames[i].framenum < frame;i++);
356 // ignore outdated frame acks (out of order packets)
360 // if some queue is left, slide it down to beginning of array
362 memmove(&d->frames[0], &d->frames[i], sizeof(d->frames[0]) * d->numframes);
365 // (server) clears frame, to prepare for adding entities
366 void EntityFrame_Clear(entity_frame_t *f, vec3_t eye, int framenum)
369 f->framenum = framenum;
377 VectorCopy(eye, f->eye);
381 // (server) adds an entity to frame
382 void EntityFrame_AddEntity(entity_frame_t *f, entity_state_t *s)
384 if (f->numentities < MAX_ENTITY_DATABASE)
386 f->entitydata[f->numentities] = *s;
387 f->entitydata[f->numentities++].active = true;
391 // (server and client) reads a frame from the database
392 void EntityFrame_FetchFrame(entity_database_t *d, int framenum, entity_frame_t *f)
395 EntityFrame_Clear(f, NULL, -1);
396 for (i = 0;i < d->numframes && d->frames[i].framenum < framenum;i++);
397 if (i < d->numframes && framenum == d->frames[i].framenum)
399 f->framenum = framenum;
400 f->numentities = d->frames[i].endentity - d->frames[i].firstentity;
401 n = MAX_ENTITY_DATABASE - (d->frames[i].firstentity % MAX_ENTITY_DATABASE);
402 if (n > f->numentities)
404 memcpy(f->entitydata, d->entitydata + d->frames[i].firstentity % MAX_ENTITY_DATABASE, sizeof(*f->entitydata) * n);
405 if (f->numentities > n)
406 memcpy(f->entitydata + n, d->entitydata, sizeof(*f->entitydata) * (f->numentities - n));
407 VectorCopy(d->eye, f->eye);
411 // (server and client) adds a entity_frame to the database, for future reference
412 void EntityFrame_AddFrame(entity_database_t *d, entity_frame_t *f)
415 entity_frameinfo_t *info;
417 VectorCopy(f->eye, d->eye);
419 // figure out how many entity slots are used already
422 n = d->frames[d->numframes - 1].endentity - d->frames[0].firstentity;
423 if (n + f->numentities > MAX_ENTITY_DATABASE || d->numframes >= MAX_ENTITY_HISTORY)
425 // ran out of room, dump database
426 EntityFrame_ClearDatabase(d);
430 info = &d->frames[d->numframes];
431 info->framenum = f->framenum;
433 // make sure we check the newly added frame as well, but we haven't incremented numframes yet
434 for (n = 0;n <= d->numframes;n++)
436 if (e >= d->frames[n].framenum)
438 if (e == f->framenum)
439 Con_Printf("EntityFrame_AddFrame: tried to add out of sequence frame to database\n");
441 Con_Printf("EntityFrame_AddFrame: out of sequence frames in database\n");
444 e = d->frames[n].framenum;
446 // if database still has frames after that...
448 info->firstentity = d->frames[d->numframes - 1].endentity;
450 info->firstentity = 0;
451 info->endentity = info->firstentity + f->numentities;
454 n = info->firstentity % MAX_ENTITY_DATABASE;
455 e = MAX_ENTITY_DATABASE - n;
456 if (e > f->numentities)
458 memcpy(d->entitydata + n, f->entitydata, sizeof(entity_state_t) * e);
459 if (f->numentities > e)
460 memcpy(d->entitydata, f->entitydata + e, sizeof(entity_state_t) * (f->numentities - e));
463 // (server) writes a frame to network stream
464 static entity_frame_t deltaframe; // FIXME?
465 void EntityFrame_Write(entity_database_t *d, entity_frame_t *f, sizebuf_t *msg)
468 entity_frame_t *o = &deltaframe;
469 entity_state_t *ent, *delta;
471 EntityFrame_AddFrame(d, f);
473 EntityFrame_FetchFrame(d, d->ackframe > 0 ? d->ackframe : -1, o);
474 MSG_WriteByte (msg, svc_entities);
475 MSG_WriteLong (msg, o->framenum);
476 MSG_WriteLong (msg, f->framenum);
477 MSG_WriteFloat (msg, f->eye[0]);
478 MSG_WriteFloat (msg, f->eye[1]);
479 MSG_WriteFloat (msg, f->eye[2]);
482 for (i = 0;i < f->numentities;i++)
484 ent = f->entitydata + i;
485 number = ent->number;
486 for (;onum < o->numentities && o->entitydata[onum].number < number;onum++)
488 // write remove message
489 MSG_WriteShort(msg, o->entitydata[onum].number | 0x8000);
491 if (onum < o->numentities && (o->entitydata[onum].number == number))
493 // delta from previous frame
494 delta = o->entitydata + onum;
495 // advance to next entity in delta frame
500 // delta from defaults
501 delta = &defaultstate;
503 EntityState_Write(ent, msg, delta);
505 for (;onum < o->numentities;onum++)
507 // write remove message
508 MSG_WriteShort(msg, o->entitydata[onum].number | 0x8000);
510 MSG_WriteShort(msg, 0xFFFF);
513 // (client) reads a frame from network stream
514 static entity_frame_t framedata; // FIXME?
515 void EntityFrame_Read(entity_database_t *d)
518 entity_frame_t *f = &framedata, *delta = &deltaframe;
519 entity_state_t *e, *old, *oldend;
521 EntityFrame_Clear(f, NULL, -1);
523 // read the frame header info
524 f->time = cl.mtime[0];
525 number = MSG_ReadLong();
526 f->framenum = MSG_ReadLong();
527 f->eye[0] = MSG_ReadFloat();
528 f->eye[1] = MSG_ReadFloat();
529 f->eye[2] = MSG_ReadFloat();
530 EntityFrame_AckFrame(d, number);
531 EntityFrame_FetchFrame(d, number, delta);
532 old = delta->entitydata;
533 oldend = old + delta->numentities;
534 // read entities until we hit the magic 0xFFFF end tag
535 while ((number = (unsigned short) MSG_ReadShort()) != 0xFFFF)
538 Host_Error("EntityFrame_Read: read error\n");
539 removed = number & 0x8000;
541 if (number >= MAX_EDICTS)
542 Host_Error("EntityFrame_Read: number (%i) >= MAX_EDICTS (%i)\n", number, MAX_EDICTS);
544 // seek to entity, while copying any skipped entities (assume unchanged)
545 while (old < oldend && old->number < number)
547 if (f->numentities >= MAX_ENTITY_DATABASE)
548 Host_Error("EntityFrame_Read: entity list too big\n");
549 f->entitydata[f->numentities] = *old++;
550 f->entitydata[f->numentities++].time = cl.mtime[0];
554 if (old < oldend && old->number == number)
557 Con_Printf("EntityFrame_Read: REMOVE on unused entity %i\n", number);
561 if (f->numentities >= MAX_ENTITY_DATABASE)
562 Host_Error("EntityFrame_Read: entity list too big\n");
565 e = f->entitydata + f->numentities++;
567 if (old < oldend && old->number == number)
569 // delta from old entity
574 // delta from defaults
578 EntityState_ReadUpdate(e, number);
583 if (f->numentities >= MAX_ENTITY_DATABASE)
584 Host_Error("EntityFrame_Read: entity list too big\n");
585 f->entitydata[f->numentities] = *old++;
586 f->entitydata[f->numentities++].time = cl.mtime[0];
588 EntityFrame_AddFrame(d, f);
592 // (client) returns the frame number of the most recent frame recieved
593 int EntityFrame_MostRecentlyRecievedFrameNum(entity_database_t *d)
596 return d->frames[d->numframes - 1].framenum;
606 int EntityFrame4_SV_ChooseCommitToReplace(entity_database4_t *d)
608 int i, best, bestframenum;
610 bestframenum = d->commit[0].framenum;
611 for (i = 0;i < MAX_ENTITY_HISTORY;i++)
613 if (!d->commit[i].numentities)
615 if (bestframenum > d->commit[i].framenum)
617 bestframenum = d->commit[i].framenum;
624 entity_state_t *EntityFrame4_GetReferenceEntity(entity_database4_t *d, int number)
626 if (d->maxreferenceentities <= number)
628 int oldmax = d->maxreferenceentities;
629 entity_state_t *oldentity = d->referenceentity;
630 d->maxreferenceentities = (number + 15) & ~7;
631 d->referenceentity = Mem_Alloc(d->mempool, d->maxreferenceentities * sizeof(*d->referenceentity));
634 memcpy(d->referenceentity, oldentity, oldmax * sizeof(*d->referenceentity));
637 // clear the newly created entities
638 for (;oldmax < d->maxreferenceentities;oldmax++)
640 d->referenceentity[oldmax] = defaultstate;
641 d->referenceentity[oldmax].number = oldmax;
644 return d->referenceentity + number;
647 void EntityFrame4_AddCommitEntity(entity_database4_t *d, entity_state_t *s)
649 // resize commit's entity list if full
650 if (d->currentcommit->maxentities <= d->currentcommit->numentities)
652 entity_state_t *oldentity = d->currentcommit->entity;
653 d->currentcommit->maxentities += 8;
654 d->currentcommit->entity = Mem_Alloc(d->mempool, d->currentcommit->maxentities * sizeof(*d->currentcommit->entity));
657 memcpy(d->currentcommit->entity, oldentity, d->currentcommit->numentities * sizeof(*d->currentcommit->entity));
661 d->currentcommit->entity[d->currentcommit->numentities++] = *s;
664 entity_database4_t *EntityFrame4_AllocDatabase(mempool_t *pool)
666 entity_database4_t *d;
667 d = Mem_Alloc(pool, sizeof(*d));
669 EntityFrame4_ResetDatabase(d);
674 void EntityFrame4_FreeDatabase(entity_database4_t *d)
677 for (i = 0;i < MAX_ENTITY_HISTORY;i++)
678 if (d->commit[i].entity)
679 Mem_Free(d->commit[i].entity);
680 if (d->referenceentity)
681 Mem_Free(d->referenceentity);
685 void EntityFrame4_ResetDatabase(entity_database4_t *d)
688 d->referenceframenum = -1;
689 for (i = 0;i < MAX_ENTITY_HISTORY;i++)
690 d->commit[i].numentities = 0;
691 for (i = 0;i < d->maxreferenceentities;i++)
692 d->referenceentity[i] = defaultstate;
695 void EntityFrame4_AckFrame(entity_database4_t *d, int framenum)
698 entity_database4_commit_t *commit;
699 // check if the peer is requesting no delta compression
702 EntityFrame4_ResetDatabase(d);
705 // return early if this is already the reference frame
706 if (d->referenceframenum == framenum)
708 // find the frame in the database
709 for (i = 0, commit = d->commit;i < MAX_ENTITY_HISTORY;i++, commit++)
710 if (commit->numentities && commit->framenum == framenum)
712 // check if the frame was found
713 if (i == MAX_ENTITY_HISTORY)
715 Con_Printf("EntityFrame4_AckFrame: frame %i not found in database, expect glitches! (VERY BAD ERROR)\n", framenum);
717 EntityFrame4_ResetDatabase(d);
720 // apply commit to database
721 d->referenceframenum = framenum;
722 for (j = 0;j < commit->numentities;j++)
724 //*EntityFrame4_GetReferenceEntity(d, commit->entity[j].number) = commit->entity[j];
725 entity_state_t *s = EntityFrame4_GetReferenceEntity(d, commit->entity[j].number);
726 if (commit->entity[j].active != s->active)
728 if (commit->entity[j].active)
729 Con_Printf("commit entity %i has become active (modelindex %i)\n", commit->entity[j].number, commit->entity[j].modelindex);
731 Con_Printf("commit entity %i has become inactive (modelindex %i)\n", commit->entity[j].number, commit->entity[j].modelindex);
733 *s = commit->entity[j];
735 // purge the now-obsolete updates
736 for (i = 0;i < MAX_ENTITY_HISTORY;i++)
737 if (d->commit[i].framenum <= framenum)
738 d->commit[i].numentities = 0;
741 void EntityFrame4_SV_WriteFrame_Begin(entity_database4_t *d, sizebuf_t *msg, int framenum)
743 d->currentcommit = d->commit + EntityFrame4_SV_ChooseCommitToReplace(d);
744 d->currentcommit->numentities = 0;
745 d->currentcommit->framenum = framenum;
746 MSG_WriteByte(msg, svc_entities);
747 MSG_WriteLong(msg, d->referenceframenum);
748 MSG_WriteLong(msg, d->currentcommit->framenum);
751 int EntityFrame4_SV_WriteFrame_Entity(entity_database4_t *d, sizebuf_t *msg, int maxbytes, entity_state_t *s)
756 // prepare the buffer
757 memset(&buf, 0, sizeof(buf));
759 buf.maxsize = sizeof(data);
761 e = EntityFrame4_GetReferenceEntity(d, s->number);
762 // send an update (may update or remove the entity)
763 EntityState_Write(s, &buf, e);
764 // if the message is empty, skip out now
767 // if the commit is full, we're done
768 if (msg->cursize + buf.cursize + 2 >= min(msg->maxsize, maxbytes))
770 // add the entity to the commit
771 EntityFrame4_AddCommitEntity(d, s);
772 // write the message to the packet
773 SZ_Write(msg, buf.data, buf.cursize);
778 void EntityFrame4_SV_WriteFrame_End(entity_database4_t *d, sizebuf_t *msg)
780 // remove world message (invalid, and thus a good terminator)
781 MSG_WriteShort(msg, 0x8000);
783 d->currentcommit = NULL;
786 extern void CL_MoveLerpEntityStates(entity_t *ent);
787 void EntityFrame4_CL_ReadFrame(entity_database4_t *d)
789 int i, n, cnumber, referenceframenum, framenum, enumber, done, stopnumber, skip = false;
790 // read the number of the frame this refers to
791 referenceframenum = MSG_ReadLong();
792 // read the number of this frame
793 framenum = MSG_ReadLong();
794 // read the start number
795 enumber = MSG_ReadShort();
796 for (i = 0;i < MAX_ENTITY_HISTORY;i++)
797 if (!d->commit[i].numentities)
799 if (i < MAX_ENTITY_HISTORY)
801 d->currentcommit = d->commit + i;
802 d->currentcommit->framenum = d->ackframenum = framenum;
803 d->currentcommit->numentities = 0;
804 EntityFrame4_AckFrame(d, referenceframenum);
805 if (d->ackframenum == -1)
807 Con_Printf("EntityFrame4_CL_ReadFrame: reference frame invalid, this update will be skipped\n");
813 Con_Printf("EntityFrame4_CL_ReadFrame: error while decoding frame %i: database full, reading but not storing this update\n", framenum);
817 while (!done && !msg_badread)
819 // read the number of the modified entity
820 // (gaps will be copied unmodified)
821 n = (unsigned short)MSG_ReadShort();
824 // no more entities in this update, but we still need to copy the
825 // rest of the reference entities (final gap)
827 // read end of range number, then process normally
828 n = (unsigned short)MSG_ReadShort();
830 // high bit means it's a remove message
831 cnumber = n & 0x7FFF;
832 // add one (the changed one) if not done
833 stopnumber = cnumber + !done;
834 // process entities in range from the last one to the changed one
835 for (;enumber < stopnumber;enumber++)
839 if (enumber == cnumber && (n & 0x8000) == 0)
841 entity_state_t tempstate;
842 EntityState_ReadUpdate(&tempstate, enumber);
846 // slide the current into the previous slot
847 cl_entities[enumber].state_previous = cl_entities[enumber].state_current;
848 // copy a new current from reference database
849 cl_entities[enumber].state_current = *EntityFrame4_GetReferenceEntity(d, enumber);
850 // if this is the one to modify, read more data...
851 if (enumber == cnumber)
856 if (developer_networkentities.integer)
857 Con_Printf("entity %i: remove\n", enumber);
858 cl_entities[enumber].state_current = defaultstate;
863 if (developer_networkentities.integer)
864 Con_Printf("entity %i: update\n", enumber);
865 EntityState_ReadUpdate(&cl_entities[enumber].state_current, enumber);
868 //else if (developer_networkentities.integer)
869 // Con_Printf("entity %i: copy\n", enumber);
870 // fix the number (it gets wiped occasionally by copying from defaultstate)
871 cl_entities[enumber].state_current.number = enumber;
872 // check if we need to update the lerp stuff
873 if (cl_entities[enumber].state_current.active)
875 CL_MoveLerpEntityStates(&cl_entities[enumber]);
876 cl_entities_active[enumber] = true;
878 // add this to the commit entry whether it is modified or not
879 if (d->currentcommit)
880 EntityFrame4_AddCommitEntity(d, &cl_entities[enumber].state_current);
881 // print extra messages if desired
882 if (developer_networkentities.integer && cl_entities[enumber].state_current.active != cl_entities[enumber].state_previous.active)
884 if (cl_entities[enumber].state_current.active)
885 Con_Printf("entity #%i has become active\n", enumber);
886 else if (cl_entities[enumber].state_previous.active)
887 Con_Printf("entity #%i has become inactive\n", enumber);
891 d->currentcommit = NULL;