]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - protocol.c
Added a mempool parameter to FS_LoadFile
[xonotic/darkplaces.git] / protocol.c
index 32624a4f0d2b1ee932492761f4ddbc54bb08156d..3278be058b49c5f5375261305b7b3338fcb96224 100644 (file)
@@ -1,12 +1,45 @@
 
 #include "quakedef.h"
 
+entity_state_t defaultstate =
+{
+       0,//double time; // time this state was built
+       {0,0,0},//vec3_t origin;
+       {0,0,0},//vec3_t angles;
+       0,//int number; // entity number this state is for
+       0,//unsigned short active; // true if a valid state
+       0,//unsigned short modelindex;
+       0,//unsigned short frame;
+       0,//unsigned short effects;
+       0,//unsigned short tagentity;
+       0,//unsigned short specialvisibilityradius;
+       0,//unsigned short viewmodelforclient;
+       0,//unsigned short exteriormodelforclient;
+       0,//unsigned short nodrawtoclient;
+       0,//unsigned short drawonlytoclient;
+       {0,0,0,0},//short light[4];
+       0,//qbyte lightstyle;
+       0,//qbyte lightpflags;
+       0,//qbyte colormap;
+       0,//qbyte skin;
+       255,//qbyte alpha;
+       16,//qbyte scale;
+       0,//qbyte glowsize;
+       254,//qbyte glowcolor;
+       0,//qbyte flags;
+       0,//qbyte tagindex;
+       {0,0,0,0,0,0}//qbyte unused[6];
+};
+
 void ClearStateToDefault(entity_state_t *s)
 {
+       *s = defaultstate;
+       /*
        memset(s, 0, sizeof(*s));
        s->alpha = 255;
        s->scale = 16;
        s->glowcolor = 254;
+       */
 }
 
 void EntityState_Write(entity_state_t *ent, sizebuf_t *msg, entity_state_t *delta)
@@ -15,6 +48,9 @@ void EntityState_Write(entity_state_t *ent, sizebuf_t *msg, entity_state_t *delt
        vec3_t org, deltaorg;
        if (ent->active)
        {
+               // if not active last frame, delta from defaults
+               if (!delta->active)
+                       delta = &defaultstate;
                bits = 0;
                VectorCopy(ent->origin, org);
                VectorCopy(delta->origin, deltaorg);
@@ -88,6 +124,12 @@ void EntityState_Write(entity_state_t *ent, sizebuf_t *msg, entity_state_t *delt
                        bits |= E_FLAGS;
                if (ent->tagindex != delta->tagindex || ent->tagentity != delta->tagentity)
                        bits |= E_TAGATTACHMENT;
+               if (ent->light[0] != delta->light[0] || ent->light[1] != delta->light[1] || ent->light[2] != delta->light[2] || ent->light[3] != delta->light[3])
+                       bits |= E_LIGHT;
+               if (ent->lightstyle != delta->lightstyle)
+                       bits |= E_LIGHTSTYLE;
+               if (ent->lightpflags != delta->lightpflags)
+                       bits |= E_LIGHTPFLAGS;
 
                if (bits) // don't send anything if it hasn't changed
                {
@@ -121,6 +163,12 @@ void EntityState_Write(entity_state_t *ent, sizebuf_t *msg, entity_state_t *delt
                                        MSG_WriteShort(msg, org[1]);
                                if (bits & E_ORIGIN3)
                                        MSG_WriteShort(msg, org[2]);
+                               if (bits & E_ANGLE1)
+                                       MSG_WriteAngle(msg, ent->angles[0]);
+                               if (bits & E_ANGLE2)
+                                       MSG_WriteAngle(msg, ent->angles[1]);
+                               if (bits & E_ANGLE3)
+                                       MSG_WriteAngle(msg, ent->angles[2]);
                        }
                        else
                        {
@@ -130,13 +178,13 @@ void EntityState_Write(entity_state_t *ent, sizebuf_t *msg, entity_state_t *delt
                                        MSG_WriteFloat(msg, org[1]);
                                if (bits & E_ORIGIN3)
                                        MSG_WriteFloat(msg, org[2]);
+                               if (bits & E_ANGLE1)
+                                       MSG_WritePreciseAngle(msg, ent->angles[0]);
+                               if (bits & E_ANGLE2)
+                                       MSG_WritePreciseAngle(msg, ent->angles[1]);
+                               if (bits & E_ANGLE3)
+                                       MSG_WritePreciseAngle(msg, ent->angles[2]);
                        }
-                       if (bits & E_ANGLE1)
-                               MSG_WriteAngle(msg, ent->angles[0]);
-                       if (bits & E_ANGLE2)
-                               MSG_WriteAngle(msg, ent->angles[1]);
-                       if (bits & E_ANGLE3)
-                               MSG_WriteAngle(msg, ent->angles[2]);
                        if (bits & E_MODEL1)
                                MSG_WriteByte(msg, ent->modelindex & 0xFF);
                        if (bits & E_MODEL2)
@@ -166,16 +214,27 @@ void EntityState_Write(entity_state_t *ent, sizebuf_t *msg, entity_state_t *delt
                                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);
                }
        }
-       else if (!delta->active)
+       else if (delta->active)
                MSG_WriteShort(msg, ent->number | 0x8000);
 }
 
-void EntityState_Read(entity_state_t *e, entity_state_t *delta, int number)
+void EntityState_ReadUpdate(entity_state_t *e, int number)
 {
        int bits;
-       memcpy(e, delta, sizeof(*e));
+       cl_entities_active[number] = true;
        e->active = true;
        e->time = cl.mtime[0];
        e->number = number;
@@ -192,7 +251,7 @@ void EntityState_Read(entity_state_t *e, entity_state_t *delta, int number)
                }
        }
 
-       if (dpprotocol == DPPROTOCOL_VERSION2)
+       if (cl.protocol == PROTOCOL_DARKPLACES2)
        {
                if (bits & E_ORIGIN1)
                        e->origin[0] = (signed short) MSG_ReadShort();
@@ -205,7 +264,7 @@ void EntityState_Read(entity_state_t *e, entity_state_t *delta, int number)
        {
                if (bits & E_FLAGS)
                        e->flags = MSG_ReadByte();
-               if (e->flags & RENDER_LOWPRECISION || dpprotocol == DPPROTOCOL_VERSION2)
+               if (e->flags & RENDER_LOWPRECISION || cl.protocol == PROTOCOL_DARKPLACES2)
                {
                        if (bits & E_ORIGIN1)
                                e->origin[0] = (signed short) MSG_ReadShort();
@@ -224,12 +283,24 @@ void EntityState_Read(entity_state_t *e, entity_state_t *delta, int number)
                                e->origin[2] = MSG_ReadFloat();
                }
        }
-       if (bits & E_ANGLE1)
-               e->angles[0] = MSG_ReadAngle();
-       if (bits & E_ANGLE2)
-               e->angles[1] = MSG_ReadAngle();
-       if (bits & E_ANGLE3)
-               e->angles[2] = MSG_ReadAngle();
+       if (cl.protocol == PROTOCOL_DARKPLACES5 && !(e->flags & RENDER_LOWPRECISION))
+       {
+               if (bits & E_ANGLE1)
+                       e->angles[0] = MSG_ReadPreciseAngle();
+               if (bits & E_ANGLE2)
+                       e->angles[1] = MSG_ReadPreciseAngle();
+               if (bits & E_ANGLE3)
+                       e->angles[2] = MSG_ReadPreciseAngle();
+       }
+       else
+       {
+               if (bits & E_ANGLE1)
+                       e->angles[0] = MSG_ReadAngle();
+               if (bits & E_ANGLE2)
+                       e->angles[1] = MSG_ReadAngle();
+               if (bits & E_ANGLE3)
+                       e->angles[2] = MSG_ReadAngle();
+       }
        if (bits & E_MODEL1)
                e->modelindex = (e->modelindex & 0xFF00) | (unsigned int) MSG_ReadByte();
        if (bits & E_MODEL2)
@@ -254,7 +325,7 @@ void EntityState_Read(entity_state_t *e, entity_state_t *delta, int number)
                e->glowsize = MSG_ReadByte();
        if (bits & E_GLOWCOLOR)
                e->glowcolor = MSG_ReadByte();
-       if (dpprotocol == DPPROTOCOL_VERSION2)
+       if (cl.protocol == PROTOCOL_DARKPLACES2)
                if (bits & E_FLAGS)
                        e->flags = MSG_ReadByte();
        if (bits & E_TAGATTACHMENT)
@@ -262,6 +333,66 @@ void EntityState_Read(entity_state_t *e, entity_state_t *delta, int number)
                e->tagentity = MSG_ReadShort();
                e->tagindex = MSG_ReadByte();
        }
+       if (bits & E_LIGHT)
+       {
+               e->light[0] = MSG_ReadShort();
+               e->light[1] = MSG_ReadShort();
+               e->light[2] = MSG_ReadShort();
+               e->light[3] = 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("ReadUpdate e%i", 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");
+       }
 }
 
 // (server) clears the database to contain no frames (thus delta compression compresses against nothing)
@@ -360,9 +491,9 @@ void EntityFrame_AddFrame(entity_database_t *d, entity_frame_t *f)
                if (e >= d->frames[n].framenum)
                {
                        if (e == f->framenum)
-                               Con_Printf("EntityFrame_AddFrame: tried to add out of sequence frame to database\n");
+                               Con_Print("EntityFrame_AddFrame: tried to add out of sequence frame to database\n");
                        else
-                               Con_Printf("EntityFrame_AddFrame: out of sequence frames in database\n");
+                               Con_Print("EntityFrame_AddFrame: out of sequence frames in database\n");
                        return;
                }
                e = d->frames[n].framenum;
@@ -390,11 +521,10 @@ void EntityFrame_Write(entity_database_t *d, entity_frame_t *f, sizebuf_t *msg)
 {
        int i, onum, number;
        entity_frame_t *o = &deltaframe;
-       entity_state_t *ent, *delta, baseline;
+       entity_state_t *ent, *delta;
 
        EntityFrame_AddFrame(d, f);
 
-       ClearStateToDefault(&baseline);
        EntityFrame_FetchFrame(d, d->ackframe > 0 ? d->ackframe : -1, o);
        MSG_WriteByte (msg, svc_entities);
        MSG_WriteLong (msg, o->framenum);
@@ -422,8 +552,8 @@ void EntityFrame_Write(entity_database_t *d, entity_frame_t *f, sizebuf_t *msg)
                }
                else
                {
-                       // delta from baseline
-                       delta = &baseline;
+                       // delta from defaults
+                       delta = &defaultstate;
                }
                EntityState_Write(ent, msg, delta);
        }
@@ -441,9 +571,7 @@ void EntityFrame_Read(entity_database_t *d)
 {
        int number, removed;
        entity_frame_t *f = &framedata, *delta = &deltaframe;
-       entity_state_t *e, baseline, *old, *oldend, *edelta;
-
-       ClearStateToDefault(&baseline);
+       entity_state_t *e, *old, *oldend;
 
        EntityFrame_Clear(f, NULL, -1);
 
@@ -473,10 +601,8 @@ void EntityFrame_Read(entity_database_t *d)
                {
                        if (f->numentities >= MAX_ENTITY_DATABASE)
                                Host_Error("EntityFrame_Read: entity list too big\n");
-                       memcpy(f->entitydata + f->numentities, old, sizeof(entity_state_t));
-                       f->entitydata[f->numentities].time = cl.mtime[0];
-                       old++;
-                       f->numentities++;
+                       f->entitydata[f->numentities] = *old++;
+                       f->entitydata[f->numentities++].time = cl.mtime[0];
                }
                if (removed)
                {
@@ -496,25 +622,23 @@ void EntityFrame_Read(entity_database_t *d)
                        if (old < oldend && old->number == number)
                        {
                                // delta from old entity
-                               edelta = old++;
+                               *e = *old++;
                        }
                        else
                        {
-                               // delta from baseline
-                               edelta = &baseline;
+                               // delta from defaults
+                               *e = defaultstate;
                        }
 
-                       EntityState_Read(e, edelta, number);
+                       EntityState_ReadUpdate(e, number);
                }
        }
        while (old < oldend)
        {
                if (f->numentities >= MAX_ENTITY_DATABASE)
                        Host_Error("EntityFrame_Read: entity list too big\n");
-               memcpy(f->entitydata + f->numentities, old, sizeof(entity_state_t));
-               f->entitydata[f->numentities].time = cl.mtime[0];
-               old++;
-               f->numentities++;
+               f->entitydata[f->numentities] = *old++;
+               f->entitydata[f->numentities++].time = cl.mtime[0];
        }
        EntityFrame_AddFrame(d, f);
 }
@@ -534,25 +658,7 @@ int EntityFrame_MostRecentlyRecievedFrameNum(entity_database_t *d)
 
 
 
-static int EntityFrame4_SV_ChooseCommitToReplace(entity_database4_t *d)
-{
-       int i, best, bestframenum;
-       best = 0;
-       bestframenum = d->commit[0].framenum;
-       for (i = 0;i < MAX_ENTITY_HISTORY;i++)
-       {
-               if (!d->commit[i].numentities)
-                       return i;
-               if (bestframenum > d->commit[i].framenum)
-               {
-                       bestframenum = d->commit[i].framenum;
-                       best = i;
-               }
-       }
-       return best;
-}
-
-static entity_state_t *EntityFrame4_GetReferenceEntity(entity_database4_t *d, int number)
+entity_state_t *EntityFrame4_GetReferenceEntity(entity_database4_t *d, int number)
 {
        if (d->maxreferenceentities <= number)
        {
@@ -567,12 +673,15 @@ static entity_state_t *EntityFrame4_GetReferenceEntity(entity_database4_t *d, in
                }
                // clear the newly created entities
                for (;oldmax < d->maxreferenceentities;oldmax++)
-                       ClearStateToDefault(d->referenceentity + oldmax);
+               {
+                       d->referenceentity[oldmax] = defaultstate;
+                       d->referenceentity[oldmax].number = oldmax;
+               }
        }
        return d->referenceentity + number;
 }
 
-static void EntityFrame4_AddCommitEntity(entity_database4_t *d, entity_state_t *s)
+void EntityFrame4_AddCommitEntity(entity_database4_t *d, entity_state_t *s)
 {
        // resize commit's entity list if full
        if (d->currentcommit->maxentities <= d->currentcommit->numentities)
@@ -594,7 +703,8 @@ entity_database4_t *EntityFrame4_AllocDatabase(mempool_t *pool)
        entity_database4_t *d;
        d = Mem_Alloc(pool, sizeof(*d));
        d->mempool = pool;
-       d->referenceframenum = -1;
+       EntityFrame4_ResetDatabase(d);
+       d->ackframenum = -1;
        return d;
 }
 
@@ -615,45 +725,52 @@ void EntityFrame4_ResetDatabase(entity_database4_t *d)
        d->referenceframenum = -1;
        for (i = 0;i < MAX_ENTITY_HISTORY;i++)
                d->commit[i].numentities = 0;
+       for (i = 0;i < d->maxreferenceentities;i++)
+               d->referenceentity[i] = defaultstate;
 }
 
-void EntityFrame4_AckFrame(entity_database4_t *d, int framenum)
+int EntityFrame4_AckFrame(entity_database4_t *d, int framenum)
 {
-       int i, foundit = false;
-       // check if client is requesting no delta compression
+       int i, j, found = false;
+       entity_database4_commit_t *commit;
+       if (d->referenceframenum == framenum)
+               return true;
        if (framenum == -1)
        {
                EntityFrame4_ResetDatabase(d);
-               return;
+               return true;
        }
-       for (i = 0;i < MAX_ENTITY_HISTORY;i++)
+       for (i = 0, commit = d->commit;i < MAX_ENTITY_HISTORY;i++, commit++)
        {
-               if (d->commit[i].numentities && d->commit[i].framenum <= framenum)
+               if (commit->numentities && commit->framenum <= framenum)
                {
-                       if (d->commit[i].framenum == framenum)
+                       if (commit->framenum == framenum)
                        {
-                               // apply commit to database
-                               d->referenceframenum = d->commit[i].framenum;
-                               while (d->commit[i].numentities--)
-                                       *EntityFrame4_GetReferenceEntity(d, d->commit[i].entity[d->commit[i].numentities].number) = d->commit[i].entity[d->commit[i].numentities];
-                               foundit = true;
+                               found = true;
+                               d->referenceframenum = framenum;
+                               if (developer_networkentities.integer >= 3)
+                               {
+                                       for (j = 0;j < commit->numentities;j++)
+                                       {
+                                               entity_state_t *s = EntityFrame4_GetReferenceEntity(d, commit->entity[j].number);
+                                               if (commit->entity[j].active != s->active)
+                                               {
+                                                       if (commit->entity[j].active)
+                                                               Con_Printf("commit entity %i has become active (modelindex %i)\n", commit->entity[j].number, commit->entity[j].modelindex);
+                                                       else
+                                                               Con_Printf("commit entity %i has become inactive (modelindex %i)\n", commit->entity[j].number, commit->entity[j].modelindex);
+                                               }
+                                               *s = commit->entity[j];
+                                       }
+                               }
+                               else
+                                       for (j = 0;j < commit->numentities;j++)
+                                               *EntityFrame4_GetReferenceEntity(d, commit->entity[j].number) = commit->entity[j];
                        }
-                       d->commit[i].numentities = 0;
-                       d->commit[i].framenum = -1;
+                       commit->numentities = 0;
                }
        }
-       if (!foundit)
-               Con_DPrintf("EntityFrame4_AckFrame: frame %i not found in database, expect glitches!\n", framenum);
-}
-
-void EntityFrame4_SV_WriteFrame_Begin(entity_database4_t *d, sizebuf_t *msg, int framenum)
-{
-       d->currentcommit = d->commit + EntityFrame4_SV_ChooseCommitToReplace(d);
-       d->currentcommit->numentities = 0;
-       d->currentcommit->framenum = framenum;
-       MSG_WriteByte(msg, svc_entities);
-       MSG_WriteLong(msg, d->referenceframenum);
-       MSG_WriteLong(msg, d->currentcommit->framenum);
+       return found;
 }
 
 int EntityFrame4_SV_WriteFrame_Entity(entity_database4_t *d, sizebuf_t *msg, int maxbytes, entity_state_t *s)
@@ -667,16 +784,8 @@ int EntityFrame4_SV_WriteFrame_Entity(entity_database4_t *d, sizebuf_t *msg, int
        buf.maxsize = sizeof(data);
        // make the message
        e = EntityFrame4_GetReferenceEntity(d, s->number);
-       if (s->active)
-       {
-               // entity exists, send an update
-               EntityState_Write(s, &buf, e);
-       }
-       else if (e->active)
-       {
-               // entity used to exist but doesn't anymore, send remove
-               MSG_WriteShort(&buf, s->number | 0x8000);
-       }
+       // send an update (may update or remove the entity)
+       EntityState_Write(s, &buf, e);
        // if the message is empty, skip out now
        if (!buf.cursize)
                return true;
@@ -691,53 +800,122 @@ int EntityFrame4_SV_WriteFrame_Entity(entity_database4_t *d, sizebuf_t *msg, int
        return true;
 }
 
-void EntityFrame4_SV_WriteFrame_End(entity_database4_t *d, sizebuf_t *msg)
-{
-       // remove world message (invalid, and thus a good terminator)
-       MSG_WriteShort(msg, 0x8000);
-       // just to be sure
-       d->currentcommit = NULL;
-}
-
 extern void CL_MoveLerpEntityStates(entity_t *ent);
 void EntityFrame4_CL_ReadFrame(entity_database4_t *d)
 {
-       int i, n, number, referenceframenum, framenum;
+       int i, n, cnumber, referenceframenum, framenum, enumber, done, stopnumber, skip = false;
+       // read the number of the frame this refers to
        referenceframenum = MSG_ReadLong();
+       // read the number of this frame
        framenum = MSG_ReadLong();
-       EntityFrame4_AckFrame(d, referenceframenum);
-       for (i = 0;i < MAX_ENTITY_HISTORY;i++)
-               if (!d->commit[i].numentities)
-                       break;
-       if (i < MAX_ENTITY_HISTORY)
+       // read the start number
+       enumber = MSG_ReadShort();
+       if (developer_networkentities.integer >= 1)
        {
-               d->currentcommit = d->commit + i;
-               d->currentcommit->framenum = framenum;
-               d->currentcommit->numentities = 0;
+               Con_Printf("recv svc_entities ref:%i num:%i (database: ref:%i commits:", referenceframenum, framenum, d->referenceframenum);
+               for (i = 0;i < MAX_ENTITY_HISTORY;i++)
+                       if (d->commit[i].numentities)
+                               Con_Printf(" %i", d->commit[i].framenum);
+               Con_Print("\n");
        }
-       else
+       if (!EntityFrame4_AckFrame(d, referenceframenum))
        {
-               Con_Printf("EntityFrame4_CL_ReadFrame: error while decoding frame %i: database full, resetting, expect glitches!!\n", framenum);
-               d->currentcommit = NULL;
+               Con_Print("EntityFrame4_CL_ReadFrame: reference frame invalid (VERY BAD ERROR), this update will be skipped\n");
+               skip = true;
+               d->ackframenum = -1;
                EntityFrame4_ResetDatabase(d);
        }
-       while((n = MSG_ReadShort()) != 0x8000)
+       d->currentcommit = NULL;
+       for (i = 0;i < MAX_ENTITY_HISTORY;i++)
        {
-               number = n & 0x7FFF;
-               cl_entities[number].state_previous = cl_entities[number].state_current;
-               if (number & 0x8000)
+               if (!d->commit[i].numentities)
                {
-                       ClearStateToDefault(&cl_entities[number].state_current);
-                       cl_entities[number].state_current.active = false;
-                       cl_entities[number].state_current.number = number;
+                       d->currentcommit = d->commit + i;
+                       d->currentcommit->framenum = d->ackframenum = framenum;
+                       d->currentcommit->numentities = 0;
+               }
+       }
+       if (d->currentcommit == NULL)
+       {
+               Con_Printf("EntityFrame4_CL_ReadFrame: error while decoding frame %i: database full, reading but not storing this update\n", framenum);
+               skip = true;
+       }
+       done = false;
+       while (!done && !msg_badread)
+       {
+               // read the number of the modified entity
+               // (gaps will be copied unmodified)
+               n = (unsigned short)MSG_ReadShort();
+               if (n == 0x8000)
+               {
+                       // no more entities in this update, but we still need to copy the
+                       // rest of the reference entities (final gap)
+                       done = true;
+                       // read end of range number, then process normally
+                       n = (unsigned short)MSG_ReadShort();
+               }
+               // high bit means it's a remove message
+               cnumber = n & 0x7FFF;
+               // add one (the changed one) if not done
+               stopnumber = cnumber + !done;
+               // process entities in range from the last one to the changed one
+               for (;enumber < stopnumber;enumber++)
+               {
+                       if (skip)
+                       {
+                               if (enumber == cnumber && (n & 0x8000) == 0)
+                               {
+                                       entity_state_t tempstate;
+                                       EntityState_ReadUpdate(&tempstate, enumber);
+                               }
+                               continue;
+                       }
+                       // slide the current into the previous slot
+                       cl_entities[enumber].state_previous = cl_entities[enumber].state_current;
+                       // copy a new current from reference database
+                       cl_entities[enumber].state_current = *EntityFrame4_GetReferenceEntity(d, enumber);
+                       // if this is the one to modify, read more data...
+                       if (enumber == cnumber)
+                       {
+                               if (n & 0x8000)
+                               {
+                                       // simply removed
+                                       if (developer_networkentities.integer >= 2)
+                                               Con_Printf("entity %i: remove\n", enumber);
+                                       cl_entities[enumber].state_current = defaultstate;
+                               }
+                               else
+                               {
+                                       // read the changes
+                                       if (developer_networkentities.integer >= 2)
+                                               Con_Printf("entity %i: update\n", enumber);
+                                       EntityState_ReadUpdate(&cl_entities[enumber].state_current, enumber);
+                               }
+                       }
+                       else if (developer_networkentities.integer >= 4)
+                               Con_Printf("entity %i: copy\n", enumber);
+                       // fix the number (it gets wiped occasionally by copying from defaultstate)
+                       cl_entities[enumber].state_current.number = enumber;
+                       // check if we need to update the lerp stuff
+                       if (cl_entities[enumber].state_current.active)
+                       {
+                               CL_MoveLerpEntityStates(&cl_entities[enumber]);
+                               cl_entities_active[enumber] = true;
+                       }
+                       // add this to the commit entry whether it is modified or not
+                       if (d->currentcommit)
+                               EntityFrame4_AddCommitEntity(d, &cl_entities[enumber].state_current);
+                       // print extra messages if desired
+                       if (developer_networkentities.integer >= 2 && cl_entities[enumber].state_current.active != cl_entities[enumber].state_previous.active)
+                       {
+                               if (cl_entities[enumber].state_current.active)
+                                       Con_Printf("entity #%i has become active\n", enumber);
+                               else if (cl_entities[enumber].state_previous.active)
+                                       Con_Printf("entity #%i has become inactive\n", enumber);
+                       }
                }
-               else
-                       EntityState_Read(&cl_entities[number].state_current, EntityFrame4_GetReferenceEntity(d, number), number);
-               CL_MoveLerpEntityStates(&cl_entities[number]);
-               cl_entities_active[number] = true;
-               if (d->currentcommit)
-                       EntityFrame4_AddCommitEntity(d, &cl_entities[number].state_current);
        }
        d->currentcommit = NULL;
 }
 
+