cheat_gui.cpp

Go to the documentation of this file.
00001 /* $Id: cheat_gui.cpp 15428 2009-02-09 02:57:15Z rubidium $ */
00002 
00005 #include "stdafx.h"
00006 #include "command_func.h"
00007 #include "cheat_type.h"
00008 #include "company_base.h"
00009 #include "company_func.h"
00010 #include "gfx_func.h"
00011 #include "date_func.h"
00012 #include "saveload/saveload.h"
00013 #include "window_gui.h"
00014 #include "newgrf.h"
00015 #include "settings_type.h"
00016 #include "strings_func.h"
00017 #include "window_func.h"
00018 #include "rail_gui.h"
00019 #include "gui.h"
00020 #include "company_gui.h"
00021 #include "gamelog.h"
00022 
00023 #include "table/strings.h"
00024 #include "table/sprites.h"
00025 
00026 
00032 static int32 _money_cheat_amount = 10000000;
00033 
00034 static int32 ClickMoneyCheat(int32 p1, int32 p2)
00035 {
00036   DoCommandP(0, (uint32)(p2 * _money_cheat_amount), 0, CMD_MONEY_CHEAT);
00037   return _money_cheat_amount;
00038 }
00039 
00044 static int32 ClickChangeCompanyCheat(int32 p1, int32 p2)
00045 {
00046   while ((uint)p1 < GetCompanyPoolSize()) {
00047     if (IsValidCompanyID((CompanyID)p1)) {
00048       SetLocalCompany((CompanyID)p1);
00049       return _local_company;
00050     }
00051     p1 += p2;
00052   }
00053 
00054   return _local_company;
00055 }
00056 
00061 static int32 ClickChangeClimateCheat(int32 p1, int32 p2)
00062 {
00063   if (p1 == -1) p1 = 3;
00064   if (p1 ==  4) p1 = 0;
00065   _settings_game.game_creation.landscape = p1;
00066 
00067   GamelogStartAction(GLAT_CHEAT);
00068   GamelogTestMode();
00069   ReloadNewGRFData();
00070   GamelogStopAction();
00071 
00072   return _settings_game.game_creation.landscape;
00073 }
00074 
00075 extern void EnginesMonthlyLoop();
00076 
00081 static int32 ClickChangeDateCheat(int32 p1, int32 p2)
00082 {
00083   YearMonthDay ymd;
00084   ConvertDateToYMD(_date, &ymd);
00085 
00086   if ((ymd.year == MIN_YEAR && p2 == -1) || (ymd.year == MAX_YEAR && p2 == 1)) return _cur_year;
00087 
00088   SetDate(ConvertYMDToDate(_cur_year + p2, ymd.month, ymd.day));
00089   EnginesMonthlyLoop();
00090   SetWindowDirty(FindWindowById(WC_STATUS_BAR, 0));
00091   ResetSignalVariant();
00092   return _cur_year;
00093 }
00094 
00095 typedef int32 CheckButtonClick(int32, int32);
00096 
00097 struct CheatEntry {
00098   VarType type;          
00099   StringID str;          
00100   void *variable;        
00101   bool *been_used;       
00102   CheckButtonClick *proc;
00103 };
00104 
00105 static const CheatEntry _cheats_ui[] = {
00106   {SLE_INT32, STR_CHEAT_MONEY,           &_money_cheat_amount,                    &_cheats.money.been_used,            &ClickMoneyCheat         },
00107   {SLE_UINT8, STR_CHEAT_CHANGE_COMPANY,  &_local_company,                         &_cheats.switch_company.been_used,   &ClickChangeCompanyCheat },
00108   {SLE_BOOL,  STR_CHEAT_EXTRA_DYNAMITE,  &_cheats.magic_bulldozer.value,          &_cheats.magic_bulldozer.been_used,  NULL                     },
00109   {SLE_BOOL,  STR_CHEAT_CROSSINGTUNNELS, &_cheats.crossing_tunnels.value,         &_cheats.crossing_tunnels.been_used, NULL                     },
00110   {SLE_BOOL,  STR_CHEAT_BUILD_IN_PAUSE,  &_cheats.build_in_pause.value,           &_cheats.build_in_pause.been_used,   NULL                     },
00111   {SLE_BOOL,  STR_CHEAT_NO_JETCRASH,     &_cheats.no_jetcrash.value,              &_cheats.no_jetcrash.been_used,      NULL                     },
00112   {SLE_BOOL,  STR_CHEAT_SETUP_PROD,      &_cheats.setup_prod.value,               &_cheats.setup_prod.been_used,       NULL                     },
00113   {SLE_UINT8, STR_CHEAT_SWITCH_CLIMATE,  &_settings_game.game_creation.landscape, &_cheats.switch_climate.been_used,   &ClickChangeClimateCheat },
00114   {SLE_INT32, STR_CHEAT_CHANGE_DATE,     &_cur_year,                              &_cheats.change_date.been_used,      &ClickChangeDateCheat    },
00115 };
00116 
00117 
00118 static const Widget _cheat_widgets[] = {
00119 {   WWT_CLOSEBOX,   RESIZE_NONE,  COLOUR_GREY,     0,    10,     0,    13, STR_00C5,   STR_018B_CLOSE_WINDOW},
00120 {    WWT_CAPTION,   RESIZE_NONE,  COLOUR_GREY,    11,   399,     0,    13, STR_CHEATS, STR_018C_WINDOW_TITLE_DRAG_THIS},
00121 {      WWT_PANEL,   RESIZE_NONE,  COLOUR_GREY,     0,   399,    14,   169, 0x0,        STR_NULL},
00122 {      WWT_PANEL,   RESIZE_NONE,  COLOUR_GREY,     0,   399,    14,   169, 0x0,        STR_CHEATS_TIP},
00123 {   WIDGETS_END},
00124 };
00125 
00126 struct CheatWindow : Window {
00127   int clicked;
00128 
00129   CheatWindow(const WindowDesc *desc) : Window(desc)
00130   {
00131     this->FindWindowPlacementAndResize(desc);
00132   }
00133 
00134   virtual void OnPaint()
00135   {
00136     this->DrawWidgets();
00137     DrawStringMultiCenter(200, 25, STR_CHEATS_WARNING, width - 50);
00138 
00139     for (int i = 0, x = 0, y = 45; i != lengthof(_cheats_ui); i++) {
00140       const CheatEntry *ce = &_cheats_ui[i];
00141 
00142       DrawSprite((*ce->been_used) ? SPR_BOX_CHECKED : SPR_BOX_EMPTY, PAL_NONE, x + 5, y + 2);
00143 
00144       switch (ce->type) {
00145         case SLE_BOOL: {
00146           bool on = (*(bool*)ce->variable);
00147 
00148           DrawFrameRect(x + 20, y + 1, x + 30 + 9, y + 9, on ? COLOUR_GREEN : COLOUR_RED, on ? FR_LOWERED : FR_NONE);
00149           SetDParam(0, on ? STR_CONFIG_SETTING_ON : STR_CONFIG_SETTING_OFF);
00150         } break;
00151 
00152         default: {
00153           int32 val = (int32)ReadValue(ce->variable, ce->type);
00154           char buf[512];
00155 
00156           /* Draw [<][>] boxes for settings of an integer-type */
00157           DrawArrowButtons(x + 20, y, COLOUR_YELLOW, clicked - (i * 2), true, true);
00158 
00159           switch (ce->str) {
00160             /* Display date for change date cheat */
00161             case STR_CHEAT_CHANGE_DATE: SetDParam(0, _date); break;
00162 
00163             /* Draw coloured flag for change company cheat */
00164             case STR_CHEAT_CHANGE_COMPANY:
00165               SetDParam(0, val + 1);
00166               GetString(buf, STR_CHEAT_CHANGE_COMPANY, lastof(buf));
00167               DrawCompanyIcon(_local_company, 60 + GetStringBoundingBox(buf).width, y + 2);
00168               break;
00169 
00170             /* Set correct string for switch climate cheat */
00171             case STR_CHEAT_SWITCH_CLIMATE: val += STR_TEMPERATE_LANDSCAPE;
00172 
00173             /* Fallthrough */
00174             default: SetDParam(0, val);
00175           }
00176         } break;
00177       }
00178 
00179       DrawString(50, y + 1, ce->str, TC_FROMSTRING);
00180 
00181       y += 12;
00182     }
00183   }
00184 
00185   virtual void OnClick(Point pt, int widget)
00186   {
00187     uint btn = (pt.y - 46) / 12;
00188     uint x = pt.x;
00189 
00190     /* Not clicking a button? */
00191     if (!IsInsideMM(x, 20, 40) || btn >= lengthof(_cheats_ui)) return;
00192 
00193     const CheatEntry *ce = &_cheats_ui[btn];
00194     int value = (int32)ReadValue(ce->variable, ce->type);
00195     int oldvalue = value;
00196 
00197     *ce->been_used = true;
00198 
00199     switch (ce->type) {
00200       case SLE_BOOL:
00201         value ^= 1;
00202         if (ce->proc != NULL) ce->proc(value, 0);
00203         break;
00204 
00205       default:
00206         /* Take whatever the function returns */
00207         value = ce->proc(value + ((x >= 30) ? 1 : -1), (x >= 30) ? 1 : -1);
00208 
00209         /* The first cheat (money), doesn't return a different value. */
00210         if (value != oldvalue || btn == 0) this->clicked = btn * 2 + 1 + ((x >= 30) ? 1 : 0);
00211         break;
00212     }
00213 
00214     if (value != oldvalue) WriteValue(ce->variable, ce->type, (int64)value);
00215 
00216     this->flags4 |= WF_TIMEOUT_BEGIN;
00217 
00218     SetDirty();
00219   }
00220 
00221   virtual void OnTimeout()
00222   {
00223     this->clicked = 0;
00224     this->SetDirty();
00225   }
00226 };
00227 
00228 static const WindowDesc _cheats_desc = {
00229   240, 22, 400, 170, 400, 170,
00230   WC_CHEATS, WC_NONE,
00231   WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS,
00232   _cheat_widgets,
00233 };
00234 
00235 
00236 void ShowCheatWindow()
00237 {
00238   DeleteWindowById(WC_CHEATS, 0);
00239   new CheatWindow(&_cheats_desc);
00240 }

Generated on Mon Feb 16 23:12:06 2009 for openttd by  doxygen 1.5.6