diff --git a/CHANGELOG.md b/CHANGELOG.md index 2cbc553151..ae1d9c5e2b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -120,6 +120,7 @@ Bug #6730: LoopGroup stalls animation after playing :Stop frame until another animation is played Bug #6753: Info records without a DATA subrecords are loaded incorrectly Bug #6794: Light sources are attached to mesh bounds centers instead of mesh origins when AttachLight NiNode is missing + Bug #6799: Game crashes if an NPC has no Class attached Feature #890: OpenMW-CS: Column filtering Feature #1465: "Reset" argument for AI functions Feature #2491: Ability to make OpenMW "portable" diff --git a/apps/openmw/mwworld/esmstore.cpp b/apps/openmw/mwworld/esmstore.cpp index bd93cc7c4e..690d1d3a63 100644 --- a/apps/openmw/mwworld/esmstore.cpp +++ b/apps/openmw/mwworld/esmstore.cpp @@ -59,15 +59,18 @@ namespace } } - std::vector getNPCsToReplace(const MWWorld::Store& factions, const MWWorld::Store& classes, const std::unordered_map& npcs) + const std::string& getDefaultClass(const MWWorld::Store& classes) { - // Cache first class from store - we will use it if current class is not found - std::string defaultCls; auto it = classes.begin(); if (it != classes.end()) - defaultCls = it->mId; - else - throw std::runtime_error("List of NPC classes is empty!"); + return it->mId; + throw std::runtime_error("List of NPC classes is empty!"); + } + + std::vector getNPCsToReplace(const MWWorld::Store& factions, const MWWorld::Store& classes, const std::unordered_map& npcs) + { + // Cache first class from store - we will use it if current class is not found + const std::string& defaultCls = getDefaultClass(classes); // Validate NPCs for non-existing class and faction. // We will replace invalid entries by fixed ones @@ -78,7 +81,7 @@ namespace ESM::NPC npc = npcIter.second; bool changed = false; - const std::string npcFaction = npc.mFaction; + const std::string& npcFaction = npc.mFaction; if (!npcFaction.empty()) { const ESM::Faction *fact = factions.search(npcFaction); @@ -91,16 +94,13 @@ namespace } } - std::string npcClass = npc.mClass; - if (!npcClass.empty()) + const std::string& npcClass = npc.mClass; + const ESM::Class *cls = classes.search(npcClass); + if (!cls) { - const ESM::Class *cls = classes.search(npcClass); - if (!cls) - { - Log(Debug::Verbose) << "NPC '" << npc.mId << "' (" << npc.mName << ") has nonexistent class '" << npc.mClass << "', using '" << defaultCls << "' class as replacement."; - npc.mClass = defaultCls; - changed = true; - } + Log(Debug::Verbose) << "NPC '" << npc.mId << "' (" << npc.mName << ") has nonexistent class '" << npc.mClass << "', using '" << defaultCls << "' class as replacement."; + npc.mClass = defaultCls; + changed = true; } if (changed)