Pokered Save Editor 2
Pokemon Red & Blue save file editor - Qt 6 C++/QML
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"
26#include <pse-db/mapsdb.h>
28#include <pse-common/random.h>
29
30struct TmpSignPos {
33};
34
36{
37 load(saveFile, index);
38}
39
41
42void 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();
59
60 delete it;
61}
62
63void 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
76{
77 x = 0;
78 xChanged();
79
80 y = 0;
81 yChanged();
82
83 txtId = 0;
85}
86
87QVector<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
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
135QVector<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
151void 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}
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(var8 val, var16 padding=0)
Write a byte at the cursor; advances.
SaveFileIterator * offsetTo(var16 val)
Move the cursor to an absolute offset. Returns this for chaining.
One loaded save: the raw 32 KB bytes, their expanded object tree, and the tools that move between the...
Definition savefile.h:46
SaveFileIterator * iterator()
Returns a unique iterator that's setup to iterate over the raw sav file data.
Definition savefile.cpp:53
virtual ~SignData()
Definition signdata.cpp:40
void setTo(MapDBEntrySign *signData)
Copy values from a map-defined sign.
Definition signdata.cpp:118
void load(SaveFile *saveFile=nullptr, var8 index=0)
Expand sign index from the save.
Definition signdata.cpp:42
void yChanged()
void randomize(QVector< TmpSignPos * > *tmpPos=nullptr)
Randomize position (avoiding tmpPos clashes).
Definition signdata.cpp:151
void txtIdChanged()
protected::void xChanged()
int txtId
Definition signdata.h:67
void save(SaveFile *saveFile, var8 index)
Flatten sign index to the save.
Definition signdata.cpp:63
SignData(SaveFile *saveFile=nullptr, var8 index=0)
< Sign tile X.
Definition signdata.cpp:35
int y
Definition signdata.h:66
void reset()
Blank this sign.
Definition signdata.cpp:75
static QVector< SignData * > randomizeAll(QVector< MapDBEntrySign * > mapSigns)
Randomize a whole map's signs.
Definition signdata.cpp:87
int x
Definition signdata.h:65
static QVector< SignData * > setToAll(QVector< MapDBEntrySign * > mapSigns)
Build signs from a map's sign list.
Definition signdata.cpp:135
var8e var8
Everyday 8-bit alias. Exact (not "fastest") to dodge the pointer-width bug noted above.
Definition types.h:124
One sign defined on a map: its position and text id.
int getTextID() const
int getX() const
< Sign tile X.