cheat_gui.cpp

Go to the documentation of this file.
00001 /* $Id: cheat_gui.cpp 25867 2013-10-15 17:32:31Z fonsinchen $ */
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 "string_func.h"
00022 #include "strings_func.h"
00023 #include "window_func.h"
00024 #include "rail_gui.h"
00025 #include "settings_gui.h"
00026 #include "company_gui.h"
00027 #include "linkgraph/linkgraphschedule.h"
00028 
00029 #include "widgets/cheat_widget.h"
00030 
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 
00088 extern void EnginesMonthlyLoop();
00089 
00096 static int32 ClickChangeDateCheat(int32 p1, int32 p2)
00097 {
00098   YearMonthDay ymd;
00099   ConvertDateToYMD(_date, &ymd);
00100 
00101   p1 = Clamp(p1, MIN_YEAR, MAX_YEAR);
00102   if (p1 == _cur_year) return _cur_year;
00103 
00104   Date new_date = ConvertYMDToDate(p1, ymd.month, ymd.day);
00105   LinkGraphSchedule::Instance()->ShiftDates(new_date - _date);
00106   SetDate(new_date, _date_fract);
00107   EnginesMonthlyLoop();
00108   SetWindowDirty(WC_STATUS_BAR, 0);
00109   InvalidateWindowClassesData(WC_BUILD_STATION, 0);
00110   InvalidateWindowClassesData(WC_BUILD_OBJECT, 0);
00111   ResetSignalVariant();
00112   return _cur_year;
00113 }
00114 
00116 enum CheatNumbers {
00117   CHT_MONEY,           
00118   CHT_CHANGE_COMPANY,  
00119   CHT_EXTRA_DYNAMITE,  
00120   CHT_CROSSINGTUNNELS, 
00121   CHT_NO_JETCRASH,     
00122   CHT_SETUP_PROD,      
00123   CHT_CHANGE_DATE,     
00124 
00125   CHT_NUM_CHEATS,      
00126 };
00127 
00133 typedef int32 CheckButtonClick(int32 p1, int32 p2);
00134 
00136 struct CheatEntry {
00137   VarType type;          
00138   StringID str;          
00139   void *variable;        
00140   bool *been_used;       
00141   CheckButtonClick *proc;
00142 };
00143 
00148 static const CheatEntry _cheats_ui[] = {
00149   {SLE_INT32, STR_CHEAT_MONEY,           &_money_cheat_amount,                    &_cheats.money.been_used,            &ClickMoneyCheat         },
00150   {SLE_UINT8, STR_CHEAT_CHANGE_COMPANY,  &_local_company,                         &_cheats.switch_company.been_used,   &ClickChangeCompanyCheat },
00151   {SLE_BOOL,  STR_CHEAT_EXTRA_DYNAMITE,  &_cheats.magic_bulldozer.value,          &_cheats.magic_bulldozer.been_used,  NULL                     },
00152   {SLE_BOOL,  STR_CHEAT_CROSSINGTUNNELS, &_cheats.crossing_tunnels.value,         &_cheats.crossing_tunnels.been_used, NULL                     },
00153   {SLE_BOOL,  STR_CHEAT_NO_JETCRASH,     &_cheats.no_jetcrash.value,              &_cheats.no_jetcrash.been_used,      NULL                     },
00154   {SLE_BOOL,  STR_CHEAT_SETUP_PROD,      &_cheats.setup_prod.value,               &_cheats.setup_prod.been_used,       &ClickSetProdCheat       },
00155   {SLE_INT32, STR_CHEAT_CHANGE_DATE,     &_cur_year,                              &_cheats.change_date.been_used,      &ClickChangeDateCheat    },
00156 };
00157 
00158 assert_compile(CHT_NUM_CHEATS == lengthof(_cheats_ui));
00159 
00161 static const NWidgetPart _nested_cheat_widgets[] = {
00162   NWidget(NWID_HORIZONTAL),
00163     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
00164     NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_CHEATS, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00165     NWidget(WWT_SHADEBOX, COLOUR_GREY),
00166     NWidget(WWT_STICKYBOX, COLOUR_GREY),
00167   EndContainer(),
00168   NWidget(WWT_PANEL, COLOUR_GREY, WID_C_PANEL), SetDataTip(0x0, STR_CHEATS_TOOLTIP), EndContainer(),
00169 };
00170 
00172 struct CheatWindow : Window {
00173   int clicked;
00174   int header_height;
00175 
00176   CheatWindow(WindowDesc *desc) : Window(desc)
00177   {
00178     this->InitNested();
00179   }
00180 
00181   virtual void DrawWidget(const Rect &r, int widget) const
00182   {
00183     if (widget != WID_C_PANEL) return;
00184 
00185     int y = r.top + WD_FRAMERECT_TOP + this->header_height;
00186     DrawStringMultiLine(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, r.top + WD_FRAMERECT_TOP, y, STR_CHEATS_WARNING, TC_FROMSTRING, SA_CENTER);
00187 
00188     bool rtl = _current_text_dir == TD_RTL;
00189     uint box_left    = rtl ? r.right - 12 : r.left + 5;
00190     uint button_left = rtl ? r.right - 20 - SETTING_BUTTON_WIDTH : r.left + 20;
00191     uint text_left   = r.left + (rtl ? WD_FRAMERECT_LEFT : 30 + SETTING_BUTTON_WIDTH);
00192     uint text_right  = r.right - (rtl ? 30 + SETTING_BUTTON_WIDTH : WD_FRAMERECT_RIGHT);
00193 
00194     for (int i = 0; i != lengthof(_cheats_ui); i++) {
00195       const CheatEntry *ce = &_cheats_ui[i];
00196 
00197       DrawSprite((*ce->been_used) ? SPR_BOX_CHECKED : SPR_BOX_EMPTY, PAL_NONE, box_left, y + 2);
00198 
00199       switch (ce->type) {
00200         case SLE_BOOL: {
00201           bool on = (*(bool*)ce->variable);
00202 
00203           DrawBoolButton(button_left, y, on, true);
00204           SetDParam(0, on ? STR_CONFIG_SETTING_ON : STR_CONFIG_SETTING_OFF);
00205           break;
00206         }
00207 
00208         default: {
00209           int32 val = (int32)ReadValue(ce->variable, ce->type);
00210           char buf[512];
00211 
00212           /* Draw [<][>] boxes for settings of an integer-type */
00213           DrawArrowButtons(button_left, y, COLOUR_YELLOW, clicked - (i * 2), true, true);
00214 
00215           switch (ce->str) {
00216             /* Display date for change date cheat */
00217             case STR_CHEAT_CHANGE_DATE: SetDParam(0, _date); break;
00218 
00219             /* Draw coloured flag for change company cheat */
00220             case STR_CHEAT_CHANGE_COMPANY: {
00221               SetDParam(0, val + 1);
00222               GetString(buf, STR_CHEAT_CHANGE_COMPANY, lastof(buf));
00223               uint offset = 10 + GetStringBoundingBox(buf).width;
00224               DrawCompanyIcon(_local_company, rtl ? text_right - offset - 10 : text_left + offset, y + 2);
00225               break;
00226             }
00227 
00228             default: SetDParam(0, val);
00229           }
00230           break;
00231         }
00232       }
00233 
00234       DrawString(text_left, text_right, y + 1, ce->str);
00235 
00236       y += FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
00237     }
00238   }
00239 
00240   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00241   {
00242     if (widget != WID_C_PANEL) return;
00243 
00244     uint width = 0;
00245     for (int i = 0; i != lengthof(_cheats_ui); i++) {
00246       const CheatEntry *ce = &_cheats_ui[i];
00247       switch (ce->type) {
00248         case SLE_BOOL:
00249           SetDParam(0, STR_CONFIG_SETTING_ON);
00250           width = max(width, GetStringBoundingBox(ce->str).width);
00251           SetDParam(0, STR_CONFIG_SETTING_OFF);
00252           width = max(width, GetStringBoundingBox(ce->str).width);
00253           break;
00254 
00255         default:
00256           switch (ce->str) {
00257             /* Display date for change date cheat */
00258             case STR_CHEAT_CHANGE_DATE:
00259               SetDParam(0, ConvertYMDToDate(MAX_YEAR, 11, 31));
00260               width = max(width, GetStringBoundingBox(ce->str).width);
00261               break;
00262 
00263             /* Draw coloured flag for change company cheat */
00264             case STR_CHEAT_CHANGE_COMPANY:
00265               SetDParamMaxValue(0, MAX_COMPANIES);
00266               width = max(width, GetStringBoundingBox(ce->str).width + 10 + 10);
00267               break;
00268 
00269             default:
00270               SetDParam(0, INT64_MAX);
00271               width = max(width, GetStringBoundingBox(ce->str).width);
00272               break;
00273           }
00274           break;
00275       }
00276     }
00277 
00278     size->width = width + 50 /* stuff on the left */ + 10 /* extra spacing on right */;
00279     this->header_height = GetStringHeight(STR_CHEATS_WARNING, size->width - WD_FRAMERECT_LEFT - WD_FRAMERECT_RIGHT) + WD_PAR_VSEP_WIDE;
00280     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);
00281   }
00282 
00283   virtual void OnClick(Point pt, int widget, int click_count)
00284   {
00285     const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_C_PANEL);
00286     uint btn = (pt.y - wid->pos_y - WD_FRAMERECT_TOP - this->header_height) / (FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL);
00287     uint x = pt.x - wid->pos_x;
00288     bool rtl = _current_text_dir == TD_RTL;
00289     if (rtl) x = wid->current_x - x;
00290 
00291     if (btn >= lengthof(_cheats_ui)) return;
00292 
00293     const CheatEntry *ce = &_cheats_ui[btn];
00294     int value = (int32)ReadValue(ce->variable, ce->type);
00295     int oldvalue = value;
00296 
00297     if (btn == CHT_CHANGE_DATE && x >= 20 + SETTING_BUTTON_WIDTH) {
00298       /* Click at the date text directly. */
00299       SetDParam(0, value);
00300       ShowQueryString(STR_JUST_INT, STR_CHEAT_CHANGE_DATE_QUERY_CAPT, 8, this, CS_NUMERAL, QSF_ACCEPT_UNCHANGED);
00301       return;
00302     }
00303 
00304     /* Not clicking a button? */
00305     if (!IsInsideMM(x, 20, 20 + SETTING_BUTTON_WIDTH)) return;
00306 
00307     *ce->been_used = true;
00308 
00309     switch (ce->type) {
00310       case SLE_BOOL:
00311         value ^= 1;
00312         if (ce->proc != NULL) ce->proc(value, 0);
00313         break;
00314 
00315       default:
00316         /* Take whatever the function returns */
00317         value = ce->proc(value + ((x >= 20 + SETTING_BUTTON_WIDTH / 2) ? 1 : -1), (x >= 20 + SETTING_BUTTON_WIDTH / 2) ? 1 : -1);
00318 
00319         /* The first cheat (money), doesn't return a different value. */
00320         if (value != oldvalue || btn == CHT_MONEY) this->clicked = btn * 2 + 1 + ((x >= 20 + SETTING_BUTTON_WIDTH / 2) != rtl ? 1 : 0);
00321         break;
00322     }
00323 
00324     if (value != oldvalue) WriteValue(ce->variable, ce->type, (int64)value);
00325 
00326     this->SetTimeout();
00327 
00328     this->SetDirty();
00329   }
00330 
00331   virtual void OnTimeout()
00332   {
00333     this->clicked = 0;
00334     this->SetDirty();
00335   }
00336 
00337   virtual void OnQueryTextFinished(char *str)
00338   {
00339     /* Was 'cancel' pressed or nothing entered? */
00340     if (str == NULL || StrEmpty(str)) return;
00341 
00342     const CheatEntry *ce = &_cheats_ui[CHT_CHANGE_DATE];
00343     int oldvalue = (int32)ReadValue(ce->variable, ce->type);
00344     int value = atoi(str);
00345     *ce->been_used = true;
00346     value = ce->proc(value, value - oldvalue);
00347 
00348     if (value != oldvalue) WriteValue(ce->variable, ce->type, (int64)value);
00349     this->SetDirty();
00350   }
00351 };
00352 
00354 static WindowDesc _cheats_desc(
00355   WDP_AUTO, "cheats", 0, 0,
00356   WC_CHEATS, WC_NONE,
00357   0,
00358   _nested_cheat_widgets, lengthof(_nested_cheat_widgets)
00359 );
00360 
00362 void ShowCheatWindow()
00363 {
00364   DeleteWindowById(WC_CHEATS, 0);
00365   new CheatWindow(&_cheats_desc);
00366 }