Skip to content

Commit

Permalink
Set reasonable defaults for wild encounter rates
Browse files Browse the repository at this point in the history
  • Loading branch information
GriffinRichards committed Dec 29, 2023
1 parent 6a8d3a8 commit 9452010
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
1 change: 1 addition & 0 deletions include/core/wildmoninfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ struct EncounterField {

typedef QVector<EncounterField> EncounterFields;

void setDefaultEncounterRate(QString fieldName, int rate);
WildMonInfo getDefaultMonInfo(EncounterField field);
void combineEncounters(WildMonInfo &to, WildMonInfo from);

Expand Down
7 changes: 5 additions & 2 deletions src/core/wildmoninfo.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
#include "wildmoninfo.h"
#include "montabwidget.h"


QMap<QString, int> defaultEncounterRates;
void setDefaultEncounterRate(QString fieldName, int rate) {
defaultEncounterRates[fieldName] = rate;
}

WildMonInfo getDefaultMonInfo(EncounterField field) {
WildMonInfo newInfo;
newInfo.active = true;
newInfo.encounterRate = 0;
newInfo.encounterRate = defaultEncounterRates.value(field.name, 1);

int size = field.encounterRates.size();
while (size--)
Expand Down
22 changes: 22 additions & 0 deletions src/project.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1625,6 +1625,10 @@ bool Project::readWildMonData() {
return true;
}

// For each encounter type, count the number of times each encounter rate value occurs.
// The most common value will be used as the default for new groups.
QMap<QString, QMap<int, int>> encounterRateFrequencyMaps;

for (OrderedJson subObjectRef : wildMonObj["wild_encounter_groups"].array_items()) {
OrderedJson::object subObject = subObjectRef.object_items();
if (!subObject["for_maps"].bool_value()) {
Expand All @@ -1650,6 +1654,7 @@ bool Project::readWildMonData() {
encounterField.groups[group].append(slotNum.int_value());
}
}
encounterRateFrequencyMaps.insert(encounterField.name, QMap<int, int>());
wildMonFields.append(encounterField);
}

Expand All @@ -1666,6 +1671,7 @@ bool Project::readWildMonData() {
OrderedJson::object encounterFieldObj = encounterObj[field].object_items();
header.wildMons[field].active = true;
header.wildMons[field].encounterRate = encounterFieldObj["encounter_rate"].int_value();
encounterRateFrequencyMaps[field][header.wildMons[field].encounterRate]++;
for (auto mon : encounterFieldObj["mons"].array_items()) {
WildPokemon newMon;
OrderedJson::object monObj = mon.object_items();
Expand All @@ -1686,6 +1692,22 @@ bool Project::readWildMonData() {
}
}

// For each encounter type, set default encounter rate to most common value.
// Iterate over map of encounter type names to frequency maps...
for (auto i = encounterRateFrequencyMaps.cbegin(), i_end = encounterRateFrequencyMaps.cend(); i != i_end; i++) {
int frequency = 0;
int rate = 1;
const QMap<int, int> frequencyMap = i.value();
// Iterate over frequency map (encounter rate to number of occurrences)...
for (auto j = frequencyMap.cbegin(), j_end = frequencyMap.cend(); j != j_end; j++) {
if (j.value() > frequency) {
frequency = j.value();
rate = j.key();
}
}
setDefaultEncounterRate(i.key(), rate);
}

return true;
}

Expand Down

0 comments on commit 9452010

Please sign in to comment.