4 // this is 88 bytes (must match entity_state_t in protocol.h)
5 entity_state_t defaultstate =
7 // ! means this is not sent to client
8 0,//double time; // ! time this state was built (used on client for interpolation)
9 {0,0,0},//float origin[3];
10 {0,0,0},//float angles[3];
11 0,//int number; // entity number this state is for
13 0,//unsigned int customizeentityforclient; // !
14 0,//unsigned short modelindex;
15 0,//unsigned short frame;
16 0,//unsigned short tagentity;
17 0,//unsigned short specialvisibilityradius; // ! larger if it has effects/light
18 0,//unsigned short viewmodelforclient; // !
19 0,//unsigned short exteriormodelforclient; // ! not shown if first person viewing from this entity, shown in all other cases
20 0,//unsigned short nodrawtoclient; // !
21 0,//unsigned short drawonlytoclient; // !
22 {0,0,0,0},//unsigned short light[4]; // color*256 (0.00 to 255.996), and radius*1
23 0,//unsigned char active; // true if a valid state
24 0,//unsigned char lightstyle;
25 0,//unsigned char lightpflags;
26 0,//unsigned char colormap;
27 0,//unsigned char skin; // also chooses cubemap for rtlights if lightpflags & LIGHTPFLAGS_FULLDYNAMIC
28 255,//unsigned char alpha;
29 16,//unsigned char scale;
30 0,//unsigned char glowsize;
31 254,//unsigned char glowcolor;
32 0,//unsigned char flags;
33 0,//unsigned char tagindex;
34 {32, 32, 32},//unsigned char colormod[3];
35 // padding to a multiple of 8 bytes (to align the double time)
36 {0,0,0,0,0,0}//unsigned char unused[6]; // !
39 // LordHavoc: I own protocol ranges 96, 97, 3500-3599
41 struct protocolversioninfo_s
46 protocolversioninfo[] =
57 {250, "NEHAHRAMOVIE"},
62 protocolversion_t Protocol_EnumForName(const char *s)
65 for (i = 1;protocolversioninfo[i].name;i++)
66 if (!strcasecmp(s, protocolversioninfo[i].name))
67 return (protocolversion_t)i;
68 return PROTOCOL_UNKNOWN;
71 const char *Protocol_NameForEnum(protocolversion_t p)
73 return protocolversioninfo[p].name;
76 protocolversion_t Protocol_EnumForNumber(int n)
79 for (i = 1;protocolversioninfo[i].name;i++)
80 if (protocolversioninfo[i].number == n)
81 return (protocolversion_t)i;
82 return PROTOCOL_UNKNOWN;
85 int Protocol_NumberForEnum(protocolversion_t p)
87 return protocolversioninfo[p].number;
90 void Protocol_Names(char *buffer, size_t buffersize)
96 for (i = 1;protocolversioninfo[i].name;i++)
99 strlcat(buffer, " ", sizeof(buffer));
100 strlcat(buffer, protocolversioninfo[i].name, sizeof(buffer));
104 // keep track of quake entities because they need to be killed if they get stale
105 int cl_lastquakeentity = 0;
106 unsigned char cl_isquakeentity[MAX_EDICTS];
108 void EntityFrameQuake_ReadEntity(int bits)
114 if (bits & U_MOREBITS)
115 bits |= (MSG_ReadByte()<<8);
116 if ((bits & U_EXTEND1) && cl.protocol != PROTOCOL_NEHAHRAMOVIE)
118 bits |= MSG_ReadByte() << 16;
119 if (bits & U_EXTEND2)
120 bits |= MSG_ReadByte() << 24;
123 if (bits & U_LONGENTITY)
124 num = (unsigned short) MSG_ReadShort ();
126 num = MSG_ReadByte ();
128 if (num >= MAX_EDICTS)
129 Host_Error("EntityFrameQuake_ReadEntity: entity number (%i) >= MAX_EDICTS (%i)", num, MAX_EDICTS);
131 Host_Error("EntityFrameQuake_ReadEntity: invalid entity number (%i)", num);
133 if (cl_num_entities <= num)
135 cl_num_entities = num + 1;
136 if (num >= cl_max_entities)
137 CL_ExpandEntities(num);
140 ent = cl_entities + num;
142 // note: this inherits the 'active' state of the baseline chosen
143 // (state_baseline is always active, state_current may not be active if
144 // the entity was missing in the last frame)
146 s = ent->state_current;
149 s = ent->state_baseline;
153 cl_isquakeentity[num] = true;
154 if (cl_lastquakeentity < num)
155 cl_lastquakeentity = num;
157 s.time = cl.mtime[0];
159 if (bits & U_MODEL) s.modelindex = (s.modelindex & 0xFF00) | MSG_ReadByte();
160 if (bits & U_FRAME) s.frame = (s.frame & 0xFF00) | MSG_ReadByte();
161 if (bits & U_COLORMAP) s.colormap = MSG_ReadByte();
162 if (bits & U_SKIN) s.skin = MSG_ReadByte();
163 if (bits & U_EFFECTS) s.effects = (s.effects & 0xFF00) | MSG_ReadByte();
164 if (bits & U_ORIGIN1) s.origin[0] = MSG_ReadCoord(cl.protocol);
165 if (bits & U_ANGLE1) s.angles[0] = MSG_ReadAngle(cl.protocol);
166 if (bits & U_ORIGIN2) s.origin[1] = MSG_ReadCoord(cl.protocol);
167 if (bits & U_ANGLE2) s.angles[1] = MSG_ReadAngle(cl.protocol);
168 if (bits & U_ORIGIN3) s.origin[2] = MSG_ReadCoord(cl.protocol);
169 if (bits & U_ANGLE3) s.angles[2] = MSG_ReadAngle(cl.protocol);
170 if (bits & U_STEP) s.flags |= RENDER_STEP;
171 if (bits & U_ALPHA) s.alpha = MSG_ReadByte();
172 if (bits & U_SCALE) s.scale = MSG_ReadByte();
173 if (bits & U_EFFECTS2) s.effects = (s.effects & 0x00FF) | (MSG_ReadByte() << 8);
174 if (bits & U_GLOWSIZE) s.glowsize = MSG_ReadByte();
175 if (bits & U_GLOWCOLOR) s.glowcolor = MSG_ReadByte();
176 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));}
177 if (bits & U_GLOWTRAIL) s.flags |= RENDER_GLOWTRAIL;
178 if (bits & U_FRAME2) s.frame = (s.frame & 0x00FF) | (MSG_ReadByte() << 8);
179 if (bits & U_MODEL2) s.modelindex = (s.modelindex & 0x00FF) | (MSG_ReadByte() << 8);
180 if (bits & U_VIEWMODEL) s.flags |= RENDER_VIEWMODEL;
181 if (bits & U_EXTERIORMODEL) s.flags |= RENDER_EXTERIORMODEL;
183 // LordHavoc: to allow playback of the Nehahra movie
184 if (cl.protocol == PROTOCOL_NEHAHRAMOVIE && (bits & U_EXTEND1))
186 // LordHavoc: evil format
187 int i = MSG_ReadFloat();
188 int j = MSG_ReadFloat() * 255.0f;
193 s.effects |= EF_FULLBRIGHT;
197 else if (j == 0 || j >= 255)
203 ent->state_previous = ent->state_current;
204 ent->state_current = s;
205 if (ent->state_current.active)
207 CL_MoveLerpEntityStates(ent);
208 cl_entities_active[ent->state_current.number] = true;
212 Host_Error("EntityFrameQuake_ReadEntity: read error");
215 void EntityFrameQuake_ISeeDeadEntities(void)
218 if (cl_lastquakeentity == 0)
220 lastentity = cl_lastquakeentity;
221 cl_lastquakeentity = 0;
222 for (num = 0;num <= lastentity;num++)
224 if (cl_isquakeentity[num])
226 if (cl_entities_active[num] && cl_entities[num].state_current.time == cl.mtime[0])
228 cl_isquakeentity[num] = true;
229 cl_lastquakeentity = num;
233 cl_isquakeentity[num] = false;
234 cl_entities_active[num] = false;
235 cl_entities[num].state_current = defaultstate;
236 cl_entities[num].state_current.number = num;
242 static mempool_t *sv2csqc = NULL;
244 sizebuf_t *sv2csqcbuf = NULL;
245 static unsigned char *sv2csqcents_version[64];
247 void EntityFrameCSQC_ClearVersions (void)
251 Mem_FreePool(&sv2csqc);
254 memset(sv2csqcents_version, 0, 64*sizeof(unsigned char *));
257 void EntityFrameCSQC_InitClientVersions (int client, qboolean clear)
260 sv2csqc = Mem_AllocPool("SV2CSQC", 0, NULL);
261 if(sv2csqcents_version[client])
263 Mem_Free(sv2csqcents_version[client]);
264 sv2csqcents_version[client] = NULL;
266 sv2csqcents_version[client] = Mem_Alloc(sv2csqc, MAX_EDICTS);
267 memset(sv2csqcents_version[client], 0, MAX_EDICTS);
270 //[515]: we use only one array per-client for SendEntity feature
271 void EntityFrameCSQC_WriteFrame (sizebuf_t *msg, int numstates, const entity_state_t *states)
274 unsigned char data[2048];
275 const entity_state_t *s;
276 unsigned short i, t, t2, t0;
277 prvm_eval_t *val, *val2;
280 if(!eval_SendEntity || !eval_Version)
283 if(!sv2csqcents_version[csqc_clent])
284 EntityFrameCSQC_InitClientVersions(csqc_clent, false);
286 for (csqcents = i = 0, s = states;i < numstates;i++, s++)
288 //[515]: entities remove
290 t2 = prog->num_edicts;
292 t2 = states[i+1].number;
301 if(sv2csqcents_version[csqc_clent][t])
306 memset(&buf, 0, sizeof(buf));
308 buf.maxsize = sizeof(data);
311 MSG_WriteByte(&buf, svc_csqcentities);
313 sv2csqcents_version[csqc_clent][t] = 0;
314 MSG_WriteShort(&buf, (unsigned short)t | 0x8000);
317 //[515]: entities remove
321 val = PRVM_GETEDICTFIELDVALUE((&prog->edicts[s->number]), eval_SendEntity);
324 val2 = PRVM_GETEDICTFIELDVALUE((&prog->edicts[s->number]), eval_Version);
325 if(sv2csqcents_version[csqc_clent][s->number] == (unsigned char)val2->_float)
330 memset(&buf, 0, sizeof(buf));
332 buf.maxsize = sizeof(data);
335 MSG_WriteByte(&buf, svc_csqcentities);
337 if((unsigned char)val2->_float == 0)
339 MSG_WriteShort(&buf, s->number);
340 ((int *)prog->globals.generic)[OFS_PARM0] = csqc_clent+1;
341 prog->globals.server->self = s->number;
342 PRVM_ExecuteProgram(val->function, "Null SendEntity\n");
343 if(!prog->globals.generic[OFS_RETURN])
346 if(sv2csqcents_version[csqc_clent][s->number])
348 sv2csqcents_version[csqc_clent][s->number] = 0;
349 MSG_WriteShort(&buf, (unsigned short)s->number | 0x8000);
355 sv2csqcents_version[csqc_clent][s->number] = (unsigned char)val2->_float;
358 if (msg->cursize + buf.cursize > msg->maxsize)
366 MSG_WriteShort(&buf, 0);
367 SZ_Write(msg, buf.data, buf.cursize);
373 void EntityFrameQuake_WriteFrame(sizebuf_t *msg, int numstates, const entity_state_t *states)
375 const entity_state_t *s;
376 entity_state_t baseline;
379 unsigned char data[128];
382 // prepare the buffer
383 memset(&buf, 0, sizeof(buf));
385 buf.maxsize = sizeof(data);
387 for (i = 0, s = states;i < numstates;i++, s++)
389 val = PRVM_GETEDICTFIELDVALUE((&prog->edicts[s->number]), eval_SendEntity);
390 if(val && val->function)
393 // prepare the buffer
398 if (s->number >= 256)
399 bits |= U_LONGENTITY;
400 if (s->flags & RENDER_STEP)
402 if (s->flags & RENDER_VIEWMODEL)
404 if (s->flags & RENDER_GLOWTRAIL)
406 if (s->flags & RENDER_EXTERIORMODEL)
407 bits |= U_EXTERIORMODEL;
409 // LordHavoc: old stuff, but rewritten to have more exact tolerances
410 baseline = prog->edicts[s->number].priv.server->baseline;
411 if (baseline.origin[0] != s->origin[0])
413 if (baseline.origin[1] != s->origin[1])
415 if (baseline.origin[2] != s->origin[2])
417 if (baseline.angles[0] != s->angles[0])
419 if (baseline.angles[1] != s->angles[1])
421 if (baseline.angles[2] != s->angles[2])
423 if (baseline.colormap != s->colormap)
425 if (baseline.skin != s->skin)
427 if (baseline.frame != s->frame)
430 if (s->frame & 0xFF00)
433 if (baseline.effects != s->effects)
436 if (s->effects & 0xFF00)
439 if (baseline.modelindex != s->modelindex)
442 if (s->modelindex & 0xFF00)
445 if (baseline.alpha != s->alpha)
447 if (baseline.scale != s->scale)
449 if (baseline.glowsize != s->glowsize)
451 if (baseline.glowcolor != s->glowcolor)
454 // if extensions are disabled, clear the relevant update flags
455 if (sv.protocol == PROTOCOL_QUAKE || sv.protocol == PROTOCOL_NEHAHRAMOVIE)
457 if (sv.protocol == PROTOCOL_NEHAHRAMOVIE)
458 if (s->alpha != 255 || s->effects & EF_FULLBRIGHT)
462 if (bits >= 16777216)
470 MSG_WriteByte (&buf, bits);
471 if (bits & U_MOREBITS) MSG_WriteByte(&buf, bits>>8);
472 if (bits & U_EXTEND1) MSG_WriteByte(&buf, bits>>16);
473 if (bits & U_EXTEND2) MSG_WriteByte(&buf, bits>>24);
474 if (bits & U_LONGENTITY) MSG_WriteShort(&buf, s->number);
475 else MSG_WriteByte(&buf, s->number);
477 if (bits & U_MODEL) MSG_WriteByte(&buf, s->modelindex);
478 if (bits & U_FRAME) MSG_WriteByte(&buf, s->frame);
479 if (bits & U_COLORMAP) MSG_WriteByte(&buf, s->colormap);
480 if (bits & U_SKIN) MSG_WriteByte(&buf, s->skin);
481 if (bits & U_EFFECTS) MSG_WriteByte(&buf, s->effects);
482 if (bits & U_ORIGIN1) MSG_WriteCoord(&buf, s->origin[0], sv.protocol);
483 if (bits & U_ANGLE1) MSG_WriteAngle(&buf, s->angles[0], sv.protocol);
484 if (bits & U_ORIGIN2) MSG_WriteCoord(&buf, s->origin[1], sv.protocol);
485 if (bits & U_ANGLE2) MSG_WriteAngle(&buf, s->angles[1], sv.protocol);
486 if (bits & U_ORIGIN3) MSG_WriteCoord(&buf, s->origin[2], sv.protocol);
487 if (bits & U_ANGLE3) MSG_WriteAngle(&buf, s->angles[2], sv.protocol);
488 if (bits & U_ALPHA) MSG_WriteByte(&buf, s->alpha);
489 if (bits & U_SCALE) MSG_WriteByte(&buf, s->scale);
490 if (bits & U_EFFECTS2) MSG_WriteByte(&buf, s->effects >> 8);
491 if (bits & U_GLOWSIZE) MSG_WriteByte(&buf, s->glowsize);
492 if (bits & U_GLOWCOLOR) MSG_WriteByte(&buf, s->glowcolor);
493 if (bits & U_COLORMOD) {int c = ((int)bound(0, s->colormod[0] * (7.0f / 32.0f), 7) << 5) | ((int)bound(0, s->colormod[0] * (7.0f / 32.0f), 7) << 2) | ((int)bound(0, s->colormod[0] * (3.0f / 32.0f), 3) << 0);MSG_WriteByte(&buf, c);}
494 if (bits & U_FRAME2) MSG_WriteByte(&buf, s->frame >> 8);
495 if (bits & U_MODEL2) MSG_WriteByte(&buf, s->modelindex >> 8);
497 // the nasty protocol
498 if ((bits & U_EXTEND1) && sv.protocol == PROTOCOL_NEHAHRAMOVIE)
500 if (s->effects & EF_FULLBRIGHT)
502 MSG_WriteFloat(&buf, 2); // QSG protocol version
503 MSG_WriteFloat(&buf, s->alpha <= 0 ? 0 : (s->alpha >= 255 ? 1 : s->alpha * (1.0f / 255.0f))); // alpha
504 MSG_WriteFloat(&buf, 1); // fullbright
508 MSG_WriteFloat(&buf, 1); // QSG protocol version
509 MSG_WriteFloat(&buf, s->alpha <= 0 ? 0 : (s->alpha >= 255 ? 1 : s->alpha * (1.0f / 255.0f))); // alpha
513 // if the commit is full, we're done this frame
514 if (msg->cursize + buf.cursize > msg->maxsize)
516 // next frame we will continue where we left off
519 // write the message to the packet
520 SZ_Write(msg, buf.data, buf.cursize);
524 int EntityState_DeltaBits(const entity_state_t *o, const entity_state_t *n)
527 // if o is not active, delta from default
531 if (fabs(n->origin[0] - o->origin[0]) > (1.0f / 256.0f))
533 if (fabs(n->origin[1] - o->origin[1]) > (1.0f / 256.0f))
535 if (fabs(n->origin[2] - o->origin[2]) > (1.0f / 256.0f))
537 if ((unsigned char) (n->angles[0] * (256.0f / 360.0f)) != (unsigned char) (o->angles[0] * (256.0f / 360.0f)))
539 if ((unsigned char) (n->angles[1] * (256.0f / 360.0f)) != (unsigned char) (o->angles[1] * (256.0f / 360.0f)))
541 if ((unsigned char) (n->angles[2] * (256.0f / 360.0f)) != (unsigned char) (o->angles[2] * (256.0f / 360.0f)))
543 if ((n->modelindex ^ o->modelindex) & 0x00FF)
545 if ((n->modelindex ^ o->modelindex) & 0xFF00)
547 if ((n->frame ^ o->frame) & 0x00FF)
549 if ((n->frame ^ o->frame) & 0xFF00)
551 if ((n->effects ^ o->effects) & 0x00FF)
553 if ((n->effects ^ o->effects) & 0xFF00)
555 if (n->colormap != o->colormap)
557 if (n->skin != o->skin)
559 if (n->alpha != o->alpha)
561 if (n->scale != o->scale)
563 if (n->glowsize != o->glowsize)
565 if (n->glowcolor != o->glowcolor)
567 if (n->flags != o->flags)
569 if (n->tagindex != o->tagindex || n->tagentity != o->tagentity)
570 bits |= E_TAGATTACHMENT;
571 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])
573 if (n->lightstyle != o->lightstyle)
574 bits |= E_LIGHTSTYLE;
575 if (n->lightpflags != o->lightpflags)
576 bits |= E_LIGHTPFLAGS;
580 if (bits & 0xFF000000)
582 if (bits & 0x00FF0000)
584 if (bits & 0x0000FF00)
590 void EntityState_WriteExtendBits(sizebuf_t *msg, unsigned int bits)
592 MSG_WriteByte(msg, bits & 0xFF);
593 if (bits & 0x00000080)
595 MSG_WriteByte(msg, (bits >> 8) & 0xFF);
596 if (bits & 0x00008000)
598 MSG_WriteByte(msg, (bits >> 16) & 0xFF);
599 if (bits & 0x00800000)
600 MSG_WriteByte(msg, (bits >> 24) & 0xFF);
605 void EntityState_WriteFields(const entity_state_t *ent, sizebuf_t *msg, unsigned int bits)
607 if (sv.protocol == PROTOCOL_DARKPLACES2)
609 if (bits & E_ORIGIN1)
610 MSG_WriteCoord16i(msg, ent->origin[0]);
611 if (bits & E_ORIGIN2)
612 MSG_WriteCoord16i(msg, ent->origin[1]);
613 if (bits & E_ORIGIN3)
614 MSG_WriteCoord16i(msg, ent->origin[2]);
618 // LordHavoc: have to write flags first, as they can modify protocol
620 MSG_WriteByte(msg, ent->flags);
621 if (ent->flags & RENDER_LOWPRECISION)
623 if (bits & E_ORIGIN1)
624 MSG_WriteCoord16i(msg, ent->origin[0]);
625 if (bits & E_ORIGIN2)
626 MSG_WriteCoord16i(msg, ent->origin[1]);
627 if (bits & E_ORIGIN3)
628 MSG_WriteCoord16i(msg, ent->origin[2]);
632 if (bits & E_ORIGIN1)
633 MSG_WriteCoord32f(msg, ent->origin[0]);
634 if (bits & E_ORIGIN2)
635 MSG_WriteCoord32f(msg, ent->origin[1]);
636 if (bits & E_ORIGIN3)
637 MSG_WriteCoord32f(msg, ent->origin[2]);
640 if ((sv.protocol == PROTOCOL_DARKPLACES1 || sv.protocol == PROTOCOL_DARKPLACES2 || sv.protocol == PROTOCOL_DARKPLACES3 || sv.protocol == PROTOCOL_DARKPLACES4) && (ent->flags & RENDER_LOWPRECISION))
643 MSG_WriteAngle8i(msg, ent->angles[0]);
645 MSG_WriteAngle8i(msg, ent->angles[1]);
647 MSG_WriteAngle8i(msg, ent->angles[2]);
652 MSG_WriteAngle16i(msg, ent->angles[0]);
654 MSG_WriteAngle16i(msg, ent->angles[1]);
656 MSG_WriteAngle16i(msg, ent->angles[2]);
659 MSG_WriteByte(msg, ent->modelindex & 0xFF);
661 MSG_WriteByte(msg, (ent->modelindex >> 8) & 0xFF);
663 MSG_WriteByte(msg, ent->frame & 0xFF);
665 MSG_WriteByte(msg, (ent->frame >> 8) & 0xFF);
666 if (bits & E_EFFECTS1)
667 MSG_WriteByte(msg, ent->effects & 0xFF);
668 if (bits & E_EFFECTS2)
669 MSG_WriteByte(msg, (ent->effects >> 8) & 0xFF);
670 if (bits & E_COLORMAP)
671 MSG_WriteByte(msg, ent->colormap);
673 MSG_WriteByte(msg, ent->skin);
675 MSG_WriteByte(msg, ent->alpha);
677 MSG_WriteByte(msg, ent->scale);
678 if (bits & E_GLOWSIZE)
679 MSG_WriteByte(msg, ent->glowsize);
680 if (bits & E_GLOWCOLOR)
681 MSG_WriteByte(msg, ent->glowcolor);
682 if (sv.protocol == PROTOCOL_DARKPLACES2)
684 MSG_WriteByte(msg, ent->flags);
685 if (bits & E_TAGATTACHMENT)
687 MSG_WriteShort(msg, ent->tagentity);
688 MSG_WriteByte(msg, ent->tagindex);
692 MSG_WriteShort(msg, ent->light[0]);
693 MSG_WriteShort(msg, ent->light[1]);
694 MSG_WriteShort(msg, ent->light[2]);
695 MSG_WriteShort(msg, ent->light[3]);
697 if (bits & E_LIGHTSTYLE)
698 MSG_WriteByte(msg, ent->lightstyle);
699 if (bits & E_LIGHTPFLAGS)
700 MSG_WriteByte(msg, ent->lightpflags);
703 void EntityState_WriteUpdate(const entity_state_t *ent, sizebuf_t *msg, const entity_state_t *delta)
708 // entity is active, check for changes from the delta
709 if ((bits = EntityState_DeltaBits(delta, ent)))
711 // write the update number, bits, and fields
712 MSG_WriteShort(msg, ent->number);
713 EntityState_WriteExtendBits(msg, bits);
714 EntityState_WriteFields(ent, msg, bits);
719 // entity is inactive, check if the delta was active
722 // write the remove number
723 MSG_WriteShort(msg, ent->number | 0x8000);
728 int EntityState_ReadExtendBits(void)
731 bits = MSG_ReadByte();
732 if (bits & 0x00000080)
734 bits |= MSG_ReadByte() << 8;
735 if (bits & 0x00008000)
737 bits |= MSG_ReadByte() << 16;
738 if (bits & 0x00800000)
739 bits |= MSG_ReadByte() << 24;
745 void EntityState_ReadFields(entity_state_t *e, unsigned int bits)
747 if (cl.protocol == PROTOCOL_DARKPLACES2)
749 if (bits & E_ORIGIN1)
750 e->origin[0] = MSG_ReadCoord16i();
751 if (bits & E_ORIGIN2)
752 e->origin[1] = MSG_ReadCoord16i();
753 if (bits & E_ORIGIN3)
754 e->origin[2] = MSG_ReadCoord16i();
759 e->flags = MSG_ReadByte();
760 if (e->flags & RENDER_LOWPRECISION)
762 if (bits & E_ORIGIN1)
763 e->origin[0] = MSG_ReadCoord16i();
764 if (bits & E_ORIGIN2)
765 e->origin[1] = MSG_ReadCoord16i();
766 if (bits & E_ORIGIN3)
767 e->origin[2] = MSG_ReadCoord16i();
771 if (bits & E_ORIGIN1)
772 e->origin[0] = MSG_ReadCoord32f();
773 if (bits & E_ORIGIN2)
774 e->origin[1] = MSG_ReadCoord32f();
775 if (bits & E_ORIGIN3)
776 e->origin[2] = MSG_ReadCoord32f();
779 if ((cl.protocol == PROTOCOL_DARKPLACES5 || cl.protocol == PROTOCOL_DARKPLACES6) && !(e->flags & RENDER_LOWPRECISION))
782 e->angles[0] = MSG_ReadAngle16i();
784 e->angles[1] = MSG_ReadAngle16i();
786 e->angles[2] = MSG_ReadAngle16i();
791 e->angles[0] = MSG_ReadAngle8i();
793 e->angles[1] = MSG_ReadAngle8i();
795 e->angles[2] = MSG_ReadAngle8i();
798 e->modelindex = (e->modelindex & 0xFF00) | (unsigned int) MSG_ReadByte();
800 e->modelindex = (e->modelindex & 0x00FF) | ((unsigned int) MSG_ReadByte() << 8);
802 e->frame = (e->frame & 0xFF00) | (unsigned int) MSG_ReadByte();
804 e->frame = (e->frame & 0x00FF) | ((unsigned int) MSG_ReadByte() << 8);
805 if (bits & E_EFFECTS1)
806 e->effects = (e->effects & 0xFF00) | (unsigned int) MSG_ReadByte();
807 if (bits & E_EFFECTS2)
808 e->effects = (e->effects & 0x00FF) | ((unsigned int) MSG_ReadByte() << 8);
809 if (bits & E_COLORMAP)
810 e->colormap = MSG_ReadByte();
812 e->skin = MSG_ReadByte();
814 e->alpha = MSG_ReadByte();
816 e->scale = MSG_ReadByte();
817 if (bits & E_GLOWSIZE)
818 e->glowsize = MSG_ReadByte();
819 if (bits & E_GLOWCOLOR)
820 e->glowcolor = MSG_ReadByte();
821 if (cl.protocol == PROTOCOL_DARKPLACES2)
823 e->flags = MSG_ReadByte();
824 if (bits & E_TAGATTACHMENT)
826 e->tagentity = (unsigned short) MSG_ReadShort();
827 e->tagindex = MSG_ReadByte();
831 e->light[0] = (unsigned short) MSG_ReadShort();
832 e->light[1] = (unsigned short) MSG_ReadShort();
833 e->light[2] = (unsigned short) MSG_ReadShort();
834 e->light[3] = (unsigned short) MSG_ReadShort();
836 if (bits & E_LIGHTSTYLE)
837 e->lightstyle = MSG_ReadByte();
838 if (bits & E_LIGHTPFLAGS)
839 e->lightpflags = MSG_ReadByte();
841 if (developer_networkentities.integer >= 2)
843 Con_Printf("ReadFields e%i", e->number);
845 if (bits & E_ORIGIN1)
846 Con_Printf(" E_ORIGIN1 %f", e->origin[0]);
847 if (bits & E_ORIGIN2)
848 Con_Printf(" E_ORIGIN2 %f", e->origin[1]);
849 if (bits & E_ORIGIN3)
850 Con_Printf(" E_ORIGIN3 %f", e->origin[2]);
852 Con_Printf(" E_ANGLE1 %f", e->angles[0]);
854 Con_Printf(" E_ANGLE2 %f", e->angles[1]);
856 Con_Printf(" E_ANGLE3 %f", e->angles[2]);
857 if (bits & (E_MODEL1 | E_MODEL2))
858 Con_Printf(" E_MODEL %i", e->modelindex);
860 if (bits & (E_FRAME1 | E_FRAME2))
861 Con_Printf(" E_FRAME %i", e->frame);
862 if (bits & (E_EFFECTS1 | E_EFFECTS2))
863 Con_Printf(" E_EFFECTS %i", e->effects);
865 Con_Printf(" E_ALPHA %f", e->alpha / 255.0f);
867 Con_Printf(" E_SCALE %f", e->scale / 16.0f);
868 if (bits & E_COLORMAP)
869 Con_Printf(" E_COLORMAP %i", e->colormap);
871 Con_Printf(" E_SKIN %i", e->skin);
873 if (bits & E_GLOWSIZE)
874 Con_Printf(" E_GLOWSIZE %i", e->glowsize * 4);
875 if (bits & E_GLOWCOLOR)
876 Con_Printf(" E_GLOWCOLOR %i", e->glowcolor);
879 Con_Printf(" E_LIGHT %i:%i:%i:%i", e->light[0], e->light[1], e->light[2], e->light[3]);
880 if (bits & E_LIGHTPFLAGS)
881 Con_Printf(" E_LIGHTPFLAGS %i", e->lightpflags);
883 if (bits & E_TAGATTACHMENT)
884 Con_Printf(" E_TAGATTACHMENT e%i:%i", e->tagentity, e->tagindex);
885 if (bits & E_LIGHTSTYLE)
886 Con_Printf(" E_LIGHTSTYLE %i", e->lightstyle);
891 // (client and server) allocates a new empty database
892 entityframe_database_t *EntityFrame_AllocDatabase(mempool_t *mempool)
894 return (entityframe_database_t *)Mem_Alloc(mempool, sizeof(entityframe_database_t));
897 // (client and server) frees the database
898 void EntityFrame_FreeDatabase(entityframe_database_t *d)
903 // (server) clears the database to contain no frames (thus delta compression compresses against nothing)
904 void EntityFrame_ClearDatabase(entityframe_database_t *d)
906 memset(d, 0, sizeof(*d));
909 // (server and client) removes frames older than 'frame' from database
910 void EntityFrame_AckFrame(entityframe_database_t *d, int frame)
913 d->ackframenum = frame;
914 for (i = 0;i < d->numframes && d->frames[i].framenum < frame;i++);
915 // ignore outdated frame acks (out of order packets)
919 // if some queue is left, slide it down to beginning of array
921 memmove(&d->frames[0], &d->frames[i], sizeof(d->frames[0]) * d->numframes);
924 // (server) clears frame, to prepare for adding entities
925 void EntityFrame_Clear(entity_frame_t *f, vec3_t eye, int framenum)
928 f->framenum = framenum;
933 VectorCopy(eye, f->eye);
936 // (server and client) reads a frame from the database
937 void EntityFrame_FetchFrame(entityframe_database_t *d, int framenum, entity_frame_t *f)
940 EntityFrame_Clear(f, NULL, -1);
941 for (i = 0;i < d->numframes && d->frames[i].framenum < framenum;i++);
942 if (i < d->numframes && framenum == d->frames[i].framenum)
944 f->framenum = framenum;
945 f->numentities = d->frames[i].endentity - d->frames[i].firstentity;
946 n = MAX_ENTITY_DATABASE - (d->frames[i].firstentity % MAX_ENTITY_DATABASE);
947 if (n > f->numentities)
949 memcpy(f->entitydata, d->entitydata + d->frames[i].firstentity % MAX_ENTITY_DATABASE, sizeof(*f->entitydata) * n);
950 if (f->numentities > n)
951 memcpy(f->entitydata + n, d->entitydata, sizeof(*f->entitydata) * (f->numentities - n));
952 VectorCopy(d->eye, f->eye);
956 // (server and client) adds a entity_frame to the database, for future reference
957 void EntityFrame_AddFrame(entityframe_database_t *d, vec3_t eye, int framenum, int numentities, const entity_state_t *entitydata)
960 entity_frameinfo_t *info;
962 VectorCopy(eye, d->eye);
964 // figure out how many entity slots are used already
967 n = d->frames[d->numframes - 1].endentity - d->frames[0].firstentity;
968 if (n + numentities > MAX_ENTITY_DATABASE || d->numframes >= MAX_ENTITY_HISTORY)
970 // ran out of room, dump database
971 EntityFrame_ClearDatabase(d);
975 info = &d->frames[d->numframes];
976 info->framenum = framenum;
978 // make sure we check the newly added frame as well, but we haven't incremented numframes yet
979 for (n = 0;n <= d->numframes;n++)
981 if (e >= d->frames[n].framenum)
984 Con_Print("EntityFrame_AddFrame: tried to add out of sequence frame to database\n");
986 Con_Print("EntityFrame_AddFrame: out of sequence frames in database\n");
989 e = d->frames[n].framenum;
991 // if database still has frames after that...
993 info->firstentity = d->frames[d->numframes - 1].endentity;
995 info->firstentity = 0;
996 info->endentity = info->firstentity + numentities;
999 n = info->firstentity % MAX_ENTITY_DATABASE;
1000 e = MAX_ENTITY_DATABASE - n;
1001 if (e > numentities)
1003 memcpy(d->entitydata + n, entitydata, sizeof(entity_state_t) * e);
1004 if (numentities > e)
1005 memcpy(d->entitydata, entitydata + e, sizeof(entity_state_t) * (numentities - e));
1008 // (server) writes a frame to network stream
1009 static entity_frame_t deltaframe; // FIXME?
1010 void EntityFrame_WriteFrame(sizebuf_t *msg, entityframe_database_t *d, int numstates, const entity_state_t *states, int viewentnum)
1012 int i, onum, number;
1013 entity_frame_t *o = &deltaframe;
1014 const entity_state_t *ent, *delta;
1018 d->latestframenum++;
1021 for (i = 0;i < numstates;i++)
1023 if (states[i].number == viewentnum)
1025 VectorSet(eye, states[i].origin[0], states[i].origin[1], states[i].origin[2] + 22);
1030 EntityFrame_AddFrame(d, eye, d->latestframenum, numstates, states);
1032 EntityFrame_FetchFrame(d, d->ackframenum, o);
1034 MSG_WriteByte (msg, svc_entities);
1035 MSG_WriteLong (msg, o->framenum);
1036 MSG_WriteLong (msg, d->latestframenum);
1037 MSG_WriteFloat (msg, eye[0]);
1038 MSG_WriteFloat (msg, eye[1]);
1039 MSG_WriteFloat (msg, eye[2]);
1042 for (i = 0;i < numstates;i++)
1045 number = ent->number;
1047 val = PRVM_GETEDICTFIELDVALUE((&prog->edicts[number]), eval_SendEntity);
1048 if(val && val->function)
1050 for (;onum < o->numentities && o->entitydata[onum].number < number;onum++)
1052 // write remove message
1053 MSG_WriteShort(msg, o->entitydata[onum].number | 0x8000);
1055 if (onum < o->numentities && (o->entitydata[onum].number == number))
1057 // delta from previous frame
1058 delta = o->entitydata + onum;
1059 // advance to next entity in delta frame
1064 // delta from defaults
1065 delta = &defaultstate;
1067 EntityState_WriteUpdate(ent, msg, delta);
1069 for (;onum < o->numentities;onum++)
1071 // write remove message
1072 MSG_WriteShort(msg, o->entitydata[onum].number | 0x8000);
1074 MSG_WriteShort(msg, 0xFFFF);
1077 // (client) reads a frame from network stream
1078 static entity_frame_t framedata; // FIXME?
1079 void EntityFrame_CL_ReadFrame(void)
1081 int i, number, removed;
1082 entity_frame_t *f = &framedata, *delta = &deltaframe;
1083 entity_state_t *e, *old, *oldend;
1085 entityframe_database_t *d;
1086 if (!cl.entitydatabase)
1087 cl.entitydatabase = EntityFrame_AllocDatabase(cl_mempool);
1088 d = cl.entitydatabase;
1090 EntityFrame_Clear(f, NULL, -1);
1092 // read the frame header info
1093 f->time = cl.mtime[0];
1094 number = MSG_ReadLong();
1095 for (i = 0;i < LATESTFRAMENUMS-1;i++)
1096 cl.latestframenums[i] = cl.latestframenums[i+1];
1097 cl.latestframenums[LATESTFRAMENUMS-1] = f->framenum = MSG_ReadLong();
1098 f->eye[0] = MSG_ReadFloat();
1099 f->eye[1] = MSG_ReadFloat();
1100 f->eye[2] = MSG_ReadFloat();
1101 EntityFrame_AckFrame(d, number);
1102 EntityFrame_FetchFrame(d, number, delta);
1103 old = delta->entitydata;
1104 oldend = old + delta->numentities;
1105 // read entities until we hit the magic 0xFFFF end tag
1106 while ((number = (unsigned short) MSG_ReadShort()) != 0xFFFF && !msg_badread)
1109 Host_Error("EntityFrame_Read: read error");
1110 removed = number & 0x8000;
1112 if (number >= MAX_EDICTS)
1113 Host_Error("EntityFrame_Read: number (%i) >= MAX_EDICTS (%i)", number, MAX_EDICTS);
1115 // seek to entity, while copying any skipped entities (assume unchanged)
1116 while (old < oldend && old->number < number)
1118 if (f->numentities >= MAX_ENTITY_DATABASE)
1119 Host_Error("EntityFrame_Read: entity list too big");
1120 f->entitydata[f->numentities] = *old++;
1121 f->entitydata[f->numentities++].time = cl.mtime[0];
1125 if (old < oldend && old->number == number)
1128 Con_Printf("EntityFrame_Read: REMOVE on unused entity %i\n", number);
1132 if (f->numentities >= MAX_ENTITY_DATABASE)
1133 Host_Error("EntityFrame_Read: entity list too big");
1135 // reserve this slot
1136 e = f->entitydata + f->numentities++;
1138 if (old < oldend && old->number == number)
1140 // delta from old entity
1145 // delta from defaults
1149 if (cl_num_entities <= number)
1151 cl_num_entities = number + 1;
1152 if (number >= cl_max_entities)
1153 CL_ExpandEntities(number);
1155 cl_entities_active[number] = true;
1157 e->time = cl.mtime[0];
1159 EntityState_ReadFields(e, EntityState_ReadExtendBits());
1162 while (old < oldend)
1164 if (f->numentities >= MAX_ENTITY_DATABASE)
1165 Host_Error("EntityFrame_Read: entity list too big");
1166 f->entitydata[f->numentities] = *old++;
1167 f->entitydata[f->numentities++].time = cl.mtime[0];
1169 EntityFrame_AddFrame(d, f->eye, f->framenum, f->numentities, f->entitydata);
1171 memset(cl_entities_active, 0, cl_num_entities * sizeof(unsigned char));
1173 for (i = 0;i < f->numentities;i++)
1175 for (;number < f->entitydata[i].number && number < cl_num_entities;number++)
1177 if (cl_entities_active[number])
1179 cl_entities_active[number] = false;
1180 cl_entities[number].state_current.active = false;
1183 if (number >= cl_num_entities)
1185 // update the entity
1186 ent = &cl_entities[number];
1187 ent->state_previous = ent->state_current;
1188 ent->state_current = f->entitydata[i];
1189 CL_MoveLerpEntityStates(ent);
1190 // the entity lives again...
1191 cl_entities_active[number] = true;
1194 for (;number < cl_num_entities;number++)
1196 if (cl_entities_active[number])
1198 cl_entities_active[number] = false;
1199 cl_entities[number].state_current.active = false;
1205 // (client) returns the frame number of the most recent frame recieved
1206 int EntityFrame_MostRecentlyRecievedFrameNum(entityframe_database_t *d)
1209 return d->frames[d->numframes - 1].framenum;
1219 entity_state_t *EntityFrame4_GetReferenceEntity(entityframe4_database_t *d, int number)
1221 if (d->maxreferenceentities <= number)
1223 int oldmax = d->maxreferenceentities;
1224 entity_state_t *oldentity = d->referenceentity;
1225 d->maxreferenceentities = (number + 15) & ~7;
1226 d->referenceentity = (entity_state_t *)Mem_Alloc(d->mempool, d->maxreferenceentities * sizeof(*d->referenceentity));
1229 memcpy(d->referenceentity, oldentity, oldmax * sizeof(*d->referenceentity));
1230 Mem_Free(oldentity);
1232 // clear the newly created entities
1233 for (;oldmax < d->maxreferenceentities;oldmax++)
1235 d->referenceentity[oldmax] = defaultstate;
1236 d->referenceentity[oldmax].number = oldmax;
1239 return d->referenceentity + number;
1242 void EntityFrame4_AddCommitEntity(entityframe4_database_t *d, const entity_state_t *s)
1244 // resize commit's entity list if full
1245 if (d->currentcommit->maxentities <= d->currentcommit->numentities)
1247 entity_state_t *oldentity = d->currentcommit->entity;
1248 d->currentcommit->maxentities += 8;
1249 d->currentcommit->entity = (entity_state_t *)Mem_Alloc(d->mempool, d->currentcommit->maxentities * sizeof(*d->currentcommit->entity));
1252 memcpy(d->currentcommit->entity, oldentity, d->currentcommit->numentities * sizeof(*d->currentcommit->entity));
1253 Mem_Free(oldentity);
1256 d->currentcommit->entity[d->currentcommit->numentities++] = *s;
1259 entityframe4_database_t *EntityFrame4_AllocDatabase(mempool_t *pool)
1261 entityframe4_database_t *d;
1262 d = (entityframe4_database_t *)Mem_Alloc(pool, sizeof(*d));
1264 EntityFrame4_ResetDatabase(d);
1268 void EntityFrame4_FreeDatabase(entityframe4_database_t *d)
1271 for (i = 0;i < MAX_ENTITY_HISTORY;i++)
1272 if (d->commit[i].entity)
1273 Mem_Free(d->commit[i].entity);
1274 if (d->referenceentity)
1275 Mem_Free(d->referenceentity);
1279 void EntityFrame4_ResetDatabase(entityframe4_database_t *d)
1282 d->referenceframenum = -1;
1283 for (i = 0;i < MAX_ENTITY_HISTORY;i++)
1284 d->commit[i].numentities = 0;
1285 for (i = 0;i < d->maxreferenceentities;i++)
1286 d->referenceentity[i] = defaultstate;
1289 int EntityFrame4_AckFrame(entityframe4_database_t *d, int framenum, int servermode)
1292 entity_database4_commit_t *commit;
1295 // reset reference, but leave commits alone
1296 d->referenceframenum = -1;
1297 for (i = 0;i < d->maxreferenceentities;i++)
1298 d->referenceentity[i] = defaultstate;
1299 // if this is the server, remove commits
1300 for (i = 0, commit = d->commit;i < MAX_ENTITY_HISTORY;i++, commit++)
1301 commit->numentities = 0;
1304 else if (d->referenceframenum == framenum)
1309 for (i = 0, commit = d->commit;i < MAX_ENTITY_HISTORY;i++, commit++)
1311 if (commit->numentities && commit->framenum <= framenum)
1313 if (commit->framenum == framenum)
1316 d->referenceframenum = framenum;
1317 if (developer_networkentities.integer >= 3)
1319 for (j = 0;j < commit->numentities;j++)
1321 entity_state_t *s = EntityFrame4_GetReferenceEntity(d, commit->entity[j].number);
1322 if (commit->entity[j].active != s->active)
1324 if (commit->entity[j].active)
1325 Con_Printf("commit entity %i has become active (modelindex %i)\n", commit->entity[j].number, commit->entity[j].modelindex);
1327 Con_Printf("commit entity %i has become inactive (modelindex %i)\n", commit->entity[j].number, commit->entity[j].modelindex);
1329 *s = commit->entity[j];
1333 for (j = 0;j < commit->numentities;j++)
1334 *EntityFrame4_GetReferenceEntity(d, commit->entity[j].number) = commit->entity[j];
1336 commit->numentities = 0;
1340 if (developer_networkentities.integer >= 1)
1342 Con_Printf("ack ref:%i database updated to: ref:%i commits:", framenum, d->referenceframenum);
1343 for (i = 0;i < MAX_ENTITY_HISTORY;i++)
1344 if (d->commit[i].numentities)
1345 Con_Printf(" %i", d->commit[i].framenum);
1351 void EntityFrame4_CL_ReadFrame(void)
1353 int i, n, cnumber, referenceframenum, framenum, enumber, done, stopnumber, skip = false;
1355 entityframe4_database_t *d;
1356 if (!cl.entitydatabase4)
1357 cl.entitydatabase4 = EntityFrame4_AllocDatabase(cl_mempool);
1358 d = cl.entitydatabase4;
1359 // read the number of the frame this refers to
1360 referenceframenum = MSG_ReadLong();
1361 // read the number of this frame
1362 for (i = 0;i < LATESTFRAMENUMS-1;i++)
1363 cl.latestframenums[i] = cl.latestframenums[i+1];
1364 cl.latestframenums[LATESTFRAMENUMS-1] = framenum = MSG_ReadLong();
1365 // read the start number
1366 enumber = (unsigned short) MSG_ReadShort();
1367 if (developer_networkentities.integer >= 1)
1369 Con_Printf("recv svc_entities num:%i ref:%i database: ref:%i commits:", framenum, referenceframenum, d->referenceframenum);
1370 for (i = 0;i < MAX_ENTITY_HISTORY;i++)
1371 if (d->commit[i].numentities)
1372 Con_Printf(" %i", d->commit[i].framenum);
1375 if (!EntityFrame4_AckFrame(d, referenceframenum, false))
1377 Con_Print("EntityFrame4_CL_ReadFrame: reference frame invalid (VERY BAD ERROR), this update will be skipped\n");
1380 d->currentcommit = NULL;
1381 for (i = 0;i < MAX_ENTITY_HISTORY;i++)
1383 if (!d->commit[i].numentities)
1385 d->currentcommit = d->commit + i;
1386 d->currentcommit->framenum = framenum;
1387 d->currentcommit->numentities = 0;
1390 if (d->currentcommit == NULL)
1392 Con_Printf("EntityFrame4_CL_ReadFrame: error while decoding frame %i: database full, reading but not storing this update\n", framenum);
1396 while (!done && !msg_badread)
1398 // read the number of the modified entity
1399 // (gaps will be copied unmodified)
1400 n = (unsigned short)MSG_ReadShort();
1403 // no more entities in this update, but we still need to copy the
1404 // rest of the reference entities (final gap)
1406 // read end of range number, then process normally
1407 n = (unsigned short)MSG_ReadShort();
1409 // high bit means it's a remove message
1410 cnumber = n & 0x7FFF;
1411 // if this is a live entity we may need to expand the array
1412 if (cl_num_entities <= cnumber && !(n & 0x8000))
1414 cl_num_entities = cnumber + 1;
1415 if (cnumber >= cl_max_entities)
1416 CL_ExpandEntities(cnumber);
1418 // add one (the changed one) if not done
1419 stopnumber = cnumber + !done;
1420 // process entities in range from the last one to the changed one
1421 for (;enumber < stopnumber;enumber++)
1423 if (skip || enumber >= cl_num_entities)
1425 if (enumber == cnumber && (n & 0x8000) == 0)
1427 entity_state_t tempstate;
1428 EntityState_ReadFields(&tempstate, EntityState_ReadExtendBits());
1432 // slide the current into the previous slot
1433 cl_entities[enumber].state_previous = cl_entities[enumber].state_current;
1434 // copy a new current from reference database
1435 cl_entities[enumber].state_current = *EntityFrame4_GetReferenceEntity(d, enumber);
1436 s = &cl_entities[enumber].state_current;
1437 // if this is the one to modify, read more data...
1438 if (enumber == cnumber)
1443 if (developer_networkentities.integer >= 2)
1444 Con_Printf("entity %i: remove\n", enumber);
1450 if (developer_networkentities.integer >= 2)
1451 Con_Printf("entity %i: update\n", enumber);
1453 EntityState_ReadFields(s, EntityState_ReadExtendBits());
1456 else if (developer_networkentities.integer >= 4)
1457 Con_Printf("entity %i: copy\n", enumber);
1458 // set the cl_entities_active flag
1459 cl_entities_active[enumber] = s->active;
1460 // set the update time
1461 s->time = cl.mtime[0];
1462 // fix the number (it gets wiped occasionally by copying from defaultstate)
1463 s->number = enumber;
1464 // check if we need to update the lerp stuff
1466 CL_MoveLerpEntityStates(&cl_entities[enumber]);
1467 // add this to the commit entry whether it is modified or not
1468 if (d->currentcommit)
1469 EntityFrame4_AddCommitEntity(d, &cl_entities[enumber].state_current);
1470 // print extra messages if desired
1471 if (developer_networkentities.integer >= 2 && cl_entities[enumber].state_current.active != cl_entities[enumber].state_previous.active)
1473 if (cl_entities[enumber].state_current.active)
1474 Con_Printf("entity #%i has become active\n", enumber);
1475 else if (cl_entities[enumber].state_previous.active)
1476 Con_Printf("entity #%i has become inactive\n", enumber);
1480 d->currentcommit = NULL;
1482 EntityFrame4_ResetDatabase(d);
1485 void EntityFrame4_WriteFrame(sizebuf_t *msg, entityframe4_database_t *d, int numstates, const entity_state_t *states)
1487 const entity_state_t *e, *s;
1488 entity_state_t inactiveentitystate;
1489 int i, n, startnumber;
1491 unsigned char data[128];
1494 // if there isn't enough space to accomplish anything, skip it
1495 if (msg->cursize + 24 > msg->maxsize)
1498 // prepare the buffer
1499 memset(&buf, 0, sizeof(buf));
1501 buf.maxsize = sizeof(data);
1503 for (i = 0;i < MAX_ENTITY_HISTORY;i++)
1504 if (!d->commit[i].numentities)
1506 // if commit buffer full, just don't bother writing an update this frame
1507 if (i == MAX_ENTITY_HISTORY)
1509 d->currentcommit = d->commit + i;
1511 // this state's number gets played around with later
1512 inactiveentitystate = defaultstate;
1514 d->currentcommit->numentities = 0;
1515 d->currentcommit->framenum = ++d->latestframenumber;
1516 MSG_WriteByte(msg, svc_entities);
1517 MSG_WriteLong(msg, d->referenceframenum);
1518 MSG_WriteLong(msg, d->currentcommit->framenum);
1519 if (developer_networkentities.integer >= 1)
1521 Con_Printf("send svc_entities num:%i ref:%i (database: ref:%i commits:", d->currentcommit->framenum, d->referenceframenum, d->referenceframenum);
1522 for (i = 0;i < MAX_ENTITY_HISTORY;i++)
1523 if (d->commit[i].numentities)
1524 Con_Printf(" %i", d->commit[i].framenum);
1527 if (d->currententitynumber >= prog->max_edicts)
1530 startnumber = bound(1, d->currententitynumber, prog->max_edicts - 1);
1531 MSG_WriteShort(msg, startnumber);
1532 // reset currententitynumber so if the loop does not break it we will
1533 // start at beginning next frame (if it does break, it will set it)
1534 d->currententitynumber = 1;
1535 for (i = 0, n = startnumber;n < prog->max_edicts;n++)
1537 val = PRVM_GETEDICTFIELDVALUE((&prog->edicts[n]), eval_SendEntity);
1538 if(val && val->function)
1540 // find the old state to delta from
1541 e = EntityFrame4_GetReferenceEntity(d, n);
1542 // prepare the buffer
1544 // entity exists, build an update (if empty there is no change)
1545 // find the state in the list
1546 for (;i < numstates && states[i].number < n;i++);
1552 EntityState_WriteUpdate(s, &buf, e);
1556 inactiveentitystate.number = n;
1557 s = &inactiveentitystate;
1560 // entity used to exist but doesn't anymore, send remove
1561 MSG_WriteShort(&buf, n | 0x8000);
1564 // if the commit is full, we're done this frame
1565 if (msg->cursize + buf.cursize > msg->maxsize - 4)
1567 // next frame we will continue where we left off
1570 // add the entity to the commit
1571 EntityFrame4_AddCommitEntity(d, s);
1572 // if the message is empty, skip out now
1575 // write the message to the packet
1576 SZ_Write(msg, buf.data, buf.cursize);
1579 d->currententitynumber = n;
1581 // remove world message (invalid, and thus a good terminator)
1582 MSG_WriteShort(msg, 0x8000);
1583 // write the number of the end entity
1584 MSG_WriteShort(msg, d->currententitynumber);
1586 d->currentcommit = NULL;
1592 #define E5_PROTOCOL_PRIORITYLEVELS 32
1594 entityframe5_database_t *EntityFrame5_AllocDatabase(mempool_t *pool)
1596 entityframe5_database_t *d;
1597 d = (entityframe5_database_t *)Mem_Alloc(pool, sizeof(*d));
1598 EntityFrame5_ResetDatabase(d);
1602 void EntityFrame5_FreeDatabase(entityframe5_database_t *d)
1604 // all the [maxedicts] memory is allocated at once, so there's only one
1607 Mem_Free(d->deltabits);
1611 void EntityFrame5_ResetDatabase(entityframe5_database_t *d)
1614 memset(d, 0, sizeof(*d));
1615 d->latestframenum = 0;
1616 for (i = 0;i < d->maxedicts;i++)
1617 d->states[i] = defaultstate;
1620 void EntityFrame5_ExpandEdicts(entityframe5_database_t *d, int newmax)
1622 if (d->maxedicts < newmax)
1624 unsigned char *data;
1625 int oldmaxedicts = d->maxedicts;
1626 int *olddeltabits = d->deltabits;
1627 unsigned char *oldpriorities = d->priorities;
1628 int *oldupdateframenum = d->updateframenum;
1629 entity_state_t *oldstates = d->states;
1630 unsigned char *oldvisiblebits = d->visiblebits;
1631 d->maxedicts = newmax;
1632 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));
1633 d->deltabits = (int *)data;data += d->maxedicts * sizeof(int);
1634 d->priorities = (unsigned char *)data;data += d->maxedicts * sizeof(unsigned char);
1635 d->updateframenum = (int *)data;data += d->maxedicts * sizeof(int);
1636 d->states = (entity_state_t *)data;data += d->maxedicts * sizeof(entity_state_t);
1637 d->visiblebits = (unsigned char *)data;data += (d->maxedicts+7)/8 * sizeof(unsigned char);
1640 memcpy(d->deltabits, olddeltabits, oldmaxedicts * sizeof(int));
1641 memcpy(d->priorities, oldpriorities, oldmaxedicts * sizeof(unsigned char));
1642 memcpy(d->updateframenum, oldupdateframenum, oldmaxedicts * sizeof(int));
1643 memcpy(d->states, oldstates, oldmaxedicts * sizeof(entity_state_t));
1644 memcpy(d->visiblebits, oldvisiblebits, (oldmaxedicts+7)/8 * sizeof(unsigned char));
1645 // the previous buffers were a single allocation, so just one free
1646 Mem_Free(olddeltabits);
1651 int EntityState5_Priority(entityframe5_database_t *d, int stateindex)
1653 int lowprecision, limit, priority;
1657 entity_state_t *view, *s;
1658 changedbits = d->deltabits[stateindex];
1661 if (!d->states[stateindex].active/* && changedbits & E5_FULLUPDATE*/)
1662 return E5_PROTOCOL_PRIORITYLEVELS - 1;
1663 // check whole attachment chain to judge relevance to player
1664 view = d->states + d->viewentnum;
1665 lowprecision = false;
1666 for (limit = 0;limit < 256;limit++)
1668 if (d->maxedicts < stateindex)
1669 EntityFrame5_ExpandEdicts(d, (stateindex+256)&~255);
1670 s = d->states + stateindex;
1672 return E5_PROTOCOL_PRIORITYLEVELS - 1;
1673 if (s->flags & RENDER_VIEWMODEL)
1674 return E5_PROTOCOL_PRIORITYLEVELS - 1;
1675 if (s->flags & RENDER_LOWPRECISION)
1676 lowprecision = true;
1679 if (VectorCompare(s->origin, view->origin))
1680 return E5_PROTOCOL_PRIORITYLEVELS - 1;
1683 stateindex = s->tagentity;
1687 Con_Printf("Protocol: Runaway loop recursing tagentity links on entity %i\n", stateindex);
1690 // it's not a viewmodel for this client
1691 distance = VectorDistance(view->origin, s->origin);
1692 age = d->latestframenum - d->updateframenum[stateindex];
1693 priority = (E5_PROTOCOL_PRIORITYLEVELS / 2) + age - (int)(distance * (E5_PROTOCOL_PRIORITYLEVELS / 16384.0f));
1695 priority -= (E5_PROTOCOL_PRIORITYLEVELS / 4);
1696 //if (changedbits & E5_FULLUPDATE)
1698 //if (changedbits & (E5_ATTACHMENT | E5_MODEL | E5_FLAGS | E5_COLORMAP))
1700 return (int) bound(1, priority, E5_PROTOCOL_PRIORITYLEVELS - 1);
1703 void EntityState5_WriteUpdate(int number, const entity_state_t *s, int changedbits, sizebuf_t *msg)
1705 unsigned int bits = 0;
1708 val = PRVM_GETEDICTFIELDVALUE((&prog->edicts[s->number]), eval_SendEntity);
1709 if(val && val->function)
1713 MSG_WriteShort(msg, number | 0x8000);
1717 if ((bits & E5_ORIGIN) && (!(s->flags & RENDER_LOWPRECISION) || s->origin[0] < -4096 || s->origin[0] >= 4096 || s->origin[1] < -4096 || s->origin[1] >= 4096 || s->origin[2] < -4096 || s->origin[2] >= 4096))
1718 bits |= E5_ORIGIN32;
1719 if ((bits & E5_ANGLES) && !(s->flags & RENDER_LOWPRECISION))
1720 bits |= E5_ANGLES16;
1721 if ((bits & E5_MODEL) && s->modelindex >= 256)
1723 if ((bits & E5_FRAME) && s->frame >= 256)
1725 if (bits & E5_EFFECTS)
1727 if (s->effects >= 65536)
1728 bits |= E5_EFFECTS32;
1729 else if (s->effects >= 256)
1730 bits |= E5_EFFECTS16;
1736 if (bits >= 16777216)
1738 MSG_WriteShort(msg, number);
1739 MSG_WriteByte(msg, bits & 0xFF);
1740 if (bits & E5_EXTEND1)
1741 MSG_WriteByte(msg, (bits >> 8) & 0xFF);
1742 if (bits & E5_EXTEND2)
1743 MSG_WriteByte(msg, (bits >> 16) & 0xFF);
1744 if (bits & E5_EXTEND3)
1745 MSG_WriteByte(msg, (bits >> 24) & 0xFF);
1746 if (bits & E5_FLAGS)
1747 MSG_WriteByte(msg, s->flags);
1748 if (bits & E5_ORIGIN)
1750 if (bits & E5_ORIGIN32)
1752 MSG_WriteCoord32f(msg, s->origin[0]);
1753 MSG_WriteCoord32f(msg, s->origin[1]);
1754 MSG_WriteCoord32f(msg, s->origin[2]);
1758 MSG_WriteCoord13i(msg, s->origin[0]);
1759 MSG_WriteCoord13i(msg, s->origin[1]);
1760 MSG_WriteCoord13i(msg, s->origin[2]);
1763 if (bits & E5_ANGLES)
1765 if (bits & E5_ANGLES16)
1767 MSG_WriteAngle16i(msg, s->angles[0]);
1768 MSG_WriteAngle16i(msg, s->angles[1]);
1769 MSG_WriteAngle16i(msg, s->angles[2]);
1773 MSG_WriteAngle8i(msg, s->angles[0]);
1774 MSG_WriteAngle8i(msg, s->angles[1]);
1775 MSG_WriteAngle8i(msg, s->angles[2]);
1778 if (bits & E5_MODEL)
1780 if (bits & E5_MODEL16)
1781 MSG_WriteShort(msg, s->modelindex);
1783 MSG_WriteByte(msg, s->modelindex);
1785 if (bits & E5_FRAME)
1787 if (bits & E5_FRAME16)
1788 MSG_WriteShort(msg, s->frame);
1790 MSG_WriteByte(msg, s->frame);
1793 MSG_WriteByte(msg, s->skin);
1794 if (bits & E5_EFFECTS)
1796 if (bits & E5_EFFECTS32)
1797 MSG_WriteLong(msg, s->effects);
1798 else if (bits & E5_EFFECTS16)
1799 MSG_WriteShort(msg, s->effects);
1801 MSG_WriteByte(msg, s->effects);
1803 if (bits & E5_ALPHA)
1804 MSG_WriteByte(msg, s->alpha);
1805 if (bits & E5_SCALE)
1806 MSG_WriteByte(msg, s->scale);
1807 if (bits & E5_COLORMAP)
1808 MSG_WriteByte(msg, s->colormap);
1809 if (bits & E5_ATTACHMENT)
1811 MSG_WriteShort(msg, s->tagentity);
1812 MSG_WriteByte(msg, s->tagindex);
1814 if (bits & E5_LIGHT)
1816 MSG_WriteShort(msg, s->light[0]);
1817 MSG_WriteShort(msg, s->light[1]);
1818 MSG_WriteShort(msg, s->light[2]);
1819 MSG_WriteShort(msg, s->light[3]);
1820 MSG_WriteByte(msg, s->lightstyle);
1821 MSG_WriteByte(msg, s->lightpflags);
1825 MSG_WriteByte(msg, s->glowsize);
1826 MSG_WriteByte(msg, s->glowcolor);
1828 if (bits & E5_COLORMOD)
1830 MSG_WriteByte(msg, s->colormod[0]);
1831 MSG_WriteByte(msg, s->colormod[1]);
1832 MSG_WriteByte(msg, s->colormod[2]);
1837 void EntityState5_ReadUpdate(entity_state_t *s)
1840 bits = MSG_ReadByte();
1841 if (bits & E5_EXTEND1)
1843 bits |= MSG_ReadByte() << 8;
1844 if (bits & E5_EXTEND2)
1846 bits |= MSG_ReadByte() << 16;
1847 if (bits & E5_EXTEND3)
1848 bits |= MSG_ReadByte() << 24;
1851 if (bits & E5_FULLUPDATE)
1856 if (bits & E5_FLAGS)
1857 s->flags = MSG_ReadByte();
1858 if (bits & E5_ORIGIN)
1860 if (bits & E5_ORIGIN32)
1862 s->origin[0] = MSG_ReadCoord32f();
1863 s->origin[1] = MSG_ReadCoord32f();
1864 s->origin[2] = MSG_ReadCoord32f();
1868 s->origin[0] = MSG_ReadCoord13i();
1869 s->origin[1] = MSG_ReadCoord13i();
1870 s->origin[2] = MSG_ReadCoord13i();
1873 if (bits & E5_ANGLES)
1875 if (bits & E5_ANGLES16)
1877 s->angles[0] = MSG_ReadAngle16i();
1878 s->angles[1] = MSG_ReadAngle16i();
1879 s->angles[2] = MSG_ReadAngle16i();
1883 s->angles[0] = MSG_ReadAngle8i();
1884 s->angles[1] = MSG_ReadAngle8i();
1885 s->angles[2] = MSG_ReadAngle8i();
1888 if (bits & E5_MODEL)
1890 if (bits & E5_MODEL16)
1891 s->modelindex = (unsigned short) MSG_ReadShort();
1893 s->modelindex = MSG_ReadByte();
1895 if (bits & E5_FRAME)
1897 if (bits & E5_FRAME16)
1898 s->frame = (unsigned short) MSG_ReadShort();
1900 s->frame = MSG_ReadByte();
1903 s->skin = MSG_ReadByte();
1904 if (bits & E5_EFFECTS)
1906 if (bits & E5_EFFECTS32)
1907 s->effects = (unsigned int) MSG_ReadLong();
1908 else if (bits & E5_EFFECTS16)
1909 s->effects = (unsigned short) MSG_ReadShort();
1911 s->effects = MSG_ReadByte();
1913 if (bits & E5_ALPHA)
1914 s->alpha = MSG_ReadByte();
1915 if (bits & E5_SCALE)
1916 s->scale = MSG_ReadByte();
1917 if (bits & E5_COLORMAP)
1918 s->colormap = MSG_ReadByte();
1919 if (bits & E5_ATTACHMENT)
1921 s->tagentity = (unsigned short) MSG_ReadShort();
1922 s->tagindex = MSG_ReadByte();
1924 if (bits & E5_LIGHT)
1926 s->light[0] = (unsigned short) MSG_ReadShort();
1927 s->light[1] = (unsigned short) MSG_ReadShort();
1928 s->light[2] = (unsigned short) MSG_ReadShort();
1929 s->light[3] = (unsigned short) MSG_ReadShort();
1930 s->lightstyle = MSG_ReadByte();
1931 s->lightpflags = MSG_ReadByte();
1935 s->glowsize = MSG_ReadByte();
1936 s->glowcolor = MSG_ReadByte();
1938 if (bits & E5_COLORMOD)
1940 s->colormod[0] = MSG_ReadByte();
1941 s->colormod[1] = MSG_ReadByte();
1942 s->colormod[2] = MSG_ReadByte();
1946 if (developer_networkentities.integer >= 2)
1948 Con_Printf("ReadFields e%i", s->number);
1950 if (bits & E5_ORIGIN)
1951 Con_Printf(" E5_ORIGIN %f %f %f", s->origin[0], s->origin[1], s->origin[2]);
1952 if (bits & E5_ANGLES)
1953 Con_Printf(" E5_ANGLES %f %f %f", s->angles[0], s->angles[1], s->angles[2]);
1954 if (bits & E5_MODEL)
1955 Con_Printf(" E5_MODEL %i", s->modelindex);
1956 if (bits & E5_FRAME)
1957 Con_Printf(" E5_FRAME %i", s->frame);
1959 Con_Printf(" E5_SKIN %i", s->skin);
1960 if (bits & E5_EFFECTS)
1961 Con_Printf(" E5_EFFECTS %i", s->effects);
1962 if (bits & E5_FLAGS)
1964 Con_Printf(" E5_FLAGS %i (", s->flags);
1965 if (s->flags & RENDER_STEP)
1967 if (s->flags & RENDER_GLOWTRAIL)
1968 Con_Print(" GLOWTRAIL");
1969 if (s->flags & RENDER_VIEWMODEL)
1970 Con_Print(" VIEWMODEL");
1971 if (s->flags & RENDER_EXTERIORMODEL)
1972 Con_Print(" EXTERIORMODEL");
1973 if (s->flags & RENDER_LOWPRECISION)
1974 Con_Print(" LOWPRECISION");
1975 if (s->flags & RENDER_COLORMAPPED)
1976 Con_Print(" COLORMAPPED");
1977 if (s->flags & RENDER_SHADOW)
1978 Con_Print(" SHADOW");
1979 if (s->flags & RENDER_LIGHT)
1980 Con_Print(" LIGHT");
1983 if (bits & E5_ALPHA)
1984 Con_Printf(" E5_ALPHA %f", s->alpha / 255.0f);
1985 if (bits & E5_SCALE)
1986 Con_Printf(" E5_SCALE %f", s->scale / 16.0f);
1987 if (bits & E5_COLORMAP)
1988 Con_Printf(" E5_COLORMAP %i", s->colormap);
1989 if (bits & E5_ATTACHMENT)
1990 Con_Printf(" E5_ATTACHMENT e%i:%i", s->tagentity, s->tagindex);
1991 if (bits & E5_LIGHT)
1992 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);
1994 Con_Printf(" E5_GLOW %i:%i", s->glowsize * 4, s->glowcolor);
1995 if (bits & E5_COLORMOD)
1996 Con_Printf(" E5_COLORMOD %f:%f:%f", s->colormod[0] / 32.0f, s->colormod[1] / 32.0f, s->colormod[2] / 32.0f);
2001 int EntityState5_DeltaBits(const entity_state_t *o, const entity_state_t *n)
2003 unsigned int bits = 0;
2007 bits |= E5_FULLUPDATE;
2008 if (!VectorCompare(o->origin, n->origin))
2010 if (!VectorCompare(o->angles, n->angles))
2012 if (o->modelindex != n->modelindex)
2014 if (o->frame != n->frame)
2016 if (o->skin != n->skin)
2018 if (o->effects != n->effects)
2020 if (o->flags != n->flags)
2022 if (o->alpha != n->alpha)
2024 if (o->scale != n->scale)
2026 if (o->colormap != n->colormap)
2027 bits |= E5_COLORMAP;
2028 if (o->tagentity != n->tagentity || o->tagindex != n->tagindex)
2029 bits |= E5_ATTACHMENT;
2030 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)
2032 if (o->glowsize != n->glowsize || o->glowcolor != n->glowcolor)
2034 if (o->colormod[0] != n->colormod[0] || o->colormod[1] != n->colormod[1] || o->colormod[2] != n->colormod[2])
2035 bits |= E5_COLORMOD;
2039 bits |= E5_FULLUPDATE;
2043 void EntityFrame5_CL_ReadFrame(void)
2048 // read the number of this frame to echo back in next input packet
2049 for (i = 0;i < LATESTFRAMENUMS-1;i++)
2050 cl.latestframenums[i] = cl.latestframenums[i+1];
2051 cl.latestframenums[LATESTFRAMENUMS-1] = MSG_ReadLong();
2052 if (cl.protocol != PROTOCOL_QUAKE && cl.protocol != PROTOCOL_QUAKEDP && cl.protocol != PROTOCOL_NEHAHRAMOVIE && cl.protocol != PROTOCOL_DARKPLACES1 && cl.protocol != PROTOCOL_DARKPLACES2 && cl.protocol != PROTOCOL_DARKPLACES3 && cl.protocol != PROTOCOL_DARKPLACES4 && cl.protocol != PROTOCOL_DARKPLACES5 && cl.protocol != PROTOCOL_DARKPLACES6)
2053 cl.servermovesequence = MSG_ReadLong();
2054 // read entity numbers until we find a 0x8000
2055 // (which would be remove world entity, but is actually a terminator)
2056 while ((n = (unsigned short)MSG_ReadShort()) != 0x8000 && !msg_badread)
2058 // get the entity number
2059 enumber = n & 0x7FFF;
2060 // we may need to expand the array
2061 if (cl_num_entities <= enumber)
2063 cl_num_entities = enumber + 1;
2064 if (enumber >= cl_max_entities)
2065 CL_ExpandEntities(enumber);
2067 // look up the entity
2068 ent = cl_entities + enumber;
2069 // slide the current into the previous slot
2070 ent->state_previous = ent->state_current;
2072 s = &ent->state_current;
2081 EntityState5_ReadUpdate(s);
2083 // set the cl_entities_active flag
2084 cl_entities_active[enumber] = s->active;
2085 // set the update time
2086 s->time = cl.mtime[0];
2087 // fix the number (it gets wiped occasionally by copying from defaultstate)
2088 s->number = enumber;
2089 // check if we need to update the lerp stuff
2091 CL_MoveLerpEntityStates(&cl_entities[enumber]);
2092 // print extra messages if desired
2093 if (developer_networkentities.integer >= 2 && cl_entities[enumber].state_current.active != cl_entities[enumber].state_previous.active)
2095 if (cl_entities[enumber].state_current.active)
2096 Con_Printf("entity #%i has become active\n", enumber);
2097 else if (cl_entities[enumber].state_previous.active)
2098 Con_Printf("entity #%i has become inactive\n", enumber);
2103 void EntityFrame5_LostFrame(entityframe5_database_t *d, int framenum)
2105 int i, j, k, l, bits;
2106 entityframe5_changestate_t *s, *s2;
2107 entityframe5_packetlog_t *p, *p2;
2108 unsigned char statsdeltabits[(MAX_CL_STATS+7)/8];
2109 // scan for packets that were lost
2110 for (i = 0, p = d->packetlog;i < ENTITYFRAME5_MAXPACKETLOGS;i++, p++)
2112 if (p->packetnumber && p->packetnumber <= framenum)
2114 // packet was lost - merge deltabits into the main array so they
2115 // will be re-sent, but only if there is no newer update of that
2116 // bit in the logs (as those will arrive before this update)
2117 for (j = 0, s = p->states;j < p->numstates;j++, s++)
2119 // check for any newer updates to this entity and mask off any
2120 // overlapping bits (we don't need to send something again if
2121 // it has already been sent more recently)
2122 bits = s->bits & ~d->deltabits[s->number];
2123 for (k = 0, p2 = d->packetlog;k < ENTITYFRAME5_MAXPACKETLOGS && bits;k++, p2++)
2125 if (p2->packetnumber > framenum)
2127 for (l = 0, s2 = p2->states;l < p2->numstates;l++, s2++)
2129 if (s2->number == s->number)
2137 // if the bits haven't all been cleared, there were some bits
2138 // lost with this packet, so set them again now
2140 d->deltabits[s->number] |= bits;
2143 for (j = 0;j < MAX_CL_STATS;j++)
2145 for (l = 0;l < (MAX_CL_STATS+7)/8;l++)
2146 statsdeltabits[l] = p->statsdeltabits[l] & ~d->statsdeltabits[l];
2147 for (k = 0, p2 = d->packetlog;k < ENTITYFRAME5_MAXPACKETLOGS;k++, p2++)
2148 if (p2->packetnumber > framenum)
2149 for (l = 0;l < (MAX_CL_STATS+7)/8;l++)
2150 statsdeltabits[l] = p->statsdeltabits[l] & ~p2->statsdeltabits[l];
2151 for (l = 0;l < (MAX_CL_STATS+7)/8;l++)
2152 d->statsdeltabits[l] |= statsdeltabits[l];
2154 // delete this packet log as it is now obsolete
2155 p->packetnumber = 0;
2160 void EntityFrame5_AckFrame(entityframe5_database_t *d, int framenum)
2163 // scan for packets made obsolete by this ack and delete them
2164 for (i = 0;i < ENTITYFRAME5_MAXPACKETLOGS;i++)
2165 if (d->packetlog[i].packetnumber <= framenum)
2166 d->packetlog[i].packetnumber = 0;
2169 int entityframe5_prioritychaincounts[E5_PROTOCOL_PRIORITYLEVELS];
2170 unsigned short entityframe5_prioritychains[E5_PROTOCOL_PRIORITYLEVELS][ENTITYFRAME5_MAXSTATES];
2172 void EntityFrame5_WriteFrame(sizebuf_t *msg, entityframe5_database_t *d, int numstates, const entity_state_t *states, int viewentnum, int *stats, int movesequence)
2174 const entity_state_t *n;
2175 int i, num, l, framenum, packetlognumber, priority;
2177 unsigned char data[128];
2178 entityframe5_packetlog_t *packetlog;
2180 if (prog->max_edicts > d->maxedicts)
2181 EntityFrame5_ExpandEdicts(d, prog->max_edicts);
2183 framenum = d->latestframenum + 1;
2184 d->viewentnum = viewentnum;
2186 // if packet log is full, mark all frames as lost, this will cause
2187 // it to send the lost data again
2188 for (packetlognumber = 0;packetlognumber < ENTITYFRAME5_MAXPACKETLOGS;packetlognumber++)
2189 if (d->packetlog[packetlognumber].packetnumber == 0)
2191 if (packetlognumber == ENTITYFRAME5_MAXPACKETLOGS)
2193 Con_DPrintf("EntityFrame5_WriteFrame: packetlog overflow for a client, resetting\n");
2194 EntityFrame5_LostFrame(d, framenum);
2195 packetlognumber = 0;
2198 // prepare the buffer
2199 memset(&buf, 0, sizeof(buf));
2201 buf.maxsize = sizeof(data);
2203 // detect changes in stats
2204 for (i = 0;i < MAX_CL_STATS;i++)
2206 if (d->stats[i] != stats[i])
2208 d->statsdeltabits[i>>3] |= (1<<(i&7));
2209 d->stats[i] = stats[i];
2213 // detect changes in states
2215 for (i = 0, n = states;i < numstates;i++, n++)
2217 // mark gaps in entity numbering as removed entities
2218 for (;num < n->number;num++)
2220 // if the entity used to exist, clear it
2221 if (CHECKPVSBIT(d->visiblebits, num))
2223 CLEARPVSBIT(d->visiblebits, num);
2224 d->deltabits[num] = E5_FULLUPDATE;
2225 d->priorities[num] = EntityState5_Priority(d, num);
2226 d->states[num] = defaultstate;
2227 d->states[num].number = num;
2230 // update the entity state data
2231 if (!CHECKPVSBIT(d->visiblebits, num))
2233 // entity just spawned in, don't let it completely hog priority
2234 // because of being ancient on the first frame
2235 d->updateframenum[num] = framenum;
2237 SETPVSBIT(d->visiblebits, num);
2238 d->deltabits[num] |= EntityState5_DeltaBits(d->states + num, n);
2239 d->priorities[num] = EntityState5_Priority(d, num);
2240 d->states[num] = *n;
2241 d->states[num].number = num;
2242 // advance to next entity so the next iteration doesn't immediately remove it
2245 // all remaining entities are dead
2246 for (;num < d->maxedicts;num++)
2248 if (CHECKPVSBIT(d->visiblebits, num))
2250 CLEARPVSBIT(d->visiblebits, num);
2251 d->deltabits[num] = E5_FULLUPDATE;
2252 d->priorities[num] = EntityState5_Priority(d, num);
2253 d->states[num] = defaultstate;
2254 d->states[num].number = num;
2258 // if there isn't at least enough room for an empty svc_entities,
2259 // don't bother trying...
2260 if (buf.cursize + 11 > buf.maxsize)
2263 // build lists of entities by priority level
2264 memset(entityframe5_prioritychaincounts, 0, sizeof(entityframe5_prioritychaincounts));
2266 for (num = 0;num < d->maxedicts;num++)
2268 if (d->priorities[num])
2271 priority = d->priorities[num];
2272 if (entityframe5_prioritychaincounts[priority] < ENTITYFRAME5_MAXSTATES)
2273 entityframe5_prioritychains[priority][entityframe5_prioritychaincounts[priority]++] = num;
2277 // add packetlog entry
2278 packetlog = d->packetlog + packetlognumber;
2279 packetlog->packetnumber = framenum;
2280 packetlog->numstates = 0;
2281 // write stat updates
2282 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)
2284 for (i = 0;i < MAX_CL_STATS && msg->cursize + 6 + 11 <= msg->maxsize;i++)
2286 if (d->statsdeltabits[i>>3] & (1<<(i&7)))
2288 d->statsdeltabits[i>>3] &= ~(1<<(i&7));
2289 packetlog->statsdeltabits[i>>3] |= (1<<(i&7));
2290 if (d->stats[i] >= 0 && d->stats[i] < 256)
2292 MSG_WriteByte(msg, svc_updatestatubyte);
2293 MSG_WriteByte(msg, i);
2294 MSG_WriteByte(msg, d->stats[i]);
2298 MSG_WriteByte(msg, svc_updatestat);
2299 MSG_WriteByte(msg, i);
2300 MSG_WriteLong(msg, d->stats[i]);
2305 // write state updates
2306 d->latestframenum = framenum;
2307 MSG_WriteByte(msg, svc_entities);
2308 MSG_WriteLong(msg, framenum);
2309 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)
2310 MSG_WriteLong(msg, movesequence);
2311 for (priority = E5_PROTOCOL_PRIORITYLEVELS - 1;priority >= 0 && packetlog->numstates < ENTITYFRAME5_MAXSTATES;priority--)
2313 for (i = 0;i < entityframe5_prioritychaincounts[priority] && packetlog->numstates < ENTITYFRAME5_MAXSTATES;i++)
2315 num = entityframe5_prioritychains[priority][i];
2316 n = d->states + num;
2317 if (d->deltabits[num] & E5_FULLUPDATE)
2318 d->deltabits[num] = E5_FULLUPDATE | EntityState5_DeltaBits(&defaultstate, n);
2320 EntityState5_WriteUpdate(num, n, d->deltabits[num], &buf);
2321 // if the entity won't fit, try the next one
2322 if (msg->cursize + buf.cursize + 2 > msg->maxsize)
2324 // write entity to the packet
2325 SZ_Write(msg, buf.data, buf.cursize);
2326 // mark age on entity for prioritization
2327 d->updateframenum[num] = framenum;
2328 // log entity so deltabits can be restored later if lost
2329 packetlog->states[packetlog->numstates].number = num;
2330 packetlog->states[packetlog->numstates].bits = d->deltabits[num];
2331 packetlog->numstates++;
2332 // clear deltabits and priority so it won't be sent again
2333 d->deltabits[num] = 0;
2334 d->priorities[num] = 0;
2337 MSG_WriteShort(msg, 0x8000);