# ai_gui.cpp

Go to the documentation of this file.

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