cheat_gui.cpp

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