# ai_gui.cpp

Go to the documentation of this file.

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