Skip to content

Commit

Permalink
Seed finder: Allow setting multiple desired values for a randomization
Browse files Browse the repository at this point in the history
Currently not available on the GUI, but can be set from a randomizations file by adding more values under `desired_values` (which is a `float` array).
  • Loading branch information
Vankata453 committed Mar 7, 2024
1 parent f273f2d commit 3b02b69
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 17 deletions.
8 changes: 4 additions & 4 deletions src/supertux/menu/seed_finder_menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ SeedFinderMenu::import_randomization(const int index)
const int desired_value_id = -9 - (index + 1) * 2;
add_item(std::unique_ptr<MenuItem>(new ItemAction(_("Set desired value..."),
desired_value_id)), items_pos + 3);
if (rand->m_desired_value != boost::none) // Check if the desired value isn't already set.
if (!rand->m_desired_values.empty()) // Check if a desired value isn't already set.
set_desired_value(desired_value_id, &items_pos);

add_item(std::unique_ptr<MenuItem>(new ItemHorizontalLine()), items_pos + 4);
Expand Down Expand Up @@ -201,11 +201,11 @@ SeedFinderMenu::set_desired_value(const int item_id, int* pos_)
const int item_pos = get_item_pos(item_id);
auto* rand = s_seed_finder->m_randomizations[randomization_id].get();

if (rand->m_desired_value == boost::none)
rand->m_desired_value = 0;
if (rand->m_desired_values.empty())
rand->m_desired_values.push_back(1);

delete_item(item_pos);
add_item(std::unique_ptr<MenuItem>(new ItemNumField(_("Desired value"), &(rand->m_desired_value.get()))), item_pos)->id = item_id;
add_item(std::unique_ptr<MenuItem>(new ItemNumField(_("Desired value"), &(rand->m_desired_values[0]))), item_pos)->id = item_id;
add_item(std::unique_ptr<MenuItem>(new ItemNumField(_("Precision"), &rand->m_precision)), item_pos + 1);
if (pos_) (*pos_) += 1; // Shift item positions.
set_active_item(item_id);
Expand Down
35 changes: 23 additions & 12 deletions src/supertux/seed_finder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,17 @@ SeedFinder::Randomization::Randomization(float range_start, float range_end, Ran
m_time(time),
m_type(type),
m_value_type(RANDVALUE_EQUAL),
m_desired_value(desired_value),
m_desired_values(),
m_precision(precision),
m_value(),
m_temp_time(),
m_pilot_timeframe(false),
m_pilot_timeframe_time(-1.f)
{
reset();

if (desired_value)
m_desired_values.push_back(desired_value.get());
}

SeedFinder::Randomization::Randomization(ReaderMapping& mapping) :
Expand All @@ -55,7 +58,7 @@ SeedFinder::Randomization::Randomization(ReaderMapping& mapping) :
m_time(-1.f),
m_type(),
m_value_type(),
m_desired_value(),
m_desired_values(),
m_precision(0.01f),
m_value(),
m_temp_time(),
Expand All @@ -70,9 +73,8 @@ SeedFinder::Randomization::Randomization(ReaderMapping& mapping) :
mapping.get("type", m_type);
mapping.get("value-type", m_value_type);

float desired_value;
if (mapping.get("desired-value", desired_value))
m_desired_value = desired_value;
if (!mapping.get("desired-values", m_desired_values))
mapping.get("desired-value", m_desired_values); // Retro-compatibility

mapping.get("precision", m_precision);

Expand All @@ -91,10 +93,10 @@ SeedFinder::Randomization::save(Writer& writer)

writer.write("type", m_type);

if (m_desired_value)
if (!m_desired_values.empty())
{
writer.write("value-type", m_value_type);
writer.write("desired-value", m_desired_value.get());
writer.write("desired-values", m_desired_values);
writer.write("precision", m_precision);
}

Expand Down Expand Up @@ -139,20 +141,29 @@ SeedFinder::Randomization::to_string() const

bool
SeedFinder::Randomization::has_match() const
{
for (float desired_value : m_desired_values)
{
if (has_value_match(desired_value))
return true;
}
return false;
}

bool
SeedFinder::Randomization::has_value_match(float desired_value) const
{
if (m_value == boost::none)
return false;
if (m_desired_value == boost::none)
return true;

switch (m_value_type)
{
case RANDVALUE_EQUAL:
return std::fabs(*m_value - m_desired_value.get()) < m_precision;
return std::fabs(*m_value - desired_value) < m_precision;
case RANDVALUE_LESSTHAN:
return *m_value <= m_desired_value.get();
return *m_value <= desired_value;
case RANDVALUE_MORETHAN:
return *m_value >= m_desired_value.get();
return *m_value >= desired_value;
}
return false;
}
Expand Down
5 changes: 4 additions & 1 deletion src/supertux/seed_finder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class SeedFinder final

int m_type;
int m_value_type;
boost::optional<float> m_desired_value;
std::vector<float> m_desired_values;
float m_precision;

boost::optional<float> m_value;
Expand Down Expand Up @@ -92,6 +92,9 @@ class SeedFinder final
float get_temp_time() const { return m_temp_time; }
void set_temp_time(const float& temp_time) { m_temp_time = temp_time; }

private:
bool has_value_match(float desired_value) const;

private:
Randomization(const Randomization&);
Randomization& operator=(const Randomization&);
Expand Down

0 comments on commit 3b02b69

Please sign in to comment.