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