Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Random Count #20

Merged
merged 5 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
22 changes: 11 additions & 11 deletions SPID/include/Distribute.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ namespace Distribute
// for now, only packages/death items use this
template <class Form>
void for_each_form(
const NPCData& a_npcData,
Forms::Distributables<Form>& a_distributables,
const PCLevelMult::Input& a_input,
std::function<bool(Form*, IdxOrCount)> a_callback)
const NPCData& a_npcData,
Forms::Distributables<Form>& a_distributables,
const PCLevelMult::Input& a_input,
std::function<bool(Form*, IndexOrCount)> a_callback)
{
auto& vec = a_distributables.GetForms(a_input.onlyPlayerLevelEntries);

Expand Down Expand Up @@ -131,26 +131,26 @@ 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>&, bool)> a_callback)
const NPCData& a_npcData,
Forms::Distributables<Form>& a_distributables,
const PCLevelMult::Input& a_input,
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, std::get<RandomCount>(formData.idxOrCount).GetRandom());
++formData.npcCount;
}
}
Expand Down
6 changes: 3 additions & 3 deletions SPID/include/FormData.h
Original file line number Diff line number Diff line change
Expand Up @@ -307,9 +307,9 @@ namespace Forms
{
std::uint32_t index{ 0 };

Form* form{ nullptr };
IdxOrCount idxOrCount{ 1 };
FilterData filters{};
Form* form{ nullptr };
IndexOrCount idxOrCount{ RandomCount(1, 1) };
FilterData filters{};

std::string path{};
std::uint32_t npcCount{ 0 };
Expand Down
2 changes: 1 addition & 1 deletion SPID/include/LookupConfigs.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ namespace INI
Filters<FormOrEditorID> rawFormFilters{};
LevelFilters levelFilters{};
Traits traits{};
IdxOrCount idxOrCount{ 1 };
IndexOrCount idxOrCount{ RandomCount(1, 1) };
Chance chance{ 100 };
std::string path{};
};
Expand Down
6 changes: 3 additions & 3 deletions SPID/src/Distribute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ 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) {
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 +172,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
6 changes: 4 additions & 2 deletions SPID/src/DistributeManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,10 @@ 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) {
auto count = std::get<RandomCount>(idxOrCount);

detail::add_item(actor, deathItem, count.GetRandom());
return true;
});
}
Expand Down
20 changes: 18 additions & 2 deletions SPID/src/LookupConfigs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,25 @@ namespace INI
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") { // If it's a package, then we only expect a single number.
if (const auto& str = sections[kIdxOrCount]; distribution::is_valid_entry(str)) {
data.idxOrCount = 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.idxOrCount = RandomCount(minCount, maxCount);
} else {
auto count = string::to_num<Count>(str);

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

Expand Down
Loading