Silence uninitialised variable warnings in SSE blitters
[openttd/fttd.git] / src / misc_cmd.cpp
blobc35ccc8f82b75f3a17f83a2c4296c409ae26a66c
1 /* $Id$ */
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 */
10 /** @file misc_cmd.cpp Some misc functions that are better fitted in other files, but never got moved there... */
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"
26 #include "table/strings.h"
28 /**
29 * Increase the loan of your company.
30 * @param tile unused
31 * @param flags operation to perform
32 * @param p1 amount to increase the loan with, multitude of LOAN_INTERVAL. Only used when p2 == 2.
33 * @param p2 when 0: loans LOAN_INTERVAL
34 * when 1: loans the maximum loan permitting money (press CTRL),
35 * when 2: loans the amount specified in p1
36 * @param text unused
37 * @return the cost of this operation or an error
39 CommandCost CmdIncreaseLoan(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
41 Company *c = Company::Get(_current_company);
43 if (c->current_loan >= _economy.max_loan) {
44 SetDParam(0, _economy.max_loan);
45 return_cmd_error(STR_ERROR_MAXIMUM_PERMITTED_LOAN);
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;
63 /* Overflow protection */
64 if (c->money + c->current_loan + loan < c->money) return CMD_ERROR;
66 if (flags & DC_EXEC) {
67 c->money += loan;
68 c->current_loan += loan;
69 InvalidateCompanyWindows(c);
72 return CommandCost(EXPENSES_OTHER);
75 /**
76 * Decrease the loan of your company.
77 * @param tile unused
78 * @param flags operation to perform
79 * @param p1 amount to decrease the loan with, multitude of LOAN_INTERVAL. Only used when p2 == 2.
80 * @param p2 when 0: pays back LOAN_INTERVAL
81 * when 1: pays back the maximum loan permitting money (press CTRL),
82 * when 2: pays back the amount specified in p1
83 * @param text unused
84 * @return the cost of this operation or an error
86 CommandCost CmdDecreaseLoan(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
88 Company *c = Company::Get(_current_company);
90 if (c->current_loan == 0) return_cmd_error(STR_ERROR_LOAN_ALREADY_REPAYED);
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;
108 if (c->money < loan) {
109 SetDParam(0, loan);
110 return_cmd_error(STR_ERROR_CURRENCY_REQUIRED);
113 if (flags & DC_EXEC) {
114 c->money -= loan;
115 c->current_loan -= loan;
116 InvalidateCompanyWindows(c);
118 return CommandCost();
122 * In case of an unsafe unpause, we want the
123 * user to confirm that it might crash.
124 * @param w unused
125 * @param confirmed whether the user confirms his/her action
127 static void AskUnsafeUnpauseCallback(Window *w, bool confirmed)
129 if (confirmed) {
130 DoCommandP(0, PM_PAUSED_ERROR, 0, CMD_PAUSE);
135 * Pause/Unpause the game (server-only).
136 * Set or unset a bit in the pause mode. If pause mode is zero the game is
137 * unpaused. A bitset is used instead of a boolean value/counter to have
138 * more control over the game when saving/loading, etc.
139 * @param tile unused
140 * @param flags operation to perform
141 * @param p1 the pause mode to change
142 * @param p2 1 pauses, 0 unpauses this mode
143 * @param text unused
144 * @return the cost of this operation or an error
146 CommandCost CmdPause(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
148 switch (p1) {
149 case PM_PAUSED_SAVELOAD:
150 case PM_PAUSED_ERROR:
151 case PM_PAUSED_NORMAL:
152 case PM_PAUSED_GAME_SCRIPT:
153 break;
155 #ifdef ENABLE_NETWORK
156 case PM_PAUSED_JOIN:
157 case PM_PAUSED_ACTIVE_CLIENTS:
158 if (!_networking) return CMD_ERROR;
159 break;
160 #endif /* ENABLE_NETWORK */
162 default: return CMD_ERROR;
164 if (flags & DC_EXEC) {
165 if (p1 == PM_PAUSED_NORMAL && _pause_mode & PM_PAUSED_ERROR) {
166 ShowQuery(
167 STR_NEWGRF_UNPAUSE_WARNING_TITLE,
168 STR_NEWGRF_UNPAUSE_WARNING,
169 NULL,
170 AskUnsafeUnpauseCallback
172 } else {
173 #ifdef ENABLE_NETWORK
174 PauseMode prev_mode = _pause_mode;
175 #endif /* ENABLE_NETWORK */
177 if (p2 == 0) {
178 _pause_mode = _pause_mode & ~p1;
179 } else {
180 _pause_mode = _pause_mode | p1;
183 #ifdef ENABLE_NETWORK
184 NetworkHandlePauseChange(prev_mode, (PauseMode)p1);
185 #endif /* ENABLE_NETWORK */
188 SetWindowDirty(WC_STATUS_BAR, 0);
189 SetWindowDirty(WC_MAIN_TOOLBAR, 0);
191 return CommandCost();
195 * Change the financial flow of your company.
196 * @param tile unused
197 * @param flags operation to perform
198 * @param p1 the amount of money to receive (if positive), or spend (if negative)
199 * @param p2 unused
200 * @param text unused
201 * @return the cost of this operation or an error
203 CommandCost CmdMoneyCheat(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
205 return CommandCost(EXPENSES_OTHER, -(int32)p1);
209 * Change the bank bank balance of a company by inserting or removing money without affecting the loan.
210 * @param tile unused
211 * @param flags operation to perform
212 * @param p1 the amount of money to receive (if positive), or spend (if negative)
213 * @param p2 (bit 0-7) - the company ID.
214 * (bit 8-15) - the expenses type which should register the cost/income @see ExpensesType.
215 * @param text unused
216 * @return zero cost or an error
218 CommandCost CmdChangeBankBalance(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
220 int32 delta = (int32)p1;
221 CompanyID company = (CompanyID) GB(p2, 0, 8);
222 ExpensesType expenses_type = Extract<ExpensesType, 8, 8>(p2);
224 if (!Company::IsValidID(company)) return CMD_ERROR;
225 if (expenses_type >= EXPENSES_END) return CMD_ERROR;
226 if (_current_company != OWNER_DEITY) return CMD_ERROR;
228 if (flags & DC_EXEC) {
229 /* Change company bank balance of company. */
230 Backup<CompanyByte> cur_company(_current_company, company, FILE_LINE);
231 SubtractMoneyFromCompany(CommandCost(expenses_type, -delta));
232 cur_company.Restore();
235 /* This command doesn't cost anyting for deity. */
236 CommandCost zero_cost(expenses_type, 0);
237 return zero_cost;
241 * Transfer funds (money) from one company to another.
242 * To prevent abuse in multiplayer games you can only send money to other
243 * companies if you have paid off your loan (either explicitly, or implicitly
244 * given the fact that you have more money than loan).
245 * @param tile unused
246 * @param flags operation to perform
247 * @param p1 the amount of money to transfer; max 20.000.000
248 * @param p2 the company to transfer the money to
249 * @param text unused
250 * @return the cost of this operation or an error
252 CommandCost CmdGiveMoney(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
254 if (!_settings_game.economy.give_money) return CMD_ERROR;
256 const Company *c = Company::Get(_current_company);
257 CommandCost amount(EXPENSES_OTHER, min((Money)p1, (Money)20000000LL));
258 CompanyID dest_company = (CompanyID)p2;
260 /* You can only transfer funds that is in excess of your loan */
261 if (c->money - c->current_loan < amount.GetCost() || amount.GetCost() < 0) return CMD_ERROR;
262 if (!_networking || !Company::IsValidID(dest_company)) return CMD_ERROR;
264 if (flags & DC_EXEC) {
265 /* Add money to company */
266 Backup<CompanyByte> cur_company(_current_company, dest_company, FILE_LINE);
267 SubtractMoneyFromCompany(CommandCost(EXPENSES_OTHER, -amount.GetCost()));
268 cur_company.Restore();
271 /* Subtract money from local-company */
272 return amount;