3 #define ENTITYSIZEPROFILING_START(msg, num) \
4 int entityprofiling_startsize = msg->cursize
6 #define ENTITYSIZEPROFILING_END(msg, num) \
7 if(developer_networkentities.integer >= 2) \
9 prvm_edict_t *ed = prog->edicts + num; \
10 const char *cname = "(no classname)"; \
11 if(prog->fieldoffsets.classname >= 0) \
13 string_t handle = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.classname)->string; \
15 cname = PRVM_GetString(handle); \
17 Con_Printf("sent entity update of size %d for a %s\n", (msg->cursize - entityprofiling_startsize), cname); \
20 // this is 88 bytes (must match entity_state_t in protocol.h)
21 entity_state_t defaultstate =
23 // ! means this is not sent to client
24 0,//double time; // ! time this state was built (used on client for interpolation)
25 {0,0,0},//float netcenter[3]; // ! for network prioritization, this is the center of the bounding box (which may differ from the origin)
26 {0,0,0},//float origin[3];
27 {0,0,0},//float angles[3];
29 0,//unsigned int customizeentityforclient; // !
30 0,//unsigned short number; // entity number this state is for
31 0,//unsigned short modelindex;
32 0,//unsigned short frame;
33 0,//unsigned short tagentity;
34 0,//unsigned short specialvisibilityradius; // ! larger if it has effects/light
35 0,//unsigned short viewmodelforclient; // !
36 0,//unsigned short exteriormodelforclient; // ! not shown if first person viewing from this entity, shown in all other cases
37 0,//unsigned short nodrawtoclient; // !
38 0,//unsigned short drawonlytoclient; // !
39 {0,0,0,0},//unsigned short light[4]; // color*256 (0.00 to 255.996), and radius*1
40 0,//unsigned char active; // true if a valid state
41 0,//unsigned char lightstyle;
42 0,//unsigned char lightpflags;
43 0,//unsigned char colormap;
44 0,//unsigned char skin; // also chooses cubemap for rtlights if lightpflags & LIGHTPFLAGS_FULLDYNAMIC
45 255,//unsigned char alpha;
46 16,//unsigned char scale;
47 0,//unsigned char glowsize;
48 254,//unsigned char glowcolor;
49 0,//unsigned char flags;
50 0,//unsigned char internaleffects; // INTEF_FLAG1QW and so on
51 0,//unsigned char tagindex;
52 {32, 32, 32},//unsigned char colormod[3];
53 // padding to a multiple of 8 bytes (to align the double time)
54 {0,0,0,0,0}//unsigned char unused[5]; // !
57 // LordHavoc: I own protocol ranges 96, 97, 3500-3599
59 struct protocolversioninfo_s
64 protocolversioninfo[] =
75 {250, "NEHAHRAMOVIE"},
78 {10000, "NEHAHRABJP"},
79 {10001, "NEHAHRABJP2"},
80 {10002, "NEHAHRABJP3"},
84 protocolversion_t Protocol_EnumForName(const char *s)
87 for (i = 1;protocolversioninfo[i].name;i++)
88 if (!strcasecmp(s, protocolversioninfo[i].name))
89 return (protocolversion_t)i;
90 return PROTOCOL_UNKNOWN;
93 const char *Protocol_NameForEnum(protocolversion_t p)
95 return protocolversioninfo[p].name;
98 protocolversion_t Protocol_EnumForNumber(int n)
101 for (i = 1;protocolversioninfo[i].name;i++)
102 if (protocolversioninfo[i].number == n)
103 return (protocolversion_t)i;
104 return PROTOCOL_UNKNOWN;
107 int Protocol_NumberForEnum(protocolversion_t p)
109 return protocolversioninfo[p].number;
112 void Protocol_Names(char *buffer, size_t buffersize)
118 for (i = 1;protocolversioninfo[i].name;i++)
121 strlcat(buffer, " ", buffersize);
122 strlcat(buffer, protocolversioninfo[i].name, buffersize);
126 void EntityFrameQuake_ReadEntity(int bits)
132 if (bits & U_MOREBITS)
133 bits |= (MSG_ReadByte()<<8);
134 if ((bits & U_EXTEND1) && cls.protocol != PROTOCOL_NEHAHRAMOVIE)
136 bits |= MSG_ReadByte() << 16;
137 if (bits & U_EXTEND2)
138 bits |= MSG_ReadByte() << 24;
141 if (bits & U_LONGENTITY)
142 num = (unsigned short) MSG_ReadShort ();
144 num = MSG_ReadByte ();
146 if (num >= MAX_EDICTS)
147 Host_Error("EntityFrameQuake_ReadEntity: entity number (%i) >= MAX_EDICTS (%i)", num, MAX_EDICTS);
149 Host_Error("EntityFrameQuake_ReadEntity: invalid entity number (%i)", num);
151 if (cl.num_entities <= num)
153 cl.num_entities = num + 1;
154 if (num >= cl.max_entities)
155 CL_ExpandEntities(num);
158 ent = cl.entities + num;
160 // note: this inherits the 'active' state of the baseline chosen
161 // (state_baseline is always active, state_current may not be active if
162 // the entity was missing in the last frame)
164 s = ent->state_current;
167 s = ent->state_baseline;
171 cl.isquakeentity[num] = true;
172 if (cl.lastquakeentity < num)
173 cl.lastquakeentity = num;
175 s.time = cl.mtime[0];
179 if (cls.protocol == PROTOCOL_NEHAHRABJP || cls.protocol == PROTOCOL_NEHAHRABJP2 || cls.protocol == PROTOCOL_NEHAHRABJP3)
180 s.modelindex = (unsigned short) MSG_ReadShort();
182 s.modelindex = (s.modelindex & 0xFF00) | MSG_ReadByte();
184 if (bits & U_FRAME) s.frame = (s.frame & 0xFF00) | MSG_ReadByte();
185 if (bits & U_COLORMAP) s.colormap = MSG_ReadByte();
186 if (bits & U_SKIN) s.skin = MSG_ReadByte();
187 if (bits & U_EFFECTS) s.effects = (s.effects & 0xFF00) | MSG_ReadByte();
188 if (bits & U_ORIGIN1) s.origin[0] = MSG_ReadCoord(cls.protocol);
189 if (bits & U_ANGLE1) s.angles[0] = MSG_ReadAngle(cls.protocol);
190 if (bits & U_ORIGIN2) s.origin[1] = MSG_ReadCoord(cls.protocol);
191 if (bits & U_ANGLE2) s.angles[1] = MSG_ReadAngle(cls.protocol);
192 if (bits & U_ORIGIN3) s.origin[2] = MSG_ReadCoord(cls.protocol);
193 if (bits & U_ANGLE3) s.angles[2] = MSG_ReadAngle(cls.protocol);
194 if (bits & U_STEP) s.flags |= RENDER_STEP;
195 if (bits & U_ALPHA) s.alpha = MSG_ReadByte();
196 if (bits & U_SCALE) s.scale = MSG_ReadByte();
197 if (bits & U_EFFECTS2) s.effects = (s.effects & 0x00FF) | (MSG_ReadByte() << 8);
198 if (bits & U_GLOWSIZE) s.glowsize = MSG_ReadByte();
199 if (bits & U_GLOWCOLOR) s.glowcolor = MSG_ReadByte();
200 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));}
201 if (bits & U_GLOWTRAIL) s.flags |= RENDER_GLOWTRAIL;
202 if (bits & U_FRAME2) s.frame = (s.frame & 0x00FF) | (MSG_ReadByte() << 8);
203 if (bits & U_MODEL2) s.modelindex = (s.modelindex & 0x00FF) | (MSG_ReadByte() << 8);
204 if (bits & U_VIEWMODEL) s.flags |= RENDER_VIEWMODEL;
205 if (bits & U_EXTERIORMODEL) s.flags |= RENDER_EXTERIORMODEL;
207 // LordHavoc: to allow playback of the Nehahra movie
208 if (cls.protocol == PROTOCOL_NEHAHRAMOVIE && (bits & U_EXTEND1))
210 // LordHavoc: evil format
211 int i = (int)MSG_ReadFloat();
212 int j = (int)(MSG_ReadFloat() * 255.0f);
215 i = (int)MSG_ReadFloat();
217 s.effects |= EF_FULLBRIGHT;
221 else if (j == 0 || j >= 255)
227 ent->state_previous = ent->state_current;
228 ent->state_current = s;
229 if (ent->state_current.active)
231 CL_MoveLerpEntityStates(ent);
232 cl.entities_active[ent->state_current.number] = true;
236 Host_Error("EntityFrameQuake_ReadEntity: read error");
239 void EntityFrameQuake_ISeeDeadEntities(void)
242 if (cl.lastquakeentity == 0)
244 lastentity = cl.lastquakeentity;
245 cl.lastquakeentity = 0;
246 for (num = 0;num <= lastentity;num++)
248 if (cl.isquakeentity[num])
250 if (cl.entities_active[num] && cl.entities[num].state_current.time == cl.mtime[0])
252 cl.isquakeentity[num] = true;
253 cl.lastquakeentity = num;
257 cl.isquakeentity[num] = false;
258 cl.entities_active[num] = false;
259 cl.entities[num].state_current = defaultstate;
260 cl.entities[num].state_current.number = num;
266 // NOTE: this only works with DP5 protocol and upwards. For lower protocols
267 // (including QUAKE), no packet loss handling for CSQC is done, which makes
268 // CSQC basically useless.
269 // Always use the DP5 protocol, or a higher one, when using CSQC entities.
270 static void EntityFrameCSQC_LostAllFrames(client_t *client)
272 // mark ALL csqc entities as requiring a FULL resend!
273 // I know this is a bad workaround, but better than nothing.
278 if(prog->fieldoffsets.SendEntity < 0 || prog->fieldoffsets.Version < 0)
281 n = client->csqcnumedicts;
282 for(i = 0; i < n; ++i)
284 if(client->csqcentityglobalhistory[i])
286 ed = prog->edicts + i;
287 val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.SendEntity);
289 client->csqcentitysendflags[i] |= 0xFFFFFF; // FULL RESEND
290 else // if it was ever sent to that client as a CSQC entity
292 client->csqcentityscope[i] = 1; // REMOVE
293 client->csqcentitysendflags[i] |= 0xFFFFFF;
298 void EntityFrameCSQC_LostFrame(client_t *client, int framenum)
300 // marks a frame as lost
303 int ringfirst, ringlast;
304 int recoversendflags[MAX_EDICTS];
305 csqcentityframedb_t *d;
307 n = client->csqcnumedicts;
309 // is our frame out of history?
310 ringfirst = client->csqcentityframehistory_next; // oldest entry
311 ringlast = (ringfirst + NUM_CSQCENTITYDB_FRAMES - 1) % NUM_CSQCENTITYDB_FRAMES; // most recently added entry
315 for(j = 0; j < NUM_CSQCENTITYDB_FRAMES; ++j)
317 d = &client->csqcentityframehistory[(ringfirst + j) % NUM_CSQCENTITYDB_FRAMES];
320 if(d->framenum == framenum)
322 else if(d->framenum < framenum)
325 if(j == NUM_CSQCENTITYDB_FRAMES)
327 if(valid) // got beaten, i.e. there is a frame < framenum
329 // a non-csqc frame got lost... great
334 // a too old frame got lost... sorry, cannot handle this
335 Con_DPrintf("CSQC entity DB: lost a frame too early to do any handling (resending ALL)...\n");
336 Con_DPrintf("Lost frame = %d\n", framenum);
337 Con_DPrintf("Entity DB = %d to %d\n", client->csqcentityframehistory[ringfirst].framenum, client->csqcentityframehistory[ringlast].framenum);
338 EntityFrameCSQC_LostAllFrames(client);
343 // so j is the frame that got lost
344 // ringlast is the frame that we have to go to
345 ringfirst = (ringfirst + j) % NUM_CSQCENTITYDB_FRAMES;
346 if(ringlast < ringfirst)
347 ringlast += NUM_CSQCENTITYDB_FRAMES;
349 memset(recoversendflags, 0, sizeof(recoversendflags));
351 for(j = ringfirst; j <= ringlast; ++j)
353 d = &client->csqcentityframehistory[j % NUM_CSQCENTITYDB_FRAMES];
358 else if(d->framenum < framenum)
360 // a frame in the past... should never happen
361 Con_Printf("CSQC entity DB encountered a frame from the past when recovering from PL...?\n");
363 else if(d->framenum == framenum)
365 // handling the actually lost frame now
366 for(i = 0; i < d->num; ++i)
368 int sf = d->sendflags[i];
369 int ent = d->entno[i];
371 recoversendflags[ent] |= -1; // all bits, including sign
373 recoversendflags[ent] |= sf;
378 // handling the frames that followed it now
379 for(i = 0; i < d->num; ++i)
381 int sf = d->sendflags[i];
382 int ent = d->entno[i];
385 recoversendflags[ent] = 0; // no need to update, we got a more recent remove (and will fix it THEN)
386 break; // no flags left to remove...
389 recoversendflags[ent] &= ~sf; // no need to update these bits, we already got them later
394 for(i = 0; i < client->csqcnumedicts; ++i)
396 if(recoversendflags[i] < 0)
398 // a remove got lost, then either send a remove or - if it was
399 // recreated later - a FULL update to make totally sure
400 client->csqcentityscope[i] = 1;
401 client->csqcentitysendflags[i] = 0xFFFFFF;
404 client->csqcentitysendflags[i] |= recoversendflags[i];
407 static int EntityFrameCSQC_AllocFrame(client_t *client, int framenum)
409 int ringfirst = client->csqcentityframehistory_next; // oldest entry
410 client->csqcentityframehistory_next += 1;
411 client->csqcentityframehistory_next %= NUM_CSQCENTITYDB_FRAMES;
412 client->csqcentityframehistory[ringfirst].framenum = framenum;
413 client->csqcentityframehistory[ringfirst].num = 0;
416 static void EntityFrameCSQC_DeallocFrame(client_t *client, int framenum)
418 int ringfirst = client->csqcentityframehistory_next; // oldest entry
419 int ringlast = (ringfirst + NUM_CSQCENTITYDB_FRAMES - 1) % NUM_CSQCENTITYDB_FRAMES; // most recently added entry
420 if(framenum == client->csqcentityframehistory[ringlast].framenum)
422 client->csqcentityframehistory[ringlast].framenum = -1;
423 client->csqcentityframehistory[ringlast].num = 0;
424 client->csqcentityframehistory_next = ringlast;
427 Con_Printf("Trying to dealloc the wrong entity frame\n");
430 //[515]: we use only one array per-client for SendEntity feature
431 // TODO: add some handling for entity send priorities, to better deal with huge
432 // amounts of csqc networked entities
433 qboolean EntityFrameCSQC_WriteFrame (sizebuf_t *msg, int maxsize, int numstates, const entity_state_t *states, int framenum)
435 int num, number, end, sendflags;
436 qboolean sectionstarted = false;
437 const entity_state_t *n;
440 client_t *client = svs.clients + sv.writeentitiestoclient_clientnumber;
441 int dbframe = EntityFrameCSQC_AllocFrame(client, framenum);
442 csqcentityframedb_t *db = &client->csqcentityframehistory[dbframe];
444 maxsize -= 24; // always fit in an empty svc_entities message (for packet loss detection!)
446 // if this server progs is not CSQC-aware, return early
447 if(prog->fieldoffsets.SendEntity < 0 || prog->fieldoffsets.Version < 0)
450 // make sure there is enough room to store the svc_csqcentities byte,
451 // the terminator (0x0000) and at least one entity update
452 if (msg->cursize + 32 >= maxsize)
455 if (client->csqcnumedicts < prog->num_edicts)
456 client->csqcnumedicts = prog->num_edicts;
459 for (num = 0, n = states;num < numstates;num++, n++)
462 for (;number < end;number++)
464 if (client->csqcentityscope[number])
466 client->csqcentityscope[number] = 1;
467 client->csqcentitysendflags[number] = 0xFFFFFF;
470 ed = prog->edicts + number;
471 val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.SendEntity);
473 client->csqcentityscope[number] = 2;
474 else if (client->csqcentityscope[number])
476 client->csqcentityscope[number] = 1;
477 client->csqcentitysendflags[number] = 0xFFFFFF;
481 end = client->csqcnumedicts;
482 for (;number < end;number++)
484 if (client->csqcentityscope[number])
486 client->csqcentityscope[number] = 1;
487 client->csqcentitysendflags[number] = 0xFFFFFF;
492 // mark all scope entities as remove
493 for (number = 1;number < client->csqcnumedicts;number++)
494 if (client->csqcentityscope[number])
495 client->csqcentityscope[number] = 1;
496 // keep visible entities
497 for (i = 0, n = states;i < numstates;i++, n++)
500 ed = prog->edicts + number;
501 val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.SendEntity);
503 client->csqcentityscope[number] = 2;
507 // now try to emit the entity updates
508 // (FIXME: prioritize by distance?)
509 end = client->csqcnumedicts;
510 for (number = 1;number < end;number++)
512 if (!client->csqcentityscope[number])
514 sendflags = client->csqcentitysendflags[number];
517 if(db->num >= NUM_CSQCENTITIES_PER_FRAME)
519 ed = prog->edicts + number;
520 // entity scope is either update (2) or remove (1)
521 if (client->csqcentityscope[number] == 1)
523 // write a remove message
524 // first write the message identifier if needed
528 MSG_WriteByte(msg, svc_csqcentities);
530 // write the remove message
532 ENTITYSIZEPROFILING_START(msg, number);
533 MSG_WriteShort(msg, (unsigned short)number | 0x8000);
534 client->csqcentityscope[number] = 0;
535 client->csqcentitysendflags[number] = 0xFFFFFF; // resend completely if it becomes active again
536 db->entno[db->num] = number;
537 db->sendflags[db->num] = -1;
539 client->csqcentityglobalhistory[number] = 1;
540 ENTITYSIZEPROFILING_END(msg, number);
542 if (msg->cursize + 17 >= maxsize)
548 // save the cursize value in case we overflow and have to rollback
549 int oldcursize = msg->cursize;
550 client->csqcentityscope[number] = 1;
551 val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.SendEntity);
555 MSG_WriteByte(msg, svc_csqcentities);
557 ENTITYSIZEPROFILING_START(msg, number);
558 MSG_WriteShort(msg, number);
559 msg->allowoverflow = true;
560 PRVM_G_INT(OFS_PARM0) = sv.writeentitiestoclient_cliententitynumber;
561 PRVM_G_FLOAT(OFS_PARM1) = sendflags;
562 prog->globals.server->self = number;
563 PRVM_ExecuteProgram(val->function, "Null SendEntity\n");
564 msg->allowoverflow = false;
565 if(PRVM_G_FLOAT(OFS_RETURN) && msg->cursize + 2 <= maxsize)
567 // an update has been successfully written
568 client->csqcentitysendflags[number] = 0;
569 db->entno[db->num] = number;
570 db->sendflags[db->num] = sendflags;
572 client->csqcentityglobalhistory[number] = 1;
573 // and take note that we have begun the svc_csqcentities
574 // section of the packet
576 ENTITYSIZEPROFILING_END(msg, number);
577 if (msg->cursize + 17 >= maxsize)
583 // self.SendEntity returned false (or does not exist) or the
584 // update was too big for this packet - rollback the buffer to its
585 // state before the writes occurred, we'll try again next frame
586 msg->cursize = oldcursize;
587 msg->overflowed = false;
592 // write index 0 to end the update (0 is never used by real entities)
593 MSG_WriteShort(msg, 0);
597 // if no single ent got added, remove the frame from the DB again, to allow
598 // for a larger history
599 EntityFrameCSQC_DeallocFrame(client, framenum);
601 return sectionstarted;
604 void Protocol_UpdateClientStats(const int *stats)
607 // update the stats array and set deltabits for any changed stats
608 for (i = 0;i < MAX_CL_STATS;i++)
610 if (host_client->stats[i] != stats[i])
612 host_client->statsdeltabits[i >> 3] |= 1 << (i & 7);
613 host_client->stats[i] = stats[i];
618 // only a few stats are within the 32 stat limit of Quake, and most of them
619 // are sent every frame in svc_clientdata messages, so we only send the
620 // remaining ones here
621 static const int sendquakestats[] =
623 // quake did not send these secrets/monsters stats in this way, but doing so
624 // allows a mod to increase STAT_TOTALMONSTERS during the game, and ensures
625 // that STAT_SECRETS and STAT_MONSTERS are always correct (even if a client
626 // didn't receive an svc_foundsecret or svc_killedmonster), which may be most
627 // valuable if randomly seeking around in a demo
628 STAT_TOTALSECRETS, // never changes during game
629 STAT_TOTALMONSTERS, // changes in some mods
630 STAT_SECRETS, // this makes svc_foundsecret unnecessary
631 STAT_MONSTERS, // this makes svc_killedmonster unnecessary
632 STAT_VIEWHEIGHT, // sent just for FTEQW clients
633 STAT_VIEWZOOM, // this rarely changes
637 void Protocol_WriteStatsReliable(void)
640 if (!host_client->netconnection)
642 // detect changes in stats and write reliable messages
643 // this only deals with 32 stats because the older protocols which use
644 // this function can only cope with 32 stats,
645 // they also do not support svc_updatestatubyte which was introduced in
646 // DP6 protocol (except for QW)
647 for (j = 0;sendquakestats[j] >= 0;j++)
649 i = sendquakestats[j];
650 // check if this bit is set
651 if (host_client->statsdeltabits[i >> 3] & (1 << (i & 7)))
653 host_client->statsdeltabits[i >> 3] -= (1 << (i & 7));
654 // send the stat as a byte if possible
655 if (sv.protocol == PROTOCOL_QUAKEWORLD)
657 if (host_client->stats[i] >= 0 && host_client->stats[i] < 256)
659 MSG_WriteByte(&host_client->netconnection->message, qw_svc_updatestat);
660 MSG_WriteByte(&host_client->netconnection->message, i);
661 MSG_WriteByte(&host_client->netconnection->message, host_client->stats[i]);
665 MSG_WriteByte(&host_client->netconnection->message, qw_svc_updatestatlong);
666 MSG_WriteByte(&host_client->netconnection->message, i);
667 MSG_WriteLong(&host_client->netconnection->message, host_client->stats[i]);
672 // this could make use of svc_updatestatubyte in DP6 and later
673 // protocols but those protocols do not use this function
674 MSG_WriteByte(&host_client->netconnection->message, svc_updatestat);
675 MSG_WriteByte(&host_client->netconnection->message, i);
676 MSG_WriteLong(&host_client->netconnection->message, host_client->stats[i]);
683 void EntityFrameQuake_WriteFrame(sizebuf_t *msg, int maxsize, int numstates, const entity_state_t *states)
685 const entity_state_t *s;
686 entity_state_t baseline;
689 unsigned char data[128];
692 // prepare the buffer
693 memset(&buf, 0, sizeof(buf));
695 buf.maxsize = sizeof(data);
697 for (i = 0, s = states;i < numstates;i++, s++)
699 ENTITYSIZEPROFILING_START(msg, s->number);
700 val = PRVM_EDICTFIELDVALUE((&prog->edicts[s->number]), prog->fieldoffsets.SendEntity);
701 if(val && val->function)
704 // prepare the buffer
709 if (s->number >= 256)
710 bits |= U_LONGENTITY;
711 if (s->flags & RENDER_STEP)
713 if (s->flags & RENDER_VIEWMODEL)
715 if (s->flags & RENDER_GLOWTRAIL)
717 if (s->flags & RENDER_EXTERIORMODEL)
718 bits |= U_EXTERIORMODEL;
720 // LordHavoc: old stuff, but rewritten to have more exact tolerances
721 baseline = prog->edicts[s->number].priv.server->baseline;
722 if (baseline.origin[0] != s->origin[0])
724 if (baseline.origin[1] != s->origin[1])
726 if (baseline.origin[2] != s->origin[2])
728 if (baseline.angles[0] != s->angles[0])
730 if (baseline.angles[1] != s->angles[1])
732 if (baseline.angles[2] != s->angles[2])
734 if (baseline.colormap != s->colormap)
736 if (baseline.skin != s->skin)
738 if (baseline.frame != s->frame)
741 if (s->frame & 0xFF00)
744 if (baseline.effects != s->effects)
747 if (s->effects & 0xFF00)
750 if (baseline.modelindex != s->modelindex)
753 if ((s->modelindex & 0xFF00) && sv.protocol != PROTOCOL_NEHAHRABJP && sv.protocol != PROTOCOL_NEHAHRABJP2 && sv.protocol != PROTOCOL_NEHAHRABJP3)
756 if (baseline.alpha != s->alpha)
758 if (baseline.scale != s->scale)
760 if (baseline.glowsize != s->glowsize)
762 if (baseline.glowcolor != s->glowcolor)
764 if (!VectorCompare(baseline.colormod, s->colormod))
767 // if extensions are disabled, clear the relevant update flags
768 if (sv.protocol == PROTOCOL_QUAKE || sv.protocol == PROTOCOL_NEHAHRAMOVIE)
770 if (sv.protocol == PROTOCOL_NEHAHRAMOVIE)
771 if (s->alpha != 255 || s->effects & EF_FULLBRIGHT)
775 if (bits >= 16777216)
783 MSG_WriteByte (&buf, bits);
784 if (bits & U_MOREBITS) MSG_WriteByte(&buf, bits>>8);
785 if (sv.protocol != PROTOCOL_NEHAHRAMOVIE)
787 if (bits & U_EXTEND1) MSG_WriteByte(&buf, bits>>16);
788 if (bits & U_EXTEND2) MSG_WriteByte(&buf, bits>>24);
790 if (bits & U_LONGENTITY) MSG_WriteShort(&buf, s->number);
791 else MSG_WriteByte(&buf, s->number);
795 if (sv.protocol == PROTOCOL_NEHAHRABJP || sv.protocol == PROTOCOL_NEHAHRABJP2 || sv.protocol == PROTOCOL_NEHAHRABJP3)
796 MSG_WriteShort(&buf, s->modelindex);
798 MSG_WriteByte(&buf, s->modelindex);
800 if (bits & U_FRAME) MSG_WriteByte(&buf, s->frame);
801 if (bits & U_COLORMAP) MSG_WriteByte(&buf, s->colormap);
802 if (bits & U_SKIN) MSG_WriteByte(&buf, s->skin);
803 if (bits & U_EFFECTS) MSG_WriteByte(&buf, s->effects);
804 if (bits & U_ORIGIN1) MSG_WriteCoord(&buf, s->origin[0], sv.protocol);
805 if (bits & U_ANGLE1) MSG_WriteAngle(&buf, s->angles[0], sv.protocol);
806 if (bits & U_ORIGIN2) MSG_WriteCoord(&buf, s->origin[1], sv.protocol);
807 if (bits & U_ANGLE2) MSG_WriteAngle(&buf, s->angles[1], sv.protocol);
808 if (bits & U_ORIGIN3) MSG_WriteCoord(&buf, s->origin[2], sv.protocol);
809 if (bits & U_ANGLE3) MSG_WriteAngle(&buf, s->angles[2], sv.protocol);
810 if (bits & U_ALPHA) MSG_WriteByte(&buf, s->alpha);
811 if (bits & U_SCALE) MSG_WriteByte(&buf, s->scale);
812 if (bits & U_EFFECTS2) MSG_WriteByte(&buf, s->effects >> 8);
813 if (bits & U_GLOWSIZE) MSG_WriteByte(&buf, s->glowsize);
814 if (bits & U_GLOWCOLOR) MSG_WriteByte(&buf, s->glowcolor);
815 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);}
816 if (bits & U_FRAME2) MSG_WriteByte(&buf, s->frame >> 8);
817 if (bits & U_MODEL2) MSG_WriteByte(&buf, s->modelindex >> 8);
819 // the nasty protocol
820 if ((bits & U_EXTEND1) && sv.protocol == PROTOCOL_NEHAHRAMOVIE)
822 if (s->effects & EF_FULLBRIGHT)
824 MSG_WriteFloat(&buf, 2); // QSG protocol version
825 MSG_WriteFloat(&buf, s->alpha <= 0 ? 0 : (s->alpha >= 255 ? 1 : s->alpha * (1.0f / 255.0f))); // alpha
826 MSG_WriteFloat(&buf, 1); // fullbright
830 MSG_WriteFloat(&buf, 1); // QSG protocol version
831 MSG_WriteFloat(&buf, s->alpha <= 0 ? 0 : (s->alpha >= 255 ? 1 : s->alpha * (1.0f / 255.0f))); // alpha
835 // if the commit is full, we're done this frame
836 if (msg->cursize + buf.cursize > maxsize)
838 // next frame we will continue where we left off
841 // write the message to the packet
842 SZ_Write(msg, buf.data, buf.cursize);
843 ENTITYSIZEPROFILING_END(msg, s->number);
847 int EntityState_DeltaBits(const entity_state_t *o, const entity_state_t *n)
850 // if o is not active, delta from default
854 if (fabs(n->origin[0] - o->origin[0]) > (1.0f / 256.0f))
856 if (fabs(n->origin[1] - o->origin[1]) > (1.0f / 256.0f))
858 if (fabs(n->origin[2] - o->origin[2]) > (1.0f / 256.0f))
860 if ((unsigned char) (n->angles[0] * (256.0f / 360.0f)) != (unsigned char) (o->angles[0] * (256.0f / 360.0f)))
862 if ((unsigned char) (n->angles[1] * (256.0f / 360.0f)) != (unsigned char) (o->angles[1] * (256.0f / 360.0f)))
864 if ((unsigned char) (n->angles[2] * (256.0f / 360.0f)) != (unsigned char) (o->angles[2] * (256.0f / 360.0f)))
866 if ((n->modelindex ^ o->modelindex) & 0x00FF)
868 if ((n->modelindex ^ o->modelindex) & 0xFF00)
870 if ((n->frame ^ o->frame) & 0x00FF)
872 if ((n->frame ^ o->frame) & 0xFF00)
874 if ((n->effects ^ o->effects) & 0x00FF)
876 if ((n->effects ^ o->effects) & 0xFF00)
878 if (n->colormap != o->colormap)
880 if (n->skin != o->skin)
882 if (n->alpha != o->alpha)
884 if (n->scale != o->scale)
886 if (n->glowsize != o->glowsize)
888 if (n->glowcolor != o->glowcolor)
890 if (n->flags != o->flags)
892 if (n->tagindex != o->tagindex || n->tagentity != o->tagentity)
893 bits |= E_TAGATTACHMENT;
894 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])
896 if (n->lightstyle != o->lightstyle)
897 bits |= E_LIGHTSTYLE;
898 if (n->lightpflags != o->lightpflags)
899 bits |= E_LIGHTPFLAGS;
903 if (bits & 0xFF000000)
905 if (bits & 0x00FF0000)
907 if (bits & 0x0000FF00)
913 void EntityState_WriteExtendBits(sizebuf_t *msg, unsigned int bits)
915 MSG_WriteByte(msg, bits & 0xFF);
916 if (bits & 0x00000080)
918 MSG_WriteByte(msg, (bits >> 8) & 0xFF);
919 if (bits & 0x00008000)
921 MSG_WriteByte(msg, (bits >> 16) & 0xFF);
922 if (bits & 0x00800000)
923 MSG_WriteByte(msg, (bits >> 24) & 0xFF);
928 void EntityState_WriteFields(const entity_state_t *ent, sizebuf_t *msg, unsigned int bits)
930 if (sv.protocol == PROTOCOL_DARKPLACES2)
932 if (bits & E_ORIGIN1)
933 MSG_WriteCoord16i(msg, ent->origin[0]);
934 if (bits & E_ORIGIN2)
935 MSG_WriteCoord16i(msg, ent->origin[1]);
936 if (bits & E_ORIGIN3)
937 MSG_WriteCoord16i(msg, ent->origin[2]);
941 // LordHavoc: have to write flags first, as they can modify protocol
943 MSG_WriteByte(msg, ent->flags);
944 if (ent->flags & RENDER_LOWPRECISION)
946 if (bits & E_ORIGIN1)
947 MSG_WriteCoord16i(msg, ent->origin[0]);
948 if (bits & E_ORIGIN2)
949 MSG_WriteCoord16i(msg, ent->origin[1]);
950 if (bits & E_ORIGIN3)
951 MSG_WriteCoord16i(msg, ent->origin[2]);
955 if (bits & E_ORIGIN1)
956 MSG_WriteCoord32f(msg, ent->origin[0]);
957 if (bits & E_ORIGIN2)
958 MSG_WriteCoord32f(msg, ent->origin[1]);
959 if (bits & E_ORIGIN3)
960 MSG_WriteCoord32f(msg, ent->origin[2]);
963 if ((sv.protocol == PROTOCOL_DARKPLACES1 || sv.protocol == PROTOCOL_DARKPLACES2 || sv.protocol == PROTOCOL_DARKPLACES3 || sv.protocol == PROTOCOL_DARKPLACES4) && (ent->flags & RENDER_LOWPRECISION))
966 MSG_WriteAngle8i(msg, ent->angles[0]);
968 MSG_WriteAngle8i(msg, ent->angles[1]);
970 MSG_WriteAngle8i(msg, ent->angles[2]);
975 MSG_WriteAngle16i(msg, ent->angles[0]);
977 MSG_WriteAngle16i(msg, ent->angles[1]);
979 MSG_WriteAngle16i(msg, ent->angles[2]);
982 MSG_WriteByte(msg, ent->modelindex & 0xFF);
984 MSG_WriteByte(msg, (ent->modelindex >> 8) & 0xFF);
986 MSG_WriteByte(msg, ent->frame & 0xFF);
988 MSG_WriteByte(msg, (ent->frame >> 8) & 0xFF);
989 if (bits & E_EFFECTS1)
990 MSG_WriteByte(msg, ent->effects & 0xFF);
991 if (bits & E_EFFECTS2)
992 MSG_WriteByte(msg, (ent->effects >> 8) & 0xFF);
993 if (bits & E_COLORMAP)
994 MSG_WriteByte(msg, ent->colormap);
996 MSG_WriteByte(msg, ent->skin);
998 MSG_WriteByte(msg, ent->alpha);
1000 MSG_WriteByte(msg, ent->scale);
1001 if (bits & E_GLOWSIZE)
1002 MSG_WriteByte(msg, ent->glowsize);
1003 if (bits & E_GLOWCOLOR)
1004 MSG_WriteByte(msg, ent->glowcolor);
1005 if (sv.protocol == PROTOCOL_DARKPLACES2)
1007 MSG_WriteByte(msg, ent->flags);
1008 if (bits & E_TAGATTACHMENT)
1010 MSG_WriteShort(msg, ent->tagentity);
1011 MSG_WriteByte(msg, ent->tagindex);
1015 MSG_WriteShort(msg, ent->light[0]);
1016 MSG_WriteShort(msg, ent->light[1]);
1017 MSG_WriteShort(msg, ent->light[2]);
1018 MSG_WriteShort(msg, ent->light[3]);
1020 if (bits & E_LIGHTSTYLE)
1021 MSG_WriteByte(msg, ent->lightstyle);
1022 if (bits & E_LIGHTPFLAGS)
1023 MSG_WriteByte(msg, ent->lightpflags);
1026 void EntityState_WriteUpdate(const entity_state_t *ent, sizebuf_t *msg, const entity_state_t *delta)
1029 ENTITYSIZEPROFILING_START(msg, ent->number);
1032 // entity is active, check for changes from the delta
1033 if ((bits = EntityState_DeltaBits(delta, ent)))
1035 // write the update number, bits, and fields
1036 MSG_WriteShort(msg, ent->number);
1037 EntityState_WriteExtendBits(msg, bits);
1038 EntityState_WriteFields(ent, msg, bits);
1043 // entity is inactive, check if the delta was active
1046 // write the remove number
1047 MSG_WriteShort(msg, ent->number | 0x8000);
1050 ENTITYSIZEPROFILING_END(msg, ent->number);
1053 int EntityState_ReadExtendBits(void)
1056 bits = MSG_ReadByte();
1057 if (bits & 0x00000080)
1059 bits |= MSG_ReadByte() << 8;
1060 if (bits & 0x00008000)
1062 bits |= MSG_ReadByte() << 16;
1063 if (bits & 0x00800000)
1064 bits |= MSG_ReadByte() << 24;
1070 void EntityState_ReadFields(entity_state_t *e, unsigned int bits)
1072 if (cls.protocol == PROTOCOL_DARKPLACES2)
1074 if (bits & E_ORIGIN1)
1075 e->origin[0] = MSG_ReadCoord16i();
1076 if (bits & E_ORIGIN2)
1077 e->origin[1] = MSG_ReadCoord16i();
1078 if (bits & E_ORIGIN3)
1079 e->origin[2] = MSG_ReadCoord16i();
1084 e->flags = MSG_ReadByte();
1085 if (e->flags & RENDER_LOWPRECISION)
1087 if (bits & E_ORIGIN1)
1088 e->origin[0] = MSG_ReadCoord16i();
1089 if (bits & E_ORIGIN2)
1090 e->origin[1] = MSG_ReadCoord16i();
1091 if (bits & E_ORIGIN3)
1092 e->origin[2] = MSG_ReadCoord16i();
1096 if (bits & E_ORIGIN1)
1097 e->origin[0] = MSG_ReadCoord32f();
1098 if (bits & E_ORIGIN2)
1099 e->origin[1] = MSG_ReadCoord32f();
1100 if (bits & E_ORIGIN3)
1101 e->origin[2] = MSG_ReadCoord32f();
1104 if ((cls.protocol == PROTOCOL_DARKPLACES5 || cls.protocol == PROTOCOL_DARKPLACES6) && !(e->flags & RENDER_LOWPRECISION))
1106 if (bits & E_ANGLE1)
1107 e->angles[0] = MSG_ReadAngle16i();
1108 if (bits & E_ANGLE2)
1109 e->angles[1] = MSG_ReadAngle16i();
1110 if (bits & E_ANGLE3)
1111 e->angles[2] = MSG_ReadAngle16i();
1115 if (bits & E_ANGLE1)
1116 e->angles[0] = MSG_ReadAngle8i();
1117 if (bits & E_ANGLE2)
1118 e->angles[1] = MSG_ReadAngle8i();
1119 if (bits & E_ANGLE3)
1120 e->angles[2] = MSG_ReadAngle8i();
1122 if (bits & E_MODEL1)
1123 e->modelindex = (e->modelindex & 0xFF00) | (unsigned int) MSG_ReadByte();
1124 if (bits & E_MODEL2)
1125 e->modelindex = (e->modelindex & 0x00FF) | ((unsigned int) MSG_ReadByte() << 8);
1126 if (bits & E_FRAME1)
1127 e->frame = (e->frame & 0xFF00) | (unsigned int) MSG_ReadByte();
1128 if (bits & E_FRAME2)
1129 e->frame = (e->frame & 0x00FF) | ((unsigned int) MSG_ReadByte() << 8);
1130 if (bits & E_EFFECTS1)
1131 e->effects = (e->effects & 0xFF00) | (unsigned int) MSG_ReadByte();
1132 if (bits & E_EFFECTS2)
1133 e->effects = (e->effects & 0x00FF) | ((unsigned int) MSG_ReadByte() << 8);
1134 if (bits & E_COLORMAP)
1135 e->colormap = MSG_ReadByte();
1137 e->skin = MSG_ReadByte();
1139 e->alpha = MSG_ReadByte();
1141 e->scale = MSG_ReadByte();
1142 if (bits & E_GLOWSIZE)
1143 e->glowsize = MSG_ReadByte();
1144 if (bits & E_GLOWCOLOR)
1145 e->glowcolor = MSG_ReadByte();
1146 if (cls.protocol == PROTOCOL_DARKPLACES2)
1148 e->flags = MSG_ReadByte();
1149 if (bits & E_TAGATTACHMENT)
1151 e->tagentity = (unsigned short) MSG_ReadShort();
1152 e->tagindex = MSG_ReadByte();
1156 e->light[0] = (unsigned short) MSG_ReadShort();
1157 e->light[1] = (unsigned short) MSG_ReadShort();
1158 e->light[2] = (unsigned short) MSG_ReadShort();
1159 e->light[3] = (unsigned short) MSG_ReadShort();
1161 if (bits & E_LIGHTSTYLE)
1162 e->lightstyle = MSG_ReadByte();
1163 if (bits & E_LIGHTPFLAGS)
1164 e->lightpflags = MSG_ReadByte();
1166 if (developer_networkentities.integer >= 2)
1168 Con_Printf("ReadFields e%i", e->number);
1170 if (bits & E_ORIGIN1)
1171 Con_Printf(" E_ORIGIN1 %f", e->origin[0]);
1172 if (bits & E_ORIGIN2)
1173 Con_Printf(" E_ORIGIN2 %f", e->origin[1]);
1174 if (bits & E_ORIGIN3)
1175 Con_Printf(" E_ORIGIN3 %f", e->origin[2]);
1176 if (bits & E_ANGLE1)
1177 Con_Printf(" E_ANGLE1 %f", e->angles[0]);
1178 if (bits & E_ANGLE2)
1179 Con_Printf(" E_ANGLE2 %f", e->angles[1]);
1180 if (bits & E_ANGLE3)
1181 Con_Printf(" E_ANGLE3 %f", e->angles[2]);
1182 if (bits & (E_MODEL1 | E_MODEL2))
1183 Con_Printf(" E_MODEL %i", e->modelindex);
1185 if (bits & (E_FRAME1 | E_FRAME2))
1186 Con_Printf(" E_FRAME %i", e->frame);
1187 if (bits & (E_EFFECTS1 | E_EFFECTS2))
1188 Con_Printf(" E_EFFECTS %i", e->effects);
1190 Con_Printf(" E_ALPHA %f", e->alpha / 255.0f);
1192 Con_Printf(" E_SCALE %f", e->scale / 16.0f);
1193 if (bits & E_COLORMAP)
1194 Con_Printf(" E_COLORMAP %i", e->colormap);
1196 Con_Printf(" E_SKIN %i", e->skin);
1198 if (bits & E_GLOWSIZE)
1199 Con_Printf(" E_GLOWSIZE %i", e->glowsize * 4);
1200 if (bits & E_GLOWCOLOR)
1201 Con_Printf(" E_GLOWCOLOR %i", e->glowcolor);
1204 Con_Printf(" E_LIGHT %i:%i:%i:%i", e->light[0], e->light[1], e->light[2], e->light[3]);
1205 if (bits & E_LIGHTPFLAGS)
1206 Con_Printf(" E_LIGHTPFLAGS %i", e->lightpflags);
1208 if (bits & E_TAGATTACHMENT)
1209 Con_Printf(" E_TAGATTACHMENT e%i:%i", e->tagentity, e->tagindex);
1210 if (bits & E_LIGHTSTYLE)
1211 Con_Printf(" E_LIGHTSTYLE %i", e->lightstyle);
1216 // (client and server) allocates a new empty database
1217 entityframe_database_t *EntityFrame_AllocDatabase(mempool_t *mempool)
1219 return (entityframe_database_t *)Mem_Alloc(mempool, sizeof(entityframe_database_t));
1222 // (client and server) frees the database
1223 void EntityFrame_FreeDatabase(entityframe_database_t *d)
1228 // (server) clears the database to contain no frames (thus delta compression compresses against nothing)
1229 void EntityFrame_ClearDatabase(entityframe_database_t *d)
1231 memset(d, 0, sizeof(*d));
1234 // (server and client) removes frames older than 'frame' from database
1235 void EntityFrame_AckFrame(entityframe_database_t *d, int frame)
1238 d->ackframenum = frame;
1239 for (i = 0;i < d->numframes && d->frames[i].framenum < frame;i++);
1240 // ignore outdated frame acks (out of order packets)
1244 // if some queue is left, slide it down to beginning of array
1246 memmove(&d->frames[0], &d->frames[i], sizeof(d->frames[0]) * d->numframes);
1249 // (server) clears frame, to prepare for adding entities
1250 void EntityFrame_Clear(entity_frame_t *f, vec3_t eye, int framenum)
1253 f->framenum = framenum;
1256 VectorClear(f->eye);
1258 VectorCopy(eye, f->eye);
1261 // (server and client) reads a frame from the database
1262 void EntityFrame_FetchFrame(entityframe_database_t *d, int framenum, entity_frame_t *f)
1265 EntityFrame_Clear(f, NULL, -1);
1266 for (i = 0;i < d->numframes && d->frames[i].framenum < framenum;i++);
1267 if (i < d->numframes && framenum == d->frames[i].framenum)
1269 f->framenum = framenum;
1270 f->numentities = d->frames[i].endentity - d->frames[i].firstentity;
1271 n = MAX_ENTITY_DATABASE - (d->frames[i].firstentity % MAX_ENTITY_DATABASE);
1272 if (n > f->numentities)
1274 memcpy(f->entitydata, d->entitydata + d->frames[i].firstentity % MAX_ENTITY_DATABASE, sizeof(*f->entitydata) * n);
1275 if (f->numentities > n)
1276 memcpy(f->entitydata + n, d->entitydata, sizeof(*f->entitydata) * (f->numentities - n));
1277 VectorCopy(d->eye, f->eye);
1281 // (server and client) adds a entity_frame to the database, for future reference
1282 void EntityFrame_AddFrame(entityframe_database_t *d, vec3_t eye, int framenum, int numentities, const entity_state_t *entitydata)
1285 entity_frameinfo_t *info;
1287 VectorCopy(eye, d->eye);
1289 // figure out how many entity slots are used already
1292 n = d->frames[d->numframes - 1].endentity - d->frames[0].firstentity;
1293 if (n + numentities > MAX_ENTITY_DATABASE || d->numframes >= MAX_ENTITY_HISTORY)
1295 // ran out of room, dump database
1296 EntityFrame_ClearDatabase(d);
1300 info = &d->frames[d->numframes];
1301 info->framenum = framenum;
1303 // make sure we check the newly added frame as well, but we haven't incremented numframes yet
1304 for (n = 0;n <= d->numframes;n++)
1306 if (e >= d->frames[n].framenum)
1309 Con_Print("EntityFrame_AddFrame: tried to add out of sequence frame to database\n");
1311 Con_Print("EntityFrame_AddFrame: out of sequence frames in database\n");
1314 e = d->frames[n].framenum;
1316 // if database still has frames after that...
1318 info->firstentity = d->frames[d->numframes - 1].endentity;
1320 info->firstentity = 0;
1321 info->endentity = info->firstentity + numentities;
1324 n = info->firstentity % MAX_ENTITY_DATABASE;
1325 e = MAX_ENTITY_DATABASE - n;
1326 if (e > numentities)
1328 memcpy(d->entitydata + n, entitydata, sizeof(entity_state_t) * e);
1329 if (numentities > e)
1330 memcpy(d->entitydata, entitydata + e, sizeof(entity_state_t) * (numentities - e));
1333 // (server) writes a frame to network stream
1334 void EntityFrame_WriteFrame(sizebuf_t *msg, int maxsize, entityframe_database_t *d, int numstates, const entity_state_t *states, int viewentnum)
1336 int i, onum, number;
1337 entity_frame_t *o = &d->deltaframe;
1338 const entity_state_t *ent, *delta;
1342 d->latestframenum++;
1345 for (i = 0;i < numstates;i++)
1347 if (states[i].number == viewentnum)
1349 VectorSet(eye, states[i].origin[0], states[i].origin[1], states[i].origin[2] + 22);
1354 EntityFrame_AddFrame(d, eye, d->latestframenum, numstates, states);
1356 EntityFrame_FetchFrame(d, d->ackframenum, o);
1358 MSG_WriteByte (msg, svc_entities);
1359 MSG_WriteLong (msg, o->framenum);
1360 MSG_WriteLong (msg, d->latestframenum);
1361 MSG_WriteFloat (msg, eye[0]);
1362 MSG_WriteFloat (msg, eye[1]);
1363 MSG_WriteFloat (msg, eye[2]);
1366 for (i = 0;i < numstates;i++)
1369 number = ent->number;
1371 val = PRVM_EDICTFIELDVALUE((&prog->edicts[number]), prog->fieldoffsets.SendEntity);
1372 if(val && val->function)
1374 for (;onum < o->numentities && o->entitydata[onum].number < number;onum++)
1376 // write remove message
1377 MSG_WriteShort(msg, o->entitydata[onum].number | 0x8000);
1379 if (onum < o->numentities && (o->entitydata[onum].number == number))
1381 // delta from previous frame
1382 delta = o->entitydata + onum;
1383 // advance to next entity in delta frame
1388 // delta from defaults
1389 delta = &defaultstate;
1391 EntityState_WriteUpdate(ent, msg, delta);
1393 for (;onum < o->numentities;onum++)
1395 // write remove message
1396 MSG_WriteShort(msg, o->entitydata[onum].number | 0x8000);
1398 MSG_WriteShort(msg, 0xFFFF);
1401 // (client) reads a frame from network stream
1402 void EntityFrame_CL_ReadFrame(void)
1404 int i, number, removed;
1405 entity_frame_t *f, *delta;
1406 entity_state_t *e, *old, *oldend;
1408 entityframe_database_t *d;
1409 if (!cl.entitydatabase)
1410 cl.entitydatabase = EntityFrame_AllocDatabase(cls.levelmempool);
1411 d = cl.entitydatabase;
1413 delta = &d->deltaframe;
1415 EntityFrame_Clear(f, NULL, -1);
1417 // read the frame header info
1418 f->time = cl.mtime[0];
1419 number = MSG_ReadLong();
1420 for (i = 0;i < LATESTFRAMENUMS-1;i++)
1421 cl.latestframenums[i] = cl.latestframenums[i+1];
1422 cl.latestframenums[LATESTFRAMENUMS-1] = f->framenum = MSG_ReadLong();
1423 f->eye[0] = MSG_ReadFloat();
1424 f->eye[1] = MSG_ReadFloat();
1425 f->eye[2] = MSG_ReadFloat();
1426 EntityFrame_AckFrame(d, number);
1427 EntityFrame_FetchFrame(d, number, delta);
1428 old = delta->entitydata;
1429 oldend = old + delta->numentities;
1430 // read entities until we hit the magic 0xFFFF end tag
1431 while ((number = (unsigned short) MSG_ReadShort()) != 0xFFFF && !msg_badread)
1434 Host_Error("EntityFrame_Read: read error");
1435 removed = number & 0x8000;
1437 if (number >= MAX_EDICTS)
1438 Host_Error("EntityFrame_Read: number (%i) >= MAX_EDICTS (%i)", number, MAX_EDICTS);
1440 // seek to entity, while copying any skipped entities (assume unchanged)
1441 while (old < oldend && old->number < number)
1443 if (f->numentities >= MAX_ENTITY_DATABASE)
1444 Host_Error("EntityFrame_Read: entity list too big");
1445 f->entitydata[f->numentities] = *old++;
1446 f->entitydata[f->numentities++].time = cl.mtime[0];
1450 if (old < oldend && old->number == number)
1453 Con_Printf("EntityFrame_Read: REMOVE on unused entity %i\n", number);
1457 if (f->numentities >= MAX_ENTITY_DATABASE)
1458 Host_Error("EntityFrame_Read: entity list too big");
1460 // reserve this slot
1461 e = f->entitydata + f->numentities++;
1463 if (old < oldend && old->number == number)
1465 // delta from old entity
1470 // delta from defaults
1474 if (cl.num_entities <= number)
1476 cl.num_entities = number + 1;
1477 if (number >= cl.max_entities)
1478 CL_ExpandEntities(number);
1480 cl.entities_active[number] = true;
1482 e->time = cl.mtime[0];
1484 EntityState_ReadFields(e, EntityState_ReadExtendBits());
1487 while (old < oldend)
1489 if (f->numentities >= MAX_ENTITY_DATABASE)
1490 Host_Error("EntityFrame_Read: entity list too big");
1491 f->entitydata[f->numentities] = *old++;
1492 f->entitydata[f->numentities++].time = cl.mtime[0];
1494 EntityFrame_AddFrame(d, f->eye, f->framenum, f->numentities, f->entitydata);
1496 memset(cl.entities_active, 0, cl.num_entities * sizeof(unsigned char));
1498 for (i = 0;i < f->numentities;i++)
1500 for (;number < f->entitydata[i].number && number < cl.num_entities;number++)
1502 if (cl.entities_active[number])
1504 cl.entities_active[number] = false;
1505 cl.entities[number].state_current.active = false;
1508 if (number >= cl.num_entities)
1510 // update the entity
1511 ent = &cl.entities[number];
1512 ent->state_previous = ent->state_current;
1513 ent->state_current = f->entitydata[i];
1514 CL_MoveLerpEntityStates(ent);
1515 // the entity lives again...
1516 cl.entities_active[number] = true;
1519 for (;number < cl.num_entities;number++)
1521 if (cl.entities_active[number])
1523 cl.entities_active[number] = false;
1524 cl.entities[number].state_current.active = false;
1530 // (client) returns the frame number of the most recent frame recieved
1531 int EntityFrame_MostRecentlyRecievedFrameNum(entityframe_database_t *d)
1534 return d->frames[d->numframes - 1].framenum;
1544 entity_state_t *EntityFrame4_GetReferenceEntity(entityframe4_database_t *d, int number)
1546 if (d->maxreferenceentities <= number)
1548 int oldmax = d->maxreferenceentities;
1549 entity_state_t *oldentity = d->referenceentity;
1550 d->maxreferenceentities = (number + 15) & ~7;
1551 d->referenceentity = (entity_state_t *)Mem_Alloc(d->mempool, d->maxreferenceentities * sizeof(*d->referenceentity));
1554 memcpy(d->referenceentity, oldentity, oldmax * sizeof(*d->referenceentity));
1555 Mem_Free(oldentity);
1557 // clear the newly created entities
1558 for (;oldmax < d->maxreferenceentities;oldmax++)
1560 d->referenceentity[oldmax] = defaultstate;
1561 d->referenceentity[oldmax].number = oldmax;
1564 return d->referenceentity + number;
1567 void EntityFrame4_AddCommitEntity(entityframe4_database_t *d, const entity_state_t *s)
1569 // resize commit's entity list if full
1570 if (d->currentcommit->maxentities <= d->currentcommit->numentities)
1572 entity_state_t *oldentity = d->currentcommit->entity;
1573 d->currentcommit->maxentities += 8;
1574 d->currentcommit->entity = (entity_state_t *)Mem_Alloc(d->mempool, d->currentcommit->maxentities * sizeof(*d->currentcommit->entity));
1577 memcpy(d->currentcommit->entity, oldentity, d->currentcommit->numentities * sizeof(*d->currentcommit->entity));
1578 Mem_Free(oldentity);
1581 d->currentcommit->entity[d->currentcommit->numentities++] = *s;
1584 entityframe4_database_t *EntityFrame4_AllocDatabase(mempool_t *pool)
1586 entityframe4_database_t *d;
1587 d = (entityframe4_database_t *)Mem_Alloc(pool, sizeof(*d));
1589 EntityFrame4_ResetDatabase(d);
1593 void EntityFrame4_FreeDatabase(entityframe4_database_t *d)
1596 for (i = 0;i < MAX_ENTITY_HISTORY;i++)
1597 if (d->commit[i].entity)
1598 Mem_Free(d->commit[i].entity);
1599 if (d->referenceentity)
1600 Mem_Free(d->referenceentity);
1604 void EntityFrame4_ResetDatabase(entityframe4_database_t *d)
1607 d->referenceframenum = -1;
1608 for (i = 0;i < MAX_ENTITY_HISTORY;i++)
1609 d->commit[i].numentities = 0;
1610 for (i = 0;i < d->maxreferenceentities;i++)
1611 d->referenceentity[i] = defaultstate;
1614 int EntityFrame4_AckFrame(entityframe4_database_t *d, int framenum, int servermode)
1617 entity_database4_commit_t *commit;
1620 // reset reference, but leave commits alone
1621 d->referenceframenum = -1;
1622 for (i = 0;i < d->maxreferenceentities;i++)
1623 d->referenceentity[i] = defaultstate;
1624 // if this is the server, remove commits
1625 for (i = 0, commit = d->commit;i < MAX_ENTITY_HISTORY;i++, commit++)
1626 commit->numentities = 0;
1629 else if (d->referenceframenum == framenum)
1634 for (i = 0, commit = d->commit;i < MAX_ENTITY_HISTORY;i++, commit++)
1636 if (commit->numentities && commit->framenum <= framenum)
1638 if (commit->framenum == framenum)
1641 d->referenceframenum = framenum;
1642 if (developer_networkentities.integer >= 3)
1644 for (j = 0;j < commit->numentities;j++)
1646 entity_state_t *s = EntityFrame4_GetReferenceEntity(d, commit->entity[j].number);
1647 if (commit->entity[j].active != s->active)
1649 if (commit->entity[j].active)
1650 Con_Printf("commit entity %i has become active (modelindex %i)\n", commit->entity[j].number, commit->entity[j].modelindex);
1652 Con_Printf("commit entity %i has become inactive (modelindex %i)\n", commit->entity[j].number, commit->entity[j].modelindex);
1654 *s = commit->entity[j];
1658 for (j = 0;j < commit->numentities;j++)
1659 *EntityFrame4_GetReferenceEntity(d, commit->entity[j].number) = commit->entity[j];
1661 commit->numentities = 0;
1665 if (developer_networkentities.integer >= 1)
1667 Con_Printf("ack ref:%i database updated to: ref:%i commits:", framenum, d->referenceframenum);
1668 for (i = 0;i < MAX_ENTITY_HISTORY;i++)
1669 if (d->commit[i].numentities)
1670 Con_Printf(" %i", d->commit[i].framenum);
1676 void EntityFrame4_CL_ReadFrame(void)
1678 int i, n, cnumber, referenceframenum, framenum, enumber, done, stopnumber, skip = false;
1680 entityframe4_database_t *d;
1681 if (!cl.entitydatabase4)
1682 cl.entitydatabase4 = EntityFrame4_AllocDatabase(cls.levelmempool);
1683 d = cl.entitydatabase4;
1684 // read the number of the frame this refers to
1685 referenceframenum = MSG_ReadLong();
1686 // read the number of this frame
1687 for (i = 0;i < LATESTFRAMENUMS-1;i++)
1688 cl.latestframenums[i] = cl.latestframenums[i+1];
1689 cl.latestframenums[LATESTFRAMENUMS-1] = framenum = MSG_ReadLong();
1690 // read the start number
1691 enumber = (unsigned short) MSG_ReadShort();
1692 if (developer_networkentities.integer >= 10)
1694 Con_Printf("recv svc_entities num:%i ref:%i database: ref:%i commits:", framenum, referenceframenum, d->referenceframenum);
1695 for (i = 0;i < MAX_ENTITY_HISTORY;i++)
1696 if (d->commit[i].numentities)
1697 Con_Printf(" %i", d->commit[i].framenum);
1700 if (!EntityFrame4_AckFrame(d, referenceframenum, false))
1702 Con_Print("EntityFrame4_CL_ReadFrame: reference frame invalid (VERY BAD ERROR), this update will be skipped\n");
1705 d->currentcommit = NULL;
1706 for (i = 0;i < MAX_ENTITY_HISTORY;i++)
1708 if (!d->commit[i].numentities)
1710 d->currentcommit = d->commit + i;
1711 d->currentcommit->framenum = framenum;
1712 d->currentcommit->numentities = 0;
1715 if (d->currentcommit == NULL)
1717 Con_Printf("EntityFrame4_CL_ReadFrame: error while decoding frame %i: database full, reading but not storing this update\n", framenum);
1721 while (!done && !msg_badread)
1723 // read the number of the modified entity
1724 // (gaps will be copied unmodified)
1725 n = (unsigned short)MSG_ReadShort();
1728 // no more entities in this update, but we still need to copy the
1729 // rest of the reference entities (final gap)
1731 // read end of range number, then process normally
1732 n = (unsigned short)MSG_ReadShort();
1734 // high bit means it's a remove message
1735 cnumber = n & 0x7FFF;
1736 // if this is a live entity we may need to expand the array
1737 if (cl.num_entities <= cnumber && !(n & 0x8000))
1739 cl.num_entities = cnumber + 1;
1740 if (cnumber >= cl.max_entities)
1741 CL_ExpandEntities(cnumber);
1743 // add one (the changed one) if not done
1744 stopnumber = cnumber + !done;
1745 // process entities in range from the last one to the changed one
1746 for (;enumber < stopnumber;enumber++)
1748 if (skip || enumber >= cl.num_entities)
1750 if (enumber == cnumber && (n & 0x8000) == 0)
1752 entity_state_t tempstate;
1753 EntityState_ReadFields(&tempstate, EntityState_ReadExtendBits());
1757 // slide the current into the previous slot
1758 cl.entities[enumber].state_previous = cl.entities[enumber].state_current;
1759 // copy a new current from reference database
1760 cl.entities[enumber].state_current = *EntityFrame4_GetReferenceEntity(d, enumber);
1761 s = &cl.entities[enumber].state_current;
1762 // if this is the one to modify, read more data...
1763 if (enumber == cnumber)
1768 if (developer_networkentities.integer >= 2)
1769 Con_Printf("entity %i: remove\n", enumber);
1775 if (developer_networkentities.integer >= 2)
1776 Con_Printf("entity %i: update\n", enumber);
1778 EntityState_ReadFields(s, EntityState_ReadExtendBits());
1781 else if (developer_networkentities.integer >= 4)
1782 Con_Printf("entity %i: copy\n", enumber);
1783 // set the cl.entities_active flag
1784 cl.entities_active[enumber] = s->active;
1785 // set the update time
1786 s->time = cl.mtime[0];
1787 // fix the number (it gets wiped occasionally by copying from defaultstate)
1788 s->number = enumber;
1789 // check if we need to update the lerp stuff
1791 CL_MoveLerpEntityStates(&cl.entities[enumber]);
1792 // add this to the commit entry whether it is modified or not
1793 if (d->currentcommit)
1794 EntityFrame4_AddCommitEntity(d, &cl.entities[enumber].state_current);
1795 // print extra messages if desired
1796 if (developer_networkentities.integer >= 2 && cl.entities[enumber].state_current.active != cl.entities[enumber].state_previous.active)
1798 if (cl.entities[enumber].state_current.active)
1799 Con_Printf("entity #%i has become active\n", enumber);
1800 else if (cl.entities[enumber].state_previous.active)
1801 Con_Printf("entity #%i has become inactive\n", enumber);
1805 d->currentcommit = NULL;
1807 EntityFrame4_ResetDatabase(d);
1810 void EntityFrame4_WriteFrame(sizebuf_t *msg, int maxsize, entityframe4_database_t *d, int numstates, const entity_state_t *states)
1812 const entity_state_t *e, *s;
1813 entity_state_t inactiveentitystate;
1814 int i, n, startnumber;
1816 unsigned char data[128];
1819 // if there isn't enough space to accomplish anything, skip it
1820 if (msg->cursize + 24 > maxsize)
1823 // prepare the buffer
1824 memset(&buf, 0, sizeof(buf));
1826 buf.maxsize = sizeof(data);
1828 for (i = 0;i < MAX_ENTITY_HISTORY;i++)
1829 if (!d->commit[i].numentities)
1831 // if commit buffer full, just don't bother writing an update this frame
1832 if (i == MAX_ENTITY_HISTORY)
1834 d->currentcommit = d->commit + i;
1836 // this state's number gets played around with later
1837 inactiveentitystate = defaultstate;
1839 d->currentcommit->numentities = 0;
1840 d->currentcommit->framenum = ++d->latestframenumber;
1841 MSG_WriteByte(msg, svc_entities);
1842 MSG_WriteLong(msg, d->referenceframenum);
1843 MSG_WriteLong(msg, d->currentcommit->framenum);
1844 if (developer_networkentities.integer >= 10)
1846 Con_Printf("send svc_entities num:%i ref:%i (database: ref:%i commits:", d->currentcommit->framenum, d->referenceframenum, d->referenceframenum);
1847 for (i = 0;i < MAX_ENTITY_HISTORY;i++)
1848 if (d->commit[i].numentities)
1849 Con_Printf(" %i", d->commit[i].framenum);
1852 if (d->currententitynumber >= prog->max_edicts)
1855 startnumber = bound(1, d->currententitynumber, prog->max_edicts - 1);
1856 MSG_WriteShort(msg, startnumber);
1857 // reset currententitynumber so if the loop does not break it we will
1858 // start at beginning next frame (if it does break, it will set it)
1859 d->currententitynumber = 1;
1860 for (i = 0, n = startnumber;n < prog->max_edicts;n++)
1862 val = PRVM_EDICTFIELDVALUE((&prog->edicts[n]), prog->fieldoffsets.SendEntity);
1863 if(val && val->function)
1865 // find the old state to delta from
1866 e = EntityFrame4_GetReferenceEntity(d, n);
1867 // prepare the buffer
1869 // entity exists, build an update (if empty there is no change)
1870 // find the state in the list
1871 for (;i < numstates && states[i].number < n;i++);
1877 EntityState_WriteUpdate(s, &buf, e);
1881 inactiveentitystate.number = n;
1882 s = &inactiveentitystate;
1885 // entity used to exist but doesn't anymore, send remove
1886 MSG_WriteShort(&buf, n | 0x8000);
1889 // if the commit is full, we're done this frame
1890 if (msg->cursize + buf.cursize > maxsize - 4)
1892 // next frame we will continue where we left off
1895 // add the entity to the commit
1896 EntityFrame4_AddCommitEntity(d, s);
1897 // if the message is empty, skip out now
1900 // write the message to the packet
1901 SZ_Write(msg, buf.data, buf.cursize);
1904 d->currententitynumber = n;
1906 // remove world message (invalid, and thus a good terminator)
1907 MSG_WriteShort(msg, 0x8000);
1908 // write the number of the end entity
1909 MSG_WriteShort(msg, d->currententitynumber);
1911 d->currentcommit = NULL;
1917 entityframe5_database_t *EntityFrame5_AllocDatabase(mempool_t *pool)
1920 entityframe5_database_t *d;
1921 d = (entityframe5_database_t *)Mem_Alloc(pool, sizeof(*d));
1922 d->latestframenum = 0;
1923 for (i = 0;i < d->maxedicts;i++)
1924 d->states[i] = defaultstate;
1928 void EntityFrame5_FreeDatabase(entityframe5_database_t *d)
1930 // all the [maxedicts] memory is allocated at once, so there's only one
1933 Mem_Free(d->deltabits);
1937 static void EntityFrame5_ExpandEdicts(entityframe5_database_t *d, int newmax)
1939 if (d->maxedicts < newmax)
1941 unsigned char *data;
1942 int oldmaxedicts = d->maxedicts;
1943 int *olddeltabits = d->deltabits;
1944 unsigned char *oldpriorities = d->priorities;
1945 int *oldupdateframenum = d->updateframenum;
1946 entity_state_t *oldstates = d->states;
1947 unsigned char *oldvisiblebits = d->visiblebits;
1948 d->maxedicts = newmax;
1949 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));
1950 d->deltabits = (int *)data;data += d->maxedicts * sizeof(int);
1951 d->priorities = (unsigned char *)data;data += d->maxedicts * sizeof(unsigned char);
1952 d->updateframenum = (int *)data;data += d->maxedicts * sizeof(int);
1953 d->states = (entity_state_t *)data;data += d->maxedicts * sizeof(entity_state_t);
1954 d->visiblebits = (unsigned char *)data;data += (d->maxedicts+7)/8 * sizeof(unsigned char);
1957 memcpy(d->deltabits, olddeltabits, oldmaxedicts * sizeof(int));
1958 memcpy(d->priorities, oldpriorities, oldmaxedicts * sizeof(unsigned char));
1959 memcpy(d->updateframenum, oldupdateframenum, oldmaxedicts * sizeof(int));
1960 memcpy(d->states, oldstates, oldmaxedicts * sizeof(entity_state_t));
1961 memcpy(d->visiblebits, oldvisiblebits, (oldmaxedicts+7)/8 * sizeof(unsigned char));
1962 // the previous buffers were a single allocation, so just one free
1963 Mem_Free(olddeltabits);
1968 static int EntityState5_Priority(entityframe5_database_t *d, int stateindex)
1970 int limit, priority;
1972 // if it is the player, update urgently
1973 if (stateindex == d->viewentnum)
1974 return ENTITYFRAME5_PRIORITYLEVELS - 1;
1975 // priority increases each frame no matter what happens
1976 priority = d->priorities[stateindex] + 1;
1977 // players get an extra priority boost
1978 if (stateindex <= svs.maxclients)
1980 // remove dead entities very quickly because they are just 2 bytes
1981 if (!d->states[stateindex].active)
1984 return bound(1, priority, ENTITYFRAME5_PRIORITYLEVELS - 1);
1986 // certain changes are more noticable than others
1987 if (d->deltabits[stateindex] & (E5_FULLUPDATE | E5_ATTACHMENT | E5_MODEL | E5_FLAGS | E5_COLORMAP))
1989 // find the root entity this one is attached to, and judge relevance by it
1990 for (limit = 0;limit < 256;limit++)
1992 s = d->states + stateindex;
1993 if (s->flags & RENDER_VIEWMODEL)
1994 stateindex = d->viewentnum;
1995 else if (s->tagentity)
1996 stateindex = s->tagentity;
1999 if (d->maxedicts < stateindex)
2000 EntityFrame5_ExpandEdicts(d, (stateindex+256)&~255);
2003 Con_DPrintf("Protocol: Runaway loop recursing tagentity links on entity %i\n", stateindex);
2004 // now that we have the parent entity we can make some decisions based on
2005 // distance from the player
2006 if (VectorDistance(d->states[d->viewentnum].netcenter, s->netcenter) < 1024.0f)
2008 return bound(1, priority, ENTITYFRAME5_PRIORITYLEVELS - 1);
2011 void EntityState5_WriteUpdate(int number, const entity_state_t *s, int changedbits, sizebuf_t *msg)
2013 unsigned int bits = 0;
2014 //dp_model_t *model;
2015 ENTITYSIZEPROFILING_START(msg, s->number);
2018 val = PRVM_EDICTFIELDVALUE((&prog->edicts[s->number]), prog->fieldoffsets.SendEntity);
2019 if(val && val->function)
2023 MSG_WriteShort(msg, number | 0x8000);
2027 if ((bits & E5_ORIGIN) && (s->exteriormodelforclient || s->tagentity || s->viewmodelforclient || (s->number >= 1 && s->number <= svs.maxclients) || s->origin[0] <= -4096.0625 || s->origin[0] >= 4095.9375 || s->origin[1] <= -4096.0625 || s->origin[1] >= 4095.9375 || s->origin[2] <= -4096.0625 || s->origin[2] >= 4095.9375))
2028 // maybe also add: ((model = sv.models[s->modelindex]) != NULL && model->name[0] == '*')
2029 bits |= E5_ORIGIN32;
2032 // (int)(f * 8 - 0.5) >= -32768
2033 // (f * 8 - 0.5) > -32769
2036 // (int)(f * 8 + 0.5) <= 32767
2037 // (f * 8 + 0.5) < 32768
2038 // f * 8 + 0.5) < 4095.9375
2039 if ((bits & E5_ANGLES) && !(s->flags & RENDER_LOWPRECISION))
2040 bits |= E5_ANGLES16;
2041 if ((bits & E5_MODEL) && s->modelindex >= 256)
2043 if ((bits & E5_FRAME) && s->frame >= 256)
2045 if (bits & E5_EFFECTS)
2047 if (s->effects & 0xFFFF0000)
2048 bits |= E5_EFFECTS32;
2049 else if (s->effects & 0xFFFFFF00)
2050 bits |= E5_EFFECTS16;
2056 if (bits >= 16777216)
2058 MSG_WriteShort(msg, number);
2059 MSG_WriteByte(msg, bits & 0xFF);
2060 if (bits & E5_EXTEND1)
2061 MSG_WriteByte(msg, (bits >> 8) & 0xFF);
2062 if (bits & E5_EXTEND2)
2063 MSG_WriteByte(msg, (bits >> 16) & 0xFF);
2064 if (bits & E5_EXTEND3)
2065 MSG_WriteByte(msg, (bits >> 24) & 0xFF);
2066 if (bits & E5_FLAGS)
2067 MSG_WriteByte(msg, s->flags);
2068 if (bits & E5_ORIGIN)
2070 if (bits & E5_ORIGIN32)
2072 MSG_WriteCoord32f(msg, s->origin[0]);
2073 MSG_WriteCoord32f(msg, s->origin[1]);
2074 MSG_WriteCoord32f(msg, s->origin[2]);
2078 MSG_WriteCoord13i(msg, s->origin[0]);
2079 MSG_WriteCoord13i(msg, s->origin[1]);
2080 MSG_WriteCoord13i(msg, s->origin[2]);
2083 if (bits & E5_ANGLES)
2085 if (bits & E5_ANGLES16)
2087 MSG_WriteAngle16i(msg, s->angles[0]);
2088 MSG_WriteAngle16i(msg, s->angles[1]);
2089 MSG_WriteAngle16i(msg, s->angles[2]);
2093 MSG_WriteAngle8i(msg, s->angles[0]);
2094 MSG_WriteAngle8i(msg, s->angles[1]);
2095 MSG_WriteAngle8i(msg, s->angles[2]);
2098 if (bits & E5_MODEL)
2100 if (bits & E5_MODEL16)
2101 MSG_WriteShort(msg, s->modelindex);
2103 MSG_WriteByte(msg, s->modelindex);
2105 if (bits & E5_FRAME)
2107 if (bits & E5_FRAME16)
2108 MSG_WriteShort(msg, s->frame);
2110 MSG_WriteByte(msg, s->frame);
2113 MSG_WriteByte(msg, s->skin);
2114 if (bits & E5_EFFECTS)
2116 if (bits & E5_EFFECTS32)
2117 MSG_WriteLong(msg, s->effects);
2118 else if (bits & E5_EFFECTS16)
2119 MSG_WriteShort(msg, s->effects);
2121 MSG_WriteByte(msg, s->effects);
2123 if (bits & E5_ALPHA)
2124 MSG_WriteByte(msg, s->alpha);
2125 if (bits & E5_SCALE)
2126 MSG_WriteByte(msg, s->scale);
2127 if (bits & E5_COLORMAP)
2128 MSG_WriteByte(msg, s->colormap);
2129 if (bits & E5_ATTACHMENT)
2131 MSG_WriteShort(msg, s->tagentity);
2132 MSG_WriteByte(msg, s->tagindex);
2134 if (bits & E5_LIGHT)
2136 MSG_WriteShort(msg, s->light[0]);
2137 MSG_WriteShort(msg, s->light[1]);
2138 MSG_WriteShort(msg, s->light[2]);
2139 MSG_WriteShort(msg, s->light[3]);
2140 MSG_WriteByte(msg, s->lightstyle);
2141 MSG_WriteByte(msg, s->lightpflags);
2145 MSG_WriteByte(msg, s->glowsize);
2146 MSG_WriteByte(msg, s->glowcolor);
2148 if (bits & E5_COLORMOD)
2150 MSG_WriteByte(msg, s->colormod[0]);
2151 MSG_WriteByte(msg, s->colormod[1]);
2152 MSG_WriteByte(msg, s->colormod[2]);
2156 ENTITYSIZEPROFILING_END(msg, s->number);
2159 static void EntityState5_ReadUpdate(entity_state_t *s, int number)
2162 bits = MSG_ReadByte();
2163 if (bits & E5_EXTEND1)
2165 bits |= MSG_ReadByte() << 8;
2166 if (bits & E5_EXTEND2)
2168 bits |= MSG_ReadByte() << 16;
2169 if (bits & E5_EXTEND3)
2170 bits |= MSG_ReadByte() << 24;
2173 if (bits & E5_FULLUPDATE)
2178 if (bits & E5_FLAGS)
2179 s->flags = MSG_ReadByte();
2180 if (bits & E5_ORIGIN)
2182 if (bits & E5_ORIGIN32)
2184 s->origin[0] = MSG_ReadCoord32f();
2185 s->origin[1] = MSG_ReadCoord32f();
2186 s->origin[2] = MSG_ReadCoord32f();
2190 s->origin[0] = MSG_ReadCoord13i();
2191 s->origin[1] = MSG_ReadCoord13i();
2192 s->origin[2] = MSG_ReadCoord13i();
2195 if (bits & E5_ANGLES)
2197 if (bits & E5_ANGLES16)
2199 s->angles[0] = MSG_ReadAngle16i();
2200 s->angles[1] = MSG_ReadAngle16i();
2201 s->angles[2] = MSG_ReadAngle16i();
2205 s->angles[0] = MSG_ReadAngle8i();
2206 s->angles[1] = MSG_ReadAngle8i();
2207 s->angles[2] = MSG_ReadAngle8i();
2210 if (bits & E5_MODEL)
2212 if (bits & E5_MODEL16)
2213 s->modelindex = (unsigned short) MSG_ReadShort();
2215 s->modelindex = MSG_ReadByte();
2217 if (bits & E5_FRAME)
2219 if (bits & E5_FRAME16)
2220 s->frame = (unsigned short) MSG_ReadShort();
2222 s->frame = MSG_ReadByte();
2225 s->skin = MSG_ReadByte();
2226 if (bits & E5_EFFECTS)
2228 if (bits & E5_EFFECTS32)
2229 s->effects = (unsigned int) MSG_ReadLong();
2230 else if (bits & E5_EFFECTS16)
2231 s->effects = (unsigned short) MSG_ReadShort();
2233 s->effects = MSG_ReadByte();
2235 if (bits & E5_ALPHA)
2236 s->alpha = MSG_ReadByte();
2237 if (bits & E5_SCALE)
2238 s->scale = MSG_ReadByte();
2239 if (bits & E5_COLORMAP)
2240 s->colormap = MSG_ReadByte();
2241 if (bits & E5_ATTACHMENT)
2243 s->tagentity = (unsigned short) MSG_ReadShort();
2244 s->tagindex = MSG_ReadByte();
2246 if (bits & E5_LIGHT)
2248 s->light[0] = (unsigned short) MSG_ReadShort();
2249 s->light[1] = (unsigned short) MSG_ReadShort();
2250 s->light[2] = (unsigned short) MSG_ReadShort();
2251 s->light[3] = (unsigned short) MSG_ReadShort();
2252 s->lightstyle = MSG_ReadByte();
2253 s->lightpflags = MSG_ReadByte();
2257 s->glowsize = MSG_ReadByte();
2258 s->glowcolor = MSG_ReadByte();
2260 if (bits & E5_COLORMOD)
2262 s->colormod[0] = MSG_ReadByte();
2263 s->colormod[1] = MSG_ReadByte();
2264 s->colormod[2] = MSG_ReadByte();
2268 if (developer_networkentities.integer >= 2)
2270 Con_Printf("ReadFields e%i", number);
2272 if (bits & E5_ORIGIN)
2273 Con_Printf(" E5_ORIGIN %f %f %f", s->origin[0], s->origin[1], s->origin[2]);
2274 if (bits & E5_ANGLES)
2275 Con_Printf(" E5_ANGLES %f %f %f", s->angles[0], s->angles[1], s->angles[2]);
2276 if (bits & E5_MODEL)
2277 Con_Printf(" E5_MODEL %i", s->modelindex);
2278 if (bits & E5_FRAME)
2279 Con_Printf(" E5_FRAME %i", s->frame);
2281 Con_Printf(" E5_SKIN %i", s->skin);
2282 if (bits & E5_EFFECTS)
2283 Con_Printf(" E5_EFFECTS %i", s->effects);
2284 if (bits & E5_FLAGS)
2286 Con_Printf(" E5_FLAGS %i (", s->flags);
2287 if (s->flags & RENDER_STEP)
2289 if (s->flags & RENDER_GLOWTRAIL)
2290 Con_Print(" GLOWTRAIL");
2291 if (s->flags & RENDER_VIEWMODEL)
2292 Con_Print(" VIEWMODEL");
2293 if (s->flags & RENDER_EXTERIORMODEL)
2294 Con_Print(" EXTERIORMODEL");
2295 if (s->flags & RENDER_LOWPRECISION)
2296 Con_Print(" LOWPRECISION");
2297 if (s->flags & RENDER_COLORMAPPED)
2298 Con_Print(" COLORMAPPED");
2299 if (s->flags & RENDER_SHADOW)
2300 Con_Print(" SHADOW");
2301 if (s->flags & RENDER_LIGHT)
2302 Con_Print(" LIGHT");
2303 if (s->flags & RENDER_NOSELFSHADOW)
2304 Con_Print(" NOSELFSHADOW");
2307 if (bits & E5_ALPHA)
2308 Con_Printf(" E5_ALPHA %f", s->alpha / 255.0f);
2309 if (bits & E5_SCALE)
2310 Con_Printf(" E5_SCALE %f", s->scale / 16.0f);
2311 if (bits & E5_COLORMAP)
2312 Con_Printf(" E5_COLORMAP %i", s->colormap);
2313 if (bits & E5_ATTACHMENT)
2314 Con_Printf(" E5_ATTACHMENT e%i:%i", s->tagentity, s->tagindex);
2315 if (bits & E5_LIGHT)
2316 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);
2318 Con_Printf(" E5_GLOW %i:%i", s->glowsize * 4, s->glowcolor);
2319 if (bits & E5_COLORMOD)
2320 Con_Printf(" E5_COLORMOD %f:%f:%f", s->colormod[0] / 32.0f, s->colormod[1] / 32.0f, s->colormod[2] / 32.0f);
2325 static int EntityState5_DeltaBits(const entity_state_t *o, const entity_state_t *n)
2327 unsigned int bits = 0;
2331 bits |= E5_FULLUPDATE;
2332 if (!VectorCompare(o->origin, n->origin))
2334 if (!VectorCompare(o->angles, n->angles))
2336 if (o->modelindex != n->modelindex)
2338 if (o->frame != n->frame)
2340 if (o->skin != n->skin)
2342 if (o->effects != n->effects)
2344 if (o->flags != n->flags)
2346 if (o->alpha != n->alpha)
2348 if (o->scale != n->scale)
2350 if (o->colormap != n->colormap)
2351 bits |= E5_COLORMAP;
2352 if (o->tagentity != n->tagentity || o->tagindex != n->tagindex)
2353 bits |= E5_ATTACHMENT;
2354 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)
2356 if (o->glowsize != n->glowsize || o->glowcolor != n->glowcolor)
2358 if (o->colormod[0] != n->colormod[0] || o->colormod[1] != n->colormod[1] || o->colormod[2] != n->colormod[2])
2359 bits |= E5_COLORMOD;
2363 bits |= E5_FULLUPDATE;
2367 void EntityFrame5_CL_ReadFrame(void)
2372 // read the number of this frame to echo back in next input packet
2373 for (i = 0;i < LATESTFRAMENUMS-1;i++)
2374 cl.latestframenums[i] = cl.latestframenums[i+1];
2375 cl.latestframenums[LATESTFRAMENUMS-1] = MSG_ReadLong();
2376 if (developer_networkentities.integer >= 10)
2377 Con_Printf("recv: svc_entities %i\n", cl.latestframenums[LATESTFRAMENUMS-1]);
2378 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)
2379 cls.servermovesequence = MSG_ReadLong();
2380 // read entity numbers until we find a 0x8000
2381 // (which would be remove world entity, but is actually a terminator)
2382 while ((n = (unsigned short)MSG_ReadShort()) != 0x8000 && !msg_badread)
2384 // get the entity number
2385 enumber = n & 0x7FFF;
2386 // we may need to expand the array
2387 if (cl.num_entities <= enumber)
2389 cl.num_entities = enumber + 1;
2390 if (enumber >= cl.max_entities)
2391 CL_ExpandEntities(enumber);
2393 // look up the entity
2394 ent = cl.entities + enumber;
2395 // slide the current into the previous slot
2396 ent->state_previous = ent->state_current;
2398 s = &ent->state_current;
2407 EntityState5_ReadUpdate(s, enumber);
2409 // set the cl.entities_active flag
2410 cl.entities_active[enumber] = s->active;
2411 // set the update time
2412 s->time = cl.mtime[0];
2413 // fix the number (it gets wiped occasionally by copying from defaultstate)
2414 s->number = enumber;
2415 // check if we need to update the lerp stuff
2417 CL_MoveLerpEntityStates(&cl.entities[enumber]);
2418 // print extra messages if desired
2419 if (developer_networkentities.integer >= 2 && cl.entities[enumber].state_current.active != cl.entities[enumber].state_previous.active)
2421 if (cl.entities[enumber].state_current.active)
2422 Con_Printf("entity #%i has become active\n", enumber);
2423 else if (cl.entities[enumber].state_previous.active)
2424 Con_Printf("entity #%i has become inactive\n", enumber);
2429 void EntityFrame5_LostFrame(entityframe5_database_t *d, int framenum)
2431 int i, j, k, l, bits;
2432 entityframe5_changestate_t *s, *s2;
2433 entityframe5_packetlog_t *p, *p2;
2434 unsigned char statsdeltabits[(MAX_CL_STATS+7)/8];
2435 // scan for packets that were lost
2436 for (i = 0, p = d->packetlog;i < ENTITYFRAME5_MAXPACKETLOGS;i++, p++)
2438 if (p->packetnumber && p->packetnumber <= framenum)
2440 // packet was lost - merge deltabits into the main array so they
2441 // will be re-sent, but only if there is no newer update of that
2442 // bit in the logs (as those will arrive before this update)
2443 for (j = 0, s = p->states;j < p->numstates;j++, s++)
2445 // check for any newer updates to this entity and mask off any
2446 // overlapping bits (we don't need to send something again if
2447 // it has already been sent more recently)
2448 bits = s->bits & ~d->deltabits[s->number];
2449 for (k = 0, p2 = d->packetlog;k < ENTITYFRAME5_MAXPACKETLOGS && bits;k++, p2++)
2451 if (p2->packetnumber > framenum)
2453 for (l = 0, s2 = p2->states;l < p2->numstates;l++, s2++)
2455 if (s2->number == s->number)
2463 // if the bits haven't all been cleared, there were some bits
2464 // lost with this packet, so set them again now
2467 d->deltabits[s->number] |= bits;
2468 // if it was a very important update, set priority higher
2469 if (bits & (E5_FULLUPDATE | E5_ATTACHMENT | E5_MODEL | E5_COLORMAP))
2470 d->priorities[s->number] = max(d->priorities[s->number], 4);
2472 d->priorities[s->number] = max(d->priorities[s->number], 1);
2476 for (j = 0;j < MAX_CL_STATS;j++)
2478 for (l = 0;l < (MAX_CL_STATS+7)/8;l++)
2479 statsdeltabits[l] = p->statsdeltabits[l] & ~host_client->statsdeltabits[l];
2480 for (k = 0, p2 = d->packetlog;k < ENTITYFRAME5_MAXPACKETLOGS;k++, p2++)
2481 if (p2->packetnumber > framenum)
2482 for (l = 0;l < (MAX_CL_STATS+7)/8;l++)
2483 statsdeltabits[l] = p->statsdeltabits[l] & ~p2->statsdeltabits[l];
2484 for (l = 0;l < (MAX_CL_STATS+7)/8;l++)
2485 host_client->statsdeltabits[l] |= statsdeltabits[l];
2487 // delete this packet log as it is now obsolete
2488 p->packetnumber = 0;
2493 void EntityFrame5_AckFrame(entityframe5_database_t *d, int framenum)
2496 // scan for packets made obsolete by this ack and delete them
2497 for (i = 0;i < ENTITYFRAME5_MAXPACKETLOGS;i++)
2498 if (d->packetlog[i].packetnumber <= framenum)
2499 d->packetlog[i].packetnumber = 0;
2502 void EntityFrame5_WriteFrame(sizebuf_t *msg, int maxsize, entityframe5_database_t *d, int numstates, const entity_state_t *states, int viewentnum, int movesequence, qboolean need_empty)
2504 const entity_state_t *n;
2505 int i, num, l, framenum, packetlognumber, priority;
2507 unsigned char data[128];
2508 entityframe5_packetlog_t *packetlog;
2510 if (prog->max_edicts > d->maxedicts)
2511 EntityFrame5_ExpandEdicts(d, prog->max_edicts);
2513 framenum = d->latestframenum + 1;
2514 d->viewentnum = viewentnum;
2516 // if packet log is full, mark all frames as lost, this will cause
2517 // it to send the lost data again
2518 for (packetlognumber = 0;packetlognumber < ENTITYFRAME5_MAXPACKETLOGS;packetlognumber++)
2519 if (d->packetlog[packetlognumber].packetnumber == 0)
2521 if (packetlognumber == ENTITYFRAME5_MAXPACKETLOGS)
2523 Con_DPrintf("EntityFrame5_WriteFrame: packetlog overflow for a client, resetting\n");
2524 EntityFrame5_LostFrame(d, framenum);
2525 packetlognumber = 0;
2528 // prepare the buffer
2529 memset(&buf, 0, sizeof(buf));
2531 buf.maxsize = sizeof(data);
2533 // detect changes in states
2535 for (i = 0, n = states;i < numstates;i++, n++)
2537 // mark gaps in entity numbering as removed entities
2538 for (;num < n->number;num++)
2540 // if the entity used to exist, clear it
2541 if (CHECKPVSBIT(d->visiblebits, num))
2543 CLEARPVSBIT(d->visiblebits, num);
2544 d->deltabits[num] = E5_FULLUPDATE;
2545 d->priorities[num] = max(d->priorities[num], 8); // removal is cheap
2546 d->states[num] = defaultstate;
2547 d->states[num].number = num;
2550 // update the entity state data
2551 if (!CHECKPVSBIT(d->visiblebits, num))
2553 // entity just spawned in, don't let it completely hog priority
2554 // because of being ancient on the first frame
2555 d->updateframenum[num] = framenum;
2556 // initial priority is a bit high to make projectiles send on the
2557 // first frame, among other things
2558 d->priorities[num] = max(d->priorities[num], 4);
2560 SETPVSBIT(d->visiblebits, num);
2561 d->deltabits[num] |= EntityState5_DeltaBits(d->states + num, n);
2562 d->priorities[num] = max(d->priorities[num], 1);
2563 d->states[num] = *n;
2564 d->states[num].number = num;
2565 // advance to next entity so the next iteration doesn't immediately remove it
2568 // all remaining entities are dead
2569 for (;num < d->maxedicts;num++)
2571 if (CHECKPVSBIT(d->visiblebits, num))
2573 CLEARPVSBIT(d->visiblebits, num);
2574 d->deltabits[num] = E5_FULLUPDATE;
2575 d->priorities[num] = max(d->priorities[num], 8); // removal is cheap
2576 d->states[num] = defaultstate;
2577 d->states[num].number = num;
2581 // if there isn't at least enough room for an empty svc_entities,
2582 // don't bother trying...
2583 if (buf.cursize + 11 > buf.maxsize)
2586 // build lists of entities by priority level
2587 memset(d->prioritychaincounts, 0, sizeof(d->prioritychaincounts));
2589 for (num = 0;num < d->maxedicts;num++)
2591 if (d->priorities[num])
2593 if (d->deltabits[num])
2595 if (d->priorities[num] < (ENTITYFRAME5_PRIORITYLEVELS - 1))
2596 d->priorities[num] = EntityState5_Priority(d, num);
2598 priority = d->priorities[num];
2599 if (d->prioritychaincounts[priority] < ENTITYFRAME5_MAXSTATES)
2600 d->prioritychains[priority][d->prioritychaincounts[priority]++] = num;
2603 d->priorities[num] = 0;
2607 // add packetlog entry
2608 packetlog = d->packetlog + packetlognumber;
2609 packetlog->packetnumber = framenum;
2610 packetlog->numstates = 0;
2611 // write stat updates
2612 if (sv.protocol != PROTOCOL_QUAKE && sv.protocol != PROTOCOL_QUAKEDP && sv.protocol != PROTOCOL_NEHAHRAMOVIE && sv.protocol != PROTOCOL_NEHAHRABJP && sv.protocol != PROTOCOL_NEHAHRABJP2 && sv.protocol != PROTOCOL_NEHAHRABJP3 && sv.protocol != PROTOCOL_DARKPLACES1 && sv.protocol != PROTOCOL_DARKPLACES2 && sv.protocol != PROTOCOL_DARKPLACES3 && sv.protocol != PROTOCOL_DARKPLACES4 && sv.protocol != PROTOCOL_DARKPLACES5)
2614 for (i = 0;i < MAX_CL_STATS && msg->cursize + 6 + 11 <= maxsize;i++)
2616 if (host_client->statsdeltabits[i>>3] & (1<<(i&7)))
2618 host_client->statsdeltabits[i>>3] &= ~(1<<(i&7));
2619 packetlog->statsdeltabits[i>>3] |= (1<<(i&7));
2620 if (host_client->stats[i] >= 0 && host_client->stats[i] < 256)
2622 MSG_WriteByte(msg, svc_updatestatubyte);
2623 MSG_WriteByte(msg, i);
2624 MSG_WriteByte(msg, host_client->stats[i]);
2629 MSG_WriteByte(msg, svc_updatestat);
2630 MSG_WriteByte(msg, i);
2631 MSG_WriteLong(msg, host_client->stats[i]);
2638 // only send empty svc_entities frame if needed
2639 if(!l && !need_empty)
2642 // write state updates
2643 if (developer_networkentities.integer >= 10)
2644 Con_Printf("send: svc_entities %i\n", framenum);
2645 d->latestframenum = framenum;
2646 MSG_WriteByte(msg, svc_entities);
2647 MSG_WriteLong(msg, framenum);
2648 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)
2649 MSG_WriteLong(msg, movesequence);
2650 for (priority = ENTITYFRAME5_PRIORITYLEVELS - 1;priority >= 0 && packetlog->numstates < ENTITYFRAME5_MAXSTATES;priority--)
2652 for (i = 0;i < d->prioritychaincounts[priority] && packetlog->numstates < ENTITYFRAME5_MAXSTATES;i++)
2654 num = d->prioritychains[priority][i];
2655 n = d->states + num;
2656 if (d->deltabits[num] & E5_FULLUPDATE)
2657 d->deltabits[num] = E5_FULLUPDATE | EntityState5_DeltaBits(&defaultstate, n);
2659 EntityState5_WriteUpdate(num, n, d->deltabits[num], &buf);
2660 // if the entity won't fit, try the next one
2661 if (msg->cursize + buf.cursize + 2 > maxsize)
2663 // write entity to the packet
2664 SZ_Write(msg, buf.data, buf.cursize);
2665 // mark age on entity for prioritization
2666 d->updateframenum[num] = framenum;
2667 // log entity so deltabits can be restored later if lost
2668 packetlog->states[packetlog->numstates].number = num;
2669 packetlog->states[packetlog->numstates].bits = d->deltabits[num];
2670 packetlog->numstates++;
2671 // clear deltabits and priority so it won't be sent again
2672 d->deltabits[num] = 0;
2673 d->priorities[num] = 0;
2676 MSG_WriteShort(msg, 0x8000);
2680 static void QW_TranslateEffects(entity_state_t *s, int qweffects)
2683 s->internaleffects = 0;
2684 if (qweffects & QW_EF_BRIGHTFIELD)
2685 s->effects |= EF_BRIGHTFIELD;
2686 if (qweffects & QW_EF_MUZZLEFLASH)
2687 s->effects |= EF_MUZZLEFLASH;
2688 if (qweffects & QW_EF_FLAG1)
2690 // mimic FTEQW's interpretation of EF_FLAG1 as EF_NODRAW on non-player entities
2691 if (s->number > cl.maxclients)
2692 s->effects |= EF_NODRAW;
2694 s->internaleffects |= INTEF_FLAG1QW;
2696 if (qweffects & QW_EF_FLAG2)
2698 // mimic FTEQW's interpretation of EF_FLAG2 as EF_ADDITIVE on non-player entities
2699 if (s->number > cl.maxclients)
2700 s->effects |= EF_ADDITIVE;
2702 s->internaleffects |= INTEF_FLAG2QW;
2704 if (qweffects & QW_EF_RED)
2706 if (qweffects & QW_EF_BLUE)
2707 s->effects |= EF_RED | EF_BLUE;
2709 s->effects |= EF_RED;
2711 else if (qweffects & QW_EF_BLUE)
2712 s->effects |= EF_BLUE;
2713 else if (qweffects & QW_EF_BRIGHTLIGHT)
2714 s->effects |= EF_BRIGHTLIGHT;
2715 else if (qweffects & QW_EF_DIMLIGHT)
2716 s->effects |= EF_DIMLIGHT;
2719 void EntityStateQW_ReadPlayerUpdate(void)
2721 int slot = MSG_ReadByte();
2722 int enumber = slot + 1;
2728 // look up the entity
2729 entity_t *ent = cl.entities + enumber;
2733 // slide the current state into the previous
2734 ent->state_previous = ent->state_current;
2737 s = &ent->state_current;
2740 s->number = enumber;
2741 s->colormap = enumber;
2742 playerflags = MSG_ReadShort();
2743 MSG_ReadVector(s->origin, cls.protocol);
2744 s->frame = MSG_ReadByte();
2746 VectorClear(viewangles);
2747 VectorClear(velocity);
2749 if (playerflags & QW_PF_MSEC)
2751 // time difference between last update this player sent to the server,
2752 // and last input we sent to the server (this packet is in response to
2753 // our input, so msec is how long ago the last update of this player
2754 // entity occurred, compared to our input being received)
2755 msec = MSG_ReadByte();
2759 if (playerflags & QW_PF_COMMAND)
2761 bits = MSG_ReadByte();
2762 if (bits & QW_CM_ANGLE1)
2763 viewangles[0] = MSG_ReadAngle16i(); // cmd->angles[0]
2764 if (bits & QW_CM_ANGLE2)
2765 viewangles[1] = MSG_ReadAngle16i(); // cmd->angles[1]
2766 if (bits & QW_CM_ANGLE3)
2767 viewangles[2] = MSG_ReadAngle16i(); // cmd->angles[2]
2768 if (bits & QW_CM_FORWARD)
2769 MSG_ReadShort(); // cmd->forwardmove
2770 if (bits & QW_CM_SIDE)
2771 MSG_ReadShort(); // cmd->sidemove
2772 if (bits & QW_CM_UP)
2773 MSG_ReadShort(); // cmd->upmove
2774 if (bits & QW_CM_BUTTONS)
2775 MSG_ReadByte(); // cmd->buttons
2776 if (bits & QW_CM_IMPULSE)
2777 MSG_ReadByte(); // cmd->impulse
2778 MSG_ReadByte(); // cmd->msec
2780 if (playerflags & QW_PF_VELOCITY1)
2781 velocity[0] = MSG_ReadShort();
2782 if (playerflags & QW_PF_VELOCITY2)
2783 velocity[1] = MSG_ReadShort();
2784 if (playerflags & QW_PF_VELOCITY3)
2785 velocity[2] = MSG_ReadShort();
2786 if (playerflags & QW_PF_MODEL)
2787 s->modelindex = MSG_ReadByte();
2789 s->modelindex = cl.qw_modelindex_player;
2790 if (playerflags & QW_PF_SKINNUM)
2791 s->skin = MSG_ReadByte();
2792 if (playerflags & QW_PF_EFFECTS)
2793 QW_TranslateEffects(s, MSG_ReadByte());
2794 if (playerflags & QW_PF_WEAPONFRAME)
2795 weaponframe = MSG_ReadByte();
2799 if (enumber == cl.playerentity)
2801 // if this is an update on our player, update the angles
2802 VectorCopy(cl.viewangles, viewangles);
2805 // calculate the entity angles from the viewangles
2806 s->angles[0] = viewangles[0] * -0.0333;
2807 s->angles[1] = viewangles[1];
2809 s->angles[2] = V_CalcRoll(s->angles, velocity)*4;
2811 // if this is an update on our player, update interpolation state
2812 if (enumber == cl.playerentity)
2814 VectorCopy (cl.mpunchangle[0], cl.mpunchangle[1]);
2815 VectorCopy (cl.mpunchvector[0], cl.mpunchvector[1]);
2816 VectorCopy (cl.mvelocity[0], cl.mvelocity[1]);
2817 cl.mviewzoom[1] = cl.mviewzoom[0];
2820 cl.mpunchangle[0][0] = 0;
2821 cl.mpunchangle[0][1] = 0;
2822 cl.mpunchangle[0][2] = 0;
2823 cl.mpunchvector[0][0] = 0;
2824 cl.mpunchvector[0][1] = 0;
2825 cl.mpunchvector[0][2] = 0;
2826 cl.mvelocity[0][0] = 0;
2827 cl.mvelocity[0][1] = 0;
2828 cl.mvelocity[0][2] = 0;
2829 cl.mviewzoom[0] = 1;
2831 VectorCopy(velocity, cl.mvelocity[0]);
2832 cl.stats[STAT_WEAPONFRAME] = weaponframe;
2833 if (playerflags & QW_PF_GIB)
2834 cl.stats[STAT_VIEWHEIGHT] = 8;
2835 else if (playerflags & QW_PF_DEAD)
2836 cl.stats[STAT_VIEWHEIGHT] = -16;
2838 cl.stats[STAT_VIEWHEIGHT] = 22;
2841 // set the cl.entities_active flag
2842 cl.entities_active[enumber] = s->active;
2843 // set the update time
2844 s->time = cl.mtime[0] - msec * 0.001; // qw has no clock
2845 // check if we need to update the lerp stuff
2847 CL_MoveLerpEntityStates(&cl.entities[enumber]);
2850 static void EntityStateQW_ReadEntityUpdate(entity_state_t *s, int bits)
2854 s->number = bits & 511;
2856 if (bits & QW_U_MOREBITS)
2857 bits |= MSG_ReadByte();
2859 // store the QW_U_SOLID bit here?
2861 if (bits & QW_U_MODEL)
2862 s->modelindex = MSG_ReadByte();
2863 if (bits & QW_U_FRAME)
2864 s->frame = MSG_ReadByte();
2865 if (bits & QW_U_COLORMAP)
2866 s->colormap = MSG_ReadByte();
2867 if (bits & QW_U_SKIN)
2868 s->skin = MSG_ReadByte();
2869 if (bits & QW_U_EFFECTS)
2870 QW_TranslateEffects(s, qweffects = MSG_ReadByte());
2871 if (bits & QW_U_ORIGIN1)
2872 s->origin[0] = MSG_ReadCoord13i();
2873 if (bits & QW_U_ANGLE1)
2874 s->angles[0] = MSG_ReadAngle8i();
2875 if (bits & QW_U_ORIGIN2)
2876 s->origin[1] = MSG_ReadCoord13i();
2877 if (bits & QW_U_ANGLE2)
2878 s->angles[1] = MSG_ReadAngle8i();
2879 if (bits & QW_U_ORIGIN3)
2880 s->origin[2] = MSG_ReadCoord13i();
2881 if (bits & QW_U_ANGLE3)
2882 s->angles[2] = MSG_ReadAngle8i();
2884 if (developer_networkentities.integer >= 2)
2886 Con_Printf("ReadFields e%i", s->number);
2887 if (bits & QW_U_MODEL)
2888 Con_Printf(" U_MODEL %i", s->modelindex);
2889 if (bits & QW_U_FRAME)
2890 Con_Printf(" U_FRAME %i", s->frame);
2891 if (bits & QW_U_COLORMAP)
2892 Con_Printf(" U_COLORMAP %i", s->colormap);
2893 if (bits & QW_U_SKIN)
2894 Con_Printf(" U_SKIN %i", s->skin);
2895 if (bits & QW_U_EFFECTS)
2896 Con_Printf(" U_EFFECTS %i", qweffects);
2897 if (bits & QW_U_ORIGIN1)
2898 Con_Printf(" U_ORIGIN1 %f", s->origin[0]);
2899 if (bits & QW_U_ANGLE1)
2900 Con_Printf(" U_ANGLE1 %f", s->angles[0]);
2901 if (bits & QW_U_ORIGIN2)
2902 Con_Printf(" U_ORIGIN2 %f", s->origin[1]);
2903 if (bits & QW_U_ANGLE2)
2904 Con_Printf(" U_ANGLE2 %f", s->angles[1]);
2905 if (bits & QW_U_ORIGIN3)
2906 Con_Printf(" U_ORIGIN3 %f", s->origin[2]);
2907 if (bits & QW_U_ANGLE3)
2908 Con_Printf(" U_ANGLE3 %f", s->angles[2]);
2909 if (bits & QW_U_SOLID)
2910 Con_Printf(" U_SOLID");
2915 entityframeqw_database_t *EntityFrameQW_AllocDatabase(mempool_t *pool)
2917 entityframeqw_database_t *d;
2918 d = (entityframeqw_database_t *)Mem_Alloc(pool, sizeof(*d));
2922 void EntityFrameQW_FreeDatabase(entityframeqw_database_t *d)
2927 void EntityFrameQW_CL_ReadFrame(qboolean delta)
2929 qboolean invalid = false;
2930 int number, oldsnapindex, newsnapindex, oldindex, newindex, oldnum, newnum;
2932 entityframeqw_database_t *d;
2933 entityframeqw_snapshot_t *oldsnap, *newsnap;
2935 if (!cl.entitydatabaseqw)
2936 cl.entitydatabaseqw = EntityFrameQW_AllocDatabase(cls.levelmempool);
2937 d = cl.entitydatabaseqw;
2939 // there is no cls.netcon in demos, so this reading code can't access
2940 // cls.netcon-> at all... so cls.qw_incoming_sequence and
2941 // cls.qw_outgoing_sequence are updated every time the corresponding
2942 // cls.netcon->qw. variables are updated
2943 // read the number of this frame to echo back in next input packet
2944 cl.qw_validsequence = cls.qw_incoming_sequence;
2945 newsnapindex = cl.qw_validsequence & QW_UPDATE_MASK;
2946 newsnap = d->snapshot + newsnapindex;
2947 memset(newsnap, 0, sizeof(*newsnap));
2952 number = MSG_ReadByte();
2953 oldsnapindex = cl.qw_deltasequence[newsnapindex];
2954 if ((number & QW_UPDATE_MASK) != (oldsnapindex & QW_UPDATE_MASK))
2955 Con_DPrintf("WARNING: from mismatch\n");
2956 if (oldsnapindex != -1)
2958 if (cls.qw_outgoing_sequence - oldsnapindex >= QW_UPDATE_BACKUP-1)
2960 Con_DPrintf("delta update too old\n");
2961 newsnap->invalid = invalid = true; // too old
2964 oldsnap = d->snapshot + (oldsnapindex & QW_UPDATE_MASK);
2970 // if we can't decode this frame properly, report that to the server
2972 cl.qw_validsequence = 0;
2974 // read entity numbers until we find a 0x0000
2975 // (which would be an empty update on world entity, but is actually a terminator)
2976 newsnap->num_entities = 0;
2980 int word = (unsigned short)MSG_ReadShort();
2982 return; // just return, the main parser will print an error
2983 newnum = word == 0 ? 512 : (word & 511);
2984 oldnum = delta ? (oldindex >= oldsnap->num_entities ? 9999 : oldsnap->entities[oldindex].number) : 9999;
2986 // copy unmodified oldsnap entities
2987 while (newnum > oldnum) // delta only
2989 if (developer_networkentities.integer >= 2)
2990 Con_Printf("copy %i\n", oldnum);
2991 // copy one of the old entities
2992 if (newsnap->num_entities >= QW_MAX_PACKET_ENTITIES)
2993 Host_Error("EntityFrameQW_CL_ReadFrame: newsnap->num_entities == MAX_PACKETENTITIES");
2994 newsnap->entities[newsnap->num_entities] = oldsnap->entities[oldindex++];
2995 newsnap->num_entities++;
2996 oldnum = oldindex >= oldsnap->num_entities ? 9999 : oldsnap->entities[oldindex].number;
3002 if (developer_networkentities.integer >= 2)
3004 if (word & QW_U_REMOVE)
3005 Con_Printf("remove %i\n", newnum);
3006 else if (newnum == oldnum)
3007 Con_Printf("delta %i\n", newnum);
3009 Con_Printf("baseline %i\n", newnum);
3012 if (word & QW_U_REMOVE)
3014 if (newnum != oldnum && !delta && !invalid)
3016 cl.qw_validsequence = 0;
3017 Con_Printf("WARNING: U_REMOVE %i on full update\n", newnum);
3022 if (newsnap->num_entities >= QW_MAX_PACKET_ENTITIES)
3023 Host_Error("EntityFrameQW_CL_ReadFrame: newsnap->num_entities == MAX_PACKETENTITIES");
3024 newsnap->entities[newsnap->num_entities] = (newnum == oldnum) ? oldsnap->entities[oldindex] : cl.entities[newnum].state_baseline;
3025 EntityStateQW_ReadEntityUpdate(newsnap->entities + newsnap->num_entities, word);
3026 newsnap->num_entities++;
3029 if (newnum == oldnum)
3033 // expand cl.num_entities to include every entity we've seen this game
3034 newnum = newsnap->num_entities ? newsnap->entities[newsnap->num_entities - 1].number : 1;
3035 if (cl.num_entities <= newnum)
3037 cl.num_entities = newnum + 1;
3038 if (cl.max_entities < newnum + 1)
3039 CL_ExpandEntities(newnum);
3042 // now update the non-player entities from the snapshot states
3043 number = cl.maxclients + 1;
3044 for (newindex = 0;;newindex++)
3046 newnum = newindex >= newsnap->num_entities ? cl.num_entities : newsnap->entities[newindex].number;
3047 // kill any missing entities
3048 for (;number < newnum;number++)
3050 if (cl.entities_active[number])
3052 cl.entities_active[number] = false;
3053 cl.entities[number].state_current.active = false;
3056 if (number >= cl.num_entities)
3058 // update the entity
3059 ent = &cl.entities[number];
3060 ent->state_previous = ent->state_current;
3061 ent->state_current = newsnap->entities[newindex];
3062 ent->state_current.time = cl.mtime[0];
3063 CL_MoveLerpEntityStates(ent);
3064 // the entity lives again...
3065 cl.entities_active[number] = true;