Pokered Save Editor 2
Pokemon Red & Blue save file editor - Qt 6 C++/QML
Loading...
Searching...
No Matches
mapdbentry.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2020 Twilight
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15*/
16
22
23#include <QJsonValue>
24#include <QJsonArray>
25#include <QJsonObject>
26#include <QDebug>
27#include <QQmlEngine>
28#include <pse-common/utility.h>
29
30#include "./mapdbentry.h"
31#include "./mapdbentrywarpout.h"
32#include "./mapdbentrywarpin.h"
33#include "./mapdbentrysign.h"
34#include "./mapdbentrysprite.h"
35#include "./mapdbentryconnect.h"
36#include "./mapdbentrywildmon.h"
41#include "../spriteSet.h"
42#include "../music.h"
43#include "../tileset.h"
44#include "./eventdbentry.h"
45#include "./flydbentry.h"
46#include "./hiddenitemdbentry.h"
47#include "../scripts.h"
48#include "../mapsdb.h"
49
54
55MapDBEntry::MapDBEntry(const QJsonValue& data)
56{
57 // Set simple properties
58 name = data["name"].toString();
59 ind = data["ind"].toDouble();
60
61 // Set simple optional properties
62 if(data["special"].isBool())
63 special = data["special"].toBool();
64
65 if(data["glitch"].isBool())
66 glitch = data["glitch"].toBool();
67
68 if(data["bank"].isDouble())
69 bank = data["bank"].toDouble();
70
71 if(data["dataPtr"].isDouble())
72 dataPtr = data["dataPtr"].toDouble();
73
74 if(data["scriptPtr"].isDouble())
75 scriptPtr = data["scriptPtr"].toDouble();
76
77 if(data["textPtr"].isDouble())
78 textPtr = data["textPtr"].toDouble();
79
80 if(data["width"].isDouble())
81 width = data["width"].toDouble();
82
83 if(data["height"].isDouble())
84 height = data["height"].toDouble();
85
86 if(data["music"].isString())
87 music = data["music"].toString();
88
89 if(data["tileset"].isString())
90 tileset = data["tileset"].toString();
91
92 if(data["modernName"].isString())
93 modernName = data["modernName"].toString();
94
95 if(data["incomplete"].isString())
96 incomplete = data["incomplete"].toString();
97
98 if(data["border"].isDouble())
99 border = data["border"].toDouble();
100
101 if(data["spriteSet"].isDouble())
102 spriteSet = data["spriteSet"].toDouble();
103
104 if(data["warpOut"].isArray())
105 {
106 for(QJsonValue warpEntry : data["warpOut"].toArray()) {
107 auto tmp = new MapDBEntryWarpOut(warpEntry, this);
108 warpOut.append(tmp);
109 }
110 }
111
112 if(data["warpIn"].isArray())
113 {
114 for(QJsonValue warpEntry : data["warpIn"].toArray()) {
115 auto tmp = new MapDBEntryWarpIn(warpEntry, this);
116 warpIn.append(tmp);
117 }
118 }
119
120 if(data["signs"].isArray())
121 {
122 for(QJsonValue signEntry : data["signs"].toArray()) {
123 auto tmp = new MapDBEntrySign(signEntry, this);
124 signs.append(tmp);
125 }
126 }
127
128 if(data["redMons"].isArray())
129 {
130 for(QJsonValue monEntry : data["redMons"].toArray()) {
131 auto tmp = new MapDBEntryWildMon(monEntry, this);
132 monsRed.append(tmp);
133 }
134 }
135
136 if(data["blueMons"].isArray())
137 {
138 for(QJsonValue monEntry : data["blueMons"].toArray()) {
139 auto tmp = new MapDBEntryWildMon(monEntry, this);
140 monsBlue.append(tmp);
141 }
142 }
143
144 if(data["waterMons"].isArray())
145 {
146 for(QJsonValue monEntry : data["waterMons"].toArray()) {
147 auto tmp = new MapDBEntryWildMon(monEntry, this);
148 monsWater.append(tmp);
149 }
150 }
151
152 if(data["monRate"].isDouble())
153 monRate = data["monRate"].toDouble();
154
155 if(data["monRateWater"].isDouble())
156 monRateWater = data["monRateWater"].toDouble();
157
158 if(data["sprites"].isArray())
159 {
160 for(QJsonValue spriteEntry : data["sprites"].toArray()) {
161
162 MapDBEntrySprite* ret;
163
164 if(spriteEntry["item"].isString()) {
165 auto tmp = new MapDBEntrySpriteItem(spriteEntry, this);
166 ret = tmp;
167 }
168 else if(spriteEntry["class"].isString()) {
169 auto tmp = new MapDBEntrySpriteTrainer(spriteEntry, this);
170 ret = tmp;
171 }
172 else if(spriteEntry["pokemon"].isString()) {
173 auto tmp = new MapDBEntrySpritePokemon(spriteEntry, this);
174 ret = tmp;
175 }
176 else {
177 auto tmp = new MapDBEntrySpriteNPC(spriteEntry, this);
178 ret = tmp;
179 }
180
181 sprites.append(ret);
182 }
183 }
184
185 if(data["connect"].isObject())
186 {
187 QJsonValue conVal = data["connect"].toObject();
188
189 if(conVal["north"].isObject()) {
190 QJsonValue tmp = conVal["north"].toObject();
191 connect.insert(
194 }
195 if(conVal["east"].isObject()) {
196 QJsonValue tmp = conVal["east"].toObject();
197 connect.insert(
200 }
201 if(conVal["south"].isObject()) {
202 QJsonValue tmp = conVal["south"].toObject();
203 connect.insert(
206 }
207 if(conVal["west"].isObject()) {
208 QJsonValue tmp = conVal["west"].toObject();
209 connect.insert(
212 }
213 }
214}
215
217{
218 if(music != "")
220
221 if(tileset != "")
223
224 if(incomplete != "")
225 toComplete = MapsDB::inst()->getInd().value(incomplete, nullptr);
226
228 connect.value(MapDBEntryConnect::NORTH)->deepLink();
229 if(connect.contains(MapDBEntryConnect::EAST))
230 connect.value(MapDBEntryConnect::EAST)->deepLink();
232 connect.value(MapDBEntryConnect::SOUTH)->deepLink();
233 if(connect.contains(MapDBEntryConnect::WEST))
234 connect.value(MapDBEntryConnect::WEST)->deepLink();
235
236 if(warpOut.size() > 0)
237 for(auto warpEntry : warpOut)
238 warpEntry->deepLink();
239
240 if(sprites.size() > 0)
241 for(auto spriteEntry : sprites)
242 spriteEntry->deepLink();
243
244 if(monsRed.size() > 0)
245 for(auto monEntry : monsRed)
246 monEntry->deepLink();
247
248 if(monsBlue.size() > 0)
249 for(auto monEntry : monsBlue)
250 monEntry->deepLink();
251
252 if(monsWater.size() > 0)
253 for(auto monEntry : monsWater)
254 monEntry->deepLink();
255
256 if(spriteSet >= 0)
258 SpriteSetDB::inst()->getIndAt(QString::number(spriteSet));
259
260#ifdef QT_DEBUG
261 if(music != "" && toMusic == nullptr)
262 qCritical() << "Map: " << name << ", could not be deep linked to music" << music;
263
264 if(tileset != "" && toTileset == nullptr)
265 qCritical() << "Map: " << name << ", could not be deep linked to tileset" << tileset;
266
267 if(incomplete != "" && toComplete == nullptr)
268 qCritical() << "Map: " << name << ", could not be deep linked to complete" << incomplete;
269
270 if(spriteSet >= 0 && toSpriteSet == nullptr)
271 qCritical() << "Map: " << name << ", could not be deep linked to sprite set" << spriteSet;
272#endif
273
274 if(toSpriteSet != nullptr)
275 toSpriteSet->toMaps.append(this);
276
277 if(toMusic != nullptr)
278 toMusic->toMaps.append(this);
279
280 if(toTileset != nullptr)
281 toTileset->toMaps.append(this);
282}
283
285{
286 static bool once = false;
287 if(once)
288 return;
289
290 qmlRegisterUncreatableType<MapDBEntry>(
291 "PSE.DB.MapDBEntry", 1, 0, "MapDBEntry", "Can't instantiate in QML");
292 once = true;
293}
294
296{
297 return toScript;
298}
299
300void MapDBEntry::qmlProtect(const QQmlEngine* const engine) const
301{
302 Utility::qmlProtectUtil(this, engine);
303}
304
305const QVector<HiddenItemDBEntry*> MapDBEntry::getToHiddenItems() const
306{
307 return toHiddenItems;
308}
309
311{
312 return toHiddenItems.size();
313}
314
316{
317 if(ind >= toHiddenItems.size())
318 return nullptr;
319
320 return toHiddenItems.at(ind);
321}
322
327
328const QVector<EventDBEntry*> MapDBEntry::getToEvents() const
329{
330 return toEvents;
331}
332
334{
335 return toEvents.size();
336}
337
339{
340 if(ind >= toEvents.size())
341 return nullptr;
342
343 return toEvents.at(ind);
344}
345
347{
348 return toComplete;
349}
350
352{
353 return toTileset;
354}
355
357{
358 return toMusic;
359}
360
361const QString MapDBEntry::getIncomplete() const
362{
363 return incomplete;
364}
365
366const QString MapDBEntry::getModernName() const
367{
368 return modernName;
369}
370
371const QString MapDBEntry::getTileset() const
372{
373 return tileset;
374}
375
376const QString MapDBEntry::getMusic() const
377{
378 return music;
379}
380
382{
383 return height;
384}
385
387{
388 return width;
389}
390
392{
393 return textPtr;
394}
395
397{
398 return scriptPtr;
399}
400
402{
403 return dataPtr;
404}
405
407{
408 return bank;
409}
410
412{
413 return border;
414}
415
420
422{
423 return spriteSet;
424}
425
426const QVector<MapDBEntryWildMon*> MapDBEntry::getMonsWater() const
427{
428 return monsWater;
429}
430
432{
433 return monsWater.size();
434}
435
437{
438 if(ind >= monsWater.size())
439 return nullptr;
440
441 return monsWater.at(ind);
442}
443
444const QVector<MapDBEntryWildMon*> MapDBEntry::getMonsBlue() const
445{
446 return monsBlue;
447}
448
450{
451 return monsBlue.size();
452}
453
455{
456 if(ind >= monsBlue.size())
457 return nullptr;
458
459 return monsBlue.at(ind);
460}
461
462const QVector<MapDBEntryWildMon*> MapDBEntry::getMonsRed() const
463{
464 return monsRed;
465}
466
468{
469 return monsRed.size();
470}
471
473{
474 if(ind >= monsRed.size())
475 return nullptr;
476
477 return monsRed.at(ind);
478}
479
481{
482 return monRateWater;
483}
484
486{
487 return monRate;
488}
489
490const QHash<int, MapDBEntryConnect*> MapDBEntry::getConnect() const
491{
492 return connect;
493}
494
496{
497 return connect.value(val, nullptr);
498}
499
500const QVector<MapDBEntrySprite*> MapDBEntry::getSprites() const
501{
502 return sprites;
503}
504
506{
507 return sprites.size();
508}
509
511{
512 if(ind >= sprites.size())
513 return nullptr;
514
515 return sprites.at(ind);
516}
517
518const QVector<MapDBEntrySign*> MapDBEntry::getSigns() const
519{
520 return signs;
521}
522
524{
525 return signs.size();
526}
527
529{
530 if(ind >= signs.size())
531 return nullptr;
532
533 return signs.at(ind);
534}
535
536const QVector<MapDBEntryWarpIn*> MapDBEntry::getWarpIn() const
537{
538 return warpIn;
539}
540
542{
543 return warpIn.size();
544}
545
547{
548 if(ind >= warpIn.size())
549 return nullptr;
550
551 return warpIn.at(ind);
552}
553
554const QVector<MapDBEntryWarpOut*> MapDBEntry::getWarpOut() const
555{
556 return warpOut;
557}
558
560{
561 return warpOut.size();
562}
563
565{
566 if(ind >= warpOut.size())
567 return nullptr;
568
569 return warpOut.at(ind);
570}
571
573{
574 return special;
575}
576
578{
579 return glitch;
580}
581
583{
584 return ind;
585}
586
587const QString MapDBEntry::getName() const
588{
589 return name;
590}
591
592const QString MapDBEntry::bestName() const
593{
594 if(modernName != "")
595 return modernName;
596 else
597 return name;
598}
599
601{
602 if(height >= 0)
603 return height * 2;
604
605 return -1;
606}
607
609{
610 if(width >= 0)
611 return width * 2;
612
613 return -1;
614}
static MapsDB * inst()
< Number of maps.
Definition mapsdb.cpp:35
const QHash< QString, MapDBEntry * > getInd() const
Name->map index.
Definition mapsdb.cpp:46
MusicDBEntry * getIndAt(const QString &key) const
Track by name key (for QML).
Definition music.cpp:53
static MusicDB * inst()
< Number of tracks.
Definition music.cpp:37
static SpriteSetDB * inst()
< Number of sprite-sets.
Definition spriteSet.cpp:89
SpriteSetDBEntry * getIndAt(const QString &key) const
Set by name key (for QML).
TilesetDBEntry * getIndAt(const QString &key) const
Tileset by name key (for QML).
Definition tileset.cpp:72
static TilesetDB * inst()
< Number of tilesets.
Definition tileset.cpp:56
static void qmlProtectUtil(const QObject *const obj, const QQmlEngine *const engine)
Pin obj to C++ ownership so the QML engine never garbage-collects it.
Definition utility.cpp:63
One edge connection of a map (the seam to a neighbouring map).
One sign defined on a map: its position and text id.
A map sprite that is a pick-up item (type ITEM).
A map sprite that is a plain NPC (type NPC).
A map sprite that is a static, battleable Pokemon (type POKEMON).
A map sprite that is a battleable trainer (type TRAINER).
A map's sprite definition – base class for the four sprite kinds.
A warp-in point: a destination spot other maps' warp-outs land on.
A warp-out point: a tile that warps the player to another map.
One wild-encounter slot in a map's encounter table: species + level.
const QVector< EventDBEntry * > getToEvents() const
Events associated with this map.
const QVector< MapDBEntryWarpOut * > getWarpOut() const
Outgoing warps.
const QString getTileset() const
ScriptDBEntry * getToScript() const
const QString getIncomplete() const
void deepLink()
Resolve the full cross-reference web.
QString music
Music name.
Definition mapdbentry.h:230
QVector< HiddenItemDBEntry * > toHiddenItems
Definition mapdbentry.h:241
int textPtr
Map text pointer.
Definition mapdbentry.h:226
QVector< MapDBEntryWildMon * > monsBlue
Blue-version land encounters.
Definition mapdbentry.h:213
const MapDBEntryWildMon * getMonsRedAt(const int ind) const
Red wild ind (for QML).
int getMonRate() const
int border
Border block number.
Definition mapdbentry.h:221
SpriteSetDBEntry * toSpriteSet
Resolved sprite set (deepLink).
Definition mapdbentry.h:218
QVector< MapDBEntryWildMon * > monsWater
Water encounters (shared; see note).
Definition mapdbentry.h:214
bool getGlitch() const
int scriptPtr
Map script pointer.
Definition mapdbentry.h:225
FlyDBEntry * toFlyDestination
Definition mapdbentry.h:240
const QString getName() const
int getSpriteSet() const
TilesetDBEntry * getToTileset() const
int getWarpInSize() const
int getWarpOutSize() const
SpriteSetDBEntry * getToSpriteSet() const
QString tileset
Tileset name.
Definition mapdbentry.h:231
const MapDBEntryConnect * getConnectAt(const int val) const
Connection in direction val (for QML).
int bank
Map data bank.
Definition mapdbentry.h:223
int height
Height (blocks).
Definition mapdbentry.h:228
friend struct ScriptDBEntry
Definition mapdbentry.h:249
int getDataPtr() const
MapDBEntry()
Empty entry (built by MapsDB).
QVector< MapDBEntryWarpOut * > warpOut
Outgoing warps.
Definition mapdbentry.h:191
int spriteSet
Sprite-set index.
Definition mapdbentry.h:217
bool glitch
Backing field (read via getGlitch()).
Definition mapdbentry.h:187
int getToEventsSize() const
friend struct FlyDBEntry
Definition mapdbentry.h:247
friend struct EventDBEntry
Definition mapdbentry.h:246
int dataPtr
Map data pointer.
Definition mapdbentry.h:224
int getToHiddenItemsSize() const
void qmlProtect(const QQmlEngine *const engine) const
Pin to C++ ownership.
int getMonsWaterSize() const
bool special
Backing field (read via getSpecial()).
Definition mapdbentry.h:188
int getWidth() const
QVector< MapDBEntryWildMon * > monsRed
Red-version land encounters.
Definition mapdbentry.h:212
const QVector< MapDBEntrySprite * > getSprites() const
Sprites on the map.
int getMonsBlueSize() const
const QVector< MapDBEntryWildMon * > getMonsBlue() const
Blue-version wild encounters.
friend struct HiddenItemDBEntry
Definition mapdbentry.h:248
void qmlRegister() const
Register with QML.
TilesetDBEntry * toTileset
Definition mapdbentry.h:237
const QVector< MapDBEntryWarpIn * > getWarpIn() const
Incoming warps.
const QVector< MapDBEntryWildMon * > getMonsWater() const
Water wild encounters.
QVector< MapDBEntryWarpIn * > warpIn
Incoming warps.
Definition mapdbentry.h:194
QVector< MapDBEntrySprite * > sprites
Sprites.
Definition mapdbentry.h:200
int getScriptPtr() const
MusicDBEntry * toMusic
Definition mapdbentry.h:236
const QVector< MapDBEntrySign * > getSigns() const
Signs on the map.
int getMonRateWater() const
const QVector< MapDBEntryWildMon * > getMonsRed() const
Red-version wild encounters.
const MapDBEntrySprite * getSpritesAt(const int ind) const
Sprite ind (for QML).
const MapDBEntryWarpIn * getWarpInAt(const int ind) const
Incoming warp ind (for QML).
int getBorder() const
FlyDBEntry * getToFlyDestination() const
int getTextPtr() const
const MapDBEntryWildMon * getMonsWaterAt(const int ind) const
Water wild ind (for QML).
int getSpritesSize() const
QVector< MapDBEntrySign * > signs
Signs.
Definition mapdbentry.h:197
int getMonsRedSize() const
const QString getModernName() const
const QString getMusic() const
const MapDBEntryWarpOut * getWarpOutAt(const int ind) const
Outgoing warp ind (for QML).
const MapDBEntrySign * getSignsAt(const int ind) const
Sign ind (for QML).
const QString bestName() const
< Best display name (modern/internal).
ScriptDBEntry * toScript
Resolved script (deepLink).
Definition mapdbentry.h:242
int monRate
Land encounter rate.
Definition mapdbentry.h:210
QVector< EventDBEntry * > toEvents
Definition mapdbentry.h:239
QHash< int, MapDBEntryConnect * > connect
Edge connections by direction.
Definition mapdbentry.h:203
bool getSpecial() const
QString modernName
Modern display name.
Definition mapdbentry.h:232
int getBank() const
int width
Width (blocks).
Definition mapdbentry.h:227
MusicDBEntry * getToMusic() const
const EventDBEntry * getToEventsAt(const int ind) const
Associated event ind (for QML).
const HiddenItemDBEntry * getToHiddenItemsAt(const int ind) const
Hidden item ind (for QML).
int monRateWater
Water encounter rate.
Definition mapdbentry.h:211
int height2X2() const
Height x2 (derived; see note).
QString name
Backing field (read via getName()).
Definition mapdbentry.h:184
MapDBEntry * toComplete
Definition mapdbentry.h:238
int width2X2() const
Width x2 (derived; see note).
MapDBEntry * getToComplete() const
const MapDBEntryWildMon * getMonsBlueAt(const int ind) const
Blue wild ind (for QML).
int getHeight() const
const QVector< HiddenItemDBEntry * > getToHiddenItems() const
Hidden items on this map.
int getInd() const
const QHash< int, MapDBEntryConnect * > getConnect() const
Edge connections by direction.
int getSignsSize() const
QString incomplete
Incomplete-map marker.
Definition mapdbentry.h:233
int ind
Backing field (read via getInd()).
Definition mapdbentry.h:185
One music track: its name and bank/id, plus the maps that use it.
Definition music.h:38
One sprite-set: the pre-loaded sprite group for an outdoor area.
Definition spriteSet.h:47
One tileset definition: its type, graphics/block/collision pointers, etc.
Definition tileset.h:45