Pokered Save Editor 2
Pokemon Red & Blue save file editor - Qt 6 C++/QML
Loading...
Searching...
No Matches
daycare.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 "./daycare.h"
26#include "../savefile.h"
27#include "../savefiletoolset.h"
28#include "../savefileiterator.h"
29#include <pse-common/random.h>
30
32{
33 load(saveFile);
34}
35
37{
38 // Guard the empty Day Care: `pokemon` is nullptr when no mon is deposited
39 // (load() only allocates it when 0x2CF4 > 0; reset() leaves it null). The old
40 // unconditional pokemon->deleteLater() dereferenced a null `this` and crashed
41 // whenever an empty Daycare was destroyed -- masked in the running app because a
42 // SaveFile is only torn down at process exit. reset() already guards the same way.
43 if(pokemon != nullptr)
44 pokemon->deleteLater();
45}
46
47void Daycare::load(SaveFile* saveFile)
48{
49 reset();
50
51 if(saveFile == nullptr)
52 return;
53
54 auto toolset = saveFile->toolset;
55
56 // Is the daycare in use, if so extract daycare Pokemon Information
57 if (toolset->getByte(0x2CF4) > 0) {
58 pokemon = new PokemonBox(saveFile, 0x2D0B, 0x2CF5, 0x2D00, 0);
60 }
61}
62
63void Daycare::save(SaveFile* saveFile)
64{
65 auto toolset = saveFile->toolset;
66
67 toolset->setByte(0x2CF4, (pokemon != nullptr) ? 1 : 0);
68
69 if(pokemon != nullptr)
70 pokemon->save(saveFile, 0x2D0B, -1, 0x2CF5, 0x2D00, 0);
71}
72
74{
75 if(pokemon != nullptr) {
76 pokemon->deleteLater();
77 }
78
79 pokemon = nullptr;
81}
82
84{
85 reset();
86
87 // Give a 50/50 chance the daycare will be in use
88 if(!Random::inst()->flipCoin())
89 return;
90
91 pokemon = new PokemonBox;
92 pokemon->randomize(basics);
93
95}
virtual ~Daycare()
Definition daycare.cpp:36
void reset()
Empty the Day Care.
Definition daycare.cpp:73
Daycare(SaveFile *saveFile=nullptr)
< The Pokemon left at the Day Care (may be empty).
Definition daycare.cpp:31
protected::void pokemonChanged()
void load(SaveFile *saveFile=nullptr)
Expand the day-care mon from the save.
Definition daycare.cpp:47
void save(SaveFile *saveFile)
Flatten the day-care mon to the save.
Definition daycare.cpp:63
void randomize(PlayerBasics *basics)
Put a random mon in the Day Care.
Definition daycare.cpp:83
PokemonBox * pokemon
Definition daycare.h:62
The trainer's headline values: name, ID, money, coins, badges, starter.
A single Pokemon record – the most property-rich object in the tree.
Definition pokemonbox.h:213
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