5 entity_state_t defaultstate =
7 // ! means this is sent to client
8 0,//double time; // time this state was built (used on client for interpolation)
9 {0,0,0},//float origin[3]; // !
10 {0,0,0},//float angles[3]; // !
11 0,//int number; // ! entity number this state is for
13 0,//unsigned short modelindex; // !
14 0,//unsigned short frame; // !
15 0,//unsigned short tagentity; // !
16 0,//unsigned short specialvisibilityradius; // larger if it has effects/light
17 0,//unsigned short viewmodelforclient;
18 0,//unsigned short exteriormodelforclient; // not shown if first person viewing from this entity, shown in all other cases
19 0,//unsigned short nodrawtoclient;
20 0,//unsigned short drawonlytoclient;
21 {0,0,0,0},//unsigned short light[4]; // ! color*256 (0.00 to 255.996), and radius*1
22 0,//unsigned char active; // ! true if a valid state
23 0,//unsigned char lightstyle; // !
24 0,//unsigned char lightpflags; // !
25 0,//unsigned char colormap; // !
26 0,//unsigned char skin; // ! also chooses cubemap for rtlights if lightpflags & LIGHTPFLAGS_FULLDYNAMIC
27 255,//unsigned char alpha; // !
28 16,//unsigned char scale; // !
29 0,//unsigned char glowsize; // !
30 254,//unsigned char glowcolor; // !
31 0,//unsigned char flags; // !
32 0,//unsigned char tagindex; // !
33 // padding to a multiple of 8 bytes (to align the double time)
34 {0,0,0,0,0}//unsigned char unused[5];
37 void ClearStateToDefault(entity_state_t *s)
42 int EntityState_DeltaBits(const entity_state_t *o, const entity_state_t *n)
45 // if o is not active, delta from default
49 if (fabs(n->origin[0] - o->origin[0]) > (1.0f / 256.0f))
51 if (fabs(n->origin[1] - o->origin[1]) > (1.0f / 256.0f))
53 if (fabs(n->origin[2] - o->origin[2]) > (1.0f / 256.0f))
55 if ((qbyte) (n->angles[0] * (256.0f / 360.0f)) != (qbyte) (o->angles[0] * (256.0f / 360.0f)))
57 if ((qbyte) (n->angles[1] * (256.0f / 360.0f)) != (qbyte) (o->angles[1] * (256.0f / 360.0f)))
59 if ((qbyte) (n->angles[2] * (256.0f / 360.0f)) != (qbyte) (o->angles[2] * (256.0f / 360.0f)))
61 if ((n->modelindex ^ o->modelindex) & 0x00FF)
63 if ((n->modelindex ^ o->modelindex) & 0xFF00)
65 if ((n->frame ^ o->frame) & 0x00FF)
67 if ((n->frame ^ o->frame) & 0xFF00)
69 if ((n->effects ^ o->effects) & 0x00FF)
71 if ((n->effects ^ o->effects) & 0xFF00)
73 if (n->colormap != o->colormap)
75 if (n->skin != o->skin)
77 if (n->alpha != o->alpha)
79 if (n->scale != o->scale)
81 if (n->glowsize != o->glowsize)
83 if (n->glowcolor != o->glowcolor)
85 if (n->flags != o->flags)
87 if (n->tagindex != o->tagindex || n->tagentity != o->tagentity)
88 bits |= E_TAGATTACHMENT;
89 if (n->light[0] != o->light[0] || n->light[1] != o->light[1] || n->light[2] != o->light[2] || n->light[3] != o->light[3])
91 if (n->lightstyle != o->lightstyle)
93 if (n->lightpflags != o->lightpflags)
94 bits |= E_LIGHTPFLAGS;
98 if (bits & 0xFF000000)
100 if (bits & 0x00FF0000)
102 if (bits & 0x0000FF00)
108 void EntityState_WriteExtendBits(sizebuf_t *msg, unsigned int bits)
110 MSG_WriteByte(msg, bits & 0xFF);
111 if (bits & 0x00000080)
113 MSG_WriteByte(msg, (bits >> 8) & 0xFF);
114 if (bits & 0x00008000)
116 MSG_WriteByte(msg, (bits >> 16) & 0xFF);
117 if (bits & 0x00800000)
118 MSG_WriteByte(msg, (bits >> 24) & 0xFF);
123 void EntityState_WriteFields(entity_state_t *ent, sizebuf_t *msg, unsigned int bits)
125 // LordHavoc: have to write flags first, as they can modify protocol
127 MSG_WriteByte(msg, ent->flags);
128 if (ent->flags & RENDER_LOWPRECISION)
130 if (bits & E_ORIGIN1)
131 MSG_WriteShort(msg, ent->origin[0]);
132 if (bits & E_ORIGIN2)
133 MSG_WriteShort(msg, ent->origin[1]);
134 if (bits & E_ORIGIN3)
135 MSG_WriteShort(msg, ent->origin[2]);
137 MSG_WriteAngle(msg, ent->angles[0]);
139 MSG_WriteAngle(msg, ent->angles[1]);
141 MSG_WriteAngle(msg, ent->angles[2]);
145 if (bits & E_ORIGIN1)
146 MSG_WriteFloat(msg, ent->origin[0]);
147 if (bits & E_ORIGIN2)
148 MSG_WriteFloat(msg, ent->origin[1]);
149 if (bits & E_ORIGIN3)
150 MSG_WriteFloat(msg, ent->origin[2]);
152 MSG_WritePreciseAngle(msg, ent->angles[0]);
154 MSG_WritePreciseAngle(msg, ent->angles[1]);
156 MSG_WritePreciseAngle(msg, ent->angles[2]);
159 MSG_WriteByte(msg, ent->modelindex & 0xFF);
161 MSG_WriteByte(msg, (ent->modelindex >> 8) & 0xFF);
163 MSG_WriteByte(msg, ent->frame & 0xFF);
165 MSG_WriteByte(msg, (ent->frame >> 8) & 0xFF);
166 if (bits & E_EFFECTS1)
167 MSG_WriteByte(msg, ent->effects & 0xFF);
168 if (bits & E_EFFECTS2)
169 MSG_WriteByte(msg, (ent->effects >> 8) & 0xFF);
170 if (bits & E_COLORMAP)
171 MSG_WriteByte(msg, ent->colormap);
173 MSG_WriteByte(msg, ent->skin);
175 MSG_WriteByte(msg, ent->alpha);
177 MSG_WriteByte(msg, ent->scale);
178 if (bits & E_GLOWSIZE)
179 MSG_WriteByte(msg, ent->glowsize);
180 if (bits & E_GLOWCOLOR)
181 MSG_WriteByte(msg, ent->glowcolor);
182 if (bits & E_TAGATTACHMENT)
184 MSG_WriteShort(msg, ent->tagentity);
185 MSG_WriteByte(msg, ent->tagindex);
189 MSG_WriteShort(msg, ent->light[0]);
190 MSG_WriteShort(msg, ent->light[1]);
191 MSG_WriteShort(msg, ent->light[2]);
192 MSG_WriteShort(msg, ent->light[3]);
194 if (bits & E_LIGHTSTYLE)
195 MSG_WriteByte(msg, ent->lightstyle);
196 if (bits & E_LIGHTPFLAGS)
197 MSG_WriteByte(msg, ent->lightpflags);
200 void EntityState_WriteUpdate(entity_state_t *ent, sizebuf_t *msg, entity_state_t *delta)
205 // entity is active, check for changes from the delta
206 if ((bits = EntityState_DeltaBits(delta, ent)))
208 // write the update number, bits, and fields
209 MSG_WriteShort(msg, ent->number);
210 EntityState_WriteExtendBits(msg, bits);
211 EntityState_WriteFields(ent, msg, bits);
216 // entity is inactive, check if the delta was active
219 // write the remove number
220 MSG_WriteShort(msg, ent->number | 0x8000);
225 int EntityState_ReadExtendBits(void)
228 bits = MSG_ReadByte();
229 if (bits & 0x00000080)
231 bits |= MSG_ReadByte() << 8;
232 if (bits & 0x00008000)
234 bits |= MSG_ReadByte() << 16;
235 if (bits & 0x00800000)
236 bits |= MSG_ReadByte() << 24;
242 void EntityState_ReadFields(entity_state_t *e, unsigned int bits)
244 if (cl.protocol == PROTOCOL_DARKPLACES2)
246 if (bits & E_ORIGIN1)
247 e->origin[0] = (signed short) MSG_ReadShort();
248 if (bits & E_ORIGIN2)
249 e->origin[1] = (signed short) MSG_ReadShort();
250 if (bits & E_ORIGIN3)
251 e->origin[2] = (signed short) MSG_ReadShort();
256 e->flags = MSG_ReadByte();
257 if (e->flags & RENDER_LOWPRECISION || cl.protocol == PROTOCOL_DARKPLACES2)
259 if (bits & E_ORIGIN1)
260 e->origin[0] = (signed short) MSG_ReadShort();
261 if (bits & E_ORIGIN2)
262 e->origin[1] = (signed short) MSG_ReadShort();
263 if (bits & E_ORIGIN3)
264 e->origin[2] = (signed short) MSG_ReadShort();
268 if (bits & E_ORIGIN1)
269 e->origin[0] = MSG_ReadFloat();
270 if (bits & E_ORIGIN2)
271 e->origin[1] = MSG_ReadFloat();
272 if (bits & E_ORIGIN3)
273 e->origin[2] = MSG_ReadFloat();
276 if (cl.protocol == PROTOCOL_DARKPLACES5 && !(e->flags & RENDER_LOWPRECISION))
279 e->angles[0] = MSG_ReadPreciseAngle();
281 e->angles[1] = MSG_ReadPreciseAngle();
283 e->angles[2] = MSG_ReadPreciseAngle();
288 e->angles[0] = MSG_ReadAngle();
290 e->angles[1] = MSG_ReadAngle();
292 e->angles[2] = MSG_ReadAngle();
295 e->modelindex = (e->modelindex & 0xFF00) | (unsigned int) MSG_ReadByte();
297 e->modelindex = (e->modelindex & 0x00FF) | ((unsigned int) MSG_ReadByte() << 8);
299 e->frame = (e->frame & 0xFF00) | (unsigned int) MSG_ReadByte();
301 e->frame = (e->frame & 0x00FF) | ((unsigned int) MSG_ReadByte() << 8);
302 if (bits & E_EFFECTS1)
303 e->effects = (e->effects & 0xFF00) | (unsigned int) MSG_ReadByte();
304 if (bits & E_EFFECTS2)
305 e->effects = (e->effects & 0x00FF) | ((unsigned int) MSG_ReadByte() << 8);
306 if (bits & E_COLORMAP)
307 e->colormap = MSG_ReadByte();
309 e->skin = MSG_ReadByte();
311 e->alpha = MSG_ReadByte();
313 e->scale = MSG_ReadByte();
314 if (bits & E_GLOWSIZE)
315 e->glowsize = MSG_ReadByte();
316 if (bits & E_GLOWCOLOR)
317 e->glowcolor = MSG_ReadByte();
318 if (cl.protocol == PROTOCOL_DARKPLACES2)
320 e->flags = MSG_ReadByte();
321 if (bits & E_TAGATTACHMENT)
323 e->tagentity = MSG_ReadShort();
324 e->tagindex = MSG_ReadByte();
328 e->light[0] = MSG_ReadShort();
329 e->light[1] = MSG_ReadShort();
330 e->light[2] = MSG_ReadShort();
331 e->light[3] = MSG_ReadShort();
333 if (bits & E_LIGHTSTYLE)
334 e->lightstyle = MSG_ReadByte();
335 if (bits & E_LIGHTPFLAGS)
336 e->lightpflags = MSG_ReadByte();
338 if (developer_networkentities.integer >= 2)
340 Con_Printf("ReadFields e%i", e->number);
342 if (bits & E_ORIGIN1)
343 Con_Printf(" E_ORIGIN1 %f", e->origin[0]);
344 if (bits & E_ORIGIN2)
345 Con_Printf(" E_ORIGIN2 %f", e->origin[1]);
346 if (bits & E_ORIGIN3)
347 Con_Printf(" E_ORIGIN3 %f", e->origin[2]);
349 Con_Printf(" E_ANGLE1 %f", e->angles[0]);
351 Con_Printf(" E_ANGLE2 %f", e->angles[1]);
353 Con_Printf(" E_ANGLE3 %f", e->angles[2]);
354 if (bits & (E_MODEL1 | E_MODEL2))
355 Con_Printf(" E_MODEL %i", e->modelindex);
357 if (bits & (E_FRAME1 | E_FRAME2))
358 Con_Printf(" E_FRAME %i", e->frame);
359 if (bits & (E_EFFECTS1 | E_EFFECTS2))
360 Con_Printf(" E_EFFECTS %i", e->effects);
362 Con_Printf(" E_ALPHA %f", e->alpha / 255.0f);
364 Con_Printf(" E_SCALE %f", e->scale / 16.0f);
365 if (bits & E_COLORMAP)
366 Con_Printf(" E_COLORMAP %i", e->colormap);
368 Con_Printf(" E_SKIN %i", e->skin);
370 if (bits & E_GLOWSIZE)
371 Con_Printf(" E_GLOWSIZE %i", e->glowsize * 4);
372 if (bits & E_GLOWCOLOR)
373 Con_Printf(" E_GLOWCOLOR %i", e->glowcolor);
376 Con_Printf(" E_LIGHT %i:%i:%i:%i", e->light[0], e->light[1], e->light[2], e->light[3]);
377 if (bits & E_LIGHTPFLAGS)
378 Con_Printf(" E_LIGHTPFLAGS %i", e->lightpflags);
380 if (bits & E_TAGATTACHMENT)
381 Con_Printf(" E_TAGATTACHMENT e%i:%i", e->tagentity, e->tagindex);
382 if (bits & E_LIGHTSTYLE)
383 Con_Printf(" E_LIGHTSTYLE %i", e->lightstyle);
388 // (server) clears the database to contain no frames (thus delta compression compresses against nothing)
389 void EntityFrame_ClearDatabase(entity_database_t *d)
391 memset(d, 0, sizeof(*d));
394 // (server and client) removes frames older than 'frame' from database
395 void EntityFrame_AckFrame(entity_database_t *d, int frame)
398 if (d->ackframe < frame)
400 for (i = 0;i < d->numframes && d->frames[i].framenum < frame;i++);
401 // ignore outdated frame acks (out of order packets)
405 // if some queue is left, slide it down to beginning of array
407 memmove(&d->frames[0], &d->frames[i], sizeof(d->frames[0]) * d->numframes);
410 // (server) clears frame, to prepare for adding entities
411 void EntityFrame_Clear(entity_frame_t *f, vec3_t eye, int framenum)
414 f->framenum = framenum;
422 VectorCopy(eye, f->eye);
426 // (server) adds an entity to frame
427 void EntityFrame_AddEntity(entity_frame_t *f, entity_state_t *s)
429 if (f->numentities < MAX_ENTITY_DATABASE)
431 f->entitydata[f->numentities] = *s;
432 f->entitydata[f->numentities++].active = true;
436 // (server and client) reads a frame from the database
437 void EntityFrame_FetchFrame(entity_database_t *d, int framenum, entity_frame_t *f)
440 EntityFrame_Clear(f, NULL, -1);
441 for (i = 0;i < d->numframes && d->frames[i].framenum < framenum;i++);
442 if (i < d->numframes && framenum == d->frames[i].framenum)
444 f->framenum = framenum;
445 f->numentities = d->frames[i].endentity - d->frames[i].firstentity;
446 n = MAX_ENTITY_DATABASE - (d->frames[i].firstentity % MAX_ENTITY_DATABASE);
447 if (n > f->numentities)
449 memcpy(f->entitydata, d->entitydata + d->frames[i].firstentity % MAX_ENTITY_DATABASE, sizeof(*f->entitydata) * n);
450 if (f->numentities > n)
451 memcpy(f->entitydata + n, d->entitydata, sizeof(*f->entitydata) * (f->numentities - n));
452 VectorCopy(d->eye, f->eye);
456 // (server and client) adds a entity_frame to the database, for future reference
457 void EntityFrame_AddFrame(entity_database_t *d, entity_frame_t *f)
460 entity_frameinfo_t *info;
462 VectorCopy(f->eye, d->eye);
464 // figure out how many entity slots are used already
467 n = d->frames[d->numframes - 1].endentity - d->frames[0].firstentity;
468 if (n + f->numentities > MAX_ENTITY_DATABASE || d->numframes >= MAX_ENTITY_HISTORY)
470 // ran out of room, dump database
471 EntityFrame_ClearDatabase(d);
475 info = &d->frames[d->numframes];
476 info->framenum = f->framenum;
478 // make sure we check the newly added frame as well, but we haven't incremented numframes yet
479 for (n = 0;n <= d->numframes;n++)
481 if (e >= d->frames[n].framenum)
483 if (e == f->framenum)
484 Con_Print("EntityFrame_AddFrame: tried to add out of sequence frame to database\n");
486 Con_Print("EntityFrame_AddFrame: out of sequence frames in database\n");
489 e = d->frames[n].framenum;
491 // if database still has frames after that...
493 info->firstentity = d->frames[d->numframes - 1].endentity;
495 info->firstentity = 0;
496 info->endentity = info->firstentity + f->numentities;
499 n = info->firstentity % MAX_ENTITY_DATABASE;
500 e = MAX_ENTITY_DATABASE - n;
501 if (e > f->numentities)
503 memcpy(d->entitydata + n, f->entitydata, sizeof(entity_state_t) * e);
504 if (f->numentities > e)
505 memcpy(d->entitydata, f->entitydata + e, sizeof(entity_state_t) * (f->numentities - e));
508 // (server) writes a frame to network stream
509 static entity_frame_t deltaframe; // FIXME?
510 void EntityFrame_Write(entity_database_t *d, entity_frame_t *f, sizebuf_t *msg)
513 entity_frame_t *o = &deltaframe;
514 entity_state_t *ent, *delta;
516 EntityFrame_AddFrame(d, f);
518 EntityFrame_FetchFrame(d, d->ackframe > 0 ? d->ackframe : -1, o);
519 MSG_WriteByte (msg, svc_entities);
520 MSG_WriteLong (msg, o->framenum);
521 MSG_WriteLong (msg, f->framenum);
522 MSG_WriteFloat (msg, f->eye[0]);
523 MSG_WriteFloat (msg, f->eye[1]);
524 MSG_WriteFloat (msg, f->eye[2]);
527 for (i = 0;i < f->numentities;i++)
529 ent = f->entitydata + i;
530 number = ent->number;
531 for (;onum < o->numentities && o->entitydata[onum].number < number;onum++)
533 // write remove message
534 MSG_WriteShort(msg, o->entitydata[onum].number | 0x8000);
536 if (onum < o->numentities && (o->entitydata[onum].number == number))
538 // delta from previous frame
539 delta = o->entitydata + onum;
540 // advance to next entity in delta frame
545 // delta from defaults
546 delta = &defaultstate;
548 EntityState_WriteUpdate(ent, msg, delta);
550 for (;onum < o->numentities;onum++)
552 // write remove message
553 MSG_WriteShort(msg, o->entitydata[onum].number | 0x8000);
555 MSG_WriteShort(msg, 0xFFFF);
558 // (client) reads a frame from network stream
559 static entity_frame_t framedata; // FIXME?
560 void EntityFrame_Read(entity_database_t *d)
563 entity_frame_t *f = &framedata, *delta = &deltaframe;
564 entity_state_t *e, *old, *oldend;
566 EntityFrame_Clear(f, NULL, -1);
568 // read the frame header info
569 f->time = cl.mtime[0];
570 number = MSG_ReadLong();
571 f->framenum = MSG_ReadLong();
572 f->eye[0] = MSG_ReadFloat();
573 f->eye[1] = MSG_ReadFloat();
574 f->eye[2] = MSG_ReadFloat();
575 EntityFrame_AckFrame(d, number);
576 EntityFrame_FetchFrame(d, number, delta);
577 old = delta->entitydata;
578 oldend = old + delta->numentities;
579 // read entities until we hit the magic 0xFFFF end tag
580 while ((number = (unsigned short) MSG_ReadShort()) != 0xFFFF)
583 Host_Error("EntityFrame_Read: read error\n");
584 removed = number & 0x8000;
586 if (number >= MAX_EDICTS)
587 Host_Error("EntityFrame_Read: number (%i) >= MAX_EDICTS (%i)\n", number, MAX_EDICTS);
589 // seek to entity, while copying any skipped entities (assume unchanged)
590 while (old < oldend && old->number < number)
592 if (f->numentities >= MAX_ENTITY_DATABASE)
593 Host_Error("EntityFrame_Read: entity list too big\n");
594 f->entitydata[f->numentities] = *old++;
595 f->entitydata[f->numentities++].time = cl.mtime[0];
599 if (old < oldend && old->number == number)
602 Con_Printf("EntityFrame_Read: REMOVE on unused entity %i\n", number);
606 if (f->numentities >= MAX_ENTITY_DATABASE)
607 Host_Error("EntityFrame_Read: entity list too big\n");
610 e = f->entitydata + f->numentities++;
612 if (old < oldend && old->number == number)
614 // delta from old entity
619 // delta from defaults
623 cl_entities_active[number] = true;
625 e->time = cl.mtime[0];
627 EntityState_ReadFields(e, EntityState_ReadExtendBits());
632 if (f->numentities >= MAX_ENTITY_DATABASE)
633 Host_Error("EntityFrame_Read: entity list too big\n");
634 f->entitydata[f->numentities] = *old++;
635 f->entitydata[f->numentities++].time = cl.mtime[0];
637 EntityFrame_AddFrame(d, f);
641 // (client) returns the frame number of the most recent frame recieved
642 int EntityFrame_MostRecentlyRecievedFrameNum(entity_database_t *d)
645 return d->frames[d->numframes - 1].framenum;
655 entity_state_t *EntityFrame4_GetReferenceEntity(entity_database4_t *d, int number)
657 if (d->maxreferenceentities <= number)
659 int oldmax = d->maxreferenceentities;
660 entity_state_t *oldentity = d->referenceentity;
661 d->maxreferenceentities = (number + 15) & ~7;
662 d->referenceentity = Mem_Alloc(d->mempool, d->maxreferenceentities * sizeof(*d->referenceentity));
665 memcpy(d->referenceentity, oldentity, oldmax * sizeof(*d->referenceentity));
668 // clear the newly created entities
669 for (;oldmax < d->maxreferenceentities;oldmax++)
671 d->referenceentity[oldmax] = defaultstate;
672 d->referenceentity[oldmax].number = oldmax;
675 return d->referenceentity + number;
678 void EntityFrame4_AddCommitEntity(entity_database4_t *d, entity_state_t *s)
680 // resize commit's entity list if full
681 if (d->currentcommit->maxentities <= d->currentcommit->numentities)
683 entity_state_t *oldentity = d->currentcommit->entity;
684 d->currentcommit->maxentities += 8;
685 d->currentcommit->entity = Mem_Alloc(d->mempool, d->currentcommit->maxentities * sizeof(*d->currentcommit->entity));
688 memcpy(d->currentcommit->entity, oldentity, d->currentcommit->numentities * sizeof(*d->currentcommit->entity));
692 d->currentcommit->entity[d->currentcommit->numentities++] = *s;
695 entity_database4_t *EntityFrame4_AllocDatabase(mempool_t *pool)
697 entity_database4_t *d;
698 d = Mem_Alloc(pool, sizeof(*d));
700 EntityFrame4_ResetDatabase(d);
705 void EntityFrame4_FreeDatabase(entity_database4_t *d)
708 for (i = 0;i < MAX_ENTITY_HISTORY;i++)
709 if (d->commit[i].entity)
710 Mem_Free(d->commit[i].entity);
711 if (d->referenceentity)
712 Mem_Free(d->referenceentity);
716 void EntityFrame4_ResetDatabase(entity_database4_t *d)
720 d->referenceframenum = -1;
721 for (i = 0;i < MAX_ENTITY_HISTORY;i++)
722 d->commit[i].numentities = 0;
723 for (i = 0;i < d->maxreferenceentities;i++)
724 d->referenceentity[i] = defaultstate;
727 int EntityFrame4_AckFrame(entity_database4_t *d, int framenum)
730 entity_database4_commit_t *commit;
733 // reset reference, but leave commits alone
734 d->referenceframenum = -1;
735 for (i = 0;i < d->maxreferenceentities;i++)
736 d->referenceentity[i] = defaultstate;
739 else if (d->referenceframenum == framenum)
744 for (i = 0, commit = d->commit;i < MAX_ENTITY_HISTORY;i++, commit++)
746 if (commit->numentities && commit->framenum <= framenum)
748 if (commit->framenum == framenum)
751 d->referenceframenum = framenum;
752 if (developer_networkentities.integer >= 3)
754 for (j = 0;j < commit->numentities;j++)
756 entity_state_t *s = EntityFrame4_GetReferenceEntity(d, commit->entity[j].number);
757 if (commit->entity[j].active != s->active)
759 if (commit->entity[j].active)
760 Con_Printf("commit entity %i has become active (modelindex %i)\n", commit->entity[j].number, commit->entity[j].modelindex);
762 Con_Printf("commit entity %i has become inactive (modelindex %i)\n", commit->entity[j].number, commit->entity[j].modelindex);
764 *s = commit->entity[j];
768 for (j = 0;j < commit->numentities;j++)
769 *EntityFrame4_GetReferenceEntity(d, commit->entity[j].number) = commit->entity[j];
771 commit->numentities = 0;
775 if (developer_networkentities.integer >= 1)
777 Con_Printf("ack ref:%i database updated to: ref:%i commits:", framenum, d->referenceframenum);
778 for (i = 0;i < MAX_ENTITY_HISTORY;i++)
779 if (d->commit[i].numentities)
780 Con_Printf(" %i", d->commit[i].framenum);
786 int EntityFrame4_SV_WriteFrame_Entity(entity_database4_t *d, sizebuf_t *msg, int maxbytes, entity_state_t *s)
791 // prepare the buffer
792 memset(&buf, 0, sizeof(buf));
794 buf.maxsize = sizeof(data);
795 // make the update message
796 e = EntityFrame4_GetReferenceEntity(d, s->number);
797 EntityState_WriteUpdate(s, &buf, e);
798 // if the message is empty, skip out now
801 // if the commit is full, we're done
802 if (msg->cursize + buf.cursize + 2 >= min(msg->maxsize, maxbytes))
804 // add the entity to the commit
805 EntityFrame4_AddCommitEntity(d, s);
806 // write the message to the packet
807 SZ_Write(msg, buf.data, buf.cursize);
812 extern void CL_MoveLerpEntityStates(entity_t *ent);
813 void EntityFrame4_CL_ReadFrame(entity_database4_t *d)
815 int i, n, cnumber, referenceframenum, framenum, enumber, done, stopnumber, skip = false;
817 // read the number of the frame this refers to
818 referenceframenum = MSG_ReadLong();
819 // read the number of this frame
820 framenum = MSG_ReadLong();
821 // read the start number
822 enumber = MSG_ReadShort();
823 if (developer_networkentities.integer >= 1)
825 Con_Printf("recv svc_entities num:%i ref:%i database: ref:%i commits:", framenum, referenceframenum, d->referenceframenum);
826 for (i = 0;i < MAX_ENTITY_HISTORY;i++)
827 if (d->commit[i].numentities)
828 Con_Printf(" %i", d->commit[i].framenum);
831 if (!EntityFrame4_AckFrame(d, referenceframenum))
833 Con_Print("EntityFrame4_CL_ReadFrame: reference frame invalid (VERY BAD ERROR), this update will be skipped\n");
836 d->currentcommit = NULL;
837 for (i = 0;i < MAX_ENTITY_HISTORY;i++)
839 if (!d->commit[i].numentities)
841 d->currentcommit = d->commit + i;
842 d->currentcommit->framenum = d->ackframenum = framenum;
843 d->currentcommit->numentities = 0;
846 if (d->currentcommit == NULL)
848 Con_Printf("EntityFrame4_CL_ReadFrame: error while decoding frame %i: database full, reading but not storing this update\n", framenum);
852 while (!done && !msg_badread)
854 // read the number of the modified entity
855 // (gaps will be copied unmodified)
856 n = (unsigned short)MSG_ReadShort();
859 // no more entities in this update, but we still need to copy the
860 // rest of the reference entities (final gap)
862 // read end of range number, then process normally
863 n = (unsigned short)MSG_ReadShort();
865 // high bit means it's a remove message
866 cnumber = n & 0x7FFF;
867 // add one (the changed one) if not done
868 stopnumber = cnumber + !done;
869 // process entities in range from the last one to the changed one
870 for (;enumber < stopnumber;enumber++)
874 if (enumber == cnumber && (n & 0x8000) == 0)
876 entity_state_t tempstate;
877 EntityState_ReadFields(&tempstate, EntityState_ReadExtendBits());
881 // slide the current into the previous slot
882 cl_entities[enumber].state_previous = cl_entities[enumber].state_current;
883 // copy a new current from reference database
884 cl_entities[enumber].state_current = *EntityFrame4_GetReferenceEntity(d, enumber);
885 s = &cl_entities[enumber].state_current;
886 // if this is the one to modify, read more data...
887 if (enumber == cnumber)
892 if (developer_networkentities.integer >= 2)
893 Con_Printf("entity %i: remove\n", enumber);
899 if (developer_networkentities.integer >= 2)
900 Con_Printf("entity %i: update\n", enumber);
902 EntityState_ReadFields(s, EntityState_ReadExtendBits());
905 else if (developer_networkentities.integer >= 4)
906 Con_Printf("entity %i: copy\n", enumber);
907 // set the cl_entities_active flag
908 cl_entities_active[enumber] = s->active;
909 // set the update time
910 s->time = cl.mtime[0];
911 // fix the number (it gets wiped occasionally by copying from defaultstate)
913 // check if we need to update the lerp stuff
915 CL_MoveLerpEntityStates(&cl_entities[enumber]);
916 // add this to the commit entry whether it is modified or not
917 if (d->currentcommit)
918 EntityFrame4_AddCommitEntity(d, &cl_entities[enumber].state_current);
919 // print extra messages if desired
920 if (developer_networkentities.integer >= 2 && cl_entities[enumber].state_current.active != cl_entities[enumber].state_previous.active)
922 if (cl_entities[enumber].state_current.active)
923 Con_Printf("entity #%i has become active\n", enumber);
924 else if (cl_entities[enumber].state_previous.active)
925 Con_Printf("entity #%i has become inactive\n", enumber);
929 d->currentcommit = NULL;
931 EntityFrame4_ResetDatabase(d);
939 int EntityState5_PriorityForChangedBits(int changedbits)
941 if (changedbits & E5_ISACTIVE)
943 else if (changedbits & (E5_FLAGS | E5_ATTACHMENT | E5_MODEL | E5_SKIN | E5_EXTERIORFORENTITY | E5_COLORMAP | E5_LIGHT | E5_GLOW | E5_EFFECTS | E5_ORIGIN | E5_ANGLES | E5_FRAME | E5_ALPHA | E5_SCALE))
949 void EntityState5_WriteUpdate(int number, entitystate_t *s, int changedbits, sizebuf_t *msg)
953 MSG_WriteShort(msg, number | 0x8000);
957 if (changedbits & E5_ORIGIN)
960 if (s->origin[0] < -4096 || s->origin[0] >= 4096 || s->origin[1] < -4096 || s->origin[1] >= 4096 || s->origin[2] < -4096 || s->origin[2] >= 4096)
963 if (changedbits & E5_ANGLES)
966 if (!(s->flags & RENDERFLAGS_LOWPRECISION))
969 if (changedbits & E5_MODEL)
972 if (s->modelindex >= 256)
975 if (changedbits & E5_FRAME)
981 if (changedbits & E5_SKIN)
983 if (changedbits & E5_EFFECTS)
986 if (s->modelindex >= 256)
989 if (changedbits & E5_FLAGS)
991 if (changedbits & E5_ALPHA)
993 if (changedbits & E5_SCALE)
995 if (changedbits & E5_ATTACHMENT)
996 bits |= E5_ATTACHMENT;
997 if (changedbits & E5_EXTERIORFORENTITY)
998 bits |= E5_EXTERIORFORENTITY;
999 if (changedbits & E5_LIGHT)
1001 if (changedbits & E5_COLORMAP)
1002 bits |= E5_COLORMAP;
1003 if (changedbits & E5_GLOW)
1009 if (bits >= 16777216)
1011 MSG_WriteShort(msg, number);
1012 MSG_WriteByte(msg, bits & 0xFF);
1013 if (bits & E5_EXTEND1)
1014 MSG_WriteByte(msg, (bits >> 8) & 0xFF);
1015 if (bits & E5_EXTEND2)
1016 MSG_WriteByte(msg, (bits >> 16) & 0xFF);
1017 if (bits & E5_EXTEND3)
1018 MSG_WriteByte(msg, (bits >> 24) & 0xFF);
1019 if (bits & E5_FLAGS)
1020 MSG_WriteByte(msg, s->flags);
1021 if (bits & E5_ORIGIN)
1023 if (bits & E5_ORIGIN32)
1025 MSG_WriteFloat(msg, s->origin[0]);
1026 MSG_WriteFloat(msg, s->origin[1]);
1027 MSG_WriteFloat(msg, s->origin[2]);
1031 MSG_WriteShort(msg, (int)floor(s->origin[0] * 8 + 0.5f));
1032 MSG_WriteShort(msg, (int)floor(s->origin[1] * 8 + 0.5f));
1033 MSG_WriteShort(msg, (int)floor(s->origin[2] * 8 + 0.5f));
1036 if (bits & E5_ANGLES)
1038 if (bits & E5_ANGLES16)
1040 MSG_WriteShort(msg, (int)floor(s->angles[0] * (65536.0f / 360.0f) + 0.5f));
1041 MSG_WriteShort(msg, (int)floor(s->angles[1] * (65536.0f / 360.0f) + 0.5f));
1042 MSG_WriteShort(msg, (int)floor(s->angles[2] * (65536.0f / 360.0f) + 0.5f));
1046 MSG_WriteByte(msg, (int)floor(s->angles[0] * (256.0f / 360.0f) + 0.5f));
1047 MSG_WriteByte(msg, (int)floor(s->angles[1] * (256.0f / 360.0f) + 0.5f));
1048 MSG_WriteByte(msg, (int)floor(s->angles[2] * (256.0f / 360.0f) + 0.5f));
1051 if (bits & E5_MODEL)
1053 if (bits & E5_MODEL16)
1054 MSG_WriteShort(msg, s->modelindex);
1056 MSG_WriteByte(msg, s->modelindex);
1058 if (bits & E5_FRAME)
1060 if (bits & E5_FRAME16)
1061 MSG_WriteShort(msg, s->frame);
1063 MSG_WriteByte(msg, s->frame);
1066 MSG_WriteByte(msg, s->flags);
1067 if (bits & E5_EFFECTS)
1069 if (bits & E5_EFFECTS32)
1070 MSG_WriteLong(msg, s->effects);
1071 else if (bits & E5_EFFECTS16)
1072 MSG_WriteShort(msg, s->effects);
1074 MSG_WriteByte(msg, s->effects);
1076 if (bits & E5_ALPHA)
1077 MSG_WriteByte(msg, s->flags);
1078 if (bits & E5_SCALE)
1079 MSG_WriteByte(msg, s->flags);
1080 if (bits & E5_ATTACHMENT)
1082 MSG_WriteShort(msg, s->tagentity);
1083 MSG_WriteByte(msg, s->tagindex);
1085 if (bits & E5_EXTERIORFORENTITY)
1086 MSG_WriteShort(msg, s->tagentity);
1087 if (bits & E5_LIGHT)
1089 MSG_WriteShort(msg, s->light[0]);
1090 MSG_WriteShort(msg, s->light[1]);
1091 MSG_WriteShort(msg, s->light[2]);
1092 MSG_WriteShort(msg, s->light[3]);
1094 if (bits & E5_COLORMAP)
1095 MSG_WriteByte(msg, s->colormap);
1098 MSG_WriteByte(msg, s->glowsize);
1099 MSG_WriteByte(msg, s->glowcolor);
1104 int EntityFrame5_ReadUpdate(void)
1106 number = MSG_ReadShort();
1107 e = cl_entities + (number & 0x7FFF);
1108 e->state_previous = e->state_current;
1109 if (number & 0x8000)
1111 if (number == 0x8000)
1113 // end of entity list
1118 e->state_current = defaultstate;
1119 e->state_current.number = number;
1127 int cl_entityframe5_lastreceivedframenum;
1129 void EntityFrame5_CL_ReadFrame(void)
1134 // read the number of this frame to echo back in next input packet
1135 cl_entityframe5_lastreceivedframenum = MSG_ReadLong();
1136 // read entity numbers until we find a 0x8000
1137 // (which would be remove world entity, but is actually a terminator)
1138 while ((n = MSG_ReadShort()) != 0x8000)
1140 // get the entity number and look it up
1141 enumber = n & 0x7FFF;
1142 ent = cl_entities + enumber;
1143 // slide the current into the previous slot
1144 ent->state_previous = ent->state_current;
1146 s = &ent->state_current;
1156 EntityState_ReadFields(s, EntityState_ReadExtendBits());
1158 // set the cl_entities_active flag
1159 cl_entities_active[enumber] = s->active;
1160 // set the update time
1161 s->time = cl.mtime[0];
1162 // fix the number (it gets wiped occasionally by copying from defaultstate)
1163 s->number = enumber;
1164 // check if we need to update the lerp stuff
1166 CL_MoveLerpEntityStates(&cl_entities[enumber]);
1167 // print extra messages if desired
1168 if (developer_networkentities.integer >= 2 && cl_entities[enumber].state_current.active != cl_entities[enumber].state_previous.active)
1170 if (cl_entities[enumber].state_current.active)
1171 Con_Printf("entity #%i has become active\n", enumber);
1172 else if (cl_entities[enumber].state_previous.active)
1173 Con_Printf("entity #%i has become inactive\n", enumber);
1178 #define ENTITYFRAME5_MAXPACKETLOGS 64
1179 #define ENTITYFRAME5_MAXSTATES 128
1181 typedef struct entityframe5_state_s
1183 unsigned short entitynumber;
1185 qbyte activedirtybit;
1188 entityframe5_state_t;
1190 typedef struct entityframe5_packetlog_s
1194 entityframe5_state_t states[ENTITYFRAME5_MAXSTATES];
1196 entityframe5_packetlog_t;
1198 typedef struct entityframe5_s
1201 entityframe5_packetlog_t packetlog[ENTITYFRAME5_MAXPACKETLOGS];
1202 qbyte activedirtybits[(MAX_EDICTS + 7) / 8];
1203 int dirtybits[MAX_EDICTS];
1207 void EntityFrame5_AckFrame(entityframe5_t *d, int framenum)
1209 int i, j, k, l, dirtybits, activedirtybit;
1210 entityframe5_state_t *s, *s2;
1211 entityframe5_packetlog_t *p, *p2;
1212 if (framenum >= d->ackedframenum)
1214 d->ackedframenum = framenum;
1215 // scan for packets made obsolete by this ack
1216 for (i = 0, p = d->packetlog;i < ENTITYFRAME5_MAXPACKETLOGS;i++, p++)
1218 // skip packets that are empty or in the future
1219 if (p->packetnumber == 0 || p->packetnumber > framenum)
1221 // if the packetnumber matches it is deleted without any processing
1222 // (since it was received).
1223 // if the packet number is less than this ack it was lost and its
1224 // important information will be repeated in this update if it is not
1225 // already obsolete due to a later update.
1226 if (p->packetnumber < framenum)
1228 // packet was lost - merge dirtybits into the main array so they
1229 // will be re-sent, but only if there is no newer update of that
1230 // bit in the logs (as those will arrive before this update)
1231 for (j = 0, s = p->states;j < p->numstates;j++, s++)
1233 activedirtybit = s->activedirtybit;
1234 dirtybits = s->dirtybits;
1235 // check for any newer updates to this entity
1236 for (k = 0, p2 = d->packetlog;k < ENTITYFRAME5_MAXPACKETLOGS;k++, p2++)
1238 if (p2->packetnumber > framenum)
1240 for (l = 0, s2 = p2->states;l < p2->numstates;l++, p2++)
1242 if (s2->entitynumber == s->entitynumber)
1244 activedirtybit &= ~s2->activedirtybit;
1245 dirtybits &= ~s2->dirtybits;
1249 if (!activedirtybit && !dirtybits)
1253 // if the bits haven't all been cleared, there were some bits
1254 // lost with this packet, so set them again now
1256 d->activedirtybits[s->entitynumber / 8] |= 1 << (s->entitynumber & 7);
1258 d->dirtybits[s->entitynumber] |= dirtybits;
1261 // delete this packet log as it is now obsolete
1262 p->packetnumber = 0;
1266 void EntityFrame5_WriteFrame(sizebuf_t *msg, int numstates, entity_state_t *states)