Pokered Save Editor 2
Pokemon Red & Blue save file editor - Qt 6 C++/QML
Toggle main menu visibility
Loading...
Searching...
No Matches
itemmarketentryplayeritem.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 "
./itemmarketentryplayeritem.h
"
23
#include <
pse-db/itemsdb.h
>
24
#include <
pse-db/entries/itemdbentry.h
>
25
#include <
pse-savefile/expanded/fragments/item.h
>
26
#include <
pse-savefile/expanded/fragments/itemstoragebox.h
>
27
#include <
pse-savefile/expanded/player/playerbasics.h
>
28
29
ItemMarketEntryPlayerItem::ItemMarketEntryPlayerItem
(
ItemStorageBox
*
toBox
,
30
Item
*
toItem
)
31
:
ItemMarketEntry
(
CompatEither
,
CompatEither
),
// Any currency; selling, but coexists
32
// with buys in the unified cart, so it
33
// no longer mode-gates on the buy flag.
34
toBox
(
toBox
),
35
toItem
(
toItem
)
36
{
37
finishConstruction
();
38
}
39
40
ItemMarketEntryPlayerItem::~ItemMarketEntryPlayerItem
() {}
41
42
QString
ItemMarketEntryPlayerItem::infoText
()
43
{
44
if
(!
toItem
)
45
return
QString();
46
auto
itemData =
toItem
->toItem();
47
return
(itemData !=
nullptr
) ? itemData->getInfo() : QString();
48
}
49
50
QString
ItemMarketEntryPlayerItem::_name
()
51
{
52
if
(!
toItem
|| !
requestFilter
())
// toItem may have been freed (stale entry)
53
return
""
;
54
55
auto
itemData =
toItem
->toItem();
56
if
(itemData ==
nullptr
)
57
return
""
;
58
59
return
itemData->getReadable();
60
}
61
62
int
ItemMarketEntryPlayerItem::_inStockCount
()
63
{
64
if
(!
toItem
|| !
requestFilter
())
65
return
0;
66
67
return
toItem
->amount;
68
}
69
70
bool
ItemMarketEntryPlayerItem::_canSell
()
71
{
72
if
(!
toItem
|| !
requestFilter
())
73
return
false
;
74
75
auto
itemData =
toItem
->toItem();
76
if
(itemData ==
nullptr
)
77
return
false
;
78
79
return
itemData->canSell();
80
}
81
82
int
ItemMarketEntryPlayerItem::_itemWorth
()
83
{
84
// A stale entry (left in the static instances registry by a torn-down model) can
85
// outlive its Item; QPointer nulls on free, so bail before touching it. This is the
86
// exact dangling read AddressSanitizer caught at itemmarketentryplayeritem.cpp:79.
87
if
(!
toItem
|| !
requestFilter
())
88
return
0;
89
90
// Sell item to money
91
if
(*
isMoneyCurrency
)
92
return
toItem
->sellPriceOneMoney();
93
94
// Sell item to coins
95
else
if
(!(*
isMoneyCurrency
))
96
return
toItem
->sellPriceOneCoins();
97
98
return
0;
99
}
100
101
QString
ItemMarketEntryPlayerItem::_whichType
()
102
{
103
return
type
;
104
}
105
106
int
ItemMarketEntryPlayerItem::onCartLeft
()
107
{
108
if
(!
toItem
|| !
requestFilter
())
109
return
0;
110
111
// Maximum items left that can be sold
112
int
maxItems =
toItem
->amount -
onCart
;
113
114
// Maximum money left that can be obtained from a sell
115
int
maxMoney = (*isMoneyCurrency)
116
? 999999
117
: 9999;
118
119
maxMoney -=
moneyLeftover
();
120
121
int
maxMoneyItems = (
itemWorth
() == 0)
122
? INT_MAX
123
: maxMoney /
itemWorth
();
124
125
// Return smallest of the numbers
126
return
qMin(maxItems, maxMoneyItems);
127
}
128
129
int
ItemMarketEntryPlayerItem::stackCount
()
130
{
131
// Applies only to buying items
132
return
0;
133
}
134
135
void
ItemMarketEntryPlayerItem::checkout
()
136
{
137
if
(!
toItem
|| !
toBox
||
138
!
requestFilter
() ||
139
!
canCheckout
())
140
return
;
141
142
// Subtract amount owed
143
toItem
->amount -=
onCart
;
144
toItem
->amountChanged();
145
146
// Remove if empty
147
if
(
toItem
->amount <= 0)
148
toBox
->itemRemove(
toBox
->items.indexOf(
toItem
));
149
150
// Sell item to money
151
if
(*
isMoneyCurrency
) {
152
player
->money +=
cartWorth
();
153
player
->moneyChanged();
154
}
155
156
// Sell item to coins
157
else
{
158
player
->coins +=
cartWorth
();
159
player
->coinsChanged();
160
}
161
}
ItemMarketEntryPlayerItem::type
static constexpr const char * type
This row's type key.
Definition
itemmarketentryplayeritem.h:61
ItemMarketEntryPlayerItem::checkout
virtual void checkout() override
Sell the item (remove qty, credit balance).
Definition
itemmarketentryplayeritem.cpp:135
ItemMarketEntryPlayerItem::toBox
QPointer< ItemStorageBox > toBox
The box the item is sold from (auto-nulls if freed).
Definition
itemmarketentryplayeritem.h:59
ItemMarketEntryPlayerItem::_canSell
virtual bool _canSell() override
Subtype: compute sellability.
Definition
itemmarketentryplayeritem.cpp:70
ItemMarketEntryPlayerItem::onCartLeft
virtual int onCartLeft() override
Subtype: how many more may be added.
Definition
itemmarketentryplayeritem.cpp:106
ItemMarketEntryPlayerItem::infoText
virtual QString infoText() override
Detailed-tooltip body (default none).
Definition
itemmarketentryplayeritem.cpp:42
ItemMarketEntryPlayerItem::stackCount
virtual int stackCount() override
Subtype: new stack slots needed (see note above).
Definition
itemmarketentryplayeritem.cpp:129
ItemMarketEntryPlayerItem::_itemWorth
virtual int _itemWorth() override
Subtype: compute the unit value.
Definition
itemmarketentryplayeritem.cpp:82
ItemMarketEntryPlayerItem::_whichType
virtual QString _whichType() override
Subtype: report the type label.
Definition
itemmarketentryplayeritem.cpp:101
ItemMarketEntryPlayerItem::toItem
QPointer< Item > toItem
The item being sold (auto-nulls if freed).
Definition
itemmarketentryplayeritem.h:60
ItemMarketEntryPlayerItem::_inStockCount
virtual int _inStockCount() override
Subtype: compute the owned/sellable count.
Definition
itemmarketentryplayeritem.cpp:62
ItemMarketEntryPlayerItem::ItemMarketEntryPlayerItem
ItemMarketEntryPlayerItem(ItemStorageBox *toBox, Item *toItem)
Definition
itemmarketentryplayeritem.cpp:29
ItemMarketEntryPlayerItem::_name
virtual QString _name() override
Subtype: compute the display name.
Definition
itemmarketentryplayeritem.cpp:50
ItemMarketEntryPlayerItem::~ItemMarketEntryPlayerItem
virtual ~ItemMarketEntryPlayerItem()
Definition
itemmarketentryplayeritem.cpp:40
ItemMarketEntry::ItemMarketEntry
ItemMarketEntry(int compatMoneyCurrency=CompatEither, int compatBuyMode=CompatEither)
Definition
itemmarketentry.cpp:28
ItemMarketEntry::moneyLeftover
int moneyLeftover()
Money remaining if this checks out.
Definition
itemmarketentry.cpp:202
ItemMarketEntry::requestFilter
bool requestFilter()
Helper: does this row pass the current mode filter?
Definition
itemmarketentry.cpp:124
ItemMarketEntry::isMoneyCurrency
static bool * isMoneyCurrency
Shared: current currency mode.
Definition
itemmarketentry.h:191
ItemMarketEntry::itemWorth
int itemWorth()
Cached unit value.
Definition
itemmarketentry.cpp:95
ItemMarketEntry::player
static PlayerBasics * player
Shared: player money/coins.
Definition
itemmarketentry.h:193
ItemMarketEntry::finishConstruction
void finishConstruction()
Finalise construction (register the instance).
Definition
itemmarketentry.cpp:58
ItemMarketEntry::onCart
int onCart
Backing cart quantity.
Definition
itemmarketentry.h:169
ItemMarketEntry::cartWorth
int cartWorth()
Value of the cart quantity.
Definition
itemmarketentry.cpp:154
ItemMarketEntry::canCheckout
virtual bool canCheckout()
Can this row alone check out?
Definition
itemmarketentry.cpp:208
ItemMarketEntry::CompatEither
@ CompatEither
Definition
itemmarketentry.h:76
ItemStorageBox
A container of Items – either the trainer's bag or a PC item box.
Definition
itemstoragebox.h:36
Item
One inventory slot: an item index and an amount, with live pricing.
Definition
item.h:36
item.h
itemdbentry.h
itemmarketentryplayeritem.h
itemsdb.h
itemstoragebox.h
playerbasics.h
projects
app
src
mvc
itemmarket
itemmarketentryplayeritem.cpp
Generated by
1.17.0