5 entity_state_t defaultstate =
7 // ! means this is not 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 {32, 32, 32},//unsigned char colormod[3];
34 // padding to a multiple of 8 bytes (to align the double time)
35 {0,0}//unsigned char unused[2]; // !
38 // keep track of quake entities because they need to be killed if they get stale
39 int cl_lastquakeentity = 0;
40 qbyte cl_isquakeentity[MAX_EDICTS];
42 void EntityFrameQuake_ReadEntity(int bits)
48 if (bits & U_MOREBITS)
49 bits |= (MSG_ReadByte()<<8);
50 if ((bits & U_EXTEND1) && cl.protocol != PROTOCOL_NEHAHRAMOVIE)
52 bits |= MSG_ReadByte() << 16;
54 bits |= MSG_ReadByte() << 24;
57 if (bits & U_LONGENTITY)
58 num = (unsigned short) MSG_ReadShort ();
60 num = MSG_ReadByte ();
62 if (num >= MAX_EDICTS)
63 Host_Error("EntityFrameQuake_ReadEntity: entity number (%i) >= MAX_EDICTS (%i)\n", num, MAX_EDICTS);
65 Host_Error("EntityFrameQuake_ReadEntity: invalid entity number (%i)\n", num);
67 ent = cl_entities + num;
69 // note: this inherits the 'active' state of the baseline chosen
70 // (state_baseline is always active, state_current may not be active if
71 // the entity was missing in the last frame)
73 s = ent->state_current;
76 s = ent->state_baseline;
80 cl_isquakeentity[num] = true;
81 if (cl_lastquakeentity < num)
82 cl_lastquakeentity = num;
86 if (bits & U_MODEL) s.modelindex = (s.modelindex & 0xFF00) | MSG_ReadByte();
87 if (bits & U_FRAME) s.frame = (s.frame & 0xFF00) | MSG_ReadByte();
88 if (bits & U_COLORMAP) s.colormap = MSG_ReadByte();
89 if (bits & U_SKIN) s.skin = MSG_ReadByte();
90 if (bits & U_EFFECTS) s.effects = (s.effects & 0xFF00) | MSG_ReadByte();
91 if (bits & U_ORIGIN1) s.origin[0] = MSG_ReadCoord(cl.protocol);
92 if (bits & U_ANGLE1) s.angles[0] = MSG_ReadAngle(cl.protocol);
93 if (bits & U_ORIGIN2) s.origin[1] = MSG_ReadCoord(cl.protocol);
94 if (bits & U_ANGLE2) s.angles[1] = MSG_ReadAngle(cl.protocol);
95 if (bits & U_ORIGIN3) s.origin[2] = MSG_ReadCoord(cl.protocol);
96 if (bits & U_ANGLE3) s.angles[2] = MSG_ReadAngle(cl.protocol);
97 if (bits & U_STEP) s.flags |= RENDER_STEP;
98 if (bits & U_ALPHA) s.alpha = MSG_ReadByte();
99 if (bits & U_SCALE) s.scale = MSG_ReadByte();
100 if (bits & U_EFFECTS2) s.effects = (s.effects & 0x00FF) | (MSG_ReadByte() << 8);
101 if (bits & U_GLOWSIZE) s.glowsize = MSG_ReadByte();
102 if (bits & U_GLOWCOLOR) s.glowcolor = MSG_ReadByte();
103 if (bits & U_COLORMOD) {int c = MSG_ReadByte();s.colormod[0] = (qbyte)(((c >> 5) & 7) * (32.0f / 7.0f));s.colormod[1] = (qbyte)(((c >> 2) & 7) * (32.0f / 7.0f));s.colormod[2] = (qbyte)((c & 3) * (32.0f / 3.0f));}
104 if (bits & U_GLOWTRAIL) s.flags |= RENDER_GLOWTRAIL;
105 if (bits & U_FRAME2) s.frame = (s.frame & 0x00FF) | (MSG_ReadByte() << 8);
106 if (bits & U_MODEL2) s.modelindex = (s.modelindex & 0x00FF) | (MSG_ReadByte() << 8);
107 if (bits & U_VIEWMODEL) s.flags |= RENDER_VIEWMODEL;
108 if (bits & U_EXTERIORMODEL) s.flags |= RENDER_EXTERIORMODEL;
110 // LordHavoc: to allow playback of the Nehahra movie
111 if (cl.protocol == PROTOCOL_NEHAHRAMOVIE && (bits & U_EXTEND1))
113 // LordHavoc: evil format
114 int i = MSG_ReadFloat();
115 int j = MSG_ReadFloat() * 255.0f;
120 s.effects |= EF_FULLBRIGHT;
124 else if (j == 0 || j >= 255)
130 ent->state_previous = ent->state_current;
131 ent->state_current = s;
132 if (ent->state_current.active)
134 CL_MoveLerpEntityStates(ent);
135 cl_entities_active[ent->state_current.number] = true;
139 Host_Error("EntityFrameQuake_ReadEntity: read error\n");
142 void EntityFrameQuake_ISeeDeadEntities(void)
145 if (cl_lastquakeentity == 0)
147 lastentity = cl_lastquakeentity;
148 cl_lastquakeentity = 0;
149 for (num = 0;num <= lastentity;num++)
151 if (cl_isquakeentity[num])
153 if (cl_entities_active[num] && cl_entities[num].state_current.time == cl.mtime[0])
155 cl_isquakeentity[num] = true;
156 cl_lastquakeentity = num;
160 cl_isquakeentity[num] = false;
161 cl_entities_active[num] = false;
162 cl_entities[num].state_current = defaultstate;
163 cl_entities[num].state_current.number = num;
169 void EntityFrameQuake_WriteFrame(sizebuf_t *msg, int numstates, const entity_state_t *states)
171 const entity_state_t *s;
172 entity_state_t baseline;
177 // prepare the buffer
178 memset(&buf, 0, sizeof(buf));
180 buf.maxsize = sizeof(data);
182 for (i = 0, s = states;i < numstates;i++, s++)
184 // prepare the buffer
189 if (s->number >= 256)
190 bits |= U_LONGENTITY;
191 if (s->flags & RENDER_STEP)
193 if (s->flags & RENDER_VIEWMODEL)
195 if (s->flags & RENDER_GLOWTRAIL)
197 if (s->flags & RENDER_EXTERIORMODEL)
198 bits |= U_EXTERIORMODEL;
200 // LordHavoc: old stuff, but rewritten to have more exact tolerances
201 baseline = sv.edicts[s->number].e->baseline;
202 if (baseline.origin[0] != s->origin[0])
204 if (baseline.origin[1] != s->origin[1])
206 if (baseline.origin[2] != s->origin[2])
208 if (baseline.angles[0] != s->angles[0])
210 if (baseline.angles[1] != s->angles[1])
212 if (baseline.angles[2] != s->angles[2])
214 if (baseline.colormap != s->colormap)
216 if (baseline.skin != s->skin)
218 if (baseline.frame != s->frame)
221 if (s->frame & 0xFF00)
224 if (baseline.effects != s->effects)
227 if (s->effects & 0xFF00)
230 if (baseline.modelindex != s->modelindex)
233 if (s->modelindex & 0xFF00)
236 if (baseline.alpha != s->alpha)
238 if (baseline.scale != s->scale)
240 if (baseline.glowsize != s->glowsize)
242 if (baseline.glowcolor != s->glowcolor)
245 // if extensions are disabled, clear the relevant update flags
246 if (sv.netquakecompatible)
250 if (bits >= 16777216)
258 MSG_WriteByte (&buf, bits);
259 if (bits & U_MOREBITS) MSG_WriteByte(&buf, bits>>8);
260 if (bits & U_EXTEND1) MSG_WriteByte(&buf, bits>>16);
261 if (bits & U_EXTEND2) MSG_WriteByte(&buf, bits>>24);
262 if (bits & U_LONGENTITY) MSG_WriteShort(&buf, s->number);
263 else MSG_WriteByte(&buf, s->number);
265 if (bits & U_MODEL) MSG_WriteByte(&buf, s->modelindex);
266 if (bits & U_FRAME) MSG_WriteByte(&buf, s->frame);
267 if (bits & U_COLORMAP) MSG_WriteByte(&buf, s->colormap);
268 if (bits & U_SKIN) MSG_WriteByte(&buf, s->skin);
269 if (bits & U_EFFECTS) MSG_WriteByte(&buf, s->effects);
270 if (bits & U_ORIGIN1) MSG_WriteCoord(&buf, s->origin[0], sv.protocol);
271 if (bits & U_ANGLE1) MSG_WriteAngle(&buf, s->angles[0], sv.protocol);
272 if (bits & U_ORIGIN2) MSG_WriteCoord(&buf, s->origin[1], sv.protocol);
273 if (bits & U_ANGLE2) MSG_WriteAngle(&buf, s->angles[1], sv.protocol);
274 if (bits & U_ORIGIN3) MSG_WriteCoord(&buf, s->origin[2], sv.protocol);
275 if (bits & U_ANGLE3) MSG_WriteAngle(&buf, s->angles[2], sv.protocol);
276 if (bits & U_ALPHA) MSG_WriteByte(&buf, s->alpha);
277 if (bits & U_SCALE) MSG_WriteByte(&buf, s->scale);
278 if (bits & U_EFFECTS2) MSG_WriteByte(&buf, s->effects >> 8);
279 if (bits & U_GLOWSIZE) MSG_WriteByte(&buf, s->glowsize);
280 if (bits & U_GLOWCOLOR) MSG_WriteByte(&buf, s->glowcolor);
281 if (bits & U_COLORMOD) {int c = ((int)bound(0, s->colormod[0] * (7.0f / 32.0f), 7) << 5) | ((int)bound(0, s->colormod[0] * (7.0f / 32.0f), 7) << 2) | ((int)bound(0, s->colormod[0] * (3.0f / 32.0f), 3) << 0);MSG_WriteByte(&buf, c);}
282 if (bits & U_FRAME2) MSG_WriteByte(&buf, s->frame >> 8);
283 if (bits & U_MODEL2) MSG_WriteByte(&buf, s->modelindex >> 8);
285 // if the commit is full, we're done this frame
286 if (msg->cursize + buf.cursize > msg->maxsize)
288 // next frame we will continue where we left off
291 // write the message to the packet
292 SZ_Write(msg, buf.data, buf.cursize);
296 int EntityState_DeltaBits(const entity_state_t *o, const entity_state_t *n)
299 // if o is not active, delta from default
303 if (fabs(n->origin[0] - o->origin[0]) > (1.0f / 256.0f))
305 if (fabs(n->origin[1] - o->origin[1]) > (1.0f / 256.0f))
307 if (fabs(n->origin[2] - o->origin[2]) > (1.0f / 256.0f))
309 if ((qbyte) (n->angles[0] * (256.0f / 360.0f)) != (qbyte) (o->angles[0] * (256.0f / 360.0f)))
311 if ((qbyte) (n->angles[1] * (256.0f / 360.0f)) != (qbyte) (o->angles[1] * (256.0f / 360.0f)))
313 if ((qbyte) (n->angles[2] * (256.0f / 360.0f)) != (qbyte) (o->angles[2] * (256.0f / 360.0f)))
315 if ((n->modelindex ^ o->modelindex) & 0x00FF)
317 if ((n->modelindex ^ o->modelindex) & 0xFF00)
319 if ((n->frame ^ o->frame) & 0x00FF)
321 if ((n->frame ^ o->frame) & 0xFF00)
323 if ((n->effects ^ o->effects) & 0x00FF)
325 if ((n->effects ^ o->effects) & 0xFF00)
327 if (n->colormap != o->colormap)
329 if (n->skin != o->skin)
331 if (n->alpha != o->alpha)
333 if (n->scale != o->scale)
335 if (n->glowsize != o->glowsize)
337 if (n->glowcolor != o->glowcolor)
339 if (n->flags != o->flags)
341 if (n->tagindex != o->tagindex || n->tagentity != o->tagentity)
342 bits |= E_TAGATTACHMENT;
343 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])
345 if (n->lightstyle != o->lightstyle)
346 bits |= E_LIGHTSTYLE;
347 if (n->lightpflags != o->lightpflags)
348 bits |= E_LIGHTPFLAGS;
352 if (bits & 0xFF000000)
354 if (bits & 0x00FF0000)
356 if (bits & 0x0000FF00)
362 void EntityState_WriteExtendBits(sizebuf_t *msg, unsigned int bits)
364 MSG_WriteByte(msg, bits & 0xFF);
365 if (bits & 0x00000080)
367 MSG_WriteByte(msg, (bits >> 8) & 0xFF);
368 if (bits & 0x00008000)
370 MSG_WriteByte(msg, (bits >> 16) & 0xFF);
371 if (bits & 0x00800000)
372 MSG_WriteByte(msg, (bits >> 24) & 0xFF);
377 void EntityState_WriteFields(const entity_state_t *ent, sizebuf_t *msg, unsigned int bits)
379 if (sv.protocol == PROTOCOL_DARKPLACES2)
381 if (bits & E_ORIGIN1)
382 MSG_WriteCoord16i(msg, ent->origin[0]);
383 if (bits & E_ORIGIN2)
384 MSG_WriteCoord16i(msg, ent->origin[1]);
385 if (bits & E_ORIGIN3)
386 MSG_WriteCoord16i(msg, ent->origin[2]);
388 else if (sv.protocol == PROTOCOL_DARKPLACES1 || sv.protocol == PROTOCOL_DARKPLACES3 || sv.protocol == PROTOCOL_DARKPLACES4 || sv.protocol == PROTOCOL_DARKPLACES5 || sv.protocol == PROTOCOL_DARKPLACES6)
390 // LordHavoc: have to write flags first, as they can modify protocol
392 MSG_WriteByte(msg, ent->flags);
393 if (ent->flags & RENDER_LOWPRECISION)
395 if (bits & E_ORIGIN1)
396 MSG_WriteCoord16i(msg, ent->origin[0]);
397 if (bits & E_ORIGIN2)
398 MSG_WriteCoord16i(msg, ent->origin[1]);
399 if (bits & E_ORIGIN3)
400 MSG_WriteCoord16i(msg, ent->origin[2]);
404 if (bits & E_ORIGIN1)
405 MSG_WriteCoord32f(msg, ent->origin[0]);
406 if (bits & E_ORIGIN2)
407 MSG_WriteCoord32f(msg, ent->origin[1]);
408 if (bits & E_ORIGIN3)
409 MSG_WriteCoord32f(msg, ent->origin[2]);
412 if ((sv.protocol == PROTOCOL_DARKPLACES5 || sv.protocol == PROTOCOL_DARKPLACES6) && !(ent->flags & RENDER_LOWPRECISION))
415 MSG_WriteAngle16i(msg, ent->angles[0]);
417 MSG_WriteAngle16i(msg, ent->angles[1]);
419 MSG_WriteAngle16i(msg, ent->angles[2]);
424 MSG_WriteAngle8i(msg, ent->angles[0]);
426 MSG_WriteAngle8i(msg, ent->angles[1]);
428 MSG_WriteAngle8i(msg, ent->angles[2]);
431 MSG_WriteByte(msg, ent->modelindex & 0xFF);
433 MSG_WriteByte(msg, (ent->modelindex >> 8) & 0xFF);
435 MSG_WriteByte(msg, ent->frame & 0xFF);
437 MSG_WriteByte(msg, (ent->frame >> 8) & 0xFF);
438 if (bits & E_EFFECTS1)
439 MSG_WriteByte(msg, ent->effects & 0xFF);
440 if (bits & E_EFFECTS2)
441 MSG_WriteByte(msg, (ent->effects >> 8) & 0xFF);
442 if (bits & E_COLORMAP)
443 MSG_WriteByte(msg, ent->colormap);
445 MSG_WriteByte(msg, ent->skin);
447 MSG_WriteByte(msg, ent->alpha);
449 MSG_WriteByte(msg, ent->scale);
450 if (bits & E_GLOWSIZE)
451 MSG_WriteByte(msg, ent->glowsize);
452 if (bits & E_GLOWCOLOR)
453 MSG_WriteByte(msg, ent->glowcolor);
454 if (sv.protocol == PROTOCOL_DARKPLACES2)
456 MSG_WriteByte(msg, ent->flags);
457 if (bits & E_TAGATTACHMENT)
459 MSG_WriteShort(msg, ent->tagentity);
460 MSG_WriteByte(msg, ent->tagindex);
464 MSG_WriteShort(msg, ent->light[0]);
465 MSG_WriteShort(msg, ent->light[1]);
466 MSG_WriteShort(msg, ent->light[2]);
467 MSG_WriteShort(msg, ent->light[3]);
469 if (bits & E_LIGHTSTYLE)
470 MSG_WriteByte(msg, ent->lightstyle);
471 if (bits & E_LIGHTPFLAGS)
472 MSG_WriteByte(msg, ent->lightpflags);
475 void EntityState_WriteUpdate(const entity_state_t *ent, sizebuf_t *msg, const entity_state_t *delta)
480 // entity is active, check for changes from the delta
481 if ((bits = EntityState_DeltaBits(delta, ent)))
483 // write the update number, bits, and fields
484 MSG_WriteShort(msg, ent->number);
485 EntityState_WriteExtendBits(msg, bits);
486 EntityState_WriteFields(ent, msg, bits);
491 // entity is inactive, check if the delta was active
494 // write the remove number
495 MSG_WriteShort(msg, ent->number | 0x8000);
500 int EntityState_ReadExtendBits(void)
503 bits = MSG_ReadByte();
504 if (bits & 0x00000080)
506 bits |= MSG_ReadByte() << 8;
507 if (bits & 0x00008000)
509 bits |= MSG_ReadByte() << 16;
510 if (bits & 0x00800000)
511 bits |= MSG_ReadByte() << 24;
517 void EntityState_ReadFields(entity_state_t *e, unsigned int bits)
519 if (cl.protocol == PROTOCOL_DARKPLACES2)
521 if (bits & E_ORIGIN1)
522 e->origin[0] = MSG_ReadCoord16i();
523 if (bits & E_ORIGIN2)
524 e->origin[1] = MSG_ReadCoord16i();
525 if (bits & E_ORIGIN3)
526 e->origin[2] = MSG_ReadCoord16i();
528 else if (cl.protocol == PROTOCOL_DARKPLACES1 || cl.protocol == PROTOCOL_DARKPLACES3 || cl.protocol == PROTOCOL_DARKPLACES4 || cl.protocol == PROTOCOL_DARKPLACES5 || cl.protocol == PROTOCOL_DARKPLACES6)
531 e->flags = MSG_ReadByte();
532 if (e->flags & RENDER_LOWPRECISION)
534 if (bits & E_ORIGIN1)
535 e->origin[0] = MSG_ReadCoord16i();
536 if (bits & E_ORIGIN2)
537 e->origin[1] = MSG_ReadCoord16i();
538 if (bits & E_ORIGIN3)
539 e->origin[2] = MSG_ReadCoord16i();
543 if (bits & E_ORIGIN1)
544 e->origin[0] = MSG_ReadCoord32f();
545 if (bits & E_ORIGIN2)
546 e->origin[1] = MSG_ReadCoord32f();
547 if (bits & E_ORIGIN3)
548 e->origin[2] = MSG_ReadCoord32f();
552 Host_Error("EntityState_ReadFields: unknown cl.protocol %i\n", cl.protocol);
553 if ((cl.protocol == PROTOCOL_DARKPLACES5 || cl.protocol == PROTOCOL_DARKPLACES6) && !(e->flags & RENDER_LOWPRECISION))
556 e->angles[0] = MSG_ReadAngle16i();
558 e->angles[1] = MSG_ReadAngle16i();
560 e->angles[2] = MSG_ReadAngle16i();
565 e->angles[0] = MSG_ReadAngle8i();
567 e->angles[1] = MSG_ReadAngle8i();
569 e->angles[2] = MSG_ReadAngle8i();
572 e->modelindex = (e->modelindex & 0xFF00) | (unsigned int) MSG_ReadByte();
574 e->modelindex = (e->modelindex & 0x00FF) | ((unsigned int) MSG_ReadByte() << 8);
576 e->frame = (e->frame & 0xFF00) | (unsigned int) MSG_ReadByte();
578 e->frame = (e->frame & 0x00FF) | ((unsigned int) MSG_ReadByte() << 8);
579 if (bits & E_EFFECTS1)
580 e->effects = (e->effects & 0xFF00) | (unsigned int) MSG_ReadByte();
581 if (bits & E_EFFECTS2)
582 e->effects = (e->effects & 0x00FF) | ((unsigned int) MSG_ReadByte() << 8);
583 if (bits & E_COLORMAP)
584 e->colormap = MSG_ReadByte();
586 e->skin = MSG_ReadByte();
588 e->alpha = MSG_ReadByte();
590 e->scale = MSG_ReadByte();
591 if (bits & E_GLOWSIZE)
592 e->glowsize = MSG_ReadByte();
593 if (bits & E_GLOWCOLOR)
594 e->glowcolor = MSG_ReadByte();
595 if (cl.protocol == PROTOCOL_DARKPLACES2)
597 e->flags = MSG_ReadByte();
598 if (bits & E_TAGATTACHMENT)
600 e->tagentity = (unsigned short) MSG_ReadShort();
601 e->tagindex = MSG_ReadByte();
605 e->light[0] = (unsigned short) MSG_ReadShort();
606 e->light[1] = (unsigned short) MSG_ReadShort();
607 e->light[2] = (unsigned short) MSG_ReadShort();
608 e->light[3] = (unsigned short) MSG_ReadShort();
610 if (bits & E_LIGHTSTYLE)
611 e->lightstyle = MSG_ReadByte();
612 if (bits & E_LIGHTPFLAGS)
613 e->lightpflags = MSG_ReadByte();
615 if (developer_networkentities.integer >= 2)
617 Con_Printf("ReadFields e%i", e->number);
619 if (bits & E_ORIGIN1)
620 Con_Printf(" E_ORIGIN1 %f", e->origin[0]);
621 if (bits & E_ORIGIN2)
622 Con_Printf(" E_ORIGIN2 %f", e->origin[1]);
623 if (bits & E_ORIGIN3)
624 Con_Printf(" E_ORIGIN3 %f", e->origin[2]);
626 Con_Printf(" E_ANGLE1 %f", e->angles[0]);
628 Con_Printf(" E_ANGLE2 %f", e->angles[1]);
630 Con_Printf(" E_ANGLE3 %f", e->angles[2]);
631 if (bits & (E_MODEL1 | E_MODEL2))
632 Con_Printf(" E_MODEL %i", e->modelindex);
634 if (bits & (E_FRAME1 | E_FRAME2))
635 Con_Printf(" E_FRAME %i", e->frame);
636 if (bits & (E_EFFECTS1 | E_EFFECTS2))
637 Con_Printf(" E_EFFECTS %i", e->effects);
639 Con_Printf(" E_ALPHA %f", e->alpha / 255.0f);
641 Con_Printf(" E_SCALE %f", e->scale / 16.0f);
642 if (bits & E_COLORMAP)
643 Con_Printf(" E_COLORMAP %i", e->colormap);
645 Con_Printf(" E_SKIN %i", e->skin);
647 if (bits & E_GLOWSIZE)
648 Con_Printf(" E_GLOWSIZE %i", e->glowsize * 4);
649 if (bits & E_GLOWCOLOR)
650 Con_Printf(" E_GLOWCOLOR %i", e->glowcolor);
653 Con_Printf(" E_LIGHT %i:%i:%i:%i", e->light[0], e->light[1], e->light[2], e->light[3]);
654 if (bits & E_LIGHTPFLAGS)
655 Con_Printf(" E_LIGHTPFLAGS %i", e->lightpflags);
657 if (bits & E_TAGATTACHMENT)
658 Con_Printf(" E_TAGATTACHMENT e%i:%i", e->tagentity, e->tagindex);
659 if (bits & E_LIGHTSTYLE)
660 Con_Printf(" E_LIGHTSTYLE %i", e->lightstyle);
665 // (client and server) allocates a new empty database
666 entityframe_database_t *EntityFrame_AllocDatabase(mempool_t *mempool)
668 return Mem_Alloc(mempool, sizeof(entityframe_database_t));
671 // (client and server) frees the database
672 void EntityFrame_FreeDatabase(entityframe_database_t *d)
677 // (server) clears the database to contain no frames (thus delta compression compresses against nothing)
678 void EntityFrame_ClearDatabase(entityframe_database_t *d)
680 memset(d, 0, sizeof(*d));
683 // (server and client) removes frames older than 'frame' from database
684 void EntityFrame_AckFrame(entityframe_database_t *d, int frame)
687 d->ackframenum = frame;
688 for (i = 0;i < d->numframes && d->frames[i].framenum < frame;i++);
689 // ignore outdated frame acks (out of order packets)
693 // if some queue is left, slide it down to beginning of array
695 memmove(&d->frames[0], &d->frames[i], sizeof(d->frames[0]) * d->numframes);
698 // (server) clears frame, to prepare for adding entities
699 void EntityFrame_Clear(entity_frame_t *f, vec3_t eye, int framenum)
702 f->framenum = framenum;
707 VectorCopy(eye, f->eye);
710 // (server and client) reads a frame from the database
711 void EntityFrame_FetchFrame(entityframe_database_t *d, int framenum, entity_frame_t *f)
714 EntityFrame_Clear(f, NULL, -1);
715 for (i = 0;i < d->numframes && d->frames[i].framenum < framenum;i++);
716 if (i < d->numframes && framenum == d->frames[i].framenum)
718 f->framenum = framenum;
719 f->numentities = d->frames[i].endentity - d->frames[i].firstentity;
720 n = MAX_ENTITY_DATABASE - (d->frames[i].firstentity % MAX_ENTITY_DATABASE);
721 if (n > f->numentities)
723 memcpy(f->entitydata, d->entitydata + d->frames[i].firstentity % MAX_ENTITY_DATABASE, sizeof(*f->entitydata) * n);
724 if (f->numentities > n)
725 memcpy(f->entitydata + n, d->entitydata, sizeof(*f->entitydata) * (f->numentities - n));
726 VectorCopy(d->eye, f->eye);
730 // (server and client) adds a entity_frame to the database, for future reference
731 void EntityFrame_AddFrame(entityframe_database_t *d, vec3_t eye, int framenum, int numentities, const entity_state_t *entitydata)
734 entity_frameinfo_t *info;
736 VectorCopy(eye, d->eye);
738 // figure out how many entity slots are used already
741 n = d->frames[d->numframes - 1].endentity - d->frames[0].firstentity;
742 if (n + numentities > MAX_ENTITY_DATABASE || d->numframes >= MAX_ENTITY_HISTORY)
744 // ran out of room, dump database
745 EntityFrame_ClearDatabase(d);
749 info = &d->frames[d->numframes];
750 info->framenum = framenum;
752 // make sure we check the newly added frame as well, but we haven't incremented numframes yet
753 for (n = 0;n <= d->numframes;n++)
755 if (e >= d->frames[n].framenum)
758 Con_Print("EntityFrame_AddFrame: tried to add out of sequence frame to database\n");
760 Con_Print("EntityFrame_AddFrame: out of sequence frames in database\n");
763 e = d->frames[n].framenum;
765 // if database still has frames after that...
767 info->firstentity = d->frames[d->numframes - 1].endentity;
769 info->firstentity = 0;
770 info->endentity = info->firstentity + numentities;
773 n = info->firstentity % MAX_ENTITY_DATABASE;
774 e = MAX_ENTITY_DATABASE - n;
777 memcpy(d->entitydata + n, entitydata, sizeof(entity_state_t) * e);
779 memcpy(d->entitydata, entitydata + e, sizeof(entity_state_t) * (numentities - e));
782 // (server) writes a frame to network stream
783 static entity_frame_t deltaframe; // FIXME?
784 void EntityFrame_WriteFrame(sizebuf_t *msg, entityframe_database_t *d, int numstates, const entity_state_t *states, int viewentnum)
787 entity_frame_t *o = &deltaframe;
788 const entity_state_t *ent, *delta;
794 for (i = 0;i < numstates;i++)
796 if (states[i].number == viewentnum)
798 VectorSet(eye, states[i].origin[0], states[i].origin[1], states[i].origin[2] + 22);
803 EntityFrame_AddFrame(d, eye, d->latestframenum, numstates, states);
805 EntityFrame_FetchFrame(d, d->ackframenum, o);
807 MSG_WriteByte (msg, svc_entities);
808 MSG_WriteLong (msg, o->framenum);
809 MSG_WriteLong (msg, d->latestframenum);
810 MSG_WriteFloat (msg, eye[0]);
811 MSG_WriteFloat (msg, eye[1]);
812 MSG_WriteFloat (msg, eye[2]);
815 for (i = 0;i < numstates;i++)
818 number = ent->number;
819 for (;onum < o->numentities && o->entitydata[onum].number < number;onum++)
821 // write remove message
822 MSG_WriteShort(msg, o->entitydata[onum].number | 0x8000);
824 if (onum < o->numentities && (o->entitydata[onum].number == number))
826 // delta from previous frame
827 delta = o->entitydata + onum;
828 // advance to next entity in delta frame
833 // delta from defaults
834 delta = &defaultstate;
836 EntityState_WriteUpdate(ent, msg, delta);
838 for (;onum < o->numentities;onum++)
840 // write remove message
841 MSG_WriteShort(msg, o->entitydata[onum].number | 0x8000);
843 MSG_WriteShort(msg, 0xFFFF);
846 // (client) reads a frame from network stream
847 static entity_frame_t framedata; // FIXME?
848 void EntityFrame_CL_ReadFrame(void)
850 int i, number, removed;
851 entity_frame_t *f = &framedata, *delta = &deltaframe;
852 entity_state_t *e, *old, *oldend;
854 entityframe_database_t *d;
855 if (!cl.entitydatabase)
856 cl.entitydatabase = EntityFrame_AllocDatabase(cl_entities_mempool);
857 d = cl.entitydatabase;
859 EntityFrame_Clear(f, NULL, -1);
861 // read the frame header info
862 f->time = cl.mtime[0];
863 number = MSG_ReadLong();
864 for (i = 0;i < LATESTFRAMENUMS-1;i++)
865 cl.latestframenums[i] = cl.latestframenums[i+1];
866 cl.latestframenums[LATESTFRAMENUMS-1] = f->framenum = MSG_ReadLong();
867 f->eye[0] = MSG_ReadFloat();
868 f->eye[1] = MSG_ReadFloat();
869 f->eye[2] = MSG_ReadFloat();
870 EntityFrame_AckFrame(d, number);
871 EntityFrame_FetchFrame(d, number, delta);
872 old = delta->entitydata;
873 oldend = old + delta->numentities;
874 // read entities until we hit the magic 0xFFFF end tag
875 while ((number = (unsigned short) MSG_ReadShort()) != 0xFFFF && !msg_badread)
878 Host_Error("EntityFrame_Read: read error\n");
879 removed = number & 0x8000;
881 if (number >= MAX_EDICTS)
882 Host_Error("EntityFrame_Read: number (%i) >= MAX_EDICTS (%i)\n", number, MAX_EDICTS);
884 // seek to entity, while copying any skipped entities (assume unchanged)
885 while (old < oldend && old->number < number)
887 if (f->numentities >= MAX_ENTITY_DATABASE)
888 Host_Error("EntityFrame_Read: entity list too big\n");
889 f->entitydata[f->numentities] = *old++;
890 f->entitydata[f->numentities++].time = cl.mtime[0];
894 if (old < oldend && old->number == number)
897 Con_Printf("EntityFrame_Read: REMOVE on unused entity %i\n", number);
901 if (f->numentities >= MAX_ENTITY_DATABASE)
902 Host_Error("EntityFrame_Read: entity list too big\n");
905 e = f->entitydata + f->numentities++;
907 if (old < oldend && old->number == number)
909 // delta from old entity
914 // delta from defaults
918 cl_entities_active[number] = true;
920 e->time = cl.mtime[0];
922 EntityState_ReadFields(e, EntityState_ReadExtendBits());
927 if (f->numentities >= MAX_ENTITY_DATABASE)
928 Host_Error("EntityFrame_Read: entity list too big\n");
929 f->entitydata[f->numentities] = *old++;
930 f->entitydata[f->numentities++].time = cl.mtime[0];
932 EntityFrame_AddFrame(d, f->eye, f->framenum, f->numentities, f->entitydata);
934 memset(cl_entities_active, 0, cl_max_entities * sizeof(qbyte));
936 for (i = 0;i < f->numentities;i++)
938 for (;number < f->entitydata[i].number;number++)
940 if (cl_entities_active[number])
942 cl_entities_active[number] = false;
943 cl_entities[number].state_current.active = false;
947 ent = &cl_entities[number];
948 ent->state_previous = ent->state_current;
949 ent->state_current = f->entitydata[i];
950 CL_MoveLerpEntityStates(ent);
951 // the entity lives again...
952 cl_entities_active[number] = true;
955 for (;number < cl_max_entities;number++)
957 if (cl_entities_active[number])
959 cl_entities_active[number] = false;
960 cl_entities[number].state_current.active = false;
966 // (client) returns the frame number of the most recent frame recieved
967 int EntityFrame_MostRecentlyRecievedFrameNum(entityframe_database_t *d)
970 return d->frames[d->numframes - 1].framenum;
980 entity_state_t *EntityFrame4_GetReferenceEntity(entityframe4_database_t *d, int number)
982 if (d->maxreferenceentities <= number)
984 int oldmax = d->maxreferenceentities;
985 entity_state_t *oldentity = d->referenceentity;
986 d->maxreferenceentities = (number + 15) & ~7;
987 d->referenceentity = Mem_Alloc(d->mempool, d->maxreferenceentities * sizeof(*d->referenceentity));
990 memcpy(d->referenceentity, oldentity, oldmax * sizeof(*d->referenceentity));
993 // clear the newly created entities
994 for (;oldmax < d->maxreferenceentities;oldmax++)
996 d->referenceentity[oldmax] = defaultstate;
997 d->referenceentity[oldmax].number = oldmax;
1000 return d->referenceentity + number;
1003 void EntityFrame4_AddCommitEntity(entityframe4_database_t *d, const entity_state_t *s)
1005 // resize commit's entity list if full
1006 if (d->currentcommit->maxentities <= d->currentcommit->numentities)
1008 entity_state_t *oldentity = d->currentcommit->entity;
1009 d->currentcommit->maxentities += 8;
1010 d->currentcommit->entity = Mem_Alloc(d->mempool, d->currentcommit->maxentities * sizeof(*d->currentcommit->entity));
1013 memcpy(d->currentcommit->entity, oldentity, d->currentcommit->numentities * sizeof(*d->currentcommit->entity));
1014 Mem_Free(oldentity);
1017 d->currentcommit->entity[d->currentcommit->numentities++] = *s;
1020 entityframe4_database_t *EntityFrame4_AllocDatabase(mempool_t *pool)
1022 entityframe4_database_t *d;
1023 d = Mem_Alloc(pool, sizeof(*d));
1025 EntityFrame4_ResetDatabase(d);
1029 void EntityFrame4_FreeDatabase(entityframe4_database_t *d)
1032 for (i = 0;i < MAX_ENTITY_HISTORY;i++)
1033 if (d->commit[i].entity)
1034 Mem_Free(d->commit[i].entity);
1035 if (d->referenceentity)
1036 Mem_Free(d->referenceentity);
1040 void EntityFrame4_ResetDatabase(entityframe4_database_t *d)
1043 d->referenceframenum = -1;
1044 for (i = 0;i < MAX_ENTITY_HISTORY;i++)
1045 d->commit[i].numentities = 0;
1046 for (i = 0;i < d->maxreferenceentities;i++)
1047 d->referenceentity[i] = defaultstate;
1050 int EntityFrame4_AckFrame(entityframe4_database_t *d, int framenum, int servermode)
1053 entity_database4_commit_t *commit;
1056 // reset reference, but leave commits alone
1057 d->referenceframenum = -1;
1058 for (i = 0;i < d->maxreferenceentities;i++)
1059 d->referenceentity[i] = defaultstate;
1060 // if this is the server, remove commits
1061 for (i = 0, commit = d->commit;i < MAX_ENTITY_HISTORY;i++, commit++)
1062 commit->numentities = 0;
1065 else if (d->referenceframenum == framenum)
1070 for (i = 0, commit = d->commit;i < MAX_ENTITY_HISTORY;i++, commit++)
1072 if (commit->numentities && commit->framenum <= framenum)
1074 if (commit->framenum == framenum)
1077 d->referenceframenum = framenum;
1078 if (developer_networkentities.integer >= 3)
1080 for (j = 0;j < commit->numentities;j++)
1082 entity_state_t *s = EntityFrame4_GetReferenceEntity(d, commit->entity[j].number);
1083 if (commit->entity[j].active != s->active)
1085 if (commit->entity[j].active)
1086 Con_Printf("commit entity %i has become active (modelindex %i)\n", commit->entity[j].number, commit->entity[j].modelindex);
1088 Con_Printf("commit entity %i has become inactive (modelindex %i)\n", commit->entity[j].number, commit->entity[j].modelindex);
1090 *s = commit->entity[j];
1094 for (j = 0;j < commit->numentities;j++)
1095 *EntityFrame4_GetReferenceEntity(d, commit->entity[j].number) = commit->entity[j];
1097 commit->numentities = 0;
1101 if (developer_networkentities.integer >= 1)
1103 Con_Printf("ack ref:%i database updated to: ref:%i commits:", framenum, d->referenceframenum);
1104 for (i = 0;i < MAX_ENTITY_HISTORY;i++)
1105 if (d->commit[i].numentities)
1106 Con_Printf(" %i", d->commit[i].framenum);
1112 void EntityFrame4_CL_ReadFrame(void)
1114 int i, n, cnumber, referenceframenum, framenum, enumber, done, stopnumber, skip = false;
1116 entityframe4_database_t *d;
1117 if (!cl.entitydatabase4)
1118 cl.entitydatabase4 = EntityFrame4_AllocDatabase(cl_entities_mempool);
1119 d = cl.entitydatabase4;
1120 // read the number of the frame this refers to
1121 referenceframenum = MSG_ReadLong();
1122 // read the number of this frame
1123 for (i = 0;i < LATESTFRAMENUMS-1;i++)
1124 cl.latestframenums[i] = cl.latestframenums[i+1];
1125 cl.latestframenums[LATESTFRAMENUMS-1] = framenum = MSG_ReadLong();
1126 // read the start number
1127 enumber = (unsigned short) MSG_ReadShort();
1128 if (developer_networkentities.integer >= 1)
1130 Con_Printf("recv svc_entities num:%i ref:%i database: ref:%i commits:", framenum, referenceframenum, d->referenceframenum);
1131 for (i = 0;i < MAX_ENTITY_HISTORY;i++)
1132 if (d->commit[i].numentities)
1133 Con_Printf(" %i", d->commit[i].framenum);
1136 if (!EntityFrame4_AckFrame(d, referenceframenum, false))
1138 Con_Print("EntityFrame4_CL_ReadFrame: reference frame invalid (VERY BAD ERROR), this update will be skipped\n");
1141 d->currentcommit = NULL;
1142 for (i = 0;i < MAX_ENTITY_HISTORY;i++)
1144 if (!d->commit[i].numentities)
1146 d->currentcommit = d->commit + i;
1147 d->currentcommit->framenum = framenum;
1148 d->currentcommit->numentities = 0;
1151 if (d->currentcommit == NULL)
1153 Con_Printf("EntityFrame4_CL_ReadFrame: error while decoding frame %i: database full, reading but not storing this update\n", framenum);
1157 while (!done && !msg_badread)
1159 // read the number of the modified entity
1160 // (gaps will be copied unmodified)
1161 n = (unsigned short)MSG_ReadShort();
1164 // no more entities in this update, but we still need to copy the
1165 // rest of the reference entities (final gap)
1167 // read end of range number, then process normally
1168 n = (unsigned short)MSG_ReadShort();
1170 // high bit means it's a remove message
1171 cnumber = n & 0x7FFF;
1172 // add one (the changed one) if not done
1173 stopnumber = cnumber + !done;
1174 // process entities in range from the last one to the changed one
1175 for (;enumber < stopnumber;enumber++)
1179 if (enumber == cnumber && (n & 0x8000) == 0)
1181 entity_state_t tempstate;
1182 EntityState_ReadFields(&tempstate, EntityState_ReadExtendBits());
1186 // slide the current into the previous slot
1187 cl_entities[enumber].state_previous = cl_entities[enumber].state_current;
1188 // copy a new current from reference database
1189 cl_entities[enumber].state_current = *EntityFrame4_GetReferenceEntity(d, enumber);
1190 s = &cl_entities[enumber].state_current;
1191 // if this is the one to modify, read more data...
1192 if (enumber == cnumber)
1197 if (developer_networkentities.integer >= 2)
1198 Con_Printf("entity %i: remove\n", enumber);
1204 if (developer_networkentities.integer >= 2)
1205 Con_Printf("entity %i: update\n", enumber);
1207 EntityState_ReadFields(s, EntityState_ReadExtendBits());
1210 else if (developer_networkentities.integer >= 4)
1211 Con_Printf("entity %i: copy\n", enumber);
1212 // set the cl_entities_active flag
1213 cl_entities_active[enumber] = s->active;
1214 // set the update time
1215 s->time = cl.mtime[0];
1216 // fix the number (it gets wiped occasionally by copying from defaultstate)
1217 s->number = enumber;
1218 // check if we need to update the lerp stuff
1220 CL_MoveLerpEntityStates(&cl_entities[enumber]);
1221 // add this to the commit entry whether it is modified or not
1222 if (d->currentcommit)
1223 EntityFrame4_AddCommitEntity(d, &cl_entities[enumber].state_current);
1224 // print extra messages if desired
1225 if (developer_networkentities.integer >= 2 && cl_entities[enumber].state_current.active != cl_entities[enumber].state_previous.active)
1227 if (cl_entities[enumber].state_current.active)
1228 Con_Printf("entity #%i has become active\n", enumber);
1229 else if (cl_entities[enumber].state_previous.active)
1230 Con_Printf("entity #%i has become inactive\n", enumber);
1234 d->currentcommit = NULL;
1236 EntityFrame4_ResetDatabase(d);
1239 void EntityFrame4_WriteFrame(sizebuf_t *msg, entityframe4_database_t *d, int numstates, const entity_state_t *states)
1241 const entity_state_t *e, *s;
1242 entity_state_t inactiveentitystate;
1243 int i, n, startnumber;
1247 // if there isn't enough space to accomplish anything, skip it
1248 if (msg->cursize + 24 > msg->maxsize)
1251 // prepare the buffer
1252 memset(&buf, 0, sizeof(buf));
1254 buf.maxsize = sizeof(data);
1256 for (i = 0;i < MAX_ENTITY_HISTORY;i++)
1257 if (!d->commit[i].numentities)
1259 // if commit buffer full, just don't bother writing an update this frame
1260 if (i == MAX_ENTITY_HISTORY)
1262 d->currentcommit = d->commit + i;
1264 // this state's number gets played around with later
1265 inactiveentitystate = defaultstate;
1267 d->currentcommit->numentities = 0;
1268 d->currentcommit->framenum = ++d->latestframenumber;
1269 MSG_WriteByte(msg, svc_entities);
1270 MSG_WriteLong(msg, d->referenceframenum);
1271 MSG_WriteLong(msg, d->currentcommit->framenum);
1272 if (developer_networkentities.integer >= 1)
1274 Con_Printf("send svc_entities num:%i ref:%i (database: ref:%i commits:", d->currentcommit->framenum, d->referenceframenum, d->referenceframenum);
1275 for (i = 0;i < MAX_ENTITY_HISTORY;i++)
1276 if (d->commit[i].numentities)
1277 Con_Printf(" %i", d->commit[i].framenum);
1280 if (d->currententitynumber >= sv.max_edicts)
1283 startnumber = bound(1, d->currententitynumber, sv.max_edicts - 1);
1284 MSG_WriteShort(msg, startnumber);
1285 // reset currententitynumber so if the loop does not break it we will
1286 // start at beginning next frame (if it does break, it will set it)
1287 d->currententitynumber = 1;
1288 for (i = 0, n = startnumber;n < sv.max_edicts;n++)
1290 // find the old state to delta from
1291 e = EntityFrame4_GetReferenceEntity(d, n);
1292 // prepare the buffer
1294 // entity exists, build an update (if empty there is no change)
1295 // find the state in the list
1296 for (;i < numstates && states[i].number < n;i++);
1302 EntityState_WriteUpdate(s, &buf, e);
1306 inactiveentitystate.number = n;
1307 s = &inactiveentitystate;
1310 // entity used to exist but doesn't anymore, send remove
1311 MSG_WriteShort(&buf, n | 0x8000);
1314 // if the commit is full, we're done this frame
1315 if (msg->cursize + buf.cursize > msg->maxsize - 4)
1317 // next frame we will continue where we left off
1320 // add the entity to the commit
1321 EntityFrame4_AddCommitEntity(d, s);
1322 // if the message is empty, skip out now
1325 // write the message to the packet
1326 SZ_Write(msg, buf.data, buf.cursize);
1329 d->currententitynumber = n;
1331 // remove world message (invalid, and thus a good terminator)
1332 MSG_WriteShort(msg, 0x8000);
1333 // write the number of the end entity
1334 MSG_WriteShort(msg, d->currententitynumber);
1336 d->currentcommit = NULL;
1342 #define E5_PROTOCOL_PRIORITYLEVELS 32
1344 entityframe5_database_t *EntityFrame5_AllocDatabase(mempool_t *pool)
1346 entityframe5_database_t *d;
1347 d = Mem_Alloc(pool, sizeof(*d));
1348 EntityFrame5_ResetDatabase(d);
1352 void EntityFrame5_FreeDatabase(entityframe5_database_t *d)
1357 void EntityFrame5_ResetDatabase(entityframe5_database_t *d)
1360 memset(d, 0, sizeof(*d));
1361 d->latestframenum = 0;
1362 for (i = 0;i < MAX_EDICTS;i++)
1363 d->states[i] = defaultstate;
1367 int EntityState5_Priority(entityframe5_database_t *d, entity_state_t *view, entity_state_t *s, int changedbits, int age)
1369 int lowprecision, limit, priority;
1373 if (!s->active/* && changedbits & E5_FULLUPDATE*/)
1374 return E5_PROTOCOL_PRIORITYLEVELS - 1;
1375 // check whole attachment chain to judge relevance to player
1376 lowprecision = false;
1377 for (limit = 0;limit < 256;limit++)
1380 return E5_PROTOCOL_PRIORITYLEVELS - 1;
1381 if (s->flags & RENDER_VIEWMODEL)
1382 return E5_PROTOCOL_PRIORITYLEVELS - 1;
1383 if (s->flags & RENDER_LOWPRECISION)
1384 lowprecision = true;
1387 if (VectorCompare(s->origin, view->origin))
1388 return E5_PROTOCOL_PRIORITYLEVELS - 1;
1391 s = d->states + s->tagentity;
1394 Con_Printf("Protocol: Runaway loop recursing tagentity links on entity %i\n", s->number);
1395 // it's not a viewmodel for this client
1396 distance = VectorDistance(view->origin, s->origin);
1397 priority = (E5_PROTOCOL_PRIORITYLEVELS / 2) + age - (int)(distance * (E5_PROTOCOL_PRIORITYLEVELS / 16384.0f));
1399 priority -= (E5_PROTOCOL_PRIORITYLEVELS / 4);
1400 //if (changedbits & E5_FULLUPDATE)
1402 //if (changedbits & (E5_ATTACHMENT | E5_MODEL | E5_FLAGS | E5_COLORMAP))
1404 return (int) bound(1, priority, E5_PROTOCOL_PRIORITYLEVELS - 1);
1407 void EntityState5_WriteUpdate(int number, const entity_state_t *s, int changedbits, sizebuf_t *msg)
1409 unsigned int bits = 0;
1411 MSG_WriteShort(msg, number | 0x8000);
1415 if ((bits & E5_ORIGIN) && (s->origin[0] < -4096 || s->origin[0] >= 4096 || s->origin[1] < -4096 || s->origin[1] >= 4096 || s->origin[2] < -4096 || s->origin[2] >= 4096))
1416 bits |= E5_ORIGIN32;
1417 if ((bits & E5_ANGLES) && !(s->flags & RENDER_LOWPRECISION))
1418 bits |= E5_ANGLES16;
1419 if ((bits & E5_MODEL) && s->modelindex >= 256)
1421 if ((bits & E5_FRAME) && s->frame >= 256)
1423 if (bits & E5_EFFECTS)
1425 if (s->effects >= 65536)
1426 bits |= E5_EFFECTS32;
1427 else if (s->effects >= 256)
1428 bits |= E5_EFFECTS16;
1434 if (bits >= 16777216)
1436 MSG_WriteShort(msg, number);
1437 MSG_WriteByte(msg, bits & 0xFF);
1438 if (bits & E5_EXTEND1)
1439 MSG_WriteByte(msg, (bits >> 8) & 0xFF);
1440 if (bits & E5_EXTEND2)
1441 MSG_WriteByte(msg, (bits >> 16) & 0xFF);
1442 if (bits & E5_EXTEND3)
1443 MSG_WriteByte(msg, (bits >> 24) & 0xFF);
1444 if (bits & E5_FLAGS)
1445 MSG_WriteByte(msg, s->flags);
1446 if (bits & E5_ORIGIN)
1448 if (bits & E5_ORIGIN32)
1450 MSG_WriteCoord32f(msg, s->origin[0]);
1451 MSG_WriteCoord32f(msg, s->origin[1]);
1452 MSG_WriteCoord32f(msg, s->origin[2]);
1456 MSG_WriteCoord13i(msg, s->origin[0]);
1457 MSG_WriteCoord13i(msg, s->origin[1]);
1458 MSG_WriteCoord13i(msg, s->origin[2]);
1461 if (bits & E5_ANGLES)
1463 if (bits & E5_ANGLES16)
1465 MSG_WriteAngle16i(msg, s->angles[0]);
1466 MSG_WriteAngle16i(msg, s->angles[1]);
1467 MSG_WriteAngle16i(msg, s->angles[2]);
1471 MSG_WriteAngle8i(msg, s->angles[0]);
1472 MSG_WriteAngle8i(msg, s->angles[1]);
1473 MSG_WriteAngle8i(msg, s->angles[2]);
1476 if (bits & E5_MODEL)
1478 if (bits & E5_MODEL16)
1479 MSG_WriteShort(msg, s->modelindex);
1481 MSG_WriteByte(msg, s->modelindex);
1483 if (bits & E5_FRAME)
1485 if (bits & E5_FRAME16)
1486 MSG_WriteShort(msg, s->frame);
1488 MSG_WriteByte(msg, s->frame);
1491 MSG_WriteByte(msg, s->skin);
1492 if (bits & E5_EFFECTS)
1494 if (bits & E5_EFFECTS32)
1495 MSG_WriteLong(msg, s->effects);
1496 else if (bits & E5_EFFECTS16)
1497 MSG_WriteShort(msg, s->effects);
1499 MSG_WriteByte(msg, s->effects);
1501 if (bits & E5_ALPHA)
1502 MSG_WriteByte(msg, s->alpha);
1503 if (bits & E5_SCALE)
1504 MSG_WriteByte(msg, s->scale);
1505 if (bits & E5_COLORMAP)
1506 MSG_WriteByte(msg, s->colormap);
1507 if (bits & E5_ATTACHMENT)
1509 MSG_WriteShort(msg, s->tagentity);
1510 MSG_WriteByte(msg, s->tagindex);
1512 if (bits & E5_LIGHT)
1514 MSG_WriteShort(msg, s->light[0]);
1515 MSG_WriteShort(msg, s->light[1]);
1516 MSG_WriteShort(msg, s->light[2]);
1517 MSG_WriteShort(msg, s->light[3]);
1518 MSG_WriteByte(msg, s->lightstyle);
1519 MSG_WriteByte(msg, s->lightpflags);
1523 MSG_WriteByte(msg, s->glowsize);
1524 MSG_WriteByte(msg, s->glowcolor);
1526 if (bits & E5_COLORMOD)
1528 MSG_WriteByte(msg, s->colormod[0]);
1529 MSG_WriteByte(msg, s->colormod[1]);
1530 MSG_WriteByte(msg, s->colormod[2]);
1535 void EntityState5_ReadUpdate(entity_state_t *s)
1538 bits = MSG_ReadByte();
1539 if (bits & E5_EXTEND1)
1541 bits |= MSG_ReadByte() << 8;
1542 if (bits & E5_EXTEND2)
1544 bits |= MSG_ReadByte() << 16;
1545 if (bits & E5_EXTEND3)
1546 bits |= MSG_ReadByte() << 24;
1549 if (bits & E5_FULLUPDATE)
1554 if (bits & E5_FLAGS)
1555 s->flags = MSG_ReadByte();
1556 if (bits & E5_ORIGIN)
1558 if (bits & E5_ORIGIN32)
1560 s->origin[0] = MSG_ReadCoord32f();
1561 s->origin[1] = MSG_ReadCoord32f();
1562 s->origin[2] = MSG_ReadCoord32f();
1566 s->origin[0] = MSG_ReadCoord13i();
1567 s->origin[1] = MSG_ReadCoord13i();
1568 s->origin[2] = MSG_ReadCoord13i();
1571 if (bits & E5_ANGLES)
1573 if (bits & E5_ANGLES16)
1575 s->angles[0] = MSG_ReadAngle16i();
1576 s->angles[1] = MSG_ReadAngle16i();
1577 s->angles[2] = MSG_ReadAngle16i();
1581 s->angles[0] = MSG_ReadAngle8i();
1582 s->angles[1] = MSG_ReadAngle8i();
1583 s->angles[2] = MSG_ReadAngle8i();
1586 if (bits & E5_MODEL)
1588 if (bits & E5_MODEL16)
1589 s->modelindex = (unsigned short) MSG_ReadShort();
1591 s->modelindex = MSG_ReadByte();
1593 if (bits & E5_FRAME)
1595 if (bits & E5_FRAME16)
1596 s->frame = (unsigned short) MSG_ReadShort();
1598 s->frame = MSG_ReadByte();
1601 s->skin = MSG_ReadByte();
1602 if (bits & E5_EFFECTS)
1604 if (bits & E5_EFFECTS32)
1605 s->effects = (unsigned int) MSG_ReadLong();
1606 else if (bits & E5_EFFECTS16)
1607 s->effects = (unsigned short) MSG_ReadShort();
1609 s->effects = MSG_ReadByte();
1611 if (bits & E5_ALPHA)
1612 s->alpha = MSG_ReadByte();
1613 if (bits & E5_SCALE)
1614 s->scale = MSG_ReadByte();
1615 if (bits & E5_COLORMAP)
1616 s->colormap = MSG_ReadByte();
1617 if (bits & E5_ATTACHMENT)
1619 s->tagentity = (unsigned short) MSG_ReadShort();
1620 s->tagindex = MSG_ReadByte();
1622 if (bits & E5_LIGHT)
1624 s->light[0] = (unsigned short) MSG_ReadShort();
1625 s->light[1] = (unsigned short) MSG_ReadShort();
1626 s->light[2] = (unsigned short) MSG_ReadShort();
1627 s->light[3] = (unsigned short) MSG_ReadShort();
1628 s->lightstyle = MSG_ReadByte();
1629 s->lightpflags = MSG_ReadByte();
1633 s->glowsize = MSG_ReadByte();
1634 s->glowcolor = MSG_ReadByte();
1636 if (bits & E5_COLORMOD)
1638 s->colormod[0] = MSG_ReadByte();
1639 s->colormod[1] = MSG_ReadByte();
1640 s->colormod[2] = MSG_ReadByte();
1644 if (developer_networkentities.integer >= 2)
1646 Con_Printf("ReadFields e%i", s->number);
1648 if (bits & E5_ORIGIN)
1649 Con_Printf(" E5_ORIGIN %f %f %f", s->origin[0], s->origin[1], s->origin[2]);
1650 if (bits & E5_ANGLES)
1651 Con_Printf(" E5_ANGLES %f %f %f", s->angles[0], s->angles[1], s->angles[2]);
1652 if (bits & E5_MODEL)
1653 Con_Printf(" E5_MODEL %i", s->modelindex);
1654 if (bits & E5_FRAME)
1655 Con_Printf(" E5_FRAME %i", s->frame);
1657 Con_Printf(" E5_SKIN %i", s->skin);
1658 if (bits & E5_EFFECTS)
1659 Con_Printf(" E5_EFFECTS %i", s->effects);
1660 if (bits & E5_FLAGS)
1662 Con_Printf(" E5_FLAGS %i (", s->flags);
1663 if (s->flags & RENDER_STEP)
1665 if (s->flags & RENDER_GLOWTRAIL)
1666 Con_Print(" GLOWTRAIL");
1667 if (s->flags & RENDER_VIEWMODEL)
1668 Con_Print(" VIEWMODEL");
1669 if (s->flags & RENDER_EXTERIORMODEL)
1670 Con_Print(" EXTERIORMODEL");
1671 if (s->flags & RENDER_LOWPRECISION)
1672 Con_Print(" LOWPRECISION");
1673 if (s->flags & RENDER_COLORMAPPED)
1674 Con_Print(" COLORMAPPED");
1675 if (s->flags & RENDER_SHADOW)
1676 Con_Print(" SHADOW");
1677 if (s->flags & RENDER_LIGHT)
1678 Con_Print(" LIGHT");
1681 if (bits & E5_ALPHA)
1682 Con_Printf(" E5_ALPHA %f", s->alpha / 255.0f);
1683 if (bits & E5_SCALE)
1684 Con_Printf(" E5_SCALE %f", s->scale / 16.0f);
1685 if (bits & E5_COLORMAP)
1686 Con_Printf(" E5_COLORMAP %i", s->colormap);
1687 if (bits & E5_ATTACHMENT)
1688 Con_Printf(" E5_ATTACHMENT e%i:%i", s->tagentity, s->tagindex);
1689 if (bits & E5_LIGHT)
1690 Con_Printf(" E5_LIGHT %i:%i:%i:%i %i:%i", s->light[0], s->light[1], s->light[2], s->light[3], s->lightstyle, s->lightpflags);
1692 Con_Printf(" E5_GLOW %i:%i", s->glowsize * 4, s->glowcolor);
1693 if (bits & E5_COLORMOD)
1694 Con_Printf(" E5_COLORMOD %f:%f:%f", s->colormod[0] / 32.0f, s->colormod[1] / 32.0f, s->colormod[2] / 32.0f);
1699 int EntityState5_DeltaBits(const entity_state_t *o, const entity_state_t *n)
1701 unsigned int bits = 0;
1705 bits |= E5_FULLUPDATE;
1706 if (!VectorCompare(o->origin, n->origin))
1708 if (!VectorCompare(o->angles, n->angles))
1710 if (o->modelindex != n->modelindex)
1712 if (o->frame != n->frame)
1714 if (o->skin != n->skin)
1716 if (o->effects != n->effects)
1718 if (o->flags != n->flags)
1720 if (o->alpha != n->alpha)
1722 if (o->scale != n->scale)
1724 if (o->colormap != n->colormap)
1725 bits |= E5_COLORMAP;
1726 if (o->tagentity != n->tagentity || o->tagindex != n->tagindex)
1727 bits |= E5_ATTACHMENT;
1728 if (o->light[0] != n->light[0] || o->light[1] != n->light[1] || o->light[2] != n->light[2] || o->light[3] != n->light[3] || o->lightstyle != n->lightstyle || o->lightpflags != n->lightpflags)
1730 if (o->glowsize != n->glowsize || o->glowcolor != n->glowcolor)
1732 if (o->colormod[0] != n->colormod[0] || o->colormod[1] != n->colormod[1] || o->colormod[2] != n->colormod[2])
1733 bits |= E5_COLORMOD;
1737 bits |= E5_FULLUPDATE;
1741 void EntityFrame5_CL_ReadFrame(void)
1746 // read the number of this frame to echo back in next input packet
1747 for (i = 0;i < LATESTFRAMENUMS-1;i++)
1748 cl.latestframenums[i] = cl.latestframenums[i+1];
1749 cl.latestframenums[LATESTFRAMENUMS-1] = MSG_ReadLong();
1750 // read entity numbers until we find a 0x8000
1751 // (which would be remove world entity, but is actually a terminator)
1752 while ((n = (unsigned short)MSG_ReadShort()) != 0x8000 && !msg_badread)
1754 // get the entity number and look it up
1755 enumber = n & 0x7FFF;
1756 ent = cl_entities + enumber;
1757 // slide the current into the previous slot
1758 ent->state_previous = ent->state_current;
1760 s = &ent->state_current;
1769 EntityState5_ReadUpdate(s);
1771 // set the cl_entities_active flag
1772 cl_entities_active[enumber] = s->active;
1773 // set the update time
1774 s->time = cl.mtime[0];
1775 // fix the number (it gets wiped occasionally by copying from defaultstate)
1776 s->number = enumber;
1777 // check if we need to update the lerp stuff
1779 CL_MoveLerpEntityStates(&cl_entities[enumber]);
1780 // print extra messages if desired
1781 if (developer_networkentities.integer >= 2 && cl_entities[enumber].state_current.active != cl_entities[enumber].state_previous.active)
1783 if (cl_entities[enumber].state_current.active)
1784 Con_Printf("entity #%i has become active\n", enumber);
1785 else if (cl_entities[enumber].state_previous.active)
1786 Con_Printf("entity #%i has become inactive\n", enumber);
1791 void EntityFrame5_LostFrame(entityframe5_database_t *d, int framenum, int viewentnum)
1793 int i, j, k, l, bits;
1794 entityframe5_changestate_t *s, *s2;
1795 entityframe5_packetlog_t *p, *p2;
1796 qbyte statsdeltabits[(MAX_CL_STATS+7)/8];
1797 // scan for packets that were lost
1798 for (i = 0, p = d->packetlog;i < ENTITYFRAME5_MAXPACKETLOGS;i++, p++)
1800 if (p->packetnumber && p->packetnumber <= framenum)
1802 // packet was lost - merge deltabits into the main array so they
1803 // will be re-sent, but only if there is no newer update of that
1804 // bit in the logs (as those will arrive before this update)
1805 for (j = 0, s = p->states;j < p->numstates;j++, s++)
1807 // check for any newer updates to this entity and mask off any
1808 // overlapping bits (we don't need to send something again if
1809 // it has already been sent more recently)
1810 bits = s->bits & ~d->deltabits[s->number];
1811 for (k = 0, p2 = d->packetlog;k < ENTITYFRAME5_MAXPACKETLOGS && bits;k++, p2++)
1813 if (p2->packetnumber > framenum)
1815 for (l = 0, s2 = p2->states;l < p2->numstates;l++, s2++)
1817 if (s2->number == s->number)
1825 // if the bits haven't all been cleared, there were some bits
1826 // lost with this packet, so set them again now
1829 d->deltabits[s->number] |= bits;
1830 d->priorities[s->number] = EntityState5_Priority(d, d->states + viewentnum, d->states + s->number, d->deltabits[s->number], d->latestframenum - d->updateframenum[s->number]);
1834 for (j = 0;j < MAX_CL_STATS;j++)
1836 for (l = 0;l < (MAX_CL_STATS+7)/8;l++)
1837 statsdeltabits[l] = p->statsdeltabits[l] & ~d->statsdeltabits[l];
1838 for (k = 0, p2 = d->packetlog;k < ENTITYFRAME5_MAXPACKETLOGS;k++, p2++)
1839 if (p2->packetnumber > framenum)
1840 for (l = 0;l < (MAX_CL_STATS+7)/8;l++)
1841 statsdeltabits[l] = p->statsdeltabits[l] & ~p2->statsdeltabits[l];
1842 for (l = 0;l < (MAX_CL_STATS+7)/8;l++)
1843 d->statsdeltabits[l] |= statsdeltabits[l];
1845 // delete this packet log as it is now obsolete
1846 p->packetnumber = 0;
1851 void EntityFrame5_AckFrame(entityframe5_database_t *d, int framenum)
1854 // scan for packets made obsolete by this ack and delete them
1855 for (i = 0;i < ENTITYFRAME5_MAXPACKETLOGS;i++)
1856 if (d->packetlog[i].packetnumber <= framenum)
1857 d->packetlog[i].packetnumber = 0;
1860 int entityframe5_prioritychaincounts[E5_PROTOCOL_PRIORITYLEVELS];
1861 unsigned short entityframe5_prioritychains[E5_PROTOCOL_PRIORITYLEVELS][ENTITYFRAME5_MAXSTATES];
1863 void EntityFrame5_WriteFrame(sizebuf_t *msg, entityframe5_database_t *d, int numstates, const entity_state_t *states, int viewentnum, int *stats)
1865 const entity_state_t *n;
1866 int i, num, l, framenum, packetlognumber, priority;
1869 entityframe5_packetlog_t *packetlog;
1871 framenum = d->latestframenum + 1;
1873 // if packet log is full, mark all frames as lost, this will cause
1874 // it to send the lost data again
1875 for (packetlognumber = 0;packetlognumber < ENTITYFRAME5_MAXPACKETLOGS;packetlognumber++)
1876 if (d->packetlog[packetlognumber].packetnumber == 0)
1878 if (packetlognumber == ENTITYFRAME5_MAXPACKETLOGS)
1880 EntityFrame5_LostFrame(d, framenum, viewentnum);
1881 packetlognumber = 0;
1884 // prepare the buffer
1885 memset(&buf, 0, sizeof(buf));
1887 buf.maxsize = sizeof(data);
1889 // detect changes in stats
1890 for (i = 0;i < MAX_CL_STATS;i++)
1892 if (d->stats[i] != stats[i])
1894 d->statsdeltabits[i>>3] |= (1<<(i&7));
1895 d->stats[i] = stats[i];
1899 // detect changes in states
1901 for (i = 0, n = states;i < numstates;i++, n++)
1903 // mark gaps in entity numbering as removed entities
1904 for (;num < n->number;num++)
1906 // if the entity used to exist, clear it
1907 if (CHECKPVSBIT(d->visiblebits, num))
1909 CLEARPVSBIT(d->visiblebits, num);
1910 d->deltabits[num] = E5_FULLUPDATE;
1911 d->priorities[num] = EntityState5_Priority(d, d->states + viewentnum, d->states + num, d->deltabits[num], framenum - d->updateframenum[num]);
1912 d->states[num] = defaultstate;
1913 d->states[num].number = num;
1916 // update the entity state data
1917 if (!CHECKPVSBIT(d->visiblebits, num))
1919 // entity just spawned in, don't let it completely hog priority
1920 // because of being ancient on the first frame
1921 d->updateframenum[num] = framenum;
1923 SETPVSBIT(d->visiblebits, num);
1924 d->deltabits[num] |= EntityState5_DeltaBits(d->states + num, n);
1925 d->priorities[num] = EntityState5_Priority(d, d->states + viewentnum, d->states + num, d->deltabits[num], framenum - d->updateframenum[num]);
1926 d->states[num] = *n;
1927 d->states[num].number = num;
1928 // advance to next entity so the next iteration doesn't immediately remove it
1931 // all remaining entities are dead
1932 for (;num < MAX_EDICTS;num++)
1934 if (CHECKPVSBIT(d->visiblebits, num))
1936 CLEARPVSBIT(d->visiblebits, num);
1937 d->deltabits[num] = E5_FULLUPDATE;
1938 d->priorities[num] = EntityState5_Priority(d, d->states + viewentnum, d->states + num, d->deltabits[num], framenum - d->updateframenum[num]);
1939 d->states[num] = defaultstate;
1940 d->states[num].number = num;
1944 // build lists of entities by priority level
1945 memset(entityframe5_prioritychaincounts, 0, sizeof(entityframe5_prioritychaincounts));
1947 for (num = 0;num < MAX_EDICTS;num++)
1949 if (d->priorities[num])
1952 priority = d->priorities[num];
1953 if (entityframe5_prioritychaincounts[priority] < ENTITYFRAME5_MAXSTATES)
1954 entityframe5_prioritychains[priority][entityframe5_prioritychaincounts[priority]++] = num;
1958 // add packetlog entry
1959 packetlog = d->packetlog + packetlognumber;
1960 packetlog->packetnumber = framenum;
1961 packetlog->numstates = 0;
1962 // write stat updates
1963 if (sv.protocol == PROTOCOL_DARKPLACES6)
1965 for (i = 0;i < MAX_CL_STATS;i++)
1967 if (d->statsdeltabits[i>>3] & (1<<(i&7)))
1969 d->statsdeltabits[i>>3] &= ~(1<<(i&7));
1970 packetlog->statsdeltabits[i>>3] |= (1<<(i&7));
1971 if (d->stats[i] >= 0 && d->stats[i] < 256)
1973 MSG_WriteByte(msg, svc_updatestatubyte);
1974 MSG_WriteByte(msg, i);
1975 MSG_WriteByte(msg, d->stats[i]);
1979 MSG_WriteByte(msg, svc_updatestat);
1980 MSG_WriteByte(msg, i);
1981 MSG_WriteLong(msg, d->stats[i]);
1986 // write state updates
1987 d->latestframenum = framenum;
1988 MSG_WriteByte(msg, svc_entities);
1989 MSG_WriteLong(msg, framenum);
1990 for (priority = E5_PROTOCOL_PRIORITYLEVELS - 1;priority >= 0 && packetlog->numstates < ENTITYFRAME5_MAXSTATES;priority--)
1992 for (i = 0;i < entityframe5_prioritychaincounts[priority] && packetlog->numstates < ENTITYFRAME5_MAXSTATES;i++)
1994 num = entityframe5_prioritychains[priority][i];
1995 n = d->states + num;
1996 if (d->deltabits[num] & E5_FULLUPDATE)
1997 d->deltabits[num] = E5_FULLUPDATE | EntityState5_DeltaBits(&defaultstate, n);
1999 EntityState5_WriteUpdate(num, n, d->deltabits[num], &buf);
2000 // if the entity won't fit, try the next one
2001 if (msg->cursize + buf.cursize + 2 > msg->maxsize)
2003 // write entity to the packet
2004 SZ_Write(msg, buf.data, buf.cursize);
2005 // mark age on entity for prioritization
2006 d->updateframenum[num] = framenum;
2007 // log entity so deltabits can be restored later if lost
2008 packetlog->states[packetlog->numstates].number = num;
2009 packetlog->states[packetlog->numstates].bits = d->deltabits[num];
2010 packetlog->numstates++;
2011 // clear deltabits and priority so it won't be sent again
2012 d->deltabits[num] = 0;
2013 d->priorities[num] = 0;
2016 MSG_WriteShort(msg, 0x8000);