1 /* $Id: misc_cmd.cpp 25788 2013-09-21 13:07:42Z zuu $ */
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/>.
10 /** @file misc_cmd.cpp Some misc functions that are better fitted in other files, but never got moved there... */
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"
26 #include "table/strings.h"
28 #include "safeguards.h"
31 * Increase the loan of your company.
33 * @param flags operation to perform
34 * @param p1 amount to increase the loan with, multitude of LOAN_INTERVAL. Only used when p2 == 2.
35 * @param p2 when 0: loans LOAN_INTERVAL
36 * when 1: loans the maximum loan permitting money (press CTRL),
37 * when 2: loans the amount specified in p1
39 * @return the cost of this operation or an error
41 CommandCost
CmdIncreaseLoan(TileIndex tile
, DoCommandFlag flags
, uint32 p1
, uint32 p2
, const char *text
)
43 Company
*c
= Company::Get(_current_company
);
45 if (c
->current_loan
>= _economy
.max_loan
) {
46 SetDParam(0, _economy
.max_loan
);
47 return CommandError(STR_ERROR_MAXIMUM_PERMITTED_LOAN
);
52 default: return CommandError(); // Invalid method
53 case 0: // Take some extra loan
56 case 1: // Take a loan as big as possible
57 loan
= _economy
.max_loan
- c
->current_loan
;
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 CommandError();
65 /* Overflow protection */
66 if (c
->money
+ c
->current_loan
+ loan
< c
->money
) return CommandError();
68 if (flags
& DC_EXEC
) {
70 c
->current_loan
+= loan
;
71 InvalidateCompanyWindows(c
);
74 return CommandCost(EXPENSES_OTHER
);
78 * Decrease the loan of your company.
80 * @param flags operation to perform
81 * @param p1 amount to decrease the loan with, multitude of LOAN_INTERVAL. Only used when p2 == 2.
82 * @param p2 when 0: pays back LOAN_INTERVAL
83 * when 1: pays back the maximum loan permitting money (press CTRL),
84 * when 2: pays back the amount specified in p1
86 * @return the cost of this operation or an error
88 CommandCost
CmdDecreaseLoan(TileIndex tile
, DoCommandFlag flags
, uint32 p1
, uint32 p2
, const char *text
)
90 Company
*c
= Company::Get(_current_company
);
92 if (c
->current_loan
== 0) return CommandError(STR_ERROR_LOAN_ALREADY_REPAYED
);
96 default: return CommandError(); // Invalid method
97 case 0: // Pay back one step
98 loan
= min(c
->current_loan
, (Money
)LOAN_INTERVAL
);
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
;
104 case 2: // Repay the given amount of loan
105 if (p1
% LOAN_INTERVAL
!= 0 || (int32
)p1
< LOAN_INTERVAL
|| p1
> c
->current_loan
) return CommandError(); // Invalid amount to loan
110 if (c
->money
< loan
) {
112 return CommandError(STR_ERROR_CURRENCY_REQUIRED
);
115 if (flags
& DC_EXEC
) {
117 c
->current_loan
-= loan
;
118 InvalidateCompanyWindows(c
);
120 return CommandCost();
124 * In case of an unsafe unpause, we want the
125 * user to confirm that it might crash.
127 * @param confirmed whether the user confirms his/her action
129 static void AskUnsafeUnpauseCallback(Window
*w
, bool confirmed
)
132 DoCommandP(0, PM_PAUSED_ERROR
, 0, CMD_PAUSE
);
137 * Pause/Unpause the game (server-only).
138 * Set or unset a bit in the pause mode. If pause mode is zero the game is
139 * unpaused. A bitset is used instead of a boolean value/counter to have
140 * more control over the game when saving/loading, etc.
142 * @param flags operation to perform
143 * @param p1 the pause mode to change
144 * @param p2 1 pauses, 0 unpauses this mode
146 * @return the cost of this operation or an error
148 CommandCost
CmdPause(TileIndex tile
, DoCommandFlag flags
, uint32 p1
, uint32 p2
, const char *text
)
151 case PM_PAUSED_SAVELOAD
:
152 case PM_PAUSED_ERROR
:
153 case PM_PAUSED_NORMAL
:
154 case PM_PAUSED_GAME_SCRIPT
:
155 case PM_PAUSED_LINK_GRAPH
:
158 #ifdef ENABLE_NETWORK
160 case PM_PAUSED_ACTIVE_CLIENTS
:
161 if (!_networking
) return CommandError();
163 #endif /* ENABLE_NETWORK */
165 default: return CommandError();
167 if (flags
& DC_EXEC
) {
168 if (p1
== PM_PAUSED_NORMAL
&& _pause_mode
& PM_PAUSED_ERROR
) {
170 STR_NEWGRF_UNPAUSE_WARNING_TITLE
,
171 STR_NEWGRF_UNPAUSE_WARNING
,
173 AskUnsafeUnpauseCallback
176 #ifdef ENABLE_NETWORK
177 PauseMode prev_mode
= _pause_mode
;
178 #endif /* ENABLE_NETWORK */
181 _pause_mode
= _pause_mode
& ~p1
;
183 _pause_mode
= _pause_mode
| p1
;
186 #ifdef ENABLE_NETWORK
187 NetworkHandlePauseChange(prev_mode
, (PauseMode
)p1
);
188 #endif /* ENABLE_NETWORK */
191 SetWindowDirty(WC_STATUS_BAR
, 0);
192 SetWindowDirty(WC_MAIN_TOOLBAR
, 0);
194 return CommandCost();
198 * Change the financial flow of your company.
200 * @param flags operation to perform
201 * @param p1 the amount of money to receive (if positive), or spend (if negative)
204 * @return the cost of this operation or an error
206 CommandCost
CmdMoneyCheat(TileIndex tile
, DoCommandFlag flags
, uint32 p1
, uint32 p2
, const char *text
)
208 return CommandCost(EXPENSES_OTHER
, -(int32
)p1
);
212 * Change the bank bank balance of a company by inserting or removing money without affecting the loan.
214 * @param flags operation to perform
215 * @param p1 the amount of money to receive (if positive), or spend (if negative)
216 * @param p2 (bit 0-7) - the company ID.
217 * (bit 8-15) - the expenses type which should register the cost/income @see ExpensesType.
219 * @return zero cost or an error
221 CommandCost
CmdChangeBankBalance(TileIndex tile
, DoCommandFlag flags
, uint32 p1
, uint32 p2
, const char *text
)
223 int32 delta
= (int32
)p1
;
224 CompanyID company
= (CompanyID
) GB(p2
, 0, 8);
225 ExpensesType expenses_type
= Extract
<ExpensesType
, 8, 8>(p2
);
227 if (!Company::IsValidID(company
)) return CommandError();
228 if (expenses_type
>= EXPENSES_END
) return CommandError();
229 if (_current_company
!= OWNER_DEITY
) return CommandError();
231 if (flags
& DC_EXEC
) {
232 /* Change company bank balance of company. */
233 Backup
<CompanyByte
> cur_company(_current_company
, company
, FILE_LINE
);
234 SubtractMoneyFromCompany(CommandCost(expenses_type
, -delta
));
235 cur_company
.Restore();
238 /* This command doesn't cost anyting for deity. */
239 CommandCost
zero_cost(expenses_type
, 0);
244 * Transfer funds (money) from one company to another.
245 * To prevent abuse in multiplayer games you can only send money to other
246 * companies if you have paid off your loan (either explicitly, or implicitly
247 * given the fact that you have more money than loan).
249 * @param flags operation to perform
250 * @param p1 the amount of money to transfer; max 20.000.000
251 * @param p2 the company to transfer the money to
253 * @return the cost of this operation or an error
255 CommandCost
CmdGiveMoney(TileIndex tile
, DoCommandFlag flags
, uint32 p1
, uint32 p2
, const char *text
)
257 if (!_settings_game
.economy
.give_money
) return CommandError();
259 const Company
*c
= Company::Get(_current_company
);
260 CommandCost
amount(EXPENSES_OTHER
, min((Money
)p1
, (Money
)20000000LL));
261 CompanyID dest_company
= (CompanyID
)p2
;
263 /* You can only transfer funds that is in excess of your loan */
264 if (c
->money
- c
->current_loan
< amount
.GetCost() || amount
.GetCost() < 0) return CommandError();
265 if (!_networking
|| !Company::IsValidID(dest_company
)) return CommandError();
267 if (flags
& DC_EXEC
) {
268 /* Add money to company */
269 Backup
<CompanyByte
> cur_company(_current_company
, dest_company
, FILE_LINE
);
270 SubtractMoneyFromCompany(CommandCost(EXPENSES_OTHER
, -amount
.GetCost()));
271 cur_company
.Restore();
274 /* Subtract money from local-company */