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