Pokered Save Editor 2
Pokemon Red & Blue save file editor - Qt 6 C++/QML
Loading...
Searching...
No Matches
hofrecord.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 "hofrecord.h"
23#include "../../qmlownership.h"
24#include "./hofpokemon.h"
25#include "../../savefile.h"
28#include <pse-common/random.h>
29
31{
32 load(saveFile, ind);
33}
34
36{
37 for(auto entry : pokemon)
38 entry->deleteLater();
39}
40
42{
43 return pokemon.size();
44}
45
47{
48 return maxPokemon;
49}
50
52{
53 return qmlCppOwned(pokemon.at(ind));
54}
55
56void HoFRecord::pokemonSwap(int from, int to)
57{
58 auto eFrom = pokemon.at(from);
59 auto eTo = pokemon.at(to);
60
61 pokemon.replace(from, eTo);
62 pokemon.replace(to, eFrom);
63
65}
66
68{
69 // Can't have a team of no Pokemon
70 if(pokemon.size() <= 1)
71 return;
72
73 pokemon.at(ind)->deleteLater();
74
75 pokemon.removeAt(ind);
77}
78
80{
81 if(pokemon.size() >= maxPokemon)
82 return;
83
84 pokemon.append(new HoFPokemon);
86}
87
88void HoFRecord::load(SaveFile* saveFile, var8 ind)
89{
90 // Reset to clear list as all the entries have ti be deleted first
91 reset();
92
93 if(saveFile == nullptr) {
94 return;
95 }
96
97 auto toolset = saveFile->toolset;
98
99 // Calculate HoF Offset for this record
100 // Records are 0x60 in size, all records start at 0x598
101 // Multiply record number with 0x60 (Record Size) and add to offset
102 // of first record (Record 0) which is 0x598
103 var16 offset = (0x60 * ind) + 0x598;
104
105 for (var8 i = 0; i < 6; i++) {
106 // If Pokemon doesn't exist then don't proceed any further
107 // the data stop code is 0xFF
108 var16 pokemonOffset = (0x10 * i) + offset;
109 var8 speciesByte = toolset->getByte(pokemonOffset + 0);
110 if (speciesByte == 0xFF)
111 break;
112
113 pokemon.append(new HoFPokemon(saveFile, offset, i));
114 }
115
117}
118
119void HoFRecord::save(SaveFile* saveFile, var8 ind)
120{
121 auto toolset = saveFile->toolset;
122 var16 offset = (0x60 * ind) + 0x598;
123
124 for (var8 i = 0; i < pokemon.size(); i++) {
125 pokemon.at(i)->save(saveFile, offset, i);
126 }
127
128 // If the record isn't filled up with 6 Pokemon then
129 // we need to insert an ending marker and not touch the rest of the bytes
130 if(pokemon.size() >= 6)
131 return;
132
133 // Calculate the ending marker position which is
134 // 1 byte after all record data and set to 0xFF thus sealing the rest
135 // of the data
136
137 // Pokemon Record Size * Pokemon Record Number + Record Start Location
138 var16 endingOffset = (0x10 * pokemon.size()) + offset;
139 toolset->setByte(endingOffset, 0xFF);
140}
141
143{
144 for(auto entry : pokemon)
145 entry->deleteLater();
146
147 pokemon.clear();
149}
150
152{
153 // Reset
154 reset();
155
156 // Get a random amount of Pokemon between 1-6
157 var8 size = Random::inst()->rangeInclusive(1,6);
158
159 // Create blank Pokemon entries
160 for(var8 i = 0; i < size; i++)
161 pokemon.append(new HoFPokemon);
162
164
165 // Insert random data
166 for(auto entry : pokemon)
167 entry->randomize();
168}
One Pokemon entry within a Hall of Fame record: species, level, name.
Definition hofpokemon.h:34
void pokemonNew()
Add a fresh mon.
Definition hofrecord.cpp:79
void save(SaveFile *saveFile, var8 ind)
Flatten record ind to the save.
protected::void pokemonChanged()
The record's mon list changed.
void reset()
Empty this record.
void randomize()
Fill with random mons.
void load(SaveFile *saveFile=nullptr, var8 ind=0)
Expand record ind from the save.
Definition hofrecord.cpp:88
HoFRecord(SaveFile *saveFile=nullptr, var8 ind=0)
Definition hofrecord.cpp:30
QVector< HoFPokemon * > pokemon
The recorded team.
Definition hofrecord.h:63
int pokemonMax()
Capacity (maxPokemon).
Definition hofrecord.cpp:46
void pokemonRemove(int ind)
Remove mon ind.
Definition hofrecord.cpp:67
HoFPokemon * pokemonAt(int ind)
Mon at ind (GC-protected return).
Definition hofrecord.cpp:51
void pokemonSwap(int from, int to)
Reorder mons.
Definition hofrecord.cpp:56
int pokemonCount()
Mons in this record.
Definition hofrecord.cpp:41
virtual ~HoFRecord()
Definition hofrecord.cpp:35
int rangeInclusive(const int start, const int end) const
Random integer in the closed interval [start, end].
Definition random.cpp:42
static Random * inst()
< Convenience 50% coin flip (integer path), readable from QML.
Definition random.cpp:31
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
var16e var16
Everyday 16-bit alias. Exact width to avoid the "fastest" widening bug.
Definition types.h:125
constexpr var8 maxPokemon
Mons recorded per Hall of Fame entry (a full party).
Definition hofrecord.h:25
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.