4 // this is 88 bytes (must match entity_state_t in protocol.h)
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 netcenter[3]; // ! for network prioritization, this is the center of the bounding box (which may differ from the origin)
10 {0,0,0},//float origin[3];
11 {0,0,0},//float angles[3];
12 0,//int number; // entity number this state is for
14 0,//unsigned short modelindex;
15 0,//unsigned short frame;
16 0,//unsigned short tagentity;
17 {0,0,0,0},//unsigned short light[4]; // color*256 (0.00 to 255.996), and radius*1
18 0,//unsigned char active; // true if a valid state
19 0,//unsigned char lightstyle;
20 0,//unsigned char lightpflags;
21 0,//unsigned char colormap;
22 0,//unsigned char skin; // also chooses cubemap for rtlights if lightpflags & LIGHTPFLAGS_FULLDYNAMIC
23 255,//unsigned char alpha;
24 16,//unsigned char scale;
25 0,//unsigned char glowsize;
26 254,//unsigned char glowcolor;
27 0,//unsigned char flags;
28 0,//unsigned char internaleffects; // INTEF_FLAG1QW and so on
29 0,//unsigned char tagindex;
30 {32, 32, 32},//unsigned char colormod[3];
31 // padding to a multiple of 8 bytes (to align the double time)
32 0//unsigned char unused; // !
35 // LordHavoc: I own protocol ranges 96, 97, 3500-3599
37 struct protocolversioninfo_s
42 protocolversioninfo[] =
53 {250, "NEHAHRAMOVIE"},
59 protocolversion_t Protocol_EnumForName(const char *s)
62 for (i = 1;protocolversioninfo[i].name;i++)
63 if (!strcasecmp(s, protocolversioninfo[i].name))
64 return (protocolversion_t)i;
65 return PROTOCOL_UNKNOWN;
68 const char *Protocol_NameForEnum(protocolversion_t p)
70 return protocolversioninfo[p].name;
73 protocolversion_t Protocol_EnumForNumber(int n)
76 for (i = 1;protocolversioninfo[i].name;i++)
77 if (protocolversioninfo[i].number == n)
78 return (protocolversion_t)i;
79 return PROTOCOL_UNKNOWN;
82 int Protocol_NumberForEnum(protocolversion_t p)
84 return protocolversioninfo[p].number;
87 void Protocol_Names(char *buffer, size_t buffersize)
93 for (i = 1;protocolversioninfo[i].name;i++)
96 strlcat(buffer, " ", buffersize);
97 strlcat(buffer, protocolversioninfo[i].name, buffersize);
101 void EntityFrameQuake_ReadEntity(int bits)
107 if (bits & U_MOREBITS)
108 bits |= (MSG_ReadByte()<<8);
109 if ((bits & U_EXTEND1) && cls.protocol != PROTOCOL_NEHAHRAMOVIE)
111 bits |= MSG_ReadByte() << 16;
112 if (bits & U_EXTEND2)
113 bits |= MSG_ReadByte() << 24;
116 if (bits & U_LONGENTITY)
117 num = (unsigned short) MSG_ReadShort ();
119 num = MSG_ReadByte ();
121 if (num >= MAX_EDICTS)
122 Host_Error("EntityFrameQuake_ReadEntity: entity number (%i) >= MAX_EDICTS (%i)", num, MAX_EDICTS);
124 Host_Error("EntityFrameQuake_ReadEntity: invalid entity number (%i)", num);
126 if (cl.num_entities <= num)
128 cl.num_entities = num + 1;
129 if (num >= cl.max_entities)
130 CL_ExpandEntities(num);
133 ent = cl.entities + num;
135 // note: this inherits the 'active' state of the baseline chosen
136 // (state_baseline is always active, state_current may not be active if
137 // the entity was missing in the last frame)
139 s = ent->state_current;
142 s = ent->state_baseline;
146 cl.isquakeentity[num] = true;
147 if (cl.lastquakeentity < num)
148 cl.lastquakeentity = num;
150 s.time = cl.mtime[0];
152 if (bits & U_MODEL) s.modelindex = (s.modelindex & 0xFF00) | MSG_ReadByte();
153 if (bits & U_FRAME) s.frame = (s.frame & 0xFF00) | MSG_ReadByte();
154 if (bits & U_COLORMAP) s.colormap = MSG_ReadByte();
155 if (bits & U_SKIN) s.skin = MSG_ReadByte();
156 if (bits & U_EFFECTS) s.effects = (s.effects & 0xFF00) | MSG_ReadByte();
157 if (bits & U_ORIGIN1) s.origin[0] = MSG_ReadCoord(cls.protocol);
158 if (bits & U_ANGLE1) s.angles[0] = MSG_ReadAngle(cls.protocol);
159 if (bits & U_ORIGIN2) s.origin[1] = MSG_ReadCoord(cls.protocol);
160 if (bits & U_ANGLE2) s.angles[1] = MSG_ReadAngle(cls.protocol);
161 if (bits & U_ORIGIN3) s.origin[2] = MSG_ReadCoord(cls.protocol);
162 if (bits & U_ANGLE3) s.angles[2] = MSG_ReadAngle(cls.protocol);
163 if (bits & U_STEP) s.flags |= RENDER_STEP;
164 if (bits & U_ALPHA) s.alpha = MSG_ReadByte();
165 if (bits & U_SCALE) s.scale = MSG_ReadByte();
166 if (bits & U_EFFECTS2) s.effects = (s.effects & 0x00FF) | (MSG_ReadByte() << 8);
167 if (bits & U_GLOWSIZE) s.glowsize = MSG_ReadByte();
168 if (bits & U_GLOWCOLOR) s.glowcolor = MSG_ReadByte();
169 if (bits & U_COLORMOD) {int c = MSG_ReadByte();s.colormod[0] = (unsigned char)(((c >> 5) & 7) * (32.0f / 7.0f));s.colormod[1] = (unsigned char)(((c >> 2) & 7) * (32.0f / 7.0f));s.colormod[2] = (unsigned char)((c & 3) * (32.0f / 3.0f));}
170 if (bits & U_GLOWTRAIL) s.flags |= RENDER_GLOWTRAIL;
171 if (bits & U_FRAME2) s.frame = (s.frame & 0x00FF) | (MSG_ReadByte() << 8);
172 if (bits & U_MODEL2) s.modelindex = (s.modelindex & 0x00FF) | (MSG_ReadByte() << 8);
173 if (bits & U_VIEWMODEL) s.flags |= RENDER_VIEWMODEL;
174 if (bits & U_EXTERIORMODEL) s.flags |= RENDER_EXTERIORMODEL;
176 // LordHavoc: to allow playback of the Nehahra movie
177 if (cls.protocol == PROTOCOL_NEHAHRAMOVIE && (bits & U_EXTEND1))
179 // LordHavoc: evil format
180 int i = (int)MSG_ReadFloat();
181 int j = (int)(MSG_ReadFloat() * 255.0f);
184 i = (int)MSG_ReadFloat();
186 s.effects |= EF_FULLBRIGHT;
190 else if (j == 0 || j >= 255)
196 ent->state_previous = ent->state_current;
197 ent->state_current = s;
198 if (ent->state_current.active)
200 CL_MoveLerpEntityStates(ent);
201 cl.entities_active[ent->state_current.number] = true;
205 Host_Error("EntityFrameQuake_ReadEntity: read error");
208 void EntityFrameQuake_ISeeDeadEntities(void)
211 if (cl.lastquakeentity == 0)
213 lastentity = cl.lastquakeentity;
214 cl.lastquakeentity = 0;
215 for (num = 0;num <= lastentity;num++)
217 if (cl.isquakeentity[num])
219 if (cl.entities_active[num] && cl.entities[num].state_current.time == cl.mtime[0])
221 cl.isquakeentity[num] = true;
222 cl.lastquakeentity = num;
226 cl.isquakeentity[num] = false;
227 cl.entities_active[num] = false;
228 cl.entities[num].state_current = defaultstate;
229 cl.entities[num].state_current.number = num;
235 // FIXME FIXME FIXME: at this time the CSQC entity writing does not store
236 // packet logs and thus if an update is lost it is never repeated, this makes
237 // csqc entities useless at the moment.
239 void EntityFrameCSQC_WriteState (sizebuf_t *msg, int number, qboolean doupdate, qboolean *sectionstarted)
242 prvm_eval_t *val, *val2;
246 if (msg->cursize + !*sectionstarted + 2 + 1 + 2 > msg->maxsize)
248 val2 = PRVM_EDICTFIELDVALUE((&prog->edicts[number]), prog->fieldoffsets.Version);
249 version = (int)val2->_float;
250 // LordHavoc: do some validity checks on self.Version
251 // if self.Version reaches 255, it will soon exceed the byte used to
252 // store an entity version in the client struct, so we need to reset
253 // all the version to 1 and force all the existing clients' version of
254 // it to 255 (which we're not allowing to actually occur)
260 val2->_float = version = 1;
261 // since we just reset the Version field to 1, it may accidentally
262 // end up being equal to an existing client version now or in the
263 // future, so to fix this situation we have to loop over all
264 // clients and change their versions for this entity to be -1
265 // which never matches, thus causing them to receive the update
266 // soon, as they should
267 for (i = 0;i < svs.maxclients;i++)
268 if (svs.clients[sv.writeentitiestoclient_clientnumber].csqcentityversion[number])
269 svs.clients[sv.writeentitiestoclient_clientnumber].csqcentityversion[number] = 255;
272 // if the version already matches, we don't need to do anything as the
273 // latest version has already been sent.
274 if (svs.clients[sv.writeentitiestoclient_clientnumber].csqcentityversion[number] == version)
278 // if there's no SendEntity function, treat it as a remove
279 val = PRVM_EDICTFIELDVALUE((&prog->edicts[number]), prog->fieldoffsets.SendEntity);
282 // there is a function to call, save the cursize value incase we
283 // have to do a rollback due to overflow
284 int oldcursize = msg->cursize;
286 MSG_WriteByte(msg, svc_csqcentities);
287 MSG_WriteShort(msg, number);
288 ((int *)prog->globals.generic)[OFS_PARM0] = sv.writeentitiestoclient_cliententitynumber;
289 prog->globals.server->self = number;
290 PRVM_ExecuteProgram(val->function, "Null SendEntity\n");
291 if(prog->globals.generic[OFS_RETURN])
293 if (msg->cursize + 2 > msg->maxsize)
295 // if the packet no longer has enough room to write the
296 // final index code that ends the message, rollback to the
297 // state before we tried to write anything and then return
298 msg->cursize = oldcursize;
299 msg->overflowed = false;
302 // an update has been successfully written, update the version
303 svs.clients[sv.writeentitiestoclient_clientnumber].csqcentityversion[number] = version;
304 // and take note that we have begun the svc_csqcentities
305 // section of the packet
311 // rollback the buffer to its state before the writes
312 msg->cursize = oldcursize;
313 // if the function returned FALSE, simply write a remove
314 // this is done by falling through to the remove code below
319 // write a remove message if needed
320 // if already removed, do nothing
321 if (!svs.clients[sv.writeentitiestoclient_clientnumber].csqcentityversion[number])
323 // if there isn't enough room to write the remove message, just return, as
324 // it will be handled in a later packet
325 if (msg->cursize + !*sectionstarted + 2 + 2 > msg->maxsize)
327 // first write the message identifier if needed
331 MSG_WriteByte(msg, svc_csqcentities);
333 // write the remove message
334 MSG_WriteShort(msg, (unsigned short)number | 0x8000);
335 svs.clients[sv.writeentitiestoclient_clientnumber].csqcentityversion[number] = 0;
338 //[515]: we use only one array per-client for SendEntity feature
339 void EntityFrameCSQC_WriteFrame (sizebuf_t *msg, int numstates, const entity_state_t *states)
342 qboolean sectionstarted = false;
343 const entity_state_t *n;
345 // if this server progs is not CSQC-aware, return early
346 if(prog->fieldoffsets.SendEntity < 0 || prog->fieldoffsets.Version < 0)
348 // make sure there is enough room to store the svc_csqcentities byte,
349 // the terminator (0x0000) and at least one entity update
350 if (msg->cursize + 32 >= msg->maxsize)
354 for (i = 0, n = states;i < numstates;i++, n++)
356 // all entities between the previous entity state and this one are dead
357 for (;num < n->number;num++)
358 if(svs.clients[sv.writeentitiestoclient_clientnumber].csqcentityversion[num])
359 EntityFrameCSQC_WriteState(msg, num, false, §ionstarted);
360 // update this entity
361 EntityFrameCSQC_WriteState(msg, num, true, §ionstarted);
362 // advance to next entity so the next iteration doesn't immediately remove it
365 // all remaining entities are dead
366 for (;num < prog->num_edicts;num++)
367 if(svs.clients[sv.writeentitiestoclient_clientnumber].csqcentityversion[num])
368 EntityFrameCSQC_WriteState(msg, num, false, §ionstarted);
371 // write index 0 to end the update (0 is never used by real entities)
372 MSG_WriteShort(msg, 0);
376 void Protocol_UpdateClientStats(const int *stats)
379 // update the stats array and set deltabits for any changed stats
380 for (i = 0;i < MAX_CL_STATS;i++)
382 if (host_client->stats[i] != stats[i])
384 host_client->statsdeltabits[i >> 3] |= 1 << (i & 7);
385 host_client->stats[i] = stats[i];
390 void Protocol_WriteStatsReliable(void)
393 if (!host_client->netconnection)
395 // detect changes in stats and write reliable messages
396 // this only deals with 32 stats because the older protocols which use
397 // this function can only cope with 32 stats,
398 // they also do not support svc_updatestatubyte which was introduced in
399 // DP6 protocol (except for QW)
400 for (i = 0;i < 32;i++)
402 // quickly skip zero bytes
403 if (!host_client->statsdeltabits[i >> 3])
408 // check if this bit is set
409 if (host_client->statsdeltabits[i >> 3] & (1 << (i & 7)))
411 host_client->statsdeltabits[i >> 3] -= (1 << (i & 7));
412 // send the stat as a byte if possible
413 if (sv.protocol == PROTOCOL_QUAKEWORLD)
415 if (host_client->stats[i] >= 0 && host_client->stats[i] < 256)
417 MSG_WriteByte(&host_client->netconnection->message, qw_svc_updatestat);
418 MSG_WriteByte(&host_client->netconnection->message, i);
419 MSG_WriteByte(&host_client->netconnection->message, host_client->stats[i]);
423 MSG_WriteByte(&host_client->netconnection->message, qw_svc_updatestatlong);
424 MSG_WriteByte(&host_client->netconnection->message, i);
425 MSG_WriteLong(&host_client->netconnection->message, host_client->stats[i]);
430 // this could make use of svc_updatestatubyte in DP6 and later
431 // protocols but those protocols do not use this function
432 MSG_WriteByte(&host_client->netconnection->message, svc_updatestat);
433 MSG_WriteByte(&host_client->netconnection->message, i);
434 MSG_WriteLong(&host_client->netconnection->message, host_client->stats[i]);
441 void EntityFrameQuake_WriteFrame(sizebuf_t *msg, int numstates, const entity_state_t *states)
443 const entity_state_t *s;
444 entity_state_t baseline;
447 unsigned char data[128];
450 // prepare the buffer
451 memset(&buf, 0, sizeof(buf));
453 buf.maxsize = sizeof(data);
455 for (i = 0, s = states;i < numstates;i++, s++)
457 val = PRVM_EDICTFIELDVALUE((&prog->edicts[s->number]), prog->fieldoffsets.SendEntity);
458 if(val && val->function)
461 // prepare the buffer
466 if (s->number >= 256)
467 bits |= U_LONGENTITY;
468 if (s->flags & RENDER_STEP)
470 if (s->flags & RENDER_VIEWMODEL)
472 if (s->flags & RENDER_GLOWTRAIL)
474 if (s->flags & RENDER_EXTERIORMODEL)
475 bits |= U_EXTERIORMODEL;
477 // LordHavoc: old stuff, but rewritten to have more exact tolerances
478 baseline = prog->edicts[s->number].priv.server->baseline;
479 if (baseline.origin[0] != s->origin[0])
481 if (baseline.origin[1] != s->origin[1])
483 if (baseline.origin[2] != s->origin[2])
485 if (baseline.angles[0] != s->angles[0])
487 if (baseline.angles[1] != s->angles[1])
489 if (baseline.angles[2] != s->angles[2])
491 if (baseline.colormap != s->colormap)
493 if (baseline.skin != s->skin)
495 if (baseline.frame != s->frame)
498 if (s->frame & 0xFF00)
501 if (baseline.effects != s->effects)
504 if (s->effects & 0xFF00)
507 if (baseline.modelindex != s->modelindex)
510 if (s->modelindex & 0xFF00)
513 if (baseline.alpha != s->alpha)
515 if (baseline.scale != s->scale)
517 if (baseline.glowsize != s->glowsize)
519 if (baseline.glowcolor != s->glowcolor)
521 if (!VectorCompare(baseline.colormod, s->colormod))
524 // if extensions are disabled, clear the relevant update flags
525 if (sv.protocol == PROTOCOL_QUAKE || sv.protocol == PROTOCOL_NEHAHRAMOVIE)
527 if (sv.protocol == PROTOCOL_NEHAHRAMOVIE)
528 if (s->alpha != 255 || s->effects & EF_FULLBRIGHT)
532 if (bits >= 16777216)
540 MSG_WriteByte (&buf, bits);
541 if (bits & U_MOREBITS) MSG_WriteByte(&buf, bits>>8);
542 if (bits & U_EXTEND1) MSG_WriteByte(&buf, bits>>16);
543 if (bits & U_EXTEND2) MSG_WriteByte(&buf, bits>>24);
544 if (bits & U_LONGENTITY) MSG_WriteShort(&buf, s->number);
545 else MSG_WriteByte(&buf, s->number);
547 if (bits & U_MODEL) MSG_WriteByte(&buf, s->modelindex);
548 if (bits & U_FRAME) MSG_WriteByte(&buf, s->frame);
549 if (bits & U_COLORMAP) MSG_WriteByte(&buf, s->colormap);
550 if (bits & U_SKIN) MSG_WriteByte(&buf, s->skin);
551 if (bits & U_EFFECTS) MSG_WriteByte(&buf, s->effects);
552 if (bits & U_ORIGIN1) MSG_WriteCoord(&buf, s->origin[0], sv.protocol);
553 if (bits & U_ANGLE1) MSG_WriteAngle(&buf, s->angles[0], sv.protocol);
554 if (bits & U_ORIGIN2) MSG_WriteCoord(&buf, s->origin[1], sv.protocol);
555 if (bits & U_ANGLE2) MSG_WriteAngle(&buf, s->angles[1], sv.protocol);
556 if (bits & U_ORIGIN3) MSG_WriteCoord(&buf, s->origin[2], sv.protocol);
557 if (bits & U_ANGLE3) MSG_WriteAngle(&buf, s->angles[2], sv.protocol);
558 if (bits & U_ALPHA) MSG_WriteByte(&buf, s->alpha);
559 if (bits & U_SCALE) MSG_WriteByte(&buf, s->scale);
560 if (bits & U_EFFECTS2) MSG_WriteByte(&buf, s->effects >> 8);
561 if (bits & U_GLOWSIZE) MSG_WriteByte(&buf, s->glowsize);
562 if (bits & U_GLOWCOLOR) MSG_WriteByte(&buf, s->glowcolor);
563 if (bits & U_COLORMOD) {int c = ((int)bound(0, s->colormod[0] * (7.0f / 32.0f), 7) << 5) | ((int)bound(0, s->colormod[1] * (7.0f / 32.0f), 7) << 2) | ((int)bound(0, s->colormod[2] * (3.0f / 32.0f), 3) << 0);MSG_WriteByte(&buf, c);}
564 if (bits & U_FRAME2) MSG_WriteByte(&buf, s->frame >> 8);
565 if (bits & U_MODEL2) MSG_WriteByte(&buf, s->modelindex >> 8);
567 // the nasty protocol
568 if ((bits & U_EXTEND1) && sv.protocol == PROTOCOL_NEHAHRAMOVIE)
570 if (s->effects & EF_FULLBRIGHT)
572 MSG_WriteFloat(&buf, 2); // QSG protocol version
573 MSG_WriteFloat(&buf, s->alpha <= 0 ? 0 : (s->alpha >= 255 ? 1 : s->alpha * (1.0f / 255.0f))); // alpha
574 MSG_WriteFloat(&buf, 1); // fullbright
578 MSG_WriteFloat(&buf, 1); // QSG protocol version
579 MSG_WriteFloat(&buf, s->alpha <= 0 ? 0 : (s->alpha >= 255 ? 1 : s->alpha * (1.0f / 255.0f))); // alpha
583 // if the commit is full, we're done this frame
584 if (msg->cursize + buf.cursize > msg->maxsize)
586 // next frame we will continue where we left off
589 // write the message to the packet
590 SZ_Write(msg, buf.data, buf.cursize);
594 int EntityState_DeltaBits(const entity_state_t *o, const entity_state_t *n)
597 // if o is not active, delta from default
601 if (fabs(n->origin[0] - o->origin[0]) > (1.0f / 256.0f))
603 if (fabs(n->origin[1] - o->origin[1]) > (1.0f / 256.0f))
605 if (fabs(n->origin[2] - o->origin[2]) > (1.0f / 256.0f))
607 if ((unsigned char) (n->angles[0] * (256.0f / 360.0f)) != (unsigned char) (o->angles[0] * (256.0f / 360.0f)))
609 if ((unsigned char) (n->angles[1] * (256.0f / 360.0f)) != (unsigned char) (o->angles[1] * (256.0f / 360.0f)))
611 if ((unsigned char) (n->angles[2] * (256.0f / 360.0f)) != (unsigned char) (o->angles[2] * (256.0f / 360.0f)))
613 if ((n->modelindex ^ o->modelindex) & 0x00FF)
615 if ((n->modelindex ^ o->modelindex) & 0xFF00)
617 if ((n->frame ^ o->frame) & 0x00FF)
619 if ((n->frame ^ o->frame) & 0xFF00)
621 if ((n->effects ^ o->effects) & 0x00FF)
623 if ((n->effects ^ o->effects) & 0xFF00)
625 if (n->colormap != o->colormap)
627 if (n->skin != o->skin)
629 if (n->alpha != o->alpha)
631 if (n->scale != o->scale)
633 if (n->glowsize != o->glowsize)
635 if (n->glowcolor != o->glowcolor)
637 if (n->flags != o->flags)
639 if (n->tagindex != o->tagindex || n->tagentity != o->tagentity)
640 bits |= E_TAGATTACHMENT;
641 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])
643 if (n->lightstyle != o->lightstyle)
644 bits |= E_LIGHTSTYLE;
645 if (n->lightpflags != o->lightpflags)
646 bits |= E_LIGHTPFLAGS;
650 if (bits & 0xFF000000)
652 if (bits & 0x00FF0000)
654 if (bits & 0x0000FF00)
660 void EntityState_WriteExtendBits(sizebuf_t *msg, unsigned int bits)
662 MSG_WriteByte(msg, bits & 0xFF);
663 if (bits & 0x00000080)
665 MSG_WriteByte(msg, (bits >> 8) & 0xFF);
666 if (bits & 0x00008000)
668 MSG_WriteByte(msg, (bits >> 16) & 0xFF);
669 if (bits & 0x00800000)
670 MSG_WriteByte(msg, (bits >> 24) & 0xFF);
675 void EntityState_WriteFields(const entity_state_t *ent, sizebuf_t *msg, unsigned int bits)
677 if (sv.protocol == PROTOCOL_DARKPLACES2)
679 if (bits & E_ORIGIN1)
680 MSG_WriteCoord16i(msg, ent->origin[0]);
681 if (bits & E_ORIGIN2)
682 MSG_WriteCoord16i(msg, ent->origin[1]);
683 if (bits & E_ORIGIN3)
684 MSG_WriteCoord16i(msg, ent->origin[2]);
688 // LordHavoc: have to write flags first, as they can modify protocol
690 MSG_WriteByte(msg, ent->flags);
691 if (ent->flags & RENDER_LOWPRECISION)
693 if (bits & E_ORIGIN1)
694 MSG_WriteCoord16i(msg, ent->origin[0]);
695 if (bits & E_ORIGIN2)
696 MSG_WriteCoord16i(msg, ent->origin[1]);
697 if (bits & E_ORIGIN3)
698 MSG_WriteCoord16i(msg, ent->origin[2]);
702 if (bits & E_ORIGIN1)
703 MSG_WriteCoord32f(msg, ent->origin[0]);
704 if (bits & E_ORIGIN2)
705 MSG_WriteCoord32f(msg, ent->origin[1]);
706 if (bits & E_ORIGIN3)
707 MSG_WriteCoord32f(msg, ent->origin[2]);
710 if ((sv.protocol == PROTOCOL_DARKPLACES1 || sv.protocol == PROTOCOL_DARKPLACES2 || sv.protocol == PROTOCOL_DARKPLACES3 || sv.protocol == PROTOCOL_DARKPLACES4) && (ent->flags & RENDER_LOWPRECISION))
713 MSG_WriteAngle8i(msg, ent->angles[0]);
715 MSG_WriteAngle8i(msg, ent->angles[1]);
717 MSG_WriteAngle8i(msg, ent->angles[2]);
722 MSG_WriteAngle16i(msg, ent->angles[0]);
724 MSG_WriteAngle16i(msg, ent->angles[1]);
726 MSG_WriteAngle16i(msg, ent->angles[2]);
729 MSG_WriteByte(msg, ent->modelindex & 0xFF);
731 MSG_WriteByte(msg, (ent->modelindex >> 8) & 0xFF);
733 MSG_WriteByte(msg, ent->frame & 0xFF);
735 MSG_WriteByte(msg, (ent->frame >> 8) & 0xFF);
736 if (bits & E_EFFECTS1)
737 MSG_WriteByte(msg, ent->effects & 0xFF);
738 if (bits & E_EFFECTS2)
739 MSG_WriteByte(msg, (ent->effects >> 8) & 0xFF);
740 if (bits & E_COLORMAP)
741 MSG_WriteByte(msg, ent->colormap);
743 MSG_WriteByte(msg, ent->skin);
745 MSG_WriteByte(msg, ent->alpha);
747 MSG_WriteByte(msg, ent->scale);
748 if (bits & E_GLOWSIZE)
749 MSG_WriteByte(msg, ent->glowsize);
750 if (bits & E_GLOWCOLOR)
751 MSG_WriteByte(msg, ent->glowcolor);
752 if (sv.protocol == PROTOCOL_DARKPLACES2)
754 MSG_WriteByte(msg, ent->flags);
755 if (bits & E_TAGATTACHMENT)
757 MSG_WriteShort(msg, ent->tagentity);
758 MSG_WriteByte(msg, ent->tagindex);
762 MSG_WriteShort(msg, ent->light[0]);
763 MSG_WriteShort(msg, ent->light[1]);
764 MSG_WriteShort(msg, ent->light[2]);
765 MSG_WriteShort(msg, ent->light[3]);
767 if (bits & E_LIGHTSTYLE)
768 MSG_WriteByte(msg, ent->lightstyle);
769 if (bits & E_LIGHTPFLAGS)
770 MSG_WriteByte(msg, ent->lightpflags);
773 void EntityState_WriteUpdate(const entity_state_t *ent, sizebuf_t *msg, const entity_state_t *delta)
778 // entity is active, check for changes from the delta
779 if ((bits = EntityState_DeltaBits(delta, ent)))
781 // write the update number, bits, and fields
782 MSG_WriteShort(msg, ent->number);
783 EntityState_WriteExtendBits(msg, bits);
784 EntityState_WriteFields(ent, msg, bits);
789 // entity is inactive, check if the delta was active
792 // write the remove number
793 MSG_WriteShort(msg, ent->number | 0x8000);
798 int EntityState_ReadExtendBits(void)
801 bits = MSG_ReadByte();
802 if (bits & 0x00000080)
804 bits |= MSG_ReadByte() << 8;
805 if (bits & 0x00008000)
807 bits |= MSG_ReadByte() << 16;
808 if (bits & 0x00800000)
809 bits |= MSG_ReadByte() << 24;
815 void EntityState_ReadFields(entity_state_t *e, unsigned int bits)
817 if (cls.protocol == PROTOCOL_DARKPLACES2)
819 if (bits & E_ORIGIN1)
820 e->origin[0] = MSG_ReadCoord16i();
821 if (bits & E_ORIGIN2)
822 e->origin[1] = MSG_ReadCoord16i();
823 if (bits & E_ORIGIN3)
824 e->origin[2] = MSG_ReadCoord16i();
829 e->flags = MSG_ReadByte();
830 if (e->flags & RENDER_LOWPRECISION)
832 if (bits & E_ORIGIN1)
833 e->origin[0] = MSG_ReadCoord16i();
834 if (bits & E_ORIGIN2)
835 e->origin[1] = MSG_ReadCoord16i();
836 if (bits & E_ORIGIN3)
837 e->origin[2] = MSG_ReadCoord16i();
841 if (bits & E_ORIGIN1)
842 e->origin[0] = MSG_ReadCoord32f();
843 if (bits & E_ORIGIN2)
844 e->origin[1] = MSG_ReadCoord32f();
845 if (bits & E_ORIGIN3)
846 e->origin[2] = MSG_ReadCoord32f();
849 if ((cls.protocol == PROTOCOL_DARKPLACES5 || cls.protocol == PROTOCOL_DARKPLACES6) && !(e->flags & RENDER_LOWPRECISION))
852 e->angles[0] = MSG_ReadAngle16i();
854 e->angles[1] = MSG_ReadAngle16i();
856 e->angles[2] = MSG_ReadAngle16i();
861 e->angles[0] = MSG_ReadAngle8i();
863 e->angles[1] = MSG_ReadAngle8i();
865 e->angles[2] = MSG_ReadAngle8i();
868 e->modelindex = (e->modelindex & 0xFF00) | (unsigned int) MSG_ReadByte();
870 e->modelindex = (e->modelindex & 0x00FF) | ((unsigned int) MSG_ReadByte() << 8);
872 e->frame = (e->frame & 0xFF00) | (unsigned int) MSG_ReadByte();
874 e->frame = (e->frame & 0x00FF) | ((unsigned int) MSG_ReadByte() << 8);
875 if (bits & E_EFFECTS1)
876 e->effects = (e->effects & 0xFF00) | (unsigned int) MSG_ReadByte();
877 if (bits & E_EFFECTS2)
878 e->effects = (e->effects & 0x00FF) | ((unsigned int) MSG_ReadByte() << 8);
879 if (bits & E_COLORMAP)
880 e->colormap = MSG_ReadByte();
882 e->skin = MSG_ReadByte();
884 e->alpha = MSG_ReadByte();
886 e->scale = MSG_ReadByte();
887 if (bits & E_GLOWSIZE)
888 e->glowsize = MSG_ReadByte();
889 if (bits & E_GLOWCOLOR)
890 e->glowcolor = MSG_ReadByte();
891 if (cls.protocol == PROTOCOL_DARKPLACES2)
893 e->flags = MSG_ReadByte();
894 if (bits & E_TAGATTACHMENT)
896 e->tagentity = (unsigned short) MSG_ReadShort();
897 e->tagindex = MSG_ReadByte();
901 e->light[0] = (unsigned short) MSG_ReadShort();
902 e->light[1] = (unsigned short) MSG_ReadShort();
903 e->light[2] = (unsigned short) MSG_ReadShort();
904 e->light[3] = (unsigned short) MSG_ReadShort();
906 if (bits & E_LIGHTSTYLE)
907 e->lightstyle = MSG_ReadByte();
908 if (bits & E_LIGHTPFLAGS)
909 e->lightpflags = MSG_ReadByte();
911 if (developer_networkentities.integer >= 2)
913 Con_Printf("ReadFields e%i", e->number);
915 if (bits & E_ORIGIN1)
916 Con_Printf(" E_ORIGIN1 %f", e->origin[0]);
917 if (bits & E_ORIGIN2)
918 Con_Printf(" E_ORIGIN2 %f", e->origin[1]);
919 if (bits & E_ORIGIN3)
920 Con_Printf(" E_ORIGIN3 %f", e->origin[2]);
922 Con_Printf(" E_ANGLE1 %f", e->angles[0]);
924 Con_Printf(" E_ANGLE2 %f", e->angles[1]);
926 Con_Printf(" E_ANGLE3 %f", e->angles[2]);
927 if (bits & (E_MODEL1 | E_MODEL2))
928 Con_Printf(" E_MODEL %i", e->modelindex);
930 if (bits & (E_FRAME1 | E_FRAME2))
931 Con_Printf(" E_FRAME %i", e->frame);
932 if (bits & (E_EFFECTS1 | E_EFFECTS2))
933 Con_Printf(" E_EFFECTS %i", e->effects);
935 Con_Printf(" E_ALPHA %f", e->alpha / 255.0f);
937 Con_Printf(" E_SCALE %f", e->scale / 16.0f);
938 if (bits & E_COLORMAP)
939 Con_Printf(" E_COLORMAP %i", e->colormap);
941 Con_Printf(" E_SKIN %i", e->skin);
943 if (bits & E_GLOWSIZE)
944 Con_Printf(" E_GLOWSIZE %i", e->glowsize * 4);
945 if (bits & E_GLOWCOLOR)
946 Con_Printf(" E_GLOWCOLOR %i", e->glowcolor);
949 Con_Printf(" E_LIGHT %i:%i:%i:%i", e->light[0], e->light[1], e->light[2], e->light[3]);
950 if (bits & E_LIGHTPFLAGS)
951 Con_Printf(" E_LIGHTPFLAGS %i", e->lightpflags);
953 if (bits & E_TAGATTACHMENT)
954 Con_Printf(" E_TAGATTACHMENT e%i:%i", e->tagentity, e->tagindex);
955 if (bits & E_LIGHTSTYLE)
956 Con_Printf(" E_LIGHTSTYLE %i", e->lightstyle);
961 // (client and server) allocates a new empty database
962 entityframe_database_t *EntityFrame_AllocDatabase(mempool_t *mempool)
964 return (entityframe_database_t *)Mem_Alloc(mempool, sizeof(entityframe_database_t));
967 // (client and server) frees the database
968 void EntityFrame_FreeDatabase(entityframe_database_t *d)
973 // (server) clears the database to contain no frames (thus delta compression compresses against nothing)
974 void EntityFrame_ClearDatabase(entityframe_database_t *d)
976 memset(d, 0, sizeof(*d));
979 // (server and client) removes frames older than 'frame' from database
980 void EntityFrame_AckFrame(entityframe_database_t *d, int frame)
983 d->ackframenum = frame;
984 for (i = 0;i < d->numframes && d->frames[i].framenum < frame;i++);
985 // ignore outdated frame acks (out of order packets)
989 // if some queue is left, slide it down to beginning of array
991 memmove(&d->frames[0], &d->frames[i], sizeof(d->frames[0]) * d->numframes);
994 // (server) clears frame, to prepare for adding entities
995 void EntityFrame_Clear(entity_frame_t *f, vec3_t eye, int framenum)
998 f->framenum = framenum;
1001 VectorClear(f->eye);
1003 VectorCopy(eye, f->eye);
1006 // (server and client) reads a frame from the database
1007 void EntityFrame_FetchFrame(entityframe_database_t *d, int framenum, entity_frame_t *f)
1010 EntityFrame_Clear(f, NULL, -1);
1011 for (i = 0;i < d->numframes && d->frames[i].framenum < framenum;i++);
1012 if (i < d->numframes && framenum == d->frames[i].framenum)
1014 f->framenum = framenum;
1015 f->numentities = d->frames[i].endentity - d->frames[i].firstentity;
1016 n = MAX_ENTITY_DATABASE - (d->frames[i].firstentity % MAX_ENTITY_DATABASE);
1017 if (n > f->numentities)
1019 memcpy(f->entitydata, d->entitydata + d->frames[i].firstentity % MAX_ENTITY_DATABASE, sizeof(*f->entitydata) * n);
1020 if (f->numentities > n)
1021 memcpy(f->entitydata + n, d->entitydata, sizeof(*f->entitydata) * (f->numentities - n));
1022 VectorCopy(d->eye, f->eye);
1026 // (server and client) adds a entity_frame to the database, for future reference
1027 void EntityFrame_AddFrame(entityframe_database_t *d, vec3_t eye, int framenum, int numentities, const entity_state_t *entitydata)
1030 entity_frameinfo_t *info;
1032 VectorCopy(eye, d->eye);
1034 // figure out how many entity slots are used already
1037 n = d->frames[d->numframes - 1].endentity - d->frames[0].firstentity;
1038 if (n + numentities > MAX_ENTITY_DATABASE || d->numframes >= MAX_ENTITY_HISTORY)
1040 // ran out of room, dump database
1041 EntityFrame_ClearDatabase(d);
1045 info = &d->frames[d->numframes];
1046 info->framenum = framenum;
1048 // make sure we check the newly added frame as well, but we haven't incremented numframes yet
1049 for (n = 0;n <= d->numframes;n++)
1051 if (e >= d->frames[n].framenum)
1054 Con_Print("EntityFrame_AddFrame: tried to add out of sequence frame to database\n");
1056 Con_Print("EntityFrame_AddFrame: out of sequence frames in database\n");
1059 e = d->frames[n].framenum;
1061 // if database still has frames after that...
1063 info->firstentity = d->frames[d->numframes - 1].endentity;
1065 info->firstentity = 0;
1066 info->endentity = info->firstentity + numentities;
1069 n = info->firstentity % MAX_ENTITY_DATABASE;
1070 e = MAX_ENTITY_DATABASE - n;
1071 if (e > numentities)
1073 memcpy(d->entitydata + n, entitydata, sizeof(entity_state_t) * e);
1074 if (numentities > e)
1075 memcpy(d->entitydata, entitydata + e, sizeof(entity_state_t) * (numentities - e));
1078 // (server) writes a frame to network stream
1079 void EntityFrame_WriteFrame(sizebuf_t *msg, entityframe_database_t *d, int numstates, const entity_state_t *states, int viewentnum)
1081 int i, onum, number;
1082 entity_frame_t *o = &d->deltaframe;
1083 const entity_state_t *ent, *delta;
1087 d->latestframenum++;
1090 for (i = 0;i < numstates;i++)
1092 if (states[i].number == viewentnum)
1094 VectorSet(eye, states[i].origin[0], states[i].origin[1], states[i].origin[2] + 22);
1099 EntityFrame_AddFrame(d, eye, d->latestframenum, numstates, states);
1101 EntityFrame_FetchFrame(d, d->ackframenum, o);
1103 MSG_WriteByte (msg, svc_entities);
1104 MSG_WriteLong (msg, o->framenum);
1105 MSG_WriteLong (msg, d->latestframenum);
1106 MSG_WriteFloat (msg, eye[0]);
1107 MSG_WriteFloat (msg, eye[1]);
1108 MSG_WriteFloat (msg, eye[2]);
1111 for (i = 0;i < numstates;i++)
1114 number = ent->number;
1116 val = PRVM_EDICTFIELDVALUE((&prog->edicts[number]), prog->fieldoffsets.SendEntity);
1117 if(val && val->function)
1119 for (;onum < o->numentities && o->entitydata[onum].number < number;onum++)
1121 // write remove message
1122 MSG_WriteShort(msg, o->entitydata[onum].number | 0x8000);
1124 if (onum < o->numentities && (o->entitydata[onum].number == number))
1126 // delta from previous frame
1127 delta = o->entitydata + onum;
1128 // advance to next entity in delta frame
1133 // delta from defaults
1134 delta = &defaultstate;
1136 EntityState_WriteUpdate(ent, msg, delta);
1138 for (;onum < o->numentities;onum++)
1140 // write remove message
1141 MSG_WriteShort(msg, o->entitydata[onum].number | 0x8000);
1143 MSG_WriteShort(msg, 0xFFFF);
1146 // (client) reads a frame from network stream
1147 void EntityFrame_CL_ReadFrame(void)
1149 int i, number, removed;
1150 entity_frame_t *f, *delta;
1151 entity_state_t *e, *old, *oldend;
1153 entityframe_database_t *d;
1154 if (!cl.entitydatabase)
1155 cl.entitydatabase = EntityFrame_AllocDatabase(cls.levelmempool);
1156 d = cl.entitydatabase;
1158 delta = &d->deltaframe;
1160 EntityFrame_Clear(f, NULL, -1);
1162 // read the frame header info
1163 f->time = cl.mtime[0];
1164 number = MSG_ReadLong();
1165 for (i = 0;i < LATESTFRAMENUMS-1;i++)
1166 cl.latestframenums[i] = cl.latestframenums[i+1];
1167 cl.latestframenums[LATESTFRAMENUMS-1] = f->framenum = MSG_ReadLong();
1168 f->eye[0] = MSG_ReadFloat();
1169 f->eye[1] = MSG_ReadFloat();
1170 f->eye[2] = MSG_ReadFloat();
1171 EntityFrame_AckFrame(d, number);
1172 EntityFrame_FetchFrame(d, number, delta);
1173 old = delta->entitydata;
1174 oldend = old + delta->numentities;
1175 // read entities until we hit the magic 0xFFFF end tag
1176 while ((number = (unsigned short) MSG_ReadShort()) != 0xFFFF && !msg_badread)
1179 Host_Error("EntityFrame_Read: read error");
1180 removed = number & 0x8000;
1182 if (number >= MAX_EDICTS)
1183 Host_Error("EntityFrame_Read: number (%i) >= MAX_EDICTS (%i)", number, MAX_EDICTS);
1185 // seek to entity, while copying any skipped entities (assume unchanged)
1186 while (old < oldend && old->number < number)
1188 if (f->numentities >= MAX_ENTITY_DATABASE)
1189 Host_Error("EntityFrame_Read: entity list too big");
1190 f->entitydata[f->numentities] = *old++;
1191 f->entitydata[f->numentities++].time = cl.mtime[0];
1195 if (old < oldend && old->number == number)
1198 Con_Printf("EntityFrame_Read: REMOVE on unused entity %i\n", number);
1202 if (f->numentities >= MAX_ENTITY_DATABASE)
1203 Host_Error("EntityFrame_Read: entity list too big");
1205 // reserve this slot
1206 e = f->entitydata + f->numentities++;
1208 if (old < oldend && old->number == number)
1210 // delta from old entity
1215 // delta from defaults
1219 if (cl.num_entities <= number)
1221 cl.num_entities = number + 1;
1222 if (number >= cl.max_entities)
1223 CL_ExpandEntities(number);
1225 cl.entities_active[number] = true;
1227 e->time = cl.mtime[0];
1229 EntityState_ReadFields(e, EntityState_ReadExtendBits());
1232 while (old < oldend)
1234 if (f->numentities >= MAX_ENTITY_DATABASE)
1235 Host_Error("EntityFrame_Read: entity list too big");
1236 f->entitydata[f->numentities] = *old++;
1237 f->entitydata[f->numentities++].time = cl.mtime[0];
1239 EntityFrame_AddFrame(d, f->eye, f->framenum, f->numentities, f->entitydata);
1241 memset(cl.entities_active, 0, cl.num_entities * sizeof(unsigned char));
1243 for (i = 0;i < f->numentities;i++)
1245 for (;number < f->entitydata[i].number && number < cl.num_entities;number++)
1247 if (cl.entities_active[number])
1249 cl.entities_active[number] = false;
1250 cl.entities[number].state_current.active = false;
1253 if (number >= cl.num_entities)
1255 // update the entity
1256 ent = &cl.entities[number];
1257 ent->state_previous = ent->state_current;
1258 ent->state_current = f->entitydata[i];
1259 CL_MoveLerpEntityStates(ent);
1260 // the entity lives again...
1261 cl.entities_active[number] = true;
1264 for (;number < cl.num_entities;number++)
1266 if (cl.entities_active[number])
1268 cl.entities_active[number] = false;
1269 cl.entities[number].state_current.active = false;
1275 // (client) returns the frame number of the most recent frame recieved
1276 int EntityFrame_MostRecentlyRecievedFrameNum(entityframe_database_t *d)
1279 return d->frames[d->numframes - 1].framenum;
1289 entity_state_t *EntityFrame4_GetReferenceEntity(entityframe4_database_t *d, int number)
1291 if (d->maxreferenceentities <= number)
1293 int oldmax = d->maxreferenceentities;
1294 entity_state_t *oldentity = d->referenceentity;
1295 d->maxreferenceentities = (number + 15) & ~7;
1296 d->referenceentity = (entity_state_t *)Mem_Alloc(d->mempool, d->maxreferenceentities * sizeof(*d->referenceentity));
1299 memcpy(d->referenceentity, oldentity, oldmax * sizeof(*d->referenceentity));
1300 Mem_Free(oldentity);
1302 // clear the newly created entities
1303 for (;oldmax < d->maxreferenceentities;oldmax++)
1305 d->referenceentity[oldmax] = defaultstate;
1306 d->referenceentity[oldmax].number = oldmax;
1309 return d->referenceentity + number;
1312 void EntityFrame4_AddCommitEntity(entityframe4_database_t *d, const entity_state_t *s)
1314 // resize commit's entity list if full
1315 if (d->currentcommit->maxentities <= d->currentcommit->numentities)
1317 entity_state_t *oldentity = d->currentcommit->entity;
1318 d->currentcommit->maxentities += 8;
1319 d->currentcommit->entity = (entity_state_t *)Mem_Alloc(d->mempool, d->currentcommit->maxentities * sizeof(*d->currentcommit->entity));
1322 memcpy(d->currentcommit->entity, oldentity, d->currentcommit->numentities * sizeof(*d->currentcommit->entity));
1323 Mem_Free(oldentity);
1326 d->currentcommit->entity[d->currentcommit->numentities++] = *s;
1329 entityframe4_database_t *EntityFrame4_AllocDatabase(mempool_t *pool)
1331 entityframe4_database_t *d;
1332 d = (entityframe4_database_t *)Mem_Alloc(pool, sizeof(*d));
1334 EntityFrame4_ResetDatabase(d);
1338 void EntityFrame4_FreeDatabase(entityframe4_database_t *d)
1341 for (i = 0;i < MAX_ENTITY_HISTORY;i++)
1342 if (d->commit[i].entity)
1343 Mem_Free(d->commit[i].entity);
1344 if (d->referenceentity)
1345 Mem_Free(d->referenceentity);
1349 void EntityFrame4_ResetDatabase(entityframe4_database_t *d)
1352 d->referenceframenum = -1;
1353 for (i = 0;i < MAX_ENTITY_HISTORY;i++)
1354 d->commit[i].numentities = 0;
1355 for (i = 0;i < d->maxreferenceentities;i++)
1356 d->referenceentity[i] = defaultstate;
1359 int EntityFrame4_AckFrame(entityframe4_database_t *d, int framenum, int servermode)
1362 entity_database4_commit_t *commit;
1365 // reset reference, but leave commits alone
1366 d->referenceframenum = -1;
1367 for (i = 0;i < d->maxreferenceentities;i++)
1368 d->referenceentity[i] = defaultstate;
1369 // if this is the server, remove commits
1370 for (i = 0, commit = d->commit;i < MAX_ENTITY_HISTORY;i++, commit++)
1371 commit->numentities = 0;
1374 else if (d->referenceframenum == framenum)
1379 for (i = 0, commit = d->commit;i < MAX_ENTITY_HISTORY;i++, commit++)
1381 if (commit->numentities && commit->framenum <= framenum)
1383 if (commit->framenum == framenum)
1386 d->referenceframenum = framenum;
1387 if (developer_networkentities.integer >= 3)
1389 for (j = 0;j < commit->numentities;j++)
1391 entity_state_t *s = EntityFrame4_GetReferenceEntity(d, commit->entity[j].number);
1392 if (commit->entity[j].active != s->active)
1394 if (commit->entity[j].active)
1395 Con_Printf("commit entity %i has become active (modelindex %i)\n", commit->entity[j].number, commit->entity[j].modelindex);
1397 Con_Printf("commit entity %i has become inactive (modelindex %i)\n", commit->entity[j].number, commit->entity[j].modelindex);
1399 *s = commit->entity[j];
1403 for (j = 0;j < commit->numentities;j++)
1404 *EntityFrame4_GetReferenceEntity(d, commit->entity[j].number) = commit->entity[j];
1406 commit->numentities = 0;
1410 if (developer_networkentities.integer >= 1)
1412 Con_Printf("ack ref:%i database updated to: ref:%i commits:", framenum, d->referenceframenum);
1413 for (i = 0;i < MAX_ENTITY_HISTORY;i++)
1414 if (d->commit[i].numentities)
1415 Con_Printf(" %i", d->commit[i].framenum);
1421 void EntityFrame4_CL_ReadFrame(void)
1423 int i, n, cnumber, referenceframenum, framenum, enumber, done, stopnumber, skip = false;
1425 entityframe4_database_t *d;
1426 if (!cl.entitydatabase4)
1427 cl.entitydatabase4 = EntityFrame4_AllocDatabase(cls.levelmempool);
1428 d = cl.entitydatabase4;
1429 // read the number of the frame this refers to
1430 referenceframenum = MSG_ReadLong();
1431 // read the number of this frame
1432 for (i = 0;i < LATESTFRAMENUMS-1;i++)
1433 cl.latestframenums[i] = cl.latestframenums[i+1];
1434 cl.latestframenums[LATESTFRAMENUMS-1] = framenum = MSG_ReadLong();
1435 // read the start number
1436 enumber = (unsigned short) MSG_ReadShort();
1437 if (developer_networkentities.integer >= 10)
1439 Con_Printf("recv svc_entities num:%i ref:%i database: ref:%i commits:", framenum, referenceframenum, d->referenceframenum);
1440 for (i = 0;i < MAX_ENTITY_HISTORY;i++)
1441 if (d->commit[i].numentities)
1442 Con_Printf(" %i", d->commit[i].framenum);
1445 if (!EntityFrame4_AckFrame(d, referenceframenum, false))
1447 Con_Print("EntityFrame4_CL_ReadFrame: reference frame invalid (VERY BAD ERROR), this update will be skipped\n");
1450 d->currentcommit = NULL;
1451 for (i = 0;i < MAX_ENTITY_HISTORY;i++)
1453 if (!d->commit[i].numentities)
1455 d->currentcommit = d->commit + i;
1456 d->currentcommit->framenum = framenum;
1457 d->currentcommit->numentities = 0;
1460 if (d->currentcommit == NULL)
1462 Con_Printf("EntityFrame4_CL_ReadFrame: error while decoding frame %i: database full, reading but not storing this update\n", framenum);
1466 while (!done && !msg_badread)
1468 // read the number of the modified entity
1469 // (gaps will be copied unmodified)
1470 n = (unsigned short)MSG_ReadShort();
1473 // no more entities in this update, but we still need to copy the
1474 // rest of the reference entities (final gap)
1476 // read end of range number, then process normally
1477 n = (unsigned short)MSG_ReadShort();
1479 // high bit means it's a remove message
1480 cnumber = n & 0x7FFF;
1481 // if this is a live entity we may need to expand the array
1482 if (cl.num_entities <= cnumber && !(n & 0x8000))
1484 cl.num_entities = cnumber + 1;
1485 if (cnumber >= cl.max_entities)
1486 CL_ExpandEntities(cnumber);
1488 // add one (the changed one) if not done
1489 stopnumber = cnumber + !done;
1490 // process entities in range from the last one to the changed one
1491 for (;enumber < stopnumber;enumber++)
1493 if (skip || enumber >= cl.num_entities)
1495 if (enumber == cnumber && (n & 0x8000) == 0)
1497 entity_state_t tempstate;
1498 EntityState_ReadFields(&tempstate, EntityState_ReadExtendBits());
1502 // slide the current into the previous slot
1503 cl.entities[enumber].state_previous = cl.entities[enumber].state_current;
1504 // copy a new current from reference database
1505 cl.entities[enumber].state_current = *EntityFrame4_GetReferenceEntity(d, enumber);
1506 s = &cl.entities[enumber].state_current;
1507 // if this is the one to modify, read more data...
1508 if (enumber == cnumber)
1513 if (developer_networkentities.integer >= 2)
1514 Con_Printf("entity %i: remove\n", enumber);
1520 if (developer_networkentities.integer >= 2)
1521 Con_Printf("entity %i: update\n", enumber);
1523 EntityState_ReadFields(s, EntityState_ReadExtendBits());
1526 else if (developer_networkentities.integer >= 4)
1527 Con_Printf("entity %i: copy\n", enumber);
1528 // set the cl.entities_active flag
1529 cl.entities_active[enumber] = s->active;
1530 // set the update time
1531 s->time = cl.mtime[0];
1532 // fix the number (it gets wiped occasionally by copying from defaultstate)
1533 s->number = enumber;
1534 // check if we need to update the lerp stuff
1536 CL_MoveLerpEntityStates(&cl.entities[enumber]);
1537 // add this to the commit entry whether it is modified or not
1538 if (d->currentcommit)
1539 EntityFrame4_AddCommitEntity(d, &cl.entities[enumber].state_current);
1540 // print extra messages if desired
1541 if (developer_networkentities.integer >= 2 && cl.entities[enumber].state_current.active != cl.entities[enumber].state_previous.active)
1543 if (cl.entities[enumber].state_current.active)
1544 Con_Printf("entity #%i has become active\n", enumber);
1545 else if (cl.entities[enumber].state_previous.active)
1546 Con_Printf("entity #%i has become inactive\n", enumber);
1550 d->currentcommit = NULL;
1552 EntityFrame4_ResetDatabase(d);
1555 void EntityFrame4_WriteFrame(sizebuf_t *msg, entityframe4_database_t *d, int numstates, const entity_state_t *states)
1557 const entity_state_t *e, *s;
1558 entity_state_t inactiveentitystate;
1559 int i, n, startnumber;
1561 unsigned char data[128];
1564 // if there isn't enough space to accomplish anything, skip it
1565 if (msg->cursize + 24 > msg->maxsize)
1568 // prepare the buffer
1569 memset(&buf, 0, sizeof(buf));
1571 buf.maxsize = sizeof(data);
1573 for (i = 0;i < MAX_ENTITY_HISTORY;i++)
1574 if (!d->commit[i].numentities)
1576 // if commit buffer full, just don't bother writing an update this frame
1577 if (i == MAX_ENTITY_HISTORY)
1579 d->currentcommit = d->commit + i;
1581 // this state's number gets played around with later
1582 inactiveentitystate = defaultstate;
1584 d->currentcommit->numentities = 0;
1585 d->currentcommit->framenum = ++d->latestframenumber;
1586 MSG_WriteByte(msg, svc_entities);
1587 MSG_WriteLong(msg, d->referenceframenum);
1588 MSG_WriteLong(msg, d->currentcommit->framenum);
1589 if (developer_networkentities.integer >= 10)
1591 Con_Printf("send svc_entities num:%i ref:%i (database: ref:%i commits:", d->currentcommit->framenum, d->referenceframenum, d->referenceframenum);
1592 for (i = 0;i < MAX_ENTITY_HISTORY;i++)
1593 if (d->commit[i].numentities)
1594 Con_Printf(" %i", d->commit[i].framenum);
1597 if (d->currententitynumber >= prog->max_edicts)
1600 startnumber = bound(1, d->currententitynumber, prog->max_edicts - 1);
1601 MSG_WriteShort(msg, startnumber);
1602 // reset currententitynumber so if the loop does not break it we will
1603 // start at beginning next frame (if it does break, it will set it)
1604 d->currententitynumber = 1;
1605 for (i = 0, n = startnumber;n < prog->max_edicts;n++)
1607 val = PRVM_EDICTFIELDVALUE((&prog->edicts[n]), prog->fieldoffsets.SendEntity);
1608 if(val && val->function)
1610 // find the old state to delta from
1611 e = EntityFrame4_GetReferenceEntity(d, n);
1612 // prepare the buffer
1614 // entity exists, build an update (if empty there is no change)
1615 // find the state in the list
1616 for (;i < numstates && states[i].number < n;i++);
1622 EntityState_WriteUpdate(s, &buf, e);
1626 inactiveentitystate.number = n;
1627 s = &inactiveentitystate;
1630 // entity used to exist but doesn't anymore, send remove
1631 MSG_WriteShort(&buf, n | 0x8000);
1634 // if the commit is full, we're done this frame
1635 if (msg->cursize + buf.cursize > msg->maxsize - 4)
1637 // next frame we will continue where we left off
1640 // add the entity to the commit
1641 EntityFrame4_AddCommitEntity(d, s);
1642 // if the message is empty, skip out now
1645 // write the message to the packet
1646 SZ_Write(msg, buf.data, buf.cursize);
1649 d->currententitynumber = n;
1651 // remove world message (invalid, and thus a good terminator)
1652 MSG_WriteShort(msg, 0x8000);
1653 // write the number of the end entity
1654 MSG_WriteShort(msg, d->currententitynumber);
1656 d->currentcommit = NULL;
1662 entityframe5_database_t *EntityFrame5_AllocDatabase(mempool_t *pool)
1665 entityframe5_database_t *d;
1666 d = (entityframe5_database_t *)Mem_Alloc(pool, sizeof(*d));
1667 d->latestframenum = 0;
1668 for (i = 0;i < d->maxedicts;i++)
1669 d->states[i] = defaultstate;
1673 void EntityFrame5_FreeDatabase(entityframe5_database_t *d)
1675 // all the [maxedicts] memory is allocated at once, so there's only one
1678 Mem_Free(d->deltabits);
1682 void EntityFrame5_ExpandEdicts(entityframe5_database_t *d, int newmax)
1684 if (d->maxedicts < newmax)
1686 unsigned char *data;
1687 int oldmaxedicts = d->maxedicts;
1688 int *olddeltabits = d->deltabits;
1689 unsigned char *oldpriorities = d->priorities;
1690 int *oldupdateframenum = d->updateframenum;
1691 entity_state_t *oldstates = d->states;
1692 unsigned char *oldvisiblebits = d->visiblebits;
1693 d->maxedicts = newmax;
1694 data = (unsigned char *)Mem_Alloc(sv_mempool, d->maxedicts * sizeof(int) + d->maxedicts * sizeof(unsigned char) + d->maxedicts * sizeof(int) + d->maxedicts * sizeof(entity_state_t) + (d->maxedicts+7)/8 * sizeof(unsigned char));
1695 d->deltabits = (int *)data;data += d->maxedicts * sizeof(int);
1696 d->priorities = (unsigned char *)data;data += d->maxedicts * sizeof(unsigned char);
1697 d->updateframenum = (int *)data;data += d->maxedicts * sizeof(int);
1698 d->states = (entity_state_t *)data;data += d->maxedicts * sizeof(entity_state_t);
1699 d->visiblebits = (unsigned char *)data;data += (d->maxedicts+7)/8 * sizeof(unsigned char);
1702 memcpy(d->deltabits, olddeltabits, oldmaxedicts * sizeof(int));
1703 memcpy(d->priorities, oldpriorities, oldmaxedicts * sizeof(unsigned char));
1704 memcpy(d->updateframenum, oldupdateframenum, oldmaxedicts * sizeof(int));
1705 memcpy(d->states, oldstates, oldmaxedicts * sizeof(entity_state_t));
1706 memcpy(d->visiblebits, oldvisiblebits, (oldmaxedicts+7)/8 * sizeof(unsigned char));
1707 // the previous buffers were a single allocation, so just one free
1708 Mem_Free(olddeltabits);
1713 int EntityState5_Priority(entityframe5_database_t *d, int stateindex)
1715 int limit, priority;
1717 // if it is the player, update urgently
1718 if (stateindex == d->viewentnum)
1719 return ENTITYFRAME5_PRIORITYLEVELS - 1;
1720 // priority increases each frame no matter what happens
1721 priority = d->priorities[stateindex] + 1;
1722 // players get an extra priority boost
1723 if (stateindex <= svs.maxclients)
1725 // remove dead entities very quickly because they are just 2 bytes
1726 if (!d->states[stateindex].active)
1729 return bound(1, priority, ENTITYFRAME5_PRIORITYLEVELS - 1);
1731 // certain changes are more noticable than others
1732 if (d->deltabits[stateindex] & (E5_FULLUPDATE | E5_ATTACHMENT | E5_MODEL | E5_FLAGS | E5_COLORMAP))
1734 // find the root entity this one is attached to, and judge relevance by it
1735 for (limit = 0;limit < 256;limit++)
1737 s = d->states + stateindex;
1738 if (s->flags & RENDER_VIEWMODEL)
1739 stateindex = d->viewentnum;
1740 else if (s->tagentity)
1741 stateindex = s->tagentity;
1744 if (d->maxedicts < stateindex)
1745 EntityFrame5_ExpandEdicts(d, (stateindex+256)&~255);
1748 Con_DPrintf("Protocol: Runaway loop recursing tagentity links on entity %i\n", stateindex);
1749 // now that we have the parent entity we can make some decisions based on
1750 // distance from the player
1751 if (VectorDistance(d->states[d->viewentnum].netcenter, s->netcenter) < 1024.0f)
1753 return bound(1, priority, ENTITYFRAME5_PRIORITYLEVELS - 1);
1756 void EntityState5_WriteUpdate(int number, const entity_state_t *s, int changedbits, sizebuf_t *msg)
1758 unsigned int bits = 0;
1761 val = PRVM_EDICTFIELDVALUE((&prog->edicts[s->number]), prog->fieldoffsets.SendEntity);
1762 if(val && val->function)
1766 MSG_WriteShort(msg, number | 0x8000);
1770 if ((bits & E5_ORIGIN) && ((s->flags & RENDER_EXTERIORMODEL) || s->origin[0] < -4096 || s->origin[0] >= 4096 || s->origin[1] < -4096 || s->origin[1] >= 4096 || s->origin[2] < -4096 || s->origin[2] >= 4096))
1771 bits |= E5_ORIGIN32;
1772 if ((bits & E5_ANGLES) && !(s->flags & RENDER_LOWPRECISION))
1773 bits |= E5_ANGLES16;
1774 if ((bits & E5_MODEL) && s->modelindex >= 256)
1776 if ((bits & E5_FRAME) && s->frame >= 256)
1778 if (bits & E5_EFFECTS)
1780 if (s->effects & 0xFFFF0000)
1781 bits |= E5_EFFECTS32;
1782 else if (s->effects & 0xFFFFFF00)
1783 bits |= E5_EFFECTS16;
1789 if (bits >= 16777216)
1791 MSG_WriteShort(msg, number);
1792 MSG_WriteByte(msg, bits & 0xFF);
1793 if (bits & E5_EXTEND1)
1794 MSG_WriteByte(msg, (bits >> 8) & 0xFF);
1795 if (bits & E5_EXTEND2)
1796 MSG_WriteByte(msg, (bits >> 16) & 0xFF);
1797 if (bits & E5_EXTEND3)
1798 MSG_WriteByte(msg, (bits >> 24) & 0xFF);
1799 if (bits & E5_FLAGS)
1800 MSG_WriteByte(msg, s->flags);
1801 if (bits & E5_ORIGIN)
1803 if (bits & E5_ORIGIN32)
1805 MSG_WriteCoord32f(msg, s->origin[0]);
1806 MSG_WriteCoord32f(msg, s->origin[1]);
1807 MSG_WriteCoord32f(msg, s->origin[2]);
1811 MSG_WriteCoord13i(msg, s->origin[0]);
1812 MSG_WriteCoord13i(msg, s->origin[1]);
1813 MSG_WriteCoord13i(msg, s->origin[2]);
1816 if (bits & E5_ANGLES)
1818 if (bits & E5_ANGLES16)
1820 MSG_WriteAngle16i(msg, s->angles[0]);
1821 MSG_WriteAngle16i(msg, s->angles[1]);
1822 MSG_WriteAngle16i(msg, s->angles[2]);
1826 MSG_WriteAngle8i(msg, s->angles[0]);
1827 MSG_WriteAngle8i(msg, s->angles[1]);
1828 MSG_WriteAngle8i(msg, s->angles[2]);
1831 if (bits & E5_MODEL)
1833 if (bits & E5_MODEL16)
1834 MSG_WriteShort(msg, s->modelindex);
1836 MSG_WriteByte(msg, s->modelindex);
1838 if (bits & E5_FRAME)
1840 if (bits & E5_FRAME16)
1841 MSG_WriteShort(msg, s->frame);
1843 MSG_WriteByte(msg, s->frame);
1846 MSG_WriteByte(msg, s->skin);
1847 if (bits & E5_EFFECTS)
1849 if (bits & E5_EFFECTS32)
1850 MSG_WriteLong(msg, s->effects);
1851 else if (bits & E5_EFFECTS16)
1852 MSG_WriteShort(msg, s->effects);
1854 MSG_WriteByte(msg, s->effects);
1856 if (bits & E5_ALPHA)
1857 MSG_WriteByte(msg, s->alpha);
1858 if (bits & E5_SCALE)
1859 MSG_WriteByte(msg, s->scale);
1860 if (bits & E5_COLORMAP)
1861 MSG_WriteByte(msg, s->colormap);
1862 if (bits & E5_ATTACHMENT)
1864 MSG_WriteShort(msg, s->tagentity);
1865 MSG_WriteByte(msg, s->tagindex);
1867 if (bits & E5_LIGHT)
1869 MSG_WriteShort(msg, s->light[0]);
1870 MSG_WriteShort(msg, s->light[1]);
1871 MSG_WriteShort(msg, s->light[2]);
1872 MSG_WriteShort(msg, s->light[3]);
1873 MSG_WriteByte(msg, s->lightstyle);
1874 MSG_WriteByte(msg, s->lightpflags);
1878 MSG_WriteByte(msg, s->glowsize);
1879 MSG_WriteByte(msg, s->glowcolor);
1881 if (bits & E5_COLORMOD)
1883 MSG_WriteByte(msg, s->colormod[0]);
1884 MSG_WriteByte(msg, s->colormod[1]);
1885 MSG_WriteByte(msg, s->colormod[2]);
1890 void EntityState5_ReadUpdate(entity_state_t *s, int number)
1893 bits = MSG_ReadByte();
1894 if (bits & E5_EXTEND1)
1896 bits |= MSG_ReadByte() << 8;
1897 if (bits & E5_EXTEND2)
1899 bits |= MSG_ReadByte() << 16;
1900 if (bits & E5_EXTEND3)
1901 bits |= MSG_ReadByte() << 24;
1904 if (bits & E5_FULLUPDATE)
1909 if (bits & E5_FLAGS)
1910 s->flags = MSG_ReadByte();
1911 if (bits & E5_ORIGIN)
1913 if (bits & E5_ORIGIN32)
1915 s->origin[0] = MSG_ReadCoord32f();
1916 s->origin[1] = MSG_ReadCoord32f();
1917 s->origin[2] = MSG_ReadCoord32f();
1921 s->origin[0] = MSG_ReadCoord13i();
1922 s->origin[1] = MSG_ReadCoord13i();
1923 s->origin[2] = MSG_ReadCoord13i();
1926 if (bits & E5_ANGLES)
1928 if (bits & E5_ANGLES16)
1930 s->angles[0] = MSG_ReadAngle16i();
1931 s->angles[1] = MSG_ReadAngle16i();
1932 s->angles[2] = MSG_ReadAngle16i();
1936 s->angles[0] = MSG_ReadAngle8i();
1937 s->angles[1] = MSG_ReadAngle8i();
1938 s->angles[2] = MSG_ReadAngle8i();
1941 if (bits & E5_MODEL)
1943 if (bits & E5_MODEL16)
1944 s->modelindex = (unsigned short) MSG_ReadShort();
1946 s->modelindex = MSG_ReadByte();
1948 if (bits & E5_FRAME)
1950 if (bits & E5_FRAME16)
1951 s->frame = (unsigned short) MSG_ReadShort();
1953 s->frame = MSG_ReadByte();
1956 s->skin = MSG_ReadByte();
1957 if (bits & E5_EFFECTS)
1959 if (bits & E5_EFFECTS32)
1960 s->effects = (unsigned int) MSG_ReadLong();
1961 else if (bits & E5_EFFECTS16)
1962 s->effects = (unsigned short) MSG_ReadShort();
1964 s->effects = MSG_ReadByte();
1966 if (bits & E5_ALPHA)
1967 s->alpha = MSG_ReadByte();
1968 if (bits & E5_SCALE)
1969 s->scale = MSG_ReadByte();
1970 if (bits & E5_COLORMAP)
1971 s->colormap = MSG_ReadByte();
1972 if (bits & E5_ATTACHMENT)
1974 s->tagentity = (unsigned short) MSG_ReadShort();
1975 s->tagindex = MSG_ReadByte();
1977 if (bits & E5_LIGHT)
1979 s->light[0] = (unsigned short) MSG_ReadShort();
1980 s->light[1] = (unsigned short) MSG_ReadShort();
1981 s->light[2] = (unsigned short) MSG_ReadShort();
1982 s->light[3] = (unsigned short) MSG_ReadShort();
1983 s->lightstyle = MSG_ReadByte();
1984 s->lightpflags = MSG_ReadByte();
1988 s->glowsize = MSG_ReadByte();
1989 s->glowcolor = MSG_ReadByte();
1991 if (bits & E5_COLORMOD)
1993 s->colormod[0] = MSG_ReadByte();
1994 s->colormod[1] = MSG_ReadByte();
1995 s->colormod[2] = MSG_ReadByte();
1999 if (developer_networkentities.integer >= 2)
2001 Con_Printf("ReadFields e%i", number);
2003 if (bits & E5_ORIGIN)
2004 Con_Printf(" E5_ORIGIN %f %f %f", s->origin[0], s->origin[1], s->origin[2]);
2005 if (bits & E5_ANGLES)
2006 Con_Printf(" E5_ANGLES %f %f %f", s->angles[0], s->angles[1], s->angles[2]);
2007 if (bits & E5_MODEL)
2008 Con_Printf(" E5_MODEL %i", s->modelindex);
2009 if (bits & E5_FRAME)
2010 Con_Printf(" E5_FRAME %i", s->frame);
2012 Con_Printf(" E5_SKIN %i", s->skin);
2013 if (bits & E5_EFFECTS)
2014 Con_Printf(" E5_EFFECTS %i", s->effects);
2015 if (bits & E5_FLAGS)
2017 Con_Printf(" E5_FLAGS %i (", s->flags);
2018 if (s->flags & RENDER_STEP)
2020 if (s->flags & RENDER_GLOWTRAIL)
2021 Con_Print(" GLOWTRAIL");
2022 if (s->flags & RENDER_VIEWMODEL)
2023 Con_Print(" VIEWMODEL");
2024 if (s->flags & RENDER_EXTERIORMODEL)
2025 Con_Print(" EXTERIORMODEL");
2026 if (s->flags & RENDER_LOWPRECISION)
2027 Con_Print(" LOWPRECISION");
2028 if (s->flags & RENDER_COLORMAPPED)
2029 Con_Print(" COLORMAPPED");
2030 if (s->flags & RENDER_SHADOW)
2031 Con_Print(" SHADOW");
2032 if (s->flags & RENDER_LIGHT)
2033 Con_Print(" LIGHT");
2034 if (s->flags & RENDER_NOSELFSHADOW)
2035 Con_Print(" NOSELFSHADOW");
2038 if (bits & E5_ALPHA)
2039 Con_Printf(" E5_ALPHA %f", s->alpha / 255.0f);
2040 if (bits & E5_SCALE)
2041 Con_Printf(" E5_SCALE %f", s->scale / 16.0f);
2042 if (bits & E5_COLORMAP)
2043 Con_Printf(" E5_COLORMAP %i", s->colormap);
2044 if (bits & E5_ATTACHMENT)
2045 Con_Printf(" E5_ATTACHMENT e%i:%i", s->tagentity, s->tagindex);
2046 if (bits & E5_LIGHT)
2047 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);
2049 Con_Printf(" E5_GLOW %i:%i", s->glowsize * 4, s->glowcolor);
2050 if (bits & E5_COLORMOD)
2051 Con_Printf(" E5_COLORMOD %f:%f:%f", s->colormod[0] / 32.0f, s->colormod[1] / 32.0f, s->colormod[2] / 32.0f);
2056 int EntityState5_DeltaBits(const entity_state_t *o, const entity_state_t *n)
2058 unsigned int bits = 0;
2062 bits |= E5_FULLUPDATE;
2063 if (!VectorCompare(o->origin, n->origin))
2065 if (!VectorCompare(o->angles, n->angles))
2067 if (o->modelindex != n->modelindex)
2069 if (o->frame != n->frame)
2071 if (o->skin != n->skin)
2073 if (o->effects != n->effects)
2075 if (o->flags != n->flags)
2077 if (o->alpha != n->alpha)
2079 if (o->scale != n->scale)
2081 if (o->colormap != n->colormap)
2082 bits |= E5_COLORMAP;
2083 if (o->tagentity != n->tagentity || o->tagindex != n->tagindex)
2084 bits |= E5_ATTACHMENT;
2085 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)
2087 if (o->glowsize != n->glowsize || o->glowcolor != n->glowcolor)
2089 if (o->colormod[0] != n->colormod[0] || o->colormod[1] != n->colormod[1] || o->colormod[2] != n->colormod[2])
2090 bits |= E5_COLORMOD;
2094 bits |= E5_FULLUPDATE;
2098 void EntityFrame5_CL_ReadFrame(void)
2103 // read the number of this frame to echo back in next input packet
2104 for (i = 0;i < LATESTFRAMENUMS-1;i++)
2105 cl.latestframenums[i] = cl.latestframenums[i+1];
2106 cl.latestframenums[LATESTFRAMENUMS-1] = MSG_ReadLong();
2107 if (developer_networkentities.integer >= 10)
2108 Con_Printf("recv: svc_entities %i\n", cl.latestframenums[LATESTFRAMENUMS-1]);
2109 if (cls.protocol != PROTOCOL_QUAKE && cls.protocol != PROTOCOL_QUAKEDP && cls.protocol != PROTOCOL_NEHAHRAMOVIE && cls.protocol != PROTOCOL_DARKPLACES1 && cls.protocol != PROTOCOL_DARKPLACES2 && cls.protocol != PROTOCOL_DARKPLACES3 && cls.protocol != PROTOCOL_DARKPLACES4 && cls.protocol != PROTOCOL_DARKPLACES5 && cls.protocol != PROTOCOL_DARKPLACES6)
2110 cls.servermovesequence = MSG_ReadLong();
2111 // read entity numbers until we find a 0x8000
2112 // (which would be remove world entity, but is actually a terminator)
2113 while ((n = (unsigned short)MSG_ReadShort()) != 0x8000 && !msg_badread)
2115 // get the entity number
2116 enumber = n & 0x7FFF;
2117 // we may need to expand the array
2118 if (cl.num_entities <= enumber)
2120 cl.num_entities = enumber + 1;
2121 if (enumber >= cl.max_entities)
2122 CL_ExpandEntities(enumber);
2124 // look up the entity
2125 ent = cl.entities + enumber;
2126 // slide the current into the previous slot
2127 ent->state_previous = ent->state_current;
2129 s = &ent->state_current;
2138 EntityState5_ReadUpdate(s, enumber);
2140 // set the cl.entities_active flag
2141 cl.entities_active[enumber] = s->active;
2142 // set the update time
2143 s->time = cl.mtime[0];
2144 // fix the number (it gets wiped occasionally by copying from defaultstate)
2145 s->number = enumber;
2146 // check if we need to update the lerp stuff
2148 CL_MoveLerpEntityStates(&cl.entities[enumber]);
2149 // print extra messages if desired
2150 if (developer_networkentities.integer >= 2 && cl.entities[enumber].state_current.active != cl.entities[enumber].state_previous.active)
2152 if (cl.entities[enumber].state_current.active)
2153 Con_Printf("entity #%i has become active\n", enumber);
2154 else if (cl.entities[enumber].state_previous.active)
2155 Con_Printf("entity #%i has become inactive\n", enumber);
2160 void EntityFrame5_LostFrame(entityframe5_database_t *d, int framenum)
2162 int i, j, k, l, bits;
2163 entityframe5_changestate_t *s, *s2;
2164 entityframe5_packetlog_t *p, *p2;
2165 unsigned char statsdeltabits[(MAX_CL_STATS+7)/8];
2166 // scan for packets that were lost
2167 for (i = 0, p = d->packetlog;i < ENTITYFRAME5_MAXPACKETLOGS;i++, p++)
2169 if (p->packetnumber && p->packetnumber <= framenum)
2171 // packet was lost - merge deltabits into the main array so they
2172 // will be re-sent, but only if there is no newer update of that
2173 // bit in the logs (as those will arrive before this update)
2174 for (j = 0, s = p->states;j < p->numstates;j++, s++)
2176 // check for any newer updates to this entity and mask off any
2177 // overlapping bits (we don't need to send something again if
2178 // it has already been sent more recently)
2179 bits = s->bits & ~d->deltabits[s->number];
2180 for (k = 0, p2 = d->packetlog;k < ENTITYFRAME5_MAXPACKETLOGS && bits;k++, p2++)
2182 if (p2->packetnumber > framenum)
2184 for (l = 0, s2 = p2->states;l < p2->numstates;l++, s2++)
2186 if (s2->number == s->number)
2194 // if the bits haven't all been cleared, there were some bits
2195 // lost with this packet, so set them again now
2198 d->deltabits[s->number] |= bits;
2199 // if it was a very important update, set priority higher
2200 if (bits & (E5_FULLUPDATE | E5_ATTACHMENT | E5_MODEL || E5_COLORMAP))
2201 d->priorities[s->number] = max(d->priorities[s->number], 4);
2203 d->priorities[s->number] = max(d->priorities[s->number], 1);
2207 for (j = 0;j < MAX_CL_STATS;j++)
2209 for (l = 0;l < (MAX_CL_STATS+7)/8;l++)
2210 statsdeltabits[l] = p->statsdeltabits[l] & ~host_client->statsdeltabits[l];
2211 for (k = 0, p2 = d->packetlog;k < ENTITYFRAME5_MAXPACKETLOGS;k++, p2++)
2212 if (p2->packetnumber > framenum)
2213 for (l = 0;l < (MAX_CL_STATS+7)/8;l++)
2214 statsdeltabits[l] = p->statsdeltabits[l] & ~p2->statsdeltabits[l];
2215 for (l = 0;l < (MAX_CL_STATS+7)/8;l++)
2216 host_client->statsdeltabits[l] |= statsdeltabits[l];
2218 // delete this packet log as it is now obsolete
2219 p->packetnumber = 0;
2224 void EntityFrame5_AckFrame(entityframe5_database_t *d, int framenum)
2227 // scan for packets made obsolete by this ack and delete them
2228 for (i = 0;i < ENTITYFRAME5_MAXPACKETLOGS;i++)
2229 if (d->packetlog[i].packetnumber <= framenum)
2230 d->packetlog[i].packetnumber = 0;
2233 void EntityFrame5_WriteFrame(sizebuf_t *msg, entityframe5_database_t *d, int numstates, const entity_state_t *states, int viewentnum, int movesequence)
2235 const entity_state_t *n;
2236 int i, num, l, framenum, packetlognumber, priority;
2238 unsigned char data[128];
2239 entityframe5_packetlog_t *packetlog;
2241 if (prog->max_edicts > d->maxedicts)
2242 EntityFrame5_ExpandEdicts(d, prog->max_edicts);
2244 framenum = d->latestframenum + 1;
2245 d->viewentnum = viewentnum;
2247 // if packet log is full, mark all frames as lost, this will cause
2248 // it to send the lost data again
2249 for (packetlognumber = 0;packetlognumber < ENTITYFRAME5_MAXPACKETLOGS;packetlognumber++)
2250 if (d->packetlog[packetlognumber].packetnumber == 0)
2252 if (packetlognumber == ENTITYFRAME5_MAXPACKETLOGS)
2254 Con_DPrintf("EntityFrame5_WriteFrame: packetlog overflow for a client, resetting\n");
2255 EntityFrame5_LostFrame(d, framenum);
2256 packetlognumber = 0;
2259 // prepare the buffer
2260 memset(&buf, 0, sizeof(buf));
2262 buf.maxsize = sizeof(data);
2264 // detect changes in states
2266 for (i = 0, n = states;i < numstates;i++, n++)
2268 // mark gaps in entity numbering as removed entities
2269 for (;num < n->number;num++)
2271 // if the entity used to exist, clear it
2272 if (CHECKPVSBIT(d->visiblebits, num))
2274 CLEARPVSBIT(d->visiblebits, num);
2275 d->deltabits[num] = E5_FULLUPDATE;
2276 d->priorities[num] = max(d->priorities[num], 8); // removal is cheap
2277 d->states[num] = defaultstate;
2278 d->states[num].number = num;
2281 // update the entity state data
2282 if (!CHECKPVSBIT(d->visiblebits, num))
2284 // entity just spawned in, don't let it completely hog priority
2285 // because of being ancient on the first frame
2286 d->updateframenum[num] = framenum;
2287 // initial priority is a bit high to make projectiles send on the
2288 // first frame, among other things
2289 d->priorities[num] = max(d->priorities[num], 4);
2291 SETPVSBIT(d->visiblebits, num);
2292 d->deltabits[num] |= EntityState5_DeltaBits(d->states + num, n);
2293 d->priorities[num] = max(d->priorities[num], 1);
2294 d->states[num] = *n;
2295 d->states[num].number = num;
2296 // advance to next entity so the next iteration doesn't immediately remove it
2299 // all remaining entities are dead
2300 for (;num < d->maxedicts;num++)
2302 if (CHECKPVSBIT(d->visiblebits, num))
2304 CLEARPVSBIT(d->visiblebits, num);
2305 d->deltabits[num] = E5_FULLUPDATE;
2306 d->priorities[num] = max(d->priorities[num], 8); // removal is cheap
2307 d->states[num] = defaultstate;
2308 d->states[num].number = num;
2312 // if there isn't at least enough room for an empty svc_entities,
2313 // don't bother trying...
2314 if (buf.cursize + 11 > buf.maxsize)
2317 // build lists of entities by priority level
2318 memset(d->prioritychaincounts, 0, sizeof(d->prioritychaincounts));
2320 for (num = 0;num < d->maxedicts;num++)
2322 if (d->priorities[num])
2324 if (d->deltabits[num])
2326 if (d->priorities[num] < (ENTITYFRAME5_PRIORITYLEVELS - 1))
2327 d->priorities[num] = EntityState5_Priority(d, num);
2329 priority = d->priorities[num];
2330 if (d->prioritychaincounts[priority] < ENTITYFRAME5_MAXSTATES)
2331 d->prioritychains[priority][d->prioritychaincounts[priority]++] = num;
2334 d->priorities[num] = 0;
2338 // add packetlog entry
2339 packetlog = d->packetlog + packetlognumber;
2340 packetlog->packetnumber = framenum;
2341 packetlog->numstates = 0;
2342 // write stat updates
2343 if (sv.protocol != PROTOCOL_QUAKE && sv.protocol != PROTOCOL_QUAKEDP && sv.protocol != PROTOCOL_NEHAHRAMOVIE && sv.protocol != PROTOCOL_DARKPLACES1 && sv.protocol != PROTOCOL_DARKPLACES2 && sv.protocol != PROTOCOL_DARKPLACES3 && sv.protocol != PROTOCOL_DARKPLACES4 && sv.protocol != PROTOCOL_DARKPLACES5)
2345 for (i = 0;i < MAX_CL_STATS && msg->cursize + 6 + 11 <= msg->maxsize;i++)
2347 if (host_client->statsdeltabits[i>>3] & (1<<(i&7)))
2349 host_client->statsdeltabits[i>>3] &= ~(1<<(i&7));
2350 packetlog->statsdeltabits[i>>3] |= (1<<(i&7));
2351 if (host_client->stats[i] >= 0 && host_client->stats[i] < 256)
2353 MSG_WriteByte(msg, svc_updatestatubyte);
2354 MSG_WriteByte(msg, i);
2355 MSG_WriteByte(msg, host_client->stats[i]);
2359 MSG_WriteByte(msg, svc_updatestat);
2360 MSG_WriteByte(msg, i);
2361 MSG_WriteLong(msg, host_client->stats[i]);
2366 // write state updates
2367 if (developer_networkentities.integer >= 10)
2368 Con_Printf("send: svc_entities %i\n", framenum);
2369 d->latestframenum = framenum;
2370 MSG_WriteByte(msg, svc_entities);
2371 MSG_WriteLong(msg, framenum);
2372 if (sv.protocol != PROTOCOL_QUAKE && sv.protocol != PROTOCOL_QUAKEDP && sv.protocol != PROTOCOL_NEHAHRAMOVIE && sv.protocol != PROTOCOL_DARKPLACES1 && sv.protocol != PROTOCOL_DARKPLACES2 && sv.protocol != PROTOCOL_DARKPLACES3 && sv.protocol != PROTOCOL_DARKPLACES4 && sv.protocol != PROTOCOL_DARKPLACES5 && sv.protocol != PROTOCOL_DARKPLACES6)
2373 MSG_WriteLong(msg, movesequence);
2374 for (priority = ENTITYFRAME5_PRIORITYLEVELS - 1;priority >= 0 && packetlog->numstates < ENTITYFRAME5_MAXSTATES;priority--)
2376 for (i = 0;i < d->prioritychaincounts[priority] && packetlog->numstates < ENTITYFRAME5_MAXSTATES;i++)
2378 num = d->prioritychains[priority][i];
2379 n = d->states + num;
2380 if (d->deltabits[num] & E5_FULLUPDATE)
2381 d->deltabits[num] = E5_FULLUPDATE | EntityState5_DeltaBits(&defaultstate, n);
2383 EntityState5_WriteUpdate(num, n, d->deltabits[num], &buf);
2384 // if the entity won't fit, try the next one
2385 if (msg->cursize + buf.cursize + 2 > msg->maxsize)
2387 // write entity to the packet
2388 SZ_Write(msg, buf.data, buf.cursize);
2389 // mark age on entity for prioritization
2390 d->updateframenum[num] = framenum;
2391 // log entity so deltabits can be restored later if lost
2392 packetlog->states[packetlog->numstates].number = num;
2393 packetlog->states[packetlog->numstates].bits = d->deltabits[num];
2394 packetlog->numstates++;
2395 // clear deltabits and priority so it won't be sent again
2396 d->deltabits[num] = 0;
2397 d->priorities[num] = 0;
2400 MSG_WriteShort(msg, 0x8000);
2404 static void QW_TranslateEffects(entity_state_t *s, int qweffects)
2407 s->internaleffects = 0;
2408 if (qweffects & QW_EF_BRIGHTFIELD)
2409 s->effects |= EF_BRIGHTFIELD;
2410 if (qweffects & QW_EF_MUZZLEFLASH)
2411 s->effects |= EF_MUZZLEFLASH;
2412 if (qweffects & QW_EF_FLAG1)
2414 // mimic FTEQW's interpretation of EF_FLAG1 as EF_NODRAW on non-player entities
2415 if (s->number > cl.maxclients)
2416 s->effects |= EF_NODRAW;
2418 s->internaleffects |= INTEF_FLAG1QW;
2420 if (qweffects & QW_EF_FLAG2)
2422 // mimic FTEQW's interpretation of EF_FLAG2 as EF_ADDITIVE on non-player entities
2423 if (s->number > cl.maxclients)
2424 s->effects |= EF_ADDITIVE;
2426 s->internaleffects |= INTEF_FLAG2QW;
2428 if (qweffects & QW_EF_RED)
2430 if (qweffects & QW_EF_BLUE)
2431 s->effects |= EF_RED | EF_BLUE;
2433 s->effects |= EF_RED;
2435 else if (qweffects & QW_EF_BLUE)
2436 s->effects |= EF_BLUE;
2437 else if (qweffects & QW_EF_BRIGHTLIGHT)
2438 s->effects |= EF_BRIGHTLIGHT;
2439 else if (qweffects & QW_EF_DIMLIGHT)
2440 s->effects |= EF_DIMLIGHT;
2443 void EntityStateQW_ReadPlayerUpdate(void)
2445 int slot = MSG_ReadByte();
2446 int enumber = slot + 1;
2452 // look up the entity
2453 entity_t *ent = cl.entities + enumber;
2457 // slide the current state into the previous
2458 ent->state_previous = ent->state_current;
2461 s = &ent->state_current;
2464 s->number = enumber;
2465 s->colormap = enumber;
2466 playerflags = MSG_ReadShort();
2467 MSG_ReadVector(s->origin, cls.protocol);
2468 s->frame = MSG_ReadByte();
2470 VectorClear(viewangles);
2471 VectorClear(velocity);
2473 if (playerflags & QW_PF_MSEC)
2475 // time difference between last update this player sent to the server,
2476 // and last input we sent to the server (this packet is in response to
2477 // our input, so msec is how long ago the last update of this player
2478 // entity occurred, compared to our input being received)
2479 msec = MSG_ReadByte();
2483 if (playerflags & QW_PF_COMMAND)
2485 bits = MSG_ReadByte();
2486 if (bits & QW_CM_ANGLE1)
2487 viewangles[0] = MSG_ReadAngle16i(); // cmd->angles[0]
2488 if (bits & QW_CM_ANGLE2)
2489 viewangles[1] = MSG_ReadAngle16i(); // cmd->angles[1]
2490 if (bits & QW_CM_ANGLE3)
2491 viewangles[2] = MSG_ReadAngle16i(); // cmd->angles[2]
2492 if (bits & QW_CM_FORWARD)
2493 MSG_ReadShort(); // cmd->forwardmove
2494 if (bits & QW_CM_SIDE)
2495 MSG_ReadShort(); // cmd->sidemove
2496 if (bits & QW_CM_UP)
2497 MSG_ReadShort(); // cmd->upmove
2498 if (bits & QW_CM_BUTTONS)
2499 MSG_ReadByte(); // cmd->buttons
2500 if (bits & QW_CM_IMPULSE)
2501 MSG_ReadByte(); // cmd->impulse
2502 MSG_ReadByte(); // cmd->msec
2504 if (playerflags & QW_PF_VELOCITY1)
2505 velocity[0] = MSG_ReadShort();
2506 if (playerflags & QW_PF_VELOCITY2)
2507 velocity[1] = MSG_ReadShort();
2508 if (playerflags & QW_PF_VELOCITY3)
2509 velocity[2] = MSG_ReadShort();
2510 if (playerflags & QW_PF_MODEL)
2511 s->modelindex = MSG_ReadByte();
2513 s->modelindex = cl.qw_modelindex_player;
2514 if (playerflags & QW_PF_SKINNUM)
2515 s->skin = MSG_ReadByte();
2516 if (playerflags & QW_PF_EFFECTS)
2517 QW_TranslateEffects(s, MSG_ReadByte());
2518 if (playerflags & QW_PF_WEAPONFRAME)
2519 weaponframe = MSG_ReadByte();
2523 if (enumber == cl.playerentity)
2525 // if this is an update on our player, update the angles
2526 VectorCopy(cl.viewangles, viewangles);
2529 // calculate the entity angles from the viewangles
2530 s->angles[0] = viewangles[0] * -0.0333;
2531 s->angles[1] = viewangles[1];
2533 s->angles[2] = V_CalcRoll(s->angles, velocity)*4;
2535 // if this is an update on our player, update interpolation state
2536 if (enumber == cl.playerentity)
2538 VectorCopy (cl.mpunchangle[0], cl.mpunchangle[1]);
2539 VectorCopy (cl.mpunchvector[0], cl.mpunchvector[1]);
2540 VectorCopy (cl.mvelocity[0], cl.mvelocity[1]);
2541 cl.mviewzoom[1] = cl.mviewzoom[0];
2544 cl.mpunchangle[0][0] = 0;
2545 cl.mpunchangle[0][1] = 0;
2546 cl.mpunchangle[0][2] = 0;
2547 cl.mpunchvector[0][0] = 0;
2548 cl.mpunchvector[0][1] = 0;
2549 cl.mpunchvector[0][2] = 0;
2550 cl.mvelocity[0][0] = 0;
2551 cl.mvelocity[0][1] = 0;
2552 cl.mvelocity[0][2] = 0;
2553 cl.mviewzoom[0] = 1;
2555 VectorCopy(velocity, cl.mvelocity[0]);
2556 cl.stats[STAT_WEAPONFRAME] = weaponframe;
2557 if (playerflags & QW_PF_GIB)
2558 cl.stats[STAT_VIEWHEIGHT] = 8;
2559 else if (playerflags & QW_PF_DEAD)
2560 cl.stats[STAT_VIEWHEIGHT] = -16;
2562 cl.stats[STAT_VIEWHEIGHT] = 22;
2565 // set the cl.entities_active flag
2566 cl.entities_active[enumber] = s->active;
2567 // set the update time
2568 s->time = cl.mtime[0] - msec * 0.001; // qw has no clock
2569 // check if we need to update the lerp stuff
2571 CL_MoveLerpEntityStates(&cl.entities[enumber]);
2574 static void EntityStateQW_ReadEntityUpdate(entity_state_t *s, int bits)
2578 s->number = bits & 511;
2580 if (bits & QW_U_MOREBITS)
2581 bits |= MSG_ReadByte();
2583 // store the QW_U_SOLID bit here?
2585 if (bits & QW_U_MODEL)
2586 s->modelindex = MSG_ReadByte();
2587 if (bits & QW_U_FRAME)
2588 s->frame = MSG_ReadByte();
2589 if (bits & QW_U_COLORMAP)
2590 s->colormap = MSG_ReadByte();
2591 if (bits & QW_U_SKIN)
2592 s->skin = MSG_ReadByte();
2593 if (bits & QW_U_EFFECTS)
2594 QW_TranslateEffects(s, qweffects = MSG_ReadByte());
2595 if (bits & QW_U_ORIGIN1)
2596 s->origin[0] = MSG_ReadCoord13i();
2597 if (bits & QW_U_ANGLE1)
2598 s->angles[0] = MSG_ReadAngle8i();
2599 if (bits & QW_U_ORIGIN2)
2600 s->origin[1] = MSG_ReadCoord13i();
2601 if (bits & QW_U_ANGLE2)
2602 s->angles[1] = MSG_ReadAngle8i();
2603 if (bits & QW_U_ORIGIN3)
2604 s->origin[2] = MSG_ReadCoord13i();
2605 if (bits & QW_U_ANGLE3)
2606 s->angles[2] = MSG_ReadAngle8i();
2608 if (developer_networkentities.integer >= 2)
2610 Con_Printf("ReadFields e%i", s->number);
2611 if (bits & QW_U_MODEL)
2612 Con_Printf(" U_MODEL %i", s->modelindex);
2613 if (bits & QW_U_FRAME)
2614 Con_Printf(" U_FRAME %i", s->frame);
2615 if (bits & QW_U_COLORMAP)
2616 Con_Printf(" U_COLORMAP %i", s->colormap);
2617 if (bits & QW_U_SKIN)
2618 Con_Printf(" U_SKIN %i", s->skin);
2619 if (bits & QW_U_EFFECTS)
2620 Con_Printf(" U_EFFECTS %i", qweffects);
2621 if (bits & QW_U_ORIGIN1)
2622 Con_Printf(" U_ORIGIN1 %f", s->origin[0]);
2623 if (bits & QW_U_ANGLE1)
2624 Con_Printf(" U_ANGLE1 %f", s->angles[0]);
2625 if (bits & QW_U_ORIGIN2)
2626 Con_Printf(" U_ORIGIN2 %f", s->origin[1]);
2627 if (bits & QW_U_ANGLE2)
2628 Con_Printf(" U_ANGLE2 %f", s->angles[1]);
2629 if (bits & QW_U_ORIGIN3)
2630 Con_Printf(" U_ORIGIN3 %f", s->origin[2]);
2631 if (bits & QW_U_ANGLE3)
2632 Con_Printf(" U_ANGLE3 %f", s->angles[2]);
2633 if (bits & QW_U_SOLID)
2634 Con_Printf(" U_SOLID");
2639 entityframeqw_database_t *EntityFrameQW_AllocDatabase(mempool_t *pool)
2641 entityframeqw_database_t *d;
2642 d = (entityframeqw_database_t *)Mem_Alloc(pool, sizeof(*d));
2646 void EntityFrameQW_FreeDatabase(entityframeqw_database_t *d)
2651 void EntityFrameQW_CL_ReadFrame(qboolean delta)
2653 qboolean invalid = false;
2654 int number, oldsnapindex, newsnapindex, oldindex, newindex, oldnum, newnum;
2656 entityframeqw_database_t *d;
2657 entityframeqw_snapshot_t *oldsnap, *newsnap;
2659 if (!cl.entitydatabaseqw)
2660 cl.entitydatabaseqw = EntityFrameQW_AllocDatabase(cls.levelmempool);
2661 d = cl.entitydatabaseqw;
2663 // there is no cls.netcon in demos, so this reading code can't access
2664 // cls.netcon-> at all... so cls.qw_incoming_sequence and
2665 // cls.qw_outgoing_sequence are updated every time the corresponding
2666 // cls.netcon->qw. variables are updated
2667 // read the number of this frame to echo back in next input packet
2668 cl.qw_validsequence = cls.qw_incoming_sequence;
2669 newsnapindex = cl.qw_validsequence & QW_UPDATE_MASK;
2670 newsnap = d->snapshot + newsnapindex;
2671 memset(newsnap, 0, sizeof(*newsnap));
2676 number = MSG_ReadByte();
2677 oldsnapindex = cl.qw_deltasequence[newsnapindex];
2678 if ((number & QW_UPDATE_MASK) != (oldsnapindex & QW_UPDATE_MASK))
2679 Con_DPrintf("WARNING: from mismatch\n");
2680 if (oldsnapindex != -1)
2682 if (cls.qw_outgoing_sequence - oldsnapindex >= QW_UPDATE_BACKUP-1)
2684 Con_DPrintf("delta update too old\n");
2685 newsnap->invalid = invalid = true; // too old
2688 oldsnap = d->snapshot + (oldsnapindex & QW_UPDATE_MASK);
2694 // if we can't decode this frame properly, report that to the server
2696 cl.qw_validsequence = 0;
2698 // read entity numbers until we find a 0x0000
2699 // (which would be an empty update on world entity, but is actually a terminator)
2700 newsnap->num_entities = 0;
2704 int word = (unsigned short)MSG_ReadShort();
2706 return; // just return, the main parser will print an error
2707 newnum = word == 0 ? 512 : (word & 511);
2708 oldnum = delta ? (oldindex >= oldsnap->num_entities ? 9999 : oldsnap->entities[oldindex].number) : 9999;
2710 // copy unmodified oldsnap entities
2711 while (newnum > oldnum) // delta only
2713 if (developer_networkentities.integer >= 2)
2714 Con_Printf("copy %i\n", oldnum);
2715 // copy one of the old entities
2716 if (newsnap->num_entities >= QW_MAX_PACKET_ENTITIES)
2717 Host_Error("EntityFrameQW_CL_ReadFrame: newsnap->num_entities == MAX_PACKETENTITIES");
2718 newsnap->entities[newsnap->num_entities] = oldsnap->entities[oldindex++];
2719 newsnap->num_entities++;
2720 oldnum = oldindex >= oldsnap->num_entities ? 9999 : oldsnap->entities[oldindex].number;
2726 if (developer_networkentities.integer >= 2)
2728 if (word & QW_U_REMOVE)
2729 Con_Printf("remove %i\n", newnum);
2730 else if (newnum == oldnum)
2731 Con_Printf("delta %i\n", newnum);
2733 Con_Printf("baseline %i\n", newnum);
2736 if (word & QW_U_REMOVE)
2738 if (newnum != oldnum && !delta && !invalid)
2740 cl.qw_validsequence = 0;
2741 Con_Printf("WARNING: U_REMOVE %i on full update\n", newnum);
2746 if (newsnap->num_entities >= QW_MAX_PACKET_ENTITIES)
2747 Host_Error("EntityFrameQW_CL_ReadFrame: newsnap->num_entities == MAX_PACKETENTITIES");
2748 newsnap->entities[newsnap->num_entities] = (newnum == oldnum) ? oldsnap->entities[oldindex] : cl.entities[newnum].state_baseline;
2749 EntityStateQW_ReadEntityUpdate(newsnap->entities + newsnap->num_entities, word);
2750 newsnap->num_entities++;
2753 if (newnum == oldnum)
2757 // expand cl.num_entities to include every entity we've seen this game
2758 newnum = newsnap->num_entities ? newsnap->entities[newsnap->num_entities - 1].number : 1;
2759 if (cl.num_entities <= newnum)
2761 cl.num_entities = newnum + 1;
2762 if (cl.max_entities < newnum + 1)
2763 CL_ExpandEntities(newnum);
2766 // now update the non-player entities from the snapshot states
2767 number = cl.maxclients + 1;
2768 for (newindex = 0;;newindex++)
2770 newnum = newindex >= newsnap->num_entities ? cl.num_entities : newsnap->entities[newindex].number;
2771 // kill any missing entities
2772 for (;number < newnum;number++)
2774 if (cl.entities_active[number])
2776 cl.entities_active[number] = false;
2777 cl.entities[number].state_current.active = false;
2780 if (number >= cl.num_entities)
2782 // update the entity
2783 ent = &cl.entities[number];
2784 ent->state_previous = ent->state_current;
2785 ent->state_current = newsnap->entities[newindex];
2786 ent->state_current.time = cl.mtime[0];
2787 CL_MoveLerpEntityStates(ent);
2788 // the entity lives again...
2789 cl.entities_active[number] = true;