Pokered Save Editor 2
Pokemon Red & Blue save file editor - Qt 6 C++/QML
Loading...
Searching...
No Matches
moves.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2019 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 <QJsonArray>
23#include <QQmlEngine>
24#include <pse-common/utility.h>
25
26#ifdef QT_DEBUG
27#include <QtDebug>
28#endif
29
30#include "./moves.h"
31#include "./types.h"
32#include "./itemsdb.h"
33#include "./util/gamedata.h"
34
36MoveDBEntry::MoveDBEntry(QJsonValue& data)
37{
38 name = data["name"].toString();
39 ind = static_cast<var8>(data["ind"].toDouble());
40 readable = data["readable"].toString();
41
42 if (data["glitch"].isBool()) glitch = data["glitch"].toBool();
43 if (data["type"].isString()) type = data["type"].toString();
44 if (data["power"].isDouble()) power = static_cast<var8>(data["power"].toDouble());
45 if (data["accuracy"].isDouble()) accuracy = static_cast<var8>(data["accuracy"].toDouble());
46 if (data["pp"].isDouble()) pp = static_cast<var8>(data["pp"].toDouble());
47 if (data["tm"].isDouble()) tm = static_cast<var8>(data["tm"].toDouble());
48 if (data["hm"].isDouble()) hm = static_cast<var8>(data["hm"].toDouble());
49}
50
52{
53 if (!type.isEmpty())
55
56 if (tm && !hm)
57 toItem = ItemsDB::inst()->getIndAt("tm" + QString::number(*tm));
58 else if (hm)
59 toItem = ItemsDB::inst()->getIndAt("hm" + QString::number(*hm));
60
61#ifdef QT_DEBUG
62 if (!type.isEmpty() && !toType)
63 qCritical() << "Move type:" << type << "could not be deep linked.";
64 if ((tm || hm) && !toItem)
65 qCritical() << "Move:" << name << "TM/HM item could not be deep linked.";
66#endif
67
68 if (toType)
69 toType->toMoves.append(this);
70}
71
72MovesDB* MovesDB::inst()
73{
74 static MovesDB* _inst = new MovesDB;
75 return _inst;
76}
77
78const QVector<MoveDBEntry*> MovesDB::getStore() const { return store; }
79const QHash<QString, MoveDBEntry*> MovesDB::getInd() const { return ind; }
80int MovesDB::getStoreSize() const { return store.size(); }
81
83{
84 if (idx < 0 || idx >= store.size()) return nullptr;
85 return store.at(idx);
86}
87
88MoveDBEntry* MovesDB::getIndAt(const QString& key) const
89{
90 return ind.value(key, nullptr);
91}
92
94{
95 static bool once = false;
96 if (once) return;
97 auto jsonData = GameData::inst()->json("moves");
98 for (QJsonValue entry : jsonData.array())
99 store.append(new MoveDBEntry(entry));
100 once = true;
101}
102
104{
105 static bool once = false;
106 if (once) return;
107 for (auto* entry : store) {
108 ind.insert(entry->name, entry);
109 ind.insert(QString::number(entry->ind), entry);
110 ind.insert(entry->readable, entry);
111 if (entry->tm) ind.insert("tm" + QString::number(*entry->tm), entry);
112 if (entry->hm) ind.insert("hm" + QString::number(*entry->hm), entry);
113 }
114 once = true;
115}
116
118{
119 static bool once = false;
120 if (once) return;
121 for (auto* entry : store)
122 entry->deepLink();
123 once = true;
124}
125
126void MovesDB::qmlProtect(const QQmlEngine* const engine) const
127{
128 Utility::qmlProtectUtil(this, engine);
129}
130
131void MovesDB::qmlRegister() const
132{
133 static bool once = false;
134 if (once) return;
135 qmlRegisterUncreatableType<MovesDB>("PSE.DB.MovesDB", 1, 0, "MovesDB", "Can't instantiate in QML");
136 once = true;
137}
138
139MovesDB::MovesDB()
140{
141 qmlRegister();
142}
const QJsonDocument json(const QString filename) const
Parsed document for filename.
Definition gamedata.cpp:45
static GameData * inst()
The process-wide GameData singleton.
Definition gamedata.cpp:71
ItemDBEntry * getIndAt(const QString val) const
Item by name key (for QML).
Definition itemsdb.cpp:66
static ItemsDB * inst()
< Number of items.
Definition itemsdb.cpp:37
void load()
Load moves from JSON.
Definition moves.cpp:93
void deepLink()
Resolve every move's cross-DB links.
Definition moves.cpp:117
void qmlProtect(const QQmlEngine *const engine) const
Pin to C++ ownership.
Definition moves.cpp:126
int getStoreSize() const
Move count.
Definition moves.cpp:80
const QHash< QString, MoveDBEntry * > getInd() const
Name->entry index.
Definition moves.cpp:79
void index()
Build the name->entry index.
Definition moves.cpp:103
MoveDBEntry * getStoreAt(int idx) const
Move by store index (for QML).
Definition moves.cpp:82
const QVector< MoveDBEntry * > getStore() const
All moves, in load order.
Definition moves.cpp:78
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
TypeDBEntry * getIndAt(const QString &key) const
Type by name key (for QML).
Definition types.cpp:53
static TypesDB * inst()
< Number of types.
Definition types.cpp:37
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
var8e var8
Everyday 8-bit alias. Exact (not "fastest") to dodge the pointer-width bug noted above.
Definition types.h:124
One move's static data (type, power, accuracy, PP, TM/HM), with links.
Definition moves.h:46
std::optional< var8 > hm
HM number teaching this move, if any.
Definition moves.h:61
QString type
Type name (resolved to toType).
Definition moves.h:54
ItemDBEntry * toItem
Resolved TM/HM item entry (deepLink).
Definition moves.h:64
std::optional< var8 > pp
Base PP, if any.
Definition moves.h:59
std::optional< var8 > accuracy
Accuracy, if any.
Definition moves.h:58
void deepLink()
Resolve cross-DB links (type, item) after load.
Definition moves.cpp:51
TypeDBEntry * toType
Resolved type entry (deepLink).
Definition moves.h:63
var8 ind
Move index/id.
Definition moves.h:52
std::optional< var8 > power
Base power, if any.
Definition moves.h:57
QString readable
Human-readable display name.
Definition moves.h:55
std::optional< var8 > tm
TM number teaching this move, if any.
Definition moves.h:60
QString name
Internal move name (key).
Definition moves.h:51
bool glitch
Whether this is a glitch move.
Definition moves.h:53
MoveDBEntry()
Empty entry.
Definition moves.cpp:35