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