diff --git a/include/core/wildmoninfo.h b/include/core/wildmoninfo.h index 3845a52a..48d9e997 100644 --- a/include/core/wildmoninfo.h +++ b/include/core/wildmoninfo.h @@ -29,9 +29,9 @@ struct WildPokemonHeader { class Project; QWidget *newSpeciesTableEntry(Project *project, WildPokemon mon, int index); -void createSpeciesTableRow(Project *, QTableWidget *, WildPokemon, int); +void createSpeciesTableRow(Project *, QTableWidget *, WildPokemon, int, QString); void clearTabWidget(QLayout *tab); void clearTable(QTableWidget *table); -void populateWildMonTabWidget(QTabWidget *tabWidget, QVector fields); +void populateWildMonTabWidget(QTabWidget *tabWidget, QVector>> fields); #endif // GUARD_WILDMONINFO_H diff --git a/include/project.h b/include/project.h index e7b455a7..53d251a6 100644 --- a/include/project.h +++ b/include/project.h @@ -10,6 +10,8 @@ #include #include +#include +#include #include static QString NONE_MAP_CONSTANT = "MAP_NONE"; @@ -86,10 +88,14 @@ public: void readWildMonData(); QMap wildMonData;// "MAP_CONSTANT": wild_encounter_json + QMap encounterMapToBaseLabel; // when saving, preserve the extra fields for gBattlePyramidWildMonHeaders, gBattlePikeWildMonHeaders void readSpeciesIconPaths(); QMap speciesToIconPath; - QVector wildMonFields; + //QVector wildMonFields; + QVector>> wildMonFields; + // temporary? + QMap extraEncounterGroups; QMap getTopLevelMapFields(); bool loadMapData(Map*); @@ -109,6 +115,7 @@ public: void saveAllDataStructures(); void saveMapLayouts(); void saveMapGroups(); + void saveWildMonData(); void saveMapConstantsHeader(); void saveHealLocationStruct(Map*); void saveTilesets(Tileset*, Tileset*); diff --git a/src/core/wildmoninfo.cpp b/src/core/wildmoninfo.cpp index 8fb87b55..4ae83e97 100644 --- a/src/core/wildmoninfo.cpp +++ b/src/core/wildmoninfo.cpp @@ -20,14 +20,13 @@ void clearTabWidget(QLayout *tab) { } void clearTable(QTableWidget *table) { - // if (table) { table->clear(); table->horizontalHeader()->hide(); } } -void createSpeciesTableRow(Project *project, QTableWidget *table, WildPokemon mon, int index) { +void createSpeciesTableRow(Project *project, QTableWidget *table, WildPokemon mon, int index, QString fieldName) { // QPixmap monIcon = QPixmap(project->speciesToIconPath.value(mon.species)).copy(0, 0, 32, 32); @@ -57,8 +56,14 @@ void createSpeciesTableRow(Project *project, QTableWidget *table, WildPokemon mo minLevel->setValue(mon.minLevel); maxLevel->setValue(mon.maxLevel); - // percentage -- add to json settings - QLabel *percentLabel = new QLabel(landPercentages[index]); + int fieldIndex = 0; + for (auto field : project->wildMonFields) { + if (field.first == fieldName) break; + fieldIndex++; + } + QLabel *percentLabel = new QLabel(QString("%1%").arg( + QString::number(project->wildMonFields[fieldIndex].second[index - 1] + ))); QFrame *speciesSelector = new QFrame; QHBoxLayout *speciesSelectorLayout = new QHBoxLayout; @@ -84,7 +89,7 @@ void createSpeciesTableRow(Project *project, QTableWidget *table, WildPokemon mo table->setCellWidget(index - 1, 4, percentLabel); } -void populateWildMonTabWidget(QTabWidget *tabWidget, QVector fields) { +void populateWildMonTabWidget(QTabWidget *tabWidget, QVector>> fields) { QPushButton *newTabButton = new QPushButton("Configure JSON..."); QObject::connect(newTabButton, &QPushButton::clicked, [=](){ // TODO @@ -92,12 +97,12 @@ void populateWildMonTabWidget(QTabWidget *tabWidget, QVector fields) { }); tabWidget->setCornerWidget(newTabButton); - for (QString field : fields) { + for (QPair> field : fields) { QTableWidget *table = new QTableWidget; table->setEditTriggers(QAbstractItemView::NoEditTriggers); table->setFocusPolicy(Qt::NoFocus); table->setSelectionMode(QAbstractItemView::NoSelection); table->clearFocus(); - tabWidget->addTab(table, field); + tabWidget->addTab(table, field.first); } } diff --git a/src/editor.cpp b/src/editor.cpp index 40549b89..0c70f594 100644 --- a/src/editor.cpp +++ b/src/editor.cpp @@ -158,7 +158,8 @@ void Editor::displayWildMonTables() { WildPokemonHeader header = project->wildMonData.value(map->constantName); int tabIndex = 0; - for (QString field : project->wildMonFields) { + for (QPair> monField : project->wildMonFields) { + QString field = monField.first; QTableWidget *speciesTable = static_cast(ui->tabWidget_WildMons->widget(tabIndex++)); clearTable(speciesTable); //speciesTable->horizontalHeader()->hide(); @@ -170,7 +171,7 @@ void Editor::displayWildMonTables() { speciesTable->setColumnCount(6);// TODO: stretch last column? QStringList landMonTableHeaders; - landMonTableHeaders << "Index" << "Species" << "Min Level" << "Max Level" << "Catch Percentage" << "Encounter Rate"; + landMonTableHeaders << "Index" << "Species" << "Min Level" << "Max Level" << "Index Percentage" << "Encounter Rate"; speciesTable->setHorizontalHeaderLabels(landMonTableHeaders); speciesTable->horizontalHeader()->show(); speciesTable->verticalHeader()->hide(); @@ -202,7 +203,7 @@ void Editor::displayWildMonTables() { speciesTable->setCellWidget(0, 5, encounterFrame); for (WildPokemon mon : header.wildMons[field].wildPokemon) { - createSpeciesTableRow(project, speciesTable, mon, i); + createSpeciesTableRow(project, speciesTable, mon, i, field); i++; } } else { diff --git a/src/project.cpp b/src/project.cpp index 6173a67c..1355c5ea 100644 --- a/src/project.cpp +++ b/src/project.cpp @@ -520,6 +520,48 @@ void Project::saveMapGroups() { mapGroupsFile.write(mapGroupsDoc.toJson()); } +void Project::saveWildMonData() { + // + //QString wildEncountersJsonFilepath = QString("%1/src/data/wild_encounters.json").arg(root); + QString wildEncountersJsonFilepath = QString("%1/src/data/wild_encounters_test.json").arg(root); + QFile wildEncountersFile(wildEncountersJsonFilepath); + if (!wildEncountersFile.open(QIODevice::WriteOnly)) { + logError(QString("Error: Could not open %1 for writing").arg(wildEncountersJsonFilepath)); + return; + } + + QJsonObject wildEncountersObject; + QJsonArray wildEncounterGroups = QJsonArray(); + + // gWildMonHeaders + QJsonObject monHeadersObject; + monHeadersObject["label"] = "gWildMonHeaders"; + monHeadersObject["for_maps"] = true; + QJsonArray encountersArray = QJsonArray(); + for (QString key : wildMonData.keys()) { + QJsonObject encounterObject; + encounterObject["map"] = key; + encounterObject["base_label"] = encounterMapToBaseLabel[key]; + // + //text += key + "\n"; + // ["base_label"] = encounterMapToBaseLabel[mapConstant] + encountersArray.append(encounterObject); + } + monHeadersObject["encounters"] = encountersArray; + wildEncounterGroups.append(monHeadersObject); + + // add extra Json objects that are not associated with maps to the file + for (QString label : extraEncounterGroups.keys()) { + qDebug() << "extra label:" << label; + wildEncounterGroups.append(extraEncounterGroups[label]); + } + + wildEncountersObject["wild_encounter_groups"] = wildEncounterGroups; + QJsonDocument wildEncountersDoc(wildEncountersObject); + wildEncountersFile.write(wildEncountersDoc.toJson()); + wildEncountersFile.close(); +} + void Project::saveMapConstantsHeader() { QString text = QString("#ifndef GUARD_CONSTANTS_MAP_GROUPS_H\n"); text += QString("#define GUARD_CONSTANTS_MAP_GROUPS_H\n"); @@ -1066,6 +1108,7 @@ void Project::saveAllDataStructures() { saveMapLayouts(); saveMapGroups(); saveMapConstantsHeader(); + saveWildMonData(); } void Project::loadTilesetAssets(Tileset* tileset) { @@ -1306,7 +1349,6 @@ void Project::deleteFile(QString path) { } void Project::readWildMonData() { - qDebug() << "Project::readWildMonData";// speed testing // QString wildMonJsonFilepath = QString("%1/src/data/wild_encounters.json").arg(root); QJsonDocument wildMonsJsonDoc; @@ -1319,24 +1361,30 @@ void Project::readWildMonData() { for (auto subObjectRef : wildMonObj["wild_encounter_groups"].toArray()) { QJsonObject subObject = subObjectRef.toObject(); - if (!subObject["for_maps"].toBool()) continue; + if (!subObject["for_maps"].toBool()) { + extraEncounterGroups.insert(subObject["label"].toString(), subObject); + continue; + } - // fill wildMonFields for (auto field : subObject["fields"].toArray()) { - wildMonFields.append(field.toString()); + QPair> encounterField; + encounterField.first = field.toObject()["type"].toString(); + for (auto val : field.toObject()["encounter_rates"].toArray()) + encounterField.second.append(val.toInt()); + wildMonFields.append(encounterField); } QJsonArray encounters = subObject["encounters"].toArray(); for (QJsonValue encounter : encounters) { - // - //qDebug() << encounter["map"].toString(); QString mapConstant = encounter["map"].toString(); - //QString mapName = mapConstantsToMapNames->value(mapConstant); + + encounterMapToBaseLabel.insert(mapConstant, encounter["base_label"].toString()); WildPokemonHeader header; - for (QString field : wildMonFields) { + for (QPair> monField : wildMonFields) { // + QString field = monField.first; if (encounter[field] != QJsonValue::Undefined) { header.wildMons[field].active = true; header.wildMons[field].encounterRate = encounter[field]["encounter_rate"].toInt();