author | glx |
Fri, 13 Jun 2008 15:43:39 +0000 | |
branch | noai |
changeset 10955 | 56b381e5253f |
parent 10829 | 8a0ec0f0f928 |
permissions | -rw-r--r-- |
10249 | 1 |
/* $Id$ */ |
2 |
||
10455
22c441f5adf9
(svn r12997) [NoAI] -Sync: with trunk r12895:12996.
rubidium
parents:
10249
diff
changeset
|
3 |
/** @file cheat_gui.cpp GUI related to cheating. */ |
10249 | 4 |
|
5 |
#include "stdafx.h" |
|
6 |
#include "openttd.h" |
|
7 |
#include "command_func.h" |
|
8 |
#include "cheat_func.h" |
|
9 |
#include "player_base.h" |
|
10 |
#include "player_func.h" |
|
11 |
#include "gfx_func.h" |
|
12 |
#include "date_func.h" |
|
13 |
#include "saveload.h" |
|
14 |
#include "window_gui.h" |
|
15 |
#include "newgrf.h" |
|
16 |
#include "settings_type.h" |
|
17 |
#include "strings_func.h" |
|
18 |
#include "window_func.h" |
|
19 |
#include "rail_gui.h" |
|
20 |
#include "gui.h" |
|
21 |
#include "player_gui.h" |
|
10829 | 22 |
#include "gamelog.h" |
10249 | 23 |
|
24 |
#include "table/strings.h" |
|
25 |
#include "table/sprites.h" |
|
26 |
||
27 |
||
28 |
/** |
|
29 |
* The 'amount' to cheat with. |
|
30 |
* This variable is semantically a constant value, but because the cheat |
|
31 |
* code requires to be able to write to the variable it is not constified. |
|
32 |
*/ |
|
33 |
static int32 _money_cheat_amount = 10000000; |
|
34 |
||
35 |
static int32 ClickMoneyCheat(int32 p1, int32 p2) |
|
36 |
{ |
|
37 |
DoCommandP(0, (uint32)(p2 * _money_cheat_amount), 0, NULL, CMD_MONEY_CHEAT); |
|
38 |
return _money_cheat_amount; |
|
39 |
} |
|
40 |
||
41 |
/** |
|
42 |
* @param p1 player to set to |
|
43 |
* @param p2 is -1 or +1 (down/up) |
|
44 |
*/ |
|
45 |
static int32 ClickChangePlayerCheat(int32 p1, int32 p2) |
|
46 |
{ |
|
47 |
while (IsValidPlayer((PlayerID)p1)) { |
|
48 |
if (_players[p1].is_active) { |
|
49 |
SetLocalPlayer((PlayerID)p1); |
|
50 |
||
51 |
MarkWholeScreenDirty(); |
|
52 |
return _local_player; |
|
53 |
} |
|
54 |
p1 += p2; |
|
55 |
} |
|
56 |
||
57 |
return _local_player; |
|
58 |
} |
|
59 |
||
60 |
/** |
|
61 |
* @param p1 -1 or +1 (down/up) |
|
62 |
* @param p2 unused |
|
63 |
*/ |
|
64 |
static int32 ClickChangeClimateCheat(int32 p1, int32 p2) |
|
65 |
{ |
|
66 |
if (p1 == -1) p1 = 3; |
|
67 |
if (p1 == 4) p1 = 0; |
|
10776 | 68 |
_settings_game.game_creation.landscape = p1; |
10829 | 69 |
|
70 |
GamelogStartAction(GLAT_CHEAT); |
|
71 |
GamelogTestMode(); |
|
10249 | 72 |
ReloadNewGRFData(); |
10829 | 73 |
GamelogStopAction(); |
74 |
||
10776 | 75 |
return _settings_game.game_creation.landscape; |
10249 | 76 |
} |
77 |
||
78 |
extern void EnginesMonthlyLoop(); |
|
79 |
||
80 |
/** |
|
81 |
* @param p1 unused |
|
82 |
* @param p2 1 (increase) or -1 (decrease) |
|
83 |
*/ |
|
84 |
static int32 ClickChangeDateCheat(int32 p1, int32 p2) |
|
85 |
{ |
|
86 |
YearMonthDay ymd; |
|
87 |
ConvertDateToYMD(_date, &ymd); |
|
88 |
||
89 |
if ((ymd.year == MIN_YEAR && p2 == -1) || (ymd.year == MAX_YEAR && p2 == 1)) return _cur_year; |
|
90 |
||
91 |
SetDate(ConvertYMDToDate(_cur_year + p2, ymd.month, ymd.day)); |
|
92 |
EnginesMonthlyLoop(); |
|
93 |
SetWindowDirty(FindWindowById(WC_STATUS_BAR, 0)); |
|
94 |
ResetSignalVariant(); |
|
95 |
return _cur_year; |
|
96 |
} |
|
97 |
||
98 |
typedef int32 CheckButtonClick(int32, int32); |
|
99 |
||
100 |
struct CheatEntry { |
|
101 |
VarType type; ///< type of selector |
|
102 |
StringID str; ///< string with descriptive text |
|
103 |
void *variable; ///< pointer to the variable |
|
104 |
bool *been_used; ///< has this cheat been used before? |
|
105 |
CheckButtonClick *proc;///< procedure |
|
106 |
}; |
|
107 |
||
108 |
static const CheatEntry _cheats_ui[] = { |
|
10715
6bdf79ffb022
(svn r13265) [NoAI] -Sync with trunk r13185:13264.
rubidium
parents:
10645
diff
changeset
|
109 |
{SLE_INT32, STR_CHEAT_MONEY, &_money_cheat_amount, &_cheats.money.been_used, &ClickMoneyCheat }, |
6bdf79ffb022
(svn r13265) [NoAI] -Sync with trunk r13185:13264.
rubidium
parents:
10645
diff
changeset
|
110 |
{SLE_UINT8, STR_CHEAT_CHANGE_PLAYER, &_local_player, &_cheats.switch_player.been_used, &ClickChangePlayerCheat }, |
6bdf79ffb022
(svn r13265) [NoAI] -Sync with trunk r13185:13264.
rubidium
parents:
10645
diff
changeset
|
111 |
{SLE_BOOL, STR_CHEAT_EXTRA_DYNAMITE, &_cheats.magic_bulldozer.value, &_cheats.magic_bulldozer.been_used, NULL }, |
6bdf79ffb022
(svn r13265) [NoAI] -Sync with trunk r13185:13264.
rubidium
parents:
10645
diff
changeset
|
112 |
{SLE_BOOL, STR_CHEAT_CROSSINGTUNNELS, &_cheats.crossing_tunnels.value, &_cheats.crossing_tunnels.been_used, NULL }, |
6bdf79ffb022
(svn r13265) [NoAI] -Sync with trunk r13185:13264.
rubidium
parents:
10645
diff
changeset
|
113 |
{SLE_BOOL, STR_CHEAT_BUILD_IN_PAUSE, &_cheats.build_in_pause.value, &_cheats.build_in_pause.been_used, NULL }, |
6bdf79ffb022
(svn r13265) [NoAI] -Sync with trunk r13185:13264.
rubidium
parents:
10645
diff
changeset
|
114 |
{SLE_BOOL, STR_CHEAT_NO_JETCRASH, &_cheats.no_jetcrash.value, &_cheats.no_jetcrash.been_used, NULL }, |
6bdf79ffb022
(svn r13265) [NoAI] -Sync with trunk r13185:13264.
rubidium
parents:
10645
diff
changeset
|
115 |
{SLE_BOOL, STR_CHEAT_SETUP_PROD, &_cheats.setup_prod.value, &_cheats.setup_prod.been_used, NULL }, |
10776 | 116 |
{SLE_UINT8, STR_CHEAT_SWITCH_CLIMATE, &_settings_game.game_creation.landscape, &_cheats.switch_climate.been_used, &ClickChangeClimateCheat}, |
10715
6bdf79ffb022
(svn r13265) [NoAI] -Sync with trunk r13185:13264.
rubidium
parents:
10645
diff
changeset
|
117 |
{SLE_INT32, STR_CHEAT_CHANGE_DATE, &_cur_year, &_cheats.change_date.been_used, &ClickChangeDateCheat }, |
10249 | 118 |
}; |
119 |
||
120 |
||
121 |
static const Widget _cheat_widgets[] = { |
|
122 |
{ WWT_CLOSEBOX, RESIZE_NONE, 14, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW}, |
|
123 |
{ WWT_CAPTION, RESIZE_NONE, 14, 11, 399, 0, 13, STR_CHEATS, STR_018C_WINDOW_TITLE_DRAG_THIS}, |
|
124 |
{ WWT_PANEL, RESIZE_NONE, 14, 0, 399, 14, 169, 0x0, STR_NULL}, |
|
125 |
{ WWT_PANEL, RESIZE_NONE, 14, 0, 399, 14, 169, 0x0, STR_CHEATS_TIP}, |
|
126 |
{ WIDGETS_END}, |
|
127 |
}; |
|
128 |
||
10645 | 129 |
struct CheatWindow : Window { |
130 |
int clicked; |
|
10249 | 131 |
|
10645 | 132 |
CheatWindow(const WindowDesc *desc) : Window(desc) |
133 |
{ |
|
10715
6bdf79ffb022
(svn r13265) [NoAI] -Sync with trunk r13185:13264.
rubidium
parents:
10645
diff
changeset
|
134 |
this->FindWindowPlacementAndResize(desc); |
10645 | 135 |
} |
10249 | 136 |
|
10645 | 137 |
virtual void OnPaint() |
138 |
{ |
|
139 |
this->DrawWidgets(); |
|
140 |
DrawStringMultiCenter(200, 25, STR_CHEATS_WARNING, width - 50); |
|
10249 | 141 |
|
10645 | 142 |
for (int i = 0, x = 0, y = 45; i != lengthof(_cheats_ui); i++) { |
143 |
const CheatEntry *ce = &_cheats_ui[i]; |
|
10249 | 144 |
|
10645 | 145 |
DrawSprite((*ce->been_used) ? SPR_BOX_CHECKED : SPR_BOX_EMPTY, PAL_NONE, x + 5, y + 2); |
10249 | 146 |
|
10645 | 147 |
switch (ce->type) { |
148 |
case SLE_BOOL: { |
|
149 |
bool on = (*(bool*)ce->variable); |
|
150 |
||
151 |
DrawFrameRect(x + 20, y + 1, x + 30 + 9, y + 9, on ? 6 : 4, on ? FR_LOWERED : FR_NONE); |
|
152 |
SetDParam(0, on ? STR_CONFIG_PATCHES_ON : STR_CONFIG_PATCHES_OFF); |
|
153 |
} break; |
|
154 |
||
155 |
default: { |
|
156 |
int32 val = (int32)ReadValue(ce->variable, ce->type); |
|
157 |
char buf[512]; |
|
158 |
||
159 |
/* Draw [<][>] boxes for settings of an integer-type */ |
|
160 |
DrawArrowButtons(x + 20, y, 3, clicked - (i * 2), true, true); |
|
161 |
||
162 |
switch (ce->str) { |
|
163 |
/* Display date for change date cheat */ |
|
164 |
case STR_CHEAT_CHANGE_DATE: SetDParam(0, _date); break; |
|
165 |
||
166 |
/* Draw colored flag for change player cheat */ |
|
167 |
case STR_CHEAT_CHANGE_PLAYER: |
|
168 |
SetDParam(0, val); |
|
169 |
GetString(buf, STR_CHEAT_CHANGE_PLAYER, lastof(buf)); |
|
170 |
DrawPlayerIcon(_current_player, 60 + GetStringBoundingBox(buf).width, y + 2); |
|
171 |
break; |
|
172 |
||
173 |
/* Set correct string for switch climate cheat */ |
|
174 |
case STR_CHEAT_SWITCH_CLIMATE: val += STR_TEMPERATE_LANDSCAPE; |
|
175 |
||
176 |
/* Fallthrough */ |
|
177 |
default: SetDParam(0, val); |
|
178 |
} |
|
179 |
} break; |
|
10249 | 180 |
} |
10645 | 181 |
|
182 |
DrawString(50, y + 1, ce->str, TC_FROMSTRING); |
|
183 |
||
184 |
y += 12; |
|
185 |
} |
|
186 |
} |
|
187 |
||
188 |
virtual void OnClick(Point pt, int widget) |
|
189 |
{ |
|
190 |
uint btn = (pt.y - 46) / 12; |
|
191 |
uint x = pt.x; |
|
192 |
||
193 |
/* Not clicking a button? */ |
|
194 |
if (!IsInsideMM(x, 20, 40) || btn >= lengthof(_cheats_ui)) return; |
|
195 |
||
196 |
const CheatEntry *ce = &_cheats_ui[btn]; |
|
197 |
int value = (int32)ReadValue(ce->variable, ce->type); |
|
198 |
int oldvalue = value; |
|
199 |
||
200 |
*ce->been_used = true; |
|
201 |
||
202 |
switch (ce->type) { |
|
203 |
case SLE_BOOL: |
|
204 |
value ^= 1; |
|
205 |
if (ce->proc != NULL) ce->proc(value, 0); |
|
206 |
break; |
|
207 |
||
208 |
default: |
|
209 |
/* Take whatever the function returns */ |
|
210 |
value = ce->proc(value + ((x >= 30) ? 1 : -1), (x >= 30) ? 1 : -1); |
|
211 |
||
212 |
/* The first cheat (money), doesn't return a different value. */ |
|
213 |
if (value != oldvalue || btn == 0) this->clicked = btn * 2 + 1 + ((x >= 30) ? 1 : 0); |
|
214 |
break; |
|
10249 | 215 |
} |
216 |
||
10645 | 217 |
if (value != oldvalue) WriteValue(ce->variable, ce->type, (int64)value); |
10249 | 218 |
|
10645 | 219 |
flags4 |= 5 << WF_TIMEOUT_SHL; |
10249 | 220 |
|
10645 | 221 |
SetDirty(); |
222 |
} |
|
10249 | 223 |
|
10645 | 224 |
virtual void OnTimeout() |
225 |
{ |
|
226 |
this->clicked = 0; |
|
227 |
this->SetDirty(); |
|
10249 | 228 |
} |
10645 | 229 |
}; |
10249 | 230 |
|
231 |
static const WindowDesc _cheats_desc = { |
|
232 |
240, 22, 400, 170, 400, 170, |
|
233 |
WC_CHEATS, WC_NONE, |
|
234 |
WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS, |
|
235 |
_cheat_widgets, |
|
236 |
}; |
|
237 |
||
238 |
||
239 |
void ShowCheatWindow() |
|
240 |
{ |
|
241 |
DeleteWindowById(WC_CHEATS, 0); |
|
10645 | 242 |
new CheatWindow(&_cheats_desc); |
10249 | 243 |
} |