Use qfplist for light entities.

This removes a lot of redundant code from qflight (though it does become
dependent of libQFgamecode *shrug*). The nice thing is qflight now uses the
exact same code to load entities as does the server.
This commit is contained in:
Bill Currie 2012-12-26 16:24:59 +09:00
parent 675db274a6
commit 48809404b1
3 changed files with 67 additions and 87 deletions

View file

@ -377,7 +377,8 @@ QF_DEPS(QFCC_TEST,
) )
QF_DEPS(QFLIGHT, QF_DEPS(QFLIGHT,
[-I$(top_srcdir)/tools/qflight/include], [-I$(top_srcdir)/tools/qflight/include],
[$(top_builddir)/libs/util/libQFutil.la], [$(top_builddir)/libs/gamecode/libQFgamecode.la
$(top_builddir)/libs/util/libQFutil.la],
[$(WIN32_LIBS)], [$(WIN32_LIBS)],
) )
QF_DEPS(QFLMP, QF_DEPS(QFLMP,

View file

@ -50,12 +50,6 @@
#define NOISE_SMOOTH 1 // low res noise with interpolation #define NOISE_SMOOTH 1 // low res noise with interpolation
#define NOISE_PERLIN 2 // combines several noise frequencies #define NOISE_PERLIN 2 // combines several noise frequencies
typedef struct epair_s {
struct epair_s *next;
const char *key;
const char *value;
} epair_t;
typedef struct entity_s { typedef struct entity_s {
const char *classname; const char *classname;
vec3_t origin; vec3_t origin;
@ -87,8 +81,8 @@ typedef struct entity_s {
const char *target; const char *target;
const char *targetname; const char *targetname;
struct epair_s *epairs;
struct entity_s *targetent; struct entity_s *targetent;
struct plitem_s *dict;
} entity_t; } entity_t;
extern entity_t *entities; extern entity_t *entities;

View file

@ -47,6 +47,7 @@
#include "QF/bspfile.h" #include "QF/bspfile.h"
#include "QF/dstring.h" #include "QF/dstring.h"
#include "QF/mathlib.h" #include "QF/mathlib.h"
#include "QF/progs.h"
#include "QF/qfplist.h" #include "QF/qfplist.h"
#include "QF/qtypes.h" #include "QF/qtypes.h"
#include "QF/quakefs.h" #include "QF/quakefs.h"
@ -61,7 +62,6 @@
entity_t *entities; entity_t *entities;
int num_entities; int num_entities;
static int max_entities;
/* /*
ENTITY FILE PARSING ENTITY FILE PARSING
@ -164,36 +164,31 @@ WriteLights (void)
void void
LoadEntities (void) LoadEntities (void)
{ {
const char *key;
double vec[4]; double vec[4];
entity_t *entity; entity_t *entity;
epair_t *epair;
float cutoff_range; float cutoff_range;
float intensity; float intensity;
plitem_t *dict; plitem_t *dict;
plitem_t *entity_list;
plitem_t *keys;
int count;
script_t *script; script_t *script;
const char *field_name;
const char *value;
int i;
script = Script_New (); script = Script_New ();
Script_Start (script, "ent data", bsp->entdata); Script_Start (script, "ent data", bsp->entdata);
entity_list = ED_ConvertToPlist (script);
Script_Delete (script);
// start parsing // start parsing
max_entities = num_entities = 0; num_entities = PL_A_NumObjects (entity_list);
entities = 0; entities = malloc (num_entities * sizeof (entity_t));;
// go through all the entities // go through all the entities
while (Script_GetToken (script, 1)) { for (i = 0; i < num_entities; i++) {
// parse the opening brace entity = &entities[i];
if (strcmp (script->token->str, "{"))
fprintf (stderr, "LoadEntities: found %s when expecting {\n",
script->token->str);
if (num_entities == max_entities) {
max_entities += 128;
entities = realloc (entities, max_entities * sizeof (entity_t));
}
entity = &entities[num_entities];
num_entities++;
memset (entity, 0, sizeof (*entity)); memset (entity, 0, sizeof (*entity));
entity->color[0] = entity->color[1] = entity->color[2] = 1.0f; entity->color[0] = entity->color[1] = entity->color[2] = 1.0f;
VectorCopy (entity->color, entity->color2); VectorCopy (entity->color, entity->color2);
@ -201,41 +196,27 @@ LoadEntities (void)
entity->lightradius = 0; entity->lightradius = 0;
entity->lightoffset = LIGHTDISTBIAS; entity->lightoffset = LIGHTDISTBIAS;
entity->attenuation = options.attenuation; entity->attenuation = options.attenuation;
entity->dict = PL_ObjectAtIndex (entity_list, i);
dict = PL_NewDictionary (); dict = PL_NewDictionary ();
// go through all the keys in this entity // go through all the keys in this entity
while (1) { keys = PL_D_AllKeys (entity->dict);
// parse key count = PL_A_NumObjects (keys);
if (!Script_GetToken (script, 1)) while (count-- > 0) {
fprintf (stderr, "LoadEntities: EOF without closing brace"); field_name = PL_String (PL_ObjectAtIndex (keys, count));
if (!strcmp (script->token->str, "}")) value = PL_String (PL_ObjectForKey (entity->dict, field_name));
break;
key = strdup (script->token->str);
// parse value if (!strcmp (field_name, "classname"))
// FIXME shouldn't cross line entity->classname = value;
if (!Script_GetToken (script, 1)) else if (!strcmp (field_name, "target"))
fprintf (stderr, "LoadEntities: EOF without closing brace"); entity->target = value;
if (!strcmp (script->token->str, "}")) else if (!strcmp (field_name, "targetname"))
fprintf (stderr, "LoadEntities: closing brace without data"); entity->targetname = value;
else if (!strcmp (field_name, "origin")) {
epair = calloc (1, sizeof (epair_t));
epair->key = key;
epair->value = strdup (script->token->str);
epair->next = entity->epairs;
entity->epairs = epair;
if (!strcmp (key, "classname"))
entity->classname = epair->value;
else if (!strcmp (key, "target"))
entity->target = epair->value;
else if (!strcmp (key, "targetname"))
entity->targetname = epair->value;
else if (!strcmp (key, "origin")) {
// scan into doubles, then assign // scan into doubles, then assign
// which makes it vec_t size independent // which makes it vec_t size independent
if (sscanf (script->token->str, "%lf %lf %lf", if (sscanf (value, "%lf %lf %lf",
&vec[0], &vec[1], &vec[2]) != 3) &vec[0], &vec[1], &vec[2]) != 3)
fprintf (stderr, "LoadEntities: not 3 values for origin"); fprintf (stderr, "LoadEntities: not 3 values for origin");
else else
@ -244,9 +225,9 @@ LoadEntities (void)
// the leading _ is so the engine doesn't search for the field, // the leading _ is so the engine doesn't search for the field,
// but it's not wanted in the properties dictionary // but it's not wanted in the properties dictionary
if (*key == '_') if (*field_name == '_')
key++; field_name++;
PL_D_AddObject (dict, key, PL_NewString (script->token->str)); PL_D_AddObject (dict, field_name, PL_NewString (value));
} }
if (options.verbosity > 1 && entity->targetname) if (options.verbosity > 1 && entity->targetname)
@ -346,48 +327,46 @@ LoadEntities (void)
const char * const char *
ValueForKey (entity_t *ent, const char *key) ValueForKey (entity_t *ent, const char *key)
{ {
epair_t *ep; plitem_t *obj = PL_ObjectForKey (ent->dict, key);
const char *val;
for (ep = ent->epairs; ep; ep = ep->next) if (!obj)
if (!strcmp (ep->key, key))
return ep->value;
return ""; return "";
val = PL_String (obj);
if (!val)
return "";
return val;
} }
void void
SetKeyValue (entity_t *ent, const char *key, const char *value) SetKeyValue (entity_t *ent, const char *key, const char *value)
{ {
epair_t *ep; plitem_t *obj;
for (ep = ent->epairs; ep; ep = ep->next) obj = PL_NewString (value);
if (!strcmp (ep->key, key)) { PL_D_AddObject (ent->dict, key, obj);
ep->value = strdup (value);
return;
}
ep = malloc (sizeof (*ep));
ep->next = ent->epairs;
ent->epairs = ep;
ep->key = strdup (key);
ep->value = strdup (value);
} }
entity_t * entity_t *
FindEntityWithKeyPair (const char *key, const char *value) FindEntityWithKeyPair (const char *key, const char *value)
{ {
entity_t *ent;
epair_t *ep;
int i; int i;
entity_t *ent;
plitem_t *obj;
const char *val;
for (i = 0; i < num_entities; i++) { for (i = 0; i < num_entities; i++) {
ent = &entities[i]; ent = &entities[i];
for (ep = ent->epairs; ep; ep = ep->next) { obj = PL_ObjectForKey (ent->dict, key);
if (!strcmp (ep->key, key)) { if (!obj)
if (!strcmp (ep->value, value)) continue;
val = PL_String (obj);
if (!val)
break;
if (!strcmp (val, value))
return ent; return ent;
break; break;
} }
}
}
return 0; return 0;
} }
@ -404,8 +383,11 @@ void
WriteEntitiesToString (void) WriteEntitiesToString (void)
{ {
dstring_t *buf; dstring_t *buf;
epair_t *ep;
int i; int i;
plitem_t *keys;
int count;
const char *field_name;
const char *value;
buf = dstring_newstr (); buf = dstring_newstr ();
@ -413,14 +395,17 @@ WriteEntitiesToString (void)
printf ("%i switchable light styles\n", numlighttargets); printf ("%i switchable light styles\n", numlighttargets);
for (i = 0; i < num_entities; i++) { for (i = 0; i < num_entities; i++) {
ep = entities[i].epairs; if (!entities[i].dict)
if (!ep)
continue; // ent got removed continue; // ent got removed
dstring_appendstr (buf, "{\n"); dstring_appendstr (buf, "{\n");
for (ep = entities[i].epairs; ep; ep = ep->next) { keys = PL_D_AllKeys (entities[i].dict);
dasprintf (buf, "\"%s\" \"%s\"\n", ep->key, ep->value); count = PL_A_NumObjects (keys);
while (count-- > 0) {
field_name = PL_String (PL_ObjectAtIndex (keys, count));
value = PL_String (PL_ObjectForKey (entities[i].dict, field_name));
dasprintf (buf, "\"%s\" \"%s\"\n", field_name, value);
} }
dstring_appendstr (buf, "}\n"); dstring_appendstr (buf, "}\n");
} }