4 void ClearStateToDefault(entity_state_t *s)
6 memset(s, 0, sizeof(*s));
12 void EntityState_Write(entity_state_t *ent, sizebuf_t *msg, entity_state_t *delta)
19 VectorCopy(ent->origin, org);
20 VectorCopy(delta->origin, deltaorg);
21 if (ent->flags & RENDER_LOWPRECISION)
24 org[0] = (int) (org[0] + 0.5f);
26 org[0] = (int) (org[0] - 0.5f);
28 org[1] = (int) (org[1] + 0.5f);
30 org[1] = (int) (org[1] - 0.5f);
32 org[2] = (int) (org[2] + 0.5f);
34 org[2] = (int) (org[2] - 0.5f);
36 if (delta->flags & RENDER_LOWPRECISION)
39 deltaorg[0] = (int) (deltaorg[0] + 0.5f);
41 deltaorg[0] = (int) (deltaorg[0] - 0.5f);
43 deltaorg[1] = (int) (deltaorg[1] + 0.5f);
45 deltaorg[1] = (int) (deltaorg[1] - 0.5f);
47 deltaorg[2] = (int) (deltaorg[2] + 0.5f);
49 deltaorg[2] = (int) (deltaorg[2] - 0.5f);
51 if (fabs(org[0] - deltaorg[0]) > 0.01f)
53 if (fabs(org[1] - deltaorg[1]) > 0.01f)
55 if (fabs(org[2] - deltaorg[2]) > 0.01f)
57 if ((qbyte) (ent->angles[0] * (256.0f / 360.0f)) != (qbyte) (delta->angles[0] * (256.0f / 360.0f)))
59 if ((qbyte) (ent->angles[1] * (256.0f / 360.0f)) != (qbyte) (delta->angles[1] * (256.0f / 360.0f)))
61 if ((qbyte) (ent->angles[2] * (256.0f / 360.0f)) != (qbyte) (delta->angles[2] * (256.0f / 360.0f)))
63 if ((ent->modelindex ^ delta->modelindex) & 0x00FF)
65 if ((ent->modelindex ^ delta->modelindex) & 0xFF00)
67 if ((ent->frame ^ delta->frame) & 0x00FF)
69 if ((ent->frame ^ delta->frame) & 0xFF00)
71 if ((ent->effects ^ delta->effects) & 0x00FF)
73 if ((ent->effects ^ delta->effects) & 0xFF00)
75 if (ent->colormap != delta->colormap)
77 if (ent->skin != delta->skin)
79 if (ent->alpha != delta->alpha)
81 if (ent->scale != delta->scale)
83 if (ent->glowsize != delta->glowsize)
85 if (ent->glowcolor != delta->glowcolor)
87 if (ent->flags != delta->flags)
89 if (ent->tagindex != delta->tagindex || ent->tagentity != delta->tagentity)
90 bits |= E_TAGATTACHMENT;
92 if (bits) // don't send anything if it hasn't changed
94 if (bits & 0xFF000000)
96 if (bits & 0x00FF0000)
98 if (bits & 0x0000FF00)
101 MSG_WriteShort(msg, ent->number);
102 MSG_WriteByte(msg, bits & 0xFF);
103 if (bits & E_EXTEND1)
105 MSG_WriteByte(msg, (bits >> 8) & 0xFF);
106 if (bits & E_EXTEND2)
108 MSG_WriteByte(msg, (bits >> 16) & 0xFF);
109 if (bits & E_EXTEND3)
110 MSG_WriteByte(msg, (bits >> 24) & 0xFF);
113 // LordHavoc: have to write flags first, as they can modify protocol
115 MSG_WriteByte(msg, ent->flags);
116 if (ent->flags & RENDER_LOWPRECISION)
118 if (bits & E_ORIGIN1)
119 MSG_WriteShort(msg, org[0]);
120 if (bits & E_ORIGIN2)
121 MSG_WriteShort(msg, org[1]);
122 if (bits & E_ORIGIN3)
123 MSG_WriteShort(msg, org[2]);
127 if (bits & E_ORIGIN1)
128 MSG_WriteFloat(msg, org[0]);
129 if (bits & E_ORIGIN2)
130 MSG_WriteFloat(msg, org[1]);
131 if (bits & E_ORIGIN3)
132 MSG_WriteFloat(msg, org[2]);
135 MSG_WriteAngle(msg, ent->angles[0]);
137 MSG_WriteAngle(msg, ent->angles[1]);
139 MSG_WriteAngle(msg, ent->angles[2]);
141 MSG_WriteByte(msg, ent->modelindex & 0xFF);
143 MSG_WriteByte(msg, (ent->modelindex >> 8) & 0xFF);
145 MSG_WriteByte(msg, ent->frame & 0xFF);
147 MSG_WriteByte(msg, (ent->frame >> 8) & 0xFF);
148 if (bits & E_EFFECTS1)
149 MSG_WriteByte(msg, ent->effects & 0xFF);
150 if (bits & E_EFFECTS2)
151 MSG_WriteByte(msg, (ent->effects >> 8) & 0xFF);
152 if (bits & E_COLORMAP)
153 MSG_WriteByte(msg, ent->colormap);
155 MSG_WriteByte(msg, ent->skin);
157 MSG_WriteByte(msg, ent->alpha);
159 MSG_WriteByte(msg, ent->scale);
160 if (bits & E_GLOWSIZE)
161 MSG_WriteByte(msg, ent->glowsize);
162 if (bits & E_GLOWCOLOR)
163 MSG_WriteByte(msg, ent->glowcolor);
164 if (bits & E_TAGATTACHMENT)
166 MSG_WriteShort(msg, ent->tagentity);
167 MSG_WriteByte(msg, ent->tagindex);
171 else if (!delta->active)
172 MSG_WriteShort(msg, ent->number | 0x8000);
175 void EntityState_Read(entity_state_t *e, entity_state_t *delta, int number)
178 memcpy(e, delta, sizeof(*e));
180 e->time = cl.mtime[0];
183 bits = MSG_ReadByte();
184 if (bits & E_EXTEND1)
186 bits |= MSG_ReadByte() << 8;
187 if (bits & E_EXTEND2)
189 bits |= MSG_ReadByte() << 16;
190 if (bits & E_EXTEND3)
191 bits |= MSG_ReadByte() << 24;
195 if (dpprotocol == DPPROTOCOL_VERSION2)
197 if (bits & E_ORIGIN1)
198 e->origin[0] = (signed short) MSG_ReadShort();
199 if (bits & E_ORIGIN2)
200 e->origin[1] = (signed short) MSG_ReadShort();
201 if (bits & E_ORIGIN3)
202 e->origin[2] = (signed short) MSG_ReadShort();
207 e->flags = MSG_ReadByte();
208 if (e->flags & RENDER_LOWPRECISION || dpprotocol == DPPROTOCOL_VERSION2)
210 if (bits & E_ORIGIN1)
211 e->origin[0] = (signed short) MSG_ReadShort();
212 if (bits & E_ORIGIN2)
213 e->origin[1] = (signed short) MSG_ReadShort();
214 if (bits & E_ORIGIN3)
215 e->origin[2] = (signed short) MSG_ReadShort();
219 if (bits & E_ORIGIN1)
220 e->origin[0] = MSG_ReadFloat();
221 if (bits & E_ORIGIN2)
222 e->origin[1] = MSG_ReadFloat();
223 if (bits & E_ORIGIN3)
224 e->origin[2] = MSG_ReadFloat();
228 e->angles[0] = MSG_ReadAngle();
230 e->angles[1] = MSG_ReadAngle();
232 e->angles[2] = MSG_ReadAngle();
234 e->modelindex = (e->modelindex & 0xFF00) | (unsigned int) MSG_ReadByte();
236 e->modelindex = (e->modelindex & 0x00FF) | ((unsigned int) MSG_ReadByte() << 8);
238 e->frame = (e->frame & 0xFF00) | (unsigned int) MSG_ReadByte();
240 e->frame = (e->frame & 0x00FF) | ((unsigned int) MSG_ReadByte() << 8);
241 if (bits & E_EFFECTS1)
242 e->effects = (e->effects & 0xFF00) | (unsigned int) MSG_ReadByte();
243 if (bits & E_EFFECTS2)
244 e->effects = (e->effects & 0x00FF) | ((unsigned int) MSG_ReadByte() << 8);
245 if (bits & E_COLORMAP)
246 e->colormap = MSG_ReadByte();
248 e->skin = MSG_ReadByte();
250 e->alpha = MSG_ReadByte();
252 e->scale = MSG_ReadByte();
253 if (bits & E_GLOWSIZE)
254 e->glowsize = MSG_ReadByte();
255 if (bits & E_GLOWCOLOR)
256 e->glowcolor = MSG_ReadByte();
257 if (dpprotocol == DPPROTOCOL_VERSION2)
259 e->flags = MSG_ReadByte();
260 if (bits & E_TAGATTACHMENT)
262 e->tagentity = MSG_ReadShort();
263 e->tagindex = MSG_ReadByte();
267 // (server) clears the database to contain no frames (thus delta compression compresses against nothing)
268 void EntityFrame_ClearDatabase(entity_database_t *d)
270 memset(d, 0, sizeof(*d));
273 // (server and client) removes frames older than 'frame' from database
274 void EntityFrame_AckFrame(entity_database_t *d, int frame)
277 if (d->ackframe < frame)
279 for (i = 0;i < d->numframes && d->frames[i].framenum < frame;i++);
280 // ignore outdated frame acks (out of order packets)
284 // if some queue is left, slide it down to beginning of array
286 memmove(&d->frames[0], &d->frames[i], sizeof(d->frames[0]) * d->numframes);
289 // (server) clears frame, to prepare for adding entities
290 void EntityFrame_Clear(entity_frame_t *f, vec3_t eye, int framenum)
293 f->framenum = framenum;
301 VectorCopy(eye, f->eye);
305 // (server) adds an entity to frame
306 void EntityFrame_AddEntity(entity_frame_t *f, entity_state_t *s)
308 if (f->numentities < MAX_ENTITY_DATABASE)
310 f->entitydata[f->numentities] = *s;
311 f->entitydata[f->numentities++].active = true;
315 // (server and client) reads a frame from the database
316 void EntityFrame_FetchFrame(entity_database_t *d, int framenum, entity_frame_t *f)
319 EntityFrame_Clear(f, NULL, -1);
320 for (i = 0;i < d->numframes && d->frames[i].framenum < framenum;i++);
321 if (i < d->numframes && framenum == d->frames[i].framenum)
323 f->framenum = framenum;
324 f->numentities = d->frames[i].endentity - d->frames[i].firstentity;
325 n = MAX_ENTITY_DATABASE - (d->frames[i].firstentity % MAX_ENTITY_DATABASE);
326 if (n > f->numentities)
328 memcpy(f->entitydata, d->entitydata + d->frames[i].firstentity % MAX_ENTITY_DATABASE, sizeof(*f->entitydata) * n);
329 if (f->numentities > n)
330 memcpy(f->entitydata + n, d->entitydata, sizeof(*f->entitydata) * (f->numentities - n));
331 VectorCopy(d->eye, f->eye);
335 // (server and client) adds a entity_frame to the database, for future reference
336 void EntityFrame_AddFrame(entity_database_t *d, entity_frame_t *f)
339 entity_frameinfo_t *info;
341 VectorCopy(f->eye, d->eye);
343 // figure out how many entity slots are used already
346 n = d->frames[d->numframes - 1].endentity - d->frames[0].firstentity;
347 if (n + f->numentities > MAX_ENTITY_DATABASE || d->numframes >= MAX_ENTITY_HISTORY)
349 // ran out of room, dump database
350 EntityFrame_ClearDatabase(d);
354 info = &d->frames[d->numframes];
355 info->framenum = f->framenum;
357 // make sure we check the newly added frame as well, but we haven't incremented numframes yet
358 for (n = 0;n <= d->numframes;n++)
360 if (e >= d->frames[n].framenum)
362 if (e == f->framenum)
363 Con_Printf("EntityFrame_AddFrame: tried to add out of sequence frame to database\n");
365 Con_Printf("EntityFrame_AddFrame: out of sequence frames in database\n");
368 e = d->frames[n].framenum;
370 // if database still has frames after that...
372 info->firstentity = d->frames[d->numframes - 1].endentity;
374 info->firstentity = 0;
375 info->endentity = info->firstentity + f->numentities;
378 n = info->firstentity % MAX_ENTITY_DATABASE;
379 e = MAX_ENTITY_DATABASE - n;
380 if (e > f->numentities)
382 memcpy(d->entitydata + n, f->entitydata, sizeof(entity_state_t) * e);
383 if (f->numentities > e)
384 memcpy(d->entitydata, f->entitydata + e, sizeof(entity_state_t) * (f->numentities - e));
387 // (server) writes a frame to network stream
388 static entity_frame_t deltaframe; // FIXME?
389 void EntityFrame_Write(entity_database_t *d, entity_frame_t *f, sizebuf_t *msg)
392 entity_frame_t *o = &deltaframe;
393 entity_state_t *ent, *delta, baseline;
395 EntityFrame_AddFrame(d, f);
397 ClearStateToDefault(&baseline);
398 EntityFrame_FetchFrame(d, d->ackframe > 0 ? d->ackframe : -1, o);
399 MSG_WriteByte (msg, svc_entities);
400 MSG_WriteLong (msg, o->framenum);
401 MSG_WriteLong (msg, f->framenum);
402 MSG_WriteFloat (msg, f->eye[0]);
403 MSG_WriteFloat (msg, f->eye[1]);
404 MSG_WriteFloat (msg, f->eye[2]);
407 for (i = 0;i < f->numentities;i++)
409 ent = f->entitydata + i;
410 number = ent->number;
411 for (;onum < o->numentities && o->entitydata[onum].number < number;onum++)
413 // write remove message
414 MSG_WriteShort(msg, o->entitydata[onum].number | 0x8000);
416 if (onum < o->numentities && (o->entitydata[onum].number == number))
418 // delta from previous frame
419 delta = o->entitydata + onum;
420 // advance to next entity in delta frame
425 // delta from baseline
428 EntityState_Write(ent, msg, delta);
430 for (;onum < o->numentities;onum++)
432 // write remove message
433 MSG_WriteShort(msg, o->entitydata[onum].number | 0x8000);
435 MSG_WriteShort(msg, 0xFFFF);
438 // (client) reads a frame from network stream
439 static entity_frame_t framedata; // FIXME?
440 void EntityFrame_Read(entity_database_t *d)
443 entity_frame_t *f = &framedata, *delta = &deltaframe;
444 entity_state_t *e, baseline, *old, *oldend, *edelta;
446 ClearStateToDefault(&baseline);
448 EntityFrame_Clear(f, NULL, -1);
450 // read the frame header info
451 f->time = cl.mtime[0];
452 number = MSG_ReadLong();
453 f->framenum = MSG_ReadLong();
454 f->eye[0] = MSG_ReadFloat();
455 f->eye[1] = MSG_ReadFloat();
456 f->eye[2] = MSG_ReadFloat();
457 EntityFrame_AckFrame(d, number);
458 EntityFrame_FetchFrame(d, number, delta);
459 old = delta->entitydata;
460 oldend = old + delta->numentities;
461 // read entities until we hit the magic 0xFFFF end tag
462 while ((number = (unsigned short) MSG_ReadShort()) != 0xFFFF)
465 Host_Error("EntityFrame_Read: read error\n");
466 removed = number & 0x8000;
468 if (number >= MAX_EDICTS)
469 Host_Error("EntityFrame_Read: number (%i) >= MAX_EDICTS (%i)\n", number, MAX_EDICTS);
471 // seek to entity, while copying any skipped entities (assume unchanged)
472 while (old < oldend && old->number < number)
474 if (f->numentities >= MAX_ENTITY_DATABASE)
475 Host_Error("EntityFrame_Read: entity list too big\n");
476 memcpy(f->entitydata + f->numentities, old, sizeof(entity_state_t));
477 f->entitydata[f->numentities].time = cl.mtime[0];
483 if (old < oldend && old->number == number)
486 Con_Printf("EntityFrame_Read: REMOVE on unused entity %i\n", number);
490 if (f->numentities >= MAX_ENTITY_DATABASE)
491 Host_Error("EntityFrame_Read: entity list too big\n");
494 e = f->entitydata + f->numentities++;
496 if (old < oldend && old->number == number)
498 // delta from old entity
503 // delta from baseline
507 EntityState_Read(e, edelta, number);
512 if (f->numentities >= MAX_ENTITY_DATABASE)
513 Host_Error("EntityFrame_Read: entity list too big\n");
514 memcpy(f->entitydata + f->numentities, old, sizeof(entity_state_t));
515 f->entitydata[f->numentities].time = cl.mtime[0];
519 EntityFrame_AddFrame(d, f);
523 // (client) returns the frame number of the most recent frame recieved
524 int EntityFrame_MostRecentlyRecievedFrameNum(entity_database_t *d)
527 return d->frames[d->numframes - 1].framenum;
537 static int EntityFrame4_SV_ChooseCommitToReplace(entity_database4_t *d)
539 int i, best, bestframenum;
541 bestframenum = d->commit[0].framenum;
542 for (i = 0;i < MAX_ENTITY_HISTORY;i++)
544 if (!d->commit[i].numentities)
546 if (bestframenum > d->commit[i].framenum)
548 bestframenum = d->commit[i].framenum;
555 static entity_state_t *EntityFrame4_GetReferenceEntity(entity_database4_t *d, int number)
557 if (d->maxreferenceentities <= number)
559 int oldmax = d->maxreferenceentities;
560 entity_state_t *oldentity = d->referenceentity;
561 d->maxreferenceentities = (number + 15) & ~7;
562 d->referenceentity = Mem_Alloc(d->mempool, d->maxreferenceentities * sizeof(*d->referenceentity));
565 memcpy(d->referenceentity, oldentity, oldmax * sizeof(*d->referenceentity));
568 // clear the newly created entities
569 for (;oldmax < d->maxreferenceentities;oldmax++)
570 ClearStateToDefault(d->referenceentity + oldmax);
572 return d->referenceentity + number;
575 static void EntityFrame4_AddCommitEntity(entity_database4_t *d, entity_state_t *s)
577 // resize commit's entity list if full
578 if (d->currentcommit->maxentities <= d->currentcommit->numentities)
580 entity_state_t *oldentity = d->currentcommit->entity;
581 d->currentcommit->maxentities += 8;
582 d->currentcommit->entity = Mem_Alloc(d->mempool, d->currentcommit->maxentities * sizeof(*d->currentcommit->entity));
585 memcpy(d->currentcommit->entity, oldentity, d->currentcommit->numentities * sizeof(*d->currentcommit->entity));
589 d->currentcommit->entity[d->currentcommit->numentities++] = *s;
592 entity_database4_t *EntityFrame4_AllocDatabase(mempool_t *pool)
594 entity_database4_t *d;
595 d = Mem_Alloc(pool, sizeof(*d));
597 d->referenceframenum = -1;
601 void EntityFrame4_FreeDatabase(entity_database4_t *d)
604 for (i = 0;i < MAX_ENTITY_HISTORY;i++)
605 if (d->commit[i].entity)
606 Mem_Free(d->commit[i].entity);
607 if (d->referenceentity)
608 Mem_Free(d->referenceentity);
612 void EntityFrame4_ResetDatabase(entity_database4_t *d)
615 d->referenceframenum = -1;
616 for (i = 0;i < MAX_ENTITY_HISTORY;i++)
617 d->commit[i].numentities = 0;
620 void EntityFrame4_AckFrame(entity_database4_t *d, int framenum)
622 int i, foundit = false;
623 // check if client is requesting no delta compression
626 EntityFrame4_ResetDatabase(d);
629 for (i = 0;i < MAX_ENTITY_HISTORY;i++)
631 if (d->commit[i].numentities && d->commit[i].framenum <= framenum)
633 if (d->commit[i].framenum == framenum)
635 // apply commit to database
636 d->referenceframenum = d->commit[i].framenum;
637 while (d->commit[i].numentities--)
638 *EntityFrame4_GetReferenceEntity(d, d->commit[i].entity[d->commit[i].numentities].number) = d->commit[i].entity[d->commit[i].numentities];
641 d->commit[i].numentities = 0;
642 d->commit[i].framenum = -1;
646 Con_DPrintf("EntityFrame4_AckFrame: frame %i not found in database, expect glitches!\n", framenum);
649 void EntityFrame4_SV_WriteFrame_Begin(entity_database4_t *d, sizebuf_t *msg, int framenum)
651 d->currentcommit = d->commit + EntityFrame4_SV_ChooseCommitToReplace(d);
652 d->currentcommit->numentities = 0;
653 d->currentcommit->framenum = framenum;
654 MSG_WriteByte(msg, svc_entities);
655 MSG_WriteLong(msg, d->referenceframenum);
656 MSG_WriteLong(msg, d->currentcommit->framenum);
659 int EntityFrame4_SV_WriteFrame_Entity(entity_database4_t *d, sizebuf_t *msg, int maxbytes, entity_state_t *s)
664 // prepare the buffer
665 memset(&buf, 0, sizeof(buf));
667 buf.maxsize = sizeof(data);
669 e = EntityFrame4_GetReferenceEntity(d, s->number);
672 // entity exists, send an update
673 EntityState_Write(s, &buf, e);
677 // entity used to exist but doesn't anymore, send remove
678 MSG_WriteShort(&buf, s->number | 0x8000);
680 // if the message is empty, skip out now
683 // if the commit is full, we're done
684 if (msg->cursize + buf.cursize + 2 >= min(msg->maxsize, maxbytes))
686 // add the entity to the commit
687 EntityFrame4_AddCommitEntity(d, s);
688 // write the message to the packet
689 SZ_Write(msg, buf.data, buf.cursize);
694 void EntityFrame4_SV_WriteFrame_End(entity_database4_t *d, sizebuf_t *msg)
696 // remove world message (invalid, and thus a good terminator)
697 MSG_WriteShort(msg, 0x8000);
699 d->currentcommit = NULL;
702 extern void CL_MoveLerpEntityStates(entity_t *ent);
703 void EntityFrame4_CL_ReadFrame(entity_database4_t *d)
705 int i, n, number, referenceframenum, framenum;
706 referenceframenum = MSG_ReadLong();
707 framenum = MSG_ReadLong();
708 EntityFrame4_AckFrame(d, referenceframenum);
709 for (i = 0;i < MAX_ENTITY_HISTORY;i++)
710 if (!d->commit[i].numentities)
712 if (i < MAX_ENTITY_HISTORY)
714 d->currentcommit = d->commit + i;
715 d->currentcommit->framenum = framenum;
716 d->currentcommit->numentities = 0;
720 Con_Printf("EntityFrame4_CL_ReadFrame: error while decoding frame %i: database full, resetting, expect glitches!!\n", framenum);
721 d->currentcommit = NULL;
722 EntityFrame4_ResetDatabase(d);
724 while((n = MSG_ReadShort()) != 0x8000)
727 cl_entities[number].state_previous = cl_entities[number].state_current;
730 ClearStateToDefault(&cl_entities[number].state_current);
731 cl_entities[number].state_current.active = false;
732 cl_entities[number].state_current.number = number;
735 EntityState_Read(&cl_entities[number].state_current, EntityFrame4_GetReferenceEntity(d, number), number);
736 CL_MoveLerpEntityStates(&cl_entities[number]);
737 cl_entities_active[number] = true;
738 if (d->currentcommit)
739 EntityFrame4_AddCommitEntity(d, &cl_entities[number].state_current);
741 d->currentcommit = NULL;