Pokered Save Editor 2
Pokemon Red & Blue save file editor - Qt 6 C++/QML
Toggle main menu visibility
Loading...
Searching...
No Matches
mapdbentryconnect.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 <QDebug>
24
#include <QQmlEngine>
25
#include <
pse-common/utility.h
>
26
#include "
./mapdbentryconnect.h
"
27
#include "
../mapsdb.h
"
28
#include "
./mapdbentry.h
"
29
30
MapDBEntryConnect::MapDBEntryConnect
() {
31
qmlRegister
();
32
}
33
MapDBEntryConnect::MapDBEntryConnect
(
const
ConnectDir
dir
,
34
MapDBEntry
*
const
fromMap
,
35
const
QJsonValue& data)
36
{
37
qmlRegister
();
38
// Set Direction
39
this->dir =
dir
;
40
41
// Save from map
42
this->fromMap =
fromMap
;
43
parent
=
fromMap
;
44
45
// Set other values from JSON
46
map
= data[
"map"
].toString();
47
stripMove
= data[
"stripMove"
].toDouble();
48
stripOffset
= data[
"stripOffset"
].toDouble();
49
flag
= data[
"flag"
].toBool(
false
);
50
}
51
52
void
MapDBEntryConnect::deepLink
()
53
{
54
toMap
=
MapsDB::inst
()->
getInd
().value(
map
,
nullptr
);
55
56
#ifdef QT_DEBUG
57
if
(
toMap
==
nullptr
)
58
qCritical() <<
"Map Connect: "
<<
map
<<
", could not be deep linked to"
;
59
#endif
60
}
61
62
void
MapDBEntryConnect::qmlRegister
()
const
63
{
64
static
bool
once =
false
;
65
if
(once)
66
return
;
67
68
qmlRegisterUncreatableType<MapDBEntryConnect>(
69
"PSE.DB.MapDBEntryConnect"
, 1, 0,
"MapDBEntryConnect"
,
"Can't instantiate in QML"
);
70
once =
true
;
71
}
72
73
MapDBEntry
*
MapDBEntryConnect::getParent
()
const
74
{
75
return
parent
;
76
}
77
78
void
MapDBEntryConnect::qmlProtect
(
const
QQmlEngine*
const
engine)
const
79
{
80
Utility::qmlProtectUtil
(
this
, engine);
81
}
82
83
MapDBEntry
*
MapDBEntryConnect::getFromMap
()
const
84
{
85
return
fromMap
;
86
}
87
88
MapDBEntry
*
MapDBEntryConnect::getToMap
()
const
89
{
90
return
toMap
;
91
}
92
93
bool
MapDBEntryConnect::getFlag
()
const
94
{
95
return
flag
;
96
}
97
98
int
MapDBEntryConnect::getStripOffset
()
const
99
{
100
return
stripOffset
;
101
}
102
103
int
MapDBEntryConnect::getStripMove
()
const
104
{
105
return
stripMove
;
106
}
107
108
MapDBEntryConnect::ConnectDir
MapDBEntryConnect::getDir
()
const
109
{
110
return
dir
;
111
}
112
113
const
QString
MapDBEntryConnect::getMap
()
const
114
{
115
return
map
;
116
}
117
118
int
MapDBEntryConnect::stripLocation
()
const
119
{
120
// Stop if toMap is not accessible or it doesn't have a data pointer
121
// A valid toMap is required
122
if
(
toMap
==
nullptr
||
123
toMap
->getDataPtr() <= 0 ||
124
toMap
->getWidth() <= 0 ||
125
toMap
->getHeight() <= 0)
126
return
0;
127
128
int
ret = 0;
129
130
int
dataPtr =
toMap
->getDataPtr();
131
int
toWidth =
toMap
->getWidth();
132
int
toHeight =
toMap
->getHeight();
133
134
// These can vary based on direction
135
if
(
dir
==
ConnectDir::NORTH
) {
136
ret = dataPtr + (toWidth * (toHeight - 3)) +
stripOffset
;
137
}
138
else
if
(
dir
==
ConnectDir::SOUTH
) {
139
ret = dataPtr +
stripOffset
;
140
}
141
else
if
(
dir
==
ConnectDir::WEST
) {
142
ret = dataPtr + (toWidth *
stripOffset
) + toWidth - 3;
143
}
144
else
{
145
ret = dataPtr + (toWidth *
stripOffset
);
146
}
147
148
return
ret;
149
}
150
151
int
MapDBEntryConnect::mapPos
()
const
152
{
153
if
(
fromMap
==
nullptr
||
154
fromMap
->getHeight() <= 0 ||
155
fromMap
->getWidth() <= 0)
156
return
0;
157
158
int
ret = 0;
159
int
fromHeight =
fromMap
->getHeight();
160
int
fromWidth =
fromMap
->getWidth();
161
162
if
(
dir
==
ConnectDir::NORTH
) {
163
ret =
worldMapPtr
+ 3 +
stripMove
;
164
}
165
else
if
(
dir
==
ConnectDir::SOUTH
) {
166
ret =
worldMapPtr
+ 3 + (fromHeight + 3) * (fromWidth + 6) +
stripMove
;
167
}
168
else
if
(
dir
==
ConnectDir::WEST
) {
169
ret =
worldMapPtr
+ (fromWidth + 6) * (
stripMove
+ 3);
170
}
171
else
{
172
ret =
worldMapPtr
- 3 + (fromWidth + 6) * (
stripMove
+ 4);
173
}
174
175
return
ret;
176
}
177
178
int
MapDBEntryConnect::stripSize
()
const
179
{
180
if
(
fromMap
==
nullptr
||
181
toMap
==
nullptr
||
182
fromMap
->getWidth() <= 0 ||
183
toMap
->getWidth() <= 0 ||
184
fromMap
->getHeight() <= 0 ||
185
toMap
->getHeight() <= 0)
186
return
0;
187
188
int
ret = 0;
189
int
fromWidth =
fromMap
->getWidth();
190
int
toWidth =
toMap
->getWidth();
191
int
fromHeight =
fromMap
->getHeight();
192
int
toHeight =
toMap
->getHeight();
193
194
if
(
dir
==
ConnectDir::NORTH
) {
195
if
(fromWidth < toWidth)
196
ret = fromWidth -
stripMove
+ 3;
197
else
198
ret = toWidth -
stripOffset
;
199
}
200
else
if
(
dir
==
ConnectDir::SOUTH
) {
201
if
(fromWidth < toWidth) {
202
if
(
flag
)
203
ret = fromWidth -
stripMove
+ 3;
204
else
205
ret = fromWidth -
stripMove
;
206
}
207
else
{
208
ret = toWidth -
stripOffset
;
209
}
210
}
211
else
if
(
dir
==
ConnectDir::WEST
) {
212
if
(fromHeight < toHeight)
213
ret = fromHeight -
stripMove
+ 3;
214
else
215
ret = toHeight -
stripOffset
;
216
}
217
else
{
218
if
(fromHeight < toHeight) {
219
if
(
flag
)
220
ret = fromHeight -
stripMove
+ 3;
221
else
222
ret = fromHeight -
stripMove
;
223
}
224
else
{
225
ret = toHeight -
stripOffset
;
226
}
227
}
228
229
return
ret;
230
}
231
232
int
MapDBEntryConnect::yAlign
()
const
233
{
234
if
(
toMap
==
nullptr
||
235
toMap
->getHeight() <= 0)
236
return
0;
237
238
int
ret;
239
int
toHeight =
toMap
->getHeight();
240
241
if
(
dir
==
ConnectDir::NORTH
) {
242
ret = (toHeight * 2) - 1;
243
}
244
else
if
(
dir
==
ConnectDir::SOUTH
) {
245
ret = 0;
246
}
247
else
if
(
dir
==
ConnectDir::WEST
) {
248
ret = (
stripMove
-
stripOffset
) * -2;
249
}
250
else
{
251
ret = (
stripMove
-
stripOffset
) * -2;
252
}
253
254
return
ret;
255
}
256
257
int
MapDBEntryConnect::xAlign
()
const
258
{
259
// Same "valid toMap required" guard as yAlign()/window()/stripSize(): a missing
260
// map or a non-positive width yields 0. The `<= 0` was missing here (a copy-paste
261
// typo), so the bare `toMap->getWidth()` inverted the test -- it returned 0 for
262
// every real map and only computed when width was 0. Confirmed against the
263
// connection-data formulas in notes/reference/gen1-knowledge.md (xAlign mirrors
264
// yAlign, which matches the documented North Y-align = (height*2)-1).
265
if
(
toMap
==
nullptr
||
266
toMap
->getWidth() <= 0)
267
return
0;
268
269
int
ret;
270
int
toWidth =
toMap
->getWidth();
271
272
if
(
dir
==
ConnectDir::NORTH
) {
273
ret = (
stripMove
-
stripOffset
) * -2;
274
}
275
else
if
(
dir
==
ConnectDir::SOUTH
) {
276
ret = (
stripMove
-
stripOffset
) * -2;
277
}
278
else
if
(
dir
==
ConnectDir::WEST
) {
279
ret = (toWidth * 2) - 1;
280
}
281
else
{
282
ret = 0;
283
}
284
285
return
ret;
286
}
287
288
int
MapDBEntryConnect::window
()
const
289
{
290
if
(
toMap
==
nullptr
||
291
toMap
->getHeight() <= 0 ||
292
toMap
->getWidth() <= 0)
293
return
0;
294
295
int
ret;
296
int
toHeight =
toMap
->getHeight();
297
int
toWidth =
toMap
->getWidth();
298
299
if
(
dir
==
ConnectDir::NORTH
) {
300
ret =
worldMapPtr
+ 1 + (toHeight * (toWidth + 6));
301
}
302
else
if
(
dir
==
ConnectDir::SOUTH
) {
303
ret =
worldMapPtr
+ 7 + toWidth;
304
}
305
else
if
(
dir
==
ConnectDir::WEST
) {
306
ret =
worldMapPtr
+ 6 + (2 * toWidth);
307
}
308
else
{
309
ret =
worldMapPtr
+ 7 + toWidth;
310
}
311
312
return
ret;
313
}
MapsDB::inst
static MapsDB * inst()
< Number of maps.
Definition
mapsdb.cpp:35
MapsDB::getInd
const QHash< QString, MapDBEntry * > getInd() const
Name->map index.
Definition
mapsdb.cpp:46
Utility::qmlProtectUtil
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
mapdbentry.h
mapdbentryconnect.h
mapsdb.h
MapDBEntryConnect::getStripOffset
int getStripOffset() const
Definition
mapdbentryconnect.cpp:98
MapDBEntryConnect::getMap
const QString getMap() const
Definition
mapdbentryconnect.cpp:113
MapDBEntryConnect::toMap
MapDBEntry * toMap
Resolved connected map (deepLink).
Definition
mapdbentryconnect.h:155
MapDBEntryConnect::flag
bool flag
+3 offset flag (purpose unknown; see note).
Definition
mapdbentryconnect.h:152
MapDBEntryConnect::MapDBEntry
friend class MapDBEntry
Definition
mapdbentryconnect.h:161
MapDBEntryConnect::worldMapPtr
static const constexpr int worldMapPtr
< Strip source pointer.
Definition
mapdbentryconnect.h:80
MapDBEntryConnect::stripOffset
int stripOffset
Strip position.
Definition
mapdbentryconnect.h:148
MapDBEntryConnect::mapPos
int mapPos() const
Definition
mapdbentryconnect.cpp:151
MapDBEntryConnect::stripSize
int stripSize() const
Definition
mapdbentryconnect.cpp:178
MapDBEntryConnect::dir
ConnectDir dir
Edge direction.
Definition
mapdbentryconnect.h:139
MapDBEntryConnect::xAlign
int xAlign() const
Definition
mapdbentryconnect.cpp:257
MapDBEntryConnect::stripMove
int stripMove
Strip centering.
Definition
mapdbentryconnect.h:145
MapDBEntryConnect::qmlRegister
void qmlRegister() const
Register with QML.
Definition
mapdbentryconnect.cpp:62
MapDBEntryConnect::getStripMove
int getStripMove() const
Definition
mapdbentryconnect.cpp:103
MapDBEntryConnect::qmlProtect
void qmlProtect(const QQmlEngine *const engine) const
Pin to C++ ownership.
Definition
mapdbentryconnect.cpp:78
MapDBEntryConnect::fromMap
MapDBEntry * fromMap
Owning map.
Definition
mapdbentryconnect.h:158
MapDBEntryConnect::getFlag
bool getFlag() const
Definition
mapdbentryconnect.cpp:93
MapDBEntryConnect::window
int window() const
Definition
mapdbentryconnect.cpp:288
MapDBEntryConnect::map
QString map
Connected map name.
Definition
mapdbentryconnect.h:142
MapDBEntryConnect::stripLocation
int stripLocation() const
Definition
mapdbentryconnect.cpp:118
MapDBEntryConnect::yAlign
int yAlign() const
Definition
mapdbentryconnect.cpp:232
MapDBEntryConnect::getToMap
MapDBEntry * getToMap() const
Definition
mapdbentryconnect.cpp:88
MapDBEntryConnect::parent
MapDBEntry * parent
Definition
mapdbentryconnect.h:159
MapDBEntryConnect::getParent
MapDBEntry * getParent() const
Definition
mapdbentryconnect.cpp:73
MapDBEntryConnect::getDir
ConnectDir getDir() const
Definition
mapdbentryconnect.cpp:108
MapDBEntryConnect::getFromMap
MapDBEntry * getFromMap() const
Definition
mapdbentryconnect.cpp:83
MapDBEntryConnect::deepLink
void deepLink()
Resolve the connected map.
Definition
mapdbentryconnect.cpp:52
MapDBEntryConnect::ConnectDir
ConnectDir
The four connectable edges.
Definition
mapdbentryconnect.h:84
MapDBEntryConnect::SOUTH
@ SOUTH
Definition
mapdbentryconnect.h:86
MapDBEntryConnect::NORTH
@ NORTH
Definition
mapdbentryconnect.h:85
MapDBEntryConnect::WEST
@ WEST
Definition
mapdbentryconnect.h:88
MapDBEntryConnect::MapDBEntryConnect
MapDBEntryConnect()
Empty entry.
Definition
mapdbentryconnect.cpp:30
utility.h
projects
db
src
pse-db
entries
mapdbentryconnect.cpp
Generated by
1.17.0