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
 #include <QDebug>
4
 #include <QDebug>
5
 #include <QPainter>
5
 #include <QPainter>
6
 #include <QImage>
6
 #include <QImage>
7
+#include <QRegularExpression>
7
 
8
 
8
 Map::Map(QObject *parent) : QObject(parent)
9
 Map::Map(QObject *parent) : QObject(parent)
9
 {
10
 {
16
     paint_elevation = 3;
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
 int Map::getWidth() {
38
 int Map::getWidth() {
20
     return width.toInt(nullptr, 0);
39
     return width.toInt(nullptr, 0);
21
 }
40
 }

+ 3
- 0
map.h View File

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

+ 12
- 3
project.cpp View File

31
 Map* Project::loadMap(QString map_name) {
31
 Map* Project::loadMap(QString map_name) {
32
     Map *map = new Map;
32
     Map *map = new Map;
33
 
33
 
34
-    map->name = map_name;
34
+    map->setName(map_name);
35
     readMapHeader(map);
35
     readMapHeader(map);
36
     readMapAttributes(map);
36
     readMapAttributes(map);
37
     getTilesets(map);
37
     getTilesets(map);
66
                     Connection *connection = new Connection;
66
                     Connection *connection = new Connection;
67
                     connection->direction = command.value(1);
67
                     connection->direction = command.value(1);
68
                     connection->offset = command.value(2);
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
                     QStringList *list = groupedMaps->value(group);
520
                     QStringList *list = groupedMaps->value(group);
516
                     list->append(params.value(j));
521
                     list->append(params.value(j));
517
                     maps->append(params.value(j));
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
     QStringList *groupNames = NULL;
15
     QStringList *groupNames = NULL;
16
     QList<QStringList*> *groupedMapNames = NULL;
16
     QList<QStringList*> *groupedMapNames = NULL;
17
     QStringList *mapNames = NULL;
17
     QStringList *mapNames = NULL;
18
+    QMap<QString, QString> mapConstantsToMapNames;
18
 
19
 
19
     QMap<QString, Map*> *map_cache;
20
     QMap<QString, Map*> *map_cache;
20
     Map* loadMap(QString);
21
     Map* loadMap(QString);