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   WDF_UNCLICK_BUTTONS,
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   WDF_UNCLICK_BUTTONS,
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   WDF_UNCLICK_BUTTONS,
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 QueryStringBaseWindow {
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   static StringFilter break_string_filter;               
00998   static bool case_sensitive_break_check;                
00999   int highlight_row;                                     
01000   Scrollbar *vscroll;                                    
01001 
01002   ScriptLog::LogData *GetLogPointer() const
01003   {
01004     if (ai_debug_company == OWNER_DEITY) return (ScriptLog::LogData *)Game::GetInstance()->GetLogPointer();
01005     return (ScriptLog::LogData *)Company::Get(ai_debug_company)->ai_instance->GetLogPointer();
01006   }
01007 
01013   AIDebugWindow(const WindowDesc *desc, WindowNumber number) : QueryStringBaseWindow(MAX_BREAK_STR_STRING_LENGTH)
01014   {
01015     this->CreateNestedTree(desc);
01016     this->vscroll = this->GetScrollbar(WID_AID_SCROLLBAR);
01017     this->show_break_box = _settings_client.gui.ai_developer_tools;
01018     this->GetWidget<NWidgetStacked>(WID_AID_BREAK_STRING_WIDGETS)->SetDisplayedPlane(this->show_break_box ? 0 : SZSP_HORIZONTAL);
01019     this->FinishInitNested(desc, number);
01020 
01021     if (!this->show_break_box) break_check_enabled = false;
01022     /* Disable the companies who are not active or not an AI */
01023     for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
01024       this->SetWidgetDisabledState(i + WID_AID_COMPANY_BUTTON_START, !Company::IsValidAiID(i));
01025     }
01026     this->EnableWidget(WID_AID_SCRIPT_GAME);
01027     this->DisableWidget(WID_AID_RELOAD_TOGGLE);
01028     this->DisableWidget(WID_AID_SETTINGS);
01029     this->DisableWidget(WID_AID_CONTINUE_BTN);
01030 
01031     this->last_vscroll_pos = 0;
01032     this->autoscroll = true;
01033     this->highlight_row = -1;
01034     this->text.Initialize(this->edit_str_buf, this->edit_str_size, MAX_BREAK_STR_STRING_LENGTH);
01035 
01036     /* Restore the break string value from static variable */
01037     strecpy(this->edit_str_buf, this->break_string, this->edit_str_buf + MAX_BREAK_STR_STRING_LENGTH);
01038     this->text.UpdateSize();
01039 
01040     /* Restore button state from static class variables */
01041     if (ai_debug_company == OWNER_DEITY) {
01042       this->LowerWidget(WID_AID_SCRIPT_GAME);
01043       this->SetWidgetDisabledState(WID_AID_CONTINUE_BTN, !Game::IsPaused());
01044     } else if (ai_debug_company != INVALID_COMPANY) {
01045       this->LowerWidget(ai_debug_company + WID_AID_COMPANY_BUTTON_START);
01046       this->SetWidgetDisabledState(WID_AID_CONTINUE_BTN, !AI::IsPaused(ai_debug_company));
01047     }
01048     this->SetWidgetLoweredState(WID_AID_BREAK_STR_ON_OFF_BTN, this->break_check_enabled);
01049     this->SetWidgetLoweredState(WID_AID_MATCH_CASE_BTN, this->case_sensitive_break_check);
01050 
01051   }
01052 
01053   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
01054   {
01055     if (widget == WID_AID_LOG_PANEL) {
01056       resize->height = FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
01057       size->height = 14 * resize->height + this->top_offset + this->bottom_offset;
01058     }
01059   }
01060 
01061   virtual void OnPaint()
01062   {
01063     /* Check if the currently selected company is still active. */
01064     if (ai_debug_company == INVALID_COMPANY || (ai_debug_company != OWNER_DEITY && !Company::IsValidAiID(ai_debug_company))) {
01065       if (ai_debug_company != INVALID_COMPANY) {
01066         /* Raise the widget for the previous selection. */
01067         this->RaiseWidget(ai_debug_company + WID_AID_COMPANY_BUTTON_START);
01068 
01069         ai_debug_company = INVALID_COMPANY;
01070       }
01071 
01072       const Company *c;
01073       FOR_ALL_COMPANIES(c) {
01074         if (c->is_ai) {
01075           /* Lower the widget corresponding to this company. */
01076           this->LowerWidget(c->index + WID_AID_COMPANY_BUTTON_START);
01077 
01078           ai_debug_company = c->index;
01079           break;
01080         }
01081       }
01082 
01083       /* If no AI is available, see if there is a game script. */
01084       if (ai_debug_company == INVALID_COMPANY && Game::GetInstance() != NULL) {
01085         /* Lower the widget corresponding to the game script. */
01086         this->LowerWidget(WID_AID_SCRIPT_GAME);
01087 
01088         ai_debug_company = OWNER_DEITY;
01089       }
01090     }
01091 
01092     /* Update "Reload AI" and "AI settings" buttons */
01093     this->SetWidgetDisabledState(WID_AID_SETTINGS, ai_debug_company == INVALID_COMPANY);
01094     this->SetWidgetDisabledState(WID_AID_RELOAD_TOGGLE, ai_debug_company == INVALID_COMPANY || ai_debug_company == OWNER_DEITY);
01095     this->SetWidgetDisabledState(WID_AID_SCRIPT_GAME, Game::GetGameInstance() == NULL);
01096 
01097     /* Draw standard stuff */
01098     this->DrawWidgets();
01099 
01100     if (this->IsShaded()) return; // Don't draw anything when the window is shaded.
01101 
01102     if (this->show_break_box) this->DrawEditBox(WID_AID_BREAK_STR_EDIT_BOX);
01103 
01104     /* Paint the company icons */
01105     for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
01106       NWidgetCore *button = this->GetWidget<NWidgetCore>(i + WID_AID_COMPANY_BUTTON_START);
01107       bool dirty = false;
01108 
01109       bool valid = Company::IsValidAiID(i);
01110       bool disabled = !valid;
01111       if (button->IsDisabled() != disabled) {
01112         /* Invalid/non-AI companies have button disabled */
01113         button->SetDisabled(disabled);
01114         dirty = true;
01115       }
01116 
01117       /* Mark dead/paused AIs by setting the background colour. */
01118       bool dead = valid && Company::Get(i)->ai_instance->IsDead();
01119       bool paused = valid && Company::Get(i)->ai_instance->IsPaused();
01120       /* Re-paint if the button was updated.
01121        * (note that it is intentional that SetScriptButtonColour is always called) */
01122       dirty = SetScriptButtonColour(*button, dead, paused) || dirty;
01123 
01124       /* Do we need a repaint? */
01125       if (dirty) this->SetDirty();
01126       /* Draw company icon only for valid AI companies */
01127       if (!valid) continue;
01128 
01129       byte offset = (i == ai_debug_company) ? 1 : 0;
01130       DrawCompanyIcon(i, button->pos_x + button->current_x / 2 - 7 + offset, this->GetWidget<NWidgetBase>(WID_AID_COMPANY_BUTTON_START + i)->pos_y + 2 + offset);
01131     }
01132 
01133     /* Set button colour for Game Script. */
01134     GameInstance *game = Game::GetInstance();
01135     bool dead = game != NULL && game->IsDead();
01136     bool paused = game != NULL && game->IsPaused();
01137     NWidgetCore *button = this->GetWidget<NWidgetCore>(WID_AID_SCRIPT_GAME);
01138     if (SetScriptButtonColour(*button, dead, paused)) {
01139       /* Re-paint if the button was updated. */
01140       this->SetWidgetDirty(WID_AID_SCRIPT_GAME);
01141     }
01142 
01143     /* If there are no active companies, don't display anything else. */
01144     if (ai_debug_company == INVALID_COMPANY) return;
01145 
01146     ScriptLog::LogData *log = this->GetLogPointer();
01147 
01148     int scroll_count = (log == NULL) ? 0 : log->used;
01149     if (this->vscroll->GetCount() != scroll_count) {
01150       this->vscroll->SetCount(scroll_count);
01151 
01152       /* We need a repaint */
01153       this->SetWidgetDirty(WID_AID_SCROLLBAR);
01154     }
01155 
01156     if (log == NULL) return;
01157 
01158     /* Detect when the user scrolls the window. Enable autoscroll when the
01159      * bottom-most line becomes visible. */
01160     if (this->last_vscroll_pos != this->vscroll->GetPosition()) {
01161       this->autoscroll = this->vscroll->GetPosition() >= log->used - this->vscroll->GetCapacity();
01162     }
01163     if (this->autoscroll) {
01164       int scroll_pos = max(0, log->used - this->vscroll->GetCapacity());
01165       if (scroll_pos != this->vscroll->GetPosition()) {
01166         this->vscroll->SetPosition(scroll_pos);
01167 
01168         /* We need a repaint */
01169         this->SetWidgetDirty(WID_AID_SCROLLBAR);
01170         this->SetWidgetDirty(WID_AID_LOG_PANEL);
01171       }
01172     }
01173     this->last_vscroll_pos = this->vscroll->GetPosition();
01174   }
01175 
01176   virtual void SetStringParameters(int widget) const
01177   {
01178     switch (widget) {
01179       case WID_AID_NAME_TEXT:
01180         if (ai_debug_company == OWNER_DEITY) {
01181           const GameInfo *info = Game::GetInfo();
01182           assert(info != NULL);
01183           SetDParam(0, STR_AI_DEBUG_NAME_AND_VERSION);
01184           SetDParamStr(1, info->GetName());
01185           SetDParam(2, info->GetVersion());
01186         } else if (ai_debug_company == INVALID_COMPANY || !Company::IsValidAiID(ai_debug_company)) {
01187           SetDParam(0, STR_EMPTY);
01188         } else {
01189           const AIInfo *info = Company::Get(ai_debug_company)->ai_info;
01190           assert(info != NULL);
01191           SetDParam(0, STR_AI_DEBUG_NAME_AND_VERSION);
01192           SetDParamStr(1, info->GetName());
01193           SetDParam(2, info->GetVersion());
01194         }
01195         break;
01196     }
01197   }
01198 
01199   virtual void DrawWidget(const Rect &r, int widget) const
01200   {
01201     if (ai_debug_company == INVALID_COMPANY) return;
01202 
01203     switch (widget) {
01204       case WID_AID_LOG_PANEL: {
01205         ScriptLog::LogData *log = this->GetLogPointer();
01206         if (log == NULL) return;
01207 
01208         int y = this->top_offset;
01209         for (int i = this->vscroll->GetPosition(); this->vscroll->IsVisible(i) && i < log->used; i++) {
01210           int pos = (i + log->pos + 1 - log->used + log->count) % log->count;
01211           if (log->lines[pos] == NULL) break;
01212 
01213           TextColour colour;
01214           switch (log->type[pos]) {
01215             case ScriptLog::LOG_SQ_INFO:  colour = TC_BLACK;  break;
01216             case ScriptLog::LOG_SQ_ERROR: colour = TC_RED;    break;
01217             case ScriptLog::LOG_INFO:     colour = TC_BLACK;  break;
01218             case ScriptLog::LOG_WARNING:  colour = TC_YELLOW; break;
01219             case ScriptLog::LOG_ERROR:    colour = TC_RED;    break;
01220             default:                  colour = TC_BLACK;  break;
01221           }
01222 
01223           /* Check if the current line should be highlighted */
01224           if (pos == this->highlight_row) {
01225             GfxFillRect(r.left + 1, r.top + y, r.right - 1, r.top + y + this->resize.step_height - WD_PAR_VSEP_NORMAL, PC_BLACK);
01226             if (colour == TC_BLACK) colour = TC_WHITE; // Make black text readable by inverting it to white.
01227           }
01228 
01229           DrawString(r.left + 7, r.right - 7, r.top + y, log->lines[pos], colour, SA_LEFT | SA_FORCE);
01230           y += this->resize.step_height;
01231         }
01232         break;
01233       }
01234     }
01235   }
01236 
01241   void ChangeToAI(CompanyID show_ai)
01242   {
01243     if (ai_debug_company == OWNER_DEITY) {
01244       this->RaiseWidget(WID_AID_SCRIPT_GAME);
01245     } else {
01246       this->RaiseWidget(ai_debug_company + WID_AID_COMPANY_BUTTON_START);
01247     }
01248     ai_debug_company = show_ai;
01249 
01250     ScriptLog::LogData *log = this->GetLogPointer();
01251     this->vscroll->SetCount((log == NULL) ? 0 : log->used);
01252 
01253     if (ai_debug_company == OWNER_DEITY) {
01254       this->LowerWidget(WID_AID_SCRIPT_GAME);
01255       this->SetWidgetDisabledState(WID_AID_CONTINUE_BTN, !Game::IsPaused());
01256     } else {
01257       this->LowerWidget(ai_debug_company + WID_AID_COMPANY_BUTTON_START);
01258       this->SetWidgetDisabledState(WID_AID_CONTINUE_BTN, !AI::IsPaused(ai_debug_company));
01259     }
01260 
01261     this->highlight_row = -1; // The highlight of one AI make little sense for another AI.
01262     this->autoscroll = true;
01263     this->last_vscroll_pos = this->vscroll->GetPosition();
01264     this->SetDirty();
01265     /* Close AI settings window to prevent confusion */
01266     DeleteWindowByClass(WC_AI_SETTINGS);
01267   }
01268 
01269   virtual void OnClick(Point pt, int widget, int click_count)
01270   {
01271     /* Check which button is clicked */
01272     if (IsInsideMM(widget, WID_AID_COMPANY_BUTTON_START, WID_AID_COMPANY_BUTTON_END + 1)) {
01273       /* Is it no on disable? */
01274       if (!this->IsWidgetDisabled(widget)) {
01275         ChangeToAI((CompanyID)(widget - WID_AID_COMPANY_BUTTON_START));
01276       }
01277     }
01278 
01279     switch (widget) {
01280       case WID_AID_SCRIPT_GAME:
01281         ChangeToAI(OWNER_DEITY);
01282         break;
01283 
01284       case WID_AID_RELOAD_TOGGLE:
01285         if (ai_debug_company == OWNER_DEITY) break;
01286         /* First kill the company of the AI, then start a new one. This should start the current AI again */
01287         DoCommandP(0, 2 | ai_debug_company << 16, CRR_MANUAL, CMD_COMPANY_CTRL);
01288         DoCommandP(0, 1 | ai_debug_company << 16, 0, CMD_COMPANY_CTRL);
01289         break;
01290 
01291       case WID_AID_SETTINGS:
01292         ShowAISettingsWindow(ai_debug_company);
01293         break;
01294 
01295       case WID_AID_BREAK_STR_ON_OFF_BTN:
01296         this->break_check_enabled = !this->break_check_enabled;
01297         this->SetWidgetLoweredState(WID_AID_BREAK_STR_ON_OFF_BTN, this->break_check_enabled);
01298         this->SetWidgetDirty(WID_AID_BREAK_STR_ON_OFF_BTN);
01299         break;
01300 
01301       case WID_AID_MATCH_CASE_BTN:
01302         this->case_sensitive_break_check = !this->case_sensitive_break_check;
01303         this->SetWidgetLoweredState(WID_AID_MATCH_CASE_BTN, this->case_sensitive_break_check);
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         this->RaiseWidget(WID_AID_CONTINUE_BTN); // Disabled widgets don't raise themself
01338         break;
01339     }
01340   }
01341 
01342   virtual void OnTimeout()
01343   {
01344     this->RaiseWidget(WID_AID_RELOAD_TOGGLE);
01345     this->RaiseWidget(WID_AID_SETTINGS);
01346     this->SetDirty();
01347   }
01348 
01349   virtual void OnMouseLoop()
01350   {
01351     this->HandleEditBox(WID_AID_BREAK_STR_EDIT_BOX);
01352   }
01353 
01354   virtual EventState OnKeyPress(uint16 key, uint16 keycode)
01355   {
01356     EventState state = ES_NOT_HANDLED;
01357     switch (this->HandleEditBoxKey(WID_AID_BREAK_STR_EDIT_BOX, key, keycode, state)) {
01358       case HEBR_EDITING:
01359         /* Save the current string to static member so it can be restored next time the window is opened. */
01360         strecpy(this->break_string, this->edit_str_buf, lastof(this->break_string));
01361         break_string_filter.SetFilterTerm(this->break_string);
01362         break;
01363 
01364       case HEBR_CANCEL:
01365         /* Unfocus the text box. */
01366         this->UnfocusFocusedWidget();
01367         break;
01368 
01369       case HEBR_NOT_FOCUSED: {
01370         /* Edit boxs is not globally foused => handle hotkeys of AI Debug window. */
01371         int num = CheckHotkeyMatch(aidebug_hotkeys, keycode, this);
01372         if (num == -1) return ES_NOT_HANDLED;
01373         if (this->show_break_box && num == WID_AID_BREAK_STR_EDIT_BOX) {
01374           this->SetFocusedWidget(WID_AID_BREAK_STR_EDIT_BOX);
01375           SetFocusedWindow(this);
01376           state = ES_HANDLED;
01377         } else if (this->show_break_box || num < WID_AID_BREAK_STRING_WIDGETS) {
01378           this->OnClick(Point(), num, 1);
01379           state = ES_HANDLED;
01380         }
01381         break;
01382       }
01383 
01384       default:
01385         break;
01386     }
01387     return state;
01388   }
01389 
01395   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
01396   {
01397     if (data == -1 || ai_debug_company == data) this->SetDirty();
01398 
01399     /* If the log message is related to the active company tab, check the break string.
01400      * This needs to be done in gameloop-scope, so the AI is suspended immediately. */
01401     if (!gui_scope && data == ai_debug_company && this->break_check_enabled && !this->break_string_filter.IsEmpty()) {
01402       /* Get the log instance of the active company */
01403       ScriptLog::LogData *log = this->GetLogPointer();
01404 
01405       if (log != NULL) {
01406         this->break_string_filter.ResetState();
01407         this->break_string_filter.AddLine(log->lines[log->pos]);
01408         if (this->break_string_filter.GetState()) {
01409           /* Pause execution of script. */
01410           if (ai_debug_company == OWNER_DEITY) {
01411             Game::Pause();
01412           } else {
01413             AI::Pause(ai_debug_company);
01414           }
01415 
01416           /* Pause the game. */
01417           if ((_pause_mode & PM_PAUSED_NORMAL) == PM_UNPAUSED) {
01418             DoCommandP(0, PM_PAUSED_NORMAL, 1, CMD_PAUSE);
01419           }
01420 
01421           /* Make it possible to click on the continue button */
01422           this->EnableWidget(WID_AID_CONTINUE_BTN);
01423           this->SetWidgetDirty(WID_AID_CONTINUE_BTN);
01424 
01425           /* Highlight row that matched */
01426           this->highlight_row = log->pos;
01427         }
01428       }
01429     }
01430   }
01431 
01432   virtual void OnResize()
01433   {
01434     this->vscroll->SetCapacityFromWidget(this, WID_AID_LOG_PANEL);
01435   }
01436 
01437   static Hotkey<AIDebugWindow> aidebug_hotkeys[];
01438 };
01439 
01440 const int AIDebugWindow::top_offset = WD_FRAMERECT_TOP + 2;
01441 const int AIDebugWindow::bottom_offset = WD_FRAMERECT_BOTTOM;
01442 CompanyID AIDebugWindow::ai_debug_company = INVALID_COMPANY;
01443 char AIDebugWindow::break_string[MAX_BREAK_STR_STRING_LENGTH] = "";
01444 bool AIDebugWindow::break_check_enabled = true;
01445 bool AIDebugWindow::case_sensitive_break_check = false;
01446 StringFilter AIDebugWindow::break_string_filter(&AIDebugWindow::case_sensitive_break_check);
01447 
01449 NWidgetBase *MakeCompanyButtonRowsAIDebug(int *biggest_index)
01450 {
01451   return MakeCompanyButtonRows(biggest_index, WID_AID_COMPANY_BUTTON_START, WID_AID_COMPANY_BUTTON_END, 8, STR_AI_DEBUG_SELECT_AI_TOOLTIP);
01452 }
01453 
01454 Hotkey<AIDebugWindow> AIDebugWindow::aidebug_hotkeys[] = {
01455   Hotkey<AIDebugWindow>('1', "company_1", WID_AID_COMPANY_BUTTON_START),
01456   Hotkey<AIDebugWindow>('2', "company_2", WID_AID_COMPANY_BUTTON_START + 1),
01457   Hotkey<AIDebugWindow>('3', "company_3", WID_AID_COMPANY_BUTTON_START + 2),
01458   Hotkey<AIDebugWindow>('4', "company_4", WID_AID_COMPANY_BUTTON_START + 3),
01459   Hotkey<AIDebugWindow>('5', "company_5", WID_AID_COMPANY_BUTTON_START + 4),
01460   Hotkey<AIDebugWindow>('6', "company_6", WID_AID_COMPANY_BUTTON_START + 5),
01461   Hotkey<AIDebugWindow>('7', "company_7", WID_AID_COMPANY_BUTTON_START + 6),
01462   Hotkey<AIDebugWindow>('8', "company_8", WID_AID_COMPANY_BUTTON_START + 7),
01463   Hotkey<AIDebugWindow>('9', "company_9", WID_AID_COMPANY_BUTTON_START + 8),
01464   Hotkey<AIDebugWindow>((uint16)0, "company_10", WID_AID_COMPANY_BUTTON_START + 9),
01465   Hotkey<AIDebugWindow>((uint16)0, "company_11", WID_AID_COMPANY_BUTTON_START + 10),
01466   Hotkey<AIDebugWindow>((uint16)0, "company_12", WID_AID_COMPANY_BUTTON_START + 11),
01467   Hotkey<AIDebugWindow>((uint16)0, "company_13", WID_AID_COMPANY_BUTTON_START + 12),
01468   Hotkey<AIDebugWindow>((uint16)0, "company_14", WID_AID_COMPANY_BUTTON_START + 13),
01469   Hotkey<AIDebugWindow>((uint16)0, "company_15", WID_AID_COMPANY_BUTTON_START + 14),
01470   Hotkey<AIDebugWindow>('S', "settings", WID_AID_SETTINGS),
01471   Hotkey<AIDebugWindow>('0', "game_script", WID_AID_SCRIPT_GAME),
01472   Hotkey<AIDebugWindow>((uint16)0, "reload", WID_AID_RELOAD_TOGGLE),
01473   Hotkey<AIDebugWindow>('B', "break_toggle", WID_AID_BREAK_STR_ON_OFF_BTN),
01474   Hotkey<AIDebugWindow>('F', "break_string", WID_AID_BREAK_STR_EDIT_BOX),
01475   Hotkey<AIDebugWindow>('C', "match_case", WID_AID_MATCH_CASE_BTN),
01476   Hotkey<AIDebugWindow>(WKC_RETURN, "continue", WID_AID_CONTINUE_BTN),
01477   HOTKEY_LIST_END(AIDebugWindow)
01478 };
01479 Hotkey<AIDebugWindow> *_aidebug_hotkeys = AIDebugWindow::aidebug_hotkeys;
01480 
01482 static const NWidgetPart _nested_ai_debug_widgets[] = {
01483   NWidget(NWID_HORIZONTAL),
01484     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
01485     NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_AI_DEBUG, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
01486     NWidget(WWT_SHADEBOX, COLOUR_GREY),
01487     NWidget(WWT_STICKYBOX, COLOUR_GREY),
01488   EndContainer(),
01489   NWidget(WWT_PANEL, COLOUR_GREY, WID_AID_VIEW),
01490     NWidgetFunction(MakeCompanyButtonRowsAIDebug), SetPadding(0, 2, 1, 2),
01491   EndContainer(),
01492   NWidget(NWID_HORIZONTAL),
01493     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_AID_SCRIPT_GAME), SetMinimalSize(100, 20), SetResize(1, 0), SetDataTip(STR_AI_GAME_SCRIPT, STR_AI_GAME_SCRIPT_TOOLTIP),
01494     NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_AID_NAME_TEXT), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_JUST_STRING, STR_AI_DEBUG_NAME_TOOLTIP),
01495     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_AID_SETTINGS), SetMinimalSize(100, 20), SetDataTip(STR_AI_DEBUG_SETTINGS, STR_AI_DEBUG_SETTINGS_TOOLTIP),
01496     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_AID_RELOAD_TOGGLE), SetMinimalSize(100, 20), SetDataTip(STR_AI_DEBUG_RELOAD, STR_AI_DEBUG_RELOAD_TOOLTIP),
01497   EndContainer(),
01498   NWidget(NWID_HORIZONTAL),
01499     NWidget(NWID_VERTICAL),
01500       /* Log panel */
01501       NWidget(WWT_PANEL, COLOUR_GREY, WID_AID_LOG_PANEL), SetMinimalSize(287, 180), SetResize(1, 1), SetScrollbar(WID_AID_SCROLLBAR),
01502       EndContainer(),
01503       /* Break string widgets */
01504       NWidget(NWID_SELECTION, INVALID_COLOUR, WID_AID_BREAK_STRING_WIDGETS),
01505         NWidget(NWID_HORIZONTAL),
01506           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),
01507           NWidget(WWT_PANEL, COLOUR_GREY),
01508             NWidget(NWID_HORIZONTAL),
01509               NWidget(WWT_LABEL, COLOUR_GREY), SetPadding(2, 2, 2, 4), SetDataTip(STR_AI_DEBUG_BREAK_ON_LABEL, 0x0),
01510               NWidget(WWT_EDITBOX, COLOUR_WHITE, 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),
01511             EndContainer(),
01512           EndContainer(),
01513           NWidget(WWT_PUSHTXTBTN, 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),
01514           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),
01515         EndContainer(),
01516       EndContainer(),
01517     EndContainer(),
01518     NWidget(NWID_VERTICAL),
01519       NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_AID_SCROLLBAR),
01520       NWidget(WWT_RESIZEBOX, COLOUR_GREY),
01521     EndContainer(),
01522   EndContainer(),
01523 };
01524 
01526 static const WindowDesc _ai_debug_desc(
01527   WDP_AUTO, 600, 450,
01528   WC_AI_DEBUG, WC_NONE,
01529   0,
01530   _nested_ai_debug_widgets, lengthof(_nested_ai_debug_widgets)
01531 );
01532 
01537 Window *ShowAIDebugWindow(CompanyID show_company)
01538 {
01539   if (!_networking || _network_server) {
01540     AIDebugWindow *w = (AIDebugWindow *)BringWindowToFrontById(WC_AI_DEBUG, 0);
01541     if (w == NULL) w = new AIDebugWindow(&_ai_debug_desc, 0);
01542     if (show_company != INVALID_COMPANY) w->ChangeToAI(show_company);
01543     return w;
01544   } else {
01545     ShowErrorMessage(STR_ERROR_AI_DEBUG_SERVER_ONLY, INVALID_STRING_ID, WL_INFO);
01546   }
01547 
01548   return NULL;
01549 }
01550 
01554 EventState AIDebugGlobalHotkeys(uint16 key, uint16 keycode)
01555 {
01556   int num = CheckHotkeyMatch<AIDebugWindow>(_aidebug_hotkeys, keycode, NULL, true);
01557   if (num == -1) return ES_NOT_HANDLED;
01558   Window *w = ShowAIDebugWindow(INVALID_COMPANY);
01559   if (w == NULL) return ES_NOT_HANDLED;
01560   return w->OnKeyPress(key, keycode);
01561 }
01562 
01566 void InitializeAIGui()
01567 {
01568   AIDebugWindow::ai_debug_company = INVALID_COMPANY;
01569 }
01570 
01572 void ShowAIDebugWindowIfAIError()
01573 {
01574   /* Network clients can't debug AIs. */
01575   if (_networking && !_network_server) return;
01576 
01577   Company *c;
01578   FOR_ALL_COMPANIES(c) {
01579     if (c->is_ai && c->ai_instance->IsDead()) {
01580       ShowAIDebugWindow(c->index);
01581       break;
01582     }
01583   }
01584 
01585   GameInstance *g = Game::GetGameInstance();
01586   if (g != NULL && g->IsDead()) {
01587     ShowAIDebugWindow(OWNER_DEITY);
01588   }
01589 }