OpenTTD
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 "core/backup_type.hpp"
23 
24 #include "table/strings.h"
25 
26 #include "safeguards.h"
27 
39 CommandCost CmdIncreaseLoan(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
40 {
42 
43  if (c->current_loan >= _economy.max_loan) {
44  SetDParam(0, _economy.max_loan);
45  return_cmd_error(STR_ERROR_MAXIMUM_PERMITTED_LOAN);
46  }
47 
48  Money loan;
49  switch (p2) {
50  default: return CMD_ERROR; // Invalid method
51  case 0: // Take some extra loan
52  loan = LOAN_INTERVAL;
53  break;
54  case 1: // Take a loan as big as possible
55  loan = _economy.max_loan - c->current_loan;
56  break;
57  case 2: // Take the given amount of loan
58  if ((int32)p1 < LOAN_INTERVAL || c->current_loan + (int32)p1 > _economy.max_loan || p1 % LOAN_INTERVAL != 0) return CMD_ERROR;
59  loan = p1;
60  break;
61  }
62 
63  /* Overflow protection */
64  if (c->money + c->current_loan + loan < c->money) return CMD_ERROR;
65 
66  if (flags & DC_EXEC) {
67  c->money += loan;
68  c->current_loan += loan;
70  }
71 
73 }
74 
86 CommandCost CmdDecreaseLoan(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
87 {
89 
90  if (c->current_loan == 0) return_cmd_error(STR_ERROR_LOAN_ALREADY_REPAYED);
91 
92  Money loan;
93  switch (p2) {
94  default: return CMD_ERROR; // Invalid method
95  case 0: // Pay back one step
96  loan = min(c->current_loan, (Money)LOAN_INTERVAL);
97  break;
98  case 1: // Pay back as much as possible
99  loan = max(min(c->current_loan, c->money), (Money)LOAN_INTERVAL);
100  loan -= loan % LOAN_INTERVAL;
101  break;
102  case 2: // Repay the given amount of loan
103  if (p1 % LOAN_INTERVAL != 0 || (int32)p1 < LOAN_INTERVAL || p1 > c->current_loan) return CMD_ERROR; // Invalid amount to loan
104  loan = p1;
105  break;
106  }
107 
108  if (c->money < loan) {
109  SetDParam(0, loan);
110  return_cmd_error(STR_ERROR_CURRENCY_REQUIRED);
111  }
112 
113  if (flags & DC_EXEC) {
114  c->money -= loan;
115  c->current_loan -= loan;
117  }
118  return CommandCost();
119 }
120 
127 static void AskUnsafeUnpauseCallback(Window *w, bool confirmed)
128 {
129  if (confirmed) {
131  }
132 }
133 
146 CommandCost CmdPause(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
147 {
148  switch (p1) {
149  case PM_PAUSED_SAVELOAD:
150  case PM_PAUSED_ERROR:
151  case PM_PAUSED_NORMAL:
153  break;
154 
155  case PM_PAUSED_JOIN:
157  if (!_networking) return CMD_ERROR;
158  break;
159 
160  default: return CMD_ERROR;
161  }
162  if (flags & DC_EXEC) {
164  ShowQuery(
165  STR_NEWGRF_UNPAUSE_WARNING_TITLE,
166  STR_NEWGRF_UNPAUSE_WARNING,
167  nullptr,
169  );
170  } else {
171  PauseMode prev_mode = _pause_mode;
172 
173  if (p2 == 0) {
174  _pause_mode = static_cast<PauseMode>(_pause_mode & (byte)~p1);
175  } else {
176  _pause_mode = static_cast<PauseMode>(_pause_mode | (byte)p1);
177  }
178 
179  NetworkHandlePauseChange(prev_mode, (PauseMode)p1);
180  }
181 
184  }
185  return CommandCost();
186 }
187 
197 CommandCost CmdMoneyCheat(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
198 {
199  return CommandCost(EXPENSES_OTHER, -(int32)p1);
200 }
201 
212 CommandCost CmdChangeBankBalance(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
213 {
214  int32 delta = (int32)p1;
215  CompanyID company = (CompanyID) GB(p2, 0, 8);
216  ExpensesType expenses_type = Extract<ExpensesType, 8, 8>(p2);
217 
218  if (!Company::IsValidID(company)) return CMD_ERROR;
219  if (expenses_type >= EXPENSES_END) return CMD_ERROR;
220  if (_current_company != OWNER_DEITY) return CMD_ERROR;
221 
222  if (flags & DC_EXEC) {
223  /* Change company bank balance of company. */
224  Backup<CompanyID> cur_company(_current_company, company, FILE_LINE);
225  SubtractMoneyFromCompany(CommandCost(expenses_type, -delta));
226  cur_company.Restore();
227  }
228 
229  /* This command doesn't cost anything for deity. */
230  CommandCost zero_cost(expenses_type, 0);
231  return zero_cost;
232 }
233 
246 CommandCost CmdGiveMoney(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
247 {
249 
251  CommandCost amount(EXPENSES_OTHER, min((Money)p1, (Money)20000000LL));
252  CompanyID dest_company = (CompanyID)p2;
253 
254  /* You can only transfer funds that is in excess of your loan */
255  if (c->money - c->current_loan < amount.GetCost() || amount.GetCost() < 0) return CMD_ERROR;
256  if (!_networking || !Company::IsValidID(dest_company)) return CMD_ERROR;
257 
258  if (flags & DC_EXEC) {
259  /* Add money to company */
260  Backup<CompanyID> cur_company(_current_company, dest_company, FILE_LINE);
262  cur_company.Restore();
263  }
264 
265  /* Subtract money from local-company */
266  return amount;
267 }
void InvalidateCompanyWindows(const Company *company)
Refresh all windows owned by a company.
Functions related to OTTD&#39;s strings.
Owner
Enum for all companies/owners.
Definition: company_type.h:18
CommandCost CmdGiveMoney(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
Transfer funds (money) from one company to another.
Definition: misc_cmd.cpp:246
A game paused for &#39;min_active_clients&#39;.
Definition: openttd.h:61
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:79
Definition of stuff that is very close to a company, like the company struct itself.
bool _networking
are we in networking mode?
Definition: network.cpp:52
void SetWindowDirty(WindowClass cls, WindowNumber number)
Mark window as dirty (in need of repainting)
Definition: window.cpp:3215
EconomySettings economy
settings to change the economy
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:291
static const CommandCost CMD_ERROR
Define a default return value for a failed command.
Definition: command_func.h:23
CommandCost CmdMoneyCheat(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
Change the financial flow of your company.
Definition: misc_cmd.cpp:197
static const int LOAN_INTERVAL
The "steps" in loan size, in British Pounds!
Definition: economy_type.h:189
bool give_money
allow giving other companies money
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:127
Helper functions to extract data from command parameters.
Other expenses.
Definition: economy_type.h:161
Money GetCost() const
The costs as made up to this moment.
Definition: command_type.h:82
Stuff related to the text buffer GUI.
Common return value for all commands.
Definition: command_type.h:23
static T max(const T a, const T b)
Returns the maximum of two values.
Definition: math_func.hpp:24
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:336
Class to backup a specific variable and restore it later.
Definition: backup_type.hpp:21
Data structure for an opened window.
Definition: window_gui.h:276
CommandCost CmdPause(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
Pause/Unpause the game (server-only).
Definition: misc_cmd.cpp:146
CommandCost CmdDecreaseLoan(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
Decrease the loan of your company.
Definition: misc_cmd.cpp:86
void ShowQuery(StringID caption, StringID message, Window *parent, QueryCallbackProc *callback)
Show a modal confirmation window with standard &#39;yes&#39; and &#39;no&#39; buttons The window is aligned to the ce...
Definition: misc_gui.cpp:1260
A game paused for saving/loading.
Definition: openttd.h:58
DoCommandFlag
List of flags for a command.
Definition: command_type.h:342
Money current_loan
Amount of money borrowed from the bank.
Definition: company_base.h:67
Definition of base types and functions in a cross-platform compatible way.
A number of safeguards to prevent using unsafe methods.
GUI Functions related to companies.
Money money
Money owned by the company.
Definition: company_base.h:65
A game normally paused.
Definition: openttd.h:57
Basic functions/variables used all over the place.
bool DoCommandP(const CommandContainer *container, bool my_cmd)
Shortcut for the long DoCommandP when having a container with the data.
Definition: command.cpp:532
static T min(const T a, const T b)
Returns the minimum of two values.
Definition: math_func.hpp:40
PauseMode _pause_mode
The current pause mode.
Definition: gfx.cpp:47
Money max_loan
NOSAVE: Maximum possible loan.
Definition: economy_type.h:20
PauseMode
Modes of pausing we&#39;ve got.
Definition: openttd.h:55
#define return_cmd_error(errcode)
Returns from a function with a specific StringID as error.
Definition: command_func.h:33
execute the given command
Definition: command_type.h:344
Functions related to companies.
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:212
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:78
Functions related to the economy.
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
void SubtractMoneyFromCompany(CommandCost cost)
Subtract money from the _current_company, if the company is valid.
CommandCost CmdIncreaseLoan(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
Increase the loan of your company.
Definition: misc_cmd.cpp:39
Statusbar (at the bottom of your screen); Window numbers:
Definition: window_type.h:57
Functions related to commands.
Network functions used by other parts of OpenTTD.
Main toolbar (the long bar at the top); Window numbers:
Definition: window_type.h:51
CompanyID _current_company
Company currently doing an action.
Definition: company_cmd.cpp:45
static bool IsValidID(size_t index)
Tests whether given index can be used to get valid (non-nullptr) Titem.
Definition: pool_type.hpp:280
void Restore()
Restore the variable.
A game paused by a game script.
Definition: openttd.h:62
Window functions not directly related to making/drawing windows.
A game paused for &#39;pause_on_join&#39;.
Definition: openttd.h:59
Number of expense types.
Definition: economy_type.h:162
ExpensesType
Types of expenses.
Definition: economy_type.h:148
The object is owned by a superuser / goal script.
Definition: company_type.h:27
Class for backupping variables and making sure they are restored later.
A game paused because a (critical) error.
Definition: openttd.h:60
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
pause the game
Definition: command_type.h:254