# cheat_gui.cpp

Go to the documentation of this file.

    00001 /* $Id: cheat_gui.cpp 21890 2011-01-22 14:52:20Z rubidium $ */
    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   ResetSignalVariant();
    00129   return _cur_year;
    00130 }
    00131
    00133 enum CheatNumbers {
    00134   CHT_MONEY,
    00135   CHT_CHANGE_COMPANY,
    00136   CHT_EXTRA_DYNAMITE,
    00137   CHT_CROSSINGTUNNELS,
    00138   CHT_NO_JETCRASH,
    00139   CHT_SETUP_PROD,
    00140   CHT_SWITCH_CLIMATE,
    00141   CHT_CHANGE_DATE,
    00142
    00143   CHT_NUM_CHEATS,
    00144 };
    00145
    00151 typedef int32 CheckButtonClick(int32 p1, int32 p2);
    00152
    00154 struct CheatEntry {
    00155   VarType type;
    00156   StringID str;
    00157   void *variable;
    00158   bool *been_used;
    00159   CheckButtonClick *proc;
    00160 };
    00161
    00166 static const CheatEntry _cheats_ui[] = {
    00167   {SLE_INT32, STR_CHEAT_MONEY,           &_money_cheat_amount,                    &_cheats.money.been_used,            &ClickMoneyCheat         },
    00168   {SLE_UINT8, STR_CHEAT_CHANGE_COMPANY,  &_local_company,                         &_cheats.switch_company.been_used,   &ClickChangeCompanyCheat },
    00169   {SLE_BOOL,  STR_CHEAT_EXTRA_DYNAMITE,  &_cheats.magic_bulldozer.value,          &_cheats.magic_bulldozer.been_used,  NULL                     },
    00170   {SLE_BOOL,  STR_CHEAT_CROSSINGTUNNELS, &_cheats.crossing_tunnels.value,         &_cheats.crossing_tunnels.been_used, NULL                     },
    00171   {SLE_BOOL,  STR_CHEAT_NO_JETCRASH,     &_cheats.no_jetcrash.value,              &_cheats.no_jetcrash.been_used,      NULL                     },
    00172   {SLE_BOOL,  STR_CHEAT_SETUP_PROD,      &_cheats.setup_prod.value,               &_cheats.setup_prod.been_used,       &ClickSetProdCheat       },
    00173   {SLE_UINT8, STR_CHEAT_SWITCH_CLIMATE,  &_settings_game.game_creation.landscape, &_cheats.switch_climate.been_used,   &ClickChangeClimateCheat },
    00174   {SLE_INT32, STR_CHEAT_CHANGE_DATE,     &_cur_year,                              &_cheats.change_date.been_used,      &ClickChangeDateCheat    },
    00175 };
    00176
    00177 assert_compile(CHT_NUM_CHEATS == lengthof(_cheats_ui));
    00178
    00180 enum CheatWidgets {
    00181   CW_PANEL,
    00182 };
    00183
    00185 static const NWidgetPart _nested_cheat_widgets[] = {
    00186   NWidget(NWID_HORIZONTAL),
    00187     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
    00188     NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_CHEATS, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    00189     NWidget(WWT_SHADEBOX, COLOUR_GREY),
    00190     NWidget(WWT_STICKYBOX, COLOUR_GREY),
    00191   EndContainer(),
    00192   NWidget(WWT_PANEL, COLOUR_GREY, CW_PANEL), SetDataTip(0x0, STR_CHEATS_TOOLTIP), EndContainer(),
    00193 };
    00194
    00196 struct CheatWindow : Window {
    00197   int clicked;
    00198   int header_height;
    00199
    00200   CheatWindow(const WindowDesc *desc) : Window()
    00201   {
    00202     this->InitNested(desc);
    00203   }
    00204
    00205   virtual void DrawWidget(const Rect &r, int widget) const
    00206   {
    00207     if (widget != CW_PANEL) return;
    00208
    00209     int y = r.top + WD_FRAMERECT_TOP + this->header_height;
    00210     DrawStringMultiLine(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, r.top + WD_FRAMERECT_TOP, y, STR_CHEATS_WARNING, TC_FROMSTRING, SA_CENTER);
    00211
    00212     bool rtl = _current_text_dir == TD_RTL;
    00213     uint box_left    = rtl ? r.right - 12 : r.left + 5;
    00214     uint button_left = rtl ? r.right - 40 : r.left + 20;
    00215     uint text_left   = r.left + (rtl ? WD_FRAMERECT_LEFT: 50);
    00216     uint text_right  = r.right - (rtl ? 50 : WD_FRAMERECT_RIGHT);
    00217
    00218     for (int i = 0; i != lengthof(_cheats_ui); i++) {
    00219       const CheatEntry *ce = &_cheats_ui[i];
    00220
    00221       DrawSprite((*ce->been_used) ? SPR_BOX_CHECKED : SPR_BOX_EMPTY, PAL_NONE, box_left, y + 2);
    00222
    00223       switch (ce->type) {
    00224         case SLE_BOOL: {
    00225           bool on = (*(bool*)ce->variable);
    00226
    00227           DrawFrameRect(button_left, y + 1, button_left + 20 - 1, y + FONT_HEIGHT_NORMAL - 1, on ? COLOUR_GREEN : COLOUR_RED, on ? FR_LOWERED : FR_NONE);
    00228           SetDParam(0, on ? STR_CONFIG_SETTING_ON : STR_CONFIG_SETTING_OFF);
    00229           break;
    00230         }
    00231
    00232         default: {
    00233           int32 val = (int32)ReadValue(ce->variable, ce->type);
    00234           char buf[512];
    00235
    00236           /* Draw [<][>] boxes for settings of an integer-type */
    00237           DrawArrowButtons(button_left, y, COLOUR_YELLOW, clicked - (i * 2), true, true);
    00238
    00239           switch (ce->str) {
    00240             /* Display date for change date cheat */
    00241             case STR_CHEAT_CHANGE_DATE: SetDParam(0, _date); break;
    00242
    00243             /* Draw coloured flag for change company cheat */
    00244             case STR_CHEAT_CHANGE_COMPANY: {
    00245               SetDParam(0, val + 1);
    00246               GetString(buf, STR_CHEAT_CHANGE_COMPANY, lastof(buf));
    00247               uint offset = 10 + GetStringBoundingBox(buf).width;
    00248               DrawCompanyIcon(_local_company, rtl ? text_right - offset - 10 : text_left + offset, y + 2);
    00249               break;
    00250             }
    00251
    00252             /* Set correct string for switch climate cheat */
    00253             case STR_CHEAT_SWITCH_CLIMATE: val += STR_CHEAT_SWITCH_CLIMATE_TEMPERATE_LANDSCAPE;
    00254               /* FALL THROUGH */
    00255
    00256             default: SetDParam(0, val);
    00257           }
    00258           break;
    00259         }
    00260       }
    00261
    00262       DrawString(text_left, text_right, y + 1, ce->str);
    00263
    00264       y += FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
    00265     }
    00266   }
    00267
    00268   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    00269   {
    00270     if (widget != CW_PANEL) return;
    00271
    00272     uint width = 0;
    00273     for (int i = 0; i != lengthof(_cheats_ui); i++) {
    00274       const CheatEntry *ce = &_cheats_ui[i];
    00275       switch (ce->type) {
    00276         case SLE_BOOL:
    00277           SetDParam(0, STR_CONFIG_SETTING_ON);
    00278           width = max(width, GetStringBoundingBox(ce->str).width);
    00279           SetDParam(0, STR_CONFIG_SETTING_OFF);
    00280           width = max(width, GetStringBoundingBox(ce->str).width);
    00281           break;
    00282
    00283         default:
    00284           switch (ce->str) {
    00285             /* Display date for change date cheat */
    00286             case STR_CHEAT_CHANGE_DATE:
    00287               SetDParam(0, ConvertYMDToDate(MAX_YEAR, 11, 31));
    00288               width = max(width, GetStringBoundingBox(ce->str).width);
    00289               break;
    00290
    00291             /* Draw coloured flag for change company cheat */
    00292             case STR_CHEAT_CHANGE_COMPANY:
    00293               SetDParam(0, 15);
    00294               width = max(width, GetStringBoundingBox(ce->str).width + 10 + 10);
    00295               break;
    00296
    00297             /* Set correct string for switch climate cheat */
    00298             case STR_CHEAT_SWITCH_CLIMATE:
    00299               for (StringID i = STR_CHEAT_SWITCH_CLIMATE_TEMPERATE_LANDSCAPE; i <= STR_CHEAT_SWITCH_CLIMATE_TOYLAND_LANDSCAPE; i++) {
    00300                 SetDParam(0, i);
    00301                 width = max(width, GetStringBoundingBox(ce->str).width);
    00302               }
    00303               break;
    00304
    00305             default:
    00306               SetDParam(0, INT64_MAX);
    00307               width = max(width, GetStringBoundingBox(ce->str).width);
    00308               break;
    00309           }
    00310           break;
    00311       }
    00312     }
    00313
    00314     size->width = width + 50 /* stuff on the left */ + 10 /* extra spacing on right */;
    00315     this->header_height = GetStringHeight(STR_CHEATS_WARNING, size->width - WD_FRAMERECT_LEFT - WD_FRAMERECT_RIGHT) + WD_PAR_VSEP_WIDE;
    00316     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);
    00317   }
    00318
    00319   virtual void OnClick(Point pt, int widget, int click_count)
    00320   {
    00321     const NWidgetBase *wid = this->GetWidget<NWidgetBase>(CW_PANEL);
    00322     uint btn = (pt.y - wid->pos_y - WD_FRAMERECT_TOP - this->header_height) / (FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL);
    00323     uint x = pt.x - wid->pos_x;
    00324     bool rtl = _current_text_dir == TD_RTL;
    00325     if (rtl) x = wid->current_x - x;
    00326
    00327     if (btn >= lengthof(_cheats_ui)) return;
    00328
    00329     const CheatEntry *ce = &_cheats_ui[btn];
    00330     int value = (int32)ReadValue(ce->variable, ce->type);
    00331     int oldvalue = value;
    00332
    00333     if (btn == CHT_CHANGE_DATE && x >= 40) {
    00334       /* Click at the date text directly. */
    00335       SetDParam(0, value);
    00336       ShowQueryString(STR_JUST_INT, STR_CHEAT_CHANGE_DATE_QUERY_CAPT, 8, 100, this, CS_NUMERAL, QSF_ACCEPT_UNCHANGED);
    00337       return;
    00338     }
    00339
    00340     /* Not clicking a button? */
    00341     if (!IsInsideMM(x, 20, 40)) return;
    00342
    00343     *ce->been_used = true;
    00344
    00345     switch (ce->type) {
    00346       case SLE_BOOL:
    00347         value ^= 1;
    00348         if (ce->proc != NULL) ce->proc(value, 0);
    00349         break;
    00350
    00351       default:
    00352         /* Take whatever the function returns */
    00353         value = ce->proc(value + ((x >= 30) ? 1 : -1), (x >= 30) ? 1 : -1);
    00354
    00355         /* The first cheat (money), doesn't return a different value. */
    00356         if (value != oldvalue || btn == CHT_MONEY) this->clicked = btn * 2 + 1 + ((x >= 30) != rtl ? 1 : 0);
    00357         break;
    00358     }
    00359
    00360     if (value != oldvalue) WriteValue(ce->variable, ce->type, (int64)value);
    00361
    00362     this->flags4 |= WF_TIMEOUT_BEGIN;
    00363
    00364     this->SetDirty();
    00365   }
    00366
    00367   virtual void OnTimeout()
    00368   {
    00369     this->clicked = 0;
    00370     this->SetDirty();
    00371   }
    00372
    00373   virtual void OnQueryTextFinished(char *str)
    00374   {
    00375     /* Was 'cancel' pressed or nothing entered? */
    00376     if (str == NULL || StrEmpty(str)) return;
    00377
    00378     const CheatEntry *ce = &_cheats_ui[CHT_CHANGE_DATE];
    00379     int oldvalue = (int32)ReadValue(ce->variable, ce->type);
    00380     int value = atoi(str);
    00381     *ce->been_used = true;
    00382     value = ce->proc(value, value - oldvalue);
    00383
    00384     if (value != oldvalue) WriteValue(ce->variable, ce->type, (int64)value);
    00385     this->SetDirty();
    00386   }
    00387 };
    00388
    00390 static const WindowDesc _cheats_desc(
    00391   WDP_AUTO, 0, 0,
    00392   WC_CHEATS, WC_NONE,
    00393   WDF_UNCLICK_BUTTONS,
    00394   _nested_cheat_widgets, lengthof(_nested_cheat_widgets)
    00395 );
    00396
    00398 void ShowCheatWindow()
    00399 {
    00400   DeleteWindowById(WC_CHEATS, 0);
    00401   new CheatWindow(&_cheats_desc);
    00402 }