X-Git-Url: http://de.git.xonotic.org/?p=xonotic%2Fdarkplaces.git;a=blobdiff_plain;f=pr_edict.c;h=750c26c23d82e4829600b262508ecb70fd0e0494;hp=dc4166ad8647e50406ff86cb71a499a1c6186532;hb=af0b7a1fa11c2ceedeb9f66ca950f6c0bff8ad8f;hpb=d57be67cb00229acb8564b92c8b7c58eeed8a0cb diff --git a/pr_edict.c b/pr_edict.c index dc4166ad..750c26c2 100644 --- a/pr_edict.c +++ b/pr_edict.c @@ -22,7 +22,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "quakedef.h" dprograms_t *progs; -dfunction_t *pr_functions; +mfunction_t *pr_functions; char *pr_strings; ddef_t *pr_fielddefs; ddef_t *pr_globaldefs; @@ -39,8 +39,8 @@ mempool_t *edictstring_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, 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 nomonsters = {0, "nomonsters", "0"}; @@ -68,6 +68,8 @@ cvar_t nehx18 = {0, "nehx18", "0"};cvar_t nehx19 = {0, "nehx19", "0"}; cvar_t cutscene = {0, "cutscene", "1"}; // 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_t pr_boundscheck = {0, "pr_boundscheck", "1"}; +// LordHavoc: prints every opcode as it executes - warning: this is significant spew +cvar_t pr_traceqc = {0, "pr_traceqc", "0"}; #define MAX_FIELD_LEN 64 #define GEFV_CACHESIZE 2 @@ -79,8 +81,8 @@ typedef struct { static gefv_cache gefvCache[GEFV_CACHESIZE] = {{NULL, ""}, {NULL, ""}}; -ddef_t *ED_FindField (char *name); -dfunction_t *ED_FindFunction (char *name); +ddef_t *ED_FindField (const char *name); +mfunction_t *ED_FindFunction (const char *name); // LordHavoc: in an effort to eliminate time wasted on GetEdictFieldValue... these are defined as externs in progs.h int eval_gravity; @@ -116,11 +118,17 @@ int eval_ping; int eval_movement; int eval_pmodel; int eval_punchvector; +int eval_viewzoom; +int eval_clientcolors; +int eval_tag_entity; +int eval_tag_index; -dfunction_t *SV_PlayerPhysicsQC; -dfunction_t *EndFrameQC; +mfunction_t *SV_PlayerPhysicsQC; +mfunction_t *EndFrameQC; +//KrimZon - SERVER COMMANDS IN QUAKEC +mfunction_t *SV_ParseClientCommandQC; -int FindFieldOffset(char *field) +int FindFieldOffset(const char *field) { ddef_t *d; d = ED_FindField(field); @@ -164,12 +172,18 @@ void FindEdictFieldOffsets(void) eval_movement = FindFieldOffset("movement"); eval_pmodel = FindFieldOffset("pmodel"); eval_punchvector = FindFieldOffset("punchvector"); + eval_viewzoom = FindFieldOffset("viewzoom"); + eval_clientcolors = FindFieldOffset("clientcolors"); + eval_tag_entity = FindFieldOffset("tag_entity"); + eval_tag_index = FindFieldOffset("tag_index"); // LordHavoc: allowing QuakeC to override the player movement code SV_PlayerPhysicsQC = ED_FindFunction ("SV_PlayerPhysics"); // LordHavoc: support for endframe EndFrameQC = ED_FindFunction ("EndFrame"); -}; + //KrimZon - SERVER COMMANDS IN QUAKEC + SV_ParseClientCommandQC = ED_FindFunction ("SV_ParseClientCommand"); +} /* ================= @@ -180,8 +194,17 @@ Sets everything to NULL */ void ED_ClearEdict (edict_t *e) { - memset (&e->v, 0, progs->entityfields * 4); - e->free = false; + int num; + memset (e->v, 0, progs->entityfields * 4); + e->e->free = false; + // LordHavoc: for consistency set these here + num = NUM_FOR_EDICT(e) - 1; + if (num >= 0 && num < svs.maxclients) + { + e->v->colormap = num + 1; + e->v->team = (svs.clients[num].colors & 15) + 1; + e->v->netname = PR_SetString(svs.clients[num].name); + } } /* @@ -200,22 +223,24 @@ edict_t *ED_Alloc (void) int i; edict_t *e; - for ( i=svs.maxclients+1 ; ifree && ( e->freetime < 2 || sv.time - e->freetime > 0.5 ) ) + if (e->e->free && ( e->e->freetime < 2 || sv.time - e->e->freetime > 0.5 ) ) { ED_ClearEdict (e); return e; } } - + if (i == MAX_EDICTS) Host_Error ("ED_Alloc: no free edicts"); - + sv.num_edicts++; + if (sv.num_edicts >= sv.max_edicts) + SV_IncreaseEdicts(); e = EDICT_NUM(i); ED_ClearEdict (e); @@ -234,19 +259,19 @@ void ED_Free (edict_t *ed) { SV_UnlinkEdict (ed); // unlink from world bsp - ed->free = true; - ed->v.model = 0; - ed->v.takedamage = 0; - ed->v.modelindex = 0; - ed->v.colormap = 0; - ed->v.skin = 0; - ed->v.frame = 0; - VectorClear(ed->v.origin); - VectorClear(ed->v.angles); - ed->v.nextthink = -1; - ed->v.solid = 0; - - ed->freetime = sv.time; + ed->e->free = true; + ed->v->model = 0; + ed->v->takedamage = 0; + ed->v->modelindex = 0; + ed->v->colormap = 0; + ed->v->skin = 0; + ed->v->frame = 0; + VectorClear(ed->v->origin); + VectorClear(ed->v->angles); + ed->v->nextthink = -1; + ed->v->solid = 0; + + ed->e->freetime = sv.time; } //=========================================================================== @@ -260,7 +285,7 @@ ddef_t *ED_GlobalAtOfs (int ofs) { ddef_t *def; int i; - + for (i=0 ; inumglobaldefs ; i++) { def = &pr_globaldefs[i]; @@ -279,7 +304,7 @@ ddef_t *ED_FieldAtOfs (int ofs) { ddef_t *def; int i; - + for (i=0 ; inumfielddefs ; i++) { def = &pr_fielddefs[i]; @@ -294,15 +319,15 @@ ddef_t *ED_FieldAtOfs (int ofs) ED_FindField ============ */ -ddef_t *ED_FindField (char *name) +ddef_t *ED_FindField (const char *name) { - ddef_t *def; - int i; + ddef_t *def; + int i; for (i=0 ; inumfielddefs ; i++) { def = &pr_fielddefs[i]; - if (!strcmp(pr_strings + def->s_name,name) ) + if (!strcmp(PR_GetString(def->s_name), name)) return def; } return NULL; @@ -313,15 +338,15 @@ ddef_t *ED_FindField (char *name) ED_FindGlobal ============ */ -ddef_t *ED_FindGlobal (char *name) +ddef_t *ED_FindGlobal (const char *name) { - ddef_t *def; - int i; - + ddef_t *def; + int i; + for (i=0 ; inumglobaldefs ; i++) { def = &pr_globaldefs[i]; - if (!strcmp(pr_strings + def->s_name,name) ) + if (!strcmp(PR_GetString(def->s_name), name)) return def; } return NULL; @@ -333,54 +358,21 @@ ddef_t *ED_FindGlobal (char *name) ED_FindFunction ============ */ -dfunction_t *ED_FindFunction (char *name) +mfunction_t *ED_FindFunction (const char *name) { - dfunction_t *func; + mfunction_t *func; int i; - + for (i=0 ; inumfunctions ; i++) { func = &pr_functions[i]; - if (!strcmp(pr_strings + func->s_name,name) ) + if (!strcmp(PR_GetString(func->s_name), name)) return func; } return NULL; } -/* -eval_t *GetEdictFieldValue(edict_t *ed, char *field) -{ - ddef_t *def = NULL; - int i; - static int rep = 0; - - for (i=0 ; iv + def->ofs*4); -} -*/ - /* ============ PR_ValueString @@ -393,7 +385,7 @@ 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; + mfunction_t *f; int n; type &= ~DEF_SAVEGLOBAL; @@ -401,10 +393,11 @@ char *PR_ValueString (etype_t type, eval_t *val) switch (type) { case ev_string: - sprintf (line, "%s", pr_strings + val->string); + sprintf (line, "%s", PR_GetString(val->string)); break; case ev_entity: - n = NoCrash_NUM_FOR_EDICT(PROG_TO_EDICT(val->edict)); + //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); else @@ -412,11 +405,11 @@ char *PR_ValueString (etype_t type, eval_t *val) break; case ev_function: f = pr_functions + val->function; - sprintf (line, "%s()", pr_strings + f->s_name); + sprintf (line, "%s()", PR_GetString(f->s_name)); break; case ev_field: def = ED_FieldAtOfs ( val->_int ); - sprintf (line, ".%s", pr_strings + def->s_name); + sprintf (line, ".%s", PR_GetString(def->s_name)); break; case ev_void: sprintf (line, "void"); @@ -436,7 +429,7 @@ char *PR_ValueString (etype_t type, eval_t *val) sprintf (line, "bad type %i", type); break; } - + return line; } @@ -450,27 +443,50 @@ Easier to parse than PR_ValueString */ char *PR_UglyValueString (etype_t type, eval_t *val) { - static char line[256]; - ddef_t *def; - dfunction_t *f; - + static char line[4096]; + int i; + char *s; + ddef_t *def; + mfunction_t *f; + type &= ~DEF_SAVEGLOBAL; switch (type) { case ev_string: - sprintf (line, "%s", pr_strings + val->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_strings + f->s_name); + sprintf (line, "%s", PR_GetString(f->s_name)); break; case ev_field: def = ED_FieldAtOfs ( val->_int ); - sprintf (line, "%s", pr_strings + def->s_name); + // LordHavoc: 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;) + { + if (*s == '\n') + { + line[i++] = '\\'; + line[i++] = 'n'; + } + else if (*s == '\r') + { + line[i++] = '\\'; + line[i++] = 'r'; + } + else + line[i] = *s; + s++; + } + line[i++] = 0; break; case ev_void: sprintf (line, "void"); @@ -485,7 +501,7 @@ char *PR_UglyValueString (etype_t type, eval_t *val) sprintf (line, "bad type %i", type); break; } - + return line; } @@ -508,17 +524,17 @@ char *PR_GlobalString (int ofs) val = (void *)&pr_globals[ofs]; def = ED_GlobalAtOfs(ofs); if (!def) - sprintf (line,"%i(\?\?\?)", ofs); // LordHavoc: escaping the ?s so it is not a trigraph + snprintf (line, sizeof (line), "%i(?)", ofs); else { s = PR_ValueString (def->type, val); - sprintf (line,"%i(%s)%s", ofs, pr_strings + def->s_name, s); + snprintf (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; } @@ -531,14 +547,14 @@ char *PR_GlobalStringNoContents (int ofs) def = ED_GlobalAtOfs(ofs); if (!def) - sprintf (line,"%i(\?\?\?)", ofs); // LordHavoc: escaping the ?s so it is not a trigraph + snprintf (line, sizeof (line), "%i(?)", ofs); else - sprintf (line,"%i(%s)", ofs, pr_strings + def->s_name); + snprintf (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; } @@ -563,7 +579,7 @@ void ED_Print (edict_t *ed) int type; char tempstring[8192], tempstring2[260]; // temporary string buffers - if (ed->free) + if (ed->e->free) { Con_Printf ("FREE\n"); return; @@ -574,11 +590,11 @@ void ED_Print (edict_t *ed) for (i=1 ; inumfielddefs ; i++) { d = &pr_fielddefs[i]; - name = pr_strings + d->s_name; + name = PR_GetString(d->s_name); if (name[strlen(name)-2] == '_') continue; // skip _x, _y, _z vars - - v = (int *)((char *)&ed->v + d->ofs*4); + + v = (int *)((char *)ed->v + d->ofs*4); // if the value is still all 0, skip the field type = d->type & ~DEF_SAVEGLOBAL; @@ -596,7 +612,7 @@ void ED_Print (edict_t *ed) tempstring2[259] = 0; name = tempstring2; } - strcat(tempstring, name); + strlcat (tempstring, name, sizeof (tempstring)); for (l = strlen(name);l < 14;l++) strcat(tempstring, " "); strcat(tempstring, " "); @@ -609,8 +625,8 @@ void ED_Print (edict_t *ed) 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); @@ -628,7 +644,7 @@ ED_Write For savegames ============= */ -void ED_Write (QFile *f, edict_t *ed) +void ED_Write (qfile_t *f, edict_t *ed) { ddef_t *d; int *v; @@ -636,22 +652,22 @@ void ED_Write (QFile *f, edict_t *ed) char *name; int type; - Qprintf (f, "{\n"); + FS_Printf (f, "{\n"); - if (ed->free) + if (ed->e->free) { - Qprintf (f, "}\n"); + FS_Printf (f, "}\n"); return; } - + for (i=1 ; inumfielddefs ; i++) { d = &pr_fielddefs[i]; - name = pr_strings + d->s_name; + name = PR_GetString(d->s_name); if (name[strlen(name)-2] == '_') continue; // skip _x, _y, _z vars - v = (int *)((char *)&ed->v + d->ofs*4); + v = (int *)((char *)ed->v + d->ofs*4); // if the value is still all 0, skip the field type = d->type & ~DEF_SAVEGLOBAL; @@ -661,11 +677,11 @@ void ED_Write (QFile *f, edict_t *ed) if (j == type_size[type]) continue; - Qprintf (f,"\"%s\" ",name); - Qprintf (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)); } - Qprintf (f, "}\n"); + FS_Printf (f, "}\n"); } void ED_PrintNum (int ent) @@ -683,7 +699,7 @@ For debugging, prints all the entities in the current server void ED_PrintEdicts (void) { int i; - + Con_Printf ("%i entities\n", sv.num_edicts); for (i=0 ; i= sv.num_edicts) { @@ -726,14 +742,14 @@ void ED_Count (void) for (i=0 ; ifree) + if (ent->e->free) continue; active++; - if (ent->v.solid) + if (ent->v->solid) solid++; - if (ent->v.model) + if (ent->v->model) models++; - if (ent->v.movetype == MOVETYPE_STEP) + if (ent->v->movetype == MOVETYPE_STEP) step++; } @@ -759,14 +775,14 @@ FIXME: need to tag constants, doesn't really work ED_WriteGlobals ============= */ -void ED_WriteGlobals (QFile *f) +void ED_WriteGlobals (qfile_t *f) { ddef_t *def; int i; char *name; int type; - Qprintf (f,"{\n"); + FS_Printf (f,"{\n"); for (i=0 ; inumglobaldefs ; i++) { def = &pr_globaldefs[i]; @@ -778,11 +794,11 @@ void ED_WriteGlobals (QFile *f) if (type != ev_string && type != ev_float && type != ev_entity) continue; - name = pr_strings + def->s_name; - Qprintf (f,"\"%s\" ", name); - Qprintf (f,"\"%s\"\n", PR_UglyValueString(type, (eval_t *)&pr_globals[def->ofs])); + 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])); } - Qprintf (f,"}\n"); + FS_Printf (f,"}\n"); } /* @@ -790,25 +806,23 @@ void ED_WriteGlobals (QFile *f) ED_ParseGlobals ============= */ -void ED_ParseGlobals (char *data) +void ED_ParseGlobals (const char *data) { - char keyname[1024]; // LordHavoc: good idea? bad idea? was 64 - ddef_t *key; + char keyname[1024]; // LordHavoc: good idea? bad idea? was 64 + ddef_t *key; while (1) - { - // parse key - data = COM_Parse (data); + { + // parse key + if (!COM_ParseToken(&data, false)) + Host_Error ("ED_ParseEntity: EOF without closing brace"); if (com_token[0] == '}') break; - if (!data) - Host_Error ("ED_ParseEntity: EOF without closing brace"); strcpy (keyname, com_token); - // parse value - data = COM_Parse (data); - if (!data) + // parse value + if (!COM_ParseToken(&data, false)) Host_Error ("ED_ParseEntity: EOF without closing brace"); if (com_token[0] == '}') @@ -821,7 +835,7 @@ void ED_ParseGlobals (char *data) continue; } - if (!ED_ParseEpair ((void *)pr_globals, key, com_token)) + if (!ED_ParseEpair(NULL, key, com_token)) Host_Error ("ED_ParseGlobals: parse error"); } } @@ -834,10 +848,10 @@ void ED_ParseGlobals (char *data) ED_NewString ============= */ -char *ED_NewString (char *string) +char *ED_NewString (const char *string) { - char *new, *new_p; - int i,l; + char *new, *new_p; + int i,l; l = strlen(string) + 1; new = Mem_Alloc(edictstring_mempool, l); @@ -869,69 +883,81 @@ Can parse either fields or globals returns false if error ============= */ -qboolean ED_ParseEpair (void *base, ddef_t *key, 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; - dfunction_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 = ED_NewString (s) - pr_strings; + 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) + break; + val->vector[i] = atof(s); + while (*s > ' ') + s++; + if (!*s) + break; } break; case ev_entity: - *(int *)d = EDICT_TO_PROG(EDICT_NUM(atoi (s))); + while (*s && *s <= ' ') + s++; + i = atoi(s); + if (i < 0 || 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(); + // 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); 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; } @@ -945,29 +971,28 @@ ed should be a properly initialized empty edict. Used for initial level load and for savegames. ==================== */ -char *ED_ParseEdict (char *data, edict_t *ent) +const char *ED_ParseEdict (const char *data, edict_t *ent) { - ddef_t *key; - qboolean anglehack; - qboolean init; - char keyname[256]; - int n; + ddef_t *key; + qboolean anglehack; + qboolean init; + char keyname[256]; + int n; init = false; // clear it if (ent != sv.edicts) // hack - memset (&ent->v, 0, progs->entityfields * 4); + memset (ent->v, 0, progs->entityfields * 4); // go through all the dictionary pairs while (1) { // parse key - data = COM_Parse (data); + if (!COM_ParseToken(&data, false)) + Host_Error ("ED_ParseEntity: EOF without closing brace"); 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...) @@ -994,8 +1019,7 @@ char *ED_ParseEdict (char *data, edict_t *ent) } // parse value - data = COM_Parse (data); - if (!data) + if (!COM_ParseToken(&data, false)) Host_Error ("ED_ParseEntity: EOF without closing brace"); if (com_token[0] == '}') @@ -1022,12 +1046,12 @@ char *ED_ParseEdict (char *data, edict_t *ent) sprintf (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"); } if (!init) - ent->free = true; + ent->e->free = true; return data; } @@ -1048,22 +1072,24 @@ Used for both fresh maps and savegame loads. A fresh map would also need to call ED_CallSpawnFunctions () to let the objects initialize themselves. ================ */ -void ED_LoadFromFile (char *data) -{ - edict_t *ent; - int inhibit; - dfunction_t *func; +void ED_LoadFromFile (const char *data) +{ + edict_t *ent; + int parsed, inhibited, spawned, died; + mfunction_t *func; ent = NULL; - inhibit = 0; + parsed = 0; + inhibited = 0; + spawned = 0; + died = 0; pr_global_struct->time = sv.time; - + // parse ents while (1) { -// parse the opening brace - data = COM_Parse (data); - if (!data) +// parse the opening brace + if (!COM_ParseToken(&data, false)) break; if (com_token[0] != '{') Host_Error ("ED_LoadFromFile: found %s when expecting {",com_token); @@ -1073,30 +1099,31 @@ void ED_LoadFromFile (char *data) else ent = ED_Alloc (); data = ED_ParseEdict (data, ent); + parsed++; // remove things from different skill levels or deathmatch if (deathmatch.integer) { - if (((int)ent->v.spawnflags & SPAWNFLAG_NOT_DEATHMATCH)) + if (((int)ent->v->spawnflags & SPAWNFLAG_NOT_DEATHMATCH)) { - ED_Free (ent); - inhibit++; + 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 ))) + 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); - inhibit++; + ED_Free (ent); + inhibited++; continue; } // // immediately call spawn function // - if (!ent->v.classname) + if (!ent->v->classname) { Con_Printf ("No classname for:\n"); ED_Print (ent); @@ -1105,7 +1132,7 @@ void ED_LoadFromFile (char *data) } // look for the spawn function - func = ED_FindFunction ( pr_strings + ent->v.classname ); + func = ED_FindFunction (PR_GetString(ent->v->classname)); if (!func) { @@ -1120,9 +1147,12 @@ void ED_LoadFromFile (char *data) pr_global_struct->self = EDICT_TO_PROG(ent); PR_ExecuteProgram (func - pr_functions, ""); + spawned++; + if (ent->e->free) + died++; } - Con_DPrintf ("%i entities inhibited\n", inhibit); + Con_DPrintf ("%i entities parsed, %i inhibited, %i spawned (%i removed self, %i stayed)\n", parsed, inhibited, spawned, died, spawned - died); } @@ -1169,7 +1199,10 @@ dpfield_t dpfields[] = {ev_float, "ping"}, {ev_vector, "movement"}, {ev_float, "pmodel"}, - {ev_vector, "punchvector"} + {ev_vector, "punchvector"}, + {ev_float, "clientcolors"}, + {ev_entity, "tag_entity"}, + {ev_float, "tag_index"} }; /* @@ -1177,12 +1210,14 @@ dpfield_t dpfields[] = PR_LoadProgs =============== */ +extern void PR_Cmd_Reset (void); void PR_LoadProgs (void) { int i; dstatement_t *st; ddef_t *infielddefs; void *temp; + dfunction_t *dfunctions; // flush the non-C variable lookup cache for (i=0 ; iversion != PROG_VERSION) @@ -1213,20 +1248,21 @@ void PR_LoadProgs (void) if (progs->crc != PROGHEADER_CRC) Host_Error ("progs.dat system vars have been modified, progdefs.h is out of date"); - pr_functions = (dfunction_t *)((byte *)progs + progs->ofs_functions); + //pr_functions = (dfunction_t *)((qbyte *)progs + progs->ofs_functions); + dfunctions = (dfunction_t *)((qbyte *)progs + progs->ofs_functions); pr_strings = (char *)progs + progs->ofs_strings; - pr_globaldefs = (ddef_t *)((byte *)progs + progs->ofs_globaldefs); + pr_globaldefs = (ddef_t *)((qbyte *)progs + progs->ofs_globaldefs); // 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); + infielddefs = (ddef_t *)((qbyte *)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 *)((qbyte *)progs + progs->ofs_statements); // moved edict_size calculation down below field adding code - pr_global_struct = (globalvars_t *)((byte *)progs + progs->ofs_globals); + pr_global_struct = (globalvars_t *)((qbyte *)progs + progs->ofs_globals); pr_globals = (float *)pr_global_struct; // byte swap the lumps @@ -1238,14 +1274,16 @@ void PR_LoadProgs (void) pr_statements[i].c = LittleShort(pr_statements[i].c); } - for (i=0 ; inumfunctions; i++) + pr_functions = Mem_Alloc(progs_mempool, sizeof(mfunction_t) * progs->numfunctions); + for (i = 0;i < progs->numfunctions;i++) { - pr_functions[i].first_statement = LittleLong (pr_functions[i].first_statement); - pr_functions[i].parm_start = LittleLong (pr_functions[i].parm_start); - pr_functions[i].s_name = LittleLong (pr_functions[i].s_name); - 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].first_statement = LittleLong (dfunctions[i].first_statement); + pr_functions[i].parm_start = LittleLong (dfunctions[i].parm_start); + pr_functions[i].s_name = LittleLong (dfunctions[i].s_name); + pr_functions[i].s_file = LittleLong (dfunctions[i].s_file); + pr_functions[i].numparms = LittleLong (dfunctions[i].numparms); + pr_functions[i].locals = LittleLong (dfunctions[i].locals); + memcpy(pr_functions[i].parm_size, dfunctions[i].parm_size, sizeof(dfunctions[i].parm_size)); } for (i=0 ; inumglobaldefs ; i++) @@ -1266,11 +1304,11 @@ void PR_LoadProgs (void) } // append the darkplaces fields - for (i = 0;i < DPFIELDS;i++) + for (i = 0;i < (int) 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; + pr_fielddefs[progs->numfielddefs].s_name = PR_SetString(dpfields[i].string); if (pr_fielddefs[progs->numfielddefs].type == ev_vector) progs->entityfields += 3; else @@ -1282,8 +1320,8 @@ void PR_LoadProgs (void) ((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); - + // LordHavoc: this no longer includes the edict_t header + pr_edict_size = progs->entityfields * 4; pr_edictareasize = pr_edict_size * MAX_EDICTS; // LordHavoc: bounds check anything static @@ -1386,20 +1424,114 @@ void PR_LoadProgs (void) } FindEdictFieldOffsets(); // LordHavoc: update field offset list + PR_Execute_ProgsLoaded(); + PR_Cmd_Reset(); } void PR_Fields_f (void) { - int i; + int i, j, ednum, used, usedamount; + int *counts; + char tempstring[5000], tempstring2[260], *name; + edict_t *ed; + ddef_t *d; + int *v; if (!sv.active) { Con_Printf("no progs loaded\n"); return; } + counts = Mem_Alloc(tempmempool, progs->numfielddefs * sizeof(int)); + for (ednum = 0;ednum < sv.max_edicts;ednum++) + { + ed = EDICT_NUM(ednum); + if (ed->e->free) + continue; + for (i = 1;i < progs->numfielddefs;i++) + { + d = &pr_fielddefs[i]; + name = PR_GetString(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 + for (j = 0;j < type_size[d->type & ~DEF_SAVEGLOBAL];j++) + { + if (v[j]) + { + counts[i]++; + break; + } + } + } + } + used = 0; + usedamount = 0; + tempstring[0] = 0; for (i = 0;i < progs->numfielddefs;i++) - Con_Printf("%s\n", (pr_strings + pr_fielddefs[i].s_name)); - Con_Printf("%i entity fields, totalling %i bytes per edict, %i edicts, %i bytes total spent on edict fields\n", progs->entityfields, progs->entityfields * 4, MAX_EDICTS, progs->entityfields * 4 * MAX_EDICTS); + { + d = &pr_fielddefs[i]; + name = PR_GetString(d->s_name); + if (name[strlen(name)-2] == '_') + continue; // skip _x, _y, _z vars + switch(d->type & ~DEF_SAVEGLOBAL) + { + case ev_string: + strlcat (tempstring, "string ", sizeof (tempstring)); + break; + case ev_entity: + strlcat (tempstring, "entity ", sizeof (tempstring)); + break; + case ev_function: + strlcat (tempstring, "function ", sizeof (tempstring)); + break; + case ev_field: + strlcat (tempstring, "field ", sizeof (tempstring)); + break; + case ev_void: + strlcat (tempstring, "void ", sizeof (tempstring)); + break; + case ev_float: + strlcat (tempstring, "float ", sizeof (tempstring)); + break; + case ev_vector: + strlcat (tempstring, "vector ", sizeof (tempstring)); + break; + case ev_pointer: + strlcat (tempstring, "pointer ", sizeof (tempstring)); + break; + default: + snprintf (tempstring2, sizeof (tempstring2), "bad type %i ", d->type & ~DEF_SAVEGLOBAL); + strlcat (tempstring, tempstring2, sizeof (tempstring)); + break; + } + if (strlen(name) > 256) + { + strncpy(tempstring2, name, 256); + tempstring2[256] = tempstring2[257] = tempstring2[258] = '.'; + tempstring2[259] = 0; + name = tempstring2; + } + strcat (tempstring, name); + for (j = strlen(name);j < 25;j++) + strcat(tempstring, " "); + snprintf (tempstring2, sizeof (tempstring2), "%5d", counts[i]); + strlcat (tempstring, tempstring2, sizeof (tempstring)); + strlcat (tempstring, "\n", sizeof (tempstring)); + if (strlen(tempstring) >= 4096) + { + Con_Printf("%s", tempstring); + tempstring[0] = 0; + } + if (counts[i]) + { + used++; + usedamount += type_size[d->type & ~DEF_SAVEGLOBAL]; + } + } + Mem_Free(counts); + 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) @@ -1411,7 +1543,7 @@ void PR_Globals_f (void) return; } for (i = 0;i < progs->numglobaldefs;i++) - Con_Printf("%s\n", (pr_strings + pr_globaldefs[i].s_name)); + Con_Printf("%s\n", PR_GetString(pr_globaldefs[i].s_name)); Con_Printf("%i global variables, totalling %i bytes\n", progs->numglobals, progs->numglobals * 4); } @@ -1420,6 +1552,7 @@ void PR_Globals_f (void) PR_Init =============== */ +extern void PR_Cmd_Init(void); void PR_Init (void) { Cmd_AddCommand ("edict", ED_PrintEdict_f); @@ -1459,43 +1592,59 @@ 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 (&pr_traceqc); progs_mempool = Mem_AllocPool("progs.dat"); edictstring_mempool = Mem_AllocPool("edict strings"); + + PR_Cmd_Init(); } // LordHavoc: turned EDICT_NUM into a #define for speed reasons -edict_t *EDICT_NUM_ERROR(int n) +edict_t *EDICT_NUM_ERROR(int n, char *filename, int fileline) { - Host_Error ("EDICT_NUM: bad number %i", n); + Host_Error ("EDICT_NUM: bad number %i (called at %s:%i)", n, filename, fileline); return NULL; } + /* -edict_t *EDICT_NUM(int n) +int NUM_FOR_EDICT_ERROR(edict_t *e) { - if (n < 0 || n >= sv.max_edicts) - Sys_Error ("EDICT_NUM: bad number %i", n); - return (edict_t *)((byte *)sv.edicts+ (n)*pr_edict_size); + Host_Error ("NUM_FOR_EDICT: bad pointer %p (world is %p, entity number would be %i)", e, sv.edicts, e - sv.edicts); + return 0; } -*/ int NUM_FOR_EDICT(edict_t *e) { - int b; - - b = (byte *)e - (byte *)sv.edicts; - b = b / pr_edict_size; - - if (b < 0 || b >= sv.num_edicts) + int n; + n = e - sv.edicts; + if ((unsigned int)n >= MAX_EDICTS) Host_Error ("NUM_FOR_EDICT: bad pointer"); - return b; + return n; } -int NoCrash_NUM_FOR_EDICT(edict_t *e) -{ - int b; +//int NoCrash_NUM_FOR_EDICT(edict_t *e) +//{ +// return e - sv.edicts; +//} - b = (byte *)e - (byte *)sv.edicts; - b = b / pr_edict_size; - return b; +//#define EDICT_TO_PROG(e) ((qbyte *)(((edict_t *)e)->v) - (qbyte *)(sv.edictsfields)) +//#define PROG_TO_EDICT(e) (sv.edicts + ((e) / (progs->entityfields * 4))) +int EDICT_TO_PROG(edict_t *e) +{ + int n; + n = e - sv.edicts; + if ((unsigned int)n >= (unsigned int)sv.max_edicts) + Host_Error("EDICT_TO_PROG: invalid edict %8p (number %i compared to world at %8p)\n", e, n, sv.edicts); + return n;// EXPERIMENTAL + //return (qbyte *)e->v - (qbyte *)sv.edictsfields; } +edict_t *PROG_TO_EDICT(int n) +{ + if ((unsigned int)n >= (unsigned int)sv.max_edicts) + Host_Error("PROG_TO_EDICT: invalid edict number %i\n", n); + return sv.edicts + n; // EXPERIMENTAL + //return sv.edicts + ((n) / (progs->entityfields * 4)); +} +*/ +