Add support for FRLG .map files and border importing for pokefirered and custom border size supporting projects.

This commit is contained in:
ultima-soul 2020-09-21 14:26:58 -07:00
parent 38e7951f3e
commit 8575b83212
3 changed files with 38 additions and 6 deletions

View file

@ -26,7 +26,11 @@ MapLayout *MapParser::parse(QString filepath, bool *error, Project *project)
return nullptr;
}
int mapDataOffset = 20;
int borderWidth = static_cast<unsigned char>(in.at(16)); // 0 in RSE .map files
int borderHeight = static_cast<unsigned char>(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<unsigned char>(in.at(0)) |
(static_cast<unsigned char>(in.at(1)) << 8) |
(static_cast<unsigned char>(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<uint16_t>((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<QString> 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;
}

View file

@ -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);

View file

@ -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()) {