Skip to content

Commit

Permalink
Added support for random count value.
Browse files Browse the repository at this point in the history
Count can now be specified in form of range, e.g. 5/20 to produce anywhere between 5 and 20 items.
  • Loading branch information
adya committed Mar 14, 2024
1 parent 1e407a1 commit 1bf640d
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 25 deletions.
15 changes: 14 additions & 1 deletion SPID/include/Defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ struct Range
return value >= min && value <= max;
}

[[nodiscard]] bool IsExact() const
{
return min == max;
}

[[nodiscard]] T GetRandom() const
{
return IsExact() ? min : RNG().generate<T>(min, max);
}

// members
T min{ std::numeric_limits<T>::min() };
T max{ std::numeric_limits<T>::max() };
Expand Down Expand Up @@ -83,7 +93,10 @@ struct Traits
std::optional<bool> teammate{};
};

using IdxOrCount = std::int32_t;
using Index = std::int32_t;
using Count = std::int32_t;
using RandomCount = Range<Count>;
using IndexOrCount = std::variant<Index, RandomCount>;
using Chance = std::uint32_t;

/// A standardized way of converting any object to string.
Expand Down
21 changes: 14 additions & 7 deletions SPID/include/Distribute.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,21 @@ namespace Distribute
const NPCData& a_npcData,
Forms::Distributables<Form>& a_distributables,
const PCLevelMult::Input& a_input,
std::function<bool(Form*, IdxOrCount)> a_callback)
std::function<bool(Form*, IndexOrCount)> a_callback)
{
auto& vec = a_distributables.GetForms(a_input.onlyPlayerLevelEntries);

for (auto& formData : vec) {
if (!a_npcData.HasMutuallyExclusiveForm(formData.form) && detail::passed_filters(a_npcData, a_input, formData)) {
a_callback(formData.form, formData.idxOrCount);
++formData.npcCount;
if constexpr (std::is_same_v<RE::TESBoundObject, Form>) {
if (a_callback(formData.form, formData.count.GetRandom())) {
++formData.npcCount;
}
} else {
if (a_callback(formData.form, formData.packageIndex)) {
++formData.npcCount;
}
}
}
}
}
Expand Down Expand Up @@ -134,23 +141,23 @@ namespace Distribute
const NPCData& a_npcData,
Forms::Distributables<Form>& a_distributables,
const PCLevelMult::Input& a_input,
std::function<bool(std::map<Form*, IdxOrCount>&, bool)> a_callback)
std::function<bool(std::map<Form*, Count>&, bool)> a_callback)
{
auto& vec = a_distributables.GetForms(a_input.onlyPlayerLevelEntries);

if (vec.empty()) {
return;
}

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

for (auto& formData : vec) {
if (!a_npcData.HasMutuallyExclusiveForm(formData.form) && detail::passed_filters(a_npcData, a_input, formData)) {
if (formData.form->Is(RE::FormType::LeveledItem)) {
hasLeveledItems = true;
}
collectedForms.emplace(formData.form, formData.idxOrCount);
collectedForms.emplace(formData.form, formData.count.GetRandom());
++formData.npcCount;
}
}
Expand Down
11 changes: 6 additions & 5 deletions SPID/include/FormData.h
Original file line number Diff line number Diff line change
Expand Up @@ -307,9 +307,10 @@ namespace Forms
{
std::uint32_t index{ 0 };

Form* form{ nullptr };
IdxOrCount idxOrCount{ 1 };
FilterData filters{};
Form* form{ nullptr };
Index packageIndex{ 0 };
RandomCount count{ 1, 1 };
FilterData filters{};

std::string path{};
std::uint32_t npcCount{ 0 };
Expand Down Expand Up @@ -442,7 +443,7 @@ void Forms::Distributables<Form>::LookupForms(RE::TESDataHandler* a_dataHandler,
forms.reserve(a_INIDataVec.size());
std::uint32_t index = 0;

for (auto& [formOrEditorID, strings, filterIDs, level, traits, idxOrCount, chance, path] : a_INIDataVec) {
for (auto& [formOrEditorID, strings, filterIDs, level, traits, packageIndex, itemsCount, chance, path] : a_INIDataVec) {
try {
if (auto form = detail::get_form<Form>(a_dataHandler, formOrEditorID, path); form) {
FormFilters filterForms{};
Expand All @@ -456,7 +457,7 @@ void Forms::Distributables<Form>::LookupForms(RE::TESDataHandler* a_dataHandler,
}

if (validEntry) {
forms.emplace_back(index, form, idxOrCount, FilterData(strings, filterForms, level, traits, chance), path);
forms.emplace_back(index, form, packageIndex, itemsCount, FilterData(strings, filterForms, level, traits, chance), path);
index++;
}
}
Expand Down
3 changes: 2 additions & 1 deletion SPID/include/LookupConfigs.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ namespace INI
Filters<FormOrEditorID> rawFormFilters{};
LevelFilters levelFilters{};
Traits traits{};
IdxOrCount idxOrCount{ 1 };
Index index{ 0 };
RandomCount count{ 1, 1 };
Chance chance{ 100 };
std::string path{};
};
Expand Down
9 changes: 6 additions & 3 deletions SPID/src/Distribute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,11 @@ namespace Distribute
npc->GetSpellList()->AddShouts(a_shouts);
});

for_each_form<RE::TESForm>(a_npcData, Forms::packages, a_input, [&](auto* a_packageOrList, [[maybe_unused]] IdxOrCount a_idx) {
auto packageIdx = a_idx;
for_each_form<RE::TESForm>(a_npcData, Forms::packages, a_input, [&](auto* a_packageOrList, [[maybe_unused]] IndexOrCount a_idx) {
if (!std::holds_alternative<Index>(a_idx)) {
return false;
}
auto packageIdx = std::get<Index>(a_idx);

if (a_packageOrList->Is(RE::FormType::Package)) {
auto package = a_packageOrList->As<RE::TESPackage>();
Expand Down Expand Up @@ -172,7 +175,7 @@ namespace Distribute
const auto npc = a_npcData.GetNPC();
const auto actor = a_npcData.GetActor();

for_each_form<RE::TESBoundObject>(a_npcData, Forms::items, a_input, [&](std::map<RE::TESBoundObject*, IdxOrCount>& a_objects, const bool a_hasLvlItem) {
for_each_form<RE::TESBoundObject>(a_npcData, Forms::items, a_input, [&](std::map<RE::TESBoundObject*, Count>& a_objects, const bool a_hasLvlItem) {
if (npc->AddObjectsToContainer(a_objects, npc)) {
if (a_hasLvlItem) {
detail::init_leveled_items(actor);
Expand Down
10 changes: 8 additions & 2 deletions SPID/src/DistributeManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,14 @@ namespace Distribute::Event
const auto npcData = NPCData(actor, npc);
const auto input = PCLevelMult::Input{ actor, npc, false };

for_each_form<RE::TESBoundObject>(npcData, Forms::deathItems, input, [&](auto* a_deathItem, IdxOrCount a_count) {
detail::add_item(actor, a_deathItem, a_count);
for_each_form<RE::TESBoundObject>(npcData, Forms::deathItems, input, [&](auto* deathItem, IndexOrCount idxOrCount) {
if (!std::holds_alternative<RandomCount>(idxOrCount)) {
return false;
}

auto count = std::get<RandomCount>(idxOrCount);

detail::add_item(actor, deathItem, count.GetRandom());
return true;
});
}
Expand Down
25 changes: 19 additions & 6 deletions SPID/src/LookupConfigs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,15 +246,28 @@ namespace INI
}

//ITEMCOUNT/INDEX
if (a_key == "Package") { // reuse item count for package stack index
data.idxOrCount = 0;
}

if (kIdxOrCount < size) {
if (const auto& str = sections[kIdxOrCount]; distribution::is_valid_entry(str)) {
data.idxOrCount = string::to_num<std::int32_t>(str);
if (a_key == "Package") { // reuse item count for package stack index
if (const auto& str = sections[kIdxOrCount]; distribution::is_valid_entry(str)) {
data.index = string::to_num<Index>(str);
}
} else {
if (const auto& str = sections[kIdxOrCount]; distribution::is_valid_entry(str)) {
if (auto countPair = string::split(str, "/"); countPair.size() > 1) {
auto minCount = string::to_num<Count>(countPair[0]);
auto maxCount = string::to_num<Count>(countPair[1]);

data.count = RandomCount(minCount, maxCount);
} else {
auto count = string::to_num<Count>(str);

data.count = RandomCount(count, count); // create the exact match range.
}
}
}
}

//CHANCE
if (kChance < size) {
if (const auto& str = sections[kChance]; distribution::is_valid_entry(str)) {
Expand Down

0 comments on commit 1bf640d

Please sign in to comment.