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