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

Fractional Chances #30

Merged
merged 3 commits into from
Mar 30, 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
18 changes: 17 additions & 1 deletion SPID/include/Defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,23 @@ 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;

/// <summary>
/// A chance that is represented as a decimal value between 0 and 1.
/// For example, 0.5 would be 50%.
///
/// This one is used in a processed Data for filtering.
/// </summary>
using DecimalChance = double;

/// <summary>
/// A chance that is represented as a percent value between 0 and 100.
/// It also can be decimal, but would describe fraction of a percent.
/// So that 0.5 would be 0.5%.
///
/// This is used during parsing of INI files.
/// </summary>
using PercentChance = double;

/// A standardized way of converting any object to string.
///
Expand Down
8 changes: 4 additions & 4 deletions SPID/include/LinkedDistribution.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ namespace LinkedDistribution
/// Raw filters in RawLinkedForm only use MATCH, there is no meaning for ALL or NOT, so they are ignored.
Filters<FormOrEditorID> formIDs{};

IndexOrCount idxOrCount{ RandomCount(1, 1) };
Chance chance{ 100 };
IndexOrCount idxOrCount{ RandomCount(1, 1) };
PercentChance chance{ 100 };

std::string path{};
};
Expand Down Expand Up @@ -66,7 +66,7 @@ namespace LinkedDistribution
RECORD::TYPE type;
FormsMap forms{};

void Link(Form* form, const FormVec& linkedForms, const IndexOrCount& idxOrCount, const Chance& chance, const std::string& path);
void Link(Form* form, const FormVec& linkedForms, const IndexOrCount& idxOrCount, const PercentChance& chance, const std::string& path);
};

class Manager : public ISingleton<Manager>
Expand Down Expand Up @@ -215,7 +215,7 @@ namespace LinkedDistribution
}

template <class Form>
void LinkedForms<Form>::Link(Form* form, const FormVec& linkedForms, const IndexOrCount& idxOrCount, const Chance& chance, const std::string& path)
void LinkedForms<Form>::Link(Form* form, const FormVec& linkedForms, const IndexOrCount& idxOrCount, const PercentChance& chance, const std::string& path)
{
for (const auto& linkedForm : linkedForms) {
if (std::holds_alternative<RE::TESForm*>(linkedForm)) {
Expand Down
2 changes: 1 addition & 1 deletion SPID/include/LookupConfigs.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ namespace INI
LevelFilters levelFilters{};
Traits traits{};
IndexOrCount idxOrCount{ RandomCount(1, 1) };
Chance chance{ 100 };
PercentChance chance{ 100 };
std::string path{};
};

Expand Down
5 changes: 3 additions & 2 deletions SPID/include/LookupFilters.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ namespace Filter

struct Data
{
Data(StringFilters a_strings, FormFilters a_formFilters, LevelFilters a_level, Traits a_traits, Chance a_chance);
// Note that chance passed to this constructor is expected to be in percent. It will be converted to a decimal chance by the constructor.
Data(StringFilters a_strings, FormFilters a_formFilters, LevelFilters a_level, Traits a_traits, PercentChance a_chance);

StringFilters strings{};
FormFilters forms{};
LevelFilters levels{};
Traits traits{};
Chance chance{ 100 };
DecimalChance chance{ 1 };

bool hasLeveledFilters;

Expand Down
2 changes: 1 addition & 1 deletion SPID/src/LinkedDistribution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ namespace LinkedDistribution

if (kChance < size) {
if (const auto& str = sections[kChance]; distribution::is_valid_entry(str)) {
item.chance = string::to_num<Chance>(str);
item.chance = string::to_num<PercentChance>(str);
}
}

Expand Down
2 changes: 1 addition & 1 deletion SPID/src/LookupConfigs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ namespace INI
//CHANCE
if (kChance < size) {
if (const auto& str = sections[kChance]; distribution::is_valid_entry(str)) {
data.chance = string::to_num<Chance>(str);
data.chance = string::to_num<PercentChance>(str);
}
}

Expand Down
8 changes: 4 additions & 4 deletions SPID/src/LookupFilters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

namespace Filter
{
Data::Data(StringFilters a_strings, FormFilters a_formFilters, LevelFilters a_level, Traits a_traits, Chance a_chance) :
Data::Data(StringFilters a_strings, FormFilters a_formFilters, LevelFilters a_level, Traits a_traits, PercentChance a_chance) :
strings(std::move(a_strings)),
forms(std::move(a_formFilters)),
levels(std::move(a_level)),
traits(a_traits),
chance(a_chance)
chance(a_chance / 100)
{
hasLeveledFilters = HasLevelFiltersImpl();
}
Expand Down Expand Up @@ -192,8 +192,8 @@ namespace Filter
Result Data::PassedFilters(const NPCData& a_npcData) const
{
// Fail chance first to avoid running unnecessary checks
if (chance < 100) {
const auto randNum = RNG().generate<Chance>(0, 100);
if (chance < 1) {
const auto randNum = RNG().generate();
if (randNum > chance) {
return Result::kFailRNG;
}
Expand Down
4 changes: 2 additions & 2 deletions cmake/ports/clib-util/portfile.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
vcpkg_from_github(
OUT_SOURCE_PATH SOURCE_PATH
REPO powerof3/CLibUtil
REF a491e2f7919211eac6d32dd2814b7908134ec1b2
SHA512 e6b830a061d7fc99115ee7d28de0165b8c6281006df6d053342e6b5b85606a46a846ebb30f3e4598aa976c25cfe7a9dffd1c4eb72942829d1a4615d81e62cf3c
REF 88d78d94464a04e582669beac56346edbbc4a662
SHA512 960cf62e5317356f7c0d994e49f56effb89c415377e9c865e801c5ec28b57e9ec0fd2a9fd54136cd2382addedb6745cd5cc062c46cab5cccb1f634999491c9e1
HEAD_REF master
)

Expand Down
Loading