diff --git a/include/core/wildmoninfo.h b/include/core/wildmoninfo.h index d436ff1f..7c16d1a0 100644 --- a/include/core/wildmoninfo.h +++ b/include/core/wildmoninfo.h @@ -29,6 +29,7 @@ struct EncounterField { typedef QVector EncounterFields; +void setDefaultEncounterRate(QString fieldName, int rate); WildMonInfo getDefaultMonInfo(EncounterField field); void combineEncounters(WildMonInfo &to, WildMonInfo from); diff --git a/src/core/wildmoninfo.cpp b/src/core/wildmoninfo.cpp index 3e884c84..ecad908f 100644 --- a/src/core/wildmoninfo.cpp +++ b/src/core/wildmoninfo.cpp @@ -1,12 +1,15 @@ #include "wildmoninfo.h" #include "montabwidget.h" - +QMap 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--) diff --git a/src/project.cpp b/src/project.cpp index d3db3d86..bfa12449 100644 --- a/src/project.cpp +++ b/src/project.cpp @@ -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> encounterRateFrequencyMaps; + for (OrderedJson subObjectRef : wildMonObj["wild_encounter_groups"].array_items()) { OrderedJson::object subObject = subObjectRef.object_items(); if (!subObject["for_maps"].bool_value()) { @@ -1650,6 +1654,7 @@ bool Project::readWildMonData() { encounterField.groups[group].append(slotNum.int_value()); } } + encounterRateFrequencyMaps.insert(encounterField.name, QMap()); wildMonFields.append(encounterField); } @@ -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(); @@ -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 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; }