quakeforge/libs/gamecode/engine/pr_parse.c

479 lines
10 KiB
C
Raw Normal View History

2002-10-23 04:57:08 +00:00
/*
pr_parse.c
map and savegame parsing
Copyright (C) 1996-1997 Id Software, Inc.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
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
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to:
Free Software Foundation, Inc.
59 Temple Place - Suite 330
Boston, MA 02111-1307, USA
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
static __attribute__ ((used)) const char rcsid[] =
"$Id$";
2002-10-23 04:57:08 +00:00
#ifdef HAVE_STRING_H
# include <string.h>
#endif
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
#include <stdarg.h>
#include <stdio.h>
#include "QF/cbuf.h"
#include "QF/crc.h"
#include "QF/cvar.h"
#include "QF/dstring.h"
#include "QF/hash.h"
#include "QF/progs.h"
#include "QF/qdefs.h"
2003-07-17 18:27:00 +00:00
#include "QF/qfplist.h"
2002-10-23 04:57:08 +00:00
#include "QF/qendian.h"
#include "QF/quakefs.h"
#include "QF/script.h"
2002-10-23 04:57:08 +00:00
#include "QF/sys.h"
#include "QF/zone.h"
#include "QF/va.h"
#include "compat.h"
/*
PR_UglyValueString
Returns a string describing *data in a type specific manner
Easier to parse than PR_ValueString
*/
static char *
2002-10-23 04:57:08 +00:00
PR_UglyValueString (progs_t *pr, etype_t type, pr_type_t *val)
{
static char line[256];
2002-10-23 04:57:08 +00:00
ddef_t *def;
dfunction_t *f;
type &= ~DEF_SAVEGLOBAL;
switch (type) {
case ev_string:
snprintf (line, sizeof (line), "%s",
PR_GetString (pr, val->string_var));
break;
case ev_entity:
snprintf (line, sizeof (line), "%ld",
2002-10-23 04:57:08 +00:00
NUM_FOR_BAD_EDICT (pr, PROG_TO_EDICT (pr, val->entity_var)));
break;
case ev_func:
f = pr->pr_functions + val->func_var;
snprintf (line, sizeof (line), "%s", PR_GetString (pr, f->s_name));
break;
case ev_field:
def = PR_FieldAtOfs (pr, val->integer_var);
2002-10-23 04:57:08 +00:00
snprintf (line, sizeof (line), "%s",
PR_GetString (pr, def->s_name));
break;
case ev_void:
strcpy (line, "void");
break;
case ev_float:
snprintf (line, sizeof (line), "%f", val->float_var);
break;
case ev_vector:
snprintf (line, sizeof (line), "%f %f %f", val->vector_var[0],
val->vector_var[1], val->vector_var[2]);
break;
default:
snprintf (line, sizeof (line), "bad type %i", type);
break;
}
return line;
}
plitem_t *
ED_EntityDict (progs_t *pr, edict_t *ed)
2002-10-23 04:57:08 +00:00
{
plitem_t *entity = PL_NewDictionary ();
unsigned i;
2003-04-17 00:01:48 +00:00
int j;
int type;
const char *name;
const char *value;
pr_type_t *v;
2002-10-23 04:57:08 +00:00
if (!ed->free) {
for (i = 0; i < pr->progs->numfielddefs; i++) {
ddef_t *d = &pr->pr_fielddefs[i];
2002-10-23 04:57:08 +00:00
name = PR_GetString (pr, d->s_name);
if (name[strlen (name) - 2] == '_')
continue; // skip _x, _y, _z vars
2002-10-23 04:57:08 +00:00
v = &ed->v[d->ofs];
2002-10-23 04:57:08 +00:00
// if the value is still all 0, skip the field
type = d->type & ~DEF_SAVEGLOBAL;
for (j = 0; j < pr_type_size[type]; j++)
if (v[j].integer_var)
break;
if (j == pr_type_size[type])
continue;
2002-10-23 04:57:08 +00:00
value = PR_UglyValueString (pr, type, v);
PL_D_AddObject (entity, name, PL_NewString (value));
}
2002-10-23 04:57:08 +00:00
}
return entity;
2002-10-23 04:57:08 +00:00
}
/*
ARCHIVING GLOBALS
FIXME: need to tag constants, doesn't really work
*/
plitem_t *
ED_GlobalsDict (progs_t *pr)
2002-10-23 04:57:08 +00:00
{
plitem_t *globals = PL_NewDictionary ();
unsigned i;
const char *name;
const char *value;
ddef_t *def;
int type;
2002-10-23 04:57:08 +00:00
for (i = 0; i < pr->progs->numglobaldefs; i++) {
def = &pr->pr_globaldefs[i];
type = def->type;
if (!(def->type & DEF_SAVEGLOBAL))
continue;
type &= ~DEF_SAVEGLOBAL;
if (type != ev_string && type != ev_float && type != ev_entity)
continue;
name = PR_GetString (pr, def->s_name);
value = PR_UglyValueString (pr, type, &pr->pr_globals[def->ofs]);
PL_D_AddObject (globals, name, PL_NewString (value));
2002-10-23 04:57:08 +00:00
}
return globals;
2002-10-23 04:57:08 +00:00
}
static int
2002-10-23 04:57:08 +00:00
ED_NewString (progs_t *pr, const char *string)
{
char *new, *new_p;
int i, l;
l = strlen (string) + 1;
new = alloca (l);
2002-10-23 04:57:08 +00:00
new_p = new;
for (i = 0; i < l; i++) {
if (string[i] == '\\' && i < l - 1) {
i++;
if (string[i] == 'n')
*new_p++ = '\n';
else
*new_p++ = '\\';
} else
*new_p++ = string[i];
}
return PR_SetString (pr, new);
2002-10-23 04:57:08 +00:00
}
/*
ED_ParseEval
Can parse either fields or globals
returns false if error
*/
qboolean
2002-10-23 04:57:08 +00:00
ED_ParseEpair (progs_t *pr, pr_type_t *base, ddef_t *key, const char *s)
{
int i;
char *string;
2002-10-23 04:57:08 +00:00
ddef_t *def;
char *v, *w;
pr_type_t *d;
dfunction_t *func;
d = &base[key->ofs];
switch (key->type & ~DEF_SAVEGLOBAL) {
case ev_string:
d->string_var = ED_NewString (pr, s);
2002-10-23 04:57:08 +00:00
break;
case ev_float:
d->float_var = atof (s);
break;
case ev_vector:
string = strdup (s);
2002-10-23 04:57:08 +00:00
v = string;
w = string;
for (i = 0; i < 3; i++) {
while (*v && *v != ' ')
v++;
*v = 0;
d->vector_var[i] = atof (w);
w = v = v + 1;
}
free (string);
2002-10-23 04:57:08 +00:00
break;
case ev_entity:
d->entity_var = EDICT_TO_PROG (pr, EDICT_NUM (pr, atoi (s)));
break;
case ev_field:
def = PR_FindField (pr, s);
2002-10-23 04:57:08 +00:00
if (!def) {
Sys_Printf ("Can't find field %s\n", s);
return false;
}
d->integer_var = G_INT (pr, def->ofs);
break;
case ev_func:
func = PR_FindFunction (pr, s);
2002-10-23 04:57:08 +00:00
if (!func) {
Sys_Printf ("Can't find function %s\n", s);
return false;
}
d->func_var = func - pr->pr_functions;
break;
default:
break;
}
return true;
}
/*
ED_ParseOld
2002-10-23 04:57:08 +00:00
The entities are directly placed in the array, rather than allocated with
ED_Alloc, because otherwise an error loading the map would have entity
number references out of order.
2002-10-23 04:57:08 +00:00
Creates a server's entity / program execution context by
parsing textual entity definitions out of an ent file.
2002-10-23 04:57:08 +00:00
Used for both fresh maps and savegame loads. A fresh map would also need
to call ED_CallSpawnFunctions () to let the objects initialize themselves.
*/
2002-10-23 04:57:08 +00:00
plitem_t *
ED_ConvertToPlist (progs_t *pr, script_t *script)
{
plitem_t *plist = PL_NewArray ();
plitem_t *ent;
plitem_t *key;
plitem_t *value;
const char *token;
int anglehack;
2002-10-23 04:57:08 +00:00
while (Script_GetToken (script, 1)) {
token = script->token->str;
if (!strequal (token, "{"))
PR_Error (pr, "ED_ParseEntity: EOF without closing brace");
ent = PL_NewDictionary ();
while (1) {
if (!Script_GetToken (script, 1))
PR_Error (pr, "ED_ParseEntity: EOF without closing brace");
token = script->token->str;
if (strequal (token, "}"))
break;
anglehack = 0;
if (strequal (token, "angle")) {
key = PL_NewString ("angles");
anglehack = 1;
} else if (strequal (token, "light")) {
key = PL_NewString ("light_lev");
2002-10-23 04:57:08 +00:00
} else {
key = PL_NewString (token);
2002-10-23 04:57:08 +00:00
}
if (!Script_TokenAvailable (script, 0))
PR_Error (pr, "ED_ParseEntity: EOL without value");
Script_GetToken (script, 0);
token = script->token->str;
if (strequal (token, "}"))
PR_Error (pr, "ED_ParseEntity: closing brace without data");
if (anglehack)
value = PL_NewString (va ("0 %s 0", token));
else
value = PL_NewString (token);
PL_D_AddObject (ent, PL_String (key), value);
PL_Free (key);
2002-10-23 04:57:08 +00:00
}
PL_A_AddObject (plist, ent);
2002-10-23 04:57:08 +00:00
}
return plist;
2002-10-23 04:57:08 +00:00
}
2002-10-23 04:57:08 +00:00
void
ED_InitGlobals (progs_t *pr, plitem_t *globals)
2002-10-23 04:57:08 +00:00
{
ddef_t *global;
plitem_t *keys;
int count;
const char *global_name;
const char *value;
keys = PL_D_AllKeys (globals);
count = PL_A_NumObjects (keys);
while (count--) {
global_name = PL_String (PL_ObjectAtIndex (keys, count));
value = PL_String (PL_ObjectForKey (globals, global_name));
global = PR_FindGlobal (pr, global_name);
if (!global) {
Sys_Printf ("'%s' is not a global\n", global_name);
2002-10-23 04:57:08 +00:00
continue;
}
if (!ED_ParseEpair (pr, pr->pr_globals, global, value))
PR_Error (pr, "ED_InitGlobals: parse error");
2002-10-23 04:57:08 +00:00
}
PL_Free (keys);
2002-10-23 04:57:08 +00:00
}
void
ED_InitEntity (progs_t *pr, plitem_t *entity, edict_t *ent)
{
ddef_t *field;
plitem_t *keys;
const char *field_name;
const char *value;
int count;
keys = PL_D_AllKeys (entity);
count = PL_A_NumObjects (keys);
while (count--) {
field_name = PL_String (PL_ObjectAtIndex (keys, count));
value = PL_String (PL_ObjectForKey (entity, field_name));
field = PR_FindField (pr, field_name);
if (!field) {
if (!pr->parse_field
|| !pr->parse_field (pr, field_name, value)) {
Sys_Printf ("'%s' is not a field\n", field_name);
continue;
}
} else {
if (!ED_ParseEpair (pr, ent->v, field, value))
PR_Error (pr, "ED_InitEntity: parse error");
}
}
PL_Free (keys);
}
2002-10-23 04:57:08 +00:00
2003-07-17 18:27:00 +00:00
static void
ED_SpawnEntities (progs_t *pr, plitem_t *entity_list)
2002-10-23 04:57:08 +00:00
{
edict_t *ent;
int inhibit;
plitem_t *entity;
plitem_t *item;
int i;
int count;
const char *classname;
dfunction_t *func;
count = PL_A_NumObjects (entity_list);
for (i = 0; i < count; i++) {
entity = PL_ObjectAtIndex (entity_list, i);
item = PL_ObjectForKey (entity, "classname");
if (!item)
PR_Error (pr, "no classname for entity %d", i);
classname = PL_String (item);
if (strequal (classname, "worldspawn"))
2002-10-23 04:57:08 +00:00
ent = EDICT_NUM (pr, 0);
else
ent = ED_Alloc (pr);
ED_InitEntity (pr, entity, ent);
2002-10-23 04:57:08 +00:00
// remove things from different skill levels or deathmatch
if (pr->prune_edict && pr->prune_edict (pr, ent)) {
ED_Free (pr, ent);
inhibit++;
continue;
}
//XXX should the field be checked instead of going direct?
func = PR_FindFunction (pr, classname);
2002-10-23 04:57:08 +00:00
if (!func) {
Sys_Printf ("No spawn function for :\n");
2002-10-23 04:57:08 +00:00
ED_Print (pr, ent);
ED_Free (pr, ent);
continue;
}
*pr->globals.self = EDICT_TO_PROG (pr, ent);
PR_ExecuteProgram (pr, func - pr->pr_functions);
if (pr->flush)
pr->flush ();
}
}
2003-07-17 18:27:00 +00:00
void
ED_LoadFromFile (progs_t *pr, const char *data)
{
script_t *script;
plitem_t *entity_list;
if (pr->edict_parse) {
PR_PushFrame (pr);
PR_RESET_PARAMS (pr);
P_INT (pr, 0) = PR_SetTempString (pr, data);
PR_ExecuteProgram (pr, pr->edict_parse);
PR_PopFrame (pr);
return;
}
script = Script_New ();
Script_Start (script, "ent data", data);
if (Script_GetToken (script, 1)) {
if (strequal (script->token->str, "(")) {
// new style (plist) entity data
entity_list = PL_GetPropertyList (data);
} else {
// oldstyle entity data
Script_UngetToken (script);
entity_list = ED_ConvertToPlist (pr, script);
}
Script_Delete (script);
ED_SpawnEntities (pr, entity_list);
2003-07-17 18:27:00 +00:00
}
}
void
ED_EntityParseFunction (progs_t *pr)
{
pr->edict_parse = P_FUNCTION (pr, 0);
2003-07-17 18:27:00 +00:00
}