Browse Source

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.
Marcus Huderle 6 years ago
parent
commit
d7756865a9
4 changed files with 35 additions and 3 deletions
  1. 19
    0
      map.cpp
  2. 3
    0
      map.h
  3. 12
    3
      project.cpp
  4. 1
    0
      project.h

+ 19
- 0
map.cpp View File

@@ -4,6 +4,7 @@
4 4
 #include <QDebug>
5 5
 #include <QPainter>
6 6
 #include <QImage>
7
+#include <QRegularExpression>
7 8
 
8 9
 Map::Map(QObject *parent) : QObject(parent)
9 10
 {
@@ -16,6 +17,24 @@ Map::Map(QObject *parent) : QObject(parent)
16 17
     paint_elevation = 3;
17 18
 }
18 19
 
20
+void Map::setName(QString mapName) {
21
+    name = mapName;
22
+    constantName = mapConstantFromName(mapName);
23
+}
24
+
25
+QString Map::mapConstantFromName(QString mapName) {
26
+    // Transform map names of the form 'GraniteCave_B1F` into map constants like 'MAP_GRANITE_CAVE_B1F'.
27
+    QString nameWithUnderscores = mapName.replace(QRegularExpression("([a-z])([A-Z])"), "\\1_\\2");
28
+    QString withMapAndUppercase = "MAP_" + nameWithUnderscores.toUpper();
29
+    QString constantName = withMapAndUppercase.replace(QRegularExpression("_+"), "_");
30
+
31
+    // Handle special cases.
32
+    // SSTidal needs to be SS_TIDAL, rather than SSTIDAL
33
+    constantName = constantName.replace("SSTIDAL", "SS_TIDAL");
34
+
35
+    return constantName;
36
+}
37
+
19 38
 int Map::getWidth() {
20 39
     return width.toInt(nullptr, 0);
21 40
 }

+ 3
- 0
map.h View File

@@ -75,6 +75,7 @@ public:
75 75
 
76 76
 public:
77 77
     QString name;
78
+    QString constantName;
78 79
     QString attributes_label;
79 80
     QString events_label;
80 81
     QString scripts_label;
@@ -102,6 +103,8 @@ public:
102 103
     Blockdata* blockdata = NULL;
103 104
 
104 105
 public:
106
+    void setName(QString mapName);
107
+    static QString mapConstantFromName(QString mapName);
105 108
     int getWidth();
106 109
     int getHeight();
107 110
     Tileset* getBlockTileset(int);

+ 12
- 3
project.cpp View File

@@ -31,7 +31,7 @@ QString Project::getProjectTitle() {
31 31
 Map* Project::loadMap(QString map_name) {
32 32
     Map *map = new Map;
33 33
 
34
-    map->name = map_name;
34
+    map->setName(map_name);
35 35
     readMapHeader(map);
36 36
     readMapAttributes(map);
37 37
     getTilesets(map);
@@ -66,8 +66,13 @@ void Project::loadMapConnections(Map *map) {
66 66
                     Connection *connection = new Connection;
67 67
                     connection->direction = command.value(1);
68 68
                     connection->offset = command.value(2);
69
-                    connection->map_name = command.value(3);
70
-                    map->connections.append(connection);
69
+                    QString mapConstant = command.value(3);
70
+                    if (mapConstantsToMapNames.contains(mapConstant)) {
71
+                        connection->map_name = mapConstantsToMapNames[mapConstant];
72
+                        map->connections.append(connection);
73
+                    } else {
74
+                        qDebug() << QString("Failed to find connected map for map constant '%1'").arg(mapConstant);
75
+                    }
71 76
                 }
72 77
             }
73 78
         }
@@ -515,6 +520,10 @@ void Project::readMapGroups() {
515 520
                     QStringList *list = groupedMaps->value(group);
516 521
                     list->append(params.value(j));
517 522
                     maps->append(params.value(j));
523
+
524
+                    // Build the mapping between map constants and map names.
525
+                    QString mapConstant = Map::mapConstantFromName(params.value(j));
526
+                    mapConstantsToMapNames.insert(mapConstant, params.value(j));
518 527
                 }
519 528
             }
520 529
         }

+ 1
- 0
project.h View File

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