From 8575b832126571f176eda733b0d0e8d55104c0ab Mon Sep 17 00:00:00 2001 From: ultima-soul Date: Mon, 21 Sep 2020 14:26:58 -0700 Subject: [PATCH] Add support for FRLG .map files and border importing for pokefirered and custom border size supporting projects. --- src/core/mapparser.cpp | 27 +++++++++++++++++++++++++-- src/project.cpp | 4 +++- src/ui/newmappopup.cpp | 13 ++++++++++--- 3 files changed, 38 insertions(+), 6 deletions(-) diff --git a/src/core/mapparser.cpp b/src/core/mapparser.cpp index 18a0f918..b9f9fab4 100644 --- a/src/core/mapparser.cpp +++ b/src/core/mapparser.cpp @@ -26,7 +26,11 @@ MapLayout *MapParser::parse(QString filepath, bool *error, Project *project) return nullptr; } - int mapDataOffset = 20; + int borderWidth = static_cast(in.at(16)); // 0 in RSE .map files + int borderHeight = static_cast(in.at(17)); // 0 in RSE .map files + int numBorderTiles = borderWidth * borderHeight; // 0 if RSE + + int mapDataOffset = 20 + (numBorderTiles * 2); // FRLG .map files store border metatile data after the header int mapWidth = static_cast(in.at(0)) | (static_cast(in.at(1)) << 8) | (static_cast(in.at(2)) << 16) | @@ -62,7 +66,7 @@ MapLayout *MapParser::parse(QString filepath, bool *error, Project *project) }*/ int numMetatiles = mapWidth * mapHeight; - int expectedFileSize = numMetatiles * 2 + 20; + int expectedFileSize = 20 + (numBorderTiles * 2) + (numMetatiles * 2); if (in.length() != expectedFileSize) { *error = true; logError(QString(".map file is an unexpected size. Expected %1 bytes, but it has %2 bytes.").arg(expectedFileSize).arg(in.length())); @@ -75,10 +79,23 @@ MapLayout *MapParser::parse(QString filepath, bool *error, Project *project) blockdata->addBlock(word); } + Blockdata *border = nullptr; + if (numBorderTiles != 0) { + border = new Blockdata(); + for (int i = 20; (i + 1) < 20 + (numBorderTiles * 2); i += 2) { + uint16_t word = static_cast((in[i] & 0xff) + ((in[i + 1] & 0xff) << 8)); + border->addBlock(word); + } + } + MapLayout *mapLayout = new MapLayout(); mapLayout->width = QString::number(mapWidth); mapLayout->height = QString::number(mapHeight); + mapLayout->border_width = (borderWidth == 0) ? QString::number(2) : QString::number(borderWidth); + mapLayout->border_height = (borderHeight == 0) ? QString::number(2) : QString::number(borderHeight); + QList tilesets = project->tilesetLabelsOrdered; + if (mapPrimaryTilesetNum > tilesets.size()) mapLayout->tileset_primary_label = tilesets.at(0); else @@ -88,6 +105,12 @@ MapLayout *MapParser::parse(QString filepath, bool *error, Project *project) mapLayout->tileset_secondary_label = tilesets.at(1); else mapLayout->tileset_secondary_label = tilesets.at(mapSecondaryTilesetNum); + mapLayout->blockdata = blockdata->copy(); + + if (border != nullptr) { + mapLayout->border = border->copy(); + } + return mapLayout; } diff --git a/src/project.cpp b/src/project.cpp index 594827fa..0997977c 100644 --- a/src/project.cpp +++ b/src/project.cpp @@ -1930,7 +1930,9 @@ Map* Project::addNewMapToGroup(QString mapName, int groupNum, Map *newMap, bool } else { setNewMapBlockdata(map); } - setNewMapBorder(map); + if (map->layout->border == nullptr) { + setNewMapBorder(map); + } } loadMapTilesets(map); diff --git a/src/ui/newmappopup.cpp b/src/ui/newmappopup.cpp index ce3a7166..04d82410 100644 --- a/src/ui/newmappopup.cpp +++ b/src/ui/newmappopup.cpp @@ -181,13 +181,10 @@ void NewMapPopup::setDefaultValuesImportMap(MapLayout *mapLayout) { ui->comboBox_NewMap_Group->addItems(*project->groupNames); ui->comboBox_NewMap_Group->setCurrentText(project->groupNames->at(0)); - ui->spinBox_NewMap_Width->setValue(mapLayout->width.toInt(nullptr, 0)); ui->spinBox_NewMap_Height->setValue(mapLayout->height.toInt(nullptr, 0)); ui->comboBox_NewMap_Primary_Tileset->setCurrentText(mapLayout->tileset_primary_label); ui->comboBox_NewMap_Secondary_Tileset->setCurrentText(mapLayout->tileset_secondary_label); - ui->spinBox_NewMap_BorderWidth->setValue(DEFAULT_BORDER_WIDTH); - ui->spinBox_NewMap_BorderHeight->setValue(DEFAULT_BORDER_HEIGHT); ui->comboBox_NewMap_Type->addItems(*project->mapTypes); ui->comboBox_NewMap_Location->addItems(project->mapSectionValueToName.values()); @@ -223,11 +220,15 @@ void NewMapPopup::setDefaultValuesImportMap(MapLayout *mapLayout) { break; } if (projectConfig.getUseCustomBorderSize()) { + ui->spinBox_NewMap_BorderWidth->setValue(mapLayout->border_width.toInt(nullptr, 0)); + ui->spinBox_NewMap_BorderHeight->setValue(mapLayout->border_height.toInt(nullptr, 0)); ui->spinBox_NewMap_BorderWidth->setVisible(true); ui->spinBox_NewMap_BorderHeight->setVisible(true); ui->label_NewMap_BorderWidth->setVisible(true); ui->label_NewMap_BorderHeight->setVisible(true); } else { + ui->spinBox_NewMap_BorderWidth->setValue(DEFAULT_BORDER_WIDTH); + ui->spinBox_NewMap_BorderHeight->setValue(DEFAULT_BORDER_HEIGHT); ui->spinBox_NewMap_BorderWidth->setVisible(false); ui->spinBox_NewMap_BorderHeight->setVisible(false); ui->label_NewMap_BorderWidth->setVisible(false); @@ -244,6 +245,10 @@ void NewMapPopup::setDefaultValuesImportMap(MapLayout *mapLayout) { map = new Map(); map->layout = new MapLayout(); map->layout->blockdata = mapLayout->blockdata->copy(); + + if (mapLayout->border != nullptr) { + map->layout->border = mapLayout->border->copy(); + } } void NewMapPopup::on_lineEdit_NewMap_Name_textChanged(const QString &text) { @@ -305,6 +310,8 @@ void NewMapPopup::on_pushButton_NewMap_Accept_clicked() { if (this->importedMap) { layout->blockdata = map->layout->blockdata->copy(); + if (map->layout->border != nullptr) + layout->border = map->layout->border->copy(); } if (this->ui->checkBox_NewMap_Flyable->isChecked()) {