Pokered Save Editor 2
Pokemon Red & Blue save file editor - Qt 6 C++/QML
Loading...
Searching...
No Matches
halloffame.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 "./halloffame.h"
24#include "../qmlownership.h"
25#include "../savefile.h"
26#include "../savefiletoolset.h"
27#include "../savefileiterator.h"
29#include <pse-common/random.h>
30
32{
33 load(saveFile);
34}
35
37{
38 for(auto record : records)
39 record->deleteLater();
40}
41
43{
44 return records.size();
45}
46
48{
49 return recordsMax;
50}
51
53{
54 return qmlCppOwned(records.at(ind));
55}
56
57void HallOfFame::recordSwap(int from, int to)
58{
59 auto fRecord = records.at(from);
60 auto tRecord = records.at(to);
61
62 records.replace(from, tRecord);
63 records.replace(to, fRecord);
64
66}
67
69{
70 if(records.size() < 1)
71 return;
72
73 records.at(ind)->deleteLater();
74 records.remove(ind);
75
77}
78
80{
81 if(records.size() >= recordsMax)
82 return;
83
84 records.append(new HoFRecord);
86}
87
89{
90 reset();
91
92 if(saveFile == nullptr)
93 return;
94
95 auto toolset = saveFile->toolset;
96
97 var8 hofRecordCount = toolset->getByte(0x284E);
98 for (var8 i = 0; i < hofRecordCount && i < recordsMax; i++) {
99 records.append(new HoFRecord(saveFile, i));
100 }
101
103}
104
106{
107 auto toolset = saveFile->toolset;
108
109 toolset->setByte(0x284E, records.size());
110 for (var8 i = 0; i < records.size() && i < recordsMax; i++) {
111 records.at(i)->save(saveFile, i);
112 }
113}
114
116{
117 for(auto record : records)
118 record->deleteLater();
119
120 records.clear();
122}
123
125{
126 reset();
127
128 // Create up to 5 random records
129 var8 rndCount = Random::inst()->rangeInclusive(0, 5);
130
131 for (var8 i = 0; i < rndCount && i < recordsMax; i++) {
132 auto tmp = new HoFRecord;
133 records.append(tmp);
134 tmp->randomize();
135 }
136
138}
QVector< HoFRecord * > records
Every Hall of Fame record.
Definition halloffame.h:68
int recordCount()
Number of records.
void save(SaveFile *saveFile)
Flatten the Hall of Fame records to the save.
void randomize()
Fill with random records.
virtual ~HallOfFame()
HallOfFame(SaveFile *saveFile=nullptr)
HoFRecord * recordAt(int ind)
Record ind (GC-protected return).
void load(SaveFile *saveFile=nullptr)
Expand the Hall of Fame records from the save.
protected::void recordsChanged()
void reset()
Clear all records.
void recordNew()
Add a fresh record.
int recordMax()
Capacity (recordsMax).
void recordSwap(int from, int to)
Reorder records.
void recordRemove(int ind)
Remove record ind.
One Hall of Fame record: the team that beat the Elite Four, in order.
Definition hofrecord.h:37
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
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
constexpr var8 recordsMax
Maximum Hall of Fame records the save keeps.
Definition halloffame.h:26
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.