]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - protocol.c
removed \n from all Host_Error, Sys_Error, PRVM_ERROR, PF_ERROR calls, since Host_Err...
[xonotic/darkplaces.git] / protocol.c
index c044a8dfeb0c98a3bea30e232d904e74dcfac814..d3f14fe5eb112dbc04af51be390e94b914f9f5f3 100644 (file)
@@ -35,9 +35,74 @@ entity_state_t defaultstate =
        {0,0}//unsigned char unused[2]; // !
 };
 
+// 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"},
+       {0, NULL}
+};
+
+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));
+       }
+}
+
 // keep track of quake entities because they need to be killed if they get stale
 int cl_lastquakeentity = 0;
-qbyte cl_isquakeentity[MAX_EDICTS];
+unsigned char cl_isquakeentity[MAX_EDICTS];
 
 void EntityFrameQuake_ReadEntity(int bits)
 {
@@ -60,9 +125,16 @@ void EntityFrameQuake_ReadEntity(int bits)
                num = MSG_ReadByte ();
 
        if (num >= MAX_EDICTS)
-               Host_Error("EntityFrameQuake_ReadEntity: entity number (%i) >= MAX_EDICTS (%i)\n", 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)\n", num);
+               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;
 
@@ -100,7 +172,7 @@ void EntityFrameQuake_ReadEntity(int bits)
        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] = (qbyte)(((c >> 5) & 7) * (32.0f / 7.0f));s.colormod[1] = (qbyte)(((c >> 2) & 7) * (32.0f / 7.0f));s.colormod[2] = (qbyte)((c & 3) * (32.0f / 3.0f));}
+       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);
@@ -136,7 +208,7 @@ void EntityFrameQuake_ReadEntity(int bits)
        }
 
        if (msg_badread)
-               Host_Error("EntityFrameQuake_ReadEntity: read error\n");
+               Host_Error("EntityFrameQuake_ReadEntity: read error");
 }
 
 void EntityFrameQuake_ISeeDeadEntities(void)
@@ -172,7 +244,7 @@ void EntityFrameQuake_WriteFrame(sizebuf_t *msg, int numstates, const entity_sta
        entity_state_t baseline;
        int i, bits;
        sizebuf_t buf;
-       qbyte data[128];
+       unsigned char data[128];
 
        // prepare the buffer
        memset(&buf, 0, sizeof(buf));
@@ -198,7 +270,7 @@ void EntityFrameQuake_WriteFrame(sizebuf_t *msg, int numstates, const entity_sta
                        bits |= U_EXTERIORMODEL;
 
                // LordHavoc: old stuff, but rewritten to have more exact tolerances
-               baseline = sv.edicts[s->number].e->baseline;
+               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])
@@ -243,8 +315,11 @@ void EntityFrameQuake_WriteFrame(sizebuf_t *msg, int numstates, const entity_sta
                        bits |= U_GLOWCOLOR;
 
                // if extensions are disabled, clear the relevant update flags
-               if (sv.netquakecompatible)
+               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)
@@ -282,6 +357,22 @@ void EntityFrameQuake_WriteFrame(sizebuf_t *msg, int numstates, const entity_sta
                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)
                {
@@ -306,11 +397,11 @@ int EntityState_DeltaBits(const entity_state_t *o, const entity_state_t *n)
                bits |= E_ORIGIN2;
        if (fabs(n->origin[2] - o->origin[2]) > (1.0f / 256.0f))
                bits |= E_ORIGIN3;
-       if ((qbyte) (n->angles[0] * (256.0f / 360.0f)) != (qbyte) (o->angles[0] * (256.0f / 360.0f)))
+       if ((unsigned char) (n->angles[0] * (256.0f / 360.0f)) != (unsigned char) (o->angles[0] * (256.0f / 360.0f)))
                bits |= E_ANGLE1;
-       if ((qbyte) (n->angles[1] * (256.0f / 360.0f)) != (qbyte) (o->angles[1] * (256.0f / 360.0f)))
+       if ((unsigned char) (n->angles[1] * (256.0f / 360.0f)) != (unsigned char) (o->angles[1] * (256.0f / 360.0f)))
                bits |= E_ANGLE2;
-       if ((qbyte) (n->angles[2] * (256.0f / 360.0f)) != (qbyte) (o->angles[2] * (256.0f / 360.0f)))
+       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;
@@ -385,7 +476,7 @@ void EntityState_WriteFields(const entity_state_t *ent, sizebuf_t *msg, unsigned
                if (bits & E_ORIGIN3)
                        MSG_WriteCoord16i(msg, ent->origin[2]);
        }
-       else if (sv.protocol == PROTOCOL_DARKPLACES1 || sv.protocol == PROTOCOL_DARKPLACES3 || sv.protocol == PROTOCOL_DARKPLACES4 || sv.protocol == PROTOCOL_DARKPLACES5 || sv.protocol == PROTOCOL_DARKPLACES6)
+       else
        {
                // LordHavoc: have to write flags first, as they can modify protocol
                if (bits & E_FLAGS)
@@ -409,23 +500,23 @@ void EntityState_WriteFields(const entity_state_t *ent, sizebuf_t *msg, unsigned
                                MSG_WriteCoord32f(msg, ent->origin[2]);
                }
        }
-       if ((sv.protocol == PROTOCOL_DARKPLACES5 || sv.protocol == PROTOCOL_DARKPLACES6) && !(ent->flags & RENDER_LOWPRECISION))
+       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_WriteAngle16i(msg, ent->angles[0]);
+                       MSG_WriteAngle8i(msg, ent->angles[0]);
                if (bits & E_ANGLE2)
-                       MSG_WriteAngle16i(msg, ent->angles[1]);
+                       MSG_WriteAngle8i(msg, ent->angles[1]);
                if (bits & E_ANGLE3)
-                       MSG_WriteAngle16i(msg, ent->angles[2]);
+                       MSG_WriteAngle8i(msg, ent->angles[2]);
        }
        else
        {
                if (bits & E_ANGLE1)
-                       MSG_WriteAngle8i(msg, ent->angles[0]);
+                       MSG_WriteAngle16i(msg, ent->angles[0]);
                if (bits & E_ANGLE2)
-                       MSG_WriteAngle8i(msg, ent->angles[1]);
+                       MSG_WriteAngle16i(msg, ent->angles[1]);
                if (bits & E_ANGLE3)
-                       MSG_WriteAngle8i(msg, ent->angles[2]);
+                       MSG_WriteAngle16i(msg, ent->angles[2]);
        }
        if (bits & E_MODEL1)
                MSG_WriteByte(msg, ent->modelindex & 0xFF);
@@ -525,7 +616,7 @@ void EntityState_ReadFields(entity_state_t *e, unsigned int bits)
                if (bits & E_ORIGIN3)
                        e->origin[2] = MSG_ReadCoord16i();
        }
-       else if (cl.protocol == PROTOCOL_DARKPLACES1 || cl.protocol == PROTOCOL_DARKPLACES3 || cl.protocol == PROTOCOL_DARKPLACES4 || cl.protocol == PROTOCOL_DARKPLACES5 || cl.protocol == PROTOCOL_DARKPLACES6)
+       else
        {
                if (bits & E_FLAGS)
                        e->flags = MSG_ReadByte();
@@ -548,8 +639,6 @@ void EntityState_ReadFields(entity_state_t *e, unsigned int bits)
                                e->origin[2] = MSG_ReadCoord32f();
                }
        }
-       else
-               Host_Error("EntityState_ReadFields: unknown cl.protocol %i\n", cl.protocol);
        if ((cl.protocol == PROTOCOL_DARKPLACES5 || cl.protocol == PROTOCOL_DARKPLACES6) && !(e->flags & RENDER_LOWPRECISION))
        {
                if (bits & E_ANGLE1)
@@ -648,10 +737,10 @@ void EntityState_ReadFields(entity_state_t *e, unsigned int bits)
                        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)                       
+               if (bits & E_LIGHTPFLAGS)
                        Con_Printf(" E_LIGHTPFLAGS %i", e->lightpflags);
 
                if (bits & E_TAGATTACHMENT)
@@ -665,7 +754,7 @@ void EntityState_ReadFields(entity_state_t *e, unsigned int bits)
 // (client and server) allocates a new empty database
 entityframe_database_t *EntityFrame_AllocDatabase(mempool_t *mempool)
 {
-       return Mem_Alloc(mempool, sizeof(entityframe_database_t));
+       return (entityframe_database_t *)Mem_Alloc(mempool, sizeof(entityframe_database_t));
 }
 
 // (client and server) frees the database
@@ -853,7 +942,7 @@ void EntityFrame_CL_ReadFrame(void)
        entity_t *ent;
        entityframe_database_t *d;
        if (!cl.entitydatabase)
-               cl.entitydatabase = EntityFrame_AllocDatabase(cl_entities_mempool);
+               cl.entitydatabase = EntityFrame_AllocDatabase(cl_mempool);
        d = cl.entitydatabase;
 
        EntityFrame_Clear(f, NULL, -1);
@@ -875,17 +964,17 @@ void EntityFrame_CL_ReadFrame(void)
        while ((number = (unsigned short) MSG_ReadShort()) != 0xFFFF && !msg_badread)
        {
                if (msg_badread)
-                       Host_Error("EntityFrame_Read: read error\n");
+                       Host_Error("EntityFrame_Read: read error");
                removed = number & 0x8000;
                number &= 0x7FFF;
                if (number >= MAX_EDICTS)
-                       Host_Error("EntityFrame_Read: number (%i) >= MAX_EDICTS (%i)\n", number, MAX_EDICTS);
+                       Host_Error("EntityFrame_Read: number (%i) >= MAX_EDICTS (%i)", number, MAX_EDICTS);
 
                // seek to entity, while copying any skipped entities (assume unchanged)
                while (old < oldend && old->number < number)
                {
                        if (f->numentities >= MAX_ENTITY_DATABASE)
-                               Host_Error("EntityFrame_Read: entity list too big\n");
+                               Host_Error("EntityFrame_Read: entity list too big");
                        f->entitydata[f->numentities] = *old++;
                        f->entitydata[f->numentities++].time = cl.mtime[0];
                }
@@ -899,7 +988,7 @@ void EntityFrame_CL_ReadFrame(void)
                else
                {
                        if (f->numentities >= MAX_ENTITY_DATABASE)
-                               Host_Error("EntityFrame_Read: entity list too big\n");
+                               Host_Error("EntityFrame_Read: entity list too big");
 
                        // reserve this slot
                        e = f->entitydata + f->numentities++;
@@ -915,6 +1004,12 @@ void EntityFrame_CL_ReadFrame(void)
                                *e = defaultstate;
                        }
 
+                       if (cl_num_entities <= number)
+                       {
+                               cl_num_entities = number + 1;
+                               if (number >= cl_max_entities)
+                                       CL_ExpandEntities(number);
+                       }
                        cl_entities_active[number] = true;
                        e->active = true;
                        e->time = cl.mtime[0];
@@ -925,17 +1020,17 @@ void EntityFrame_CL_ReadFrame(void)
        while (old < oldend)
        {
                if (f->numentities >= MAX_ENTITY_DATABASE)
-                       Host_Error("EntityFrame_Read: entity list too big\n");
+                       Host_Error("EntityFrame_Read: entity list too big");
                f->entitydata[f->numentities] = *old++;
                f->entitydata[f->numentities++].time = cl.mtime[0];
        }
        EntityFrame_AddFrame(d, f->eye, f->framenum, f->numentities, f->entitydata);
 
-       memset(cl_entities_active, 0, cl_max_entities * sizeof(qbyte));
+       memset(cl_entities_active, 0, cl_num_entities * sizeof(unsigned char));
        number = 1;
        for (i = 0;i < f->numentities;i++)
        {
-               for (;number < f->entitydata[i].number;number++)
+               for (;number < f->entitydata[i].number && number < cl_num_entities;number++)
                {
                        if (cl_entities_active[number])
                        {
@@ -943,6 +1038,8 @@ void EntityFrame_CL_ReadFrame(void)
                                cl_entities[number].state_current.active = false;
                        }
                }
+               if (number >= cl_num_entities)
+                       break;
                // update the entity
                ent = &cl_entities[number];
                ent->state_previous = ent->state_current;
@@ -952,7 +1049,7 @@ void EntityFrame_CL_ReadFrame(void)
                cl_entities_active[number] = true;
                number++;
        }
-       for (;number < cl_max_entities;number++)
+       for (;number < cl_num_entities;number++)
        {
                if (cl_entities_active[number])
                {
@@ -984,7 +1081,7 @@ entity_state_t *EntityFrame4_GetReferenceEntity(entityframe4_database_t *d, int
                int oldmax = d->maxreferenceentities;
                entity_state_t *oldentity = d->referenceentity;
                d->maxreferenceentities = (number + 15) & ~7;
-               d->referenceentity = Mem_Alloc(d->mempool, d->maxreferenceentities * sizeof(*d->referenceentity));
+               d->referenceentity = (entity_state_t *)Mem_Alloc(d->mempool, d->maxreferenceentities * sizeof(*d->referenceentity));
                if (oldentity)
                {
                        memcpy(d->referenceentity, oldentity, oldmax * sizeof(*d->referenceentity));
@@ -1007,7 +1104,7 @@ void EntityFrame4_AddCommitEntity(entityframe4_database_t *d, const entity_state
        {
                entity_state_t *oldentity = d->currentcommit->entity;
                d->currentcommit->maxentities += 8;
-               d->currentcommit->entity = Mem_Alloc(d->mempool, d->currentcommit->maxentities * sizeof(*d->currentcommit->entity));
+               d->currentcommit->entity = (entity_state_t *)Mem_Alloc(d->mempool, d->currentcommit->maxentities * sizeof(*d->currentcommit->entity));
                if (oldentity)
                {
                        memcpy(d->currentcommit->entity, oldentity, d->currentcommit->numentities * sizeof(*d->currentcommit->entity));
@@ -1020,7 +1117,7 @@ void EntityFrame4_AddCommitEntity(entityframe4_database_t *d, const entity_state
 entityframe4_database_t *EntityFrame4_AllocDatabase(mempool_t *pool)
 {
        entityframe4_database_t *d;
-       d = Mem_Alloc(pool, sizeof(*d));
+       d = (entityframe4_database_t *)Mem_Alloc(pool, sizeof(*d));
        d->mempool = pool;
        EntityFrame4_ResetDatabase(d);
        return d;
@@ -1115,7 +1212,7 @@ void EntityFrame4_CL_ReadFrame(void)
        entity_state_t *s;
        entityframe4_database_t *d;
        if (!cl.entitydatabase4)
-               cl.entitydatabase4 = EntityFrame4_AllocDatabase(cl_entities_mempool);
+               cl.entitydatabase4 = EntityFrame4_AllocDatabase(cl_mempool);
        d = cl.entitydatabase4;
        // read the number of the frame this refers to
        referenceframenum = MSG_ReadLong();
@@ -1169,12 +1266,19 @@ void EntityFrame4_CL_ReadFrame(void)
                }
                // high bit means it's a remove message
                cnumber = n & 0x7FFF;
+               // if this is a live entity we may need to expand the array
+               if (cl_num_entities <= cnumber && !(n & 0x8000))
+               {
+                       cl_num_entities = cnumber + 1;
+                       if (cnumber >= cl_max_entities)
+                               CL_ExpandEntities(cnumber);
+               }
                // 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 (skip || enumber >= cl_num_entities)
                        {
                                if (enumber == cnumber && (n & 0x8000) == 0)
                                {
@@ -1242,7 +1346,7 @@ void EntityFrame4_WriteFrame(sizebuf_t *msg, entityframe4_database_t *d, int num
        entity_state_t inactiveentitystate;
        int i, n, startnumber;
        sizebuf_t buf;
-       qbyte data[128];
+       unsigned char data[128];
 
        // if there isn't enough space to accomplish anything, skip it
        if (msg->cursize + 24 > msg->maxsize)
@@ -1277,15 +1381,15 @@ void EntityFrame4_WriteFrame(sizebuf_t *msg, entityframe4_database_t *d, int num
                                Con_Printf(" %i", d->commit[i].framenum);
                Con_Print(")\n");
        }
-       if (d->currententitynumber >= sv.max_edicts)
+       if (d->currententitynumber >= prog->max_edicts)
                startnumber = 1;
        else
-               startnumber = bound(1, d->currententitynumber, sv.max_edicts - 1);
+               startnumber = bound(1, d->currententitynumber, prog->max_edicts - 1);
        MSG_WriteShort(msg, startnumber);
        // reset currententitynumber so if the loop does not break it we will
        // start at beginning next frame (if it does break, it will set it)
        d->currententitynumber = 1;
-       for (i = 0, n = startnumber;n < sv.max_edicts;n++)
+       for (i = 0, n = startnumber;n < prog->max_edicts;n++)
        {
                // find the old state to delta from
                e = EntityFrame4_GetReferenceEntity(d, n);
@@ -1344,13 +1448,17 @@ void EntityFrame4_WriteFrame(sizebuf_t *msg, entityframe4_database_t *d, int num
 entityframe5_database_t *EntityFrame5_AllocDatabase(mempool_t *pool)
 {
        entityframe5_database_t *d;
-       d = Mem_Alloc(pool, sizeof(*d));
+       d = (entityframe5_database_t *)Mem_Alloc(pool, sizeof(*d));
        EntityFrame5_ResetDatabase(d);
        return d;
 }
 
 void EntityFrame5_FreeDatabase(entityframe5_database_t *d)
 {
+       // all the [maxedicts] memory is allocated at once, so there's only one
+       // thing to free
+       if (d->maxedicts)
+               Mem_Free(d->deltabits);
        Mem_Free(d);
 }
 
@@ -1359,23 +1467,61 @@ void EntityFrame5_ResetDatabase(entityframe5_database_t *d)
        int i;
        memset(d, 0, sizeof(*d));
        d->latestframenum = 0;
-       for (i = 0;i < MAX_EDICTS;i++)
+       for (i = 0;i < d->maxedicts;i++)
                d->states[i] = defaultstate;
 }
 
+void EntityFrame5_ExpandEdicts(entityframe5_database_t *d, int newmax)
+{
+       if (d->maxedicts < newmax)
+       {
+               unsigned char *data;
+               int oldmaxedicts = d->maxedicts;
+               int *olddeltabits = d->deltabits;
+               unsigned char *oldpriorities = d->priorities;
+               int *oldupdateframenum = d->updateframenum;
+               entity_state_t *oldstates = d->states;
+               unsigned char *oldvisiblebits = d->visiblebits;
+               d->maxedicts = newmax;
+               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));
+               d->deltabits = (int *)data;data += d->maxedicts * sizeof(int);
+               d->priorities = (unsigned char *)data;data += d->maxedicts * sizeof(unsigned char);
+               d->updateframenum = (int *)data;data += d->maxedicts * sizeof(int);
+               d->states = (entity_state_t *)data;data += d->maxedicts * sizeof(entity_state_t);
+               d->visiblebits = (unsigned char *)data;data += (d->maxedicts+7)/8 * sizeof(unsigned char);
+               if (oldmaxedicts)
+               {
+                       memcpy(d->deltabits, olddeltabits, oldmaxedicts * sizeof(int));
+                       memcpy(d->priorities, oldpriorities, oldmaxedicts * sizeof(unsigned char));
+                       memcpy(d->updateframenum, oldupdateframenum, oldmaxedicts * sizeof(int));
+                       memcpy(d->states, oldstates, oldmaxedicts * sizeof(entity_state_t));
+                       memcpy(d->visiblebits, oldvisiblebits, (oldmaxedicts+7)/8 * sizeof(unsigned char));
+                       // the previous buffers were a single allocation, so just one free
+                       Mem_Free(olddeltabits);
+               }
+       }
+}
 
-int EntityState5_Priority(entityframe5_database_t *d, entity_state_t *view, entity_state_t *s, int changedbits, int age)
+int EntityState5_Priority(entityframe5_database_t *d, int stateindex)
 {
        int lowprecision, limit, priority;
        double distance;
+       int changedbits;
+       int age;
+       entity_state_t *view, *s;
+       changedbits = d->deltabits[stateindex];
        if (!changedbits)
                return 0;
-       if (!s->active/* && changedbits & E5_FULLUPDATE*/)
+       if (!d->states[stateindex].active/* && changedbits & E5_FULLUPDATE*/)
                return E5_PROTOCOL_PRIORITYLEVELS - 1;
        // check whole attachment chain to judge relevance to player
+       view = d->states + d->viewentnum;
        lowprecision = false;
        for (limit = 0;limit < 256;limit++)
        {
+               if (d->maxedicts < stateindex)
+                       EntityFrame5_ExpandEdicts(d, (stateindex+256)&~255);
+               s = d->states + stateindex;
                if (s == view)
                        return E5_PROTOCOL_PRIORITYLEVELS - 1;
                if (s->flags & RENDER_VIEWMODEL)
@@ -1388,12 +1534,16 @@ int EntityState5_Priority(entityframe5_database_t *d, entity_state_t *view, enti
                                return E5_PROTOCOL_PRIORITYLEVELS - 1;
                        break;
                }
-               s = d->states + s->tagentity;
+               stateindex = s->tagentity;
        }
        if (limit >= 256)
-               Con_Printf("Protocol: Runaway loop recursing tagentity links on entity %i\n", s->number);
+       {
+               Con_Printf("Protocol: Runaway loop recursing tagentity links on entity %i\n", stateindex);
+               return 0;
+       }
        // it's not a viewmodel for this client
        distance = VectorDistance(view->origin, s->origin);
+       age = d->latestframenum - d->updateframenum[stateindex];
        priority = (E5_PROTOCOL_PRIORITYLEVELS / 2) + age - (int)(distance * (E5_PROTOCOL_PRIORITYLEVELS / 16384.0f));
        if (lowprecision)
                priority -= (E5_PROTOCOL_PRIORITYLEVELS / 4);
@@ -1412,7 +1562,7 @@ void EntityState5_WriteUpdate(int number, const entity_state_t *s, int changedbi
        else
        {
                bits = changedbits;
-               if ((bits & E5_ORIGIN) && (s->origin[0] < -4096 || s->origin[0] >= 4096 || s->origin[1] < -4096 || s->origin[1] >= 4096 || s->origin[2] < -4096 || s->origin[2] >= 4096))
+               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))
                        bits |= E5_ORIGIN32;
                if ((bits & E5_ANGLES) && !(s->flags & RENDER_LOWPRECISION))
                        bits |= E5_ANGLES16;
@@ -1572,15 +1722,15 @@ void EntityState5_ReadUpdate(entity_state_t *s)
        {
                if (bits & E5_ANGLES16)
                {
-                       s->angles[0] = MSG_ReadAngle16i(); 
-                       s->angles[1] = MSG_ReadAngle16i(); 
-                       s->angles[2] = MSG_ReadAngle16i(); 
+                       s->angles[0] = MSG_ReadAngle16i();
+                       s->angles[1] = MSG_ReadAngle16i();
+                       s->angles[2] = MSG_ReadAngle16i();
                }
                else
                {
-                       s->angles[0] = MSG_ReadAngle8i(); 
-                       s->angles[1] = MSG_ReadAngle8i(); 
-                       s->angles[2] = MSG_ReadAngle8i(); 
+                       s->angles[0] = MSG_ReadAngle8i();
+                       s->angles[1] = MSG_ReadAngle8i();
+                       s->angles[2] = MSG_ReadAngle8i();
                }
        }
        if (bits & E5_MODEL)
@@ -1747,12 +1897,22 @@ void EntityFrame5_CL_ReadFrame(void)
        for (i = 0;i < LATESTFRAMENUMS-1;i++)
                cl.latestframenums[i] = cl.latestframenums[i+1];
        cl.latestframenums[LATESTFRAMENUMS-1] = MSG_ReadLong();
+       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)
+               cl.servermovesequence = MSG_ReadLong();
        // read entity numbers until we find a 0x8000
        // (which would be remove world entity, but is actually a terminator)
        while ((n = (unsigned short)MSG_ReadShort()) != 0x8000 && !msg_badread)
        {
-               // get the entity number and look it up
+               // get the entity number
                enumber = n & 0x7FFF;
+               // we may need to expand the array
+               if (cl_num_entities <= enumber)
+               {
+                       cl_num_entities = enumber + 1;
+                       if (enumber >= cl_max_entities)
+                               CL_ExpandEntities(enumber);
+               }
+               // look up the entity
                ent = cl_entities + enumber;
                // slide the current into the previous slot
                ent->state_previous = ent->state_current;
@@ -1788,12 +1948,12 @@ void EntityFrame5_CL_ReadFrame(void)
        }
 }
 
-void EntityFrame5_LostFrame(entityframe5_database_t *d, int framenum, int viewentnum)
+void EntityFrame5_LostFrame(entityframe5_database_t *d, int framenum)
 {
        int i, j, k, l, bits;
        entityframe5_changestate_t *s, *s2;
        entityframe5_packetlog_t *p, *p2;
-       qbyte statsdeltabits[(MAX_CL_STATS+7)/8]; 
+       unsigned char statsdeltabits[(MAX_CL_STATS+7)/8];
        // scan for packets that were lost
        for (i = 0, p = d->packetlog;i < ENTITYFRAME5_MAXPACKETLOGS;i++, p++)
        {
@@ -1825,10 +1985,7 @@ void EntityFrame5_LostFrame(entityframe5_database_t *d, int framenum, int viewen
                                // if the bits haven't all been cleared, there were some bits
                                // lost with this packet, so set them again now
                                if (bits)
-                               {
                                        d->deltabits[s->number] |= bits;
-                                       d->priorities[s->number] = EntityState5_Priority(d, d->states + viewentnum, d->states + s->number, d->deltabits[s->number], d->latestframenum - d->updateframenum[s->number]);
-                               }
                        }
                        // mark lost stats
                        for (j = 0;j < MAX_CL_STATS;j++)
@@ -1860,15 +2017,19 @@ void EntityFrame5_AckFrame(entityframe5_database_t *d, int framenum)
 int entityframe5_prioritychaincounts[E5_PROTOCOL_PRIORITYLEVELS];
 unsigned short entityframe5_prioritychains[E5_PROTOCOL_PRIORITYLEVELS][ENTITYFRAME5_MAXSTATES];
 
-void EntityFrame5_WriteFrame(sizebuf_t *msg, entityframe5_database_t *d, int numstates, const entity_state_t *states, int viewentnum, int *stats)
+void EntityFrame5_WriteFrame(sizebuf_t *msg, entityframe5_database_t *d, int numstates, const entity_state_t *states, int viewentnum, int *stats, int movesequence)
 {
        const entity_state_t *n;
        int i, num, l, framenum, packetlognumber, priority;
        sizebuf_t buf;
-       qbyte data[128];
+       unsigned char data[128];
        entityframe5_packetlog_t *packetlog;
 
+       if (prog->max_edicts > d->maxedicts)
+               EntityFrame5_ExpandEdicts(d, prog->max_edicts);
+
        framenum = d->latestframenum + 1;
+       d->viewentnum = viewentnum;
 
        // if packet log is full, mark all frames as lost, this will cause
        // it to send the lost data again
@@ -1877,7 +2038,8 @@ void EntityFrame5_WriteFrame(sizebuf_t *msg, entityframe5_database_t *d, int num
                        break;
        if (packetlognumber == ENTITYFRAME5_MAXPACKETLOGS)
        {
-               EntityFrame5_LostFrame(d, framenum, viewentnum);
+               Con_DPrintf("EntityFrame5_WriteFrame: packetlog overflow for a client, resetting\n");
+               EntityFrame5_LostFrame(d, framenum);
                packetlognumber = 0;
        }
 
@@ -1908,7 +2070,7 @@ void EntityFrame5_WriteFrame(sizebuf_t *msg, entityframe5_database_t *d, int num
                        {
                                CLEARPVSBIT(d->visiblebits, num);
                                d->deltabits[num] = E5_FULLUPDATE;
-                               d->priorities[num] = EntityState5_Priority(d, d->states + viewentnum, d->states + num, d->deltabits[num], framenum - d->updateframenum[num]);
+                               d->priorities[num] = EntityState5_Priority(d, num);
                                d->states[num] = defaultstate;
                                d->states[num].number = num;
                        }
@@ -1922,29 +2084,34 @@ void EntityFrame5_WriteFrame(sizebuf_t *msg, entityframe5_database_t *d, int num
                }
                SETPVSBIT(d->visiblebits, num);
                d->deltabits[num] |= EntityState5_DeltaBits(d->states + num, n);
-               d->priorities[num] = EntityState5_Priority(d, d->states + viewentnum, d->states + num, d->deltabits[num], framenum - d->updateframenum[num]);
+               d->priorities[num] = EntityState5_Priority(d, num);
                d->states[num] = *n;
                d->states[num].number = num;
                // advance to next entity so the next iteration doesn't immediately remove it
                num++;
        }
        // all remaining entities are dead
-       for (;num < MAX_EDICTS;num++)
+       for (;num < d->maxedicts;num++)
        {
                if (CHECKPVSBIT(d->visiblebits, num))
                {
                        CLEARPVSBIT(d->visiblebits, num);
                        d->deltabits[num] = E5_FULLUPDATE;
-                       d->priorities[num] = EntityState5_Priority(d, d->states + viewentnum, d->states + num, d->deltabits[num], framenum - d->updateframenum[num]);
+                       d->priorities[num] = EntityState5_Priority(d, num);
                        d->states[num] = defaultstate;
                        d->states[num].number = num;
                }
        }
 
+       // if there isn't at least enough room for an empty svc_entities,
+       // don't bother trying...
+       if (buf.cursize + 11 > buf.maxsize)
+               return;
+
        // build lists of entities by priority level
        memset(entityframe5_prioritychaincounts, 0, sizeof(entityframe5_prioritychaincounts));
        l = 0;
-       for (num = 0;num < MAX_EDICTS;num++)
+       for (num = 0;num < d->maxedicts;num++)
        {
                if (d->priorities[num])
                {
@@ -1960,9 +2127,9 @@ void EntityFrame5_WriteFrame(sizebuf_t *msg, entityframe5_database_t *d, int num
        packetlog->packetnumber = framenum;
        packetlog->numstates = 0;
        // write stat updates
-       if (sv.protocol == PROTOCOL_DARKPLACES6)
+       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)
        {
-               for (i = 0;i < MAX_CL_STATS;i++)
+               for (i = 0;i < MAX_CL_STATS && msg->cursize + 6 + 11 <= msg->maxsize;i++)
                {
                        if (d->statsdeltabits[i>>3] & (1<<(i&7)))
                        {
@@ -1987,6 +2154,8 @@ void EntityFrame5_WriteFrame(sizebuf_t *msg, entityframe5_database_t *d, int num
        d->latestframenum = framenum;
        MSG_WriteByte(msg, svc_entities);
        MSG_WriteLong(msg, framenum);
+       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)
+               MSG_WriteLong(msg, movesequence);
        for (priority = E5_PROTOCOL_PRIORITYLEVELS - 1;priority >= 0 && packetlog->numstates < ENTITYFRAME5_MAXSTATES;priority--)
        {
                for (i = 0;i < entityframe5_prioritychaincounts[priority] && packetlog->numstates < ENTITYFRAME5_MAXSTATES;i++)