ai_gui.cpp

Go to the documentation of this file.
00001 /* $Id: ai_gui.cpp 22393 2011-04-30 20:50:41Z rubidium $ */
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 "../table/sprites.h"
00014 #include "../gui.h"
00015 #include "../querystring_gui.h"
00016 #include "../company_func.h"
00017 #include "../company_base.h"
00018 #include "../company_gui.h"
00019 #include "../strings_func.h"
00020 #include "../window_func.h"
00021 #include "../gfx_func.h"
00022 #include "../command_func.h"
00023 #include "../network/network.h"
00024 #include "../settings_func.h"
00025 #include "../network/network_content.h"
00026 #include "../core/backup_type.hpp"
00027 
00028 #include "ai.hpp"
00029 #include "api/ai_log.hpp"
00030 #include "ai_config.hpp"
00031 #include "ai_instance.hpp"
00032 
00033 #include "table/strings.h"
00034 
00036 enum AIListWindowWidgets {
00037   AIL_WIDGET_LIST,             
00038   AIL_WIDGET_SCROLLBAR,        
00039   AIL_WIDGET_INFO_BG,          
00040   AIL_WIDGET_ACCEPT,           
00041   AIL_WIDGET_CANCEL,           
00042 };
00043 
00047 struct AIListWindow : public Window {
00048   const AIInfoList *ai_info_list;
00049   int selected;
00050   CompanyID slot;
00051   int line_height; // Height of a row in the matrix widget.
00052   Scrollbar *vscroll;
00053 
00054   AIListWindow(const WindowDesc *desc, CompanyID slot) : Window(),
00055     slot(slot)
00056   {
00057     this->ai_info_list = AI::GetUniqueInfoList();
00058 
00059     this->CreateNestedTree(desc);
00060     this->vscroll = this->GetScrollbar(AIL_WIDGET_SCROLLBAR);
00061     this->FinishInitNested(desc); // Initializes 'this->line_height' as side effect.
00062 
00063     this->vscroll->SetCount((int)this->ai_info_list->size() + 1);
00064 
00065     /* Try if we can find the currently selected AI */
00066     this->selected = -1;
00067     if (AIConfig::GetConfig(slot)->HasAI()) {
00068       AIInfo *info = AIConfig::GetConfig(slot)->GetInfo();
00069       int i = 0;
00070       for (AIInfoList::const_iterator it = this->ai_info_list->begin(); it != this->ai_info_list->end(); it++, i++) {
00071         if ((*it).second == info) {
00072           this->selected = i;
00073           break;
00074         }
00075       }
00076     }
00077   }
00078 
00079   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00080   {
00081     if (widget == AIL_WIDGET_LIST) {
00082       this->line_height = FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM;
00083 
00084       resize->width = 1;
00085       resize->height = this->line_height;
00086       size->height = GB(this->GetWidget<NWidgetCore>(widget)->widget_data, MAT_ROW_START, MAT_ROW_BITS) * this->line_height;
00087     }
00088   }
00089 
00090   virtual void DrawWidget(const Rect &r, int widget) const
00091   {
00092     switch (widget) {
00093       case AIL_WIDGET_LIST: {
00094         /* Draw a list of all available AIs. */
00095         int y = this->GetWidget<NWidgetBase>(AIL_WIDGET_LIST)->pos_y;
00096         /* First AI in the list is hardcoded to random */
00097         if (this->vscroll->IsVisible(0)) {
00098           DrawString(r.left + WD_MATRIX_LEFT, r.right - WD_MATRIX_LEFT, y + WD_MATRIX_TOP, STR_AI_CONFIG_RANDOM_AI, this->selected == -1 ? TC_WHITE : TC_BLACK);
00099           y += this->line_height;
00100         }
00101         AIInfoList::const_iterator it = this->ai_info_list->begin();
00102         for (int i = 1; it != this->ai_info_list->end(); i++, it++) {
00103           if (this->vscroll->IsVisible(i)) {
00104             DrawString(r.left + WD_MATRIX_LEFT, r.right - WD_MATRIX_RIGHT, y + WD_MATRIX_TOP, (*it).second->GetName(), (this->selected == i - 1) ? TC_WHITE : TC_BLACK);
00105             y += this->line_height;
00106           }
00107         }
00108         break;
00109       }
00110       case AIL_WIDGET_INFO_BG: {
00111         AIInfo *selected_info = NULL;
00112         AIInfoList::const_iterator it = this->ai_info_list->begin();
00113         for (int i = 1; selected_info == NULL && it != this->ai_info_list->end(); i++, it++) {
00114           if (this->selected == i - 1) selected_info = (*it).second;
00115         }
00116         /* Some info about the currently selected AI. */
00117         if (selected_info != NULL) {
00118           int y = r.top + WD_FRAMERECT_TOP;
00119           SetDParamStr(0, selected_info->GetAuthor());
00120           DrawString(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, y, STR_AI_LIST_AUTHOR);
00121           y += FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
00122           SetDParam(0, selected_info->GetVersion());
00123           DrawString(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, y, STR_AI_LIST_VERSION);
00124           y += FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
00125           if (selected_info->GetURL() != NULL) {
00126             SetDParamStr(0, selected_info->GetURL());
00127             DrawString(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, y, STR_AI_LIST_URL);
00128             y += FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
00129           }
00130           SetDParamStr(0, selected_info->GetDescription());
00131           DrawStringMultiLine(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, y, r.bottom - WD_FRAMERECT_BOTTOM, STR_JUST_RAW_STRING, TC_BLACK);
00132         }
00133         break;
00134       }
00135     }
00136   }
00137 
00138   void ChangeAI()
00139   {
00140     if (this->selected == -1) {
00141       AIConfig::GetConfig(slot)->ChangeAI(NULL);
00142     } else {
00143       AIInfoList::const_iterator it = this->ai_info_list->begin();
00144       for (int i = 0; i < this->selected; i++) it++;
00145       AIConfig::GetConfig(slot)->ChangeAI((*it).second->GetName(), (*it).second->GetVersion());
00146     }
00147     SetWindowDirty(WC_GAME_OPTIONS, 0);
00148   }
00149 
00150   virtual void OnClick(Point pt, int widget, int click_count)
00151   {
00152     switch (widget) {
00153       case AIL_WIDGET_LIST: { // Select one of the AIs
00154         int sel = this->vscroll->GetScrolledRowFromWidget(pt.y, this, AIL_WIDGET_LIST, 0, this->line_height) - 1;
00155         if (sel < (int)this->ai_info_list->size()) {
00156           this->selected = sel;
00157           this->SetDirty();
00158           if (click_count > 1) {
00159             this->ChangeAI();
00160             delete this;
00161           }
00162         }
00163         break;
00164       }
00165 
00166       case AIL_WIDGET_ACCEPT: {
00167         this->ChangeAI();
00168         delete this;
00169         break;
00170       }
00171 
00172       case AIL_WIDGET_CANCEL:
00173         delete this;
00174         break;
00175     }
00176   }
00177 
00178   virtual void OnResize()
00179   {
00180     NWidgetCore *nwi = this->GetWidget<NWidgetCore>(AIL_WIDGET_LIST);
00181     this->vscroll->SetCapacity(nwi->current_y / this->line_height);
00182     nwi->widget_data = (this->vscroll->GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
00183   }
00184 
00190   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
00191   {
00192     if (_game_mode == GM_NORMAL && Company::IsValidID(this->slot)) {
00193       delete this;
00194       return;
00195     }
00196 
00197     if (!gui_scope) return;
00198 
00199     this->vscroll->SetCount((int)this->ai_info_list->size() + 1);
00200 
00201     /* selected goes from -1 .. length of ai list - 1. */
00202     this->selected = min(this->selected, this->vscroll->GetCount() - 2);
00203   }
00204 };
00205 
00207 static const NWidgetPart _nested_ai_list_widgets[] = {
00208   NWidget(NWID_HORIZONTAL),
00209     NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
00210     NWidget(WWT_CAPTION, COLOUR_MAUVE), SetDataTip(STR_AI_LIST_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00211   EndContainer(),
00212   NWidget(NWID_HORIZONTAL),
00213     NWidget(WWT_MATRIX, COLOUR_MAUVE, AIL_WIDGET_LIST), SetMinimalSize(188, 112), SetFill(1, 1), SetResize(1, 1), SetDataTip(0x501, STR_AI_LIST_TOOLTIP), SetScrollbar(AIL_WIDGET_SCROLLBAR),
00214     NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, AIL_WIDGET_SCROLLBAR),
00215   EndContainer(),
00216   NWidget(WWT_PANEL, COLOUR_MAUVE, AIL_WIDGET_INFO_BG), SetMinimalTextLines(8, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM), SetResize(1, 0),
00217   EndContainer(),
00218   NWidget(NWID_HORIZONTAL),
00219     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
00220       NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, AIL_WIDGET_ACCEPT), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_AI_LIST_ACCEPT, STR_AI_LIST_ACCEPT_TOOLTIP),
00221       NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, AIL_WIDGET_CANCEL), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_AI_LIST_CANCEL, STR_AI_LIST_CANCEL_TOOLTIP),
00222     EndContainer(),
00223     NWidget(WWT_RESIZEBOX, COLOUR_MAUVE),
00224   EndContainer(),
00225 };
00226 
00228 static const WindowDesc _ai_list_desc(
00229   WDP_CENTER, 200, 234,
00230   WC_AI_LIST, WC_NONE,
00231   WDF_UNCLICK_BUTTONS,
00232   _nested_ai_list_widgets, lengthof(_nested_ai_list_widgets)
00233 );
00234 
00239 static void ShowAIListWindow(CompanyID slot)
00240 {
00241   DeleteWindowByClass(WC_AI_LIST);
00242   new AIListWindow(&_ai_list_desc, slot);
00243 }
00244 
00246 enum AISettingsWindowWidgest {
00247   AIS_WIDGET_BACKGROUND,   
00248   AIS_WIDGET_SCROLLBAR,    
00249   AIS_WIDGET_ACCEPT,       
00250   AIS_WIDGET_RESET,        
00251 };
00252 
00256 struct AISettingsWindow : public Window {
00257   CompanyID slot;
00258   AIConfig *ai_config;
00259   int clicked_button;
00260   bool clicked_increase;
00261   int timeout;
00262   int clicked_row;
00263   int line_height; // Height of a row in the matrix widget.
00264   Scrollbar *vscroll;
00265 
00266   AISettingsWindow(const WindowDesc *desc, CompanyID slot) : Window(),
00267     slot(slot),
00268     clicked_button(-1),
00269     timeout(0)
00270   {
00271     this->ai_config = AIConfig::GetConfig(slot);
00272 
00273     this->CreateNestedTree(desc);
00274     this->vscroll = this->GetScrollbar(AIS_WIDGET_SCROLLBAR);
00275     this->FinishInitNested(desc, slot);  // Initializes 'this->line_height' as side effect.
00276 
00277     this->SetWidgetDisabledState(AIS_WIDGET_RESET, _game_mode != GM_MENU && Company::IsValidID(this->slot));
00278 
00279     this->vscroll->SetCount((int)this->ai_config->GetConfigList()->size());
00280   }
00281 
00282   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00283   {
00284     if (widget == AIS_WIDGET_BACKGROUND) {
00285       this->line_height = FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM;
00286 
00287       resize->width = 1;
00288       resize->height = this->line_height;
00289       size->height = GB(this->GetWidget<NWidgetCore>(widget)->widget_data, MAT_ROW_START, MAT_ROW_BITS) * this->line_height;
00290     }
00291   }
00292 
00293   virtual void DrawWidget(const Rect &r, int widget) const
00294   {
00295     if (widget != AIS_WIDGET_BACKGROUND) return;
00296 
00297     AIConfig *config = this->ai_config;
00298     AIConfigItemList::const_iterator it = config->GetConfigList()->begin();
00299     int i = 0;
00300     for (; !this->vscroll->IsVisible(i); i++) it++;
00301 
00302     bool rtl = _current_text_dir == TD_RTL;
00303     uint buttons_left = rtl ? r.right - 23 : r.left + 4;
00304     uint text_left    = r.left + (rtl ? WD_FRAMERECT_LEFT : 28);
00305     uint text_right   = r.right - (rtl ? 28 : WD_FRAMERECT_RIGHT);
00306 
00307 
00308     int y = r.top;
00309     for (; this->vscroll->IsVisible(i) && it != config->GetConfigList()->end(); i++, it++) {
00310       int current_value = config->GetSetting((*it).name);
00311       bool editable = _game_mode == GM_MENU || !Company::IsValidID(this->slot) || (it->flags & AICONFIG_INGAME) != 0;
00312 
00313       StringID str;
00314       TextColour colour;
00315       uint idx = 0;
00316       if (StrEmpty((*it).description)) {
00317         str = STR_JUST_STRING;
00318         colour = TC_ORANGE;
00319       } else {
00320         str = STR_AI_SETTINGS_SETTING;
00321         colour = TC_LIGHT_BLUE;
00322         SetDParamStr(idx++, (*it).description);
00323       }
00324 
00325       if (((*it).flags & AICONFIG_BOOLEAN) != 0) {
00326         DrawFrameRect(buttons_left, y  + 2, buttons_left + 19, y + 10, (current_value != 0) ? COLOUR_GREEN : COLOUR_RED, (current_value != 0) ? FR_LOWERED : FR_NONE);
00327         SetDParam(idx++, current_value == 0 ? STR_CONFIG_SETTING_OFF : STR_CONFIG_SETTING_ON);
00328       } else {
00329         DrawArrowButtons(buttons_left, y + 2, COLOUR_YELLOW, (this->clicked_button == i) ? 1 + (this->clicked_increase != rtl) : 0, editable && current_value > (*it).min_value, editable && current_value < (*it).max_value);
00330         if (it->labels != NULL && it->labels->Contains(current_value)) {
00331           SetDParam(idx++, STR_JUST_RAW_STRING);
00332           SetDParamStr(idx++, it->labels->Find(current_value)->second);
00333         } else {
00334           SetDParam(idx++, STR_JUST_INT);
00335           SetDParam(idx++, current_value);
00336         }
00337       }
00338 
00339       DrawString(text_left, text_right, y + WD_MATRIX_TOP, str, colour);
00340       y += this->line_height;
00341     }
00342   }
00343 
00344   void CheckDifficultyLevel()
00345   {
00346     if (_game_mode == GM_MENU) {
00347       if (_settings_newgame.difficulty.diff_level != 3) {
00348         _settings_newgame.difficulty.diff_level = 3;
00349         ShowErrorMessage(STR_WARNING_DIFFICULTY_TO_CUSTOM, INVALID_STRING_ID, WL_WARNING);
00350       }
00351     } else if (_settings_game.difficulty.diff_level != 3) {
00352       IConsoleSetSetting("difficulty.diff_level", 3);
00353     }
00354   }
00355 
00356   virtual void OnClick(Point pt, int widget, int click_count)
00357   {
00358     switch (widget) {
00359       case AIS_WIDGET_BACKGROUND: {
00360         const NWidgetBase *wid = this->GetWidget<NWidgetBase>(AIS_WIDGET_BACKGROUND);
00361         int num = (pt.y - wid->pos_y) / this->line_height + this->vscroll->GetPosition();
00362         if (num >= (int)this->ai_config->GetConfigList()->size()) break;
00363 
00364         AIConfigItemList::const_iterator it = this->ai_config->GetConfigList()->begin();
00365         for (int i = 0; i < num; i++) it++;
00366         AIConfigItem config_item = *it;
00367         if (_game_mode == GM_NORMAL && Company::IsValidID(this->slot) && (config_item.flags & AICONFIG_INGAME) == 0) return;
00368 
00369         bool bool_item = (config_item.flags & AICONFIG_BOOLEAN) != 0;
00370 
00371         int x = pt.x - wid->pos_x;
00372         if (_current_text_dir == TD_RTL) x = wid->current_x - x;
00373         x -= 4;
00374         /* One of the arrows is clicked (or green/red rect in case of bool value) */
00375         if (IsInsideMM(x, 0, 21)) {
00376           int new_val = this->ai_config->GetSetting(config_item.name);
00377           if (bool_item) {
00378             new_val = !new_val;
00379           } else if (x >= 10) {
00380             /* Increase button clicked */
00381             new_val += config_item.step_size;
00382             if (new_val > config_item.max_value) new_val = config_item.max_value;
00383             this->clicked_increase = true;
00384           } else {
00385             /* Decrease button clicked */
00386             new_val -= config_item.step_size;
00387             if (new_val < config_item.min_value) new_val = config_item.min_value;
00388             this->clicked_increase = false;
00389           }
00390 
00391           this->ai_config->SetSetting(config_item.name, new_val);
00392           this->clicked_button = num;
00393           this->timeout = 5;
00394 
00395           this->CheckDifficultyLevel();
00396         } else if (!bool_item) {
00397           /* Display a query box so users can enter a custom value. */
00398           this->clicked_row = num;
00399           SetDParam(0, this->ai_config->GetSetting(config_item.name));
00400           ShowQueryString(STR_JUST_INT, STR_CONFIG_SETTING_QUERY_CAPTION, 10, 100, this, CS_NUMERAL, QSF_NONE);
00401         }
00402 
00403         this->SetDirty();
00404         break;
00405       }
00406 
00407       case AIS_WIDGET_ACCEPT:
00408         delete this;
00409         break;
00410 
00411       case AIS_WIDGET_RESET:
00412         if (_game_mode == GM_MENU || !Company::IsValidID(this->slot)) {
00413           this->ai_config->ResetSettings();
00414           this->SetDirty();
00415         }
00416         break;
00417     }
00418   }
00419 
00420   virtual void OnQueryTextFinished(char *str)
00421   {
00422     if (StrEmpty(str)) return;
00423     AIConfigItemList::const_iterator it = this->ai_config->GetConfigList()->begin();
00424     for (int i = 0; i < this->clicked_row; i++) it++;
00425     if (_game_mode == GM_NORMAL && Company::IsValidID(this->slot) && (it->flags & AICONFIG_INGAME) == 0) return;
00426     int32 value = atoi(str);
00427     this->ai_config->SetSetting((*it).name, value);
00428     this->CheckDifficultyLevel();
00429     this->SetDirty();
00430   }
00431 
00432   virtual void OnResize()
00433   {
00434     NWidgetCore *nwi = this->GetWidget<NWidgetCore>(AIS_WIDGET_BACKGROUND);
00435     this->vscroll->SetCapacity(nwi->current_y / this->line_height);
00436     nwi->widget_data = (this->vscroll->GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
00437   }
00438 
00439   virtual void OnTick()
00440   {
00441     if (--this->timeout == 0) {
00442       this->clicked_button = -1;
00443       this->SetDirty();
00444     }
00445   }
00446 
00452   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
00453   {
00454     if (_game_mode == GM_NORMAL && Company::IsValidID(this->slot)) delete this;
00455   }
00456 };
00457 
00459 static const NWidgetPart _nested_ai_settings_widgets[] = {
00460   NWidget(NWID_HORIZONTAL),
00461     NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
00462     NWidget(WWT_CAPTION, COLOUR_MAUVE), SetDataTip(STR_AI_SETTINGS_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00463   EndContainer(),
00464   NWidget(NWID_HORIZONTAL),
00465     NWidget(WWT_MATRIX, COLOUR_MAUVE, AIS_WIDGET_BACKGROUND), SetMinimalSize(188, 182), SetResize(1, 1), SetFill(1, 0), SetDataTip(0x501, STR_NULL), SetScrollbar(AIS_WIDGET_SCROLLBAR),
00466     NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, AIS_WIDGET_SCROLLBAR),
00467   EndContainer(),
00468   NWidget(NWID_HORIZONTAL),
00469     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
00470       NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, AIS_WIDGET_ACCEPT), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_AI_SETTINGS_CLOSE, STR_NULL),
00471       NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, AIS_WIDGET_RESET), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_AI_SETTINGS_RESET, STR_NULL),
00472     EndContainer(),
00473     NWidget(WWT_RESIZEBOX, COLOUR_MAUVE),
00474   EndContainer(),
00475 };
00476 
00478 static const WindowDesc _ai_settings_desc(
00479   WDP_CENTER, 500, 208,
00480   WC_AI_SETTINGS, WC_NONE,
00481   WDF_UNCLICK_BUTTONS,
00482   _nested_ai_settings_widgets, lengthof(_nested_ai_settings_widgets)
00483 );
00484 
00489 static void ShowAISettingsWindow(CompanyID slot)
00490 {
00491   DeleteWindowByClass(WC_AI_LIST);
00492   DeleteWindowByClass(WC_AI_SETTINGS);
00493   new AISettingsWindow(&_ai_settings_desc, slot);
00494 }
00495 
00497 enum AIConfigWindowWidgets {
00498   AIC_WIDGET_BACKGROUND,   
00499   AIC_WIDGET_DECREASE,     
00500   AIC_WIDGET_INCREASE,     
00501   AIC_WIDGET_NUMBER,       
00502   AIC_WIDGET_LIST,         
00503   AIC_WIDGET_SCROLLBAR,    
00504   AIC_WIDGET_MOVE_UP,      
00505   AIC_WIDGET_MOVE_DOWN,    
00506   AIC_WIDGET_CHANGE,       
00507   AIC_WIDGET_CONFIGURE,    
00508   AIC_WIDGET_CLOSE,        
00509   AIC_WIDGET_CONTENT_DOWNLOAD, 
00510 };
00511 
00513 static const NWidgetPart _nested_ai_config_widgets[] = {
00514   NWidget(NWID_HORIZONTAL),
00515     NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
00516     NWidget(WWT_CAPTION, COLOUR_MAUVE), SetDataTip(STR_AI_CONFIG_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00517   EndContainer(),
00518   NWidget(WWT_PANEL, COLOUR_MAUVE, AIC_WIDGET_BACKGROUND),
00519     NWidget(NWID_VERTICAL), SetPIP(4, 4, 4),
00520       NWidget(NWID_HORIZONTAL), SetPIP(10, 0, 10),
00521         NWidget(WWT_PUSHARROWBTN, COLOUR_YELLOW, AIC_WIDGET_DECREASE), SetFill(0, 1), SetDataTip(AWV_DECREASE, STR_NULL),
00522         NWidget(WWT_PUSHARROWBTN, COLOUR_YELLOW, AIC_WIDGET_INCREASE), SetFill(0, 1), SetDataTip(AWV_INCREASE, STR_NULL),
00523         NWidget(NWID_SPACER), SetMinimalSize(6, 0),
00524         NWidget(WWT_TEXT, COLOUR_MAUVE, AIC_WIDGET_NUMBER), SetDataTip(STR_DIFFICULTY_LEVEL_SETTING_MAXIMUM_NO_COMPETITORS, STR_NULL), SetFill(1, 0), SetPadding(1, 0, 0, 0),
00525       EndContainer(),
00526       NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(10, 0, 10),
00527         NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, AIC_WIDGET_MOVE_UP), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_AI_CONFIG_MOVE_UP, STR_AI_CONFIG_MOVE_UP_TOOLTIP),
00528         NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, AIC_WIDGET_MOVE_DOWN), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_AI_CONFIG_MOVE_DOWN, STR_AI_CONFIG_MOVE_DOWN_TOOLTIP),
00529       EndContainer(),
00530     EndContainer(),
00531     NWidget(NWID_HORIZONTAL),
00532       NWidget(WWT_MATRIX, COLOUR_MAUVE, AIC_WIDGET_LIST), SetMinimalSize(288, 112), SetFill(1, 0), SetDataTip(0x801, STR_AI_CONFIG_LIST_TOOLTIP), SetScrollbar(AIC_WIDGET_SCROLLBAR),
00533       NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, AIC_WIDGET_SCROLLBAR),
00534     EndContainer(),
00535     NWidget(NWID_SPACER), SetMinimalSize(0, 9),
00536     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(5, 0, 5),
00537       NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, AIC_WIDGET_CHANGE), SetFill(1, 0), SetMinimalSize(93, 12), SetDataTip(STR_AI_CONFIG_CHANGE, STR_AI_CONFIG_CHANGE_TOOLTIP),
00538       NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, AIC_WIDGET_CONFIGURE), SetFill(1, 0), SetMinimalSize(93, 12), SetDataTip(STR_AI_CONFIG_CONFIGURE, STR_AI_CONFIG_CONFIGURE_TOOLTIP),
00539       NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, AIC_WIDGET_CLOSE), SetFill(1, 0), SetMinimalSize(93, 12), SetDataTip(STR_AI_SETTINGS_CLOSE, STR_NULL),
00540     EndContainer(),
00541     NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, AIC_WIDGET_CONTENT_DOWNLOAD), SetFill(1, 0), SetMinimalSize(279, 12), SetPadding(0, 5, 9, 5), SetDataTip(STR_INTRO_ONLINE_CONTENT, STR_INTRO_TOOLTIP_ONLINE_CONTENT),
00542   EndContainer(),
00543 };
00544 
00546 static const WindowDesc _ai_config_desc(
00547   WDP_CENTER, 0, 0,
00548   WC_GAME_OPTIONS, WC_NONE,
00549   WDF_UNCLICK_BUTTONS,
00550   _nested_ai_config_widgets, lengthof(_nested_ai_config_widgets)
00551 );
00552 
00556 struct AIConfigWindow : public Window {
00557   CompanyID selected_slot; 
00558   int line_height;         
00559   Scrollbar *vscroll;
00560 
00561   AIConfigWindow() : Window()
00562   {
00563     this->InitNested(&_ai_config_desc); // Initializes 'this->line_height' as a side effect.
00564     this->vscroll = this->GetScrollbar(AIC_WIDGET_SCROLLBAR);
00565     this->selected_slot = INVALID_COMPANY;
00566     NWidgetCore *nwi = this->GetWidget<NWidgetCore>(AIC_WIDGET_LIST);
00567     this->vscroll->SetCapacity(nwi->current_y / this->line_height);
00568     this->vscroll->SetCount(MAX_COMPANIES);
00569     nwi->widget_data = (this->vscroll->GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
00570     this->OnInvalidateData(0);
00571   }
00572 
00573   ~AIConfigWindow()
00574   {
00575     DeleteWindowByClass(WC_AI_LIST);
00576     DeleteWindowByClass(WC_AI_SETTINGS);
00577   }
00578 
00579   virtual void SetStringParameters(int widget) const
00580   {
00581     switch (widget) {
00582       case AIC_WIDGET_NUMBER:
00583         SetDParam(0, GetGameSettings().difficulty.max_no_competitors);
00584         break;
00585     }
00586   }
00587 
00588   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00589   {
00590     switch (widget) {
00591       case AIC_WIDGET_LIST:
00592         this->line_height = FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM;
00593         size->height = GB(this->GetWidget<NWidgetCore>(widget)->widget_data, MAT_ROW_START, MAT_ROW_BITS) * this->line_height;
00594         break;
00595     }
00596   }
00597 
00603   static bool IsEditable(CompanyID slot)
00604   {
00605     if (_game_mode != GM_NORMAL) {
00606       return slot > 0 && slot <= GetGameSettings().difficulty.max_no_competitors;
00607     }
00608     if (Company::IsValidID(slot) || slot < 0) return false;
00609 
00610     int max_slot = GetGameSettings().difficulty.max_no_competitors;
00611     for (CompanyID cid = COMPANY_FIRST; cid < (CompanyID)max_slot && cid < MAX_COMPANIES; cid++) {
00612       if (Company::IsValidHumanID(cid)) max_slot++;
00613     }
00614     return slot < max_slot;
00615   }
00616 
00617   virtual void DrawWidget(const Rect &r, int widget) const
00618   {
00619     switch (widget) {
00620       case AIC_WIDGET_LIST: {
00621         int y = r.top;
00622         for (int i = this->vscroll->GetPosition(); this->vscroll->IsVisible(i) && i < MAX_COMPANIES; i++) {
00623           StringID text;
00624 
00625           if ((_game_mode != GM_NORMAL && i == 0) || (_game_mode == GM_NORMAL && Company::IsValidHumanID(i))) {
00626             text = STR_AI_CONFIG_HUMAN_PLAYER;
00627           } else if (AIConfig::GetConfig((CompanyID)i)->GetInfo() != NULL) {
00628             SetDParamStr(0, AIConfig::GetConfig((CompanyID)i)->GetInfo()->GetName());
00629             text = STR_JUST_RAW_STRING;
00630           } else {
00631             text = STR_AI_CONFIG_RANDOM_AI;
00632           }
00633           DrawString(r.left + 10, r.right - 10, y + WD_MATRIX_TOP, text,
00634               (this->selected_slot == i) ? TC_WHITE : (IsEditable((CompanyID)i) ? TC_ORANGE : TC_SILVER));
00635           y += this->line_height;
00636         }
00637         break;
00638       }
00639     }
00640   }
00641 
00642   virtual void OnClick(Point pt, int widget, int click_count)
00643   {
00644     switch (widget) {
00645       case AIC_WIDGET_DECREASE:
00646       case AIC_WIDGET_INCREASE: {
00647         int new_value;
00648         if (widget == AIC_WIDGET_DECREASE) {
00649           new_value = max(0, GetGameSettings().difficulty.max_no_competitors - 1);
00650         } else {
00651           new_value = min(MAX_COMPANIES - 1, GetGameSettings().difficulty.max_no_competitors + 1);
00652         }
00653         IConsoleSetSetting("difficulty.max_no_competitors", new_value);
00654         this->InvalidateData();
00655         break;
00656       }
00657 
00658       case AIC_WIDGET_LIST: { // Select a slot
00659         this->selected_slot = (CompanyID)this->vscroll->GetScrolledRowFromWidget(pt.y, this, widget, 0, this->line_height);
00660         this->InvalidateData();
00661         if (click_count > 1 && this->selected_slot != INVALID_COMPANY) ShowAIListWindow((CompanyID)this->selected_slot);
00662         break;
00663       }
00664 
00665       case AIC_WIDGET_MOVE_UP:
00666         if (IsEditable(this->selected_slot) && IsEditable((CompanyID)(this->selected_slot - 1))) {
00667           Swap(GetGameSettings().ai_config[this->selected_slot], GetGameSettings().ai_config[this->selected_slot - 1]);
00668           this->selected_slot--;
00669           this->vscroll->ScrollTowards(this->selected_slot);
00670           this->InvalidateData();
00671         }
00672         break;
00673 
00674       case AIC_WIDGET_MOVE_DOWN:
00675         if (IsEditable(this->selected_slot) && IsEditable((CompanyID)(this->selected_slot + 1))) {
00676           Swap(GetGameSettings().ai_config[this->selected_slot], GetGameSettings().ai_config[this->selected_slot + 1]);
00677           this->selected_slot++;
00678           this->vscroll->ScrollTowards(this->selected_slot);
00679           this->InvalidateData();
00680         }
00681         break;
00682 
00683       case AIC_WIDGET_CHANGE:  // choose other AI
00684         ShowAIListWindow((CompanyID)this->selected_slot);
00685         break;
00686 
00687       case AIC_WIDGET_CONFIGURE: // change the settings for an AI
00688         ShowAISettingsWindow((CompanyID)this->selected_slot);
00689         break;
00690 
00691       case AIC_WIDGET_CLOSE:
00692         delete this;
00693         break;
00694 
00695       case AIC_WIDGET_CONTENT_DOWNLOAD:
00696         if (!_network_available) {
00697           ShowErrorMessage(STR_NETWORK_ERROR_NOTAVAILABLE, INVALID_STRING_ID, WL_ERROR);
00698         } else {
00699 #if defined(ENABLE_NETWORK)
00700           ShowNetworkContentListWindow(NULL, CONTENT_TYPE_AI);
00701 #endif
00702         }
00703         break;
00704     }
00705   }
00706 
00712   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
00713   {
00714     if (!IsEditable(this->selected_slot)) {
00715       this->selected_slot = INVALID_COMPANY;
00716     }
00717 
00718     if (!gui_scope) return;
00719 
00720     this->SetWidgetDisabledState(AIC_WIDGET_DECREASE, GetGameSettings().difficulty.max_no_competitors == 0);
00721     this->SetWidgetDisabledState(AIC_WIDGET_INCREASE, GetGameSettings().difficulty.max_no_competitors == MAX_COMPANIES - 1);
00722     this->SetWidgetDisabledState(AIC_WIDGET_CHANGE, this->selected_slot == INVALID_COMPANY);
00723     this->SetWidgetDisabledState(AIC_WIDGET_CONFIGURE, this->selected_slot == INVALID_COMPANY);
00724     this->SetWidgetDisabledState(AIC_WIDGET_MOVE_UP, this->selected_slot == INVALID_COMPANY || !IsEditable((CompanyID)(this->selected_slot - 1)));
00725     this->SetWidgetDisabledState(AIC_WIDGET_MOVE_DOWN, this->selected_slot == INVALID_COMPANY || !IsEditable((CompanyID)(this->selected_slot + 1)));
00726   }
00727 };
00728 
00730 void ShowAIConfigWindow()
00731 {
00732   DeleteWindowById(WC_GAME_OPTIONS, 0);
00733   new AIConfigWindow();
00734 }
00735 
00737 enum AIDebugWindowWidgets {
00738   AID_WIDGET_VIEW,
00739   AID_WIDGET_NAME_TEXT,
00740   AID_WIDGET_SETTINGS,
00741   AID_WIDGET_RELOAD_TOGGLE,
00742   AID_WIDGET_LOG_PANEL,
00743   AID_WIDGET_SCROLLBAR,
00744   AID_WIDGET_COMPANY_BUTTON_START,
00745   AID_WIDGET_COMPANY_BUTTON_END = AID_WIDGET_COMPANY_BUTTON_START + MAX_COMPANIES - 1,
00746   AID_BREAK_STRING_WIDGETS,
00747   AID_WIDGET_BREAK_STR_ON_OFF_BTN,
00748   AID_WIDGET_BREAK_STR_EDIT_BOX,
00749   AID_WIDGET_MATCH_CASE_BTN,
00750   AID_WIDGET_CONTINUE_BTN,
00751 };
00752 
00756 struct AIDebugWindow : public QueryStringBaseWindow {
00757   static const int top_offset;    
00758   static const int bottom_offset; 
00759 
00760   static const unsigned int MAX_BREAK_STR_STRING_LENGTH = 256;
00761 
00762   static CompanyID ai_debug_company;                     
00763   int redraw_timer;
00764   int last_vscroll_pos;
00765   bool autoscroll;
00766   bool show_break_box;
00767   static bool break_check_enabled;                       
00768   static char break_string[MAX_BREAK_STR_STRING_LENGTH]; 
00769   static bool case_sensitive_break_check;                
00770   int highlight_row;                                     
00771   Scrollbar *vscroll;
00772 
00773   AIDebugWindow(const WindowDesc *desc, WindowNumber number) : QueryStringBaseWindow(MAX_BREAK_STR_STRING_LENGTH)
00774   {
00775     this->CreateNestedTree(desc);
00776     this->vscroll = this->GetScrollbar(AID_WIDGET_SCROLLBAR);
00777     this->show_break_box = _settings_client.gui.ai_developer_tools;
00778     this->GetWidget<NWidgetStacked>(AID_BREAK_STRING_WIDGETS)->SetDisplayedPlane(this->show_break_box ? 0 : SZSP_HORIZONTAL);
00779     this->FinishInitNested(desc, number);
00780 
00781     if (!this->show_break_box) break_check_enabled = false;
00782     /* Disable the companies who are not active or not an AI */
00783     for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
00784       this->SetWidgetDisabledState(i + AID_WIDGET_COMPANY_BUTTON_START, !Company::IsValidAiID(i));
00785     }
00786     this->DisableWidget(AID_WIDGET_RELOAD_TOGGLE);
00787     this->DisableWidget(AID_WIDGET_SETTINGS);
00788     this->DisableWidget(AID_WIDGET_CONTINUE_BTN);
00789 
00790     this->last_vscroll_pos = 0;
00791     this->autoscroll = true;
00792     this->highlight_row = -1;
00793     InitializeTextBuffer(&this->text, this->edit_str_buf, this->edit_str_size, MAX_BREAK_STR_STRING_LENGTH);
00794 
00795     /* Restore the break string value from static variable */
00796     strecpy(this->edit_str_buf, this->break_string, this->edit_str_buf + MAX_BREAK_STR_STRING_LENGTH);
00797     UpdateTextBufferSize(&this->text);
00798 
00799     /* Restore button state from static class variables */
00800     if (ai_debug_company != INVALID_COMPANY) this->LowerWidget(ai_debug_company + AID_WIDGET_COMPANY_BUTTON_START);
00801     this->SetWidgetLoweredState(AID_WIDGET_BREAK_STR_ON_OFF_BTN, this->break_check_enabled);
00802     this->SetWidgetLoweredState(AID_WIDGET_MATCH_CASE_BTN, this->case_sensitive_break_check);
00803 
00804   }
00805 
00806   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00807   {
00808     if (widget == AID_WIDGET_LOG_PANEL) {
00809       resize->height = FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
00810       size->height = 14 * resize->height + this->top_offset + this->bottom_offset;
00811     }
00812   }
00813 
00814   virtual void OnPaint()
00815   {
00816     /* Check if the currently selected company is still active. */
00817     if (ai_debug_company == INVALID_COMPANY || !Company::IsValidAiID(ai_debug_company)) {
00818       if (ai_debug_company != INVALID_COMPANY) {
00819         /* Raise the widget for the previous selection. */
00820         this->RaiseWidget(ai_debug_company + AID_WIDGET_COMPANY_BUTTON_START);
00821 
00822         ai_debug_company = INVALID_COMPANY;
00823       }
00824 
00825       const Company *c;
00826       FOR_ALL_COMPANIES(c) {
00827         if (c->is_ai) {
00828           /* Lower the widget corresponding to this company. */
00829           this->LowerWidget(c->index + AID_WIDGET_COMPANY_BUTTON_START);
00830 
00831           ai_debug_company = c->index;
00832           break;
00833         }
00834       }
00835     }
00836 
00837     /* Update "Reload AI" and "AI settings" buttons */
00838     this->SetWidgetsDisabledState(ai_debug_company == INVALID_COMPANY,
00839       AID_WIDGET_RELOAD_TOGGLE,
00840       AID_WIDGET_SETTINGS,
00841       WIDGET_LIST_END);
00842 
00843     /* Draw standard stuff */
00844     this->DrawWidgets();
00845 
00846     if (this->IsShaded()) return; // Don't draw anything when the window is shaded.
00847 
00848     if (this->show_break_box) this->DrawEditBox(AID_WIDGET_BREAK_STR_EDIT_BOX);
00849 
00850     /* Paint the company icons */
00851     for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
00852       NWidgetCore *button = this->GetWidget<NWidgetCore>(i + AID_WIDGET_COMPANY_BUTTON_START);
00853       bool dirty = false;
00854 
00855       bool valid = Company::IsValidAiID(i);
00856       bool disabled = !valid;
00857       if (button->IsDisabled() != disabled) {
00858         /* Invalid/non-AI companies have button disabled */
00859         button->SetDisabled(disabled);
00860         dirty = true;
00861       }
00862 
00863       bool dead = valid && Company::Get(i)->ai_instance->IsDead();
00864       Colours colour = dead ? COLOUR_RED : COLOUR_GREY;
00865       if (button->colour != colour) {
00866         /* Mark dead AIs by red background */
00867         button->colour = colour;
00868         dirty = true;
00869       }
00870 
00871       /* Do we need a repaint? */
00872       if (dirty) this->SetDirty();
00873       /* Draw company icon only for valid AI companies */
00874       if (!valid) continue;
00875 
00876       byte offset = (i == ai_debug_company) ? 1 : 0;
00877       DrawCompanyIcon(i, button->pos_x + button->current_x / 2 - 7 + offset, this->GetWidget<NWidgetBase>(AID_WIDGET_COMPANY_BUTTON_START + i)->pos_y + 2 + offset);
00878     }
00879 
00880     /* If there are no active companies, don't display anything else. */
00881     if (ai_debug_company == INVALID_COMPANY) return;
00882 
00883     Backup<CompanyByte> cur_company(_current_company, ai_debug_company, FILE_LINE);
00884     AILog::LogData *log = (AILog::LogData *)AIObject::GetLogPointer();
00885     cur_company.Restore();
00886 
00887     int scroll_count = (log == NULL) ? 0 : log->used;
00888     if (this->vscroll->GetCount() != scroll_count) {
00889       this->vscroll->SetCount(scroll_count);
00890 
00891       /* We need a repaint */
00892       this->SetWidgetDirty(AID_WIDGET_SCROLLBAR);
00893     }
00894 
00895     if (log == NULL) return;
00896 
00897     /* Detect when the user scrolls the window. Enable autoscroll when the
00898      * bottom-most line becomes visible. */
00899     if (this->last_vscroll_pos != this->vscroll->GetPosition()) {
00900       this->autoscroll = this->vscroll->GetPosition() >= log->used - this->vscroll->GetCapacity();
00901     }
00902     if (this->autoscroll) {
00903       int scroll_pos = max(0, log->used - this->vscroll->GetCapacity());
00904       if (scroll_pos != this->vscroll->GetPosition()) {
00905         this->vscroll->SetPosition(scroll_pos);
00906 
00907         /* We need a repaint */
00908         this->SetWidgetDirty(AID_WIDGET_SCROLLBAR);
00909         this->SetWidgetDirty(AID_WIDGET_LOG_PANEL);
00910       }
00911     }
00912     this->last_vscroll_pos = this->vscroll->GetPosition();
00913   }
00914 
00915   virtual void SetStringParameters(int widget) const
00916   {
00917     switch (widget) {
00918       case AID_WIDGET_NAME_TEXT:
00919         if (ai_debug_company == INVALID_COMPANY || !Company::IsValidAiID(ai_debug_company)) {
00920           SetDParam(0, STR_EMPTY);
00921         } else {
00922           const AIInfo *info = Company::Get(ai_debug_company)->ai_info;
00923           assert(info != NULL);
00924           SetDParam(0, STR_AI_DEBUG_NAME_AND_VERSION);
00925           SetDParamStr(1, info->GetName());
00926           SetDParam(2, info->GetVersion());
00927         }
00928         break;
00929     }
00930   }
00931 
00932   virtual void DrawWidget(const Rect &r, int widget) const
00933   {
00934     if (ai_debug_company == INVALID_COMPANY) return;
00935 
00936     switch (widget) {
00937       case AID_WIDGET_LOG_PANEL: {
00938         Backup<CompanyByte> cur_company(_current_company, ai_debug_company, FILE_LINE);
00939         AILog::LogData *log = (AILog::LogData *)AIObject::GetLogPointer();
00940         cur_company.Restore();
00941         if (log == NULL) return;
00942 
00943         int y = this->top_offset;
00944         for (int i = this->vscroll->GetPosition(); this->vscroll->IsVisible(i) && i < log->used; i++) {
00945           int pos = (i + log->pos + 1 - log->used + log->count) % log->count;
00946           if (log->lines[pos] == NULL) break;
00947 
00948           TextColour colour;
00949           switch (log->type[pos]) {
00950             case AILog::LOG_SQ_INFO:  colour = TC_BLACK;  break;
00951             case AILog::LOG_SQ_ERROR: colour = TC_RED;    break;
00952             case AILog::LOG_INFO:     colour = TC_BLACK;  break;
00953             case AILog::LOG_WARNING:  colour = TC_YELLOW; break;
00954             case AILog::LOG_ERROR:    colour = TC_RED;    break;
00955             default:                  colour = TC_BLACK;  break;
00956           }
00957 
00958           /* Check if the current line should be highlighted */
00959           if (pos == this->highlight_row) {
00960             GfxFillRect(r.left + 1, r.top + y, r.right - 1, r.top + y + this->resize.step_height - WD_PAR_VSEP_NORMAL, 0);
00961             if (colour == TC_BLACK) colour = TC_WHITE; // Make black text readable by inverting it to white.
00962           }
00963 
00964           DrawString(r.left + 7, r.right - 7, r.top + y, log->lines[pos], colour, SA_LEFT | SA_FORCE);
00965           y += this->resize.step_height;
00966         }
00967         break;
00968       }
00969     }
00970   }
00971 
00972   void ChangeToAI(CompanyID show_ai)
00973   {
00974     this->RaiseWidget(ai_debug_company + AID_WIDGET_COMPANY_BUTTON_START);
00975     ai_debug_company = show_ai;
00976 
00977     Backup<CompanyByte> cur_company(_current_company, ai_debug_company, FILE_LINE);
00978     AILog::LogData *log = (AILog::LogData *)AIObject::GetLogPointer();
00979     cur_company.Restore();
00980     this->vscroll->SetCount((log == NULL) ? 0 : log->used);
00981 
00982     this->LowerWidget(ai_debug_company + AID_WIDGET_COMPANY_BUTTON_START);
00983     this->autoscroll = true;
00984     this->last_vscroll_pos = this->vscroll->GetPosition();
00985     this->SetDirty();
00986     /* Close AI settings window to prevent confusion */
00987     DeleteWindowByClass(WC_AI_SETTINGS);
00988   }
00989 
00990   virtual void OnClick(Point pt, int widget, int click_count)
00991   {
00992     /* Check which button is clicked */
00993     if (IsInsideMM(widget, AID_WIDGET_COMPANY_BUTTON_START, AID_WIDGET_COMPANY_BUTTON_END + 1)) {
00994       /* Is it no on disable? */
00995       if (!this->IsWidgetDisabled(widget)) {
00996         ChangeToAI((CompanyID)(widget - AID_WIDGET_COMPANY_BUTTON_START));
00997       }
00998     }
00999 
01000     switch (widget) {
01001       case AID_WIDGET_RELOAD_TOGGLE:
01002         /* First kill the company of the AI, then start a new one. This should start the current AI again */
01003         DoCommandP(0, 2 | ai_debug_company << 16, 0, CMD_COMPANY_CTRL);
01004         DoCommandP(0, 1 | ai_debug_company << 16, 0, CMD_COMPANY_CTRL);
01005         break;
01006 
01007       case AID_WIDGET_SETTINGS:
01008         ShowAISettingsWindow(ai_debug_company);
01009         break;
01010 
01011       case AID_WIDGET_BREAK_STR_ON_OFF_BTN:
01012         this->break_check_enabled = !this->break_check_enabled;
01013         this->SetWidgetLoweredState(AID_WIDGET_BREAK_STR_ON_OFF_BTN, this->break_check_enabled);
01014         this->SetWidgetDirty(AID_WIDGET_BREAK_STR_ON_OFF_BTN);
01015         break;
01016 
01017       case AID_WIDGET_MATCH_CASE_BTN:
01018         this->case_sensitive_break_check = !this->case_sensitive_break_check;
01019         this->SetWidgetLoweredState(AID_WIDGET_MATCH_CASE_BTN, this->case_sensitive_break_check);
01020         break;
01021 
01022       case AID_WIDGET_CONTINUE_BTN:
01023         /* Unpause */
01024         DoCommandP(0, PM_PAUSED_NORMAL, 0, CMD_PAUSE);
01025         this->DisableWidget(AID_WIDGET_CONTINUE_BTN);
01026         this->RaiseWidget(AID_WIDGET_CONTINUE_BTN); // Disabled widgets don't raise themself
01027         break;
01028     }
01029   }
01030 
01031   virtual void OnTimeout()
01032   {
01033     this->RaiseWidget(AID_WIDGET_RELOAD_TOGGLE);
01034     this->RaiseWidget(AID_WIDGET_SETTINGS);
01035     this->SetDirty();
01036   }
01037 
01038   virtual void OnMouseLoop()
01039   {
01040     this->HandleEditBox(AID_WIDGET_BREAK_STR_EDIT_BOX);
01041   }
01042 
01043   virtual EventState OnKeyPress(uint16 key, uint16 keycode)
01044   {
01045     EventState state = ES_NOT_HANDLED;
01046     if (this->HandleEditBoxKey(AID_WIDGET_BREAK_STR_EDIT_BOX, key, keycode, state) != HEBR_NOT_FOCUSED) {
01047       /* Save the current string to static member so it can be restored next time the window is opened */
01048       strecpy(this->break_string, this->edit_str_buf, lastof(this->break_string));
01049     }
01050     return state;
01051   }
01052 
01058   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
01059   {
01060     if (data == -1 || ai_debug_company == data) this->SetDirty();
01061 
01062     if (gui_scope && data == -2) {
01063       /* The continue button should be disabled when the game is unpaused and
01064        * it was previously paused by the break string ( = a line in the log
01065        * was highlighted )*/
01066       if ((_pause_mode & PM_PAUSED_NORMAL) == PM_UNPAUSED && this->highlight_row != -1) {
01067         this->DisableWidget(AID_WIDGET_CONTINUE_BTN);
01068         this->SetWidgetDirty(AID_WIDGET_CONTINUE_BTN);
01069         this->SetWidgetDirty(AID_WIDGET_LOG_PANEL);
01070         this->highlight_row = -1;
01071       }
01072     }
01073 
01074     /* If the log message is related to the active company tab, check the break string.
01075      * This needs to be done in gameloop-scope, so the AI is suspended immediately. */
01076     if (!gui_scope && data == ai_debug_company && this->break_check_enabled && !StrEmpty(this->edit_str_buf)) {
01077       /* Get the log instance of the active company */
01078       Backup<CompanyByte> cur_company(_current_company, ai_debug_company, FILE_LINE);
01079       AILog::LogData *log = (AILog::LogData *)AIObject::GetLogPointer();
01080 
01081       if (log != NULL && case_sensitive_break_check?
01082           strstr(log->lines[log->pos], this->edit_str_buf) != 0 :
01083           strcasestr(log->lines[log->pos], this->edit_str_buf) != 0) {
01084 
01085         AI::Suspend(ai_debug_company);
01086         if ((_pause_mode & PM_PAUSED_NORMAL) == PM_UNPAUSED) {
01087           DoCommandP(0, PM_PAUSED_NORMAL, 1, CMD_PAUSE);
01088         }
01089 
01090         /* Make it possible to click on the continue button */
01091         this->EnableWidget(AID_WIDGET_CONTINUE_BTN);
01092         this->SetWidgetDirty(AID_WIDGET_CONTINUE_BTN);
01093 
01094         /* Highlight row that matched */
01095         this->highlight_row = log->pos;
01096       }
01097 
01098       cur_company.Restore();
01099     }
01100   }
01101 
01102   virtual void OnResize()
01103   {
01104     this->vscroll->SetCapacityFromWidget(this, AID_WIDGET_LOG_PANEL);
01105   }
01106 };
01107 
01108 const int AIDebugWindow::top_offset = WD_FRAMERECT_TOP + 2;
01109 const int AIDebugWindow::bottom_offset = WD_FRAMERECT_BOTTOM;
01110 CompanyID AIDebugWindow::ai_debug_company = INVALID_COMPANY;
01111 char AIDebugWindow::break_string[MAX_BREAK_STR_STRING_LENGTH] = "";
01112 bool AIDebugWindow::break_check_enabled = true;
01113 bool AIDebugWindow::case_sensitive_break_check = false;
01114 
01116 NWidgetBase *MakeCompanyButtonRowsAIDebug(int *biggest_index)
01117 {
01118   return MakeCompanyButtonRows(biggest_index, AID_WIDGET_COMPANY_BUTTON_START, AID_WIDGET_COMPANY_BUTTON_END, 8, STR_AI_DEBUG_SELECT_AI_TOOLTIP);
01119 }
01120 
01122 static const NWidgetPart _nested_ai_debug_widgets[] = {
01123   NWidget(NWID_HORIZONTAL),
01124     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
01125     NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_AI_DEBUG, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
01126     NWidget(WWT_SHADEBOX, COLOUR_GREY),
01127     NWidget(WWT_STICKYBOX, COLOUR_GREY),
01128   EndContainer(),
01129   NWidget(WWT_PANEL, COLOUR_GREY, AID_WIDGET_VIEW),
01130     NWidgetFunction(MakeCompanyButtonRowsAIDebug), SetPadding(0, 2, 1, 2),
01131   EndContainer(),
01132   NWidget(NWID_HORIZONTAL),
01133     NWidget(WWT_TEXTBTN, COLOUR_GREY, AID_WIDGET_NAME_TEXT), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_JUST_STRING, STR_AI_DEBUG_NAME_TOOLTIP),
01134     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, AID_WIDGET_SETTINGS), SetMinimalSize(100, 20), SetDataTip(STR_AI_DEBUG_SETTINGS, STR_AI_DEBUG_SETTINGS_TOOLTIP),
01135     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, AID_WIDGET_RELOAD_TOGGLE), SetMinimalSize(100, 20), SetDataTip(STR_AI_DEBUG_RELOAD, STR_AI_DEBUG_RELOAD_TOOLTIP),
01136   EndContainer(),
01137   NWidget(NWID_HORIZONTAL),
01138     NWidget(NWID_VERTICAL),
01139       /* Log panel */
01140       NWidget(WWT_PANEL, COLOUR_GREY, AID_WIDGET_LOG_PANEL), SetMinimalSize(287, 180), SetResize(1, 1), SetScrollbar(AID_WIDGET_SCROLLBAR),
01141       EndContainer(),
01142       /* Break string widgets */
01143       NWidget(NWID_SELECTION, INVALID_COLOUR, AID_BREAK_STRING_WIDGETS),
01144         NWidget(NWID_HORIZONTAL),
01145           NWidget(WWT_IMGBTN_2, COLOUR_GREY, AID_WIDGET_BREAK_STR_ON_OFF_BTN), SetFill(0, 1), SetDataTip(SPR_FLAG_VEH_STOPPED, STR_AI_DEBUG_BREAK_STR_ON_OFF_TOOLTIP),
01146           NWidget(WWT_PANEL, COLOUR_GREY),
01147             NWidget(NWID_HORIZONTAL),
01148               NWidget(WWT_LABEL, COLOUR_GREY), SetPadding(2, 2, 2, 4), SetDataTip(STR_AI_DEBUG_BREAK_ON_LABEL, 0x0),
01149               NWidget(WWT_EDITBOX, COLOUR_WHITE, AID_WIDGET_BREAK_STR_EDIT_BOX), SetFill(1, 1), SetResize(1, 0), SetPadding(2, 2, 2, 2), SetDataTip(STR_AI_DEBUG_BREAK_STR_OSKTITLE, STR_AI_DEBUG_BREAK_STR_TOOLTIP),
01150             EndContainer(),
01151           EndContainer(),
01152           NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, AID_WIDGET_MATCH_CASE_BTN), SetMinimalSize(100, 0), SetFill(0, 1), SetDataTip(STR_AI_DEBUG_MATCH_CASE, STR_AI_DEBUG_MATCH_CASE_TOOLTIP),
01153           NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, AID_WIDGET_CONTINUE_BTN), SetMinimalSize(100, 0), SetFill(0, 1), SetDataTip(STR_AI_DEBUG_CONTINUE, STR_AI_DEBUG_CONTINUE_TOOLTIP),
01154         EndContainer(),
01155       EndContainer(),
01156     EndContainer(),
01157     NWidget(NWID_VERTICAL),
01158       NWidget(NWID_VSCROLLBAR, COLOUR_GREY, AID_WIDGET_SCROLLBAR),
01159       NWidget(WWT_RESIZEBOX, COLOUR_GREY),
01160     EndContainer(),
01161   EndContainer(),
01162 };
01163 
01165 static const WindowDesc _ai_debug_desc(
01166   WDP_AUTO, 600, 450,
01167   WC_AI_DEBUG, WC_NONE,
01168   0,
01169   _nested_ai_debug_widgets, lengthof(_nested_ai_debug_widgets)
01170 );
01171 
01176 void ShowAIDebugWindow(CompanyID show_company)
01177 {
01178   if (!_networking || _network_server) {
01179     AIDebugWindow *w = (AIDebugWindow *)BringWindowToFrontById(WC_AI_DEBUG, 0);
01180     if (w == NULL) w = new AIDebugWindow(&_ai_debug_desc, 0);
01181     if (show_company != INVALID_COMPANY) w->ChangeToAI(show_company);
01182   } else {
01183     ShowErrorMessage(STR_ERROR_AI_DEBUG_SERVER_ONLY, INVALID_STRING_ID, WL_INFO);
01184   }
01185 }
01186 
01190 void InitializeAIGui()
01191 {
01192   AIDebugWindow::ai_debug_company = INVALID_COMPANY;
01193 }
01194 
01196 void ShowAIDebugWindowIfAIError()
01197 {
01198   /* Network clients can't debug AIs. */
01199   if (_networking && !_network_server) return;
01200 
01201   Company *c;
01202   FOR_ALL_COMPANIES(c) {
01203     if (c->is_ai && c->ai_instance->IsDead()) {
01204       ShowAIDebugWindow(c->index);
01205       break;
01206     }
01207   }
01208 }

Generated on Sun May 15 19:20:05 2011 for OpenTTD by  doxygen 1.6.1