# cheat_gui.cpp

Go to the documentation of this file.

    00001 /* $Id$ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * 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.
    00006  * 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.
    00007  * 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/>.
    00008  */
    00009
    00012 #include "stdafx.h"
    00013 #include "command_func.h"
    00014 #include "cheat_type.h"
    00015 #include "company_base.h"
    00016 #include "company_func.h"
    00017 #include "date_func.h"
    00018 #include "saveload/saveload.h"
    00019 #include "textbuf_gui.h"
    00020 #include "window_gui.h"
    00021 #include "newgrf.h"
    00022 #include "string_func.h"
    00023 #include "strings_func.h"
    00024 #include "window_func.h"
    00025 #include "rail_gui.h"
    00026 #include "gui.h"
    00027 #include "company_gui.h"
    00028 #include "gamelog.h"
    00029
    00030 #include "table/strings.h"
    00031 #include "table/sprites.h"
    00032
    00033
    00039 static int32 _money_cheat_amount = 10000000;
    00040
    00050 static int32 ClickMoneyCheat(int32 p1, int32 p2)
    00051 {
    00052   DoCommandP(0, (uint32)(p2 * _money_cheat_amount), 0, CMD_MONEY_CHEAT);
    00053   return _money_cheat_amount;
    00054 }
    00055
    00062 static int32 ClickChangeCompanyCheat(int32 p1, int32 p2)
    00063 {
    00064   while ((uint)p1 < Company::GetPoolSize()) {
    00065     if (Company::IsValidID((CompanyID)p1)) {
    00066       SetLocalCompany((CompanyID)p1);
    00067       return _local_company;
    00068     }
    00069     p1 += p2;
    00070   }
    00071
    00072   return _local_company;
    00073 }
    00074
    00081 static int32 ClickSetProdCheat(int32 p1, int32 p2)
    00082 {
    00083   _cheats.setup_prod.value = (p1 != 0);
    00084   InvalidateWindowClassesData(WC_INDUSTRY_VIEW);
    00085   return _cheats.setup_prod.value;
    00086 }
    00087
    00094 static int32 ClickChangeClimateCheat(int32 p1, int32 p2)
    00095 {
    00096   if (p1 == -1) p1 = 3;
    00097   if (p1 ==  4) p1 = 0;
    00098   _settings_game.game_creation.landscape = p1;
    00099
    00100   GamelogStartAction(GLAT_CHEAT);
    00101   GamelogTestMode();
    00102   ReloadNewGRFData();
    00103   GamelogStopAction();
    00104
    00105   return _settings_game.game_creation.landscape;
    00106 }
    00107
    00108 extern void EnginesMonthlyLoop();
    00109
    00116 static int32 ClickChangeDateCheat(int32 p1, int32 p2)
    00117 {
    00118   YearMonthDay ymd;
    00119   ConvertDateToYMD(_date, &ymd);
    00120
    00121   p1 = Clamp(p1, MIN_YEAR, MAX_YEAR);
    00122   if (p1 == _cur_year) return _cur_year;
    00123
    00124   SetDate(ConvertYMDToDate(p1, ymd.month, ymd.day), _date_fract);
    00125   EnginesMonthlyLoop();
    00126   SetWindowDirty(WC_STATUS_BAR, 0);
    00127   InvalidateWindowClassesData(WC_BUILD_STATION, 0);
    00128   InvalidateWindowClassesData(WC_BUILD_OBJECT, 0);
    00129   ResetSignalVariant();
    00130   return _cur_year;
    00131 }
    00132
    00134 enum CheatNumbers {
    00135   CHT_MONEY,
    00136   CHT_CHANGE_COMPANY,
    00137   CHT_EXTRA_DYNAMITE,
    00138   CHT_CROSSINGTUNNELS,
    00139   CHT_NO_JETCRASH,
    00140   CHT_SETUP_PROD,
    00141   CHT_SWITCH_CLIMATE,
    00142   CHT_CHANGE_DATE,
    00143
    00144   CHT_NUM_CHEATS,
    00145 };
    00146
    00152 typedef int32 CheckButtonClick(int32 p1, int32 p2);
    00153
    00155 struct CheatEntry {
    00156   VarType type;
    00157   StringID str;
    00158   void *variable;
    00159   bool *been_used;
    00160   CheckButtonClick *proc;
    00161 };
    00162
    00167 static const CheatEntry _cheats_ui[] = {
    00168   {SLE_INT32, STR_CHEAT_MONEY,           &_money_cheat_amount,                    &_cheats.money.been_used,            &ClickMoneyCheat         },
    00169   {SLE_UINT8, STR_CHEAT_CHANGE_COMPANY,  &_local_company,                         &_cheats.switch_company.been_used,   &ClickChangeCompanyCheat },
    00170   {SLE_BOOL,  STR_CHEAT_EXTRA_DYNAMITE,  &_cheats.magic_bulldozer.value,          &_cheats.magic_bulldozer.been_used,  NULL                     },
    00171   {SLE_BOOL,  STR_CHEAT_CROSSINGTUNNELS, &_cheats.crossing_tunnels.value,         &_cheats.crossing_tunnels.been_used, NULL                     },
    00172   {SLE_BOOL,  STR_CHEAT_NO_JETCRASH,     &_cheats.no_jetcrash.value,              &_cheats.no_jetcrash.been_used,      NULL                     },
    00173   {SLE_BOOL,  STR_CHEAT_SETUP_PROD,      &_cheats.setup_prod.value,               &_cheats.setup_prod.been_used,       &ClickSetProdCheat       },
    00174   {SLE_UINT8, STR_CHEAT_SWITCH_CLIMATE,  &_settings_game.game_creation.landscape, &_cheats.switch_climate.been_used,   &ClickChangeClimateCheat },
    00175   {SLE_INT32, STR_CHEAT_CHANGE_DATE,     &_cur_year,                              &_cheats.change_date.been_used,      &ClickChangeDateCheat    },
    00176 };
    00177
    00178 assert_compile(CHT_NUM_CHEATS == lengthof(_cheats_ui));
    00179
    00181 enum CheatWidgets {
    00182   CW_PANEL,
    00183 };
    00184
    00186 static const NWidgetPart _nested_cheat_widgets[] = {
    00187   NWidget(NWID_HORIZONTAL),
    00188     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
    00189     NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_CHEATS, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    00190     NWidget(WWT_SHADEBOX, COLOUR_GREY),
    00191     NWidget(WWT_STICKYBOX, COLOUR_GREY),
    00192   EndContainer(),
    00193   NWidget(WWT_PANEL, COLOUR_GREY, CW_PANEL), SetDataTip(0x0, STR_CHEATS_TOOLTIP), EndContainer(),
    00194 };
    00195
    00197 struct CheatWindow : Window {
    00198   int clicked;
    00199   int header_height;
    00200
    00201   CheatWindow(const WindowDesc *desc) : Window()
    00202   {
    00203     this->InitNested(desc);
    00204   }
    00205
    00206   virtual void DrawWidget(const Rect &r, int widget) const
    00207   {
    00208     if (widget != CW_PANEL) return;
    00209
    00210     int y = r.top + WD_FRAMERECT_TOP + this->header_height;
    00211     DrawStringMultiLine(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, r.top + WD_FRAMERECT_TOP, y, STR_CHEATS_WARNING, TC_FROMSTRING, SA_CENTER);
    00212
    00213     bool rtl = _current_text_dir == TD_RTL;
    00214     uint box_left    = rtl ? r.right - 12 : r.left + 5;
    00215     uint button_left = rtl ? r.right - 40 : r.left + 20;
    00216     uint text_left   = r.left + (rtl ? WD_FRAMERECT_LEFT: 50);
    00217     uint text_right  = r.right - (rtl ? 50 : WD_FRAMERECT_RIGHT);
    00218
    00219     for (int i = 0; i != lengthof(_cheats_ui); i++) {
    00220       const CheatEntry *ce = &_cheats_ui[i];
    00221
    00222       DrawSprite((*ce->been_used) ? SPR_BOX_CHECKED : SPR_BOX_EMPTY, PAL_NONE, box_left, y + 2);
    00223
    00224       switch (ce->type) {
    00225         case SLE_BOOL: {
    00226           bool on = (*(bool*)ce->variable);
    00227
    00228           DrawFrameRect(button_left, y + 1, button_left + 20 - 1, y + FONT_HEIGHT_NORMAL - 1, on ? COLOUR_GREEN : COLOUR_RED, on ? FR_LOWERED : FR_NONE);
    00229           SetDParam(0, on ? STR_CONFIG_SETTING_ON : STR_CONFIG_SETTING_OFF);
    00230           break;
    00231         }
    00232
    00233         default: {
    00234           int32 val = (int32)ReadValue(ce->variable, ce->type);
    00235           char buf[512];
    00236
    00237           /* Draw [<][>] boxes for settings of an integer-type */
    00238           DrawArrowButtons(button_left, y, COLOUR_YELLOW, clicked - (i * 2), true, true);
    00239
    00240           switch (ce->str) {
    00241             /* Display date for change date cheat */
    00242             case STR_CHEAT_CHANGE_DATE: SetDParam(0, _date); break;
    00243
    00244             /* Draw coloured flag for change company cheat */
    00245             case STR_CHEAT_CHANGE_COMPANY: {
    00246               SetDParam(0, val + 1);
    00247               GetString(buf, STR_CHEAT_CHANGE_COMPANY, lastof(buf));
    00248               uint offset = 10 + GetStringBoundingBox(buf).width;
    00249               DrawCompanyIcon(_local_company, rtl ? text_right - offset - 10 : text_left + offset, y + 2);
    00250               break;
    00251             }
    00252
    00253             /* Set correct string for switch climate cheat */
    00254             case STR_CHEAT_SWITCH_CLIMATE: val += STR_CHEAT_SWITCH_CLIMATE_TEMPERATE_LANDSCAPE;
    00255               /* FALL THROUGH */
    00256
    00257             default: SetDParam(0, val);
    00258           }
    00259           break;
    00260         }
    00261       }
    00262
    00263       DrawString(text_left, text_right, y + 1, ce->str);
    00264
    00265       y += FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
    00266     }
    00267   }
    00268
    00269   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    00270   {
    00271     if (widget != CW_PANEL) return;
    00272
    00273     uint width = 0;
    00274     for (int i = 0; i != lengthof(_cheats_ui); i++) {
    00275       const CheatEntry *ce = &_cheats_ui[i];
    00276       switch (ce->type) {
    00277         case SLE_BOOL:
    00278           SetDParam(0, STR_CONFIG_SETTING_ON);
    00279           width = max(width, GetStringBoundingBox(ce->str).width);
    00280           SetDParam(0, STR_CONFIG_SETTING_OFF);
    00281           width = max(width, GetStringBoundingBox(ce->str).width);
    00282           break;
    00283
    00284         default:
    00285           switch (ce->str) {
    00286             /* Display date for change date cheat */
    00287             case STR_CHEAT_CHANGE_DATE:
    00288               SetDParam(0, ConvertYMDToDate(MAX_YEAR, 11, 31));
    00289               width = max(width, GetStringBoundingBox(ce->str).width);
    00290               break;
    00291
    00292             /* Draw coloured flag for change company cheat */
    00293             case STR_CHEAT_CHANGE_COMPANY:
    00294               SetDParam(0, 15);
    00295               width = max(width, GetStringBoundingBox(ce->str).width + 10 + 10);
    00296               break;
    00297
    00298             /* Set correct string for switch climate cheat */
    00299             case STR_CHEAT_SWITCH_CLIMATE:
    00300               for (StringID i = STR_CHEAT_SWITCH_CLIMATE_TEMPERATE_LANDSCAPE; i <= STR_CHEAT_SWITCH_CLIMATE_TOYLAND_LANDSCAPE; i++) {
    00301                 SetDParam(0, i);
    00302                 width = max(width, GetStringBoundingBox(ce->str).width);
    00303               }
    00304               break;
    00305
    00306             default:
    00307               SetDParam(0, INT64_MAX);
    00308               width = max(width, GetStringBoundingBox(ce->str).width);
    00309               break;
    00310           }
    00311           break;
    00312       }
    00313     }
    00314
    00315     size->width = width + 50 /* stuff on the left */ + 10 /* extra spacing on right */;
    00316     this->header_height = GetStringHeight(STR_CHEATS_WARNING, size->width - WD_FRAMERECT_LEFT - WD_FRAMERECT_RIGHT) + WD_PAR_VSEP_WIDE;
    00317     size->height = this->header_height + WD_FRAMERECT_TOP + WD_PAR_VSEP_NORMAL + WD_FRAMERECT_BOTTOM + (FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL) * lengthof(_cheats_ui);
    00318   }
    00319
    00320   virtual void OnClick(Point pt, int widget, int click_count)
    00321   {
    00322     const NWidgetBase *wid = this->GetWidget<NWidgetBase>(CW_PANEL);
    00323     uint btn = (pt.y - wid->pos_y - WD_FRAMERECT_TOP - this->header_height) / (FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL);
    00324     uint x = pt.x - wid->pos_x;
    00325     bool rtl = _current_text_dir == TD_RTL;
    00326     if (rtl) x = wid->current_x - x;
    00327
    00328     if (btn >= lengthof(_cheats_ui)) return;
    00329
    00330     const CheatEntry *ce = &_cheats_ui[btn];
    00331     int value = (int32)ReadValue(ce->variable, ce->type);
    00332     int oldvalue = value;
    00333
    00334     if (btn == CHT_CHANGE_DATE && x >= 40) {
    00335       /* Click at the date text directly. */
    00336       SetDParam(0, value);
    00337       ShowQueryString(STR_JUST_INT, STR_CHEAT_CHANGE_DATE_QUERY_CAPT, 8, 100, this, CS_NUMERAL, QSF_ACCEPT_UNCHANGED);
    00338       return;
    00339     }
    00340
    00341     /* Not clicking a button? */
    00342     if (!IsInsideMM(x, 20, 40)) return;
    00343
    00344     *ce->been_used = true;
    00345
    00346     switch (ce->type) {
    00347       case SLE_BOOL:
    00348         value ^= 1;
    00349         if (ce->proc != NULL) ce->proc(value, 0);
    00350         break;
    00351
    00352       default:
    00353         /* Take whatever the function returns */
    00354         value = ce->proc(value + ((x >= 30) ? 1 : -1), (x >= 30) ? 1 : -1);
    00355
    00356         /* The first cheat (money), doesn't return a different value. */
    00357         if (value != oldvalue || btn == CHT_MONEY) this->clicked = btn * 2 + 1 + ((x >= 30) != rtl ? 1 : 0);
    00358         break;
    00359     }
    00360
    00361     if (value != oldvalue) WriteValue(ce->variable, ce->type, (int64)value);
    00362
    00363     this->flags4 |= WF_TIMEOUT_BEGIN;
    00364
    00365     this->SetDirty();
    00366   }
    00367
    00368   virtual void OnTimeout()
    00369   {
    00370     this->clicked = 0;
    00371     this->SetDirty();
    00372   }
    00373
    00374   virtual void OnQueryTextFinished(char *str)
    00375   {
    00376     /* Was 'cancel' pressed or nothing entered? */
    00377     if (str == NULL || StrEmpty(str)) return;
    00378
    00379     const CheatEntry *ce = &_cheats_ui[CHT_CHANGE_DATE];
    00380     int oldvalue = (int32)ReadValue(ce->variable, ce->type);
    00381     int value = atoi(str);
    00382     *ce->been_used = true;
    00383     value = ce->proc(value, value - oldvalue);
    00384
    00385     if (value != oldvalue) WriteValue(ce->variable, ce->type, (int64)value);
    00386     this->SetDirty();
    00387   }
    00388 };
    00389
    00391 static const WindowDesc _cheats_desc(
    00392   WDP_AUTO, 0, 0,
    00393   WC_CHEATS, WC_NONE,
    00394   WDF_UNCLICK_BUTTONS,
    00395   _nested_cheat_widgets, lengthof(_nested_cheat_widgets)
    00396 );
    00397
    00399 void ShowCheatWindow()
    00400 {
    00401   DeleteWindowById(WC_CHEATS, 0);
    00402   new CheatWindow(&_cheats_desc);
    00403 }