Pokered Save Editor 2
Pokemon Red & Blue save file editor - Qt 6 C++/QML
Loading...
Searching...
No Matches
areamap.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#include "./areamap.h"
23#include "../../qmlownership.h"
25#include "../../savefile.h"
28#include <pse-db/mapsdb.h>
31
32#include <QRandomGenerator>
33
35{
36 load(saveFile);
37}
38
40{
41 for(auto conn : connections)
42 conn->deleteLater();
43}
44
46{
47 return connections.size();
48}
49
51{
52 return qmlCppOwned(connections.value(dir));
53}
54
56{
57 if(!connections.contains(dir))
58 return;
59
60 connections.value(dir)->deleteLater();
61 connections.remove(dir);
63}
64
65void AreaMap::connNew(int dir)
66{
67 connRemove(dir);
68 connections.insert(dir, new MapConnData);
70}
71
73{
74 return MapsDB::inst()->getIndAt(QString::number(curMap));
75}
76
77void AreaMap::load(SaveFile* saveFile)
78{
79 reset();
80
81 if(saveFile == nullptr)
82 return;
83
84 auto toolset = saveFile->toolset;
85
86 curMap = toolset->getByte(0x260A);
88
89 height = toolset->getByte(0x2614);
91
92 width = toolset->getByte(0x2615);
94
95 dataPtr = toolset->getWord(0x2616, true);
97
98 txtPtr = toolset->getWord(0x2618, true);
100
101 scriptPtr = toolset->getWord(0x261A, true);
103
104 height2x2 = toolset->getByte(0x27D0);
106
107 width2x2 = toolset->getByte(0x27D1);
109
110 outOfBoundsBlock = toolset->getByte(0x2659);
112
113 currentTileBlockMapViewPointer = toolset->getWord(0x260B, true);
115
116 // East Connection
117 if(toolset->getBit(0x261C, 1, 0)) {
118 connections.insert((var8)MapDBEntryConnect::ConnectDir::EAST, new MapConnData(saveFile, 0x263E));
120 }
121
122 // West Connection
123 if(toolset->getBit(0x261C, 1, 1)) {
124 connections.insert((var8)MapDBEntryConnect::ConnectDir::WEST, new MapConnData(saveFile, 0x2633));
126 }
127
128 // South Connection
129 if(toolset->getBit(0x261C, 1, 2)) {
132 }
133
134 // North Connection
135 if(toolset->getBit(0x261C, 1, 3)) {
138 }
139
140 mapViewVRAMPointer = toolset->getWord(0x27D2, true);
142
143 forceBikeRide = toolset->getBit(0x29DE, 1, 5);
145
146 blackoutDest = toolset->getBit(0x29DE, 1, 6);
148
149 curMapNextFrame = toolset->getBit(0x29DF, 1, 4);
151
152 cardKeyDoorY = toolset->getByte(0x29EB);
154
155 cardKeyDoorX = toolset->getByte(0x29EC);
157
158 curMapScript = toolset->getByte(0x2CE5);
160}
161
162void AreaMap::save(SaveFile* saveFile)
163{
164 auto toolset = saveFile->toolset;
165
166 toolset->setByte(0x260A, curMap);
167 toolset->setByte(0x2614, height);
168 toolset->setByte(0x2615, width);
169 toolset->setWord(0x2616, dataPtr, true);
170 toolset->setWord(0x2618, txtPtr, true);
171 toolset->setWord(0x261A, scriptPtr, true);
172 toolset->setByte(0x27D0, height2x2);
173 toolset->setByte(0x27D1, width2x2);
174
175 toolset->setWord(0x260B, currentTileBlockMapViewPointer, true);
176
177 toolset->setBit(0x261C, 1, 0, connections.contains((var8)MapDBEntryConnect::ConnectDir::EAST));
178 toolset->setBit(0x261C, 1, 1, connections.contains((var8)MapDBEntryConnect::ConnectDir::WEST));
179 toolset->setBit(0x261C, 1, 2, connections.contains((var8)MapDBEntryConnect::ConnectDir::SOUTH));
180 toolset->setBit(0x261C, 1, 3, connections.contains((var8)MapDBEntryConnect::ConnectDir::NORTH));
181
183 connections.value((var8)MapDBEntryConnect::ConnectDir::EAST)->save(saveFile, 0x263E);
185 connections.value((var8)MapDBEntryConnect::ConnectDir::WEST)->save(saveFile, 0x2633);
187 connections.value((var8)MapDBEntryConnect::ConnectDir::SOUTH)->save(saveFile, 0x2628);
189 connections.value((var8)MapDBEntryConnect::ConnectDir::NORTH)->save(saveFile, 0x261D);
190
191 toolset->setWord(0x27D2, mapViewVRAMPointer, true);
192
193 toolset->setBit(0x29DE, 1, 5, forceBikeRide);
194 toolset->setBit(0x29DE, 1, 6, blackoutDest);
195 toolset->setBit(0x29DF, 1, 4, curMapNextFrame);
196 toolset->setByte(0x29EB, cardKeyDoorY);
197 toolset->setByte(0x29EC, cardKeyDoorX);
198 toolset->setByte(0x2CE5, curMapScript);
199
200 toolset->setByte(0x2659, outOfBoundsBlock);
201}
202
204{
205 // Current Map ID
206 curMap = 0;
208
209 // Map Size including it's double size
210 height = 0;
212
213 width = 0;
214 widthChanged();
215
216 height2x2 = 0;
218
219 width2x2 = 0;
221
222 // Map basic pointers
223 dataPtr = 0;
225
226 txtPtr = 0;
228
229 scriptPtr = 0;
231
232 // Map extra pointers
233 currentTileBlockMapViewPointer = 0; // <- Player coords converted to a ptr
235
236 mapViewVRAMPointer = 0; // <- Unused, reset at start of gameplay
238
239 // Current map script index
240 curMapScript = 0;
242
243 // Unknown ???
244 cardKeyDoorX = 0;
246
247 cardKeyDoorY = 0;
249
250 // Flags that may not be used, unknown
251 forceBikeRide = 0;
253
254 blackoutDest = 0;
256
257 curMapNextFrame = 0;
259
262
263 for(auto conn : connections)
264 conn->deleteLater();
265
266 connections.clear();
268}
269
270void AreaMap::randomize(MapDBEntry* map, int x, int y)
271{
272 reset();
273
274 // Stop here if map is not present
275 if(map == nullptr)
276 return;
277
278 // Current Map ID
279 curMap = map->getInd();
281
282 // Map Size including it's double size
283 height = (map->getHeight() >= 0) ? map->getHeight() : 0;
285
286 width = (map->getWidth() >= 0) ? map->getWidth() : 0;
287 widthChanged();
288
289 height2x2 = (map->getHeight() >= 0) ? map->height2X2() : 0;
291
292 width2x2 = (map->getWidth() >= 0) ? map->width2X2() : 0;
294
295 // Map basic pointers
296 dataPtr = (map->getDataPtr() >= 0) ? map->getDataPtr() : 0;
298
299 txtPtr = (map->getTextPtr() >= 0) ? map->getTextPtr() : 0;
301
302 scriptPtr = (map->getScriptPtr() >= 0) ? map->getScriptPtr() : 0;
304
305 // Map extra pointers
306 if(map->getWidth() >= 0)
309
312
313 if(map->getBorder() >= 0) {
316 }
317
318 // Leave these off for now
319
321 auto conn = new MapConnData;
325 }
327 auto conn = new MapConnData;
331 }
333 auto conn = new MapConnData;
337 }
339 auto conn = new MapConnData;
343 }
344}
345
346void AreaMap::setTo(MapDBEntry* map, int x, int y)
347{
348 // Right now randomize doesn't really randomize and instead just loads the map
349 // and coords given which is what this does. no use in copying it.
350 randomize(map, x, y);
351}
352
353int AreaMap::coordsToPtr(int x, int y, int width)
354{
355 return (width+7)+x+(y*(width+6)) + MapDBEntryConnect::worldMapPtr;
356}
constexpr var16 VramBGPtr
Fixed GB VRAM background-tilemap pointer (never changes).
Definition areamap.h:30
void forceBikeRideChanged()
void setTo(MapDBEntry *map, int x, int y)
Configure to map with the player at (x,y).
Definition areamap.cpp:346
void mapViewVRAMPointerChanged()
void cardKeyDoorYChanged()
int height
Map Size including it's double size.
Definition areamap.h:117
int mapViewVRAMPointer
<- Unused, reset at start of gameplay
Definition areamap.h:129
void blackoutDestChanged()
void height2x2Changed()
bool blackoutDest
Definition areamap.h:140
void widthChanged()
protected::void curMapChanged()
void cardKeyDoorXChanged()
int connCount()
Number of edge connections.
Definition areamap.cpp:45
int currentTileBlockMapViewPointer
<- Player coords converted to a ptr
Definition areamap.h:128
int scriptPtr
Definition areamap.h:125
bool forceBikeRide
Flags that may not be used, unknown.
Definition areamap.h:139
void connRemove(int dir)
Remove the connection in dir.
Definition areamap.cpp:55
int cardKeyDoorX
Unknown ???
Definition areamap.h:135
MapDBEntry * toCurMap()
The current map's DB entry.
Definition areamap.cpp:72
int coordsToPtr(int x, int y, int width)
Converts X & Y values to a pointer for currentTileBlockMapViewPointer.
Definition areamap.cpp:353
int width2x2
Definition areamap.h:120
void scriptPtrChanged()
void load(SaveFile *saveFile=nullptr)
Expand the map block from the save.
Definition areamap.cpp:77
void curMapScriptChanged()
void connNew(int dir)
Add a connection in dir.
Definition areamap.cpp:65
void width2x2Changed()
int outOfBoundsBlock
This is not a tile, it's a block.
Definition areamap.h:114
void outOfBoundsBlockChanged()
QHash< var8, MapConnData * > connections
Edge connections by direction (see the IDE note above).
Definition areamap.h:147
int width
Definition areamap.h:118
int curMap
Current Map ID.
Definition areamap.h:109
void save(SaveFile *saveFile)
Flatten the map block to the save.
Definition areamap.cpp:162
void txtPtrChanged()
void curMapNextFrameChanged()
int cardKeyDoorY
Definition areamap.h:136
virtual ~AreaMap()
Definition areamap.cpp:39
void dataPtrChanged()
void currentTileBlockMapViewPointerChanged()
AreaMap(SaveFile *saveFile=nullptr)
< Current map id.
Definition areamap.cpp:34
MapConnData * connAt(int dir)
Connection in direction dir (GC-protected return).
Definition areamap.cpp:50
void reset()
Blank the map block.
Definition areamap.cpp:203
int curMapScript
Current map script index.
Definition areamap.h:132
bool curMapNextFrame
Definition areamap.h:141
void heightChanged()
void randomize(MapDBEntry *map, int x, int y)
Randomize to map with the player at (x,y).
Definition areamap.cpp:270
void connectionsChanged()
int txtPtr
Definition areamap.h:124
int height2x2
Definition areamap.h:119
int dataPtr
Map basic pointers.
Definition areamap.h:123
One edge connection of the current map (the seam to a neighbouring map).
Definition mapconndata.h:37
void loadFromData(MapDBEntryConnect *connect)
Populate from a map-defined connection.
static MapsDB * inst()
< Number of maps.
Definition mapsdb.cpp:35
MapDBEntry * getIndAt(const QString val) const
Map by name key (for QML).
Definition mapsdb.cpp:155
void setByte(var16 addr, var8 val)
Simply sets a byte.
One loaded save: the raw 32 KB bytes, their expanded object tree, and the tools that move between the...
Definition savefile.h:46
SaveFileToolset * toolset
Tools to operate directly on the raw sav file data.
Definition savefile.h:117
var8e var8
Everyday 8-bit alias. Exact (not "fastest") to dodge the pointer-width bug noted above.
Definition types.h:124
qmlCppOwned() – protect Q_INVOKABLE QObject returns from QML's GC.
static T * qmlCppOwned(T *obj)
Hand QML CppOwnership of a C++-owned QObject returned from a Q_INVOKABLE.
static const constexpr int worldMapPtr
< Strip source pointer.
One map's complete static definition – the root of the MapDBEntry family.
Definition mapdbentry.h:56
int getDataPtr() const
int getWidth() const
int getScriptPtr() const
int getBorder() const
int getTextPtr() const
int height2X2() const
Height x2 (derived; see note).
int width2X2() const
Width x2 (derived; see note).
int getHeight() const
int getInd() const
const QHash< int, MapDBEntryConnect * > getConnect() const
Edge connections by direction.