pr_comp.h, pr_edict.c, progs.h, pr_cmds.c, pr_exec.c: made some of the

variables and functions private and did some whitespace tidy-ups.

git-svn-id: svn+ssh://svn.code.sf.net/p/quakespasm/code/trunk@504 af15c1b1-3010-417e-b628-4374ebc0bcbd
This commit is contained in:
sezero 2011-12-12 08:56:25 +00:00
parent dd15b1a5bf
commit 5291960d56
5 changed files with 598 additions and 579 deletions

File diff suppressed because it is too large Load diff

View file

@ -1,6 +1,5 @@
/*
Copyright (C) 1996-2001 Id Software, Inc.
Copyright (C) 2002-2009 John Fitzgibbons and others
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
@ -27,12 +26,22 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
typedef int func_t;
typedef int string_t;
typedef enum {ev_void, ev_string, ev_float, ev_vector, ev_entity, ev_field, ev_function, ev_pointer} etype_t;
typedef enum
{
ev_bad = -1,
ev_void = 0,
ev_string,
ev_float,
ev_vector,
ev_entity,
ev_field,
ev_function,
ev_pointer
} etype_t;
#define OFS_NULL 0
#define OFS_RETURN 1
#define OFS_PARM0 4 // leave 3 ofs for each parm to hold vectors
#define OFS_PARM0 4 // leave 3 ofs for each parm to hold vectors
#define OFS_PARM1 7
#define OFS_PARM2 10
#define OFS_PARM3 13
@ -43,7 +52,8 @@ typedef enum {ev_void, ev_string, ev_float, ev_vector, ev_entity, ev_field, ev_f
#define RESERVED_OFS 28
enum {
enum
{
OP_DONE,
OP_MUL_F,
OP_MUL_V,
@ -121,20 +131,20 @@ enum {
OP_BITOR
};
typedef struct statement_s
{
unsigned short op;
short a,b,c;
short a, b, c;
} dstatement_t;
typedef struct
{
unsigned short type; // if DEF_SAVEGLOBAL bit is set
// the variable needs to be saved in savegames
unsigned short type; // if DEF_SAVEGLOBAL bit is set
// the variable needs to be saved in savegames
unsigned short ofs;
int s_name;
int s_name;
} ddef_t;
#define DEF_SAVEGLOBAL (1<<15)
#define MAX_PARMS 8
@ -143,7 +153,7 @@ typedef struct
{
int first_statement; // negative numbers are builtins
int parm_start;
int locals; // total ints of parms + locals
int locals; // total ints of parms + locals
int profile; // runtime
@ -159,7 +169,7 @@ typedef struct
typedef struct
{
int version;
int crc; // check of header file
int crc; // check of header file
int ofs_statements;
int numstatements; // statement 0 is an error
@ -174,7 +184,7 @@ typedef struct
int numfunctions; // function 0 is an empty
int ofs_strings;
int numstrings; // first string is a null string
int numstrings; // first string is a null string
int ofs_globals;
int numglobals;

View file

@ -22,24 +22,25 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "quakedef.h"
qboolean pr_alpha_supported; //johnfitz
dprograms_t *progs;
dfunction_t *pr_functions;
static char *pr_strings;
static int pr_stringssize;
static const char **pr_knownstrings;
static int pr_maxknownstrings;
static int pr_numknownstrings;
static ddef_t *pr_fielddefs;
static ddef_t *pr_globaldefs;
qboolean pr_alpha_supported; //johnfitz
dstatement_t *pr_statements;
globalvars_t *pr_global_struct;
float *pr_globals; // same as pr_global_struct
int pr_edict_size; // in bytes
float *pr_globals; // same as pr_global_struct
int pr_edict_size; // in bytes
static char *pr_strings;
static int pr_stringssize;
static const char **pr_knownstrings;
static int pr_maxknownstrings;
static int pr_numknownstrings;
static ddef_t *pr_fielddefs;
static ddef_t *pr_globaldefs;
unsigned short pr_crc;
unsigned short pr_crc;
int type_size[8] = {
1, // ev_void
@ -52,8 +53,22 @@ int type_size[8] = {
1 // sizeof(void *) / 4 // ev_pointer
};
ddef_t *ED_FieldAtOfs (int ofs);
qboolean ED_ParseEpair (void *base, ddef_t *key, const char *s);
static ddef_t *ED_FieldAtOfs (int ofs);
static qboolean ED_ParseEpair (void *base, ddef_t *key, const char *s);
#define MAX_FIELD_LEN 64
#define GEFV_CACHESIZE 2
typedef struct {
ddef_t *pcache;
char field[MAX_FIELD_LEN];
} gefv_cache;
static gefv_cache gefvCache[GEFV_CACHESIZE] =
{
{ NULL, "" },
{ NULL, "" }
};
cvar_t nomonsters = {"nomonsters", "0"};
cvar_t gamecfg = {"gamecfg", "0"};
@ -67,16 +82,6 @@ cvar_t saved2 = {"saved2", "0", true};
cvar_t saved3 = {"saved3", "0", true};
cvar_t saved4 = {"saved4", "0", true};
#define MAX_FIELD_LEN 64
#define GEFV_CACHESIZE 2
typedef struct {
ddef_t *pcache;
char field[MAX_FIELD_LEN];
} gefv_cache;
static gefv_cache gefvCache[GEFV_CACHESIZE] = {{NULL, ""}, {NULL, ""}};
/*
=================
ED_ClearEdict
@ -106,7 +111,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
@ -163,12 +168,12 @@ void ED_Free (edict_t *ed)
ED_GlobalAtOfs
============
*/
ddef_t *ED_GlobalAtOfs (int ofs)
static ddef_t *ED_GlobalAtOfs (int ofs)
{
ddef_t *def;
int i;
for (i=0 ; i<progs->numglobaldefs ; i++)
for (i = 0; i < progs->numglobaldefs; i++)
{
def = &pr_globaldefs[i];
if (def->ofs == ofs)
@ -182,12 +187,12 @@ ddef_t *ED_GlobalAtOfs (int ofs)
ED_FieldAtOfs
============
*/
ddef_t *ED_FieldAtOfs (int ofs)
static ddef_t *ED_FieldAtOfs (int ofs)
{
ddef_t *def;
int i;
for (i=0 ; i<progs->numfielddefs ; i++)
for (i = 0; i < progs->numfielddefs; i++)
{
def = &pr_fielddefs[i];
if (def->ofs == ofs)
@ -201,15 +206,15 @@ ddef_t *ED_FieldAtOfs (int ofs)
ED_FindField
============
*/
ddef_t *ED_FindField (const char *name)
static ddef_t *ED_FindField (const char *name)
{
ddef_t *def;
int i;
for (i=0 ; i<progs->numfielddefs ; i++)
for (i = 0; i < progs->numfielddefs; i++)
{
def = &pr_fielddefs[i];
if (!strcmp(PR_GetString(def->s_name),name))
if ( !strcmp(PR_GetString(def->s_name), name) )
return def;
}
return NULL;
@ -221,15 +226,15 @@ ddef_t *ED_FindField (const char *name)
ED_FindGlobal
============
*/
ddef_t *ED_FindGlobal (const char *name)
static ddef_t *ED_FindGlobal (const char *name)
{
ddef_t *def;
int i;
for (i=0 ; i<progs->numglobaldefs ; i++)
for (i = 0; i < progs->numglobaldefs; i++)
{
def = &pr_globaldefs[i];
if (!strcmp(PR_GetString(def->s_name),name))
if ( !strcmp(PR_GetString(def->s_name), name) )
return def;
}
return NULL;
@ -241,15 +246,15 @@ ddef_t *ED_FindGlobal (const char *name)
ED_FindFunction
============
*/
dfunction_t *ED_FindFunction (const char *name)
static dfunction_t *ED_FindFunction (const char *fn_name)
{
dfunction_t *func;
int i;
for (i=0 ; i<progs->numfunctions ; i++)
for (i = 0; i < progs->numfunctions; i++)
{
func = &pr_functions[i];
if (!strcmp(PR_GetString(func->s_name),name))
if ( !strcmp(PR_GetString(func->s_name), fn_name) )
return func;
}
return NULL;
@ -266,7 +271,7 @@ eval_t *GetEdictFieldValue(edict_t *ed, const char *field)
int i;
static int rep = 0;
for (i=0 ; i<GEFV_CACHESIZE ; i++)
for (i = 0; i < GEFV_CACHESIZE; i++)
{
if (!strcmp(field, gefvCache[i].field))
{
@ -300,7 +305,7 @@ PR_ValueString
Returns a string describing *data in a type specific manner
=============
*/
const char *PR_ValueString (int type, eval_t *val)
static const char *PR_ValueString (int type, eval_t *val)
{
static char line[256];
ddef_t *def;
@ -353,7 +358,7 @@ Returns a string describing *data in a type specific manner
Easier to parse than PR_ValueString
=============
*/
const char *PR_UglyValueString (int type, eval_t *val)
static const char *PR_UglyValueString (int type, eval_t *val)
{
static char line[256];
ddef_t *def;
@ -406,14 +411,14 @@ const char *PR_GlobalString (int ofs)
{
const char *s;
int i;
ddef_t *def;
void *val;
ddef_t *def;
void *val;
static char line[128];
val = (void *)&pr_globals[ofs];
def = ED_GlobalAtOfs(ofs);
if (!def)
sprintf (line,"%i(???)", ofs);
sprintf (line,"%i(?)", ofs);
else
{
s = PR_ValueString (def->type, (eval_t *)val);
@ -421,9 +426,9 @@ const char *PR_GlobalString (int ofs)
}
i = strlen(line);
for ( ; i<20 ; i++)
strcat (line," ");
strcat (line," ");
for ( ; i < 20; i++)
strcat (line, " ");
strcat (line, " ");
return line;
}
@ -431,19 +436,19 @@ const char *PR_GlobalString (int ofs)
const char *PR_GlobalStringNoContents (int ofs)
{
int i;
ddef_t *def;
ddef_t *def;
static char line[128];
def = ED_GlobalAtOfs(ofs);
if (!def)
sprintf (line,"%i(???)", ofs);
sprintf (line,"%i(?)", ofs);
else
sprintf (line,"%i(%s)", ofs, PR_GetString(def->s_name));
i = strlen(line);
for ( ; i<20 ; i++)
strcat (line," ");
strcat (line," ");
for ( ; i < 20; i++)
strcat (line, " ");
strcat (line, " ");
return line;
}
@ -471,11 +476,11 @@ void ED_Print (edict_t *ed)
}
Con_SafePrintf("\nEDICT %i:\n", NUM_FOR_EDICT(ed)); //johnfitz -- was Con_Printf
for (i=1 ; i<progs->numfielddefs ; i++)
for (i = 1; i < progs->numfielddefs; i++)
{
d = &pr_fielddefs[i];
name = PR_GetString(d->s_name);
l = strlen(name);
l = strlen (name);
if (l > 1 && name[l - 2] == '_')
continue; // skip _x, _y, _z vars
@ -484,13 +489,15 @@ void ED_Print (edict_t *ed)
// if the value is still all 0, skip the field
type = d->type & ~DEF_SAVEGLOBAL;
for (j=0 ; j<type_size[type] ; j++)
for (j = 0; j < type_size[type]; j++)
{
if (v[j])
break;
}
if (j == type_size[type])
continue;
Con_SafePrintf ("%s",name); //johnfitz -- was Con_Printf
Con_SafePrintf ("%s", name); //johnfitz -- was Con_Printf
while (l++ < 15)
Con_SafePrintf (" "); //johnfitz -- was Con_Printf
@ -521,11 +528,11 @@ void ED_Write (FILE *f, edict_t *ed)
return;
}
for (i=1 ; i<progs->numfielddefs ; i++)
for (i = 1; i < progs->numfielddefs; i++)
{
d = &pr_fielddefs[i];
name = PR_GetString(d->s_name);
j = strlen(name);
j = strlen (name);
if (j > 1 && name[j - 2] == '_')
continue; // skip _x, _y, _z vars
@ -533,19 +540,21 @@ void ED_Write (FILE *f, edict_t *ed)
// if the value is still all 0, skip the field
type = d->type & ~DEF_SAVEGLOBAL;
for (j=0 ; j<type_size[type] ; j++)
for (j = 0; j < type_size[type]; j++)
{
if (v[j])
break;
}
if (j == type_size[type])
continue;
fprintf (f,"\"%s\" ",name);
fprintf (f,"\"%s\"\n", PR_UglyValueString(d->type, (eval_t *)v));
fprintf (f, "\"%s\" ", name);
fprintf (f, "\"%s\"\n", PR_UglyValueString(d->type, (eval_t *)v));
}
//johnfitz -- save entity alpha manually when progs.dat doesn't know about alpha
if (!pr_alpha_supported && ed->alpha != ENTALPHA_DEFAULT)
fprintf (f,"\"alpha\" \"%f\"\n", ENTALPHA_TOSAVE(ed->alpha));
fprintf (f, "\"alpha\" \"%f\"\n", ENTALPHA_TOSAVE(ed->alpha));
//johnfitz
fprintf (f, "}\n");
@ -568,7 +577,7 @@ void ED_PrintEdicts (void)
int i;
Con_Printf ("%i entities\n", sv.num_edicts);
for (i=0 ; i<sv.num_edicts ; i++)
for (i = 0; i < sv.num_edicts; i++)
ED_PrintNum (i);
}
@ -579,7 +588,7 @@ ED_PrintEdict_f
For debugging, prints a single edicy
=============
*/
void ED_PrintEdict_f (void)
static void ED_PrintEdict_f (void)
{
int i;
@ -599,14 +608,14 @@ ED_Count
For debugging
=============
*/
void ED_Count (void)
static void ED_Count (void)
{
int i;
edict_t *ent;
int active, models, solid, step;
active = models = solid = step = 0;
for (i=0 ; i<sv.num_edicts ; i++)
for (i = 0; i < sv.num_edicts; i++)
{
ent = EDICT_NUM(i);
if (ent->free)
@ -625,13 +634,13 @@ void ED_Count (void)
Con_Printf ("view :%3i\n", models);
Con_Printf ("touch :%3i\n", solid);
Con_Printf ("step :%3i\n", step);
}
/*
==============================================================================
ARCHIVING GLOBALS
ARCHIVING GLOBALS
FIXME: need to tag constants, doesn't really work
==============================================================================
@ -649,8 +658,8 @@ void ED_WriteGlobals (FILE *f)
const char *name;
int type;
fprintf (f,"{\n");
for (i=0 ; i<progs->numglobaldefs ; i++)
fprintf (f, "{\n");
for (i = 0; i < progs->numglobaldefs; i++)
{
def = &pr_globaldefs[i];
type = def->type;
@ -662,10 +671,10 @@ void ED_WriteGlobals (FILE *f)
continue;
name = PR_GetString(def->s_name);
fprintf (f,"\"%s\" ", name);
fprintf (f,"\"%s\"\n", PR_UglyValueString(type, (eval_t *)&pr_globals[def->ofs]));
fprintf (f, "\"%s\" ", name);
fprintf (f, "\"%s\"\n", PR_UglyValueString(type, (eval_t *)&pr_globals[def->ofs]));
}
fprintf (f,"}\n");
fprintf (f, "}\n");
}
/*
@ -726,7 +735,7 @@ static string_t ED_NewString (const char *string)
l = strlen(string) + 1;
num = PR_AllocString (l, &new_p);
for (i=0 ; i< l ; i++)
for (i = 0; i < l; i++)
{
if (string[i] == '\\' && i < l-1)
{
@ -752,7 +761,7 @@ Can parse either fields or globals
returns false if error
=============
*/
qboolean ED_ParseEpair (void *base, ddef_t *key, const char *s)
static qboolean ED_ParseEpair (void *base, ddef_t *key, const char *s)
{
int i;
char string[128];
@ -766,7 +775,7 @@ qboolean ED_ParseEpair (void *base, ddef_t *key, const char *s)
switch (key->type & ~DEF_SAVEGLOBAL)
{
case ev_string:
*(string_t *)d = ED_NewString (s);
*(string_t *)d = ED_NewString(s);
break;
case ev_float:
@ -777,7 +786,7 @@ qboolean ED_ParseEpair (void *base, ddef_t *key, const char *s)
strcpy (string, s);
v = string;
w = string;
for (i=0 ; i<3 ; i++)
for (i = 0; i < 3; i++)
{
while (*v && *v != ' ')
v++;
@ -838,14 +847,14 @@ const char *ED_ParseEdict (const char *data, edict_t *ent)
init = false;
// clear it
// clear it
if (ent != sv.edicts) // hack
memset (&ent->v, 0, progs->entityfields * 4);
// go through all the dictionary pairs
// go through all the dictionary pairs
while (1)
{
// parse key
// parse key
data = COM_Parse (data);
if (com_token[0] == '}')
break;
@ -940,16 +949,16 @@ to call ED_CallSpawnFunctions () to let the objects initialize themselves.
*/
void ED_LoadFromFile (const char *data)
{
dfunction_t *func;
edict_t *ent = NULL;
int inhibit = 0;
dfunction_t *func;
pr_global_struct->time = sv.time;
// parse ents
// parse ents
while (1)
{
// parse the opening brace
// parse the opening brace
data = COM_Parse (data);
if (!data)
break;
@ -962,7 +971,7 @@ void ED_LoadFromFile (const char *data)
ent = ED_Alloc ();
data = ED_ParseEdict (data, ent);
// remove things from different skill levels or deathmatch
// remove things from different skill levels or deathmatch
if (deathmatch.value)
{
if (((int)ent->v.spawnflags & SPAWNFLAG_NOT_DEATHMATCH))
@ -993,7 +1002,7 @@ void ED_LoadFromFile (const char *data)
}
// look for the spawn function
func = ED_FindFunction (PR_GetString(ent->v.classname));
func = ED_FindFunction ( PR_GetString(ent->v.classname) );
if (!func)
{
@ -1018,10 +1027,10 @@ PR_LoadProgs
*/
void PR_LoadProgs (void)
{
int i;
int i;
// flush the non-C variable lookup cache
for (i=0 ; i<GEFV_CACHESIZE ; i++)
// flush the non-C variable lookup cache
for (i = 0; i < GEFV_CACHESIZE; i++)
gefvCache[i].field[0] = 0;
CRC_Init (&pr_crc);
@ -1031,11 +1040,11 @@ void PR_LoadProgs (void)
Sys_Error ("PR_LoadProgs: couldn't load progs.dat");
Con_DPrintf ("Programs occupy %iK.\n", com_filesize/1024);
for (i=0 ; i<com_filesize ; i++)
for (i = 0; i < com_filesize; i++)
CRC_ProcessByte (&pr_crc, ((byte *)progs)[i]);
// byte swap the header
for (i=0 ; i<sizeof(*progs)/4 ; i++)
// byte swap the header
for (i = 0; i < (int) sizeof(*progs) / 4; i++)
((int *)progs)[i] = LittleLong ( ((int *)progs)[i] );
if (progs->version != PROG_VERSION)
@ -1047,13 +1056,16 @@ void PR_LoadProgs (void)
pr_strings = (char *)progs + progs->ofs_strings;
if (progs->ofs_strings + progs->numstrings >= com_filesize)
Host_Error ("progs.dat strings go past end of file\n");
// initialize the strings
pr_numknownstrings = 0;
pr_maxknownstrings = 0;
pr_stringssize = progs->numstrings;
if (pr_knownstrings)
Z_Free ((void *)pr_knownstrings);
pr_knownstrings = NULL;
PR_SetEngineString(""); // initialize the strings
PR_SetEngineString("");
pr_globaldefs = (ddef_t *)((byte *)progs + progs->ofs_globaldefs);
pr_fielddefs = (ddef_t *)((byte *)progs + progs->ofs_fielddefs);
pr_statements = (dstatement_t *)((byte *)progs + progs->ofs_statements);
@ -1061,8 +1073,8 @@ void PR_LoadProgs (void)
pr_global_struct = (globalvars_t *)((byte *)progs + progs->ofs_globals);
pr_globals = (float *)pr_global_struct;
// byte swap the lumps
for (i=0 ; i<progs->numstatements ; i++)
// byte swap the lumps
for (i = 0; i < progs->numstatements; i++)
{
pr_statements[i].op = LittleShort(pr_statements[i].op);
pr_statements[i].a = LittleShort(pr_statements[i].a);
@ -1070,17 +1082,17 @@ void PR_LoadProgs (void)
pr_statements[i].c = LittleShort(pr_statements[i].c);
}
for (i=0 ; i<progs->numfunctions; i++)
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 (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);
}
for (i=0 ; i<progs->numglobaldefs ; i++)
for (i = 0; i < progs->numglobaldefs; i++)
{
pr_globaldefs[i].type = LittleShort (pr_globaldefs[i].type);
pr_globaldefs[i].ofs = LittleShort (pr_globaldefs[i].ofs);
@ -1089,7 +1101,7 @@ void PR_LoadProgs (void)
pr_alpha_supported = false; //johnfitz
for (i=0 ; i<progs->numfielddefs ; i++)
for (i = 0; i < progs->numfielddefs; i++)
{
pr_fielddefs[i].type = LittleShort (pr_fielddefs[i].type);
if (pr_fielddefs[i].type & DEF_SAVEGLOBAL)
@ -1103,7 +1115,7 @@ void PR_LoadProgs (void)
//johnfitz
}
for (i=0 ; i<progs->numglobals ; i++)
for (i = 0; i < progs->numglobals; i++)
((int *)pr_globals)[i] = LittleLong (((int *)pr_globals)[i]);
pr_edict_size = progs->entityfields * 4 + sizeof(edict_t) - sizeof(entvars_t);
@ -1140,12 +1152,11 @@ void PR_Init (void)
}
edict_t *EDICT_NUM(int n)
{
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);
return (edict_t *)((byte *)sv.edicts + (n)*pr_edict_size);
}
int NUM_FOR_EDICT(edict_t *e)
@ -1162,6 +1173,7 @@ int NUM_FOR_EDICT(edict_t *e)
//===========================================================================
#define PR_STRING_ALLOCSLOTS 256
static void PR_AllocStringSlots (void)

View file

@ -1,6 +1,5 @@
/*
Copyright (C) 1996-2001 Id Software, Inc.
Copyright (C) 2002-2009 John Fitzgibbons and others
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
@ -21,120 +20,112 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "quakedef.h"
/*
*/
typedef struct
{
int s;
dfunction_t *f;
int s;
dfunction_t *f;
} prstack_t;
#define MAX_STACK_DEPTH 32
prstack_t pr_stack[MAX_STACK_DEPTH];
int pr_depth;
static prstack_t pr_stack[MAX_STACK_DEPTH];
static int pr_depth;
#define LOCALSTACK_SIZE 2048
int localstack[LOCALSTACK_SIZE];
int localstack_used;
static int localstack[LOCALSTACK_SIZE];
static int localstack_used;
qboolean pr_trace;
dfunction_t *pr_xfunction;
int pr_xstatement;
int pr_xstatement;
int pr_argc;
const char *pr_opnames[] =
static const char *pr_opnames[] =
{
"DONE",
"DONE",
"MUL_F",
"MUL_V",
"MUL_FV",
"MUL_VF",
"MUL_F",
"MUL_V",
"MUL_FV",
"MUL_VF",
"DIV",
"DIV",
"ADD_F",
"ADD_V",
"ADD_F",
"ADD_V",
"SUB_F",
"SUB_V",
"SUB_F",
"SUB_V",
"EQ_F",
"EQ_V",
"EQ_S",
"EQ_E",
"EQ_FNC",
"EQ_F",
"EQ_V",
"EQ_S",
"EQ_E",
"EQ_FNC",
"NE_F",
"NE_V",
"NE_S",
"NE_E",
"NE_FNC",
"NE_F",
"NE_V",
"NE_S",
"NE_E",
"NE_FNC",
"LE",
"GE",
"LT",
"GT",
"LE",
"GE",
"LT",
"GT",
"INDIRECT",
"INDIRECT",
"INDIRECT",
"INDIRECT",
"INDIRECT",
"INDIRECT",
"INDIRECT",
"INDIRECT",
"INDIRECT",
"INDIRECT",
"INDIRECT",
"INDIRECT",
"ADDRESS",
"ADDRESS",
"STORE_F",
"STORE_V",
"STORE_S",
"STORE_ENT",
"STORE_FLD",
"STORE_FNC",
"STORE_F",
"STORE_V",
"STORE_S",
"STORE_ENT",
"STORE_FLD",
"STORE_FNC",
"STOREP_F",
"STOREP_V",
"STOREP_S",
"STOREP_ENT",
"STOREP_FLD",
"STOREP_FNC",
"STOREP_F",
"STOREP_V",
"STOREP_S",
"STOREP_ENT",
"STOREP_FLD",
"STOREP_FNC",
"RETURN",
"RETURN",
"NOT_F",
"NOT_V",
"NOT_S",
"NOT_ENT",
"NOT_FNC",
"NOT_F",
"NOT_V",
"NOT_S",
"NOT_ENT",
"NOT_FNC",
"IF",
"IFNOT",
"IF",
"IFNOT",
"CALL0",
"CALL1",
"CALL2",
"CALL3",
"CALL4",
"CALL5",
"CALL6",
"CALL7",
"CALL8",
"CALL0",
"CALL1",
"CALL2",
"CALL3",
"CALL4",
"CALL5",
"CALL6",
"CALL7",
"CALL8",
"STATE",
"STATE",
"GOTO",
"GOTO",
"AND",
"OR",
"AND",
"OR",
"BITAND",
"BITOR"
"BITAND",
"BITOR"
};
const char *PR_GlobalString (int ofs);
@ -148,39 +139,39 @@ const char *PR_GlobalStringNoContents (int ofs);
PR_PrintStatement
=================
*/
void PR_PrintStatement (dstatement_t *s)
static void PR_PrintStatement (dstatement_t *s)
{
int i;
int i;
if ( (unsigned)s->op < sizeof(pr_opnames)/sizeof(pr_opnames[0]))
if ((unsigned int)s->op < sizeof(pr_opnames)/sizeof(pr_opnames[0]))
{
Con_Printf ("%s ", pr_opnames[s->op]);
Con_Printf("%s ", pr_opnames[s->op]);
i = strlen(pr_opnames[s->op]);
for ( ; i<10 ; i++)
Con_Printf (" ");
for ( ; i < 10; i++)
Con_Printf(" ");
}
if (s->op == OP_IF || s->op == OP_IFNOT)
Con_Printf ("%sbranch %i",PR_GlobalString(s->a),s->b);
Con_Printf("%sbranch %i", PR_GlobalString(s->a), s->b);
else if (s->op == OP_GOTO)
{
Con_Printf ("branch %i",s->a);
Con_Printf("branch %i", s->a);
}
else if ( (unsigned)(s->op - OP_STORE_F) < 6)
else if ((unsigned int)(s->op-OP_STORE_F) < 6)
{
Con_Printf ("%s",PR_GlobalString(s->a));
Con_Printf ("%s", PR_GlobalStringNoContents(s->b));
Con_Printf("%s", PR_GlobalString(s->a));
Con_Printf("%s", PR_GlobalStringNoContents(s->b));
}
else
{
if (s->a)
Con_Printf ("%s",PR_GlobalString(s->a));
Con_Printf("%s", PR_GlobalString(s->a));
if (s->b)
Con_Printf ("%s",PR_GlobalString(s->b));
Con_Printf("%s", PR_GlobalString(s->b));
if (s->c)
Con_Printf ("%s", PR_GlobalStringNoContents(s->c));
Con_Printf("%s", PR_GlobalStringNoContents(s->c));
}
Con_Printf ("\n");
Con_Printf("\n");
}
/*
@ -188,28 +179,29 @@ void PR_PrintStatement (dstatement_t *s)
PR_StackTrace
============
*/
void PR_StackTrace (void)
static void PR_StackTrace (void)
{
int i;
dfunction_t *f;
int i;
if (pr_depth == 0)
{
Con_Printf ("<NO STACK>\n");
Con_Printf("<NO STACK>\n");
return;
}
pr_stack[pr_depth].f = pr_xfunction;
for (i=pr_depth ; i>=0 ; i--)
for (i = pr_depth; i >= 0; i--)
{
f = pr_stack[i].f;
if (!f)
{
Con_Printf ("<NO FUNCTION>\n");
Con_Printf("<NO FUNCTION>\n");
}
else
Con_Printf ("%12s : %s\n", PR_GetString(f->s_file), PR_GetString(f->s_name));
{
Con_Printf("%12s : %s\n", PR_GetString(f->s_file), PR_GetString(f->s_name));
}
}
}
@ -222,10 +214,9 @@ PR_Profile_f
*/
void PR_Profile_f (void)
{
int i, num;
int pmax;
dfunction_t *f, *best;
int pmax;
int num;
int i;
if (!sv.active)
return;
@ -235,7 +226,7 @@ void PR_Profile_f (void)
{
pmax = 0;
best = NULL;
for (i=0 ; i<progs->numfunctions ; i++)
for (i = 0; i < progs->numfunctions; i++)
{
f = &pr_functions[i];
if (f->profile > pmax)
@ -247,7 +238,7 @@ void PR_Profile_f (void)
if (best)
{
if (num < 10)
Con_Printf ("%7i %s\n", best->profile, PR_GetString(best->s_name));
Con_Printf("%7i %s\n", best->profile, PR_GetString(best->s_name));
num++;
best->profile = 0;
}
@ -264,30 +255,23 @@ Aborts the currently executing function
*/
void PR_RunError (const char *error, ...)
{
va_list argptr;
char string[1024];
va_list argptr;
char string[1024];
va_start (argptr,error);
va_start (argptr, error);
q_vsnprintf (string, sizeof(string), error, argptr);
va_end (argptr);
PR_PrintStatement (pr_statements + pr_xstatement);
PR_StackTrace ();
Con_Printf ("%s\n", string);
PR_PrintStatement(pr_statements + pr_xstatement);
PR_StackTrace();
pr_depth = 0; // dump the stack so host_error can shutdown functions
Con_Printf("%s\n", string);
Host_Error ("Program error");
pr_depth = 0; // dump the stack so host_error can shutdown functions
Host_Error("Program error");
}
/*
============================================================================
PR_ExecuteProgram
The interpretation main loop
============================================================================
*/
/*
====================
PR_EnterFunction
@ -295,32 +279,32 @@ PR_EnterFunction
Returns the new program statement counter
====================
*/
int PR_EnterFunction (dfunction_t *f)
static int PR_EnterFunction (dfunction_t *f)
{
int i, j, c, o;
int i, j, c, o;
pr_stack[pr_depth].s = pr_xstatement;
pr_stack[pr_depth].f = pr_xfunction;
pr_depth++;
if (pr_depth >= MAX_STACK_DEPTH)
PR_RunError ("stack overflow");
PR_RunError("stack overflow");
// save off any locals that the new function steps on
// save off any locals that the new function steps on
c = f->locals;
if (localstack_used + c > LOCALSTACK_SIZE)
PR_RunError ("PR_ExecuteProgram: locals stack overflow\n");
PR_RunError("PR_ExecuteProgram: locals stack overflow\n");
for (i=0 ; i < c ; i++)
localstack[localstack_used+i] = ((int *)pr_globals)[f->parm_start + i];
for (i = 0; i < c ; i++)
localstack[localstack_used + i] = ((int *)pr_globals)[f->parm_start + i];
localstack_used += c;
// copy parameters
// copy parameters
o = f->parm_start;
for (i=0 ; i<f->numparms ; i++)
for (i = 0; i < f->numparms; i++)
{
for (j=0 ; j<f->parm_size[i] ; j++)
for (j = 0; j < f->parm_size[i]; j++)
{
((int *)pr_globals)[o] = ((int *)pr_globals)[OFS_PARM0+i*3+j];
((int *)pr_globals)[o] = ((int *)pr_globals)[OFS_PARM0 + i*3 + j];
o++;
}
}
@ -334,23 +318,23 @@ int PR_EnterFunction (dfunction_t *f)
PR_LeaveFunction
====================
*/
int PR_LeaveFunction (void)
static int PR_LeaveFunction (void)
{
int i, c;
int i, c;
if (pr_depth <= 0)
Sys_Error ("prog stack underflow");
Sys_Error("prog stack underflow");
// restore locals from the stack
// Restore locals from the stack
c = pr_xfunction->locals;
localstack_used -= c;
if (localstack_used < 0)
PR_RunError ("PR_ExecuteProgram: locals stack underflow\n");
PR_RunError("PR_ExecuteProgram: locals stack underflow");
for (i=0 ; i < c ; i++)
((int *)pr_globals)[pr_xfunction->parm_start + i] = localstack[localstack_used+i];
for (i = 0; i < c; i++)
((int *)pr_globals)[pr_xfunction->parm_start + i] = localstack[localstack_used + i];
// up stack
// up stack
pr_depth--;
pr_xfunction = pr_stack[pr_depth].f;
return pr_stack[pr_depth].s;
@ -360,19 +344,21 @@ int PR_LeaveFunction (void)
/*
====================
PR_ExecuteProgram
The interpretation main loop
====================
*/
void PR_ExecuteProgram (func_t fnum)
{
eval_t *a, *b, *c;
int s;
eval_t *ptr;
int s;
dstatement_t *st;
dfunction_t *f, *newf;
int runaway;
int i;
edict_t *ed;
edict_t *ed;
int exitdepth;
eval_t *ptr;
if (!fnum || fnum >= progs->numfunctions)
{
@ -389,10 +375,10 @@ void PR_ExecuteProgram (func_t fnum)
// make a stack frame
exitdepth = pr_depth;
s = PR_EnterFunction (f);
s = PR_EnterFunction(f);
while (1)
{
while (1)
{
s++; // next statement
st = &pr_statements[s];
@ -401,13 +387,13 @@ while (1)
c = (eval_t *)&pr_globals[st->c];
if (!--runaway)
PR_RunError ("runaway loop error");
PR_RunError("runaway loop error");
pr_xfunction->profile++;
pr_xstatement = s;
if (pr_trace)
PR_PrintStatement (st);
PR_PrintStatement(st);
switch (st->op)
{
@ -433,9 +419,9 @@ while (1)
c->_float = a->_float * b->_float;
break;
case OP_MUL_V:
c->_float = a->vector[0]*b->vector[0]
+ a->vector[1]*b->vector[1]
+ a->vector[2]*b->vector[2];
c->_float = a->vector[0]*b->vector[0] +
a->vector[1]*b->vector[1] +
a->vector[2]*b->vector[2];
break;
case OP_MUL_FV:
c->vector[0] = a->_float * b->vector[0];
@ -460,7 +446,6 @@ while (1)
c->_float = (int)a->_float | (int)b->_float;
break;
case OP_GE:
c->_float = a->_float >= b->_float;
break;
@ -501,11 +486,11 @@ while (1)
break;
case OP_EQ_V:
c->_float = (a->vector[0] == b->vector[0]) &&
(a->vector[1] == b->vector[1]) &&
(a->vector[2] == b->vector[2]);
(a->vector[1] == b->vector[1]) &&
(a->vector[2] == b->vector[2]);
break;
case OP_EQ_S:
c->_float = !strcmp(PR_GetString(a->string),PR_GetString(b->string));
c->_float = !strcmp(PR_GetString(a->string), PR_GetString(b->string));
break;
case OP_EQ_E:
c->_float = a->_int == b->_int;
@ -514,14 +499,13 @@ while (1)
c->_float = a->function == b->function;
break;
case OP_NE_F:
c->_float = a->_float != b->_float;
break;
case OP_NE_V:
c->_float = (a->vector[0] != b->vector[0]) ||
(a->vector[1] != b->vector[1]) ||
(a->vector[2] != b->vector[2]);
(a->vector[1] != b->vector[1]) ||
(a->vector[2] != b->vector[2]);
break;
case OP_NE_S:
c->_float = strcmp(PR_GetString(a->string),PR_GetString(b->string));
@ -533,12 +517,11 @@ while (1)
c->_float = a->function != b->function;
break;
//==================
case OP_STORE_F:
case OP_STORE_ENT:
case OP_STORE_FLD: // integers
case OP_STORE_FLD: // integers
case OP_STORE_S:
case OP_STORE_FNC: // pointers
case OP_STORE_FNC: // pointers
b->_int = a->_int;
break;
case OP_STORE_V:
@ -549,9 +532,9 @@ while (1)
case OP_STOREP_F:
case OP_STOREP_ENT:
case OP_STOREP_FLD: // integers
case OP_STOREP_FLD: // integers
case OP_STOREP_S:
case OP_STOREP_FNC: // pointers
case OP_STOREP_FNC: // pointers
ptr = (eval_t *)((byte *)sv.edicts + b->_int);
ptr->_int = a->_int;
break;
@ -565,10 +548,10 @@ while (1)
case OP_ADDRESS:
ed = PROG_TO_EDICT(a->edict);
#ifdef PARANOID
NUM_FOR_EDICT(ed); // make sure it's in range
NUM_FOR_EDICT(ed); // Make sure it's in range
#endif
if (ed == (edict_t *)sv.edicts && sv.state == ss_active)
PR_RunError ("assignment to world entity");
PR_RunError("assignment to world entity");
c->_int = (byte *)((int *)&ed->v + b->_int) - (byte *)sv.edicts;
break;
@ -579,7 +562,7 @@ while (1)
case OP_LOAD_FNC:
ed = PROG_TO_EDICT(a->edict);
#ifdef PARANOID
NUM_FOR_EDICT(ed); // make sure it's in range
NUM_FOR_EDICT(ed); // Make sure it's in range
#endif
a = (eval_t *)((int *)&ed->v + b->_int);
c->_int = a->_int;
@ -588,7 +571,7 @@ while (1)
case OP_LOAD_V:
ed = PROG_TO_EDICT(a->edict);
#ifdef PARANOID
NUM_FOR_EDICT(ed); // make sure it's in range
NUM_FOR_EDICT(ed); // Make sure it's in range
#endif
a = (eval_t *)((int *)&ed->v + b->_int);
c->vector[0] = a->vector[0];
@ -609,7 +592,7 @@ while (1)
break;
case OP_GOTO:
s += st->a - 1; // offset the s++
s += st->a - 1; // offset the s++
break;
case OP_CALL0:
@ -623,31 +606,30 @@ while (1)
case OP_CALL8:
pr_argc = st->op - OP_CALL0;
if (!a->function)
PR_RunError ("NULL function");
PR_RunError("NULL function");
newf = &pr_functions[a->function];
if (newf->first_statement < 0)
{ // negative statements are built in functions
{ // Built-in function
i = -newf->first_statement;
if (i >= pr_numbuiltins)
PR_RunError ("Bad builtin call number");
pr_builtins[i] ();
PR_RunError("Bad builtin call number %d", i);
pr_builtins[i]();
break;
}
s = PR_EnterFunction (newf);
// Normal function
s = PR_EnterFunction(newf);
break;
case OP_DONE:
case OP_RETURN:
pr_globals[OFS_RETURN] = pr_globals[st->a];
pr_globals[OFS_RETURN+1] = pr_globals[st->a+1];
pr_globals[OFS_RETURN+2] = pr_globals[st->a+2];
s = PR_LeaveFunction ();
pr_globals[OFS_RETURN + 1] = pr_globals[st->a + 1];
pr_globals[OFS_RETURN + 2] = pr_globals[st->a + 2];
s = PR_LeaveFunction();
if (pr_depth == exitdepth)
return; // all done
{ // Done
return;
}
break;
case OP_STATE:
@ -661,8 +643,8 @@ while (1)
break;
default:
PR_RunError ("Bad opcode %i", st->op);
PR_RunError("Bad opcode %i", st->op);
}
} /* end of while(1) loop */
}
}

View file

@ -22,58 +22,62 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#ifndef _QUAKE_PROGS_H
#define _QUAKE_PROGS_H
#include "pr_comp.h" // defs shared with qcc
#include "progdefs.h" // generated by program cdefs
#include "pr_comp.h" /* defs shared with qcc */
#include "progdefs.h" /* generated by program cdefs */
typedef union eval_s
{
string_t string;
float _float;
float vector[3];
func_t function;
int _int;
int edict;
string_t string;
float _float;
float vector[3];
func_t function;
int _int;
int edict;
} eval_t;
#define MAX_ENT_LEAFS 16
typedef struct edict_s
{
qboolean free;
link_t area; // linked to a division node or leaf
int num_leafs;
short leafnums[MAX_ENT_LEAFS];
qboolean free;
link_t area; /* linked to a division node or leaf */
int num_leafs;
short leafnums[MAX_ENT_LEAFS];
entity_state_t baseline;
unsigned char alpha; // johnfitz -- hack to support alpha since it's not part of entvars_t
qboolean sendinterval; // johnfitz -- send time until nextthink to client for better lerp timing
float freetime; // sv.time when the object was freed
entvars_t v; // C exported fields from progs
// other fields from progs come immediately after
unsigned char alpha; /* johnfitz -- hack to support alpha since it's not part of entvars_t */
qboolean sendinterval; /* johnfitz -- send time until nextthink to client for better lerp timing */
float freetime; /* sv.time when the object was freed */
entvars_t v; /* C exported fields from progs */
/* other fields from progs come immediately after */
} edict_t;
#define EDICT_FROM_AREA(l) STRUCT_FROM_LINK(l,edict_t,area)
#define EDICT_FROM_AREA(l) STRUCT_FROM_LINK(l,edict_t,area)
//============================================================================
extern dprograms_t *progs;
extern dfunction_t *pr_functions;
extern dprograms_t *progs;
extern dfunction_t *pr_functions;
extern dstatement_t *pr_statements;
extern globalvars_t *pr_global_struct;
extern float *pr_globals; // same as pr_global_struct
extern float *pr_globals; /* same as pr_global_struct */
extern int pr_edict_size; // in bytes
extern int pr_edict_size; /* in bytes */
//============================================================================
void PR_Init (void);
void PR_ExecuteProgram (func_t fnum);
void PR_LoadProgs (void);
void PR_Profile_f (void);
const char *PR_GetString (int num);
int PR_SetEngineString (const char *s);
int PR_AllocString (int bufferlength, char **ptr);
void PR_Profile_f (void);
edict_t *ED_Alloc (void);
void ED_Free (edict_t *ed);
@ -86,45 +90,44 @@ void ED_ParseGlobals (const char *data);
void ED_LoadFromFile (const char *data);
//define EDICT_NUM(n) ((edict_t *)(sv.edicts+ (n)*pr_edict_size))
//define NUM_FOR_EDICT(e) (((byte *)(e) - sv.edicts)/pr_edict_size)
/*
#define EDICT_NUM(n) ((edict_t *)(sv.edicts+ (n)*pr_edict_size))
#define NUM_FOR_EDICT(e) (((byte *)(e) - sv.edicts) / pr_edict_size)
*/
edict_t *EDICT_NUM(int n);
int NUM_FOR_EDICT(edict_t *e);
#define NEXT_EDICT(e) ((edict_t *)( (byte *)e + pr_edict_size))
#define NEXT_EDICT(e) ((edict_t *)( (byte *)e + pr_edict_size))
#define EDICT_TO_PROG(e) ((byte *)e - (byte *)sv.edicts)
#define PROG_TO_EDICT(e) ((edict_t *)((byte *)sv.edicts + e))
#define EDICT_TO_PROG(e) ((byte *)e - (byte *)sv.edicts)
#define PROG_TO_EDICT(e) ((edict_t *)((byte *)sv.edicts + e))
//============================================================================
#define G_FLOAT(o) (pr_globals[o])
#define G_INT(o) (*(int *)&pr_globals[o])
#define G_EDICT(o) ((edict_t *)((byte *)sv.edicts+ *(int *)&pr_globals[o]))
#define G_EDICTNUM(o) NUM_FOR_EDICT(G_EDICT(o))
#define G_VECTOR(o) (&pr_globals[o])
#define G_STRING(o) (PR_GetString(*(string_t *)&pr_globals[o]))
#define G_FUNCTION(o) (*(func_t *)&pr_globals[o])
#define G_FLOAT(o) (pr_globals[o])
#define G_INT(o) (*(int *)&pr_globals[o])
#define G_EDICT(o) ((edict_t *)((byte *)sv.edicts+ *(int *)&pr_globals[o]))
#define G_EDICTNUM(o) NUM_FOR_EDICT(G_EDICT(o))
#define G_VECTOR(o) (&pr_globals[o])
#define G_STRING(o) (PR_GetString(*(string_t *)&pr_globals[o]))
#define G_FUNCTION(o) (*(func_t *)&pr_globals[o])
#define E_FLOAT(e,o) (((float*)&e->v)[o])
#define E_INT(e,o) (*(int *)&((float*)&e->v)[o])
#define E_VECTOR(e,o) (&((float*)&e->v)[o])
#define E_STRING(e,o) (PR_GetString(*(string_t *)&((float*)&e->v)[o]))
#define E_FLOAT(e,o) (((float*)&e->v)[o])
#define E_INT(e,o) (*(int *)&((float*)&e->v)[o])
#define E_VECTOR(e,o) (&((float*)&e->v)[o])
#define E_STRING(e,o) (PR_GetString(*(string_t *)&((float*)&e->v)[o]))
extern int type_size[8];
typedef void (*builtin_t) (void);
extern builtin_t *pr_builtins;
extern int pr_numbuiltins;
extern builtin_t *pr_builtins;
extern int pr_numbuiltins;
extern int pr_argc;
extern int pr_argc;
extern qboolean pr_trace;
extern dfunction_t *pr_xfunction;
extern int pr_xstatement;
extern int pr_xstatement;
extern unsigned short pr_crc;
extern unsigned short pr_crc;
void PR_RunError (const char *error, ...) __attribute__((__format__(__printf__,1,2), __noreturn__));