cheat_gui.cpp

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

Generated on Fri Nov 21 19:01:31 2008 for openttd by  doxygen 1.5.6