4 #define ENTITYSIZEPROFILING_START(msg, num) \
5 int entityprofiling_startsize = msg->cursize
7 #define ENTITYSIZEPROFILING_END(msg, num) \
8 if(developer_networkentities.integer >= 2) \
10 prvm_edict_t *ed = prog->edicts + num; \
11 const char *cname = "(no classname)"; \
12 if(prog->fieldoffsets.classname >= 0) \
14 string_t handle = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.classname)->string; \
16 cname = PRVM_GetString(handle); \
18 Con_Printf("sent entity update of size %d for a %s\n", (msg->cursize - entityprofiling_startsize), cname); \
21 // this is 88 bytes (must match entity_state_t in protocol.h)
22 entity_state_t defaultstate =
24 // ! means this is not sent to client
25 0,//double time; // ! time this state was built (used on client for interpolation)
26 {0,0,0},//float netcenter[3]; // ! for network prioritization, this is the center of the bounding box (which may differ from the origin)
27 {0,0,0},//float origin[3];
28 {0,0,0},//float angles[3];
30 0,//unsigned int customizeentityforclient; // !
31 0,//unsigned short number; // entity number this state is for
32 0,//unsigned short modelindex;
33 0,//unsigned short frame;
34 0,//unsigned short tagentity;
35 0,//unsigned short specialvisibilityradius; // ! larger if it has effects/light
36 0,//unsigned short viewmodelforclient; // !
37 0,//unsigned short exteriormodelforclient; // ! not shown if first person viewing from this entity, shown in all other cases
38 0,//unsigned short nodrawtoclient; // !
39 0,//unsigned short drawonlytoclient; // !
40 {0,0,0,0},//unsigned short light[4]; // color*256 (0.00 to 255.996), and radius*1
41 0,//unsigned char active; // true if a valid state
42 0,//unsigned char lightstyle;
43 0,//unsigned char lightpflags;
44 0,//unsigned char colormap;
45 0,//unsigned char skin; // also chooses cubemap for rtlights if lightpflags & LIGHTPFLAGS_FULLDYNAMIC
46 255,//unsigned char alpha;
47 16,//unsigned char scale;
48 0,//unsigned char glowsize;
49 254,//unsigned char glowcolor;
50 0,//unsigned char flags;
51 0,//unsigned char internaleffects; // INTEF_FLAG1QW and so on
52 0,//unsigned char tagindex;
53 {32, 32, 32},//unsigned char colormod[3];
54 // padding to a multiple of 8 bytes (to align the double time)
55 {0,0,0,0,0}//unsigned char unused[5]; // !
58 // LordHavoc: I own protocol ranges 96, 97, 3500-3599
60 struct protocolversioninfo_s
65 protocolversioninfo[] =
76 {250, "NEHAHRAMOVIE"},
79 {10000, "NEHAHRABJP"},
80 {10001, "NEHAHRABJP2"},
81 {10002, "NEHAHRABJP3"},
85 protocolversion_t Protocol_EnumForName(const char *s)
88 for (i = 1;protocolversioninfo[i].name;i++)
89 if (!strcasecmp(s, protocolversioninfo[i].name))
90 return (protocolversion_t)i;
91 return PROTOCOL_UNKNOWN;
94 const char *Protocol_NameForEnum(protocolversion_t p)
96 return protocolversioninfo[p].name;
99 protocolversion_t Protocol_EnumForNumber(int n)
102 for (i = 1;protocolversioninfo[i].name;i++)
103 if (protocolversioninfo[i].number == n)
104 return (protocolversion_t)i;
105 return PROTOCOL_UNKNOWN;
108 int Protocol_NumberForEnum(protocolversion_t p)
110 return protocolversioninfo[p].number;
113 void Protocol_Names(char *buffer, size_t buffersize)
119 for (i = 1;protocolversioninfo[i].name;i++)
122 strlcat(buffer, " ", buffersize);
123 strlcat(buffer, protocolversioninfo[i].name, buffersize);
127 void EntityFrameQuake_ReadEntity(int bits)
133 if (bits & U_MOREBITS)
134 bits |= (MSG_ReadByte()<<8);
135 if ((bits & U_EXTEND1) && cls.protocol != PROTOCOL_NEHAHRAMOVIE)
137 bits |= MSG_ReadByte() << 16;
138 if (bits & U_EXTEND2)
139 bits |= MSG_ReadByte() << 24;
142 if (bits & U_LONGENTITY)
143 num = (unsigned short) MSG_ReadShort ();
145 num = MSG_ReadByte ();
147 if (num >= MAX_EDICTS)
148 Host_Error("EntityFrameQuake_ReadEntity: entity number (%i) >= MAX_EDICTS (%i)", num, MAX_EDICTS);
150 Host_Error("EntityFrameQuake_ReadEntity: invalid entity number (%i)", num);
152 if (cl.num_entities <= num)
154 cl.num_entities = num + 1;
155 if (num >= cl.max_entities)
156 CL_ExpandEntities(num);
159 ent = cl.entities + num;
161 // note: this inherits the 'active' state of the baseline chosen
162 // (state_baseline is always active, state_current may not be active if
163 // the entity was missing in the last frame)
165 s = ent->state_current;
168 s = ent->state_baseline;
172 cl.isquakeentity[num] = true;
173 if (cl.lastquakeentity < num)
174 cl.lastquakeentity = num;
176 s.time = cl.mtime[0];
180 if (cls.protocol == PROTOCOL_NEHAHRABJP || cls.protocol == PROTOCOL_NEHAHRABJP2 || cls.protocol == PROTOCOL_NEHAHRABJP3)
181 s.modelindex = (unsigned short) MSG_ReadShort();
183 s.modelindex = (s.modelindex & 0xFF00) | MSG_ReadByte();
185 if (bits & U_FRAME) s.frame = (s.frame & 0xFF00) | MSG_ReadByte();
186 if (bits & U_COLORMAP) s.colormap = MSG_ReadByte();
187 if (bits & U_SKIN) s.skin = MSG_ReadByte();
188 if (bits & U_EFFECTS) s.effects = (s.effects & 0xFF00) | MSG_ReadByte();
189 if (bits & U_ORIGIN1) s.origin[0] = MSG_ReadCoord(cls.protocol);
190 if (bits & U_ANGLE1) s.angles[0] = MSG_ReadAngle(cls.protocol);
191 if (bits & U_ORIGIN2) s.origin[1] = MSG_ReadCoord(cls.protocol);
192 if (bits & U_ANGLE2) s.angles[1] = MSG_ReadAngle(cls.protocol);
193 if (bits & U_ORIGIN3) s.origin[2] = MSG_ReadCoord(cls.protocol);
194 if (bits & U_ANGLE3) s.angles[2] = MSG_ReadAngle(cls.protocol);
195 if (bits & U_STEP) s.flags |= RENDER_STEP;
196 if (bits & U_ALPHA) s.alpha = MSG_ReadByte();
197 if (bits & U_SCALE) s.scale = MSG_ReadByte();
198 if (bits & U_EFFECTS2) s.effects = (s.effects & 0x00FF) | (MSG_ReadByte() << 8);
199 if (bits & U_GLOWSIZE) s.glowsize = MSG_ReadByte();
200 if (bits & U_GLOWCOLOR) s.glowcolor = MSG_ReadByte();
201 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));}
202 if (bits & U_GLOWTRAIL) s.flags |= RENDER_GLOWTRAIL;
203 if (bits & U_FRAME2) s.frame = (s.frame & 0x00FF) | (MSG_ReadByte() << 8);
204 if (bits & U_MODEL2) s.modelindex = (s.modelindex & 0x00FF) | (MSG_ReadByte() << 8);
205 if (bits & U_VIEWMODEL) s.flags |= RENDER_VIEWMODEL;
206 if (bits & U_EXTERIORMODEL) s.flags |= RENDER_EXTERIORMODEL;
208 // LordHavoc: to allow playback of the Nehahra movie
209 if (cls.protocol == PROTOCOL_NEHAHRAMOVIE && (bits & U_EXTEND1))
211 // LordHavoc: evil format
212 int i = (int)MSG_ReadFloat();
213 int j = (int)(MSG_ReadFloat() * 255.0f);
216 i = (int)MSG_ReadFloat();
218 s.effects |= EF_FULLBRIGHT;
222 else if (j == 0 || j >= 255)
228 ent->state_previous = ent->state_current;
229 ent->state_current = s;
230 if (ent->state_current.active)
232 CL_MoveLerpEntityStates(ent);
233 cl.entities_active[ent->state_current.number] = true;
237 Host_Error("EntityFrameQuake_ReadEntity: read error");
240 void EntityFrameQuake_ISeeDeadEntities(void)
243 if (cl.lastquakeentity == 0)
245 lastentity = cl.lastquakeentity;
246 cl.lastquakeentity = 0;
247 for (num = 0;num <= lastentity;num++)
249 if (cl.isquakeentity[num])
251 if (cl.entities_active[num] && cl.entities[num].state_current.time == cl.mtime[0])
253 cl.isquakeentity[num] = true;
254 cl.lastquakeentity = num;
258 cl.isquakeentity[num] = false;
259 cl.entities_active[num] = false;
260 cl.entities[num].state_current = defaultstate;
261 cl.entities[num].state_current.number = num;
267 // FIXME FIXME FIXME: at this time the CSQC entity writing does not store
268 // packet logs and thus if an update is lost it is never repeated, this makes
269 // csqc entities useless at the moment.
271 //[515]: we use only one array per-client for SendEntity feature
272 void EntityFrameCSQC_WriteFrame (sizebuf_t *msg, int maxsize, int numstates, const entity_state_t *states)
274 int num, number, end, sendflags;
275 qboolean sectionstarted = false;
276 const entity_state_t *n;
279 client_t *client = svs.clients + sv.writeentitiestoclient_clientnumber;
281 // if this server progs is not CSQC-aware, return early
282 if(prog->fieldoffsets.SendEntity < 0 || prog->fieldoffsets.Version < 0)
285 // make sure there is enough room to store the svc_csqcentities byte,
286 // the terminator (0x0000) and at least one entity update
287 if (msg->cursize + 32 >= maxsize)
290 if (client->csqcnumedicts < prog->num_edicts)
291 client->csqcnumedicts = prog->num_edicts;
294 for (num = 0, n = states;num < numstates;num++, n++)
297 for (;number < end;number++)
299 if (client->csqcentityscope[number])
301 client->csqcentityscope[number] = 1;
302 client->csqcentitysendflags[number] = 0xFFFFFF;
305 ed = prog->edicts + number;
306 val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.SendEntity);
308 client->csqcentityscope[number] = 2;
309 else if (client->csqcentityscope[number])
311 client->csqcentityscope[number] = 1;
312 client->csqcentitysendflags[number] = 0xFFFFFF;
316 end = client->csqcnumedicts;
317 for (;number < end;number++)
319 if (client->csqcentityscope[number])
321 client->csqcentityscope[number] = 1;
322 client->csqcentitysendflags[number] = 0xFFFFFF;
327 // mark all scope entities as remove
328 for (number = 1;number < client->csqcnumedicts;number++)
329 if (client->csqcentityscope[number])
330 client->csqcentityscope[number] = 1;
331 // keep visible entities
332 for (i = 0, n = states;i < numstates;i++, n++)
335 ed = prog->edicts + number;
336 val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.SendEntity);
338 client->csqcentityscope[number] = 2;
342 // now try to emit the entity updates
343 // (FIXME: prioritize by distance?)
344 end = client->csqcnumedicts;
345 for (number = 1;number < end;number++)
347 if (!client->csqcentityscope[number])
349 sendflags = client->csqcentitysendflags[number];
352 ed = prog->edicts + number;
353 // entity scope is either update (2) or remove (1)
354 if (client->csqcentityscope[number] == 1)
356 // write a remove message
357 // first write the message identifier if needed
361 MSG_WriteByte(msg, svc_csqcentities);
363 // write the remove message
365 ENTITYSIZEPROFILING_START(msg, number);
366 MSG_WriteShort(msg, (unsigned short)number | 0x8000);
367 client->csqcentityscope[number] = 0;
368 client->csqcentitysendflags[number] = 0xFFFFFF; // resend completely if it becomes active again
369 ENTITYSIZEPROFILING_END(msg, number);
371 if (msg->cursize + 17 >= maxsize)
377 // save the cursize value in case we overflow and have to rollback
378 int oldcursize = msg->cursize;
379 client->csqcentityscope[number] = 1;
380 val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.SendEntity);
384 MSG_WriteByte(msg, svc_csqcentities);
386 ENTITYSIZEPROFILING_START(msg, number);
387 MSG_WriteShort(msg, number);
388 msg->allowoverflow = true;
389 PRVM_G_INT(OFS_PARM0) = sv.writeentitiestoclient_cliententitynumber;
390 PRVM_G_FLOAT(OFS_PARM1) = sendflags;
391 prog->globals.server->self = number;
392 PRVM_ExecuteProgram(val->function, "Null SendEntity\n");
393 msg->allowoverflow = false;
394 if(PRVM_G_FLOAT(OFS_RETURN) && msg->cursize + 2 <= maxsize)
396 // an update has been successfully written
397 client->csqcentitysendflags[number] = 0;
398 // and take note that we have begun the svc_csqcentities
399 // section of the packet
401 if (msg->cursize + 17 >= maxsize)
403 ENTITYSIZEPROFILING_END(msg, number);
408 // self.SendEntity returned false (or does not exist) or the
409 // update was too big for this packet - rollback the buffer to its
410 // state before the writes occurred, we'll try again next frame
411 msg->cursize = oldcursize;
412 msg->overflowed = false;
417 // write index 0 to end the update (0 is never used by real entities)
418 MSG_WriteShort(msg, 0);
422 void Protocol_UpdateClientStats(const int *stats)
425 // update the stats array and set deltabits for any changed stats
426 for (i = 0;i < MAX_CL_STATS;i++)
428 if (host_client->stats[i] != stats[i])
430 host_client->statsdeltabits[i >> 3] |= 1 << (i & 7);
431 host_client->stats[i] = stats[i];
436 // only a few stats are within the 32 stat limit of Quake, and most of them
437 // are sent every frame in svc_clientdata messages, so we only send the
438 // remaining ones here
439 static const int sendquakestats[] =
441 // quake did not send these secrets/monsters stats in this way, but doing so
442 // allows a mod to increase STAT_TOTALMONSTERS during the game, and ensures
443 // that STAT_SECRETS and STAT_MONSTERS are always correct (even if a client
444 // didn't receive an svc_foundsecret or svc_killedmonster), which may be most
445 // valuable if randomly seeking around in a demo
446 STAT_TOTALSECRETS, // never changes during game
447 STAT_TOTALMONSTERS, // changes in some mods
448 STAT_SECRETS, // this makes svc_foundsecret unnecessary
449 STAT_MONSTERS, // this makes svc_killedmonster unnecessary
450 STAT_VIEWHEIGHT, // sent just for FTEQW clients
451 STAT_VIEWZOOM, // this rarely changes
455 void Protocol_WriteStatsReliable(void)
458 if (!host_client->netconnection)
460 // detect changes in stats and write reliable messages
461 // this only deals with 32 stats because the older protocols which use
462 // this function can only cope with 32 stats,
463 // they also do not support svc_updatestatubyte which was introduced in
464 // DP6 protocol (except for QW)
465 for (j = 0;sendquakestats[j] >= 0;j++)
467 i = sendquakestats[j];
468 // check if this bit is set
469 if (host_client->statsdeltabits[i >> 3] & (1 << (i & 7)))
471 host_client->statsdeltabits[i >> 3] -= (1 << (i & 7));
472 // send the stat as a byte if possible
473 if (sv.protocol == PROTOCOL_QUAKEWORLD)
475 if (host_client->stats[i] >= 0 && host_client->stats[i] < 256)
477 MSG_WriteByte(&host_client->netconnection->message, qw_svc_updatestat);
478 MSG_WriteByte(&host_client->netconnection->message, i);
479 MSG_WriteByte(&host_client->netconnection->message, host_client->stats[i]);
483 MSG_WriteByte(&host_client->netconnection->message, qw_svc_updatestatlong);
484 MSG_WriteByte(&host_client->netconnection->message, i);
485 MSG_WriteLong(&host_client->netconnection->message, host_client->stats[i]);
490 // this could make use of svc_updatestatubyte in DP6 and later
491 // protocols but those protocols do not use this function
492 MSG_WriteByte(&host_client->netconnection->message, svc_updatestat);
493 MSG_WriteByte(&host_client->netconnection->message, i);
494 MSG_WriteLong(&host_client->netconnection->message, host_client->stats[i]);
501 void EntityFrameQuake_WriteFrame(sizebuf_t *msg, int maxsize, int numstates, const entity_state_t *states)
503 const entity_state_t *s;
504 entity_state_t baseline;
507 unsigned char data[128];
510 // prepare the buffer
511 memset(&buf, 0, sizeof(buf));
513 buf.maxsize = sizeof(data);
515 for (i = 0, s = states;i < numstates;i++, s++)
517 ENTITYSIZEPROFILING_START(msg, s->number);
518 val = PRVM_EDICTFIELDVALUE((&prog->edicts[s->number]), prog->fieldoffsets.SendEntity);
519 if(val && val->function)
522 // prepare the buffer
527 if (s->number >= 256)
528 bits |= U_LONGENTITY;
529 if (s->flags & RENDER_STEP)
531 if (s->flags & RENDER_VIEWMODEL)
533 if (s->flags & RENDER_GLOWTRAIL)
535 if (s->flags & RENDER_EXTERIORMODEL)
536 bits |= U_EXTERIORMODEL;
538 // LordHavoc: old stuff, but rewritten to have more exact tolerances
539 baseline = prog->edicts[s->number].priv.server->baseline;
540 if (baseline.origin[0] != s->origin[0])
542 if (baseline.origin[1] != s->origin[1])
544 if (baseline.origin[2] != s->origin[2])
546 if (baseline.angles[0] != s->angles[0])
548 if (baseline.angles[1] != s->angles[1])
550 if (baseline.angles[2] != s->angles[2])
552 if (baseline.colormap != s->colormap)
554 if (baseline.skin != s->skin)
556 if (baseline.frame != s->frame)
559 if (s->frame & 0xFF00)
562 if (baseline.effects != s->effects)
565 if (s->effects & 0xFF00)
568 if (baseline.modelindex != s->modelindex)
571 if ((s->modelindex & 0xFF00) && sv.protocol != PROTOCOL_NEHAHRABJP && sv.protocol != PROTOCOL_NEHAHRABJP2 && sv.protocol != PROTOCOL_NEHAHRABJP3)
574 if (baseline.alpha != s->alpha)
576 if (baseline.scale != s->scale)
578 if (baseline.glowsize != s->glowsize)
580 if (baseline.glowcolor != s->glowcolor)
582 if (!VectorCompare(baseline.colormod, s->colormod))
585 // if extensions are disabled, clear the relevant update flags
586 if (sv.protocol == PROTOCOL_QUAKE || sv.protocol == PROTOCOL_NEHAHRAMOVIE)
588 if (sv.protocol == PROTOCOL_NEHAHRAMOVIE)
589 if (s->alpha != 255 || s->effects & EF_FULLBRIGHT)
593 if (bits >= 16777216)
601 MSG_WriteByte (&buf, bits);
602 if (bits & U_MOREBITS) MSG_WriteByte(&buf, bits>>8);
603 if (sv.protocol != PROTOCOL_NEHAHRAMOVIE)
605 if (bits & U_EXTEND1) MSG_WriteByte(&buf, bits>>16);
606 if (bits & U_EXTEND2) MSG_WriteByte(&buf, bits>>24);
608 if (bits & U_LONGENTITY) MSG_WriteShort(&buf, s->number);
609 else MSG_WriteByte(&buf, s->number);
613 if (sv.protocol == PROTOCOL_NEHAHRABJP || sv.protocol == PROTOCOL_NEHAHRABJP2 || sv.protocol == PROTOCOL_NEHAHRABJP3)
614 MSG_WriteShort(&buf, s->modelindex);
616 MSG_WriteByte(&buf, s->modelindex);
618 if (bits & U_FRAME) MSG_WriteByte(&buf, s->frame);
619 if (bits & U_COLORMAP) MSG_WriteByte(&buf, s->colormap);
620 if (bits & U_SKIN) MSG_WriteByte(&buf, s->skin);
621 if (bits & U_EFFECTS) MSG_WriteByte(&buf, s->effects);
622 if (bits & U_ORIGIN1) MSG_WriteCoord(&buf, s->origin[0], sv.protocol);
623 if (bits & U_ANGLE1) MSG_WriteAngle(&buf, s->angles[0], sv.protocol);
624 if (bits & U_ORIGIN2) MSG_WriteCoord(&buf, s->origin[1], sv.protocol);
625 if (bits & U_ANGLE2) MSG_WriteAngle(&buf, s->angles[1], sv.protocol);
626 if (bits & U_ORIGIN3) MSG_WriteCoord(&buf, s->origin[2], sv.protocol);
627 if (bits & U_ANGLE3) MSG_WriteAngle(&buf, s->angles[2], sv.protocol);
628 if (bits & U_ALPHA) MSG_WriteByte(&buf, s->alpha);
629 if (bits & U_SCALE) MSG_WriteByte(&buf, s->scale);
630 if (bits & U_EFFECTS2) MSG_WriteByte(&buf, s->effects >> 8);
631 if (bits & U_GLOWSIZE) MSG_WriteByte(&buf, s->glowsize);
632 if (bits & U_GLOWCOLOR) MSG_WriteByte(&buf, s->glowcolor);
633 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);}
634 if (bits & U_FRAME2) MSG_WriteByte(&buf, s->frame >> 8);
635 if (bits & U_MODEL2) MSG_WriteByte(&buf, s->modelindex >> 8);
637 // the nasty protocol
638 if ((bits & U_EXTEND1) && sv.protocol == PROTOCOL_NEHAHRAMOVIE)
640 if (s->effects & EF_FULLBRIGHT)
642 MSG_WriteFloat(&buf, 2); // QSG protocol version
643 MSG_WriteFloat(&buf, s->alpha <= 0 ? 0 : (s->alpha >= 255 ? 1 : s->alpha * (1.0f / 255.0f))); // alpha
644 MSG_WriteFloat(&buf, 1); // fullbright
648 MSG_WriteFloat(&buf, 1); // QSG protocol version
649 MSG_WriteFloat(&buf, s->alpha <= 0 ? 0 : (s->alpha >= 255 ? 1 : s->alpha * (1.0f / 255.0f))); // alpha
653 // if the commit is full, we're done this frame
654 if (msg->cursize + buf.cursize > maxsize)
656 // next frame we will continue where we left off
659 // write the message to the packet
660 SZ_Write(msg, buf.data, buf.cursize);
661 ENTITYSIZEPROFILING_END(msg, s->number);
665 int EntityState_DeltaBits(const entity_state_t *o, const entity_state_t *n)
668 // if o is not active, delta from default
672 if (fabs(n->origin[0] - o->origin[0]) > (1.0f / 256.0f))
674 if (fabs(n->origin[1] - o->origin[1]) > (1.0f / 256.0f))
676 if (fabs(n->origin[2] - o->origin[2]) > (1.0f / 256.0f))
678 if ((unsigned char) (n->angles[0] * (256.0f / 360.0f)) != (unsigned char) (o->angles[0] * (256.0f / 360.0f)))
680 if ((unsigned char) (n->angles[1] * (256.0f / 360.0f)) != (unsigned char) (o->angles[1] * (256.0f / 360.0f)))
682 if ((unsigned char) (n->angles[2] * (256.0f / 360.0f)) != (unsigned char) (o->angles[2] * (256.0f / 360.0f)))
684 if ((n->modelindex ^ o->modelindex) & 0x00FF)
686 if ((n->modelindex ^ o->modelindex) & 0xFF00)
688 if ((n->frame ^ o->frame) & 0x00FF)
690 if ((n->frame ^ o->frame) & 0xFF00)
692 if ((n->effects ^ o->effects) & 0x00FF)
694 if ((n->effects ^ o->effects) & 0xFF00)
696 if (n->colormap != o->colormap)
698 if (n->skin != o->skin)
700 if (n->alpha != o->alpha)
702 if (n->scale != o->scale)
704 if (n->glowsize != o->glowsize)
706 if (n->glowcolor != o->glowcolor)
708 if (n->flags != o->flags)
710 if (n->tagindex != o->tagindex || n->tagentity != o->tagentity)
711 bits |= E_TAGATTACHMENT;
712 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])
714 if (n->lightstyle != o->lightstyle)
715 bits |= E_LIGHTSTYLE;
716 if (n->lightpflags != o->lightpflags)
717 bits |= E_LIGHTPFLAGS;
721 if (bits & 0xFF000000)
723 if (bits & 0x00FF0000)
725 if (bits & 0x0000FF00)
731 void EntityState_WriteExtendBits(sizebuf_t *msg, unsigned int bits)
733 MSG_WriteByte(msg, bits & 0xFF);
734 if (bits & 0x00000080)
736 MSG_WriteByte(msg, (bits >> 8) & 0xFF);
737 if (bits & 0x00008000)
739 MSG_WriteByte(msg, (bits >> 16) & 0xFF);
740 if (bits & 0x00800000)
741 MSG_WriteByte(msg, (bits >> 24) & 0xFF);
746 void EntityState_WriteFields(const entity_state_t *ent, sizebuf_t *msg, unsigned int bits)
748 if (sv.protocol == PROTOCOL_DARKPLACES2)
750 if (bits & E_ORIGIN1)
751 MSG_WriteCoord16i(msg, ent->origin[0]);
752 if (bits & E_ORIGIN2)
753 MSG_WriteCoord16i(msg, ent->origin[1]);
754 if (bits & E_ORIGIN3)
755 MSG_WriteCoord16i(msg, ent->origin[2]);
759 // LordHavoc: have to write flags first, as they can modify protocol
761 MSG_WriteByte(msg, ent->flags);
762 if (ent->flags & RENDER_LOWPRECISION)
764 if (bits & E_ORIGIN1)
765 MSG_WriteCoord16i(msg, ent->origin[0]);
766 if (bits & E_ORIGIN2)
767 MSG_WriteCoord16i(msg, ent->origin[1]);
768 if (bits & E_ORIGIN3)
769 MSG_WriteCoord16i(msg, ent->origin[2]);
773 if (bits & E_ORIGIN1)
774 MSG_WriteCoord32f(msg, ent->origin[0]);
775 if (bits & E_ORIGIN2)
776 MSG_WriteCoord32f(msg, ent->origin[1]);
777 if (bits & E_ORIGIN3)
778 MSG_WriteCoord32f(msg, ent->origin[2]);
781 if ((sv.protocol == PROTOCOL_DARKPLACES1 || sv.protocol == PROTOCOL_DARKPLACES2 || sv.protocol == PROTOCOL_DARKPLACES3 || sv.protocol == PROTOCOL_DARKPLACES4) && (ent->flags & RENDER_LOWPRECISION))
784 MSG_WriteAngle8i(msg, ent->angles[0]);
786 MSG_WriteAngle8i(msg, ent->angles[1]);
788 MSG_WriteAngle8i(msg, ent->angles[2]);
793 MSG_WriteAngle16i(msg, ent->angles[0]);
795 MSG_WriteAngle16i(msg, ent->angles[1]);
797 MSG_WriteAngle16i(msg, ent->angles[2]);
800 MSG_WriteByte(msg, ent->modelindex & 0xFF);
802 MSG_WriteByte(msg, (ent->modelindex >> 8) & 0xFF);
804 MSG_WriteByte(msg, ent->frame & 0xFF);
806 MSG_WriteByte(msg, (ent->frame >> 8) & 0xFF);
807 if (bits & E_EFFECTS1)
808 MSG_WriteByte(msg, ent->effects & 0xFF);
809 if (bits & E_EFFECTS2)
810 MSG_WriteByte(msg, (ent->effects >> 8) & 0xFF);
811 if (bits & E_COLORMAP)
812 MSG_WriteByte(msg, ent->colormap);
814 MSG_WriteByte(msg, ent->skin);
816 MSG_WriteByte(msg, ent->alpha);
818 MSG_WriteByte(msg, ent->scale);
819 if (bits & E_GLOWSIZE)
820 MSG_WriteByte(msg, ent->glowsize);
821 if (bits & E_GLOWCOLOR)
822 MSG_WriteByte(msg, ent->glowcolor);
823 if (sv.protocol == PROTOCOL_DARKPLACES2)
825 MSG_WriteByte(msg, ent->flags);
826 if (bits & E_TAGATTACHMENT)
828 MSG_WriteShort(msg, ent->tagentity);
829 MSG_WriteByte(msg, ent->tagindex);
833 MSG_WriteShort(msg, ent->light[0]);
834 MSG_WriteShort(msg, ent->light[1]);
835 MSG_WriteShort(msg, ent->light[2]);
836 MSG_WriteShort(msg, ent->light[3]);
838 if (bits & E_LIGHTSTYLE)
839 MSG_WriteByte(msg, ent->lightstyle);
840 if (bits & E_LIGHTPFLAGS)
841 MSG_WriteByte(msg, ent->lightpflags);
844 void EntityState_WriteUpdate(const entity_state_t *ent, sizebuf_t *msg, const entity_state_t *delta)
847 ENTITYSIZEPROFILING_START(msg, ent->number);
850 // entity is active, check for changes from the delta
851 if ((bits = EntityState_DeltaBits(delta, ent)))
853 // write the update number, bits, and fields
854 MSG_WriteShort(msg, ent->number);
855 EntityState_WriteExtendBits(msg, bits);
856 EntityState_WriteFields(ent, msg, bits);
861 // entity is inactive, check if the delta was active
864 // write the remove number
865 MSG_WriteShort(msg, ent->number | 0x8000);
868 ENTITYSIZEPROFILING_END(msg, ent->number);
871 int EntityState_ReadExtendBits(void)
874 bits = MSG_ReadByte();
875 if (bits & 0x00000080)
877 bits |= MSG_ReadByte() << 8;
878 if (bits & 0x00008000)
880 bits |= MSG_ReadByte() << 16;
881 if (bits & 0x00800000)
882 bits |= MSG_ReadByte() << 24;
888 void EntityState_ReadFields(entity_state_t *e, unsigned int bits)
890 if (cls.protocol == PROTOCOL_DARKPLACES2)
892 if (bits & E_ORIGIN1)
893 e->origin[0] = MSG_ReadCoord16i();
894 if (bits & E_ORIGIN2)
895 e->origin[1] = MSG_ReadCoord16i();
896 if (bits & E_ORIGIN3)
897 e->origin[2] = MSG_ReadCoord16i();
902 e->flags = MSG_ReadByte();
903 if (e->flags & RENDER_LOWPRECISION)
905 if (bits & E_ORIGIN1)
906 e->origin[0] = MSG_ReadCoord16i();
907 if (bits & E_ORIGIN2)
908 e->origin[1] = MSG_ReadCoord16i();
909 if (bits & E_ORIGIN3)
910 e->origin[2] = MSG_ReadCoord16i();
914 if (bits & E_ORIGIN1)
915 e->origin[0] = MSG_ReadCoord32f();
916 if (bits & E_ORIGIN2)
917 e->origin[1] = MSG_ReadCoord32f();
918 if (bits & E_ORIGIN3)
919 e->origin[2] = MSG_ReadCoord32f();
922 if ((cls.protocol == PROTOCOL_DARKPLACES5 || cls.protocol == PROTOCOL_DARKPLACES6) && !(e->flags & RENDER_LOWPRECISION))
925 e->angles[0] = MSG_ReadAngle16i();
927 e->angles[1] = MSG_ReadAngle16i();
929 e->angles[2] = MSG_ReadAngle16i();
934 e->angles[0] = MSG_ReadAngle8i();
936 e->angles[1] = MSG_ReadAngle8i();
938 e->angles[2] = MSG_ReadAngle8i();
941 e->modelindex = (e->modelindex & 0xFF00) | (unsigned int) MSG_ReadByte();
943 e->modelindex = (e->modelindex & 0x00FF) | ((unsigned int) MSG_ReadByte() << 8);
945 e->frame = (e->frame & 0xFF00) | (unsigned int) MSG_ReadByte();
947 e->frame = (e->frame & 0x00FF) | ((unsigned int) MSG_ReadByte() << 8);
948 if (bits & E_EFFECTS1)
949 e->effects = (e->effects & 0xFF00) | (unsigned int) MSG_ReadByte();
950 if (bits & E_EFFECTS2)
951 e->effects = (e->effects & 0x00FF) | ((unsigned int) MSG_ReadByte() << 8);
952 if (bits & E_COLORMAP)
953 e->colormap = MSG_ReadByte();
955 e->skin = MSG_ReadByte();
957 e->alpha = MSG_ReadByte();
959 e->scale = MSG_ReadByte();
960 if (bits & E_GLOWSIZE)
961 e->glowsize = MSG_ReadByte();
962 if (bits & E_GLOWCOLOR)
963 e->glowcolor = MSG_ReadByte();
964 if (cls.protocol == PROTOCOL_DARKPLACES2)
966 e->flags = MSG_ReadByte();
967 if (bits & E_TAGATTACHMENT)
969 e->tagentity = (unsigned short) MSG_ReadShort();
970 e->tagindex = MSG_ReadByte();
974 e->light[0] = (unsigned short) MSG_ReadShort();
975 e->light[1] = (unsigned short) MSG_ReadShort();
976 e->light[2] = (unsigned short) MSG_ReadShort();
977 e->light[3] = (unsigned short) MSG_ReadShort();
979 if (bits & E_LIGHTSTYLE)
980 e->lightstyle = MSG_ReadByte();
981 if (bits & E_LIGHTPFLAGS)
982 e->lightpflags = MSG_ReadByte();
984 if (developer_networkentities.integer >= 2)
986 Con_Printf("ReadFields e%i", e->number);
988 if (bits & E_ORIGIN1)
989 Con_Printf(" E_ORIGIN1 %f", e->origin[0]);
990 if (bits & E_ORIGIN2)
991 Con_Printf(" E_ORIGIN2 %f", e->origin[1]);
992 if (bits & E_ORIGIN3)
993 Con_Printf(" E_ORIGIN3 %f", e->origin[2]);
995 Con_Printf(" E_ANGLE1 %f", e->angles[0]);
997 Con_Printf(" E_ANGLE2 %f", e->angles[1]);
999 Con_Printf(" E_ANGLE3 %f", e->angles[2]);
1000 if (bits & (E_MODEL1 | E_MODEL2))
1001 Con_Printf(" E_MODEL %i", e->modelindex);
1003 if (bits & (E_FRAME1 | E_FRAME2))
1004 Con_Printf(" E_FRAME %i", e->frame);
1005 if (bits & (E_EFFECTS1 | E_EFFECTS2))
1006 Con_Printf(" E_EFFECTS %i", e->effects);
1008 Con_Printf(" E_ALPHA %f", e->alpha / 255.0f);
1010 Con_Printf(" E_SCALE %f", e->scale / 16.0f);
1011 if (bits & E_COLORMAP)
1012 Con_Printf(" E_COLORMAP %i", e->colormap);
1014 Con_Printf(" E_SKIN %i", e->skin);
1016 if (bits & E_GLOWSIZE)
1017 Con_Printf(" E_GLOWSIZE %i", e->glowsize * 4);
1018 if (bits & E_GLOWCOLOR)
1019 Con_Printf(" E_GLOWCOLOR %i", e->glowcolor);
1022 Con_Printf(" E_LIGHT %i:%i:%i:%i", e->light[0], e->light[1], e->light[2], e->light[3]);
1023 if (bits & E_LIGHTPFLAGS)
1024 Con_Printf(" E_LIGHTPFLAGS %i", e->lightpflags);
1026 if (bits & E_TAGATTACHMENT)
1027 Con_Printf(" E_TAGATTACHMENT e%i:%i", e->tagentity, e->tagindex);
1028 if (bits & E_LIGHTSTYLE)
1029 Con_Printf(" E_LIGHTSTYLE %i", e->lightstyle);
1034 // (client and server) allocates a new empty database
1035 entityframe_database_t *EntityFrame_AllocDatabase(mempool_t *mempool)
1037 return (entityframe_database_t *)Mem_Alloc(mempool, sizeof(entityframe_database_t));
1040 // (client and server) frees the database
1041 void EntityFrame_FreeDatabase(entityframe_database_t *d)
1046 // (server) clears the database to contain no frames (thus delta compression compresses against nothing)
1047 void EntityFrame_ClearDatabase(entityframe_database_t *d)
1049 memset(d, 0, sizeof(*d));
1052 // (server and client) removes frames older than 'frame' from database
1053 void EntityFrame_AckFrame(entityframe_database_t *d, int frame)
1056 d->ackframenum = frame;
1057 for (i = 0;i < d->numframes && d->frames[i].framenum < frame;i++);
1058 // ignore outdated frame acks (out of order packets)
1062 // if some queue is left, slide it down to beginning of array
1064 memmove(&d->frames[0], &d->frames[i], sizeof(d->frames[0]) * d->numframes);
1067 // (server) clears frame, to prepare for adding entities
1068 void EntityFrame_Clear(entity_frame_t *f, vec3_t eye, int framenum)
1071 f->framenum = framenum;
1074 VectorClear(f->eye);
1076 VectorCopy(eye, f->eye);
1079 // (server and client) reads a frame from the database
1080 void EntityFrame_FetchFrame(entityframe_database_t *d, int framenum, entity_frame_t *f)
1083 EntityFrame_Clear(f, NULL, -1);
1084 for (i = 0;i < d->numframes && d->frames[i].framenum < framenum;i++);
1085 if (i < d->numframes && framenum == d->frames[i].framenum)
1087 f->framenum = framenum;
1088 f->numentities = d->frames[i].endentity - d->frames[i].firstentity;
1089 n = MAX_ENTITY_DATABASE - (d->frames[i].firstentity % MAX_ENTITY_DATABASE);
1090 if (n > f->numentities)
1092 memcpy(f->entitydata, d->entitydata + d->frames[i].firstentity % MAX_ENTITY_DATABASE, sizeof(*f->entitydata) * n);
1093 if (f->numentities > n)
1094 memcpy(f->entitydata + n, d->entitydata, sizeof(*f->entitydata) * (f->numentities - n));
1095 VectorCopy(d->eye, f->eye);
1099 // (server and client) adds a entity_frame to the database, for future reference
1100 void EntityFrame_AddFrame(entityframe_database_t *d, vec3_t eye, int framenum, int numentities, const entity_state_t *entitydata)
1103 entity_frameinfo_t *info;
1105 VectorCopy(eye, d->eye);
1107 // figure out how many entity slots are used already
1110 n = d->frames[d->numframes - 1].endentity - d->frames[0].firstentity;
1111 if (n + numentities > MAX_ENTITY_DATABASE || d->numframes >= MAX_ENTITY_HISTORY)
1113 // ran out of room, dump database
1114 EntityFrame_ClearDatabase(d);
1118 info = &d->frames[d->numframes];
1119 info->framenum = framenum;
1121 // make sure we check the newly added frame as well, but we haven't incremented numframes yet
1122 for (n = 0;n <= d->numframes;n++)
1124 if (e >= d->frames[n].framenum)
1127 Con_Print("EntityFrame_AddFrame: tried to add out of sequence frame to database\n");
1129 Con_Print("EntityFrame_AddFrame: out of sequence frames in database\n");
1132 e = d->frames[n].framenum;
1134 // if database still has frames after that...
1136 info->firstentity = d->frames[d->numframes - 1].endentity;
1138 info->firstentity = 0;
1139 info->endentity = info->firstentity + numentities;
1142 n = info->firstentity % MAX_ENTITY_DATABASE;
1143 e = MAX_ENTITY_DATABASE - n;
1144 if (e > numentities)
1146 memcpy(d->entitydata + n, entitydata, sizeof(entity_state_t) * e);
1147 if (numentities > e)
1148 memcpy(d->entitydata, entitydata + e, sizeof(entity_state_t) * (numentities - e));
1151 // (server) writes a frame to network stream
1152 void EntityFrame_WriteFrame(sizebuf_t *msg, int maxsize, entityframe_database_t *d, int numstates, const entity_state_t *states, int viewentnum)
1154 int i, onum, number;
1155 entity_frame_t *o = &d->deltaframe;
1156 const entity_state_t *ent, *delta;
1160 d->latestframenum++;
1163 for (i = 0;i < numstates;i++)
1165 if (states[i].number == viewentnum)
1167 VectorSet(eye, states[i].origin[0], states[i].origin[1], states[i].origin[2] + 22);
1172 EntityFrame_AddFrame(d, eye, d->latestframenum, numstates, states);
1174 EntityFrame_FetchFrame(d, d->ackframenum, o);
1176 MSG_WriteByte (msg, svc_entities);
1177 MSG_WriteLong (msg, o->framenum);
1178 MSG_WriteLong (msg, d->latestframenum);
1179 MSG_WriteFloat (msg, eye[0]);
1180 MSG_WriteFloat (msg, eye[1]);
1181 MSG_WriteFloat (msg, eye[2]);
1184 for (i = 0;i < numstates;i++)
1187 number = ent->number;
1189 val = PRVM_EDICTFIELDVALUE((&prog->edicts[number]), prog->fieldoffsets.SendEntity);
1190 if(val && val->function)
1192 for (;onum < o->numentities && o->entitydata[onum].number < number;onum++)
1194 // write remove message
1195 MSG_WriteShort(msg, o->entitydata[onum].number | 0x8000);
1197 if (onum < o->numentities && (o->entitydata[onum].number == number))
1199 // delta from previous frame
1200 delta = o->entitydata + onum;
1201 // advance to next entity in delta frame
1206 // delta from defaults
1207 delta = &defaultstate;
1209 EntityState_WriteUpdate(ent, msg, delta);
1211 for (;onum < o->numentities;onum++)
1213 // write remove message
1214 MSG_WriteShort(msg, o->entitydata[onum].number | 0x8000);
1216 MSG_WriteShort(msg, 0xFFFF);
1219 // (client) reads a frame from network stream
1220 void EntityFrame_CL_ReadFrame(void)
1222 int i, number, removed;
1223 entity_frame_t *f, *delta;
1224 entity_state_t *e, *old, *oldend;
1226 entityframe_database_t *d;
1227 if (!cl.entitydatabase)
1228 cl.entitydatabase = EntityFrame_AllocDatabase(cls.levelmempool);
1229 d = cl.entitydatabase;
1231 delta = &d->deltaframe;
1233 EntityFrame_Clear(f, NULL, -1);
1235 // read the frame header info
1236 f->time = cl.mtime[0];
1237 number = MSG_ReadLong();
1238 for (i = 0;i < LATESTFRAMENUMS-1;i++)
1239 cl.latestframenums[i] = cl.latestframenums[i+1];
1240 cl.latestframenums[LATESTFRAMENUMS-1] = f->framenum = MSG_ReadLong();
1241 f->eye[0] = MSG_ReadFloat();
1242 f->eye[1] = MSG_ReadFloat();
1243 f->eye[2] = MSG_ReadFloat();
1244 EntityFrame_AckFrame(d, number);
1245 EntityFrame_FetchFrame(d, number, delta);
1246 old = delta->entitydata;
1247 oldend = old + delta->numentities;
1248 // read entities until we hit the magic 0xFFFF end tag
1249 while ((number = (unsigned short) MSG_ReadShort()) != 0xFFFF && !msg_badread)
1252 Host_Error("EntityFrame_Read: read error");
1253 removed = number & 0x8000;
1255 if (number >= MAX_EDICTS)
1256 Host_Error("EntityFrame_Read: number (%i) >= MAX_EDICTS (%i)", number, MAX_EDICTS);
1258 // seek to entity, while copying any skipped entities (assume unchanged)
1259 while (old < oldend && old->number < number)
1261 if (f->numentities >= MAX_ENTITY_DATABASE)
1262 Host_Error("EntityFrame_Read: entity list too big");
1263 f->entitydata[f->numentities] = *old++;
1264 f->entitydata[f->numentities++].time = cl.mtime[0];
1268 if (old < oldend && old->number == number)
1271 Con_Printf("EntityFrame_Read: REMOVE on unused entity %i\n", number);
1275 if (f->numentities >= MAX_ENTITY_DATABASE)
1276 Host_Error("EntityFrame_Read: entity list too big");
1278 // reserve this slot
1279 e = f->entitydata + f->numentities++;
1281 if (old < oldend && old->number == number)
1283 // delta from old entity
1288 // delta from defaults
1292 if (cl.num_entities <= number)
1294 cl.num_entities = number + 1;
1295 if (number >= cl.max_entities)
1296 CL_ExpandEntities(number);
1298 cl.entities_active[number] = true;
1300 e->time = cl.mtime[0];
1302 EntityState_ReadFields(e, EntityState_ReadExtendBits());
1305 while (old < oldend)
1307 if (f->numentities >= MAX_ENTITY_DATABASE)
1308 Host_Error("EntityFrame_Read: entity list too big");
1309 f->entitydata[f->numentities] = *old++;
1310 f->entitydata[f->numentities++].time = cl.mtime[0];
1312 EntityFrame_AddFrame(d, f->eye, f->framenum, f->numentities, f->entitydata);
1314 memset(cl.entities_active, 0, cl.num_entities * sizeof(unsigned char));
1316 for (i = 0;i < f->numentities;i++)
1318 for (;number < f->entitydata[i].number && number < cl.num_entities;number++)
1320 if (cl.entities_active[number])
1322 cl.entities_active[number] = false;
1323 cl.entities[number].state_current.active = false;
1326 if (number >= cl.num_entities)
1328 // update the entity
1329 ent = &cl.entities[number];
1330 ent->state_previous = ent->state_current;
1331 ent->state_current = f->entitydata[i];
1332 CL_MoveLerpEntityStates(ent);
1333 // the entity lives again...
1334 cl.entities_active[number] = true;
1337 for (;number < cl.num_entities;number++)
1339 if (cl.entities_active[number])
1341 cl.entities_active[number] = false;
1342 cl.entities[number].state_current.active = false;
1348 // (client) returns the frame number of the most recent frame recieved
1349 int EntityFrame_MostRecentlyRecievedFrameNum(entityframe_database_t *d)
1352 return d->frames[d->numframes - 1].framenum;
1362 entity_state_t *EntityFrame4_GetReferenceEntity(entityframe4_database_t *d, int number)
1364 if (d->maxreferenceentities <= number)
1366 int oldmax = d->maxreferenceentities;
1367 entity_state_t *oldentity = d->referenceentity;
1368 d->maxreferenceentities = (number + 15) & ~7;
1369 d->referenceentity = (entity_state_t *)Mem_Alloc(d->mempool, d->maxreferenceentities * sizeof(*d->referenceentity));
1372 memcpy(d->referenceentity, oldentity, oldmax * sizeof(*d->referenceentity));
1373 Mem_Free(oldentity);
1375 // clear the newly created entities
1376 for (;oldmax < d->maxreferenceentities;oldmax++)
1378 d->referenceentity[oldmax] = defaultstate;
1379 d->referenceentity[oldmax].number = oldmax;
1382 return d->referenceentity + number;
1385 void EntityFrame4_AddCommitEntity(entityframe4_database_t *d, const entity_state_t *s)
1387 // resize commit's entity list if full
1388 if (d->currentcommit->maxentities <= d->currentcommit->numentities)
1390 entity_state_t *oldentity = d->currentcommit->entity;
1391 d->currentcommit->maxentities += 8;
1392 d->currentcommit->entity = (entity_state_t *)Mem_Alloc(d->mempool, d->currentcommit->maxentities * sizeof(*d->currentcommit->entity));
1395 memcpy(d->currentcommit->entity, oldentity, d->currentcommit->numentities * sizeof(*d->currentcommit->entity));
1396 Mem_Free(oldentity);
1399 d->currentcommit->entity[d->currentcommit->numentities++] = *s;
1402 entityframe4_database_t *EntityFrame4_AllocDatabase(mempool_t *pool)
1404 entityframe4_database_t *d;
1405 d = (entityframe4_database_t *)Mem_Alloc(pool, sizeof(*d));
1407 EntityFrame4_ResetDatabase(d);
1411 void EntityFrame4_FreeDatabase(entityframe4_database_t *d)
1414 for (i = 0;i < MAX_ENTITY_HISTORY;i++)
1415 if (d->commit[i].entity)
1416 Mem_Free(d->commit[i].entity);
1417 if (d->referenceentity)
1418 Mem_Free(d->referenceentity);
1422 void EntityFrame4_ResetDatabase(entityframe4_database_t *d)
1425 d->referenceframenum = -1;
1426 for (i = 0;i < MAX_ENTITY_HISTORY;i++)
1427 d->commit[i].numentities = 0;
1428 for (i = 0;i < d->maxreferenceentities;i++)
1429 d->referenceentity[i] = defaultstate;
1432 int EntityFrame4_AckFrame(entityframe4_database_t *d, int framenum, int servermode)
1435 entity_database4_commit_t *commit;
1438 // reset reference, but leave commits alone
1439 d->referenceframenum = -1;
1440 for (i = 0;i < d->maxreferenceentities;i++)
1441 d->referenceentity[i] = defaultstate;
1442 // if this is the server, remove commits
1443 for (i = 0, commit = d->commit;i < MAX_ENTITY_HISTORY;i++, commit++)
1444 commit->numentities = 0;
1447 else if (d->referenceframenum == framenum)
1452 for (i = 0, commit = d->commit;i < MAX_ENTITY_HISTORY;i++, commit++)
1454 if (commit->numentities && commit->framenum <= framenum)
1456 if (commit->framenum == framenum)
1459 d->referenceframenum = framenum;
1460 if (developer_networkentities.integer >= 3)
1462 for (j = 0;j < commit->numentities;j++)
1464 entity_state_t *s = EntityFrame4_GetReferenceEntity(d, commit->entity[j].number);
1465 if (commit->entity[j].active != s->active)
1467 if (commit->entity[j].active)
1468 Con_Printf("commit entity %i has become active (modelindex %i)\n", commit->entity[j].number, commit->entity[j].modelindex);
1470 Con_Printf("commit entity %i has become inactive (modelindex %i)\n", commit->entity[j].number, commit->entity[j].modelindex);
1472 *s = commit->entity[j];
1476 for (j = 0;j < commit->numentities;j++)
1477 *EntityFrame4_GetReferenceEntity(d, commit->entity[j].number) = commit->entity[j];
1479 commit->numentities = 0;
1483 if (developer_networkentities.integer >= 1)
1485 Con_Printf("ack ref:%i database updated to: ref:%i commits:", framenum, d->referenceframenum);
1486 for (i = 0;i < MAX_ENTITY_HISTORY;i++)
1487 if (d->commit[i].numentities)
1488 Con_Printf(" %i", d->commit[i].framenum);
1494 void EntityFrame4_CL_ReadFrame(void)
1496 int i, n, cnumber, referenceframenum, framenum, enumber, done, stopnumber, skip = false;
1498 entityframe4_database_t *d;
1499 if (!cl.entitydatabase4)
1500 cl.entitydatabase4 = EntityFrame4_AllocDatabase(cls.levelmempool);
1501 d = cl.entitydatabase4;
1502 // read the number of the frame this refers to
1503 referenceframenum = MSG_ReadLong();
1504 // read the number of this frame
1505 for (i = 0;i < LATESTFRAMENUMS-1;i++)
1506 cl.latestframenums[i] = cl.latestframenums[i+1];
1507 cl.latestframenums[LATESTFRAMENUMS-1] = framenum = MSG_ReadLong();
1508 // read the start number
1509 enumber = (unsigned short) MSG_ReadShort();
1510 if (developer_networkentities.integer >= 10)
1512 Con_Printf("recv svc_entities num:%i ref:%i database: ref:%i commits:", framenum, referenceframenum, d->referenceframenum);
1513 for (i = 0;i < MAX_ENTITY_HISTORY;i++)
1514 if (d->commit[i].numentities)
1515 Con_Printf(" %i", d->commit[i].framenum);
1518 if (!EntityFrame4_AckFrame(d, referenceframenum, false))
1520 Con_Print("EntityFrame4_CL_ReadFrame: reference frame invalid (VERY BAD ERROR), this update will be skipped\n");
1523 d->currentcommit = NULL;
1524 for (i = 0;i < MAX_ENTITY_HISTORY;i++)
1526 if (!d->commit[i].numentities)
1528 d->currentcommit = d->commit + i;
1529 d->currentcommit->framenum = framenum;
1530 d->currentcommit->numentities = 0;
1533 if (d->currentcommit == NULL)
1535 Con_Printf("EntityFrame4_CL_ReadFrame: error while decoding frame %i: database full, reading but not storing this update\n", framenum);
1539 while (!done && !msg_badread)
1541 // read the number of the modified entity
1542 // (gaps will be copied unmodified)
1543 n = (unsigned short)MSG_ReadShort();
1546 // no more entities in this update, but we still need to copy the
1547 // rest of the reference entities (final gap)
1549 // read end of range number, then process normally
1550 n = (unsigned short)MSG_ReadShort();
1552 // high bit means it's a remove message
1553 cnumber = n & 0x7FFF;
1554 // if this is a live entity we may need to expand the array
1555 if (cl.num_entities <= cnumber && !(n & 0x8000))
1557 cl.num_entities = cnumber + 1;
1558 if (cnumber >= cl.max_entities)
1559 CL_ExpandEntities(cnumber);
1561 // add one (the changed one) if not done
1562 stopnumber = cnumber + !done;
1563 // process entities in range from the last one to the changed one
1564 for (;enumber < stopnumber;enumber++)
1566 if (skip || enumber >= cl.num_entities)
1568 if (enumber == cnumber && (n & 0x8000) == 0)
1570 entity_state_t tempstate;
1571 EntityState_ReadFields(&tempstate, EntityState_ReadExtendBits());
1575 // slide the current into the previous slot
1576 cl.entities[enumber].state_previous = cl.entities[enumber].state_current;
1577 // copy a new current from reference database
1578 cl.entities[enumber].state_current = *EntityFrame4_GetReferenceEntity(d, enumber);
1579 s = &cl.entities[enumber].state_current;
1580 // if this is the one to modify, read more data...
1581 if (enumber == cnumber)
1586 if (developer_networkentities.integer >= 2)
1587 Con_Printf("entity %i: remove\n", enumber);
1593 if (developer_networkentities.integer >= 2)
1594 Con_Printf("entity %i: update\n", enumber);
1596 EntityState_ReadFields(s, EntityState_ReadExtendBits());
1599 else if (developer_networkentities.integer >= 4)
1600 Con_Printf("entity %i: copy\n", enumber);
1601 // set the cl.entities_active flag
1602 cl.entities_active[enumber] = s->active;
1603 // set the update time
1604 s->time = cl.mtime[0];
1605 // fix the number (it gets wiped occasionally by copying from defaultstate)
1606 s->number = enumber;
1607 // check if we need to update the lerp stuff
1609 CL_MoveLerpEntityStates(&cl.entities[enumber]);
1610 // add this to the commit entry whether it is modified or not
1611 if (d->currentcommit)
1612 EntityFrame4_AddCommitEntity(d, &cl.entities[enumber].state_current);
1613 // print extra messages if desired
1614 if (developer_networkentities.integer >= 2 && cl.entities[enumber].state_current.active != cl.entities[enumber].state_previous.active)
1616 if (cl.entities[enumber].state_current.active)
1617 Con_Printf("entity #%i has become active\n", enumber);
1618 else if (cl.entities[enumber].state_previous.active)
1619 Con_Printf("entity #%i has become inactive\n", enumber);
1623 d->currentcommit = NULL;
1625 EntityFrame4_ResetDatabase(d);
1628 void EntityFrame4_WriteFrame(sizebuf_t *msg, int maxsize, entityframe4_database_t *d, int numstates, const entity_state_t *states)
1630 const entity_state_t *e, *s;
1631 entity_state_t inactiveentitystate;
1632 int i, n, startnumber;
1634 unsigned char data[128];
1637 // if there isn't enough space to accomplish anything, skip it
1638 if (msg->cursize + 24 > maxsize)
1641 // prepare the buffer
1642 memset(&buf, 0, sizeof(buf));
1644 buf.maxsize = sizeof(data);
1646 for (i = 0;i < MAX_ENTITY_HISTORY;i++)
1647 if (!d->commit[i].numentities)
1649 // if commit buffer full, just don't bother writing an update this frame
1650 if (i == MAX_ENTITY_HISTORY)
1652 d->currentcommit = d->commit + i;
1654 // this state's number gets played around with later
1655 inactiveentitystate = defaultstate;
1657 d->currentcommit->numentities = 0;
1658 d->currentcommit->framenum = ++d->latestframenumber;
1659 MSG_WriteByte(msg, svc_entities);
1660 MSG_WriteLong(msg, d->referenceframenum);
1661 MSG_WriteLong(msg, d->currentcommit->framenum);
1662 if (developer_networkentities.integer >= 10)
1664 Con_Printf("send svc_entities num:%i ref:%i (database: ref:%i commits:", d->currentcommit->framenum, d->referenceframenum, d->referenceframenum);
1665 for (i = 0;i < MAX_ENTITY_HISTORY;i++)
1666 if (d->commit[i].numentities)
1667 Con_Printf(" %i", d->commit[i].framenum);
1670 if (d->currententitynumber >= prog->max_edicts)
1673 startnumber = bound(1, d->currententitynumber, prog->max_edicts - 1);
1674 MSG_WriteShort(msg, startnumber);
1675 // reset currententitynumber so if the loop does not break it we will
1676 // start at beginning next frame (if it does break, it will set it)
1677 d->currententitynumber = 1;
1678 for (i = 0, n = startnumber;n < prog->max_edicts;n++)
1680 val = PRVM_EDICTFIELDVALUE((&prog->edicts[n]), prog->fieldoffsets.SendEntity);
1681 if(val && val->function)
1683 // find the old state to delta from
1684 e = EntityFrame4_GetReferenceEntity(d, n);
1685 // prepare the buffer
1687 // entity exists, build an update (if empty there is no change)
1688 // find the state in the list
1689 for (;i < numstates && states[i].number < n;i++);
1695 EntityState_WriteUpdate(s, &buf, e);
1699 inactiveentitystate.number = n;
1700 s = &inactiveentitystate;
1703 // entity used to exist but doesn't anymore, send remove
1704 MSG_WriteShort(&buf, n | 0x8000);
1707 // if the commit is full, we're done this frame
1708 if (msg->cursize + buf.cursize > maxsize - 4)
1710 // next frame we will continue where we left off
1713 // add the entity to the commit
1714 EntityFrame4_AddCommitEntity(d, s);
1715 // if the message is empty, skip out now
1718 // write the message to the packet
1719 SZ_Write(msg, buf.data, buf.cursize);
1722 d->currententitynumber = n;
1724 // remove world message (invalid, and thus a good terminator)
1725 MSG_WriteShort(msg, 0x8000);
1726 // write the number of the end entity
1727 MSG_WriteShort(msg, d->currententitynumber);
1729 d->currentcommit = NULL;
1735 entityframe5_database_t *EntityFrame5_AllocDatabase(mempool_t *pool)
1738 entityframe5_database_t *d;
1739 d = (entityframe5_database_t *)Mem_Alloc(pool, sizeof(*d));
1740 d->latestframenum = 0;
1741 for (i = 0;i < d->maxedicts;i++)
1742 d->states[i] = defaultstate;
1746 void EntityFrame5_FreeDatabase(entityframe5_database_t *d)
1748 // all the [maxedicts] memory is allocated at once, so there's only one
1751 Mem_Free(d->deltabits);
1755 void EntityFrame5_ExpandEdicts(entityframe5_database_t *d, int newmax)
1757 if (d->maxedicts < newmax)
1759 unsigned char *data;
1760 int oldmaxedicts = d->maxedicts;
1761 int *olddeltabits = d->deltabits;
1762 unsigned char *oldpriorities = d->priorities;
1763 int *oldupdateframenum = d->updateframenum;
1764 entity_state_t *oldstates = d->states;
1765 unsigned char *oldvisiblebits = d->visiblebits;
1766 d->maxedicts = newmax;
1767 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));
1768 d->deltabits = (int *)data;data += d->maxedicts * sizeof(int);
1769 d->priorities = (unsigned char *)data;data += d->maxedicts * sizeof(unsigned char);
1770 d->updateframenum = (int *)data;data += d->maxedicts * sizeof(int);
1771 d->states = (entity_state_t *)data;data += d->maxedicts * sizeof(entity_state_t);
1772 d->visiblebits = (unsigned char *)data;data += (d->maxedicts+7)/8 * sizeof(unsigned char);
1775 memcpy(d->deltabits, olddeltabits, oldmaxedicts * sizeof(int));
1776 memcpy(d->priorities, oldpriorities, oldmaxedicts * sizeof(unsigned char));
1777 memcpy(d->updateframenum, oldupdateframenum, oldmaxedicts * sizeof(int));
1778 memcpy(d->states, oldstates, oldmaxedicts * sizeof(entity_state_t));
1779 memcpy(d->visiblebits, oldvisiblebits, (oldmaxedicts+7)/8 * sizeof(unsigned char));
1780 // the previous buffers were a single allocation, so just one free
1781 Mem_Free(olddeltabits);
1786 int EntityState5_Priority(entityframe5_database_t *d, int stateindex)
1788 int limit, priority;
1790 // if it is the player, update urgently
1791 if (stateindex == d->viewentnum)
1792 return ENTITYFRAME5_PRIORITYLEVELS - 1;
1793 // priority increases each frame no matter what happens
1794 priority = d->priorities[stateindex] + 1;
1795 // players get an extra priority boost
1796 if (stateindex <= svs.maxclients)
1798 // remove dead entities very quickly because they are just 2 bytes
1799 if (!d->states[stateindex].active)
1802 return bound(1, priority, ENTITYFRAME5_PRIORITYLEVELS - 1);
1804 // certain changes are more noticable than others
1805 if (d->deltabits[stateindex] & (E5_FULLUPDATE | E5_ATTACHMENT | E5_MODEL | E5_FLAGS | E5_COLORMAP))
1807 // find the root entity this one is attached to, and judge relevance by it
1808 for (limit = 0;limit < 256;limit++)
1810 s = d->states + stateindex;
1811 if (s->flags & RENDER_VIEWMODEL)
1812 stateindex = d->viewentnum;
1813 else if (s->tagentity)
1814 stateindex = s->tagentity;
1817 if (d->maxedicts < stateindex)
1818 EntityFrame5_ExpandEdicts(d, (stateindex+256)&~255);
1821 Con_DPrintf("Protocol: Runaway loop recursing tagentity links on entity %i\n", stateindex);
1822 // now that we have the parent entity we can make some decisions based on
1823 // distance from the player
1824 if (VectorDistance(d->states[d->viewentnum].netcenter, s->netcenter) < 1024.0f)
1826 return bound(1, priority, ENTITYFRAME5_PRIORITYLEVELS - 1);
1829 void EntityState5_WriteUpdate(int number, const entity_state_t *s, int changedbits, sizebuf_t *msg)
1831 unsigned int bits = 0;
1832 ENTITYSIZEPROFILING_START(msg, s->number);
1835 val = PRVM_EDICTFIELDVALUE((&prog->edicts[s->number]), prog->fieldoffsets.SendEntity);
1836 if(val && val->function)
1840 MSG_WriteShort(msg, number | 0x8000);
1844 if ((bits & E5_ORIGIN) && ((s->flags & RENDER_EXTERIORMODEL) || 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))
1845 bits |= E5_ORIGIN32;
1848 // (int)(f * 8 - 0.5) >= -32768
1849 // (f * 8 - 0.5) > -32769
1852 // (int)(f * 8 + 0.5) <= 32767
1853 // (f * 8 + 0.5) < 32768
1854 // f * 8 + 0.5) < 4095.9375
1855 if ((bits & E5_ANGLES) && !(s->flags & RENDER_LOWPRECISION))
1856 bits |= E5_ANGLES16;
1857 if ((bits & E5_MODEL) && s->modelindex >= 256)
1859 if ((bits & E5_FRAME) && s->frame >= 256)
1861 if (bits & E5_EFFECTS)
1863 if (s->effects & 0xFFFF0000)
1864 bits |= E5_EFFECTS32;
1865 else if (s->effects & 0xFFFFFF00)
1866 bits |= E5_EFFECTS16;
1872 if (bits >= 16777216)
1874 MSG_WriteShort(msg, number);
1875 MSG_WriteByte(msg, bits & 0xFF);
1876 if (bits & E5_EXTEND1)
1877 MSG_WriteByte(msg, (bits >> 8) & 0xFF);
1878 if (bits & E5_EXTEND2)
1879 MSG_WriteByte(msg, (bits >> 16) & 0xFF);
1880 if (bits & E5_EXTEND3)
1881 MSG_WriteByte(msg, (bits >> 24) & 0xFF);
1882 if (bits & E5_FLAGS)
1883 MSG_WriteByte(msg, s->flags);
1884 if (bits & E5_ORIGIN)
1886 if (bits & E5_ORIGIN32)
1888 MSG_WriteCoord32f(msg, s->origin[0]);
1889 MSG_WriteCoord32f(msg, s->origin[1]);
1890 MSG_WriteCoord32f(msg, s->origin[2]);
1894 MSG_WriteCoord13i(msg, s->origin[0]);
1895 MSG_WriteCoord13i(msg, s->origin[1]);
1896 MSG_WriteCoord13i(msg, s->origin[2]);
1899 if (bits & E5_ANGLES)
1901 if (bits & E5_ANGLES16)
1903 MSG_WriteAngle16i(msg, s->angles[0]);
1904 MSG_WriteAngle16i(msg, s->angles[1]);
1905 MSG_WriteAngle16i(msg, s->angles[2]);
1909 MSG_WriteAngle8i(msg, s->angles[0]);
1910 MSG_WriteAngle8i(msg, s->angles[1]);
1911 MSG_WriteAngle8i(msg, s->angles[2]);
1914 if (bits & E5_MODEL)
1916 if (bits & E5_MODEL16)
1917 MSG_WriteShort(msg, s->modelindex);
1919 MSG_WriteByte(msg, s->modelindex);
1921 if (bits & E5_FRAME)
1923 if (bits & E5_FRAME16)
1924 MSG_WriteShort(msg, s->frame);
1926 MSG_WriteByte(msg, s->frame);
1929 MSG_WriteByte(msg, s->skin);
1930 if (bits & E5_EFFECTS)
1932 if (bits & E5_EFFECTS32)
1933 MSG_WriteLong(msg, s->effects);
1934 else if (bits & E5_EFFECTS16)
1935 MSG_WriteShort(msg, s->effects);
1937 MSG_WriteByte(msg, s->effects);
1939 if (bits & E5_ALPHA)
1940 MSG_WriteByte(msg, s->alpha);
1941 if (bits & E5_SCALE)
1942 MSG_WriteByte(msg, s->scale);
1943 if (bits & E5_COLORMAP)
1944 MSG_WriteByte(msg, s->colormap);
1945 if (bits & E5_ATTACHMENT)
1947 MSG_WriteShort(msg, s->tagentity);
1948 MSG_WriteByte(msg, s->tagindex);
1950 if (bits & E5_LIGHT)
1952 MSG_WriteShort(msg, s->light[0]);
1953 MSG_WriteShort(msg, s->light[1]);
1954 MSG_WriteShort(msg, s->light[2]);
1955 MSG_WriteShort(msg, s->light[3]);
1956 MSG_WriteByte(msg, s->lightstyle);
1957 MSG_WriteByte(msg, s->lightpflags);
1961 MSG_WriteByte(msg, s->glowsize);
1962 MSG_WriteByte(msg, s->glowcolor);
1964 if (bits & E5_COLORMOD)
1966 MSG_WriteByte(msg, s->colormod[0]);
1967 MSG_WriteByte(msg, s->colormod[1]);
1968 MSG_WriteByte(msg, s->colormod[2]);
1972 ENTITYSIZEPROFILING_END(msg, s->number);
1975 void EntityState5_ReadUpdate(entity_state_t *s, int number)
1978 bits = MSG_ReadByte();
1979 if (bits & E5_EXTEND1)
1981 bits |= MSG_ReadByte() << 8;
1982 if (bits & E5_EXTEND2)
1984 bits |= MSG_ReadByte() << 16;
1985 if (bits & E5_EXTEND3)
1986 bits |= MSG_ReadByte() << 24;
1989 if (bits & E5_FULLUPDATE)
1994 if (bits & E5_FLAGS)
1995 s->flags = MSG_ReadByte();
1996 if (bits & E5_ORIGIN)
1998 if (bits & E5_ORIGIN32)
2000 s->origin[0] = MSG_ReadCoord32f();
2001 s->origin[1] = MSG_ReadCoord32f();
2002 s->origin[2] = MSG_ReadCoord32f();
2006 s->origin[0] = MSG_ReadCoord13i();
2007 s->origin[1] = MSG_ReadCoord13i();
2008 s->origin[2] = MSG_ReadCoord13i();
2011 if (bits & E5_ANGLES)
2013 if (bits & E5_ANGLES16)
2015 s->angles[0] = MSG_ReadAngle16i();
2016 s->angles[1] = MSG_ReadAngle16i();
2017 s->angles[2] = MSG_ReadAngle16i();
2021 s->angles[0] = MSG_ReadAngle8i();
2022 s->angles[1] = MSG_ReadAngle8i();
2023 s->angles[2] = MSG_ReadAngle8i();
2026 if (bits & E5_MODEL)
2028 if (bits & E5_MODEL16)
2029 s->modelindex = (unsigned short) MSG_ReadShort();
2031 s->modelindex = MSG_ReadByte();
2033 if (bits & E5_FRAME)
2035 if (bits & E5_FRAME16)
2036 s->frame = (unsigned short) MSG_ReadShort();
2038 s->frame = MSG_ReadByte();
2041 s->skin = MSG_ReadByte();
2042 if (bits & E5_EFFECTS)
2044 if (bits & E5_EFFECTS32)
2045 s->effects = (unsigned int) MSG_ReadLong();
2046 else if (bits & E5_EFFECTS16)
2047 s->effects = (unsigned short) MSG_ReadShort();
2049 s->effects = MSG_ReadByte();
2051 if (bits & E5_ALPHA)
2052 s->alpha = MSG_ReadByte();
2053 if (bits & E5_SCALE)
2054 s->scale = MSG_ReadByte();
2055 if (bits & E5_COLORMAP)
2056 s->colormap = MSG_ReadByte();
2057 if (bits & E5_ATTACHMENT)
2059 s->tagentity = (unsigned short) MSG_ReadShort();
2060 s->tagindex = MSG_ReadByte();
2062 if (bits & E5_LIGHT)
2064 s->light[0] = (unsigned short) MSG_ReadShort();
2065 s->light[1] = (unsigned short) MSG_ReadShort();
2066 s->light[2] = (unsigned short) MSG_ReadShort();
2067 s->light[3] = (unsigned short) MSG_ReadShort();
2068 s->lightstyle = MSG_ReadByte();
2069 s->lightpflags = MSG_ReadByte();
2073 s->glowsize = MSG_ReadByte();
2074 s->glowcolor = MSG_ReadByte();
2076 if (bits & E5_COLORMOD)
2078 s->colormod[0] = MSG_ReadByte();
2079 s->colormod[1] = MSG_ReadByte();
2080 s->colormod[2] = MSG_ReadByte();
2084 if (developer_networkentities.integer >= 2)
2086 Con_Printf("ReadFields e%i", number);
2088 if (bits & E5_ORIGIN)
2089 Con_Printf(" E5_ORIGIN %f %f %f", s->origin[0], s->origin[1], s->origin[2]);
2090 if (bits & E5_ANGLES)
2091 Con_Printf(" E5_ANGLES %f %f %f", s->angles[0], s->angles[1], s->angles[2]);
2092 if (bits & E5_MODEL)
2093 Con_Printf(" E5_MODEL %i", s->modelindex);
2094 if (bits & E5_FRAME)
2095 Con_Printf(" E5_FRAME %i", s->frame);
2097 Con_Printf(" E5_SKIN %i", s->skin);
2098 if (bits & E5_EFFECTS)
2099 Con_Printf(" E5_EFFECTS %i", s->effects);
2100 if (bits & E5_FLAGS)
2102 Con_Printf(" E5_FLAGS %i (", s->flags);
2103 if (s->flags & RENDER_STEP)
2105 if (s->flags & RENDER_GLOWTRAIL)
2106 Con_Print(" GLOWTRAIL");
2107 if (s->flags & RENDER_VIEWMODEL)
2108 Con_Print(" VIEWMODEL");
2109 if (s->flags & RENDER_EXTERIORMODEL)
2110 Con_Print(" EXTERIORMODEL");
2111 if (s->flags & RENDER_LOWPRECISION)
2112 Con_Print(" LOWPRECISION");
2113 if (s->flags & RENDER_COLORMAPPED)
2114 Con_Print(" COLORMAPPED");
2115 if (s->flags & RENDER_SHADOW)
2116 Con_Print(" SHADOW");
2117 if (s->flags & RENDER_LIGHT)
2118 Con_Print(" LIGHT");
2119 if (s->flags & RENDER_NOSELFSHADOW)
2120 Con_Print(" NOSELFSHADOW");
2123 if (bits & E5_ALPHA)
2124 Con_Printf(" E5_ALPHA %f", s->alpha / 255.0f);
2125 if (bits & E5_SCALE)
2126 Con_Printf(" E5_SCALE %f", s->scale / 16.0f);
2127 if (bits & E5_COLORMAP)
2128 Con_Printf(" E5_COLORMAP %i", s->colormap);
2129 if (bits & E5_ATTACHMENT)
2130 Con_Printf(" E5_ATTACHMENT e%i:%i", s->tagentity, s->tagindex);
2131 if (bits & E5_LIGHT)
2132 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);
2134 Con_Printf(" E5_GLOW %i:%i", s->glowsize * 4, s->glowcolor);
2135 if (bits & E5_COLORMOD)
2136 Con_Printf(" E5_COLORMOD %f:%f:%f", s->colormod[0] / 32.0f, s->colormod[1] / 32.0f, s->colormod[2] / 32.0f);
2141 int EntityState5_DeltaBits(const entity_state_t *o, const entity_state_t *n)
2143 unsigned int bits = 0;
2147 bits |= E5_FULLUPDATE;
2148 if (!VectorCompare(o->origin, n->origin))
2150 if (!VectorCompare(o->angles, n->angles))
2152 if (o->modelindex != n->modelindex)
2154 if (o->frame != n->frame)
2156 if (o->skin != n->skin)
2158 if (o->effects != n->effects)
2160 if (o->flags != n->flags)
2162 if (o->alpha != n->alpha)
2164 if (o->scale != n->scale)
2166 if (o->colormap != n->colormap)
2167 bits |= E5_COLORMAP;
2168 if (o->tagentity != n->tagentity || o->tagindex != n->tagindex)
2169 bits |= E5_ATTACHMENT;
2170 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)
2172 if (o->glowsize != n->glowsize || o->glowcolor != n->glowcolor)
2174 if (o->colormod[0] != n->colormod[0] || o->colormod[1] != n->colormod[1] || o->colormod[2] != n->colormod[2])
2175 bits |= E5_COLORMOD;
2179 bits |= E5_FULLUPDATE;
2183 void EntityFrame5_CL_ReadFrame(void)
2188 // read the number of this frame to echo back in next input packet
2189 for (i = 0;i < LATESTFRAMENUMS-1;i++)
2190 cl.latestframenums[i] = cl.latestframenums[i+1];
2191 cl.latestframenums[LATESTFRAMENUMS-1] = MSG_ReadLong();
2192 if (developer_networkentities.integer >= 10)
2193 Con_Printf("recv: svc_entities %i\n", cl.latestframenums[LATESTFRAMENUMS-1]);
2194 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)
2195 cls.servermovesequence = MSG_ReadLong();
2196 // read entity numbers until we find a 0x8000
2197 // (which would be remove world entity, but is actually a terminator)
2198 while ((n = (unsigned short)MSG_ReadShort()) != 0x8000 && !msg_badread)
2200 // get the entity number
2201 enumber = n & 0x7FFF;
2202 // we may need to expand the array
2203 if (cl.num_entities <= enumber)
2205 cl.num_entities = enumber + 1;
2206 if (enumber >= cl.max_entities)
2207 CL_ExpandEntities(enumber);
2209 // look up the entity
2210 ent = cl.entities + enumber;
2211 // slide the current into the previous slot
2212 ent->state_previous = ent->state_current;
2214 s = &ent->state_current;
2223 EntityState5_ReadUpdate(s, enumber);
2225 // set the cl.entities_active flag
2226 cl.entities_active[enumber] = s->active;
2227 // set the update time
2228 s->time = cl.mtime[0];
2229 // fix the number (it gets wiped occasionally by copying from defaultstate)
2230 s->number = enumber;
2231 // check if we need to update the lerp stuff
2233 CL_MoveLerpEntityStates(&cl.entities[enumber]);
2234 // print extra messages if desired
2235 if (developer_networkentities.integer >= 2 && cl.entities[enumber].state_current.active != cl.entities[enumber].state_previous.active)
2237 if (cl.entities[enumber].state_current.active)
2238 Con_Printf("entity #%i has become active\n", enumber);
2239 else if (cl.entities[enumber].state_previous.active)
2240 Con_Printf("entity #%i has become inactive\n", enumber);
2245 void EntityFrame5_LostFrame(entityframe5_database_t *d, int framenum)
2247 int i, j, k, l, bits;
2248 entityframe5_changestate_t *s, *s2;
2249 entityframe5_packetlog_t *p, *p2;
2250 unsigned char statsdeltabits[(MAX_CL_STATS+7)/8];
2251 // scan for packets that were lost
2252 for (i = 0, p = d->packetlog;i < ENTITYFRAME5_MAXPACKETLOGS;i++, p++)
2254 if (p->packetnumber && p->packetnumber <= framenum)
2256 // packet was lost - merge deltabits into the main array so they
2257 // will be re-sent, but only if there is no newer update of that
2258 // bit in the logs (as those will arrive before this update)
2259 for (j = 0, s = p->states;j < p->numstates;j++, s++)
2261 // check for any newer updates to this entity and mask off any
2262 // overlapping bits (we don't need to send something again if
2263 // it has already been sent more recently)
2264 bits = s->bits & ~d->deltabits[s->number];
2265 for (k = 0, p2 = d->packetlog;k < ENTITYFRAME5_MAXPACKETLOGS && bits;k++, p2++)
2267 if (p2->packetnumber > framenum)
2269 for (l = 0, s2 = p2->states;l < p2->numstates;l++, s2++)
2271 if (s2->number == s->number)
2279 // if the bits haven't all been cleared, there were some bits
2280 // lost with this packet, so set them again now
2283 d->deltabits[s->number] |= bits;
2284 // if it was a very important update, set priority higher
2285 if (bits & (E5_FULLUPDATE | E5_ATTACHMENT | E5_MODEL | E5_COLORMAP))
2286 d->priorities[s->number] = max(d->priorities[s->number], 4);
2288 d->priorities[s->number] = max(d->priorities[s->number], 1);
2292 for (j = 0;j < MAX_CL_STATS;j++)
2294 for (l = 0;l < (MAX_CL_STATS+7)/8;l++)
2295 statsdeltabits[l] = p->statsdeltabits[l] & ~host_client->statsdeltabits[l];
2296 for (k = 0, p2 = d->packetlog;k < ENTITYFRAME5_MAXPACKETLOGS;k++, p2++)
2297 if (p2->packetnumber > framenum)
2298 for (l = 0;l < (MAX_CL_STATS+7)/8;l++)
2299 statsdeltabits[l] = p->statsdeltabits[l] & ~p2->statsdeltabits[l];
2300 for (l = 0;l < (MAX_CL_STATS+7)/8;l++)
2301 host_client->statsdeltabits[l] |= statsdeltabits[l];
2303 // delete this packet log as it is now obsolete
2304 p->packetnumber = 0;
2309 void EntityFrame5_AckFrame(entityframe5_database_t *d, int framenum)
2312 // scan for packets made obsolete by this ack and delete them
2313 for (i = 0;i < ENTITYFRAME5_MAXPACKETLOGS;i++)
2314 if (d->packetlog[i].packetnumber <= framenum)
2315 d->packetlog[i].packetnumber = 0;
2318 void EntityFrame5_WriteFrame(sizebuf_t *msg, int maxsize, entityframe5_database_t *d, int numstates, const entity_state_t *states, int viewentnum, int movesequence)
2320 const entity_state_t *n;
2321 int i, num, l, framenum, packetlognumber, priority;
2323 unsigned char data[128];
2324 entityframe5_packetlog_t *packetlog;
2326 if (prog->max_edicts > d->maxedicts)
2327 EntityFrame5_ExpandEdicts(d, prog->max_edicts);
2329 framenum = d->latestframenum + 1;
2330 d->viewentnum = viewentnum;
2332 // if packet log is full, mark all frames as lost, this will cause
2333 // it to send the lost data again
2334 for (packetlognumber = 0;packetlognumber < ENTITYFRAME5_MAXPACKETLOGS;packetlognumber++)
2335 if (d->packetlog[packetlognumber].packetnumber == 0)
2337 if (packetlognumber == ENTITYFRAME5_MAXPACKETLOGS)
2339 Con_DPrintf("EntityFrame5_WriteFrame: packetlog overflow for a client, resetting\n");
2340 EntityFrame5_LostFrame(d, framenum);
2341 packetlognumber = 0;
2344 // prepare the buffer
2345 memset(&buf, 0, sizeof(buf));
2347 buf.maxsize = sizeof(data);
2349 // detect changes in states
2351 for (i = 0, n = states;i < numstates;i++, n++)
2353 // mark gaps in entity numbering as removed entities
2354 for (;num < n->number;num++)
2356 // if the entity used to exist, clear it
2357 if (CHECKPVSBIT(d->visiblebits, num))
2359 CLEARPVSBIT(d->visiblebits, num);
2360 d->deltabits[num] = E5_FULLUPDATE;
2361 d->priorities[num] = max(d->priorities[num], 8); // removal is cheap
2362 d->states[num] = defaultstate;
2363 d->states[num].number = num;
2366 // update the entity state data
2367 if (!CHECKPVSBIT(d->visiblebits, num))
2369 // entity just spawned in, don't let it completely hog priority
2370 // because of being ancient on the first frame
2371 d->updateframenum[num] = framenum;
2372 // initial priority is a bit high to make projectiles send on the
2373 // first frame, among other things
2374 d->priorities[num] = max(d->priorities[num], 4);
2376 SETPVSBIT(d->visiblebits, num);
2377 d->deltabits[num] |= EntityState5_DeltaBits(d->states + num, n);
2378 d->priorities[num] = max(d->priorities[num], 1);
2379 d->states[num] = *n;
2380 d->states[num].number = num;
2381 // advance to next entity so the next iteration doesn't immediately remove it
2384 // all remaining entities are dead
2385 for (;num < d->maxedicts;num++)
2387 if (CHECKPVSBIT(d->visiblebits, num))
2389 CLEARPVSBIT(d->visiblebits, num);
2390 d->deltabits[num] = E5_FULLUPDATE;
2391 d->priorities[num] = max(d->priorities[num], 8); // removal is cheap
2392 d->states[num] = defaultstate;
2393 d->states[num].number = num;
2397 // if there isn't at least enough room for an empty svc_entities,
2398 // don't bother trying...
2399 if (buf.cursize + 11 > buf.maxsize)
2402 // build lists of entities by priority level
2403 memset(d->prioritychaincounts, 0, sizeof(d->prioritychaincounts));
2405 for (num = 0;num < d->maxedicts;num++)
2407 if (d->priorities[num])
2409 if (d->deltabits[num])
2411 if (d->priorities[num] < (ENTITYFRAME5_PRIORITYLEVELS - 1))
2412 d->priorities[num] = EntityState5_Priority(d, num);
2414 priority = d->priorities[num];
2415 if (d->prioritychaincounts[priority] < ENTITYFRAME5_MAXSTATES)
2416 d->prioritychains[priority][d->prioritychaincounts[priority]++] = num;
2419 d->priorities[num] = 0;
2423 // add packetlog entry
2424 packetlog = d->packetlog + packetlognumber;
2425 packetlog->packetnumber = framenum;
2426 packetlog->numstates = 0;
2427 // write stat updates
2428 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)
2430 for (i = 0;i < MAX_CL_STATS && msg->cursize + 6 + 11 <= maxsize;i++)
2432 if (host_client->statsdeltabits[i>>3] & (1<<(i&7)))
2434 host_client->statsdeltabits[i>>3] &= ~(1<<(i&7));
2435 packetlog->statsdeltabits[i>>3] |= (1<<(i&7));
2436 if (host_client->stats[i] >= 0 && host_client->stats[i] < 256)
2438 MSG_WriteByte(msg, svc_updatestatubyte);
2439 MSG_WriteByte(msg, i);
2440 MSG_WriteByte(msg, host_client->stats[i]);
2444 MSG_WriteByte(msg, svc_updatestat);
2445 MSG_WriteByte(msg, i);
2446 MSG_WriteLong(msg, host_client->stats[i]);
2451 // write state updates
2452 if (developer_networkentities.integer >= 10)
2453 Con_Printf("send: svc_entities %i\n", framenum);
2454 d->latestframenum = framenum;
2455 MSG_WriteByte(msg, svc_entities);
2456 MSG_WriteLong(msg, framenum);
2457 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)
2458 MSG_WriteLong(msg, movesequence);
2459 for (priority = ENTITYFRAME5_PRIORITYLEVELS - 1;priority >= 0 && packetlog->numstates < ENTITYFRAME5_MAXSTATES;priority--)
2461 for (i = 0;i < d->prioritychaincounts[priority] && packetlog->numstates < ENTITYFRAME5_MAXSTATES;i++)
2463 num = d->prioritychains[priority][i];
2464 n = d->states + num;
2465 if (d->deltabits[num] & E5_FULLUPDATE)
2466 d->deltabits[num] = E5_FULLUPDATE | EntityState5_DeltaBits(&defaultstate, n);
2468 EntityState5_WriteUpdate(num, n, d->deltabits[num], &buf);
2469 // if the entity won't fit, try the next one
2470 if (msg->cursize + buf.cursize + 2 > maxsize)
2472 // write entity to the packet
2473 SZ_Write(msg, buf.data, buf.cursize);
2474 // mark age on entity for prioritization
2475 d->updateframenum[num] = framenum;
2476 // log entity so deltabits can be restored later if lost
2477 packetlog->states[packetlog->numstates].number = num;
2478 packetlog->states[packetlog->numstates].bits = d->deltabits[num];
2479 packetlog->numstates++;
2480 // clear deltabits and priority so it won't be sent again
2481 d->deltabits[num] = 0;
2482 d->priorities[num] = 0;
2485 MSG_WriteShort(msg, 0x8000);
2489 static void QW_TranslateEffects(entity_state_t *s, int qweffects)
2492 s->internaleffects = 0;
2493 if (qweffects & QW_EF_BRIGHTFIELD)
2494 s->effects |= EF_BRIGHTFIELD;
2495 if (qweffects & QW_EF_MUZZLEFLASH)
2496 s->effects |= EF_MUZZLEFLASH;
2497 if (qweffects & QW_EF_FLAG1)
2499 // mimic FTEQW's interpretation of EF_FLAG1 as EF_NODRAW on non-player entities
2500 if (s->number > cl.maxclients)
2501 s->effects |= EF_NODRAW;
2503 s->internaleffects |= INTEF_FLAG1QW;
2505 if (qweffects & QW_EF_FLAG2)
2507 // mimic FTEQW's interpretation of EF_FLAG2 as EF_ADDITIVE on non-player entities
2508 if (s->number > cl.maxclients)
2509 s->effects |= EF_ADDITIVE;
2511 s->internaleffects |= INTEF_FLAG2QW;
2513 if (qweffects & QW_EF_RED)
2515 if (qweffects & QW_EF_BLUE)
2516 s->effects |= EF_RED | EF_BLUE;
2518 s->effects |= EF_RED;
2520 else if (qweffects & QW_EF_BLUE)
2521 s->effects |= EF_BLUE;
2522 else if (qweffects & QW_EF_BRIGHTLIGHT)
2523 s->effects |= EF_BRIGHTLIGHT;
2524 else if (qweffects & QW_EF_DIMLIGHT)
2525 s->effects |= EF_DIMLIGHT;
2528 void EntityStateQW_ReadPlayerUpdate(void)
2530 int slot = MSG_ReadByte();
2531 int enumber = slot + 1;
2537 // look up the entity
2538 entity_t *ent = cl.entities + enumber;
2542 // slide the current state into the previous
2543 ent->state_previous = ent->state_current;
2546 s = &ent->state_current;
2549 s->number = enumber;
2550 s->colormap = enumber;
2551 playerflags = MSG_ReadShort();
2552 MSG_ReadVector(s->origin, cls.protocol);
2553 s->frame = MSG_ReadByte();
2555 VectorClear(viewangles);
2556 VectorClear(velocity);
2558 if (playerflags & QW_PF_MSEC)
2560 // time difference between last update this player sent to the server,
2561 // and last input we sent to the server (this packet is in response to
2562 // our input, so msec is how long ago the last update of this player
2563 // entity occurred, compared to our input being received)
2564 msec = MSG_ReadByte();
2568 if (playerflags & QW_PF_COMMAND)
2570 bits = MSG_ReadByte();
2571 if (bits & QW_CM_ANGLE1)
2572 viewangles[0] = MSG_ReadAngle16i(); // cmd->angles[0]
2573 if (bits & QW_CM_ANGLE2)
2574 viewangles[1] = MSG_ReadAngle16i(); // cmd->angles[1]
2575 if (bits & QW_CM_ANGLE3)
2576 viewangles[2] = MSG_ReadAngle16i(); // cmd->angles[2]
2577 if (bits & QW_CM_FORWARD)
2578 MSG_ReadShort(); // cmd->forwardmove
2579 if (bits & QW_CM_SIDE)
2580 MSG_ReadShort(); // cmd->sidemove
2581 if (bits & QW_CM_UP)
2582 MSG_ReadShort(); // cmd->upmove
2583 if (bits & QW_CM_BUTTONS)
2584 MSG_ReadByte(); // cmd->buttons
2585 if (bits & QW_CM_IMPULSE)
2586 MSG_ReadByte(); // cmd->impulse
2587 MSG_ReadByte(); // cmd->msec
2589 if (playerflags & QW_PF_VELOCITY1)
2590 velocity[0] = MSG_ReadShort();
2591 if (playerflags & QW_PF_VELOCITY2)
2592 velocity[1] = MSG_ReadShort();
2593 if (playerflags & QW_PF_VELOCITY3)
2594 velocity[2] = MSG_ReadShort();
2595 if (playerflags & QW_PF_MODEL)
2596 s->modelindex = MSG_ReadByte();
2598 s->modelindex = cl.qw_modelindex_player;
2599 if (playerflags & QW_PF_SKINNUM)
2600 s->skin = MSG_ReadByte();
2601 if (playerflags & QW_PF_EFFECTS)
2602 QW_TranslateEffects(s, MSG_ReadByte());
2603 if (playerflags & QW_PF_WEAPONFRAME)
2604 weaponframe = MSG_ReadByte();
2608 if (enumber == cl.playerentity)
2610 // if this is an update on our player, update the angles
2611 VectorCopy(cl.viewangles, viewangles);
2614 // calculate the entity angles from the viewangles
2615 s->angles[0] = viewangles[0] * -0.0333;
2616 s->angles[1] = viewangles[1];
2618 s->angles[2] = V_CalcRoll(s->angles, velocity)*4;
2620 // if this is an update on our player, update interpolation state
2621 if (enumber == cl.playerentity)
2623 VectorCopy (cl.mpunchangle[0], cl.mpunchangle[1]);
2624 VectorCopy (cl.mpunchvector[0], cl.mpunchvector[1]);
2625 VectorCopy (cl.mvelocity[0], cl.mvelocity[1]);
2626 cl.mviewzoom[1] = cl.mviewzoom[0];
2629 cl.mpunchangle[0][0] = 0;
2630 cl.mpunchangle[0][1] = 0;
2631 cl.mpunchangle[0][2] = 0;
2632 cl.mpunchvector[0][0] = 0;
2633 cl.mpunchvector[0][1] = 0;
2634 cl.mpunchvector[0][2] = 0;
2635 cl.mvelocity[0][0] = 0;
2636 cl.mvelocity[0][1] = 0;
2637 cl.mvelocity[0][2] = 0;
2638 cl.mviewzoom[0] = 1;
2640 VectorCopy(velocity, cl.mvelocity[0]);
2641 cl.stats[STAT_WEAPONFRAME] = weaponframe;
2642 if (playerflags & QW_PF_GIB)
2643 cl.stats[STAT_VIEWHEIGHT] = 8;
2644 else if (playerflags & QW_PF_DEAD)
2645 cl.stats[STAT_VIEWHEIGHT] = -16;
2647 cl.stats[STAT_VIEWHEIGHT] = 22;
2650 // set the cl.entities_active flag
2651 cl.entities_active[enumber] = s->active;
2652 // set the update time
2653 s->time = cl.mtime[0] - msec * 0.001; // qw has no clock
2654 // check if we need to update the lerp stuff
2656 CL_MoveLerpEntityStates(&cl.entities[enumber]);
2659 static void EntityStateQW_ReadEntityUpdate(entity_state_t *s, int bits)
2663 s->number = bits & 511;
2665 if (bits & QW_U_MOREBITS)
2666 bits |= MSG_ReadByte();
2668 // store the QW_U_SOLID bit here?
2670 if (bits & QW_U_MODEL)
2671 s->modelindex = MSG_ReadByte();
2672 if (bits & QW_U_FRAME)
2673 s->frame = MSG_ReadByte();
2674 if (bits & QW_U_COLORMAP)
2675 s->colormap = MSG_ReadByte();
2676 if (bits & QW_U_SKIN)
2677 s->skin = MSG_ReadByte();
2678 if (bits & QW_U_EFFECTS)
2679 QW_TranslateEffects(s, qweffects = MSG_ReadByte());
2680 if (bits & QW_U_ORIGIN1)
2681 s->origin[0] = MSG_ReadCoord13i();
2682 if (bits & QW_U_ANGLE1)
2683 s->angles[0] = MSG_ReadAngle8i();
2684 if (bits & QW_U_ORIGIN2)
2685 s->origin[1] = MSG_ReadCoord13i();
2686 if (bits & QW_U_ANGLE2)
2687 s->angles[1] = MSG_ReadAngle8i();
2688 if (bits & QW_U_ORIGIN3)
2689 s->origin[2] = MSG_ReadCoord13i();
2690 if (bits & QW_U_ANGLE3)
2691 s->angles[2] = MSG_ReadAngle8i();
2693 if (developer_networkentities.integer >= 2)
2695 Con_Printf("ReadFields e%i", s->number);
2696 if (bits & QW_U_MODEL)
2697 Con_Printf(" U_MODEL %i", s->modelindex);
2698 if (bits & QW_U_FRAME)
2699 Con_Printf(" U_FRAME %i", s->frame);
2700 if (bits & QW_U_COLORMAP)
2701 Con_Printf(" U_COLORMAP %i", s->colormap);
2702 if (bits & QW_U_SKIN)
2703 Con_Printf(" U_SKIN %i", s->skin);
2704 if (bits & QW_U_EFFECTS)
2705 Con_Printf(" U_EFFECTS %i", qweffects);
2706 if (bits & QW_U_ORIGIN1)
2707 Con_Printf(" U_ORIGIN1 %f", s->origin[0]);
2708 if (bits & QW_U_ANGLE1)
2709 Con_Printf(" U_ANGLE1 %f", s->angles[0]);
2710 if (bits & QW_U_ORIGIN2)
2711 Con_Printf(" U_ORIGIN2 %f", s->origin[1]);
2712 if (bits & QW_U_ANGLE2)
2713 Con_Printf(" U_ANGLE2 %f", s->angles[1]);
2714 if (bits & QW_U_ORIGIN3)
2715 Con_Printf(" U_ORIGIN3 %f", s->origin[2]);
2716 if (bits & QW_U_ANGLE3)
2717 Con_Printf(" U_ANGLE3 %f", s->angles[2]);
2718 if (bits & QW_U_SOLID)
2719 Con_Printf(" U_SOLID");
2724 entityframeqw_database_t *EntityFrameQW_AllocDatabase(mempool_t *pool)
2726 entityframeqw_database_t *d;
2727 d = (entityframeqw_database_t *)Mem_Alloc(pool, sizeof(*d));
2731 void EntityFrameQW_FreeDatabase(entityframeqw_database_t *d)
2736 void EntityFrameQW_CL_ReadFrame(qboolean delta)
2738 qboolean invalid = false;
2739 int number, oldsnapindex, newsnapindex, oldindex, newindex, oldnum, newnum;
2741 entityframeqw_database_t *d;
2742 entityframeqw_snapshot_t *oldsnap, *newsnap;
2744 if (!cl.entitydatabaseqw)
2745 cl.entitydatabaseqw = EntityFrameQW_AllocDatabase(cls.levelmempool);
2746 d = cl.entitydatabaseqw;
2748 // there is no cls.netcon in demos, so this reading code can't access
2749 // cls.netcon-> at all... so cls.qw_incoming_sequence and
2750 // cls.qw_outgoing_sequence are updated every time the corresponding
2751 // cls.netcon->qw. variables are updated
2752 // read the number of this frame to echo back in next input packet
2753 cl.qw_validsequence = cls.qw_incoming_sequence;
2754 newsnapindex = cl.qw_validsequence & QW_UPDATE_MASK;
2755 newsnap = d->snapshot + newsnapindex;
2756 memset(newsnap, 0, sizeof(*newsnap));
2761 number = MSG_ReadByte();
2762 oldsnapindex = cl.qw_deltasequence[newsnapindex];
2763 if ((number & QW_UPDATE_MASK) != (oldsnapindex & QW_UPDATE_MASK))
2764 Con_DPrintf("WARNING: from mismatch\n");
2765 if (oldsnapindex != -1)
2767 if (cls.qw_outgoing_sequence - oldsnapindex >= QW_UPDATE_BACKUP-1)
2769 Con_DPrintf("delta update too old\n");
2770 newsnap->invalid = invalid = true; // too old
2773 oldsnap = d->snapshot + (oldsnapindex & QW_UPDATE_MASK);
2779 // if we can't decode this frame properly, report that to the server
2781 cl.qw_validsequence = 0;
2783 // read entity numbers until we find a 0x0000
2784 // (which would be an empty update on world entity, but is actually a terminator)
2785 newsnap->num_entities = 0;
2789 int word = (unsigned short)MSG_ReadShort();
2791 return; // just return, the main parser will print an error
2792 newnum = word == 0 ? 512 : (word & 511);
2793 oldnum = delta ? (oldindex >= oldsnap->num_entities ? 9999 : oldsnap->entities[oldindex].number) : 9999;
2795 // copy unmodified oldsnap entities
2796 while (newnum > oldnum) // delta only
2798 if (developer_networkentities.integer >= 2)
2799 Con_Printf("copy %i\n", oldnum);
2800 // copy one of the old entities
2801 if (newsnap->num_entities >= QW_MAX_PACKET_ENTITIES)
2802 Host_Error("EntityFrameQW_CL_ReadFrame: newsnap->num_entities == MAX_PACKETENTITIES");
2803 newsnap->entities[newsnap->num_entities] = oldsnap->entities[oldindex++];
2804 newsnap->num_entities++;
2805 oldnum = oldindex >= oldsnap->num_entities ? 9999 : oldsnap->entities[oldindex].number;
2811 if (developer_networkentities.integer >= 2)
2813 if (word & QW_U_REMOVE)
2814 Con_Printf("remove %i\n", newnum);
2815 else if (newnum == oldnum)
2816 Con_Printf("delta %i\n", newnum);
2818 Con_Printf("baseline %i\n", newnum);
2821 if (word & QW_U_REMOVE)
2823 if (newnum != oldnum && !delta && !invalid)
2825 cl.qw_validsequence = 0;
2826 Con_Printf("WARNING: U_REMOVE %i on full update\n", newnum);
2831 if (newsnap->num_entities >= QW_MAX_PACKET_ENTITIES)
2832 Host_Error("EntityFrameQW_CL_ReadFrame: newsnap->num_entities == MAX_PACKETENTITIES");
2833 newsnap->entities[newsnap->num_entities] = (newnum == oldnum) ? oldsnap->entities[oldindex] : cl.entities[newnum].state_baseline;
2834 EntityStateQW_ReadEntityUpdate(newsnap->entities + newsnap->num_entities, word);
2835 newsnap->num_entities++;
2838 if (newnum == oldnum)
2842 // expand cl.num_entities to include every entity we've seen this game
2843 newnum = newsnap->num_entities ? newsnap->entities[newsnap->num_entities - 1].number : 1;
2844 if (cl.num_entities <= newnum)
2846 cl.num_entities = newnum + 1;
2847 if (cl.max_entities < newnum + 1)
2848 CL_ExpandEntities(newnum);
2851 // now update the non-player entities from the snapshot states
2852 number = cl.maxclients + 1;
2853 for (newindex = 0;;newindex++)
2855 newnum = newindex >= newsnap->num_entities ? cl.num_entities : newsnap->entities[newindex].number;
2856 // kill any missing entities
2857 for (;number < newnum;number++)
2859 if (cl.entities_active[number])
2861 cl.entities_active[number] = false;
2862 cl.entities[number].state_current.active = false;
2865 if (number >= cl.num_entities)
2867 // update the entity
2868 ent = &cl.entities[number];
2869 ent->state_previous = ent->state_current;
2870 ent->state_current = newsnap->entities[newindex];
2871 ent->state_current.time = cl.mtime[0];
2872 CL_MoveLerpEntityStates(ent);
2873 // the entity lives again...
2874 cl.entities_active[number] = true;