diff --git a/map.cpp b/map.cpp index 4d92f90c..f72d758d 100755 --- a/map.cpp +++ b/map.cpp @@ -4,6 +4,7 @@ #include #include #include +#include Map::Map(QObject *parent) : QObject(parent) { @@ -16,6 +17,24 @@ Map::Map(QObject *parent) : QObject(parent) paint_elevation = 3; } +void Map::setName(QString mapName) { + name = mapName; + constantName = mapConstantFromName(mapName); +} + +QString Map::mapConstantFromName(QString mapName) { + // Transform map names of the form 'GraniteCave_B1F` into map constants like 'MAP_GRANITE_CAVE_B1F'. + QString nameWithUnderscores = mapName.replace(QRegularExpression("([a-z])([A-Z])"), "\\1_\\2"); + QString withMapAndUppercase = "MAP_" + nameWithUnderscores.toUpper(); + QString constantName = withMapAndUppercase.replace(QRegularExpression("_+"), "_"); + + // Handle special cases. + // SSTidal needs to be SS_TIDAL, rather than SSTIDAL + constantName = constantName.replace("SSTIDAL", "SS_TIDAL"); + + return constantName; +} + int Map::getWidth() { return width.toInt(nullptr, 0); } diff --git a/map.h b/map.h index 6bda7c55..30b8bd9b 100755 --- a/map.h +++ b/map.h @@ -75,6 +75,7 @@ public: public: QString name; + QString constantName; QString attributes_label; QString events_label; QString scripts_label; @@ -102,6 +103,8 @@ public: Blockdata* blockdata = NULL; public: + void setName(QString mapName); + static QString mapConstantFromName(QString mapName); int getWidth(); int getHeight(); Tileset* getBlockTileset(int); diff --git a/project.cpp b/project.cpp index 491b4171..b0fae542 100755 --- a/project.cpp +++ b/project.cpp @@ -31,7 +31,7 @@ QString Project::getProjectTitle() { Map* Project::loadMap(QString map_name) { Map *map = new Map; - map->name = map_name; + map->setName(map_name); readMapHeader(map); readMapAttributes(map); getTilesets(map); @@ -66,8 +66,13 @@ void Project::loadMapConnections(Map *map) { Connection *connection = new Connection; connection->direction = command.value(1); connection->offset = command.value(2); - connection->map_name = command.value(3); - map->connections.append(connection); + QString mapConstant = command.value(3); + if (mapConstantsToMapNames.contains(mapConstant)) { + connection->map_name = mapConstantsToMapNames[mapConstant]; + map->connections.append(connection); + } else { + qDebug() << QString("Failed to find connected map for map constant '%1'").arg(mapConstant); + } } } } @@ -515,6 +520,10 @@ void Project::readMapGroups() { QStringList *list = groupedMaps->value(group); list->append(params.value(j)); maps->append(params.value(j)); + + // Build the mapping between map constants and map names. + QString mapConstant = Map::mapConstantFromName(params.value(j)); + mapConstantsToMapNames.insert(mapConstant, params.value(j)); } } } diff --git a/project.h b/project.h index c06be738..b966a58f 100755 --- a/project.h +++ b/project.h @@ -15,6 +15,7 @@ public: QStringList *groupNames = NULL; QList *groupedMapNames = NULL; QStringList *mapNames = NULL; + QMap mapConstantsToMapNames; QMap *map_cache; Map* loadMap(QString);