OpenTTD
misc_cmd.cpp
Go to the documentation of this file.
1 /* $Id: misc_cmd.cpp 26482 2014-04-23 20:13:33Z rubidium $ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * 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.
6  * 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.
7  * 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/>.
8  */
9 
12 #include "stdafx.h"
13 #include "command_func.h"
14 #include "economy_func.h"
15 #include "cmd_helper.h"
16 #include "window_func.h"
17 #include "textbuf_gui.h"
18 #include "network/network.h"
19 #include "network/network_func.h"
20 #include "strings_func.h"
21 #include "company_func.h"
22 #include "company_gui.h"
23 #include "company_base.h"
24 #include "core/backup_type.hpp"
25 
26 #include "table/strings.h"
27 
28 #include "safeguards.h"
29 
41 CommandCost CmdIncreaseLoan(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
42 {
44 
45  if (c->current_loan >= _economy.max_loan) {
46  SetDParam(0, _economy.max_loan);
47  return_cmd_error(STR_ERROR_MAXIMUM_PERMITTED_LOAN);
48  }
49 
50  Money loan;
51  switch (p2) {
52  default: return CMD_ERROR; // Invalid method
53  case 0: // Take some extra loan
54  loan = LOAN_INTERVAL;
55  break;
56  case 1: // Take a loan as big as possible
57  loan = _economy.max_loan - c->current_loan;
58  break;
59  case 2: // Take the given amount of loan
60  if ((int32)p1 < LOAN_INTERVAL || c->current_loan + (int32)p1 > _economy.max_loan || p1 % LOAN_INTERVAL != 0) return CMD_ERROR;
61  loan = p1;
62  break;
63  }
64 
65  /* Overflow protection */
66  if (c->money + c->current_loan + loan < c->money) return CMD_ERROR;
67 
68  if (flags & DC_EXEC) {
69  c->money += loan;
70  c->current_loan += loan;
72  }
73 
75 }
76 
88 CommandCost CmdDecreaseLoan(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
89 {
91 
92  if (c->current_loan == 0) return_cmd_error(STR_ERROR_LOAN_ALREADY_REPAYED);
93 
94  Money loan;
95  switch (p2) {
96  default: return CMD_ERROR; // Invalid method
97  case 0: // Pay back one step
98  loan = min(c->current_loan, (Money)LOAN_INTERVAL);
99  break;
100  case 1: // Pay back as much as possible
101  loan = max(min(c->current_loan, c->money), (Money)LOAN_INTERVAL);
102  loan -= loan % LOAN_INTERVAL;
103  break;
104  case 2: // Repay the given amount of loan
105  if (p1 % LOAN_INTERVAL != 0 || (int32)p1 < LOAN_INTERVAL || p1 > c->current_loan) return CMD_ERROR; // Invalid amount to loan
106  loan = p1;
107  break;
108  }
109 
110  if (c->money < loan) {
111  SetDParam(0, loan);
112  return_cmd_error(STR_ERROR_CURRENCY_REQUIRED);
113  }
114 
115  if (flags & DC_EXEC) {
116  c->money -= loan;
117  c->current_loan -= loan;
119  }
120  return CommandCost();
121 }
122 
129 static void AskUnsafeUnpauseCallback(Window *w, bool confirmed)
130 {
131  if (confirmed) {
133  }
134 }
135 
148 CommandCost CmdPause(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
149 {
150  switch (p1) {
151  case PM_PAUSED_SAVELOAD:
152  case PM_PAUSED_ERROR:
153  case PM_PAUSED_NORMAL:
155  break;
156 
157 #ifdef ENABLE_NETWORK
158  case PM_PAUSED_JOIN:
160  if (!_networking) return CMD_ERROR;
161  break;
162 #endif /* ENABLE_NETWORK */
163 
164  default: return CMD_ERROR;
165  }
166  if (flags & DC_EXEC) {
168  ShowQuery(
169  STR_NEWGRF_UNPAUSE_WARNING_TITLE,
170  STR_NEWGRF_UNPAUSE_WARNING,
171  NULL,
173  );
174  } else {
175 #ifdef ENABLE_NETWORK
176  PauseMode prev_mode = _pause_mode;
177 #endif /* ENABLE_NETWORK */
178 
179  if (p2 == 0) {
180  _pause_mode = _pause_mode & ~p1;
181  } else {
182  _pause_mode = _pause_mode | p1;
183  }
184 
185 #ifdef ENABLE_NETWORK
186  NetworkHandlePauseChange(prev_mode, (PauseMode)p1);
187 #endif /* ENABLE_NETWORK */
188  }
189 
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<CompanyByte> cur_company(_current_company, company, FILE_LINE);
233  SubtractMoneyFromCompany(CommandCost(expenses_type, -delta));
234  cur_company.Restore();
235  }
236 
237  /* This command doesn't cost anyting for deity. */
238  CommandCost zero_cost(expenses_type, 0);
239  return zero_cost;
240 }
241 
254 CommandCost CmdGiveMoney(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
255 {
257 
259  CommandCost amount(EXPENSES_OTHER, min((Money)p1, (Money)20000000LL));
260  CompanyID dest_company = (CompanyID)p2;
261 
262  /* You can only transfer funds that is in excess of your loan */
263  if (c->money - c->current_loan < amount.GetCost() || amount.GetCost() < 0) return CMD_ERROR;
264  if (!_networking || !Company::IsValidID(dest_company)) return CMD_ERROR;
265 
266  if (flags & DC_EXEC) {
267  /* Add money to company */
268  Backup<CompanyByte> cur_company(_current_company, dest_company, FILE_LINE);
270  cur_company.Restore();
271  }
272 
273  /* Subtract money from local-company */
274  return amount;
275 }
void InvalidateCompanyWindows(const Company *company)
Refresh all windows owned by a company.
Functions related to OTTD&#39;s strings.
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:254
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:77
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:56
A game paused for &#39;min_active_clients&#39;.
Definition: openttd.h:63
A game normally paused.
Definition: openttd.h:59
void SetWindowDirty(WindowClass cls, WindowNumber number)
Mark window as dirty (in need of repainting)
Definition: window.cpp:3120
EconomySettings economy
settings to change the economy
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:246
A game paused by a game script.
Definition: openttd.h:64
static const CommandCost CMD_ERROR
Define a default return value for a failed command.
Definition: command_func.h:25
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
static const int LOAN_INTERVAL
The "steps" in loan size, in British Pounds!
Definition: economy_type.h:192
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:129
Helper functions to extract data from command parameters.
A game paused for &#39;pause_on_join&#39;.
Definition: openttd.h:61
A game paused because a (critical) error.
Definition: openttd.h:62
Money GetCost() const
The costs as made up to this moment.
Definition: command_type.h:84
Stuff related to the text buffer GUI.
Common return value for all commands.
Definition: command_type.h:25
static T max(const T a, const T b)
Returns the maximum of two values.
Definition: math_func.hpp:26
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:348
Class to backup a specific variable and restore it later.
Definition: backup_type.hpp:23
The object is owned by a superuser / goal script.
Definition: company_type.h:29
Data structure for an opened window.
Definition: window_gui.h:271
CommandCost CmdPause(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
Pause/Unpause the game (server-only).
Definition: misc_cmd.cpp:148
CommandCost CmdDecreaseLoan(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
Decrease the loan of your company.
Definition: misc_cmd.cpp:88
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:1206
DoCommandFlag
List of flags for a command.
Definition: command_type.h:340
Money current_loan
Amount of money borrowed from the bank.
Definition: company_base.h:66
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.
ExpensesType
Types of expenses.
Definition: economy_type.h:150
Money money
Money owned by the company.
Definition: company_base.h:64
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:527
PauseModeByte _pause_mode
The current pause mode.
Definition: gfx.cpp:48
static T min(const T a, const T b)
Returns the minimum of two values.
Definition: math_func.hpp:42
Money max_loan
NOSAVE: Maximum possible loan.
Definition: economy_type.h:22
#define return_cmd_error(errcode)
Returns from a function with a specific StringID as error.
Definition: command_func.h:35
execute the given command
Definition: command_type.h:342
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:220
CompanyByte _current_company
Company currently doing an action.
Definition: company_cmd.cpp:47
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:80
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.
PauseMode
Modes of pausing we&#39;ve got.
Definition: openttd.h:57
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:41
Statusbar (at the bottom of your screen); Window numbers:
Definition: window_type.h:59
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:53
static bool IsValidID(size_t index)
Tests whether given index can be used to get valid (non-NULL) Titem.
Definition: pool_type.hpp:235
void Restore()
Restore the variable.
Number of expense types.
Definition: economy_type.h:164
Other expenses.
Definition: economy_type.h:163
A game paused for saving/loading.
Definition: openttd.h:60
Owner
Enum for all companies/owners.
Definition: company_type.h:20
Window functions not directly related to making/drawing windows.
Class for backupping variables and making sure they are restored later.
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:201
pause the game
Definition: command_type.h:255