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