Codechange: Use cached town, station, industry names for list window sorting
[openttd-github.git] / src / cheat_gui.cpp
blob7290414c004092b523dc329d2682e6d73640c07b
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 */
8 /** @file cheat_gui.cpp GUI related to cheating. */
10 #include "stdafx.h"
11 #include "command_func.h"
12 #include "cheat_type.h"
13 #include "company_base.h"
14 #include "company_func.h"
15 #include "date_func.h"
16 #include "saveload/saveload.h"
17 #include "textbuf_gui.h"
18 #include "window_gui.h"
19 #include "string_func.h"
20 #include "strings_func.h"
21 #include "window_func.h"
22 #include "rail_gui.h"
23 #include "settings_gui.h"
24 #include "company_gui.h"
25 #include "linkgraph/linkgraphschedule.h"
26 #include "map_func.h"
27 #include "tile_map.h"
28 #include "newgrf.h"
29 #include "error.h"
31 #include "widgets/cheat_widget.h"
33 #include "table/sprites.h"
35 #include "safeguards.h"
38 /**
39 * The 'amount' to cheat with.
40 * This variable is semantically a constant value, but because the cheat
41 * code requires to be able to write to the variable it is not constified.
43 static int32 _money_cheat_amount = 10000000;
45 /**
46 * Handle cheating of money.
47 * Note that the amount of money of a company must be changed through a command
48 * rather than by setting a variable. Since the cheat data structure expects a
49 * variable, the amount of given/taken money is used for this purpose.
50 * @param p1 not used.
51 * @param p2 is -1 or +1 (down/up)
52 * @return Amount of money cheat.
54 static int32 ClickMoneyCheat(int32 p1, int32 p2)
56 DoCommandP(0, (uint32)(p2 * _money_cheat_amount), 0, CMD_MONEY_CHEAT);
57 return _money_cheat_amount;
60 /**
61 * Handle changing of company.
62 * @param p1 company to set to
63 * @param p2 is -1 or +1 (down/up)
64 * @return The new company.
66 static int32 ClickChangeCompanyCheat(int32 p1, int32 p2)
68 while ((uint)p1 < Company::GetPoolSize()) {
69 if (Company::IsValidID((CompanyID)p1)) {
70 SetLocalCompany((CompanyID)p1);
71 return _local_company;
73 p1 += p2;
76 return _local_company;
79 /**
80 * Allow (or disallow) changing production of all industries.
81 * @param p1 new value
82 * @param p2 unused
83 * @return New value allowing change of industry production.
85 static int32 ClickSetProdCheat(int32 p1, int32 p2)
87 _cheats.setup_prod.value = (p1 != 0);
88 InvalidateWindowClassesData(WC_INDUSTRY_VIEW);
89 return _cheats.setup_prod.value;
92 extern void EnginesMonthlyLoop();
94 /**
95 * Handle changing of the current year.
96 * @param p1 Unused.
97 * @param p2 +1 (increase) or -1 (decrease).
98 * @return New year.
100 static int32 ClickChangeDateCheat(int32 p1, int32 p2)
102 YearMonthDay ymd;
103 ConvertDateToYMD(_date, &ymd);
105 p1 = Clamp(p1, MIN_YEAR, MAX_YEAR);
106 if (p1 == _cur_year) return _cur_year;
108 Date new_date = ConvertYMDToDate(p1, ymd.month, ymd.day);
109 LinkGraphSchedule::instance.ShiftDates(new_date - _date);
110 SetDate(new_date, _date_fract);
111 EnginesMonthlyLoop();
112 SetWindowDirty(WC_STATUS_BAR, 0);
113 InvalidateWindowClassesData(WC_BUILD_STATION, 0);
114 InvalidateWindowClassesData(WC_BUILD_OBJECT, 0);
115 ResetSignalVariant();
116 return _cur_year;
120 * Allow (or disallow) a change of the maximum allowed heightlevel.
121 * @param p1 new value
122 * @param p2 unused
123 * @return New value (or unchanged old value) of the maximum
124 * allowed heightlevel value.
126 static int32 ClickChangeMaxHlCheat(int32 p1, int32 p2)
128 p1 = Clamp(p1, MIN_MAX_HEIGHTLEVEL, MAX_MAX_HEIGHTLEVEL);
130 /* Check if at least one mountain on the map is higher than the new value.
131 * If yes, disallow the change. */
132 for (TileIndex t = 0; t < MapSize(); t++) {
133 if ((int32)TileHeight(t) > p1) {
134 ShowErrorMessage(STR_CONFIG_SETTING_TOO_HIGH_MOUNTAIN, INVALID_STRING_ID, WL_ERROR);
135 /* Return old, unchanged value */
136 return _settings_game.construction.max_heightlevel;
140 /* Execute the change and reload GRF Data */
141 _settings_game.construction.max_heightlevel = p1;
142 ReloadNewGRFData();
144 /* The smallmap uses an index from heightlevels to colours. Trigger rebuilding it. */
145 InvalidateWindowClassesData(WC_SMALLMAP, 2);
147 return _settings_game.construction.max_heightlevel;
150 /** Available cheats. */
151 enum CheatNumbers {
152 CHT_MONEY, ///< Change amount of money.
153 CHT_CHANGE_COMPANY, ///< Switch company.
154 CHT_EXTRA_DYNAMITE, ///< Dynamite anything.
155 CHT_CROSSINGTUNNELS, ///< Allow tunnels to cross each other.
156 CHT_NO_JETCRASH, ///< Disable jet-airplane crashes.
157 CHT_SETUP_PROD, ///< Allow manually editing of industry production.
158 CHT_EDIT_MAX_HL, ///< Edit maximum allowed heightlevel
159 CHT_CHANGE_DATE, ///< Do time traveling.
161 CHT_NUM_CHEATS, ///< Number of cheats.
165 * Signature of handler function when user clicks at a cheat.
166 * @param p1 The new value.
167 * @param p2 Change direction (+1, +1), \c 0 for boolean settings.
169 typedef int32 CheckButtonClick(int32 p1, int32 p2);
171 /** Information of a cheat. */
172 struct CheatEntry {
173 VarType type; ///< type of selector
174 StringID str; ///< string with descriptive text
175 void *variable; ///< pointer to the variable
176 bool *been_used; ///< has this cheat been used before?
177 CheckButtonClick *proc;///< procedure
181 * The available cheats.
182 * Order matches with the values of #CheatNumbers
184 static const CheatEntry _cheats_ui[] = {
185 {SLE_INT32, STR_CHEAT_MONEY, &_money_cheat_amount, &_cheats.money.been_used, &ClickMoneyCheat },
186 {SLE_UINT8, STR_CHEAT_CHANGE_COMPANY, &_local_company, &_cheats.switch_company.been_used, &ClickChangeCompanyCheat },
187 {SLE_BOOL, STR_CHEAT_EXTRA_DYNAMITE, &_cheats.magic_bulldozer.value, &_cheats.magic_bulldozer.been_used, nullptr },
188 {SLE_BOOL, STR_CHEAT_CROSSINGTUNNELS, &_cheats.crossing_tunnels.value, &_cheats.crossing_tunnels.been_used, nullptr },
189 {SLE_BOOL, STR_CHEAT_NO_JETCRASH, &_cheats.no_jetcrash.value, &_cheats.no_jetcrash.been_used, nullptr },
190 {SLE_BOOL, STR_CHEAT_SETUP_PROD, &_cheats.setup_prod.value, &_cheats.setup_prod.been_used, &ClickSetProdCheat },
191 {SLE_UINT8, STR_CHEAT_EDIT_MAX_HL, &_settings_game.construction.max_heightlevel, &_cheats.edit_max_hl.been_used, &ClickChangeMaxHlCheat },
192 {SLE_INT32, STR_CHEAT_CHANGE_DATE, &_cur_year, &_cheats.change_date.been_used, &ClickChangeDateCheat },
195 assert_compile(CHT_NUM_CHEATS == lengthof(_cheats_ui));
197 /** Widget definitions of the cheat GUI. */
198 static const NWidgetPart _nested_cheat_widgets[] = {
199 NWidget(NWID_HORIZONTAL),
200 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
201 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_CHEATS, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
202 NWidget(WWT_SHADEBOX, COLOUR_GREY),
203 NWidget(WWT_STICKYBOX, COLOUR_GREY),
204 EndContainer(),
205 NWidget(WWT_PANEL, COLOUR_GREY, WID_C_PANEL), SetDataTip(0x0, STR_CHEATS_TOOLTIP), EndContainer(),
208 /** GUI for the cheats. */
209 struct CheatWindow : Window {
210 int clicked;
211 int header_height;
212 int clicked_widget;
213 uint line_height;
214 int box_width;
216 CheatWindow(WindowDesc *desc) : Window(desc)
218 this->box_width = GetSpriteSize(SPR_BOX_EMPTY).width;
219 this->InitNested();
222 void DrawWidget(const Rect &r, int widget) const override
224 if (widget != WID_C_PANEL) return;
226 int y = r.top + WD_FRAMERECT_TOP + this->header_height;
227 DrawStringMultiLine(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, r.top + WD_FRAMERECT_TOP, y, STR_CHEATS_WARNING, TC_FROMSTRING, SA_CENTER);
229 bool rtl = _current_text_dir == TD_RTL;
230 uint box_left = rtl ? r.right - this->box_width - 5 : r.left + 5;
231 uint button_left = rtl ? r.right - this->box_width - 10 - SETTING_BUTTON_WIDTH : r.left + this->box_width + 10;
232 uint text_left = r.left + (rtl ? WD_FRAMERECT_LEFT : 20 + this->box_width + SETTING_BUTTON_WIDTH);
233 uint text_right = r.right - (rtl ? 20 + this->box_width + SETTING_BUTTON_WIDTH : WD_FRAMERECT_RIGHT);
235 int text_y_offset = (this->line_height - FONT_HEIGHT_NORMAL) / 2;
236 int icon_y_offset = (this->line_height - SETTING_BUTTON_HEIGHT) / 2;
238 for (int i = 0; i != lengthof(_cheats_ui); i++) {
239 const CheatEntry *ce = &_cheats_ui[i];
241 DrawSprite((*ce->been_used) ? SPR_BOX_CHECKED : SPR_BOX_EMPTY, PAL_NONE, box_left, y + icon_y_offset + 2);
243 switch (ce->type) {
244 case SLE_BOOL: {
245 bool on = (*(bool*)ce->variable);
247 DrawBoolButton(button_left, y + icon_y_offset, on, true);
248 SetDParam(0, on ? STR_CONFIG_SETTING_ON : STR_CONFIG_SETTING_OFF);
249 break;
252 default: {
253 int32 val = (int32)ReadValue(ce->variable, ce->type);
254 char buf[512];
256 /* Draw [<][>] boxes for settings of an integer-type */
257 DrawArrowButtons(button_left, y + icon_y_offset, COLOUR_YELLOW, clicked - (i * 2), true, true);
259 switch (ce->str) {
260 /* Display date for change date cheat */
261 case STR_CHEAT_CHANGE_DATE: SetDParam(0, _date); break;
263 /* Draw coloured flag for change company cheat */
264 case STR_CHEAT_CHANGE_COMPANY: {
265 SetDParam(0, val + 1);
266 GetString(buf, STR_CHEAT_CHANGE_COMPANY, lastof(buf));
267 uint offset = 10 + GetStringBoundingBox(buf).width;
268 DrawCompanyIcon(_local_company, rtl ? text_right - offset - 10 : text_left + offset, y + icon_y_offset + 2);
269 break;
272 default: SetDParam(0, val);
274 break;
278 DrawString(text_left, text_right, y + text_y_offset, ce->str);
280 y += this->line_height;
284 void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
286 if (widget != WID_C_PANEL) return;
288 uint width = 0;
289 for (int i = 0; i != lengthof(_cheats_ui); i++) {
290 const CheatEntry *ce = &_cheats_ui[i];
291 switch (ce->type) {
292 case SLE_BOOL:
293 SetDParam(0, STR_CONFIG_SETTING_ON);
294 width = max(width, GetStringBoundingBox(ce->str).width);
295 SetDParam(0, STR_CONFIG_SETTING_OFF);
296 width = max(width, GetStringBoundingBox(ce->str).width);
297 break;
299 default:
300 switch (ce->str) {
301 /* Display date for change date cheat */
302 case STR_CHEAT_CHANGE_DATE:
303 SetDParam(0, ConvertYMDToDate(MAX_YEAR, 11, 31));
304 width = max(width, GetStringBoundingBox(ce->str).width);
305 break;
307 /* Draw coloured flag for change company cheat */
308 case STR_CHEAT_CHANGE_COMPANY:
309 SetDParamMaxValue(0, MAX_COMPANIES);
310 width = max(width, GetStringBoundingBox(ce->str).width + 10 + 10);
311 break;
313 default:
314 SetDParam(0, INT64_MAX);
315 width = max(width, GetStringBoundingBox(ce->str).width);
316 break;
318 break;
322 this->line_height = max(GetSpriteSize(SPR_BOX_CHECKED).height, GetSpriteSize(SPR_BOX_EMPTY).height);
323 this->line_height = max<uint>(this->line_height, SETTING_BUTTON_HEIGHT);
324 this->line_height = max<uint>(this->line_height, FONT_HEIGHT_NORMAL) + WD_PAR_VSEP_NORMAL;
326 size->width = width + 20 + this->box_width + SETTING_BUTTON_WIDTH /* stuff on the left */ + 10 /* extra spacing on right */;
327 this->header_height = GetStringHeight(STR_CHEATS_WARNING, size->width - WD_FRAMERECT_LEFT - WD_FRAMERECT_RIGHT) + WD_PAR_VSEP_WIDE;
328 size->height = this->header_height + WD_FRAMERECT_TOP + WD_PAR_VSEP_NORMAL + WD_FRAMERECT_BOTTOM + this->line_height * lengthof(_cheats_ui);
331 void OnClick(Point pt, int widget, int click_count) override
333 const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_C_PANEL);
334 uint btn = (pt.y - wid->pos_y - WD_FRAMERECT_TOP - this->header_height) / this->line_height;
335 int x = pt.x - wid->pos_x;
336 bool rtl = _current_text_dir == TD_RTL;
337 if (rtl) x = wid->current_x - x;
339 if (btn >= lengthof(_cheats_ui)) return;
341 const CheatEntry *ce = &_cheats_ui[btn];
342 int value = (int32)ReadValue(ce->variable, ce->type);
343 int oldvalue = value;
345 if (btn == CHT_CHANGE_DATE && x >= 20 + this->box_width + SETTING_BUTTON_WIDTH) {
346 /* Click at the date text directly. */
347 clicked_widget = CHT_CHANGE_DATE;
348 SetDParam(0, value);
349 ShowQueryString(STR_JUST_INT, STR_CHEAT_CHANGE_DATE_QUERY_CAPT, 8, this, CS_NUMERAL, QSF_ACCEPT_UNCHANGED);
350 return;
351 } else if (btn == CHT_EDIT_MAX_HL && x >= 20 + this->box_width + SETTING_BUTTON_WIDTH) {
352 clicked_widget = CHT_EDIT_MAX_HL;
353 SetDParam(0, value);
354 ShowQueryString(STR_JUST_INT, STR_CHEAT_EDIT_MAX_HL_QUERY_CAPT, 8, this, CS_NUMERAL, QSF_ACCEPT_UNCHANGED);
355 return;
358 /* Not clicking a button? */
359 if (!IsInsideMM(x, 10 + this->box_width, 10 + this->box_width + SETTING_BUTTON_WIDTH)) return;
361 *ce->been_used = true;
363 switch (ce->type) {
364 case SLE_BOOL:
365 value ^= 1;
366 if (ce->proc != nullptr) ce->proc(value, 0);
367 break;
369 default:
370 /* Take whatever the function returns */
371 value = ce->proc(value + ((x >= 10 + this->box_width + SETTING_BUTTON_WIDTH / 2) ? 1 : -1), (x >= 10 + this->box_width + SETTING_BUTTON_WIDTH / 2) ? 1 : -1);
373 /* The first cheat (money), doesn't return a different value. */
374 if (value != oldvalue || btn == CHT_MONEY) this->clicked = btn * 2 + 1 + ((x >= 10 + this->box_width + SETTING_BUTTON_WIDTH / 2) != rtl ? 1 : 0);
375 break;
378 if (value != oldvalue) WriteValue(ce->variable, ce->type, (int64)value);
380 this->SetTimeout();
382 this->SetDirty();
385 void OnTimeout() override
387 this->clicked = 0;
388 this->SetDirty();
391 void OnQueryTextFinished(char *str) override
393 /* Was 'cancel' pressed or nothing entered? */
394 if (str == nullptr || StrEmpty(str)) return;
396 const CheatEntry *ce = &_cheats_ui[clicked_widget];
397 int oldvalue = (int32)ReadValue(ce->variable, ce->type);
398 int value = atoi(str);
399 *ce->been_used = true;
400 value = ce->proc(value, value - oldvalue);
402 if (value != oldvalue) WriteValue(ce->variable, ce->type, (int64)value);
403 this->SetDirty();
407 /** Window description of the cheats GUI. */
408 static WindowDesc _cheats_desc(
409 WDP_AUTO, "cheats", 0, 0,
410 WC_CHEATS, WC_NONE,
412 _nested_cheat_widgets, lengthof(_nested_cheat_widgets)
415 /** Open cheat window. */
416 void ShowCheatWindow()
418 DeleteWindowById(WC_CHEATS, 0);
419 new CheatWindow(&_cheats_desc);