OpenTTD Source 20260206-master-g4d4e37dbf1
misc_cmd.cpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#include "stdafx.h"
11#include "openttd.h"
12#include "command_func.h"
13#include "economy_func.h"
14#include "window_func.h"
15#include "textbuf_gui.h"
16#include "network/network.h"
18#include "strings_func.h"
19#include "company_func.h"
20#include "company_gui.h"
21#include "company_base.h"
22#include "tile_map.h"
23#include "texteff.hpp"
24#include "core/backup_type.hpp"
25#include "misc_cmd.h"
27
28#include "table/strings.h"
29
30#include "safeguards.h"
31
41CommandCost CmdIncreaseLoan(DoCommandFlags flags, LoanCommand cmd, Money amount)
42{
44 Money max_loan = c->GetMaxLoan();
45 if (c->current_loan >= max_loan) {
46 return CommandCostWithParam(STR_ERROR_MAXIMUM_PERMITTED_LOAN, max_loan);
47 }
48
49 Money loan;
50 switch (cmd) {
51 default: return CMD_ERROR; // Invalid method
52 case LoanCommand::Interval: // Take some extra loan
53 loan = LOAN_INTERVAL;
54 break;
55 case LoanCommand::Max: // Take a loan as big as possible
56 loan = max_loan - c->current_loan;
57 break;
58 case LoanCommand::Amount: // Take the given amount of loan
59 loan = amount;
60 if (loan < LOAN_INTERVAL || c->current_loan + loan > max_loan || loan % LOAN_INTERVAL != 0) return CMD_ERROR;
61 break;
62 }
63
64 /* In case adding the loan triggers the overflow protection of Money,
65 * we would essentially be losing money as taking and repaying the loan
66 * immediately would not get us back to the same bank balance anymore. */
67 if (c->money > Money::max() - loan) return CMD_ERROR;
68
69 if (flags.Test(DoCommandFlag::Execute)) {
70 c->money += loan;
71 c->current_loan += loan;
73 }
74
76}
77
87CommandCost CmdDecreaseLoan(DoCommandFlags flags, LoanCommand cmd, Money amount)
88{
90
91 if (c->current_loan == 0) return CommandCost(STR_ERROR_LOAN_ALREADY_REPAID);
92
93 Money loan;
94 switch (cmd) {
95 default: return CMD_ERROR; // Invalid method
96 case LoanCommand::Interval: // Pay back one step
97 loan = std::min(c->current_loan, (Money)LOAN_INTERVAL);
98 break;
99 case LoanCommand::Max: // Pay back as much as possible
100 loan = std::max(std::min(c->current_loan, GetAvailableMoneyForCommand()), (Money)LOAN_INTERVAL);
101 loan -= loan % LOAN_INTERVAL;
102 break;
103 case LoanCommand::Amount: // Repay the given amount of loan
104 loan = amount;
105 if (loan % LOAN_INTERVAL != 0 || loan < LOAN_INTERVAL || loan > c->current_loan) return CMD_ERROR; // Invalid amount to loan
106 break;
107 }
108
109 if (GetAvailableMoneyForCommand() < loan) {
110 return CommandCostWithParam(STR_ERROR_CURRENCY_REQUIRED, loan);
111 }
112
113 if (flags.Test(DoCommandFlag::Execute)) {
114 c->money -= loan;
115 c->current_loan -= loan;
117 }
118 return CommandCost();
119}
120
128CommandCost CmdSetCompanyMaxLoan(DoCommandFlags flags, CompanyID company, Money amount)
129{
131 if (amount != COMPANY_MAX_LOAN_DEFAULT) {
132 if (amount < 0 || amount > (Money)MAX_LOAN_LIMIT) return CMD_ERROR;
133 }
134
135 Company *c = Company::GetIfValid(company);
136 if (c == nullptr) return CMD_ERROR;
137
138 if (flags.Test(DoCommandFlag::Execute)) {
139 /* Round the amount down to a multiple of LOAN_INTERVAL. */
140 if (amount != COMPANY_MAX_LOAN_DEFAULT) amount -= (int64_t)amount % LOAN_INTERVAL;
141
142 c->max_loan = amount;
144 }
145 return CommandCost();
146}
147
153static void AskUnsafeUnpauseCallback(Window *, bool confirmed)
154{
155 if (confirmed) {
156 Command<Commands::Pause>::Post(PauseMode::Error, false);
157 }
158}
159
170CommandCost CmdPause(DoCommandFlags flags, PauseMode mode, bool pause)
171{
172 switch (mode) {
174 case PauseMode::Error:
178 break;
179
180 case PauseMode::Join:
182 if (!_networking) return CMD_ERROR;
183 break;
184
185 default: return CMD_ERROR;
186 }
187 if (flags.Test(DoCommandFlag::Execute)) {
188 if (mode == PauseMode::Normal && _pause_mode.Test(PauseMode::Error)) {
189 ShowQuery(
190 GetEncodedString(STR_NEWGRF_UNPAUSE_WARNING_TITLE),
191 GetEncodedString(STR_NEWGRF_UNPAUSE_WARNING),
192 nullptr,
194 );
195 } else {
196 PauseModes prev_mode = _pause_mode;
197
198 if (pause) {
199 _pause_mode.Set(mode);
200 } else {
201 _pause_mode.Reset(mode);
202
203 /* If the only remaining reason to be paused is that we saw a command during pause, unpause. */
205 _pause_mode = {};
206 }
207 }
208
209 NetworkHandlePauseChange(prev_mode, mode);
210
211 /* Screensaver should always be inhibited unless we're paused. */
213 }
214
217 }
218 return CommandCost();
219}
220
226CommandCost CmdMoneyCheat(DoCommandFlags, Money amount)
227{
228 return CommandCost(EXPENSES_OTHER, -amount);
229}
230
240CommandCost CmdChangeBankBalance(DoCommandFlags flags, TileIndex tile, Money delta, CompanyID company, ExpensesType expenses_type)
241{
242 if (!Company::IsValidID(company)) return CMD_ERROR;
243 if (expenses_type >= EXPENSES_END) return CMD_ERROR;
245
246 if (flags.Test(DoCommandFlag::Execute)) {
247 /* Change company bank balance of company. */
248 SubtractMoneyFromCompany(company, CommandCost(expenses_type, -delta));
249
250 if (tile != 0) {
251 ShowCostOrIncomeAnimation(TileX(tile) * TILE_SIZE, TileY(tile) * TILE_SIZE, GetTilePixelZ(tile), -delta);
252 }
253 }
254
255 /* This command doesn't cost anything for deity. */
256 CommandCost zero_cost(expenses_type, (Money)0);
257 return zero_cost;
258}
Class for backupping variables and making sure they are restored later.
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
Common return value for all commands.
virtual void SetScreensaverInhibited(bool inhibited)
Prevents the system from going to sleep.
static VideoDriver * GetInstance()
Get the currently active instance of the video driver.
CommandCost CommandCostWithParam(StringID str, uint64_t value)
Return an error status, with string and parameter.
Definition command.cpp:416
Functions related to commands.
static const CommandCost CMD_ERROR
Define a default return value for a failed command.
@ Execute
execute the given command
Definition of stuff that is very close to a company, like the company struct itself.
void InvalidateCompanyWindows(const Company *company)
Mark all finance windows owned by a company as needing a refresh.
static void SubtractMoneyFromCompany(Company *c, const CommandCost &cost)
Deduct costs of a command from the money of a company.
Money GetAvailableMoneyForCommand()
This functions returns the money which can be used to execute a command.
CompanyID _current_company
Company currently doing an action.
Functions related to companies.
GUI Functions related to companies.
static constexpr Owner OWNER_DEITY
The object is owned by a superuser / goal script.
Functions related to the economy.
static const int64_t MAX_LOAN_LIMIT
The max amount possible to configure for a max loan of a company.
ExpensesType
Types of expenses.
@ EXPENSES_END
Number of expense types.
@ EXPENSES_OTHER
Other expenses.
static const int LOAN_INTERVAL
The "steps" in loan size, in British Pounds!
PauseModes _pause_mode
The current pause mode.
Definition gfx.cpp:51
static uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition map_func.h:427
static uint TileX(TileIndex tile)
Get the X component of a tile.
Definition map_func.h:417
CommandCost CmdChangeBankBalance(DoCommandFlags flags, TileIndex tile, Money delta, CompanyID company, ExpensesType expenses_type)
Change the bank bank balance of a company by inserting or removing money without affecting the loan.
Definition misc_cmd.cpp:240
CommandCost CmdDecreaseLoan(DoCommandFlags flags, LoanCommand cmd, Money amount)
Decrease the loan of your company.
Definition misc_cmd.cpp:87
CommandCost CmdIncreaseLoan(DoCommandFlags flags, LoanCommand cmd, Money amount)
Increase the loan of your company.
Definition misc_cmd.cpp:41
CommandCost CmdMoneyCheat(DoCommandFlags, Money amount)
Change the financial flow of your company.
Definition misc_cmd.cpp:226
CommandCost CmdPause(DoCommandFlags flags, PauseMode mode, bool pause)
Pause/Unpause the game (server-only).
Definition misc_cmd.cpp:170
CommandCost CmdSetCompanyMaxLoan(DoCommandFlags flags, CompanyID company, Money amount)
Sets the max loan amount of your company.
Definition misc_cmd.cpp:128
static void AskUnsafeUnpauseCallback(Window *, bool confirmed)
In case of an unsafe unpause, we want the user to confirm that it might crash.
Definition misc_cmd.cpp:153
Miscellaneous command definitions.
LoanCommand
Different ways to determine the amount to loan/repay.
Definition misc_cmd.h:18
@ Max
Loan/repay the maximum amount permitting money/settings.
Definition misc_cmd.h:20
@ Amount
Loan/repay the given amount.
Definition misc_cmd.h:21
@ Interval
Loan/repay LOAN_INTERVAL.
Definition misc_cmd.h:19
void ShowQuery(EncodedString &&caption, EncodedString &&message, Window *parent, QueryCallbackProc *callback, bool focus)
Show a confirmation window with standard 'yes' and 'no' buttons The window is aligned to the centre o...
void ShowCostOrIncomeAnimation(int x, int y, int z, Money cost)
Display animated income or costs on the map.
Definition misc_gui.cpp:513
void NetworkHandlePauseChange(PauseModes prev_mode, PauseMode changed_mode)
Handle the pause mode change so we send the right messages to the chat.
Definition network.cpp:351
bool _networking
are we in networking mode?
Definition network.cpp:66
Basic functions/variables used all over the place.
Network functions used by other parts of OpenTTD.
Some generic types.
PauseMode
Modes of pausing we've got.
Definition openttd.h:68
@ LinkGraph
A game paused due to the link graph schedule lagging.
Definition openttd.h:75
@ Error
A game paused because a (critical) error.
Definition openttd.h:72
@ GameScript
A game paused by a game script.
Definition openttd.h:74
@ ActiveClients
A game paused for 'min_active_clients'.
Definition openttd.h:73
@ Normal
A game normally paused.
Definition openttd.h:69
@ Join
A game paused for 'pause_on_join'.
Definition openttd.h:71
@ CommandDuringPause
A game paused, and a command executed during the pause; resets on autosave.
Definition openttd.h:76
@ SaveLoad
A game paused for saving/loading.
Definition openttd.h:70
A number of safeguards to prevent using unsafe methods.
Definition of base types and functions in a cross-platform compatible way.
EncodedString GetEncodedString(StringID str)
Encode a string with no parameters into an encoded string.
Definition strings.cpp:90
Functions related to OTTD's strings.
Money current_loan
Amount of money borrowed from the bank.
Money max_loan
Max allowed amount of the loan or COMPANY_MAX_LOAN_DEFAULT.
Money money
Money owned by the company.
Money GetMaxLoan() const
Calculate the max allowed loan for this company.
static Company * Get(auto index)
static Company * GetIfValid(auto index)
Data structure for an opened window.
Definition window_gui.h:274
Stuff related to the text buffer GUI.
Functions related to text effects.
Map writing/reading functions for tiles.
int GetTilePixelZ(TileIndex tile)
Get bottom height of the tile.
Definition tile_map.h:302
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > > TileIndex
The index/ID of a Tile.
Definition tile_type.h:92
static constexpr uint TILE_SIZE
Tile size in world coordinates.
Definition tile_type.h:15
Base of all video drivers.
void SetWindowDirty(WindowClass cls, WindowNumber number)
Mark window as dirty (in need of repainting).
Definition window.cpp:3188
Window functions not directly related to making/drawing windows.
@ WC_STATUS_BAR
Statusbar (at the bottom of your screen); Window numbers:
Definition window_type.h:69
@ WC_MAIN_TOOLBAR
Main toolbar (the long bar at the top); Window numbers:
Definition window_type.h:63