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