]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - cl_parse.c
Lots of str[n]cat, str[n]cpy, and [v]sprintf have been replaced by strlcat, strlcpy...
[xonotic/darkplaces.git] / cl_parse.c
index 57b98790e8f29023df0e4541c4632f7a7b1d2791..9a7ad21ac75c0d1306aba779731db1adafb42acb 100644 (file)
@@ -96,9 +96,7 @@ char *svc_strings[128] =
 //=============================================================================
 
 cvar_t demo_nehahra = {0, "demo_nehahra", "0"};
-
-qboolean Nehahrademcompatibility; // LordHavoc: to allow playback of the early Nehahra movie segments
-int dpprotocol; // LordHavoc: version of network protocol, or 0 if not DarkPlaces
+cvar_t developer_networkentities = {0, "developer_networkentities", "0"};
 
 mempool_t *cl_scores_mempool;
 
@@ -221,13 +219,13 @@ void CL_ParseEntityLump(char *entdata)
        data = entdata;
        if (!data)
                return;
-       if (!COM_ParseToken(&data))
+       if (!COM_ParseToken(&data, false))
                return; // error
        if (com_token[0] != '{')
                return; // error
        while (1)
        {
-               if (!COM_ParseToken(&data))
+               if (!COM_ParseToken(&data, false))
                        return; // error
                if (com_token[0] == '}')
                        break; // end of worldspawn
@@ -237,7 +235,7 @@ void CL_ParseEntityLump(char *entdata)
                        strcpy(key, com_token);
                while (key[strlen(key)-1] == ' ') // remove trailing spaces
                        key[strlen(key)-1] = 0;
-               if (!COM_ParseToken(&data))
+               if (!COM_ParseToken(&data, false))
                        return; // error
                strcpy(value, com_token);
                if (!strcmp("sky", key))
@@ -331,19 +329,15 @@ void CL_ParseServerInfo (void)
 
 // parse protocol version number
        i = MSG_ReadLong ();
-       if (i != PROTOCOL_VERSION && i != DPPROTOCOL_VERSION1 && i != DPPROTOCOL_VERSION2 && i != DPPROTOCOL_VERSION3 && i != 250)
+       // hack for unmarked Nehahra movie demos which had a custom protocol
+       if (i == PROTOCOL_QUAKE && cls.demoplayback && demo_nehahra.integer)
+               i = PROTOCOL_NEHAHRAMOVIE;
+       if (i != PROTOCOL_QUAKE && i != PROTOCOL_DARKPLACES1 && i != PROTOCOL_DARKPLACES2 && i != PROTOCOL_DARKPLACES3 && i != PROTOCOL_DARKPLACES4 && i != PROTOCOL_NEHAHRAMOVIE)
        {
-               Host_Error ("Server is protocol %i, not %i, %i, %i or %i", i, DPPROTOCOL_VERSION1, DPPROTOCOL_VERSION2, DPPROTOCOL_VERSION3, PROTOCOL_VERSION);
+               Host_Error("CL_ParseServerInfo: Server is protocol %i, not %i (Quake), %i (DP1), %i (DP2), %i (DP3), %i (DP4), or %i (Nehahra movie)", i, PROTOCOL_QUAKE, PROTOCOL_DARKPLACES1, PROTOCOL_DARKPLACES2, PROTOCOL_DARKPLACES3, PROTOCOL_DARKPLACES4, PROTOCOL_NEHAHRAMOVIE);
                return;
        }
-       Nehahrademcompatibility = false;
-       if (i == 250)
-               Nehahrademcompatibility = true;
-       if (cls.demoplayback && demo_nehahra.integer)
-               Nehahrademcompatibility = true;
-       dpprotocol = i;
-       if (dpprotocol != DPPROTOCOL_VERSION1 && dpprotocol != DPPROTOCOL_VERSION2 && dpprotocol != DPPROTOCOL_VERSION3)
-               dpprotocol = 0;
+       cl.protocol = i;
 
 // parse maxclients
        cl.maxclients = MSG_ReadByte ();
@@ -360,13 +354,13 @@ void CL_ParseServerInfo (void)
 
 // parse signon message
        str = MSG_ReadString ();
-       strncpy (cl.levelname, str, sizeof(cl.levelname)-1);
+       strlcpy (cl.levelname, str, sizeof(cl.levelname));
 
 // seperate the printfs so the server message can have a color
-       if (!Nehahrademcompatibility) // no messages when playing the Nehahra movie
+       if (cl.protocol != PROTOCOL_NEHAHRAMOVIE) // no messages when playing the Nehahra movie
        {
                Con_Printf("\n\n\35\36\36\36\36\36\36\36\36\36\36\36\36\36\36\36\36\36\36\36\36\36\36\36\36\36\36\36\36\36\36\36\36\36\36\36\37\n\n");
-               Con_Printf ("%c%s\n", 2, str);
+               Con_Printf("%c%s\n", 2, str);
        }
 
        // check memory integrity
@@ -532,7 +526,7 @@ void CL_MoveLerpEntityStates(entity_t *ent)
                // not a monster
                ent->persistent.lerpstarttime = cl.mtime[1];
                // no lerp if it's singleplayer
-               if (sv.active && svs.maxclients == 1)
+               if (cl.islocalgame)
                        ent->persistent.lerpdeltatime = 0;
                else
                        ent->persistent.lerpdeltatime = cl.mtime[0] - cl.mtime[1];
@@ -560,7 +554,7 @@ void CL_ParseUpdate (int bits)
 
        if (bits & U_MOREBITS)
                bits |= (MSG_ReadByte()<<8);
-       if ((bits & U_EXTEND1) && (!Nehahrademcompatibility))
+       if ((bits & U_EXTEND1) && cl.protocol != PROTOCOL_NEHAHRAMOVIE)
        {
                bits |= MSG_ReadByte() << 16;
                if (bits & U_EXTEND2)
@@ -619,7 +613,7 @@ void CL_ParseUpdate (int bits)
        if (bits & U_EXTERIORMODEL)     new.flags |= RENDER_EXTERIORMODEL;
 
        // LordHavoc: to allow playback of the Nehahra movie
-       if (Nehahrademcompatibility && (bits & U_EXTEND1))
+       if (cl.protocol == PROTOCOL_NEHAHRAMOVIE && (bits & U_EXTEND1))
        {
                // LordHavoc: evil format
                int i = MSG_ReadFloat();
@@ -653,22 +647,32 @@ void CL_ParseUpdate (int bits)
 }
 
 static entity_frame_t entityframe;
+extern mempool_t *cl_entities_mempool;
 void CL_ReadEntityFrame(void)
 {
-       entity_t *ent;
-       int i;
-       EntityFrame_Read(&cl.entitydatabase);
-       EntityFrame_FetchFrame(&cl.entitydatabase, EntityFrame_MostRecentlyRecievedFrameNum(&cl.entitydatabase), &entityframe);
-       for (i = 0;i < entityframe.numentities;i++)
+       if (cl.protocol == PROTOCOL_DARKPLACES1 || cl.protocol == PROTOCOL_DARKPLACES2 || cl.protocol == PROTOCOL_DARKPLACES3)
        {
-               // copy the states
-               ent = &cl_entities[entityframe.entitydata[i].number];
-               ent->state_previous = ent->state_current;
-               ent->state_current = entityframe.entitydata[i];
-               CL_MoveLerpEntityStates(ent);
-               // the entity lives again...
-               entlife[ent->state_current.number] = 2;
-               cl_entities_active[ent->state_current.number] = true;
+               int i;
+               entity_t *ent;
+               EntityFrame_Read(&cl.entitydatabase);
+               EntityFrame_FetchFrame(&cl.entitydatabase, EntityFrame_MostRecentlyRecievedFrameNum(&cl.entitydatabase), &entityframe);
+               for (i = 0;i < entityframe.numentities;i++)
+               {
+                       // copy the states
+                       ent = &cl_entities[entityframe.entitydata[i].number];
+                       ent->state_previous = ent->state_current;
+                       ent->state_current = entityframe.entitydata[i];
+                       CL_MoveLerpEntityStates(ent);
+                       // the entity lives again...
+                       entlife[ent->state_current.number] = 2;
+                       cl_entities_active[ent->state_current.number] = true;
+               }
+       }
+       else
+       {
+               if (!cl.entitydatabase4)
+                       cl.entitydatabase4 = EntityFrame4_AllocDatabase(cl_entities_mempool);
+               EntityFrame4_CL_ReadFrame(cl.entitydatabase4);
        }
 }
 
@@ -678,18 +682,21 @@ void CL_EntityUpdateSetup(void)
 
 void CL_EntityUpdateEnd(void)
 {
-       int i;
-       // disable entities that disappeared this frame
-       for (i = 1;i < MAX_EDICTS;i++)
+       if (cl.protocol == PROTOCOL_QUAKE || cl.protocol == PROTOCOL_NEHAHRAMOVIE || cl.protocol == PROTOCOL_DARKPLACES1 || cl.protocol == PROTOCOL_DARKPLACES2 || cl.protocol == PROTOCOL_DARKPLACES3)
        {
-               // clear only the entities that were active last frame but not this
-               // frame, don't waste time clearing all entities (which would cause
-               // cache misses)
-               if (entlife[i])
+               int i;
+               // disable entities that disappeared this frame
+               for (i = 1;i < MAX_EDICTS;i++)
                {
-                       entlife[i]--;
-                       if (!entlife[i])
-                               cl_entities[i].state_previous.active = cl_entities[i].state_current.active = 0;
+                       // clear only the entities that were active last frame but not this
+                       // frame, don't waste time clearing all entities (which would cause
+                       // cache misses)
+                       if (entlife[i])
+                       {
+                               entlife[i]--;
+                               if (!entlife[i])
+                                       cl_entities[i].state_previous.active = cl_entities[i].state_current.active = 0;
+                       }
                }
        }
 }
@@ -703,7 +710,7 @@ void CL_ParseBaseline (entity_t *ent, int large)
 {
        int i;
 
-       memset(&ent->state_baseline, 0, sizeof(entity_state_t));
+       ClearStateToDefault(&ent->state_baseline);
        ent->state_baseline.active = true;
        if (large)
        {
@@ -722,13 +729,8 @@ void CL_ParseBaseline (entity_t *ent, int large)
                ent->state_baseline.origin[i] = MSG_ReadCoord ();
                ent->state_baseline.angles[i] = MSG_ReadAngle ();
        }
-       ent->state_baseline.alpha = 255;
-       ent->state_baseline.scale = 16;
-       ent->state_baseline.glowsize = 0;
-       ent->state_baseline.glowcolor = 254;
-       ent->state_previous = ent->state_current = ent->state_baseline;
-
        CL_ValidateState(&ent->state_baseline);
+       ent->state_previous = ent->state_current = ent->state_baseline;
 }
 
 
@@ -764,7 +766,7 @@ void CL_ParseClientdata (int bits)
        {
                if (bits & (SU_PUNCH1<<i) )
                {
-                       if (dpprotocol)
+                       if (cl.protocol == PROTOCOL_DARKPLACES1 || cl.protocol == PROTOCOL_DARKPLACES2 || cl.protocol == PROTOCOL_DARKPLACES3 || cl.protocol == PROTOCOL_DARKPLACES4)
                                cl.punchangle[i] = MSG_ReadPreciseAngle();
                        else
                                cl.punchangle[i] = MSG_ReadChar();
@@ -805,11 +807,14 @@ void CL_ParseClientdata (int bits)
        cl.stats[STAT_CELLS] = MSG_ReadByte();
 
        i = MSG_ReadByte ();
-
        if (gamemode == GAME_HIPNOTIC || gamemode == GAME_ROGUE)
-               cl.stats[STAT_ACTIVEWEAPON] = (1<<i);
-       else
-               cl.stats[STAT_ACTIVEWEAPON] = i;
+               i = (1<<i);
+       // GAME_NEXUIZ hud needs weapon change time
+       // GAME_NEXUIZ uses a bit number as it's STAT_ACTIVEWEAPON, not a bitfield
+       // like other modes
+       if (cl.stats[STAT_ACTIVEWEAPON] != i)
+               cl.weapontime = cl.time;
+       cl.stats[STAT_ACTIVEWEAPON] = i;
 
        cl.viewzoomold = cl.viewzoomnew; // for interpolation
        if (bits & SU_VIEWZOOM)
@@ -1113,12 +1118,6 @@ void CL_ParseTempEntity(void)
                count = MSG_ReadByte();
                CL_BloodPuff(pos, dir, count);
                break;
-       case TE_BLOOD2:
-               // blood puff
-               MSG_ReadVector(pos);
-               CL_FindNonSolidLocation(pos, pos, 4);
-               CL_BloodPuff(pos, vec3_origin, 10);
-               break;
        case TE_SPARK:
                // spark shower
                MSG_ReadVector(pos);
@@ -1466,8 +1465,8 @@ void CL_ParseServerMessage(void)
                                i &= 31;
                                while(count > 0)
                                {
-                                       sprintf(temp, "%3i:%s ", cmdlog[i], cmdlogname[i]);
-                                       strcat(description, temp);
+                                       snprintf (temp, sizeof (temp), "%3i:%s ", cmdlog[i], cmdlogname[i]);
+                                       strlcat (description, temp, sizeof (description));
                                        count--;
                                        i++;
                                        i &= 31;
@@ -1502,16 +1501,12 @@ void CL_ParseServerMessage(void)
 
                case svc_version:
                        i = MSG_ReadLong ();
-                       if (i != PROTOCOL_VERSION && i != DPPROTOCOL_VERSION1 && i != DPPROTOCOL_VERSION2 && i != DPPROTOCOL_VERSION3 && i != 250)
-                               Host_Error ("CL_ParseServerMessage: Server is protocol %i, not %i, %i, %i or %i", i, DPPROTOCOL_VERSION1, DPPROTOCOL_VERSION2, DPPROTOCOL_VERSION3, PROTOCOL_VERSION);
-                       Nehahrademcompatibility = false;
-                       if (i == 250)
-                               Nehahrademcompatibility = true;
-                       if (cls.demoplayback && demo_nehahra.integer)
-                               Nehahrademcompatibility = true;
-                       dpprotocol = i;
-                       if (dpprotocol != DPPROTOCOL_VERSION1 && dpprotocol != DPPROTOCOL_VERSION2 && dpprotocol != DPPROTOCOL_VERSION3)
-                               dpprotocol = 0;
+                       // hack for unmarked Nehahra movie demos which had a custom protocol
+                       if (i == PROTOCOL_QUAKE && cls.demoplayback && demo_nehahra.integer)
+                               i = PROTOCOL_NEHAHRAMOVIE;
+                       if (i != PROTOCOL_QUAKE && i != PROTOCOL_DARKPLACES1 && i != PROTOCOL_DARKPLACES2 && i != PROTOCOL_DARKPLACES3 && i != PROTOCOL_DARKPLACES4 && i != PROTOCOL_NEHAHRAMOVIE)
+                               Host_Error("CL_ParseServerMessage: Server is protocol %i, not %i (Quake), %i (DP1), %i (DP2), %i (DP3), %i (DP4), or %i (Nehahra movie)", i, PROTOCOL_QUAKE, PROTOCOL_DARKPLACES1, PROTOCOL_DARKPLACES2, PROTOCOL_DARKPLACES3, PROTOCOL_DARKPLACES4, PROTOCOL_NEHAHRAMOVIE);
+                       cl.protocol = i;
                        break;
 
                case svc_disconnect:
@@ -1555,7 +1550,7 @@ void CL_ParseServerMessage(void)
                        i = MSG_ReadByte ();
                        if (i >= MAX_LIGHTSTYLES)
                                Host_Error ("svc_lightstyle >= MAX_LIGHTSTYLES");
-                       strncpy (cl_lightstyle[i].map,  MSG_ReadString(), MAX_STYLESTRING - 1);
+                       strlcpy (cl_lightstyle[i].map,  MSG_ReadString(), sizeof (cl_lightstyle[i].map));
                        cl_lightstyle[i].map[MAX_STYLESTRING - 1] = 0;
                        cl_lightstyle[i].length = strlen(cl_lightstyle[i].map);
                        break;
@@ -1577,7 +1572,7 @@ void CL_ParseServerMessage(void)
                        i = MSG_ReadByte ();
                        if (i >= cl.maxclients)
                                Host_Error ("CL_ParseServerMessage: svc_updatename >= cl.maxclients");
-                       strcpy (cl.scores[i].name, MSG_ReadString ());
+                       strlcpy (cl.scores[i].name, MSG_ReadString (), sizeof (cl.scores[i].name));
                        break;
 
                case svc_updatefrags:
@@ -1749,4 +1744,5 @@ void CL_Parse_Init(void)
        Cvar_RegisterVariable (&demo_nehahra);
        if (gamemode == GAME_NEHAHRA)
                Cvar_SetValue("demo_nehahra", 1);
+       Cvar_RegisterVariable(&developer_networkentities);
 }