Pokered Save Editor 2
Pokemon Red & Blue save file editor - Qt 6 C++/QML
Loading...
Searching...
No Matches
item.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 "./item.h"
23#include "../../savefile.h"
26#include <pse-db/itemsdb.h>
28#include <pse-common/random.h>
29
31{
32 if(it == nullptr) {
33 reset();
34 return;
35 }
36
37 ind = it->getByte();
38 amount = it->getByte();
40}
41
47
48Item::Item(bool random)
49{
50 load(random);
52}
53
54Item::Item(QString name, var8 amount)
55{
56 load(name, amount);
58}
59
61{
62 connect(this, &Item::indChanged, this, &Item::itemChanged);
63 connect(this, &Item::amountChanged, this, &Item::itemChanged);
64}
65
67
69{
70 it->setByte(ind);
71 it->setByte(amount);
72}
73
75{
76 ind = 0;
77 indChanged();
78
79 amount = 0;
81}
82
84{
85 auto tmp = ItemsDB::inst()->getStore().at(Random::inst()->rangeExclusive(0, ItemsDB::inst()->getStoreSize()));
86
87 // No Glitch or Items only gotten once in the game
88 while(tmp->getGlitch() || tmp->getOnce())
89 tmp = ItemsDB::inst()->getStore().at(Random::inst()->rangeExclusive(0, ItemsDB::inst()->getStoreSize()));
90
91 ind = tmp->getInd();
92 indChanged();
93
94 amount = Random::inst()->rangeInclusive(1, 5); // Between 1 and 5 of them
96}
97
99{
100 auto tmp = ItemsDB::inst()->getIndAt(QString::number(ind));
101 return tmp;
102}
103
105{
106 auto el = toItem();
107
108 if(el == nullptr)
109 return false;
110 else
111 return el->canSell();
112}
113
115{
116 auto el = toItem();
117
118 if(el == nullptr)
119 return 0;
120 else
121 return el->buyPriceMoney();
122}
123
125{
126 auto el = toItem();
127
128 if(el == nullptr)
129 return 0;
130 else
131 return el->buyPriceCoins();
132}
133
135{
136 auto el = toItem();
137
138 if(el == nullptr)
139 return 0;
140 else
141 return el->sellPriceMoney();
142}
143
145{
146 auto el = toItem();
147
148 if(el == nullptr)
149 return 0;
150 else
151 return el->sellPriceCoins();
152}
153
155{
156 return buyPriceOneMoney() * amount;
157}
158
160{
161 return buyPriceOneCoins() * amount;
162}
163
165{
166 return sellPriceOneMoney() * amount;
167}
168
170{
171 return sellPriceOneCoins() * amount;
172}
173
175{
176 return amount;
177}
178
179void Item::setAmount(int val)
180{
181 amount = val;
182 if(amount < 1)
183 amount = 1;
184 else if(amount > 99)
185 amount = 99;
186}
187
188void Item::load(int ind, int amount)
189{
190 this->ind = ind;
191 this->amount = amount;
192}
193
194void Item::load(bool random)
195{
196 if(!random) {
197 reset();
198 return;
199 }
200
201 randomize();
202}
203
204void Item::load(QString name, int amount)
205{
206 auto tmp = ItemsDB::inst()->getIndAt(name);
207 if(tmp != nullptr)
208 ind = tmp->getInd();
209
210 this->amount = amount;
211}
int ind
Item index (backs property).
Definition item.h:106
virtual ~Item()
Definition item.cpp:66
void makeConnect()
Wire up internal signal connections.
Definition item.cpp:60
int sellPriceOneMoney()
Definition item.cpp:134
void reset()
Blank this slot.
Definition item.cpp:74
int amount
Item amount (max 99 in Gen 1; backs property).
Definition item.h:109
void amountChanged()
int getAmount()
Current amount (backs property READ).
Definition item.cpp:174
int buyPriceOneCoins()
Definition item.cpp:124
int sellPriceAllMoney()
Definition item.cpp:164
void randomize()
Randomize this slot.
Definition item.cpp:83
void load(int ind, int amount)
Set from an index + amount.
Definition item.cpp:188
int sellPriceOneCoins()
Definition item.cpp:144
protected::void indChanged()
int buyPriceAllCoins()
Definition item.cpp:159
int sellPriceAllCoins()
Definition item.cpp:169
Item(SaveFileIterator *it=nullptr)
< Item index (into the items DB).
Definition item.cpp:30
void setAmount(int val)
Set amount (backs property WRITE; clamped to the Gen 1 max).
Definition item.cpp:179
bool canSell()
Whether the item may be sold.
Definition item.cpp:104
int buyPriceOneMoney()
Definition item.cpp:114
int buyPriceAllMoney()
Definition item.cpp:154
void itemChanged()
void save(SaveFileIterator *it)
Given an iterator, saves 2 bytes: index and amount.
Definition item.cpp:68
ItemDBEntry * toItem()
Resolve ind to its DB entry.
Definition item.cpp:98
ItemDBEntry * getIndAt(const QString val) const
Item by name key (for QML).
Definition itemsdb.cpp:66
const QVector< ItemDBEntry * > getStore() const
All items.
Definition itemsdb.cpp:43
static ItemsDB * inst()
< Number of items.
Definition itemsdb.cpp:37
int rangeInclusive(const int start, const int end) const
Random integer in the closed interval [start, end].
Definition random.cpp:42
static Random * inst()
< Convenience 50% coin flip (integer path), readable from QML.
Definition random.cpp:31
A moving cursor over a SaveFile, layering auto-advancing reads/writes on top of SaveFileToolset.
void setByte(var8 val, var16 padding=0)
Write a byte at the cursor; advances.
var8 getByte(var16 padding=0)
Read a byte at the cursor; advances.
var8e var8
Everyday 8-bit alias. Exact (not "fastest") to dodge the pointer-width bug noted above.
Definition types.h:124
One item's static data: name/flags, pricing, and where it's used.
Definition itemdbentry.h:46