From 8d85211fa19fb1a85c098ca6e430b0c14a31dff0 Mon Sep 17 00:00:00 2001 From: Marc Zinnschlag Date: Sun, 8 Aug 2010 14:09:09 +0200 Subject: [PATCH] modified Npc and Creature class to let them store a copy of their own ID --- apps/esmtool/esmtool.cpp | 2 +- apps/openmw/mwworld/manualref.hpp | 17 +++++++ components/esm/loadcrea.hpp | 8 +++- components/esm/loadnpc.hpp | 6 ++- components/esm_store/reclists.hpp | 76 +++++++++++++++++++++++-------- components/esm_store/store.hpp | 4 +- 6 files changed, 88 insertions(+), 25 deletions(-) diff --git a/apps/esmtool/esmtool.cpp b/apps/esmtool/esmtool.cpp index ff8e53824f..fe067d85d1 100644 --- a/apps/esmtool/esmtool.cpp +++ b/apps/esmtool/esmtool.cpp @@ -180,7 +180,7 @@ int main(int argc, char**argv) case REC_CREA: { Creature b; - b.load(esm); + b.load(esm, id); if(quiet) break; cout << " Name: " << b.name << endl; break; diff --git a/apps/openmw/mwworld/manualref.hpp b/apps/openmw/mwworld/manualref.hpp index 4b7edfa430..b4d3f70073 100644 --- a/apps/openmw/mwworld/manualref.hpp +++ b/apps/openmw/mwworld/manualref.hpp @@ -36,6 +36,23 @@ namespace MWWorld return false; } + template + bool create (const ESMS::RecListWithIDT& list, const std::string& name) + { + if (const T *instance = list.search (name)) + { + ESMS::LiveCellRef ref; + ref.base = instance; + + mRef = ref; + mPtr = Ptr (&boost::any_cast&> (mRef), 0); + + return true; + } + + return false; + } + public: ManualRef (const ESMS::ESMStore& store, const std::string& name) diff --git a/components/esm/loadcrea.hpp b/components/esm/loadcrea.hpp index 2a22cebe03..cbf39bd950 100644 --- a/components/esm/loadcrea.hpp +++ b/components/esm/loadcrea.hpp @@ -62,8 +62,12 @@ struct Creature // Defined in loadcont.hpp InventoryList inventory; - void load(ESMReader &esm) + std::string mId; + + void load(ESMReader &esm, const std::string& id) { + mId = id; + model = esm.getHNString("MODL"); original = esm.getHNOString("CNAM"); name = esm.getHNOString("FNAM"); @@ -93,7 +97,7 @@ struct Creature // AI_A - activate? esm.skipRecord(); - } + } }; } #endif diff --git a/components/esm/loadnpc.hpp b/components/esm/loadnpc.hpp index 0075996ac5..61618c7312 100644 --- a/components/esm/loadnpc.hpp +++ b/components/esm/loadnpc.hpp @@ -97,8 +97,12 @@ struct NPC std::string name, model, race, cls, faction, script, hair, head; // body parts - void load(ESMReader &esm) + std::string mId; + + void load(ESMReader &esm, const std::string& id) { + mId = id; + npdt52.gold = -10; model = esm.getHNOString("MODL"); diff --git a/components/esm_store/reclists.hpp b/components/esm_store/reclists.hpp index 5763dc6f9f..3b160259c7 100644 --- a/components/esm_store/reclists.hpp +++ b/components/esm_store/reclists.hpp @@ -17,16 +17,16 @@ namespace ESMS { virtual void load(ESMReader &esm, const std::string &id) = 0; virtual int getSize() = 0; - + static std::string toLower (const std::string& name) { std::string lowerCase; - + std::transform (name.begin(), name.end(), std::back_inserter (lowerCase), (int(*)(int)) std::tolower); - + return lowerCase; - } + } }; typedef std::map RecListList; @@ -53,15 +53,53 @@ namespace ESMS return NULL; return &list.find(id2)->second; } - + + // Find the given object ID (throws an exception if not found) + const X* find(const std::string &id) const + { + const X *object = search (id); + + if (!object) + throw std::runtime_error ("object " + id + " not found"); + + return object; + } + + int getSize() { return list.size(); } + }; + + /// Modified version of RecListT for records, that need to store their own ID + template + struct RecListWithIDT : RecList + { + typedef std::map MapType; + + MapType list; + + // Load one object of this type + void load(ESMReader &esm, const std::string &id) + { + std::string id2 = toLower (id); + list[id2].load(esm, id2); + } + + // Find the given object ID, or return NULL if not found. + const X* search(const std::string &id) const + { + std::string id2 = toLower (id); + if(list.find(id2) == list.end()) + return NULL; + return &list.find(id2)->second; + } + // Find the given object ID (throws an exception if not found) const X* find(const std::string &id) const { const X *object = search (id); - + if (!object) throw std::runtime_error ("object " + id + " not found"); - + return object; } @@ -80,7 +118,7 @@ namespace ESMS void load(ESMReader &esm, const std::string &id) { - std::string id2 = toLower (id); + std::string id2 = toLower (id); X& ref = list[id2]; ref.id = id; @@ -95,18 +133,18 @@ namespace ESMS return NULL; return &list.find(id2)->second; } - + // Find the given object ID (throws an exception if not found) const X* find(const std::string &id) const { const X *object = search (id); - + if (!object) throw std::runtime_error ("object " + id + " not found"); - + return object; } - + int getSize() { return list.size(); } }; @@ -180,7 +218,7 @@ namespace ESMS } } }; - + template struct ScriptListT : RecList { @@ -193,16 +231,16 @@ namespace ESMS { X ref; ref.load (esm); - + std::string realId = toLower (ref.data.name.toString()); - + std::swap (list[realId], ref); } // Find the given object ID, or return NULL if not found. const X* search(const std::string &id) const { - std::string id2 = toLower (id); + std::string id2 = toLower (id); if(list.find(id2) == list.end()) return NULL; return &list.find(id2)->second; @@ -212,15 +250,15 @@ namespace ESMS const X* find(const std::string &id) const { const X *object = search (id); - + if (!object) throw std::runtime_error ("object " + id + " not found"); - + return object; } int getSize() { return list.size(); } - }; + }; /* We need special lists for: diff --git a/components/esm_store/store.hpp b/components/esm_store/store.hpp index fd4a47d4b8..6138014efa 100644 --- a/components/esm_store/store.hpp +++ b/components/esm_store/store.hpp @@ -40,7 +40,7 @@ namespace ESMS RecListT clothes; RecListT contChange; RecListT containers; - RecListT creatures; + RecListWithIDT creatures; RecListT creaChange; RecListT dialogs; RecListT doors; @@ -53,7 +53,7 @@ namespace ESMS RecListT lights; RecListT lockpicks; RecListT miscItems; - RecListT npcs; + RecListWithIDT npcs; RecListT npcChange; RecListT probes; RecListT races;