# 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 "../gui.h"
    00015 #include "../querystring_gui.h"
    00016 #include "../company_func.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 "../core/backup_type.hpp"
    00027
    00028 #include "ai.hpp"
    00029 #include "api/ai_log.hpp"
    00030 #include "ai_config.hpp"
    00031 #include "ai_instance.hpp"
    00032
    00033 #include "table/strings.h"
    00034
    00036 enum AIListWindowWidgets {
    00037   AIL_WIDGET_LIST,
    00038   AIL_WIDGET_SCROLLBAR,
    00039   AIL_WIDGET_INFO_BG,
    00040   AIL_WIDGET_ACCEPT,
    00041   AIL_WIDGET_CANCEL,
    00042 };
    00043
    00047 struct AIListWindow : public Window {
    00048   const AIInfoList *ai_info_list;
    00049   int selected;
    00050   CompanyID slot;
    00051   int line_height;
    00052   Scrollbar *vscroll;
    00053
    00059   AIListWindow(const WindowDesc *desc, CompanyID slot) : Window(),
    00060     slot(slot)
    00061   {
    00062     this->ai_info_list = AI::GetUniqueInfoList();
    00063
    00064     this->CreateNestedTree(desc);
    00065     this->vscroll = this->GetScrollbar(AIL_WIDGET_SCROLLBAR);
    00066     this->FinishInitNested(desc); // Initializes 'this->line_height' as side effect.
    00067
    00068     this->vscroll->SetCount((int)this->ai_info_list->size() + 1);
    00069
    00070     /* Try if we can find the currently selected AI */
    00071     this->selected = -1;
    00072     if (AIConfig::GetConfig(slot)->HasAI()) {
    00073       AIInfo *info = AIConfig::GetConfig(slot)->GetInfo();
    00074       int i = 0;
    00075       for (AIInfoList::const_iterator it = this->ai_info_list->begin(); it != this->ai_info_list->end(); it++, i++) {
    00076         if ((*it).second == info) {
    00077           this->selected = i;
    00078           break;
    00079         }
    00080       }
    00081     }
    00082   }
    00083
    00084   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    00085   {
    00086     if (widget == AIL_WIDGET_LIST) {
    00087       this->line_height = FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM;
    00088
    00089       resize->width = 1;
    00090       resize->height = this->line_height;
    00091       size->height = GB(this->GetWidget<NWidgetCore>(widget)->widget_data, MAT_ROW_START, MAT_ROW_BITS) * this->line_height;
    00092     }
    00093   }
    00094
    00095   virtual void DrawWidget(const Rect &r, int widget) const
    00096   {
    00097     switch (widget) {
    00098       case AIL_WIDGET_LIST: {
    00099         /* Draw a list of all available AIs. */
    00100         int y = this->GetWidget<NWidgetBase>(AIL_WIDGET_LIST)->pos_y;
    00101         /* First AI in the list is hardcoded to random */
    00102         if (this->vscroll->IsVisible(0)) {
    00103           DrawString(r.left + WD_MATRIX_LEFT, r.right - WD_MATRIX_LEFT, y + WD_MATRIX_TOP, STR_AI_CONFIG_RANDOM_AI, this->selected == -1 ? TC_WHITE : TC_BLACK);
    00104           y += this->line_height;
    00105         }
    00106         AIInfoList::const_iterator it = this->ai_info_list->begin();
    00107         for (int i = 1; it != this->ai_info_list->end(); i++, it++) {
    00108           if (this->vscroll->IsVisible(i)) {
    00109             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);
    00110             y += this->line_height;
    00111           }
    00112         }
    00113         break;
    00114       }
    00115       case AIL_WIDGET_INFO_BG: {
    00116         AIInfo *selected_info = NULL;
    00117         AIInfoList::const_iterator it = this->ai_info_list->begin();
    00118         for (int i = 1; selected_info == NULL && it != this->ai_info_list->end(); i++, it++) {
    00119           if (this->selected == i - 1) selected_info = (*it).second;
    00120         }
    00121         /* Some info about the currently selected AI. */
    00122         if (selected_info != NULL) {
    00123           int y = r.top + WD_FRAMERECT_TOP;
    00124           SetDParamStr(0, selected_info->GetAuthor());
    00125           DrawString(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, y, STR_AI_LIST_AUTHOR);
    00126           y += FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
    00127           SetDParam(0, selected_info->GetVersion());
    00128           DrawString(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, y, STR_AI_LIST_VERSION);
    00129           y += FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
    00130           if (selected_info->GetURL() != NULL) {
    00131             SetDParamStr(0, selected_info->GetURL());
    00132             DrawString(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, y, STR_AI_LIST_URL);
    00133             y += FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
    00134           }
    00135           SetDParamStr(0, selected_info->GetDescription());
    00136           DrawStringMultiLine(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, y, r.bottom - WD_FRAMERECT_BOTTOM, STR_JUST_RAW_STRING, TC_BLACK);
    00137         }
    00138         break;
    00139       }
    00140     }
    00141   }
    00142
    00146   void ChangeAI()
    00147   {
    00148     if (this->selected == -1) {
    00149       AIConfig::GetConfig(slot)->ChangeAI(NULL);
    00150     } else {
    00151       AIInfoList::const_iterator it = this->ai_info_list->begin();
    00152       for (int i = 0; i < this->selected; i++) it++;
    00153       AIConfig::GetConfig(slot)->ChangeAI((*it).second->GetName(), (*it).second->GetVersion());
    00154     }
    00155     SetWindowDirty(WC_GAME_OPTIONS, 0);
    00156   }
    00157
    00158   virtual void OnClick(Point pt, int widget, int click_count)
    00159   {
    00160     switch (widget) {
    00161       case AIL_WIDGET_LIST: { // Select one of the AIs
    00162         int sel = this->vscroll->GetScrolledRowFromWidget(pt.y, this, AIL_WIDGET_LIST, 0, this->line_height) - 1;
    00163         if (sel < (int)this->ai_info_list->size()) {
    00164           this->selected = sel;
    00165           this->SetDirty();
    00166           if (click_count > 1) {
    00167             this->ChangeAI();
    00168             delete this;
    00169           }
    00170         }
    00171         break;
    00172       }
    00173
    00174       case AIL_WIDGET_ACCEPT: {
    00175         this->ChangeAI();
    00176         delete this;
    00177         break;
    00178       }
    00179
    00180       case AIL_WIDGET_CANCEL:
    00181         delete this;
    00182         break;
    00183     }
    00184   }
    00185
    00186   virtual void OnResize()
    00187   {
    00188     NWidgetCore *nwi = this->GetWidget<NWidgetCore>(AIL_WIDGET_LIST);
    00189     this->vscroll->SetCapacity(nwi->current_y / this->line_height);
    00190     nwi->widget_data = (this->vscroll->GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
    00191   }
    00192
    00198   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
    00199   {
    00200     if (_game_mode == GM_NORMAL && Company::IsValidID(this->slot)) {
    00201       delete this;
    00202       return;
    00203     }
    00204
    00205     if (!gui_scope) return;
    00206
    00207     this->vscroll->SetCount((int)this->ai_info_list->size() + 1);
    00208
    00209     /* selected goes from -1 .. length of ai list - 1. */
    00210     this->selected = min(this->selected, this->vscroll->GetCount() - 2);
    00211   }
    00212 };
    00213
    00215 static const NWidgetPart _nested_ai_list_widgets[] = {
    00216   NWidget(NWID_HORIZONTAL),
    00217     NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
    00218     NWidget(WWT_CAPTION, COLOUR_MAUVE), SetDataTip(STR_AI_LIST_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    00219   EndContainer(),
    00220   NWidget(NWID_HORIZONTAL),
    00221     NWidget(WWT_MATRIX, COLOUR_MAUVE, AIL_WIDGET_LIST), SetMinimalSize(188, 112), SetFill(1, 1), SetResize(1, 1), SetDataTip(0x501, STR_AI_LIST_TOOLTIP), SetScrollbar(AIL_WIDGET_SCROLLBAR),
    00222     NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, AIL_WIDGET_SCROLLBAR),
    00223   EndContainer(),
    00224   NWidget(WWT_PANEL, COLOUR_MAUVE, AIL_WIDGET_INFO_BG), SetMinimalTextLines(8, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM), SetResize(1, 0),
    00225   EndContainer(),
    00226   NWidget(NWID_HORIZONTAL),
    00227     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
    00228       NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, AIL_WIDGET_ACCEPT), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_AI_LIST_ACCEPT, STR_AI_LIST_ACCEPT_TOOLTIP),
    00229       NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, AIL_WIDGET_CANCEL), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_AI_LIST_CANCEL, STR_AI_LIST_CANCEL_TOOLTIP),
    00230     EndContainer(),
    00231     NWidget(WWT_RESIZEBOX, COLOUR_MAUVE),
    00232   EndContainer(),
    00233 };
    00234
    00236 static const WindowDesc _ai_list_desc(
    00237   WDP_CENTER, 200, 234,
    00238   WC_AI_LIST, WC_NONE,
    00239   WDF_UNCLICK_BUTTONS,
    00240   _nested_ai_list_widgets, lengthof(_nested_ai_list_widgets)
    00241 );
    00242
    00247 static void ShowAIListWindow(CompanyID slot)
    00248 {
    00249   DeleteWindowByClass(WC_AI_LIST);
    00250   new AIListWindow(&_ai_list_desc, slot);
    00251 }
    00252
    00254 enum AISettingsWindowWidgest {
    00255   AIS_WIDGET_BACKGROUND,
    00256   AIS_WIDGET_SCROLLBAR,
    00257   AIS_WIDGET_ACCEPT,
    00258   AIS_WIDGET_RESET,
    00259 };
    00260
    00264 struct AISettingsWindow : public Window {
    00265   CompanyID slot;
    00266   AIConfig *ai_config;
    00267   int clicked_button;
    00268   bool clicked_increase;
    00269   int timeout;
    00270   int clicked_row;
    00271   int line_height;
    00272   Scrollbar *vscroll;
    00273
    00279   AISettingsWindow(const WindowDesc *desc, CompanyID slot) : Window(),
    00280     slot(slot),
    00281     clicked_button(-1),
    00282     timeout(0)
    00283   {
    00284     this->ai_config = AIConfig::GetConfig(slot);
    00285
    00286     this->CreateNestedTree(desc);
    00287     this->vscroll = this->GetScrollbar(AIS_WIDGET_SCROLLBAR);
    00288     this->FinishInitNested(desc, slot);  // Initializes 'this->line_height' as side effect.
    00289
    00290     this->SetWidgetDisabledState(AIS_WIDGET_RESET, _game_mode != GM_MENU && Company::IsValidID(this->slot));
    00291
    00292     this->vscroll->SetCount((int)this->ai_config->GetConfigList()->size());
    00293   }
    00294
    00295   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    00296   {
    00297     if (widget == AIS_WIDGET_BACKGROUND) {
    00298       this->line_height = FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM;
    00299
    00300       resize->width = 1;
    00301       resize->height = this->line_height;
    00302       size->height = GB(this->GetWidget<NWidgetCore>(widget)->widget_data, MAT_ROW_START, MAT_ROW_BITS) * this->line_height;
    00303     }
    00304   }
    00305
    00306   virtual void DrawWidget(const Rect &r, int widget) const
    00307   {
    00308     if (widget != AIS_WIDGET_BACKGROUND) return;
    00309
    00310     AIConfig *config = this->ai_config;
    00311     AIConfigItemList::const_iterator it = config->GetConfigList()->begin();
    00312     int i = 0;
    00313     for (; !this->vscroll->IsVisible(i); i++) it++;
    00314
    00315     bool rtl = _current_text_dir == TD_RTL;
    00316     uint buttons_left = rtl ? r.right - 23 : r.left + 4;
    00317     uint text_left    = r.left + (rtl ? WD_FRAMERECT_LEFT : 28);
    00318     uint text_right   = r.right - (rtl ? 28 : WD_FRAMERECT_RIGHT);
    00319
    00320
    00321     int y = r.top;
    00322     for (; this->vscroll->IsVisible(i) && it != config->GetConfigList()->end(); i++, it++) {
    00323       int current_value = config->GetSetting((*it).name);
    00324       bool editable = _game_mode == GM_MENU || !Company::IsValidID(this->slot) || (it->flags & AICONFIG_INGAME) != 0;
    00325
    00326       StringID str;
    00327       TextColour colour;
    00328       uint idx = 0;
    00329       if (StrEmpty((*it).description)) {
    00330         str = STR_JUST_STRING;
    00331         colour = TC_ORANGE;
    00332       } else {
    00333         str = STR_AI_SETTINGS_SETTING;
    00334         colour = TC_LIGHT_BLUE;
    00335         SetDParamStr(idx++, (*it).description);
    00336       }
    00337
    00338       if (((*it).flags & AICONFIG_BOOLEAN) != 0) {
    00339         DrawFrameRect(buttons_left, y  + 2, buttons_left + 19, y + 10, (current_value != 0) ? COLOUR_GREEN : COLOUR_RED, (current_value != 0) ? FR_LOWERED : FR_NONE);
    00340         SetDParam(idx++, current_value == 0 ? STR_CONFIG_SETTING_OFF : STR_CONFIG_SETTING_ON);
    00341       } else {
    00342         DrawArrowButtons(buttons_left, y + 2, COLOUR_YELLOW, (this->clicked_button == i) ? 1 + (this->clicked_increase != rtl) : 0, editable && current_value > (*it).min_value, editable && current_value < (*it).max_value);
    00343         if (it->labels != NULL && it->labels->Contains(current_value)) {
    00344           SetDParam(idx++, STR_JUST_RAW_STRING);
    00345           SetDParamStr(idx++, it->labels->Find(current_value)->second);
    00346         } else {
    00347           SetDParam(idx++, STR_JUST_INT);
    00348           SetDParam(idx++, current_value);
    00349         }
    00350       }
    00351
    00352       DrawString(text_left, text_right, y + WD_MATRIX_TOP, str, colour);
    00353       y += this->line_height;
    00354     }
    00355   }
    00356
    00360   void CheckDifficultyLevel()
    00361   {
    00362     if (_game_mode == GM_MENU) {
    00363       if (_settings_newgame.difficulty.diff_level != 3) {
    00364         _settings_newgame.difficulty.diff_level = 3;
    00365         ShowErrorMessage(STR_WARNING_DIFFICULTY_TO_CUSTOM, INVALID_STRING_ID, WL_WARNING);
    00366       }
    00367     } else if (_settings_game.difficulty.diff_level != 3) {
    00368       IConsoleSetSetting("difficulty.diff_level", 3);
    00369     }
    00370   }
    00371
    00372   virtual void OnClick(Point pt, int widget, int click_count)
    00373   {
    00374     switch (widget) {
    00375       case AIS_WIDGET_BACKGROUND: {
    00376         const NWidgetBase *wid = this->GetWidget<NWidgetBase>(AIS_WIDGET_BACKGROUND);
    00377         int num = (pt.y - wid->pos_y) / this->line_height + this->vscroll->GetPosition();
    00378         if (num >= (int)this->ai_config->GetConfigList()->size()) break;
    00379
    00380         AIConfigItemList::const_iterator it = this->ai_config->GetConfigList()->begin();
    00381         for (int i = 0; i < num; i++) it++;
    00382         AIConfigItem config_item = *it;
    00383         if (_game_mode == GM_NORMAL && Company::IsValidID(this->slot) && (config_item.flags & AICONFIG_INGAME) == 0) return;
    00384
    00385         bool bool_item = (config_item.flags & AICONFIG_BOOLEAN) != 0;
    00386
    00387         int x = pt.x - wid->pos_x;
    00388         if (_current_text_dir == TD_RTL) x = wid->current_x - x;
    00389         x -= 4;
    00390         /* One of the arrows is clicked (or green/red rect in case of bool value) */
    00391         if (IsInsideMM(x, 0, 21)) {
    00392           int new_val = this->ai_config->GetSetting(config_item.name);
    00393           int old_val = new_val;
    00394           if (bool_item) {
    00395             new_val = !new_val;
    00396           } else if (x >= 10) {
    00397             /* Increase button clicked */
    00398             new_val += config_item.step_size;
    00399             if (new_val > config_item.max_value) new_val = config_item.max_value;
    00400             this->clicked_increase = true;
    00401           } else {
    00402             /* Decrease button clicked */
    00403             new_val -= config_item.step_size;
    00404             if (new_val < config_item.min_value) new_val = config_item.min_value;
    00405             this->clicked_increase = false;
    00406           }
    00407
    00408           if (new_val != old_val) {
    00409             this->ai_config->SetSetting(config_item.name, new_val);
    00410             this->clicked_button = num;
    00411             this->timeout = 5;
    00412
    00413             this->CheckDifficultyLevel();
    00414           }
    00415         } else if (!bool_item) {
    00416           /* Display a query box so users can enter a custom value. */
    00417           this->clicked_row = num;
    00418           SetDParam(0, this->ai_config->GetSetting(config_item.name));
    00419           ShowQueryString(STR_JUST_INT, STR_CONFIG_SETTING_QUERY_CAPTION, 10, this, CS_NUMERAL, QSF_NONE);
    00420         }
    00421         this->SetDirty();
    00422         break;
    00423       }
    00424
    00425       case AIS_WIDGET_ACCEPT:
    00426         delete this;
    00427         break;
    00428
    00429       case AIS_WIDGET_RESET:
    00430         if (_game_mode == GM_MENU || !Company::IsValidID(this->slot)) {
    00431           this->ai_config->ResetSettings();
    00432           this->SetDirty();
    00433         }
    00434         break;
    00435     }
    00436   }
    00437
    00438   virtual void OnQueryTextFinished(char *str)
    00439   {
    00440     if (StrEmpty(str)) return;
    00441     AIConfigItemList::const_iterator it = this->ai_config->GetConfigList()->begin();
    00442     for (int i = 0; i < this->clicked_row; i++) it++;
    00443     if (_game_mode == GM_NORMAL && Company::IsValidID(this->slot) && (it->flags & AICONFIG_INGAME) == 0) return;
    00444     int32 value = atoi(str);
    00445     this->ai_config->SetSetting((*it).name, value);
    00446     this->CheckDifficultyLevel();
    00447     this->SetDirty();
    00448   }
    00449
    00450   virtual void OnResize()
    00451   {
    00452     NWidgetCore *nwi = this->GetWidget<NWidgetCore>(AIS_WIDGET_BACKGROUND);
    00453     this->vscroll->SetCapacity(nwi->current_y / this->line_height);
    00454     nwi->widget_data = (this->vscroll->GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
    00455   }
    00456
    00457   virtual void OnTick()
    00458   {
    00459     if (--this->timeout == 0) {
    00460       this->clicked_button = -1;
    00461       this->SetDirty();
    00462     }
    00463   }
    00464
    00470   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
    00471   {
    00472     if (_game_mode == GM_NORMAL && Company::IsValidID(this->slot)) delete this;
    00473   }
    00474 };
    00475
    00477 static const NWidgetPart _nested_ai_settings_widgets[] = {
    00478   NWidget(NWID_HORIZONTAL),
    00479     NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
    00480     NWidget(WWT_CAPTION, COLOUR_MAUVE), SetDataTip(STR_AI_SETTINGS_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    00481   EndContainer(),
    00482   NWidget(NWID_HORIZONTAL),
    00483     NWidget(WWT_MATRIX, COLOUR_MAUVE, AIS_WIDGET_BACKGROUND), SetMinimalSize(188, 182), SetResize(1, 1), SetFill(1, 0), SetDataTip(0x501, STR_NULL), SetScrollbar(AIS_WIDGET_SCROLLBAR),
    00484     NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, AIS_WIDGET_SCROLLBAR),
    00485   EndContainer(),
    00486   NWidget(NWID_HORIZONTAL),
    00487     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
    00488       NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, AIS_WIDGET_ACCEPT), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_AI_SETTINGS_CLOSE, STR_NULL),
    00489       NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, AIS_WIDGET_RESET), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_AI_SETTINGS_RESET, STR_NULL),
    00490     EndContainer(),
    00491     NWidget(WWT_RESIZEBOX, COLOUR_MAUVE),
    00492   EndContainer(),
    00493 };
    00494
    00496 static const WindowDesc _ai_settings_desc(
    00497   WDP_CENTER, 500, 208,
    00498   WC_AI_SETTINGS, WC_NONE,
    00499   WDF_UNCLICK_BUTTONS,
    00500   _nested_ai_settings_widgets, lengthof(_nested_ai_settings_widgets)
    00501 );
    00502
    00507 static void ShowAISettingsWindow(CompanyID slot)
    00508 {
    00509   DeleteWindowByClass(WC_AI_LIST);
    00510   DeleteWindowByClass(WC_AI_SETTINGS);
    00511   new AISettingsWindow(&_ai_settings_desc, slot);
    00512 }
    00513
    00515 enum AIConfigWindowWidgets {
    00516   AIC_WIDGET_BACKGROUND,
    00517   AIC_WIDGET_DECREASE,
    00518   AIC_WIDGET_INCREASE,
    00519   AIC_WIDGET_NUMBER,
    00520   AIC_WIDGET_LIST,
    00521   AIC_WIDGET_SCROLLBAR,
    00522   AIC_WIDGET_MOVE_UP,
    00523   AIC_WIDGET_MOVE_DOWN,
    00524   AIC_WIDGET_CHANGE,
    00525   AIC_WIDGET_CONFIGURE,
    00526   AIC_WIDGET_CLOSE,
    00527   AIC_WIDGET_CONTENT_DOWNLOAD,
    00528 };
    00529
    00531 static const NWidgetPart _nested_ai_config_widgets[] = {
    00532   NWidget(NWID_HORIZONTAL),
    00533     NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
    00534     NWidget(WWT_CAPTION, COLOUR_MAUVE), SetDataTip(STR_AI_CONFIG_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    00535   EndContainer(),
    00536   NWidget(WWT_PANEL, COLOUR_MAUVE, AIC_WIDGET_BACKGROUND),
    00537     NWidget(NWID_VERTICAL), SetPIP(4, 4, 4),
    00538       NWidget(NWID_HORIZONTAL), SetPIP(10, 0, 10),
    00539         NWidget(WWT_PUSHARROWBTN, COLOUR_YELLOW, AIC_WIDGET_DECREASE), SetFill(0, 1), SetDataTip(AWV_DECREASE, STR_NULL),
    00540         NWidget(WWT_PUSHARROWBTN, COLOUR_YELLOW, AIC_WIDGET_INCREASE), SetFill(0, 1), SetDataTip(AWV_INCREASE, STR_NULL),
    00541         NWidget(NWID_SPACER), SetMinimalSize(6, 0),
    00542         NWidget(WWT_TEXT, COLOUR_MAUVE, AIC_WIDGET_NUMBER), SetDataTip(STR_DIFFICULTY_LEVEL_SETTING_MAXIMUM_NO_COMPETITORS, STR_NULL), SetFill(1, 0), SetPadding(1, 0, 0, 0),
    00543       EndContainer(),
    00544       NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(10, 0, 10),
    00545         NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, AIC_WIDGET_MOVE_UP), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_AI_CONFIG_MOVE_UP, STR_AI_CONFIG_MOVE_UP_TOOLTIP),
    00546         NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, AIC_WIDGET_MOVE_DOWN), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_AI_CONFIG_MOVE_DOWN, STR_AI_CONFIG_MOVE_DOWN_TOOLTIP),
    00547       EndContainer(),
    00548     EndContainer(),
    00549     NWidget(NWID_HORIZONTAL),
    00550       NWidget(WWT_MATRIX, COLOUR_MAUVE, AIC_WIDGET_LIST), SetMinimalSize(288, 112), SetFill(1, 0), SetDataTip(0x801, STR_AI_CONFIG_LIST_TOOLTIP), SetScrollbar(AIC_WIDGET_SCROLLBAR),
    00551       NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, AIC_WIDGET_SCROLLBAR),
    00552     EndContainer(),
    00553     NWidget(NWID_SPACER), SetMinimalSize(0, 9),
    00554     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(5, 0, 5),
    00555       NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, AIC_WIDGET_CHANGE), SetFill(1, 0), SetMinimalSize(93, 12), SetDataTip(STR_AI_CONFIG_CHANGE, STR_AI_CONFIG_CHANGE_TOOLTIP),
    00556       NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, AIC_WIDGET_CONFIGURE), SetFill(1, 0), SetMinimalSize(93, 12), SetDataTip(STR_AI_CONFIG_CONFIGURE, STR_AI_CONFIG_CONFIGURE_TOOLTIP),
    00557       NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, AIC_WIDGET_CLOSE), SetFill(1, 0), SetMinimalSize(93, 12), SetDataTip(STR_AI_SETTINGS_CLOSE, STR_NULL),
    00558     EndContainer(),
    00559     NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, AIC_WIDGET_CONTENT_DOWNLOAD), SetFill(1, 0), SetMinimalSize(279, 12), SetPadding(0, 5, 9, 5), SetDataTip(STR_INTRO_ONLINE_CONTENT, STR_INTRO_TOOLTIP_ONLINE_CONTENT),
    00560   EndContainer(),
    00561 };
    00562
    00564 static const WindowDesc _ai_config_desc(
    00565   WDP_CENTER, 0, 0,
    00566   WC_GAME_OPTIONS, WC_NONE,
    00567   WDF_UNCLICK_BUTTONS,
    00568   _nested_ai_config_widgets, lengthof(_nested_ai_config_widgets)
    00569 );
    00570
    00574 struct AIConfigWindow : public Window {
    00575   CompanyID selected_slot;
    00576   int line_height;
    00577   Scrollbar *vscroll;
    00578
    00579   AIConfigWindow() : Window()
    00580   {
    00581     this->InitNested(&_ai_config_desc); // Initializes 'this->line_height' as a side effect.
    00582     this->vscroll = this->GetScrollbar(AIC_WIDGET_SCROLLBAR);
    00583     this->selected_slot = INVALID_COMPANY;
    00584     NWidgetCore *nwi = this->GetWidget<NWidgetCore>(AIC_WIDGET_LIST);
    00585     this->vscroll->SetCapacity(nwi->current_y / this->line_height);
    00586     this->vscroll->SetCount(MAX_COMPANIES);
    00587     nwi->widget_data = (this->vscroll->GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
    00588     this->OnInvalidateData(0);
    00589   }
    00590
    00591   ~AIConfigWindow()
    00592   {
    00593     DeleteWindowByClass(WC_AI_LIST);
    00594     DeleteWindowByClass(WC_AI_SETTINGS);
    00595   }
    00596
    00597   virtual void SetStringParameters(int widget) const
    00598   {
    00599     switch (widget) {
    00600       case AIC_WIDGET_NUMBER:
    00601         SetDParam(0, GetGameSettings().difficulty.max_no_competitors);
    00602         break;
    00603     }
    00604   }
    00605
    00606   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    00607   {
    00608     switch (widget) {
    00609       case AIC_WIDGET_LIST:
    00610         this->line_height = FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM;
    00611         size->height = GB(this->GetWidget<NWidgetCore>(widget)->widget_data, MAT_ROW_START, MAT_ROW_BITS) * this->line_height;
    00612         break;
    00613     }
    00614   }
    00615
    00621   static bool IsEditable(CompanyID slot)
    00622   {
    00623     if (_game_mode != GM_NORMAL) {
    00624       return slot > 0 && slot <= GetGameSettings().difficulty.max_no_competitors;
    00625     }
    00626     if (Company::IsValidID(slot) || slot < 0) return false;
    00627
    00628     int max_slot = GetGameSettings().difficulty.max_no_competitors;
    00629     for (CompanyID cid = COMPANY_FIRST; cid < (CompanyID)max_slot && cid < MAX_COMPANIES; cid++) {
    00630       if (Company::IsValidHumanID(cid)) max_slot++;
    00631     }
    00632     return slot < max_slot;
    00633   }
    00634
    00635   virtual void DrawWidget(const Rect &r, int widget) const
    00636   {
    00637     switch (widget) {
    00638       case AIC_WIDGET_LIST: {
    00639         int y = r.top;
    00640         for (int i = this->vscroll->GetPosition(); this->vscroll->IsVisible(i) && i < MAX_COMPANIES; i++) {
    00641           StringID text;
    00642
    00643           if ((_game_mode != GM_NORMAL && i == 0) || (_game_mode == GM_NORMAL && Company::IsValidHumanID(i))) {
    00644             text = STR_AI_CONFIG_HUMAN_PLAYER;
    00645           } else if (AIConfig::GetConfig((CompanyID)i)->GetInfo() != NULL) {
    00646             SetDParamStr(0, AIConfig::GetConfig((CompanyID)i)->GetInfo()->GetName());
    00647             text = STR_JUST_RAW_STRING;
    00648           } else {
    00649             text = STR_AI_CONFIG_RANDOM_AI;
    00650           }
    00651           DrawString(r.left + 10, r.right - 10, y + WD_MATRIX_TOP, text,
    00652               (this->selected_slot == i) ? TC_WHITE : (IsEditable((CompanyID)i) ? TC_ORANGE : TC_SILVER));
    00653           y += this->line_height;
    00654         }
    00655         break;
    00656       }
    00657     }
    00658   }
    00659
    00660   virtual void OnClick(Point pt, int widget, int click_count)
    00661   {
    00662     switch (widget) {
    00663       case AIC_WIDGET_DECREASE:
    00664       case AIC_WIDGET_INCREASE: {
    00665         int new_value;
    00666         if (widget == AIC_WIDGET_DECREASE) {
    00667           new_value = max(0, GetGameSettings().difficulty.max_no_competitors - 1);
    00668         } else {
    00669           new_value = min(MAX_COMPANIES - 1, GetGameSettings().difficulty.max_no_competitors + 1);
    00670         }
    00671         IConsoleSetSetting("difficulty.max_no_competitors", new_value);
    00672         this->InvalidateData();
    00673         break;
    00674       }
    00675
    00676       case AIC_WIDGET_LIST: { // Select a slot
    00677         this->selected_slot = (CompanyID)this->vscroll->GetScrolledRowFromWidget(pt.y, this, widget, 0, this->line_height);
    00678         this->InvalidateData();
    00679         if (click_count > 1 && this->selected_slot != INVALID_COMPANY) ShowAIListWindow((CompanyID)this->selected_slot);
    00680         break;
    00681       }
    00682
    00683       case AIC_WIDGET_MOVE_UP:
    00684         if (IsEditable(this->selected_slot) && IsEditable((CompanyID)(this->selected_slot - 1))) {
    00685           Swap(GetGameSettings().ai_config[this->selected_slot], GetGameSettings().ai_config[this->selected_slot - 1]);
    00686           this->selected_slot--;
    00687           this->vscroll->ScrollTowards(this->selected_slot);
    00688           this->InvalidateData();
    00689         }
    00690         break;
    00691
    00692       case AIC_WIDGET_MOVE_DOWN:
    00693         if (IsEditable(this->selected_slot) && IsEditable((CompanyID)(this->selected_slot + 1))) {
    00694           Swap(GetGameSettings().ai_config[this->selected_slot], GetGameSettings().ai_config[this->selected_slot + 1]);
    00695           this->selected_slot++;
    00696           this->vscroll->ScrollTowards(this->selected_slot);
    00697           this->InvalidateData();
    00698         }
    00699         break;
    00700
    00701       case AIC_WIDGET_CHANGE:  // choose other AI
    00702         ShowAIListWindow((CompanyID)this->selected_slot);
    00703         break;
    00704
    00705       case AIC_WIDGET_CONFIGURE: // change the settings for an AI
    00706         ShowAISettingsWindow((CompanyID)this->selected_slot);
    00707         break;
    00708
    00709       case AIC_WIDGET_CLOSE:
    00710         delete this;
    00711         break;
    00712
    00713       case AIC_WIDGET_CONTENT_DOWNLOAD:
    00714         if (!_network_available) {
    00715           ShowErrorMessage(STR_NETWORK_ERROR_NOTAVAILABLE, INVALID_STRING_ID, WL_ERROR);
    00716         } else {
    00717 #if defined(ENABLE_NETWORK)
    00718           ShowNetworkContentListWindow(NULL, CONTENT_TYPE_AI);
    00719 #endif
    00720         }
    00721         break;
    00722     }
    00723   }
    00724
    00730   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
    00731   {
    00732     if (!IsEditable(this->selected_slot)) {
    00733       this->selected_slot = INVALID_COMPANY;
    00734     }
    00735
    00736     if (!gui_scope) return;
    00737
    00738     this->SetWidgetDisabledState(AIC_WIDGET_DECREASE, GetGameSettings().difficulty.max_no_competitors == 0);
    00739     this->SetWidgetDisabledState(AIC_WIDGET_INCREASE, GetGameSettings().difficulty.max_no_competitors == MAX_COMPANIES - 1);
    00740     this->SetWidgetDisabledState(AIC_WIDGET_CHANGE, this->selected_slot == INVALID_COMPANY);
    00741     this->SetWidgetDisabledState(AIC_WIDGET_CONFIGURE, this->selected_slot == INVALID_COMPANY);
    00742     this->SetWidgetDisabledState(AIC_WIDGET_MOVE_UP, this->selected_slot == INVALID_COMPANY || !IsEditable((CompanyID)(this->selected_slot - 1)));
    00743     this->SetWidgetDisabledState(AIC_WIDGET_MOVE_DOWN, this->selected_slot == INVALID_COMPANY || !IsEditable((CompanyID)(this->selected_slot + 1)));
    00744   }
    00745 };
    00746
    00748 void ShowAIConfigWindow()
    00749 {
    00750   DeleteWindowById(WC_GAME_OPTIONS, 0);
    00751   new AIConfigWindow();
    00752 }
    00753
    00755 enum AIDebugWindowWidgets {
    00756   AID_WIDGET_VIEW,
    00757   AID_WIDGET_NAME_TEXT,
    00758   AID_WIDGET_SETTINGS,
    00759   AID_WIDGET_RELOAD_TOGGLE,
    00760   AID_WIDGET_LOG_PANEL,
    00761   AID_WIDGET_SCROLLBAR,
    00762   AID_WIDGET_COMPANY_BUTTON_START,
    00763   AID_WIDGET_COMPANY_BUTTON_END = AID_WIDGET_COMPANY_BUTTON_START + MAX_COMPANIES - 1,
    00764   AID_BREAK_STRING_WIDGETS,
    00765   AID_WIDGET_BREAK_STR_ON_OFF_BTN,
    00766   AID_WIDGET_BREAK_STR_EDIT_BOX,
    00767   AID_WIDGET_MATCH_CASE_BTN,
    00768   AID_WIDGET_CONTINUE_BTN,
    00769 };
    00770
    00774 struct AIDebugWindow : public QueryStringBaseWindow {
    00775   static const int top_offset;
    00776   static const int bottom_offset;
    00777
    00778   static const unsigned int MAX_BREAK_STR_STRING_LENGTH = 256;
    00779
    00780   static CompanyID ai_debug_company;
    00781   int redraw_timer;
    00782   int last_vscroll_pos;
    00783   bool autoscroll;
    00784   bool show_break_box;
    00785   static bool break_check_enabled;
    00786   static char break_string[MAX_BREAK_STR_STRING_LENGTH];
    00787   static bool case_sensitive_break_check;
    00788   int highlight_row;
    00789   Scrollbar *vscroll;
    00790
    00796   AIDebugWindow(const WindowDesc *desc, WindowNumber number) : QueryStringBaseWindow(MAX_BREAK_STR_STRING_LENGTH)
    00797   {
    00798     this->CreateNestedTree(desc);
    00799     this->vscroll = this->GetScrollbar(AID_WIDGET_SCROLLBAR);
    00800     this->show_break_box = _settings_client.gui.ai_developer_tools;
    00801     this->GetWidget<NWidgetStacked>(AID_BREAK_STRING_WIDGETS)->SetDisplayedPlane(this->show_break_box ? 0 : SZSP_HORIZONTAL);
    00802     this->FinishInitNested(desc, number);
    00803
    00804     if (!this->show_break_box) break_check_enabled = false;
    00805     /* Disable the companies who are not active or not an AI */
    00806     for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
    00807       this->SetWidgetDisabledState(i + AID_WIDGET_COMPANY_BUTTON_START, !Company::IsValidAiID(i));
    00808     }
    00809     this->DisableWidget(AID_WIDGET_RELOAD_TOGGLE);
    00810     this->DisableWidget(AID_WIDGET_SETTINGS);
    00811     this->DisableWidget(AID_WIDGET_CONTINUE_BTN);
    00812
    00813     this->last_vscroll_pos = 0;
    00814     this->autoscroll = true;
    00815     this->highlight_row = -1;
    00816     InitializeTextBuffer(&this->text, this->edit_str_buf, this->edit_str_size, MAX_BREAK_STR_STRING_LENGTH);
    00817
    00818     /* Restore the break string value from static variable */
    00819     strecpy(this->edit_str_buf, this->break_string, this->edit_str_buf + MAX_BREAK_STR_STRING_LENGTH);
    00820     UpdateTextBufferSize(&this->text);
    00821
    00822     /* Restore button state from static class variables */
    00823     if (ai_debug_company != INVALID_COMPANY) this->LowerWidget(ai_debug_company + AID_WIDGET_COMPANY_BUTTON_START);
    00824     this->SetWidgetLoweredState(AID_WIDGET_BREAK_STR_ON_OFF_BTN, this->break_check_enabled);
    00825     this->SetWidgetLoweredState(AID_WIDGET_MATCH_CASE_BTN, this->case_sensitive_break_check);
    00826
    00827   }
    00828
    00829   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    00830   {
    00831     if (widget == AID_WIDGET_LOG_PANEL) {
    00832       resize->height = FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
    00833       size->height = 14 * resize->height + this->top_offset + this->bottom_offset;
    00834     }
    00835   }
    00836
    00837   virtual void OnPaint()
    00838   {
    00839     /* Check if the currently selected company is still active. */
    00840     if (ai_debug_company == INVALID_COMPANY || !Company::IsValidAiID(ai_debug_company)) {
    00841       if (ai_debug_company != INVALID_COMPANY) {
    00842         /* Raise the widget for the previous selection. */
    00843         this->RaiseWidget(ai_debug_company + AID_WIDGET_COMPANY_BUTTON_START);
    00844
    00845         ai_debug_company = INVALID_COMPANY;
    00846       }
    00847
    00848       const Company *c;
    00849       FOR_ALL_COMPANIES(c) {
    00850         if (c->is_ai) {
    00851           /* Lower the widget corresponding to this company. */
    00852           this->LowerWidget(c->index + AID_WIDGET_COMPANY_BUTTON_START);
    00853
    00854           ai_debug_company = c->index;
    00855           break;
    00856         }
    00857       }
    00858     }
    00859
    00860     /* Update "Reload AI" and "AI settings" buttons */
    00861     this->SetWidgetsDisabledState(ai_debug_company == INVALID_COMPANY,
    00862       AID_WIDGET_RELOAD_TOGGLE,
    00863       AID_WIDGET_SETTINGS,
    00864       WIDGET_LIST_END);
    00865
    00866     /* Draw standard stuff */
    00867     this->DrawWidgets();
    00868
    00869     if (this->IsShaded()) return; // Don't draw anything when the window is shaded.
    00870
    00871     if (this->show_break_box) this->DrawEditBox(AID_WIDGET_BREAK_STR_EDIT_BOX);
    00872
    00873     /* Paint the company icons */
    00874     for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
    00875       NWidgetCore *button = this->GetWidget<NWidgetCore>(i + AID_WIDGET_COMPANY_BUTTON_START);
    00876       bool dirty = false;
    00877
    00878       bool valid = Company::IsValidAiID(i);
    00879       bool disabled = !valid;
    00880       if (button->IsDisabled() != disabled) {
    00881         /* Invalid/non-AI companies have button disabled */
    00882         button->SetDisabled(disabled);
    00883         dirty = true;
    00884       }
    00885
    00886       bool dead = valid && Company::Get(i)->ai_instance->IsDead();
    00887       Colours colour = dead ? COLOUR_RED : COLOUR_GREY;
    00888       if (button->colour != colour) {
    00889         /* Mark dead AIs by red background */
    00890         button->colour = colour;
    00891         dirty = true;
    00892       }
    00893
    00894       /* Do we need a repaint? */
    00895       if (dirty) this->SetDirty();
    00896       /* Draw company icon only for valid AI companies */
    00897       if (!valid) continue;
    00898
    00899       byte offset = (i == ai_debug_company) ? 1 : 0;
    00900       DrawCompanyIcon(i, button->pos_x + button->current_x / 2 - 7 + offset, this->GetWidget<NWidgetBase>(AID_WIDGET_COMPANY_BUTTON_START + i)->pos_y + 2 + offset);
    00901     }
    00902
    00903     /* If there are no active companies, don't display anything else. */
    00904     if (ai_debug_company == INVALID_COMPANY) return;
    00905
    00906     Backup<CompanyByte> cur_company(_current_company, ai_debug_company, FILE_LINE);
    00907     AILog::LogData *log = (AILog::LogData *)AIObject::GetLogPointer();
    00908     cur_company.Restore();
    00909
    00910     int scroll_count = (log == NULL) ? 0 : log->used;
    00911     if (this->vscroll->GetCount() != scroll_count) {
    00912       this->vscroll->SetCount(scroll_count);
    00913
    00914       /* We need a repaint */
    00915       this->SetWidgetDirty(AID_WIDGET_SCROLLBAR);
    00916     }
    00917
    00918     if (log == NULL) return;
    00919
    00920     /* Detect when the user scrolls the window. Enable autoscroll when the
    00921      * bottom-most line becomes visible. */
    00922     if (this->last_vscroll_pos != this->vscroll->GetPosition()) {
    00923       this->autoscroll = this->vscroll->GetPosition() >= log->used - this->vscroll->GetCapacity();
    00924     }
    00925     if (this->autoscroll) {
    00926       int scroll_pos = max(0, log->used - this->vscroll->GetCapacity());
    00927       if (scroll_pos != this->vscroll->GetPosition()) {
    00928         this->vscroll->SetPosition(scroll_pos);
    00929
    00930         /* We need a repaint */
    00931         this->SetWidgetDirty(AID_WIDGET_SCROLLBAR);
    00932         this->SetWidgetDirty(AID_WIDGET_LOG_PANEL);
    00933       }
    00934     }
    00935     this->last_vscroll_pos = this->vscroll->GetPosition();
    00936   }
    00937
    00938   virtual void SetStringParameters(int widget) const
    00939   {
    00940     switch (widget) {
    00941       case AID_WIDGET_NAME_TEXT:
    00942         if (ai_debug_company == INVALID_COMPANY || !Company::IsValidAiID(ai_debug_company)) {
    00943           SetDParam(0, STR_EMPTY);
    00944         } else {
    00945           const AIInfo *info = Company::Get(ai_debug_company)->ai_info;
    00946           assert(info != NULL);
    00947           SetDParam(0, STR_AI_DEBUG_NAME_AND_VERSION);
    00948           SetDParamStr(1, info->GetName());
    00949           SetDParam(2, info->GetVersion());
    00950         }
    00951         break;
    00952     }
    00953   }
    00954
    00955   virtual void DrawWidget(const Rect &r, int widget) const
    00956   {
    00957     if (ai_debug_company == INVALID_COMPANY) return;
    00958
    00959     switch (widget) {
    00960       case AID_WIDGET_LOG_PANEL: {
    00961         Backup<CompanyByte> cur_company(_current_company, ai_debug_company, FILE_LINE);
    00962         AILog::LogData *log = (AILog::LogData *)AIObject::GetLogPointer();
    00963         cur_company.Restore();
    00964         if (log == NULL) return;
    00965
    00966         int y = this->top_offset;
    00967         for (int i = this->vscroll->GetPosition(); this->vscroll->IsVisible(i) && i < log->used; i++) {
    00968           int pos = (i + log->pos + 1 - log->used + log->count) % log->count;
    00969           if (log->lines[pos] == NULL) break;
    00970
    00971           TextColour colour;
    00972           switch (log->type[pos]) {
    00973             case AILog::LOG_SQ_INFO:  colour = TC_BLACK;  break;
    00974             case AILog::LOG_SQ_ERROR: colour = TC_RED;    break;
    00975             case AILog::LOG_INFO:     colour = TC_BLACK;  break;
    00976             case AILog::LOG_WARNING:  colour = TC_YELLOW; break;
    00977             case AILog::LOG_ERROR:    colour = TC_RED;    break;
    00978             default:                  colour = TC_BLACK;  break;
    00979           }
    00980
    00981           /* Check if the current line should be highlighted */
    00982           if (pos == this->highlight_row) {
    00983             GfxFillRect(r.left + 1, r.top + y, r.right - 1, r.top + y + this->resize.step_height - WD_PAR_VSEP_NORMAL, PC_BLACK);
    00984             if (colour == TC_BLACK) colour = TC_WHITE; // Make black text readable by inverting it to white.
    00985           }
    00986
    00987           DrawString(r.left + 7, r.right - 7, r.top + y, log->lines[pos], colour, SA_LEFT | SA_FORCE);
    00988           y += this->resize.step_height;
    00989         }
    00990         break;
    00991       }
    00992     }
    00993   }
    00994
    00999   void ChangeToAI(CompanyID show_ai)
    01000   {
    01001     this->RaiseWidget(ai_debug_company + AID_WIDGET_COMPANY_BUTTON_START);
    01002     ai_debug_company = show_ai;
    01003
    01004     Backup<CompanyByte> cur_company(_current_company, ai_debug_company, FILE_LINE);
    01005     AILog::LogData *log = (AILog::LogData *)AIObject::GetLogPointer();
    01006     cur_company.Restore();
    01007     this->vscroll->SetCount((log == NULL) ? 0 : log->used);
    01008
    01009     this->LowerWidget(ai_debug_company + AID_WIDGET_COMPANY_BUTTON_START);
    01010     this->autoscroll = true;
    01011     this->last_vscroll_pos = this->vscroll->GetPosition();
    01012     this->SetDirty();
    01013     /* Close AI settings window to prevent confusion */
    01014     DeleteWindowByClass(WC_AI_SETTINGS);
    01015   }
    01016
    01017   virtual void OnClick(Point pt, int widget, int click_count)
    01018   {
    01019     /* Check which button is clicked */
    01020     if (IsInsideMM(widget, AID_WIDGET_COMPANY_BUTTON_START, AID_WIDGET_COMPANY_BUTTON_END + 1)) {
    01021       /* Is it no on disable? */
    01022       if (!this->IsWidgetDisabled(widget)) {
    01023         ChangeToAI((CompanyID)(widget - AID_WIDGET_COMPANY_BUTTON_START));
    01024       }
    01025     }
    01026
    01027     switch (widget) {
    01028       case AID_WIDGET_RELOAD_TOGGLE:
    01029         /* First kill the company of the AI, then start a new one. This should start the current AI again */
    01030         DoCommandP(0, 2 | ai_debug_company << 16, 0, CMD_COMPANY_CTRL);
    01031         DoCommandP(0, 1 | ai_debug_company << 16, 0, CMD_COMPANY_CTRL);
    01032         break;
    01033
    01034       case AID_WIDGET_SETTINGS:
    01035         ShowAISettingsWindow(ai_debug_company);
    01036         break;
    01037
    01038       case AID_WIDGET_BREAK_STR_ON_OFF_BTN:
    01039         this->break_check_enabled = !this->break_check_enabled;
    01040         this->SetWidgetLoweredState(AID_WIDGET_BREAK_STR_ON_OFF_BTN, this->break_check_enabled);
    01041         this->SetWidgetDirty(AID_WIDGET_BREAK_STR_ON_OFF_BTN);
    01042         break;
    01043
    01044       case AID_WIDGET_MATCH_CASE_BTN:
    01045         this->case_sensitive_break_check = !this->case_sensitive_break_check;
    01046         this->SetWidgetLoweredState(AID_WIDGET_MATCH_CASE_BTN, this->case_sensitive_break_check);
    01047         break;
    01048
    01049       case AID_WIDGET_CONTINUE_BTN:
    01050         /* Unpause */
    01051         DoCommandP(0, PM_PAUSED_NORMAL, 0, CMD_PAUSE);
    01052         this->DisableWidget(AID_WIDGET_CONTINUE_BTN);
    01053         this->RaiseWidget(AID_WIDGET_CONTINUE_BTN); // Disabled widgets don't raise themself
    01054         break;
    01055     }
    01056   }
    01057
    01058   virtual void OnTimeout()
    01059   {
    01060     this->RaiseWidget(AID_WIDGET_RELOAD_TOGGLE);
    01061     this->RaiseWidget(AID_WIDGET_SETTINGS);
    01062     this->SetDirty();
    01063   }
    01064
    01065   virtual void OnMouseLoop()
    01066   {
    01067     this->HandleEditBox(AID_WIDGET_BREAK_STR_EDIT_BOX);
    01068   }
    01069
    01070   virtual EventState OnKeyPress(uint16 key, uint16 keycode)
    01071   {
    01072     EventState state = ES_NOT_HANDLED;
    01073     if (this->HandleEditBoxKey(AID_WIDGET_BREAK_STR_EDIT_BOX, key, keycode, state) != HEBR_NOT_FOCUSED) {
    01074       /* Save the current string to static member so it can be restored next time the window is opened */
    01075       strecpy(this->break_string, this->edit_str_buf, lastof(this->break_string));
    01076     }
    01077     return state;
    01078   }
    01079
    01085   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
    01086   {
    01087     if (data == -1 || ai_debug_company == data) this->SetDirty();
    01088
    01089     if (gui_scope && data == -2) {
    01090       /* The continue button should be disabled when the game is unpaused and
    01091        * it was previously paused by the break string ( = a line in the log
    01092        * was highlighted )*/
    01093       if ((_pause_mode & PM_PAUSED_NORMAL) == PM_UNPAUSED && this->highlight_row != -1) {
    01094         this->DisableWidget(AID_WIDGET_CONTINUE_BTN);
    01095         this->SetWidgetDirty(AID_WIDGET_CONTINUE_BTN);
    01096         this->SetWidgetDirty(AID_WIDGET_LOG_PANEL);
    01097         this->highlight_row = -1;
    01098       }
    01099     }
    01100
    01101     /* If the log message is related to the active company tab, check the break string.
    01102      * This needs to be done in gameloop-scope, so the AI is suspended immediately. */
    01103     if (!gui_scope && data == ai_debug_company && this->break_check_enabled && !StrEmpty(this->edit_str_buf)) {
    01104       /* Get the log instance of the active company */
    01105       Backup<CompanyByte> cur_company(_current_company, ai_debug_company, FILE_LINE);
    01106       AILog::LogData *log = (AILog::LogData *)AIObject::GetLogPointer();
    01107
    01108       if (log != NULL && case_sensitive_break_check?
    01109           strstr(log->lines[log->pos], this->edit_str_buf) != 0 :
    01110           strcasestr(log->lines[log->pos], this->edit_str_buf) != 0) {
    01111
    01112         AI::Suspend(ai_debug_company);
    01113         if ((_pause_mode & PM_PAUSED_NORMAL) == PM_UNPAUSED) {
    01114           DoCommandP(0, PM_PAUSED_NORMAL, 1, CMD_PAUSE);
    01115         }
    01116
    01117         /* Make it possible to click on the continue button */
    01118         this->EnableWidget(AID_WIDGET_CONTINUE_BTN);
    01119         this->SetWidgetDirty(AID_WIDGET_CONTINUE_BTN);
    01120
    01121         /* Highlight row that matched */
    01122         this->highlight_row = log->pos;
    01123       }
    01124
    01125       cur_company.Restore();
    01126     }
    01127   }
    01128
    01129   virtual void OnResize()
    01130   {
    01131     this->vscroll->SetCapacityFromWidget(this, AID_WIDGET_LOG_PANEL);
    01132   }
    01133 };
    01134
    01135 const int AIDebugWindow::top_offset = WD_FRAMERECT_TOP + 2;
    01136 const int AIDebugWindow::bottom_offset = WD_FRAMERECT_BOTTOM;
    01137 CompanyID AIDebugWindow::ai_debug_company = INVALID_COMPANY;
    01138 char AIDebugWindow::break_string[MAX_BREAK_STR_STRING_LENGTH] = "";
    01139 bool AIDebugWindow::break_check_enabled = true;
    01140 bool AIDebugWindow::case_sensitive_break_check = false;
    01141
    01143 NWidgetBase *MakeCompanyButtonRowsAIDebug(int *biggest_index)
    01144 {
    01145   return MakeCompanyButtonRows(biggest_index, AID_WIDGET_COMPANY_BUTTON_START, AID_WIDGET_COMPANY_BUTTON_END, 8, STR_AI_DEBUG_SELECT_AI_TOOLTIP);
    01146 }
    01147
    01149 static const NWidgetPart _nested_ai_debug_widgets[] = {
    01150   NWidget(NWID_HORIZONTAL),
    01151     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
    01152     NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_AI_DEBUG, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    01153     NWidget(WWT_SHADEBOX, COLOUR_GREY),
    01154     NWidget(WWT_STICKYBOX, COLOUR_GREY),
    01155   EndContainer(),
    01156   NWidget(WWT_PANEL, COLOUR_GREY, AID_WIDGET_VIEW),
    01157     NWidgetFunction(MakeCompanyButtonRowsAIDebug), SetPadding(0, 2, 1, 2),
    01158   EndContainer(),
    01159   NWidget(NWID_HORIZONTAL),
    01160     NWidget(WWT_TEXTBTN, COLOUR_GREY, AID_WIDGET_NAME_TEXT), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_JUST_STRING, STR_AI_DEBUG_NAME_TOOLTIP),
    01161     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, AID_WIDGET_SETTINGS), SetMinimalSize(100, 20), SetDataTip(STR_AI_DEBUG_SETTINGS, STR_AI_DEBUG_SETTINGS_TOOLTIP),
    01162     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, AID_WIDGET_RELOAD_TOGGLE), SetMinimalSize(100, 20), SetDataTip(STR_AI_DEBUG_RELOAD, STR_AI_DEBUG_RELOAD_TOOLTIP),
    01163   EndContainer(),
    01164   NWidget(NWID_HORIZONTAL),
    01165     NWidget(NWID_VERTICAL),
    01166       /* Log panel */
    01167       NWidget(WWT_PANEL, COLOUR_GREY, AID_WIDGET_LOG_PANEL), SetMinimalSize(287, 180), SetResize(1, 1), SetScrollbar(AID_WIDGET_SCROLLBAR),
    01168       EndContainer(),
    01169       /* Break string widgets */
    01170       NWidget(NWID_SELECTION, INVALID_COLOUR, AID_BREAK_STRING_WIDGETS),
    01171         NWidget(NWID_HORIZONTAL),
    01172           NWidget(WWT_IMGBTN_2, COLOUR_GREY, AID_WIDGET_BREAK_STR_ON_OFF_BTN), SetFill(0, 1), SetDataTip(SPR_FLAG_VEH_STOPPED, STR_AI_DEBUG_BREAK_STR_ON_OFF_TOOLTIP),
    01173           NWidget(WWT_PANEL, COLOUR_GREY),
    01174             NWidget(NWID_HORIZONTAL),
    01175               NWidget(WWT_LABEL, COLOUR_GREY), SetPadding(2, 2, 2, 4), SetDataTip(STR_AI_DEBUG_BREAK_ON_LABEL, 0x0),
    01176               NWidget(WWT_EDITBOX, COLOUR_WHITE, AID_WIDGET_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),
    01177             EndContainer(),
    01178           EndContainer(),
    01179           NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, AID_WIDGET_MATCH_CASE_BTN), SetMinimalSize(100, 0), SetFill(0, 1), SetDataTip(STR_AI_DEBUG_MATCH_CASE, STR_AI_DEBUG_MATCH_CASE_TOOLTIP),
    01180           NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, AID_WIDGET_CONTINUE_BTN), SetMinimalSize(100, 0), SetFill(0, 1), SetDataTip(STR_AI_DEBUG_CONTINUE, STR_AI_DEBUG_CONTINUE_TOOLTIP),
    01181         EndContainer(),
    01182       EndContainer(),
    01183     EndContainer(),
    01184     NWidget(NWID_VERTICAL),
    01185       NWidget(NWID_VSCROLLBAR, COLOUR_GREY, AID_WIDGET_SCROLLBAR),
    01186       NWidget(WWT_RESIZEBOX, COLOUR_GREY),
    01187     EndContainer(),
    01188   EndContainer(),
    01189 };
    01190
    01192 static const WindowDesc _ai_debug_desc(
    01193   WDP_AUTO, 600, 450,
    01194   WC_AI_DEBUG, WC_NONE,
    01195   0,
    01196   _nested_ai_debug_widgets, lengthof(_nested_ai_debug_widgets)
    01197 );
    01198
    01203 void ShowAIDebugWindow(CompanyID show_company)
    01204 {
    01205   if (!_networking || _network_server) {
    01206     AIDebugWindow *w = (AIDebugWindow *)BringWindowToFrontById(WC_AI_DEBUG, 0);
    01207     if (w == NULL) w = new AIDebugWindow(&_ai_debug_desc, 0);
    01208     if (show_company != INVALID_COMPANY) w->ChangeToAI(show_company);
    01209   } else {
    01210     ShowErrorMessage(STR_ERROR_AI_DEBUG_SERVER_ONLY, INVALID_STRING_ID, WL_INFO);
    01211   }
    01212 }
    01213
    01217 void InitializeAIGui()
    01218 {
    01219   AIDebugWindow::ai_debug_company = INVALID_COMPANY;
    01220 }
    01221
    01223 void ShowAIDebugWindowIfAIError()
    01224 {
    01225   /* Network clients can't debug AIs. */
    01226   if (_networking && !_network_server) return;
    01227
    01228   Company *c;
    01229   FOR_ALL_COMPANIES(c) {
    01230     if (c->is_ai && c->ai_instance->IsDead()) {
    01231       ShowAIDebugWindow(c->index);
    01232       break;
    01233     }
    01234   }
    01235 }