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           int old_val = new_val;
00394           if (bool_item) {
00395             new_val = !new_val;
00396           } else if (x >= 10) {
00397             /* Increase button clicked */
00398             new_val += config_item.step_size;
00399             if (new_val > config_item.max_value) new_val = config_item.max_value;
00400             this->clicked_increase = true;
00401           } else {
00402             /* Decrease button clicked */
00403             new_val -= config_item.step_size;
00404             if (new_val < config_item.min_value) new_val = config_item.min_value;
00405             this->clicked_increase = false;
00406           }
00407 
00408           if (new_val != old_val) {
00409             this->ai_config->SetSetting(config_item.name, new_val);
00410             this->clicked_button = num;
00411             this->timeout = 5;
00412 
00413             this->CheckDifficultyLevel();
00414 
00415             this->SetDirty();
00416           }
00417         } else if (!bool_item) {
00418           /* Display a query box so users can enter a custom value. */
00419           this->clicked_row = num;
00420           SetDParam(0, this->ai_config->GetSetting(config_item.name));
00421           ShowQueryString(STR_JUST_INT, STR_CONFIG_SETTING_QUERY_CAPTION, 10, this, CS_NUMERAL, QSF_NONE);
00422         }
00423         break;
00424       }
00425 
00426       case AIS_WIDGET_ACCEPT:
00427         delete this;
00428         break;
00429 
00430       case AIS_WIDGET_RESET:
00431         if (_game_mode == GM_MENU || !Company::IsValidID(this->slot)) {
00432           this->ai_config->ResetSettings();
00433           this->SetDirty();
00434         }
00435         break;
00436     }
00437   }
00438 
00439   virtual void OnQueryTextFinished(char *str)
00440   {
00441     if (StrEmpty(str)) return;
00442     AIConfigItemList::const_iterator it = this->ai_config->GetConfigList()->begin();
00443     for (int i = 0; i < this->clicked_row; i++) it++;
00444     if (_game_mode == GM_NORMAL && Company::IsValidID(this->slot) && (it->flags & AICONFIG_INGAME) == 0) return;
00445     int32 value = atoi(str);
00446     this->ai_config->SetSetting((*it).name, value);
00447     this->CheckDifficultyLevel();
00448     this->SetDirty();
00449   }
00450 
00451   virtual void OnResize()
00452   {
00453     NWidgetCore *nwi = this->GetWidget<NWidgetCore>(AIS_WIDGET_BACKGROUND);
00454     this->vscroll->SetCapacity(nwi->current_y / this->line_height);
00455     nwi->widget_data = (this->vscroll->GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
00456   }
00457 
00458   virtual void OnTick()
00459   {
00460     if (--this->timeout == 0) {
00461       this->clicked_button = -1;
00462       this->SetDirty();
00463     }
00464   }
00465 
00471   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
00472   {
00473     if (_game_mode == GM_NORMAL && Company::IsValidID(this->slot)) delete this;
00474   }
00475 };
00476 
00478 static const NWidgetPart _nested_ai_settings_widgets[] = {
00479   NWidget(NWID_HORIZONTAL),
00480     NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
00481     NWidget(WWT_CAPTION, COLOUR_MAUVE), SetDataTip(STR_AI_SETTINGS_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00482   EndContainer(),
00483   NWidget(NWID_HORIZONTAL),
00484     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),
00485     NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, AIS_WIDGET_SCROLLBAR),
00486   EndContainer(),
00487   NWidget(NWID_HORIZONTAL),
00488     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
00489       NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, AIS_WIDGET_ACCEPT), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_AI_SETTINGS_CLOSE, STR_NULL),
00490       NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, AIS_WIDGET_RESET), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_AI_SETTINGS_RESET, STR_NULL),
00491     EndContainer(),
00492     NWidget(WWT_RESIZEBOX, COLOUR_MAUVE),
00493   EndContainer(),
00494 };
00495 
00497 static const WindowDesc _ai_settings_desc(
00498   WDP_CENTER, 500, 208,
00499   WC_AI_SETTINGS, WC_NONE,
00500   WDF_UNCLICK_BUTTONS,
00501   _nested_ai_settings_widgets, lengthof(_nested_ai_settings_widgets)
00502 );
00503 
00508 static void ShowAISettingsWindow(CompanyID slot)
00509 {
00510   DeleteWindowByClass(WC_AI_LIST);
00511   DeleteWindowByClass(WC_AI_SETTINGS);
00512   new AISettingsWindow(&_ai_settings_desc, slot);
00513 }
00514 
00516 enum AIConfigWindowWidgets {
00517   AIC_WIDGET_BACKGROUND,   
00518   AIC_WIDGET_DECREASE,     
00519   AIC_WIDGET_INCREASE,     
00520   AIC_WIDGET_NUMBER,       
00521   AIC_WIDGET_LIST,         
00522   AIC_WIDGET_SCROLLBAR,    
00523   AIC_WIDGET_MOVE_UP,      
00524   AIC_WIDGET_MOVE_DOWN,    
00525   AIC_WIDGET_CHANGE,       
00526   AIC_WIDGET_CONFIGURE,    
00527   AIC_WIDGET_CLOSE,        
00528   AIC_WIDGET_CONTENT_DOWNLOAD, 
00529 };
00530 
00532 static const NWidgetPart _nested_ai_config_widgets[] = {
00533   NWidget(NWID_HORIZONTAL),
00534     NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
00535     NWidget(WWT_CAPTION, COLOUR_MAUVE), SetDataTip(STR_AI_CONFIG_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00536   EndContainer(),
00537   NWidget(WWT_PANEL, COLOUR_MAUVE, AIC_WIDGET_BACKGROUND),
00538     NWidget(NWID_VERTICAL), SetPIP(4, 4, 4),
00539       NWidget(NWID_HORIZONTAL), SetPIP(10, 0, 10),
00540         NWidget(WWT_PUSHARROWBTN, COLOUR_YELLOW, AIC_WIDGET_DECREASE), SetFill(0, 1), SetDataTip(AWV_DECREASE, STR_NULL),
00541         NWidget(WWT_PUSHARROWBTN, COLOUR_YELLOW, AIC_WIDGET_INCREASE), SetFill(0, 1), SetDataTip(AWV_INCREASE, STR_NULL),
00542         NWidget(NWID_SPACER), SetMinimalSize(6, 0),
00543         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),
00544       EndContainer(),
00545       NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(10, 0, 10),
00546         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),
00547         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),
00548       EndContainer(),
00549     EndContainer(),
00550     NWidget(NWID_HORIZONTAL),
00551       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),
00552       NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, AIC_WIDGET_SCROLLBAR),
00553     EndContainer(),
00554     NWidget(NWID_SPACER), SetMinimalSize(0, 9),
00555     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(5, 0, 5),
00556       NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, AIC_WIDGET_CHANGE), SetFill(1, 0), SetMinimalSize(93, 12), SetDataTip(STR_AI_CONFIG_CHANGE, STR_AI_CONFIG_CHANGE_TOOLTIP),
00557       NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, AIC_WIDGET_CONFIGURE), SetFill(1, 0), SetMinimalSize(93, 12), SetDataTip(STR_AI_CONFIG_CONFIGURE, STR_AI_CONFIG_CONFIGURE_TOOLTIP),
00558       NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, AIC_WIDGET_CLOSE), SetFill(1, 0), SetMinimalSize(93, 12), SetDataTip(STR_AI_SETTINGS_CLOSE, STR_NULL),
00559     EndContainer(),
00560     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),
00561   EndContainer(),
00562 };
00563 
00565 static const WindowDesc _ai_config_desc(
00566   WDP_CENTER, 0, 0,
00567   WC_GAME_OPTIONS, WC_NONE,
00568   WDF_UNCLICK_BUTTONS,
00569   _nested_ai_config_widgets, lengthof(_nested_ai_config_widgets)
00570 );
00571 
00575 struct AIConfigWindow : public Window {
00576   CompanyID selected_slot; 
00577   int line_height;         
00578   Scrollbar *vscroll;      
00579 
00580   AIConfigWindow() : Window()
00581   {
00582     this->InitNested(&_ai_config_desc); // Initializes 'this->line_height' as a side effect.
00583     this->vscroll = this->GetScrollbar(AIC_WIDGET_SCROLLBAR);
00584     this->selected_slot = INVALID_COMPANY;
00585     NWidgetCore *nwi = this->GetWidget<NWidgetCore>(AIC_WIDGET_LIST);
00586     this->vscroll->SetCapacity(nwi->current_y / this->line_height);
00587     this->vscroll->SetCount(MAX_COMPANIES);
00588     nwi->widget_data = (this->vscroll->GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
00589     this->OnInvalidateData(0);
00590   }
00591 
00592   ~AIConfigWindow()
00593   {
00594     DeleteWindowByClass(WC_AI_LIST);
00595     DeleteWindowByClass(WC_AI_SETTINGS);
00596   }
00597 
00598   virtual void SetStringParameters(int widget) const
00599   {
00600     switch (widget) {
00601       case AIC_WIDGET_NUMBER:
00602         SetDParam(0, GetGameSettings().difficulty.max_no_competitors);
00603         break;
00604     }
00605   }
00606 
00607   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00608   {
00609     switch (widget) {
00610       case AIC_WIDGET_LIST:
00611         this->line_height = FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM;
00612         size->height = GB(this->GetWidget<NWidgetCore>(widget)->widget_data, MAT_ROW_START, MAT_ROW_BITS) * this->line_height;
00613         break;
00614     }
00615   }
00616 
00622   static bool IsEditable(CompanyID slot)
00623   {
00624     if (_game_mode != GM_NORMAL) {
00625       return slot > 0 && slot <= GetGameSettings().difficulty.max_no_competitors;
00626     }
00627     if (Company::IsValidID(slot) || slot < 0) return false;
00628 
00629     int max_slot = GetGameSettings().difficulty.max_no_competitors;
00630     for (CompanyID cid = COMPANY_FIRST; cid < (CompanyID)max_slot && cid < MAX_COMPANIES; cid++) {
00631       if (Company::IsValidHumanID(cid)) max_slot++;
00632     }
00633     return slot < max_slot;
00634   }
00635 
00636   virtual void DrawWidget(const Rect &r, int widget) const
00637   {
00638     switch (widget) {
00639       case AIC_WIDGET_LIST: {
00640         int y = r.top;
00641         for (int i = this->vscroll->GetPosition(); this->vscroll->IsVisible(i) && i < MAX_COMPANIES; i++) {
00642           StringID text;
00643 
00644           if ((_game_mode != GM_NORMAL && i == 0) || (_game_mode == GM_NORMAL && Company::IsValidHumanID(i))) {
00645             text = STR_AI_CONFIG_HUMAN_PLAYER;
00646           } else if (AIConfig::GetConfig((CompanyID)i)->GetInfo() != NULL) {
00647             SetDParamStr(0, AIConfig::GetConfig((CompanyID)i)->GetInfo()->GetName());
00648             text = STR_JUST_RAW_STRING;
00649           } else {
00650             text = STR_AI_CONFIG_RANDOM_AI;
00651           }
00652           DrawString(r.left + 10, r.right - 10, y + WD_MATRIX_TOP, text,
00653               (this->selected_slot == i) ? TC_WHITE : (IsEditable((CompanyID)i) ? TC_ORANGE : TC_SILVER));
00654           y += this->line_height;
00655         }
00656         break;
00657       }
00658     }
00659   }
00660 
00661   virtual void OnClick(Point pt, int widget, int click_count)
00662   {
00663     switch (widget) {
00664       case AIC_WIDGET_DECREASE:
00665       case AIC_WIDGET_INCREASE: {
00666         int new_value;
00667         if (widget == AIC_WIDGET_DECREASE) {
00668           new_value = max(0, GetGameSettings().difficulty.max_no_competitors - 1);
00669         } else {
00670           new_value = min(MAX_COMPANIES - 1, GetGameSettings().difficulty.max_no_competitors + 1);
00671         }
00672         IConsoleSetSetting("difficulty.max_no_competitors", new_value);
00673         this->InvalidateData();
00674         break;
00675       }
00676 
00677       case AIC_WIDGET_LIST: { // Select a slot
00678         this->selected_slot = (CompanyID)this->vscroll->GetScrolledRowFromWidget(pt.y, this, widget, 0, this->line_height);
00679         this->InvalidateData();
00680         if (click_count > 1 && this->selected_slot != INVALID_COMPANY) ShowAIListWindow((CompanyID)this->selected_slot);
00681         break;
00682       }
00683 
00684       case AIC_WIDGET_MOVE_UP:
00685         if (IsEditable(this->selected_slot) && IsEditable((CompanyID)(this->selected_slot - 1))) {
00686           Swap(GetGameSettings().ai_config[this->selected_slot], GetGameSettings().ai_config[this->selected_slot - 1]);
00687           this->selected_slot--;
00688           this->vscroll->ScrollTowards(this->selected_slot);
00689           this->InvalidateData();
00690         }
00691         break;
00692 
00693       case AIC_WIDGET_MOVE_DOWN:
00694         if (IsEditable(this->selected_slot) && IsEditable((CompanyID)(this->selected_slot + 1))) {
00695           Swap(GetGameSettings().ai_config[this->selected_slot], GetGameSettings().ai_config[this->selected_slot + 1]);
00696           this->selected_slot++;
00697           this->vscroll->ScrollTowards(this->selected_slot);
00698           this->InvalidateData();
00699         }
00700         break;
00701 
00702       case AIC_WIDGET_CHANGE:  // choose other AI
00703         ShowAIListWindow((CompanyID)this->selected_slot);
00704         break;
00705 
00706       case AIC_WIDGET_CONFIGURE: // change the settings for an AI
00707         ShowAISettingsWindow((CompanyID)this->selected_slot);
00708         break;
00709 
00710       case AIC_WIDGET_CLOSE:
00711         delete this;
00712         break;
00713 
00714       case AIC_WIDGET_CONTENT_DOWNLOAD:
00715         if (!_network_available) {
00716           ShowErrorMessage(STR_NETWORK_ERROR_NOTAVAILABLE, INVALID_STRING_ID, WL_ERROR);
00717         } else {
00718 #if defined(ENABLE_NETWORK)
00719           ShowNetworkContentListWindow(NULL, CONTENT_TYPE_AI);
00720 #endif
00721         }
00722         break;
00723     }
00724   }
00725 
00731   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
00732   {
00733     if (!IsEditable(this->selected_slot)) {
00734       this->selected_slot = INVALID_COMPANY;
00735     }
00736 
00737     if (!gui_scope) return;
00738 
00739     this->SetWidgetDisabledState(AIC_WIDGET_DECREASE, GetGameSettings().difficulty.max_no_competitors == 0);
00740     this->SetWidgetDisabledState(AIC_WIDGET_INCREASE, GetGameSettings().difficulty.max_no_competitors == MAX_COMPANIES - 1);
00741     this->SetWidgetDisabledState(AIC_WIDGET_CHANGE, this->selected_slot == INVALID_COMPANY);
00742     this->SetWidgetDisabledState(AIC_WIDGET_CONFIGURE, this->selected_slot == INVALID_COMPANY);
00743     this->SetWidgetDisabledState(AIC_WIDGET_MOVE_UP, this->selected_slot == INVALID_COMPANY || !IsEditable((CompanyID)(this->selected_slot - 1)));
00744     this->SetWidgetDisabledState(AIC_WIDGET_MOVE_DOWN, this->selected_slot == INVALID_COMPANY || !IsEditable((CompanyID)(this->selected_slot + 1)));
00745   }
00746 };
00747 
00749 void ShowAIConfigWindow()
00750 {
00751   DeleteWindowById(WC_GAME_OPTIONS, 0);
00752   new AIConfigWindow();
00753 }
00754 
00756 enum AIDebugWindowWidgets {
00757   AID_WIDGET_VIEW,
00758   AID_WIDGET_NAME_TEXT,
00759   AID_WIDGET_SETTINGS,
00760   AID_WIDGET_RELOAD_TOGGLE,
00761   AID_WIDGET_LOG_PANEL,
00762   AID_WIDGET_SCROLLBAR,
00763   AID_WIDGET_COMPANY_BUTTON_START,
00764   AID_WIDGET_COMPANY_BUTTON_END = AID_WIDGET_COMPANY_BUTTON_START + MAX_COMPANIES - 1,
00765   AID_BREAK_STRING_WIDGETS,
00766   AID_WIDGET_BREAK_STR_ON_OFF_BTN,
00767   AID_WIDGET_BREAK_STR_EDIT_BOX,
00768   AID_WIDGET_MATCH_CASE_BTN,
00769   AID_WIDGET_CONTINUE_BTN,
00770 };
00771 
00775 struct AIDebugWindow : public QueryStringBaseWindow {
00776   static const int top_offset;    
00777   static const int bottom_offset; 
00778 
00779   static const unsigned int MAX_BREAK_STR_STRING_LENGTH = 256; 
00780 
00781   static CompanyID ai_debug_company;                     
00782   int redraw_timer;                                      
00783   int last_vscroll_pos;                                  
00784   bool autoscroll;                                       
00785   bool show_break_box;                                   
00786   static bool break_check_enabled;                       
00787   static char break_string[MAX_BREAK_STR_STRING_LENGTH]; 
00788   static bool case_sensitive_break_check;                
00789   int highlight_row;                                     
00790   Scrollbar *vscroll;                                    
00791 
00797   AIDebugWindow(const WindowDesc *desc, WindowNumber number) : QueryStringBaseWindow(MAX_BREAK_STR_STRING_LENGTH)
00798   {
00799     this->CreateNestedTree(desc);
00800     this->vscroll = this->GetScrollbar(AID_WIDGET_SCROLLBAR);
00801     this->show_break_box = _settings_client.gui.ai_developer_tools;
00802     this->GetWidget<NWidgetStacked>(AID_BREAK_STRING_WIDGETS)->SetDisplayedPlane(this->show_break_box ? 0 : SZSP_HORIZONTAL);
00803     this->FinishInitNested(desc, number);
00804 
00805     if (!this->show_break_box) break_check_enabled = false;
00806     /* Disable the companies who are not active or not an AI */
00807     for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
00808       this->SetWidgetDisabledState(i + AID_WIDGET_COMPANY_BUTTON_START, !Company::IsValidAiID(i));
00809     }
00810     this->DisableWidget(AID_WIDGET_RELOAD_TOGGLE);
00811     this->DisableWidget(AID_WIDGET_SETTINGS);
00812     this->DisableWidget(AID_WIDGET_CONTINUE_BTN);
00813 
00814     this->last_vscroll_pos = 0;
00815     this->autoscroll = true;
00816     this->highlight_row = -1;
00817     InitializeTextBuffer(&this->text, this->edit_str_buf, this->edit_str_size, MAX_BREAK_STR_STRING_LENGTH);
00818 
00819     /* Restore the break string value from static variable */
00820     strecpy(this->edit_str_buf, this->break_string, this->edit_str_buf + MAX_BREAK_STR_STRING_LENGTH);
00821     UpdateTextBufferSize(&this->text);
00822 
00823     /* Restore button state from static class variables */
00824     if (ai_debug_company != INVALID_COMPANY) this->LowerWidget(ai_debug_company + AID_WIDGET_COMPANY_BUTTON_START);
00825     this->SetWidgetLoweredState(AID_WIDGET_BREAK_STR_ON_OFF_BTN, this->break_check_enabled);
00826     this->SetWidgetLoweredState(AID_WIDGET_MATCH_CASE_BTN, this->case_sensitive_break_check);
00827 
00828   }
00829 
00830   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00831   {
00832     if (widget == AID_WIDGET_LOG_PANEL) {
00833       resize->height = FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
00834       size->height = 14 * resize->height + this->top_offset + this->bottom_offset;
00835     }
00836   }
00837 
00838   virtual void OnPaint()
00839   {
00840     /* Check if the currently selected company is still active. */
00841     if (ai_debug_company == INVALID_COMPANY || !Company::IsValidAiID(ai_debug_company)) {
00842       if (ai_debug_company != INVALID_COMPANY) {
00843         /* Raise the widget for the previous selection. */
00844         this->RaiseWidget(ai_debug_company + AID_WIDGET_COMPANY_BUTTON_START);
00845 
00846         ai_debug_company = INVALID_COMPANY;
00847       }
00848 
00849       const Company *c;
00850       FOR_ALL_COMPANIES(c) {
00851         if (c->is_ai) {
00852           /* Lower the widget corresponding to this company. */
00853           this->LowerWidget(c->index + AID_WIDGET_COMPANY_BUTTON_START);
00854 
00855           ai_debug_company = c->index;
00856           break;
00857         }
00858       }
00859     }
00860 
00861     /* Update "Reload AI" and "AI settings" buttons */
00862     this->SetWidgetsDisabledState(ai_debug_company == INVALID_COMPANY,
00863       AID_WIDGET_RELOAD_TOGGLE,
00864       AID_WIDGET_SETTINGS,
00865       WIDGET_LIST_END);
00866 
00867     /* Draw standard stuff */
00868     this->DrawWidgets();
00869 
00870     if (this->IsShaded()) return; // Don't draw anything when the window is shaded.
00871 
00872     if (this->show_break_box) this->DrawEditBox(AID_WIDGET_BREAK_STR_EDIT_BOX);
00873 
00874     /* Paint the company icons */
00875     for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
00876       NWidgetCore *button = this->GetWidget<NWidgetCore>(i + AID_WIDGET_COMPANY_BUTTON_START);
00877       bool dirty = false;
00878 
00879       bool valid = Company::IsValidAiID(i);
00880       bool disabled = !valid;
00881       if (button->IsDisabled() != disabled) {
00882         /* Invalid/non-AI companies have button disabled */
00883         button->SetDisabled(disabled);
00884         dirty = true;
00885       }
00886 
00887       bool dead = valid && Company::Get(i)->ai_instance->IsDead();
00888       Colours colour = dead ? COLOUR_RED : COLOUR_GREY;
00889       if (button->colour != colour) {
00890         /* Mark dead AIs by red background */
00891         button->colour = colour;
00892         dirty = true;
00893       }
00894 
00895       /* Do we need a repaint? */
00896       if (dirty) this->SetDirty();
00897       /* Draw company icon only for valid AI companies */
00898       if (!valid) continue;
00899 
00900       byte offset = (i == ai_debug_company) ? 1 : 0;
00901       DrawCompanyIcon(i, button->pos_x + button->current_x / 2 - 7 + offset, this->GetWidget<NWidgetBase>(AID_WIDGET_COMPANY_BUTTON_START + i)->pos_y + 2 + offset);
00902     }
00903 
00904     /* If there are no active companies, don't display anything else. */
00905     if (ai_debug_company == INVALID_COMPANY) return;
00906 
00907     Backup<CompanyByte> cur_company(_current_company, ai_debug_company, FILE_LINE);
00908     AILog::LogData *log = (AILog::LogData *)AIObject::GetLogPointer();
00909     cur_company.Restore();
00910 
00911     int scroll_count = (log == NULL) ? 0 : log->used;
00912     if (this->vscroll->GetCount() != scroll_count) {
00913       this->vscroll->SetCount(scroll_count);
00914 
00915       /* We need a repaint */
00916       this->SetWidgetDirty(AID_WIDGET_SCROLLBAR);
00917     }
00918 
00919     if (log == NULL) return;
00920 
00921     /* Detect when the user scrolls the window. Enable autoscroll when the
00922      * bottom-most line becomes visible. */
00923     if (this->last_vscroll_pos != this->vscroll->GetPosition()) {
00924       this->autoscroll = this->vscroll->GetPosition() >= log->used - this->vscroll->GetCapacity();
00925     }
00926     if (this->autoscroll) {
00927       int scroll_pos = max(0, log->used - this->vscroll->GetCapacity());
00928       if (scroll_pos != this->vscroll->GetPosition()) {
00929         this->vscroll->SetPosition(scroll_pos);
00930 
00931         /* We need a repaint */
00932         this->SetWidgetDirty(AID_WIDGET_SCROLLBAR);
00933         this->SetWidgetDirty(AID_WIDGET_LOG_PANEL);
00934       }
00935     }
00936     this->last_vscroll_pos = this->vscroll->GetPosition();
00937   }
00938 
00939   virtual void SetStringParameters(int widget) const
00940   {
00941     switch (widget) {
00942       case AID_WIDGET_NAME_TEXT:
00943         if (ai_debug_company == INVALID_COMPANY || !Company::IsValidAiID(ai_debug_company)) {
00944           SetDParam(0, STR_EMPTY);
00945         } else {
00946           const AIInfo *info = Company::Get(ai_debug_company)->ai_info;
00947           assert(info != NULL);
00948           SetDParam(0, STR_AI_DEBUG_NAME_AND_VERSION);
00949           SetDParamStr(1, info->GetName());
00950           SetDParam(2, info->GetVersion());
00951         }
00952         break;
00953     }
00954   }
00955 
00956   virtual void DrawWidget(const Rect &r, int widget) const
00957   {
00958     if (ai_debug_company == INVALID_COMPANY) return;
00959 
00960     switch (widget) {
00961       case AID_WIDGET_LOG_PANEL: {
00962         Backup<CompanyByte> cur_company(_current_company, ai_debug_company, FILE_LINE);
00963         AILog::LogData *log = (AILog::LogData *)AIObject::GetLogPointer();
00964         cur_company.Restore();
00965         if (log == NULL) return;
00966 
00967         int y = this->top_offset;
00968         for (int i = this->vscroll->GetPosition(); this->vscroll->IsVisible(i) && i < log->used; i++) {
00969           int pos = (i + log->pos + 1 - log->used + log->count) % log->count;
00970           if (log->lines[pos] == NULL) break;
00971 
00972           TextColour colour;
00973           switch (log->type[pos]) {
00974             case AILog::LOG_SQ_INFO:  colour = TC_BLACK;  break;
00975             case AILog::LOG_SQ_ERROR: colour = TC_RED;    break;
00976             case AILog::LOG_INFO:     colour = TC_BLACK;  break;
00977             case AILog::LOG_WARNING:  colour = TC_YELLOW; break;
00978             case AILog::LOG_ERROR:    colour = TC_RED;    break;
00979             default:                  colour = TC_BLACK;  break;
00980           }
00981 
00982           /* Check if the current line should be highlighted */
00983           if (pos == this->highlight_row) {
00984             GfxFillRect(r.left + 1, r.top + y, r.right - 1, r.top + y + this->resize.step_height - WD_PAR_VSEP_NORMAL, PC_BLACK);
00985             if (colour == TC_BLACK) colour = TC_WHITE; // Make black text readable by inverting it to white.
00986           }
00987 
00988           DrawString(r.left + 7, r.right - 7, r.top + y, log->lines[pos], colour, SA_LEFT | SA_FORCE);
00989           y += this->resize.step_height;
00990         }
00991         break;
00992       }
00993     }
00994   }
00995 
01000   void ChangeToAI(CompanyID show_ai)
01001   {
01002     this->RaiseWidget(ai_debug_company + AID_WIDGET_COMPANY_BUTTON_START);
01003     ai_debug_company = show_ai;
01004 
01005     Backup<CompanyByte> cur_company(_current_company, ai_debug_company, FILE_LINE);
01006     AILog::LogData *log = (AILog::LogData *)AIObject::GetLogPointer();
01007     cur_company.Restore();
01008     this->vscroll->SetCount((log == NULL) ? 0 : log->used);
01009 
01010     this->LowerWidget(ai_debug_company + AID_WIDGET_COMPANY_BUTTON_START);
01011     this->autoscroll = true;
01012     this->last_vscroll_pos = this->vscroll->GetPosition();
01013     this->SetDirty();
01014     /* Close AI settings window to prevent confusion */
01015     DeleteWindowByClass(WC_AI_SETTINGS);
01016   }
01017 
01018   virtual void OnClick(Point pt, int widget, int click_count)
01019   {
01020     /* Check which button is clicked */
01021     if (IsInsideMM(widget, AID_WIDGET_COMPANY_BUTTON_START, AID_WIDGET_COMPANY_BUTTON_END + 1)) {
01022       /* Is it no on disable? */
01023       if (!this->IsWidgetDisabled(widget)) {
01024         ChangeToAI((CompanyID)(widget - AID_WIDGET_COMPANY_BUTTON_START));
01025       }
01026     }
01027 
01028     switch (widget) {
01029       case AID_WIDGET_RELOAD_TOGGLE:
01030         /* First kill the company of the AI, then start a new one. This should start the current AI again */
01031         DoCommandP(0, 2 | ai_debug_company << 16, 0, CMD_COMPANY_CTRL);
01032         DoCommandP(0, 1 | ai_debug_company << 16, 0, CMD_COMPANY_CTRL);
01033         break;
01034 
01035       case AID_WIDGET_SETTINGS:
01036         ShowAISettingsWindow(ai_debug_company);
01037         break;
01038 
01039       case AID_WIDGET_BREAK_STR_ON_OFF_BTN:
01040         this->break_check_enabled = !this->break_check_enabled;
01041         this->SetWidgetLoweredState(AID_WIDGET_BREAK_STR_ON_OFF_BTN, this->break_check_enabled);
01042         this->SetWidgetDirty(AID_WIDGET_BREAK_STR_ON_OFF_BTN);
01043         break;
01044 
01045       case AID_WIDGET_MATCH_CASE_BTN:
01046         this->case_sensitive_break_check = !this->case_sensitive_break_check;
01047         this->SetWidgetLoweredState(AID_WIDGET_MATCH_CASE_BTN, this->case_sensitive_break_check);
01048         break;
01049 
01050       case AID_WIDGET_CONTINUE_BTN:
01051         /* Unpause */
01052         DoCommandP(0, PM_PAUSED_NORMAL, 0, CMD_PAUSE);
01053         this->DisableWidget(AID_WIDGET_CONTINUE_BTN);
01054         this->RaiseWidget(AID_WIDGET_CONTINUE_BTN); // Disabled widgets don't raise themself
01055         break;
01056     }
01057   }
01058 
01059   virtual void OnTimeout()
01060   {
01061     this->RaiseWidget(AID_WIDGET_RELOAD_TOGGLE);
01062     this->RaiseWidget(AID_WIDGET_SETTINGS);
01063     this->SetDirty();
01064   }
01065 
01066   virtual void OnMouseLoop()
01067   {
01068     this->HandleEditBox(AID_WIDGET_BREAK_STR_EDIT_BOX);
01069   }
01070 
01071   virtual EventState OnKeyPress(uint16 key, uint16 keycode)
01072   {
01073     EventState state = ES_NOT_HANDLED;
01074     if (this->HandleEditBoxKey(AID_WIDGET_BREAK_STR_EDIT_BOX, key, keycode, state) != HEBR_NOT_FOCUSED) {
01075       /* Save the current string to static member so it can be restored next time the window is opened */
01076       strecpy(this->break_string, this->edit_str_buf, lastof(this->break_string));
01077     }
01078     return state;
01079   }
01080 
01086   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
01087   {
01088     if (data == -1 || ai_debug_company == data) this->SetDirty();
01089 
01090     if (gui_scope && data == -2) {
01091       /* The continue button should be disabled when the game is unpaused and
01092        * it was previously paused by the break string ( = a line in the log
01093        * was highlighted )*/
01094       if ((_pause_mode & PM_PAUSED_NORMAL) == PM_UNPAUSED && this->highlight_row != -1) {
01095         this->DisableWidget(AID_WIDGET_CONTINUE_BTN);
01096         this->SetWidgetDirty(AID_WIDGET_CONTINUE_BTN);
01097         this->SetWidgetDirty(AID_WIDGET_LOG_PANEL);
01098         this->highlight_row = -1;
01099       }
01100     }
01101 
01102     /* If the log message is related to the active company tab, check the break string.
01103      * This needs to be done in gameloop-scope, so the AI is suspended immediately. */
01104     if (!gui_scope && data == ai_debug_company && this->break_check_enabled && !StrEmpty(this->edit_str_buf)) {
01105       /* Get the log instance of the active company */
01106       Backup<CompanyByte> cur_company(_current_company, ai_debug_company, FILE_LINE);
01107       AILog::LogData *log = (AILog::LogData *)AIObject::GetLogPointer();
01108 
01109       if (log != NULL && case_sensitive_break_check?
01110           strstr(log->lines[log->pos], this->edit_str_buf) != 0 :
01111           strcasestr(log->lines[log->pos], this->edit_str_buf) != 0) {
01112 
01113         AI::Suspend(ai_debug_company);
01114         if ((_pause_mode & PM_PAUSED_NORMAL) == PM_UNPAUSED) {
01115           DoCommandP(0, PM_PAUSED_NORMAL, 1, CMD_PAUSE);
01116         }
01117 
01118         /* Make it possible to click on the continue button */
01119         this->EnableWidget(AID_WIDGET_CONTINUE_BTN);
01120         this->SetWidgetDirty(AID_WIDGET_CONTINUE_BTN);
01121 
01122         /* Highlight row that matched */
01123         this->highlight_row = log->pos;
01124       }
01125 
01126       cur_company.Restore();
01127     }
01128   }
01129 
01130   virtual void OnResize()
01131   {
01132     this->vscroll->SetCapacityFromWidget(this, AID_WIDGET_LOG_PANEL);
01133   }
01134 };
01135 
01136 const int AIDebugWindow::top_offset = WD_FRAMERECT_TOP + 2;
01137 const int AIDebugWindow::bottom_offset = WD_FRAMERECT_BOTTOM;
01138 CompanyID AIDebugWindow::ai_debug_company = INVALID_COMPANY;
01139 char AIDebugWindow::break_string[MAX_BREAK_STR_STRING_LENGTH] = "";
01140 bool AIDebugWindow::break_check_enabled = true;
01141 bool AIDebugWindow::case_sensitive_break_check = false;
01142 
01144 NWidgetBase *MakeCompanyButtonRowsAIDebug(int *biggest_index)
01145 {
01146   return MakeCompanyButtonRows(biggest_index, AID_WIDGET_COMPANY_BUTTON_START, AID_WIDGET_COMPANY_BUTTON_END, 8, STR_AI_DEBUG_SELECT_AI_TOOLTIP);
01147 }
01148 
01150 static const NWidgetPart _nested_ai_debug_widgets[] = {
01151   NWidget(NWID_HORIZONTAL),
01152     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
01153     NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_AI_DEBUG, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
01154     NWidget(WWT_SHADEBOX, COLOUR_GREY),
01155     NWidget(WWT_STICKYBOX, COLOUR_GREY),
01156   EndContainer(),
01157   NWidget(WWT_PANEL, COLOUR_GREY, AID_WIDGET_VIEW),
01158     NWidgetFunction(MakeCompanyButtonRowsAIDebug), SetPadding(0, 2, 1, 2),
01159   EndContainer(),
01160   NWidget(NWID_HORIZONTAL),
01161     NWidget(WWT_TEXTBTN, COLOUR_GREY, AID_WIDGET_NAME_TEXT), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_JUST_STRING, STR_AI_DEBUG_NAME_TOOLTIP),
01162     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, AID_WIDGET_SETTINGS), SetMinimalSize(100, 20), SetDataTip(STR_AI_DEBUG_SETTINGS, STR_AI_DEBUG_SETTINGS_TOOLTIP),
01163     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, AID_WIDGET_RELOAD_TOGGLE), SetMinimalSize(100, 20), SetDataTip(STR_AI_DEBUG_RELOAD, STR_AI_DEBUG_RELOAD_TOOLTIP),
01164   EndContainer(),
01165   NWidget(NWID_HORIZONTAL),
01166     NWidget(NWID_VERTICAL),
01167       /* Log panel */
01168       NWidget(WWT_PANEL, COLOUR_GREY, AID_WIDGET_LOG_PANEL), SetMinimalSize(287, 180), SetResize(1, 1), SetScrollbar(AID_WIDGET_SCROLLBAR),
01169       EndContainer(),
01170       /* Break string widgets */
01171       NWidget(NWID_SELECTION, INVALID_COLOUR, AID_BREAK_STRING_WIDGETS),
01172         NWidget(NWID_HORIZONTAL),
01173           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),
01174           NWidget(WWT_PANEL, COLOUR_GREY),
01175             NWidget(NWID_HORIZONTAL),
01176               NWidget(WWT_LABEL, COLOUR_GREY), SetPadding(2, 2, 2, 4), SetDataTip(STR_AI_DEBUG_BREAK_ON_LABEL, 0x0),
01177               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),
01178             EndContainer(),
01179           EndContainer(),
01180           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),
01181           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),
01182         EndContainer(),
01183       EndContainer(),
01184     EndContainer(),
01185     NWidget(NWID_VERTICAL),
01186       NWidget(NWID_VSCROLLBAR, COLOUR_GREY, AID_WIDGET_SCROLLBAR),
01187       NWidget(WWT_RESIZEBOX, COLOUR_GREY),
01188     EndContainer(),
01189   EndContainer(),
01190 };
01191 
01193 static const WindowDesc _ai_debug_desc(
01194   WDP_AUTO, 600, 450,
01195   WC_AI_DEBUG, WC_NONE,
01196   0,
01197   _nested_ai_debug_widgets, lengthof(_nested_ai_debug_widgets)
01198 );
01199 
01204 void ShowAIDebugWindow(CompanyID show_company)
01205 {
01206   if (!_networking || _network_server) {
01207     AIDebugWindow *w = (AIDebugWindow *)BringWindowToFrontById(WC_AI_DEBUG, 0);
01208     if (w == NULL) w = new AIDebugWindow(&_ai_debug_desc, 0);
01209     if (show_company != INVALID_COMPANY) w->ChangeToAI(show_company);
01210   } else {
01211     ShowErrorMessage(STR_ERROR_AI_DEBUG_SERVER_ONLY, INVALID_STRING_ID, WL_INFO);
01212   }
01213 }
01214 
01218 void InitializeAIGui()
01219 {
01220   AIDebugWindow::ai_debug_company = INVALID_COMPANY;
01221 }
01222 
01224 void ShowAIDebugWindowIfAIError()
01225 {
01226   /* Network clients can't debug AIs. */
01227   if (_networking && !_network_server) return;
01228 
01229   Company *c;
01230   FOR_ALL_COMPANIES(c) {
01231     if (c->is_ai && c->ai_instance->IsDead()) {
01232       ShowAIDebugWindow(c->index);
01233       break;
01234     }
01235   }
01236 }

Generated on Sun Jun 5 04:19:52 2011 for OpenTTD by  doxygen 1.6.1