00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "../stdafx.h"
00013 #include "../table/sprites.h"
00014 #include "../error.h"
00015 #include "../settings_gui.h"
00016 #include "../querystring_gui.h"
00017 #include "../stringfilter_type.h"
00018 #include "../company_base.h"
00019 #include "../company_gui.h"
00020 #include "../strings_func.h"
00021 #include "../window_func.h"
00022 #include "../gfx_func.h"
00023 #include "../command_func.h"
00024 #include "../network/network.h"
00025 #include "../settings_func.h"
00026 #include "../network/network_content.h"
00027 #include "../textfile_gui.h"
00028 #include "../widgets/dropdown_type.h"
00029 #include "../widgets/dropdown_func.h"
00030 #include "../hotkeys.h"
00031
00032 #include "ai.hpp"
00033 #include "../script/api/script_log.hpp"
00034 #include "ai_config.hpp"
00035 #include "ai_info.hpp"
00036 #include "ai_instance.hpp"
00037 #include "../game/game.hpp"
00038 #include "../game/game_config.hpp"
00039 #include "../game/game_info.hpp"
00040 #include "../game/game_instance.hpp"
00041
00042
00043 #include "table/strings.h"
00044
00045 #include <vector>
00046
00047 static ScriptConfig *GetConfig(CompanyID slot)
00048 {
00049 if (slot == OWNER_DEITY) return GameConfig::GetConfig();
00050 return AIConfig::GetConfig(slot);
00051 }
00052
00056 struct AIListWindow : public Window {
00057 const ScriptInfoList *info_list;
00058 int selected;
00059 CompanyID slot;
00060 int line_height;
00061 Scrollbar *vscroll;
00062
00068 AIListWindow(const WindowDesc *desc, CompanyID slot) : Window(),
00069 slot(slot)
00070 {
00071 if (slot == OWNER_DEITY) {
00072 this->info_list = Game::GetUniqueInfoList();
00073 } else {
00074 this->info_list = AI::GetUniqueInfoList();
00075 }
00076
00077 this->CreateNestedTree(desc);
00078 this->vscroll = this->GetScrollbar(WID_AIL_SCROLLBAR);
00079 this->FinishInitNested(desc);
00080
00081 this->vscroll->SetCount((int)this->info_list->size() + 1);
00082
00083
00084 this->selected = -1;
00085 if (GetConfig(slot)->HasScript()) {
00086 ScriptInfo *info = GetConfig(slot)->GetInfo();
00087 int i = 0;
00088 for (ScriptInfoList::const_iterator it = this->info_list->begin(); it != this->info_list->end(); it++, i++) {
00089 if ((*it).second == info) {
00090 this->selected = i;
00091 break;
00092 }
00093 }
00094 }
00095 }
00096
00097 virtual void SetStringParameters(int widget) const
00098 {
00099 switch (widget) {
00100 case WID_AIL_CAPTION:
00101 SetDParam(0, (this->slot == OWNER_DEITY) ? STR_AI_LIST_CAPTION_GAMESCRIPT : STR_AI_LIST_CAPTION_AI);
00102 break;
00103 }
00104 }
00105
00106 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00107 {
00108 if (widget == WID_AIL_LIST) {
00109 this->line_height = FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM;
00110
00111 resize->width = 1;
00112 resize->height = this->line_height;
00113 size->height = GB(this->GetWidget<NWidgetCore>(widget)->widget_data, MAT_ROW_START, MAT_ROW_BITS) * this->line_height;
00114 }
00115 }
00116
00117 virtual void DrawWidget(const Rect &r, int widget) const
00118 {
00119 switch (widget) {
00120 case WID_AIL_LIST: {
00121
00122 int y = this->GetWidget<NWidgetBase>(WID_AIL_LIST)->pos_y;
00123
00124 if (this->vscroll->IsVisible(0)) {
00125 DrawString(r.left + WD_MATRIX_LEFT, r.right - WD_MATRIX_LEFT, y + WD_MATRIX_TOP, this->slot == OWNER_DEITY ? STR_AI_CONFIG_NONE : STR_AI_CONFIG_RANDOM_AI, this->selected == -1 ? TC_WHITE : TC_ORANGE);
00126 y += this->line_height;
00127 }
00128 ScriptInfoList::const_iterator it = this->info_list->begin();
00129 for (int i = 1; it != this->info_list->end(); i++, it++) {
00130 if (this->vscroll->IsVisible(i)) {
00131 DrawString(r.left + WD_MATRIX_LEFT, r.right - WD_MATRIX_RIGHT, y + WD_MATRIX_TOP, (*it).second->GetName(), (this->selected == i - 1) ? TC_WHITE : TC_ORANGE);
00132 y += this->line_height;
00133 }
00134 }
00135 break;
00136 }
00137 case WID_AIL_INFO_BG: {
00138 AIInfo *selected_info = NULL;
00139 ScriptInfoList::const_iterator it = this->info_list->begin();
00140 for (int i = 1; selected_info == NULL && it != this->info_list->end(); i++, it++) {
00141 if (this->selected == i - 1) selected_info = static_cast<AIInfo *>((*it).second);
00142 }
00143
00144 if (selected_info != NULL) {
00145 int y = r.top + WD_FRAMERECT_TOP;
00146 SetDParamStr(0, selected_info->GetAuthor());
00147 DrawString(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, y, STR_AI_LIST_AUTHOR);
00148 y += FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
00149 SetDParam(0, selected_info->GetVersion());
00150 DrawString(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, y, STR_AI_LIST_VERSION);
00151 y += FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
00152 if (selected_info->GetURL() != NULL) {
00153 SetDParamStr(0, selected_info->GetURL());
00154 DrawString(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, y, STR_AI_LIST_URL);
00155 y += FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
00156 }
00157 SetDParamStr(0, selected_info->GetDescription());
00158 DrawStringMultiLine(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, y, r.bottom - WD_FRAMERECT_BOTTOM, STR_JUST_RAW_STRING, TC_WHITE);
00159 }
00160 break;
00161 }
00162 }
00163 }
00164
00168 void ChangeAI()
00169 {
00170 if (this->selected == -1) {
00171 GetConfig(slot)->Change(NULL);
00172 } else {
00173 ScriptInfoList::const_iterator it = this->info_list->begin();
00174 for (int i = 0; i < this->selected; i++) it++;
00175 GetConfig(slot)->Change((*it).second->GetName(), (*it).second->GetVersion());
00176 }
00177 InvalidateWindowData(WC_GAME_OPTIONS, WN_GAME_OPTIONS_AI);
00178 InvalidateWindowClassesData(WC_AI_SETTINGS);
00179 DeleteWindowByClass(WC_QUERY_STRING);
00180 }
00181
00182 virtual void OnClick(Point pt, int widget, int click_count)
00183 {
00184 switch (widget) {
00185 case WID_AIL_LIST: {
00186 int sel = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_AIL_LIST, 0, this->line_height) - 1;
00187 if (sel < (int)this->info_list->size()) {
00188 this->selected = sel;
00189 this->SetDirty();
00190 if (click_count > 1) {
00191 this->ChangeAI();
00192 delete this;
00193 }
00194 }
00195 break;
00196 }
00197
00198 case WID_AIL_ACCEPT: {
00199 this->ChangeAI();
00200 delete this;
00201 break;
00202 }
00203
00204 case WID_AIL_CANCEL:
00205 delete this;
00206 break;
00207 }
00208 }
00209
00210 virtual void OnResize()
00211 {
00212 NWidgetCore *nwi = this->GetWidget<NWidgetCore>(WID_AIL_LIST);
00213 this->vscroll->SetCapacity(nwi->current_y / this->line_height);
00214 nwi->widget_data = (this->vscroll->GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
00215 }
00216
00222 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
00223 {
00224 if (_game_mode == GM_NORMAL && Company::IsValidID(this->slot)) {
00225 delete this;
00226 return;
00227 }
00228
00229 if (!gui_scope) return;
00230
00231 this->vscroll->SetCount((int)this->info_list->size() + 1);
00232
00233
00234 this->selected = min(this->selected, this->vscroll->GetCount() - 2);
00235 }
00236 };
00237
00239 static const NWidgetPart _nested_ai_list_widgets[] = {
00240 NWidget(NWID_HORIZONTAL),
00241 NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
00242 NWidget(WWT_CAPTION, COLOUR_MAUVE, WID_AIL_CAPTION), SetDataTip(STR_AI_LIST_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00243 EndContainer(),
00244 NWidget(NWID_HORIZONTAL),
00245 NWidget(WWT_MATRIX, COLOUR_MAUVE, WID_AIL_LIST), SetMinimalSize(188, 112), SetFill(1, 1), SetResize(1, 1), SetDataTip(0x501, STR_AI_LIST_TOOLTIP), SetScrollbar(WID_AIL_SCROLLBAR),
00246 NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, WID_AIL_SCROLLBAR),
00247 EndContainer(),
00248 NWidget(WWT_PANEL, COLOUR_MAUVE, WID_AIL_INFO_BG), SetMinimalTextLines(8, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM), SetResize(1, 0),
00249 EndContainer(),
00250 NWidget(NWID_HORIZONTAL),
00251 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
00252 NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, WID_AIL_ACCEPT), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_AI_LIST_ACCEPT, STR_AI_LIST_ACCEPT_TOOLTIP),
00253 NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, WID_AIL_CANCEL), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_AI_LIST_CANCEL, STR_AI_LIST_CANCEL_TOOLTIP),
00254 EndContainer(),
00255 NWidget(WWT_RESIZEBOX, COLOUR_MAUVE),
00256 EndContainer(),
00257 };
00258
00260 static const WindowDesc _ai_list_desc(
00261 WDP_CENTER, 200, 234,
00262 WC_AI_LIST, WC_NONE,
00263 0,
00264 _nested_ai_list_widgets, lengthof(_nested_ai_list_widgets)
00265 );
00266
00271 static void ShowAIListWindow(CompanyID slot)
00272 {
00273 DeleteWindowByClass(WC_AI_LIST);
00274 new AIListWindow(&_ai_list_desc, slot);
00275 }
00276
00280 struct AISettingsWindow : public Window {
00281 CompanyID slot;
00282 ScriptConfig *ai_config;
00283 int clicked_button;
00284 bool clicked_increase;
00285 bool clicked_dropdown;
00286 bool closing_dropdown;
00287 int timeout;
00288 int clicked_row;
00289 int line_height;
00290 Scrollbar *vscroll;
00291 typedef std::vector<const ScriptConfigItem *> VisibleSettingsList;
00292 VisibleSettingsList visible_settings;
00293
00299 AISettingsWindow(const WindowDesc *desc, CompanyID slot) : Window(),
00300 slot(slot),
00301 clicked_button(-1),
00302 clicked_dropdown(false),
00303 closing_dropdown(false),
00304 timeout(0)
00305 {
00306 this->ai_config = GetConfig(slot);
00307 this->RebuildVisibleSettings();
00308
00309 this->CreateNestedTree(desc);
00310 this->vscroll = this->GetScrollbar(WID_AIS_SCROLLBAR);
00311 this->FinishInitNested(desc, slot);
00312
00313 this->SetWidgetDisabledState(WID_AIS_RESET, _game_mode != GM_MENU && Company::IsValidID(this->slot));
00314
00315 this->vscroll->SetCount((int)this->visible_settings.size());
00316 }
00317
00318 virtual void SetStringParameters(int widget) const
00319 {
00320 switch (widget) {
00321 case WID_AIS_CAPTION:
00322 SetDParam(0, (this->slot == OWNER_DEITY) ? STR_AI_SETTINGS_CAPTION_GAMESCRIPT : STR_AI_SETTINGS_CAPTION_AI);
00323 break;
00324 }
00325 }
00326
00332 void RebuildVisibleSettings()
00333 {
00334 visible_settings.clear();
00335
00336 ScriptConfigItemList::const_iterator it = this->ai_config->GetConfigList()->begin();
00337 for (; it != this->ai_config->GetConfigList()->end(); it++) {
00338 bool no_hide = (it->flags & SCRIPTCONFIG_DEVELOPER) == 0;
00339 if (no_hide || _settings_client.gui.ai_developer_tools) {
00340 visible_settings.push_back(&(*it));
00341 }
00342 }
00343 }
00344
00345 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00346 {
00347 if (widget == WID_AIS_BACKGROUND) {
00348 this->line_height = FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM;
00349
00350 resize->width = 1;
00351 resize->height = this->line_height;
00352 size->height = GB(this->GetWidget<NWidgetCore>(widget)->widget_data, MAT_ROW_START, MAT_ROW_BITS) * this->line_height;
00353 }
00354 }
00355
00356 virtual void DrawWidget(const Rect &r, int widget) const
00357 {
00358 if (widget != WID_AIS_BACKGROUND) return;
00359
00360 ScriptConfig *config = this->ai_config;
00361 VisibleSettingsList::const_iterator it = this->visible_settings.begin();
00362 int i = 0;
00363 for (; !this->vscroll->IsVisible(i); i++) it++;
00364
00365 bool rtl = _current_text_dir == TD_RTL;
00366 uint buttons_left = rtl ? r.right - SETTING_BUTTON_WIDTH - 3 : r.left + 4;
00367 uint text_left = r.left + (rtl ? WD_FRAMERECT_LEFT : SETTING_BUTTON_WIDTH + 8);
00368 uint text_right = r.right - (rtl ? SETTING_BUTTON_WIDTH + 8 : WD_FRAMERECT_RIGHT);
00369
00370
00371 int y = r.top;
00372 int button_y_offset = (this->line_height - SETTING_BUTTON_HEIGHT) / 2;
00373 for (; this->vscroll->IsVisible(i) && it != visible_settings.end(); i++, it++) {
00374 const ScriptConfigItem &config_item = **it;
00375 int current_value = config->GetSetting((config_item).name);
00376 bool editable = _game_mode == GM_MENU || ((this->slot != OWNER_DEITY) && !Company::IsValidID(this->slot)) || (config_item.flags & SCRIPTCONFIG_INGAME) != 0;
00377
00378 StringID str;
00379 TextColour colour;
00380 uint idx = 0;
00381 if (StrEmpty(config_item.description)) {
00382 if (!strcmp(config_item.name, "start_date")) {
00383
00384 str = STR_AI_SETTINGS_START_DELAY;
00385 colour = TC_LIGHT_BLUE;
00386 } else {
00387 str = STR_JUST_STRING;
00388 colour = TC_ORANGE;
00389 }
00390 } else {
00391 str = STR_AI_SETTINGS_SETTING;
00392 colour = TC_LIGHT_BLUE;
00393 SetDParamStr(idx++, config_item.description);
00394 }
00395
00396 if ((config_item.flags & SCRIPTCONFIG_BOOLEAN) != 0) {
00397 DrawBoolButton(buttons_left, y + button_y_offset, current_value != 0, editable);
00398 SetDParam(idx++, current_value == 0 ? STR_CONFIG_SETTING_OFF : STR_CONFIG_SETTING_ON);
00399 } else {
00400 if (config_item.complete_labels) {
00401 DrawDropDownButton(buttons_left, y + button_y_offset, COLOUR_YELLOW, this->clicked_row == i && clicked_dropdown, editable);
00402 } else {
00403 DrawArrowButtons(buttons_left, y + button_y_offset, COLOUR_YELLOW, (this->clicked_button == i) ? 1 + (this->clicked_increase != rtl) : 0, editable && current_value > config_item.min_value, editable && current_value < config_item.max_value);
00404 }
00405 if (config_item.labels != NULL && config_item.labels->Contains(current_value)) {
00406 SetDParam(idx++, STR_JUST_RAW_STRING);
00407 SetDParamStr(idx++, config_item.labels->Find(current_value)->second);
00408 } else {
00409 SetDParam(idx++, STR_JUST_INT);
00410 SetDParam(idx++, current_value);
00411 }
00412 }
00413
00414 DrawString(text_left, text_right, y + WD_MATRIX_TOP, str, colour);
00415 y += this->line_height;
00416 }
00417 }
00418
00419 virtual void OnPaint()
00420 {
00421 if (this->closing_dropdown) {
00422 this->closing_dropdown = false;
00423 this->clicked_dropdown = false;
00424 }
00425 this->DrawWidgets();
00426 }
00427
00428 virtual void OnClick(Point pt, int widget, int click_count)
00429 {
00430 switch (widget) {
00431 case WID_AIS_BACKGROUND: {
00432 const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_AIS_BACKGROUND);
00433 int num = (pt.y - wid->pos_y) / this->line_height + this->vscroll->GetPosition();
00434 if (num >= (int)this->visible_settings.size()) break;
00435
00436 VisibleSettingsList::const_iterator it = this->visible_settings.begin();
00437 for (int i = 0; i < num; i++) it++;
00438 const ScriptConfigItem config_item = **it;
00439 if (_game_mode == GM_NORMAL && ((this->slot == OWNER_DEITY) || Company::IsValidID(this->slot)) && (config_item.flags & SCRIPTCONFIG_INGAME) == 0) return;
00440
00441 if (this->clicked_row != num) {
00442 DeleteChildWindows(WC_QUERY_STRING);
00443 HideDropDownMenu(this);
00444 this->clicked_row = num;
00445 this->clicked_dropdown = false;
00446 }
00447
00448 bool bool_item = (config_item.flags & SCRIPTCONFIG_BOOLEAN) != 0;
00449
00450 int x = pt.x - wid->pos_x;
00451 if (_current_text_dir == TD_RTL) x = wid->current_x - 1 - x;
00452 x -= 4;
00453
00454
00455 int old_val = this->ai_config->GetSetting(config_item.name);
00456 if (!bool_item && IsInsideMM(x, 0, SETTING_BUTTON_WIDTH) && config_item.complete_labels) {
00457 if (this->clicked_dropdown) {
00458
00459 HideDropDownMenu(this);
00460 this->clicked_dropdown = false;
00461 this->closing_dropdown = false;
00462 } else {
00463 const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_AIS_BACKGROUND);
00464 int rel_y = (pt.y - (int)wid->pos_y) % this->line_height;
00465
00466 Rect wi_rect;
00467 wi_rect.left = pt.x - (_current_text_dir == TD_RTL ? SETTING_BUTTON_WIDTH - 1 - x : x);
00468 wi_rect.right = wi_rect.left + SETTING_BUTTON_WIDTH - 1;
00469 wi_rect.top = pt.y - rel_y + (this->line_height - SETTING_BUTTON_HEIGHT) / 2;
00470 wi_rect.bottom = wi_rect.top + SETTING_BUTTON_HEIGHT - 1;
00471
00472
00473 if (pt.y >= wi_rect.top && pt.y <= wi_rect.bottom) {
00474 this->clicked_dropdown = true;
00475 this->closing_dropdown = false;
00476
00477 DropDownList *list = new DropDownList();
00478 for (int i = config_item.min_value; i <= config_item.max_value; i++) {
00479 list->push_back(new DropDownListCharStringItem(config_item.labels->Find(i)->second, i, false));
00480 }
00481
00482 ShowDropDownListAt(this, list, old_val, -1, wi_rect, COLOUR_ORANGE, true);
00483 }
00484 }
00485 } else if (IsInsideMM(x, 0, SETTING_BUTTON_WIDTH)) {
00486 int new_val = old_val;
00487 if (bool_item) {
00488 new_val = !new_val;
00489 } else if (x >= SETTING_BUTTON_WIDTH / 2) {
00490
00491 new_val += config_item.step_size;
00492 if (new_val > config_item.max_value) new_val = config_item.max_value;
00493 this->clicked_increase = true;
00494 } else {
00495
00496 new_val -= config_item.step_size;
00497 if (new_val < config_item.min_value) new_val = config_item.min_value;
00498 this->clicked_increase = false;
00499 }
00500
00501 if (new_val != old_val) {
00502 this->ai_config->SetSetting(config_item.name, new_val);
00503 this->clicked_button = num;
00504 this->timeout = 5;
00505 }
00506 } else if (!bool_item && !config_item.complete_labels) {
00507
00508 SetDParam(0, old_val);
00509 ShowQueryString(STR_JUST_INT, STR_CONFIG_SETTING_QUERY_CAPTION, 10, this, CS_NUMERAL, QSF_NONE);
00510 }
00511 this->SetDirty();
00512 break;
00513 }
00514
00515 case WID_AIS_ACCEPT:
00516 delete this;
00517 break;
00518
00519 case WID_AIS_RESET:
00520 if (_game_mode == GM_MENU || !Company::IsValidID(this->slot)) {
00521 this->ai_config->ResetSettings();
00522 this->SetDirty();
00523 }
00524 break;
00525 }
00526 }
00527
00528 virtual void OnQueryTextFinished(char *str)
00529 {
00530 if (StrEmpty(str)) return;
00531 ScriptConfigItemList::const_iterator it = this->ai_config->GetConfigList()->begin();
00532 for (int i = 0; i < this->clicked_row; i++) it++;
00533 if (_game_mode == GM_NORMAL && ((this->slot == OWNER_DEITY) || Company::IsValidID(this->slot)) && (it->flags & SCRIPTCONFIG_INGAME) == 0) return;
00534 int32 value = atoi(str);
00535 this->ai_config->SetSetting((*it).name, value);
00536 this->SetDirty();
00537 }
00538
00539 virtual void OnDropdownSelect(int widget, int index)
00540 {
00541 assert(this->clicked_dropdown);
00542 ScriptConfigItemList::const_iterator it = this->ai_config->GetConfigList()->begin();
00543 for (int i = 0; i < this->clicked_row; i++) it++;
00544 if (_game_mode == GM_NORMAL && ((this->slot == OWNER_DEITY) || Company::IsValidID(this->slot)) && (it->flags & SCRIPTCONFIG_INGAME) == 0) return;
00545 this->ai_config->SetSetting((*it).name, index);
00546 this->SetDirty();
00547 }
00548
00549 virtual void OnDropdownClose(Point pt, int widget, int index, bool instant_close)
00550 {
00551
00552
00553
00554
00555 assert(this->clicked_dropdown);
00556 this->closing_dropdown = true;
00557 this->SetDirty();
00558 }
00559
00560 virtual void OnResize()
00561 {
00562 NWidgetCore *nwi = this->GetWidget<NWidgetCore>(WID_AIS_BACKGROUND);
00563 this->vscroll->SetCapacity(nwi->current_y / this->line_height);
00564 nwi->widget_data = (this->vscroll->GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
00565 }
00566
00567 virtual void OnTick()
00568 {
00569 if (--this->timeout == 0) {
00570 this->clicked_button = -1;
00571 this->SetDirty();
00572 }
00573 }
00574
00580 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
00581 {
00582 this->RebuildVisibleSettings();
00583 }
00584 };
00585
00587 static const NWidgetPart _nested_ai_settings_widgets[] = {
00588 NWidget(NWID_HORIZONTAL),
00589 NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
00590 NWidget(WWT_CAPTION, COLOUR_MAUVE, WID_AIS_CAPTION), SetDataTip(STR_AI_SETTINGS_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00591 EndContainer(),
00592 NWidget(NWID_HORIZONTAL),
00593 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),
00594 NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, WID_AIS_SCROLLBAR),
00595 EndContainer(),
00596 NWidget(NWID_HORIZONTAL),
00597 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
00598 NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, WID_AIS_ACCEPT), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_AI_SETTINGS_CLOSE, STR_NULL),
00599 NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, WID_AIS_RESET), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_AI_SETTINGS_RESET, STR_NULL),
00600 EndContainer(),
00601 NWidget(WWT_RESIZEBOX, COLOUR_MAUVE),
00602 EndContainer(),
00603 };
00604
00606 static const WindowDesc _ai_settings_desc(
00607 WDP_CENTER, 500, 208,
00608 WC_AI_SETTINGS, WC_NONE,
00609 0,
00610 _nested_ai_settings_widgets, lengthof(_nested_ai_settings_widgets)
00611 );
00612
00617 static void ShowAISettingsWindow(CompanyID slot)
00618 {
00619 DeleteWindowByClass(WC_AI_LIST);
00620 DeleteWindowByClass(WC_AI_SETTINGS);
00621 new AISettingsWindow(&_ai_settings_desc, slot);
00622 }
00623
00624
00626 struct ScriptTextfileWindow : public TextfileWindow {
00627 CompanyID slot;
00628
00629 ScriptTextfileWindow(TextfileType file_type, CompanyID slot) : TextfileWindow(file_type), slot(slot)
00630 {
00631 const char *textfile = GetConfig(slot)->GetTextfile(file_type, slot);
00632 this->LoadTextfile(textfile, (slot == OWNER_DEITY) ? GAME_DIR : AI_DIR);
00633 }
00634
00635 void SetStringParameters(int widget) const
00636 {
00637 if (widget == WID_TF_CAPTION) {
00638 SetDParam(0, (slot == OWNER_DEITY) ? STR_CONTENT_TYPE_GAME_SCRIPT : STR_CONTENT_TYPE_AI);
00639 SetDParamStr(1, GetConfig(slot)->GetName());
00640 }
00641 }
00642 };
00643
00649 void ShowScriptTextfileWindow(TextfileType file_type, CompanyID slot)
00650 {
00651 DeleteWindowByClass(WC_TEXTFILE);
00652 new ScriptTextfileWindow(file_type, slot);
00653 }
00654
00655
00657 static const NWidgetPart _nested_ai_config_widgets[] = {
00658 NWidget(NWID_HORIZONTAL),
00659 NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
00660 NWidget(WWT_CAPTION, COLOUR_MAUVE), SetDataTip(STR_AI_CONFIG_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00661 EndContainer(),
00662 NWidget(WWT_PANEL, COLOUR_MAUVE, WID_AIC_BACKGROUND),
00663 NWidget(NWID_VERTICAL), SetPIP(4, 4, 4),
00664 NWidget(NWID_HORIZONTAL), SetPIP(7, 0, 7),
00665 NWidget(WWT_PUSHARROWBTN, COLOUR_YELLOW, WID_AIC_DECREASE), SetFill(0, 1), SetDataTip(AWV_DECREASE, STR_NULL),
00666 NWidget(WWT_PUSHARROWBTN, COLOUR_YELLOW, WID_AIC_INCREASE), SetFill(0, 1), SetDataTip(AWV_INCREASE, STR_NULL),
00667 NWidget(NWID_SPACER), SetMinimalSize(6, 0),
00668 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),
00669 EndContainer(),
00670 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(7, 0, 7),
00671 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),
00672 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),
00673 EndContainer(),
00674 EndContainer(),
00675 NWidget(WWT_FRAME, COLOUR_MAUVE), SetDataTip(STR_AI_CONFIG_AI, STR_NULL), SetPadding(0, 5, 0, 5),
00676 NWidget(NWID_HORIZONTAL),
00677 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),
00678 NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, WID_AIC_SCROLLBAR),
00679 EndContainer(),
00680 EndContainer(),
00681 NWidget(NWID_SPACER), SetMinimalSize(0, 9),
00682 NWidget(WWT_FRAME, COLOUR_MAUVE), SetDataTip(STR_AI_CONFIG_GAMESCRIPT, STR_NULL), SetPadding(0, 5, 4, 5),
00683 NWidget(WWT_MATRIX, COLOUR_MAUVE, WID_AIC_GAMELIST), SetMinimalSize(288, 14), SetFill(1, 0), SetDataTip(0x101, STR_AI_CONFIG_GAMELIST_TOOLTIP),
00684 EndContainer(),
00685 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(7, 0, 7),
00686 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_AIC_CHANGE), SetFill(1, 0), SetMinimalSize(93, 12), SetDataTip(STR_AI_CONFIG_CHANGE, STR_AI_CONFIG_CHANGE_TOOLTIP),
00687 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_AIC_CONFIGURE), SetFill(1, 0), SetMinimalSize(93, 12), SetDataTip(STR_AI_CONFIG_CONFIGURE, STR_AI_CONFIG_CONFIGURE_TOOLTIP),
00688 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_AIC_CLOSE), SetFill(1, 0), SetMinimalSize(93, 12), SetDataTip(STR_AI_SETTINGS_CLOSE, STR_NULL),
00689 EndContainer(),
00690 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(7, 0, 7),
00691 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_AIC_TEXTFILE + TFT_README), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_TEXTFILE_VIEW_README, STR_NULL),
00692 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_AIC_TEXTFILE + TFT_CHANGELOG), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_TEXTFILE_VIEW_CHANGELOG, STR_NULL),
00693 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_AIC_TEXTFILE + TFT_LICENSE), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_TEXTFILE_VIEW_LICENCE, STR_NULL),
00694 EndContainer(),
00695 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),
00696 EndContainer(),
00697 };
00698
00700 static const WindowDesc _ai_config_desc(
00701 WDP_CENTER, 0, 0,
00702 WC_GAME_OPTIONS, WC_NONE,
00703 0,
00704 _nested_ai_config_widgets, lengthof(_nested_ai_config_widgets)
00705 );
00706
00710 struct AIConfigWindow : public Window {
00711 CompanyID selected_slot;
00712 int line_height;
00713 Scrollbar *vscroll;
00714
00715 AIConfigWindow() : Window()
00716 {
00717 this->InitNested(&_ai_config_desc, WN_GAME_OPTIONS_AI);
00718 this->vscroll = this->GetScrollbar(WID_AIC_SCROLLBAR);
00719 this->selected_slot = INVALID_COMPANY;
00720 NWidgetCore *nwi = this->GetWidget<NWidgetCore>(WID_AIC_LIST);
00721 this->vscroll->SetCapacity(nwi->current_y / this->line_height);
00722 this->vscroll->SetCount(MAX_COMPANIES);
00723 nwi->widget_data = (this->vscroll->GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
00724 this->OnInvalidateData(0);
00725 }
00726
00727 ~AIConfigWindow()
00728 {
00729 DeleteWindowByClass(WC_AI_LIST);
00730 DeleteWindowByClass(WC_AI_SETTINGS);
00731 }
00732
00733 virtual void SetStringParameters(int widget) const
00734 {
00735 switch (widget) {
00736 case WID_AIC_NUMBER:
00737 SetDParam(0, GetGameSettings().difficulty.max_no_competitors);
00738 break;
00739 case WID_AIC_CHANGE:
00740 switch (selected_slot) {
00741 case OWNER_DEITY:
00742 SetDParam(0, STR_AI_CONFIG_CHANGE_GAMESCRIPT);
00743 break;
00744
00745 case INVALID_COMPANY:
00746 SetDParam(0, STR_AI_CONFIG_CHANGE_NONE);
00747 break;
00748
00749 default:
00750 SetDParam(0, STR_AI_CONFIG_CHANGE_AI);
00751 break;
00752 }
00753 break;
00754 }
00755 }
00756
00757 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00758 {
00759 switch (widget) {
00760 case WID_AIC_GAMELIST:
00761 case WID_AIC_LIST:
00762 this->line_height = FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM;
00763 size->height = GB(this->GetWidget<NWidgetCore>(widget)->widget_data, MAT_ROW_START, MAT_ROW_BITS) * this->line_height;
00764 break;
00765 }
00766 }
00767
00773 static bool IsEditable(CompanyID slot)
00774 {
00775 if (slot == OWNER_DEITY) return _game_mode != GM_NORMAL;
00776
00777 if (_game_mode != GM_NORMAL) {
00778 return slot > 0 && slot <= GetGameSettings().difficulty.max_no_competitors;
00779 }
00780 if (Company::IsValidID(slot) || slot < 0) return false;
00781
00782 int max_slot = GetGameSettings().difficulty.max_no_competitors;
00783 for (CompanyID cid = COMPANY_FIRST; cid < (CompanyID)max_slot && cid < MAX_COMPANIES; cid++) {
00784 if (Company::IsValidHumanID(cid)) max_slot++;
00785 }
00786 return slot < max_slot;
00787 }
00788
00789 virtual void DrawWidget(const Rect &r, int widget) const
00790 {
00791 switch (widget) {
00792 case WID_AIC_GAMELIST: {
00793 StringID text = STR_AI_CONFIG_NONE;
00794
00795 if (GameConfig::GetConfig()->GetInfo() != NULL) {
00796 SetDParamStr(0, GameConfig::GetConfig()->GetInfo()->GetName());
00797 text = STR_JUST_RAW_STRING;
00798 }
00799
00800 DrawString(r.left + 10, r.right - 10, r.top + WD_MATRIX_TOP, text,
00801 (this->selected_slot == OWNER_DEITY) ? TC_WHITE : (IsEditable(OWNER_DEITY) ? TC_ORANGE : TC_SILVER));
00802
00803 break;
00804 }
00805
00806 case WID_AIC_LIST: {
00807 int y = r.top;
00808 for (int i = this->vscroll->GetPosition(); this->vscroll->IsVisible(i) && i < MAX_COMPANIES; i++) {
00809 StringID text;
00810
00811 if ((_game_mode != GM_NORMAL && i == 0) || (_game_mode == GM_NORMAL && Company::IsValidHumanID(i))) {
00812 text = STR_AI_CONFIG_HUMAN_PLAYER;
00813 } else if (AIConfig::GetConfig((CompanyID)i)->GetInfo() != NULL) {
00814 SetDParamStr(0, AIConfig::GetConfig((CompanyID)i)->GetInfo()->GetName());
00815 text = STR_JUST_RAW_STRING;
00816 } else {
00817 text = STR_AI_CONFIG_RANDOM_AI;
00818 }
00819 DrawString(r.left + 10, r.right - 10, y + WD_MATRIX_TOP, text,
00820 (this->selected_slot == i) ? TC_WHITE : (IsEditable((CompanyID)i) ? TC_ORANGE : TC_SILVER));
00821 y += this->line_height;
00822 }
00823 break;
00824 }
00825 }
00826 }
00827
00828 virtual void OnClick(Point pt, int widget, int click_count)
00829 {
00830 if (widget >= WID_AIC_TEXTFILE && widget < WID_AIC_TEXTFILE + TFT_END) {
00831 if (this->selected_slot == INVALID_COMPANY || GetConfig(this->selected_slot) == NULL) return;
00832
00833 ShowScriptTextfileWindow((TextfileType)(widget - WID_AIC_TEXTFILE), this->selected_slot);
00834 return;
00835 }
00836
00837 switch (widget) {
00838 case WID_AIC_DECREASE:
00839 case WID_AIC_INCREASE: {
00840 int new_value;
00841 if (widget == WID_AIC_DECREASE) {
00842 new_value = max(0, GetGameSettings().difficulty.max_no_competitors - 1);
00843 } else {
00844 new_value = min(MAX_COMPANIES - 1, GetGameSettings().difficulty.max_no_competitors + 1);
00845 }
00846 IConsoleSetSetting("difficulty.max_no_competitors", new_value);
00847 this->InvalidateData();
00848 break;
00849 }
00850
00851 case WID_AIC_GAMELIST: {
00852 this->selected_slot = OWNER_DEITY;
00853 this->InvalidateData();
00854 if (click_count > 1 && this->selected_slot != INVALID_COMPANY) ShowAIListWindow((CompanyID)this->selected_slot);
00855 break;
00856 }
00857
00858 case WID_AIC_LIST: {
00859 this->selected_slot = (CompanyID)this->vscroll->GetScrolledRowFromWidget(pt.y, this, widget, 0, this->line_height);
00860 this->InvalidateData();
00861 if (click_count > 1 && this->selected_slot != INVALID_COMPANY) ShowAIListWindow((CompanyID)this->selected_slot);
00862 break;
00863 }
00864
00865 case WID_AIC_MOVE_UP:
00866 if (IsEditable(this->selected_slot) && IsEditable((CompanyID)(this->selected_slot - 1))) {
00867 Swap(GetGameSettings().ai_config[this->selected_slot], GetGameSettings().ai_config[this->selected_slot - 1]);
00868 this->selected_slot--;
00869 this->vscroll->ScrollTowards(this->selected_slot);
00870 this->InvalidateData();
00871 }
00872 break;
00873
00874 case WID_AIC_MOVE_DOWN:
00875 if (IsEditable(this->selected_slot) && IsEditable((CompanyID)(this->selected_slot + 1))) {
00876 Swap(GetGameSettings().ai_config[this->selected_slot], GetGameSettings().ai_config[this->selected_slot + 1]);
00877 this->selected_slot++;
00878 this->vscroll->ScrollTowards(this->selected_slot);
00879 this->InvalidateData();
00880 }
00881 break;
00882
00883 case WID_AIC_CHANGE:
00884 ShowAIListWindow((CompanyID)this->selected_slot);
00885 break;
00886
00887 case WID_AIC_CONFIGURE:
00888 ShowAISettingsWindow((CompanyID)this->selected_slot);
00889 break;
00890
00891 case WID_AIC_CLOSE:
00892 delete this;
00893 break;
00894
00895 case WID_AIC_CONTENT_DOWNLOAD:
00896 if (!_network_available) {
00897 ShowErrorMessage(STR_NETWORK_ERROR_NOTAVAILABLE, INVALID_STRING_ID, WL_ERROR);
00898 } else {
00899 #if defined(ENABLE_NETWORK)
00900 ShowNetworkContentListWindow(NULL, CONTENT_TYPE_AI);
00901 _network_content_client.RequestContentList(CONTENT_TYPE_GAME);
00902 #endif
00903 }
00904 break;
00905 }
00906 }
00907
00913 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
00914 {
00915 if (!IsEditable(this->selected_slot)) {
00916 this->selected_slot = INVALID_COMPANY;
00917 }
00918
00919 if (!gui_scope) return;
00920
00921 this->SetWidgetDisabledState(WID_AIC_DECREASE, GetGameSettings().difficulty.max_no_competitors == 0);
00922 this->SetWidgetDisabledState(WID_AIC_INCREASE, GetGameSettings().difficulty.max_no_competitors == MAX_COMPANIES - 1);
00923 this->SetWidgetDisabledState(WID_AIC_CHANGE, this->selected_slot == INVALID_COMPANY);
00924 this->SetWidgetDisabledState(WID_AIC_CONFIGURE, this->selected_slot == INVALID_COMPANY || GetConfig(this->selected_slot)->GetConfigList()->size() == 0);
00925 this->SetWidgetDisabledState(WID_AIC_MOVE_UP, this->selected_slot == OWNER_DEITY || this->selected_slot == INVALID_COMPANY || !IsEditable((CompanyID)(this->selected_slot - 1)));
00926 this->SetWidgetDisabledState(WID_AIC_MOVE_DOWN, this->selected_slot == OWNER_DEITY || this->selected_slot == INVALID_COMPANY || !IsEditable((CompanyID)(this->selected_slot + 1)));
00927
00928 for (TextfileType tft = TFT_BEGIN; tft < TFT_END; tft++) {
00929 this->SetWidgetDisabledState(WID_AIC_TEXTFILE + tft, this->selected_slot == INVALID_COMPANY || (GetConfig(this->selected_slot)->GetTextfile(tft, this->selected_slot) == NULL));
00930 }
00931 }
00932 };
00933
00935 void ShowAIConfigWindow()
00936 {
00937 DeleteWindowByClass(WC_GAME_OPTIONS);
00938 new AIConfigWindow();
00939 }
00940
00949 static bool SetScriptButtonColour(NWidgetCore &button, bool dead, bool paused)
00950 {
00951
00952
00953 Colours colour = dead ? COLOUR_RED :
00954 (paused ? COLOUR_YELLOW : COLOUR_GREY);
00955 if (button.colour != colour) {
00956 button.colour = colour;
00957 return true;
00958 }
00959 return false;
00960 }
00961
00965 struct AIDebugWindow : public Window {
00966 static const int top_offset;
00967 static const int bottom_offset;
00968
00969 static const unsigned int MAX_BREAK_STR_STRING_LENGTH = 256;
00970
00971 static CompanyID ai_debug_company;
00972 int redraw_timer;
00973 int last_vscroll_pos;
00974 bool autoscroll;
00975 bool show_break_box;
00976 static bool break_check_enabled;
00977 static char break_string[MAX_BREAK_STR_STRING_LENGTH];
00978 QueryString break_editbox;
00979 static StringFilter break_string_filter;
00980 static bool case_sensitive_break_check;
00981 int highlight_row;
00982 Scrollbar *vscroll;
00983
00984 ScriptLog::LogData *GetLogPointer() const
00985 {
00986 if (ai_debug_company == OWNER_DEITY) return (ScriptLog::LogData *)Game::GetInstance()->GetLogPointer();
00987 return (ScriptLog::LogData *)Company::Get(ai_debug_company)->ai_instance->GetLogPointer();
00988 }
00989
00995 AIDebugWindow(const WindowDesc *desc, WindowNumber number) : break_editbox(MAX_BREAK_STR_STRING_LENGTH)
00996 {
00997 this->CreateNestedTree(desc);
00998 this->vscroll = this->GetScrollbar(WID_AID_SCROLLBAR);
00999 this->show_break_box = _settings_client.gui.ai_developer_tools;
01000 this->GetWidget<NWidgetStacked>(WID_AID_BREAK_STRING_WIDGETS)->SetDisplayedPlane(this->show_break_box ? 0 : SZSP_HORIZONTAL);
01001 this->FinishInitNested(desc, number);
01002
01003 if (!this->show_break_box) break_check_enabled = false;
01004
01005 for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
01006 this->SetWidgetDisabledState(i + WID_AID_COMPANY_BUTTON_START, !Company::IsValidAiID(i));
01007 }
01008 this->EnableWidget(WID_AID_SCRIPT_GAME);
01009 this->DisableWidget(WID_AID_RELOAD_TOGGLE);
01010 this->DisableWidget(WID_AID_SETTINGS);
01011 this->DisableWidget(WID_AID_CONTINUE_BTN);
01012
01013 this->last_vscroll_pos = 0;
01014 this->autoscroll = true;
01015 this->highlight_row = -1;
01016
01017 this->querystrings[WID_AID_BREAK_STR_EDIT_BOX] = &this->break_editbox;
01018
01019
01020 this->break_editbox.text.Assign(this->break_string);
01021
01022
01023 if (ai_debug_company == OWNER_DEITY) {
01024 this->LowerWidget(WID_AID_SCRIPT_GAME);
01025 this->SetWidgetDisabledState(WID_AID_CONTINUE_BTN, !Game::IsPaused());
01026 } else if (ai_debug_company != INVALID_COMPANY) {
01027 this->LowerWidget(ai_debug_company + WID_AID_COMPANY_BUTTON_START);
01028 this->SetWidgetDisabledState(WID_AID_CONTINUE_BTN, !AI::IsPaused(ai_debug_company));
01029 }
01030 this->SetWidgetLoweredState(WID_AID_BREAK_STR_ON_OFF_BTN, this->break_check_enabled);
01031 this->SetWidgetLoweredState(WID_AID_MATCH_CASE_BTN, this->case_sensitive_break_check);
01032
01033 }
01034
01035 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
01036 {
01037 if (widget == WID_AID_LOG_PANEL) {
01038 resize->height = FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
01039 size->height = 14 * resize->height + this->top_offset + this->bottom_offset;
01040 }
01041 }
01042
01043 virtual void OnPaint()
01044 {
01045
01046 if (ai_debug_company == INVALID_COMPANY || (ai_debug_company != OWNER_DEITY && !Company::IsValidAiID(ai_debug_company))) {
01047 if (ai_debug_company != INVALID_COMPANY) {
01048
01049 this->RaiseWidget(ai_debug_company + WID_AID_COMPANY_BUTTON_START);
01050
01051 ai_debug_company = INVALID_COMPANY;
01052 }
01053
01054 const Company *c;
01055 FOR_ALL_COMPANIES(c) {
01056 if (c->is_ai) {
01057
01058 this->LowerWidget(c->index + WID_AID_COMPANY_BUTTON_START);
01059
01060 ai_debug_company = c->index;
01061 break;
01062 }
01063 }
01064
01065
01066 if (ai_debug_company == INVALID_COMPANY && Game::GetInstance() != NULL) {
01067
01068 this->LowerWidget(WID_AID_SCRIPT_GAME);
01069
01070 ai_debug_company = OWNER_DEITY;
01071 }
01072 }
01073
01074
01075 this->SetWidgetDisabledState(WID_AID_SETTINGS, ai_debug_company == INVALID_COMPANY);
01076 this->SetWidgetDisabledState(WID_AID_RELOAD_TOGGLE, ai_debug_company == INVALID_COMPANY || ai_debug_company == OWNER_DEITY);
01077 this->SetWidgetDisabledState(WID_AID_SCRIPT_GAME, Game::GetGameInstance() == NULL);
01078
01079
01080 this->DrawWidgets();
01081
01082 if (this->IsShaded()) return;
01083
01084
01085 for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
01086 NWidgetCore *button = this->GetWidget<NWidgetCore>(i + WID_AID_COMPANY_BUTTON_START);
01087 bool dirty = false;
01088
01089 bool valid = Company::IsValidAiID(i);
01090 bool disabled = !valid;
01091 if (button->IsDisabled() != disabled) {
01092
01093 button->SetDisabled(disabled);
01094 dirty = true;
01095 }
01096
01097
01098 bool dead = valid && Company::Get(i)->ai_instance->IsDead();
01099 bool paused = valid && Company::Get(i)->ai_instance->IsPaused();
01100
01101
01102 dirty = SetScriptButtonColour(*button, dead, paused) || dirty;
01103
01104
01105 if (dirty) this->SetDirty();
01106
01107 if (!valid) continue;
01108
01109 byte offset = (i == ai_debug_company) ? 1 : 0;
01110 DrawCompanyIcon(i, button->pos_x + button->current_x / 2 - 7 + offset, this->GetWidget<NWidgetBase>(WID_AID_COMPANY_BUTTON_START + i)->pos_y + 2 + offset);
01111 }
01112
01113
01114 GameInstance *game = Game::GetInstance();
01115 bool dead = game != NULL && game->IsDead();
01116 bool paused = game != NULL && game->IsPaused();
01117 NWidgetCore *button = this->GetWidget<NWidgetCore>(WID_AID_SCRIPT_GAME);
01118 if (SetScriptButtonColour(*button, dead, paused)) {
01119
01120 this->SetWidgetDirty(WID_AID_SCRIPT_GAME);
01121 }
01122
01123
01124 if (ai_debug_company == INVALID_COMPANY) return;
01125
01126 ScriptLog::LogData *log = this->GetLogPointer();
01127
01128 int scroll_count = (log == NULL) ? 0 : log->used;
01129 if (this->vscroll->GetCount() != scroll_count) {
01130 this->vscroll->SetCount(scroll_count);
01131
01132
01133 this->SetWidgetDirty(WID_AID_SCROLLBAR);
01134 }
01135
01136 if (log == NULL) return;
01137
01138
01139
01140 if (this->last_vscroll_pos != this->vscroll->GetPosition()) {
01141 this->autoscroll = this->vscroll->GetPosition() >= log->used - this->vscroll->GetCapacity();
01142 }
01143 if (this->autoscroll) {
01144 int scroll_pos = max(0, log->used - this->vscroll->GetCapacity());
01145 if (scroll_pos != this->vscroll->GetPosition()) {
01146 this->vscroll->SetPosition(scroll_pos);
01147
01148
01149 this->SetWidgetDirty(WID_AID_SCROLLBAR);
01150 this->SetWidgetDirty(WID_AID_LOG_PANEL);
01151 }
01152 }
01153 this->last_vscroll_pos = this->vscroll->GetPosition();
01154 }
01155
01156 virtual void SetStringParameters(int widget) const
01157 {
01158 switch (widget) {
01159 case WID_AID_NAME_TEXT:
01160 if (ai_debug_company == OWNER_DEITY) {
01161 const GameInfo *info = Game::GetInfo();
01162 assert(info != NULL);
01163 SetDParam(0, STR_AI_DEBUG_NAME_AND_VERSION);
01164 SetDParamStr(1, info->GetName());
01165 SetDParam(2, info->GetVersion());
01166 } else if (ai_debug_company == INVALID_COMPANY || !Company::IsValidAiID(ai_debug_company)) {
01167 SetDParam(0, STR_EMPTY);
01168 } else {
01169 const AIInfo *info = Company::Get(ai_debug_company)->ai_info;
01170 assert(info != NULL);
01171 SetDParam(0, STR_AI_DEBUG_NAME_AND_VERSION);
01172 SetDParamStr(1, info->GetName());
01173 SetDParam(2, info->GetVersion());
01174 }
01175 break;
01176 }
01177 }
01178
01179 virtual void DrawWidget(const Rect &r, int widget) const
01180 {
01181 if (ai_debug_company == INVALID_COMPANY) return;
01182
01183 switch (widget) {
01184 case WID_AID_LOG_PANEL: {
01185 ScriptLog::LogData *log = this->GetLogPointer();
01186 if (log == NULL) return;
01187
01188 int y = this->top_offset;
01189 for (int i = this->vscroll->GetPosition(); this->vscroll->IsVisible(i) && i < log->used; i++) {
01190 int pos = (i + log->pos + 1 - log->used + log->count) % log->count;
01191 if (log->lines[pos] == NULL) break;
01192
01193 TextColour colour;
01194 switch (log->type[pos]) {
01195 case ScriptLog::LOG_SQ_INFO: colour = TC_BLACK; break;
01196 case ScriptLog::LOG_SQ_ERROR: colour = TC_RED; break;
01197 case ScriptLog::LOG_INFO: colour = TC_BLACK; break;
01198 case ScriptLog::LOG_WARNING: colour = TC_YELLOW; break;
01199 case ScriptLog::LOG_ERROR: colour = TC_RED; break;
01200 default: colour = TC_BLACK; break;
01201 }
01202
01203
01204 if (pos == this->highlight_row) {
01205 GfxFillRect(r.left + 1, r.top + y, r.right - 1, r.top + y + this->resize.step_height - WD_PAR_VSEP_NORMAL, PC_BLACK);
01206 if (colour == TC_BLACK) colour = TC_WHITE;
01207 }
01208
01209 DrawString(r.left + 7, r.right - 7, r.top + y, log->lines[pos], colour, SA_LEFT | SA_FORCE);
01210 y += this->resize.step_height;
01211 }
01212 break;
01213 }
01214 }
01215 }
01216
01221 void ChangeToAI(CompanyID show_ai)
01222 {
01223 if (ai_debug_company == OWNER_DEITY) {
01224 this->RaiseWidget(WID_AID_SCRIPT_GAME);
01225 } else {
01226 this->RaiseWidget(ai_debug_company + WID_AID_COMPANY_BUTTON_START);
01227 }
01228 ai_debug_company = show_ai;
01229
01230 ScriptLog::LogData *log = this->GetLogPointer();
01231 this->vscroll->SetCount((log == NULL) ? 0 : log->used);
01232
01233 if (ai_debug_company == OWNER_DEITY) {
01234 this->LowerWidget(WID_AID_SCRIPT_GAME);
01235 this->SetWidgetDisabledState(WID_AID_CONTINUE_BTN, !Game::IsPaused());
01236 } else {
01237 this->LowerWidget(ai_debug_company + WID_AID_COMPANY_BUTTON_START);
01238 this->SetWidgetDisabledState(WID_AID_CONTINUE_BTN, !AI::IsPaused(ai_debug_company));
01239 }
01240
01241 this->highlight_row = -1;
01242 this->autoscroll = true;
01243 this->last_vscroll_pos = this->vscroll->GetPosition();
01244 this->SetDirty();
01245
01246 DeleteWindowByClass(WC_AI_SETTINGS);
01247 }
01248
01249 virtual void OnClick(Point pt, int widget, int click_count)
01250 {
01251
01252 if (this->IsWidgetDisabled(widget)) return;
01253
01254
01255 if (IsInsideMM(widget, WID_AID_COMPANY_BUTTON_START, WID_AID_COMPANY_BUTTON_END + 1)) {
01256 ChangeToAI((CompanyID)(widget - WID_AID_COMPANY_BUTTON_START));
01257 }
01258
01259 switch (widget) {
01260 case WID_AID_SCRIPT_GAME:
01261 ChangeToAI(OWNER_DEITY);
01262 break;
01263
01264 case WID_AID_RELOAD_TOGGLE:
01265 if (ai_debug_company == OWNER_DEITY) break;
01266
01267 DoCommandP(0, 2 | ai_debug_company << 16, CRR_MANUAL, CMD_COMPANY_CTRL);
01268 DoCommandP(0, 1 | ai_debug_company << 16, 0, CMD_COMPANY_CTRL);
01269 break;
01270
01271 case WID_AID_SETTINGS:
01272 ShowAISettingsWindow(ai_debug_company);
01273 break;
01274
01275 case WID_AID_BREAK_STR_ON_OFF_BTN:
01276 this->break_check_enabled = !this->break_check_enabled;
01277 this->SetWidgetLoweredState(WID_AID_BREAK_STR_ON_OFF_BTN, this->break_check_enabled);
01278 this->SetWidgetDirty(WID_AID_BREAK_STR_ON_OFF_BTN);
01279 break;
01280
01281 case WID_AID_MATCH_CASE_BTN:
01282 this->case_sensitive_break_check = !this->case_sensitive_break_check;
01283 this->SetWidgetLoweredState(WID_AID_MATCH_CASE_BTN, this->case_sensitive_break_check);
01284 this->SetWidgetDirty(WID_AID_MATCH_CASE_BTN);
01285 break;
01286
01287 case WID_AID_CONTINUE_BTN:
01288
01289 if (ai_debug_company == OWNER_DEITY) {
01290 Game::Unpause();
01291 this->SetWidgetDirty(WID_AID_SCRIPT_GAME);
01292 } else {
01293 AI::Unpause(ai_debug_company);
01294 this->SetWidgetDirty(WID_AID_COMPANY_BUTTON_START + ai_debug_company);
01295 }
01296
01297
01298 if ((_pause_mode & PM_PAUSED_NORMAL) == PM_PAUSED_NORMAL) {
01299 bool all_unpaused = !Game::IsPaused();
01300 if (all_unpaused) {
01301 Company *c;
01302 FOR_ALL_COMPANIES(c) {
01303 if (c->is_ai && AI::IsPaused(c->index)) {
01304 all_unpaused = false;
01305 break;
01306 }
01307 }
01308 if (all_unpaused) {
01309
01310 DoCommandP(0, PM_PAUSED_NORMAL, 0, CMD_PAUSE);
01311 }
01312 }
01313 }
01314
01315 this->highlight_row = -1;
01316 this->SetWidgetDirty(WID_AID_LOG_PANEL);
01317 this->DisableWidget(WID_AID_CONTINUE_BTN);
01318 break;
01319 }
01320 }
01321
01322 virtual EventState OnKeyPress(uint16 key, uint16 keycode)
01323 {
01324 EventState state = ES_NOT_HANDLED;
01325 int num = CheckHotkeyMatch(aidebug_hotkeys, keycode, this);
01326 if (num != -1) {
01327 if (this->show_break_box && num == WID_AID_BREAK_STR_EDIT_BOX) {
01328 this->SetFocusedWidget(WID_AID_BREAK_STR_EDIT_BOX);
01329 SetFocusedWindow(this);
01330 state = ES_HANDLED;
01331 } else if (this->show_break_box || num < WID_AID_BREAK_STRING_WIDGETS) {
01332 this->OnClick(Point(), num, 1);
01333 state = ES_HANDLED;
01334 }
01335 }
01336 return state;
01337 }
01338
01339 virtual void OnEditboxChanged(int wid)
01340 {
01341 if (wid == WID_AID_BREAK_STR_EDIT_BOX) {
01342
01343 strecpy(this->break_string, this->break_editbox.text.buf, lastof(this->break_string));
01344 break_string_filter.SetFilterTerm(this->break_string);
01345 }
01346 }
01347
01353 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
01354 {
01355 if (data == -1 || ai_debug_company == data) this->SetDirty();
01356
01357
01358
01359 if (!gui_scope && data == ai_debug_company && this->break_check_enabled && !this->break_string_filter.IsEmpty()) {
01360
01361 ScriptLog::LogData *log = this->GetLogPointer();
01362
01363 if (log != NULL) {
01364 this->break_string_filter.ResetState();
01365 this->break_string_filter.AddLine(log->lines[log->pos]);
01366 if (this->break_string_filter.GetState()) {
01367
01368 if (ai_debug_company == OWNER_DEITY) {
01369 Game::Pause();
01370 } else {
01371 AI::Pause(ai_debug_company);
01372 }
01373
01374
01375 if ((_pause_mode & PM_PAUSED_NORMAL) == PM_UNPAUSED) {
01376 DoCommandP(0, PM_PAUSED_NORMAL, 1, CMD_PAUSE);
01377 }
01378
01379
01380 this->EnableWidget(WID_AID_CONTINUE_BTN);
01381 this->SetWidgetDirty(WID_AID_CONTINUE_BTN);
01382
01383
01384 this->highlight_row = log->pos;
01385 }
01386 }
01387 }
01388 }
01389
01390 virtual void OnResize()
01391 {
01392 this->vscroll->SetCapacityFromWidget(this, WID_AID_LOG_PANEL);
01393 }
01394
01395 static Hotkey<AIDebugWindow> aidebug_hotkeys[];
01396 };
01397
01398 const int AIDebugWindow::top_offset = WD_FRAMERECT_TOP + 2;
01399 const int AIDebugWindow::bottom_offset = WD_FRAMERECT_BOTTOM;
01400 CompanyID AIDebugWindow::ai_debug_company = INVALID_COMPANY;
01401 char AIDebugWindow::break_string[MAX_BREAK_STR_STRING_LENGTH] = "";
01402 bool AIDebugWindow::break_check_enabled = true;
01403 bool AIDebugWindow::case_sensitive_break_check = false;
01404 StringFilter AIDebugWindow::break_string_filter(&AIDebugWindow::case_sensitive_break_check);
01405
01407 NWidgetBase *MakeCompanyButtonRowsAIDebug(int *biggest_index)
01408 {
01409 return MakeCompanyButtonRows(biggest_index, WID_AID_COMPANY_BUTTON_START, WID_AID_COMPANY_BUTTON_END, 8, STR_AI_DEBUG_SELECT_AI_TOOLTIP);
01410 }
01411
01412 Hotkey<AIDebugWindow> AIDebugWindow::aidebug_hotkeys[] = {
01413 Hotkey<AIDebugWindow>('1', "company_1", WID_AID_COMPANY_BUTTON_START),
01414 Hotkey<AIDebugWindow>('2', "company_2", WID_AID_COMPANY_BUTTON_START + 1),
01415 Hotkey<AIDebugWindow>('3', "company_3", WID_AID_COMPANY_BUTTON_START + 2),
01416 Hotkey<AIDebugWindow>('4', "company_4", WID_AID_COMPANY_BUTTON_START + 3),
01417 Hotkey<AIDebugWindow>('5', "company_5", WID_AID_COMPANY_BUTTON_START + 4),
01418 Hotkey<AIDebugWindow>('6', "company_6", WID_AID_COMPANY_BUTTON_START + 5),
01419 Hotkey<AIDebugWindow>('7', "company_7", WID_AID_COMPANY_BUTTON_START + 6),
01420 Hotkey<AIDebugWindow>('8', "company_8", WID_AID_COMPANY_BUTTON_START + 7),
01421 Hotkey<AIDebugWindow>('9', "company_9", WID_AID_COMPANY_BUTTON_START + 8),
01422 Hotkey<AIDebugWindow>((uint16)0, "company_10", WID_AID_COMPANY_BUTTON_START + 9),
01423 Hotkey<AIDebugWindow>((uint16)0, "company_11", WID_AID_COMPANY_BUTTON_START + 10),
01424 Hotkey<AIDebugWindow>((uint16)0, "company_12", WID_AID_COMPANY_BUTTON_START + 11),
01425 Hotkey<AIDebugWindow>((uint16)0, "company_13", WID_AID_COMPANY_BUTTON_START + 12),
01426 Hotkey<AIDebugWindow>((uint16)0, "company_14", WID_AID_COMPANY_BUTTON_START + 13),
01427 Hotkey<AIDebugWindow>((uint16)0, "company_15", WID_AID_COMPANY_BUTTON_START + 14),
01428 Hotkey<AIDebugWindow>('S', "settings", WID_AID_SETTINGS),
01429 Hotkey<AIDebugWindow>('0', "game_script", WID_AID_SCRIPT_GAME),
01430 Hotkey<AIDebugWindow>((uint16)0, "reload", WID_AID_RELOAD_TOGGLE),
01431 Hotkey<AIDebugWindow>('B', "break_toggle", WID_AID_BREAK_STR_ON_OFF_BTN),
01432 Hotkey<AIDebugWindow>('F', "break_string", WID_AID_BREAK_STR_EDIT_BOX),
01433 Hotkey<AIDebugWindow>('C', "match_case", WID_AID_MATCH_CASE_BTN),
01434 Hotkey<AIDebugWindow>(WKC_RETURN, "continue", WID_AID_CONTINUE_BTN),
01435 HOTKEY_LIST_END(AIDebugWindow)
01436 };
01437 Hotkey<AIDebugWindow> *_aidebug_hotkeys = AIDebugWindow::aidebug_hotkeys;
01438
01440 static const NWidgetPart _nested_ai_debug_widgets[] = {
01441 NWidget(NWID_HORIZONTAL),
01442 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
01443 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_AI_DEBUG, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
01444 NWidget(WWT_SHADEBOX, COLOUR_GREY),
01445 NWidget(WWT_STICKYBOX, COLOUR_GREY),
01446 EndContainer(),
01447 NWidget(WWT_PANEL, COLOUR_GREY, WID_AID_VIEW),
01448 NWidgetFunction(MakeCompanyButtonRowsAIDebug), SetPadding(0, 2, 1, 2),
01449 EndContainer(),
01450 NWidget(NWID_HORIZONTAL),
01451 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_AID_SCRIPT_GAME), SetMinimalSize(100, 20), SetResize(1, 0), SetDataTip(STR_AI_GAME_SCRIPT, STR_AI_GAME_SCRIPT_TOOLTIP),
01452 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_AID_NAME_TEXT), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_JUST_STRING, STR_AI_DEBUG_NAME_TOOLTIP),
01453 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_AID_SETTINGS), SetMinimalSize(100, 20), SetDataTip(STR_AI_DEBUG_SETTINGS, STR_AI_DEBUG_SETTINGS_TOOLTIP),
01454 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_AID_RELOAD_TOGGLE), SetMinimalSize(100, 20), SetDataTip(STR_AI_DEBUG_RELOAD, STR_AI_DEBUG_RELOAD_TOOLTIP),
01455 EndContainer(),
01456 NWidget(NWID_HORIZONTAL),
01457 NWidget(NWID_VERTICAL),
01458
01459 NWidget(WWT_PANEL, COLOUR_GREY, WID_AID_LOG_PANEL), SetMinimalSize(287, 180), SetResize(1, 1), SetScrollbar(WID_AID_SCROLLBAR),
01460 EndContainer(),
01461
01462 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_AID_BREAK_STRING_WIDGETS),
01463 NWidget(NWID_HORIZONTAL),
01464 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),
01465 NWidget(WWT_PANEL, COLOUR_GREY),
01466 NWidget(NWID_HORIZONTAL),
01467 NWidget(WWT_LABEL, COLOUR_GREY), SetPadding(2, 2, 2, 4), SetDataTip(STR_AI_DEBUG_BREAK_ON_LABEL, 0x0),
01468 NWidget(WWT_EDITBOX, COLOUR_GREY, WID_AID_BREAK_STR_EDIT_BOX), SetFill(1, 1), SetResize(1, 0), SetPadding(2, 2, 2, 2), SetDataTip(STR_AI_DEBUG_BREAK_STR_OSKTITLE, STR_AI_DEBUG_BREAK_STR_TOOLTIP),
01469 EndContainer(),
01470 EndContainer(),
01471 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_AID_MATCH_CASE_BTN), SetMinimalSize(100, 0), SetFill(0, 1), SetDataTip(STR_AI_DEBUG_MATCH_CASE, STR_AI_DEBUG_MATCH_CASE_TOOLTIP),
01472 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),
01473 EndContainer(),
01474 EndContainer(),
01475 EndContainer(),
01476 NWidget(NWID_VERTICAL),
01477 NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_AID_SCROLLBAR),
01478 NWidget(WWT_RESIZEBOX, COLOUR_GREY),
01479 EndContainer(),
01480 EndContainer(),
01481 };
01482
01484 static const WindowDesc _ai_debug_desc(
01485 WDP_AUTO, 600, 450,
01486 WC_AI_DEBUG, WC_NONE,
01487 0,
01488 _nested_ai_debug_widgets, lengthof(_nested_ai_debug_widgets)
01489 );
01490
01495 Window *ShowAIDebugWindow(CompanyID show_company)
01496 {
01497 if (!_networking || _network_server) {
01498 AIDebugWindow *w = (AIDebugWindow *)BringWindowToFrontById(WC_AI_DEBUG, 0);
01499 if (w == NULL) w = new AIDebugWindow(&_ai_debug_desc, 0);
01500 if (show_company != INVALID_COMPANY) w->ChangeToAI(show_company);
01501 return w;
01502 } else {
01503 ShowErrorMessage(STR_ERROR_AI_DEBUG_SERVER_ONLY, INVALID_STRING_ID, WL_INFO);
01504 }
01505
01506 return NULL;
01507 }
01508
01512 EventState AIDebugGlobalHotkeys(uint16 key, uint16 keycode)
01513 {
01514 int num = CheckHotkeyMatch<AIDebugWindow>(_aidebug_hotkeys, keycode, NULL, true);
01515 if (num == -1) return ES_NOT_HANDLED;
01516 Window *w = ShowAIDebugWindow(INVALID_COMPANY);
01517 if (w == NULL) return ES_NOT_HANDLED;
01518 return w->OnKeyPress(key, keycode);
01519 }
01520
01524 void InitializeAIGui()
01525 {
01526 AIDebugWindow::ai_debug_company = INVALID_COMPANY;
01527 }
01528
01530 void ShowAIDebugWindowIfAIError()
01531 {
01532
01533 if (_networking && !_network_server) return;
01534
01535 Company *c;
01536 FOR_ALL_COMPANIES(c) {
01537 if (c->is_ai && c->ai_instance->IsDead()) {
01538 ShowAIDebugWindow(c->index);
01539 break;
01540 }
01541 }
01542
01543 GameInstance *g = Game::GetGameInstance();
01544 if (g != NULL && g->IsDead()) {
01545 ShowAIDebugWindow(OWNER_DEITY);
01546 }
01547 }