# 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 "../table/sprites.h"
    00014 #include "../error.h"
    00015 #include "../settings_gui.h"
    00016 #include "../querystring_gui.h"
    00017 #include "../stringfilter_type.h"
    00018 #include "../company_base.h"
    00019 #include "../company_gui.h"
    00020 #include "../strings_func.h"
    00021 #include "../window_func.h"
    00022 #include "../gfx_func.h"
    00023 #include "../command_func.h"
    00024 #include "../network/network.h"
    00025 #include "../settings_func.h"
    00026 #include "../network/network_content.h"
    00027 #include "../textfile_gui.h"
    00028 #include "../widgets/dropdown_type.h"
    00029 #include "../widgets/dropdown_func.h"
    00030 #include "../hotkeys.h"
    00031
    00032 #include "ai.hpp"
    00033 #include "../script/api/script_log.hpp"
    00034 #include "ai_config.hpp"
    00035 #include "ai_info.hpp"
    00036 #include "ai_instance.hpp"
    00037 #include "../game/game.hpp"
    00038 #include "../game/game_config.hpp"
    00039 #include "../game/game_info.hpp"
    00040 #include "../game/game_instance.hpp"
    00041
    00042
    00043 #include "table/strings.h"
    00044
    00045 #include <vector>
    00046
    00047 static ScriptConfig *GetConfig(CompanyID slot)
    00048 {
    00049   if (slot == OWNER_DEITY) return GameConfig::GetConfig();
    00050   return AIConfig::GetConfig(slot);
    00051 }
    00052
    00056 struct AIListWindow : public Window {
    00057   const ScriptInfoList *info_list;
    00058   int selected;
    00059   CompanyID slot;
    00060   int line_height;
    00061   Scrollbar *vscroll;
    00062
    00068   AIListWindow(const WindowDesc *desc, CompanyID slot) : Window(),
    00069     slot(slot)
    00070   {
    00071     if (slot == OWNER_DEITY) {
    00072       this->info_list = Game::GetUniqueInfoList();
    00073     } else {
    00074       this->info_list = AI::GetUniqueInfoList();
    00075     }
    00076
    00077     this->CreateNestedTree(desc);
    00078     this->vscroll = this->GetScrollbar(WID_AIL_SCROLLBAR);
    00079     this->FinishInitNested(desc); // Initializes 'this->line_height' as side effect.
    00080
    00081     this->vscroll->SetCount((int)this->info_list->size() + 1);
    00082
    00083     /* Try if we can find the currently selected AI */
    00084     this->selected = -1;
    00085     if (GetConfig(slot)->HasScript()) {
    00086       ScriptInfo *info = GetConfig(slot)->GetInfo();
    00087       int i = 0;
    00088       for (ScriptInfoList::const_iterator it = this->info_list->begin(); it != this->info_list->end(); it++, i++) {
    00089         if ((*it).second == info) {
    00090           this->selected = i;
    00091           break;
    00092         }
    00093       }
    00094     }
    00095   }
    00096
    00097   virtual void SetStringParameters(int widget) const
    00098   {
    00099     switch (widget) {
    00100       case WID_AIL_CAPTION:
    00101         SetDParam(0, (this->slot == OWNER_DEITY) ? STR_AI_LIST_CAPTION_GAMESCRIPT : STR_AI_LIST_CAPTION_AI);
    00102         break;
    00103     }
    00104   }
    00105
    00106   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    00107   {
    00108     if (widget == WID_AIL_LIST) {
    00109       this->line_height = FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM;
    00110
    00111       resize->width = 1;
    00112       resize->height = this->line_height;
    00113       size->height = GB(this->GetWidget<NWidgetCore>(widget)->widget_data, MAT_ROW_START, MAT_ROW_BITS) * this->line_height;
    00114     }
    00115   }
    00116
    00117   virtual void DrawWidget(const Rect &r, int widget) const
    00118   {
    00119     switch (widget) {
    00120       case WID_AIL_LIST: {
    00121         /* Draw a list of all available AIs. */
    00122         int y = this->GetWidget<NWidgetBase>(WID_AIL_LIST)->pos_y;
    00123         /* First AI in the list is hardcoded to random */
    00124         if (this->vscroll->IsVisible(0)) {
    00125           DrawString(r.left + WD_MATRIX_LEFT, r.right - WD_MATRIX_LEFT, y + WD_MATRIX_TOP, this->slot == OWNER_DEITY ? STR_AI_CONFIG_NONE : STR_AI_CONFIG_RANDOM_AI, this->selected == -1 ? TC_WHITE : TC_ORANGE);
    00126           y += this->line_height;
    00127         }
    00128         ScriptInfoList::const_iterator it = this->info_list->begin();
    00129         for (int i = 1; it != this->info_list->end(); i++, it++) {
    00130           if (this->vscroll->IsVisible(i)) {
    00131             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_ORANGE);
    00132             y += this->line_height;
    00133           }
    00134         }
    00135         break;
    00136       }
    00137       case WID_AIL_INFO_BG: {
    00138         AIInfo *selected_info = NULL;
    00139         ScriptInfoList::const_iterator it = this->info_list->begin();
    00140         for (int i = 1; selected_info == NULL && it != this->info_list->end(); i++, it++) {
    00141           if (this->selected == i - 1) selected_info = static_cast<AIInfo *>((*it).second);
    00142         }
    00143         /* Some info about the currently selected AI. */
    00144         if (selected_info != NULL) {
    00145           int y = r.top + WD_FRAMERECT_TOP;
    00146           SetDParamStr(0, selected_info->GetAuthor());
    00147           DrawString(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, y, STR_AI_LIST_AUTHOR);
    00148           y += FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
    00149           SetDParam(0, selected_info->GetVersion());
    00150           DrawString(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, y, STR_AI_LIST_VERSION);
    00151           y += FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
    00152           if (selected_info->GetURL() != NULL) {
    00153             SetDParamStr(0, selected_info->GetURL());
    00154             DrawString(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, y, STR_AI_LIST_URL);
    00155             y += FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
    00156           }
    00157           SetDParamStr(0, selected_info->GetDescription());
    00158           DrawStringMultiLine(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, y, r.bottom - WD_FRAMERECT_BOTTOM, STR_JUST_RAW_STRING, TC_WHITE);
    00159         }
    00160         break;
    00161       }
    00162     }
    00163   }
    00164
    00168   void ChangeAI()
    00169   {
    00170     if (this->selected == -1) {
    00171       GetConfig(slot)->Change(NULL);
    00172     } else {
    00173       ScriptInfoList::const_iterator it = this->info_list->begin();
    00174       for (int i = 0; i < this->selected; i++) it++;
    00175       GetConfig(slot)->Change((*it).second->GetName(), (*it).second->GetVersion());
    00176     }
    00177     InvalidateWindowData(WC_GAME_OPTIONS, WN_GAME_OPTIONS_AI);
    00178     InvalidateWindowClassesData(WC_AI_SETTINGS);
    00179     DeleteWindowByClass(WC_QUERY_STRING);
    00180   }
    00181
    00182   virtual void OnClick(Point pt, int widget, int click_count)
    00183   {
    00184     switch (widget) {
    00185       case WID_AIL_LIST: { // Select one of the AIs
    00186         int sel = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_AIL_LIST, 0, this->line_height) - 1;
    00187         if (sel < (int)this->info_list->size()) {
    00188           this->selected = sel;
    00189           this->SetDirty();
    00190           if (click_count > 1) {
    00191             this->ChangeAI();
    00192             delete this;
    00193           }
    00194         }
    00195         break;
    00196       }
    00197
    00198       case WID_AIL_ACCEPT: {
    00199         this->ChangeAI();
    00200         delete this;
    00201         break;
    00202       }
    00203
    00204       case WID_AIL_CANCEL:
    00205         delete this;
    00206         break;
    00207     }
    00208   }
    00209
    00210   virtual void OnResize()
    00211   {
    00212     NWidgetCore *nwi = this->GetWidget<NWidgetCore>(WID_AIL_LIST);
    00213     this->vscroll->SetCapacity(nwi->current_y / this->line_height);
    00214     nwi->widget_data = (this->vscroll->GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
    00215   }
    00216
    00222   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
    00223   {
    00224     if (_game_mode == GM_NORMAL && Company::IsValidID(this->slot)) {
    00225       delete this;
    00226       return;
    00227     }
    00228
    00229     if (!gui_scope) return;
    00230
    00231     this->vscroll->SetCount((int)this->info_list->size() + 1);
    00232
    00233     /* selected goes from -1 .. length of ai list - 1. */
    00234     this->selected = min(this->selected, this->vscroll->GetCount() - 2);
    00235   }
    00236 };
    00237
    00239 static const NWidgetPart _nested_ai_list_widgets[] = {
    00240   NWidget(NWID_HORIZONTAL),
    00241     NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
    00242     NWidget(WWT_CAPTION, COLOUR_MAUVE, WID_AIL_CAPTION), SetDataTip(STR_AI_LIST_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    00243   EndContainer(),
    00244   NWidget(NWID_HORIZONTAL),
    00245     NWidget(WWT_MATRIX, COLOUR_MAUVE, WID_AIL_LIST), SetMinimalSize(188, 112), SetFill(1, 1), SetResize(1, 1), SetDataTip(0x501, STR_AI_LIST_TOOLTIP), SetScrollbar(WID_AIL_SCROLLBAR),
    00246     NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, WID_AIL_SCROLLBAR),
    00247   EndContainer(),
    00248   NWidget(WWT_PANEL, COLOUR_MAUVE, WID_AIL_INFO_BG), SetMinimalTextLines(8, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM), SetResize(1, 0),
    00249   EndContainer(),
    00250   NWidget(NWID_HORIZONTAL),
    00251     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
    00252       NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, WID_AIL_ACCEPT), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_AI_LIST_ACCEPT, STR_AI_LIST_ACCEPT_TOOLTIP),
    00253       NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, WID_AIL_CANCEL), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_AI_LIST_CANCEL, STR_AI_LIST_CANCEL_TOOLTIP),
    00254     EndContainer(),
    00255     NWidget(WWT_RESIZEBOX, COLOUR_MAUVE),
    00256   EndContainer(),
    00257 };
    00258
    00260 static const WindowDesc _ai_list_desc(
    00261   WDP_CENTER, 200, 234,
    00262   WC_AI_LIST, WC_NONE,
    00263   0,
    00264   _nested_ai_list_widgets, lengthof(_nested_ai_list_widgets)
    00265 );
    00266
    00271 static void ShowAIListWindow(CompanyID slot)
    00272 {
    00273   DeleteWindowByClass(WC_AI_LIST);
    00274   new AIListWindow(&_ai_list_desc, slot);
    00275 }
    00276
    00280 struct AISettingsWindow : public Window {
    00281   CompanyID slot;
    00282   ScriptConfig *ai_config;
    00283   int clicked_button;
    00284   bool clicked_increase;
    00285   bool clicked_dropdown;
    00286   bool closing_dropdown;
    00287   int timeout;
    00288   int clicked_row;
    00289   int line_height;
    00290   Scrollbar *vscroll;
    00291   typedef std::vector<const ScriptConfigItem *> VisibleSettingsList;
    00292   VisibleSettingsList visible_settings;
    00293
    00299   AISettingsWindow(const WindowDesc *desc, CompanyID slot) : Window(),
    00300     slot(slot),
    00301     clicked_button(-1),
    00302     clicked_dropdown(false),
    00303     closing_dropdown(false),
    00304     timeout(0)
    00305   {
    00306     this->ai_config = GetConfig(slot);
    00307     this->RebuildVisibleSettings();
    00308
    00309     this->CreateNestedTree(desc);
    00310     this->vscroll = this->GetScrollbar(WID_AIS_SCROLLBAR);
    00311     this->FinishInitNested(desc, slot);  // Initializes 'this->line_height' as side effect.
    00312
    00313     this->SetWidgetDisabledState(WID_AIS_RESET, _game_mode != GM_MENU && Company::IsValidID(this->slot));
    00314
    00315     this->vscroll->SetCount((int)this->visible_settings.size());
    00316   }
    00317
    00318   virtual void SetStringParameters(int widget) const
    00319   {
    00320     switch (widget) {
    00321       case WID_AIS_CAPTION:
    00322         SetDParam(0, (this->slot == OWNER_DEITY) ? STR_AI_SETTINGS_CAPTION_GAMESCRIPT : STR_AI_SETTINGS_CAPTION_AI);
    00323         break;
    00324     }
    00325   }
    00326
    00332   void RebuildVisibleSettings()
    00333   {
    00334     visible_settings.clear();
    00335
    00336     ScriptConfigItemList::const_iterator it = this->ai_config->GetConfigList()->begin();
    00337     for (; it != this->ai_config->GetConfigList()->end(); it++) {
    00338       bool no_hide = (it->flags & SCRIPTCONFIG_DEVELOPER) == 0;
    00339       if (no_hide || _settings_client.gui.ai_developer_tools) {
    00340         visible_settings.push_back(&(*it));
    00341       }
    00342     }
    00343   }
    00344
    00345   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    00346   {
    00347     if (widget == WID_AIS_BACKGROUND) {
    00348       this->line_height = FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM;
    00349
    00350       resize->width = 1;
    00351       resize->height = this->line_height;
    00352       size->height = GB(this->GetWidget<NWidgetCore>(widget)->widget_data, MAT_ROW_START, MAT_ROW_BITS) * this->line_height;
    00353     }
    00354   }
    00355
    00356   virtual void DrawWidget(const Rect &r, int widget) const
    00357   {
    00358     if (widget != WID_AIS_BACKGROUND) return;
    00359
    00360     ScriptConfig *config = this->ai_config;
    00361     VisibleSettingsList::const_iterator it = this->visible_settings.begin();
    00362     int i = 0;
    00363     for (; !this->vscroll->IsVisible(i); i++) it++;
    00364
    00365     bool rtl = _current_text_dir == TD_RTL;
    00366     uint buttons_left = rtl ? r.right - SETTING_BUTTON_WIDTH - 3 : r.left + 4;
    00367     uint text_left    = r.left + (rtl ? WD_FRAMERECT_LEFT : SETTING_BUTTON_WIDTH + 8);
    00368     uint text_right   = r.right - (rtl ? SETTING_BUTTON_WIDTH + 8 : WD_FRAMERECT_RIGHT);
    00369
    00370
    00371     int y = r.top;
    00372     int button_y_offset = (this->line_height - SETTING_BUTTON_HEIGHT) / 2;
    00373     for (; this->vscroll->IsVisible(i) && it != visible_settings.end(); i++, it++) {
    00374       const ScriptConfigItem &config_item = **it;
    00375       int current_value = config->GetSetting((config_item).name);
    00376       bool editable = _game_mode == GM_MENU || ((this->slot != OWNER_DEITY) && !Company::IsValidID(this->slot)) || (config_item.flags & SCRIPTCONFIG_INGAME) != 0;
    00377
    00378       StringID str;
    00379       TextColour colour;
    00380       uint idx = 0;
    00381       if (StrEmpty(config_item.description)) {
    00382         if (!strcmp(config_item.name, "start_date")) {
    00383           /* Build-in translation */
    00384           str = STR_AI_SETTINGS_START_DELAY;
    00385           colour = TC_LIGHT_BLUE;
    00386         } else {
    00387           str = STR_JUST_STRING;
    00388           colour = TC_ORANGE;
    00389         }
    00390       } else {
    00391         str = STR_AI_SETTINGS_SETTING;
    00392         colour = TC_LIGHT_BLUE;
    00393         SetDParamStr(idx++, config_item.description);
    00394       }
    00395
    00396       if ((config_item.flags & SCRIPTCONFIG_BOOLEAN) != 0) {
    00397         DrawBoolButton(buttons_left, y + button_y_offset, current_value != 0, editable);
    00398         SetDParam(idx++, current_value == 0 ? STR_CONFIG_SETTING_OFF : STR_CONFIG_SETTING_ON);
    00399       } else {
    00400         if (config_item.complete_labels) {
    00401           DrawDropDownButton(buttons_left, y + button_y_offset, COLOUR_YELLOW, this->clicked_row == i && clicked_dropdown, editable);
    00402         } else {
    00403           DrawArrowButtons(buttons_left, y + button_y_offset, COLOUR_YELLOW, (this->clicked_button == i) ? 1 + (this->clicked_increase != rtl) : 0, editable && current_value > config_item.min_value, editable && current_value < config_item.max_value);
    00404         }
    00405         if (config_item.labels != NULL && config_item.labels->Contains(current_value)) {
    00406           SetDParam(idx++, STR_JUST_RAW_STRING);
    00407           SetDParamStr(idx++, config_item.labels->Find(current_value)->second);
    00408         } else {
    00409           SetDParam(idx++, STR_JUST_INT);
    00410           SetDParam(idx++, current_value);
    00411         }
    00412       }
    00413
    00414       DrawString(text_left, text_right, y + WD_MATRIX_TOP, str, colour);
    00415       y += this->line_height;
    00416     }
    00417   }
    00418
    00419   virtual void OnPaint()
    00420   {
    00421     if (this->closing_dropdown) {
    00422       this->closing_dropdown = false;
    00423       this->clicked_dropdown = false;
    00424     }
    00425     this->DrawWidgets();
    00426   }
    00427
    00428   virtual void OnClick(Point pt, int widget, int click_count)
    00429   {
    00430     switch (widget) {
    00431       case WID_AIS_BACKGROUND: {
    00432         const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_AIS_BACKGROUND);
    00433         int num = (pt.y - wid->pos_y) / this->line_height + this->vscroll->GetPosition();
    00434         if (num >= (int)this->visible_settings.size()) break;
    00435
    00436         VisibleSettingsList::const_iterator it = this->visible_settings.begin();
    00437         for (int i = 0; i < num; i++) it++;
    00438         const ScriptConfigItem config_item = **it;
    00439         if (_game_mode == GM_NORMAL && ((this->slot == OWNER_DEITY) || Company::IsValidID(this->slot)) && (config_item.flags & SCRIPTCONFIG_INGAME) == 0) return;
    00440
    00441         if (this->clicked_row != num) {
    00442           DeleteChildWindows(WC_QUERY_STRING);
    00443           HideDropDownMenu(this);
    00444           this->clicked_row = num;
    00445           this->clicked_dropdown = false;
    00446         }
    00447
    00448         bool bool_item = (config_item.flags & SCRIPTCONFIG_BOOLEAN) != 0;
    00449
    00450         int x = pt.x - wid->pos_x;
    00451         if (_current_text_dir == TD_RTL) x = wid->current_x - 1 - x;
    00452         x -= 4;
    00453
    00454         /* One of the arrows is clicked (or green/red rect in case of bool value) */
    00455         int old_val = this->ai_config->GetSetting(config_item.name);
    00456         if (!bool_item && IsInsideMM(x, 0, SETTING_BUTTON_WIDTH) && config_item.complete_labels) {
    00457           if (this->clicked_dropdown) {
    00458             /* unclick the dropdown */
    00459             HideDropDownMenu(this);
    00460             this->clicked_dropdown = false;
    00461             this->closing_dropdown = false;
    00462           } else {
    00463             const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_AIS_BACKGROUND);
    00464             int rel_y = (pt.y - (int)wid->pos_y) % this->line_height;
    00465
    00466             Rect wi_rect;
    00467             wi_rect.left = pt.x - (_current_text_dir == TD_RTL ? SETTING_BUTTON_WIDTH - 1 - x : x);
    00468             wi_rect.right = wi_rect.left + SETTING_BUTTON_WIDTH - 1;
    00469             wi_rect.top = pt.y - rel_y + (this->line_height - SETTING_BUTTON_HEIGHT) / 2;
    00470             wi_rect.bottom = wi_rect.top + SETTING_BUTTON_HEIGHT - 1;
    00471
    00472             /* For dropdowns we also have to check the y position thoroughly, the mouse may not above the just opening dropdown */
    00473             if (pt.y >= wi_rect.top && pt.y <= wi_rect.bottom) {
    00474               this->clicked_dropdown = true;
    00475               this->closing_dropdown = false;
    00476
    00477               DropDownList *list = new DropDownList();
    00478               for (int i = config_item.min_value; i <= config_item.max_value; i++) {
    00479                 list->push_back(new DropDownListCharStringItem(config_item.labels->Find(i)->second, i, false));
    00480               }
    00481
    00482               ShowDropDownListAt(this, list, old_val, -1, wi_rect, COLOUR_ORANGE, true);
    00483             }
    00484           }
    00485         } else if (IsInsideMM(x, 0, SETTING_BUTTON_WIDTH)) {
    00486           int new_val = old_val;
    00487           if (bool_item) {
    00488             new_val = !new_val;
    00489           } else if (x >= SETTING_BUTTON_WIDTH / 2) {
    00490             /* Increase button clicked */
    00491             new_val += config_item.step_size;
    00492             if (new_val > config_item.max_value) new_val = config_item.max_value;
    00493             this->clicked_increase = true;
    00494           } else {
    00495             /* Decrease button clicked */
    00496             new_val -= config_item.step_size;
    00497             if (new_val < config_item.min_value) new_val = config_item.min_value;
    00498             this->clicked_increase = false;
    00499           }
    00500
    00501           if (new_val != old_val) {
    00502             this->ai_config->SetSetting(config_item.name, new_val);
    00503             this->clicked_button = num;
    00504             this->timeout = 5;
    00505           }
    00506         } else if (!bool_item && !config_item.complete_labels) {
    00507           /* Display a query box so users can enter a custom value. */
    00508           SetDParam(0, old_val);
    00509           ShowQueryString(STR_JUST_INT, STR_CONFIG_SETTING_QUERY_CAPTION, 10, this, CS_NUMERAL, QSF_NONE);
    00510         }
    00511         this->SetDirty();
    00512         break;
    00513       }
    00514
    00515       case WID_AIS_ACCEPT:
    00516         delete this;
    00517         break;
    00518
    00519       case WID_AIS_RESET:
    00520         if (_game_mode == GM_MENU || !Company::IsValidID(this->slot)) {
    00521           this->ai_config->ResetSettings();
    00522           this->SetDirty();
    00523         }
    00524         break;
    00525     }
    00526   }
    00527
    00528   virtual void OnQueryTextFinished(char *str)
    00529   {
    00530     if (StrEmpty(str)) return;
    00531     ScriptConfigItemList::const_iterator it = this->ai_config->GetConfigList()->begin();
    00532     for (int i = 0; i < this->clicked_row; i++) it++;
    00533     if (_game_mode == GM_NORMAL && ((this->slot == OWNER_DEITY) || Company::IsValidID(this->slot)) && (it->flags & SCRIPTCONFIG_INGAME) == 0) return;
    00534     int32 value = atoi(str);
    00535     this->ai_config->SetSetting((*it).name, value);
    00536     this->SetDirty();
    00537   }
    00538
    00539   virtual void OnDropdownSelect(int widget, int index)
    00540   {
    00541     assert(this->clicked_dropdown);
    00542     ScriptConfigItemList::const_iterator it = this->ai_config->GetConfigList()->begin();
    00543     for (int i = 0; i < this->clicked_row; i++) it++;
    00544     if (_game_mode == GM_NORMAL && ((this->slot == OWNER_DEITY) || Company::IsValidID(this->slot)) && (it->flags & SCRIPTCONFIG_INGAME) == 0) return;
    00545     this->ai_config->SetSetting((*it).name, index);
    00546     this->SetDirty();
    00547   }
    00548
    00549   virtual void OnDropdownClose(Point pt, int widget, int index, bool instant_close)
    00550   {
    00551     /* We cannot raise the dropdown button just yet. OnClick needs some hint, whether
    00552      * the same dropdown button was clicked again, and then not open the dropdown again.
    00553      * So, we only remember that it was closed, and process it on the next OnPaint, which is
    00554      * after OnClick. */
    00555     assert(this->clicked_dropdown);
    00556     this->closing_dropdown = true;
    00557     this->SetDirty();
    00558   }
    00559
    00560   virtual void OnResize()
    00561   {
    00562     NWidgetCore *nwi = this->GetWidget<NWidgetCore>(WID_AIS_BACKGROUND);
    00563     this->vscroll->SetCapacity(nwi->current_y / this->line_height);
    00564     nwi->widget_data = (this->vscroll->GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
    00565   }
    00566
    00567   virtual void OnTick()
    00568   {
    00569     if (--this->timeout == 0) {
    00570       this->clicked_button = -1;
    00571       this->SetDirty();
    00572     }
    00573   }
    00574
    00580   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
    00581   {
    00582     this->RebuildVisibleSettings();
    00583   }
    00584 };
    00585
    00587 static const NWidgetPart _nested_ai_settings_widgets[] = {
    00588   NWidget(NWID_HORIZONTAL),
    00589     NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
    00590     NWidget(WWT_CAPTION, COLOUR_MAUVE, WID_AIS_CAPTION), SetDataTip(STR_AI_SETTINGS_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    00591   EndContainer(),
    00592   NWidget(NWID_HORIZONTAL),
    00593     NWidget(WWT_MATRIX, COLOUR_MAUVE, WID_AIS_BACKGROUND), SetMinimalSize(188, 182), SetResize(1, 1), SetFill(1, 0), SetDataTip(0x501, STR_NULL), SetScrollbar(WID_AIS_SCROLLBAR),
    00594     NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, WID_AIS_SCROLLBAR),
    00595   EndContainer(),
    00596   NWidget(NWID_HORIZONTAL),
    00597     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
    00598       NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, WID_AIS_ACCEPT), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_AI_SETTINGS_CLOSE, STR_NULL),
    00599       NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, WID_AIS_RESET), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_AI_SETTINGS_RESET, STR_NULL),
    00600     EndContainer(),
    00601     NWidget(WWT_RESIZEBOX, COLOUR_MAUVE),
    00602   EndContainer(),
    00603 };
    00604
    00606 static const WindowDesc _ai_settings_desc(
    00607   WDP_CENTER, 500, 208,
    00608   WC_AI_SETTINGS, WC_NONE,
    00609   0,
    00610   _nested_ai_settings_widgets, lengthof(_nested_ai_settings_widgets)
    00611 );
    00612
    00617 static void ShowAISettingsWindow(CompanyID slot)
    00618 {
    00619   DeleteWindowByClass(WC_AI_LIST);
    00620   DeleteWindowByClass(WC_AI_SETTINGS);
    00621   new AISettingsWindow(&_ai_settings_desc, slot);
    00622 }
    00623
    00624
    00626 struct ScriptTextfileWindow : public TextfileWindow {
    00627   CompanyID slot;
    00628
    00629   ScriptTextfileWindow(TextfileType file_type, CompanyID slot) : TextfileWindow(file_type), slot(slot)
    00630   {
    00631     const char *textfile = GetConfig(slot)->GetTextfile(file_type, slot);
    00632     this->LoadTextfile(textfile, (slot == OWNER_DEITY) ? GAME_DIR : AI_DIR);
    00633   }
    00634
    00635   /* virtual */ void SetStringParameters(int widget) const
    00636   {
    00637     if (widget == WID_TF_CAPTION) {
    00638       SetDParam(0, (slot == OWNER_DEITY) ? STR_CONTENT_TYPE_GAME_SCRIPT : STR_CONTENT_TYPE_AI);
    00639       SetDParamStr(1, GetConfig(slot)->GetName());
    00640     }
    00641   }
    00642 };
    00643
    00649 void ShowScriptTextfileWindow(TextfileType file_type, CompanyID slot)
    00650 {
    00651   DeleteWindowByClass(WC_TEXTFILE);
    00652   new ScriptTextfileWindow(file_type, slot);
    00653 }
    00654
    00655
    00657 static const NWidgetPart _nested_ai_config_widgets[] = {
    00658   NWidget(NWID_HORIZONTAL),
    00659     NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
    00660     NWidget(WWT_CAPTION, COLOUR_MAUVE), SetDataTip(STR_AI_CONFIG_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    00661   EndContainer(),
    00662   NWidget(WWT_PANEL, COLOUR_MAUVE, WID_AIC_BACKGROUND),
    00663     NWidget(NWID_VERTICAL), SetPIP(4, 4, 4),
    00664       NWidget(NWID_HORIZONTAL), SetPIP(7, 0, 7),
    00665         NWidget(WWT_PUSHARROWBTN, COLOUR_YELLOW, WID_AIC_DECREASE), SetFill(0, 1), SetDataTip(AWV_DECREASE, STR_NULL),
    00666         NWidget(WWT_PUSHARROWBTN, COLOUR_YELLOW, WID_AIC_INCREASE), SetFill(0, 1), SetDataTip(AWV_INCREASE, STR_NULL),
    00667         NWidget(NWID_SPACER), SetMinimalSize(6, 0),
    00668         NWidget(WWT_TEXT, COLOUR_MAUVE, WID_AIC_NUMBER), SetDataTip(STR_DIFFICULTY_LEVEL_SETTING_MAXIMUM_NO_COMPETITORS, STR_NULL), SetFill(1, 0), SetPadding(1, 0, 0, 0),
    00669       EndContainer(),
    00670       NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(7, 0, 7),
    00671         NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_AIC_MOVE_UP), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_AI_CONFIG_MOVE_UP, STR_AI_CONFIG_MOVE_UP_TOOLTIP),
    00672         NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_AIC_MOVE_DOWN), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_AI_CONFIG_MOVE_DOWN, STR_AI_CONFIG_MOVE_DOWN_TOOLTIP),
    00673       EndContainer(),
    00674     EndContainer(),
    00675     NWidget(WWT_FRAME, COLOUR_MAUVE), SetDataTip(STR_AI_CONFIG_AI, STR_NULL), SetPadding(0, 5, 0, 5),
    00676       NWidget(NWID_HORIZONTAL),
    00677         NWidget(WWT_MATRIX, COLOUR_MAUVE, WID_AIC_LIST), SetMinimalSize(288, 112), SetFill(1, 0), SetDataTip(0x801, STR_AI_CONFIG_AILIST_TOOLTIP), SetScrollbar(WID_AIC_SCROLLBAR),
    00678         NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, WID_AIC_SCROLLBAR),
    00679       EndContainer(),
    00680     EndContainer(),
    00681     NWidget(NWID_SPACER), SetMinimalSize(0, 9),
    00682     NWidget(WWT_FRAME, COLOUR_MAUVE), SetDataTip(STR_AI_CONFIG_GAMESCRIPT, STR_NULL), SetPadding(0, 5, 4, 5),
    00683       NWidget(WWT_MATRIX, COLOUR_MAUVE, WID_AIC_GAMELIST), SetMinimalSize(288, 14), SetFill(1, 0), SetDataTip(0x101, STR_AI_CONFIG_GAMELIST_TOOLTIP),
    00684     EndContainer(),
    00685     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(7, 0, 7),
    00686       NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_AIC_CHANGE), SetFill(1, 0), SetMinimalSize(93, 12), SetDataTip(STR_AI_CONFIG_CHANGE, STR_AI_CONFIG_CHANGE_TOOLTIP),
    00687       NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_AIC_CONFIGURE), SetFill(1, 0), SetMinimalSize(93, 12), SetDataTip(STR_AI_CONFIG_CONFIGURE, STR_AI_CONFIG_CONFIGURE_TOOLTIP),
    00688       NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_AIC_CLOSE), SetFill(1, 0), SetMinimalSize(93, 12), SetDataTip(STR_AI_SETTINGS_CLOSE, STR_NULL),
    00689       EndContainer(),
    00690     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(7, 0, 7),
    00691       NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_AIC_TEXTFILE + TFT_README), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_TEXTFILE_VIEW_README, STR_NULL),
    00692       NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_AIC_TEXTFILE + TFT_CHANGELOG), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_TEXTFILE_VIEW_CHANGELOG, STR_NULL),
    00693       NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_AIC_TEXTFILE + TFT_LICENSE), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_TEXTFILE_VIEW_LICENCE, STR_NULL),
    00694     EndContainer(),
    00695     NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_AIC_CONTENT_DOWNLOAD), SetFill(1, 0), SetMinimalSize(279, 12), SetPadding(0, 7, 9, 7), SetDataTip(STR_INTRO_ONLINE_CONTENT, STR_INTRO_TOOLTIP_ONLINE_CONTENT),
    00696   EndContainer(),
    00697 };
    00698
    00700 static const WindowDesc _ai_config_desc(
    00701   WDP_CENTER, 0, 0,
    00702   WC_GAME_OPTIONS, WC_NONE,
    00703   0,
    00704   _nested_ai_config_widgets, lengthof(_nested_ai_config_widgets)
    00705 );
    00706
    00710 struct AIConfigWindow : public Window {
    00711   CompanyID selected_slot;
    00712   int line_height;
    00713   Scrollbar *vscroll;
    00714
    00715   AIConfigWindow() : Window()
    00716   {
    00717     this->InitNested(&_ai_config_desc, WN_GAME_OPTIONS_AI); // Initializes 'this->line_height' as a side effect.
    00718     this->vscroll = this->GetScrollbar(WID_AIC_SCROLLBAR);
    00719     this->selected_slot = INVALID_COMPANY;
    00720     NWidgetCore *nwi = this->GetWidget<NWidgetCore>(WID_AIC_LIST);
    00721     this->vscroll->SetCapacity(nwi->current_y / this->line_height);
    00722     this->vscroll->SetCount(MAX_COMPANIES);
    00723     nwi->widget_data = (this->vscroll->GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
    00724     this->OnInvalidateData(0);
    00725   }
    00726
    00727   ~AIConfigWindow()
    00728   {
    00729     DeleteWindowByClass(WC_AI_LIST);
    00730     DeleteWindowByClass(WC_AI_SETTINGS);
    00731   }
    00732
    00733   virtual void SetStringParameters(int widget) const
    00734   {
    00735     switch (widget) {
    00736       case WID_AIC_NUMBER:
    00737         SetDParam(0, GetGameSettings().difficulty.max_no_competitors);
    00738         break;
    00739       case WID_AIC_CHANGE:
    00740         switch (selected_slot) {
    00741           case OWNER_DEITY:
    00742             SetDParam(0, STR_AI_CONFIG_CHANGE_GAMESCRIPT);
    00743             break;
    00744
    00745           case INVALID_COMPANY:
    00746             SetDParam(0, STR_AI_CONFIG_CHANGE_NONE);
    00747             break;
    00748
    00749           default:
    00750             SetDParam(0, STR_AI_CONFIG_CHANGE_AI);
    00751             break;
    00752         }
    00753         break;
    00754     }
    00755   }
    00756
    00757   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    00758   {
    00759     switch (widget) {
    00760       case WID_AIC_GAMELIST:
    00761       case WID_AIC_LIST:
    00762         this->line_height = FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM;
    00763         size->height = GB(this->GetWidget<NWidgetCore>(widget)->widget_data, MAT_ROW_START, MAT_ROW_BITS) * this->line_height;
    00764         break;
    00765     }
    00766   }
    00767
    00773   static bool IsEditable(CompanyID slot)
    00774   {
    00775     if (slot == OWNER_DEITY) return _game_mode != GM_NORMAL || Game::GetInstance() != NULL;
    00776
    00777     if (_game_mode != GM_NORMAL) {
    00778       return slot > 0 && slot <= GetGameSettings().difficulty.max_no_competitors;
    00779     }
    00780     if (Company::IsValidID(slot) || slot < 0) return false;
    00781
    00782     int max_slot = GetGameSettings().difficulty.max_no_competitors;
    00783     for (CompanyID cid = COMPANY_FIRST; cid < (CompanyID)max_slot && cid < MAX_COMPANIES; cid++) {
    00784       if (Company::IsValidHumanID(cid)) max_slot++;
    00785     }
    00786     return slot < max_slot;
    00787   }
    00788
    00789   virtual void DrawWidget(const Rect &r, int widget) const
    00790   {
    00791     switch (widget) {
    00792       case WID_AIC_GAMELIST: {
    00793         StringID text = STR_AI_CONFIG_NONE;
    00794
    00795         if (GameConfig::GetConfig()->GetInfo() != NULL) {
    00796           SetDParamStr(0, GameConfig::GetConfig()->GetInfo()->GetName());
    00797           text = STR_JUST_RAW_STRING;
    00798         }
    00799
    00800         DrawString(r.left + 10, r.right - 10, r.top + WD_MATRIX_TOP, text,
    00801             (this->selected_slot == OWNER_DEITY) ? TC_WHITE : (IsEditable(OWNER_DEITY) ? TC_ORANGE : TC_SILVER));
    00802
    00803         break;
    00804       }
    00805
    00806       case WID_AIC_LIST: {
    00807         int y = r.top;
    00808         for (int i = this->vscroll->GetPosition(); this->vscroll->IsVisible(i) && i < MAX_COMPANIES; i++) {
    00809           StringID text;
    00810
    00811           if ((_game_mode != GM_NORMAL && i == 0) || (_game_mode == GM_NORMAL && Company::IsValidHumanID(i))) {
    00812             text = STR_AI_CONFIG_HUMAN_PLAYER;
    00813           } else if (AIConfig::GetConfig((CompanyID)i)->GetInfo() != NULL) {
    00814             SetDParamStr(0, AIConfig::GetConfig((CompanyID)i)->GetInfo()->GetName());
    00815             text = STR_JUST_RAW_STRING;
    00816           } else {
    00817             text = STR_AI_CONFIG_RANDOM_AI;
    00818           }
    00819           DrawString(r.left + 10, r.right - 10, y + WD_MATRIX_TOP, text,
    00820               (this->selected_slot == i) ? TC_WHITE : (IsEditable((CompanyID)i) ? TC_ORANGE : TC_SILVER));
    00821           y += this->line_height;
    00822         }
    00823         break;
    00824       }
    00825     }
    00826   }
    00827
    00828   virtual void OnClick(Point pt, int widget, int click_count)
    00829   {
    00830     if (widget >= WID_AIC_TEXTFILE && widget < WID_AIC_TEXTFILE + TFT_END) {
    00831       if (this->selected_slot == INVALID_COMPANY || GetConfig(this->selected_slot) == NULL) return;
    00832
    00833       ShowScriptTextfileWindow((TextfileType)(widget - WID_AIC_TEXTFILE), this->selected_slot);
    00834       return;
    00835     }
    00836
    00837     switch (widget) {
    00838       case WID_AIC_DECREASE:
    00839       case WID_AIC_INCREASE: {
    00840         int new_value;
    00841         if (widget == WID_AIC_DECREASE) {
    00842           new_value = max(0, GetGameSettings().difficulty.max_no_competitors - 1);
    00843         } else {
    00844           new_value = min(MAX_COMPANIES - 1, GetGameSettings().difficulty.max_no_competitors + 1);
    00845         }
    00846         IConsoleSetSetting("difficulty.max_no_competitors", new_value);
    00847         this->InvalidateData();
    00848         break;
    00849       }
    00850
    00851       case WID_AIC_GAMELIST: {
    00852         this->selected_slot = OWNER_DEITY;
    00853         this->InvalidateData();
    00854         if (click_count > 1 && this->selected_slot != INVALID_COMPANY) ShowAIListWindow((CompanyID)this->selected_slot);
    00855         break;
    00856       }
    00857
    00858       case WID_AIC_LIST: { // Select a slot
    00859         this->selected_slot = (CompanyID)this->vscroll->GetScrolledRowFromWidget(pt.y, this, widget, 0, this->line_height);
    00860         this->InvalidateData();
    00861         if (click_count > 1 && this->selected_slot != INVALID_COMPANY) ShowAIListWindow((CompanyID)this->selected_slot);
    00862         break;
    00863       }
    00864
    00865       case WID_AIC_MOVE_UP:
    00866         if (IsEditable(this->selected_slot) && IsEditable((CompanyID)(this->selected_slot - 1))) {
    00867           Swap(GetGameSettings().ai_config[this->selected_slot], GetGameSettings().ai_config[this->selected_slot - 1]);
    00868           this->selected_slot--;
    00869           this->vscroll->ScrollTowards(this->selected_slot);
    00870           this->InvalidateData();
    00871         }
    00872         break;
    00873
    00874       case WID_AIC_MOVE_DOWN:
    00875         if (IsEditable(this->selected_slot) && IsEditable((CompanyID)(this->selected_slot + 1))) {
    00876           Swap(GetGameSettings().ai_config[this->selected_slot], GetGameSettings().ai_config[this->selected_slot + 1]);
    00877           this->selected_slot++;
    00878           this->vscroll->ScrollTowards(this->selected_slot);
    00879           this->InvalidateData();
    00880         }
    00881         break;
    00882
    00883       case WID_AIC_CHANGE:  // choose other AI
    00884         ShowAIListWindow((CompanyID)this->selected_slot);
    00885         break;
    00886
    00887       case WID_AIC_CONFIGURE: // change the settings for an AI
    00888         ShowAISettingsWindow((CompanyID)this->selected_slot);
    00889         break;
    00890
    00891       case WID_AIC_CLOSE:
    00892         delete this;
    00893         break;
    00894
    00895       case WID_AIC_CONTENT_DOWNLOAD:
    00896         if (!_network_available) {
    00897           ShowErrorMessage(STR_NETWORK_ERROR_NOTAVAILABLE, INVALID_STRING_ID, WL_ERROR);
    00898         } else {
    00899 #if defined(ENABLE_NETWORK)
    00900           ShowNetworkContentListWindow(NULL, CONTENT_TYPE_AI);
    00901           _network_content_client.RequestContentList(CONTENT_TYPE_GAME);
    00902 #endif
    00903         }
    00904         break;
    00905     }
    00906   }
    00907
    00913   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
    00914   {
    00915     if (!IsEditable(this->selected_slot)) {
    00916       this->selected_slot = INVALID_COMPANY;
    00917     }
    00918
    00919     if (!gui_scope) return;
    00920
    00921     this->SetWidgetDisabledState(WID_AIC_DECREASE, GetGameSettings().difficulty.max_no_competitors == 0);
    00922     this->SetWidgetDisabledState(WID_AIC_INCREASE, GetGameSettings().difficulty.max_no_competitors == MAX_COMPANIES - 1);
    00923     this->SetWidgetDisabledState(WID_AIC_CHANGE, (this->selected_slot == OWNER_DEITY && _game_mode == GM_NORMAL) || this->selected_slot == INVALID_COMPANY);
    00924     this->SetWidgetDisabledState(WID_AIC_CONFIGURE, this->selected_slot == INVALID_COMPANY || GetConfig(this->selected_slot)->GetConfigList()->size() == 0);
    00925     this->SetWidgetDisabledState(WID_AIC_MOVE_UP, this->selected_slot == OWNER_DEITY || this->selected_slot == INVALID_COMPANY || !IsEditable((CompanyID)(this->selected_slot - 1)));
    00926     this->SetWidgetDisabledState(WID_AIC_MOVE_DOWN, this->selected_slot == OWNER_DEITY || this->selected_slot == INVALID_COMPANY || !IsEditable((CompanyID)(this->selected_slot + 1)));
    00927
    00928     for (TextfileType tft = TFT_BEGIN; tft < TFT_END; tft++) {
    00929       this->SetWidgetDisabledState(WID_AIC_TEXTFILE + tft, this->selected_slot == INVALID_COMPANY || (GetConfig(this->selected_slot)->GetTextfile(tft, this->selected_slot) == NULL));
    00930     }
    00931   }
    00932 };
    00933
    00935 void ShowAIConfigWindow()
    00936 {
    00937   DeleteWindowByClass(WC_GAME_OPTIONS);
    00938   new AIConfigWindow();
    00939 }
    00940
    00949 static bool SetScriptButtonColour(NWidgetCore &button, bool dead, bool paused)
    00950 {
    00951   /* Dead scripts are indicated with red background and
    00952    * paused scripts are indicated with yellow background. */
    00953   Colours colour = dead ? COLOUR_RED :
    00954       (paused ? COLOUR_YELLOW : COLOUR_GREY);
    00955   if (button.colour != colour) {
    00956     button.colour = colour;
    00957     return true;
    00958   }
    00959   return false;
    00960 }
    00961
    00965 struct AIDebugWindow : public Window {
    00966   static const int top_offset;
    00967   static const int bottom_offset;
    00968
    00969   static const unsigned int MAX_BREAK_STR_STRING_LENGTH = 256;
    00970
    00971   static CompanyID ai_debug_company;
    00972   int redraw_timer;
    00973   int last_vscroll_pos;
    00974   bool autoscroll;
    00975   bool show_break_box;
    00976   static bool break_check_enabled;
    00977   static char break_string[MAX_BREAK_STR_STRING_LENGTH];
    00978   QueryString break_editbox;
    00979   static StringFilter break_string_filter;
    00980   static bool case_sensitive_break_check;
    00981   int highlight_row;
    00982   Scrollbar *vscroll;
    00983
    00984   ScriptLog::LogData *GetLogPointer() const
    00985   {
    00986     if (ai_debug_company == OWNER_DEITY) return (ScriptLog::LogData *)Game::GetInstance()->GetLogPointer();
    00987     return (ScriptLog::LogData *)Company::Get(ai_debug_company)->ai_instance->GetLogPointer();
    00988   }
    00989
    00994   bool IsDead() const
    00995   {
    00996     if (ai_debug_company == OWNER_DEITY) {
    00997       GameInstance *game = Game::GetInstance();
    00998       return game == NULL || game->IsDead();
    00999     }
    01000     return !Company::IsValidAiID(ai_debug_company) || Company::Get(ai_debug_company)->ai_instance->IsDead();
    01001   }
    01002
    01008   bool IsValidDebugCompany(CompanyID company) const
    01009   {
    01010     switch (company) {
    01011       case INVALID_COMPANY: return false;
    01012       case OWNER_DEITY:     return Game::GetInstance() != NULL;
    01013       default:              return Company::IsValidAiID(company);
    01014     }
    01015   }
    01016
    01021   void SelectValidDebugCompany()
    01022   {
    01023     /* Check if the currently selected company is still active. */
    01024     if (this->IsValidDebugCompany(ai_debug_company)) return;
    01025
    01026     ai_debug_company = INVALID_COMPANY;
    01027
    01028     const Company *c;
    01029     FOR_ALL_COMPANIES(c) {
    01030       if (c->is_ai) {
    01031         ChangeToAI(c->index);
    01032         return;
    01033       }
    01034     }
    01035
    01036     /* If no AI is available, see if there is a game script. */
    01037     if (Game::GetInstance() != NULL) ChangeToAI(OWNER_DEITY);
    01038   }
    01039
    01045   AIDebugWindow(const WindowDesc *desc, WindowNumber number) : break_editbox(MAX_BREAK_STR_STRING_LENGTH)
    01046   {
    01047     this->CreateNestedTree(desc);
    01048     this->vscroll = this->GetScrollbar(WID_AID_SCROLLBAR);
    01049     this->show_break_box = _settings_client.gui.ai_developer_tools;
    01050     this->GetWidget<NWidgetStacked>(WID_AID_BREAK_STRING_WIDGETS)->SetDisplayedPlane(this->show_break_box ? 0 : SZSP_HORIZONTAL);
    01051     this->FinishInitNested(desc, number);
    01052
    01053     if (!this->show_break_box) break_check_enabled = false;
    01054
    01055     this->last_vscroll_pos = 0;
    01056     this->autoscroll = true;
    01057     this->highlight_row = -1;
    01058
    01059     this->querystrings[WID_AID_BREAK_STR_EDIT_BOX] = &this->break_editbox;
    01060
    01061     /* Restore the break string value from static variable */
    01062     this->break_editbox.text.Assign(this->break_string);
    01063
    01064     this->SelectValidDebugCompany();
    01065     this->InvalidateData(-1);
    01066   }
    01067
    01068   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    01069   {
    01070     if (widget == WID_AID_LOG_PANEL) {
    01071       resize->height = FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
    01072       size->height = 14 * resize->height + this->top_offset + this->bottom_offset;
    01073     }
    01074   }
    01075
    01076   virtual void OnPaint()
    01077   {
    01078     this->SelectValidDebugCompany();
    01079
    01080     /* Draw standard stuff */
    01081     this->DrawWidgets();
    01082
    01083     if (this->IsShaded()) return; // Don't draw anything when the window is shaded.
    01084
    01085     bool dirty = false;
    01086
    01087     /* Paint the company icons */
    01088     for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
    01089       NWidgetCore *button = this->GetWidget<NWidgetCore>(i + WID_AID_COMPANY_BUTTON_START);
    01090
    01091       bool valid = Company::IsValidAiID(i);
    01092
    01093       /* Check whether the validity of the company changed */
    01094       dirty |= (button->IsDisabled() == valid);
    01095
    01096       /* Mark dead/paused AIs by setting the background colour. */
    01097       bool dead = valid && Company::Get(i)->ai_instance->IsDead();
    01098       bool paused = valid && Company::Get(i)->ai_instance->IsPaused();
    01099       /* Re-paint if the button was updated.
    01100        * (note that it is intentional that SetScriptButtonColour is always called) */
    01101       dirty |= SetScriptButtonColour(*button, dead, paused);
    01102
    01103       /* Draw company icon only for valid AI companies */
    01104       if (!valid) continue;
    01105
    01106       byte offset = (i == ai_debug_company) ? 1 : 0;
    01107       DrawCompanyIcon(i, button->pos_x + button->current_x / 2 - 7 + offset, this->GetWidget<NWidgetBase>(WID_AID_COMPANY_BUTTON_START + i)->pos_y + 2 + offset);
    01108     }
    01109
    01110     /* Set button colour for Game Script. */
    01111     GameInstance *game = Game::GetInstance();
    01112     bool valid = game != NULL;
    01113     bool dead = valid && game->IsDead();
    01114     bool paused = valid && game->IsPaused();
    01115
    01116     NWidgetCore *button = this->GetWidget<NWidgetCore>(WID_AID_SCRIPT_GAME);
    01117     dirty |= (button->IsDisabled() == valid) || SetScriptButtonColour(*button, dead, paused);
    01118
    01119     if (dirty) this->InvalidateData(-1);
    01120
    01121     /* If there are no active companies, don't display anything else. */
    01122     if (ai_debug_company == INVALID_COMPANY) return;
    01123
    01124     ScriptLog::LogData *log = this->GetLogPointer();
    01125
    01126     int scroll_count = (log == NULL) ? 0 : log->used;
    01127     if (this->vscroll->GetCount() != scroll_count) {
    01128       this->vscroll->SetCount(scroll_count);
    01129
    01130       /* We need a repaint */
    01131       this->SetWidgetDirty(WID_AID_SCROLLBAR);
    01132     }
    01133
    01134     if (log == NULL) return;
    01135
    01136     /* Detect when the user scrolls the window. Enable autoscroll when the
    01137      * bottom-most line becomes visible. */
    01138     if (this->last_vscroll_pos != this->vscroll->GetPosition()) {
    01139       this->autoscroll = this->vscroll->GetPosition() >= log->used - this->vscroll->GetCapacity();
    01140     }
    01141     if (this->autoscroll) {
    01142       int scroll_pos = max(0, log->used - this->vscroll->GetCapacity());
    01143       if (scroll_pos != this->vscroll->GetPosition()) {
    01144         this->vscroll->SetPosition(scroll_pos);
    01145
    01146         /* We need a repaint */
    01147         this->SetWidgetDirty(WID_AID_SCROLLBAR);
    01148         this->SetWidgetDirty(WID_AID_LOG_PANEL);
    01149       }
    01150     }
    01151     this->last_vscroll_pos = this->vscroll->GetPosition();
    01152   }
    01153
    01154   virtual void SetStringParameters(int widget) const
    01155   {
    01156     switch (widget) {
    01157       case WID_AID_NAME_TEXT:
    01158         if (ai_debug_company == OWNER_DEITY) {
    01159           const GameInfo *info = Game::GetInfo();
    01160           assert(info != NULL);
    01161           SetDParam(0, STR_AI_DEBUG_NAME_AND_VERSION);
    01162           SetDParamStr(1, info->GetName());
    01163           SetDParam(2, info->GetVersion());
    01164         } else if (ai_debug_company == INVALID_COMPANY || !Company::IsValidAiID(ai_debug_company)) {
    01165           SetDParam(0, STR_EMPTY);
    01166         } else {
    01167           const AIInfo *info = Company::Get(ai_debug_company)->ai_info;
    01168           assert(info != NULL);
    01169           SetDParam(0, STR_AI_DEBUG_NAME_AND_VERSION);
    01170           SetDParamStr(1, info->GetName());
    01171           SetDParam(2, info->GetVersion());
    01172         }
    01173         break;
    01174     }
    01175   }
    01176
    01177   virtual void DrawWidget(const Rect &r, int widget) const
    01178   {
    01179     if (ai_debug_company == INVALID_COMPANY) return;
    01180
    01181     switch (widget) {
    01182       case WID_AID_LOG_PANEL: {
    01183         ScriptLog::LogData *log = this->GetLogPointer();
    01184         if (log == NULL) return;
    01185
    01186         int y = this->top_offset;
    01187         for (int i = this->vscroll->GetPosition(); this->vscroll->IsVisible(i) && i < log->used; i++) {
    01188           int pos = (i + log->pos + 1 - log->used + log->count) % log->count;
    01189           if (log->lines[pos] == NULL) break;
    01190
    01191           TextColour colour;
    01192           switch (log->type[pos]) {
    01193             case ScriptLog::LOG_SQ_INFO:  colour = TC_BLACK;  break;
    01194             case ScriptLog::LOG_SQ_ERROR: colour = TC_RED;    break;
    01195             case ScriptLog::LOG_INFO:     colour = TC_BLACK;  break;
    01196             case ScriptLog::LOG_WARNING:  colour = TC_YELLOW; break;
    01197             case ScriptLog::LOG_ERROR:    colour = TC_RED;    break;
    01198             default:                  colour = TC_BLACK;  break;
    01199           }
    01200
    01201           /* Check if the current line should be highlighted */
    01202           if (pos == this->highlight_row) {
    01203             GfxFillRect(r.left + 1, r.top + y, r.right - 1, r.top + y + this->resize.step_height - WD_PAR_VSEP_NORMAL, PC_BLACK);
    01204             if (colour == TC_BLACK) colour = TC_WHITE; // Make black text readable by inverting it to white.
    01205           }
    01206
    01207           DrawString(r.left + 7, r.right - 7, r.top + y, log->lines[pos], colour, SA_LEFT | SA_FORCE);
    01208           y += this->resize.step_height;
    01209         }
    01210         break;
    01211       }
    01212     }
    01213   }
    01214
    01219   void ChangeToAI(CompanyID show_ai)
    01220   {
    01221     if (!this->IsValidDebugCompany(show_ai)) return;
    01222
    01223     ai_debug_company = show_ai;
    01224
    01225     this->highlight_row = -1; // The highlight of one AI make little sense for another AI.
    01226
    01227     /* Close AI settings window to prevent confusion */
    01228     DeleteWindowByClass(WC_AI_SETTINGS);
    01229
    01230     this->InvalidateData(-1);
    01231
    01232     this->autoscroll = true;
    01233     this->last_vscroll_pos = this->vscroll->GetPosition();
    01234   }
    01235
    01236   virtual void OnClick(Point pt, int widget, int click_count)
    01237   {
    01238     /* Also called for hotkeys, so check for disabledness */
    01239     if (this->IsWidgetDisabled(widget)) return;
    01240
    01241     /* Check which button is clicked */
    01242     if (IsInsideMM(widget, WID_AID_COMPANY_BUTTON_START, WID_AID_COMPANY_BUTTON_END + 1)) {
    01243       ChangeToAI((CompanyID)(widget - WID_AID_COMPANY_BUTTON_START));
    01244     }
    01245
    01246     switch (widget) {
    01247       case WID_AID_SCRIPT_GAME:
    01248         ChangeToAI(OWNER_DEITY);
    01249         break;
    01250
    01251       case WID_AID_RELOAD_TOGGLE:
    01252         if (ai_debug_company == OWNER_DEITY) break;
    01253         /* First kill the company of the AI, then start a new one. This should start the current AI again */
    01254         DoCommandP(0, 2 | ai_debug_company << 16, CRR_MANUAL, CMD_COMPANY_CTRL);
    01255         DoCommandP(0, 1 | ai_debug_company << 16, 0, CMD_COMPANY_CTRL);
    01256         break;
    01257
    01258       case WID_AID_SETTINGS:
    01259         ShowAISettingsWindow(ai_debug_company);
    01260         break;
    01261
    01262       case WID_AID_BREAK_STR_ON_OFF_BTN:
    01263         this->break_check_enabled = !this->break_check_enabled;
    01264         this->InvalidateData(-1);
    01265         break;
    01266
    01267       case WID_AID_MATCH_CASE_BTN:
    01268         this->case_sensitive_break_check = !this->case_sensitive_break_check;
    01269         this->InvalidateData(-1);
    01270         break;
    01271
    01272       case WID_AID_CONTINUE_BTN:
    01273         /* Unpause current AI / game script and mark the corresponding script button dirty. */
    01274         if (!this->IsDead()) {
    01275           if (ai_debug_company == OWNER_DEITY) {
    01276             Game::Unpause();
    01277           } else {
    01278             AI::Unpause(ai_debug_company);
    01279           }
    01280         }
    01281
    01282         /* If the last AI/Game Script is unpaused, unpause the game too. */
    01283         if ((_pause_mode & PM_PAUSED_NORMAL) == PM_PAUSED_NORMAL) {
    01284           bool all_unpaused = !Game::IsPaused();
    01285           if (all_unpaused) {
    01286             Company *c;
    01287             FOR_ALL_COMPANIES(c) {
    01288               if (c->is_ai && AI::IsPaused(c->index)) {
    01289                 all_unpaused = false;
    01290                 break;
    01291               }
    01292             }
    01293             if (all_unpaused) {
    01294               /* All scripts have been unpaused => unpause the game. */
    01295               DoCommandP(0, PM_PAUSED_NORMAL, 0, CMD_PAUSE);
    01296             }
    01297           }
    01298         }
    01299
    01300         this->highlight_row = -1;
    01301         this->InvalidateData(-1);
    01302         break;
    01303     }
    01304   }
    01305
    01306   virtual EventState OnKeyPress(uint16 key, uint16 keycode)
    01307   {
    01308     EventState state = ES_NOT_HANDLED;
    01309     int num = CheckHotkeyMatch(aidebug_hotkeys, keycode, this);
    01310     if (num != -1) {
    01311       if (this->show_break_box && num == WID_AID_BREAK_STR_EDIT_BOX) {
    01312         this->SetFocusedWidget(WID_AID_BREAK_STR_EDIT_BOX);
    01313         SetFocusedWindow(this);
    01314         state = ES_HANDLED;
    01315       } else if (this->show_break_box || num < WID_AID_BREAK_STRING_WIDGETS) {
    01316         this->OnClick(Point(), num, 1);
    01317         state = ES_HANDLED;
    01318       }
    01319     }
    01320     return state;
    01321   }
    01322
    01323   virtual void OnEditboxChanged(int wid)
    01324   {
    01325     if (wid == WID_AID_BREAK_STR_EDIT_BOX) {
    01326       /* Save the current string to static member so it can be restored next time the window is opened. */
    01327       strecpy(this->break_string, this->break_editbox.text.buf, lastof(this->break_string));
    01328       break_string_filter.SetFilterTerm(this->break_string);
    01329     }
    01330   }
    01331
    01338   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
    01339   {
    01340     /* If the log message is related to the active company tab, check the break string.
    01341      * This needs to be done in gameloop-scope, so the AI is suspended immediately. */
    01342     if (!gui_scope && data == ai_debug_company && this->IsValidDebugCompany(ai_debug_company) && this->break_check_enabled && !this->break_string_filter.IsEmpty()) {
    01343       /* Get the log instance of the active company */
    01344       ScriptLog::LogData *log = this->GetLogPointer();
    01345
    01346       if (log != NULL) {
    01347         this->break_string_filter.ResetState();
    01348         this->break_string_filter.AddLine(log->lines[log->pos]);
    01349         if (this->break_string_filter.GetState()) {
    01350           /* Pause execution of script. */
    01351           if (!this->IsDead()) {
    01352             if (ai_debug_company == OWNER_DEITY) {
    01353               Game::Pause();
    01354             } else {
    01355               AI::Pause(ai_debug_company);
    01356             }
    01357           }
    01358
    01359           /* Pause the game. */
    01360           if ((_pause_mode & PM_PAUSED_NORMAL) == PM_UNPAUSED) {
    01361             DoCommandP(0, PM_PAUSED_NORMAL, 1, CMD_PAUSE);
    01362           }
    01363
    01364           /* Highlight row that matched */
    01365           this->highlight_row = log->pos;
    01366         }
    01367       }
    01368     }
    01369
    01370     if (!gui_scope) return;
    01371
    01372     this->SelectValidDebugCompany();
    01373
    01374     ScriptLog::LogData *log = ai_debug_company != INVALID_COMPANY ? this->GetLogPointer() : NULL;
    01375     this->vscroll->SetCount((log == NULL) ? 0 : log->used);
    01376
    01377     /* Update company buttons */
    01378     for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
    01379       this->SetWidgetDisabledState(i + WID_AID_COMPANY_BUTTON_START, !Company::IsValidAiID(i));
    01380       this->SetWidgetLoweredState(i + WID_AID_COMPANY_BUTTON_START, ai_debug_company == i);
    01381     }
    01382
    01383     this->SetWidgetDisabledState(WID_AID_SCRIPT_GAME, Game::GetGameInstance() == NULL);
    01384     this->SetWidgetLoweredState(WID_AID_SCRIPT_GAME, ai_debug_company == OWNER_DEITY);
    01385
    01386     this->SetWidgetLoweredState(WID_AID_BREAK_STR_ON_OFF_BTN, this->break_check_enabled);
    01387     this->SetWidgetLoweredState(WID_AID_MATCH_CASE_BTN, this->case_sensitive_break_check);
    01388
    01389     this->SetWidgetDisabledState(WID_AID_SETTINGS, ai_debug_company == INVALID_COMPANY);
    01390     this->SetWidgetDisabledState(WID_AID_RELOAD_TOGGLE, ai_debug_company == INVALID_COMPANY || ai_debug_company == OWNER_DEITY);
    01391     this->SetWidgetDisabledState(WID_AID_CONTINUE_BTN, ai_debug_company == INVALID_COMPANY ||
    01392         (ai_debug_company == OWNER_DEITY ? !Game::IsPaused() : !AI::IsPaused(ai_debug_company)));
    01393   }
    01394
    01395   virtual void OnResize()
    01396   {
    01397     this->vscroll->SetCapacityFromWidget(this, WID_AID_LOG_PANEL);
    01398   }
    01399
    01400   static Hotkey<AIDebugWindow> aidebug_hotkeys[];
    01401 };
    01402
    01403 const int AIDebugWindow::top_offset = WD_FRAMERECT_TOP + 2;
    01404 const int AIDebugWindow::bottom_offset = WD_FRAMERECT_BOTTOM;
    01405 CompanyID AIDebugWindow::ai_debug_company = INVALID_COMPANY;
    01406 char AIDebugWindow::break_string[MAX_BREAK_STR_STRING_LENGTH] = "";
    01407 bool AIDebugWindow::break_check_enabled = true;
    01408 bool AIDebugWindow::case_sensitive_break_check = false;
    01409 StringFilter AIDebugWindow::break_string_filter(&AIDebugWindow::case_sensitive_break_check);
    01410
    01412 NWidgetBase *MakeCompanyButtonRowsAIDebug(int *biggest_index)
    01413 {
    01414   return MakeCompanyButtonRows(biggest_index, WID_AID_COMPANY_BUTTON_START, WID_AID_COMPANY_BUTTON_END, 8, STR_AI_DEBUG_SELECT_AI_TOOLTIP);
    01415 }
    01416
    01417 Hotkey<AIDebugWindow> AIDebugWindow::aidebug_hotkeys[] = {
    01418   Hotkey<AIDebugWindow>('1', "company_1", WID_AID_COMPANY_BUTTON_START),
    01419   Hotkey<AIDebugWindow>('2', "company_2", WID_AID_COMPANY_BUTTON_START + 1),
    01420   Hotkey<AIDebugWindow>('3', "company_3", WID_AID_COMPANY_BUTTON_START + 2),
    01421   Hotkey<AIDebugWindow>('4', "company_4", WID_AID_COMPANY_BUTTON_START + 3),
    01422   Hotkey<AIDebugWindow>('5', "company_5", WID_AID_COMPANY_BUTTON_START + 4),
    01423   Hotkey<AIDebugWindow>('6', "company_6", WID_AID_COMPANY_BUTTON_START + 5),
    01424   Hotkey<AIDebugWindow>('7', "company_7", WID_AID_COMPANY_BUTTON_START + 6),
    01425   Hotkey<AIDebugWindow>('8', "company_8", WID_AID_COMPANY_BUTTON_START + 7),
    01426   Hotkey<AIDebugWindow>('9', "company_9", WID_AID_COMPANY_BUTTON_START + 8),
    01427   Hotkey<AIDebugWindow>((uint16)0, "company_10", WID_AID_COMPANY_BUTTON_START + 9),
    01428   Hotkey<AIDebugWindow>((uint16)0, "company_11", WID_AID_COMPANY_BUTTON_START + 10),
    01429   Hotkey<AIDebugWindow>((uint16)0, "company_12", WID_AID_COMPANY_BUTTON_START + 11),
    01430   Hotkey<AIDebugWindow>((uint16)0, "company_13", WID_AID_COMPANY_BUTTON_START + 12),
    01431   Hotkey<AIDebugWindow>((uint16)0, "company_14", WID_AID_COMPANY_BUTTON_START + 13),
    01432   Hotkey<AIDebugWindow>((uint16)0, "company_15", WID_AID_COMPANY_BUTTON_START + 14),
    01433   Hotkey<AIDebugWindow>('S', "settings", WID_AID_SETTINGS),
    01434   Hotkey<AIDebugWindow>('0', "game_script", WID_AID_SCRIPT_GAME),
    01435   Hotkey<AIDebugWindow>((uint16)0, "reload", WID_AID_RELOAD_TOGGLE),
    01436   Hotkey<AIDebugWindow>('B', "break_toggle", WID_AID_BREAK_STR_ON_OFF_BTN),
    01437   Hotkey<AIDebugWindow>('F', "break_string", WID_AID_BREAK_STR_EDIT_BOX),
    01438   Hotkey<AIDebugWindow>('C', "match_case", WID_AID_MATCH_CASE_BTN),
    01439   Hotkey<AIDebugWindow>(WKC_RETURN, "continue", WID_AID_CONTINUE_BTN),
    01440   HOTKEY_LIST_END(AIDebugWindow)
    01441 };
    01442 Hotkey<AIDebugWindow> *_aidebug_hotkeys = AIDebugWindow::aidebug_hotkeys;
    01443
    01445 static const NWidgetPart _nested_ai_debug_widgets[] = {
    01446   NWidget(NWID_HORIZONTAL),
    01447     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
    01448     NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_AI_DEBUG, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    01449     NWidget(WWT_SHADEBOX, COLOUR_GREY),
    01450     NWidget(WWT_STICKYBOX, COLOUR_GREY),
    01451   EndContainer(),
    01452   NWidget(WWT_PANEL, COLOUR_GREY, WID_AID_VIEW),
    01453     NWidgetFunction(MakeCompanyButtonRowsAIDebug), SetPadding(0, 2, 1, 2),
    01454   EndContainer(),
    01455   NWidget(NWID_HORIZONTAL),
    01456     NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_AID_SCRIPT_GAME), SetMinimalSize(100, 20), SetResize(1, 0), SetDataTip(STR_AI_GAME_SCRIPT, STR_AI_GAME_SCRIPT_TOOLTIP),
    01457     NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_AID_NAME_TEXT), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_JUST_STRING, STR_AI_DEBUG_NAME_TOOLTIP),
    01458     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_AID_SETTINGS), SetMinimalSize(100, 20), SetDataTip(STR_AI_DEBUG_SETTINGS, STR_AI_DEBUG_SETTINGS_TOOLTIP),
    01459     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_AID_RELOAD_TOGGLE), SetMinimalSize(100, 20), SetDataTip(STR_AI_DEBUG_RELOAD, STR_AI_DEBUG_RELOAD_TOOLTIP),
    01460   EndContainer(),
    01461   NWidget(NWID_HORIZONTAL),
    01462     NWidget(NWID_VERTICAL),
    01463       /* Log panel */
    01464       NWidget(WWT_PANEL, COLOUR_GREY, WID_AID_LOG_PANEL), SetMinimalSize(287, 180), SetResize(1, 1), SetScrollbar(WID_AID_SCROLLBAR),
    01465       EndContainer(),
    01466       /* Break string widgets */
    01467       NWidget(NWID_SELECTION, INVALID_COLOUR, WID_AID_BREAK_STRING_WIDGETS),
    01468         NWidget(NWID_HORIZONTAL),
    01469           NWidget(WWT_IMGBTN_2, COLOUR_GREY, WID_AID_BREAK_STR_ON_OFF_BTN), SetFill(0, 1), SetDataTip(SPR_FLAG_VEH_STOPPED, STR_AI_DEBUG_BREAK_STR_ON_OFF_TOOLTIP),
    01470           NWidget(WWT_PANEL, COLOUR_GREY),
    01471             NWidget(NWID_HORIZONTAL),
    01472               NWidget(WWT_LABEL, COLOUR_GREY), SetPadding(2, 2, 2, 4), SetDataTip(STR_AI_DEBUG_BREAK_ON_LABEL, 0x0),
    01473               NWidget(WWT_EDITBOX, COLOUR_GREY, WID_AID_BREAK_STR_EDIT_BOX), SetFill(1, 1), SetResize(1, 0), SetPadding(2, 2, 2, 2), SetDataTip(STR_AI_DEBUG_BREAK_STR_OSKTITLE, STR_AI_DEBUG_BREAK_STR_TOOLTIP),
    01474             EndContainer(),
    01475           EndContainer(),
    01476           NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_AID_MATCH_CASE_BTN), SetMinimalSize(100, 0), SetFill(0, 1), SetDataTip(STR_AI_DEBUG_MATCH_CASE, STR_AI_DEBUG_MATCH_CASE_TOOLTIP),
    01477           NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_AID_CONTINUE_BTN), SetMinimalSize(100, 0), SetFill(0, 1), SetDataTip(STR_AI_DEBUG_CONTINUE, STR_AI_DEBUG_CONTINUE_TOOLTIP),
    01478         EndContainer(),
    01479       EndContainer(),
    01480     EndContainer(),
    01481     NWidget(NWID_VERTICAL),
    01482       NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_AID_SCROLLBAR),
    01483       NWidget(WWT_RESIZEBOX, COLOUR_GREY),
    01484     EndContainer(),
    01485   EndContainer(),
    01486 };
    01487
    01489 static const WindowDesc _ai_debug_desc(
    01490   WDP_AUTO, 600, 450,
    01491   WC_AI_DEBUG, WC_NONE,
    01492   0,
    01493   _nested_ai_debug_widgets, lengthof(_nested_ai_debug_widgets)
    01494 );
    01495
    01500 Window *ShowAIDebugWindow(CompanyID show_company)
    01501 {
    01502   if (!_networking || _network_server) {
    01503     AIDebugWindow *w = (AIDebugWindow *)BringWindowToFrontById(WC_AI_DEBUG, 0);
    01504     if (w == NULL) w = new AIDebugWindow(&_ai_debug_desc, 0);
    01505     if (show_company != INVALID_COMPANY) w->ChangeToAI(show_company);
    01506     return w;
    01507   } else {
    01508     ShowErrorMessage(STR_ERROR_AI_DEBUG_SERVER_ONLY, INVALID_STRING_ID, WL_INFO);
    01509   }
    01510
    01511   return NULL;
    01512 }
    01513
    01517 EventState AIDebugGlobalHotkeys(uint16 key, uint16 keycode)
    01518 {
    01519   int num = CheckHotkeyMatch<AIDebugWindow>(_aidebug_hotkeys, keycode, NULL, true);
    01520   if (num == -1) return ES_NOT_HANDLED;
    01521   Window *w = ShowAIDebugWindow(INVALID_COMPANY);
    01522   if (w == NULL) return ES_NOT_HANDLED;
    01523   return w->OnKeyPress(key, keycode);
    01524 }
    01525
    01529 void InitializeAIGui()
    01530 {
    01531   AIDebugWindow::ai_debug_company = INVALID_COMPANY;
    01532 }
    01533
    01535 void ShowAIDebugWindowIfAIError()
    01536 {
    01537   /* Network clients can't debug AIs. */
    01538   if (_networking && !_network_server) return;
    01539
    01540   Company *c;
    01541   FOR_ALL_COMPANIES(c) {
    01542     if (c->is_ai && c->ai_instance->IsDead()) {
    01543       ShowAIDebugWindow(c->index);
    01544       break;
    01545     }
    01546   }
    01547
    01548   GameInstance *g = Game::GetGameInstance();
    01549   if (g != NULL && g->IsDead()) {
    01550     ShowAIDebugWindow(OWNER_DEITY);
    01551   }
    01552 }