Files @ r28549:6bc33bc6ee38
Branch filter:

Location: cpp/openttd-patchpack/source/src/cheat_gui.cpp - annotation

Tyler Trahan
Codechange: Split dates and timers into Economy and Calendar time (#10700)
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r9111:983de9c5a848
r8965:16f6e5e9ed36
r8965:16f6e5e9ed36
r8965:16f6e5e9ed36
r10960:e97ebf9cf99b
r10208:ef8fcc3dc4ca
r10208:ef8fcc3dc4ca
r10571:99cb9a95b4cf
r26074:f767da68bcf9
r17142:9f8921b5eb4f
r8965:16f6e5e9ed36
r16647:5a3abb315d22
r8965:16f6e5e9ed36
r8965:16f6e5e9ed36
r8965:16f6e5e9ed36
r19392:c9b4216afa41
r10208:ef8fcc3dc4ca
r20823:b0feed2e317c
r21744:347302fb9fba
r21744:347302fb9fba
r21744:347302fb9fba
r21744:347302fb9fba
r26103:316b73a1be08
r26549:82dacf005b0a
r27090:1ea30951c0f3
r27090:1ea30951c0f3
r28549:6bc33bc6ee38
r8965:16f6e5e9ed36
r18670:f122c356353c
r18670:f122c356353c
r8965:16f6e5e9ed36
r8965:16f6e5e9ed36
r21383:942c32fb8b0e
r21383:942c32fb8b0e
r8965:16f6e5e9ed36
r8965:16f6e5e9ed36
r8965:16f6e5e9ed36
r8965:16f6e5e9ed36
r8965:16f6e5e9ed36
r8965:16f6e5e9ed36
r27737:728d55b97775
r8965:16f6e5e9ed36
r16644:32454b2fc699
r16644:32454b2fc699
r16644:32454b2fc699
r16644:32454b2fc699
r16644:32454b2fc699
r27092:f41d5ef4bf33
r16644:32454b2fc699
r16644:32454b2fc699
r27942:f7389062d120
r8965:16f6e5e9ed36
r27092:f41d5ef4bf33
r8965:16f6e5e9ed36
r8965:16f6e5e9ed36
r8965:16f6e5e9ed36
r8965:16f6e5e9ed36
r16644:32454b2fc699
r27092:f41d5ef4bf33
r27092:f41d5ef4bf33
r16644:32454b2fc699
r8965:16f6e5e9ed36
r27737:728d55b97775
r8965:16f6e5e9ed36
r27092:f41d5ef4bf33
r27092:f41d5ef4bf33
r27092:f41d5ef4bf33
r10207:a1fc2f2a33db
r8965:16f6e5e9ed36
r27092:f41d5ef4bf33
r8965:16f6e5e9ed36
r8965:16f6e5e9ed36
r10207:a1fc2f2a33db
r8965:16f6e5e9ed36
r8965:16f6e5e9ed36
r8965:16f6e5e9ed36
r16644:32454b2fc699
r27092:f41d5ef4bf33
r19944:25a78576fb5e
r11211:1a3a0822fe6b
r27942:f7389062d120
r11211:1a3a0822fe6b
r27092:f41d5ef4bf33
r16187:927b311c86ca
r16187:927b311c86ca
r11211:1a3a0822fe6b
r11211:1a3a0822fe6b
r28549:6bc33bc6ee38
r8965:16f6e5e9ed36
r8965:16f6e5e9ed36
r16644:32454b2fc699
r27138:ceb747b72a42
r16644:32454b2fc699
r8965:16f6e5e9ed36
r27942:f7389062d120
r8965:16f6e5e9ed36
r27138:ceb747b72a42
r27884:803962be0328
r28112:bca7556a6bb6
r8965:16f6e5e9ed36
r28343:242bc14cc675
r28549:6bc33bc6ee38
r28549:6bc33bc6ee38
r28549:6bc33bc6ee38
r27138:ceb747b72a42
r27150:49e59ca27045
r28549:6bc33bc6ee38
r28549:6bc33bc6ee38
r27138:ceb747b72a42
r27150:49e59ca27045
r28549:6bc33bc6ee38
r28549:6bc33bc6ee38
r27150:49e59ca27045
r28549:6bc33bc6ee38
r13024:48c81d0b078a
r12322:e6ca8896bfec
r26914:6e889cbdcba1
r26914:6e889cbdcba1
r17429:57e6f2cdc58c
r8965:16f6e5e9ed36
r28112:bca7556a6bb6
r8965:16f6e5e9ed36
r8965:16f6e5e9ed36
r21744:347302fb9fba
r21744:347302fb9fba
r27092:f41d5ef4bf33
r21744:347302fb9fba
r21744:347302fb9fba
r21744:347302fb9fba
r27942:f7389062d120
r21995:f3d5bc187e13
r27092:f41d5ef4bf33
r21744:347302fb9fba
r21744:347302fb9fba
r21744:347302fb9fba
r26787:a51c38e4aac5
r27737:728d55b97775
r21744:347302fb9fba
r21744:347302fb9fba
r25079:09d645764ffe
r21744:347302fb9fba
r21744:347302fb9fba
r21744:347302fb9fba
r21744:347302fb9fba
r27092:f41d5ef4bf33
r21744:347302fb9fba
r21783:009b9d2827cc
r21783:009b9d2827cc
r21783:009b9d2827cc
r21783:009b9d2827cc
r25079:09d645764ffe
r21744:347302fb9fba
r21744:347302fb9fba
r16646:2705ca01bd3b
r16646:2705ca01bd3b
r16646:2705ca01bd3b
r16646:2705ca01bd3b
r16646:2705ca01bd3b
r16646:2705ca01bd3b
r16646:2705ca01bd3b
r16646:2705ca01bd3b
r28028:cce01150c0dc
r21744:347302fb9fba
r16646:2705ca01bd3b
r16646:2705ca01bd3b
r16646:2705ca01bd3b
r16646:2705ca01bd3b
r16646:2705ca01bd3b
r16644:32454b2fc699
r16644:32454b2fc699
r27092:f41d5ef4bf33
r27092:f41d5ef4bf33
r16644:32454b2fc699
r27737:728d55b97775
r8965:16f6e5e9ed36
r16644:32454b2fc699
r8965:16f6e5e9ed36
r8965:16f6e5e9ed36
r8965:16f6e5e9ed36
r8965:16f6e5e9ed36
r8965:16f6e5e9ed36
r8965:16f6e5e9ed36
r8965:16f6e5e9ed36
r8965:16f6e5e9ed36
r16644:32454b2fc699
r16644:32454b2fc699
r16646:2705ca01bd3b
r16644:32454b2fc699
r8965:16f6e5e9ed36
r25079:09d645764ffe
r25079:09d645764ffe
r25079:09d645764ffe
r25079:09d645764ffe
r25079:09d645764ffe
r25079:09d645764ffe
r28028:cce01150c0dc
r25079:09d645764ffe
r27148:4e041ae27b9d
r8965:16f6e5e9ed36
r8965:16f6e5e9ed36
r24521:57ec498b9221
r16646:2705ca01bd3b
r16644:32454b2fc699
r28487:2130fff7270c
r11457:9a04ad49bedb
r13742:180ad925befc
r13742:180ad925befc
r14035:4f06488d5a62
r14035:4f06488d5a62
r11457:9a04ad49bedb
r18673:e5a4490b411b
r24624:ead6f9a66573
r26575:d5725bf82346
r24624:ead6f9a66573
r11457:9a04ad49bedb
r11457:9a04ad49bedb
r16644:32454b2fc699
r9243:fd31afba8994
r9243:fd31afba8994
r21744:347302fb9fba
r21811:1abd5b674a60
r26549:82dacf005b0a
r26549:82dacf005b0a
r9243:fd31afba8994
r20280:ca1fc41725ff
r9243:fd31afba8994
r20280:ca1fc41725ff
r9243:fd31afba8994
r8965:16f6e5e9ed36
r26549:82dacf005b0a
r26549:82dacf005b0a
r26549:82dacf005b0a
r26549:82dacf005b0a
r26549:82dacf005b0a
r26549:82dacf005b0a
r28353:bfc4ab63f376
r9243:fd31afba8994
r18673:e5a4490b411b
r8965:16f6e5e9ed36
r26575:d5725bf82346
r26549:82dacf005b0a
r13552:3c2235e7507b
r16431:ec558deca9d7
r26575:d5725bf82346
r26575:d5725bf82346
r26575:d5725bf82346
r26575:d5725bf82346
r21811:1abd5b674a60
r28167:b14ea36d49e5
r26549:82dacf005b0a
r26549:82dacf005b0a
r26549:82dacf005b0a
r13658:18827498fc7a
r13552:3c2235e7507b
r9243:fd31afba8994
r9243:fd31afba8994
r26549:82dacf005b0a
r9243:fd31afba8994
r9243:fd31afba8994
r9243:fd31afba8994
r9243:fd31afba8994
r8965:16f6e5e9ed36
r26575:d5725bf82346
r11069:3f09c162966b
r15608:7b580ec7448a
r15608:7b580ec7448a
r8965:16f6e5e9ed36
r9243:fd31afba8994
r27737:728d55b97775
r9243:fd31afba8994
r9243:fd31afba8994
r26549:82dacf005b0a
r8965:16f6e5e9ed36
r9243:fd31afba8994
r9243:fd31afba8994
r27148:4e041ae27b9d
r8965:16f6e5e9ed36
r11085:c87a330fb4c2
r13658:18827498fc7a
r10352:1c484901e47e
r27325:ad119f64b9b6
r26575:d5725bf82346
r15608:7b580ec7448a
r15608:7b580ec7448a
r8965:16f6e5e9ed36
r9243:fd31afba8994
r9243:fd31afba8994
r15608:7b580ec7448a
r15608:7b580ec7448a
r9243:fd31afba8994
r8965:16f6e5e9ed36
r21811:1abd5b674a60
r13552:3c2235e7507b
r21811:1abd5b674a60
r13552:3c2235e7507b
r13552:3c2235e7507b
r13552:3c2235e7507b
r28353:bfc4ab63f376
r13552:3c2235e7507b
r18673:e5a4490b411b
r13552:3c2235e7507b
r13552:3c2235e7507b
r13552:3c2235e7507b
r13552:3c2235e7507b
r13552:3c2235e7507b
r13552:3c2235e7507b
r13552:3c2235e7507b
r24597:afde5721a3b6
r13552:3c2235e7507b
r24597:afde5721a3b6
r13552:3c2235e7507b
r13552:3c2235e7507b
r13552:3c2235e7507b
r13552:3c2235e7507b
r13552:3c2235e7507b
r13552:3c2235e7507b
r27884:803962be0328
r24597:afde5721a3b6
r13552:3c2235e7507b
r9243:fd31afba8994
r13552:3c2235e7507b
r13552:3c2235e7507b
r19846:75a27628493b
r26575:d5725bf82346
r13552:3c2235e7507b
r13552:3c2235e7507b
r13552:3c2235e7507b
r13552:3c2235e7507b
r24597:afde5721a3b6
r13552:3c2235e7507b
r13552:3c2235e7507b
r13552:3c2235e7507b
r13552:3c2235e7507b
r9243:fd31afba8994
r13552:3c2235e7507b
r26575:d5725bf82346
r24597:afde5721a3b6
r28167:b14ea36d49e5
r21811:1abd5b674a60
r26575:d5725bf82346
r26575:d5725bf82346
r13552:3c2235e7507b
r13552:3c2235e7507b
r28353:bfc4ab63f376
r9243:fd31afba8994
r27943:23c15817f0a5
r27943:23c15817f0a5
r26575:d5725bf82346
r26575:d5725bf82346
r26575:d5725bf82346
r16431:ec558deca9d7
r26575:d5725bf82346
r8965:16f6e5e9ed36
r16647:5a3abb315d22
r9243:fd31afba8994
r9243:fd31afba8994
r27737:728d55b97775
r9243:fd31afba8994
r9243:fd31afba8994
r26575:d5725bf82346
r16647:5a3abb315d22
r21744:347302fb9fba
r16647:5a3abb315d22
r17569:79cc833a8f35
r16647:5a3abb315d22
r26575:d5725bf82346
r21744:347302fb9fba
r21744:347302fb9fba
r21744:347302fb9fba
r21744:347302fb9fba
r16647:5a3abb315d22
r16647:5a3abb315d22
r16647:5a3abb315d22
r26575:d5725bf82346
r16647:5a3abb315d22
r9243:fd31afba8994
r8965:16f6e5e9ed36
r9243:fd31afba8994
r9243:fd31afba8994
r9243:fd31afba8994
r23607:36c15679007d
r9243:fd31afba8994
r8965:16f6e5e9ed36
r9243:fd31afba8994
r9243:fd31afba8994
r26575:d5725bf82346
r8965:16f6e5e9ed36
r9243:fd31afba8994
r26575:d5725bf82346
r9243:fd31afba8994
r8965:16f6e5e9ed36
r8965:16f6e5e9ed36
r27737:728d55b97775
r8965:16f6e5e9ed36
r18667:3b72643a9c00
r8965:16f6e5e9ed36
r16644:32454b2fc699
r9243:fd31afba8994
r8965:16f6e5e9ed36
r23499:f9bf6ad58697
r9243:fd31afba8994
r9243:fd31afba8994
r9243:fd31afba8994
r8965:16f6e5e9ed36
r16647:5a3abb315d22
r23499:f9bf6ad58697
r16647:5a3abb315d22
r16647:5a3abb315d22
r23607:36c15679007d
r16647:5a3abb315d22
r21744:347302fb9fba
r27737:728d55b97775
r16647:5a3abb315d22
r16647:5a3abb315d22
r16647:5a3abb315d22
r16647:5a3abb315d22
r27737:728d55b97775
r16647:5a3abb315d22
r16647:5a3abb315d22
r27090:1ea30951c0f3
r27090:1ea30951c0f3
r27090:1ea30951c0f3
r27090:1ea30951c0f3
r9243:fd31afba8994
r8965:16f6e5e9ed36
r16644:32454b2fc699
r28078:5e5d6c0447b1
r20283:2a199c78224c
r8965:16f6e5e9ed36
r19745:d8f95208ad9e
r27861:6269475166e0
r11368:058349c3a02c
r8965:16f6e5e9ed36
r16644:32454b2fc699
r8965:16f6e5e9ed36
r8965:16f6e5e9ed36
r25565:6a5de7df7ea1
r9243:fd31afba8994
r8965:16f6e5e9ed36
/*
 * This file is part of OpenTTD.
 * 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.
 * 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.
 * 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/>.
 */

/** @file cheat_gui.cpp GUI related to cheating. */

#include "stdafx.h"
#include "command_func.h"
#include "cheat_type.h"
#include "company_base.h"
#include "company_func.h"
#include "saveload/saveload.h"
#include "vehicle_base.h"
#include "textbuf_gui.h"
#include "window_gui.h"
#include "string_func.h"
#include "strings_func.h"
#include "window_func.h"
#include "rail_gui.h"
#include "settings_gui.h"
#include "company_gui.h"
#include "linkgraph/linkgraphschedule.h"
#include "map_func.h"
#include "tile_map.h"
#include "newgrf.h"
#include "error.h"
#include "misc_cmd.h"
#include "core/geometry_func.hpp"
#include "timer/timer.h"
#include "timer/timer_game_calendar.h"
#include "timer/timer_game_economy.h"

#include "widgets/cheat_widget.h"

#include "table/sprites.h"

#include "safeguards.h"


/**
 * The 'amount' to cheat with.
 * This variable is semantically a constant value, but because the cheat
 * code requires to be able to write to the variable it is not constified.
 */
static int32_t _money_cheat_amount = 10000000;

/**
 * Handle cheating of money.
 * Note that the amount of money of a company must be changed through a command
 * rather than by setting a variable. Since the cheat data structure expects a
 * variable, the amount of given/taken money is used for this purpose.
 * @param change_direction is -1 or +1 (down/up)
 * @return Amount of money cheat.
 */
static int32_t ClickMoneyCheat(int32_t, int32_t change_direction)
{
	Command<CMD_MONEY_CHEAT>::Post(Money(_money_cheat_amount) * change_direction);
	return _money_cheat_amount;
}

/**
 * Handle changing of company.
 * @param new_value company to set to
 * @param change_direction is -1 or +1 (down/up)
 * @return The new company.
 */
static int32_t ClickChangeCompanyCheat(int32_t new_value, int32_t change_direction)
{
	while ((uint)new_value < Company::GetPoolSize()) {
		if (Company::IsValidID((CompanyID)new_value)) {
			SetLocalCompany((CompanyID)new_value);
			return _local_company;
		}
		new_value += change_direction;
	}

	return _local_company;
}

/**
 * Allow (or disallow) changing production of all industries.
 * @param new_value new value
 * @return New value allowing change of industry production.
 */
static int32_t ClickSetProdCheat(int32_t new_value, int32_t)
{
	_cheats.setup_prod.value = (new_value != 0);
	InvalidateWindowClassesData(WC_INDUSTRY_VIEW);
	return _cheats.setup_prod.value;
}

extern void CalendarEnginesMonthlyLoop();

/**
 * Handle changing of the current year.
 * @param new_value The chosen year to change to.
 * @return New year.
 */
static int32_t ClickChangeDateCheat(int32_t new_value, int32_t)
{
	/* Don't allow changing to an invalid year, or the current year. */
	auto new_year = Clamp(TimerGameCalendar::Year(new_value), CalendarTime::MIN_YEAR, CalendarTime::MAX_YEAR);
	if (new_year == TimerGameCalendar::year) return TimerGameCalendar::year.base();

	TimerGameCalendar::YearMonthDay ymd = TimerGameCalendar::ConvertDateToYMD(TimerGameCalendar::date);
	TimerGameCalendar::Date new_calendar_date = TimerGameCalendar::ConvertYMDToDate(new_year, ymd.month, ymd.day);
	/* Keep economy and calendar dates synced. */
	TimerGameEconomy::Date new_economy_date = new_calendar_date.base();

	/* Shift cached dates before we change the date. */
	for (auto v : Vehicle::Iterate()) v->ShiftDates(new_economy_date - TimerGameEconomy::date);
	LinkGraphSchedule::instance.ShiftDates(new_economy_date - TimerGameEconomy::date);

	/* Now it's safe to actually change the date. */
	TimerGameCalendar::SetDate(new_calendar_date, TimerGameCalendar::date_fract);
	TimerGameEconomy::SetDate(new_economy_date, TimerGameEconomy::date_fract);

	CalendarEnginesMonthlyLoop();
	SetWindowDirty(WC_STATUS_BAR, 0);
	InvalidateWindowClassesData(WC_BUILD_STATION, 0);
	InvalidateWindowClassesData(WC_BUS_STATION, 0);
	InvalidateWindowClassesData(WC_TRUCK_STATION, 0);
	InvalidateWindowClassesData(WC_BUILD_OBJECT, 0);
	ResetSignalVariant();
	return TimerGameCalendar::year.base();
}

/**
 * Allow (or disallow) a change of the maximum allowed heightlevel.
 * @param new_value new value
 * @return New value (or unchanged old value) of the maximum
 *         allowed heightlevel value.
 */
static int32_t ClickChangeMaxHlCheat(int32_t new_value, int32_t)
{
	new_value = Clamp(new_value, MIN_MAP_HEIGHT_LIMIT, MAX_MAP_HEIGHT_LIMIT);

	/* Check if at least one mountain on the map is higher than the new value.
	 * If yes, disallow the change. */
	for (TileIndex t = 0; t < Map::Size(); t++) {
		if ((int32_t)TileHeight(t) > new_value) {
			ShowErrorMessage(STR_CONFIG_SETTING_TOO_HIGH_MOUNTAIN, INVALID_STRING_ID, WL_ERROR);
			/* Return old, unchanged value */
			return _settings_game.construction.map_height_limit;
		}
	}

	/* Execute the change and reload GRF Data */
	_settings_game.construction.map_height_limit = new_value;
	ReloadNewGRFData();

	/* The smallmap uses an index from heightlevels to colours. Trigger rebuilding it. */
	InvalidateWindowClassesData(WC_SMALLMAP, 2);

	return _settings_game.construction.map_height_limit;
}

/** Available cheats. */
enum CheatNumbers {
	CHT_MONEY,           ///< Change amount of money.
	CHT_CHANGE_COMPANY,  ///< Switch company.
	CHT_EXTRA_DYNAMITE,  ///< Dynamite anything.
	CHT_CROSSINGTUNNELS, ///< Allow tunnels to cross each other.
	CHT_NO_JETCRASH,     ///< Disable jet-airplane crashes.
	CHT_SETUP_PROD,      ///< Allow manually editing of industry production.
	CHT_STATION_RATING,  ///< Fix station ratings at 100%.
	CHT_EDIT_MAX_HL,     ///< Edit maximum allowed heightlevel
	CHT_CHANGE_DATE,     ///< Do time traveling.

	CHT_NUM_CHEATS,      ///< Number of cheats.
};

/**
 * Signature of handler function when user clicks at a cheat.
 * @param new_value The new value.
 * @param change_direction Change direction (+1, +1), \c 0 for boolean settings.
 */
typedef int32_t CheckButtonClick(int32_t new_value, int32_t change_direction);

/** Information of a cheat. */
struct CheatEntry {
	VarType type;          ///< type of selector
	StringID str;          ///< string with descriptive text
	void *variable;        ///< pointer to the variable
	bool *been_used;       ///< has this cheat been used before?
	CheckButtonClick *proc;///< procedure
};

/**
 * The available cheats.
 * Order matches with the values of #CheatNumbers
 */
static const CheatEntry _cheats_ui[] = {
	{SLE_INT32, STR_CHEAT_MONEY,           &_money_cheat_amount,                          &_cheats.money.been_used,            &ClickMoneyCheat         },
	{SLE_UINT8, STR_CHEAT_CHANGE_COMPANY,  &_local_company,                               &_cheats.switch_company.been_used,   &ClickChangeCompanyCheat },
	{SLE_BOOL,  STR_CHEAT_EXTRA_DYNAMITE,  &_cheats.magic_bulldozer.value,                &_cheats.magic_bulldozer.been_used,  nullptr                  },
	{SLE_BOOL,  STR_CHEAT_CROSSINGTUNNELS, &_cheats.crossing_tunnels.value,               &_cheats.crossing_tunnels.been_used, nullptr                  },
	{SLE_BOOL,  STR_CHEAT_NO_JETCRASH,     &_cheats.no_jetcrash.value,                    &_cheats.no_jetcrash.been_used,      nullptr                  },
	{SLE_BOOL,  STR_CHEAT_SETUP_PROD,      &_cheats.setup_prod.value,                     &_cheats.setup_prod.been_used,       &ClickSetProdCheat       },
	{SLE_BOOL,  STR_CHEAT_STATION_RATING,  &_cheats.station_rating.value,                 &_cheats.station_rating.been_used,   nullptr                  },
	{SLE_UINT8, STR_CHEAT_EDIT_MAX_HL,     &_settings_game.construction.map_height_limit, &_cheats.edit_max_hl.been_used,      &ClickChangeMaxHlCheat   },
	{SLE_INT32, STR_CHEAT_CHANGE_DATE,     &TimerGameCalendar::year,                      &_cheats.change_date.been_used,      &ClickChangeDateCheat    },
};

static_assert(CHT_NUM_CHEATS == lengthof(_cheats_ui));

/** Widget definitions of the cheat GUI. */
static constexpr NWidgetPart _nested_cheat_widgets[] = {
	NWidget(NWID_HORIZONTAL),
		NWidget(WWT_CLOSEBOX, COLOUR_GREY),
		NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_CHEATS, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
		NWidget(WWT_SHADEBOX, COLOUR_GREY),
		NWidget(WWT_STICKYBOX, COLOUR_GREY),
	EndContainer(),
	NWidget(WWT_PANEL, COLOUR_GREY, WID_C_PANEL), SetDataTip(0x0, STR_CHEATS_TOOLTIP), EndContainer(),
	NWidget(WWT_PANEL, COLOUR_GREY),
		NWidget(WWT_LABEL, COLOUR_GREY, WID_C_NOTE), SetFill(1, 1), SetDataTip(STR_CHEATS_NOTE, STR_NULL), SetPadding(WidgetDimensions::unscaled.frametext),
	EndContainer(),
};

/** GUI for the cheats. */
struct CheatWindow : Window {
	int clicked;
	int clicked_widget;
	uint line_height;
	Dimension box;      ///< Dimension of box sprite
	Dimension icon;     ///< Dimension of company icon sprite

	CheatWindow(WindowDesc *desc) : Window(desc)
	{
		this->InitNested();
	}

	void OnInit() override
	{
		this->box = maxdim(GetSpriteSize(SPR_BOX_EMPTY), GetSpriteSize(SPR_BOX_CHECKED));
		this->icon = GetSpriteSize(SPR_COMPANY_ICON);
	}

	void DrawWidget(const Rect &r, WidgetID widget) const override
	{
		if (widget != WID_C_PANEL) return;

		const Rect ir = r.Shrink(WidgetDimensions::scaled.framerect);
		int y = ir.top;

		bool rtl = _current_text_dir == TD_RTL;
		uint box_left    = rtl ? ir.right - this->box.width - WidgetDimensions::scaled.hsep_wide : ir.left + WidgetDimensions::scaled.hsep_wide;
		uint button_left = rtl ? ir.right - this->box.width - WidgetDimensions::scaled.hsep_wide * 2 - SETTING_BUTTON_WIDTH : ir.left + this->box.width + WidgetDimensions::scaled.hsep_wide * 2;
		uint text_left   = ir.left + (rtl ? 0 : WidgetDimensions::scaled.hsep_wide * 4 + this->box.width + SETTING_BUTTON_WIDTH);
		uint text_right  = ir.right - (rtl ? WidgetDimensions::scaled.hsep_wide * 4 + this->box.width + SETTING_BUTTON_WIDTH : 0);

		int text_y_offset = (this->line_height - GetCharacterHeight(FS_NORMAL)) / 2;
		int box_y_offset = (this->line_height - this->box.height) / 2;
		int button_y_offset = (this->line_height - SETTING_BUTTON_HEIGHT) / 2;
		int icon_y_offset = (this->line_height - this->icon.height) / 2;

		for (int i = 0; i != lengthof(_cheats_ui); i++) {
			const CheatEntry *ce = &_cheats_ui[i];

			DrawSprite((*ce->been_used) ? SPR_BOX_CHECKED : SPR_BOX_EMPTY, PAL_NONE, box_left, y + box_y_offset);

			switch (ce->type) {
				case SLE_BOOL: {
					bool on = (*(bool*)ce->variable);

					DrawBoolButton(button_left, y + button_y_offset, on, true);
					SetDParam(0, on ? STR_CONFIG_SETTING_ON : STR_CONFIG_SETTING_OFF);
					break;
				}

				default: {
					int32_t val = (int32_t)ReadValue(ce->variable, ce->type);

					/* Draw [<][>] boxes for settings of an integer-type */
					DrawArrowButtons(button_left, y + button_y_offset, COLOUR_YELLOW, clicked - (i * 2), true, true);

					switch (ce->str) {
						/* Display date for change date cheat */
						case STR_CHEAT_CHANGE_DATE: SetDParam(0, TimerGameCalendar::date); break;

						/* Draw coloured flag for change company cheat */
						case STR_CHEAT_CHANGE_COMPANY: {
							SetDParam(0, val + 1);
							uint offset = WidgetDimensions::scaled.hsep_indent + GetStringBoundingBox(ce->str).width;
							DrawCompanyIcon(_local_company, rtl ? text_right - offset - WidgetDimensions::scaled.hsep_indent : text_left + offset, y + icon_y_offset);
							break;
						}

						default: SetDParam(0, val);
					}
					break;
				}
			}

			DrawString(text_left, text_right, y + text_y_offset, ce->str);

			y += this->line_height;
		}
	}

	void UpdateWidgetSize(WidgetID widget, Dimension *size, [[maybe_unused]] const Dimension &padding, [[maybe_unused]] Dimension *fill, [[maybe_unused]] Dimension *resize) override
	{
		if (widget != WID_C_PANEL) return;

		uint width = 0;
		for (int i = 0; i != lengthof(_cheats_ui); i++) {
			const CheatEntry *ce = &_cheats_ui[i];
			switch (ce->type) {
				case SLE_BOOL:
					SetDParam(0, STR_CONFIG_SETTING_ON);
					width = std::max(width, GetStringBoundingBox(ce->str).width);
					SetDParam(0, STR_CONFIG_SETTING_OFF);
					width = std::max(width, GetStringBoundingBox(ce->str).width);
					break;

				default:
					switch (ce->str) {
						/* Display date for change date cheat */
						case STR_CHEAT_CHANGE_DATE:
							SetDParam(0, TimerGameCalendar::ConvertYMDToDate(CalendarTime::MAX_YEAR, 11, 31));
							width = std::max(width, GetStringBoundingBox(ce->str).width);
							break;

						/* Draw coloured flag for change company cheat */
						case STR_CHEAT_CHANGE_COMPANY:
							SetDParamMaxValue(0, MAX_COMPANIES);
							width = std::max(width, GetStringBoundingBox(ce->str).width + WidgetDimensions::scaled.hsep_wide * 4);
							break;

						default:
							SetDParam(0, INT64_MAX);
							width = std::max(width, GetStringBoundingBox(ce->str).width);
							break;
					}
					break;
			}
		}

		this->line_height = std::max(this->box.height, this->icon.height);
		this->line_height = std::max<uint>(this->line_height, SETTING_BUTTON_HEIGHT);
		this->line_height = std::max<uint>(this->line_height, GetCharacterHeight(FS_NORMAL)) + WidgetDimensions::scaled.framerect.Vertical();

		size->width = width + WidgetDimensions::scaled.hsep_wide * 4 + this->box.width + SETTING_BUTTON_WIDTH /* stuff on the left */ + WidgetDimensions::scaled.hsep_wide * 2 /* extra spacing on right */;
		size->height = WidgetDimensions::scaled.framerect.Vertical() + this->line_height * lengthof(_cheats_ui);
	}

	void OnClick([[maybe_unused]] Point pt, WidgetID widget, [[maybe_unused]] int click_count) override
	{
		if (widget != WID_C_PANEL) return;

		Rect r = this->GetWidget<NWidgetBase>(WID_C_PANEL)->GetCurrentRect().Shrink(WidgetDimensions::scaled.framerect);
		uint btn = (pt.y - r.top) / this->line_height;
		uint x = pt.x - r.left;
		bool rtl = _current_text_dir == TD_RTL;
		if (rtl) x = r.Width() - 1 - x;

		if (btn >= lengthof(_cheats_ui)) return;

		const CheatEntry *ce = &_cheats_ui[btn];
		int value = (int32_t)ReadValue(ce->variable, ce->type);
		int oldvalue = value;

		if (btn == CHT_CHANGE_DATE && x >= WidgetDimensions::scaled.hsep_wide * 2 + this->box.width + SETTING_BUTTON_WIDTH) {
			/* Click at the date text directly. */
			clicked_widget = CHT_CHANGE_DATE;
			SetDParam(0, value);
			ShowQueryString(STR_JUST_INT, STR_CHEAT_CHANGE_DATE_QUERY_CAPT, 8, this, CS_NUMERAL, QSF_ACCEPT_UNCHANGED);
			return;
		} else if (btn == CHT_EDIT_MAX_HL && x >= WidgetDimensions::scaled.hsep_wide * 2 + this->box.width + SETTING_BUTTON_WIDTH) {
			clicked_widget = CHT_EDIT_MAX_HL;
			SetDParam(0, value);
			ShowQueryString(STR_JUST_INT, STR_CHEAT_EDIT_MAX_HL_QUERY_CAPT, 8, this, CS_NUMERAL, QSF_ACCEPT_UNCHANGED);
			return;
		}

		/* Not clicking a button? */
		if (!IsInsideMM(x, WidgetDimensions::scaled.hsep_wide * 2 + this->box.width, WidgetDimensions::scaled.hsep_wide * 2 + this->box.width + SETTING_BUTTON_WIDTH)) return;

		*ce->been_used = true;

		switch (ce->type) {
			case SLE_BOOL:
				value ^= 1;
				if (ce->proc != nullptr) ce->proc(value, 0);
				break;

			default:
				/* Take whatever the function returns */
				value = ce->proc(value + ((x >= WidgetDimensions::scaled.hsep_wide * 2 + this->box.width + SETTING_BUTTON_WIDTH / 2) ? 1 : -1), (x >= WidgetDimensions::scaled.hsep_wide * 2 + this->box.width + SETTING_BUTTON_WIDTH / 2) ? 1 : -1);

				/* The first cheat (money), doesn't return a different value. */
				if (value != oldvalue || btn == CHT_MONEY) this->clicked = btn * 2 + 1 + ((x >= WidgetDimensions::scaled.hsep_wide * 2 + this->box.width + SETTING_BUTTON_WIDTH / 2) != rtl ? 1 : 0);
				break;
		}

		if (value != oldvalue) WriteValue(ce->variable, ce->type, (int64_t)value);

		this->SetTimeout();

		this->SetDirty();
	}

	void OnTimeout() override
	{
		this->clicked = 0;
		this->SetDirty();
	}

	void OnQueryTextFinished(char *str) override
	{
		/* Was 'cancel' pressed or nothing entered? */
		if (str == nullptr || StrEmpty(str)) return;

		const CheatEntry *ce = &_cheats_ui[clicked_widget];
		int oldvalue = (int32_t)ReadValue(ce->variable, ce->type);
		int value = atoi(str);
		*ce->been_used = true;
		value = ce->proc(value, value - oldvalue);

		if (value != oldvalue) WriteValue(ce->variable, ce->type, (int64_t)value);
		this->SetDirty();
	}

	IntervalTimer<TimerGameCalendar> daily_interval = {{TimerGameCalendar::MONTH, TimerGameCalendar::Priority::NONE}, [this](auto) {
		this->SetDirty();
	}};
};

/** Window description of the cheats GUI. */
static WindowDesc _cheats_desc(__FILE__, __LINE__,
	WDP_AUTO, "cheats", 0, 0,
	WC_CHEATS, WC_NONE,
	0,
	std::begin(_nested_cheat_widgets), std::end(_nested_cheat_widgets)
);

/** Open cheat window. */
void ShowCheatWindow()
{
	CloseWindowById(WC_CHEATS, 0);
	new CheatWindow(&_cheats_desc);
}