-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSwapData.cpp
More file actions
136 lines (116 loc) · 4.73 KB
/
SwapData.cpp
File metadata and controls
136 lines (116 loc) · 4.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include "SwapData.h"
namespace FormSwap
{
ObjectData::ObjectData(const Input& a_input) :
properties(a_input.properties),
chance(a_input.chance),
record(a_input.record),
path(a_input.path)
{
properties.SetChance(chance);
}
bool ObjectData::HasValidProperties(const RE::TESObjectREFR* a_ref) const
{
return chance.PassedChance(a_ref) && properties.IsValid();
}
void ObjectData::GetProperties(const std::string& a_path, const std::string& a_str, std::function<void(RE::FormID, ObjectData&)> a_func)
{
const auto formPair = string::split(a_str, "|");
if (const auto baseFormID = util::GetFormID(formPair[0]); baseFormID != 0) {
const Input input(
formPair[1], // transform
formPair.size() > 2 ? formPair[2] : std::string{}, // traits
a_str,
a_path);
ObjectData objectData(input);
a_func(baseFormID, objectData);
} else {
logger::error("\t\t\t\tfail : [{}] (BASE formID not found)", a_str);
}
}
SwapFormData::SwapFormData(FormIDOrSet a_id, const Input& a_input) :
ObjectData(a_input),
formIDSet(std::move(a_id))
{}
RE::TESBoundObject* SwapFormData::GetSwapBase(const RE::TESObjectREFR* a_ref) const
{
if (!chance.PassedChance(a_ref)) {
return nullptr;
}
if (const auto formID = std::get_if<RE::FormID>(&formIDSet); formID) {
return RE::TESForm::LookupByID<RE::TESBoundObject>(*formID);
} else { // return random element from set
auto& set = std::get<FormIDSet>(formIDSet);
const auto setEnd = std::distance(set.begin(), set.end()) - 1;
const auto randIt = BOS_RNG(chance, a_ref).generate<std::int64_t>(0, setEnd);
return RE::TESForm::LookupByID<RE::TESBoundObject>(*std::next(set.begin(), randIt));
}
}
void SwapFormData::GetForms(const std::string& a_path, const std::string& a_str, std::function<void(RE::FormID, SwapFormData&)> a_func)
{
constexpr auto swap_empty = [](const FormIDOrSet& a_set) {
if (const auto formID = std::get_if<RE::FormID>(&a_set); formID) {
return *formID == 0;
} else {
return std::get<FormIDSet>(a_set).empty();
}
};
constexpr auto base_same_as_swap = [](RE::FormID a_baseID, const FormIDOrSet& a_set) {
if (const auto formID = std::get_if<RE::FormID>(&a_set); formID) {
return *formID == a_baseID;
} else {
return false;
}
};
const auto formPair = string::split(a_str, "|");
if (const auto baseFormID = util::GetFormID(formPair[0]); baseFormID != 0) {
if (const auto swapFormID = util::GetSwapFormID(formPair[1]); !swap_empty(swapFormID)) {
auto properties = formPair.size() > 2 ? formPair[2] : std::string{};
auto chance = formPair.size() > 3 ? formPair[3] : std::string{};
if (base_same_as_swap(baseFormID, swapFormID) && !distribution::is_valid_entry(properties)) {
logger::error("\t\t\t\tfail : [{}] (BASE formID == SWAP formID)", a_str);
return;
}
const Input input(properties, chance, a_str, a_path);
SwapFormData swapFormData(swapFormID, input);
a_func(baseFormID, swapFormData);
} else {
logger::error("\t\t\t\tfail : [{}] (SWAP formID not found)", a_str);
}
} else if (const auto baseFormIDs = util::GetFormIDOrderedSet(formPair[0]); !baseFormIDs.empty()) {
if (auto swapFormIDs = util::GetFormIDOrderedSet(formPair[1]); !swapFormIDs.empty()) {
auto properties = formPair.size() > 2 ? formPair[2] : std::string{};
auto chance = formPair.size() > 3 ? formPair[3] : std::string{};
// assign each baseFormID the same swapFormID
if (swapFormIDs.size() == 1) {
auto swapFormID = *(swapFormIDs.begin());
for (auto itBaseFormID : baseFormIDs) {
const Input input(properties, chance, a_str, a_path);
SwapFormData swapFormData(swapFormID, input);
a_func(itBaseFormID, swapFormData);
}
// randomly assign each baseFormID to a unique swapFormID
} else if (swapFormIDs.size() >= baseFormIDs.size()) {
auto a_chance = Chance(chance);
auto a_rng = BOS_RNG(a_chance);
for (auto itBaseFormID : baseFormIDs) {
const auto setEnd = std::distance(swapFormIDs.begin(), swapFormIDs.end()) - 1;
const auto randIt = a_rng.generate<std::int64_t>(0, setEnd);
auto swapFormID = swapFormIDs.extract(*std::next(swapFormIDs.begin(), randIt));
if (swapFormID) {
const Input input(properties, std::string{}, a_str, a_path);
SwapFormData swapFormData(swapFormID.value(), input);
a_func(itBaseFormID, swapFormData);
}
}
} else {
logger::error("\t\t\t\tfail : [{}] (SWAP formIDSet.size() must be 1 OR equal/greater than BASE formIDSet.size())", a_str);
}
} else {
logger::error("\t\t\t\tfail : [{}] (SWAP formID set not found)", a_str);
}
} else {
logger::error("\t\t\t\tfail : [{}] (BASE formID not found)", a_str);
}
}
}