Pokered Save Editor 2
Pokemon Red & Blue save file editor - Qt 6 C++/QML
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
23#include <pse-db/itemsdb.h>
28
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),
36{
38}
39
41
43{
44 if(!toItem)
45 return QString();
46 auto itemData = toItem->toItem();
47 return (itemData != nullptr) ? itemData->getInfo() : QString();
48}
49
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
63{
64 if(!toItem || !requestFilter())
65 return 0;
66
67 return toItem->amount;
68}
69
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
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
92 return toItem->sellPriceOneMoney();
93
94 // Sell item to coins
95 else if(!(*isMoneyCurrency))
96 return toItem->sellPriceOneCoins();
97
98 return 0;
99}
100
102{
103 return type;
104}
105
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
130{
131 // Applies only to buying items
132 return 0;
133}
134
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}
static constexpr const char * type
This row's type key.
virtual void checkout() override
Sell the item (remove qty, credit balance).
QPointer< ItemStorageBox > toBox
The box the item is sold from (auto-nulls if freed).
virtual bool _canSell() override
Subtype: compute sellability.
virtual int onCartLeft() override
Subtype: how many more may be added.
virtual QString infoText() override
Detailed-tooltip body (default none).
virtual int stackCount() override
Subtype: new stack slots needed (see note above).
virtual int _itemWorth() override
Subtype: compute the unit value.
virtual QString _whichType() override
Subtype: report the type label.
QPointer< Item > toItem
The item being sold (auto-nulls if freed).
virtual int _inStockCount() override
Subtype: compute the owned/sellable count.
ItemMarketEntryPlayerItem(ItemStorageBox *toBox, Item *toItem)
virtual QString _name() override
Subtype: compute the display name.
ItemMarketEntry(int compatMoneyCurrency=CompatEither, int compatBuyMode=CompatEither)
int moneyLeftover()
Money remaining if this checks out.
bool requestFilter()
Helper: does this row pass the current mode filter?
static bool * isMoneyCurrency
Shared: current currency mode.
int itemWorth()
Cached unit value.
static PlayerBasics * player
Shared: player money/coins.
void finishConstruction()
Finalise construction (register the instance).
int onCart
Backing cart quantity.
int cartWorth()
Value of the cart quantity.
virtual bool canCheckout()
Can this row alone check out?
A container of Items – either the trainer's bag or a PC item box.
One inventory slot: an item index and an amount, with live pricing.
Definition item.h:36