# 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 "../gui.h"
    00016 #include "../querystring_gui.h"
    00017 #include "../company_base.h"
    00018 #include "../company_gui.h"
    00019 #include "../strings_func.h"
    00020 #include "../window_func.h"
    00021 #include "../gfx_func.h"
    00022 #include "../command_func.h"
    00023 #include "../network/network.h"
    00024 #include "../settings_func.h"
    00025 #include "../network/network_content.h"
    00026 #include "../textfile_gui.h"
    00027
    00028 #include "ai.hpp"
    00029 #include "../script/api/script_log.hpp"
    00030 #include "ai_config.hpp"
    00031 #include "ai_info.hpp"
    00032 #include "ai_instance.hpp"
    00033 #include "../game/game.hpp"
    00034 #include "../game/game_config.hpp"
    00035 #include "../game/game_info.hpp"
    00036 #include "../game/game_instance.hpp"
    00037
    00038
    00039 #include "table/strings.h"
    00040
    00041 #include <vector>
    00042
    00043 static ScriptConfig *GetConfig(CompanyID slot)
    00044 {
    00045   if (slot == OWNER_DEITY) return GameConfig::GetConfig();
    00046   return AIConfig::GetConfig(slot);
    00047 }
    00048
    00052 struct AIListWindow : public Window {
    00053   const ScriptInfoList *info_list;
    00054   int selected;
    00055   CompanyID slot;
    00056   int line_height;
    00057   Scrollbar *vscroll;
    00058
    00064   AIListWindow(const WindowDesc *desc, CompanyID slot) : Window(),
    00065     slot(slot)
    00066   {
    00067     if (slot == OWNER_DEITY) {
    00068       this->info_list = Game::GetUniqueInfoList();
    00069     } else {
    00070       this->info_list = AI::GetUniqueInfoList();
    00071     }
    00072
    00073     this->CreateNestedTree(desc);
    00074     this->vscroll = this->GetScrollbar(WID_AIL_SCROLLBAR);
    00075     this->FinishInitNested(desc); // Initializes 'this->line_height' as side effect.
    00076
    00077     this->vscroll->SetCount((int)this->info_list->size() + 1);
    00078
    00079     /* Try if we can find the currently selected AI */
    00080     this->selected = -1;
    00081     if (GetConfig(slot)->HasScript()) {
    00082       ScriptInfo *info = GetConfig(slot)->GetInfo();
    00083       int i = 0;
    00084       for (ScriptInfoList::const_iterator it = this->info_list->begin(); it != this->info_list->end(); it++, i++) {
    00085         if ((*it).second == info) {
    00086           this->selected = i;
    00087           break;
    00088         }
    00089       }
    00090     }
    00091   }
    00092
    00093   virtual void SetStringParameters(int widget) const
    00094   {
    00095     switch (widget) {
    00096       case WID_AIL_CAPTION:
    00097         SetDParam(0, (this->slot == OWNER_DEITY) ? STR_AI_LIST_CAPTION_GAMESCRIPT : STR_AI_LIST_CAPTION_AI);
    00098         break;
    00099     }
    00100   }
    00101
    00102   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    00103   {
    00104     if (widget == WID_AIL_LIST) {
    00105       this->line_height = FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM;
    00106
    00107       resize->width = 1;
    00108       resize->height = this->line_height;
    00109       size->height = GB(this->GetWidget<NWidgetCore>(widget)->widget_data, MAT_ROW_START, MAT_ROW_BITS) * this->line_height;
    00110     }
    00111   }
    00112
    00113   virtual void DrawWidget(const Rect &r, int widget) const
    00114   {
    00115     switch (widget) {
    00116       case WID_AIL_LIST: {
    00117         /* Draw a list of all available AIs. */
    00118         int y = this->GetWidget<NWidgetBase>(WID_AIL_LIST)->pos_y;
    00119         /* First AI in the list is hardcoded to random */
    00120         if (this->vscroll->IsVisible(0)) {
    00121           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_BLACK);
    00122           y += this->line_height;
    00123         }
    00124         ScriptInfoList::const_iterator it = this->info_list->begin();
    00125         for (int i = 1; it != this->info_list->end(); i++, it++) {
    00126           if (this->vscroll->IsVisible(i)) {
    00127             DrawString(r.left + WD_MATRIX_LEFT, r.right - WD_MATRIX_RIGHT, y + WD_MATRIX_TOP, (*it).second->GetName(), (this->selected == i - 1) ? TC_WHITE : TC_BLACK);
    00128             y += this->line_height;
    00129           }
    00130         }
    00131         break;
    00132       }
    00133       case WID_AIL_INFO_BG: {
    00134         AIInfo *selected_info = NULL;
    00135         ScriptInfoList::const_iterator it = this->info_list->begin();
    00136         for (int i = 1; selected_info == NULL && it != this->info_list->end(); i++, it++) {
    00137           if (this->selected == i - 1) selected_info = static_cast<AIInfo *>((*it).second);
    00138         }
    00139         /* Some info about the currently selected AI. */
    00140         if (selected_info != NULL) {
    00141           int y = r.top + WD_FRAMERECT_TOP;
    00142           SetDParamStr(0, selected_info->GetAuthor());
    00143           DrawString(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, y, STR_AI_LIST_AUTHOR);
    00144           y += FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
    00145           SetDParam(0, selected_info->GetVersion());
    00146           DrawString(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, y, STR_AI_LIST_VERSION);
    00147           y += FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
    00148           if (selected_info->GetURL() != NULL) {
    00149             SetDParamStr(0, selected_info->GetURL());
    00150             DrawString(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, y, STR_AI_LIST_URL);
    00151             y += FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
    00152           }
    00153           SetDParamStr(0, selected_info->GetDescription());
    00154           DrawStringMultiLine(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, y, r.bottom - WD_FRAMERECT_BOTTOM, STR_JUST_RAW_STRING, TC_BLACK);
    00155         }
    00156         break;
    00157       }
    00158     }
    00159   }
    00160
    00164   void ChangeAI()
    00165   {
    00166     if (this->selected == -1) {
    00167       GetConfig(slot)->Change(NULL);
    00168     } else {
    00169       ScriptInfoList::const_iterator it = this->info_list->begin();
    00170       for (int i = 0; i < this->selected; i++) it++;
    00171       GetConfig(slot)->Change((*it).second->GetName(), (*it).second->GetVersion());
    00172     }
    00173     InvalidateWindowData(WC_GAME_OPTIONS, WN_GAME_OPTIONS_AI);
    00174     InvalidateWindowClassesData(WC_AI_SETTINGS);
    00175     DeleteWindowByClass(WC_QUERY_STRING);
    00176   }
    00177
    00178   virtual void OnClick(Point pt, int widget, int click_count)
    00179   {
    00180     switch (widget) {
    00181       case WID_AIL_LIST: { // Select one of the AIs
    00182         int sel = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_AIL_LIST, 0, this->line_height) - 1;
    00183         if (sel < (int)this->info_list->size()) {
    00184           this->selected = sel;
    00185           this->SetDirty();
    00186           if (click_count > 1) {
    00187             this->ChangeAI();
    00188             delete this;
    00189           }
    00190         }
    00191         break;
    00192       }
    00193
    00194       case WID_AIL_ACCEPT: {
    00195         this->ChangeAI();
    00196         delete this;
    00197         break;
    00198       }
    00199
    00200       case WID_AIL_CANCEL:
    00201         delete this;
    00202         break;
    00203     }
    00204   }
    00205
    00206   virtual void OnResize()
    00207   {
    00208     NWidgetCore *nwi = this->GetWidget<NWidgetCore>(WID_AIL_LIST);
    00209     this->vscroll->SetCapacity(nwi->current_y / this->line_height);
    00210     nwi->widget_data = (this->vscroll->GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
    00211   }
    00212
    00218   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
    00219   {
    00220     if (_game_mode == GM_NORMAL && Company::IsValidID(this->slot)) {
    00221       delete this;
    00222       return;
    00223     }
    00224
    00225     if (!gui_scope) return;
    00226
    00227     this->vscroll->SetCount((int)this->info_list->size() + 1);
    00228
    00229     /* selected goes from -1 .. length of ai list - 1. */
    00230     this->selected = min(this->selected, this->vscroll->GetCount() - 2);
    00231   }
    00232 };
    00233
    00235 static const NWidgetPart _nested_ai_list_widgets[] = {
    00236   NWidget(NWID_HORIZONTAL),
    00237     NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
    00238     NWidget(WWT_CAPTION, COLOUR_MAUVE, WID_AIL_CAPTION), SetDataTip(STR_AI_LIST_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    00239   EndContainer(),
    00240   NWidget(NWID_HORIZONTAL),
    00241     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),
    00242     NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, WID_AIL_SCROLLBAR),
    00243   EndContainer(),
    00244   NWidget(WWT_PANEL, COLOUR_MAUVE, WID_AIL_INFO_BG), SetMinimalTextLines(8, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM), SetResize(1, 0),
    00245   EndContainer(),
    00246   NWidget(NWID_HORIZONTAL),
    00247     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
    00248       NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, WID_AIL_ACCEPT), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_AI_LIST_ACCEPT, STR_AI_LIST_ACCEPT_TOOLTIP),
    00249       NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, WID_AIL_CANCEL), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_AI_LIST_CANCEL, STR_AI_LIST_CANCEL_TOOLTIP),
    00250     EndContainer(),
    00251     NWidget(WWT_RESIZEBOX, COLOUR_MAUVE),
    00252   EndContainer(),
    00253 };
    00254
    00256 static const WindowDesc _ai_list_desc(
    00257   WDP_CENTER, 200, 234,
    00258   WC_AI_LIST, WC_NONE,
    00259   WDF_UNCLICK_BUTTONS,
    00260   _nested_ai_list_widgets, lengthof(_nested_ai_list_widgets)
    00261 );
    00262
    00267 static void ShowAIListWindow(CompanyID slot)
    00268 {
    00269   DeleteWindowByClass(WC_AI_LIST);
    00270   new AIListWindow(&_ai_list_desc, slot);
    00271 }
    00272
    00276 struct AISettingsWindow : public Window {
    00277   CompanyID slot;
    00278   ScriptConfig *ai_config;
    00279   int clicked_button;
    00280   bool clicked_increase;
    00281   int timeout;
    00282   int clicked_row;
    00283   int line_height;
    00284   Scrollbar *vscroll;
    00285   typedef std::vector<const ScriptConfigItem *> VisibleSettingsList;
    00286   VisibleSettingsList visible_settings;
    00287
    00293   AISettingsWindow(const WindowDesc *desc, CompanyID slot) : Window(),
    00294     slot(slot),
    00295     clicked_button(-1),
    00296     timeout(0)
    00297   {
    00298     this->ai_config = GetConfig(slot);
    00299     this->RebuildVisibleSettings();
    00300
    00301     this->CreateNestedTree(desc);
    00302     this->vscroll = this->GetScrollbar(WID_AIS_SCROLLBAR);
    00303     this->FinishInitNested(desc, slot);  // Initializes 'this->line_height' as side effect.
    00304
    00305     this->SetWidgetDisabledState(WID_AIS_RESET, _game_mode != GM_MENU && Company::IsValidID(this->slot));
    00306
    00307     this->vscroll->SetCount((int)this->visible_settings.size());
    00308   }
    00309
    00310   virtual void SetStringParameters(int widget) const
    00311   {
    00312     switch (widget) {
    00313       case WID_AIS_CAPTION:
    00314         SetDParam(0, (this->slot == OWNER_DEITY) ? STR_AI_SETTINGS_CAPTION_GAMESCRIPT : STR_AI_SETTINGS_CAPTION_AI);
    00315         break;
    00316     }
    00317   }
    00318
    00324   void RebuildVisibleSettings()
    00325   {
    00326     visible_settings.clear();
    00327
    00328     ScriptConfigItemList::const_iterator it = this->ai_config->GetConfigList()->begin();
    00329     for (; it != this->ai_config->GetConfigList()->end(); it++) {
    00330       bool no_hide = (it->flags & SCRIPTCONFIG_DEVELOPER) == 0;
    00331       if (no_hide || _settings_client.gui.ai_developer_tools) {
    00332         visible_settings.push_back(&(*it));
    00333       }
    00334     }
    00335   }
    00336
    00337   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    00338   {
    00339     if (widget == WID_AIS_BACKGROUND) {
    00340       this->line_height = FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM;
    00341
    00342       resize->width = 1;
    00343       resize->height = this->line_height;
    00344       size->height = GB(this->GetWidget<NWidgetCore>(widget)->widget_data, MAT_ROW_START, MAT_ROW_BITS) * this->line_height;
    00345     }
    00346   }
    00347
    00348   virtual void DrawWidget(const Rect &r, int widget) const
    00349   {
    00350     if (widget != WID_AIS_BACKGROUND) return;
    00351
    00352     ScriptConfig *config = this->ai_config;
    00353     VisibleSettingsList::const_iterator it = this->visible_settings.begin();
    00354     int i = 0;
    00355     for (; !this->vscroll->IsVisible(i); i++) it++;
    00356
    00357     bool rtl = _current_text_dir == TD_RTL;
    00358     uint buttons_left = rtl ? r.right - 23 : r.left + 4;
    00359     uint text_left    = r.left + (rtl ? WD_FRAMERECT_LEFT : 28);
    00360     uint text_right   = r.right - (rtl ? 28 : WD_FRAMERECT_RIGHT);
    00361
    00362
    00363     int y = r.top;
    00364     for (; this->vscroll->IsVisible(i) && it != visible_settings.end(); i++, it++) {
    00365       const ScriptConfigItem &config_item = **it;
    00366       int current_value = config->GetSetting((config_item).name);
    00367       bool editable = _game_mode == GM_MENU || ((this->slot != OWNER_DEITY) && !Company::IsValidID(this->slot)) || (config_item.flags & SCRIPTCONFIG_INGAME) != 0;
    00368
    00369       StringID str;
    00370       TextColour colour;
    00371       uint idx = 0;
    00372       if (StrEmpty(config_item.description)) {
    00373         if (!strcmp(config_item.name, "start_date")) {
    00374           /* Build-in translation */
    00375           str = STR_AI_SETTINGS_START_DELAY;
    00376           colour = TC_LIGHT_BLUE;
    00377         } else {
    00378           str = STR_JUST_STRING;
    00379           colour = TC_ORANGE;
    00380         }
    00381       } else {
    00382         str = STR_AI_SETTINGS_SETTING;
    00383         colour = TC_LIGHT_BLUE;
    00384         SetDParamStr(idx++, config_item.description);
    00385       }
    00386
    00387       if ((config_item.flags & SCRIPTCONFIG_BOOLEAN) != 0) {
    00388         DrawBoolButton(buttons_left, y + 2, current_value != 0, editable);
    00389         SetDParam(idx++, current_value == 0 ? STR_CONFIG_SETTING_OFF : STR_CONFIG_SETTING_ON);
    00390       } else {
    00391         DrawArrowButtons(buttons_left, y + 2, 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);
    00392         if (config_item.labels != NULL && config_item.labels->Contains(current_value)) {
    00393           SetDParam(idx++, STR_JUST_RAW_STRING);
    00394           SetDParamStr(idx++, config_item.labels->Find(current_value)->second);
    00395         } else {
    00396           SetDParam(idx++, STR_JUST_INT);
    00397           SetDParam(idx++, current_value);
    00398         }
    00399       }
    00400
    00401       DrawString(text_left, text_right, y + WD_MATRIX_TOP, str, colour);
    00402       y += this->line_height;
    00403     }
    00404   }
    00405
    00409   void CheckDifficultyLevel()
    00410   {
    00411     if (_game_mode == GM_MENU) {
    00412       if (_settings_newgame.difficulty.diff_level != 3) {
    00413         _settings_newgame.difficulty.diff_level = 3;
    00414         ShowErrorMessage(STR_WARNING_DIFFICULTY_TO_CUSTOM, INVALID_STRING_ID, WL_WARNING);
    00415       }
    00416     } else if (_settings_game.difficulty.diff_level != 3) {
    00417       IConsoleSetSetting("difficulty.diff_level", 3);
    00418     }
    00419   }
    00420
    00421   virtual void OnClick(Point pt, int widget, int click_count)
    00422   {
    00423     switch (widget) {
    00424       case WID_AIS_BACKGROUND: {
    00425         const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_AIS_BACKGROUND);
    00426         int num = (pt.y - wid->pos_y) / this->line_height + this->vscroll->GetPosition();
    00427         if (num >= (int)this->visible_settings.size()) break;
    00428
    00429         VisibleSettingsList::const_iterator it = this->visible_settings.begin();
    00430         for (int i = 0; i < num; i++) it++;
    00431         const ScriptConfigItem config_item = **it;
    00432         if (_game_mode == GM_NORMAL && ((this->slot == OWNER_DEITY) || Company::IsValidID(this->slot)) && (config_item.flags & SCRIPTCONFIG_INGAME) == 0) return;
    00433
    00434         bool bool_item = (config_item.flags & SCRIPTCONFIG_BOOLEAN) != 0;
    00435
    00436         int x = pt.x - wid->pos_x;
    00437         if (_current_text_dir == TD_RTL) x = wid->current_x - x;
    00438         x -= 4;
    00439         /* One of the arrows is clicked (or green/red rect in case of bool value) */
    00440         if (IsInsideMM(x, 0, 21)) {
    00441           int new_val = this->ai_config->GetSetting(config_item.name);
    00442           int old_val = new_val;
    00443           if (bool_item) {
    00444             new_val = !new_val;
    00445           } else if (x >= 10) {
    00446             /* Increase button clicked */
    00447             new_val += config_item.step_size;
    00448             if (new_val > config_item.max_value) new_val = config_item.max_value;
    00449             this->clicked_increase = true;
    00450           } else {
    00451             /* Decrease button clicked */
    00452             new_val -= config_item.step_size;
    00453             if (new_val < config_item.min_value) new_val = config_item.min_value;
    00454             this->clicked_increase = false;
    00455           }
    00456
    00457           if (new_val != old_val) {
    00458             this->ai_config->SetSetting(config_item.name, new_val);
    00459             this->clicked_button = num;
    00460             this->timeout = 5;
    00461
    00462             this->CheckDifficultyLevel();
    00463           }
    00464         } else if (!bool_item) {
    00465           /* Display a query box so users can enter a custom value. */
    00466           this->clicked_row = num;
    00467           SetDParam(0, this->ai_config->GetSetting(config_item.name));
    00468           ShowQueryString(STR_JUST_INT, STR_CONFIG_SETTING_QUERY_CAPTION, 10, this, CS_NUMERAL, QSF_NONE);
    00469         }
    00470         this->SetDirty();
    00471         break;
    00472       }
    00473
    00474       case WID_AIS_ACCEPT:
    00475         delete this;
    00476         break;
    00477
    00478       case WID_AIS_RESET:
    00479         if (_game_mode == GM_MENU || !Company::IsValidID(this->slot)) {
    00480           this->ai_config->ResetSettings();
    00481           this->SetDirty();
    00482         }
    00483         break;
    00484     }
    00485   }
    00486
    00487   virtual void OnQueryTextFinished(char *str)
    00488   {
    00489     if (StrEmpty(str)) return;
    00490     ScriptConfigItemList::const_iterator it = this->ai_config->GetConfigList()->begin();
    00491     for (int i = 0; i < this->clicked_row; i++) it++;
    00492     if (_game_mode == GM_NORMAL && ((this->slot == OWNER_DEITY) || Company::IsValidID(this->slot)) && (it->flags & SCRIPTCONFIG_INGAME) == 0) return;
    00493     int32 value = atoi(str);
    00494     this->ai_config->SetSetting((*it).name, value);
    00495     this->CheckDifficultyLevel();
    00496     this->SetDirty();
    00497   }
    00498
    00499   virtual void OnResize()
    00500   {
    00501     NWidgetCore *nwi = this->GetWidget<NWidgetCore>(WID_AIS_BACKGROUND);
    00502     this->vscroll->SetCapacity(nwi->current_y / this->line_height);
    00503     nwi->widget_data = (this->vscroll->GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
    00504   }
    00505
    00506   virtual void OnTick()
    00507   {
    00508     if (--this->timeout == 0) {
    00509       this->clicked_button = -1;
    00510       this->SetDirty();
    00511     }
    00512   }
    00513
    00519   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
    00520   {
    00521     this->RebuildVisibleSettings();
    00522   }
    00523 };
    00524
    00526 static const NWidgetPart _nested_ai_settings_widgets[] = {
    00527   NWidget(NWID_HORIZONTAL),
    00528     NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
    00529     NWidget(WWT_CAPTION, COLOUR_MAUVE, WID_AIS_CAPTION), SetDataTip(STR_AI_SETTINGS_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    00530   EndContainer(),
    00531   NWidget(NWID_HORIZONTAL),
    00532     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),
    00533     NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, WID_AIS_SCROLLBAR),
    00534   EndContainer(),
    00535   NWidget(NWID_HORIZONTAL),
    00536     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
    00537       NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, WID_AIS_ACCEPT), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_AI_SETTINGS_CLOSE, STR_NULL),
    00538       NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, WID_AIS_RESET), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_AI_SETTINGS_RESET, STR_NULL),
    00539     EndContainer(),
    00540     NWidget(WWT_RESIZEBOX, COLOUR_MAUVE),
    00541   EndContainer(),
    00542 };
    00543
    00545 static const WindowDesc _ai_settings_desc(
    00546   WDP_CENTER, 500, 208,
    00547   WC_AI_SETTINGS, WC_NONE,
    00548   WDF_UNCLICK_BUTTONS,
    00549   _nested_ai_settings_widgets, lengthof(_nested_ai_settings_widgets)
    00550 );
    00551
    00556 static void ShowAISettingsWindow(CompanyID slot)
    00557 {
    00558   DeleteWindowByClass(WC_AI_LIST);
    00559   DeleteWindowByClass(WC_AI_SETTINGS);
    00560   new AISettingsWindow(&_ai_settings_desc, slot);
    00561 }
    00562
    00563
    00565 struct ScriptTextfileWindow : public TextfileWindow {
    00566   CompanyID slot;
    00567
    00568   ScriptTextfileWindow(TextfileType file_type, CompanyID slot) : TextfileWindow(file_type), slot(slot)
    00569   {
    00570     this->GetWidget<NWidgetCore>(WID_TF_CAPTION)->SetDataTip(STR_TEXTFILE_README_CAPTION + file_type, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS);
    00571
    00572     const char *textfile = GetConfig(slot)->GetTextfile(file_type, slot);
    00573     this->LoadTextfile(textfile, (slot == OWNER_DEITY) ? GAME_DIR : AI_DIR);
    00574   }
    00575
    00576   /* virtual */ void SetStringParameters(int widget) const
    00577   {
    00578     if (widget == WID_TF_CAPTION) {
    00579       SetDParam(0, (slot == OWNER_DEITY) ? STR_CONTENT_TYPE_GAME_SCRIPT : STR_CONTENT_TYPE_AI);
    00580       SetDParamStr(1, GetConfig(slot)->GetName());
    00581     }
    00582   }
    00583 };
    00584
    00590 void ShowScriptTextfileWindow(TextfileType file_type, CompanyID slot)
    00591 {
    00592   DeleteWindowByClass(WC_TEXTFILE);
    00593   new ScriptTextfileWindow(file_type, slot);
    00594 }
    00595
    00596
    00598 static const NWidgetPart _nested_ai_config_widgets[] = {
    00599   NWidget(NWID_HORIZONTAL),
    00600     NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
    00601     NWidget(WWT_CAPTION, COLOUR_MAUVE), SetDataTip(STR_AI_CONFIG_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    00602   EndContainer(),
    00603   NWidget(WWT_PANEL, COLOUR_MAUVE, WID_AIC_BACKGROUND),
    00604     NWidget(NWID_VERTICAL), SetPIP(4, 4, 4),
    00605       NWidget(NWID_HORIZONTAL), SetPIP(7, 0, 7),
    00606         NWidget(WWT_PUSHARROWBTN, COLOUR_YELLOW, WID_AIC_DECREASE), SetFill(0, 1), SetDataTip(AWV_DECREASE, STR_NULL),
    00607         NWidget(WWT_PUSHARROWBTN, COLOUR_YELLOW, WID_AIC_INCREASE), SetFill(0, 1), SetDataTip(AWV_INCREASE, STR_NULL),
    00608         NWidget(NWID_SPACER), SetMinimalSize(6, 0),
    00609         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),
    00610       EndContainer(),
    00611       NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(7, 0, 7),
    00612         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),
    00613         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),
    00614       EndContainer(),
    00615     EndContainer(),
    00616     NWidget(WWT_FRAME, COLOUR_MAUVE), SetDataTip(STR_AI_CONFIG_AI, STR_NULL), SetPadding(0, 5, 0, 5),
    00617       NWidget(NWID_HORIZONTAL),
    00618         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),
    00619         NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, WID_AIC_SCROLLBAR),
    00620       EndContainer(),
    00621     EndContainer(),
    00622     NWidget(NWID_SPACER), SetMinimalSize(0, 9),
    00623     NWidget(WWT_FRAME, COLOUR_MAUVE), SetDataTip(STR_AI_CONFIG_GAMESCRIPT, STR_NULL), SetPadding(0, 5, 4, 5),
    00624       NWidget(WWT_MATRIX, COLOUR_MAUVE, WID_AIC_GAMELIST), SetMinimalSize(288, 14), SetFill(1, 0), SetDataTip(0x101, STR_AI_CONFIG_GAMELIST_TOOLTIP),
    00625     EndContainer(),
    00626     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(7, 0, 7),
    00627       NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_AIC_CHANGE), SetFill(1, 0), SetMinimalSize(93, 12), SetDataTip(STR_AI_CONFIG_CHANGE, STR_AI_CONFIG_CHANGE_TOOLTIP),
    00628       NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_AIC_CONFIGURE), SetFill(1, 0), SetMinimalSize(93, 12), SetDataTip(STR_AI_CONFIG_CONFIGURE, STR_AI_CONFIG_CONFIGURE_TOOLTIP),
    00629       NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_AIC_CLOSE), SetFill(1, 0), SetMinimalSize(93, 12), SetDataTip(STR_AI_SETTINGS_CLOSE, STR_NULL),
    00630       EndContainer(),
    00631     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(7, 0, 7),
    00632       NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_AIC_TEXTFILE + TFT_README), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_TEXTFILE_VIEW_README, STR_NULL),
    00633       NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_AIC_TEXTFILE + TFT_CHANGELOG), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_TEXTFILE_VIEW_CHANGELOG, STR_NULL),
    00634       NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_AIC_TEXTFILE + TFT_LICENSE), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_TEXTFILE_VIEW_LICENCE, STR_NULL),
    00635     EndContainer(),
    00636     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),
    00637   EndContainer(),
    00638 };
    00639
    00641 static const WindowDesc _ai_config_desc(
    00642   WDP_CENTER, 0, 0,
    00643   WC_GAME_OPTIONS, WC_NONE,
    00644   WDF_UNCLICK_BUTTONS,
    00645   _nested_ai_config_widgets, lengthof(_nested_ai_config_widgets)
    00646 );
    00647
    00651 struct AIConfigWindow : public Window {
    00652   CompanyID selected_slot;
    00653   int line_height;
    00654   Scrollbar *vscroll;
    00655
    00656   AIConfigWindow() : Window()
    00657   {
    00658     this->InitNested(&_ai_config_desc, WN_GAME_OPTIONS_AI); // Initializes 'this->line_height' as a side effect.
    00659     this->vscroll = this->GetScrollbar(WID_AIC_SCROLLBAR);
    00660     this->selected_slot = INVALID_COMPANY;
    00661     NWidgetCore *nwi = this->GetWidget<NWidgetCore>(WID_AIC_LIST);
    00662     this->vscroll->SetCapacity(nwi->current_y / this->line_height);
    00663     this->vscroll->SetCount(MAX_COMPANIES);
    00664     nwi->widget_data = (this->vscroll->GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
    00665     this->OnInvalidateData(0);
    00666   }
    00667
    00668   ~AIConfigWindow()
    00669   {
    00670     DeleteWindowByClass(WC_AI_LIST);
    00671     DeleteWindowByClass(WC_AI_SETTINGS);
    00672   }
    00673
    00674   virtual void SetStringParameters(int widget) const
    00675   {
    00676     switch (widget) {
    00677       case WID_AIC_NUMBER:
    00678         SetDParam(0, GetGameSettings().difficulty.max_no_competitors);
    00679         break;
    00680       case WID_AIC_CHANGE:
    00681         switch (selected_slot) {
    00682           case OWNER_DEITY:
    00683             SetDParam(0, STR_AI_CONFIG_CHANGE_GAMESCRIPT);
    00684             break;
    00685
    00686           case INVALID_COMPANY:
    00687             SetDParam(0, STR_AI_CONFIG_CHANGE_NONE);
    00688             break;
    00689
    00690           default:
    00691             SetDParam(0, STR_AI_CONFIG_CHANGE_AI);
    00692             break;
    00693         }
    00694         break;
    00695     }
    00696   }
    00697
    00698   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    00699   {
    00700     switch (widget) {
    00701       case WID_AIC_GAMELIST:
    00702       case WID_AIC_LIST:
    00703         this->line_height = FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM;
    00704         size->height = GB(this->GetWidget<NWidgetCore>(widget)->widget_data, MAT_ROW_START, MAT_ROW_BITS) * this->line_height;
    00705         break;
    00706     }
    00707   }
    00708
    00714   static bool IsEditable(CompanyID slot)
    00715   {
    00716     if (slot == OWNER_DEITY) return _game_mode != GM_NORMAL;
    00717
    00718     if (_game_mode != GM_NORMAL) {
    00719       return slot > 0 && slot <= GetGameSettings().difficulty.max_no_competitors;
    00720     }
    00721     if (Company::IsValidID(slot) || slot < 0) return false;
    00722
    00723     int max_slot = GetGameSettings().difficulty.max_no_competitors;
    00724     for (CompanyID cid = COMPANY_FIRST; cid < (CompanyID)max_slot && cid < MAX_COMPANIES; cid++) {
    00725       if (Company::IsValidHumanID(cid)) max_slot++;
    00726     }
    00727     return slot < max_slot;
    00728   }
    00729
    00730   virtual void DrawWidget(const Rect &r, int widget) const
    00731   {
    00732     switch (widget) {
    00733       case WID_AIC_GAMELIST: {
    00734         StringID text = STR_AI_CONFIG_NONE;
    00735
    00736         if (GameConfig::GetConfig()->GetInfo() != NULL) {
    00737           SetDParamStr(0, GameConfig::GetConfig()->GetInfo()->GetName());
    00738           text = STR_JUST_RAW_STRING;
    00739         }
    00740
    00741         DrawString(r.left + 10, r.right - 10, r.top + WD_MATRIX_TOP, text,
    00742             (this->selected_slot == OWNER_DEITY) ? TC_WHITE : (IsEditable(OWNER_DEITY) ? TC_ORANGE : TC_SILVER));
    00743
    00744         break;
    00745       }
    00746
    00747       case WID_AIC_LIST: {
    00748         int y = r.top;
    00749         for (int i = this->vscroll->GetPosition(); this->vscroll->IsVisible(i) && i < MAX_COMPANIES; i++) {
    00750           StringID text;
    00751
    00752           if ((_game_mode != GM_NORMAL && i == 0) || (_game_mode == GM_NORMAL && Company::IsValidHumanID(i))) {
    00753             text = STR_AI_CONFIG_HUMAN_PLAYER;
    00754           } else if (AIConfig::GetConfig((CompanyID)i)->GetInfo() != NULL) {
    00755             SetDParamStr(0, AIConfig::GetConfig((CompanyID)i)->GetInfo()->GetName());
    00756             text = STR_JUST_RAW_STRING;
    00757           } else {
    00758             text = STR_AI_CONFIG_RANDOM_AI;
    00759           }
    00760           DrawString(r.left + 10, r.right - 10, y + WD_MATRIX_TOP, text,
    00761               (this->selected_slot == i) ? TC_WHITE : (IsEditable((CompanyID)i) ? TC_ORANGE : TC_SILVER));
    00762           y += this->line_height;
    00763         }
    00764         break;
    00765       }
    00766     }
    00767   }
    00768
    00769   virtual void OnClick(Point pt, int widget, int click_count)
    00770   {
    00771     if (widget >= WID_AIC_TEXTFILE && widget < WID_AIC_TEXTFILE + TFT_END) {
    00772       if (this->selected_slot == INVALID_COMPANY || GetConfig(this->selected_slot) == NULL) return;
    00773
    00774       ShowScriptTextfileWindow((TextfileType)(widget - WID_AIC_TEXTFILE), this->selected_slot);
    00775       return;
    00776     }
    00777
    00778     switch (widget) {
    00779       case WID_AIC_DECREASE:
    00780       case WID_AIC_INCREASE: {
    00781         int new_value;
    00782         if (widget == WID_AIC_DECREASE) {
    00783           new_value = max(0, GetGameSettings().difficulty.max_no_competitors - 1);
    00784         } else {
    00785           new_value = min(MAX_COMPANIES - 1, GetGameSettings().difficulty.max_no_competitors + 1);
    00786         }
    00787         IConsoleSetSetting("difficulty.max_no_competitors", new_value);
    00788         this->InvalidateData();
    00789         break;
    00790       }
    00791
    00792       case WID_AIC_GAMELIST: {
    00793         this->selected_slot = OWNER_DEITY;
    00794         this->InvalidateData();
    00795         if (click_count > 1 && this->selected_slot != INVALID_COMPANY) ShowAIListWindow((CompanyID)this->selected_slot);
    00796         break;
    00797       }
    00798
    00799       case WID_AIC_LIST: { // Select a slot
    00800         this->selected_slot = (CompanyID)this->vscroll->GetScrolledRowFromWidget(pt.y, this, widget, 0, this->line_height);
    00801         this->InvalidateData();
    00802         if (click_count > 1 && this->selected_slot != INVALID_COMPANY) ShowAIListWindow((CompanyID)this->selected_slot);
    00803         break;
    00804       }
    00805
    00806       case WID_AIC_MOVE_UP:
    00807         if (IsEditable(this->selected_slot) && IsEditable((CompanyID)(this->selected_slot - 1))) {
    00808           Swap(GetGameSettings().ai_config[this->selected_slot], GetGameSettings().ai_config[this->selected_slot - 1]);
    00809           this->selected_slot--;
    00810           this->vscroll->ScrollTowards(this->selected_slot);
    00811           this->InvalidateData();
    00812         }
    00813         break;
    00814
    00815       case WID_AIC_MOVE_DOWN:
    00816         if (IsEditable(this->selected_slot) && IsEditable((CompanyID)(this->selected_slot + 1))) {
    00817           Swap(GetGameSettings().ai_config[this->selected_slot], GetGameSettings().ai_config[this->selected_slot + 1]);
    00818           this->selected_slot++;
    00819           this->vscroll->ScrollTowards(this->selected_slot);
    00820           this->InvalidateData();
    00821         }
    00822         break;
    00823
    00824       case WID_AIC_CHANGE:  // choose other AI
    00825         ShowAIListWindow((CompanyID)this->selected_slot);
    00826         break;
    00827
    00828       case WID_AIC_CONFIGURE: // change the settings for an AI
    00829         ShowAISettingsWindow((CompanyID)this->selected_slot);
    00830         break;
    00831
    00832       case WID_AIC_CLOSE:
    00833         delete this;
    00834         break;
    00835
    00836       case WID_AIC_CONTENT_DOWNLOAD:
    00837         if (!_network_available) {
    00838           ShowErrorMessage(STR_NETWORK_ERROR_NOTAVAILABLE, INVALID_STRING_ID, WL_ERROR);
    00839         } else {
    00840 #if defined(ENABLE_NETWORK)
    00841           ShowNetworkContentListWindow(NULL, CONTENT_TYPE_AI);
    00842           _network_content_client.RequestContentList(CONTENT_TYPE_GAME);
    00843 #endif
    00844         }
    00845         break;
    00846     }
    00847   }
    00848
    00854   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
    00855   {
    00856     if (!IsEditable(this->selected_slot)) {
    00857       this->selected_slot = INVALID_COMPANY;
    00858     }
    00859
    00860     if (!gui_scope) return;
    00861
    00862     this->SetWidgetDisabledState(WID_AIC_DECREASE, GetGameSettings().difficulty.max_no_competitors == 0);
    00863     this->SetWidgetDisabledState(WID_AIC_INCREASE, GetGameSettings().difficulty.max_no_competitors == MAX_COMPANIES - 1);
    00864     this->SetWidgetDisabledState(WID_AIC_CHANGE, this->selected_slot == INVALID_COMPANY);
    00865     this->SetWidgetDisabledState(WID_AIC_CONFIGURE, this->selected_slot == INVALID_COMPANY || GetConfig(this->selected_slot)->GetConfigList()->size() == 0);
    00866     this->SetWidgetDisabledState(WID_AIC_MOVE_UP, this->selected_slot == OWNER_DEITY || this->selected_slot == INVALID_COMPANY || !IsEditable((CompanyID)(this->selected_slot - 1)));
    00867     this->SetWidgetDisabledState(WID_AIC_MOVE_DOWN, this->selected_slot == OWNER_DEITY || this->selected_slot == INVALID_COMPANY || !IsEditable((CompanyID)(this->selected_slot + 1)));
    00868
    00869     for (TextfileType tft = TFT_BEGIN; tft < TFT_END; tft++) {
    00870       this->SetWidgetDisabledState(WID_AIC_TEXTFILE + tft, this->selected_slot == INVALID_COMPANY || (GetConfig(this->selected_slot)->GetTextfile(tft, this->selected_slot) == NULL));
    00871     }
    00872   }
    00873 };
    00874
    00876 void ShowAIConfigWindow()
    00877 {
    00878   DeleteWindowByClass(WC_GAME_OPTIONS);
    00879   new AIConfigWindow();
    00880 }
    00881
    00885 struct AIDebugWindow : public QueryStringBaseWindow {
    00886   static const int top_offset;
    00887   static const int bottom_offset;
    00888
    00889   static const unsigned int MAX_BREAK_STR_STRING_LENGTH = 256;
    00890
    00891   static CompanyID ai_debug_company;
    00892   int redraw_timer;
    00893   int last_vscroll_pos;
    00894   bool autoscroll;
    00895   bool show_break_box;
    00896   static bool break_check_enabled;
    00897   static char break_string[MAX_BREAK_STR_STRING_LENGTH];
    00898   static bool case_sensitive_break_check;
    00899   int highlight_row;
    00900   Scrollbar *vscroll;
    00901
    00902   ScriptLog::LogData *GetLogPointer() const
    00903   {
    00904     if (ai_debug_company == OWNER_DEITY) return (ScriptLog::LogData *)Game::GetInstance()->GetLogPointer();
    00905     return (ScriptLog::LogData *)Company::Get(ai_debug_company)->ai_instance->GetLogPointer();
    00906   }
    00907
    00913   AIDebugWindow(const WindowDesc *desc, WindowNumber number) : QueryStringBaseWindow(MAX_BREAK_STR_STRING_LENGTH)
    00914   {
    00915     this->CreateNestedTree(desc);
    00916     this->vscroll = this->GetScrollbar(WID_AID_SCROLLBAR);
    00917     this->show_break_box = _settings_client.gui.ai_developer_tools;
    00918     this->GetWidget<NWidgetStacked>(WID_AID_BREAK_STRING_WIDGETS)->SetDisplayedPlane(this->show_break_box ? 0 : SZSP_HORIZONTAL);
    00919     this->FinishInitNested(desc, number);
    00920
    00921     if (!this->show_break_box) break_check_enabled = false;
    00922     /* Disable the companies who are not active or not an AI */
    00923     for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
    00924       this->SetWidgetDisabledState(i + WID_AID_COMPANY_BUTTON_START, !Company::IsValidAiID(i));
    00925     }
    00926     this->EnableWidget(WID_AID_SCRIPT_GAME);
    00927     this->DisableWidget(WID_AID_RELOAD_TOGGLE);
    00928     this->DisableWidget(WID_AID_SETTINGS);
    00929     this->DisableWidget(WID_AID_CONTINUE_BTN);
    00930
    00931     this->last_vscroll_pos = 0;
    00932     this->autoscroll = true;
    00933     this->highlight_row = -1;
    00934     InitializeTextBuffer(&this->text, this->edit_str_buf, this->edit_str_size, MAX_BREAK_STR_STRING_LENGTH);
    00935
    00936     /* Restore the break string value from static variable */
    00937     strecpy(this->edit_str_buf, this->break_string, this->edit_str_buf + MAX_BREAK_STR_STRING_LENGTH);
    00938     UpdateTextBufferSize(&this->text);
    00939
    00940     /* Restore button state from static class variables */
    00941     if (ai_debug_company == OWNER_DEITY) {
    00942       this->LowerWidget(WID_AID_SCRIPT_GAME);
    00943     } else if (ai_debug_company != INVALID_COMPANY) {
    00944       this->LowerWidget(ai_debug_company + WID_AID_COMPANY_BUTTON_START);
    00945     }
    00946     this->SetWidgetLoweredState(WID_AID_BREAK_STR_ON_OFF_BTN, this->break_check_enabled);
    00947     this->SetWidgetLoweredState(WID_AID_MATCH_CASE_BTN, this->case_sensitive_break_check);
    00948
    00949   }
    00950
    00951   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    00952   {
    00953     if (widget == WID_AID_LOG_PANEL) {
    00954       resize->height = FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
    00955       size->height = 14 * resize->height + this->top_offset + this->bottom_offset;
    00956     }
    00957   }
    00958
    00959   virtual void OnPaint()
    00960   {
    00961     /* Check if the currently selected company is still active. */
    00962     if (ai_debug_company == INVALID_COMPANY || (ai_debug_company != OWNER_DEITY && !Company::IsValidAiID(ai_debug_company))) {
    00963       if (ai_debug_company != INVALID_COMPANY) {
    00964         /* Raise the widget for the previous selection. */
    00965         this->RaiseWidget(ai_debug_company + WID_AID_COMPANY_BUTTON_START);
    00966
    00967         ai_debug_company = INVALID_COMPANY;
    00968       }
    00969
    00970       const Company *c;
    00971       FOR_ALL_COMPANIES(c) {
    00972         if (c->is_ai) {
    00973           /* Lower the widget corresponding to this company. */
    00974           this->LowerWidget(c->index + WID_AID_COMPANY_BUTTON_START);
    00975
    00976           ai_debug_company = c->index;
    00977           break;
    00978         }
    00979       }
    00980     }
    00981
    00982     /* Update "Reload AI" and "AI settings" buttons */
    00983     this->SetWidgetDisabledState(WID_AID_SETTINGS, ai_debug_company == INVALID_COMPANY);
    00984     this->SetWidgetDisabledState(WID_AID_RELOAD_TOGGLE, ai_debug_company == INVALID_COMPANY || ai_debug_company == OWNER_DEITY);
    00985     this->SetWidgetDisabledState(WID_AID_SCRIPT_GAME, Game::GetGameInstance() == NULL);
    00986
    00987     /* Draw standard stuff */
    00988     this->DrawWidgets();
    00989
    00990     if (this->IsShaded()) return; // Don't draw anything when the window is shaded.
    00991
    00992     if (this->show_break_box) this->DrawEditBox(WID_AID_BREAK_STR_EDIT_BOX);
    00993
    00994     /* Paint the company icons */
    00995     for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
    00996       NWidgetCore *button = this->GetWidget<NWidgetCore>(i + WID_AID_COMPANY_BUTTON_START);
    00997       bool dirty = false;
    00998
    00999       bool valid = Company::IsValidAiID(i);
    01000       bool disabled = !valid;
    01001       if (button->IsDisabled() != disabled) {
    01002         /* Invalid/non-AI companies have button disabled */
    01003         button->SetDisabled(disabled);
    01004         dirty = true;
    01005       }
    01006
    01007       bool dead = valid && Company::Get(i)->ai_instance->IsDead();
    01008       Colours colour = dead ? COLOUR_RED : COLOUR_GREY;
    01009       if (button->colour != colour) {
    01010         /* Mark dead AIs by red background */
    01011         button->colour = colour;
    01012         dirty = true;
    01013       }
    01014
    01015       /* Do we need a repaint? */
    01016       if (dirty) this->SetDirty();
    01017       /* Draw company icon only for valid AI companies */
    01018       if (!valid) continue;
    01019
    01020       byte offset = (i == ai_debug_company) ? 1 : 0;
    01021       DrawCompanyIcon(i, button->pos_x + button->current_x / 2 - 7 + offset, this->GetWidget<NWidgetBase>(WID_AID_COMPANY_BUTTON_START + i)->pos_y + 2 + offset);
    01022     }
    01023
    01024     /* If there are no active companies, don't display anything else. */
    01025     if (ai_debug_company == INVALID_COMPANY) return;
    01026
    01027     ScriptLog::LogData *log = this->GetLogPointer();
    01028
    01029     int scroll_count = (log == NULL) ? 0 : log->used;
    01030     if (this->vscroll->GetCount() != scroll_count) {
    01031       this->vscroll->SetCount(scroll_count);
    01032
    01033       /* We need a repaint */
    01034       this->SetWidgetDirty(WID_AID_SCROLLBAR);
    01035     }
    01036
    01037     if (log == NULL) return;
    01038
    01039     /* Detect when the user scrolls the window. Enable autoscroll when the
    01040      * bottom-most line becomes visible. */
    01041     if (this->last_vscroll_pos != this->vscroll->GetPosition()) {
    01042       this->autoscroll = this->vscroll->GetPosition() >= log->used - this->vscroll->GetCapacity();
    01043     }
    01044     if (this->autoscroll) {
    01045       int scroll_pos = max(0, log->used - this->vscroll->GetCapacity());
    01046       if (scroll_pos != this->vscroll->GetPosition()) {
    01047         this->vscroll->SetPosition(scroll_pos);
    01048
    01049         /* We need a repaint */
    01050         this->SetWidgetDirty(WID_AID_SCROLLBAR);
    01051         this->SetWidgetDirty(WID_AID_LOG_PANEL);
    01052       }
    01053     }
    01054     this->last_vscroll_pos = this->vscroll->GetPosition();
    01055   }
    01056
    01057   virtual void SetStringParameters(int widget) const
    01058   {
    01059     switch (widget) {
    01060       case WID_AID_NAME_TEXT:
    01061         if (ai_debug_company == OWNER_DEITY) {
    01062           const GameInfo *info = Game::GetInfo();
    01063           assert(info != NULL);
    01064           SetDParam(0, STR_AI_DEBUG_NAME_AND_VERSION);
    01065           SetDParamStr(1, info->GetName());
    01066           SetDParam(2, info->GetVersion());
    01067         } else if (ai_debug_company == INVALID_COMPANY || !Company::IsValidAiID(ai_debug_company)) {
    01068           SetDParam(0, STR_EMPTY);
    01069         } else {
    01070           const AIInfo *info = Company::Get(ai_debug_company)->ai_info;
    01071           assert(info != NULL);
    01072           SetDParam(0, STR_AI_DEBUG_NAME_AND_VERSION);
    01073           SetDParamStr(1, info->GetName());
    01074           SetDParam(2, info->GetVersion());
    01075         }
    01076         break;
    01077     }
    01078   }
    01079
    01080   virtual void DrawWidget(const Rect &r, int widget) const
    01081   {
    01082     if (ai_debug_company == INVALID_COMPANY) return;
    01083
    01084     switch (widget) {
    01085       case WID_AID_LOG_PANEL: {
    01086         ScriptLog::LogData *log = this->GetLogPointer();
    01087         if (log == NULL) return;
    01088
    01089         int y = this->top_offset;
    01090         for (int i = this->vscroll->GetPosition(); this->vscroll->IsVisible(i) && i < log->used; i++) {
    01091           int pos = (i + log->pos + 1 - log->used + log->count) % log->count;
    01092           if (log->lines[pos] == NULL) break;
    01093
    01094           TextColour colour;
    01095           switch (log->type[pos]) {
    01096             case ScriptLog::LOG_SQ_INFO:  colour = TC_BLACK;  break;
    01097             case ScriptLog::LOG_SQ_ERROR: colour = TC_RED;    break;
    01098             case ScriptLog::LOG_INFO:     colour = TC_BLACK;  break;
    01099             case ScriptLog::LOG_WARNING:  colour = TC_YELLOW; break;
    01100             case ScriptLog::LOG_ERROR:    colour = TC_RED;    break;
    01101             default:                  colour = TC_BLACK;  break;
    01102           }
    01103
    01104           /* Check if the current line should be highlighted */
    01105           if (pos == this->highlight_row) {
    01106             GfxFillRect(r.left + 1, r.top + y, r.right - 1, r.top + y + this->resize.step_height - WD_PAR_VSEP_NORMAL, PC_BLACK);
    01107             if (colour == TC_BLACK) colour = TC_WHITE; // Make black text readable by inverting it to white.
    01108           }
    01109
    01110           DrawString(r.left + 7, r.right - 7, r.top + y, log->lines[pos], colour, SA_LEFT | SA_FORCE);
    01111           y += this->resize.step_height;
    01112         }
    01113         break;
    01114       }
    01115     }
    01116   }
    01117
    01122   void ChangeToAI(CompanyID show_ai)
    01123   {
    01124     if (ai_debug_company == OWNER_DEITY) {
    01125       this->RaiseWidget(WID_AID_SCRIPT_GAME);
    01126     } else {
    01127       this->RaiseWidget(ai_debug_company + WID_AID_COMPANY_BUTTON_START);
    01128     }
    01129     ai_debug_company = show_ai;
    01130
    01131     ScriptLog::LogData *log = this->GetLogPointer();
    01132     this->vscroll->SetCount((log == NULL) ? 0 : log->used);
    01133
    01134     if (ai_debug_company == OWNER_DEITY) {
    01135       this->LowerWidget(WID_AID_SCRIPT_GAME);
    01136     } else {
    01137       this->LowerWidget(ai_debug_company + WID_AID_COMPANY_BUTTON_START);
    01138     }
    01139
    01140     this->autoscroll = true;
    01141     this->last_vscroll_pos = this->vscroll->GetPosition();
    01142     this->SetDirty();
    01143     /* Close AI settings window to prevent confusion */
    01144     DeleteWindowByClass(WC_AI_SETTINGS);
    01145   }
    01146
    01147   virtual void OnClick(Point pt, int widget, int click_count)
    01148   {
    01149     /* Check which button is clicked */
    01150     if (IsInsideMM(widget, WID_AID_COMPANY_BUTTON_START, WID_AID_COMPANY_BUTTON_END + 1)) {
    01151       /* Is it no on disable? */
    01152       if (!this->IsWidgetDisabled(widget)) {
    01153         ChangeToAI((CompanyID)(widget - WID_AID_COMPANY_BUTTON_START));
    01154       }
    01155     }
    01156
    01157     switch (widget) {
    01158       case WID_AID_SCRIPT_GAME:
    01159         ChangeToAI(OWNER_DEITY);
    01160         break;
    01161
    01162       case WID_AID_RELOAD_TOGGLE:
    01163         if (ai_debug_company == OWNER_DEITY) break;
    01164         /* First kill the company of the AI, then start a new one. This should start the current AI again */
    01165         DoCommandP(0, 2 | ai_debug_company << 16, CRR_MANUAL, CMD_COMPANY_CTRL);
    01166         DoCommandP(0, 1 | ai_debug_company << 16, 0, CMD_COMPANY_CTRL);
    01167         break;
    01168
    01169       case WID_AID_SETTINGS:
    01170         ShowAISettingsWindow(ai_debug_company);
    01171         break;
    01172
    01173       case WID_AID_BREAK_STR_ON_OFF_BTN:
    01174         this->break_check_enabled = !this->break_check_enabled;
    01175         this->SetWidgetLoweredState(WID_AID_BREAK_STR_ON_OFF_BTN, this->break_check_enabled);
    01176         this->SetWidgetDirty(WID_AID_BREAK_STR_ON_OFF_BTN);
    01177         break;
    01178
    01179       case WID_AID_MATCH_CASE_BTN:
    01180         this->case_sensitive_break_check = !this->case_sensitive_break_check;
    01181         this->SetWidgetLoweredState(WID_AID_MATCH_CASE_BTN, this->case_sensitive_break_check);
    01182         break;
    01183
    01184       case WID_AID_CONTINUE_BTN:
    01185         /* Unpause */
    01186         DoCommandP(0, PM_PAUSED_NORMAL, 0, CMD_PAUSE);
    01187         this->DisableWidget(WID_AID_CONTINUE_BTN);
    01188         this->RaiseWidget(WID_AID_CONTINUE_BTN); // Disabled widgets don't raise themself
    01189         break;
    01190     }
    01191   }
    01192
    01193   virtual void OnTimeout()
    01194   {
    01195     this->RaiseWidget(WID_AID_RELOAD_TOGGLE);
    01196     this->RaiseWidget(WID_AID_SETTINGS);
    01197     this->SetDirty();
    01198   }
    01199
    01200   virtual void OnMouseLoop()
    01201   {
    01202     this->HandleEditBox(WID_AID_BREAK_STR_EDIT_BOX);
    01203   }
    01204
    01205   virtual EventState OnKeyPress(uint16 key, uint16 keycode)
    01206   {
    01207     EventState state = ES_NOT_HANDLED;
    01208     if (this->HandleEditBoxKey(WID_AID_BREAK_STR_EDIT_BOX, key, keycode, state) != HEBR_NOT_FOCUSED) {
    01209       /* Save the current string to static member so it can be restored next time the window is opened */
    01210       strecpy(this->break_string, this->edit_str_buf, lastof(this->break_string));
    01211     }
    01212     return state;
    01213   }
    01214
    01220   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
    01221   {
    01222     if (data == -1 || ai_debug_company == data) this->SetDirty();
    01223
    01224     if (gui_scope && data == -2) {
    01225       /* The continue button should be disabled when the game is unpaused and
    01226        * it was previously paused by the break string ( = a line in the log
    01227        * was highlighted )*/
    01228       if ((_pause_mode & PM_PAUSED_NORMAL) == PM_UNPAUSED && this->highlight_row != -1) {
    01229         this->DisableWidget(WID_AID_CONTINUE_BTN);
    01230         this->SetWidgetDirty(WID_AID_CONTINUE_BTN);
    01231         this->SetWidgetDirty(WID_AID_LOG_PANEL);
    01232         this->highlight_row = -1;
    01233       }
    01234     }
    01235
    01236     /* If the log message is related to the active company tab, check the break string.
    01237      * This needs to be done in gameloop-scope, so the AI is suspended immediately. */
    01238     if (ai_debug_company != OWNER_DEITY && !gui_scope && data == ai_debug_company && this->break_check_enabled && !StrEmpty(this->edit_str_buf)) {
    01239       /* Get the log instance of the active company */
    01240       ScriptLog::LogData *log = this->GetLogPointer();
    01241
    01242       if (log != NULL && case_sensitive_break_check?
    01243           strstr(log->lines[log->pos], this->edit_str_buf) != 0 :
    01244           strcasestr(log->lines[log->pos], this->edit_str_buf) != 0) {
    01245
    01246         AI::Suspend(ai_debug_company);
    01247         if ((_pause_mode & PM_PAUSED_NORMAL) == PM_UNPAUSED) {
    01248           DoCommandP(0, PM_PAUSED_NORMAL, 1, CMD_PAUSE);
    01249         }
    01250
    01251         /* Make it possible to click on the continue button */
    01252         this->EnableWidget(WID_AID_CONTINUE_BTN);
    01253         this->SetWidgetDirty(WID_AID_CONTINUE_BTN);
    01254
    01255         /* Highlight row that matched */
    01256         this->highlight_row = log->pos;
    01257       }
    01258     }
    01259   }
    01260
    01261   virtual void OnResize()
    01262   {
    01263     this->vscroll->SetCapacityFromWidget(this, WID_AID_LOG_PANEL);
    01264   }
    01265 };
    01266
    01267 const int AIDebugWindow::top_offset = WD_FRAMERECT_TOP + 2;
    01268 const int AIDebugWindow::bottom_offset = WD_FRAMERECT_BOTTOM;
    01269 CompanyID AIDebugWindow::ai_debug_company = INVALID_COMPANY;
    01270 char AIDebugWindow::break_string[MAX_BREAK_STR_STRING_LENGTH] = "";
    01271 bool AIDebugWindow::break_check_enabled = true;
    01272 bool AIDebugWindow::case_sensitive_break_check = false;
    01273
    01275 NWidgetBase *MakeCompanyButtonRowsAIDebug(int *biggest_index)
    01276 {
    01277   return MakeCompanyButtonRows(biggest_index, WID_AID_COMPANY_BUTTON_START, WID_AID_COMPANY_BUTTON_END, 8, STR_AI_DEBUG_SELECT_AI_TOOLTIP);
    01278 }
    01279
    01281 static const NWidgetPart _nested_ai_debug_widgets[] = {
    01282   NWidget(NWID_HORIZONTAL),
    01283     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
    01284     NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_AI_DEBUG, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    01285     NWidget(WWT_SHADEBOX, COLOUR_GREY),
    01286     NWidget(WWT_STICKYBOX, COLOUR_GREY),
    01287   EndContainer(),
    01288   NWidget(WWT_PANEL, COLOUR_GREY, WID_AID_VIEW),
    01289     NWidgetFunction(MakeCompanyButtonRowsAIDebug), SetPadding(0, 2, 1, 2),
    01290   EndContainer(),
    01291   NWidget(NWID_HORIZONTAL),
    01292     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),
    01293     NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_AID_NAME_TEXT), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_JUST_STRING, STR_AI_DEBUG_NAME_TOOLTIP),
    01294     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_AID_SETTINGS), SetMinimalSize(100, 20), SetDataTip(STR_AI_DEBUG_SETTINGS, STR_AI_DEBUG_SETTINGS_TOOLTIP),
    01295     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_AID_RELOAD_TOGGLE), SetMinimalSize(100, 20), SetDataTip(STR_AI_DEBUG_RELOAD, STR_AI_DEBUG_RELOAD_TOOLTIP),
    01296   EndContainer(),
    01297   NWidget(NWID_HORIZONTAL),
    01298     NWidget(NWID_VERTICAL),
    01299       /* Log panel */
    01300       NWidget(WWT_PANEL, COLOUR_GREY, WID_AID_LOG_PANEL), SetMinimalSize(287, 180), SetResize(1, 1), SetScrollbar(WID_AID_SCROLLBAR),
    01301       EndContainer(),
    01302       /* Break string widgets */
    01303       NWidget(NWID_SELECTION, INVALID_COLOUR, WID_AID_BREAK_STRING_WIDGETS),
    01304         NWidget(NWID_HORIZONTAL),
    01305           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),
    01306           NWidget(WWT_PANEL, COLOUR_GREY),
    01307             NWidget(NWID_HORIZONTAL),
    01308               NWidget(WWT_LABEL, COLOUR_GREY), SetPadding(2, 2, 2, 4), SetDataTip(STR_AI_DEBUG_BREAK_ON_LABEL, 0x0),
    01309               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),
    01310             EndContainer(),
    01311           EndContainer(),
    01312           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),
    01313           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),
    01314         EndContainer(),
    01315       EndContainer(),
    01316     EndContainer(),
    01317     NWidget(NWID_VERTICAL),
    01318       NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_AID_SCROLLBAR),
    01319       NWidget(WWT_RESIZEBOX, COLOUR_GREY),
    01320     EndContainer(),
    01321   EndContainer(),
    01322 };
    01323
    01325 static const WindowDesc _ai_debug_desc(
    01326   WDP_AUTO, 600, 450,
    01327   WC_AI_DEBUG, WC_NONE,
    01328   0,
    01329   _nested_ai_debug_widgets, lengthof(_nested_ai_debug_widgets)
    01330 );
    01331
    01336 void ShowAIDebugWindow(CompanyID show_company)
    01337 {
    01338   if (!_networking || _network_server) {
    01339     AIDebugWindow *w = (AIDebugWindow *)BringWindowToFrontById(WC_AI_DEBUG, 0);
    01340     if (w == NULL) w = new AIDebugWindow(&_ai_debug_desc, 0);
    01341     if (show_company != INVALID_COMPANY) w->ChangeToAI(show_company);
    01342   } else {
    01343     ShowErrorMessage(STR_ERROR_AI_DEBUG_SERVER_ONLY, INVALID_STRING_ID, WL_INFO);
    01344   }
    01345 }
    01346
    01350 void InitializeAIGui()
    01351 {
    01352   AIDebugWindow::ai_debug_company = INVALID_COMPANY;
    01353 }
    01354
    01356 void ShowAIDebugWindowIfAIError()
    01357 {
    01358   /* Network clients can't debug AIs. */
    01359   if (_networking && !_network_server) return;
    01360
    01361   Company *c;
    01362   FOR_ALL_COMPANIES(c) {
    01363     if (c->is_ai && c->ai_instance->IsDead()) {
    01364       ShowAIDebugWindow(c->index);
    01365       break;
    01366     }
    01367   }
    01368
    01369   GameInstance *g = Game::GetGameInstance();
    01370   if (g != NULL && g->IsDead()) {
    01371     ShowAIDebugWindow(OWNER_DEITY);
    01372   }
    01373 }