Pokered Save Editor 2
Pokemon Red & Blue save file editor - Qt 6 C++/QML
Loading...
Searching...
No Matches
starterPokemon.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#include <pse-common/random.h>
26
27#ifdef QT_DEBUG
28#include <QtDebug>
29#endif
30
31#include "./starterPokemon.h"
32#include "./util/gamedata.h"
33#include "./pokemon.h"
34
35StarterPokemonDB* StarterPokemonDB::inst()
36{
37 static StarterPokemonDB* _inst = new StarterPokemonDB;
38 return _inst;
39}
40
41int StarterPokemonDB::getStoreSize() const { return store.size(); }
42
44{
45 var32 idx = Random::inst()->rangeExclusive(0, 3);
46 return toPokemon.at(idx);
47}
48
50{
51 var32 idx = Random::inst()->rangeExclusive(0, store.size());
52 return toPokemon.at(idx);
53}
54
56{
57 static bool once = false;
58 if (once) return;
59 auto jsonData = GameData::inst()->json("starters");
60 for (QJsonValue entry : jsonData.array())
61 store.append(entry.toString());
62 once = true;
63}
64
66{
67 static bool once = false;
68 if (once) return;
69 for (int i = 0; i < store.size(); ++i) {
70 auto* mon = PokemonDB::inst()->getIndAt(store.at(i));
71 toPokemon.append(mon);
72#ifdef QT_DEBUG
73 if (!mon) qCritical() << "Starter Pokemon:" << store.at(i) << "could not be deep linked.";
74#endif
75 }
76 once = true;
77}
78
79void StarterPokemonDB::qmlProtect(const QQmlEngine* const engine) const
80{
81 Utility::qmlProtectUtil(this, engine);
82}
83
84void StarterPokemonDB::qmlRegister() const
85{
86 static bool once = false;
87 if (once) return;
88 qmlRegisterUncreatableType<StarterPokemonDB>("PSE.DB.StarterPokemonDB", 1, 0, "StarterPokemonDB", "Can't instantiate in QML");
89 once = true;
90}
91
92StarterPokemonDB::StarterPokemonDB()
93{
94 qmlRegister();
95}
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
static PokemonDB * inst()
< Number of species.
Definition pokemon.cpp:183
PokemonDBEntry * getIndAt(const QString &key) const
Species by name key (for QML).
Definition pokemon.cpp:199
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
PokemonDBEntry * random3Starter() const
A random one of the 3 canonical starters.
static StarterPokemonDB * inst()
< Number of starter choices.
void load()
Load the starter list from JSON.
void deepLink()
Resolve the names to species entries.
int getStoreSize() const
Starter-choice count.
void qmlProtect(const QQmlEngine *const engine) const
Pin to C++ ownership.
PokemonDBEntry * randomAnyStarter() const
A random "startery" species.
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
var32e var32
Everyday 32-bit alias. Exact width to avoid the "fastest" widening bug.
Definition types.h:126
One species' complete static data – the richest entry in the db layer.
Definition pokemon.h:98