(svn r25652) -Fix: Improve text caret movement for complex scripts.
[openttd/fttd.git] / src / misc_cmd.cpp
blob9c01eef5d78d24c6a6706b4c0617d8e2bbc9abb6
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 "window_func.h"
16 #include "textbuf_gui.h"
17 #include "network/network.h"
18 #include "network/network_func.h"
19 #include "strings_func.h"
20 #include "company_func.h"
21 #include "company_gui.h"
22 #include "company_base.h"
23 #include "core/backup_type.hpp"
25 #include "table/strings.h"
27 /**
28 * Increase the loan of your company.
29 * @param tile unused
30 * @param flags operation to perform
31 * @param p1 amount to increase the loan with, multitude of LOAN_INTERVAL. Only used when p2 == 2.
32 * @param p2 when 0: loans LOAN_INTERVAL
33 * when 1: loans the maximum loan permitting money (press CTRL),
34 * when 2: loans the amount specified in p1
35 * @param text unused
36 * @return the cost of this operation or an error
38 CommandCost CmdIncreaseLoan(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
40 Company *c = Company::Get(_current_company);
42 if (c->current_loan >= _economy.max_loan) {
43 SetDParam(0, _economy.max_loan);
44 return_cmd_error(STR_ERROR_MAXIMUM_PERMITTED_LOAN);
47 Money loan;
48 switch (p2) {
49 default: return CMD_ERROR; // Invalid method
50 case 0: // Take some extra loan
51 loan = LOAN_INTERVAL;
52 break;
53 case 1: // Take a loan as big as possible
54 loan = _economy.max_loan - c->current_loan;
55 break;
56 case 2: // Take the given amount of loan
57 if ((int32)p1 < LOAN_INTERVAL || c->current_loan + (int32)p1 > _economy.max_loan || p1 % LOAN_INTERVAL != 0) return CMD_ERROR;
58 loan = p1;
59 break;
62 /* Overflow protection */
63 if (c->money + c->current_loan + loan < c->money) return CMD_ERROR;
65 if (flags & DC_EXEC) {
66 c->money += loan;
67 c->current_loan += loan;
68 InvalidateCompanyWindows(c);
71 return CommandCost(EXPENSES_OTHER);
74 /**
75 * Decrease the loan of your company.
76 * @param tile unused
77 * @param flags operation to perform
78 * @param p1 amount to decrease the loan with, multitude of LOAN_INTERVAL. Only used when p2 == 2.
79 * @param p2 when 0: pays back LOAN_INTERVAL
80 * when 1: pays back the maximum loan permitting money (press CTRL),
81 * when 2: pays back the amount specified in p1
82 * @param text unused
83 * @return the cost of this operation or an error
85 CommandCost CmdDecreaseLoan(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
87 Company *c = Company::Get(_current_company);
89 if (c->current_loan == 0) return_cmd_error(STR_ERROR_LOAN_ALREADY_REPAYED);
91 Money loan;
92 switch (p2) {
93 default: return CMD_ERROR; // Invalid method
94 case 0: // Pay back one step
95 loan = min(c->current_loan, (Money)LOAN_INTERVAL);
96 break;
97 case 1: // Pay back as much as possible
98 loan = max(min(c->current_loan, c->money), (Money)LOAN_INTERVAL);
99 loan -= loan % LOAN_INTERVAL;
100 break;
101 case 2: // Repay the given amount of loan
102 if (p1 % LOAN_INTERVAL != 0 || (int32)p1 < LOAN_INTERVAL || p1 > c->current_loan) return CMD_ERROR; // Invalid amount to loan
103 loan = p1;
104 break;
107 if (c->money < loan) {
108 SetDParam(0, loan);
109 return_cmd_error(STR_ERROR_CURRENCY_REQUIRED);
112 if (flags & DC_EXEC) {
113 c->money -= loan;
114 c->current_loan -= loan;
115 InvalidateCompanyWindows(c);
117 return CommandCost();
121 * In case of an unsafe unpause, we want the
122 * user to confirm that it might crash.
123 * @param w unused
124 * @param confirmed whether the user confirms his/her action
126 static void AskUnsafeUnpauseCallback(Window *w, bool confirmed)
128 if (confirmed) {
129 DoCommandP(0, PM_PAUSED_ERROR, 0, CMD_PAUSE);
134 * Pause/Unpause the game (server-only).
135 * Set or unset a bit in the pause mode. If pause mode is zero the game is
136 * unpaused. A bitset is used instead of a boolean value/counter to have
137 * more control over the game when saving/loading, etc.
138 * @param tile unused
139 * @param flags operation to perform
140 * @param p1 the pause mode to change
141 * @param p2 1 pauses, 0 unpauses this mode
142 * @param text unused
143 * @return the cost of this operation or an error
145 CommandCost CmdPause(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
147 switch (p1) {
148 case PM_PAUSED_SAVELOAD:
149 case PM_PAUSED_ERROR:
150 case PM_PAUSED_NORMAL:
151 case PM_PAUSED_GAME_SCRIPT:
152 break;
154 #ifdef ENABLE_NETWORK
155 case PM_PAUSED_JOIN:
156 case PM_PAUSED_ACTIVE_CLIENTS:
157 if (!_networking) return CMD_ERROR;
158 break;
159 #endif /* ENABLE_NETWORK */
161 default: return CMD_ERROR;
163 if (flags & DC_EXEC) {
164 if (p1 == PM_PAUSED_NORMAL && _pause_mode & PM_PAUSED_ERROR) {
165 ShowQuery(
166 STR_NEWGRF_UNPAUSE_WARNING_TITLE,
167 STR_NEWGRF_UNPAUSE_WARNING,
168 NULL,
169 AskUnsafeUnpauseCallback
171 } else {
172 #ifdef ENABLE_NETWORK
173 PauseMode prev_mode = _pause_mode;
174 #endif /* ENABLE_NETWORK */
176 if (p2 == 0) {
177 _pause_mode = _pause_mode & ~p1;
178 } else {
179 _pause_mode = _pause_mode | p1;
182 #ifdef ENABLE_NETWORK
183 NetworkHandlePauseChange(prev_mode, (PauseMode)p1);
184 #endif /* ENABLE_NETWORK */
187 SetWindowDirty(WC_STATUS_BAR, 0);
188 SetWindowDirty(WC_MAIN_TOOLBAR, 0);
190 return CommandCost();
194 * Change the financial flow of your company.
195 * @param tile unused
196 * @param flags operation to perform
197 * @param p1 the amount of money to receive (if negative), or spend (if positive)
198 * @param p2 unused
199 * @param text unused
200 * @return the cost of this operation or an error
202 CommandCost CmdMoneyCheat(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
204 return CommandCost(EXPENSES_OTHER, -(int32)p1);
208 * Transfer funds (money) from one company to another.
209 * To prevent abuse in multiplayer games you can only send money to other
210 * companies if you have paid off your loan (either explicitly, or implicitly
211 * given the fact that you have more money than loan).
212 * @param tile unused
213 * @param flags operation to perform
214 * @param p1 the amount of money to transfer; max 20.000.000
215 * @param p2 the company to transfer the money to
216 * @param text unused
217 * @return the cost of this operation or an error
219 CommandCost CmdGiveMoney(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
221 if (!_settings_game.economy.give_money) return CMD_ERROR;
223 const Company *c = Company::Get(_current_company);
224 CommandCost amount(EXPENSES_OTHER, min((Money)p1, (Money)20000000LL));
225 CompanyID dest_company = (CompanyID)p2;
227 /* You can only transfer funds that is in excess of your loan */
228 if (c->money - c->current_loan < amount.GetCost() || amount.GetCost() < 0) return CMD_ERROR;
229 if (!_networking || !Company::IsValidID(dest_company)) return CMD_ERROR;
231 if (flags & DC_EXEC) {
232 /* Add money to company */
233 Backup<CompanyByte> cur_company(_current_company, dest_company, FILE_LINE);
234 SubtractMoneyFromCompany(CommandCost(EXPENSES_OTHER, -amount.GetCost()));
235 cur_company.Restore();
238 /* Subtract money from local-company */
239 return amount;