]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - pr_edict.c
PR_Alloc/Free/FreeAll now report filename/fileline of their caller in the Mem_ calls...
[xonotic/darkplaces.git] / pr_edict.c
index 324ad388e95ca966e1fc6d52ac910772c9423239..985fc4db451d5435f417cc7856d2a3ac2d17e7d6 100644 (file)
@@ -34,15 +34,14 @@ int                         pr_edictareasize;               // LordHavoc: in bytes
 
 unsigned short pr_crc;
 
-mempool_t              *progs_mempool;
-mempool_t              *edictstring_mempool;
+mempool_t              *serverprogs_mempool;
 
 int            type_size[8] = {1,sizeof(string_t)/4,1,3,1,1,sizeof(func_t)/4,sizeof(void *)/4};
 
-ddef_t *ED_FieldAtOfs (int ofs);
-qboolean ED_ParseEpair (void *base, ddef_t *key, const char *s);
+ddef_t *ED_FieldAtOfs(int ofs);
+qboolean ED_ParseEpair(edict_t *ent, ddef_t *key, const char *s);
 
-cvar_t pr_checkextension = {0, "pr_checkextension", "1"};
+cvar_t pr_checkextension = {CVAR_READONLY, "pr_checkextension", "1"};
 cvar_t nomonsters = {0, "nomonsters", "0"};
 cvar_t gamecfg = {0, "gamecfg", "0"};
 cvar_t scratch1 = {0, "scratch1", "0"};
@@ -92,6 +91,8 @@ int eval_button5;
 int eval_button6;
 int eval_button7;
 int eval_button8;
+int eval_buttonuse;
+int eval_buttonchat;
 int eval_glow_size;
 int eval_glow_trail;
 int eval_glow_color;
@@ -122,6 +123,18 @@ int eval_viewzoom;
 int eval_clientcolors;
 int eval_tag_entity;
 int eval_tag_index;
+int eval_light_lev;
+int eval_color;
+int eval_style;
+int eval_pflags;
+int eval_cursor_active;
+int eval_cursor_screen;
+int eval_cursor_trace_start;
+int eval_cursor_trace_endpos;
+int eval_cursor_trace_ent;
+int eval_colormod;
+int eval_playermodel;
+int eval_playerskin;
 
 mfunction_t *SV_PlayerPhysicsQC;
 mfunction_t *EndFrameQC;
@@ -146,6 +159,8 @@ void FindEdictFieldOffsets(void)
        eval_button6 = FindFieldOffset("button6");
        eval_button7 = FindFieldOffset("button7");
        eval_button8 = FindFieldOffset("button8");
+       eval_buttonuse = FindFieldOffset("buttonuse");
+       eval_buttonchat = FindFieldOffset("buttonchat");
        eval_glow_size = FindFieldOffset("glow_size");
        eval_glow_trail = FindFieldOffset("glow_trail");
        eval_glow_color = FindFieldOffset("glow_color");
@@ -176,6 +191,18 @@ void FindEdictFieldOffsets(void)
        eval_clientcolors = FindFieldOffset("clientcolors");
        eval_tag_entity = FindFieldOffset("tag_entity");
        eval_tag_index = FindFieldOffset("tag_index");
+       eval_light_lev = FindFieldOffset("light_lev");
+       eval_color = FindFieldOffset("color");
+       eval_style = FindFieldOffset("style");
+       eval_pflags = FindFieldOffset("pflags");
+       eval_cursor_active = FindFieldOffset("cursor_active");
+       eval_cursor_screen = FindFieldOffset("cursor_screen");
+       eval_cursor_trace_start = FindFieldOffset("cursor_trace_start");
+       eval_cursor_trace_endpos = FindFieldOffset("cursor_trace_endpos");
+       eval_cursor_trace_ent = FindFieldOffset("cursor_trace_ent");
+       eval_colormod = FindFieldOffset("colormod");
+       eval_playermodel = FindFieldOffset("playermodel");
+       eval_playerskin = FindFieldOffset("playerskin");
 
        // LordHavoc: allowing QuakeC to override the player movement code
        SV_PlayerPhysicsQC = ED_FindFunction ("SV_PlayerPhysics");
@@ -201,9 +228,21 @@ void ED_ClearEdict (edict_t *e)
        num = NUM_FOR_EDICT(e) - 1;
        if (num >= 0 && num < svs.maxclients)
        {
+               eval_t *val;
+               // set colormap and team on newly created player entity
                e->v->colormap = num + 1;
                e->v->team = (svs.clients[num].colors & 15) + 1;
+               // set netname/clientcolors back to client values so that
+               // DP_SV_CLIENTNAME and DPV_SV_CLIENTCOLORS will not immediately
+               // reset them
                e->v->netname = PR_SetString(svs.clients[num].name);
+               if ((val = GETEDICTFIELDVALUE(e, eval_clientcolors)))
+                       val->_float = svs.clients[num].colors;
+               // NEXUIZ_PLAYERMODEL and NEXUIZ_PLAYERSKIN
+               if( eval_playermodel )
+                       GETEDICTFIELDVALUE(host_client->edict, eval_playermodel)->string = PR_SetString(svs.clients[num].playermodel);
+               if( eval_playerskin )
+                       GETEDICTFIELDVALUE(host_client->edict, eval_playerskin)->string = PR_SetString(svs.clients[num].playerskin);
        }
 }
 
@@ -223,7 +262,7 @@ edict_t *ED_Alloc (void)
        int                     i;
        edict_t         *e;
 
-       for ( i=svs.maxclients+1 ; i<sv.num_edicts ; i++)
+       for (i = svs.maxclients + 1;i < sv.num_edicts;i++)
        {
                e = EDICT_NUM(i);
                // the first couple seconds of server time can involve a lot of
@@ -380,7 +419,7 @@ PR_ValueString
 Returns a string describing *data in a type specific manner
 =============
 */
-int NoCrash_NUM_FOR_EDICT(edict_t *e);
+//int NoCrash_NUM_FOR_EDICT(edict_t *e);
 char *PR_ValueString (etype_t type, eval_t *val)
 {
        static char line[1024]; // LordHavoc: enlarged a bit (was 256)
@@ -393,40 +432,40 @@ char *PR_ValueString (etype_t type, eval_t *val)
        switch (type)
        {
        case ev_string:
-               sprintf (line, "%s", PR_GetString(val->string));
+               strlcpy (line, PR_GetString (val->string), sizeof (line));
                break;
        case ev_entity:
                //n = NoCrash_NUM_FOR_EDICT(PROG_TO_EDICT(val->edict));
                n = val->edict;
                if (n < 0 || n >= MAX_EDICTS)
-                       sprintf (line, "entity %i (invalid!)", n);
+                       dpsnprintf (line, sizeof (line), "entity %i (invalid!)", n);
                else
-                       sprintf (line, "entity %i", n);
+                       dpsnprintf (line, sizeof (line), "entity %i", n);
                break;
        case ev_function:
                f = pr_functions + val->function;
-               sprintf (line, "%s()", PR_GetString(f->s_name));
+               dpsnprintf (line, sizeof (line), "%s()", PR_GetString(f->s_name));
                break;
        case ev_field:
                def = ED_FieldAtOfs ( val->_int );
-               sprintf (line, ".%s", PR_GetString(def->s_name));
+               dpsnprintf (line, sizeof (line), ".%s", PR_GetString(def->s_name));
                break;
        case ev_void:
-               sprintf (line, "void");
+               dpsnprintf (line, sizeof (line), "void");
                break;
        case ev_float:
                // LordHavoc: changed from %5.1f to %10.4f
-               sprintf (line, "%10.4f", val->_float);
+               dpsnprintf (line, sizeof (line), "%10.4f", val->_float);
                break;
        case ev_vector:
                // LordHavoc: changed from %5.1f to %10.4f
-               sprintf (line, "'%10.4f %10.4f %10.4f'", val->vector[0], val->vector[1], val->vector[2]);
+               dpsnprintf (line, sizeof (line), "'%10.4f %10.4f %10.4f'", val->vector[0], val->vector[1], val->vector[2]);
                break;
        case ev_pointer:
-               sprintf (line, "pointer");
+               dpsnprintf (line, sizeof (line), "pointer");
                break;
        default:
-               sprintf (line, "bad type %i", type);
+               dpsnprintf (line, sizeof (line), "bad type %i", type);
                break;
        }
 
@@ -454,23 +493,11 @@ char *PR_UglyValueString (etype_t type, eval_t *val)
        switch (type)
        {
        case ev_string:
-               sprintf (line, "%s", PR_GetString(val->string));
-               break;
-       case ev_entity:
-               sprintf (line, "%i", NUM_FOR_EDICT(PROG_TO_EDICT(val->edict)));
-               break;
-       case ev_function:
-               f = pr_functions + val->function;
-               sprintf (line, "%s", PR_GetString(f->s_name));
-               break;
-       case ev_field:
-               def = ED_FieldAtOfs ( val->_int );
-               // LordHavoc: parse the string a bit to turn special characters
+               // Parse the string a bit to turn special characters
                // (like newline, specifically) into escape codes,
                // this fixes saving games from various mods
-               //sprintf (line, "%s", PR_GetString(def->s_name));
-               s = PR_GetString(def->s_name);
-               for (i = 0;i < 4095 && *s;)
+               s = PR_GetString (val->string);
+               for (i = 0;i < (int)sizeof(line) - 2 && *s;)
                {
                        if (*s == '\n')
                        {
@@ -483,22 +510,33 @@ char *PR_UglyValueString (etype_t type, eval_t *val)
                                line[i++] = 'r';
                        }
                        else
-                               line[i] = *s;
+                               line[i++] = *s;
                        s++;
                }
-               line[i++] = 0;
+               line[i] = '\0';
+               break;
+       case ev_entity:
+               dpsnprintf (line, sizeof (line), "%i", NUM_FOR_EDICT(PROG_TO_EDICT(val->edict)));
+               break;
+       case ev_function:
+               f = pr_functions + val->function;
+               strlcpy (line, PR_GetString (f->s_name), sizeof (line));
+               break;
+       case ev_field:
+               def = ED_FieldAtOfs ( val->_int );
+               dpsnprintf (line, sizeof (line), ".%s", PR_GetString(def->s_name));
                break;
        case ev_void:
-               sprintf (line, "void");
+               dpsnprintf (line, sizeof (line), "void");
                break;
        case ev_float:
-               sprintf (line, "%f", val->_float);
+               dpsnprintf (line, sizeof (line), "%f", val->_float);
                break;
        case ev_vector:
-               sprintf (line, "%f %f %f", val->vector[0], val->vector[1], val->vector[2]);
+               dpsnprintf (line, sizeof (line), "%f %f %f", val->vector[0], val->vector[1], val->vector[2]);
                break;
        default:
-               sprintf (line, "bad type %i", type);
+               dpsnprintf (line, sizeof (line), "bad type %i", type);
                break;
        }
 
@@ -524,17 +562,17 @@ char *PR_GlobalString (int ofs)
        val = (void *)&pr_globals[ofs];
        def = ED_GlobalAtOfs(ofs);
        if (!def)
-               sprintf (line,"%i(?)", ofs);
+               dpsnprintf (line, sizeof (line), "%i(?)", ofs);
        else
        {
                s = PR_ValueString (def->type, val);
-               sprintf (line,"%i(%s)%s", ofs, PR_GetString(def->s_name), s);
+               dpsnprintf (line, sizeof (line), "%i(%s)%s", ofs, PR_GetString(def->s_name), s);
        }
 
        i = strlen(line);
        for ( ; i<20 ; i++)
-               strcat (line," ");
-       strcat (line," ");
+               strlcat (line, " ", sizeof (line));
+       strlcat (line, " ", sizeof (line));
 
        return line;
 }
@@ -547,14 +585,14 @@ char *PR_GlobalStringNoContents (int ofs)
 
        def = ED_GlobalAtOfs(ofs);
        if (!def)
-               sprintf (line,"%i(?)", ofs);
+               dpsnprintf (line, sizeof (line), "%i(?)", ofs);
        else
-               sprintf (line,"%i(%s)", ofs, PR_GetString(def->s_name));
+               dpsnprintf (line, sizeof (line), "%i(%s)", ofs, PR_GetString(def->s_name));
 
        i = strlen(line);
        for ( ; i<20 ; i++)
-               strcat (line," ");
-       strcat (line," ");
+               strlcat (line, " ", sizeof (line));
+       strlcat (line, " ", sizeof (line));
 
        return line;
 }
@@ -569,7 +607,7 @@ For debugging
 */
 // LordHavoc: optimized this to print out much more quickly (tempstring)
 // LordHavoc: changed to print out every 4096 characters (incase there are a lot of fields to print)
-void ED_Print (edict_t *ed)
+void ED_Print(edict_t *ed)
 {
        int             l;
        ddef_t  *d;
@@ -581,12 +619,12 @@ void ED_Print (edict_t *ed)
 
        if (ed->e->free)
        {
-               Con_Print("FREE\n");
+               Con_Print("FREE\n");
                return;
        }
 
        tempstring[0] = 0;
-       sprintf(tempstring, "\nEDICT %i:\n", NUM_FOR_EDICT(ed));
+       dpsnprintf (tempstring, sizeof (tempstring), "\nEDICT %i:\n", NUM_FOR_EDICT(ed));
        for (i=1 ; i<progs->numfielddefs ; i++)
        {
                d = &pr_fielddefs[i];
@@ -607,12 +645,12 @@ void ED_Print (edict_t *ed)
 
                if (strlen(name) > 256)
                {
-                       strncpy(tempstring2, name, 256);
+                       memcpy (tempstring2, name, 256);
                        tempstring2[256] = tempstring2[257] = tempstring2[258] = '.';
                        tempstring2[259] = 0;
                        name = tempstring2;
                }
-               strcat(tempstring, name);
+               strlcat (tempstring, name, sizeof (tempstring));
                for (l = strlen(name);l < 14;l++)
                        strcat(tempstring, " ");
                strcat(tempstring, " ");
@@ -620,21 +658,21 @@ void ED_Print (edict_t *ed)
                name = PR_ValueString(d->type, (eval_t *)v);
                if (strlen(name) > 256)
                {
-                       strncpy(tempstring2, name, 256);
+                       memcpy(tempstring2, name, 256);
                        tempstring2[256] = tempstring2[257] = tempstring2[258] = '.';
                        tempstring2[259] = 0;
                        name = tempstring2;
                }
-               strcat(tempstring, name);
-               strcat(tempstring, "\n");
+               strlcat (tempstring, name, sizeof (tempstring));
+               strlcat (tempstring, "\n", sizeof (tempstring));
                if (strlen(tempstring) >= 4096)
                {
-                       Con_Printf("%s", tempstring);
+                       Con_Print(tempstring);
                        tempstring[0] = 0;
                }
        }
        if (tempstring[0])
-               Con_Printf("%s", tempstring);
+               Con_Print(tempstring);
 }
 
 /*
@@ -652,11 +690,11 @@ void ED_Write (qfile_t *f, edict_t *ed)
        char    *name;
        int             type;
 
-       FS_Print(f, "{\n");
+       FS_Print(f, "{\n");
 
        if (ed->e->free)
        {
-               FS_Print(f, "}\n");
+               FS_Print(f, "}\n");
                return;
        }
 
@@ -677,16 +715,16 @@ void ED_Write (qfile_t *f, edict_t *ed)
                if (j == type_size[type])
                        continue;
 
-               FS_Printf (f,"\"%s\" ",name);
-               FS_Printf (f,"\"%s\"\n", PR_UglyValueString(d->type, (eval_t *)v));
+               FS_Printf(f,"\"%s\" ",name);
+               FS_Printf(f,"\"%s\"\n", PR_UglyValueString(d->type, (eval_t *)v));
        }
 
-       FS_Print(f, "}\n");
+       FS_Print(f, "}\n");
 }
 
 void ED_PrintNum (int ent)
 {
-       ED_Print (EDICT_NUM(ent));
+       ED_Print(EDICT_NUM(ent));
 }
 
 /*
@@ -700,7 +738,7 @@ void ED_PrintEdicts (void)
 {
        int             i;
 
-       Con_Printf ("%i entities\n", sv.num_edicts);
+       Con_Printf("%i entities\n", sv.num_edicts);
        for (i=0 ; i<sv.num_edicts ; i++)
                ED_PrintNum (i);
 }
@@ -717,9 +755,9 @@ void ED_PrintEdict_f (void)
        int             i;
 
        i = atoi (Cmd_Argv(1));
-       if (i >= sv.num_edicts)
+       if (i < 0 || i >= sv.num_edicts)
        {
-               Con_Printf("Bad edict number\n");
+               Con_Print("Bad edict number\n");
                return;
        }
        ED_PrintNum (i);
@@ -753,11 +791,11 @@ void ED_Count (void)
                        step++;
        }
 
-       Con_Printf ("num_edicts:%3i\n", sv.num_edicts);
-       Con_Printf ("active    :%3i\n", active);
-       Con_Printf ("view      :%3i\n", models);
-       Con_Printf ("touch     :%3i\n", solid);
-       Con_Printf ("step      :%3i\n", step);
+       Con_Printf("num_edicts:%3i\n", sv.num_edicts);
+       Con_Printf("active    :%3i\n", active);
+       Con_Printf("view      :%3i\n", models);
+       Con_Printf("touch     :%3i\n", solid);
+       Con_Printf("step      :%3i\n", step);
 
 }
 
@@ -782,7 +820,7 @@ void ED_WriteGlobals (qfile_t *f)
        char            *name;
        int                     type;
 
-       FS_Print(f,"{\n");
+       FS_Print(f,"{\n");
        for (i=0 ; i<progs->numglobaldefs ; i++)
        {
                def = &pr_globaldefs[i];
@@ -795,10 +833,38 @@ void ED_WriteGlobals (qfile_t *f)
                        continue;
 
                name = PR_GetString(def->s_name);
-               FS_Printf (f,"\"%s\" ", name);
-               FS_Printf (f,"\"%s\"\n", PR_UglyValueString(type, (eval_t *)&pr_globals[def->ofs]));
+               FS_Printf(f,"\"%s\" ", name);
+               FS_Printf(f,"\"%s\"\n", PR_UglyValueString(type, (eval_t *)&pr_globals[def->ofs]));
        }
-       FS_Printf (f,"}\n");
+       FS_Print(f,"}\n");
+}
+
+/*
+=============
+ED_EdictSet_f
+
+Console command to set a field of a specified edict
+=============
+*/
+void ED_EdictSet_f(void)
+{
+       edict_t *ed;
+       ddef_t *key;
+
+       if(Cmd_Argc() != 4)
+       {
+               Con_Print("edictset <edict number> <field> <value>\n");
+               return;
+       }
+       ed = EDICT_NUM(atoi(Cmd_Argv(1)));
+
+       if((key = ED_FindField(Cmd_Argv(2))) == 0)
+       {
+               Con_Printf("Key %s not found !\n", Cmd_Argv(2));
+               return;
+       }
+
+       ED_ParseEpair(ed, key, Cmd_Argv(3));
 }
 
 /*
@@ -814,7 +880,7 @@ void ED_ParseGlobals (const char *data)
        while (1)
        {
                // parse key
-               if (!COM_ParseToken (&data))
+               if (!COM_ParseToken(&data, false))
                        Host_Error ("ED_ParseEntity: EOF without closing brace");
                if (com_token[0] == '}')
                        break;
@@ -822,7 +888,7 @@ void ED_ParseGlobals (const char *data)
                strcpy (keyname, com_token);
 
                // parse value
-               if (!COM_ParseToken (&data))
+               if (!COM_ParseToken(&data, false))
                        Host_Error ("ED_ParseEntity: EOF without closing brace");
 
                if (com_token[0] == '}')
@@ -831,11 +897,11 @@ void ED_ParseGlobals (const char *data)
                key = ED_FindGlobal (keyname);
                if (!key)
                {
-                       Con_DPrintf ("'%s' is not a global\n", keyname);
+                       Con_DPrintf("'%s' is not a global\n", keyname);
                        continue;
                }
 
-               if (!ED_ParseEpair ((void *)pr_globals, key, com_token))
+               if (!ED_ParseEpair(NULL, key, com_token))
                        Host_Error ("ED_ParseGlobals: parse error");
        }
 }
@@ -854,7 +920,7 @@ char *ED_NewString (const char *string)
        int i,l;
 
        l = strlen(string) + 1;
-       new = Mem_Alloc(edictstring_mempool, l);
+       new = PR_Alloc(l);
        new_p = new;
 
        for (i=0 ; i< l ; i++)
@@ -864,6 +930,8 @@ char *ED_NewString (const char *string)
                        i++;
                        if (string[i] == 'n')
                                *new_p++ = '\n';
+                       else if (string[i] == 'r')
+                               *new_p++ = '\r';
                        else
                                *new_p++ = '\\';
                }
@@ -883,74 +951,81 @@ Can parse either fields or globals
 returns false if error
 =============
 */
-qboolean       ED_ParseEpair (void *base, ddef_t *key, const char *s)
+qboolean ED_ParseEpair(edict_t *ent, ddef_t *key, const char *s)
 {
-       int             i;
-       char    string[128];
-       ddef_t  *def;
-       char    *v, *w;
-       void    *d;
-       mfunction_t     *func;
-
-       d = (void *)((int *)base + key->ofs);
+       int i;
+       ddef_t *def;
+       eval_t *val;
+       mfunction_t *func;
 
+       if (ent)
+               val = (eval_t *)((int *)ent->v + key->ofs);
+       else
+               val = (eval_t *)((int *)pr_globals + key->ofs);
        switch (key->type & ~DEF_SAVEGLOBAL)
        {
        case ev_string:
-               *(string_t *)d = PR_SetString(ED_NewString(s));
+               val->string = PR_SetString(ED_NewString(s));
                break;
 
        case ev_float:
-               *(float *)d = atof (s);
+               while (*s && *s <= ' ')
+                       s++;
+               val->_float = atof(s);
                break;
 
        case ev_vector:
-               strcpy (string, s);
-               v = string;
-               w = string;
-               for (i=0 ; i<3 ; i++)
+               for (i = 0;i < 3;i++)
                {
-                       while (*v && *v != ' ')
-                               v++;
-                       *v = 0;
-                       ((float *)d)[i] = atof (w);
-                       w = v = v+1;
+                       while (*s && *s <= ' ')
+                               s++;
+                       if (*s)
+                               val->vector[i] = atof(s);
+                       else
+                               val->vector[i] = 0;
+                       while (*s > ' ')
+                               s++;
                }
                break;
 
        case ev_entity:
-               i = atoi (s);
+               while (*s && *s <= ' ')
+                       s++;
+               i = atoi(s);
                if (i < 0 || i >= MAX_EDICTS)
-                       Con_DPrintf("ED_ParseEpair: ev_entity reference too large (edict %i >= MAX_EDICTS %i)\n", i, MAX_EDICTS);
+                       Con_Printf("ED_ParseEpair: ev_entity reference too large (edict %i >= MAX_EDICTS %i)\n", i, MAX_EDICTS);
                while (i >= sv.max_edicts)
                        SV_IncreaseEdicts();
-               *(int *)d = EDICT_TO_PROG(EDICT_NUM(i));
+               // if SV_IncreaseEdicts was called the base pointer needs to be updated
+               if (ent)
+                       val = (eval_t *)((int *)ent->v + key->ofs);
+               val->edict = EDICT_TO_PROG(EDICT_NUM(i));
                break;
 
        case ev_field:
-               def = ED_FindField (s);
+               def = ED_FindField(s);
                if (!def)
                {
-                       // LordHavoc: don't warn about worldspawn sky/fog fields because they don't require mod support
-                       if (strcmp(s, "sky") && strcmp(s, "fog") && strncmp(s, "fog_", 4) && strcmp(s, "farclip"))
-                               Con_DPrintf ("Can't find field %s\n", s);
+                       Con_DPrintf("ED_ParseEpair: Can't find field %s\n", s);
                        return false;
                }
-               *(int *)d = G_INT(def->ofs);
+               //val->_int = G_INT(def->ofs); // AK Please check this - seems to be an org. quake bug
+               val->_int = def->ofs;
                break;
 
        case ev_function:
-               func = ED_FindFunction (s);
+               func = ED_FindFunction(s);
                if (!func)
                {
-                       Con_DPrintf ("Can't find function %s\n", s);
+                       Con_Printf("ED_ParseEpair: Can't find function %s\n", s);
                        return false;
                }
-               *(func_t *)d = func - pr_functions;
+               val->function = func - pr_functions;
                break;
 
        default:
-               break;
+               Con_Printf("ED_ParseEpair: Unknown key->type %i for key \"%s\"\n", key->type, PR_GetString(key->s_name));
+               return false;
        }
        return true;
 }
@@ -982,26 +1057,22 @@ const char *ED_ParseEdict (const char *data, edict_t *ent)
        while (1)
        {
        // parse key
-               if (!COM_ParseToken (&data))
+               if (!COM_ParseToken(&data, false))
                        Host_Error ("ED_ParseEntity: EOF without closing brace");
                if (com_token[0] == '}')
                        break;
 
                // anglehack is to allow QuakeEd to write single scalar angles
                // and allow them to be turned into vectors. (FIXME...)
-               if (!strcmp(com_token, "angle"))
-               {
-                       strcpy (com_token, "angles");
-                       anglehack = true;
-               }
-               else
-                       anglehack = false;
+               anglehack = !strcmp (com_token, "angle");
+               if (anglehack)
+                       strlcpy (com_token, "angles", sizeof (com_token));
 
                // FIXME: change light to _light to get rid of this hack
                if (!strcmp(com_token, "light"))
-                       strcpy (com_token, "light_lev");        // hack for single light def
+                       strlcpy (com_token, "light_lev", sizeof (com_token));   // hack for single light def
 
-               strcpy (keyname, com_token);
+               strlcpy (keyname, com_token, sizeof (keyname));
 
                // another hack to fix heynames with trailing spaces
                n = strlen(keyname);
@@ -1012,7 +1083,7 @@ const char *ED_ParseEdict (const char *data, edict_t *ent)
                }
 
        // parse value
-               if (!COM_ParseToken (&data))
+               if (!COM_ParseToken(&data, false))
                        Host_Error ("ED_ParseEntity: EOF without closing brace");
 
                if (com_token[0] == '}')
@@ -1028,18 +1099,18 @@ const char *ED_ParseEdict (const char *data, edict_t *ent)
                key = ED_FindField (keyname);
                if (!key)
                {
-                       Con_DPrintf ("'%s' is not a field\n", keyname);
+                       Con_DPrintf("'%s' is not a field\n", keyname);
                        continue;
                }
 
                if (anglehack)
                {
                        char    temp[32];
-                       strcpy (temp, com_token);
-                       sprintf (com_token, "0 %s 0", temp);
+                       strlcpy (temp, com_token, sizeof (temp));
+                       dpsnprintf (com_token, sizeof (com_token), "0 %s 0", temp);
                }
 
-               if (!ED_ParseEpair ((void *)ent->v, key, com_token))
+               if (!ED_ParseEpair(ent, key, com_token))
                        Host_Error ("ED_ParseEdict: parse error");
        }
 
@@ -1082,7 +1153,7 @@ void ED_LoadFromFile (const char *data)
        while (1)
        {
 // parse the opening brace
-               if (!COM_ParseToken (&data))
+               if (!COM_ParseToken(&data, false))
                        break;
                if (com_token[0] != '{')
                        Host_Error ("ED_LoadFromFile: found %s when expecting {",com_token);
@@ -1095,31 +1166,33 @@ void ED_LoadFromFile (const char *data)
                parsed++;
 
 // remove things from different skill levels or deathmatch
-               if (deathmatch.integer)
+               if (gamemode != GAME_TRANSFUSION) //Transfusion does this in QC
                {
-                       if (((int)ent->v->spawnflags & SPAWNFLAG_NOT_DEATHMATCH))
+                       if (deathmatch.integer)
+                       {
+                               if (((int)ent->v->spawnflags & SPAWNFLAG_NOT_DEATHMATCH))
+                               {
+                                       ED_Free (ent);
+                                       inhibited++;
+                                       continue;
+                               }
+                       }
+                       else if ((current_skill <= 0 && ((int)ent->v->spawnflags & SPAWNFLAG_NOT_EASY  ))
+                               || (current_skill == 1 && ((int)ent->v->spawnflags & SPAWNFLAG_NOT_MEDIUM))
+                               || (current_skill >= 2 && ((int)ent->v->spawnflags & SPAWNFLAG_NOT_HARD  )))
                        {
                                ED_Free (ent);
                                inhibited++;
                                continue;
                        }
                }
-               else if ((current_skill == 0 && ((int)ent->v->spawnflags & SPAWNFLAG_NOT_EASY  ))
-                         || (current_skill == 1 && ((int)ent->v->spawnflags & SPAWNFLAG_NOT_MEDIUM))
-                         || (current_skill >= 2 && ((int)ent->v->spawnflags & SPAWNFLAG_NOT_HARD  )))
-               {
-                       ED_Free (ent);
-                       inhibited++;
-                       continue;
-               }
-
 //
 // immediately call spawn function
 //
                if (!ent->v->classname)
                {
-                       Con_Print("No classname for:\n");
-                       ED_Print (ent);
+                       Con_Print("No classname for:\n");
+                       ED_Print(ent);
                        ED_Free (ent);
                        continue;
                }
@@ -1131,21 +1204,21 @@ void ED_LoadFromFile (const char *data)
                {
                        if (developer.integer) // don't confuse non-developers with errors
                        {
-                               Con_Print("No spawn function for:\n");
-                               ED_Print (ent);
+                               Con_Print("No spawn function for:\n");
+                               ED_Print(ent);
                        }
                        ED_Free (ent);
                        continue;
                }
 
                pr_global_struct->self = EDICT_TO_PROG(ent);
-               PR_ExecuteProgram (func - pr_functions, "");
+               PR_ExecuteProgram (func - pr_functions, "QC function spawn is missing");
                spawned++;
                if (ent->e->free)
                        died++;
        }
 
-       Con_DPrintf ("%i entities parsed, %i inhibited, %i spawned (%i removed self, %i stayed)\n", parsed, inhibited, spawned, died, spawned - died);
+       Con_DPrintf("%i entities parsed, %i inhibited, %i spawned (%i removed self, %i stayed)\n", parsed, inhibited, spawned, died, spawned - died);
 }
 
 
@@ -1160,42 +1233,57 @@ dpfield_t;
 
 dpfield_t dpfields[] =
 {
-       {ev_float, "gravity"},
+       {ev_entity, "cursor_trace_ent"},
+       {ev_entity, "drawonlytoclient"},
+       {ev_entity, "exteriormodeltoclient"},
+       {ev_entity, "nodrawtoclient"},
+       {ev_entity, "tag_entity"},
+       {ev_entity, "viewmodelforclient"},
+       {ev_float, "alpha"},
+       {ev_float, "ammo_cells1"},
+       {ev_float, "ammo_lava_nails"},
+       {ev_float, "ammo_multi_rockets"},
+       {ev_float, "ammo_nails1"},
+       {ev_float, "ammo_plasma"},
+       {ev_float, "ammo_rockets1"},
+       {ev_float, "ammo_shells1"},
        {ev_float, "button3"},
        {ev_float, "button4"},
        {ev_float, "button5"},
        {ev_float, "button6"},
        {ev_float, "button7"},
        {ev_float, "button8"},
+       {ev_float, "buttonchat"},
+       {ev_float, "buttonuse"},
+       {ev_float, "clientcolors"},
+       {ev_float, "cursor_active"},
+       {ev_float, "fullbright"},
+       {ev_float, "glow_color"},
        {ev_float, "glow_size"},
        {ev_float, "glow_trail"},
-       {ev_float, "glow_color"},
-       {ev_float, "items2"},
-       {ev_float, "scale"},
-       {ev_float, "alpha"},
-       {ev_float, "renderamt"},
-       {ev_float, "rendermode"},
-       {ev_float, "fullbright"},
-       {ev_float, "ammo_shells1"},
-       {ev_float, "ammo_nails1"},
-       {ev_float, "ammo_lava_nails"},
-       {ev_float, "ammo_rockets1"},
-       {ev_float, "ammo_multi_rockets"},
-       {ev_float, "ammo_cells1"},
-       {ev_float, "ammo_plasma"},
+       {ev_float, "gravity"},
        {ev_float, "idealpitch"},
-       {ev_float, "pitch_speed"},
-       {ev_entity, "viewmodelforclient"},
-       {ev_entity, "nodrawtoclient"},
-       {ev_entity, "exteriormodeltoclient"},
-       {ev_entity, "drawonlytoclient"},
+       {ev_float, "items2"},
+       {ev_float, "light_lev"},
+       {ev_float, "pflags"},
        {ev_float, "ping"},
-       {ev_vector, "movement"},
+       {ev_float, "pitch_speed"},
        {ev_float, "pmodel"},
+       {ev_float, "renderamt"}, // HalfLife support
+       {ev_float, "rendermode"}, // HalfLife support
+       {ev_float, "scale"},
+       {ev_float, "style"},
+       {ev_float, "tag_index"},
+       {ev_float, "viewzoom"},
+       {ev_vector, "color"},
+       {ev_vector, "colormod"},
+       {ev_vector, "cursor_screen"},
+       {ev_vector, "cursor_trace_endpos"},
+       {ev_vector, "cursor_trace_start"},
+       {ev_vector, "movement"},
        {ev_vector, "punchvector"},
-       {ev_float, "clientcolors"},
-       {ev_entity, "tag_entity"},
-       {ev_float, "tag_index"}
+       {ev_string, "playermodel"},
+       {ev_string, "playerskin"}
 };
 
 /*
@@ -1204,31 +1292,27 @@ PR_LoadProgs
 ===============
 */
 extern void PR_Cmd_Reset (void);
-void PR_LoadProgs (void)
+void PR_LoadProgs (const char *progsname)
 {
        int i;
        dstatement_t *st;
        ddef_t *infielddefs;
-       void *temp;
        dfunction_t *dfunctions;
 
+       if (!progsname || !*progsname)
+               Host_Error("PR_LoadProgs: passed empty progsname");
+
 // flush the non-C variable lookup cache
        for (i=0 ; i<GEFV_CACHESIZE ; i++)
                gefvCache[i].field[0] = 0;
 
-       Mem_EmptyPool(progs_mempool);
-       Mem_EmptyPool(edictstring_mempool);
-
-       temp = FS_LoadFile ("progs.dat", false);
-       if (!temp)
-               Host_Error ("PR_LoadProgs: couldn't load progs.dat");
+       PR_FreeAll();
 
-       progs = (dprograms_t *)Mem_Alloc(progs_mempool, fs_filesize);
+       progs = (dprograms_t *)FS_LoadFile (progsname, serverprogs_mempool, false);
+       if (!progs)
+               Host_Error ("PR_LoadProgs: couldn't load %s", progsname);
 
-       memcpy(progs, temp, fs_filesize);
-       Mem_Free(temp);
-
-       Con_DPrintf ("Programs occupy %iK.\n", fs_filesize/1024);
+       Con_DPrintf("Programs occupy %iK.\n", fs_filesize/1024);
 
        pr_crc = CRC_Block((qbyte *)progs, fs_filesize);
 
@@ -1238,7 +1322,7 @@ void PR_LoadProgs (void)
 
        if (progs->version != PROG_VERSION)
                Host_Error ("progs.dat has wrong version number (%i should be %i)", progs->version, PROG_VERSION);
-       if (progs->crc != PROGHEADER_CRC)
+       if (progs->crc != PROGHEADER_CRC && progs->crc != 32401) // tenebrae crc also allowed
                Host_Error ("progs.dat system vars have been modified, progdefs.h is out of date");
 
        //pr_functions = (dfunction_t *)((qbyte *)progs + progs->ofs_functions);
@@ -1249,7 +1333,8 @@ void PR_LoadProgs (void)
        // we need to expand the fielddefs list to include all the engine fields,
        // so allocate a new place for it
        infielddefs = (ddef_t *)((qbyte *)progs + progs->ofs_fielddefs);
-       pr_fielddefs = Mem_Alloc(progs_mempool, (progs->numfielddefs + DPFIELDS) * sizeof(ddef_t));
+       pr_fielddefs = PR_Alloc((progs->numfielddefs + DPFIELDS) * sizeof(ddef_t));
+       pr_functions = PR_Alloc(sizeof(mfunction_t) * progs->numfunctions);
 
        pr_statements = (dstatement_t *)((qbyte *)progs + progs->ofs_statements);
 
@@ -1267,7 +1352,6 @@ void PR_LoadProgs (void)
                pr_statements[i].c = LittleShort(pr_statements[i].c);
        }
 
-       pr_functions = Mem_Alloc(progs_mempool, sizeof(mfunction_t) * progs->numfunctions);
        for (i = 0;i < progs->numfunctions;i++)
        {
                pr_functions[i].first_statement = LittleLong (dfunctions[i].first_statement);
@@ -1432,11 +1516,11 @@ void PR_Fields_f (void)
        int *v;
        if (!sv.active)
        {
-               Con_Printf("no progs loaded\n");
+               Con_Print("no progs loaded\n");
                return;
        }
        counts = Mem_Alloc(tempmempool, progs->numfielddefs * sizeof(int));
-       for (ednum = 0;ednum < MAX_EDICTS;ednum++)
+       for (ednum = 0;ednum < sv.max_edicts;ednum++)
        {
                ed = EDICT_NUM(ednum);
                if (ed->e->free)
@@ -1471,50 +1555,50 @@ void PR_Fields_f (void)
                switch(d->type & ~DEF_SAVEGLOBAL)
                {
                case ev_string:
-                       strcat(tempstring, "string   ");
+                       strlcat (tempstring, "string   ", sizeof (tempstring));
                        break;
                case ev_entity:
-                       strcat(tempstring, "entity   ");
+                       strlcat (tempstring, "entity   ", sizeof (tempstring));
                        break;
                case ev_function:
-                       strcat(tempstring, "function ");
+                       strlcat (tempstring, "function ", sizeof (tempstring));
                        break;
                case ev_field:
-                       strcat(tempstring, "field    ");
+                       strlcat (tempstring, "field    ", sizeof (tempstring));
                        break;
                case ev_void:
-                       strcat(tempstring, "void     ");
+                       strlcat (tempstring, "void     ", sizeof (tempstring));
                        break;
                case ev_float:
-                       strcat(tempstring, "float    ");
+                       strlcat (tempstring, "float    ", sizeof (tempstring));
                        break;
                case ev_vector:
-                       strcat(tempstring, "vector   ");
+                       strlcat (tempstring, "vector   ", sizeof (tempstring));
                        break;
                case ev_pointer:
-                       strcat(tempstring, "pointer  ");
+                       strlcat (tempstring, "pointer  ", sizeof (tempstring));
                        break;
                default:
-                       sprintf (tempstring2, "bad type %i ", d->type & ~DEF_SAVEGLOBAL);
-                       strcat(tempstring, tempstring2);
+                       dpsnprintf (tempstring2, sizeof (tempstring2), "bad type %i ", d->type & ~DEF_SAVEGLOBAL);
+                       strlcat (tempstring, tempstring2, sizeof (tempstring));
                        break;
                }
                if (strlen(name) > 256)
                {
-                       strncpy(tempstring2, name, 256);
+                       memcpy(tempstring2, name, 256);
                        tempstring2[256] = tempstring2[257] = tempstring2[258] = '.';
                        tempstring2[259] = 0;
                        name = tempstring2;
                }
-               strcat(tempstring, name);
+               strcat (tempstring, name);
                for (j = strlen(name);j < 25;j++)
                        strcat(tempstring, " ");
-               sprintf(tempstring2, "%5d", counts[i]);
-               strcat(tempstring, tempstring2);
-               strcat(tempstring, "\n");
+               dpsnprintf (tempstring2, sizeof (tempstring2), "%5d", counts[i]);
+               strlcat (tempstring, tempstring2, sizeof (tempstring));
+               strlcat (tempstring, "\n", sizeof (tempstring));
                if (strlen(tempstring) >= 4096)
                {
-                       Con_Printf("%s", tempstring);
+                       Con_Print(tempstring);
                        tempstring[0] = 0;
                }
                if (counts[i])
@@ -1524,7 +1608,7 @@ void PR_Fields_f (void)
                }
        }
        Mem_Free(counts);
-       Con_Printf("%i entity fields (%i in use), totalling %i bytes per edict (%i in use), %i edicts, %i bytes total spent on edict fields (%i needed)\n", progs->entityfields, used, progs->entityfields * 4, usedamount * 4, MAX_EDICTS, progs->entityfields * 4 * MAX_EDICTS, usedamount * 4 * MAX_EDICTS);
+       Con_Printf("%i entity fields (%i in use), totalling %i bytes per edict (%i in use), %i edicts allocated, %i bytes total spent on edict fields (%i needed)\n", progs->entityfields, used, progs->entityfields * 4, usedamount * 4, sv.max_edicts, progs->entityfields * 4 * sv.max_edicts, usedamount * 4 * sv.max_edicts);
 }
 
 void PR_Globals_f (void)
@@ -1532,7 +1616,7 @@ void PR_Globals_f (void)
        int i;
        if (!sv.active)
        {
-               Con_Printf("no progs loaded\n");
+               Con_Print("no progs loaded\n");
                return;
        }
        for (i = 0;i < progs->numglobaldefs;i++)
@@ -1551,6 +1635,7 @@ void PR_Init (void)
        Cmd_AddCommand ("edict", ED_PrintEdict_f);
        Cmd_AddCommand ("edicts", ED_PrintEdicts);
        Cmd_AddCommand ("edictcount", ED_Count);
+       Cmd_AddCommand ("edictset", ED_EdictSet_f);
        Cmd_AddCommand ("profile", PR_Profile_f);
        Cmd_AddCommand ("pr_fields", PR_Fields_f);
        Cmd_AddCommand ("pr_globals", PR_Globals_f);
@@ -1587,12 +1672,42 @@ void PR_Init (void)
        Cvar_RegisterVariable (&pr_boundscheck);
        Cvar_RegisterVariable (&pr_traceqc);
 
-       progs_mempool = Mem_AllocPool("progs.dat");
-       edictstring_mempool = Mem_AllocPool("edict strings");
+       serverprogs_mempool = Mem_AllocPool("server progs", 0, NULL);
 
        PR_Cmd_Init();
 }
 
+/*
+===============
+PR_Shutdown
+===============
+*/
+extern void PR_Cmd_Shutdown(void);
+void PR_Shutdown (void)
+{
+       PR_Cmd_Shutdown();
+
+       Mem_FreePool(&serverprogs_mempool);
+}
+
+void *_PR_Alloc(size_t buffersize, const char *filename, int fileline)
+{
+       return _Mem_Alloc(serverprogs_mempool, buffersize, filename, fileline);
+}
+
+void _PR_Free(void *buffer, const char *filename, int fileline)
+{
+       _Mem_Free(buffer, filename, fileline);
+}
+
+void _PR_FreeAll(const char *filename, int fileline)
+{
+       progs = NULL;
+       pr_fielddefs = NULL;
+       pr_functions = NULL;
+       _Mem_EmptyPool(serverprogs_mempool, filename, fileline);
+}
+
 // LordHavoc: turned EDICT_NUM into a #define for speed reasons
 edict_t *EDICT_NUM_ERROR(int n, char *filename, int fileline)
 {