Skip to content

Commit

Permalink
Fix outfits being removed during item distribution
Browse files Browse the repository at this point in the history
  • Loading branch information
powerof3 committed Apr 27, 2023
1 parent b9dd2b1 commit 4715bf9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
17 changes: 11 additions & 6 deletions SPID/include/Distribute.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ namespace Distribute

void equip_worn_outfit(RE::Actor* actor, const RE::BGSOutfit* a_outfit);
void add_item(RE::Actor* a_actor, RE::TESBoundObject* a_item, std::uint32_t a_itemCount);
void init_leveled_items(RE::Actor* a_actor);
}

// old method (distributing one by one)
Expand Down Expand Up @@ -105,10 +106,10 @@ namespace Distribute
// items
template <class Form>
void for_each_form(
const NPCData& a_npcData,
Forms::Distributables<Form>& a_distributables,
const PCLevelMult::Input& a_input,
std::function<bool(std::map<Form*, IdxOrCount>&)> a_callback)
const NPCData& a_npcData,
Forms::Distributables<Form>& a_distributables,
const PCLevelMult::Input& a_input,
std::function<bool(std::map<Form*, IdxOrCount>&, bool)> a_callback)
{
const auto& vec = a_distributables.GetForms(a_input.onlyPlayerLevelEntries);

Expand All @@ -117,15 +118,19 @@ namespace Distribute
}

std::map<Form*, IdxOrCount> collectedForms{};
bool hasLeveledItems = false;

for (auto& formData : vec) {
if (detail::passed_filters(a_npcData, a_input, formData)) {
collectedForms.emplace(formData.form, formData.idxOrCount);
if (formData.form->Is(RE::FormType::LeveledItem)) {
hasLeveledItems = true;
}
collectedForms.emplace(formData.form, formData.idxOrCount);
}
}

if (!collectedForms.empty()) {
a_callback(collectedForms);
a_callback(collectedForms, hasLeveledItems);
}
}

Expand Down
15 changes: 12 additions & 3 deletions SPID/src/Distribute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,14 @@ namespace Distribute
REL::Relocation<func_t> func{ RELOCATION_ID(55945, 56489) };
return func(a_actor, a_item, a_itemCount, true, 0, RE::BSScript::Internal::VirtualMachine::GetSingleton());
}
}

void init_leveled_items(RE::Actor* a_actor)
{
if (const auto invChanges = a_actor->GetInventoryChanges()) {
invChanges->InitLeveledItems();
}
}
}

void Distribute(NPCData& a_npcData, const PCLevelMult::Input& a_input)
{
Expand Down Expand Up @@ -73,9 +80,11 @@ namespace Distribute
npc->GetSpellList()->AddShouts(a_shouts);
});

for_each_form<RE::TESBoundObject>(a_npcData, Forms::items, a_input, [&](std::map<RE::TESBoundObject*, IdxOrCount>& a_objects) {
for_each_form<RE::TESBoundObject>(a_npcData, Forms::items, a_input, [&](std::map<RE::TESBoundObject*, IdxOrCount>& a_objects, const bool a_hasLvlItem) {
if (npc->AddObjectsToContainer(a_objects, npc)) {
actor->InitInventoryIfRequired();
if (a_hasLvlItem) {
detail::init_leveled_items(actor);
}
return true;
}
return false;
Expand Down

0 comments on commit 4715bf9

Please sign in to comment.