Fix map connections.

Since map constants can be inferred from map names, but not the other
way around, create a mapping between map constants and map names and use
that to find the connected map.
This commit is contained in:
Marcus Huderle 2018-02-12 08:42:01 -08:00
parent 0452156372
commit d7756865a9
4 changed files with 35 additions and 3 deletions

19
map.cpp
View file

@ -4,6 +4,7 @@
#include <QDebug>
#include <QPainter>
#include <QImage>
#include <QRegularExpression>
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);
}

3
map.h
View file

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

View file

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

View file

@ -15,6 +15,7 @@ public:
QStringList *groupNames = NULL;
QList<QStringList*> *groupedMapNames = NULL;
QStringList *mapNames = NULL;
QMap<QString, QString> mapConstantsToMapNames;
QMap<QString, Map*> *map_cache;
Map* loadMap(QString);