Pokered Save Editor 2
Pokemon Red & Blue save file editor - Qt 6 C++/QML
Toggle main menu visibility
Loading...
Searching...
No Matches
signdata.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 "
signdata.h
"
23
#include "
../../savefile.h
"
24
#include "
../../savefiletoolset.h
"
25
#include "
../../savefileiterator.h
"
26
#include <
pse-db/mapsdb.h
>
27
#include <
pse-db/entries/mapdbentrysign.h
>
28
#include <
pse-common/random.h
>
29
30
struct
TmpSignPos
{
31
var8
x
;
32
var8
y
;
33
};
34
35
SignData::SignData
(
SaveFile
* saveFile,
var8
index)
36
{
37
load
(saveFile, index);
38
}
39
40
SignData::~SignData
() {}
41
42
void
SignData::load
(
SaveFile
* saveFile,
var8
index)
43
{
44
reset
();
45
46
if
(saveFile ==
nullptr
)
47
return
;
48
49
auto
it = saveFile->
iterator
()->
offsetTo
((2 * index) + 0x275D);
50
y
= it->getByte();
51
yChanged
();
52
53
x
= it->getByte();
54
xChanged
();
55
56
it->offsetTo((1 * index) + 0x277D);
57
txtId
= it->getByte();
58
txtIdChanged
();
59
60
delete
it;
61
}
62
63
void
SignData::save
(
SaveFile
* saveFile,
var8
index)
64
{
65
auto
it = saveFile->
iterator
()->
offsetTo
((2 * index) + 0x275D);
66
it->
setByte
(
y
);
67
it->setByte(
x
);
68
69
it->offsetTo((1 * index) + 0x277D);
70
it->setByte(
txtId
);
71
72
delete
it;
73
}
74
75
void
SignData::reset
()
76
{
77
x
= 0;
78
xChanged
();
79
80
y
= 0;
81
yChanged
();
82
83
txtId
= 0;
84
txtIdChanged
();
85
}
86
87
QVector<SignData*>
SignData::randomizeAll
(QVector<MapDBEntrySign*> mapSigns)
88
{
89
// Prepare return value
90
QVector<SignData*> signs;
91
92
// Make first pass and get all sprite positions on map
93
QVector<TmpSignPos*> tmpPos;
94
95
for
(
auto
entry : mapSigns) {
96
auto
tmp =
new
TmpSignPos
;
97
tmp->
x
= entry->getX();
98
tmp->y = entry->getY();
99
tmpPos.append(tmp);
100
}
101
102
// Now create new sprites from data and randomize their contents
103
for
(
auto
entry : mapSigns) {
104
105
Q_UNUSED(entry)
106
107
// New Sprite from map data
108
auto
tmp =
new
SignData
;
109
signs.append(tmp);
110
111
// Randomize newly created sprite
112
tmp->randomize(&tmpPos);
113
}
114
115
return
signs;
116
}
117
118
void
SignData::setTo
(
MapDBEntrySign
* signData)
119
{
120
reset
();
121
122
if
(signData ==
nullptr
)
123
return
;
124
125
x
= signData->
getX
();
126
xChanged
();
127
128
y
= signData->
getY
();
129
yChanged
();
130
131
txtId
= signData->
getTextID
();
132
txtIdChanged
();
133
}
134
135
QVector<SignData*>
SignData::setToAll
(QVector<MapDBEntrySign*> mapSigns)
136
{
137
// Prepare return value
138
QVector<SignData*> signs;
139
140
for
(
auto
entry : mapSigns) {
141
142
// New Sprite from map data
143
auto
tmp =
new
SignData
;
144
signs.append(tmp);
145
tmp->setTo(entry);
146
}
147
148
return
signs;
149
}
150
151
void
SignData::randomize
(QVector<TmpSignPos*>* tmpPos)
152
{
153
// Randomize coordinates if coord list is provided
154
if
(tmpPos !=
nullptr
) {
155
156
// Pull random coordinates
157
var8
rndPos =
Random::inst
()->
rangeExclusive
(0, tmpPos->size());
158
auto
rndCoords = tmpPos->at(rndPos);
159
160
// Change coords
161
x
= rndCoords->x;
162
xChanged
();
163
164
y
= rndCoords->y;
165
yChanged
();
166
167
// Remove chosen random coordinate
168
delete
rndCoords;
169
tmpPos->removeAt(rndPos);
170
}
171
172
// Do nothing more, if no coordinate list is provided we have nothing else
173
// to work off of
174
}
Random::inst
static Random * inst()
< Convenience 50% coin flip (integer path), readable from QML.
Definition
random.cpp:31
Random::rangeExclusive
int rangeExclusive(const int start, const int end) const
Random integer in the half-open interval [start, end).
Definition
random.cpp:53
SaveFileIterator::setByte
void setByte(var8 val, var16 padding=0)
Write a byte at the cursor; advances.
Definition
savefileiterator.cpp:173
SaveFileIterator::offsetTo
SaveFileIterator * offsetTo(var16 val)
Move the cursor to an absolute offset. Returns this for chaining.
Definition
savefileiterator.cpp:31
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::iterator
SaveFileIterator * iterator()
Returns a unique iterator that's setup to iterate over the raw sav file data.
Definition
savefile.cpp:53
SignData::~SignData
virtual ~SignData()
Definition
signdata.cpp:40
SignData::setTo
void setTo(MapDBEntrySign *signData)
Copy values from a map-defined sign.
Definition
signdata.cpp:118
SignData::load
void load(SaveFile *saveFile=nullptr, var8 index=0)
Expand sign index from the save.
Definition
signdata.cpp:42
SignData::yChanged
void yChanged()
SignData::randomize
void randomize(QVector< TmpSignPos * > *tmpPos=nullptr)
Randomize position (avoiding tmpPos clashes).
Definition
signdata.cpp:151
SignData::txtIdChanged
void txtIdChanged()
SignData::xChanged
protected::void xChanged()
SignData::txtId
int txtId
Definition
signdata.h:67
SignData::save
void save(SaveFile *saveFile, var8 index)
Flatten sign index to the save.
Definition
signdata.cpp:63
SignData::SignData
SignData(SaveFile *saveFile=nullptr, var8 index=0)
< Sign tile X.
Definition
signdata.cpp:35
SignData::y
int y
Definition
signdata.h:66
SignData::reset
void reset()
Blank this sign.
Definition
signdata.cpp:75
SignData::randomizeAll
static QVector< SignData * > randomizeAll(QVector< MapDBEntrySign * > mapSigns)
Randomize a whole map's signs.
Definition
signdata.cpp:87
SignData::x
int x
Definition
signdata.h:65
SignData::setToAll
static QVector< SignData * > setToAll(QVector< MapDBEntrySign * > mapSigns)
Build signs from a map's sign list.
Definition
signdata.cpp:135
var8
var8e var8
Everyday 8-bit alias. Exact (not "fastest") to dodge the pointer-width bug noted above.
Definition
types.h:124
mapdbentrysign.h
mapsdb.h
random.h
savefile.h
savefileiterator.h
savefiletoolset.h
signdata.h
MapDBEntrySign
One sign defined on a map: its position and text id.
Definition
mapdbentrysign.h:33
MapDBEntrySign::getY
int getY() const
Definition
mapdbentrysign.cpp:45
MapDBEntrySign::getTextID
int getTextID() const
Definition
mapdbentrysign.cpp:40
MapDBEntrySign::getX
int getX() const
< Sign tile X.
Definition
mapdbentrysign.cpp:50
TmpSignPos
Definition
signdata.cpp:30
TmpSignPos::y
var8 y
Definition
signdata.cpp:32
TmpSignPos::x
var8 x
Definition
signdata.cpp:31
projects
savefile
src
pse-savefile
expanded
fragments
signdata.cpp
Generated by
1.17.0