# ai_gui.cpp

Go to the documentation of this file.

    00001 /* $Id: ai_gui.cpp 20576 2010-08-20 10:31: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 "../openttd.h"
    00014 #include "../gui.h"
    00015 #include "../window_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 "../textbuf_gui.h"
    00025 #include "../settings_func.h"
    00026 #include "../network/network_content.h"
    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
    00053   AIListWindow(const WindowDesc *desc, CompanyID slot) : Window(),
    00054     slot(slot)
    00055   {
    00056     this->ai_info_list = AI::GetUniqueInfoList();
    00057
    00058     this->InitNested(desc); // Initializes 'this->line_height' as side effect.
    00059
    00060     this->vscroll.SetCount((int)this->ai_info_list->size() + 1);
    00061
    00062     /* Try if we can find the currently selected AI */
    00063     this->selected = -1;
    00064     if (AIConfig::GetConfig(slot)->HasAI()) {
    00065       AIInfo *info = AIConfig::GetConfig(slot)->GetInfo();
    00066       int i = 0;
    00067       for (AIInfoList::const_iterator it = this->ai_info_list->begin(); it != this->ai_info_list->end(); it++, i++) {
    00068         if ((*it).second == info) {
    00069           this->selected = i;
    00070           break;
    00071         }
    00072       }
    00073     }
    00074   }
    00075
    00076   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    00077   {
    00078     if (widget == AIL_WIDGET_LIST) {
    00079       this->line_height = FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM;
    00080
    00081       resize->width = 1;
    00082       resize->height = this->line_height;
    00083       size->height = GB(this->GetWidget<NWidgetCore>(widget)->widget_data, MAT_ROW_START, MAT_ROW_BITS) * this->line_height;
    00084     }
    00085   }
    00086
    00087   virtual void OnPaint()
    00088   {
    00089     this->DrawWidgets();
    00090   }
    00091
    00092   virtual void DrawWidget(const Rect &r, int widget) const
    00093   {
    00094     switch (widget) {
    00095       case AIL_WIDGET_LIST: {
    00096         /* Draw a list of all available AIs. */
    00097         int y = this->GetWidget<NWidgetBase>(AIL_WIDGET_LIST)->pos_y;
    00098         /* First AI in the list is hardcoded to random */
    00099         if (this->vscroll.IsVisible(0)) {
    00100           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);
    00101           y += this->line_height;
    00102         }
    00103         AIInfoList::const_iterator it = this->ai_info_list->begin();
    00104         for (int i = 1; it != this->ai_info_list->end(); i++, it++) {
    00105           if (this->vscroll.IsVisible(i)) {
    00106             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);
    00107             y += this->line_height;
    00108           }
    00109         }
    00110         break;
    00111       }
    00112       case AIL_WIDGET_INFO_BG: {
    00113         AIInfo *selected_info = NULL;
    00114         AIInfoList::const_iterator it = this->ai_info_list->begin();
    00115         for (int i = 1; selected_info == NULL && it != this->ai_info_list->end(); i++, it++) {
    00116           if (this->selected == i - 1) selected_info = (*it).second;
    00117         }
    00118         /* Some info about the currently selected AI. */
    00119         if (selected_info != NULL) {
    00120           int y = r.top + WD_FRAMERECT_TOP;
    00121           SetDParamStr(0, selected_info->GetAuthor());
    00122           DrawString(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, y, STR_AI_LIST_AUTHOR);
    00123           y += FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
    00124           SetDParam(0, selected_info->GetVersion());
    00125           DrawString(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, y, STR_AI_LIST_VERSION);
    00126           y += FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
    00127           if (selected_info->GetURL() != NULL) {
    00128             SetDParamStr(0, selected_info->GetURL());
    00129             DrawString(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, y, STR_AI_LIST_URL);
    00130             y += FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
    00131           }
    00132           SetDParamStr(0, selected_info->GetDescription());
    00133           DrawStringMultiLine(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, y, r.bottom - WD_FRAMERECT_BOTTOM, STR_JUST_RAW_STRING, TC_BLACK);
    00134         }
    00135         break;
    00136       }
    00137     }
    00138   }
    00139
    00140   void ChangeAI()
    00141   {
    00142     if (this->selected == -1) {
    00143       AIConfig::GetConfig(slot)->ChangeAI(NULL);
    00144     } else {
    00145       AIInfoList::const_iterator it = this->ai_info_list->begin();
    00146       for (int i = 0; i < this->selected; i++) it++;
    00147       AIConfig::GetConfig(slot)->ChangeAI((*it).second->GetName(), (*it).second->GetVersion());
    00148     }
    00149     SetWindowDirty(WC_GAME_OPTIONS, 0);
    00150   }
    00151
    00152   virtual void OnClick(Point pt, int widget, int click_count)
    00153   {
    00154     switch (widget) {
    00155       case AIL_WIDGET_LIST: { // Select one of the AIs
    00156         int sel = (pt.y - this->GetWidget<NWidgetBase>(AIL_WIDGET_LIST)->pos_y) / this->line_height + this->vscroll.GetPosition() - 1;
    00157         if (sel < (int)this->ai_info_list->size()) {
    00158           this->selected = sel;
    00159           this->SetDirty();
    00160           if (click_count > 1) {
    00161             this->ChangeAI();
    00162             delete this;
    00163           }
    00164         }
    00165         break;
    00166       }
    00167
    00168       case AIL_WIDGET_ACCEPT: {
    00169         this->ChangeAI();
    00170         delete this;
    00171         break;
    00172       }
    00173
    00174       case AIL_WIDGET_CANCEL:
    00175         delete this;
    00176         break;
    00177     }
    00178   }
    00179
    00180   virtual void OnResize()
    00181   {
    00182     NWidgetCore *nwi = this->GetWidget<NWidgetCore>(AIL_WIDGET_LIST);
    00183     this->vscroll.SetCapacity(nwi->current_y / this->line_height);
    00184     nwi->widget_data = (this->vscroll.GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
    00185   }
    00186 };
    00187
    00188 static const NWidgetPart _nested_ai_list_widgets[] = {
    00189   NWidget(NWID_HORIZONTAL),
    00190     NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
    00191     NWidget(WWT_CAPTION, COLOUR_MAUVE), SetDataTip(STR_AI_LIST_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    00192   EndContainer(),
    00193   NWidget(NWID_HORIZONTAL),
    00194     NWidget(WWT_MATRIX, COLOUR_MAUVE, AIL_WIDGET_LIST), SetMinimalSize(188, 112), SetFill(1, 1), SetResize(1, 1), SetDataTip(0x501, STR_AI_LIST_TOOLTIP),
    00195     NWidget(WWT_SCROLLBAR, COLOUR_MAUVE, AIL_WIDGET_SCROLLBAR),
    00196   EndContainer(),
    00197   NWidget(WWT_PANEL, COLOUR_MAUVE, AIL_WIDGET_INFO_BG), SetMinimalTextLines(8, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM), SetResize(1, 0),
    00198   EndContainer(),
    00199     NWidget(NWID_HORIZONTAL),
    00200     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
    00201       NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, AIL_WIDGET_ACCEPT), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_AI_LIST_ACCEPT, STR_AI_LIST_ACCEPT_TOOLTIP),
    00202       NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, AIL_WIDGET_CANCEL), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_AI_LIST_CANCEL, STR_AI_LIST_CANCEL_TOOLTIP),
    00203     EndContainer(),
    00204     NWidget(WWT_RESIZEBOX, COLOUR_MAUVE),
    00205   EndContainer(),
    00206 };
    00207
    00208 /* Window definition for the ai list window. */
    00209 static const WindowDesc _ai_list_desc(
    00210   WDP_CENTER, 200, 234,
    00211   WC_AI_LIST, WC_NONE,
    00212   WDF_UNCLICK_BUTTONS,
    00213   _nested_ai_list_widgets, lengthof(_nested_ai_list_widgets)
    00214 );
    00215
    00216 static void ShowAIListWindow(CompanyID slot)
    00217 {
    00218   DeleteWindowByClass(WC_AI_LIST);
    00219   new AIListWindow(&_ai_list_desc, slot);
    00220 }
    00221
    00223 enum AISettingsWindowWidgest {
    00224   AIS_WIDGET_BACKGROUND,
    00225   AIS_WIDGET_SCROLLBAR,
    00226   AIS_WIDGET_ACCEPT,
    00227   AIS_WIDGET_RESET,
    00228 };
    00229
    00233 struct AISettingsWindow : public Window {
    00234   CompanyID slot;
    00235   AIConfig *ai_config;
    00236   int clicked_button;
    00237   bool clicked_increase;
    00238   int timeout;
    00239   int clicked_row;
    00240   int line_height; // Height of a row in the matrix widget.
    00241
    00242   AISettingsWindow(const WindowDesc *desc, CompanyID slot) : Window(),
    00243     slot(slot),
    00244     clicked_button(-1),
    00245     timeout(0)
    00246   {
    00247     this->ai_config = AIConfig::GetConfig(slot);
    00248
    00249     this->InitNested(desc, slot);  // Initializes 'this->line_height' as side effect.
    00250
    00251     this->SetWidgetDisabledState(AIS_WIDGET_RESET, _game_mode != GM_MENU);
    00252
    00253     this->vscroll.SetCount((int)this->ai_config->GetConfigList()->size());
    00254   }
    00255
    00256   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    00257   {
    00258     if (widget == AIS_WIDGET_BACKGROUND) {
    00259       this->line_height = FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM;
    00260
    00261       resize->width = 1;
    00262       resize->height = this->line_height;
    00263       size->height = GB(this->GetWidget<NWidgetCore>(widget)->widget_data, MAT_ROW_START, MAT_ROW_BITS) * this->line_height;
    00264     }
    00265   }
    00266
    00267   virtual void OnPaint()
    00268   {
    00269     this->DrawWidgets();
    00270   }
    00271
    00272   virtual void DrawWidget(const Rect &r, int widget) const
    00273   {
    00274     if (widget != AIS_WIDGET_BACKGROUND) return;
    00275
    00276     AIConfig *config = this->ai_config;
    00277     AIConfigItemList::const_iterator it = config->GetConfigList()->begin();
    00278     int i = 0;
    00279     for (; !this->vscroll.IsVisible(i); i++) it++;
    00280
    00281     bool rtl = _dynlang.text_dir == TD_RTL;
    00282     uint buttons_left = rtl ? r.right - 23 : r.left + 4;
    00283     uint value_left   = r.left + (rtl ? WD_FRAMERECT_LEFT : 28);
    00284     uint value_right  = r.right - (rtl ? 28 : WD_FRAMERECT_RIGHT);
    00285     uint text_left    = r.left + (rtl ? WD_FRAMERECT_LEFT : 54);
    00286     uint text_right   = r.right - (rtl ? 54 : WD_FRAMERECT_RIGHT);
    00287
    00288
    00289     int y = r.top;
    00290     for (; this->vscroll.IsVisible(i) && it != config->GetConfigList()->end(); i++, it++) {
    00291       int current_value = config->GetSetting((*it).name);
    00292       bool editable = (_game_mode == GM_MENU) || ((it->flags & AICONFIG_INGAME) != 0);
    00293
    00294       uint x = rtl ? r.right : r.left;
    00295       if (((*it).flags & AICONFIG_BOOLEAN) != 0) {
    00296         DrawFrameRect(buttons_left, y  + 2, buttons_left + 19, y + 10, (current_value != 0) ? COLOUR_GREEN : COLOUR_RED, (current_value != 0) ? FR_LOWERED : FR_NONE);
    00297       } else {
    00298         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);
    00299         if (it->labels != NULL && it->labels->Find(current_value) != it->labels->End()) {
    00300           x = DrawString(value_left, value_right, y + WD_MATRIX_TOP, it->labels->Find(current_value)->second, TC_ORANGE);
    00301         } else {
    00302           SetDParam(0, current_value);
    00303           x = DrawString(value_left, value_right, y + WD_MATRIX_TOP, STR_JUST_INT, TC_ORANGE);
    00304         }
    00305       }
    00306
    00307       DrawString(max(rtl ? 0U : x + 3, text_left), min(rtl ? x - 3 : r.right, text_right), y + WD_MATRIX_TOP, (*it).description, TC_LIGHT_BLUE);
    00308       y += this->line_height;
    00309     }
    00310   }
    00311
    00312   void CheckDifficultyLevel()
    00313   {
    00314     if (_game_mode == GM_MENU) {
    00315       if (_settings_newgame.difficulty.diff_level != 3) {
    00316         _settings_newgame.difficulty.diff_level = 3;
    00317         ShowErrorMessage(STR_WARNING_DIFFICULTY_TO_CUSTOM, INVALID_STRING_ID, 0, 0);
    00318       }
    00319     } else if (_settings_game.difficulty.diff_level != 3) {
    00320       IConsoleSetSetting("difficulty.diff_level", 3);
    00321     }
    00322   }
    00323
    00324   virtual void OnClick(Point pt, int widget, int click_count)
    00325   {
    00326     switch (widget) {
    00327       case AIS_WIDGET_BACKGROUND: {
    00328         const NWidgetBase *wid = this->GetWidget<NWidgetBase>(AIS_WIDGET_BACKGROUND);
    00329         int num = (pt.y - wid->pos_y) / this->line_height + this->vscroll.GetPosition();
    00330         if (num >= (int)this->ai_config->GetConfigList()->size()) break;
    00331
    00332         AIConfigItemList::const_iterator it = this->ai_config->GetConfigList()->begin();
    00333         for (int i = 0; i < num; i++) it++;
    00334         AIConfigItem config_item = *it;
    00335         if (_game_mode != GM_MENU && (config_item.flags & AICONFIG_INGAME) == 0) return;
    00336
    00337         bool bool_item = (config_item.flags & AICONFIG_BOOLEAN) != 0;
    00338
    00339         int x = pt.x - wid->pos_x;
    00340         if (_dynlang.text_dir == TD_RTL) x = wid->current_x - x;
    00341         x -= 4;
    00342         /* One of the arrows is clicked (or green/red rect in case of bool value) */
    00343         if (IsInsideMM(x, 0, 21)) {
    00344           int new_val = this->ai_config->GetSetting(config_item.name);
    00345           if (bool_item) {
    00346             new_val = !new_val;
    00347           } else if (x >= 10) {
    00348             /* Increase button clicked */
    00349             new_val += config_item.step_size;
    00350             if (new_val > config_item.max_value) new_val = config_item.max_value;
    00351             this->clicked_increase = true;
    00352           } else {
    00353             /* Decrease button clicked */
    00354             new_val -= config_item.step_size;
    00355             if (new_val < config_item.min_value) new_val = config_item.min_value;
    00356             this->clicked_increase = false;
    00357           }
    00358
    00359           this->ai_config->SetSetting(config_item.name, new_val);
    00360           this->clicked_button = num;
    00361           this->timeout = 5;
    00362
    00363           this->CheckDifficultyLevel();
    00364         } else if (!bool_item) {
    00365           /* Display a query box so users can enter a custom value. */
    00366           this->clicked_row = num;
    00367           SetDParam(0, this->ai_config->GetSetting(config_item.name));
    00368           ShowQueryString(STR_JUST_INT, STR_CONFIG_SETTING_QUERY_CAPTION, 10, 100, this, CS_NUMERAL, QSF_NONE);
    00369         }
    00370
    00371         this->SetDirty();
    00372         break;
    00373       }
    00374
    00375       case AIS_WIDGET_ACCEPT:
    00376         delete this;
    00377         break;
    00378
    00379       case AIS_WIDGET_RESET:
    00380         this->ai_config->ResetSettings();
    00381         this->SetDirty();
    00382         break;
    00383     }
    00384   }
    00385
    00386   virtual void OnQueryTextFinished(char *str)
    00387   {
    00388     if (StrEmpty(str)) return;
    00389     AIConfigItemList::const_iterator it = this->ai_config->GetConfigList()->begin();
    00390     for (int i = 0; i < this->clicked_row; i++) it++;
    00391     int32 value = atoi(str);
    00392     this->ai_config->SetSetting((*it).name, value);
    00393     this->CheckDifficultyLevel();
    00394     this->SetDirty();
    00395   }
    00396
    00397   virtual void OnResize()
    00398   {
    00399     NWidgetCore *nwi = this->GetWidget<NWidgetCore>(AIS_WIDGET_BACKGROUND);
    00400     this->vscroll.SetCapacity(nwi->current_y / this->line_height);
    00401     nwi->widget_data = (this->vscroll.GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
    00402   }
    00403
    00404   virtual void OnTick()
    00405   {
    00406     if (--this->timeout == 0) {
    00407       this->clicked_button = -1;
    00408       this->SetDirty();
    00409     }
    00410   }
    00411 };
    00412
    00413 static const NWidgetPart _nested_ai_settings_widgets[] = {
    00414   NWidget(NWID_HORIZONTAL),
    00415     NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
    00416     NWidget(WWT_CAPTION, COLOUR_MAUVE), SetDataTip(STR_AI_SETTINGS_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    00417   EndContainer(),
    00418   NWidget(NWID_HORIZONTAL),
    00419     NWidget(WWT_MATRIX, COLOUR_MAUVE, AIS_WIDGET_BACKGROUND), SetMinimalSize(188, 182), SetResize(1, 1), SetFill(1, 0), SetDataTip(0x501, STR_NULL),
    00420     NWidget(WWT_SCROLLBAR, COLOUR_MAUVE, AIS_WIDGET_SCROLLBAR),
    00421   EndContainer(),
    00422   NWidget(NWID_HORIZONTAL),
    00423     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
    00424       NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, AIS_WIDGET_ACCEPT), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_AI_SETTINGS_CLOSE, STR_NULL),
    00425       NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, AIS_WIDGET_RESET), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_AI_SETTINGS_RESET, STR_NULL),
    00426     EndContainer(),
    00427     NWidget(WWT_RESIZEBOX, COLOUR_MAUVE),
    00428   EndContainer(),
    00429 };
    00430
    00431 /* Window definition for the AI settings window. */
    00432 static const WindowDesc _ai_settings_desc(
    00433   WDP_CENTER, 500, 208,
    00434   WC_AI_SETTINGS, WC_NONE,
    00435   WDF_UNCLICK_BUTTONS,
    00436   _nested_ai_settings_widgets, lengthof(_nested_ai_settings_widgets)
    00437 );
    00438
    00439 static void ShowAISettingsWindow(CompanyID slot)
    00440 {
    00441   DeleteWindowByClass(WC_AI_LIST);
    00442   DeleteWindowByClass(WC_AI_SETTINGS);
    00443   new AISettingsWindow(&_ai_settings_desc, slot);
    00444 }
    00445
    00447 enum AIConfigWindowWidgets {
    00448   AIC_WIDGET_BACKGROUND,
    00449   AIC_WIDGET_DECREASE,
    00450   AIC_WIDGET_INCREASE,
    00451   AIC_WIDGET_NUMBER,
    00452   AIC_WIDGET_LIST,
    00453   AIC_WIDGET_SCROLLBAR,
    00454   AIC_WIDGET_MOVE_UP,
    00455   AIC_WIDGET_MOVE_DOWN,
    00456   AIC_WIDGET_CHANGE,
    00457   AIC_WIDGET_CONFIGURE,
    00458   AIC_WIDGET_CLOSE,
    00459   AIC_WIDGET_CONTENT_DOWNLOAD,
    00460 };
    00461
    00462 static const NWidgetPart _nested_ai_config_widgets[] = {
    00463   NWidget(NWID_HORIZONTAL),
    00464     NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
    00465     NWidget(WWT_CAPTION, COLOUR_MAUVE), SetDataTip(STR_AI_CONFIG_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    00466   EndContainer(),
    00467   NWidget(WWT_PANEL, COLOUR_MAUVE, AIC_WIDGET_BACKGROUND),
    00468     NWidget(NWID_VERTICAL), SetPIP(4, 4, 4),
    00469       NWidget(NWID_HORIZONTAL), SetPIP(10, 0, 10),
    00470         NWidget(NWID_BUTTON_ARROW, COLOUR_YELLOW, AIC_WIDGET_DECREASE), SetFill(0, 1), SetDataTip(AWV_DECREASE, STR_NULL),
    00471         NWidget(NWID_BUTTON_ARROW, COLOUR_YELLOW, AIC_WIDGET_INCREASE), SetFill(0, 1), SetDataTip(AWV_INCREASE, STR_NULL),
    00472         NWidget(NWID_SPACER), SetMinimalSize(6, 0),
    00473         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),
    00474       EndContainer(),
    00475       NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(10, 0, 10),
    00476         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),
    00477         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),
    00478       EndContainer(),
    00479     EndContainer(),
    00480     NWidget(NWID_HORIZONTAL),
    00481       NWidget(WWT_MATRIX, COLOUR_MAUVE, AIC_WIDGET_LIST), SetMinimalSize(288, 112), SetFill(1, 0), SetDataTip(0x801, STR_AI_CONFIG_LIST_TOOLTIP),
    00482       NWidget(WWT_SCROLLBAR, COLOUR_MAUVE, AIC_WIDGET_SCROLLBAR),
    00483     EndContainer(),
    00484     NWidget(NWID_SPACER), SetMinimalSize(0, 9),
    00485     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(5, 0, 5),
    00486       NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, AIC_WIDGET_CHANGE), SetFill(1, 0), SetMinimalSize(93, 12), SetDataTip(STR_AI_CONFIG_CHANGE, STR_AI_CONFIG_CHANGE_TOOLTIP),
    00487       NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, AIC_WIDGET_CONFIGURE), SetFill(1, 0), SetMinimalSize(93, 12), SetDataTip(STR_AI_CONFIG_CONFIGURE, STR_AI_CONFIG_CONFIGURE_TOOLTIP),
    00488       NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, AIC_WIDGET_CLOSE), SetFill(1, 0), SetMinimalSize(93, 12), SetDataTip(STR_AI_SETTINGS_CLOSE, STR_NULL),
    00489     EndContainer(),
    00490     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),
    00491   EndContainer(),
    00492 };
    00493
    00494 /* Window definition for the configure AI window. */
    00495 static const WindowDesc _ai_config_desc(
    00496   WDP_CENTER, 0, 0,
    00497   WC_GAME_OPTIONS, WC_NONE,
    00498   WDF_UNCLICK_BUTTONS,
    00499   _nested_ai_config_widgets, lengthof(_nested_ai_config_widgets)
    00500 );
    00501
    00505 struct AIConfigWindow : public Window {
    00506   CompanyID selected_slot;
    00507   bool clicked_button;
    00508   bool clicked_increase;
    00509   int timeout;
    00510   int line_height;
    00511
    00512   AIConfigWindow() : Window(),
    00513     clicked_button(false),
    00514     timeout(0)
    00515   {
    00516     this->InitNested(&_ai_config_desc); // Initializes 'this->line_height' as a side effect.
    00517     this->selected_slot = INVALID_COMPANY;
    00518     NWidgetCore *nwi = this->GetWidget<NWidgetCore>(AIC_WIDGET_LIST);
    00519     this->vscroll.SetCapacity(nwi->current_y / this->line_height);
    00520     this->vscroll.SetCount(MAX_COMPANIES);
    00521     nwi->widget_data = (this->vscroll.GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
    00522     this->OnInvalidateData(0);
    00523   }
    00524
    00525   ~AIConfigWindow()
    00526   {
    00527     DeleteWindowByClass(WC_AI_LIST);
    00528     DeleteWindowByClass(WC_AI_SETTINGS);
    00529   }
    00530
    00531   virtual void SetStringParameters(int widget) const
    00532   {
    00533     switch (widget) {
    00534       case AIC_WIDGET_NUMBER:
    00535         SetDParam(0, _settings_newgame.difficulty.max_no_competitors);
    00536         break;
    00537     }
    00538   }
    00539
    00540   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    00541   {
    00542     switch (widget) {
    00543       case AIC_WIDGET_LIST:
    00544         this->line_height = FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM;
    00545         size->height = GB(this->GetWidget<NWidgetCore>(widget)->widget_data, MAT_ROW_START, MAT_ROW_BITS) * this->line_height;
    00546         break;
    00547     }
    00548   }
    00549
    00550   virtual void OnPaint()
    00551   {
    00552     this->DrawWidgets();
    00553   }
    00554
    00555   virtual void DrawWidget(const Rect &r, int widget) const
    00556   {
    00557     switch (widget) {
    00558       case AIC_WIDGET_LIST: {
    00559         int y = r.top;
    00560         for (int i = this->vscroll.GetPosition(); this->vscroll.IsVisible(i) && i < MAX_COMPANIES; i++) {
    00561           StringID text;
    00562
    00563           if (AIConfig::GetConfig((CompanyID)i)->GetInfo() != NULL) {
    00564             SetDParamStr(0, AIConfig::GetConfig((CompanyID)i)->GetInfo()->GetName());
    00565             text = STR_JUST_RAW_STRING;
    00566           } else if (i == 0) {
    00567             text = STR_AI_CONFIG_HUMAN_PLAYER;
    00568           } else {
    00569             text = STR_AI_CONFIG_RANDOM_AI;
    00570           }
    00571           DrawString(r.left + 10, r.right - 10, y + WD_MATRIX_TOP, text,
    00572               (this->selected_slot == i) ? TC_WHITE : ((i > _settings_newgame.difficulty.max_no_competitors || i == 0) ? TC_SILVER : TC_ORANGE));
    00573           y += this->line_height;
    00574         }
    00575         break;
    00576       }
    00577     }
    00578   }
    00579
    00580   virtual void OnClick(Point pt, int widget, int click_count)
    00581   {
    00582     switch (widget) {
    00583       case AIC_WIDGET_DECREASE:
    00584       case AIC_WIDGET_INCREASE: {
    00585         int new_value;
    00586         if (widget == AIC_WIDGET_DECREASE) {
    00587           new_value = max(0, _settings_newgame.difficulty.max_no_competitors - 1);
    00588         } else {
    00589           new_value = min(MAX_COMPANIES - 1, _settings_newgame.difficulty.max_no_competitors + 1);
    00590         }
    00591         IConsoleSetSetting("difficulty.max_no_competitors", new_value);
    00592         this->InvalidateData();
    00593         break;
    00594       }
    00595
    00596       case AIC_WIDGET_LIST: { // Select a slot
    00597         this->selected_slot = (CompanyID)((pt.y - this->GetWidget<NWidgetBase>(widget)->pos_y) / this->line_height + this->vscroll.GetPosition());
    00598         this->InvalidateData();
    00599         if (click_count > 1 && this->selected_slot != INVALID_COMPANY) ShowAIListWindow((CompanyID)this->selected_slot);
    00600         break;
    00601       }
    00602
    00603       case AIC_WIDGET_MOVE_UP:
    00604         if (this->selected_slot > 1) {
    00605           Swap(_settings_newgame.ai_config[this->selected_slot], _settings_newgame.ai_config[this->selected_slot - 1]);
    00606           this->selected_slot--;
    00607           this->vscroll.ScrollTowards(this->selected_slot);
    00608           this->InvalidateData();
    00609         }
    00610         break;
    00611
    00612       case AIC_WIDGET_MOVE_DOWN:
    00613         if (this->selected_slot < _settings_newgame.difficulty.max_no_competitors) {
    00614           Swap(_settings_newgame.ai_config[this->selected_slot], _settings_newgame.ai_config[this->selected_slot + 1]);
    00615           this->selected_slot++;
    00616           this->vscroll.ScrollTowards(this->selected_slot);
    00617           this->InvalidateData();
    00618         }
    00619         break;
    00620
    00621       case AIC_WIDGET_CHANGE:  // choose other AI
    00622         ShowAIListWindow((CompanyID)this->selected_slot);
    00623         break;
    00624
    00625       case AIC_WIDGET_CONFIGURE: // change the settings for an AI
    00626         ShowAISettingsWindow((CompanyID)this->selected_slot);
    00627         break;
    00628
    00629       case AIC_WIDGET_CLOSE:
    00630         delete this;
    00631         break;
    00632
    00633       case AIC_WIDGET_CONTENT_DOWNLOAD:
    00634         if (!_network_available) {
    00635           ShowErrorMessage(STR_NETWORK_ERROR_NOTAVAILABLE, INVALID_STRING_ID, 0, 0);
    00636         } else {
    00637 #if defined(ENABLE_NETWORK)
    00638           ShowNetworkContentListWindow(NULL, CONTENT_TYPE_AI);
    00639 #endif
    00640         }
    00641         break;
    00642     }
    00643   }
    00644
    00645   virtual void OnInvalidateData(int data)
    00646   {
    00647     if (this->selected_slot == 0 || this->selected_slot > _settings_newgame.difficulty.max_no_competitors) {
    00648       this->selected_slot = INVALID_COMPANY;
    00649     }
    00650
    00651     this->SetWidgetDisabledState(AIC_WIDGET_DECREASE, _settings_newgame.difficulty.max_no_competitors == 0);
    00652     this->SetWidgetDisabledState(AIC_WIDGET_INCREASE, _settings_newgame.difficulty.max_no_competitors == MAX_COMPANIES - 1);
    00653     this->SetWidgetDisabledState(AIC_WIDGET_CHANGE, this->selected_slot == INVALID_COMPANY);
    00654     this->SetWidgetDisabledState(AIC_WIDGET_CONFIGURE, this->selected_slot == INVALID_COMPANY);
    00655     this->SetWidgetDisabledState(AIC_WIDGET_MOVE_UP, this->selected_slot == INVALID_COMPANY || this->selected_slot == 1);
    00656     this->SetWidgetDisabledState(AIC_WIDGET_MOVE_DOWN, this->selected_slot == INVALID_COMPANY || this->selected_slot == _settings_newgame.difficulty.max_no_competitors);
    00657   }
    00658
    00659   virtual void OnTick()
    00660   {
    00661     if (--this->timeout == 0) {
    00662       this->clicked_button = false;
    00663       this->SetDirty();
    00664     }
    00665   }
    00666 };
    00667
    00668 void ShowAIConfigWindow()
    00669 {
    00670   DeleteWindowById(WC_GAME_OPTIONS, 0);
    00671   new AIConfigWindow();
    00672 }
    00673
    00675 enum AIDebugWindowWidgets {
    00676   AID_WIDGET_VIEW,
    00677   AID_WIDGET_NAME_TEXT,
    00678   AID_WIDGET_SETTINGS,
    00679   AID_WIDGET_RELOAD_TOGGLE,
    00680   AID_WIDGET_LOG_PANEL,
    00681   AID_WIDGET_SCROLLBAR,
    00682   AID_WIDGET_COMPANY_BUTTON_START,
    00683   AID_WIDGET_COMPANY_BUTTON_END = AID_WIDGET_COMPANY_BUTTON_START + 14,
    00684 };
    00685
    00689 struct AIDebugWindow : public Window {
    00690   static const int top_offset;
    00691   static const int bottom_offset;
    00692
    00693   static CompanyID ai_debug_company;
    00694   int redraw_timer;
    00695   int last_vscroll_pos;
    00696   bool autoscroll;
    00697
    00698   AIDebugWindow(const WindowDesc *desc, WindowNumber number) : Window()
    00699   {
    00700     this->InitNested(desc, number);
    00701     /* Disable the companies who are not active or not an AI */
    00702     for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
    00703       this->SetWidgetDisabledState(i + AID_WIDGET_COMPANY_BUTTON_START, !Company::IsValidAiID(i));
    00704     }
    00705     this->DisableWidget(AID_WIDGET_RELOAD_TOGGLE);
    00706     this->DisableWidget(AID_WIDGET_SETTINGS);
    00707
    00708     this->last_vscroll_pos = 0;
    00709     this->autoscroll = true;
    00710
    00711     if (ai_debug_company != INVALID_COMPANY) this->LowerWidget(ai_debug_company + AID_WIDGET_COMPANY_BUTTON_START);
    00712   }
    00713
    00714   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    00715   {
    00716     if (widget == AID_WIDGET_LOG_PANEL) {
    00717       resize->height = FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
    00718       size->height = 14 * resize->height + this->top_offset + this->bottom_offset;
    00719     }
    00720   }
    00721
    00722   virtual void OnPaint()
    00723   {
    00724     /* Check if the currently selected company is still active. */
    00725     if (ai_debug_company == INVALID_COMPANY || !Company::IsValidAiID(ai_debug_company)) {
    00726       if (ai_debug_company != INVALID_COMPANY) {
    00727         /* Raise and disable the widget for the previous selection. */
    00728         this->RaiseWidget(ai_debug_company + AID_WIDGET_COMPANY_BUTTON_START);
    00729         this->DisableWidget(ai_debug_company + AID_WIDGET_COMPANY_BUTTON_START);
    00730
    00731         ai_debug_company = INVALID_COMPANY;
    00732       }
    00733
    00734       const Company *c;
    00735       FOR_ALL_COMPANIES(c) {
    00736         if (c->is_ai) {
    00737           /* Lower the widget corresponding to this company. */
    00738           this->LowerWidget(c->index + AID_WIDGET_COMPANY_BUTTON_START);
    00739
    00740           ai_debug_company = c->index;
    00741           break;
    00742         }
    00743       }
    00744     }
    00745
    00746     /* Update "Reload AI" and "AI settings" buttons */
    00747     this->SetWidgetsDisabledState(ai_debug_company == INVALID_COMPANY,
    00748       AID_WIDGET_RELOAD_TOGGLE,
    00749       AID_WIDGET_SETTINGS,
    00750       WIDGET_LIST_END);
    00751
    00752     /* Draw standard stuff */
    00753     this->DrawWidgets();
    00754
    00755     if (this->IsShaded()) return; // Don't draw anything when the window is shaded.
    00756
    00757     /* If there are no active companies, don't display anything else. */
    00758     if (ai_debug_company == INVALID_COMPANY) return;
    00759
    00760     /* Paint the company icons */
    00761     for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
    00762       NWidgetCore *button = this->GetWidget<NWidgetCore>(i + AID_WIDGET_COMPANY_BUTTON_START);
    00763       bool dirty = false;
    00764
    00765       bool valid = Company::IsValidAiID(i);
    00766       bool disabled = !valid;
    00767       if (button->IsDisabled() != disabled) {
    00768         /* Invalid/non-AI companies have button disabled */
    00769         button->SetDisabled(disabled);
    00770         dirty = true;
    00771       }
    00772
    00773       bool dead = valid && Company::Get(i)->ai_instance->IsDead();
    00774       Colours colour = dead ? COLOUR_RED : COLOUR_GREY;
    00775       if (button->colour != colour) {
    00776         /* Mark dead AIs by red background */
    00777         button->colour = colour;
    00778         dirty = true;
    00779       }
    00780
    00781       /* Do we need a repaint? */
    00782       if (dirty) this->SetDirty();
    00783       /* Draw company icon only for valid AI companies */
    00784       if (!valid) continue;
    00785
    00786       byte offset = (i == ai_debug_company) ? 1 : 0;
    00787       DrawCompanyIcon(i, button->pos_x + button->current_x / 2 - 7 + offset, this->GetWidget<NWidgetBase>(AID_WIDGET_COMPANY_BUTTON_START + i)->pos_y + 2 + offset);
    00788     }
    00789
    00790     CompanyID old_company = _current_company;
    00791     _current_company = ai_debug_company;
    00792     AILog::LogData *log = (AILog::LogData *)AIObject::GetLogPointer();
    00793     _current_company = old_company;
    00794
    00795     int scroll_count = (log == NULL) ? 0 : log->used;
    00796     if (this->vscroll.GetCount() != scroll_count) {
    00797       this->vscroll.SetCount(scroll_count);
    00798
    00799       /* We need a repaint */
    00800       this->SetWidgetDirty(AID_WIDGET_SCROLLBAR);
    00801     }
    00802
    00803     if (log == NULL) return;
    00804
    00805     /* Detect when the user scrolls the window. Enable autoscroll when the
    00806      * bottom-most line becomes visible. */
    00807     if (this->last_vscroll_pos != this->vscroll.GetPosition()) {
    00808       this->autoscroll = this->vscroll.GetPosition() >= log->used - this->vscroll.GetCapacity();
    00809     }
    00810     if (this->autoscroll) {
    00811       int scroll_pos = max(0, log->used - this->vscroll.GetCapacity());
    00812       if (scroll_pos != this->vscroll.GetPosition()) {
    00813         this->vscroll.SetPosition(scroll_pos);
    00814
    00815         /* We need a repaint */
    00816         this->SetWidgetDirty(AID_WIDGET_SCROLLBAR);
    00817         this->SetWidgetDirty(AID_WIDGET_LOG_PANEL);
    00818       }
    00819     }
    00820     this->last_vscroll_pos = this->vscroll.GetPosition();
    00821   }
    00822
    00823   virtual void SetStringParameters(int widget) const
    00824   {
    00825     switch (widget) {
    00826       case AID_WIDGET_NAME_TEXT:
    00827         if (ai_debug_company == INVALID_COMPANY || !Company::IsValidAiID(ai_debug_company)) {
    00828           SetDParam(0, STR_EMPTY);
    00829         } else {
    00830           const AIInfo *info = Company::Get(ai_debug_company)->ai_info;
    00831           assert(info != NULL);
    00832           SetDParam(0, STR_AI_DEBUG_NAME_AND_VERSION);
    00833           SetDParamStr(1, info->GetName());
    00834           SetDParam(2, info->GetVersion());
    00835         }
    00836         break;
    00837     }
    00838   }
    00839
    00840   virtual void DrawWidget(const Rect &r, int widget) const
    00841   {
    00842     if (ai_debug_company == INVALID_COMPANY) return;
    00843
    00844     switch (widget) {
    00845       case AID_WIDGET_LOG_PANEL: {
    00846         CompanyID old_company = _current_company;
    00847         _current_company = ai_debug_company;
    00848         AILog::LogData *log = (AILog::LogData *)AIObject::GetLogPointer();
    00849         _current_company = old_company;
    00850         if (log == NULL) return;
    00851
    00852         int y = this->top_offset;
    00853         for (int i = this->vscroll.GetPosition(); this->vscroll.IsVisible(i) && i < log->used; i++) {
    00854           uint pos = (i + log->pos + 1 - log->used + log->count) % log->count;
    00855           if (log->lines[pos] == NULL) break;
    00856
    00857           TextColour colour;
    00858           switch (log->type[pos]) {
    00859             case AILog::LOG_SQ_INFO:  colour = TC_BLACK;  break;
    00860             case AILog::LOG_SQ_ERROR: colour = TC_RED;    break;
    00861             case AILog::LOG_INFO:     colour = TC_BLACK;  break;
    00862             case AILog::LOG_WARNING:  colour = TC_YELLOW; break;
    00863             case AILog::LOG_ERROR:    colour = TC_RED;    break;
    00864             default:                  colour = TC_BLACK;  break;
    00865           }
    00866
    00867           DrawString(r.left + 7, r.right - 7, r.top + y, log->lines[pos], colour, SA_LEFT | SA_FORCE);
    00868           y += this->resize.step_height;
    00869         }
    00870         break;
    00871       }
    00872     }
    00873   }
    00874
    00875   void ChangeToAI(CompanyID show_ai)
    00876   {
    00877     this->RaiseWidget(ai_debug_company + AID_WIDGET_COMPANY_BUTTON_START);
    00878     ai_debug_company = show_ai;
    00879
    00880     CompanyID old_company = _current_company;
    00881     _current_company = ai_debug_company;
    00882     AILog::LogData *log = (AILog::LogData *)AIObject::GetLogPointer();
    00883     _current_company = old_company;
    00884     this->vscroll.SetCount((log == NULL) ? 0 : log->used);
    00885
    00886     this->LowerWidget(ai_debug_company + AID_WIDGET_COMPANY_BUTTON_START);
    00887     this->autoscroll = true;
    00888     this->last_vscroll_pos = this->vscroll.GetPosition();
    00889     this->SetDirty();
    00890     /* Close AI settings window to prevent confusion */
    00891     DeleteWindowByClass(WC_AI_SETTINGS);
    00892   }
    00893
    00894   virtual void OnClick(Point pt, int widget, int click_count)
    00895   {
    00896     /* Check which button is clicked */
    00897     if (IsInsideMM(widget, AID_WIDGET_COMPANY_BUTTON_START, AID_WIDGET_COMPANY_BUTTON_END + 1)) {
    00898       /* Is it no on disable? */
    00899       if (!this->IsWidgetDisabled(widget)) {
    00900         ChangeToAI((CompanyID)(widget - AID_WIDGET_COMPANY_BUTTON_START));
    00901       }
    00902     }
    00903
    00904     switch (widget) {
    00905       case AID_WIDGET_RELOAD_TOGGLE:
    00906         /* First kill the company of the AI, then start a new one. This should start the current AI again */
    00907         DoCommandP(0, 2, ai_debug_company, CMD_COMPANY_CTRL);
    00908         DoCommandP(0, 1, ai_debug_company, CMD_COMPANY_CTRL);
    00909         break;
    00910
    00911       case AID_WIDGET_SETTINGS:
    00912         ShowAISettingsWindow(ai_debug_company);
    00913         break;
    00914     }
    00915   }
    00916
    00917   virtual void OnTimeout()
    00918   {
    00919     this->RaiseWidget(AID_WIDGET_RELOAD_TOGGLE);
    00920     this->RaiseWidget(AID_WIDGET_SETTINGS);
    00921     this->SetDirty();
    00922   }
    00923
    00924   virtual void OnInvalidateData(int data = 0)
    00925   {
    00926     if (data == -1 || ai_debug_company == data) this->SetDirty();
    00927   }
    00928
    00929   virtual void OnResize()
    00930   {
    00931     this->vscroll.SetCapacityFromWidget(this, AID_WIDGET_LOG_PANEL);
    00932   }
    00933 };
    00934
    00935 const int AIDebugWindow::top_offset = WD_FRAMERECT_TOP + 2;
    00936 const int AIDebugWindow::bottom_offset = WD_FRAMERECT_BOTTOM;
    00937 CompanyID AIDebugWindow::ai_debug_company = INVALID_COMPANY;
    00938
    00939 static const NWidgetPart _nested_ai_debug_widgets[] = {
    00940   NWidget(NWID_HORIZONTAL),
    00941     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
    00942     NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_AI_DEBUG, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    00943     NWidget(WWT_SHADEBOX, COLOUR_GREY),
    00944     NWidget(WWT_STICKYBOX, COLOUR_GREY),
    00945   EndContainer(),
    00946   NWidget(WWT_PANEL, COLOUR_GREY, AID_WIDGET_VIEW),
    00947     NWidget(NWID_HORIZONTAL),
    00948       NWidget(NWID_SPACER), SetMinimalSize(2, 0),
    00949       NWidget(WWT_PANEL, COLOUR_GREY, AID_WIDGET_COMPANY_BUTTON_START), SetMinimalSize(37, 13), SetResize(1, 0), SetDataTip(0x0, STR_AI_DEBUG_SELECT_AI_TOOLTIP),
    00950       EndContainer(),
    00951       NWidget(WWT_PANEL, COLOUR_GREY, AID_WIDGET_COMPANY_BUTTON_START + 1), SetMinimalSize(37, 13), SetResize(1, 0), SetDataTip(0x0, STR_AI_DEBUG_SELECT_AI_TOOLTIP),
    00952       EndContainer(),
    00953       NWidget(WWT_PANEL, COLOUR_GREY, AID_WIDGET_COMPANY_BUTTON_START + 2), SetMinimalSize(37, 13), SetResize(1, 0), SetDataTip(0x0, STR_AI_DEBUG_SELECT_AI_TOOLTIP),
    00954       EndContainer(),
    00955       NWidget(WWT_PANEL, COLOUR_GREY, AID_WIDGET_COMPANY_BUTTON_START + 3), SetMinimalSize(37, 13), SetResize(1, 0), SetDataTip(0x0, STR_AI_DEBUG_SELECT_AI_TOOLTIP),
    00956       EndContainer(),
    00957       NWidget(WWT_PANEL, COLOUR_GREY, AID_WIDGET_COMPANY_BUTTON_START + 4), SetMinimalSize(37, 13), SetResize(1, 0), SetDataTip(0x0, STR_AI_DEBUG_SELECT_AI_TOOLTIP),
    00958       EndContainer(),
    00959       NWidget(WWT_PANEL, COLOUR_GREY, AID_WIDGET_COMPANY_BUTTON_START + 5), SetMinimalSize(37, 13), SetResize(1, 0), SetDataTip(0x0, STR_AI_DEBUG_SELECT_AI_TOOLTIP),
    00960       EndContainer(),
    00961       NWidget(WWT_PANEL, COLOUR_GREY, AID_WIDGET_COMPANY_BUTTON_START + 6), SetMinimalSize(37, 13), SetResize(1, 0), SetDataTip(0x0, STR_AI_DEBUG_SELECT_AI_TOOLTIP),
    00962       EndContainer(),
    00963       NWidget(WWT_PANEL, COLOUR_GREY, AID_WIDGET_COMPANY_BUTTON_START + 7), SetMinimalSize(37, 13), SetResize(1, 0), SetDataTip(0x0, STR_AI_DEBUG_SELECT_AI_TOOLTIP),
    00964       EndContainer(),
    00965       NWidget(NWID_SPACER), SetMinimalSize(2, 0),
    00966     EndContainer(),
    00967     NWidget(NWID_HORIZONTAL),
    00968       NWidget(NWID_SPACER), SetMinimalSize(2, 0),
    00969       NWidget(WWT_PANEL, COLOUR_GREY, AID_WIDGET_COMPANY_BUTTON_START + 8), SetMinimalSize(37, 13), SetResize(1, 0), SetDataTip(0x0, STR_AI_DEBUG_SELECT_AI_TOOLTIP),
    00970       EndContainer(),
    00971       NWidget(WWT_PANEL, COLOUR_GREY, AID_WIDGET_COMPANY_BUTTON_START + 9), SetMinimalSize(37, 13), SetResize(1, 0), SetDataTip(0x0, STR_AI_DEBUG_SELECT_AI_TOOLTIP),
    00972       EndContainer(),
    00973       NWidget(WWT_PANEL, COLOUR_GREY, AID_WIDGET_COMPANY_BUTTON_START + 10), SetMinimalSize(37, 13), SetResize(1, 0), SetDataTip(0x0, STR_AI_DEBUG_SELECT_AI_TOOLTIP),
    00974       EndContainer(),
    00975       NWidget(WWT_PANEL, COLOUR_GREY, AID_WIDGET_COMPANY_BUTTON_START + 11), SetMinimalSize(37, 13), SetResize(1, 0), SetDataTip(0x0, STR_AI_DEBUG_SELECT_AI_TOOLTIP),
    00976       EndContainer(),
    00977       NWidget(WWT_PANEL, COLOUR_GREY, AID_WIDGET_COMPANY_BUTTON_START + 12), SetMinimalSize(37, 13), SetResize(1, 0), SetDataTip(0x0, STR_AI_DEBUG_SELECT_AI_TOOLTIP),
    00978       EndContainer(),
    00979       NWidget(WWT_PANEL, COLOUR_GREY, AID_WIDGET_COMPANY_BUTTON_START + 13), SetMinimalSize(37, 13), SetResize(1, 0), SetDataTip(0x0, STR_AI_DEBUG_SELECT_AI_TOOLTIP),
    00980       EndContainer(),
    00981       NWidget(WWT_PANEL, COLOUR_GREY, AID_WIDGET_COMPANY_BUTTON_START + 14), SetMinimalSize(37, 13), SetResize(1, 0), SetDataTip(0x0, STR_AI_DEBUG_SELECT_AI_TOOLTIP),
    00982       EndContainer(),
    00983       NWidget(NWID_SPACER), SetMinimalSize(39, 0), SetResize(1, 0),
    00984     EndContainer(),
    00985     NWidget(NWID_SPACER), SetMinimalSize(0, 1), SetResize(1, 0),
    00986   EndContainer(),
    00987   NWidget(NWID_HORIZONTAL),
    00988     NWidget(WWT_TEXTBTN, COLOUR_GREY, AID_WIDGET_NAME_TEXT), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_JUST_STRING, STR_AI_DEBUG_NAME_TOOLTIP),
    00989     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, AID_WIDGET_SETTINGS), SetMinimalSize(100, 20), SetDataTip(STR_AI_DEBUG_SETTINGS, STR_AI_DEBUG_SETTINGS_TOOLTIP),
    00990     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, AID_WIDGET_RELOAD_TOGGLE), SetMinimalSize(100, 20), SetDataTip(STR_AI_DEBUG_RELOAD, STR_AI_DEBUG_RELOAD_TOOLTIP),
    00991   EndContainer(),
    00992   NWidget(NWID_HORIZONTAL),
    00993     NWidget(WWT_PANEL, COLOUR_GREY, AID_WIDGET_LOG_PANEL), SetMinimalSize(287, 180), SetResize(1, 1),
    00994     EndContainer(),
    00995     NWidget(NWID_VERTICAL),
    00996       NWidget(WWT_SCROLLBAR, COLOUR_GREY, AID_WIDGET_SCROLLBAR),
    00997       NWidget(WWT_RESIZEBOX, COLOUR_GREY),
    00998     EndContainer(),
    00999   EndContainer(),
    01000 };
    01001
    01002 static const WindowDesc _ai_debug_desc(
    01003   WDP_AUTO, 600, 450,
    01004   WC_AI_DEBUG, WC_NONE,
    01005   0,
    01006   _nested_ai_debug_widgets, lengthof(_nested_ai_debug_widgets)
    01007 );
    01008
    01009 void ShowAIDebugWindow(CompanyID show_company)
    01010 {
    01011   if (!_networking || _network_server) {
    01012     AIDebugWindow *w = (AIDebugWindow *)BringWindowToFrontById(WC_AI_DEBUG, 0);
    01013     if (w == NULL) w = new AIDebugWindow(&_ai_debug_desc, 0);
    01014     if (show_company != INVALID_COMPANY) w->ChangeToAI(show_company);
    01015   } else {
    01016     ShowErrorMessage(STR_ERROR_AI_DEBUG_SERVER_ONLY, INVALID_STRING_ID, 0, 0);
    01017   }
    01018 }
    01019
    01023 void InitializeAIGui()
    01024 {
    01025   AIDebugWindow::ai_debug_company = INVALID_COMPANY;
    01026 }
    01027
    01029 void ShowAIDebugWindowIfAIError()
    01030 {
    01031   /* Network clients can't debug AIs. */
    01032   if (_networking && !_network_server) return;
    01033
    01034   Company *c;
    01035   FOR_ALL_COMPANIES(c) {
    01036     if (c->is_ai && c->ai_instance->IsDead()) {
    01037       ShowAIDebugWindow(c->index);
    01038       break;
    01039     }
    01040   }
    01041 }