OpenTTD Source: src/misc_cmd.cpp Source File

OpenTTD Source 1.11.0-RC1

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 "cmd_helper.h"
14 #include "window_func.h"
15 #include "textbuf_gui.h"
16 #include "network/network.h"
17 #include "network/network_func.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
26 #include "table/strings.h"
27
28 #include "safeguards.h"
29
30 /* Make sure we can discard lower 2 bits of 64bit amount when passing it to Cmd[In|De]creaseLoan() */
31 static_assert((LOAN_INTERVAL & 3) == 0);
32
45 CommandCost CmdIncreaseLoan(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
46 {
47 Company *c = Company::Get(_current_company);
48
49 if (c->current_loan >= _economy.max_loan) {
50 SetDParam(0, _economy.max_loan);
51 return_cmd_error(STR_ERROR_MAXIMUM_PERMITTED_LOAN);
52 }
53
54 Money loan;
55 switch (p2 & 3) {
56 default: return CMD_ERROR; // Invalid method
57 case 0: // Take some extra loan
58 loan = LOAN_INTERVAL;
59 break;
60 case 1: // Take a loan as big as possible
61 loan = _economy.max_loan - c->current_loan;
62 break;
63 case 2: // Take the given amount of loan
64 loan = ((uint64)p1 << 32) | (p2 & 0xFFFFFFFC);
65 if (loan < LOAN_INTERVAL || c->current_loan + loan > _economy.max_loan || loan % LOAN_INTERVAL != 0) return CMD_ERROR;
66 break;
67 }
68
69 /* Overflow protection */
70 if (c->money + c->current_loan + loan < c->money) return CMD_ERROR;
71
72 if (flags & DC_EXEC) {
73 c->money += loan;
74 c->current_loan += loan;
75 InvalidateCompanyWindows(c);
76 }
77
78 return CommandCost(EXPENSES_OTHER);
79 }
80
93 CommandCost CmdDecreaseLoan(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
94 {
95 Company *c = Company::Get(_current_company);
96
97 if (c->current_loan == 0) return_cmd_error(STR_ERROR_LOAN_ALREADY_REPAYED);
98
99 Money loan;
100 switch (p2 & 3) {
101 default: return CMD_ERROR; // Invalid method
102 case 0: // Pay back one step
103 loan = std::min(c->current_loan, (Money)LOAN_INTERVAL);
104 break;
105 case 1: // Pay back as much as possible
106 loan = std::max(std::min(c->current_loan, c->money), (Money)LOAN_INTERVAL);
107 loan -= loan % LOAN_INTERVAL;
108 break;
109 case 2: // Repay the given amount of loan
110 loan = ((uint64)p1 << 32) | (p2 & 0xFFFFFFFC);
111 if (loan % LOAN_INTERVAL != 0 || loan < LOAN_INTERVAL || loan > c->current_loan) return CMD_ERROR; // Invalid amount to loan
112 break;
113 }
114
115 if (c->money < loan) {
116 SetDParam(0, loan);
117 return_cmd_error(STR_ERROR_CURRENCY_REQUIRED);
118 }
119
120 if (flags & DC_EXEC) {
121 c->money -= loan;
122 c->current_loan -= loan;
123 InvalidateCompanyWindows(c);
124 }
125 return CommandCost();
126 }
127
134 static void AskUnsafeUnpauseCallback(Window *w, bool confirmed)
135 {
136 if (confirmed) {
137 DoCommandP(0, PM_PAUSED_ERROR, 0, CMD_PAUSE);
138 }
139 }
140
153 CommandCost CmdPause(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
154 {
155 switch (p1) {
156 case PM_PAUSED_SAVELOAD:
157 case PM_PAUSED_ERROR:
158 case PM_PAUSED_NORMAL:
159 case PM_PAUSED_GAME_SCRIPT:
160 case PM_PAUSED_LINK_GRAPH:
161 break;
162
163 case PM_PAUSED_JOIN:
164 case PM_PAUSED_ACTIVE_CLIENTS:
165 if (!_networking) return CMD_ERROR;
166 break;
167
168 default: return CMD_ERROR;
169 }
170 if (flags & DC_EXEC) {
171 if (p1 == PM_PAUSED_NORMAL && _pause_mode & PM_PAUSED_ERROR) {
172 ShowQuery(
173 STR_NEWGRF_UNPAUSE_WARNING_TITLE,
174 STR_NEWGRF_UNPAUSE_WARNING,
175 nullptr,
176 AskUnsafeUnpauseCallback
177 );
178 } else {
179 PauseMode prev_mode = _pause_mode;
180
181 if (p2 == 0) {
182 _pause_mode = static_cast<PauseMode>(_pause_mode & (byte)~p1);
183 } else {
184 _pause_mode = static_cast<PauseMode>(_pause_mode | (byte)p1);
185 }
186
187 NetworkHandlePauseChange(prev_mode, (PauseMode)p1);
188 }
189
190 SetWindowDirty(WC_STATUS_BAR, 0);
191 SetWindowDirty(WC_MAIN_TOOLBAR, 0);
192 }
193 return CommandCost();
194 }
195
205 CommandCost CmdMoneyCheat(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
206 {
207 return CommandCost(EXPENSES_OTHER, -(int32)p1);
208 }
209
220 CommandCost CmdChangeBankBalance(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
221 {
222 int32 delta = (int32)p1;
223 CompanyID company = (CompanyID) GB(p2, 0, 8);
224 ExpensesType expenses_type = Extract<ExpensesType, 8, 8>(p2);
225
226 if (!Company::IsValidID(company)) return CMD_ERROR;
227 if (expenses_type >= EXPENSES_END) return CMD_ERROR;
228 if (_current_company != OWNER_DEITY) return CMD_ERROR;
229
230 if (flags & DC_EXEC) {
231 /* Change company bank balance of company. */
232 Backup<CompanyID> cur_company(_current_company, company, FILE_LINE);
233 SubtractMoneyFromCompany(CommandCost(expenses_type, -delta));
234 cur_company.Restore();
235
236 if (tile != 0) {
237 ShowCostOrIncomeAnimation(TileX(tile) * TILE_SIZE, TileY(tile) * TILE_SIZE, GetTilePixelZ(tile), -delta);
238 }
239 }
240
241 /* This command doesn't cost anything for deity. */
242 CommandCost zero_cost(expenses_type, 0);
243 return zero_cost;
244 }
TileIndex
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:78
CmdIncreaseLoan
CommandCost CmdIncreaseLoan(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
Increase the loan of your company.
Definition: misc_cmd.cpp:45
Pool::PoolItem<&_company_pool >::Get
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:329
EXPENSES_END
@ EXPENSES_END
Number of expense types.
Definition: economy_type.h:171
SetWindowDirty
void SetWindowDirty(WindowClass cls, WindowNumber number)
Mark window as dirty (in need of repainting)
Definition: window.cpp:3220
GB
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
Definition: bitmath_func.hpp:32
command_func.h
CMD_ERROR
static const CommandCost CMD_ERROR
Define a default return value for a failed command.
Definition: command_func.h:23
Backup
Class to backup a specific variable and restore it later.
Definition: backup_type.hpp:21
company_base.h
CmdChangeBankBalance
CommandCost CmdChangeBankBalance(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
Change the bank bank balance of a company by inserting or removing money without affecting the loan.
Definition: misc_cmd.cpp:220
EXPENSES_OTHER
@ EXPENSES_OTHER
Other expenses.
Definition: economy_type.h:170
LOAN_INTERVAL
static const int LOAN_INTERVAL
The "steps" in loan size, in British Pounds!
Definition: economy_type.h:198
company_gui.h
TILE_SIZE
static const uint TILE_SIZE
Tile size in world coordinates.
Definition: tile_type.h:13
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:337
Economy::max_loan
Money max_loan
NOSAVE: Maximum possible loan.
Definition: economy_type.h:29
TileY
static uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition: map_func.h:215
economy_func.h
Owner
Owner
Enum for all companies/owners.
Definition: company_type.h:18
DC_EXEC
@ DC_EXEC
execute the given command
Definition: command_type.h:348
SetDParam
static void SetDParam(uint n, uint64 v)
Set a string parameter v at index n in the global string parameter array.
Definition: strings_func.h:199
DoCommandFlag
DoCommandFlag
List of flags for a command.
Definition: command_type.h:346
CompanyProperties::current_loan
Money current_loan
Amount of money borrowed from the bank.
Definition: company_base.h:68
textbuf_gui.h
TileX
static uint TileX(TileIndex tile)
Get the X component of a tile.
Definition: map_func.h:205
tile_map.h
return_cmd_error
#define return_cmd_error(errcode)
Returns from a function with a specific StringID as error.
Definition: command_func.h:33
CommandCost
Common return value for all commands.
Definition: command_type.h:23
cmd_helper.h
PM_PAUSED_SAVELOAD
@ PM_PAUSED_SAVELOAD
A game paused for saving/loading.
Definition: openttd.h:61
DoCommandP
bool DoCommandP(const CommandContainer *container, bool my_cmd)
Shortcut for the long DoCommandP when having a container with the data.
Definition: command.cpp:541
CMD_PAUSE
@ CMD_PAUSE
pause the game
Definition: command_type.h:256
AskUnsafeUnpauseCallback
static void AskUnsafeUnpauseCallback(Window *w, bool confirmed)
In case of an unsafe unpause, we want the user to confirm that it might crash.
Definition: misc_cmd.cpp:134
CompanyProperties::money
Money money
Money owned by the company.
Definition: company_base.h:66
InvalidateCompanyWindows
void InvalidateCompanyWindows(const Company *company)
Refresh all windows owned by a company.
Definition: company_cmd.cpp:183
_pause_mode
PauseMode _pause_mode
The current pause mode.
Definition: gfx.cpp:47
safeguards.h
_networking
bool _networking
are we in networking mode?
Definition: network.cpp:52
PauseMode
PauseMode
Modes of pausing we've got.
Definition: openttd.h:58
stdafx.h
ShowCostOrIncomeAnimation
void ShowCostOrIncomeAnimation(int x, int y, int z, Money cost)
Display animated income or costs on the map.
Definition: misc_gui.cpp:593
PM_PAUSED_GAME_SCRIPT
@ PM_PAUSED_GAME_SCRIPT
A game paused by a game script.
Definition: openttd.h:65
CmdPause
CommandCost CmdPause(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
Pause/Unpause the game (server-only).
Definition: misc_cmd.cpp:153
texteff.hpp
CmdDecreaseLoan
CommandCost CmdDecreaseLoan(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
Decrease the loan of your company.
Definition: misc_cmd.cpp:93
PM_PAUSED_ACTIVE_CLIENTS
@ PM_PAUSED_ACTIVE_CLIENTS
A game paused for 'min_active_clients'.
Definition: openttd.h:64
ShowQuery
void ShowQuery(StringID caption, StringID message, Window *parent, QueryCallbackProc *callback)
Show a modal confirmation window with standard 'yes' and 'no' buttons The window is aligned to the ce...
Definition: misc_gui.cpp:1274
_current_company
CompanyID _current_company
Company currently doing an action.
Definition: company_cmd.cpp:47
strings_func.h
GetTilePixelZ
static int GetTilePixelZ(TileIndex tile)
Get bottom height of the tile.
Definition: tile_map.h:294
Backup::Restore
void Restore()
Restore the variable.
Definition: backup_type.hpp:112
PM_PAUSED_NORMAL
@ PM_PAUSED_NORMAL
A game normally paused.
Definition: openttd.h:60
OWNER_DEITY
@ OWNER_DEITY
The object is owned by a superuser / goal script.
Definition: company_type.h:27
company_func.h
PM_PAUSED_LINK_GRAPH
@ PM_PAUSED_LINK_GRAPH
A game paused due to the link graph schedule lagging.
Definition: openttd.h:66
PM_PAUSED_JOIN
@ PM_PAUSED_JOIN
A game paused for 'pause_on_join'.
Definition: openttd.h:62
network.h
window_func.h
OverflowSafeInt< int64, INT64_MAX, INT64_MIN >
CmdMoneyCheat
CommandCost CmdMoneyCheat(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
Change the financial flow of your company.
Definition: misc_cmd.cpp:205
Window
Data structure for an opened window.
Definition: window_gui.h:276
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:318
WC_STATUS_BAR
@ WC_STATUS_BAR
Statusbar (at the bottom of your screen); Window numbers:
Definition: window_type.h:57
SubtractMoneyFromCompany
void SubtractMoneyFromCompany(const CommandCost &cost)
Subtract money from the _current_company, if the company is valid.
Definition: company_cmd.cpp:243
ExpensesType
ExpensesType
Types of expenses.
Definition: economy_type.h:157
PM_PAUSED_ERROR
@ PM_PAUSED_ERROR
A game paused because a (critical) error.
Definition: openttd.h:63
Company
Definition: company_base.h:110
WC_MAIN_TOOLBAR
@ WC_MAIN_TOOLBAR
Main toolbar (the long bar at the top); Window numbers:
Definition: window_type.h:51
network_func.h
backup_type.hpp