OpenTTD Source  13.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 {
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 (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 = _economy.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 > _economy.max_loan || loan % LOAN_INTERVAL != 0) return CMD_ERROR;
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 
84 CommandCost CmdDecreaseLoan(DoCommandFlag flags, LoanCommand cmd, Money amount)
85 {
87 
88  if (c->current_loan == 0) return_cmd_error(STR_ERROR_LOAN_ALREADY_REPAYED);
89 
90  Money loan;
91  switch (cmd) {
92  default: return CMD_ERROR; // Invalid method
93  case LoanCommand::Interval: // Pay back one step
94  loan = std::min(c->current_loan, (Money)LOAN_INTERVAL);
95  break;
96  case LoanCommand::Max: // Pay back as much as possible
97  loan = std::max(std::min(c->current_loan, c->money), (Money)LOAN_INTERVAL);
98  loan -= loan % LOAN_INTERVAL;
99  break;
100  case LoanCommand::Amount: // Repay the given amount of loan
101  loan = amount;
102  if (loan % LOAN_INTERVAL != 0 || loan < LOAN_INTERVAL || loan > c->current_loan) return CMD_ERROR; // Invalid amount to loan
103  break;
104  }
105 
106  if (c->money < loan) {
107  SetDParam(0, loan);
108  return_cmd_error(STR_ERROR_CURRENCY_REQUIRED);
109  }
110 
111  if (flags & DC_EXEC) {
112  c->money -= loan;
113  c->current_loan -= loan;
115  }
116  return CommandCost();
117 }
118 
125 static void AskUnsafeUnpauseCallback(Window *w, bool confirmed)
126 {
127  if (confirmed) {
129  }
130 }
131 
142 CommandCost CmdPause(DoCommandFlag flags, PauseMode mode, bool pause)
143 {
144  switch (mode) {
145  case PM_PAUSED_SAVELOAD:
146  case PM_PAUSED_ERROR:
147  case PM_PAUSED_NORMAL:
150  break;
151 
152  case PM_PAUSED_JOIN:
154  if (!_networking) return CMD_ERROR;
155  break;
156 
157  default: return CMD_ERROR;
158  }
159  if (flags & DC_EXEC) {
160  if (mode == PM_PAUSED_NORMAL && _pause_mode & PM_PAUSED_ERROR) {
161  ShowQuery(
162  STR_NEWGRF_UNPAUSE_WARNING_TITLE,
163  STR_NEWGRF_UNPAUSE_WARNING,
164  nullptr,
166  );
167  } else {
168  PauseMode prev_mode = _pause_mode;
169 
170  if (pause) {
171  _pause_mode |= mode;
172  } else {
173  _pause_mode &= ~mode;
174  }
175 
176  NetworkHandlePauseChange(prev_mode, mode);
177  }
178 
181  }
182  return CommandCost();
183 }
184 
192 {
193  return CommandCost(EXPENSES_OTHER, -amount);
194 }
195 
206 {
207  if (!Company::IsValidID(company)) return CMD_ERROR;
208  if (expenses_type >= EXPENSES_END) return CMD_ERROR;
209  if (_current_company != OWNER_DEITY) return CMD_ERROR;
210 
211  if (flags & DC_EXEC) {
212  /* Change company bank balance of company. */
213  Backup<CompanyID> cur_company(_current_company, company, FILE_LINE);
214  SubtractMoneyFromCompany(CommandCost(expenses_type, -delta));
215  cur_company.Restore();
216 
217  if (tile != 0) {
218  ShowCostOrIncomeAnimation(TileX(tile) * TILE_SIZE, TileY(tile) * TILE_SIZE, GetTilePixelZ(tile), -delta);
219  }
220  }
221 
222  /* This command doesn't cost anything for deity. */
223  CommandCost zero_cost(expenses_type, 0);
224  return zero_cost;
225 }
Pool::PoolItem<&_company_pool >::Get
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:337
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:3136
command_func.h
CMD_ERROR
static const CommandCost CMD_ERROR
Define a default return value for a failed command.
Definition: command_func.h:28
Backup
Class to backup a specific variable and restore it later.
Definition: backup_type.hpp:21
company_base.h
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
misc_cmd.h
CmdDecreaseLoan
CommandCost CmdDecreaseLoan(DoCommandFlag flags, LoanCommand cmd, Money amount)
Decrease the loan of your company.
Definition: misc_cmd.cpp:84
TileIndex
The index/ID of a Tile.
Definition: tile_type.h:85
TILE_SIZE
static const uint TILE_SIZE
Tile size in world coordinates.
Definition: tile_type.h:15
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:340
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:350
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:196
DoCommandFlag
DoCommandFlag
List of flags for a command.
Definition: command_type.h:348
CompanyProperties::current_loan
Money current_loan
Amount of money borrowed from the bank.
Definition: company_base.h:69
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:38
CommandCost
Common return value for all commands.
Definition: command_type.h:24
PM_PAUSED_SAVELOAD
@ PM_PAUSED_SAVELOAD
A game paused for saving/loading.
Definition: openttd.h:63
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:125
CompanyProperties::money
Money money
Money owned by the company.
Definition: company_base.h:67
InvalidateCompanyWindows
void InvalidateCompanyWindows(const Company *company)
Refresh all windows owned by a company.
Definition: company_cmd.cpp:184
_pause_mode
PauseMode _pause_mode
The current pause mode.
Definition: gfx.cpp:50
CmdIncreaseLoan
CommandCost CmdIncreaseLoan(DoCommandFlag flags, LoanCommand cmd, Money amount)
Increase the loan of your company.
Definition: misc_cmd.cpp:39
CmdPause
CommandCost CmdPause(DoCommandFlag flags, PauseMode mode, bool pause)
Pause/Unpause the game (server-only).
Definition: misc_cmd.cpp:142
safeguards.h
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:205
_networking
bool _networking
are we in networking mode?
Definition: network.cpp:58
PauseMode
PauseMode
Modes of pausing we've got.
Definition: openttd.h:60
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:573
PM_PAUSED_GAME_SCRIPT
@ PM_PAUSED_GAME_SCRIPT
A game paused by a game script.
Definition: openttd.h:67
texteff.hpp
PM_PAUSED_ACTIVE_CLIENTS
@ PM_PAUSED_ACTIVE_CLIENTS
A game paused for 'min_active_clients'.
Definition: openttd.h:66
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:62
CmdMoneyCheat
CommandCost CmdMoneyCheat(DoCommandFlag flags, Money amount)
Change the financial flow of your company.
Definition: misc_cmd.cpp:191
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:68
PM_PAUSED_JOIN
@ PM_PAUSED_JOIN
A game paused for 'pause_on_join'.
Definition: openttd.h:64
network.h
CommandHelper
Definition: command_func.h:94
window_func.h
OverflowSafeInt< int64 >
Window
Data structure for an opened window.
Definition: window_gui.h:279
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:326
WC_STATUS_BAR
@ WC_STATUS_BAR
Statusbar (at the bottom of your screen); Window numbers:
Definition: window_type.h:56
SubtractMoneyFromCompany
void SubtractMoneyFromCompany(const CommandCost &cost)
Subtract money from the _current_company, if the company is valid.
Definition: company_cmd.cpp:244
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:65
Company
Definition: company_base.h:117
WC_MAIN_TOOLBAR
@ WC_MAIN_TOOLBAR
Main toolbar (the long bar at the top); Window numbers:
Definition: window_type.h:50
network_func.h
backup_type.hpp