# cheat_gui.cpp

Go to the documentation of this file.

    00001 /* $Id: cheat_gui.cpp 18966 2010-01-30 18:34:48Z frosch $ */
    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 "gfx_func.h"
    00018 #include "date_func.h"
    00019 #include "saveload/saveload.h"
    00020 #include "window_gui.h"
    00021 #include "newgrf.h"
    00022 #include "strings_func.h"
    00023 #include "window_func.h"
    00024 #include "rail_gui.h"
    00025 #include "gui.h"
    00026 #include "company_gui.h"
    00027 #include "gamelog.h"
    00028
    00029 #include "table/strings.h"
    00030 #include "table/sprites.h"
    00031
    00032
    00038 static int32 _money_cheat_amount = 10000000;
    00039
    00040 static int32 ClickMoneyCheat(int32 p1, int32 p2)
    00041 {
    00042   DoCommandP(0, (uint32)(p2 * _money_cheat_amount), 0, CMD_MONEY_CHEAT);
    00043   return _money_cheat_amount;
    00044 }
    00045
    00050 static int32 ClickChangeCompanyCheat(int32 p1, int32 p2)
    00051 {
    00052   while ((uint)p1 < Company::GetPoolSize()) {
    00053     if (Company::IsValidID((CompanyID)p1)) {
    00054       SetLocalCompany((CompanyID)p1);
    00055       return _local_company;
    00056     }
    00057     p1 += p2;
    00058   }
    00059
    00060   return _local_company;
    00061 }
    00062
    00067 static int32 ClickSetProdCheat(int32 p1, int32 p2)
    00068 {
    00069   SetWindowClassesDirty(WC_INDUSTRY_VIEW);
    00070   return p1;
    00071 }
    00072
    00077 static int32 ClickChangeClimateCheat(int32 p1, int32 p2)
    00078 {
    00079   if (p1 == -1) p1 = 3;
    00080   if (p1 ==  4) p1 = 0;
    00081   _settings_game.game_creation.landscape = p1;
    00082
    00083   GamelogStartAction(GLAT_CHEAT);
    00084   GamelogTestMode();
    00085   ReloadNewGRFData();
    00086   GamelogStopAction();
    00087
    00088   return _settings_game.game_creation.landscape;
    00089 }
    00090
    00091 extern void EnginesMonthlyLoop();
    00092
    00097 static int32 ClickChangeDateCheat(int32 p1, int32 p2)
    00098 {
    00099   YearMonthDay ymd;
    00100   ConvertDateToYMD(_date, &ymd);
    00101
    00102   if ((ymd.year == MIN_YEAR && p2 == -1) || (ymd.year == MAX_YEAR && p2 == 1)) return _cur_year;
    00103
    00104   SetDate(ConvertYMDToDate(_cur_year + p2, ymd.month, ymd.day));
    00105   EnginesMonthlyLoop();
    00106   SetWindowDirty(WC_STATUS_BAR, 0);
    00107   InvalidateWindowClassesData(WC_BUILD_STATION, 0);
    00108   ResetSignalVariant();
    00109   return _cur_year;
    00110 }
    00111
    00112 typedef int32 CheckButtonClick(int32, int32);
    00113
    00114 struct CheatEntry {
    00115   VarType type;
    00116   StringID str;
    00117   void *variable;
    00118   bool *been_used;
    00119   CheckButtonClick *proc;
    00120 };
    00121
    00122 static const CheatEntry _cheats_ui[] = {
    00123   {SLE_INT32, STR_CHEAT_MONEY,           &_money_cheat_amount,                    &_cheats.money.been_used,            &ClickMoneyCheat         },
    00124   {SLE_UINT8, STR_CHEAT_CHANGE_COMPANY,  &_local_company,                         &_cheats.switch_company.been_used,   &ClickChangeCompanyCheat },
    00125   {SLE_BOOL,  STR_CHEAT_EXTRA_DYNAMITE,  &_cheats.magic_bulldozer.value,          &_cheats.magic_bulldozer.been_used,  NULL                     },
    00126   {SLE_BOOL,  STR_CHEAT_CROSSINGTUNNELS, &_cheats.crossing_tunnels.value,         &_cheats.crossing_tunnels.been_used, NULL                     },
    00127   {SLE_BOOL,  STR_CHEAT_BUILD_IN_PAUSE,  &_cheats.build_in_pause.value,           &_cheats.build_in_pause.been_used,   NULL                     },
    00128   {SLE_BOOL,  STR_CHEAT_NO_JETCRASH,     &_cheats.no_jetcrash.value,              &_cheats.no_jetcrash.been_used,      NULL                     },
    00129   {SLE_BOOL,  STR_CHEAT_SETUP_PROD,      &_cheats.setup_prod.value,               &_cheats.setup_prod.been_used,       &ClickSetProdCheat       },
    00130   {SLE_UINT8, STR_CHEAT_SWITCH_CLIMATE,  &_settings_game.game_creation.landscape, &_cheats.switch_climate.been_used,   &ClickChangeClimateCheat },
    00131   {SLE_INT32, STR_CHEAT_CHANGE_DATE,     &_cur_year,                              &_cheats.change_date.been_used,      &ClickChangeDateCheat    },
    00132 };
    00133
    00134 /* Names of the cheat window widgets. */
    00135 enum CheatWidgets {
    00136   CW_PANEL,
    00137 };
    00138
    00139 static const NWidgetPart _nested_cheat_widgets[] = {
    00140   NWidget(NWID_HORIZONTAL),
    00141     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
    00142     NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_CHEATS, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    00143     NWidget(WWT_SHADEBOX, COLOUR_GREY),
    00144     NWidget(WWT_STICKYBOX, COLOUR_GREY),
    00145   EndContainer(),
    00146   NWidget(WWT_PANEL, COLOUR_GREY, CW_PANEL), SetDataTip(0x0, STR_CHEATS_TOOLTIP), EndContainer(),
    00147 };
    00148
    00149 struct CheatWindow : Window {
    00150   int clicked;
    00151   int header_height;
    00152
    00153   CheatWindow(const WindowDesc *desc) : Window()
    00154   {
    00155     this->InitNested(desc);
    00156   }
    00157
    00158   virtual void DrawWidget(const Rect &r, int widget) const
    00159   {
    00160     if (widget != CW_PANEL) return;
    00161
    00162     int y = r.top + WD_FRAMERECT_TOP + this->header_height;
    00163     DrawStringMultiLine(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, r.top + WD_FRAMERECT_TOP, y, STR_CHEATS_WARNING, TC_FROMSTRING, SA_CENTER);
    00164
    00165     bool rtl = _dynlang.text_dir == TD_RTL;
    00166     uint box_left    = rtl ? r.right - 12 : r.left + 5;
    00167     uint button_left = rtl ? r.right - 40 : r.left + 20;
    00168     uint text_left   = r.left + (rtl ? WD_FRAMERECT_LEFT: 50);
    00169     uint text_right  = r.right - (rtl ? 50 : WD_FRAMERECT_RIGHT);
    00170
    00171     for (int i = 0; i != lengthof(_cheats_ui); i++) {
    00172       const CheatEntry *ce = &_cheats_ui[i];
    00173
    00174       DrawSprite((*ce->been_used) ? SPR_BOX_CHECKED : SPR_BOX_EMPTY, PAL_NONE, box_left, y + 2);
    00175
    00176       switch (ce->type) {
    00177         case SLE_BOOL: {
    00178           bool on = (*(bool*)ce->variable);
    00179
    00180           DrawFrameRect(button_left, y + 1, button_left + 20 - 1, y + FONT_HEIGHT_NORMAL - 1, on ? COLOUR_GREEN : COLOUR_RED, on ? FR_LOWERED : FR_NONE);
    00181           SetDParam(0, on ? STR_CONFIG_SETTING_ON : STR_CONFIG_SETTING_OFF);
    00182         } break;
    00183
    00184         default: {
    00185           int32 val = (int32)ReadValue(ce->variable, ce->type);
    00186           char buf[512];
    00187
    00188           /* Draw [<][>] boxes for settings of an integer-type */
    00189           DrawArrowButtons(button_left, y, COLOUR_YELLOW, clicked - (i * 2), true, true);
    00190
    00191           switch (ce->str) {
    00192             /* Display date for change date cheat */
    00193             case STR_CHEAT_CHANGE_DATE: SetDParam(0, _date); break;
    00194
    00195             /* Draw coloured flag for change company cheat */
    00196             case STR_CHEAT_CHANGE_COMPANY: {
    00197               SetDParam(0, val + 1);
    00198               GetString(buf, STR_CHEAT_CHANGE_COMPANY, lastof(buf));
    00199               uint offset = 10 + GetStringBoundingBox(buf).width;
    00200               DrawCompanyIcon(_local_company, rtl ? text_right - offset - 10 : text_left + offset, y + 2);
    00201             } break;
    00202
    00203             /* Set correct string for switch climate cheat */
    00204             case STR_CHEAT_SWITCH_CLIMATE: val += STR_CHEAT_SWITCH_CLIMATE_TEMPERATE_LANDSCAPE;
    00205
    00206             /* Fallthrough */
    00207             default: SetDParam(0, val);
    00208           }
    00209         } break;
    00210       }
    00211
    00212       DrawString(text_left, text_right, y + 1, ce->str);
    00213
    00214       y += FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
    00215     }
    00216   }
    00217
    00218   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    00219   {
    00220     if (widget != CW_PANEL) return;
    00221
    00222     uint width = 0;
    00223     for (int i = 0; i != lengthof(_cheats_ui); i++) {
    00224       const CheatEntry *ce = &_cheats_ui[i];
    00225       switch (ce->type) {
    00226         case SLE_BOOL:
    00227           SetDParam(0, STR_CONFIG_SETTING_ON);
    00228           width = max(width, GetStringBoundingBox(ce->str).width);
    00229           SetDParam(0, STR_CONFIG_SETTING_OFF);
    00230           width = max(width, GetStringBoundingBox(ce->str).width);
    00231           break;
    00232
    00233         default:
    00234           switch (ce->str) {
    00235             /* Display date for change date cheat */
    00236             case STR_CHEAT_CHANGE_DATE:
    00237               SetDParam(0, ConvertYMDToDate(MAX_YEAR, 11, 31));
    00238               width = max(width, GetStringBoundingBox(ce->str).width);
    00239               break;
    00240
    00241             /* Draw coloured flag for change company cheat */
    00242             case STR_CHEAT_CHANGE_COMPANY:
    00243               SetDParam(0, 15);
    00244               width = max(width, GetStringBoundingBox(ce->str).width + 10 + 10);
    00245               break;
    00246
    00247             /* Set correct string for switch climate cheat */
    00248             case STR_CHEAT_SWITCH_CLIMATE:
    00249               for (StringID i = STR_CHEAT_SWITCH_CLIMATE_TEMPERATE_LANDSCAPE; i <= STR_CHEAT_SWITCH_CLIMATE_TOYLAND_LANDSCAPE; i++) {
    00250                 SetDParam(0, i);
    00251                 width = max(width, GetStringBoundingBox(ce->str).width);
    00252               }
    00253               break;
    00254
    00255             default:
    00256               SetDParam(0, INT64_MAX);
    00257               width = max(width, GetStringBoundingBox(ce->str).width);
    00258               break;
    00259           }
    00260           break;
    00261       }
    00262     }
    00263
    00264     size->width = width + 50 /* stuff on the left */ + 10 /* extra spacing on right */;
    00265     this->header_height = GetStringHeight(STR_CHEATS_WARNING, size->width - WD_FRAMERECT_LEFT - WD_FRAMERECT_RIGHT) + WD_PAR_VSEP_WIDE;
    00266     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);
    00267   }
    00268
    00269   virtual void OnPaint()
    00270   {
    00271     this->DrawWidgets();
    00272   }
    00273
    00274   virtual void OnClick(Point pt, int widget, int click_count)
    00275   {
    00276     const NWidgetBase *wid = this->GetWidget<NWidgetBase>(CW_PANEL);
    00277     uint btn = (pt.y - wid->pos_y - WD_FRAMERECT_TOP - this->header_height) / (FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL);
    00278     uint x = pt.x - wid->pos_x;
    00279     bool rtl = _dynlang.text_dir == TD_RTL;
    00280     if (rtl) x = wid->current_x - x;
    00281
    00282     /* Not clicking a button? */
    00283     if (!IsInsideMM(x, 20, 40) || btn >= lengthof(_cheats_ui)) return;
    00284
    00285     const CheatEntry *ce = &_cheats_ui[btn];
    00286     int value = (int32)ReadValue(ce->variable, ce->type);
    00287     int oldvalue = value;
    00288
    00289     *ce->been_used = true;
    00290
    00291     switch (ce->type) {
    00292       case SLE_BOOL:
    00293         value ^= 1;
    00294         if (ce->proc != NULL) ce->proc(value, 0);
    00295         break;
    00296
    00297       default:
    00298         /* Take whatever the function returns */
    00299         value = ce->proc(value + ((x >= 30) ? 1 : -1), (x >= 30) ? 1 : -1);
    00300
    00301         /* The first cheat (money), doesn't return a different value. */
    00302         if (value != oldvalue || btn == 0) this->clicked = btn * 2 + 1 + ((x >= 30) != rtl ? 1 : 0);
    00303         break;
    00304     }
    00305
    00306     if (value != oldvalue) WriteValue(ce->variable, ce->type, (int64)value);
    00307
    00308     this->flags4 |= WF_TIMEOUT_BEGIN;
    00309
    00310     SetDirty();
    00311   }
    00312
    00313   virtual void OnTimeout()
    00314   {
    00315     this->clicked = 0;
    00316     this->SetDirty();
    00317   }
    00318 };
    00319
    00320 static const WindowDesc _cheats_desc(
    00321   WDP_AUTO, 0, 0,
    00322   WC_CHEATS, WC_NONE,
    00323   WDF_UNCLICK_BUTTONS,
    00324   _nested_cheat_widgets, lengthof(_nested_cheat_widgets)
    00325 );
    00326
    00327
    00328 void ShowCheatWindow()
    00329 {
    00330   DeleteWindowById(WC_CHEATS, 0);
    00331   new CheatWindow(&_cheats_desc);
    00332 }