Pokered Save Editor 2
Pokemon Red & Blue save file editor - Qt 6 C++/QML
Loading...
Searching...
No Matches
areawarps.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 "./areawarps.h"
24#include "../../qmlownership.h"
26#include "../../savefile.h"
29#include <pse-db/mapsdb.h>
32#include <pse-common/random.h>
33
35{
36 load(saveFile);
37}
38
40 for(auto warp : warps)
41 warp->deleteLater();
42}
43
45{
46 return warps.size();
47}
48
50{
51 return maxWarps;
52}
53
55{
56 return qmlCppOwned(warps.at(ind));
57}
58
59void AreaWarps::warpSwap(int from, int to)
60{
61 auto eFrom = warps.at(from);
62 auto eTo = warps.at(to);
63
64 warps.replace(from, eTo);
65 warps.replace(to, eFrom);
66
68}
69
71{
72 if(warps.size() <= 0)
73 return;
74
75 warps.at(ind)->deleteLater();
76 warps.removeAt(ind);
78}
79
81{
82 if(warps.size() >= maxWarps)
83 return;
84
85 warps.append(new WarpData);
87}
88
90{
91 reset();
92
93 if(saveFile == nullptr)
94 return;
95
96 auto toolset = saveFile->toolset;
97
98 for (var8 i = 0; i < toolset->getByte(0x265A) && i < 32; i++) {
99 warps.append(new WarpData(saveFile, i));
100 }
101
102 warpsChanged();
103
104 warpDest = toolset->getByte(0x26DB);
106
107 dungeonWarpDestMap = toolset->getByte(0x29C9);
109
110 specialWarpDestMap = toolset->getByte(0x29C6);
112
113 whichDungeonWarp = toolset->getByte(0x29CA);
115
116 scriptedWarp = toolset->getBit(0x29D9, 1, 3);
118
119 isDungeonWarp = toolset->getBit(0x29D9, 1, 4);
121
122 flyOrDungeonWarp = toolset->getBit(0x29DE, 1, 2);
124
125 flyWarp = toolset->getBit(0x29DE, 1, 3);
127
128 dungeonWarp = toolset->getBit(0x29DE, 1, 4);
130
131 skipJoypadCheckWarps = toolset->getBit(0x29DF, 1, 2);
133
134 warpedFromWarp = toolset->getByte(0x29E7);
136
137 warpedfromMap = toolset->getByte(0x29E8);
139}
140
142{
143 auto toolset = saveFile->toolset;
144
145 toolset->setByte(0x265A, warps.size());
146
147 for (var8 i = 0; i < warps.size() && i < 32; i++) {
148 warps.at(i)->save(saveFile, i);
149 }
150
151 toolset->setByte(0x26DB, warpDest);
152 toolset->setByte(0x29C9, dungeonWarpDestMap);
153 toolset->setByte(0x29C6, specialWarpDestMap);
154 toolset->setByte(0x29CA, whichDungeonWarp);
155 toolset->setBit(0x29D9, 1, 3, scriptedWarp);
156 toolset->setBit(0x29D9, 1, 4, isDungeonWarp);
157 toolset->setBit(0x29DE, 1, 2, flyOrDungeonWarp);
158 toolset->setBit(0x29DE, 1, 3, flyWarp);
159 toolset->setBit(0x29DE, 1, 4, dungeonWarp);
160 toolset->setBit(0x29DF, 1, 2, skipJoypadCheckWarps);
161 toolset->setByte(0x29E7, warpedFromWarp);
162 toolset->setByte(0x29E8, warpedfromMap);
163}
164
166{
167 scriptedWarp = false;
169
170 isDungeonWarp = false;
172
173 skipJoypadCheckWarps = false;
175
176 warpDest = 0xFF;
178
181
184
185 flyOrDungeonWarp = false;
187
188 flyWarp = false;
190
191 dungeonWarp = false;
193
196
197 warpedFromWarp = 0;
199
200 warpedfromMap = 0;
202
203 for(auto warp : warps)
204 warp->deleteLater();
205
206 warps.clear();
207 warpsChanged();
208}
209
211{
212 reset();
213
214 // Pick random map that you came from
215 auto dungeonWarp = MapsDB::inst()->search()->isGood()->isType("Cave")->pickRandom();
216
217 // Assign index
220
221 // Pick another random map for special warp destination
222 specialWarpDestMap = MapsDB::inst()->search()->isGood()->pickRandom()->getInd();
224
225 // Make up some random warps on said dungeon warp
226 whichDungeonWarp = Random::inst()->rangeExclusive(0, dungeonWarp->getWarpOut().size());
228
229 warpedFromWarp = Random::inst()->rangeExclusive(0, dungeonWarp->getWarpOut().size());
231
232 // Re-create all map warps out, we can't blantly make-up stuff here
233 // and have to be careful
234 for(auto warpDataEntry : map->getWarpOut()) {
235 auto tmp = new WarpData(warpDataEntry);
236
237 // Randomize it so long as it's not a return warp
238 // Return warps return you back outside, for now we'll leave them as they
239 // are
240 if(tmp->destMap != 0xFF)
241 tmp->randomize();
242
243 warps.append(tmp);
244 }
245
246 warpsChanged();
247}
248
250{
251 reset();
252
253 // Pick random map that you came from
254 auto dungeonWarp = MapsDB::inst()->search()->isGood()->isType("Cave")->pickRandom();
255
256 // Assign index
259
260 // Pick another random map for special warp destination
261 specialWarpDestMap = MapsDB::inst()->search()->isGood()->pickRandom()->getInd();
263
264 // Make up some random warps on said dungeon warp
265 whichDungeonWarp = Random::inst()->rangeExclusive(0, dungeonWarp->getWarpOut().size());
267
268 warpedFromWarp = Random::inst()->rangeExclusive(0, dungeonWarp->getWarpOut().size());
270
271 // Re-create all map warps out, we can't blantly make-up stuff here
272 // and have to be careful
273 for(auto warpDataEntry : map->getWarpOut()) {
274 auto tmp = new WarpData(warpDataEntry);
275 warps.append(tmp);
276 }
277
278 warpsChanged();
279}
constexpr var8 maxWarps
Maximum warps on a map.
Definition areawarps.h:26
void warpNew()
Add a fresh warp.
Definition areawarps.cpp:80
protected::void scriptedWarpChanged()
void flyOrDungeonWarpChanged()
bool skipJoypadCheckWarps
Skips check for warp after not collided (Forced Warp)??
Definition areawarps.h:94
int warpedFromWarp
Warped from which warp.
Definition areawarps.h:106
WarpData * warpAt(int ind)
Warp ind (GC-protected return).
Definition areawarps.cpp:54
int whichDungeonWarp
Warped from which dungeon warp.
Definition areawarps.h:105
void dungeonWarpChanged()
int specialWarpDestMap
Destination Map for special warps.
Definition areawarps.h:99
void warpSwap(int from, int to)
Reorder warps.
Definition areawarps.cpp:59
void warpedfromMapChanged()
AreaWarps(SaveFile *saveFile=nullptr)
< Do a scripted warp.
Definition areawarps.cpp:34
bool isDungeonWarp
On a dungeon warp.
Definition areawarps.h:93
void whichDungeonWarpChanged()
void flyWarpChanged()
void warpsChanged()
void randomize(MapDBEntry *map)
Randomize warps for map.
bool flyOrDungeonWarp
Is a fly or dungeon warp.
Definition areawarps.h:100
virtual ~AreaWarps()
Definition areawarps.cpp:39
int warpDest
Warp actively warping to or 0xFF to warp to same position.
Definition areawarps.h:97
int warpedfromMap
Warped from which map.
Definition areawarps.h:107
bool scriptedWarp
Do a scripted warp.
Definition areawarps.h:92
void dungeonWarpDestMapChanged()
int warpMax()
Capacity (maxWarps).
Definition areawarps.cpp:49
int warpCount()
Number of warps.
Definition areawarps.cpp:44
void setTo(MapDBEntry *map)
Rebuild warps from map.
void save(SaveFile *saveFile)
Flatten the warp list + state to the save.
int dungeonWarpDestMap
Destination Map for dungeon warps.
Definition areawarps.h:98
bool dungeonWarp
Is a dungeon warp.
Definition areawarps.h:102
void warpDestChanged()
void warpedFromWarpChanged()
void load(SaveFile *saveFile=nullptr)
Expand the warp list + state from the save.
Definition areawarps.cpp:89
void specialWarpDestMapChanged()
void reset()
Empty warps + clear state.
void isDungeonWarpChanged()
bool flyWarp
Is a fly warp.
Definition areawarps.h:101
void warpRemove(int ind)
Remove warp ind.
Definition areawarps.cpp:70
void skipJoypadCheckWarpsChanged()
QVector< WarpData * > warps
The map's warp points.
Definition areawarps.h:109
static MapsDB * inst()
< Number of maps.
Definition mapsdb.cpp:35
QScopedPointer< MapSearch, QScopedPointerDeleteLater > search() const
C++-owned finder (smart pointer).
Definition mapsdb.cpp:141
static Random * inst()
< Convenience 50% coin flip (integer path), readable from QML.
Definition random.cpp:31
int rangeExclusive(const int start, const int end) const
Random integer in the half-open interval [start, end).
Definition random.cpp:53
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
One warp point on the current map: its tile and where it leads.
Definition warpdata.h:36
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.
One map's complete static definition – the root of the MapDBEntry family.
Definition mapdbentry.h:56
const QVector< MapDBEntryWarpOut * > getWarpOut() const
Outgoing warps.