# ai_gui.cpp

Go to the documentation of this file.

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