Pokered Save Editor 2
Pokemon Red & Blue save file editor - Qt 6 C++/QML
Loading...
Searching...
No Matches
itemmarketentrymoney.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
28
29#include <QDebug>
30
32#include <pse-db/gamecornerdb.h>
34
35// Compatibility is Either/Either: these rows are only ever built into the dedicated
36// Exchange list now, so they always pass the filter; the swap direction comes from
37// forceDir, not the global buy/sell flag.
45
47
49{
50 if(forceDir != DirGlobal)
51 return forceDir == DirToCoins;
52 return *isBuyMode;
53}
54
56{
57 if(buying())
58 return tr("Money => Coins");
59
60 return tr("Coins => Money");
61}
62
63// Always "selling" something, so it always has an in-stock value: how many of the
64// SOURCE currency you hold (money when buying coins, coins when selling them).
66{
67 if(buying())
68 return player->money;
69
70 return player->coins;
71}
72
74{
75 return true;
76}
77
78// The per-coin rate: cost to buy a coin, or money returned for selling one.
86
88{
89 return type;
90}
91
93{
94 const int rate = buying() ? GameCornerDB::inst()->getBuyPrice()
96 // Buying coins spends money; selling coins gains money.
97 return (buying() ? -rate : rate) * onCart;
98}
99
101{
102 // Buying gains coins; selling spends them.
103 return (buying() ? 1 : -1) * onCart;
104}
105
106// How many MORE coins can be added to the cart, bounded by both currencies' caps and
107// by what the player can afford / actually owns.
109{
110 const int rate = buying() ? GameCornerDB::inst()->getBuyPrice()
112 if(rate <= 0)
113 return 0;
114
115 int maxCoins = 0;
116 if(buying()) {
117 const int coinCapRoom = 9999 - player->coins; // coins fit under 9,999
118 const int affordable = player->money / rate; // coins you can pay for
119 maxCoins = qMin(coinCapRoom, affordable);
120 } else {
121 const int coinsOwned = player->coins; // coins you actually have
122 const int moneyCapRoom = (999999 - player->money) / rate; // money fits under 999,999
123 maxCoins = qMin(coinsOwned, moneyCapRoom);
124 }
125
126 return maxCoins - onCart;
127}
128
130{
131 // Applies only to buying items
132 return 0;
133}
134
135// Exchange-aware affordability: gate on the currency actually being spent.
137{
138 if(onCart <= 0)
139 return false;
140 if(onCartLeft() < 0)
141 return false;
142
143 const int rate = GameCornerDB::inst()->getBuyPrice();
144 return buying() ? (player->money >= rate * onCart) // can pay for the coins
145 : (player->coins >= onCart); // owns the coins to sell
146}
147
149{
150 if(!canCheckout())
151 return;
152
153 player->money += moneyDelta();
154 player->coins += coinsDelta();
155
156 player->moneyChanged();
157 player->coinsChanged();
158
159 onCart = 0;
160}
int getBuyPrice() const
The buy rate (backs getBuyPrice).
static GameCornerDB * inst()
< Number of prize entries.
int getSellPrice() const
The sell rate (half the buy rate).
int coinsDelta() const
Signed coins change for this row's onCart (+buying, -selling).
ItemMarketEntryMoney(int forceDir=DirGlobal)
bool buying() const
This row's effective direction: true = Money=>Coins (spend money), false = Coins=>Money (spend coins)...
virtual bool _canSell() override
Subtype: compute sellability.
virtual QString _whichType() override
Subtype: report the type label.
@ DirGlobal
Follow the model's global buy/sell flag (legacy behaviour).
@ DirToCoins
Money => Coins (the "buy" direction).
virtual int _inStockCount() override
Subtype: compute the owned/sellable count.
static constexpr const char * type
This row's type key.
virtual int onCartLeft() override
Subtype: how many more may be added.
virtual int stackCount() override
Subtype: new stack slots needed (see note above).
virtual bool canCheckout() override
Exchange-aware affordability gate.
int forceDir
Fixed direction, or DirGlobal to follow the model.
virtual QString _name() override
Subtype: compute the display name.
virtual int _itemWorth() override
Subtype: compute the unit value.
int moneyDelta() const
Signed money change for this row's onCart (-cost buying, +gain selling).
virtual void checkout() override
Apply the money/coins change.
ItemMarketEntry(int compatMoneyCurrency=CompatEither, int compatBuyMode=CompatEither)
static PlayerBasics * player
Shared: player money/coins.
void finishConstruction()
Finalise construction (register the instance).
int onCart
Backing cart quantity.
bool exclude
Exclude from aggregate totals (see note).
static bool * isBuyMode
Shared: current buy/sell mode.