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

Generated on Mon May 9 05:18:50 2011 for OpenTTD by  doxygen 1.6.1