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]);
167 MSG_WriteAngle(msg, ent->angles[0]);
169 MSG_WriteAngle(msg, ent->angles[1]);
171 MSG_WriteAngle(msg, ent->angles[2]);
175 if (bits & E_ORIGIN1)
176 MSG_WriteFloat(msg, org[0]);
177 if (bits & E_ORIGIN2)
178 MSG_WriteFloat(msg, org[1]);
179 if (bits & E_ORIGIN3)
180 MSG_WriteFloat(msg, org[2]);
182 MSG_WritePreciseAngle(msg, ent->angles[0]);
184 MSG_WritePreciseAngle(msg, ent->angles[1]);
186 MSG_WritePreciseAngle(msg, ent->angles[2]);
189 MSG_WriteByte(msg, ent->modelindex & 0xFF);
191 MSG_WriteByte(msg, (ent->modelindex >> 8) & 0xFF);
193 MSG_WriteByte(msg, ent->frame & 0xFF);
195 MSG_WriteByte(msg, (ent->frame >> 8) & 0xFF);
196 if (bits & E_EFFECTS1)
197 MSG_WriteByte(msg, ent->effects & 0xFF);
198 if (bits & E_EFFECTS2)
199 MSG_WriteByte(msg, (ent->effects >> 8) & 0xFF);
200 if (bits & E_COLORMAP)
201 MSG_WriteByte(msg, ent->colormap);
203 MSG_WriteByte(msg, ent->skin);
205 MSG_WriteByte(msg, ent->alpha);
207 MSG_WriteByte(msg, ent->scale);
208 if (bits & E_GLOWSIZE)
209 MSG_WriteByte(msg, ent->glowsize);
210 if (bits & E_GLOWCOLOR)
211 MSG_WriteByte(msg, ent->glowcolor);
212 if (bits & E_TAGATTACHMENT)
214 MSG_WriteShort(msg, ent->tagentity);
215 MSG_WriteByte(msg, ent->tagindex);
219 MSG_WriteShort(msg, ent->light[0]);
220 MSG_WriteShort(msg, ent->light[1]);
221 MSG_WriteShort(msg, ent->light[2]);
222 MSG_WriteShort(msg, ent->light[3]);
224 if (bits & E_LIGHTSTYLE)
225 MSG_WriteByte(msg, ent->lightstyle);
226 if (bits & E_LIGHTPFLAGS)
227 MSG_WriteByte(msg, ent->lightpflags);
230 else if (delta->active)
231 MSG_WriteShort(msg, ent->number | 0x8000);
234 void EntityState_ReadUpdate(entity_state_t *e, int number)
237 cl_entities_active[number] = true;
239 e->time = cl.mtime[0];
242 bits = MSG_ReadByte();
243 if (bits & E_EXTEND1)
245 bits |= MSG_ReadByte() << 8;
246 if (bits & E_EXTEND2)
248 bits |= MSG_ReadByte() << 16;
249 if (bits & E_EXTEND3)
250 bits |= MSG_ReadByte() << 24;
254 if (cl.protocol == PROTOCOL_DARKPLACES2)
256 if (bits & E_ORIGIN1)
257 e->origin[0] = (signed short) MSG_ReadShort();
258 if (bits & E_ORIGIN2)
259 e->origin[1] = (signed short) MSG_ReadShort();
260 if (bits & E_ORIGIN3)
261 e->origin[2] = (signed short) MSG_ReadShort();
266 e->flags = MSG_ReadByte();
267 if (e->flags & RENDER_LOWPRECISION || cl.protocol == PROTOCOL_DARKPLACES2)
269 if (bits & E_ORIGIN1)
270 e->origin[0] = (signed short) MSG_ReadShort();
271 if (bits & E_ORIGIN2)
272 e->origin[1] = (signed short) MSG_ReadShort();
273 if (bits & E_ORIGIN3)
274 e->origin[2] = (signed short) MSG_ReadShort();
278 if (bits & E_ORIGIN1)
279 e->origin[0] = MSG_ReadFloat();
280 if (bits & E_ORIGIN2)
281 e->origin[1] = MSG_ReadFloat();
282 if (bits & E_ORIGIN3)
283 e->origin[2] = MSG_ReadFloat();
286 if (cl.protocol == PROTOCOL_DARKPLACES5 && !(e->flags & RENDER_LOWPRECISION))
289 e->angles[0] = MSG_ReadPreciseAngle();
291 e->angles[1] = MSG_ReadPreciseAngle();
293 e->angles[2] = MSG_ReadPreciseAngle();
298 e->angles[0] = MSG_ReadAngle();
300 e->angles[1] = MSG_ReadAngle();
302 e->angles[2] = MSG_ReadAngle();
305 e->modelindex = (e->modelindex & 0xFF00) | (unsigned int) MSG_ReadByte();
307 e->modelindex = (e->modelindex & 0x00FF) | ((unsigned int) MSG_ReadByte() << 8);
309 e->frame = (e->frame & 0xFF00) | (unsigned int) MSG_ReadByte();
311 e->frame = (e->frame & 0x00FF) | ((unsigned int) MSG_ReadByte() << 8);
312 if (bits & E_EFFECTS1)
313 e->effects = (e->effects & 0xFF00) | (unsigned int) MSG_ReadByte();
314 if (bits & E_EFFECTS2)
315 e->effects = (e->effects & 0x00FF) | ((unsigned int) MSG_ReadByte() << 8);
316 if (bits & E_COLORMAP)
317 e->colormap = MSG_ReadByte();
319 e->skin = MSG_ReadByte();
321 e->alpha = MSG_ReadByte();
323 e->scale = MSG_ReadByte();
324 if (bits & E_GLOWSIZE)
325 e->glowsize = MSG_ReadByte();
326 if (bits & E_GLOWCOLOR)
327 e->glowcolor = MSG_ReadByte();
328 if (cl.protocol == PROTOCOL_DARKPLACES2)
330 e->flags = MSG_ReadByte();
331 if (bits & E_TAGATTACHMENT)
333 e->tagentity = MSG_ReadShort();
334 e->tagindex = MSG_ReadByte();
338 e->light[0] = MSG_ReadShort();
339 e->light[1] = MSG_ReadShort();
340 e->light[2] = MSG_ReadShort();
341 e->light[3] = MSG_ReadShort();
343 if (bits & E_LIGHTSTYLE)
344 e->lightstyle = MSG_ReadByte();
345 if (bits & E_LIGHTPFLAGS)
346 e->lightpflags = MSG_ReadByte();
348 if (developer_networkentities.integer >= 2)
350 Con_Printf("ReadUpdate e%i", number);
352 if (bits & E_ORIGIN1)
353 Con_Printf(" E_ORIGIN1 %f", e->origin[0]);
354 if (bits & E_ORIGIN2)
355 Con_Printf(" E_ORIGIN2 %f", e->origin[1]);
356 if (bits & E_ORIGIN3)
357 Con_Printf(" E_ORIGIN3 %f", e->origin[2]);
359 Con_Printf(" E_ANGLE1 %f", e->angles[0]);
361 Con_Printf(" E_ANGLE2 %f", e->angles[1]);
363 Con_Printf(" E_ANGLE3 %f", e->angles[2]);
364 if (bits & (E_MODEL1 | E_MODEL2))
365 Con_Printf(" E_MODEL %i", e->modelindex);
367 if (bits & (E_FRAME1 | E_FRAME2))
368 Con_Printf(" E_FRAME %i", e->frame);
369 if (bits & (E_EFFECTS1 | E_EFFECTS2))
370 Con_Printf(" E_EFFECTS %i", e->effects);
372 Con_Printf(" E_ALPHA %f", e->alpha / 255.0f);
374 Con_Printf(" E_SCALE %f", e->scale / 16.0f);
375 if (bits & E_COLORMAP)
376 Con_Printf(" E_COLORMAP %i", e->colormap);
378 Con_Printf(" E_SKIN %i", e->skin);
380 if (bits & E_GLOWSIZE)
381 Con_Printf(" E_GLOWSIZE %i", e->glowsize * 4);
382 if (bits & E_GLOWCOLOR)
383 Con_Printf(" E_GLOWCOLOR %i", e->glowcolor);
386 Con_Printf(" E_LIGHT %i:%i:%i:%i", e->light[0], e->light[1], e->light[2], e->light[3]);
387 if (bits & E_LIGHTPFLAGS)
388 Con_Printf(" E_LIGHTPFLAGS %i", e->lightpflags);
390 if (bits & E_TAGATTACHMENT)
391 Con_Printf(" E_TAGATTACHMENT e%i:%i", e->tagentity, e->tagindex);
392 if (bits & E_LIGHTSTYLE)
393 Con_Printf(" E_LIGHTSTYLE %i", e->lightstyle);
398 // (server) clears the database to contain no frames (thus delta compression compresses against nothing)
399 void EntityFrame_ClearDatabase(entity_database_t *d)
401 memset(d, 0, sizeof(*d));
404 // (server and client) removes frames older than 'frame' from database
405 void EntityFrame_AckFrame(entity_database_t *d, int frame)
408 if (d->ackframe < frame)
410 for (i = 0;i < d->numframes && d->frames[i].framenum < frame;i++);
411 // ignore outdated frame acks (out of order packets)
415 // if some queue is left, slide it down to beginning of array
417 memmove(&d->frames[0], &d->frames[i], sizeof(d->frames[0]) * d->numframes);
420 // (server) clears frame, to prepare for adding entities
421 void EntityFrame_Clear(entity_frame_t *f, vec3_t eye, int framenum)
424 f->framenum = framenum;
432 VectorCopy(eye, f->eye);
436 // (server) adds an entity to frame
437 void EntityFrame_AddEntity(entity_frame_t *f, entity_state_t *s)
439 if (f->numentities < MAX_ENTITY_DATABASE)
441 f->entitydata[f->numentities] = *s;
442 f->entitydata[f->numentities++].active = true;
446 // (server and client) reads a frame from the database
447 void EntityFrame_FetchFrame(entity_database_t *d, int framenum, entity_frame_t *f)
450 EntityFrame_Clear(f, NULL, -1);
451 for (i = 0;i < d->numframes && d->frames[i].framenum < framenum;i++);
452 if (i < d->numframes && framenum == d->frames[i].framenum)
454 f->framenum = framenum;
455 f->numentities = d->frames[i].endentity - d->frames[i].firstentity;
456 n = MAX_ENTITY_DATABASE - (d->frames[i].firstentity % MAX_ENTITY_DATABASE);
457 if (n > f->numentities)
459 memcpy(f->entitydata, d->entitydata + d->frames[i].firstentity % MAX_ENTITY_DATABASE, sizeof(*f->entitydata) * n);
460 if (f->numentities > n)
461 memcpy(f->entitydata + n, d->entitydata, sizeof(*f->entitydata) * (f->numentities - n));
462 VectorCopy(d->eye, f->eye);
466 // (server and client) adds a entity_frame to the database, for future reference
467 void EntityFrame_AddFrame(entity_database_t *d, entity_frame_t *f)
470 entity_frameinfo_t *info;
472 VectorCopy(f->eye, d->eye);
474 // figure out how many entity slots are used already
477 n = d->frames[d->numframes - 1].endentity - d->frames[0].firstentity;
478 if (n + f->numentities > MAX_ENTITY_DATABASE || d->numframes >= MAX_ENTITY_HISTORY)
480 // ran out of room, dump database
481 EntityFrame_ClearDatabase(d);
485 info = &d->frames[d->numframes];
486 info->framenum = f->framenum;
488 // make sure we check the newly added frame as well, but we haven't incremented numframes yet
489 for (n = 0;n <= d->numframes;n++)
491 if (e >= d->frames[n].framenum)
493 if (e == f->framenum)
494 Con_Print("EntityFrame_AddFrame: tried to add out of sequence frame to database\n");
496 Con_Print("EntityFrame_AddFrame: out of sequence frames in database\n");
499 e = d->frames[n].framenum;
501 // if database still has frames after that...
503 info->firstentity = d->frames[d->numframes - 1].endentity;
505 info->firstentity = 0;
506 info->endentity = info->firstentity + f->numentities;
509 n = info->firstentity % MAX_ENTITY_DATABASE;
510 e = MAX_ENTITY_DATABASE - n;
511 if (e > f->numentities)
513 memcpy(d->entitydata + n, f->entitydata, sizeof(entity_state_t) * e);
514 if (f->numentities > e)
515 memcpy(d->entitydata, f->entitydata + e, sizeof(entity_state_t) * (f->numentities - e));
518 // (server) writes a frame to network stream
519 static entity_frame_t deltaframe; // FIXME?
520 void EntityFrame_Write(entity_database_t *d, entity_frame_t *f, sizebuf_t *msg)
523 entity_frame_t *o = &deltaframe;
524 entity_state_t *ent, *delta;
526 EntityFrame_AddFrame(d, f);
528 EntityFrame_FetchFrame(d, d->ackframe > 0 ? d->ackframe : -1, o);
529 MSG_WriteByte (msg, svc_entities);
530 MSG_WriteLong (msg, o->framenum);
531 MSG_WriteLong (msg, f->framenum);
532 MSG_WriteFloat (msg, f->eye[0]);
533 MSG_WriteFloat (msg, f->eye[1]);
534 MSG_WriteFloat (msg, f->eye[2]);
537 for (i = 0;i < f->numentities;i++)
539 ent = f->entitydata + i;
540 number = ent->number;
541 for (;onum < o->numentities && o->entitydata[onum].number < number;onum++)
543 // write remove message
544 MSG_WriteShort(msg, o->entitydata[onum].number | 0x8000);
546 if (onum < o->numentities && (o->entitydata[onum].number == number))
548 // delta from previous frame
549 delta = o->entitydata + onum;
550 // advance to next entity in delta frame
555 // delta from defaults
556 delta = &defaultstate;
558 EntityState_Write(ent, msg, delta);
560 for (;onum < o->numentities;onum++)
562 // write remove message
563 MSG_WriteShort(msg, o->entitydata[onum].number | 0x8000);
565 MSG_WriteShort(msg, 0xFFFF);
568 // (client) reads a frame from network stream
569 static entity_frame_t framedata; // FIXME?
570 void EntityFrame_Read(entity_database_t *d)
573 entity_frame_t *f = &framedata, *delta = &deltaframe;
574 entity_state_t *e, *old, *oldend;
576 EntityFrame_Clear(f, NULL, -1);
578 // read the frame header info
579 f->time = cl.mtime[0];
580 number = MSG_ReadLong();
581 f->framenum = MSG_ReadLong();
582 f->eye[0] = MSG_ReadFloat();
583 f->eye[1] = MSG_ReadFloat();
584 f->eye[2] = MSG_ReadFloat();
585 EntityFrame_AckFrame(d, number);
586 EntityFrame_FetchFrame(d, number, delta);
587 old = delta->entitydata;
588 oldend = old + delta->numentities;
589 // read entities until we hit the magic 0xFFFF end tag
590 while ((number = (unsigned short) MSG_ReadShort()) != 0xFFFF)
593 Host_Error("EntityFrame_Read: read error\n");
594 removed = number & 0x8000;
596 if (number >= MAX_EDICTS)
597 Host_Error("EntityFrame_Read: number (%i) >= MAX_EDICTS (%i)\n", number, MAX_EDICTS);
599 // seek to entity, while copying any skipped entities (assume unchanged)
600 while (old < oldend && old->number < number)
602 if (f->numentities >= MAX_ENTITY_DATABASE)
603 Host_Error("EntityFrame_Read: entity list too big\n");
604 f->entitydata[f->numentities] = *old++;
605 f->entitydata[f->numentities++].time = cl.mtime[0];
609 if (old < oldend && old->number == number)
612 Con_Printf("EntityFrame_Read: REMOVE on unused entity %i\n", number);
616 if (f->numentities >= MAX_ENTITY_DATABASE)
617 Host_Error("EntityFrame_Read: entity list too big\n");
620 e = f->entitydata + f->numentities++;
622 if (old < oldend && old->number == number)
624 // delta from old entity
629 // delta from defaults
633 EntityState_ReadUpdate(e, number);
638 if (f->numentities >= MAX_ENTITY_DATABASE)
639 Host_Error("EntityFrame_Read: entity list too big\n");
640 f->entitydata[f->numentities] = *old++;
641 f->entitydata[f->numentities++].time = cl.mtime[0];
643 EntityFrame_AddFrame(d, f);
647 // (client) returns the frame number of the most recent frame recieved
648 int EntityFrame_MostRecentlyRecievedFrameNum(entity_database_t *d)
651 return d->frames[d->numframes - 1].framenum;
661 entity_state_t *EntityFrame4_GetReferenceEntity(entity_database4_t *d, int number)
663 if (d->maxreferenceentities <= number)
665 int oldmax = d->maxreferenceentities;
666 entity_state_t *oldentity = d->referenceentity;
667 d->maxreferenceentities = (number + 15) & ~7;
668 d->referenceentity = Mem_Alloc(d->mempool, d->maxreferenceentities * sizeof(*d->referenceentity));
671 memcpy(d->referenceentity, oldentity, oldmax * sizeof(*d->referenceentity));
674 // clear the newly created entities
675 for (;oldmax < d->maxreferenceentities;oldmax++)
677 d->referenceentity[oldmax] = defaultstate;
678 d->referenceentity[oldmax].number = oldmax;
681 return d->referenceentity + number;
684 void EntityFrame4_AddCommitEntity(entity_database4_t *d, entity_state_t *s)
686 // resize commit's entity list if full
687 if (d->currentcommit->maxentities <= d->currentcommit->numentities)
689 entity_state_t *oldentity = d->currentcommit->entity;
690 d->currentcommit->maxentities += 8;
691 d->currentcommit->entity = Mem_Alloc(d->mempool, d->currentcommit->maxentities * sizeof(*d->currentcommit->entity));
694 memcpy(d->currentcommit->entity, oldentity, d->currentcommit->numentities * sizeof(*d->currentcommit->entity));
698 d->currentcommit->entity[d->currentcommit->numentities++] = *s;
701 entity_database4_t *EntityFrame4_AllocDatabase(mempool_t *pool)
703 entity_database4_t *d;
704 d = Mem_Alloc(pool, sizeof(*d));
706 EntityFrame4_ResetDatabase(d);
711 void EntityFrame4_FreeDatabase(entity_database4_t *d)
714 for (i = 0;i < MAX_ENTITY_HISTORY;i++)
715 if (d->commit[i].entity)
716 Mem_Free(d->commit[i].entity);
717 if (d->referenceentity)
718 Mem_Free(d->referenceentity);
722 void EntityFrame4_ResetDatabase(entity_database4_t *d)
725 d->referenceframenum = -1;
726 for (i = 0;i < MAX_ENTITY_HISTORY;i++)
727 d->commit[i].numentities = 0;
728 for (i = 0;i < d->maxreferenceentities;i++)
729 d->referenceentity[i] = defaultstate;
732 int EntityFrame4_AckFrame(entity_database4_t *d, int framenum)
734 int i, j, found = false;
735 entity_database4_commit_t *commit;
738 EntityFrame4_ResetDatabase(d);
741 if (d->referenceframenum == framenum)
743 for (i = 0, commit = d->commit;i < MAX_ENTITY_HISTORY;i++, commit++)
745 if (commit->numentities && commit->framenum <= framenum)
747 if (commit->framenum == framenum)
750 d->referenceframenum = framenum;
751 if (developer_networkentities.integer >= 3)
753 for (j = 0;j < commit->numentities;j++)
755 entity_state_t *s = EntityFrame4_GetReferenceEntity(d, commit->entity[j].number);
756 if (commit->entity[j].active != s->active)
758 if (commit->entity[j].active)
759 Con_Printf("commit entity %i has become active (modelindex %i)\n", commit->entity[j].number, commit->entity[j].modelindex);
761 Con_Printf("commit entity %i has become inactive (modelindex %i)\n", commit->entity[j].number, commit->entity[j].modelindex);
763 *s = commit->entity[j];
767 for (j = 0;j < commit->numentities;j++)
768 *EntityFrame4_GetReferenceEntity(d, commit->entity[j].number) = commit->entity[j];
770 commit->numentities = 0;
776 int EntityFrame4_SV_WriteFrame_Entity(entity_database4_t *d, sizebuf_t *msg, int maxbytes, entity_state_t *s)
781 // prepare the buffer
782 memset(&buf, 0, sizeof(buf));
784 buf.maxsize = sizeof(data);
786 e = EntityFrame4_GetReferenceEntity(d, s->number);
787 // send an update (may update or remove the entity)
788 EntityState_Write(s, &buf, e);
789 // if the message is empty, skip out now
792 // if the commit is full, we're done
793 if (msg->cursize + buf.cursize + 2 >= min(msg->maxsize, maxbytes))
795 // add the entity to the commit
796 EntityFrame4_AddCommitEntity(d, s);
797 // write the message to the packet
798 SZ_Write(msg, buf.data, buf.cursize);
803 extern void CL_MoveLerpEntityStates(entity_t *ent);
804 void EntityFrame4_CL_ReadFrame(entity_database4_t *d)
806 int i, n, cnumber, referenceframenum, framenum, enumber, done, stopnumber, skip = false;
807 // read the number of the frame this refers to
808 referenceframenum = MSG_ReadLong();
809 // read the number of this frame
810 framenum = MSG_ReadLong();
811 // read the start number
812 enumber = MSG_ReadShort();
813 if (developer_networkentities.integer >= 1)
815 Con_Printf("recv svc_entities ref:%i num:%i (database: ref:%i commits:", referenceframenum, framenum, d->referenceframenum);
816 for (i = 0;i < MAX_ENTITY_HISTORY;i++)
817 if (d->commit[i].numentities)
818 Con_Printf(" %i", d->commit[i].framenum);
821 if (!EntityFrame4_AckFrame(d, referenceframenum))
823 Con_Print("EntityFrame4_CL_ReadFrame: reference frame invalid (VERY BAD ERROR), this update will be skipped\n");
826 EntityFrame4_ResetDatabase(d);
828 d->currentcommit = NULL;
829 for (i = 0;i < MAX_ENTITY_HISTORY;i++)
831 if (!d->commit[i].numentities)
833 d->currentcommit = d->commit + i;
834 d->currentcommit->framenum = d->ackframenum = framenum;
835 d->currentcommit->numentities = 0;
838 if (d->currentcommit == NULL)
840 Con_Printf("EntityFrame4_CL_ReadFrame: error while decoding frame %i: database full, reading but not storing this update\n", framenum);
844 while (!done && !msg_badread)
846 // read the number of the modified entity
847 // (gaps will be copied unmodified)
848 n = (unsigned short)MSG_ReadShort();
851 // no more entities in this update, but we still need to copy the
852 // rest of the reference entities (final gap)
854 // read end of range number, then process normally
855 n = (unsigned short)MSG_ReadShort();
857 // high bit means it's a remove message
858 cnumber = n & 0x7FFF;
859 // add one (the changed one) if not done
860 stopnumber = cnumber + !done;
861 // process entities in range from the last one to the changed one
862 for (;enumber < stopnumber;enumber++)
866 if (enumber == cnumber && (n & 0x8000) == 0)
868 entity_state_t tempstate;
869 EntityState_ReadUpdate(&tempstate, enumber);
873 // slide the current into the previous slot
874 cl_entities[enumber].state_previous = cl_entities[enumber].state_current;
875 // copy a new current from reference database
876 cl_entities[enumber].state_current = *EntityFrame4_GetReferenceEntity(d, enumber);
877 // if this is the one to modify, read more data...
878 if (enumber == cnumber)
883 if (developer_networkentities.integer >= 2)
884 Con_Printf("entity %i: remove\n", enumber);
885 cl_entities[enumber].state_current = defaultstate;
890 if (developer_networkentities.integer >= 2)
891 Con_Printf("entity %i: update\n", enumber);
892 EntityState_ReadUpdate(&cl_entities[enumber].state_current, enumber);
895 else if (developer_networkentities.integer >= 4)
896 Con_Printf("entity %i: copy\n", enumber);
897 // fix the number (it gets wiped occasionally by copying from defaultstate)
898 cl_entities[enumber].state_current.number = enumber;
899 // check if we need to update the lerp stuff
900 if (cl_entities[enumber].state_current.active)
902 CL_MoveLerpEntityStates(&cl_entities[enumber]);
903 cl_entities_active[enumber] = true;
905 // add this to the commit entry whether it is modified or not
906 if (d->currentcommit)
907 EntityFrame4_AddCommitEntity(d, &cl_entities[enumber].state_current);
908 // print extra messages if desired
909 if (developer_networkentities.integer >= 2 && cl_entities[enumber].state_current.active != cl_entities[enumber].state_previous.active)
911 if (cl_entities[enumber].state_current.active)
912 Con_Printf("entity #%i has become active\n", enumber);
913 else if (cl_entities[enumber].state_previous.active)
914 Con_Printf("entity #%i has become inactive\n", enumber);
918 d->currentcommit = NULL;