From aba63c0145ddb363a62326948c696e7699dcf9e2 Mon Sep 17 00:00:00 2001 From: Zackhasacat Date: Fri, 1 Sep 2023 16:44:58 -0500 Subject: [PATCH] Shorten, move to the correct place --- apps/openmw/mwlua/types/creature.cpp | 55 +++++++-------------- apps/openmw/mwlua/types/npc.cpp | 71 +++++++++------------------- 2 files changed, 39 insertions(+), 87 deletions(-) diff --git a/apps/openmw/mwlua/types/creature.cpp b/apps/openmw/mwlua/types/creature.cpp index 484de9d969..8e8e875745 100644 --- a/apps/openmw/mwlua/types/creature.cpp +++ b/apps/openmw/mwlua/types/creature.cpp @@ -51,48 +51,25 @@ namespace MWLua record["baseGold"] = sol::readonly_property([](const ESM::Creature& rec) -> int { return rec.mData.mGold; }); record["servicesOffered"] = sol::readonly_property([](const ESM::Creature& rec) { std::vector providedServices; + std::map serviceNames = { { ESM::NPC::Spells, "Spells" }, + { ESM::NPC::Spellmaking, "Spellmaking" }, { ESM::NPC::Enchanting, "Enchanting" }, + { ESM::NPC::Training, "Training" }, { ESM::NPC::Repair, "Repair" }, { ESM::NPC::AllItems, "Barter" }, + { ESM::NPC::Weapon, "Weapon" }, { ESM::NPC::Armor, "Armor" }, { ESM::NPC::Clothing, "Clothing" }, + { ESM::NPC::Books, "Books" }, { ESM::NPC::Ingredients, "Ingredients" }, { ESM::NPC::Picks, "Picks" }, + { ESM::NPC::Probes, "Probes" }, { ESM::NPC::Lights, "Lights" }, { ESM::NPC::Apparatus, "Apparatus" }, + { ESM::NPC::RepairItem, "RepairItem" }, { ESM::NPC::Misc, "Misc" }, { ESM::NPC::Potions, "Potions" }, + { ESM::NPC::MagicItems, "MagicItems" } }; int mServices = rec.mAiData.mServices; - if (mServices & ESM::NPC::Spells) - providedServices.push_back("Spells"); - if (mServices & ESM::NPC::Spellmaking) - providedServices.push_back("Spellmaking"); - if (mServices & ESM::NPC::Enchanting) - providedServices.push_back("Enchanting"); - if (mServices & ESM::NPC::Repair) - providedServices.push_back("Repair"); - if (mServices & ESM::NPC::AllItems) - providedServices.push_back("Barter"); - - if (mServices & ESM::NPC::Weapon) - providedServices.push_back("Weapon"); - if (mServices & ESM::NPC::Armor) - providedServices.push_back("Armor"); - if (mServices & ESM::NPC::Clothing) - providedServices.push_back("Clothing"); - if (mServices & ESM::NPC::Books) - providedServices.push_back("Books"); - if (mServices & ESM::NPC::Ingredients) - providedServices.push_back("Ingredients"); - if (mServices & ESM::NPC::Picks) - providedServices.push_back("Picks"); - if (mServices & ESM::NPC::Probes) - providedServices.push_back("Probes"); - if (mServices & ESM::NPC::Lights) - providedServices.push_back("Lights"); - if (mServices & ESM::NPC::Apparatus) - providedServices.push_back("Apparatus"); - if (mServices & ESM::NPC::RepairItem) - providedServices.push_back("RepairItem"); - if (mServices & ESM::NPC::Misc) - providedServices.push_back("Misc"); - if (mServices & ESM::NPC::Potions) - providedServices.push_back("Potions"); - if (mServices & ESM::NPC::MagicItems) - providedServices.push_back("MagicItems"); - if (rec.getTransport().size() > 0) + for (const auto& entry : serviceNames) + { + if (mServices & entry.first) + { + providedServices.push_back(entry.second); + } + } + if (!rec.getTransport().empty()) providedServices.push_back("Travel"); - return providedServices; }); } diff --git a/apps/openmw/mwlua/types/npc.cpp b/apps/openmw/mwlua/types/npc.cpp index 6d7b62b222..5b24005e16 100644 --- a/apps/openmw/mwlua/types/npc.cpp +++ b/apps/openmw/mwlua/types/npc.cpp @@ -50,6 +50,29 @@ namespace MWLua record["isMale"] = sol::readonly_property([](const ESM::NPC& rec) -> bool { return rec.isMale(); }); record["baseGold"] = sol::readonly_property([](const ESM::NPC& rec) -> int { return rec.mNpdt.mGold; }); + record["servicesOffered"] = sol::readonly_property([](const ESM::NPC& rec) { + std::vector providedServices; + std::map serviceNames = { { ESM::NPC::Spells, "Spells" }, + { ESM::NPC::Spellmaking, "Spellmaking" }, { ESM::NPC::Enchanting, "Enchanting" }, + { ESM::NPC::Training, "Training" }, { ESM::NPC::Repair, "Repair" }, { ESM::NPC::AllItems, "Barter" }, + { ESM::NPC::Weapon, "Weapon" }, { ESM::NPC::Armor, "Armor" }, { ESM::NPC::Clothing, "Clothing" }, + { ESM::NPC::Books, "Books" }, { ESM::NPC::Ingredients, "Ingredients" }, { ESM::NPC::Picks, "Picks" }, + { ESM::NPC::Probes, "Probes" }, { ESM::NPC::Lights, "Lights" }, { ESM::NPC::Apparatus, "Apparatus" }, + { ESM::NPC::RepairItem, "RepairItem" }, { ESM::NPC::Misc, "Misc" }, { ESM::NPC::Potions, "Potions" }, + { ESM::NPC::MagicItems, "MagicItems" } }; + + int mServices = rec.mAiData.mServices; + for (const auto& entry : serviceNames) + { + if (mServices & entry.first) + { + providedServices.push_back(entry.second); + } + } + if (!rec.getTransport().empty()) + providedServices.push_back("Travel"); + return providedServices; + }); // This function is game-specific, in future we should replace it with something more universal. npc["isWerewolf"] = [](const Object& o) { const MWWorld::Class& cls = o.ptr().getClass(); @@ -58,54 +81,6 @@ namespace MWLua else throw std::runtime_error("NPC or Player expected"); }; - record["servicesOffered"] = sol::readonly_property([](const ESM::NPC& rec) { - std::vector providedServices; - - int mServices = rec.mAiData.mServices; - if (mServices & ESM::NPC::Spells) - providedServices.push_back("Spells"); - if (mServices & ESM::NPC::Spellmaking) - providedServices.push_back("Spellmaking"); - if (mServices & ESM::NPC::Enchanting) - providedServices.push_back("Enchanting"); - if (mServices & ESM::NPC::Training) - providedServices.push_back("Training"); - if (mServices & ESM::NPC::Repair) - providedServices.push_back("Repair"); - if (mServices & ESM::NPC::AllItems) - providedServices.push_back("Barter"); - - if (mServices & ESM::NPC::Weapon) - providedServices.push_back("Weapon"); - if (mServices & ESM::NPC::Armor) - providedServices.push_back("Armor"); - if (mServices & ESM::NPC::Clothing) - providedServices.push_back("Clothing"); - if (mServices & ESM::NPC::Books) - providedServices.push_back("Books"); - if (mServices & ESM::NPC::Ingredients) - providedServices.push_back("Ingredients"); - if (mServices & ESM::NPC::Picks) - providedServices.push_back("Picks"); - if (mServices & ESM::NPC::Probes) - providedServices.push_back("Probes"); - if (mServices & ESM::NPC::Lights) - providedServices.push_back("Lights"); - if (mServices & ESM::NPC::Apparatus) - providedServices.push_back("Apparatus"); - if (mServices & ESM::NPC::RepairItem) - providedServices.push_back("RepairItem"); - if (mServices & ESM::NPC::Misc) - providedServices.push_back("Misc"); - if (mServices & ESM::NPC::Potions) - providedServices.push_back("Potions"); - if (mServices & ESM::NPC::MagicItems) - providedServices.push_back("MagicItems"); - if (rec.getTransport().size() > 0) - providedServices.push_back("Travel"); - - return providedServices; - }); npc["getDisposition"] = [](const Object& o, const Object& player) -> int { if (player.ptr() != MWBase::Environment::get().getWorld()->getPlayerPtr())