Pokered Save Editor 2
Pokemon Red & Blue save file editor - Qt 6 C++/QML
Toggle main menu visibility
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
"
24
#include "
./fragments/pokemonbox.h
"
25
#include "
./player/playerbasics.h
"
26
#include "
../savefile.h
"
27
#include "
../savefiletoolset.h
"
28
#include "
../savefileiterator.h
"
29
#include <
pse-common/random.h
>
30
31
Daycare::Daycare
(
SaveFile
* saveFile)
32
{
33
load
(saveFile);
34
}
35
36
Daycare::~Daycare
()
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
47
void
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);
59
pokemonChanged
();
60
}
61
}
62
63
void
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
73
void
Daycare::reset
()
74
{
75
if
(
pokemon
!=
nullptr
) {
76
pokemon
->deleteLater();
77
}
78
79
pokemon
=
nullptr
;
80
pokemonChanged
();
81
}
82
83
void
Daycare::randomize
(
PlayerBasics
* basics)
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
94
pokemonChanged
();
95
}
Daycare::~Daycare
virtual ~Daycare()
Definition
daycare.cpp:36
Daycare::reset
void reset()
Empty the Day Care.
Definition
daycare.cpp:73
Daycare::Daycare
Daycare(SaveFile *saveFile=nullptr)
< The Pokemon left at the Day Care (may be empty).
Definition
daycare.cpp:31
Daycare::pokemonChanged
protected::void pokemonChanged()
Daycare::load
void load(SaveFile *saveFile=nullptr)
Expand the day-care mon from the save.
Definition
daycare.cpp:47
Daycare::save
void save(SaveFile *saveFile)
Flatten the day-care mon to the save.
Definition
daycare.cpp:63
Daycare::randomize
void randomize(PlayerBasics *basics)
Put a random mon in the Day Care.
Definition
daycare.cpp:83
Daycare::pokemon
PokemonBox * pokemon
Definition
daycare.h:62
PlayerBasics
The trainer's headline values: name, ID, money, coins, badges, starter.
Definition
playerbasics.h:67
PokemonBox
A single Pokemon record – the most property-rich object in the tree.
Definition
pokemonbox.h:213
Random::inst
static Random * inst()
< Convenience 50% coin flip (integer path), readable from QML.
Definition
random.cpp:31
SaveFileToolset::setByte
void setByte(var16 addr, var8 val)
Simply sets a byte.
Definition
savefiletoolset.cpp:237
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
daycare.h
playerbasics.h
pokemonbox.h
random.h
savefile.h
savefileiterator.h
savefiletoolset.h
projects
savefile
src
pse-savefile
expanded
daycare.cpp
Generated by
1.17.0