]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - pr_edict.c
rewrote memory system entirely (hunk, cache, and zone are gone, memory pools replaced...
[xonotic/darkplaces.git] / pr_edict.c
index c7e722abed5b5bfe761afcfe202c1f3e082c8a6a..dc4166ad8647e50406ff86cb71a499a1c6186532 100644 (file)
@@ -8,7 +8,7 @@ of the License, or (at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
 See the GNU General Public License for more details.
 
 
 See the GNU General Public License for more details.
 
@@ -32,7 +32,10 @@ float                        *pr_globals;                    // same as pr_global_struct
 int                            pr_edict_size;                  // in bytes
 int                            pr_edictareasize;               // LordHavoc: in bytes
 
 int                            pr_edict_size;                  // in bytes
 int                            pr_edictareasize;               // LordHavoc: in bytes
 
-unsigned short         pr_crc;
+unsigned short pr_crc;
+
+mempool_t              *progs_mempool;
+mempool_t              *edictstring_mempool;
 
 int            type_size[8] = {1,sizeof(string_t)/4,1,3,1,1,sizeof(func_t)/4,sizeof(void *)/4};
 
 
 int            type_size[8] = {1,sizeof(string_t)/4,1,3,1,1,sizeof(func_t)/4,sizeof(void *)/4};
 
@@ -109,7 +112,6 @@ int eval_viewmodelforclient;
 int eval_nodrawtoclient;
 int eval_exteriormodeltoclient;
 int eval_drawonlytoclient;
 int eval_nodrawtoclient;
 int eval_exteriormodeltoclient;
 int eval_drawonlytoclient;
-int eval_colormod;
 int eval_ping;
 int eval_movement;
 int eval_pmodel;
 int eval_ping;
 int eval_movement;
 int eval_pmodel;
@@ -158,7 +160,6 @@ void FindEdictFieldOffsets(void)
        eval_nodrawtoclient = FindFieldOffset("nodrawtoclient");
        eval_exteriormodeltoclient = FindFieldOffset("exteriormodeltoclient");
        eval_drawonlytoclient = FindFieldOffset("drawonlytoclient");
        eval_nodrawtoclient = FindFieldOffset("nodrawtoclient");
        eval_exteriormodeltoclient = FindFieldOffset("exteriormodeltoclient");
        eval_drawonlytoclient = FindFieldOffset("drawonlytoclient");
-       eval_colormod = FindFieldOffset("colormod");
        eval_ping = FindFieldOffset("ping");
        eval_movement = FindFieldOffset("movement");
        eval_pmodel = FindFieldOffset("pmodel");
        eval_ping = FindFieldOffset("ping");
        eval_movement = FindFieldOffset("movement");
        eval_pmodel = FindFieldOffset("pmodel");
@@ -297,7 +298,7 @@ ddef_t *ED_FindField (char *name)
 {
        ddef_t          *def;
        int                     i;
 {
        ddef_t          *def;
        int                     i;
-       
+
        for (i=0 ; i<progs->numfielddefs ; i++)
        {
                def = &pr_fielddefs[i];
        for (i=0 ; i<progs->numfielddefs ; i++)
        {
                def = &pr_fielddefs[i];
@@ -387,12 +388,14 @@ PR_ValueString
 Returns a string describing *data in a type specific manner
 =============
 */
 Returns a string describing *data in a type specific manner
 =============
 */
+int NoCrash_NUM_FOR_EDICT(edict_t *e);
 char *PR_ValueString (etype_t type, eval_t *val)
 {
 char *PR_ValueString (etype_t type, eval_t *val)
 {
-       static char     line[1024]; // LordHavoc: enlarged a bit (was 256)
-       ddef_t          *def;
-       dfunction_t     *f;
-       
+       static char line[1024]; // LordHavoc: enlarged a bit (was 256)
+       ddef_t *def;
+       dfunction_t *f;
+       int n;
+
        type &= ~DEF_SAVEGLOBAL;
 
        switch (type)
        type &= ~DEF_SAVEGLOBAL;
 
        switch (type)
@@ -400,8 +403,12 @@ char *PR_ValueString (etype_t type, eval_t *val)
        case ev_string:
                sprintf (line, "%s", pr_strings + val->string);
                break;
        case ev_string:
                sprintf (line, "%s", pr_strings + val->string);
                break;
-       case ev_entity: 
-               sprintf (line, "entity %i", NUM_FOR_EDICT(PROG_TO_EDICT(val->edict)) );
+       case ev_entity:
+               n = NoCrash_NUM_FOR_EDICT(PROG_TO_EDICT(val->edict));
+               if (n < 0 || n >= MAX_EDICTS)
+                       sprintf (line, "entity %i (invalid!)", n);
+               else
+                       sprintf (line, "entity %i", n);
                break;
        case ev_function:
                f = pr_functions + val->function;
                break;
        case ev_function:
                f = pr_functions + val->function;
@@ -454,7 +461,7 @@ char *PR_UglyValueString (etype_t type, eval_t *val)
        case ev_string:
                sprintf (line, "%s", pr_strings + val->string);
                break;
        case ev_string:
                sprintf (line, "%s", pr_strings + val->string);
                break;
-       case ev_entity: 
+       case ev_entity:
                sprintf (line, "%i", NUM_FOR_EDICT(PROG_TO_EDICT(val->edict)));
                break;
        case ev_function:
                sprintf (line, "%i", NUM_FOR_EDICT(PROG_TO_EDICT(val->edict)));
                break;
        case ev_function:
@@ -501,7 +508,7 @@ char *PR_GlobalString (int ofs)
        val = (void *)&pr_globals[ofs];
        def = ED_GlobalAtOfs(ofs);
        if (!def)
        val = (void *)&pr_globals[ofs];
        def = ED_GlobalAtOfs(ofs);
        if (!def)
-               sprintf (line,"%i(??\?)", ofs); // LordHavoc: escaping the third ? so it is not a trigraph
+               sprintf (line,"%i(\?\?\?)", ofs); // LordHavoc: escaping the ?s so it is not a trigraph
        else
        {
                s = PR_ValueString (def->type, val);
        else
        {
                s = PR_ValueString (def->type, val);
@@ -524,7 +531,7 @@ char *PR_GlobalStringNoContents (int ofs)
 
        def = ED_GlobalAtOfs(ofs);
        if (!def)
 
        def = ED_GlobalAtOfs(ofs);
        if (!def)
-               sprintf (line,"%i(??\?)", ofs); // LordHavoc: escaping the third ? so it is not a trigraph
+               sprintf (line,"%i(\?\?\?)", ofs); // LordHavoc: escaping the ?s so it is not a trigraph
        else
                sprintf (line,"%i(%s)", ofs, pr_strings + def->s_name);
 
        else
                sprintf (line,"%i(%s)", ofs, pr_strings + def->s_name);
 
@@ -575,7 +582,7 @@ void ED_Print (edict_t *ed)
 
        // if the value is still all 0, skip the field
                type = d->type & ~DEF_SAVEGLOBAL;
 
        // if the value is still all 0, skip the field
                type = d->type & ~DEF_SAVEGLOBAL;
-               
+
                for (j=0 ; j<type_size[type] ; j++)
                        if (v[j])
                                break;
                for (j=0 ; j<type_size[type] ; j++)
                        if (v[j])
                                break;
@@ -643,7 +650,7 @@ void ED_Write (QFile *f, edict_t *ed)
                name = pr_strings + d->s_name;
                if (name[strlen(name)-2] == '_')
                        continue;       // skip _x, _y, _z vars
                name = pr_strings + d->s_name;
                if (name[strlen(name)-2] == '_')
                        continue;       // skip _x, _y, _z vars
-                       
+
                v = (int *)((char *)&ed->v + d->ofs*4);
 
        // if the value is still all 0, skip the field
                v = (int *)((char *)&ed->v + d->ofs*4);
 
        // if the value is still all 0, skip the field
@@ -653,9 +660,9 @@ void ED_Write (QFile *f, edict_t *ed)
                                break;
                if (j == type_size[type])
                        continue;
                                break;
                if (j == type_size[type])
                        continue;
-       
+
                Qprintf (f,"\"%s\" ",name);
                Qprintf (f,"\"%s\" ",name);
-               Qprintf (f,"\"%s\"\n", PR_UglyValueString(d->type, (eval_t *)v));               
+               Qprintf (f,"\"%s\"\n", PR_UglyValueString(d->type, (eval_t *)v));
        }
 
        Qprintf (f, "}\n");
        }
 
        Qprintf (f, "}\n");
@@ -799,7 +806,7 @@ void ED_ParseGlobals (char *data)
 
                strcpy (keyname, com_token);
 
 
                strcpy (keyname, com_token);
 
-       // parse value  
+       // parse value
                data = COM_Parse (data);
                if (!data)
                        Host_Error ("ED_ParseEntity: EOF without closing brace");
                data = COM_Parse (data);
                if (!data)
                        Host_Error ("ED_ParseEntity: EOF without closing brace");
@@ -831,9 +838,9 @@ char *ED_NewString (char *string)
 {
        char    *new, *new_p;
        int             i,l;
 {
        char    *new, *new_p;
        int             i,l;
-       
+
        l = strlen(string) + 1;
        l = strlen(string) + 1;
-       new = Hunk_AllocName (l, "edict string");
+       new = Mem_Alloc(edictstring_mempool, l);
        new_p = new;
 
        for (i=0 ; i< l ; i++)
        new_p = new;
 
        for (i=0 ; i< l ; i++)
@@ -849,7 +856,7 @@ char *ED_NewString (char *string)
                else
                        *new_p++ = string[i];
        }
                else
                        *new_p++ = string[i];
        }
-       
+
        return new;
 }
 
        return new;
 }
 
@@ -870,9 +877,9 @@ qboolean    ED_ParseEpair (void *base, ddef_t *key, char *s)
        char    *v, *w;
        void    *d;
        dfunction_t     *func;
        char    *v, *w;
        void    *d;
        dfunction_t     *func;
-       
+
        d = (void *)((int *)base + key->ofs);
        d = (void *)((int *)base + key->ofs);
-       
+
        switch (key->type & ~DEF_SAVEGLOBAL)
        {
        case ev_string:
        switch (key->type & ~DEF_SAVEGLOBAL)
        {
        case ev_string:
@@ -896,7 +903,7 @@ qboolean    ED_ParseEpair (void *base, ddef_t *key, char *s)
                        w = v = v+1;
                }
                break;
                        w = v = v+1;
                }
                break;
-               
+
        case ev_entity:
                *(int *)d = EDICT_TO_PROG(EDICT_NUM(atoi (s)));
                break;
        case ev_entity:
                *(int *)d = EDICT_TO_PROG(EDICT_NUM(atoi (s)));
                break;
@@ -922,7 +929,7 @@ qboolean    ED_ParseEpair (void *base, ddef_t *key, char *s)
                }
                *(func_t *)d = func - pr_functions;
                break;
                }
                *(func_t *)d = func - pr_functions;
                break;
-               
+
        default:
                break;
        }
        default:
                break;
        }
@@ -954,27 +961,27 @@ char *ED_ParseEdict (char *data, edict_t *ent)
 
 // go through all the dictionary pairs
        while (1)
 
 // go through all the dictionary pairs
        while (1)
-       {       
+       {
        // parse key
                data = COM_Parse (data);
                if (com_token[0] == '}')
                        break;
                if (!data)
                        Host_Error ("ED_ParseEntity: EOF without closing brace");
        // parse key
                data = COM_Parse (data);
                if (com_token[0] == '}')
                        break;
                if (!data)
                        Host_Error ("ED_ParseEntity: EOF without closing brace");
-               
-// 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;
 
 
-// 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
+               // 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;
+
+               // 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
 
                strcpy (keyname, com_token);
 
 
                strcpy (keyname, com_token);
 
@@ -986,7 +993,7 @@ if (!strcmp(com_token, "light"))
                        n--;
                }
 
                        n--;
                }
 
-       // parse value  
+       // parse value
                data = COM_Parse (data);
                if (!data)
                        Host_Error ("ED_ParseEntity: EOF without closing brace");
                data = COM_Parse (data);
                if (!data)
                        Host_Error ("ED_ParseEntity: EOF without closing brace");
@@ -994,13 +1001,13 @@ if (!strcmp(com_token, "light"))
                if (com_token[0] == '}')
                        Host_Error ("ED_ParseEntity: closing brace without data");
 
                if (com_token[0] == '}')
                        Host_Error ("ED_ParseEntity: closing brace without data");
 
-               init = true;    
+               init = true;
 
 // keynames with a leading underscore are used for utility comments,
 // and are immediately discarded by quake
                if (keyname[0] == '_')
                        continue;
 
 // keynames with a leading underscore are used for utility comments,
 // and are immediately discarded by quake
                if (keyname[0] == '_')
                        continue;
-               
+
                key = ED_FindField (keyname);
                if (!key)
                {
                key = ED_FindField (keyname);
                if (!key)
                {
@@ -1008,12 +1015,12 @@ if (!strcmp(com_token, "light"))
                        continue;
                }
 
                        continue;
                }
 
-if (anglehack)
-{
-char   temp[32];
-strcpy (temp, com_token);
-sprintf (com_token, "0 %s 0", temp);
-}
+               if (anglehack)
+               {
+                       char    temp[32];
+                       strcpy (temp, com_token);
+                       sprintf (com_token, "0 %s 0", temp);
+               }
 
                if (!ED_ParseEpair ((void *)&ent->v, key, com_token))
                        Host_Error ("ED_ParseEdict: parse error");
 
                if (!ED_ParseEpair ((void *)&ent->v, key, com_token))
                        Host_Error ("ED_ParseEdict: parse error");
@@ -1046,7 +1053,7 @@ void ED_LoadFromFile (char *data)
        edict_t         *ent;
        int                     inhibit;
        dfunction_t     *func;
        edict_t         *ent;
        int                     inhibit;
        dfunction_t     *func;
-       
+
        ent = NULL;
        inhibit = 0;
        pr_global_struct->time = sv.time;
        ent = NULL;
        inhibit = 0;
        pr_global_struct->time = sv.time;
@@ -1068,7 +1075,7 @@ void ED_LoadFromFile (char *data)
                data = ED_ParseEdict (data, ent);
 
 // remove things from different skill levels or deathmatch
                data = ED_ParseEdict (data, ent);
 
 // remove things from different skill levels or deathmatch
-               if (deathmatch.value)
+               if (deathmatch.integer)
                {
                        if (((int)ent->v.spawnflags & SPAWNFLAG_NOT_DEATHMATCH))
                        {
                {
                        if (((int)ent->v.spawnflags & SPAWNFLAG_NOT_DEATHMATCH))
                        {
@@ -1102,7 +1109,7 @@ void ED_LoadFromFile (char *data)
 
                if (!func)
                {
 
                if (!func)
                {
-                       if (developer.value) // don't confuse non-developers with errors
+                       if (developer.integer) // don't confuse non-developers with errors
                        {
                                Con_Printf ("No spawn function for:\n");
                                ED_Print (ent);
                        {
                                Con_Printf ("No spawn function for:\n");
                                ED_Print (ent);
@@ -1113,12 +1120,58 @@ void ED_LoadFromFile (char *data)
 
                pr_global_struct->self = EDICT_TO_PROG(ent);
                PR_ExecuteProgram (func - pr_functions, "");
 
                pr_global_struct->self = EDICT_TO_PROG(ent);
                PR_ExecuteProgram (func - pr_functions, "");
-       }       
+       }
 
        Con_DPrintf ("%i entities inhibited\n", inhibit);
 }
 
 
 
        Con_DPrintf ("%i entities inhibited\n", inhibit);
 }
 
 
+typedef struct dpfield_s
+{
+       int type;
+       char *string;
+}
+dpfield_t;
+
+#define DPFIELDS (sizeof(dpfields) / sizeof(dpfield_t))
+
+dpfield_t dpfields[] =
+{
+       {ev_float, "gravity"},
+       {ev_float, "button3"},
+       {ev_float, "button4"},
+       {ev_float, "button5"},
+       {ev_float, "button6"},
+       {ev_float, "button7"},
+       {ev_float, "button8"},
+       {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, "idealpitch"},
+       {ev_float, "pitch_speed"},
+       {ev_entity, "viewmodelforclient"},
+       {ev_entity, "nodrawtoclient"},
+       {ev_entity, "exteriormodeltoclient"},
+       {ev_entity, "drawonlytoclient"},
+       {ev_float, "ping"},
+       {ev_vector, "movement"},
+       {ev_float, "pmodel"},
+       {ev_vector, "punchvector"}
+};
+
 /*
 ===============
 PR_LoadProgs
 /*
 ===============
 PR_LoadProgs
@@ -1126,23 +1179,34 @@ PR_LoadProgs
 */
 void PR_LoadProgs (void)
 {
 */
 void PR_LoadProgs (void)
 {
-       int             i;
+       int i;
        dstatement_t *st;
        dstatement_t *st;
+       ddef_t *infielddefs;
+       void *temp;
 
 // flush the non-C variable lookup cache
        for (i=0 ; i<GEFV_CACHESIZE ; i++)
                gefvCache[i].field[0] = 0;
 
 
 // flush the non-C variable lookup cache
        for (i=0 ; i<GEFV_CACHESIZE ; i++)
                gefvCache[i].field[0] = 0;
 
-       progs = (dprograms_t *)COM_LoadHunkFile ("progs.dat", false);
-       if (!progs)
+       Mem_EmptyPool(progs_mempool);
+       Mem_EmptyPool(edictstring_mempool);
+
+       temp = COM_LoadFile ("progs.dat", false);
+       if (!temp)
                Host_Error ("PR_LoadProgs: couldn't load progs.dat");
                Host_Error ("PR_LoadProgs: couldn't load progs.dat");
+
+       progs = (dprograms_t *)Mem_Alloc(progs_mempool, com_filesize);
+
+       memcpy(progs, temp, com_filesize);
+       Mem_Free(temp);
+
        Con_DPrintf ("Programs occupy %iK.\n", com_filesize/1024);
 
        pr_crc = CRC_Block((byte *)progs, com_filesize);
 
 // byte swap the header
        for (i=0 ; i<sizeof(*progs)/4 ; i++)
        Con_DPrintf ("Programs occupy %iK.\n", com_filesize/1024);
 
        pr_crc = CRC_Block((byte *)progs, com_filesize);
 
 // byte swap the header
        for (i=0 ; i<sizeof(*progs)/4 ; i++)
-               ((int *)progs)[i] = LittleLong ( ((int *)progs)[i] );           
+               ((int *)progs)[i] = LittleLong ( ((int *)progs)[i] );
 
        if (progs->version != PROG_VERSION)
                Host_Error ("progs.dat has wrong version number (%i should be %i)", progs->version, PROG_VERSION);
 
        if (progs->version != PROG_VERSION)
                Host_Error ("progs.dat has wrong version number (%i should be %i)", progs->version, PROG_VERSION);
@@ -1152,15 +1216,18 @@ void PR_LoadProgs (void)
        pr_functions = (dfunction_t *)((byte *)progs + progs->ofs_functions);
        pr_strings = (char *)progs + progs->ofs_strings;
        pr_globaldefs = (ddef_t *)((byte *)progs + progs->ofs_globaldefs);
        pr_functions = (dfunction_t *)((byte *)progs + progs->ofs_functions);
        pr_strings = (char *)progs + progs->ofs_strings;
        pr_globaldefs = (ddef_t *)((byte *)progs + progs->ofs_globaldefs);
-       pr_fielddefs = (ddef_t *)((byte *)progs + progs->ofs_fielddefs);
+
+       // we need to expand the fielddefs list to include all the engine fields,
+       // so allocate a new place for it
+       infielddefs = (ddef_t *)((byte *)progs + progs->ofs_fielddefs);
+       pr_fielddefs = Mem_Alloc(progs_mempool, (progs->numfielddefs + DPFIELDS) * sizeof(ddef_t));
+
        pr_statements = (dstatement_t *)((byte *)progs + progs->ofs_statements);
 
        pr_statements = (dstatement_t *)((byte *)progs + progs->ofs_statements);
 
+       // moved edict_size calculation down below field adding code
+
        pr_global_struct = (globalvars_t *)((byte *)progs + progs->ofs_globals);
        pr_globals = (float *)pr_global_struct;
        pr_global_struct = (globalvars_t *)((byte *)progs + progs->ofs_globals);
        pr_globals = (float *)pr_global_struct;
-       
-       pr_edict_size = progs->entityfields * 4 + sizeof (edict_t) - sizeof(entvars_t);
-
-       pr_edictareasize = pr_edict_size * MAX_EDICTS;
 
 // byte swap the lumps
        for (i=0 ; i<progs->numstatements ; i++)
 
 // byte swap the lumps
        for (i=0 ; i<progs->numstatements ; i++)
@@ -1179,7 +1246,7 @@ void PR_LoadProgs (void)
                pr_functions[i].s_file = LittleLong (pr_functions[i].s_file);
                pr_functions[i].numparms = LittleLong (pr_functions[i].numparms);
                pr_functions[i].locals = LittleLong (pr_functions[i].locals);
                pr_functions[i].s_file = LittleLong (pr_functions[i].s_file);
                pr_functions[i].numparms = LittleLong (pr_functions[i].numparms);
                pr_functions[i].locals = LittleLong (pr_functions[i].locals);
-       }       
+       }
 
        for (i=0 ; i<progs->numglobaldefs ; i++)
        {
 
        for (i=0 ; i<progs->numglobaldefs ; i++)
        {
@@ -1188,18 +1255,37 @@ void PR_LoadProgs (void)
                pr_globaldefs[i].s_name = LittleLong (pr_globaldefs[i].s_name);
        }
 
                pr_globaldefs[i].s_name = LittleLong (pr_globaldefs[i].s_name);
        }
 
-       for (i=0 ; i<progs->numfielddefs ; i++)
+       // copy the progs fields to the new fields list
+       for (i = 0;i < progs->numfielddefs;i++)
        {
        {
-               pr_fielddefs[i].type = LittleShort (pr_fielddefs[i].type);
+               pr_fielddefs[i].type = LittleShort (infielddefs[i].type);
                if (pr_fielddefs[i].type & DEF_SAVEGLOBAL)
                        Host_Error ("PR_LoadProgs: pr_fielddefs[i].type & DEF_SAVEGLOBAL");
                if (pr_fielddefs[i].type & DEF_SAVEGLOBAL)
                        Host_Error ("PR_LoadProgs: pr_fielddefs[i].type & DEF_SAVEGLOBAL");
-               pr_fielddefs[i].ofs = LittleShort (pr_fielddefs[i].ofs);
-               pr_fielddefs[i].s_name = LittleLong (pr_fielddefs[i].s_name);
+               pr_fielddefs[i].ofs = LittleShort (infielddefs[i].ofs);
+               pr_fielddefs[i].s_name = LittleLong (infielddefs[i].s_name);
+       }
+
+       // append the darkplaces fields
+       for (i = 0;i < DPFIELDS;i++)
+       {
+               pr_fielddefs[progs->numfielddefs].type = dpfields[i].type;
+               pr_fielddefs[progs->numfielddefs].ofs = progs->entityfields;
+               pr_fielddefs[progs->numfielddefs].s_name = dpfields[i].string - pr_strings;
+               if (pr_fielddefs[progs->numfielddefs].type == ev_vector)
+                       progs->entityfields += 3;
+               else
+                       progs->entityfields++;
+               progs->numfielddefs++;
        }
 
        for (i=0 ; i<progs->numglobals ; i++)
                ((int *)pr_globals)[i] = LittleLong (((int *)pr_globals)[i]);
 
        }
 
        for (i=0 ; i<progs->numglobals ; i++)
                ((int *)pr_globals)[i] = LittleLong (((int *)pr_globals)[i]);
 
+       // moved edict_size calculation down here, below field adding code
+       pr_edict_size = progs->entityfields * 4 + sizeof (edict_t) - sizeof(entvars_t);
+
+       pr_edictareasize = pr_edict_size * MAX_EDICTS;
+
        // LordHavoc: bounds check anything static
        for (i = 0,st = pr_statements;i < progs->numstatements;i++,st++)
        {
        // LordHavoc: bounds check anything static
        for (i = 0,st = pr_statements;i < progs->numstatements;i++,st++)
        {
@@ -1357,7 +1443,7 @@ void PR_Init (void)
        // LordHavoc: for DarkPlaces, this overrides the number of decors (shell casings, gibs, etc)
        Cvar_RegisterVariable (&decors);
        // LordHavoc: Nehahra uses these to pass data around cutscene demos
        // LordHavoc: for DarkPlaces, this overrides the number of decors (shell casings, gibs, etc)
        Cvar_RegisterVariable (&decors);
        // LordHavoc: Nehahra uses these to pass data around cutscene demos
-       if (nehahra)
+       if (gamemode == GAME_NEHAHRA)
        {
                Cvar_RegisterVariable (&nehx00);Cvar_RegisterVariable (&nehx01);
                Cvar_RegisterVariable (&nehx02);Cvar_RegisterVariable (&nehx03);
        {
                Cvar_RegisterVariable (&nehx00);Cvar_RegisterVariable (&nehx01);
                Cvar_RegisterVariable (&nehx02);Cvar_RegisterVariable (&nehx03);
@@ -1373,6 +1459,9 @@ void PR_Init (void)
        Cvar_RegisterVariable (&cutscene); // for Nehahra but useful to other mods as well
        // LordHavoc: optional runtime bounds checking (speed drain, but worth it for security, on by default - breaks most QCCX features (used by CRMod and others))
        Cvar_RegisterVariable (&pr_boundscheck);
        Cvar_RegisterVariable (&cutscene); // for Nehahra but useful to other mods as well
        // LordHavoc: optional runtime bounds checking (speed drain, but worth it for security, on by default - breaks most QCCX features (used by CRMod and others))
        Cvar_RegisterVariable (&pr_boundscheck);
+
+       progs_mempool = Mem_AllocPool("progs.dat");
+       edictstring_mempool = Mem_AllocPool("edict strings");
 }
 
 // LordHavoc: turned EDICT_NUM into a #define for speed reasons
 }
 
 // LordHavoc: turned EDICT_NUM into a #define for speed reasons
@@ -1393,11 +1482,20 @@ edict_t *EDICT_NUM(int n)
 int NUM_FOR_EDICT(edict_t *e)
 {
        int             b;
 int NUM_FOR_EDICT(edict_t *e)
 {
        int             b;
-       
+
        b = (byte *)e - (byte *)sv.edicts;
        b = b / pr_edict_size;
        b = (byte *)e - (byte *)sv.edicts;
        b = b / pr_edict_size;
-       
+
        if (b < 0 || b >= sv.num_edicts)
                Host_Error ("NUM_FOR_EDICT: bad pointer");
        return b;
 }
        if (b < 0 || b >= sv.num_edicts)
                Host_Error ("NUM_FOR_EDICT: bad pointer");
        return b;
 }
+
+int NoCrash_NUM_FOR_EDICT(edict_t *e)
+{
+       int             b;
+
+       b = (byte *)e - (byte *)sv.edicts;
+       b = b / pr_edict_size;
+       return b;
+}