From ca507336ca04f88886dce063d9faf740fab49d25 Mon Sep 17 00:00:00 2001 From: Eric Wasylishen Date: Sun, 24 Jul 2016 16:08:38 -0600 Subject: [PATCH] light: use entdicts array for FIndModelInfo --- include/light/entities.hh | 7 ++++++- light/entities.cc | 22 +++++++--------------- light/light.cc | 14 ++++++++------ 3 files changed, 21 insertions(+), 22 deletions(-) diff --git a/include/light/entities.hh b/include/light/entities.hh index 8fb6465c..a592cacc 100644 --- a/include/light/entities.hh +++ b/include/light/entities.hh @@ -30,6 +30,8 @@ #define DEFAULTLIGHTLEVEL 300.0f +using entdict_t = std::map; + /* * Light attenuation formalae * (relative to distance 'x' from the light source) @@ -146,10 +148,13 @@ public: #define MAX_LIGHTS 65536 extern entity_t *lights[MAX_LIGHTS]; -entity_t *FindEntityWithKeyPair(const char *key, const char *value); +const entdict_t *FindEntDictWithKeyPair(const std::string &key, const std::string &value); const char *ValueForKey(const entity_t *ent, const char *key); void GetVectorForKey(const entity_t *ent, const char *key, vec3_t vec); +std::string EntDict_StringForKey(const entdict_t &dict, const std::string key); +float EntDict_FloatForKey(const entdict_t &dict, const std::string key); + void SetWorldKeyValue(const char *key, const char *value); const char *WorldValueForKey(const char *key); diff --git a/light/entities.cc b/light/entities.cc index 4dad2456..038d5636 100644 --- a/light/entities.cc +++ b/light/entities.cc @@ -38,7 +38,6 @@ int num_surfacelight_templates; static void MakeSurfaceLights(const bsp2_t *bsp); using strings = std::vector; -using entdict_t = std::map; std::vector entdicts; @@ -891,7 +890,7 @@ EntData_Write(const std::vector &ents) return out.str(); } -static std::string +std::string EntDict_StringForKey(const entdict_t &dict, const std::string key) { auto it = dict.find(key); @@ -901,7 +900,7 @@ EntDict_StringForKey(const entdict_t &dict, const std::string key) return ""; } -static float +float EntDict_FloatForKey(const entdict_t &dict, const std::string key) { auto s = EntDict_StringForKey(dict, key); @@ -1164,21 +1163,14 @@ ValueForKey(const entity_t *ent, const char *key) } } -entity_t * -FindEntityWithKeyPair(const char *key, const char *value) +const entdict_t *FindEntDictWithKeyPair(const std::string &key, const std::string &value) { - entity_t *ent; - std::string value_stdstring { value }; - - for (ent = entities; ent; ent = ent->next) { - auto iter = ent->epairs.find(key); - if (iter != ent->epairs.end()) { - if ((*iter).second == value_stdstring) { - return ent; - } + for (const auto &entdict : entdicts) { + if (EntDict_StringForKey(entdict, key) == value) { + return &entdict; } } - return NULL; + return nullptr; } void diff --git a/light/light.cc b/light/light.cc index 6462f4ba..4b6be2ba 100644 --- a/light/light.cc +++ b/light/light.cc @@ -330,7 +330,6 @@ FindModelInfo(const bsp2_t *bsp, const char *lmscaleoverride) { int i, numshadowmodels, numselfshadowmodels; entity_t *entity; - char modelname[20]; const char *attribute; const modelinfo_t **shadowmodels; const modelinfo_t **selfshadowmodels; @@ -382,14 +381,17 @@ FindModelInfo(const bsp2_t *bsp, const char *lmscaleoverride) info.lightmapscale = lightmapscale; /* Find the entity for the model */ - snprintf(modelname, sizeof(modelname), "*%d", i); - entity = FindEntityWithKeyPair("model", modelname); - if (!entity) + std::stringstream ss; + ss << "*" << i; + std::string modelname = ss.str(); + + const entdict_t *entdict = FindEntDictWithKeyPair("model", modelname); + if (entdict == nullptr) Error("%s: Couldn't find entity for model %s.\n", __func__, - modelname); + modelname.c_str()); // apply settings - info.settings.setSettings(entity->epairs, false); + info.settings.setSettings(*entdict, false); /* Check if this model will cast shadows (shadow => shadowself) */ if (info.shadow.boolValue()) {