Pokered Save Editor 2
Pokemon Red & Blue save file editor - Qt 6 C++/QML
Toggle main menu visibility
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
"
26
#include "
../../savefiletoolset.h
"
27
#include "
../../savefileiterator.h
"
28
#include <
pse-common/random.h
>
29
30
HoFRecord::HoFRecord
(
SaveFile
* saveFile,
var8
ind)
31
{
32
load
(saveFile, ind);
33
}
34
35
HoFRecord::~HoFRecord
()
36
{
37
for
(
auto
entry :
pokemon
)
38
entry->deleteLater();
39
}
40
41
int
HoFRecord::pokemonCount
()
42
{
43
return
pokemon
.size();
44
}
45
46
int
HoFRecord::pokemonMax
()
47
{
48
return
maxPokemon
;
49
}
50
51
HoFPokemon
*
HoFRecord::pokemonAt
(
int
ind)
52
{
53
return
qmlCppOwned
(
pokemon
.at(ind));
54
}
55
56
void
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
64
pokemonChanged
();
65
}
66
67
void
HoFRecord::pokemonRemove
(
int
ind)
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);
76
pokemonChanged
();
77
}
78
79
void
HoFRecord::pokemonNew
()
80
{
81
if
(
pokemon
.size() >=
maxPokemon
)
82
return
;
83
84
pokemon
.append(
new
HoFPokemon
);
85
pokemonChanged
();
86
}
87
88
void
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
116
pokemonChanged
();
117
}
118
119
void
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
142
void
HoFRecord::reset
()
143
{
144
for
(
auto
entry :
pokemon
)
145
entry->deleteLater();
146
147
pokemon
.clear();
148
pokemonChanged
();
149
}
150
151
void
HoFRecord::randomize
()
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
163
pokemonChanged
();
164
165
// Insert random data
166
for
(
auto
entry :
pokemon
)
167
entry->randomize();
168
}
HoFPokemon
One Pokemon entry within a Hall of Fame record: species, level, name.
Definition
hofpokemon.h:34
HoFRecord::pokemonNew
void pokemonNew()
Add a fresh mon.
Definition
hofrecord.cpp:79
HoFRecord::save
void save(SaveFile *saveFile, var8 ind)
Flatten record ind to the save.
Definition
hofrecord.cpp:119
HoFRecord::pokemonChanged
protected::void pokemonChanged()
The record's mon list changed.
HoFRecord::reset
void reset()
Empty this record.
Definition
hofrecord.cpp:142
HoFRecord::randomize
void randomize()
Fill with random mons.
Definition
hofrecord.cpp:151
HoFRecord::load
void load(SaveFile *saveFile=nullptr, var8 ind=0)
Expand record ind from the save.
Definition
hofrecord.cpp:88
HoFRecord::HoFRecord
HoFRecord(SaveFile *saveFile=nullptr, var8 ind=0)
Definition
hofrecord.cpp:30
HoFRecord::pokemon
QVector< HoFPokemon * > pokemon
The recorded team.
Definition
hofrecord.h:63
HoFRecord::pokemonMax
int pokemonMax()
Capacity (maxPokemon).
Definition
hofrecord.cpp:46
HoFRecord::pokemonRemove
void pokemonRemove(int ind)
Remove mon ind.
Definition
hofrecord.cpp:67
HoFRecord::pokemonAt
HoFPokemon * pokemonAt(int ind)
Mon at ind (GC-protected return).
Definition
hofrecord.cpp:51
HoFRecord::pokemonSwap
void pokemonSwap(int from, int to)
Reorder mons.
Definition
hofrecord.cpp:56
HoFRecord::pokemonCount
int pokemonCount()
Mons in this record.
Definition
hofrecord.cpp:41
HoFRecord::~HoFRecord
virtual ~HoFRecord()
Definition
hofrecord.cpp:35
Random::rangeInclusive
int rangeInclusive(const int start, const int end) const
Random integer in the closed interval [start, end].
Definition
random.cpp:42
Random::inst
static Random * inst()
< Convenience 50% coin flip (integer path), readable from QML.
Definition
random.cpp:31
SaveFile
One loaded save: the raw 32 KB bytes, their expanded object tree, and the tools that move between the...
Definition
savefile.h:46
SaveFile::toolset
SaveFileToolset * toolset
Tools to operate directly on the raw sav file data.
Definition
savefile.h:117
var8
var8e var8
Everyday 8-bit alias. Exact (not "fastest") to dodge the pointer-width bug noted above.
Definition
types.h:124
var16
var16e var16
Everyday 16-bit alias. Exact width to avoid the "fastest" widening bug.
Definition
types.h:125
hofpokemon.h
hofrecord.h
maxPokemon
constexpr var8 maxPokemon
Mons recorded per Hall of Fame entry (a full party).
Definition
hofrecord.h:25
qmlownership.h
qmlCppOwned() – protect Q_INVOKABLE QObject returns from QML's GC.
qmlCppOwned
static T * qmlCppOwned(T *obj)
Hand QML CppOwnership of a C++-owned QObject returned from a Q_INVOKABLE.
Definition
qmlownership.h:43
random.h
savefile.h
savefileiterator.h
savefiletoolset.h
projects
savefile
src
pse-savefile
expanded
fragments
hofrecord.cpp
Generated by
1.17.0