modified Npc and Creature class to let them store a copy of their own ID

pull/7/head
Marc Zinnschlag 15 years ago
parent 7afc8ffd2e
commit 8d85211fa1

@ -180,7 +180,7 @@ int main(int argc, char**argv)
case REC_CREA: case REC_CREA:
{ {
Creature b; Creature b;
b.load(esm); b.load(esm, id);
if(quiet) break; if(quiet) break;
cout << " Name: " << b.name << endl; cout << " Name: " << b.name << endl;
break; break;

@ -36,6 +36,23 @@ namespace MWWorld
return false; return false;
} }
template<typename T>
bool create (const ESMS::RecListWithIDT<T>& list, const std::string& name)
{
if (const T *instance = list.search (name))
{
ESMS::LiveCellRef<T, RefData> ref;
ref.base = instance;
mRef = ref;
mPtr = Ptr (&boost::any_cast<ESMS::LiveCellRef<T, RefData>&> (mRef), 0);
return true;
}
return false;
}
public: public:
ManualRef (const ESMS::ESMStore& store, const std::string& name) ManualRef (const ESMS::ESMStore& store, const std::string& name)

@ -62,8 +62,12 @@ struct Creature
// Defined in loadcont.hpp // Defined in loadcont.hpp
InventoryList inventory; InventoryList inventory;
void load(ESMReader &esm) std::string mId;
void load(ESMReader &esm, const std::string& id)
{ {
mId = id;
model = esm.getHNString("MODL"); model = esm.getHNString("MODL");
original = esm.getHNOString("CNAM"); original = esm.getHNOString("CNAM");
name = esm.getHNOString("FNAM"); name = esm.getHNOString("FNAM");
@ -93,7 +97,7 @@ struct Creature
// AI_A - activate? // AI_A - activate?
esm.skipRecord(); esm.skipRecord();
} }
}; };
} }
#endif #endif

@ -97,8 +97,12 @@ struct NPC
std::string name, model, race, cls, faction, script, std::string name, model, race, cls, faction, script,
hair, head; // body parts hair, head; // body parts
void load(ESMReader &esm) std::string mId;
void load(ESMReader &esm, const std::string& id)
{ {
mId = id;
npdt52.gold = -10; npdt52.gold = -10;
model = esm.getHNOString("MODL"); model = esm.getHNOString("MODL");

@ -17,16 +17,16 @@ namespace ESMS
{ {
virtual void load(ESMReader &esm, const std::string &id) = 0; virtual void load(ESMReader &esm, const std::string &id) = 0;
virtual int getSize() = 0; virtual int getSize() = 0;
static std::string toLower (const std::string& name) static std::string toLower (const std::string& name)
{ {
std::string lowerCase; std::string lowerCase;
std::transform (name.begin(), name.end(), std::back_inserter (lowerCase), std::transform (name.begin(), name.end(), std::back_inserter (lowerCase),
(int(*)(int)) std::tolower); (int(*)(int)) std::tolower);
return lowerCase; return lowerCase;
} }
}; };
typedef std::map<int,RecList*> RecListList; typedef std::map<int,RecList*> RecListList;
@ -53,15 +53,53 @@ namespace ESMS
return NULL; return NULL;
return &list.find(id2)->second; 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 <typename X>
struct RecListWithIDT : RecList
{
typedef std::map<std::string,X> 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) // Find the given object ID (throws an exception if not found)
const X* find(const std::string &id) const const X* find(const std::string &id) const
{ {
const X *object = search (id); const X *object = search (id);
if (!object) if (!object)
throw std::runtime_error ("object " + id + " not found"); throw std::runtime_error ("object " + id + " not found");
return object; return object;
} }
@ -80,7 +118,7 @@ namespace ESMS
void load(ESMReader &esm, const std::string &id) void load(ESMReader &esm, const std::string &id)
{ {
std::string id2 = toLower (id); std::string id2 = toLower (id);
X& ref = list[id2]; X& ref = list[id2];
ref.id = id; ref.id = id;
@ -95,18 +133,18 @@ namespace ESMS
return NULL; return NULL;
return &list.find(id2)->second; return &list.find(id2)->second;
} }
// Find the given object ID (throws an exception if not found) // Find the given object ID (throws an exception if not found)
const X* find(const std::string &id) const const X* find(const std::string &id) const
{ {
const X *object = search (id); const X *object = search (id);
if (!object) if (!object)
throw std::runtime_error ("object " + id + " not found"); throw std::runtime_error ("object " + id + " not found");
return object; return object;
} }
int getSize() { return list.size(); } int getSize() { return list.size(); }
}; };
@ -180,7 +218,7 @@ namespace ESMS
} }
} }
}; };
template <typename X> template <typename X>
struct ScriptListT : RecList struct ScriptListT : RecList
{ {
@ -193,16 +231,16 @@ namespace ESMS
{ {
X ref; X ref;
ref.load (esm); ref.load (esm);
std::string realId = toLower (ref.data.name.toString()); std::string realId = toLower (ref.data.name.toString());
std::swap (list[realId], ref); std::swap (list[realId], ref);
} }
// Find the given object ID, or return NULL if not found. // Find the given object ID, or return NULL if not found.
const X* search(const std::string &id) const const X* search(const std::string &id) const
{ {
std::string id2 = toLower (id); std::string id2 = toLower (id);
if(list.find(id2) == list.end()) if(list.find(id2) == list.end())
return NULL; return NULL;
return &list.find(id2)->second; return &list.find(id2)->second;
@ -212,15 +250,15 @@ namespace ESMS
const X* find(const std::string &id) const const X* find(const std::string &id) const
{ {
const X *object = search (id); const X *object = search (id);
if (!object) if (!object)
throw std::runtime_error ("object " + id + " not found"); throw std::runtime_error ("object " + id + " not found");
return object; return object;
} }
int getSize() { return list.size(); } int getSize() { return list.size(); }
}; };
/* We need special lists for: /* We need special lists for:

@ -40,7 +40,7 @@ namespace ESMS
RecListT<Clothing> clothes; RecListT<Clothing> clothes;
RecListT<LoadCNTC> contChange; RecListT<LoadCNTC> contChange;
RecListT<Container> containers; RecListT<Container> containers;
RecListT<Creature> creatures; RecListWithIDT<Creature> creatures;
RecListT<LoadCREC> creaChange; RecListT<LoadCREC> creaChange;
RecListT<Dialogue> dialogs; RecListT<Dialogue> dialogs;
RecListT<Door> doors; RecListT<Door> doors;
@ -53,7 +53,7 @@ namespace ESMS
RecListT<Light> lights; RecListT<Light> lights;
RecListT<Tool> lockpicks; RecListT<Tool> lockpicks;
RecListT<Misc> miscItems; RecListT<Misc> miscItems;
RecListT<NPC> npcs; RecListWithIDT<NPC> npcs;
RecListT<LoadNPCC> npcChange; RecListT<LoadNPCC> npcChange;
RecListT<Probe> probes; RecListT<Probe> probes;
RecListT<Race> races; RecListT<Race> races;

Loading…
Cancel
Save