OpenTTD Source: src/misc_cmd.cpp Source File

OpenTTD Source 15.0-beta1

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 <http://www.gnu.org/licenses/>.
6 */
7
10#include "stdafx.h"
11#include "command_func.h"
12#include "economy_func.h"
13#include "window_func.h"
14#include "textbuf_gui.h"
15#include "network/network.h"
16#include "network/network_func.h"
17#include "strings_func.h"
18#include "company_func.h"
19#include "company_gui.h"
20#include "company_base.h"
21#include "tile_map.h"
22#include "texteff.hpp"
23#include "core/backup_type.hpp"
24#include "misc_cmd.h"
25
26#include "table/strings.h"
27
28#include "safeguards.h"
29
39 CommandCost CmdIncreaseLoan(DoCommandFlag flags, LoanCommand cmd, Money amount)
40{
41 Company *c = Company::Get(_current_company);
42 Money max_loan = c->GetMaxLoan();
43 if (c->current_loan >= max_loan) {
44 SetDParam(0, max_loan);
45 return CommandCost(STR_ERROR_MAXIMUM_PERMITTED_LOAN);
46 }
47
48 Money loan;
49 switch (cmd) {
50 default: return CMD_ERROR; // Invalid method
51 case LoanCommand::Interval: // Take some extra loan
52 loan = LOAN_INTERVAL;
53 break;
54 case LoanCommand::Max: // Take a loan as big as possible
55 loan = max_loan - c->current_loan;
56 break;
57 case LoanCommand::Amount: // Take the given amount of loan
58 loan = amount;
59 if (loan < LOAN_INTERVAL || c->current_loan + loan > max_loan || loan % LOAN_INTERVAL != 0) return CMD_ERROR;
60 break;
61 }
62
63 /* In case adding the loan triggers the overflow protection of Money,
64 * we would essentially be losing money as taking and repaying the loan
65 * immediately would not get us back to the same bank balance anymore. */
66 if (c->money > Money::max() - loan) return CMD_ERROR;
67
68 if (flags & DC_EXEC) {
69 c->money += loan;
70 c->current_loan += loan;
71 InvalidateCompanyWindows(c);
72 }
73
74 return CommandCost(EXPENSES_OTHER);
75}
76
86 CommandCost CmdDecreaseLoan(DoCommandFlag flags, LoanCommand cmd, Money amount)
87{
88 Company *c = Company::Get(_current_company);
89
90 if (c->current_loan == 0) return CommandCost(STR_ERROR_LOAN_ALREADY_REPAYED);
91
92 Money loan;
93 switch (cmd) {
94 default: return CMD_ERROR; // Invalid method
95 case LoanCommand::Interval: // Pay back one step
96 loan = std::min(c->current_loan, (Money)LOAN_INTERVAL);
97 break;
98 case LoanCommand::Max: // Pay back as much as possible
99 loan = std::max(std::min(c->current_loan, GetAvailableMoneyForCommand()), (Money)LOAN_INTERVAL);
100 loan -= loan % LOAN_INTERVAL;
101 break;
102 case LoanCommand::Amount: // Repay the given amount of loan
103 loan = amount;
104 if (loan % LOAN_INTERVAL != 0 || loan < LOAN_INTERVAL || loan > c->current_loan) return CMD_ERROR; // Invalid amount to loan
105 break;
106 }
107
108 if (GetAvailableMoneyForCommand() < loan) {
109 SetDParam(0, loan);
110 return CommandCost(STR_ERROR_CURRENCY_REQUIRED);
111 }
112
113 if (flags & DC_EXEC) {
114 c->money -= loan;
115 c->current_loan -= loan;
116 InvalidateCompanyWindows(c);
117 }
118 return CommandCost();
119}
120
127 CommandCost CmdSetCompanyMaxLoan(DoCommandFlag flags, CompanyID company, Money amount)
128{
129 if (_current_company != OWNER_DEITY) return CMD_ERROR;
130 if (amount != COMPANY_MAX_LOAN_DEFAULT) {
131 if (amount < 0 || amount > (Money)MAX_LOAN_LIMIT) return CMD_ERROR;
132 }
133
134 Company *c = Company::GetIfValid(company);
135 if (c == nullptr) return CMD_ERROR;
136
137 if (flags & DC_EXEC) {
138 /* Round the amount down to a multiple of LOAN_INTERVAL. */
139 if (amount != COMPANY_MAX_LOAN_DEFAULT) amount -= (int64_t)amount % LOAN_INTERVAL;
140
141 c->max_loan = amount;
142 InvalidateCompanyWindows(c);
143 }
144 return CommandCost();
145}
146
152 static void AskUnsafeUnpauseCallback(Window *, bool confirmed)
153{
154 if (confirmed) {
155 Command<CMD_PAUSE>::Post(PM_PAUSED_ERROR, false);
156 }
157}
158
169 CommandCost CmdPause(DoCommandFlag flags, PauseMode mode, bool pause)
170{
171 switch (mode) {
172 case PM_PAUSED_SAVELOAD:
173 case PM_PAUSED_ERROR:
174 case PM_PAUSED_NORMAL:
175 case PM_PAUSED_GAME_SCRIPT:
176 case PM_PAUSED_LINK_GRAPH:
177 break;
178
179 case PM_PAUSED_JOIN:
180 case PM_PAUSED_ACTIVE_CLIENTS:
181 if (!_networking) return CMD_ERROR;
182 break;
183
184 default: return CMD_ERROR;
185 }
186 if (flags & DC_EXEC) {
187 if (mode == PM_PAUSED_NORMAL && _pause_mode & PM_PAUSED_ERROR) {
188 ShowQuery(
189 STR_NEWGRF_UNPAUSE_WARNING_TITLE,
190 STR_NEWGRF_UNPAUSE_WARNING,
191 nullptr,
192 AskUnsafeUnpauseCallback
193 );
194 } else {
195 PauseMode prev_mode = _pause_mode;
196
197 if (pause) {
198 _pause_mode |= mode;
199 } else {
200 _pause_mode &= ~mode;
201
202 /* If the only remaining reason to be paused is that we saw a command during pause, unpause. */
203 if (_pause_mode == PM_COMMAND_DURING_PAUSE) {
204 _pause_mode = PM_UNPAUSED;
205 }
206 }
207
208 NetworkHandlePauseChange(prev_mode, mode);
209 }
210
211 SetWindowDirty(WC_STATUS_BAR, 0);
212 SetWindowDirty(WC_MAIN_TOOLBAR, 0);
213 }
214 return CommandCost();
215}
216
222 CommandCost CmdMoneyCheat(DoCommandFlag, Money amount)
223{
224 return CommandCost(EXPENSES_OTHER, -amount);
225}
226
236 CommandCost CmdChangeBankBalance(DoCommandFlag flags, TileIndex tile, Money delta, CompanyID company, ExpensesType expenses_type)
237{
238 if (!Company::IsValidID(company)) return CMD_ERROR;
239 if (expenses_type >= EXPENSES_END) return CMD_ERROR;
240 if (_current_company != OWNER_DEITY) return CMD_ERROR;
241
242 if (flags & DC_EXEC) {
243 /* Change company bank balance of company. */
244 Backup<CompanyID> cur_company(_current_company, company);
245 SubtractMoneyFromCompany(CommandCost(expenses_type, -delta));
246 cur_company.Restore();
247
248 if (tile != 0) {
249 ShowCostOrIncomeAnimation(TileX(tile) * TILE_SIZE, TileY(tile) * TILE_SIZE, GetTilePixelZ(tile), -delta);
250 }
251 }
252
253 /* This command doesn't cost anything for deity. */
254 CommandCost zero_cost(expenses_type, (Money)0);
255 return zero_cost;
256}
backup_type.hpp
Class for backupping variables and making sure they are restored later.
CommandCost
Common return value for all commands.
Definition command_type.h:23
OverflowSafeInt< int64_t >
command_func.h
Functions related to commands.
CMD_ERROR
static const CommandCost CMD_ERROR
Define a default return value for a failed command.
Definition command_func.h:28
DoCommandFlag
DoCommandFlag
List of flags for a command.
Definition command_type.h:374
DC_EXEC
@ DC_EXEC
execute the given command
Definition command_type.h:376
company_base.h
Definition of stuff that is very close to a company, like the company struct itself.
InvalidateCompanyWindows
void InvalidateCompanyWindows(const Company *company)
Refresh all windows owned by a company.
Definition company_cmd.cpp:201
GetAvailableMoneyForCommand
Money GetAvailableMoneyForCommand()
This functions returns the money which can be used to execute a command.
Definition company_cmd.cpp:230
SubtractMoneyFromCompany
void SubtractMoneyFromCompany(const CommandCost &cost)
Subtract money from the _current_company, if the company is valid.
Definition company_cmd.cpp:289
_current_company
CompanyID _current_company
Company currently doing an action.
Definition company_cmd.cpp:53
company_func.h
Functions related to companies.
company_gui.h
GUI Functions related to companies.
Owner
Owner
Enum for all companies/owners.
Definition company_type.h:18
OWNER_DEITY
@ OWNER_DEITY
The object is owned by a superuser / goal script.
Definition company_type.h:27
economy_func.h
Functions related to the economy.
MAX_LOAN_LIMIT
static const int64_t MAX_LOAN_LIMIT
The max amount possible to configure for a max loan of a company.
Definition economy_type.h:219
ExpensesType
ExpensesType
Types of expenses.
Definition economy_type.h:172
EXPENSES_END
@ EXPENSES_END
Number of expense types.
Definition economy_type.h:186
EXPENSES_OTHER
@ EXPENSES_OTHER
Other expenses.
Definition economy_type.h:185
LOAN_INTERVAL
static const int LOAN_INTERVAL
The "steps" in loan size, in British Pounds!
Definition economy_type.h:215
_pause_mode
PauseMode _pause_mode
The current pause mode.
Definition gfx.cpp:50
TileY
static debug_inline uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition map_func.h:425
TileX
static debug_inline uint TileX(TileIndex tile)
Get the X component of a tile.
Definition map_func.h:415
CmdChangeBankBalance
CommandCost CmdChangeBankBalance(DoCommandFlag 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:236
CmdSetCompanyMaxLoan
CommandCost CmdSetCompanyMaxLoan(DoCommandFlag flags, CompanyID company, Money amount)
Sets the max loan amount of your company.
Definition misc_cmd.cpp:127
CmdPause
CommandCost CmdPause(DoCommandFlag flags, PauseMode mode, bool pause)
Pause/Unpause the game (server-only).
Definition misc_cmd.cpp:169
CmdDecreaseLoan
CommandCost CmdDecreaseLoan(DoCommandFlag flags, LoanCommand cmd, Money amount)
Decrease the loan of your company.
Definition misc_cmd.cpp:86
AskUnsafeUnpauseCallback
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:152
CmdMoneyCheat
CommandCost CmdMoneyCheat(DoCommandFlag, Money amount)
Change the financial flow of your company.
Definition misc_cmd.cpp:222
CmdIncreaseLoan
CommandCost CmdIncreaseLoan(DoCommandFlag flags, LoanCommand cmd, Money amount)
Increase the loan of your company.
Definition misc_cmd.cpp:39
misc_cmd.h
Miscellaneous command definitions.
ShowCostOrIncomeAnimation
void ShowCostOrIncomeAnimation(int x, int y, int z, Money cost)
Display animated income or costs on the map.
Definition misc_gui.cpp:550
ShowQuery
void ShowQuery(StringID caption, StringID 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...
Definition misc_gui.cpp:1203
_networking
bool _networking
are we in networking mode?
Definition network.cpp:65
NetworkHandlePauseChange
void NetworkHandlePauseChange(PauseMode prev_mode, PauseMode changed_mode)
Handle the pause mode change so we send the right messages to the chat.
Definition network.cpp:354
network.h
Basic functions/variables used all over the place.
network_func.h
Network functions used by other parts of OpenTTD.
PauseMode
PauseMode
Modes of pausing we've got.
Definition openttd.h:68
PM_COMMAND_DURING_PAUSE
@ PM_COMMAND_DURING_PAUSE
A game paused, and a command executed during the pause; resets on autosave.
Definition openttd.h:77
PM_UNPAUSED
@ PM_UNPAUSED
A normal unpaused game.
Definition openttd.h:69
PM_PAUSED_ACTIVE_CLIENTS
@ PM_PAUSED_ACTIVE_CLIENTS
A game paused for 'min_active_clients'.
Definition openttd.h:74
PM_PAUSED_ERROR
@ PM_PAUSED_ERROR
A game paused because a (critical) error.
Definition openttd.h:73
PM_PAUSED_GAME_SCRIPT
@ PM_PAUSED_GAME_SCRIPT
A game paused by a game script.
Definition openttd.h:75
PM_PAUSED_JOIN
@ PM_PAUSED_JOIN
A game paused for 'pause_on_join'.
Definition openttd.h:72
PM_PAUSED_LINK_GRAPH
@ PM_PAUSED_LINK_GRAPH
A game paused due to the link graph schedule lagging.
Definition openttd.h:76
PM_PAUSED_NORMAL
@ PM_PAUSED_NORMAL
A game normally paused.
Definition openttd.h:70
PM_PAUSED_SAVELOAD
@ PM_PAUSED_SAVELOAD
A game paused for saving/loading.
Definition openttd.h:71
safeguards.h
A number of safeguards to prevent using unsafe methods.
stdafx.h
Definition of base types and functions in a cross-platform compatible way.
SetDParam
void SetDParam(size_t n, uint64_t v)
Set a string parameter v at index n in the global string parameter array.
Definition strings.cpp:104
strings_func.h
Functions related to OTTD's strings.
Backup
Class to backup a specific variable and restore it later.
Definition backup_type.hpp:21
Backup::Restore
void Restore()
Restore the variable.
Definition backup_type.hpp:110
CommandHelper
Definition command_func.h:83
CompanyProperties::current_loan
Money current_loan
Amount of money borrowed from the bank.
Definition company_base.h:84
CompanyProperties::max_loan
Money max_loan
Max allowed amount of the loan or COMPANY_MAX_LOAN_DEFAULT.
Definition company_base.h:85
CompanyProperties::money
Money money
Money owned by the company.
Definition company_base.h:82
Company
Definition company_base.h:133
Company::GetMaxLoan
Money GetMaxLoan() const
Calculate the max allowed loan for this company.
Definition company_cmd.cpp:105
Pool::PoolItem<&_company_pool >::IsValidID
static bool IsValidID(size_t index)
Tests whether given index can be used to get valid (non-nullptr) Titem.
Definition pool_type.hpp:328
Pool::PoolItem<&_company_pool >::GetIfValid
static Titem * GetIfValid(size_t index)
Returns Titem with given index.
Definition pool_type.hpp:350
Pool::PoolItem<&_company_pool >::Get
static Titem * Get(size_t index)
Returns Titem with given index.
Definition pool_type.hpp:339
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > >
Window
Data structure for an opened window.
Definition window_gui.h:273
textbuf_gui.h
Stuff related to the text buffer GUI.
texteff.hpp
Functions related to text effects.
tile_map.h
Map writing/reading functions for tiles.
GetTilePixelZ
int GetTilePixelZ(TileIndex tile)
Get bottom height of the tile.
Definition tile_map.h:302
TILE_SIZE
static const uint TILE_SIZE
Tile size in world coordinates.
Definition tile_type.h:15
SetWindowDirty
void SetWindowDirty(WindowClass cls, WindowNumber number)
Mark window as dirty (in need of repainting)
Definition window.cpp:3101
window_func.h
Window functions not directly related to making/drawing windows.
WC_STATUS_BAR
@ WC_STATUS_BAR
Statusbar (at the bottom of your screen); Window numbers:
Definition window_type.h:64
WC_MAIN_TOOLBAR
@ WC_MAIN_TOOLBAR
Main toolbar (the long bar at the top); Window numbers:
Definition window_type.h:58