Pokered Save Editor 2
Pokemon Red & Blue save file editor - Qt 6 C++/QML
Loading...
Searching...
No Matches
itemdbentry.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
21
22#include <QDebug>
23#include <QQmlEngine>
24#include <pse-common/utility.h>
25#include "itemdbentry.h"
26#include "../moves.h"
27#include "./gamecornerdbentry.h"
28#include "../gamecornerdb.h"
29
33ItemDBEntry::ItemDBEntry(const QJsonValue& data)
34{
36
37 // Set simple properties
38 name = data["name"].toString();
39 ind = data["ind"].toDouble();
40 readable = data["readable"].toString();
41
42 // Set simple optional properties
43 if(data["once"].isBool())
44 once = data["once"].toBool();
45
46 if(data["glitch"].isBool())
47 glitch = data["glitch"].toBool();
48
49 if(data["tm"].isDouble())
50 tm = data["tm"].toDouble();
51
52 if(data["hm"].isDouble())
53 hm = data["hm"].toDouble();
54
55 if(data["price"].isDouble())
56 price = data["price"].toDouble();
57
58 if(data["info"].isString())
59 info = data["info"].toString();
60}
61
63{
64 if(tm > 0 && hm <= 0)
65 toMove = MovesDB::inst()->getIndAt("tm" + QString::number(tm));
66 else if(tm > 0 && hm > 0)
67 toMove = MovesDB::inst()->getIndAt("hm" + QString::number(hm));
68
69#ifdef QT_DEBUG
70 if((tm || hm) && toMove == nullptr)
71 qCritical() << "Item: " << name << ", could not be deep linked." ;
72#endif
73}
74
76{
77 static bool once = false;
78 if(once)
79 return;
80
81 qmlRegisterUncreatableType<ItemDBEntry>(
82 "PSE.DB.ItemDBEntry", 1, 0, "ItemDBEntry", "Can't instantiate in QML");
83 once = true;
84}
85
86const QVector<PokemonDBEntry*> ItemDBEntry::getToTeachPokemon() const
87{
88 return toTeachPokemon;
89}
90
92{
93 return toTeachPokemon.size();
94}
95
97{
98 if(ind >= toTeachPokemon.size())
99 return nullptr;
100
101 return toTeachPokemon.at(ind);
102}
103
104void ItemDBEntry::qmlProtect(const QQmlEngine* const engine) const
105{
106 Utility::qmlProtectUtil(this, engine);
107}
108
109const QVector<PokemonDBEntryEvolution*> ItemDBEntry::getToEvolvePokemon() const
110{
111 return toEvolvePokemon;
112}
113
115{
116 return toEvolvePokemon.size();
117}
118
120{
121 if(ind >= toEvolvePokemon.size())
122 return nullptr;
123
124 return toEvolvePokemon.at(ind);
125}
126
127const QVector<MapDBEntrySpriteItem*> ItemDBEntry::getToMapSpriteItem() const
128{
129 return toMapSpriteItem;
130}
131
133{
134 return toMapSpriteItem.size();
135}
136
138{
139 if(ind >= toMapSpriteItem.size())
140 return nullptr;
141
142 return toMapSpriteItem.at(ind);
143}
144
149
151{
152 return toMove;
153}
154
156{
157 return price;
158}
159
160QString ItemDBEntry::getInfo() const
161{
162 return info;
163}
164
166{
167 return hm;
168}
169
171{
172 return tm;
173}
174
176{
177 return readable;
178}
179
181{
182 return glitch;
183}
184
186{
187 return once;
188}
189
191{
192 return ind;
193}
194
195QString ItemDBEntry::getName() const
196{
197 return name;
198}
199
201{
202 if(price <= 0)
203 return 0;
204
205 return price;
206}
207
209{
210 // Sometimes there might be a duplicate game corner price
211 // We want to pick the game-corner price over market price anytime
212
213 int val = 0;
214
215 // If there's a game corner price attached, always use that. It will be in
216 // coins.
217 if(toGameCorner != nullptr)
218 val = toGameCorner->getPrice();
219
220 // Otherwise if there's no market price or the market price is 0 then keep it
221 // at 0
222 else if(price <= 0)
223 val = 0;
224
225 // In all other cases, divide market price by Game Corner exchange rate to
226 // produce the coins amount
227 else
229
230 // Return whatever value we have
231 return val;
232}
233
234// These are simpler, we did all the work above, here we just divide by 2 which
235// is the global sell-back ratio for anything
236
238{
239 return buyPriceMoney() / 2;
240}
241
243{
244 return buyPriceCoins() / 2;
245}
246
247// This is a bit tricky
248// We can sell an item, even if the sell price is zero. But if there's no listed
249// price then we can't sell.
250// Non-sellable items are considered key items and must be tossed
252{
253 return price >= 0;
254}
255
257{
258 return toGameCorner != nullptr;
259}
int getBuyPrice() const
The buy rate (backs getBuyPrice).
static GameCornerDB * inst()
< Number of prize entries.
MoveDBEntry * getIndAt(const QString &key) const
Move by name key (for QML).
Definition moves.cpp:88
static MovesDB * inst()
< Number of moves.
Definition moves.cpp:72
static void qmlProtectUtil(const QObject *const obj, const QQmlEngine *const engine)
Pin obj to C++ ownership so the QML engine never garbage-collects it.
Definition utility.cpp:63
int getPrice() const
int sellPriceMoney() const
QString info
Backing field (read via getInfo()); optional "info" in items.json.
int sellPriceCoins() const
MapDBEntrySpriteItem * getToMapSpriteItemAt(int ind) const
Map-sprite-item ind (for QML).
void deepLink()
Resolve the move/Game-Corner links.
GameCornerDBEntry * getToGameCorner() const
QString readable
Backing field (read via getReadable()).
QString name
Backing field (read via getName()).
MoveDBEntry * toMove
Resolved taught move (deepLink).
friend struct PokemonDBEntry
bool isGameCornerExclusive() const
void qmlRegister() const
Register with QML.
bool canSell() const
int getToMapSpriteItemSize() const
int getToTeachPokemonSize() const
QString getReadable() const
int tm
Backing field (read via getTm()).
int buyPriceMoney() const
PokemonDBEntry * getToTeachPokemonAt(int ind) const
Taught Pokemon ind (for QML).
int getTm() const
int getToEvolvePokemonSize() const
bool getGlitch() const
GameCornerDBEntry * toGameCorner
Resolved Game Corner entry (deepLink).
int buyPriceCoins() const
const QVector< PokemonDBEntryEvolution * > getToEvolvePokemon() const
Evolutions triggered by this item.
QVector< PokemonDBEntryEvolution * > toEvolvePokemon
Evolutions using this (back-ref).
bool glitch
Backing field (read via getGlitch()).
QVector< MapDBEntrySpriteItem * > toMapSpriteItem
Map sprites dropping this (back-ref).
QVector< PokemonDBEntry * > toTeachPokemon
Pokemon this teaches (back-ref).
ItemDBEntry()
Empty entry (built by ItemsDB).
QString getName() const
< Internal item name.
QString getInfo() const
PokemonDBEntryEvolution * getToEvolvePokemonAt(int ind) const
Evolution ind (for QML).
int hm
Backing field (read via getHm()).
const QVector< PokemonDBEntry * > getToTeachPokemon() const
Pokemon this TM/HM teaches.
int ind
Backing field (read via getInd()).
bool once
Backing field (read via getOnce()).
bool getOnce() const
MoveDBEntry * getToMove() const
int price
Backing field (read via getPrice()).
int getInd() const
int getHm() const
friend struct MapDBEntrySpriteItem
Writes toMapSpriteItem.
const QVector< MapDBEntrySpriteItem * > getToMapSpriteItem() const
Map sprites that drop this item.
friend struct PokemonDBEntryEvolution
friend struct GameCornerDBEntry
void qmlProtect(const QQmlEngine *const engine) const
Pin to C++ ownership.
One move's static data (type, power, accuracy, PP, TM/HM), with links.
Definition moves.h:46