Pokered Save Editor 2
Pokemon Red & Blue save file editor - Qt 6 C++/QML
Loading...
Searching...
No Matches
playerpokemon.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 "playerpokemon.h"
23#include "../../qmlownership.h"
24#include "./player.h"
25#include "./playerbasics.h"
26#include "../../savefile.h"
30#include <pse-db/moves.h>
31#include <pse-common/random.h>
32#include "../savefileexpanded.h"
33
36{
37 isParty = true;
38 maxSize = 6;
39 load(saveFile);
40}
41
43
44void PlayerPokemon::load(SaveFile* saveFile, var16 boxOffset)
45{
46 Q_UNUSED(boxOffset)
47
48 reset();
49
50 this->file = saveFile;
51
52 if(saveFile == nullptr)
53 return;
54
55 auto toolset = saveFile->toolset;
56
57 for (var8 i = 0; i < toolset->getByte(0x2F2C) && i < 6; i++) {
58 pokemon.append(new PokemonParty(
59 saveFile,
60 0x2F34,
61 0x307E,
62 0x303C,
63 i));
64
66 }
67
69}
70
71void PlayerPokemon::save(SaveFile* saveFile, var16 boxOffset)
72{
73 Q_UNUSED(boxOffset)
74
75 auto toolset = saveFile->toolset;
76
77 // Set party length and save current party data
78 toolset->setByte(0x2F2C, pokemon.size());
79
80 for(var8 i = 0; i < pokemon.size() && i < 6; i++) {
81 pokemon.at(i)->save(
82 saveFile,
83 0x2F34,
84 0x2F2D,
85 0x307E,
86 0x303C,
87 i
88 );
89 }
90
91 // Mark end of species list if not full party
92 if(pokemon.size() >= 6)
93 return;
94
95 var16 speciesOffset = 0x2F2D + pokemon.size();
96 toolset->setByte(speciesOffset, 0xFF);
97}
98
100{
101 return qmlCppOwned((PokemonParty*)pokemon.at(ind));
102}
103
105{
106 // Randomize up to 5 Pokemon
107 var8 count = Random::inst()->rangeInclusive(1, 5);
108
109 // Clear Pokemon Party and add them in
110 reset();
111
112 for(var8 i = 0; i < count; i++) {
113 auto tmp = new PokemonParty;
114 pokemon.append(tmp);
115 tmp->randomize(basics);
117 }
118
119 // Give an extra Pokemon that's an HM slave
120 // I have no idea where randomize will drop you so to be able to progress
121 // in the game you need to be able to get around
122 auto tmp = new PokemonParty;
123 pokemon.append(tmp);
124 tmp->randomize(basics);
125
126 tmp->clearMoves();
127
128 // Add in 4 most important HM's
129 tmp->changeMove(0, MovesDB::inst()->getIndAt("FLY")->ind);
130 tmp->changeMove(1, MovesDB::inst()->getIndAt("SURF")->ind);
131 tmp->changeMove(2, MovesDB::inst()->getIndAt("STRENGTH")->ind);
132 tmp->changeMove(3, MovesDB::inst()->getIndAt("CUT")->ind);
133
134 // Generate random PP Ups and heal PP like other Pokemon
135 for(auto move : tmp->moves) {
136 // Generate random PP Ups
137 move->ppUp = Random::inst()->rangeInclusive(0, 3);
138
139 // Restore PP of move
140 move->restorePP();
141 }
142
145}
146
148{
149 if(pokemon.size() >= maxSize)
150 return;
151
153 pokemon.append(mon);
156}
static MovesDB * inst()
< Number of moves.
Definition moves.cpp:72
The trainer's headline values: name, ID, money, coins, badges, starter.
virtual void load(SaveFile *saveFile=nullptr, var16 boxOffset=0)
Expand the party from the save (boxOffset locates the party block).
PokemonParty * partyAt(int ind)
Party member at ind (GC-protected return).
virtual ~PlayerPokemon()
virtual void randomize(PlayerBasics *basics)
Randomize the party; basics supplies starter/OT context.
PlayerPokemon(SaveFile *saveFile=nullptr)
virtual void save(SaveFile *saveFile, var16 boxOffset=0)
Flatten the party back to the save (boxOffset locates the party block).
virtual void pokemonNew()
Add/initialize a fresh party Pokemon.
static PokemonBox * newPokemon(PokemonRandom::PokemonRandom_ list=PokemonRandom::Random_Starters, PlayerBasics *basics=nullptr)
A party Pokemon: a PokemonBox plus the five pre-generated battle stats.
static PokemonParty * convertToParty(PokemonBox *data)
New party mon from a box record (regenerates stats).
bool isParty
True if this box is actually the party (affects record format).
void pokemonChanged()
Box contents changed.
void pokemonInsertChange()
A mon was inserted.
QVector< PokemonBox * > pokemon
The stored mons.
void reset()
Empty the box.
SaveFile * file
Owning save.
PokemonStorageBox(int maxSize=boxMaxPokemon, SaveFile *saveFile=nullptr, var16 boxOffset=0)
< How many mons are in the box.
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
var16e var16
Everyday 16-bit alias. Exact width to avoid the "fastest" widening bug.
Definition types.h:125
constexpr var8 boxMaxPokemon
Capacity of a single PC box.
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.
@ Random_Starters
A "startery"-feeling Pokemon (non-legendary base evo).
Definition pokemonbox.h:115