Pokered Save Editor 2
Pokemon Red & Blue save file editor - Qt 6 C++/QML
Loading...
Searching...
No Matches
hofpokemon.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 "hofpokemon.h"
23#include "../../savefile.h"
26#include <pse-db/pokemon.h>
27#include <pse-db/names.h>
28#include <pse-common/random.h>
29
30HoFPokemon::HoFPokemon(SaveFile* saveFile, var16 recordOffset, var16 ind)
31{
32 load(saveFile, recordOffset, ind);
33}
34
36
37void HoFPokemon::load(SaveFile* saveFile, var16 recordOffset, var16 ind)
38{
39 reset();
40
41 // Null check MUST come before dereferencing saveFile. The default-constructed
42 // HoFPokemon (HoFRecord::randomize / pokemonNew use `new HoFPokemon`) passes a
43 // null saveFile; the old order read saveFile->toolset first and crashed.
44 if(saveFile == nullptr)
45 return;
46
47 auto toolset = saveFile->toolset;
48
49 // Calculate Pokemon Offset in the record
50 // Records are 0x10 in size
51 // Multiply record number with 0x10 (Record Size) and add to offset
52 // record start
53 var16 pokemonOffset = (0x10 * ind) + recordOffset;
54
58
59 // Extract Pokemon Data
60 species = toolset->getByte(pokemonOffset + 0);
62
63 level = toolset->getByte(pokemonOffset + 1);
65
66 name = toolset->getStr(pokemonOffset + 2, 0xB, 10+1);
68}
69
70void HoFPokemon::save(SaveFile* saveFile, var16 recordOffset, var16 ind)
71{
72 var16 pokemonOffset = (0x10 * ind) + recordOffset;
73 auto toolset = saveFile->toolset;
74
75 toolset->setByte(pokemonOffset + 0, species);
76 toolset->setByte(pokemonOffset + 1, level);
77 toolset->setStr(pokemonOffset + 2, 0xB, 10+1, name);
78
79 // Normally the Gameboy will actively zero out padding bytes
80 // but we don't set padding bytes per the strict "Only touch whats needed" rule
81}
82
84{
85 species = 0;
87
88 level = 0;
90
91 name = "";
93}
94
96{
97 // Reset
98 reset();
99
100 // Generate random dex entry and look it up to get species number
102 auto toPokemon = PokemonDB::inst()->getIndAt("dex" + QString::number(dex));
103
104 if(toPokemon != nullptr) {
105 species = toPokemon->ind;
107 }
108
109 // Random level between 1 - 100
111 levelChanged();
112
113 // Random name
115 nameChanged();
116}
117
119{
120 return PokemonDB::inst()->getIndAt(QString::number(species));
121}
QString randomExample()
A random string from the list.
QString name
Definition hofpokemon.h:63
void nameChanged()
void levelChanged()
void reset()
Blank this entry.
void save(SaveFile *saveFile, var16 recordOffset, var16 ind)
Flatten to the save.
void randomize()
Randomize this entry.
void load(SaveFile *saveFile=nullptr, var16 recordOffset=0, var16 ind=0)
Expand from the save.
HoFPokemon(SaveFile *saveFile=nullptr, var16 recordOffset=0, var16 ind=0)
< Species id.
PokemonDBEntry * toSpecies()
Resolve species to its DB entry.
virtual ~HoFPokemon()
protected::void speciesChanged()
NamesPlayer * player() const
The player-name source (backs player).
Definition names.cpp:35
static Names * inst()
< Random player-name source.
Definition names.cpp:29
static PokemonDB * inst()
< Number of species.
Definition pokemon.cpp:183
PokemonDBEntry * getIndAt(const QString &key) const
Species by name key (for QML).
Definition pokemon.cpp:199
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
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
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 pokemonDexCount
Number of species.
Definition pokemon.h:28
constexpr var8 pokemonLevelMax
Maximum level.
Definition pokemon.h:29
One species' complete static data – the richest entry in the db layer.
Definition pokemon.h:98