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 "ai_gui.hpp"
00034 #include "../script/api/script_log.hpp"
00035 #include "ai_config.hpp"
00036 #include "ai_info.hpp"
00037 #include "ai_instance.hpp"
00038 #include "../game/game.hpp"
00039 #include "../game/game_config.hpp"
00040 #include "../game/game_info.hpp"
00041 #include "../game/game_instance.hpp"
00042
00043
00044 #include "table/strings.h"
00045
00046 #include <vector>
00047
00048 static ScriptConfig *GetConfig(CompanyID slot)
00049 {
00050 if (slot == OWNER_DEITY) return GameConfig::GetConfig();
00051 return AIConfig::GetConfig(slot);
00052 }
00053
00057 struct AIListWindow : public Window {
00058 const ScriptInfoList *info_list;
00059 int selected;
00060 CompanyID slot;
00061 int line_height;
00062 Scrollbar *vscroll;
00063
00069 AIListWindow(WindowDesc *desc, CompanyID slot) : Window(desc),
00070 slot(slot)
00071 {
00072 if (slot == OWNER_DEITY) {
00073 this->info_list = Game::GetUniqueInfoList();
00074 } else {
00075 this->info_list = AI::GetUniqueInfoList();
00076 }
00077
00078 this->CreateNestedTree();
00079 this->vscroll = this->GetScrollbar(WID_AIL_SCROLLBAR);
00080 this->FinishInitNested();
00081
00082 this->vscroll->SetCount((int)this->info_list->size() + 1);
00083
00084
00085 this->selected = -1;
00086 if (GetConfig(slot)->HasScript()) {
00087 ScriptInfo *info = GetConfig(slot)->GetInfo();
00088 int i = 0;
00089 for (ScriptInfoList::const_iterator it = this->info_list->begin(); it != this->info_list->end(); it++, i++) {
00090 if ((*it).second == info) {
00091 this->selected = i;
00092 break;
00093 }
00094 }
00095 }
00096 }
00097
00098 virtual void SetStringParameters(int widget) const
00099 {
00100 switch (widget) {
00101 case WID_AIL_CAPTION:
00102 SetDParam(0, (this->slot == OWNER_DEITY) ? STR_AI_LIST_CAPTION_GAMESCRIPT : STR_AI_LIST_CAPTION_AI);
00103 break;
00104 }
00105 }
00106
00107 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00108 {
00109 if (widget == WID_AIL_LIST) {
00110 this->line_height = FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM;
00111
00112 resize->width = 1;
00113 resize->height = this->line_height;
00114 size->height = 5 * this->line_height;
00115 }
00116 }
00117
00118 virtual void DrawWidget(const Rect &r, int widget) const
00119 {
00120 switch (widget) {
00121 case WID_AIL_LIST: {
00122
00123 int y = this->GetWidget<NWidgetBase>(WID_AIL_LIST)->pos_y;
00124
00125 if (this->vscroll->IsVisible(0)) {
00126 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);
00127 y += this->line_height;
00128 }
00129 ScriptInfoList::const_iterator it = this->info_list->begin();
00130 for (int i = 1; it != this->info_list->end(); i++, it++) {
00131 if (this->vscroll->IsVisible(i)) {
00132 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);
00133 y += this->line_height;
00134 }
00135 }
00136 break;
00137 }
00138 case WID_AIL_INFO_BG: {
00139 AIInfo *selected_info = NULL;
00140 ScriptInfoList::const_iterator it = this->info_list->begin();
00141 for (int i = 1; selected_info == NULL && it != this->info_list->end(); i++, it++) {
00142 if (this->selected == i - 1) selected_info = static_cast<AIInfo *>((*it).second);
00143 }
00144
00145 if (selected_info != NULL) {
00146 int y = r.top + WD_FRAMERECT_TOP;
00147 SetDParamStr(0, selected_info->GetAuthor());
00148 DrawString(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, y, STR_AI_LIST_AUTHOR);
00149 y += FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
00150 SetDParam(0, selected_info->GetVersion());
00151 DrawString(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, y, STR_AI_LIST_VERSION);
00152 y += FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
00153 if (selected_info->GetURL() != NULL) {
00154 SetDParamStr(0, selected_info->GetURL());
00155 DrawString(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, y, STR_AI_LIST_URL);
00156 y += FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
00157 }
00158 SetDParamStr(0, selected_info->GetDescription());
00159 DrawStringMultiLine(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, y, r.bottom - WD_FRAMERECT_BOTTOM, STR_JUST_RAW_STRING, TC_WHITE);
00160 }
00161 break;
00162 }
00163 }
00164 }
00165
00169 void ChangeAI()
00170 {
00171 if (this->selected == -1) {
00172 GetConfig(slot)->Change(NULL);
00173 } else {
00174 ScriptInfoList::const_iterator it = this->info_list->begin();
00175 for (int i = 0; i < this->selected; i++) it++;
00176 GetConfig(slot)->Change((*it).second->GetName(), (*it).second->GetVersion());
00177 }
00178 InvalidateWindowData(WC_GAME_OPTIONS, WN_GAME_OPTIONS_AI);
00179 InvalidateWindowClassesData(WC_AI_SETTINGS);
00180 DeleteWindowByClass(WC_QUERY_STRING);
00181 }
00182
00183 virtual void OnClick(Point pt, int widget, int click_count)
00184 {
00185 switch (widget) {
00186 case WID_AIL_LIST: {
00187 int sel = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_AIL_LIST, 0, this->line_height) - 1;
00188 if (sel < (int)this->info_list->size()) {
00189 this->selected = sel;
00190 this->SetDirty();
00191 if (click_count > 1) {
00192 this->ChangeAI();
00193 delete this;
00194 }
00195 }
00196 break;
00197 }
00198
00199 case WID_AIL_ACCEPT: {
00200 this->ChangeAI();
00201 delete this;
00202 break;
00203 }
00204
00205 case WID_AIL_CANCEL:
00206 delete this;
00207 break;
00208 }
00209 }
00210
00211 virtual void OnResize()
00212 {
00213 this->vscroll->SetCapacityFromWidget(this, WID_AIL_LIST);
00214 }
00215
00221 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
00222 {
00223 if (_game_mode == GM_NORMAL && Company::IsValidID(this->slot)) {
00224 delete this;
00225 return;
00226 }
00227
00228 if (!gui_scope) return;
00229
00230 this->vscroll->SetCount((int)this->info_list->size() + 1);
00231
00232
00233 this->selected = min(this->selected, this->vscroll->GetCount() - 2);
00234 }
00235 };
00236
00238 static const NWidgetPart _nested_ai_list_widgets[] = {
00239 NWidget(NWID_HORIZONTAL),
00240 NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
00241 NWidget(WWT_CAPTION, COLOUR_MAUVE, WID_AIL_CAPTION), SetDataTip(STR_AI_LIST_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00242 NWidget(WWT_DEFSIZEBOX, COLOUR_MAUVE),
00243 EndContainer(),
00244 NWidget(NWID_HORIZONTAL),
00245 NWidget(WWT_MATRIX, COLOUR_MAUVE, WID_AIL_LIST), SetMinimalSize(188, 112), SetFill(1, 1), SetResize(1, 1), SetMatrixDataTip(1, 0, 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 WindowDesc _ai_list_desc(
00261 WDP_CENTER, "settings_script_list", 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(WindowDesc *desc, CompanyID slot) : Window(desc),
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();
00310 this->vscroll = this->GetScrollbar(WID_AIS_SCROLLBAR);
00311 this->FinishInitNested(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 = 5 * 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->Append() = 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 this->vscroll->SetCapacityFromWidget(this, WID_AIS_BACKGROUND);
00563 }
00564
00565 virtual void OnTick()
00566 {
00567 if (--this->timeout == 0) {
00568 this->clicked_button = -1;
00569 this->SetDirty();
00570 }
00571 }
00572
00578 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
00579 {
00580 this->RebuildVisibleSettings();
00581 }
00582 };
00583
00585 static const NWidgetPart _nested_ai_settings_widgets[] = {
00586 NWidget(NWID_HORIZONTAL),
00587 NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
00588 NWidget(WWT_CAPTION, COLOUR_MAUVE, WID_AIS_CAPTION), SetDataTip(STR_AI_SETTINGS_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00589 NWidget(WWT_DEFSIZEBOX, COLOUR_MAUVE),
00590 EndContainer(),
00591 NWidget(NWID_HORIZONTAL),
00592 NWidget(WWT_MATRIX, COLOUR_MAUVE, WID_AIS_BACKGROUND), SetMinimalSize(188, 182), SetResize(1, 1), SetFill(1, 0), SetMatrixDataTip(1, 0, STR_NULL), SetScrollbar(WID_AIS_SCROLLBAR),
00593 NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, WID_AIS_SCROLLBAR),
00594 EndContainer(),
00595 NWidget(NWID_HORIZONTAL),
00596 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
00597 NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, WID_AIS_ACCEPT), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_AI_SETTINGS_CLOSE, STR_NULL),
00598 NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, WID_AIS_RESET), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_AI_SETTINGS_RESET, STR_NULL),
00599 EndContainer(),
00600 NWidget(WWT_RESIZEBOX, COLOUR_MAUVE),
00601 EndContainer(),
00602 };
00603
00605 static WindowDesc _ai_settings_desc(
00606 WDP_CENTER, "settings_script", 500, 208,
00607 WC_AI_SETTINGS, WC_NONE,
00608 0,
00609 _nested_ai_settings_widgets, lengthof(_nested_ai_settings_widgets)
00610 );
00611
00616 static void ShowAISettingsWindow(CompanyID slot)
00617 {
00618 DeleteWindowByClass(WC_AI_LIST);
00619 DeleteWindowByClass(WC_AI_SETTINGS);
00620 new AISettingsWindow(&_ai_settings_desc, slot);
00621 }
00622
00623
00625 struct ScriptTextfileWindow : public TextfileWindow {
00626 CompanyID slot;
00627
00628 ScriptTextfileWindow(TextfileType file_type, CompanyID slot) : TextfileWindow(file_type), slot(slot)
00629 {
00630 const char *textfile = GetConfig(slot)->GetTextfile(file_type, slot);
00631 this->LoadTextfile(textfile, (slot == OWNER_DEITY) ? GAME_DIR : AI_DIR);
00632 }
00633
00634 void SetStringParameters(int widget) const
00635 {
00636 if (widget == WID_TF_CAPTION) {
00637 SetDParam(0, (slot == OWNER_DEITY) ? STR_CONTENT_TYPE_GAME_SCRIPT : STR_CONTENT_TYPE_AI);
00638 SetDParamStr(1, GetConfig(slot)->GetName());
00639 }
00640 }
00641 };
00642
00648 void ShowScriptTextfileWindow(TextfileType file_type, CompanyID slot)
00649 {
00650 DeleteWindowByClass(WC_TEXTFILE);
00651 new ScriptTextfileWindow(file_type, slot);
00652 }
00653
00654
00656 static const NWidgetPart _nested_ai_config_widgets[] = {
00657 NWidget(NWID_HORIZONTAL),
00658 NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
00659 NWidget(WWT_CAPTION, COLOUR_MAUVE), SetDataTip(STR_AI_CONFIG_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00660 EndContainer(),
00661 NWidget(WWT_PANEL, COLOUR_MAUVE, WID_AIC_BACKGROUND),
00662 NWidget(NWID_VERTICAL), SetPIP(4, 4, 4),
00663 NWidget(NWID_HORIZONTAL), SetPIP(7, 0, 7),
00664 NWidget(WWT_PUSHARROWBTN, COLOUR_YELLOW, WID_AIC_DECREASE), SetFill(0, 1), SetDataTip(AWV_DECREASE, STR_NULL),
00665 NWidget(WWT_PUSHARROWBTN, COLOUR_YELLOW, WID_AIC_INCREASE), SetFill(0, 1), SetDataTip(AWV_INCREASE, STR_NULL),
00666 NWidget(NWID_SPACER), SetMinimalSize(6, 0),
00667 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),
00668 EndContainer(),
00669 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(7, 0, 7),
00670 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),
00671 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),
00672 EndContainer(),
00673 EndContainer(),
00674 NWidget(WWT_FRAME, COLOUR_MAUVE), SetDataTip(STR_AI_CONFIG_AI, STR_NULL), SetPadding(0, 5, 0, 5),
00675 NWidget(NWID_HORIZONTAL),
00676 NWidget(WWT_MATRIX, COLOUR_MAUVE, WID_AIC_LIST), SetMinimalSize(288, 112), SetFill(1, 0), SetMatrixDataTip(1, 8, STR_AI_CONFIG_AILIST_TOOLTIP), SetScrollbar(WID_AIC_SCROLLBAR),
00677 NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, WID_AIC_SCROLLBAR),
00678 EndContainer(),
00679 EndContainer(),
00680 NWidget(NWID_SPACER), SetMinimalSize(0, 9),
00681 NWidget(WWT_FRAME, COLOUR_MAUVE), SetDataTip(STR_AI_CONFIG_GAMESCRIPT, STR_NULL), SetPadding(0, 5, 4, 5),
00682 NWidget(WWT_MATRIX, COLOUR_MAUVE, WID_AIC_GAMELIST), SetMinimalSize(288, 14), SetFill(1, 0), SetMatrixDataTip(1, 1, STR_AI_CONFIG_GAMELIST_TOOLTIP),
00683 EndContainer(),
00684 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(7, 0, 7),
00685 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_AIC_CHANGE), SetFill(1, 0), SetMinimalSize(93, 12), SetDataTip(STR_AI_CONFIG_CHANGE, STR_AI_CONFIG_CHANGE_TOOLTIP),
00686 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_AIC_CONFIGURE), SetFill(1, 0), SetMinimalSize(93, 12), SetDataTip(STR_AI_CONFIG_CONFIGURE, STR_AI_CONFIG_CONFIGURE_TOOLTIP),
00687 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_AIC_CLOSE), SetFill(1, 0), SetMinimalSize(93, 12), SetDataTip(STR_AI_SETTINGS_CLOSE, STR_NULL),
00688 EndContainer(),
00689 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(7, 0, 7),
00690 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_AIC_TEXTFILE + TFT_README), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_TEXTFILE_VIEW_README, STR_NULL),
00691 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_AIC_TEXTFILE + TFT_CHANGELOG), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_TEXTFILE_VIEW_CHANGELOG, STR_NULL),
00692 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_AIC_TEXTFILE + TFT_LICENSE), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_TEXTFILE_VIEW_LICENCE, STR_NULL),
00693 EndContainer(),
00694 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),
00695 EndContainer(),
00696 };
00697
00699 static WindowDesc _ai_config_desc(
00700 WDP_CENTER, "settings_script_config", 0, 0,
00701 WC_GAME_OPTIONS, WC_NONE,
00702 0,
00703 _nested_ai_config_widgets, lengthof(_nested_ai_config_widgets)
00704 );
00705
00709 struct AIConfigWindow : public Window {
00710 CompanyID selected_slot;
00711 int line_height;
00712 Scrollbar *vscroll;
00713
00714 AIConfigWindow() : Window(&_ai_config_desc)
00715 {
00716 this->InitNested(WN_GAME_OPTIONS_AI);
00717 this->vscroll = this->GetScrollbar(WID_AIC_SCROLLBAR);
00718 this->selected_slot = INVALID_COMPANY;
00719 NWidgetCore *nwi = this->GetWidget<NWidgetCore>(WID_AIC_LIST);
00720 this->vscroll->SetCapacity(nwi->current_y / this->line_height);
00721 this->vscroll->SetCount(MAX_COMPANIES);
00722 this->OnInvalidateData(0);
00723 }
00724
00725 ~AIConfigWindow()
00726 {
00727 DeleteWindowByClass(WC_AI_LIST);
00728 DeleteWindowByClass(WC_AI_SETTINGS);
00729 }
00730
00731 virtual void SetStringParameters(int widget) const
00732 {
00733 switch (widget) {
00734 case WID_AIC_NUMBER:
00735 SetDParam(0, GetGameSettings().difficulty.max_no_competitors);
00736 break;
00737 case WID_AIC_CHANGE:
00738 switch (selected_slot) {
00739 case OWNER_DEITY:
00740 SetDParam(0, STR_AI_CONFIG_CHANGE_GAMESCRIPT);
00741 break;
00742
00743 case INVALID_COMPANY:
00744 SetDParam(0, STR_AI_CONFIG_CHANGE_NONE);
00745 break;
00746
00747 default:
00748 SetDParam(0, STR_AI_CONFIG_CHANGE_AI);
00749 break;
00750 }
00751 break;
00752 }
00753 }
00754
00755 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00756 {
00757 switch (widget) {
00758 case WID_AIC_GAMELIST:
00759 this->line_height = FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM;
00760 size->height = 1 * this->line_height;
00761 break;
00762
00763 case WID_AIC_LIST:
00764 this->line_height = FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM;
00765 size->height = 8 * this->line_height;
00766 break;
00767 }
00768 }
00769
00775 static bool IsEditable(CompanyID slot)
00776 {
00777 if (slot == OWNER_DEITY) return _game_mode != GM_NORMAL || Game::GetInstance() != NULL;
00778
00779 if (_game_mode != GM_NORMAL) {
00780 return slot > 0 && slot <= GetGameSettings().difficulty.max_no_competitors;
00781 }
00782 if (Company::IsValidID(slot) || slot < 0) return false;
00783
00784 int max_slot = GetGameSettings().difficulty.max_no_competitors;
00785 for (CompanyID cid = COMPANY_FIRST; cid < (CompanyID)max_slot && cid < MAX_COMPANIES; cid++) {
00786 if (Company::IsValidHumanID(cid)) max_slot++;
00787 }
00788 return slot < max_slot;
00789 }
00790
00791 virtual void DrawWidget(const Rect &r, int widget) const
00792 {
00793 switch (widget) {
00794 case WID_AIC_GAMELIST: {
00795 StringID text = STR_AI_CONFIG_NONE;
00796
00797 if (GameConfig::GetConfig()->GetInfo() != NULL) {
00798 SetDParamStr(0, GameConfig::GetConfig()->GetInfo()->GetName());
00799 text = STR_JUST_RAW_STRING;
00800 }
00801
00802 DrawString(r.left + 10, r.right - 10, r.top + WD_MATRIX_TOP, text,
00803 (this->selected_slot == OWNER_DEITY) ? TC_WHITE : (IsEditable(OWNER_DEITY) ? TC_ORANGE : TC_SILVER));
00804
00805 break;
00806 }
00807
00808 case WID_AIC_LIST: {
00809 int y = r.top;
00810 for (int i = this->vscroll->GetPosition(); this->vscroll->IsVisible(i) && i < MAX_COMPANIES; i++) {
00811 StringID text;
00812
00813 if ((_game_mode != GM_NORMAL && i == 0) || (_game_mode == GM_NORMAL && Company::IsValidHumanID(i))) {
00814 text = STR_AI_CONFIG_HUMAN_PLAYER;
00815 } else if (AIConfig::GetConfig((CompanyID)i)->GetInfo() != NULL) {
00816 SetDParamStr(0, AIConfig::GetConfig((CompanyID)i)->GetInfo()->GetName());
00817 text = STR_JUST_RAW_STRING;
00818 } else {
00819 text = STR_AI_CONFIG_RANDOM_AI;
00820 }
00821 DrawString(r.left + 10, r.right - 10, y + WD_MATRIX_TOP, text,
00822 (this->selected_slot == i) ? TC_WHITE : (IsEditable((CompanyID)i) ? TC_ORANGE : TC_SILVER));
00823 y += this->line_height;
00824 }
00825 break;
00826 }
00827 }
00828 }
00829
00830 virtual void OnClick(Point pt, int widget, int click_count)
00831 {
00832 if (widget >= WID_AIC_TEXTFILE && widget < WID_AIC_TEXTFILE + TFT_END) {
00833 if (this->selected_slot == INVALID_COMPANY || GetConfig(this->selected_slot) == NULL) return;
00834
00835 ShowScriptTextfileWindow((TextfileType)(widget - WID_AIC_TEXTFILE), this->selected_slot);
00836 return;
00837 }
00838
00839 switch (widget) {
00840 case WID_AIC_DECREASE:
00841 case WID_AIC_INCREASE: {
00842 int new_value;
00843 if (widget == WID_AIC_DECREASE) {
00844 new_value = max(0, GetGameSettings().difficulty.max_no_competitors - 1);
00845 } else {
00846 new_value = min(MAX_COMPANIES - 1, GetGameSettings().difficulty.max_no_competitors + 1);
00847 }
00848 IConsoleSetSetting("difficulty.max_no_competitors", new_value);
00849 this->InvalidateData();
00850 break;
00851 }
00852
00853 case WID_AIC_GAMELIST: {
00854 this->selected_slot = OWNER_DEITY;
00855 this->InvalidateData();
00856 if (click_count > 1 && this->selected_slot != INVALID_COMPANY && _game_mode != GM_NORMAL) ShowAIListWindow((CompanyID)this->selected_slot);
00857 break;
00858 }
00859
00860 case WID_AIC_LIST: {
00861 this->selected_slot = (CompanyID)this->vscroll->GetScrolledRowFromWidget(pt.y, this, widget, 0, this->line_height);
00862 this->InvalidateData();
00863 if (click_count > 1 && this->selected_slot != INVALID_COMPANY) ShowAIListWindow((CompanyID)this->selected_slot);
00864 break;
00865 }
00866
00867 case WID_AIC_MOVE_UP:
00868 if (IsEditable(this->selected_slot) && IsEditable((CompanyID)(this->selected_slot - 1))) {
00869 Swap(GetGameSettings().ai_config[this->selected_slot], GetGameSettings().ai_config[this->selected_slot - 1]);
00870 this->selected_slot--;
00871 this->vscroll->ScrollTowards(this->selected_slot);
00872 this->InvalidateData();
00873 }
00874 break;
00875
00876 case WID_AIC_MOVE_DOWN:
00877 if (IsEditable(this->selected_slot) && IsEditable((CompanyID)(this->selected_slot + 1))) {
00878 Swap(GetGameSettings().ai_config[this->selected_slot], GetGameSettings().ai_config[this->selected_slot + 1]);
00879 this->selected_slot++;
00880 this->vscroll->ScrollTowards(this->selected_slot);
00881 this->InvalidateData();
00882 }
00883 break;
00884
00885 case WID_AIC_CHANGE:
00886 ShowAIListWindow((CompanyID)this->selected_slot);
00887 break;
00888
00889 case WID_AIC_CONFIGURE:
00890 ShowAISettingsWindow((CompanyID)this->selected_slot);
00891 break;
00892
00893 case WID_AIC_CLOSE:
00894 delete this;
00895 break;
00896
00897 case WID_AIC_CONTENT_DOWNLOAD:
00898 if (!_network_available) {
00899 ShowErrorMessage(STR_NETWORK_ERROR_NOTAVAILABLE, INVALID_STRING_ID, WL_ERROR);
00900 } else {
00901 #if defined(ENABLE_NETWORK)
00902 ShowNetworkContentListWindow(NULL, CONTENT_TYPE_AI);
00903 _network_content_client.RequestContentList(CONTENT_TYPE_GAME);
00904 #endif
00905 }
00906 break;
00907 }
00908 }
00909
00915 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
00916 {
00917 if (!IsEditable(this->selected_slot)) {
00918 this->selected_slot = INVALID_COMPANY;
00919 }
00920
00921 if (!gui_scope) return;
00922
00923 this->SetWidgetDisabledState(WID_AIC_DECREASE, GetGameSettings().difficulty.max_no_competitors == 0);
00924 this->SetWidgetDisabledState(WID_AIC_INCREASE, GetGameSettings().difficulty.max_no_competitors == MAX_COMPANIES - 1);
00925 this->SetWidgetDisabledState(WID_AIC_CHANGE, (this->selected_slot == OWNER_DEITY && _game_mode == GM_NORMAL) || this->selected_slot == INVALID_COMPANY);
00926 this->SetWidgetDisabledState(WID_AIC_CONFIGURE, this->selected_slot == INVALID_COMPANY || GetConfig(this->selected_slot)->GetConfigList()->size() == 0);
00927 this->SetWidgetDisabledState(WID_AIC_MOVE_UP, this->selected_slot == OWNER_DEITY || this->selected_slot == INVALID_COMPANY || !IsEditable((CompanyID)(this->selected_slot - 1)));
00928 this->SetWidgetDisabledState(WID_AIC_MOVE_DOWN, this->selected_slot == OWNER_DEITY || this->selected_slot == INVALID_COMPANY || !IsEditable((CompanyID)(this->selected_slot + 1)));
00929
00930 for (TextfileType tft = TFT_BEGIN; tft < TFT_END; tft++) {
00931 this->SetWidgetDisabledState(WID_AIC_TEXTFILE + tft, this->selected_slot == INVALID_COMPANY || (GetConfig(this->selected_slot)->GetTextfile(tft, this->selected_slot) == NULL));
00932 }
00933 }
00934 };
00935
00937 void ShowAIConfigWindow()
00938 {
00939 DeleteWindowByClass(WC_GAME_OPTIONS);
00940 new AIConfigWindow();
00941 }
00942
00951 static bool SetScriptButtonColour(NWidgetCore &button, bool dead, bool paused)
00952 {
00953
00954
00955 Colours colour = dead ? COLOUR_RED :
00956 (paused ? COLOUR_YELLOW : COLOUR_GREY);
00957 if (button.colour != colour) {
00958 button.colour = colour;
00959 return true;
00960 }
00961 return false;
00962 }
00963
00967 struct AIDebugWindow : public Window {
00968 static const int top_offset;
00969 static const int bottom_offset;
00970
00971 static const unsigned int MAX_BREAK_STR_STRING_LENGTH = 256;
00972
00973 static CompanyID ai_debug_company;
00974 int redraw_timer;
00975 int last_vscroll_pos;
00976 bool autoscroll;
00977 bool show_break_box;
00978 static bool break_check_enabled;
00979 static char break_string[MAX_BREAK_STR_STRING_LENGTH];
00980 QueryString break_editbox;
00981 static StringFilter break_string_filter;
00982 static bool case_sensitive_break_check;
00983 int highlight_row;
00984 Scrollbar *vscroll;
00985
00986 ScriptLog::LogData *GetLogPointer() const
00987 {
00988 if (ai_debug_company == OWNER_DEITY) return (ScriptLog::LogData *)Game::GetInstance()->GetLogPointer();
00989 return (ScriptLog::LogData *)Company::Get(ai_debug_company)->ai_instance->GetLogPointer();
00990 }
00991
00996 bool IsDead() const
00997 {
00998 if (ai_debug_company == OWNER_DEITY) {
00999 GameInstance *game = Game::GetInstance();
01000 return game == NULL || game->IsDead();
01001 }
01002 return !Company::IsValidAiID(ai_debug_company) || Company::Get(ai_debug_company)->ai_instance->IsDead();
01003 }
01004
01010 bool IsValidDebugCompany(CompanyID company) const
01011 {
01012 switch (company) {
01013 case INVALID_COMPANY: return false;
01014 case OWNER_DEITY: return Game::GetInstance() != NULL;
01015 default: return Company::IsValidAiID(company);
01016 }
01017 }
01018
01023 void SelectValidDebugCompany()
01024 {
01025
01026 if (this->IsValidDebugCompany(ai_debug_company)) return;
01027
01028 ai_debug_company = INVALID_COMPANY;
01029
01030 const Company *c;
01031 FOR_ALL_COMPANIES(c) {
01032 if (c->is_ai) {
01033 ChangeToAI(c->index);
01034 return;
01035 }
01036 }
01037
01038
01039 if (Game::GetInstance() != NULL) ChangeToAI(OWNER_DEITY);
01040 }
01041
01047 AIDebugWindow(WindowDesc *desc, WindowNumber number) : Window(desc), break_editbox(MAX_BREAK_STR_STRING_LENGTH)
01048 {
01049 this->CreateNestedTree();
01050 this->vscroll = this->GetScrollbar(WID_AID_SCROLLBAR);
01051 this->show_break_box = _settings_client.gui.ai_developer_tools;
01052 this->GetWidget<NWidgetStacked>(WID_AID_BREAK_STRING_WIDGETS)->SetDisplayedPlane(this->show_break_box ? 0 : SZSP_HORIZONTAL);
01053 this->FinishInitNested(number);
01054
01055 if (!this->show_break_box) break_check_enabled = false;
01056
01057 this->last_vscroll_pos = 0;
01058 this->autoscroll = true;
01059 this->highlight_row = -1;
01060
01061 this->querystrings[WID_AID_BREAK_STR_EDIT_BOX] = &this->break_editbox;
01062
01063 SetWidgetsDisabledState(!this->show_break_box, WID_AID_BREAK_STR_ON_OFF_BTN, WID_AID_BREAK_STR_EDIT_BOX, WID_AID_MATCH_CASE_BTN, WIDGET_LIST_END);
01064
01065
01066 this->break_editbox.text.Assign(this->break_string);
01067
01068 this->SelectValidDebugCompany();
01069 this->InvalidateData(-1);
01070 }
01071
01072 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
01073 {
01074 if (widget == WID_AID_LOG_PANEL) {
01075 resize->height = FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
01076 size->height = 14 * resize->height + this->top_offset + this->bottom_offset;
01077 }
01078 }
01079
01080 virtual void OnPaint()
01081 {
01082 this->SelectValidDebugCompany();
01083
01084
01085 this->DrawWidgets();
01086
01087 if (this->IsShaded()) return;
01088
01089 bool dirty = false;
01090
01091
01092 for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
01093 NWidgetCore *button = this->GetWidget<NWidgetCore>(i + WID_AID_COMPANY_BUTTON_START);
01094
01095 bool valid = Company::IsValidAiID(i);
01096
01097
01098 dirty |= (button->IsDisabled() == valid);
01099
01100
01101 bool dead = valid && Company::Get(i)->ai_instance->IsDead();
01102 bool paused = valid && Company::Get(i)->ai_instance->IsPaused();
01103
01104
01105 dirty |= SetScriptButtonColour(*button, dead, paused);
01106
01107
01108 if (!valid) continue;
01109
01110 byte offset = (i == ai_debug_company) ? 1 : 0;
01111 DrawCompanyIcon(i, button->pos_x + button->current_x / 2 - 7 + offset, this->GetWidget<NWidgetBase>(WID_AID_COMPANY_BUTTON_START + i)->pos_y + 2 + offset);
01112 }
01113
01114
01115 GameInstance *game = Game::GetInstance();
01116 bool valid = game != NULL;
01117 bool dead = valid && game->IsDead();
01118 bool paused = valid && game->IsPaused();
01119
01120 NWidgetCore *button = this->GetWidget<NWidgetCore>(WID_AID_SCRIPT_GAME);
01121 dirty |= (button->IsDisabled() == valid) || SetScriptButtonColour(*button, dead, paused);
01122
01123 if (dirty) this->InvalidateData(-1);
01124
01125
01126 if (ai_debug_company == INVALID_COMPANY) return;
01127
01128 ScriptLog::LogData *log = this->GetLogPointer();
01129
01130 int scroll_count = (log == NULL) ? 0 : log->used;
01131 if (this->vscroll->GetCount() != scroll_count) {
01132 this->vscroll->SetCount(scroll_count);
01133
01134
01135 this->SetWidgetDirty(WID_AID_SCROLLBAR);
01136 }
01137
01138 if (log == NULL) return;
01139
01140
01141
01142 if (this->last_vscroll_pos != this->vscroll->GetPosition()) {
01143 this->autoscroll = this->vscroll->GetPosition() >= log->used - this->vscroll->GetCapacity();
01144 }
01145 if (this->autoscroll) {
01146 int scroll_pos = max(0, log->used - this->vscroll->GetCapacity());
01147 if (scroll_pos != this->vscroll->GetPosition()) {
01148 this->vscroll->SetPosition(scroll_pos);
01149
01150
01151 this->SetWidgetDirty(WID_AID_SCROLLBAR);
01152 this->SetWidgetDirty(WID_AID_LOG_PANEL);
01153 }
01154 }
01155 this->last_vscroll_pos = this->vscroll->GetPosition();
01156 }
01157
01158 virtual void SetStringParameters(int widget) const
01159 {
01160 switch (widget) {
01161 case WID_AID_NAME_TEXT:
01162 if (ai_debug_company == OWNER_DEITY) {
01163 const GameInfo *info = Game::GetInfo();
01164 assert(info != NULL);
01165 SetDParam(0, STR_AI_DEBUG_NAME_AND_VERSION);
01166 SetDParamStr(1, info->GetName());
01167 SetDParam(2, info->GetVersion());
01168 } else if (ai_debug_company == INVALID_COMPANY || !Company::IsValidAiID(ai_debug_company)) {
01169 SetDParam(0, STR_EMPTY);
01170 } else {
01171 const AIInfo *info = Company::Get(ai_debug_company)->ai_info;
01172 assert(info != NULL);
01173 SetDParam(0, STR_AI_DEBUG_NAME_AND_VERSION);
01174 SetDParamStr(1, info->GetName());
01175 SetDParam(2, info->GetVersion());
01176 }
01177 break;
01178 }
01179 }
01180
01181 virtual void DrawWidget(const Rect &r, int widget) const
01182 {
01183 if (ai_debug_company == INVALID_COMPANY) return;
01184
01185 switch (widget) {
01186 case WID_AID_LOG_PANEL: {
01187 ScriptLog::LogData *log = this->GetLogPointer();
01188 if (log == NULL) return;
01189
01190 int y = this->top_offset;
01191 for (int i = this->vscroll->GetPosition(); this->vscroll->IsVisible(i) && i < log->used; i++) {
01192 int pos = (i + log->pos + 1 - log->used + log->count) % log->count;
01193 if (log->lines[pos] == NULL) break;
01194
01195 TextColour colour;
01196 switch (log->type[pos]) {
01197 case ScriptLog::LOG_SQ_INFO: colour = TC_BLACK; break;
01198 case ScriptLog::LOG_SQ_ERROR: colour = TC_RED; break;
01199 case ScriptLog::LOG_INFO: colour = TC_BLACK; break;
01200 case ScriptLog::LOG_WARNING: colour = TC_YELLOW; break;
01201 case ScriptLog::LOG_ERROR: colour = TC_RED; break;
01202 default: colour = TC_BLACK; break;
01203 }
01204
01205
01206 if (pos == this->highlight_row) {
01207 GfxFillRect(r.left + 1, r.top + y, r.right - 1, r.top + y + this->resize.step_height - WD_PAR_VSEP_NORMAL, PC_BLACK);
01208 if (colour == TC_BLACK) colour = TC_WHITE;
01209 }
01210
01211 DrawString(r.left + 7, r.right - 7, r.top + y, log->lines[pos], colour, SA_LEFT | SA_FORCE);
01212 y += this->resize.step_height;
01213 }
01214 break;
01215 }
01216 }
01217 }
01218
01223 void ChangeToAI(CompanyID show_ai)
01224 {
01225 if (!this->IsValidDebugCompany(show_ai)) return;
01226
01227 ai_debug_company = show_ai;
01228
01229 this->highlight_row = -1;
01230
01231
01232 DeleteWindowByClass(WC_AI_SETTINGS);
01233
01234 this->InvalidateData(-1);
01235
01236 this->autoscroll = true;
01237 this->last_vscroll_pos = this->vscroll->GetPosition();
01238 }
01239
01240 virtual void OnClick(Point pt, int widget, int click_count)
01241 {
01242
01243 if (this->IsWidgetDisabled(widget)) return;
01244
01245
01246 if (IsInsideMM(widget, WID_AID_COMPANY_BUTTON_START, WID_AID_COMPANY_BUTTON_END + 1)) {
01247 ChangeToAI((CompanyID)(widget - WID_AID_COMPANY_BUTTON_START));
01248 }
01249
01250 switch (widget) {
01251 case WID_AID_SCRIPT_GAME:
01252 ChangeToAI(OWNER_DEITY);
01253 break;
01254
01255 case WID_AID_RELOAD_TOGGLE:
01256 if (ai_debug_company == OWNER_DEITY) break;
01257
01258 DoCommandP(0, 2 | ai_debug_company << 16, CRR_MANUAL, CMD_COMPANY_CTRL);
01259 DoCommandP(0, 1 | ai_debug_company << 16, 0, CMD_COMPANY_CTRL);
01260 break;
01261
01262 case WID_AID_SETTINGS:
01263 ShowAISettingsWindow(ai_debug_company);
01264 break;
01265
01266 case WID_AID_BREAK_STR_ON_OFF_BTN:
01267 this->break_check_enabled = !this->break_check_enabled;
01268 this->InvalidateData(-1);
01269 break;
01270
01271 case WID_AID_MATCH_CASE_BTN:
01272 this->case_sensitive_break_check = !this->case_sensitive_break_check;
01273 this->InvalidateData(-1);
01274 break;
01275
01276 case WID_AID_CONTINUE_BTN:
01277
01278 if (!this->IsDead()) {
01279 if (ai_debug_company == OWNER_DEITY) {
01280 Game::Unpause();
01281 } else {
01282 AI::Unpause(ai_debug_company);
01283 }
01284 }
01285
01286
01287 if ((_pause_mode & PM_PAUSED_NORMAL) == PM_PAUSED_NORMAL) {
01288 bool all_unpaused = !Game::IsPaused();
01289 if (all_unpaused) {
01290 Company *c;
01291 FOR_ALL_COMPANIES(c) {
01292 if (c->is_ai && AI::IsPaused(c->index)) {
01293 all_unpaused = false;
01294 break;
01295 }
01296 }
01297 if (all_unpaused) {
01298
01299 DoCommandP(0, PM_PAUSED_NORMAL, 0, CMD_PAUSE);
01300 }
01301 }
01302 }
01303
01304 this->highlight_row = -1;
01305 this->InvalidateData(-1);
01306 break;
01307 }
01308 }
01309
01310 virtual void OnEditboxChanged(int wid)
01311 {
01312 if (wid == WID_AID_BREAK_STR_EDIT_BOX) {
01313
01314 strecpy(this->break_string, this->break_editbox.text.buf, lastof(this->break_string));
01315 break_string_filter.SetFilterTerm(this->break_string);
01316 }
01317 }
01318
01325 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
01326 {
01327
01328
01329 if (!gui_scope && data == ai_debug_company && this->IsValidDebugCompany(ai_debug_company) && this->break_check_enabled && !this->break_string_filter.IsEmpty()) {
01330
01331 ScriptLog::LogData *log = this->GetLogPointer();
01332
01333 if (log != NULL) {
01334 this->break_string_filter.ResetState();
01335 this->break_string_filter.AddLine(log->lines[log->pos]);
01336 if (this->break_string_filter.GetState()) {
01337
01338 if (!this->IsDead()) {
01339 if (ai_debug_company == OWNER_DEITY) {
01340 Game::Pause();
01341 } else {
01342 AI::Pause(ai_debug_company);
01343 }
01344 }
01345
01346
01347 if ((_pause_mode & PM_PAUSED_NORMAL) == PM_UNPAUSED) {
01348 DoCommandP(0, PM_PAUSED_NORMAL, 1, CMD_PAUSE);
01349 }
01350
01351
01352 this->highlight_row = log->pos;
01353 }
01354 }
01355 }
01356
01357 if (!gui_scope) return;
01358
01359 this->SelectValidDebugCompany();
01360
01361 ScriptLog::LogData *log = ai_debug_company != INVALID_COMPANY ? this->GetLogPointer() : NULL;
01362 this->vscroll->SetCount((log == NULL) ? 0 : log->used);
01363
01364
01365 for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
01366 this->SetWidgetDisabledState(i + WID_AID_COMPANY_BUTTON_START, !Company::IsValidAiID(i));
01367 this->SetWidgetLoweredState(i + WID_AID_COMPANY_BUTTON_START, ai_debug_company == i);
01368 }
01369
01370 this->SetWidgetDisabledState(WID_AID_SCRIPT_GAME, Game::GetGameInstance() == NULL);
01371 this->SetWidgetLoweredState(WID_AID_SCRIPT_GAME, ai_debug_company == OWNER_DEITY);
01372
01373 this->SetWidgetLoweredState(WID_AID_BREAK_STR_ON_OFF_BTN, this->break_check_enabled);
01374 this->SetWidgetLoweredState(WID_AID_MATCH_CASE_BTN, this->case_sensitive_break_check);
01375
01376 this->SetWidgetDisabledState(WID_AID_SETTINGS, ai_debug_company == INVALID_COMPANY);
01377 this->SetWidgetDisabledState(WID_AID_RELOAD_TOGGLE, ai_debug_company == INVALID_COMPANY || ai_debug_company == OWNER_DEITY);
01378 this->SetWidgetDisabledState(WID_AID_CONTINUE_BTN, ai_debug_company == INVALID_COMPANY ||
01379 (ai_debug_company == OWNER_DEITY ? !Game::IsPaused() : !AI::IsPaused(ai_debug_company)));
01380 }
01381
01382 virtual void OnResize()
01383 {
01384 this->vscroll->SetCapacityFromWidget(this, WID_AID_LOG_PANEL);
01385 }
01386
01387 static HotkeyList hotkeys;
01388 };
01389
01390 const int AIDebugWindow::top_offset = WD_FRAMERECT_TOP + 2;
01391 const int AIDebugWindow::bottom_offset = WD_FRAMERECT_BOTTOM;
01392 CompanyID AIDebugWindow::ai_debug_company = INVALID_COMPANY;
01393 char AIDebugWindow::break_string[MAX_BREAK_STR_STRING_LENGTH] = "";
01394 bool AIDebugWindow::break_check_enabled = true;
01395 bool AIDebugWindow::case_sensitive_break_check = false;
01396 StringFilter AIDebugWindow::break_string_filter(&AIDebugWindow::case_sensitive_break_check);
01397
01399 NWidgetBase *MakeCompanyButtonRowsAIDebug(int *biggest_index)
01400 {
01401 return MakeCompanyButtonRows(biggest_index, WID_AID_COMPANY_BUTTON_START, WID_AID_COMPANY_BUTTON_END, 8, STR_AI_DEBUG_SELECT_AI_TOOLTIP);
01402 }
01403
01409 static EventState AIDebugGlobalHotkeys(int hotkey)
01410 {
01411 if (_game_mode != GM_NORMAL) return ES_NOT_HANDLED;
01412 Window *w = ShowAIDebugWindow(INVALID_COMPANY);
01413 if (w == NULL) return ES_NOT_HANDLED;
01414 return w->OnHotkey(hotkey);
01415 }
01416
01417 static Hotkey aidebug_hotkeys[] = {
01418 Hotkey('1', "company_1", WID_AID_COMPANY_BUTTON_START),
01419 Hotkey('2', "company_2", WID_AID_COMPANY_BUTTON_START + 1),
01420 Hotkey('3', "company_3", WID_AID_COMPANY_BUTTON_START + 2),
01421 Hotkey('4', "company_4", WID_AID_COMPANY_BUTTON_START + 3),
01422 Hotkey('5', "company_5", WID_AID_COMPANY_BUTTON_START + 4),
01423 Hotkey('6', "company_6", WID_AID_COMPANY_BUTTON_START + 5),
01424 Hotkey('7', "company_7", WID_AID_COMPANY_BUTTON_START + 6),
01425 Hotkey('8', "company_8", WID_AID_COMPANY_BUTTON_START + 7),
01426 Hotkey('9', "company_9", WID_AID_COMPANY_BUTTON_START + 8),
01427 Hotkey((uint16)0, "company_10", WID_AID_COMPANY_BUTTON_START + 9),
01428 Hotkey((uint16)0, "company_11", WID_AID_COMPANY_BUTTON_START + 10),
01429 Hotkey((uint16)0, "company_12", WID_AID_COMPANY_BUTTON_START + 11),
01430 Hotkey((uint16)0, "company_13", WID_AID_COMPANY_BUTTON_START + 12),
01431 Hotkey((uint16)0, "company_14", WID_AID_COMPANY_BUTTON_START + 13),
01432 Hotkey((uint16)0, "company_15", WID_AID_COMPANY_BUTTON_START + 14),
01433 Hotkey('S', "settings", WID_AID_SETTINGS),
01434 Hotkey('0', "game_script", WID_AID_SCRIPT_GAME),
01435 Hotkey((uint16)0, "reload", WID_AID_RELOAD_TOGGLE),
01436 Hotkey('B', "break_toggle", WID_AID_BREAK_STR_ON_OFF_BTN),
01437 Hotkey('F', "break_string", WID_AID_BREAK_STR_EDIT_BOX),
01438 Hotkey('C', "match_case", WID_AID_MATCH_CASE_BTN),
01439 Hotkey(WKC_RETURN, "continue", WID_AID_CONTINUE_BTN),
01440 HOTKEY_LIST_END
01441 };
01442 HotkeyList AIDebugWindow::hotkeys("aidebug", aidebug_hotkeys, AIDebugGlobalHotkeys);
01443
01445 static const NWidgetPart _nested_ai_debug_widgets[] = {
01446 NWidget(NWID_HORIZONTAL),
01447 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
01448 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_AI_DEBUG, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
01449 NWidget(WWT_SHADEBOX, COLOUR_GREY),
01450 NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
01451 NWidget(WWT_STICKYBOX, COLOUR_GREY),
01452 EndContainer(),
01453 NWidget(WWT_PANEL, COLOUR_GREY, WID_AID_VIEW),
01454 NWidgetFunction(MakeCompanyButtonRowsAIDebug), SetPadding(0, 2, 1, 2),
01455 EndContainer(),
01456 NWidget(NWID_HORIZONTAL),
01457 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),
01458 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_AID_NAME_TEXT), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_JUST_STRING, STR_AI_DEBUG_NAME_TOOLTIP),
01459 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_AID_SETTINGS), SetMinimalSize(100, 20), SetDataTip(STR_AI_DEBUG_SETTINGS, STR_AI_DEBUG_SETTINGS_TOOLTIP),
01460 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_AID_RELOAD_TOGGLE), SetMinimalSize(100, 20), SetDataTip(STR_AI_DEBUG_RELOAD, STR_AI_DEBUG_RELOAD_TOOLTIP),
01461 EndContainer(),
01462 NWidget(NWID_HORIZONTAL),
01463 NWidget(NWID_VERTICAL),
01464
01465 NWidget(WWT_PANEL, COLOUR_GREY, WID_AID_LOG_PANEL), SetMinimalSize(287, 180), SetResize(1, 1), SetScrollbar(WID_AID_SCROLLBAR),
01466 EndContainer(),
01467
01468 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_AID_BREAK_STRING_WIDGETS),
01469 NWidget(NWID_HORIZONTAL),
01470 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),
01471 NWidget(WWT_PANEL, COLOUR_GREY),
01472 NWidget(NWID_HORIZONTAL),
01473 NWidget(WWT_LABEL, COLOUR_GREY), SetPadding(2, 2, 2, 4), SetDataTip(STR_AI_DEBUG_BREAK_ON_LABEL, 0x0),
01474 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),
01475 EndContainer(),
01476 EndContainer(),
01477 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),
01478 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),
01479 EndContainer(),
01480 EndContainer(),
01481 EndContainer(),
01482 NWidget(NWID_VERTICAL),
01483 NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_AID_SCROLLBAR),
01484 NWidget(WWT_RESIZEBOX, COLOUR_GREY),
01485 EndContainer(),
01486 EndContainer(),
01487 };
01488
01490 static WindowDesc _ai_debug_desc(
01491 WDP_AUTO, "script_debug", 600, 450,
01492 WC_AI_DEBUG, WC_NONE,
01493 0,
01494 _nested_ai_debug_widgets, lengthof(_nested_ai_debug_widgets),
01495 &AIDebugWindow::hotkeys
01496 );
01497
01502 Window *ShowAIDebugWindow(CompanyID show_company)
01503 {
01504 if (!_networking || _network_server) {
01505 AIDebugWindow *w = (AIDebugWindow *)BringWindowToFrontById(WC_AI_DEBUG, 0);
01506 if (w == NULL) w = new AIDebugWindow(&_ai_debug_desc, 0);
01507 if (show_company != INVALID_COMPANY) w->ChangeToAI(show_company);
01508 return w;
01509 } else {
01510 ShowErrorMessage(STR_ERROR_AI_DEBUG_SERVER_ONLY, INVALID_STRING_ID, WL_INFO);
01511 }
01512
01513 return NULL;
01514 }
01515
01519 void InitializeAIGui()
01520 {
01521 AIDebugWindow::ai_debug_company = INVALID_COMPANY;
01522 }
01523
01525 void ShowAIDebugWindowIfAIError()
01526 {
01527
01528 if (_networking && !_network_server) return;
01529
01530 Company *c;
01531 FOR_ALL_COMPANIES(c) {
01532 if (c->is_ai && c->ai_instance->IsDead()) {
01533 ShowAIDebugWindow(c->index);
01534 break;
01535 }
01536 }
01537
01538 GameInstance *g = Game::GetGameInstance();
01539 if (g != NULL && g->IsDead()) {
01540 ShowAIDebugWindow(OWNER_DEITY);
01541 }
01542 }