ai_gui.cpp

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

Generated on Sun Jan 9 16:01:50 2011 for OpenTTD by  doxygen 1.6.1