+#define E5_PROTOCOL_PRIORITYLEVELS 32
+
+// this is 88 bytes (must match entity_state_t in protocol.h)
+entity_state_t defaultstate =
+{
+ // ! means this is not sent to client
+ 0,//double time; // ! time this state was built (used on client for interpolation)
+ {0,0,0},//float origin[3];
+ {0,0,0},//float angles[3];
+ 0,//int number; // entity number this state is for
+ 0,//int effects;
+ 0,//unsigned int customizeentityforclient; // !
+ 0,//unsigned short modelindex;
+ 0,//unsigned short frame;
+ 0,//unsigned short tagentity;
+ 0,//unsigned short specialvisibilityradius; // ! larger if it has effects/light
+ 0,//unsigned short viewmodelforclient; // !
+ 0,//unsigned short exteriormodelforclient; // ! not shown if first person viewing from this entity, shown in all other cases
+ 0,//unsigned short nodrawtoclient; // !
+ 0,//unsigned short drawonlytoclient; // !
+ {0,0,0,0},//unsigned short light[4]; // color*256 (0.00 to 255.996), and radius*1
+ 0,//unsigned char active; // true if a valid state
+ 0,//unsigned char lightstyle;
+ 0,//unsigned char lightpflags;
+ 0,//unsigned char colormap;
+ 0,//unsigned char skin; // also chooses cubemap for rtlights if lightpflags & LIGHTPFLAGS_FULLDYNAMIC
+ 255,//unsigned char alpha;
+ 16,//unsigned char scale;
+ 0,//unsigned char glowsize;
+ 254,//unsigned char glowcolor;
+ 0,//unsigned char flags;
+ 0,//unsigned char tagindex;
+ {32, 32, 32},//unsigned char colormod[3];
+ // padding to a multiple of 8 bytes (to align the double time)
+ {0,0,0,0,0,0}//unsigned char unused[6]; // !
+};
+
+// LordHavoc: I own protocol ranges 96, 97, 3500-3599
+
+struct protocolversioninfo_s
+{
+ int number;
+ const char *name;
+}
+protocolversioninfo[] =
+{
+ {0, "UNKNOWN"},
+ {3504, "DP7"},
+ {3503, "DP6"},
+ {3502, "DP5"},
+ {3501, "DP4"},
+ {3500, "DP3"},
+ {97, "DP2"},
+ {96, "DP1"},
+ {15, "QUAKEDP"},
+ {250, "NEHAHRAMOVIE"},
+ {15, "QUAKE"},
+ {28, "QUAKEWORLD"},
+ {0, NULL}
+};
+
+static mempool_t *sv2csqc = NULL;
+int csqc_clent = 0;
+sizebuf_t *sv2csqcbuf = NULL;
+static unsigned char *sv2csqcents_version[MAX_SCOREBOARD];
+
+static entity_frame_t deltaframe; // FIXME?
+static entity_frame_t framedata; // FIXME?
+
+int entityframe5_prioritychaincounts[E5_PROTOCOL_PRIORITYLEVELS];
+unsigned short entityframe5_prioritychains[E5_PROTOCOL_PRIORITYLEVELS][ENTITYFRAME5_MAXSTATES];
+
+protocolversion_t Protocol_EnumForName(const char *s)
+{
+ int i;
+ for (i = 1;protocolversioninfo[i].name;i++)
+ if (!strcasecmp(s, protocolversioninfo[i].name))
+ return (protocolversion_t)i;
+ return PROTOCOL_UNKNOWN;
+}
+
+const char *Protocol_NameForEnum(protocolversion_t p)
+{
+ return protocolversioninfo[p].name;
+}
+
+protocolversion_t Protocol_EnumForNumber(int n)
+{
+ int i;
+ for (i = 1;protocolversioninfo[i].name;i++)
+ if (protocolversioninfo[i].number == n)
+ return (protocolversion_t)i;
+ return PROTOCOL_UNKNOWN;
+}
+
+int Protocol_NumberForEnum(protocolversion_t p)
+{
+ return protocolversioninfo[p].number;
+}
+
+void Protocol_Names(char *buffer, size_t buffersize)
+{
+ int i;
+ if (buffersize < 1)
+ return;
+ buffer[0] = 0;
+ for (i = 1;protocolversioninfo[i].name;i++)
+ {
+ if (i > 1)
+ strlcat(buffer, " ", sizeof(buffer));
+ strlcat(buffer, protocolversioninfo[i].name, sizeof(buffer));
+ }
+}
+
+void EntityFrameQuake_ReadEntity(int bits)
+{
+ int num;
+ entity_t *ent;
+ entity_state_t s;
+
+ if (bits & U_MOREBITS)
+ bits |= (MSG_ReadByte()<<8);
+ if ((bits & U_EXTEND1) && cls.protocol != PROTOCOL_NEHAHRAMOVIE)
+ {
+ bits |= MSG_ReadByte() << 16;
+ if (bits & U_EXTEND2)
+ bits |= MSG_ReadByte() << 24;
+ }
+
+ if (bits & U_LONGENTITY)
+ num = (unsigned short) MSG_ReadShort ();
+ else
+ num = MSG_ReadByte ();
+
+ if (num >= MAX_EDICTS)
+ Host_Error("EntityFrameQuake_ReadEntity: entity number (%i) >= MAX_EDICTS (%i)", num, MAX_EDICTS);
+ if (num < 1)
+ Host_Error("EntityFrameQuake_ReadEntity: invalid entity number (%i)", num);
+
+ if (cl.num_entities <= num)
+ {
+ cl.num_entities = num + 1;
+ if (num >= cl.max_entities)
+ CL_ExpandEntities(num);
+ }
+
+ ent = cl.entities + num;
+
+ // note: this inherits the 'active' state of the baseline chosen
+ // (state_baseline is always active, state_current may not be active if
+ // the entity was missing in the last frame)
+ if (bits & U_DELTA)
+ s = ent->state_current;
+ else
+ {
+ s = ent->state_baseline;
+ s.active = true;
+ }
+
+ cl.isquakeentity[num] = true;
+ if (cl.lastquakeentity < num)
+ cl.lastquakeentity = num;
+ s.number = num;
+ s.time = cl.mtime[0];
+ s.flags = 0;
+ if (bits & U_MODEL) s.modelindex = (s.modelindex & 0xFF00) | MSG_ReadByte();
+ if (bits & U_FRAME) s.frame = (s.frame & 0xFF00) | MSG_ReadByte();
+ if (bits & U_COLORMAP) s.colormap = MSG_ReadByte();
+ if (bits & U_SKIN) s.skin = MSG_ReadByte();
+ if (bits & U_EFFECTS) s.effects = (s.effects & 0xFF00) | MSG_ReadByte();
+ if (bits & U_ORIGIN1) s.origin[0] = MSG_ReadCoord(cls.protocol);
+ if (bits & U_ANGLE1) s.angles[0] = MSG_ReadAngle(cls.protocol);
+ if (bits & U_ORIGIN2) s.origin[1] = MSG_ReadCoord(cls.protocol);
+ if (bits & U_ANGLE2) s.angles[1] = MSG_ReadAngle(cls.protocol);
+ if (bits & U_ORIGIN3) s.origin[2] = MSG_ReadCoord(cls.protocol);
+ if (bits & U_ANGLE3) s.angles[2] = MSG_ReadAngle(cls.protocol);
+ if (bits & U_STEP) s.flags |= RENDER_STEP;
+ if (bits & U_ALPHA) s.alpha = MSG_ReadByte();
+ if (bits & U_SCALE) s.scale = MSG_ReadByte();
+ if (bits & U_EFFECTS2) s.effects = (s.effects & 0x00FF) | (MSG_ReadByte() << 8);
+ if (bits & U_GLOWSIZE) s.glowsize = MSG_ReadByte();
+ if (bits & U_GLOWCOLOR) s.glowcolor = MSG_ReadByte();
+ 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));}
+ if (bits & U_GLOWTRAIL) s.flags |= RENDER_GLOWTRAIL;
+ if (bits & U_FRAME2) s.frame = (s.frame & 0x00FF) | (MSG_ReadByte() << 8);
+ if (bits & U_MODEL2) s.modelindex = (s.modelindex & 0x00FF) | (MSG_ReadByte() << 8);
+ if (bits & U_VIEWMODEL) s.flags |= RENDER_VIEWMODEL;
+ if (bits & U_EXTERIORMODEL) s.flags |= RENDER_EXTERIORMODEL;
+
+ // LordHavoc: to allow playback of the Nehahra movie
+ if (cls.protocol == PROTOCOL_NEHAHRAMOVIE && (bits & U_EXTEND1))
+ {
+ // LordHavoc: evil format
+ int i = (int)MSG_ReadFloat();
+ int j = (int)(MSG_ReadFloat() * 255.0f);
+ if (i == 2)
+ {
+ i = (int)MSG_ReadFloat();
+ if (i)
+ s.effects |= EF_FULLBRIGHT;
+ }
+ if (j < 0)
+ s.alpha = 0;
+ else if (j == 0 || j >= 255)
+ s.alpha = 255;
+ else
+ s.alpha = j;
+ }
+
+ ent->state_previous = ent->state_current;
+ ent->state_current = s;
+ if (ent->state_current.active)
+ {
+ CL_MoveLerpEntityStates(ent);
+ cl.entities_active[ent->state_current.number] = true;
+ }
+
+ if (msg_badread)
+ Host_Error("EntityFrameQuake_ReadEntity: read error");
+}
+
+void EntityFrameQuake_ISeeDeadEntities(void)
+{
+ int num, lastentity;
+ if (cl.lastquakeentity == 0)
+ return;
+ lastentity = cl.lastquakeentity;
+ cl.lastquakeentity = 0;
+ for (num = 0;num <= lastentity;num++)
+ {
+ if (cl.isquakeentity[num])
+ {
+ if (cl.entities_active[num] && cl.entities[num].state_current.time == cl.mtime[0])
+ {
+ cl.isquakeentity[num] = true;
+ cl.lastquakeentity = num;
+ }
+ else
+ {
+ cl.isquakeentity[num] = false;
+ cl.entities_active[num] = false;
+ cl.entities[num].state_current = defaultstate;
+ cl.entities[num].state_current.number = num;
+ }
+ }
+ }
+}
+
+void EntityFrameCSQC_ClearVersions (void)
+{
+ if(sv2csqc)
+ {
+ Mem_FreePool(&sv2csqc);
+ sv2csqc = NULL;
+ }
+ memset(sv2csqcents_version, 0, MAX_SCOREBOARD*sizeof(unsigned char *));
+}
+
+void EntityFrameCSQC_InitClientVersions (int client, qboolean clear)
+{
+ if(!sv2csqc)
+ sv2csqc = Mem_AllocPool("SV2CSQC", 0, NULL);
+ if(sv2csqcents_version[client])
+ {
+ Mem_Free(sv2csqcents_version[client]);
+ sv2csqcents_version[client] = NULL;
+ }
+ sv2csqcents_version[client] = (unsigned char *)Mem_Alloc(sv2csqc, MAX_EDICTS);
+ memset(sv2csqcents_version[client], 0, MAX_EDICTS);
+}
+
+//[515]: we use only one array per-client for SendEntity feature
+void EntityFrameCSQC_WriteFrame (sizebuf_t *msg, int numstates, const entity_state_t *states)
+{
+ sizebuf_t buf;
+ unsigned char data[2048];
+ const entity_state_t *s;
+ unsigned short i, t, t2, t0;
+ prvm_eval_t *val, *val2;
+ int csqcents = 0;
+
+ if(!eval_SendEntity || !eval_Version)
+ return;
+ --csqc_clent;
+ if(!sv2csqcents_version[csqc_clent])
+ EntityFrameCSQC_InitClientVersions(csqc_clent, false);
+
+ for (csqcents = i = 0, s = states;i < numstates;i++, s++)
+ {
+ //[515]: entities remove
+ if(i+1 >= numstates)
+ t2 = prog->num_edicts;
+ else
+ t2 = states[i+1].number;
+ if(!i)
+ {
+ t0 = 1;
+ t2 = s->number;
+ }
+ else
+ t0 = s->number+1;
+ for(t=t0; t<t2 ;t++)
+ if(sv2csqcents_version[csqc_clent][t])
+ {
+ if(!csqcents)
+ {
+ csqcents = 1;
+ memset(&buf, 0, sizeof(buf));
+ buf.data = data;
+ buf.maxsize = sizeof(data);
+ sv2csqcbuf = &buf;
+ SZ_Clear(&buf);
+ MSG_WriteByte(&buf, svc_csqcentities);
+ }
+ sv2csqcents_version[csqc_clent][t] = 0;
+ MSG_WriteShort(&buf, (unsigned short)t | 0x8000);
+ csqcents++;
+ }
+ //[515]: entities remove
+
+// if(!s->active)
+// continue;
+ val = PRVM_GETEDICTFIELDVALUE((&prog->edicts[s->number]), eval_SendEntity);
+ if(val->function)
+ {
+ val2 = PRVM_GETEDICTFIELDVALUE((&prog->edicts[s->number]), eval_Version);
+ if(sv2csqcents_version[csqc_clent][s->number] == (unsigned char)val2->_float)
+ continue;
+ if(!csqcents)
+ {
+ csqcents = 1;
+ memset(&buf, 0, sizeof(buf));
+ buf.data = data;
+ buf.maxsize = sizeof(data);
+ sv2csqcbuf = &buf;
+ SZ_Clear(&buf);
+ MSG_WriteByte(&buf, svc_csqcentities);
+ }
+ if((unsigned char)val2->_float == 0)
+ val2->_float = 1;
+ MSG_WriteShort(&buf, s->number);
+ ((int *)prog->globals.generic)[OFS_PARM0] = csqc_clent+1;
+ prog->globals.server->self = s->number;
+ PRVM_ExecuteProgram(val->function, "Null SendEntity\n");
+ if(!prog->globals.generic[OFS_RETURN])
+ {
+ buf.cursize -= 2;
+ if(sv2csqcents_version[csqc_clent][s->number])
+ {
+ sv2csqcents_version[csqc_clent][s->number] = 0;
+ MSG_WriteShort(&buf, (unsigned short)s->number | 0x8000);
+ csqcents++;
+ }
+ }
+ else
+ {
+ sv2csqcents_version[csqc_clent][s->number] = (unsigned char)val2->_float;
+ csqcents++;
+ }
+ if (msg->cursize + buf.cursize > msg->maxsize)
+ break;
+ }
+ }
+ if(csqcents)
+ {
+ if(csqcents > 1)
+ {
+ MSG_WriteShort(&buf, 0);
+ SZ_Write(msg, buf.data, buf.cursize);
+ }
+ sv2csqcbuf = NULL;
+ }
+}
+
+void EntityFrameQuake_WriteFrame(sizebuf_t *msg, int numstates, const entity_state_t *states)
+{
+ const entity_state_t *s;
+ entity_state_t baseline;
+ int i, bits;
+ sizebuf_t buf;
+ unsigned char data[128];
+ prvm_eval_t *val;
+
+ // prepare the buffer
+ memset(&buf, 0, sizeof(buf));
+ buf.data = data;
+ buf.maxsize = sizeof(data);
+
+ for (i = 0, s = states;i < numstates;i++, s++)
+ {
+ val = PRVM_GETEDICTFIELDVALUE((&prog->edicts[s->number]), eval_SendEntity);
+ if(val && val->function)
+ continue;
+
+ // prepare the buffer
+ SZ_Clear(&buf);
+
+// send an update
+ bits = 0;
+ if (s->number >= 256)
+ bits |= U_LONGENTITY;
+ if (s->flags & RENDER_STEP)
+ bits |= U_STEP;
+ if (s->flags & RENDER_VIEWMODEL)
+ bits |= U_VIEWMODEL;
+ if (s->flags & RENDER_GLOWTRAIL)
+ bits |= U_GLOWTRAIL;
+ if (s->flags & RENDER_EXTERIORMODEL)
+ bits |= U_EXTERIORMODEL;
+
+ // LordHavoc: old stuff, but rewritten to have more exact tolerances
+ baseline = prog->edicts[s->number].priv.server->baseline;
+ if (baseline.origin[0] != s->origin[0])
+ bits |= U_ORIGIN1;
+ if (baseline.origin[1] != s->origin[1])
+ bits |= U_ORIGIN2;
+ if (baseline.origin[2] != s->origin[2])
+ bits |= U_ORIGIN3;
+ if (baseline.angles[0] != s->angles[0])
+ bits |= U_ANGLE1;
+ if (baseline.angles[1] != s->angles[1])
+ bits |= U_ANGLE2;
+ if (baseline.angles[2] != s->angles[2])
+ bits |= U_ANGLE3;
+ if (baseline.colormap != s->colormap)
+ bits |= U_COLORMAP;
+ if (baseline.skin != s->skin)
+ bits |= U_SKIN;
+ if (baseline.frame != s->frame)
+ {
+ bits |= U_FRAME;
+ if (s->frame & 0xFF00)
+ bits |= U_FRAME2;
+ }
+ if (baseline.effects != s->effects)
+ {
+ bits |= U_EFFECTS;
+ if (s->effects & 0xFF00)
+ bits |= U_EFFECTS2;
+ }
+ if (baseline.modelindex != s->modelindex)
+ {
+ bits |= U_MODEL;
+ if (s->modelindex & 0xFF00)
+ bits |= U_MODEL2;
+ }
+ if (baseline.alpha != s->alpha)
+ bits |= U_ALPHA;
+ if (baseline.scale != s->scale)
+ bits |= U_SCALE;
+ if (baseline.glowsize != s->glowsize)
+ bits |= U_GLOWSIZE;
+ if (baseline.glowcolor != s->glowcolor)
+ bits |= U_GLOWCOLOR;
+
+ // if extensions are disabled, clear the relevant update flags
+ if (sv.protocol == PROTOCOL_QUAKE || sv.protocol == PROTOCOL_NEHAHRAMOVIE)
+ bits &= 0x7FFF;
+ if (sv.protocol == PROTOCOL_NEHAHRAMOVIE)
+ if (s->alpha != 255 || s->effects & EF_FULLBRIGHT)
+ bits |= U_EXTEND1;
+
+ // write the message
+ if (bits >= 16777216)
+ bits |= U_EXTEND2;
+ if (bits >= 65536)
+ bits |= U_EXTEND1;
+ if (bits >= 256)
+ bits |= U_MOREBITS;
+ bits |= U_SIGNAL;
+
+ MSG_WriteByte (&buf, bits);
+ if (bits & U_MOREBITS) MSG_WriteByte(&buf, bits>>8);
+ if (bits & U_EXTEND1) MSG_WriteByte(&buf, bits>>16);
+ if (bits & U_EXTEND2) MSG_WriteByte(&buf, bits>>24);
+ if (bits & U_LONGENTITY) MSG_WriteShort(&buf, s->number);
+ else MSG_WriteByte(&buf, s->number);
+
+ if (bits & U_MODEL) MSG_WriteByte(&buf, s->modelindex);
+ if (bits & U_FRAME) MSG_WriteByte(&buf, s->frame);
+ if (bits & U_COLORMAP) MSG_WriteByte(&buf, s->colormap);
+ if (bits & U_SKIN) MSG_WriteByte(&buf, s->skin);
+ if (bits & U_EFFECTS) MSG_WriteByte(&buf, s->effects);
+ if (bits & U_ORIGIN1) MSG_WriteCoord(&buf, s->origin[0], sv.protocol);
+ if (bits & U_ANGLE1) MSG_WriteAngle(&buf, s->angles[0], sv.protocol);
+ if (bits & U_ORIGIN2) MSG_WriteCoord(&buf, s->origin[1], sv.protocol);
+ if (bits & U_ANGLE2) MSG_WriteAngle(&buf, s->angles[1], sv.protocol);
+ if (bits & U_ORIGIN3) MSG_WriteCoord(&buf, s->origin[2], sv.protocol);
+ if (bits & U_ANGLE3) MSG_WriteAngle(&buf, s->angles[2], sv.protocol);
+ if (bits & U_ALPHA) MSG_WriteByte(&buf, s->alpha);
+ if (bits & U_SCALE) MSG_WriteByte(&buf, s->scale);
+ if (bits & U_EFFECTS2) MSG_WriteByte(&buf, s->effects >> 8);
+ if (bits & U_GLOWSIZE) MSG_WriteByte(&buf, s->glowsize);
+ if (bits & U_GLOWCOLOR) MSG_WriteByte(&buf, s->glowcolor);
+ 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);}
+ if (bits & U_FRAME2) MSG_WriteByte(&buf, s->frame >> 8);
+ if (bits & U_MODEL2) MSG_WriteByte(&buf, s->modelindex >> 8);
+
+ // the nasty protocol
+ if ((bits & U_EXTEND1) && sv.protocol == PROTOCOL_NEHAHRAMOVIE)
+ {
+ if (s->effects & EF_FULLBRIGHT)
+ {
+ MSG_WriteFloat(&buf, 2); // QSG protocol version
+ MSG_WriteFloat(&buf, s->alpha <= 0 ? 0 : (s->alpha >= 255 ? 1 : s->alpha * (1.0f / 255.0f))); // alpha
+ MSG_WriteFloat(&buf, 1); // fullbright
+ }
+ else
+ {
+ MSG_WriteFloat(&buf, 1); // QSG protocol version
+ MSG_WriteFloat(&buf, s->alpha <= 0 ? 0 : (s->alpha >= 255 ? 1 : s->alpha * (1.0f / 255.0f))); // alpha
+ }
+ }
+
+ // if the commit is full, we're done this frame
+ if (msg->cursize + buf.cursize > msg->maxsize)
+ {
+ // next frame we will continue where we left off
+ break;
+ }
+ // write the message to the packet
+ SZ_Write(msg, buf.data, buf.cursize);
+ }
+}
+
+int EntityState_DeltaBits(const entity_state_t *o, const entity_state_t *n)
+{
+ unsigned int bits;
+ // if o is not active, delta from default
+ if (!o->active)
+ o = &defaultstate;
+ bits = 0;
+ if (fabs(n->origin[0] - o->origin[0]) > (1.0f / 256.0f))
+ bits |= E_ORIGIN1;
+ if (fabs(n->origin[1] - o->origin[1]) > (1.0f / 256.0f))
+ bits |= E_ORIGIN2;
+ if (fabs(n->origin[2] - o->origin[2]) > (1.0f / 256.0f))
+ bits |= E_ORIGIN3;
+ if ((unsigned char) (n->angles[0] * (256.0f / 360.0f)) != (unsigned char) (o->angles[0] * (256.0f / 360.0f)))
+ bits |= E_ANGLE1;
+ if ((unsigned char) (n->angles[1] * (256.0f / 360.0f)) != (unsigned char) (o->angles[1] * (256.0f / 360.0f)))
+ bits |= E_ANGLE2;
+ if ((unsigned char) (n->angles[2] * (256.0f / 360.0f)) != (unsigned char) (o->angles[2] * (256.0f / 360.0f)))
+ bits |= E_ANGLE3;
+ if ((n->modelindex ^ o->modelindex) & 0x00FF)
+ bits |= E_MODEL1;
+ if ((n->modelindex ^ o->modelindex) & 0xFF00)
+ bits |= E_MODEL2;
+ if ((n->frame ^ o->frame) & 0x00FF)
+ bits |= E_FRAME1;
+ if ((n->frame ^ o->frame) & 0xFF00)
+ bits |= E_FRAME2;
+ if ((n->effects ^ o->effects) & 0x00FF)
+ bits |= E_EFFECTS1;
+ if ((n->effects ^ o->effects) & 0xFF00)
+ bits |= E_EFFECTS2;
+ if (n->colormap != o->colormap)
+ bits |= E_COLORMAP;
+ if (n->skin != o->skin)
+ bits |= E_SKIN;
+ if (n->alpha != o->alpha)
+ bits |= E_ALPHA;
+ if (n->scale != o->scale)
+ bits |= E_SCALE;
+ if (n->glowsize != o->glowsize)
+ bits |= E_GLOWSIZE;
+ if (n->glowcolor != o->glowcolor)
+ bits |= E_GLOWCOLOR;
+ if (n->flags != o->flags)
+ bits |= E_FLAGS;
+ if (n->tagindex != o->tagindex || n->tagentity != o->tagentity)
+ bits |= E_TAGATTACHMENT;
+ 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])
+ bits |= E_LIGHT;
+ if (n->lightstyle != o->lightstyle)
+ bits |= E_LIGHTSTYLE;
+ if (n->lightpflags != o->lightpflags)
+ bits |= E_LIGHTPFLAGS;
+
+ if (bits)
+ {
+ if (bits & 0xFF000000)
+ bits |= 0x00800000;
+ if (bits & 0x00FF0000)
+ bits |= 0x00008000;
+ if (bits & 0x0000FF00)
+ bits |= 0x00000080;
+ }
+ return bits;
+}
+
+void EntityState_WriteExtendBits(sizebuf_t *msg, unsigned int bits)
+{
+ MSG_WriteByte(msg, bits & 0xFF);
+ if (bits & 0x00000080)
+ {
+ MSG_WriteByte(msg, (bits >> 8) & 0xFF);
+ if (bits & 0x00008000)
+ {
+ MSG_WriteByte(msg, (bits >> 16) & 0xFF);
+ if (bits & 0x00800000)
+ MSG_WriteByte(msg, (bits >> 24) & 0xFF);
+ }
+ }
+}
+
+void EntityState_WriteFields(const entity_state_t *ent, sizebuf_t *msg, unsigned int bits)
+{
+ if (sv.protocol == PROTOCOL_DARKPLACES2)
+ {
+ if (bits & E_ORIGIN1)
+ MSG_WriteCoord16i(msg, ent->origin[0]);
+ if (bits & E_ORIGIN2)
+ MSG_WriteCoord16i(msg, ent->origin[1]);
+ if (bits & E_ORIGIN3)
+ MSG_WriteCoord16i(msg, ent->origin[2]);
+ }
+ else
+ {
+ // LordHavoc: have to write flags first, as they can modify protocol
+ if (bits & E_FLAGS)
+ MSG_WriteByte(msg, ent->flags);
+ if (ent->flags & RENDER_LOWPRECISION)
+ {
+ if (bits & E_ORIGIN1)
+ MSG_WriteCoord16i(msg, ent->origin[0]);
+ if (bits & E_ORIGIN2)
+ MSG_WriteCoord16i(msg, ent->origin[1]);
+ if (bits & E_ORIGIN3)
+ MSG_WriteCoord16i(msg, ent->origin[2]);
+ }
+ else
+ {
+ if (bits & E_ORIGIN1)
+ MSG_WriteCoord32f(msg, ent->origin[0]);
+ if (bits & E_ORIGIN2)
+ MSG_WriteCoord32f(msg, ent->origin[1]);
+ if (bits & E_ORIGIN3)
+ MSG_WriteCoord32f(msg, ent->origin[2]);
+ }
+ }
+ if ((sv.protocol == PROTOCOL_DARKPLACES1 || sv.protocol == PROTOCOL_DARKPLACES2 || sv.protocol == PROTOCOL_DARKPLACES3 || sv.protocol == PROTOCOL_DARKPLACES4) && (ent->flags & RENDER_LOWPRECISION))
+ {
+ if (bits & E_ANGLE1)
+ MSG_WriteAngle8i(msg, ent->angles[0]);
+ if (bits & E_ANGLE2)
+ MSG_WriteAngle8i(msg, ent->angles[1]);
+ if (bits & E_ANGLE3)
+ MSG_WriteAngle8i(msg, ent->angles[2]);
+ }
+ else
+ {
+ if (bits & E_ANGLE1)
+ MSG_WriteAngle16i(msg, ent->angles[0]);
+ if (bits & E_ANGLE2)
+ MSG_WriteAngle16i(msg, ent->angles[1]);
+ if (bits & E_ANGLE3)
+ MSG_WriteAngle16i(msg, ent->angles[2]);
+ }
+ if (bits & E_MODEL1)
+ MSG_WriteByte(msg, ent->modelindex & 0xFF);
+ if (bits & E_MODEL2)
+ MSG_WriteByte(msg, (ent->modelindex >> 8) & 0xFF);
+ if (bits & E_FRAME1)
+ MSG_WriteByte(msg, ent->frame & 0xFF);
+ if (bits & E_FRAME2)
+ MSG_WriteByte(msg, (ent->frame >> 8) & 0xFF);
+ if (bits & E_EFFECTS1)
+ MSG_WriteByte(msg, ent->effects & 0xFF);
+ if (bits & E_EFFECTS2)
+ MSG_WriteByte(msg, (ent->effects >> 8) & 0xFF);
+ if (bits & E_COLORMAP)
+ MSG_WriteByte(msg, ent->colormap);
+ if (bits & E_SKIN)
+ MSG_WriteByte(msg, ent->skin);
+ if (bits & E_ALPHA)
+ MSG_WriteByte(msg, ent->alpha);
+ if (bits & E_SCALE)
+ MSG_WriteByte(msg, ent->scale);
+ if (bits & E_GLOWSIZE)
+ MSG_WriteByte(msg, ent->glowsize);
+ if (bits & E_GLOWCOLOR)
+ MSG_WriteByte(msg, ent->glowcolor);
+ if (sv.protocol == PROTOCOL_DARKPLACES2)
+ if (bits & E_FLAGS)
+ MSG_WriteByte(msg, ent->flags);
+ if (bits & E_TAGATTACHMENT)
+ {
+ MSG_WriteShort(msg, ent->tagentity);
+ MSG_WriteByte(msg, ent->tagindex);
+ }
+ if (bits & E_LIGHT)
+ {
+ MSG_WriteShort(msg, ent->light[0]);
+ MSG_WriteShort(msg, ent->light[1]);
+ MSG_WriteShort(msg, ent->light[2]);
+ MSG_WriteShort(msg, ent->light[3]);
+ }
+ if (bits & E_LIGHTSTYLE)
+ MSG_WriteByte(msg, ent->lightstyle);
+ if (bits & E_LIGHTPFLAGS)
+ MSG_WriteByte(msg, ent->lightpflags);
+}
+
+void EntityState_WriteUpdate(const entity_state_t *ent, sizebuf_t *msg, const entity_state_t *delta)
+{
+ unsigned int bits;
+ if (ent->active)
+ {
+ // entity is active, check for changes from the delta
+ if ((bits = EntityState_DeltaBits(delta, ent)))
+ {
+ // write the update number, bits, and fields
+ MSG_WriteShort(msg, ent->number);
+ EntityState_WriteExtendBits(msg, bits);
+ EntityState_WriteFields(ent, msg, bits);
+ }
+ }
+ else
+ {
+ // entity is inactive, check if the delta was active
+ if (delta->active)
+ {
+ // write the remove number
+ MSG_WriteShort(msg, ent->number | 0x8000);
+ }
+ }
+}
+
+int EntityState_ReadExtendBits(void)
+{
+ unsigned int bits;
+ bits = MSG_ReadByte();
+ if (bits & 0x00000080)
+ {
+ bits |= MSG_ReadByte() << 8;
+ if (bits & 0x00008000)
+ {
+ bits |= MSG_ReadByte() << 16;
+ if (bits & 0x00800000)
+ bits |= MSG_ReadByte() << 24;
+ }
+ }
+ return bits;
+}
+
+void EntityState_ReadFields(entity_state_t *e, unsigned int bits)
+{
+ if (cls.protocol == PROTOCOL_DARKPLACES2)
+ {
+ if (bits & E_ORIGIN1)
+ e->origin[0] = MSG_ReadCoord16i();
+ if (bits & E_ORIGIN2)
+ e->origin[1] = MSG_ReadCoord16i();
+ if (bits & E_ORIGIN3)
+ e->origin[2] = MSG_ReadCoord16i();
+ }
+ else
+ {
+ if (bits & E_FLAGS)
+ e->flags = MSG_ReadByte();
+ if (e->flags & RENDER_LOWPRECISION)
+ {
+ if (bits & E_ORIGIN1)
+ e->origin[0] = MSG_ReadCoord16i();
+ if (bits & E_ORIGIN2)
+ e->origin[1] = MSG_ReadCoord16i();
+ if (bits & E_ORIGIN3)
+ e->origin[2] = MSG_ReadCoord16i();
+ }
+ else
+ {
+ if (bits & E_ORIGIN1)
+ e->origin[0] = MSG_ReadCoord32f();
+ if (bits & E_ORIGIN2)
+ e->origin[1] = MSG_ReadCoord32f();
+ if (bits & E_ORIGIN3)
+ e->origin[2] = MSG_ReadCoord32f();
+ }
+ }
+ if ((cls.protocol == PROTOCOL_DARKPLACES5 || cls.protocol == PROTOCOL_DARKPLACES6) && !(e->flags & RENDER_LOWPRECISION))
+ {
+ if (bits & E_ANGLE1)
+ e->angles[0] = MSG_ReadAngle16i();
+ if (bits & E_ANGLE2)
+ e->angles[1] = MSG_ReadAngle16i();
+ if (bits & E_ANGLE3)
+ e->angles[2] = MSG_ReadAngle16i();
+ }
+ else
+ {
+ if (bits & E_ANGLE1)
+ e->angles[0] = MSG_ReadAngle8i();
+ if (bits & E_ANGLE2)
+ e->angles[1] = MSG_ReadAngle8i();
+ if (bits & E_ANGLE3)
+ e->angles[2] = MSG_ReadAngle8i();
+ }
+ if (bits & E_MODEL1)
+ e->modelindex = (e->modelindex & 0xFF00) | (unsigned int) MSG_ReadByte();
+ if (bits & E_MODEL2)
+ e->modelindex = (e->modelindex & 0x00FF) | ((unsigned int) MSG_ReadByte() << 8);
+ if (bits & E_FRAME1)
+ e->frame = (e->frame & 0xFF00) | (unsigned int) MSG_ReadByte();
+ if (bits & E_FRAME2)
+ e->frame = (e->frame & 0x00FF) | ((unsigned int) MSG_ReadByte() << 8);
+ if (bits & E_EFFECTS1)
+ e->effects = (e->effects & 0xFF00) | (unsigned int) MSG_ReadByte();
+ if (bits & E_EFFECTS2)
+ e->effects = (e->effects & 0x00FF) | ((unsigned int) MSG_ReadByte() << 8);
+ if (bits & E_COLORMAP)
+ e->colormap = MSG_ReadByte();
+ if (bits & E_SKIN)
+ e->skin = MSG_ReadByte();
+ if (bits & E_ALPHA)
+ e->alpha = MSG_ReadByte();
+ if (bits & E_SCALE)
+ e->scale = MSG_ReadByte();
+ if (bits & E_GLOWSIZE)
+ e->glowsize = MSG_ReadByte();
+ if (bits & E_GLOWCOLOR)
+ e->glowcolor = MSG_ReadByte();
+ if (cls.protocol == PROTOCOL_DARKPLACES2)
+ if (bits & E_FLAGS)
+ e->flags = MSG_ReadByte();
+ if (bits & E_TAGATTACHMENT)
+ {
+ e->tagentity = (unsigned short) MSG_ReadShort();
+ e->tagindex = MSG_ReadByte();
+ }
+ if (bits & E_LIGHT)
+ {
+ e->light[0] = (unsigned short) MSG_ReadShort();
+ e->light[1] = (unsigned short) MSG_ReadShort();
+ e->light[2] = (unsigned short) MSG_ReadShort();
+ e->light[3] = (unsigned short) MSG_ReadShort();
+ }
+ if (bits & E_LIGHTSTYLE)
+ e->lightstyle = MSG_ReadByte();
+ if (bits & E_LIGHTPFLAGS)
+ e->lightpflags = MSG_ReadByte();
+
+ if (developer_networkentities.integer >= 2)
+ {
+ Con_Printf("ReadFields e%i", e->number);
+
+ if (bits & E_ORIGIN1)
+ Con_Printf(" E_ORIGIN1 %f", e->origin[0]);
+ if (bits & E_ORIGIN2)
+ Con_Printf(" E_ORIGIN2 %f", e->origin[1]);
+ if (bits & E_ORIGIN3)
+ Con_Printf(" E_ORIGIN3 %f", e->origin[2]);
+ if (bits & E_ANGLE1)
+ Con_Printf(" E_ANGLE1 %f", e->angles[0]);
+ if (bits & E_ANGLE2)
+ Con_Printf(" E_ANGLE2 %f", e->angles[1]);
+ if (bits & E_ANGLE3)
+ Con_Printf(" E_ANGLE3 %f", e->angles[2]);
+ if (bits & (E_MODEL1 | E_MODEL2))
+ Con_Printf(" E_MODEL %i", e->modelindex);
+
+ if (bits & (E_FRAME1 | E_FRAME2))
+ Con_Printf(" E_FRAME %i", e->frame);
+ if (bits & (E_EFFECTS1 | E_EFFECTS2))
+ Con_Printf(" E_EFFECTS %i", e->effects);
+ if (bits & E_ALPHA)
+ Con_Printf(" E_ALPHA %f", e->alpha / 255.0f);
+ if (bits & E_SCALE)
+ Con_Printf(" E_SCALE %f", e->scale / 16.0f);
+ if (bits & E_COLORMAP)
+ Con_Printf(" E_COLORMAP %i", e->colormap);
+ if (bits & E_SKIN)
+ Con_Printf(" E_SKIN %i", e->skin);
+
+ if (bits & E_GLOWSIZE)
+ Con_Printf(" E_GLOWSIZE %i", e->glowsize * 4);
+ if (bits & E_GLOWCOLOR)
+ Con_Printf(" E_GLOWCOLOR %i", e->glowcolor);
+
+ if (bits & E_LIGHT)
+ Con_Printf(" E_LIGHT %i:%i:%i:%i", e->light[0], e->light[1], e->light[2], e->light[3]);
+ if (bits & E_LIGHTPFLAGS)
+ Con_Printf(" E_LIGHTPFLAGS %i", e->lightpflags);
+
+ if (bits & E_TAGATTACHMENT)
+ Con_Printf(" E_TAGATTACHMENT e%i:%i", e->tagentity, e->tagindex);
+ if (bits & E_LIGHTSTYLE)
+ Con_Printf(" E_LIGHTSTYLE %i", e->lightstyle);
+ Con_Print("\n");
+ }
+}
+
+// (client and server) allocates a new empty database
+entityframe_database_t *EntityFrame_AllocDatabase(mempool_t *mempool)
+{
+ return (entityframe_database_t *)Mem_Alloc(mempool, sizeof(entityframe_database_t));
+}
+
+// (client and server) frees the database
+void EntityFrame_FreeDatabase(entityframe_database_t *d)