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_base.h"
00018 #include "../company_gui.h"
00019 #include "../strings_func.h"
00020 #include "../window_func.h"
00021 #include "../gfx_func.h"
00022 #include "../command_func.h"
00023 #include "../network/network.h"
00024 #include "../settings_func.h"
00025 #include "../network/network_content.h"
00026 #include "../textfile_gui.h"
00027
00028 #include "ai.hpp"
00029 #include "../script/api/script_log.hpp"
00030 #include "ai_config.hpp"
00031 #include "ai_info.hpp"
00032 #include "ai_instance.hpp"
00033 #include "../game/game.hpp"
00034 #include "../game/game_config.hpp"
00035 #include "../game/game_info.hpp"
00036 #include "../game/game_instance.hpp"
00037
00038
00039 #include "table/strings.h"
00040
00041 #include <vector>
00042
00043 static ScriptConfig *GetConfig(CompanyID slot)
00044 {
00045 if (slot == OWNER_DEITY) return GameConfig::GetConfig();
00046 return AIConfig::GetConfig(slot);
00047 }
00048
00052 struct AIListWindow : public Window {
00053 const ScriptInfoList *info_list;
00054 int selected;
00055 CompanyID slot;
00056 int line_height;
00057 Scrollbar *vscroll;
00058
00064 AIListWindow(const WindowDesc *desc, CompanyID slot) : Window(),
00065 slot(slot)
00066 {
00067 if (slot == OWNER_DEITY) {
00068 this->info_list = Game::GetUniqueInfoList();
00069 } else {
00070 this->info_list = AI::GetUniqueInfoList();
00071 }
00072
00073 this->CreateNestedTree(desc);
00074 this->vscroll = this->GetScrollbar(WID_AIL_SCROLLBAR);
00075 this->FinishInitNested(desc);
00076
00077 this->vscroll->SetCount((int)this->info_list->size() + 1);
00078
00079
00080 this->selected = -1;
00081 if (GetConfig(slot)->HasScript()) {
00082 ScriptInfo *info = GetConfig(slot)->GetInfo();
00083 int i = 0;
00084 for (ScriptInfoList::const_iterator it = this->info_list->begin(); it != this->info_list->end(); it++, i++) {
00085 if ((*it).second == info) {
00086 this->selected = i;
00087 break;
00088 }
00089 }
00090 }
00091 }
00092
00093 virtual void SetStringParameters(int widget) const
00094 {
00095 switch (widget) {
00096 case WID_AIL_CAPTION:
00097 SetDParam(0, (this->slot == OWNER_DEITY) ? STR_AI_LIST_CAPTION_GAMESCRIPT : STR_AI_LIST_CAPTION_AI);
00098 break;
00099 }
00100 }
00101
00102 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00103 {
00104 if (widget == WID_AIL_LIST) {
00105 this->line_height = FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM;
00106
00107 resize->width = 1;
00108 resize->height = this->line_height;
00109 size->height = GB(this->GetWidget<NWidgetCore>(widget)->widget_data, MAT_ROW_START, MAT_ROW_BITS) * this->line_height;
00110 }
00111 }
00112
00113 virtual void DrawWidget(const Rect &r, int widget) const
00114 {
00115 switch (widget) {
00116 case WID_AIL_LIST: {
00117
00118 int y = this->GetWidget<NWidgetBase>(WID_AIL_LIST)->pos_y;
00119
00120 if (this->vscroll->IsVisible(0)) {
00121 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);
00122 y += this->line_height;
00123 }
00124 ScriptInfoList::const_iterator it = this->info_list->begin();
00125 for (int i = 1; it != this->info_list->end(); i++, it++) {
00126 if (this->vscroll->IsVisible(i)) {
00127 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);
00128 y += this->line_height;
00129 }
00130 }
00131 break;
00132 }
00133 case WID_AIL_INFO_BG: {
00134 AIInfo *selected_info = NULL;
00135 ScriptInfoList::const_iterator it = this->info_list->begin();
00136 for (int i = 1; selected_info == NULL && it != this->info_list->end(); i++, it++) {
00137 if (this->selected == i - 1) selected_info = static_cast<AIInfo *>((*it).second);
00138 }
00139
00140 if (selected_info != NULL) {
00141 int y = r.top + WD_FRAMERECT_TOP;
00142 SetDParamStr(0, selected_info->GetAuthor());
00143 DrawString(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, y, STR_AI_LIST_AUTHOR);
00144 y += FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
00145 SetDParam(0, selected_info->GetVersion());
00146 DrawString(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, y, STR_AI_LIST_VERSION);
00147 y += FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
00148 if (selected_info->GetURL() != NULL) {
00149 SetDParamStr(0, selected_info->GetURL());
00150 DrawString(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, y, STR_AI_LIST_URL);
00151 y += FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
00152 }
00153 SetDParamStr(0, selected_info->GetDescription());
00154 DrawStringMultiLine(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, y, r.bottom - WD_FRAMERECT_BOTTOM, STR_JUST_RAW_STRING, TC_WHITE);
00155 }
00156 break;
00157 }
00158 }
00159 }
00160
00164 void ChangeAI()
00165 {
00166 if (this->selected == -1) {
00167 GetConfig(slot)->Change(NULL);
00168 } else {
00169 ScriptInfoList::const_iterator it = this->info_list->begin();
00170 for (int i = 0; i < this->selected; i++) it++;
00171 GetConfig(slot)->Change((*it).second->GetName(), (*it).second->GetVersion());
00172 }
00173 InvalidateWindowData(WC_GAME_OPTIONS, WN_GAME_OPTIONS_AI);
00174 InvalidateWindowClassesData(WC_AI_SETTINGS);
00175 DeleteWindowByClass(WC_QUERY_STRING);
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 DrawBoolButton(buttons_left, y + 2, current_value != 0, editable);
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 if (this->clicked_row != num) {
00435 DeleteChildWindows(WC_QUERY_STRING);
00436 this->clicked_row = num;
00437 }
00438
00439 bool bool_item = (config_item.flags & SCRIPTCONFIG_BOOLEAN) != 0;
00440
00441 int x = pt.x - wid->pos_x;
00442 if (_current_text_dir == TD_RTL) x = wid->current_x - x;
00443 x -= 4;
00444
00445
00446 int old_val = this->ai_config->GetSetting(config_item.name);
00447 if (IsInsideMM(x, 0, 21)) {
00448 int new_val = old_val;
00449 if (bool_item) {
00450 new_val = !new_val;
00451 } else if (x >= 10) {
00452
00453 new_val += config_item.step_size;
00454 if (new_val > config_item.max_value) new_val = config_item.max_value;
00455 this->clicked_increase = true;
00456 } else {
00457
00458 new_val -= config_item.step_size;
00459 if (new_val < config_item.min_value) new_val = config_item.min_value;
00460 this->clicked_increase = false;
00461 }
00462
00463 if (new_val != old_val) {
00464 this->ai_config->SetSetting(config_item.name, new_val);
00465 this->clicked_button = num;
00466 this->timeout = 5;
00467
00468 this->CheckDifficultyLevel();
00469 }
00470 } else if (!bool_item) {
00471
00472 SetDParam(0, old_val);
00473 ShowQueryString(STR_JUST_INT, STR_CONFIG_SETTING_QUERY_CAPTION, 10, this, CS_NUMERAL, QSF_NONE);
00474 }
00475 this->SetDirty();
00476 break;
00477 }
00478
00479 case WID_AIS_ACCEPT:
00480 delete this;
00481 break;
00482
00483 case WID_AIS_RESET:
00484 if (_game_mode == GM_MENU || !Company::IsValidID(this->slot)) {
00485 this->ai_config->ResetSettings();
00486 this->SetDirty();
00487 }
00488 break;
00489 }
00490 }
00491
00492 virtual void OnQueryTextFinished(char *str)
00493 {
00494 if (StrEmpty(str)) return;
00495 ScriptConfigItemList::const_iterator it = this->ai_config->GetConfigList()->begin();
00496 for (int i = 0; i < this->clicked_row; i++) it++;
00497 if (_game_mode == GM_NORMAL && ((this->slot == OWNER_DEITY) || Company::IsValidID(this->slot)) && (it->flags & SCRIPTCONFIG_INGAME) == 0) return;
00498 int32 value = atoi(str);
00499 this->ai_config->SetSetting((*it).name, value);
00500 this->CheckDifficultyLevel();
00501 this->SetDirty();
00502 }
00503
00504 virtual void OnResize()
00505 {
00506 NWidgetCore *nwi = this->GetWidget<NWidgetCore>(WID_AIS_BACKGROUND);
00507 this->vscroll->SetCapacity(nwi->current_y / this->line_height);
00508 nwi->widget_data = (this->vscroll->GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
00509 }
00510
00511 virtual void OnTick()
00512 {
00513 if (--this->timeout == 0) {
00514 this->clicked_button = -1;
00515 this->SetDirty();
00516 }
00517 }
00518
00524 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
00525 {
00526 this->RebuildVisibleSettings();
00527 }
00528 };
00529
00531 static const NWidgetPart _nested_ai_settings_widgets[] = {
00532 NWidget(NWID_HORIZONTAL),
00533 NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
00534 NWidget(WWT_CAPTION, COLOUR_MAUVE, WID_AIS_CAPTION), SetDataTip(STR_AI_SETTINGS_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00535 EndContainer(),
00536 NWidget(NWID_HORIZONTAL),
00537 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),
00538 NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, WID_AIS_SCROLLBAR),
00539 EndContainer(),
00540 NWidget(NWID_HORIZONTAL),
00541 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
00542 NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, WID_AIS_ACCEPT), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_AI_SETTINGS_CLOSE, STR_NULL),
00543 NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, WID_AIS_RESET), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_AI_SETTINGS_RESET, STR_NULL),
00544 EndContainer(),
00545 NWidget(WWT_RESIZEBOX, COLOUR_MAUVE),
00546 EndContainer(),
00547 };
00548
00550 static const WindowDesc _ai_settings_desc(
00551 WDP_CENTER, 500, 208,
00552 WC_AI_SETTINGS, WC_NONE,
00553 WDF_UNCLICK_BUTTONS,
00554 _nested_ai_settings_widgets, lengthof(_nested_ai_settings_widgets)
00555 );
00556
00561 static void ShowAISettingsWindow(CompanyID slot)
00562 {
00563 DeleteWindowByClass(WC_AI_LIST);
00564 DeleteWindowByClass(WC_AI_SETTINGS);
00565 new AISettingsWindow(&_ai_settings_desc, slot);
00566 }
00567
00568
00570 struct ScriptTextfileWindow : public TextfileWindow {
00571 CompanyID slot;
00572
00573 ScriptTextfileWindow(TextfileType file_type, CompanyID slot) : TextfileWindow(file_type), slot(slot)
00574 {
00575 this->GetWidget<NWidgetCore>(WID_TF_CAPTION)->SetDataTip(STR_TEXTFILE_README_CAPTION + file_type, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS);
00576
00577 const char *textfile = GetConfig(slot)->GetTextfile(file_type, slot);
00578 this->LoadTextfile(textfile, (slot == OWNER_DEITY) ? GAME_DIR : AI_DIR);
00579 }
00580
00581 void SetStringParameters(int widget) const
00582 {
00583 if (widget == WID_TF_CAPTION) {
00584 SetDParam(0, (slot == OWNER_DEITY) ? STR_CONTENT_TYPE_GAME_SCRIPT : STR_CONTENT_TYPE_AI);
00585 SetDParamStr(1, GetConfig(slot)->GetName());
00586 }
00587 }
00588 };
00589
00595 void ShowScriptTextfileWindow(TextfileType file_type, CompanyID slot)
00596 {
00597 DeleteWindowByClass(WC_TEXTFILE);
00598 new ScriptTextfileWindow(file_type, slot);
00599 }
00600
00601
00603 static const NWidgetPart _nested_ai_config_widgets[] = {
00604 NWidget(NWID_HORIZONTAL),
00605 NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
00606 NWidget(WWT_CAPTION, COLOUR_MAUVE), SetDataTip(STR_AI_CONFIG_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00607 EndContainer(),
00608 NWidget(WWT_PANEL, COLOUR_MAUVE, WID_AIC_BACKGROUND),
00609 NWidget(NWID_VERTICAL), SetPIP(4, 4, 4),
00610 NWidget(NWID_HORIZONTAL), SetPIP(7, 0, 7),
00611 NWidget(WWT_PUSHARROWBTN, COLOUR_YELLOW, WID_AIC_DECREASE), SetFill(0, 1), SetDataTip(AWV_DECREASE, STR_NULL),
00612 NWidget(WWT_PUSHARROWBTN, COLOUR_YELLOW, WID_AIC_INCREASE), SetFill(0, 1), SetDataTip(AWV_INCREASE, STR_NULL),
00613 NWidget(NWID_SPACER), SetMinimalSize(6, 0),
00614 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),
00615 EndContainer(),
00616 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(7, 0, 7),
00617 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),
00618 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),
00619 EndContainer(),
00620 EndContainer(),
00621 NWidget(WWT_FRAME, COLOUR_MAUVE), SetDataTip(STR_AI_CONFIG_AI, STR_NULL), SetPadding(0, 5, 0, 5),
00622 NWidget(NWID_HORIZONTAL),
00623 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),
00624 NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, WID_AIC_SCROLLBAR),
00625 EndContainer(),
00626 EndContainer(),
00627 NWidget(NWID_SPACER), SetMinimalSize(0, 9),
00628 NWidget(WWT_FRAME, COLOUR_MAUVE), SetDataTip(STR_AI_CONFIG_GAMESCRIPT, STR_NULL), SetPadding(0, 5, 4, 5),
00629 NWidget(WWT_MATRIX, COLOUR_MAUVE, WID_AIC_GAMELIST), SetMinimalSize(288, 14), SetFill(1, 0), SetDataTip(0x101, STR_AI_CONFIG_GAMELIST_TOOLTIP),
00630 EndContainer(),
00631 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(7, 0, 7),
00632 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_AIC_CHANGE), SetFill(1, 0), SetMinimalSize(93, 12), SetDataTip(STR_AI_CONFIG_CHANGE, STR_AI_CONFIG_CHANGE_TOOLTIP),
00633 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_AIC_CONFIGURE), SetFill(1, 0), SetMinimalSize(93, 12), SetDataTip(STR_AI_CONFIG_CONFIGURE, STR_AI_CONFIG_CONFIGURE_TOOLTIP),
00634 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_AIC_CLOSE), SetFill(1, 0), SetMinimalSize(93, 12), SetDataTip(STR_AI_SETTINGS_CLOSE, STR_NULL),
00635 EndContainer(),
00636 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(7, 0, 7),
00637 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_AIC_TEXTFILE + TFT_README), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_TEXTFILE_VIEW_README, STR_NULL),
00638 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_AIC_TEXTFILE + TFT_CHANGELOG), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_TEXTFILE_VIEW_CHANGELOG, STR_NULL),
00639 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_AIC_TEXTFILE + TFT_LICENSE), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_TEXTFILE_VIEW_LICENCE, STR_NULL),
00640 EndContainer(),
00641 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),
00642 EndContainer(),
00643 };
00644
00646 static const WindowDesc _ai_config_desc(
00647 WDP_CENTER, 0, 0,
00648 WC_GAME_OPTIONS, WC_NONE,
00649 WDF_UNCLICK_BUTTONS,
00650 _nested_ai_config_widgets, lengthof(_nested_ai_config_widgets)
00651 );
00652
00656 struct AIConfigWindow : public Window {
00657 CompanyID selected_slot;
00658 int line_height;
00659 Scrollbar *vscroll;
00660
00661 AIConfigWindow() : Window()
00662 {
00663 this->InitNested(&_ai_config_desc, WN_GAME_OPTIONS_AI);
00664 this->vscroll = this->GetScrollbar(WID_AIC_SCROLLBAR);
00665 this->selected_slot = INVALID_COMPANY;
00666 NWidgetCore *nwi = this->GetWidget<NWidgetCore>(WID_AIC_LIST);
00667 this->vscroll->SetCapacity(nwi->current_y / this->line_height);
00668 this->vscroll->SetCount(MAX_COMPANIES);
00669 nwi->widget_data = (this->vscroll->GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
00670 this->OnInvalidateData(0);
00671 }
00672
00673 ~AIConfigWindow()
00674 {
00675 DeleteWindowByClass(WC_AI_LIST);
00676 DeleteWindowByClass(WC_AI_SETTINGS);
00677 }
00678
00679 virtual void SetStringParameters(int widget) const
00680 {
00681 switch (widget) {
00682 case WID_AIC_NUMBER:
00683 SetDParam(0, GetGameSettings().difficulty.max_no_competitors);
00684 break;
00685 case WID_AIC_CHANGE:
00686 switch (selected_slot) {
00687 case OWNER_DEITY:
00688 SetDParam(0, STR_AI_CONFIG_CHANGE_GAMESCRIPT);
00689 break;
00690
00691 case INVALID_COMPANY:
00692 SetDParam(0, STR_AI_CONFIG_CHANGE_NONE);
00693 break;
00694
00695 default:
00696 SetDParam(0, STR_AI_CONFIG_CHANGE_AI);
00697 break;
00698 }
00699 break;
00700 }
00701 }
00702
00703 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00704 {
00705 switch (widget) {
00706 case WID_AIC_GAMELIST:
00707 case WID_AIC_LIST:
00708 this->line_height = FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM;
00709 size->height = GB(this->GetWidget<NWidgetCore>(widget)->widget_data, MAT_ROW_START, MAT_ROW_BITS) * this->line_height;
00710 break;
00711 }
00712 }
00713
00719 static bool IsEditable(CompanyID slot)
00720 {
00721 if (slot == OWNER_DEITY) return _game_mode != GM_NORMAL;
00722
00723 if (_game_mode != GM_NORMAL) {
00724 return slot > 0 && slot <= GetGameSettings().difficulty.max_no_competitors;
00725 }
00726 if (Company::IsValidID(slot) || slot < 0) return false;
00727
00728 int max_slot = GetGameSettings().difficulty.max_no_competitors;
00729 for (CompanyID cid = COMPANY_FIRST; cid < (CompanyID)max_slot && cid < MAX_COMPANIES; cid++) {
00730 if (Company::IsValidHumanID(cid)) max_slot++;
00731 }
00732 return slot < max_slot;
00733 }
00734
00735 virtual void DrawWidget(const Rect &r, int widget) const
00736 {
00737 switch (widget) {
00738 case WID_AIC_GAMELIST: {
00739 StringID text = STR_AI_CONFIG_NONE;
00740
00741 if (GameConfig::GetConfig()->GetInfo() != NULL) {
00742 SetDParamStr(0, GameConfig::GetConfig()->GetInfo()->GetName());
00743 text = STR_JUST_RAW_STRING;
00744 }
00745
00746 DrawString(r.left + 10, r.right - 10, r.top + WD_MATRIX_TOP, text,
00747 (this->selected_slot == OWNER_DEITY) ? TC_WHITE : (IsEditable(OWNER_DEITY) ? TC_ORANGE : TC_SILVER));
00748
00749 break;
00750 }
00751
00752 case WID_AIC_LIST: {
00753 int y = r.top;
00754 for (int i = this->vscroll->GetPosition(); this->vscroll->IsVisible(i) && i < MAX_COMPANIES; i++) {
00755 StringID text;
00756
00757 if ((_game_mode != GM_NORMAL && i == 0) || (_game_mode == GM_NORMAL && Company::IsValidHumanID(i))) {
00758 text = STR_AI_CONFIG_HUMAN_PLAYER;
00759 } else if (AIConfig::GetConfig((CompanyID)i)->GetInfo() != NULL) {
00760 SetDParamStr(0, AIConfig::GetConfig((CompanyID)i)->GetInfo()->GetName());
00761 text = STR_JUST_RAW_STRING;
00762 } else {
00763 text = STR_AI_CONFIG_RANDOM_AI;
00764 }
00765 DrawString(r.left + 10, r.right - 10, y + WD_MATRIX_TOP, text,
00766 (this->selected_slot == i) ? TC_WHITE : (IsEditable((CompanyID)i) ? TC_ORANGE : TC_SILVER));
00767 y += this->line_height;
00768 }
00769 break;
00770 }
00771 }
00772 }
00773
00774 virtual void OnClick(Point pt, int widget, int click_count)
00775 {
00776 if (widget >= WID_AIC_TEXTFILE && widget < WID_AIC_TEXTFILE + TFT_END) {
00777 if (this->selected_slot == INVALID_COMPANY || GetConfig(this->selected_slot) == NULL) return;
00778
00779 ShowScriptTextfileWindow((TextfileType)(widget - WID_AIC_TEXTFILE), this->selected_slot);
00780 return;
00781 }
00782
00783 switch (widget) {
00784 case WID_AIC_DECREASE:
00785 case WID_AIC_INCREASE: {
00786 int new_value;
00787 if (widget == WID_AIC_DECREASE) {
00788 new_value = max(0, GetGameSettings().difficulty.max_no_competitors - 1);
00789 } else {
00790 new_value = min(MAX_COMPANIES - 1, GetGameSettings().difficulty.max_no_competitors + 1);
00791 }
00792 IConsoleSetSetting("difficulty.max_no_competitors", new_value);
00793 this->InvalidateData();
00794 break;
00795 }
00796
00797 case WID_AIC_GAMELIST: {
00798 this->selected_slot = OWNER_DEITY;
00799 this->InvalidateData();
00800 if (click_count > 1 && this->selected_slot != INVALID_COMPANY) ShowAIListWindow((CompanyID)this->selected_slot);
00801 break;
00802 }
00803
00804 case WID_AIC_LIST: {
00805 this->selected_slot = (CompanyID)this->vscroll->GetScrolledRowFromWidget(pt.y, this, widget, 0, this->line_height);
00806 this->InvalidateData();
00807 if (click_count > 1 && this->selected_slot != INVALID_COMPANY) ShowAIListWindow((CompanyID)this->selected_slot);
00808 break;
00809 }
00810
00811 case WID_AIC_MOVE_UP:
00812 if (IsEditable(this->selected_slot) && IsEditable((CompanyID)(this->selected_slot - 1))) {
00813 Swap(GetGameSettings().ai_config[this->selected_slot], GetGameSettings().ai_config[this->selected_slot - 1]);
00814 this->selected_slot--;
00815 this->vscroll->ScrollTowards(this->selected_slot);
00816 this->InvalidateData();
00817 }
00818 break;
00819
00820 case WID_AIC_MOVE_DOWN:
00821 if (IsEditable(this->selected_slot) && IsEditable((CompanyID)(this->selected_slot + 1))) {
00822 Swap(GetGameSettings().ai_config[this->selected_slot], GetGameSettings().ai_config[this->selected_slot + 1]);
00823 this->selected_slot++;
00824 this->vscroll->ScrollTowards(this->selected_slot);
00825 this->InvalidateData();
00826 }
00827 break;
00828
00829 case WID_AIC_CHANGE:
00830 ShowAIListWindow((CompanyID)this->selected_slot);
00831 break;
00832
00833 case WID_AIC_CONFIGURE:
00834 ShowAISettingsWindow((CompanyID)this->selected_slot);
00835 break;
00836
00837 case WID_AIC_CLOSE:
00838 delete this;
00839 break;
00840
00841 case WID_AIC_CONTENT_DOWNLOAD:
00842 if (!_network_available) {
00843 ShowErrorMessage(STR_NETWORK_ERROR_NOTAVAILABLE, INVALID_STRING_ID, WL_ERROR);
00844 } else {
00845 #if defined(ENABLE_NETWORK)
00846 ShowNetworkContentListWindow(NULL, CONTENT_TYPE_AI);
00847 _network_content_client.RequestContentList(CONTENT_TYPE_GAME);
00848 #endif
00849 }
00850 break;
00851 }
00852 }
00853
00859 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
00860 {
00861 if (!IsEditable(this->selected_slot)) {
00862 this->selected_slot = INVALID_COMPANY;
00863 }
00864
00865 if (!gui_scope) return;
00866
00867 this->SetWidgetDisabledState(WID_AIC_DECREASE, GetGameSettings().difficulty.max_no_competitors == 0);
00868 this->SetWidgetDisabledState(WID_AIC_INCREASE, GetGameSettings().difficulty.max_no_competitors == MAX_COMPANIES - 1);
00869 this->SetWidgetDisabledState(WID_AIC_CHANGE, this->selected_slot == INVALID_COMPANY);
00870 this->SetWidgetDisabledState(WID_AIC_CONFIGURE, this->selected_slot == INVALID_COMPANY || GetConfig(this->selected_slot)->GetConfigList()->size() == 0);
00871 this->SetWidgetDisabledState(WID_AIC_MOVE_UP, this->selected_slot == OWNER_DEITY || this->selected_slot == INVALID_COMPANY || !IsEditable((CompanyID)(this->selected_slot - 1)));
00872 this->SetWidgetDisabledState(WID_AIC_MOVE_DOWN, this->selected_slot == OWNER_DEITY || this->selected_slot == INVALID_COMPANY || !IsEditable((CompanyID)(this->selected_slot + 1)));
00873
00874 for (TextfileType tft = TFT_BEGIN; tft < TFT_END; tft++) {
00875 this->SetWidgetDisabledState(WID_AIC_TEXTFILE + tft, this->selected_slot == INVALID_COMPANY || (GetConfig(this->selected_slot)->GetTextfile(tft, this->selected_slot) == NULL));
00876 }
00877 }
00878 };
00879
00881 void ShowAIConfigWindow()
00882 {
00883 DeleteWindowByClass(WC_GAME_OPTIONS);
00884 new AIConfigWindow();
00885 }
00886
00890 struct AIDebugWindow : public QueryStringBaseWindow {
00891 static const int top_offset;
00892 static const int bottom_offset;
00893
00894 static const unsigned int MAX_BREAK_STR_STRING_LENGTH = 256;
00895
00896 static CompanyID ai_debug_company;
00897 int redraw_timer;
00898 int last_vscroll_pos;
00899 bool autoscroll;
00900 bool show_break_box;
00901 static bool break_check_enabled;
00902 static char break_string[MAX_BREAK_STR_STRING_LENGTH];
00903 static bool case_sensitive_break_check;
00904 int highlight_row;
00905 Scrollbar *vscroll;
00906
00907 ScriptLog::LogData *GetLogPointer() const
00908 {
00909 if (ai_debug_company == OWNER_DEITY) return (ScriptLog::LogData *)Game::GetInstance()->GetLogPointer();
00910 return (ScriptLog::LogData *)Company::Get(ai_debug_company)->ai_instance->GetLogPointer();
00911 }
00912
00918 AIDebugWindow(const WindowDesc *desc, WindowNumber number) : QueryStringBaseWindow(MAX_BREAK_STR_STRING_LENGTH)
00919 {
00920 this->CreateNestedTree(desc);
00921 this->vscroll = this->GetScrollbar(WID_AID_SCROLLBAR);
00922 this->show_break_box = _settings_client.gui.ai_developer_tools;
00923 this->GetWidget<NWidgetStacked>(WID_AID_BREAK_STRING_WIDGETS)->SetDisplayedPlane(this->show_break_box ? 0 : SZSP_HORIZONTAL);
00924 this->FinishInitNested(desc, number);
00925
00926 if (!this->show_break_box) break_check_enabled = false;
00927
00928 for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
00929 this->SetWidgetDisabledState(i + WID_AID_COMPANY_BUTTON_START, !Company::IsValidAiID(i));
00930 }
00931 this->EnableWidget(WID_AID_SCRIPT_GAME);
00932 this->DisableWidget(WID_AID_RELOAD_TOGGLE);
00933 this->DisableWidget(WID_AID_SETTINGS);
00934 this->DisableWidget(WID_AID_CONTINUE_BTN);
00935
00936 this->last_vscroll_pos = 0;
00937 this->autoscroll = true;
00938 this->highlight_row = -1;
00939 InitializeTextBuffer(&this->text, this->edit_str_buf, this->edit_str_size, MAX_BREAK_STR_STRING_LENGTH);
00940
00941
00942 strecpy(this->edit_str_buf, this->break_string, this->edit_str_buf + MAX_BREAK_STR_STRING_LENGTH);
00943 UpdateTextBufferSize(&this->text);
00944
00945
00946 if (ai_debug_company == OWNER_DEITY) {
00947 this->LowerWidget(WID_AID_SCRIPT_GAME);
00948 } else if (ai_debug_company != INVALID_COMPANY) {
00949 this->LowerWidget(ai_debug_company + WID_AID_COMPANY_BUTTON_START);
00950 }
00951 this->SetWidgetLoweredState(WID_AID_BREAK_STR_ON_OFF_BTN, this->break_check_enabled);
00952 this->SetWidgetLoweredState(WID_AID_MATCH_CASE_BTN, this->case_sensitive_break_check);
00953
00954 }
00955
00956 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00957 {
00958 if (widget == WID_AID_LOG_PANEL) {
00959 resize->height = FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
00960 size->height = 14 * resize->height + this->top_offset + this->bottom_offset;
00961 }
00962 }
00963
00964 virtual void OnPaint()
00965 {
00966
00967 if (ai_debug_company == INVALID_COMPANY || (ai_debug_company != OWNER_DEITY && !Company::IsValidAiID(ai_debug_company))) {
00968 if (ai_debug_company != INVALID_COMPANY) {
00969
00970 this->RaiseWidget(ai_debug_company + WID_AID_COMPANY_BUTTON_START);
00971
00972 ai_debug_company = INVALID_COMPANY;
00973 }
00974
00975 const Company *c;
00976 FOR_ALL_COMPANIES(c) {
00977 if (c->is_ai) {
00978
00979 this->LowerWidget(c->index + WID_AID_COMPANY_BUTTON_START);
00980
00981 ai_debug_company = c->index;
00982 break;
00983 }
00984 }
00985 }
00986
00987
00988 this->SetWidgetDisabledState(WID_AID_SETTINGS, ai_debug_company == INVALID_COMPANY);
00989 this->SetWidgetDisabledState(WID_AID_RELOAD_TOGGLE, ai_debug_company == INVALID_COMPANY || ai_debug_company == OWNER_DEITY);
00990 this->SetWidgetDisabledState(WID_AID_SCRIPT_GAME, Game::GetGameInstance() == NULL);
00991
00992
00993 this->DrawWidgets();
00994
00995 if (this->IsShaded()) return;
00996
00997 if (this->show_break_box) this->DrawEditBox(WID_AID_BREAK_STR_EDIT_BOX);
00998
00999
01000 for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
01001 NWidgetCore *button = this->GetWidget<NWidgetCore>(i + WID_AID_COMPANY_BUTTON_START);
01002 bool dirty = false;
01003
01004 bool valid = Company::IsValidAiID(i);
01005 bool disabled = !valid;
01006 if (button->IsDisabled() != disabled) {
01007
01008 button->SetDisabled(disabled);
01009 dirty = true;
01010 }
01011
01012 bool dead = valid && Company::Get(i)->ai_instance->IsDead();
01013 Colours colour = dead ? COLOUR_RED : COLOUR_GREY;
01014 if (button->colour != colour) {
01015
01016 button->colour = colour;
01017 dirty = true;
01018 }
01019
01020
01021 if (dirty) this->SetDirty();
01022
01023 if (!valid) continue;
01024
01025 byte offset = (i == ai_debug_company) ? 1 : 0;
01026 DrawCompanyIcon(i, button->pos_x + button->current_x / 2 - 7 + offset, this->GetWidget<NWidgetBase>(WID_AID_COMPANY_BUTTON_START + i)->pos_y + 2 + offset);
01027 }
01028
01029
01030 if (ai_debug_company == INVALID_COMPANY) return;
01031
01032 ScriptLog::LogData *log = this->GetLogPointer();
01033
01034 int scroll_count = (log == NULL) ? 0 : log->used;
01035 if (this->vscroll->GetCount() != scroll_count) {
01036 this->vscroll->SetCount(scroll_count);
01037
01038
01039 this->SetWidgetDirty(WID_AID_SCROLLBAR);
01040 }
01041
01042 if (log == NULL) return;
01043
01044
01045
01046 if (this->last_vscroll_pos != this->vscroll->GetPosition()) {
01047 this->autoscroll = this->vscroll->GetPosition() >= log->used - this->vscroll->GetCapacity();
01048 }
01049 if (this->autoscroll) {
01050 int scroll_pos = max(0, log->used - this->vscroll->GetCapacity());
01051 if (scroll_pos != this->vscroll->GetPosition()) {
01052 this->vscroll->SetPosition(scroll_pos);
01053
01054
01055 this->SetWidgetDirty(WID_AID_SCROLLBAR);
01056 this->SetWidgetDirty(WID_AID_LOG_PANEL);
01057 }
01058 }
01059 this->last_vscroll_pos = this->vscroll->GetPosition();
01060 }
01061
01062 virtual void SetStringParameters(int widget) const
01063 {
01064 switch (widget) {
01065 case WID_AID_NAME_TEXT:
01066 if (ai_debug_company == OWNER_DEITY) {
01067 const GameInfo *info = Game::GetInfo();
01068 assert(info != NULL);
01069 SetDParam(0, STR_AI_DEBUG_NAME_AND_VERSION);
01070 SetDParamStr(1, info->GetName());
01071 SetDParam(2, info->GetVersion());
01072 } else if (ai_debug_company == INVALID_COMPANY || !Company::IsValidAiID(ai_debug_company)) {
01073 SetDParam(0, STR_EMPTY);
01074 } else {
01075 const AIInfo *info = Company::Get(ai_debug_company)->ai_info;
01076 assert(info != NULL);
01077 SetDParam(0, STR_AI_DEBUG_NAME_AND_VERSION);
01078 SetDParamStr(1, info->GetName());
01079 SetDParam(2, info->GetVersion());
01080 }
01081 break;
01082 }
01083 }
01084
01085 virtual void DrawWidget(const Rect &r, int widget) const
01086 {
01087 if (ai_debug_company == INVALID_COMPANY) return;
01088
01089 switch (widget) {
01090 case WID_AID_LOG_PANEL: {
01091 ScriptLog::LogData *log = this->GetLogPointer();
01092 if (log == NULL) return;
01093
01094 int y = this->top_offset;
01095 for (int i = this->vscroll->GetPosition(); this->vscroll->IsVisible(i) && i < log->used; i++) {
01096 int pos = (i + log->pos + 1 - log->used + log->count) % log->count;
01097 if (log->lines[pos] == NULL) break;
01098
01099 TextColour colour;
01100 switch (log->type[pos]) {
01101 case ScriptLog::LOG_SQ_INFO: colour = TC_BLACK; break;
01102 case ScriptLog::LOG_SQ_ERROR: colour = TC_RED; break;
01103 case ScriptLog::LOG_INFO: colour = TC_BLACK; break;
01104 case ScriptLog::LOG_WARNING: colour = TC_YELLOW; break;
01105 case ScriptLog::LOG_ERROR: colour = TC_RED; break;
01106 default: colour = TC_BLACK; break;
01107 }
01108
01109
01110 if (pos == this->highlight_row) {
01111 GfxFillRect(r.left + 1, r.top + y, r.right - 1, r.top + y + this->resize.step_height - WD_PAR_VSEP_NORMAL, PC_BLACK);
01112 if (colour == TC_BLACK) colour = TC_WHITE;
01113 }
01114
01115 DrawString(r.left + 7, r.right - 7, r.top + y, log->lines[pos], colour, SA_LEFT | SA_FORCE);
01116 y += this->resize.step_height;
01117 }
01118 break;
01119 }
01120 }
01121 }
01122
01127 void ChangeToAI(CompanyID show_ai)
01128 {
01129 if (ai_debug_company == OWNER_DEITY) {
01130 this->RaiseWidget(WID_AID_SCRIPT_GAME);
01131 } else {
01132 this->RaiseWidget(ai_debug_company + WID_AID_COMPANY_BUTTON_START);
01133 }
01134 ai_debug_company = show_ai;
01135
01136 ScriptLog::LogData *log = this->GetLogPointer();
01137 this->vscroll->SetCount((log == NULL) ? 0 : log->used);
01138
01139 if (ai_debug_company == OWNER_DEITY) {
01140 this->LowerWidget(WID_AID_SCRIPT_GAME);
01141 } else {
01142 this->LowerWidget(ai_debug_company + WID_AID_COMPANY_BUTTON_START);
01143 }
01144
01145 this->autoscroll = true;
01146 this->last_vscroll_pos = this->vscroll->GetPosition();
01147 this->SetDirty();
01148
01149 DeleteWindowByClass(WC_AI_SETTINGS);
01150 }
01151
01152 virtual void OnClick(Point pt, int widget, int click_count)
01153 {
01154
01155 if (IsInsideMM(widget, WID_AID_COMPANY_BUTTON_START, WID_AID_COMPANY_BUTTON_END + 1)) {
01156
01157 if (!this->IsWidgetDisabled(widget)) {
01158 ChangeToAI((CompanyID)(widget - WID_AID_COMPANY_BUTTON_START));
01159 }
01160 }
01161
01162 switch (widget) {
01163 case WID_AID_SCRIPT_GAME:
01164 ChangeToAI(OWNER_DEITY);
01165 break;
01166
01167 case WID_AID_RELOAD_TOGGLE:
01168 if (ai_debug_company == OWNER_DEITY) break;
01169
01170 DoCommandP(0, 2 | ai_debug_company << 16, CRR_MANUAL, CMD_COMPANY_CTRL);
01171 DoCommandP(0, 1 | ai_debug_company << 16, 0, CMD_COMPANY_CTRL);
01172 break;
01173
01174 case WID_AID_SETTINGS:
01175 ShowAISettingsWindow(ai_debug_company);
01176 break;
01177
01178 case WID_AID_BREAK_STR_ON_OFF_BTN:
01179 this->break_check_enabled = !this->break_check_enabled;
01180 this->SetWidgetLoweredState(WID_AID_BREAK_STR_ON_OFF_BTN, this->break_check_enabled);
01181 this->SetWidgetDirty(WID_AID_BREAK_STR_ON_OFF_BTN);
01182 break;
01183
01184 case WID_AID_MATCH_CASE_BTN:
01185 this->case_sensitive_break_check = !this->case_sensitive_break_check;
01186 this->SetWidgetLoweredState(WID_AID_MATCH_CASE_BTN, this->case_sensitive_break_check);
01187 break;
01188
01189 case WID_AID_CONTINUE_BTN:
01190
01191 DoCommandP(0, PM_PAUSED_NORMAL, 0, CMD_PAUSE);
01192 this->DisableWidget(WID_AID_CONTINUE_BTN);
01193 this->RaiseWidget(WID_AID_CONTINUE_BTN);
01194 break;
01195 }
01196 }
01197
01198 virtual void OnTimeout()
01199 {
01200 this->RaiseWidget(WID_AID_RELOAD_TOGGLE);
01201 this->RaiseWidget(WID_AID_SETTINGS);
01202 this->SetDirty();
01203 }
01204
01205 virtual void OnMouseLoop()
01206 {
01207 this->HandleEditBox(WID_AID_BREAK_STR_EDIT_BOX);
01208 }
01209
01210 virtual EventState OnKeyPress(uint16 key, uint16 keycode)
01211 {
01212 EventState state = ES_NOT_HANDLED;
01213 if (this->HandleEditBoxKey(WID_AID_BREAK_STR_EDIT_BOX, key, keycode, state) != HEBR_NOT_FOCUSED) {
01214
01215 strecpy(this->break_string, this->edit_str_buf, lastof(this->break_string));
01216 }
01217 return state;
01218 }
01219
01225 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
01226 {
01227 if (data == -1 || ai_debug_company == data) this->SetDirty();
01228
01229 if (gui_scope && data == -2) {
01230
01231
01232
01233 if ((_pause_mode & PM_PAUSED_NORMAL) == PM_UNPAUSED && this->highlight_row != -1) {
01234 this->DisableWidget(WID_AID_CONTINUE_BTN);
01235 this->SetWidgetDirty(WID_AID_CONTINUE_BTN);
01236 this->SetWidgetDirty(WID_AID_LOG_PANEL);
01237 this->highlight_row = -1;
01238 }
01239 }
01240
01241
01242
01243 if (ai_debug_company != OWNER_DEITY && !gui_scope && data == ai_debug_company && this->break_check_enabled && !StrEmpty(this->edit_str_buf)) {
01244
01245 ScriptLog::LogData *log = this->GetLogPointer();
01246
01247 if (log != NULL && case_sensitive_break_check?
01248 strstr(log->lines[log->pos], this->edit_str_buf) != 0 :
01249 strcasestr(log->lines[log->pos], this->edit_str_buf) != 0) {
01250
01251 AI::Suspend(ai_debug_company);
01252 if ((_pause_mode & PM_PAUSED_NORMAL) == PM_UNPAUSED) {
01253 DoCommandP(0, PM_PAUSED_NORMAL, 1, CMD_PAUSE);
01254 }
01255
01256
01257 this->EnableWidget(WID_AID_CONTINUE_BTN);
01258 this->SetWidgetDirty(WID_AID_CONTINUE_BTN);
01259
01260
01261 this->highlight_row = log->pos;
01262 }
01263 }
01264 }
01265
01266 virtual void OnResize()
01267 {
01268 this->vscroll->SetCapacityFromWidget(this, WID_AID_LOG_PANEL);
01269 }
01270 };
01271
01272 const int AIDebugWindow::top_offset = WD_FRAMERECT_TOP + 2;
01273 const int AIDebugWindow::bottom_offset = WD_FRAMERECT_BOTTOM;
01274 CompanyID AIDebugWindow::ai_debug_company = INVALID_COMPANY;
01275 char AIDebugWindow::break_string[MAX_BREAK_STR_STRING_LENGTH] = "";
01276 bool AIDebugWindow::break_check_enabled = true;
01277 bool AIDebugWindow::case_sensitive_break_check = false;
01278
01280 NWidgetBase *MakeCompanyButtonRowsAIDebug(int *biggest_index)
01281 {
01282 return MakeCompanyButtonRows(biggest_index, WID_AID_COMPANY_BUTTON_START, WID_AID_COMPANY_BUTTON_END, 8, STR_AI_DEBUG_SELECT_AI_TOOLTIP);
01283 }
01284
01286 static const NWidgetPart _nested_ai_debug_widgets[] = {
01287 NWidget(NWID_HORIZONTAL),
01288 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
01289 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_AI_DEBUG, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
01290 NWidget(WWT_SHADEBOX, COLOUR_GREY),
01291 NWidget(WWT_STICKYBOX, COLOUR_GREY),
01292 EndContainer(),
01293 NWidget(WWT_PANEL, COLOUR_GREY, WID_AID_VIEW),
01294 NWidgetFunction(MakeCompanyButtonRowsAIDebug), SetPadding(0, 2, 1, 2),
01295 EndContainer(),
01296 NWidget(NWID_HORIZONTAL),
01297 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),
01298 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_AID_NAME_TEXT), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_JUST_STRING, STR_AI_DEBUG_NAME_TOOLTIP),
01299 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_AID_SETTINGS), SetMinimalSize(100, 20), SetDataTip(STR_AI_DEBUG_SETTINGS, STR_AI_DEBUG_SETTINGS_TOOLTIP),
01300 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_AID_RELOAD_TOGGLE), SetMinimalSize(100, 20), SetDataTip(STR_AI_DEBUG_RELOAD, STR_AI_DEBUG_RELOAD_TOOLTIP),
01301 EndContainer(),
01302 NWidget(NWID_HORIZONTAL),
01303 NWidget(NWID_VERTICAL),
01304
01305 NWidget(WWT_PANEL, COLOUR_GREY, WID_AID_LOG_PANEL), SetMinimalSize(287, 180), SetResize(1, 1), SetScrollbar(WID_AID_SCROLLBAR),
01306 EndContainer(),
01307
01308 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_AID_BREAK_STRING_WIDGETS),
01309 NWidget(NWID_HORIZONTAL),
01310 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),
01311 NWidget(WWT_PANEL, COLOUR_GREY),
01312 NWidget(NWID_HORIZONTAL),
01313 NWidget(WWT_LABEL, COLOUR_GREY), SetPadding(2, 2, 2, 4), SetDataTip(STR_AI_DEBUG_BREAK_ON_LABEL, 0x0),
01314 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),
01315 EndContainer(),
01316 EndContainer(),
01317 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),
01318 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),
01319 EndContainer(),
01320 EndContainer(),
01321 EndContainer(),
01322 NWidget(NWID_VERTICAL),
01323 NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_AID_SCROLLBAR),
01324 NWidget(WWT_RESIZEBOX, COLOUR_GREY),
01325 EndContainer(),
01326 EndContainer(),
01327 };
01328
01330 static const WindowDesc _ai_debug_desc(
01331 WDP_AUTO, 600, 450,
01332 WC_AI_DEBUG, WC_NONE,
01333 0,
01334 _nested_ai_debug_widgets, lengthof(_nested_ai_debug_widgets)
01335 );
01336
01341 void ShowAIDebugWindow(CompanyID show_company)
01342 {
01343 if (!_networking || _network_server) {
01344 AIDebugWindow *w = (AIDebugWindow *)BringWindowToFrontById(WC_AI_DEBUG, 0);
01345 if (w == NULL) w = new AIDebugWindow(&_ai_debug_desc, 0);
01346 if (show_company != INVALID_COMPANY) w->ChangeToAI(show_company);
01347 } else {
01348 ShowErrorMessage(STR_ERROR_AI_DEBUG_SERVER_ONLY, INVALID_STRING_ID, WL_INFO);
01349 }
01350 }
01351
01355 void InitializeAIGui()
01356 {
01357 AIDebugWindow::ai_debug_company = INVALID_COMPANY;
01358 }
01359
01361 void ShowAIDebugWindowIfAIError()
01362 {
01363
01364 if (_networking && !_network_server) return;
01365
01366 Company *c;
01367 FOR_ALL_COMPANIES(c) {
01368 if (c->is_ai && c->ai_instance->IsDead()) {
01369 ShowAIDebugWindow(c->index);
01370 break;
01371 }
01372 }
01373
01374 GameInstance *g = Game::GetGameInstance();
01375 if (g != NULL && g->IsDead()) {
01376 ShowAIDebugWindow(OWNER_DEITY);
01377 }
01378 }