diff --git a/src/ai/ai.hpp b/src/ai/ai.hpp index 1e2e99d22f..e090303bd9 100644 --- a/src/ai/ai.hpp +++ b/src/ai/ai.hpp @@ -13,10 +13,6 @@ #include "../script/api/script_event_types.hpp" #include "../core/string_compare_type.hpp" #include "ai_scanner.hpp" -#include - -/** A list that maps AI names to their AIInfo object. */ -typedef std::map ScriptInfoList; /** * Main AI class. Contains all functions needed to start, stop, save and load AIs. diff --git a/src/ai/ai_core.cpp b/src/ai/ai_core.cpp index 64a16758d1..220e7bdbd2 100644 --- a/src/ai/ai_core.cpp +++ b/src/ai/ai_core.cpp @@ -62,7 +62,7 @@ cur_company.Restore(); - InvalidateWindowData(WC_AI_DEBUG, 0, -1); + InvalidateWindowData(WC_SCRIPT_DEBUG, 0, -1); return; } @@ -113,8 +113,8 @@ cur_company.Restore(); - InvalidateWindowData(WC_AI_DEBUG, 0, -1); - CloseWindowById(WC_AI_SETTINGS, company); + InvalidateWindowData(WC_SCRIPT_DEBUG, 0, -1); + CloseWindowById(WC_SCRIPT_SETTINGS, company); } /* static */ void AI::Pause(CompanyID company) @@ -338,9 +338,9 @@ AI::scanner_library->RescanDir(); ResetConfig(); - InvalidateWindowData(WC_AI_LIST, 0, 1); - SetWindowClassesDirty(WC_AI_DEBUG); - InvalidateWindowClassesData(WC_AI_SETTINGS); + InvalidateWindowData(WC_SCRIPT_LIST, 0, 1); + SetWindowClassesDirty(WC_SCRIPT_DEBUG); + InvalidateWindowClassesData(WC_SCRIPT_SETTINGS); } /** diff --git a/src/ai/ai_gui.cpp b/src/ai/ai_gui.cpp index c012809c72..da60bb9f98 100644 --- a/src/ai/ai_gui.cpp +++ b/src/ai/ai_gui.cpp @@ -8,675 +8,23 @@ /** @file ai_gui.cpp %Window for configuring the AIs */ #include "../stdafx.h" -#include "../table/sprites.h" #include "../error.h" -#include "../settings_gui.h" -#include "../querystring_gui.h" -#include "../stringfilter_type.h" #include "../company_base.h" -#include "../company_gui.h" -#include "../strings_func.h" #include "../window_func.h" -#include "../gfx_func.h" -#include "../command_func.h" #include "../network/network.h" #include "../settings_func.h" #include "../network/network_content.h" -#include "../textfile_gui.h" -#include "../widgets/dropdown_type.h" -#include "../widgets/dropdown_func.h" -#include "../hotkeys.h" #include "../core/geometry_func.hpp" -#include "../guitimer_func.h" -#include "../company_cmd.h" -#include "../misc_cmd.h" #include "ai.hpp" #include "ai_gui.hpp" -#include "../script/api/script_log.hpp" #include "ai_config.hpp" #include "ai_info.hpp" -#include "ai_instance.hpp" -#include "../game/game.hpp" -#include "../game/game_config.hpp" -#include "../game/game_info.hpp" -#include "../game/game_instance.hpp" - +#include "../script/script_gui.h" #include "table/strings.h" -#include - #include "../safeguards.h" -static ScriptConfig *GetConfig(CompanyID slot) -{ - if (slot == OWNER_DEITY) return GameConfig::GetConfig(); - return AIConfig::GetConfig(slot); -} - -/** - * Window that let you choose an available AI. - */ -struct AIListWindow : public Window { - const ScriptInfoList *info_list; ///< The list of Scripts. - int selected; ///< The currently selected Script. - CompanyID slot; ///< The company we're selecting a new Script for. - int line_height; ///< Height of a row in the matrix widget. - Scrollbar *vscroll; ///< Cache of the vertical scrollbar. - - /** - * Constructor for the window. - * @param desc The description of the window. - * @param slot The company we're changing the AI for. - */ - AIListWindow(WindowDesc *desc, CompanyID slot) : Window(desc), - slot(slot) - { - if (slot == OWNER_DEITY) { - this->info_list = Game::GetUniqueInfoList(); - } else { - this->info_list = AI::GetUniqueInfoList(); - } - - this->CreateNestedTree(); - this->vscroll = this->GetScrollbar(WID_AIL_SCROLLBAR); - this->FinishInitNested(); // Initializes 'this->line_height' as side effect. - - this->vscroll->SetCount((int)this->info_list->size() + 1); - - /* Try if we can find the currently selected AI */ - this->selected = -1; - if (GetConfig(slot)->HasScript()) { - ScriptInfo *info = GetConfig(slot)->GetInfo(); - int i = 0; - for (const auto &item : *this->info_list) { - if (item.second == info) { - this->selected = i; - break; - } - - i++; - } - } - } - - void SetStringParameters(int widget) const override - { - switch (widget) { - case WID_AIL_CAPTION: - SetDParam(0, (this->slot == OWNER_DEITY) ? STR_AI_LIST_CAPTION_GAMESCRIPT : STR_AI_LIST_CAPTION_AI); - break; - } - } - - void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override - { - if (widget == WID_AIL_LIST) { - this->line_height = FONT_HEIGHT_NORMAL + padding.height; - - resize->width = 1; - resize->height = this->line_height; - size->height = 5 * this->line_height; - } - } - - void DrawWidget(const Rect &r, int widget) const override - { - switch (widget) { - case WID_AIL_LIST: { - /* Draw a list of all available AIs. */ - Rect tr = r.Shrink(WidgetDimensions::scaled.matrix); - /* First AI in the list is hardcoded to random */ - if (this->vscroll->IsVisible(0)) { - DrawString(tr, this->slot == OWNER_DEITY ? STR_AI_CONFIG_NONE : STR_AI_CONFIG_RANDOM_AI, this->selected == -1 ? TC_WHITE : TC_ORANGE); - tr.top += this->line_height; - } - int i = 0; - for (const auto &item : *this->info_list) { - i++; - if (this->vscroll->IsVisible(i)) { - DrawString(tr, item.second->GetName(), (this->selected == i - 1) ? TC_WHITE : TC_ORANGE); - tr.top += this->line_height; - } - } - break; - } - case WID_AIL_INFO_BG: { - ScriptInfo *selected_info = nullptr; - int i = 0; - for (const auto &item : *this->info_list) { - i++; - if (this->selected == i - 1) selected_info = static_cast(item.second); - } - /* Some info about the currently selected AI. */ - if (selected_info != nullptr) { - Rect tr = r.Shrink(WidgetDimensions::scaled.frametext, WidgetDimensions::scaled.framerect); - SetDParamStr(0, selected_info->GetAuthor()); - DrawString(tr, STR_AI_LIST_AUTHOR); - tr.top += FONT_HEIGHT_NORMAL + WidgetDimensions::scaled.vsep_normal; - SetDParam(0, selected_info->GetVersion()); - DrawString(tr, STR_AI_LIST_VERSION); - tr.top += FONT_HEIGHT_NORMAL + WidgetDimensions::scaled.vsep_normal; - if (selected_info->GetURL() != nullptr) { - SetDParamStr(0, selected_info->GetURL()); - DrawString(tr, STR_AI_LIST_URL); - tr.top += FONT_HEIGHT_NORMAL + WidgetDimensions::scaled.vsep_normal; - } - SetDParamStr(0, selected_info->GetDescription()); - DrawStringMultiLine(tr, STR_JUST_RAW_STRING, TC_WHITE); - } - break; - } - } - } - - /** - * Changes the AI of the current slot. - */ - void ChangeAI() - { - if (this->selected == -1) { - GetConfig(slot)->Change(nullptr); - } else { - ScriptInfoList::const_iterator it = this->info_list->begin(); - for (int i = 0; i < this->selected; i++) it++; - GetConfig(slot)->Change((*it).second->GetName(), (*it).second->GetVersion()); - } - InvalidateWindowData(WC_GAME_OPTIONS, slot == OWNER_DEITY ? WN_GAME_OPTIONS_GS : WN_GAME_OPTIONS_AI); - InvalidateWindowClassesData(WC_AI_SETTINGS); - CloseWindowByClass(WC_QUERY_STRING); - InvalidateWindowClassesData(WC_TEXTFILE); - } - - void OnClick(Point pt, int widget, int click_count) override - { - switch (widget) { - case WID_AIL_LIST: { // Select one of the AIs - int sel = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_AIL_LIST) - 1; - if (sel < (int)this->info_list->size()) { - this->selected = sel; - this->SetDirty(); - if (click_count > 1) { - this->ChangeAI(); - this->Close(); - } - } - break; - } - - case WID_AIL_ACCEPT: { - this->ChangeAI(); - this->Close(); - break; - } - - case WID_AIL_CANCEL: - this->Close(); - break; - } - } - - void OnResize() override - { - this->vscroll->SetCapacityFromWidget(this, WID_AIL_LIST); - } - - /** - * Some data on this window has become invalid. - * @param data Information about the changed data. - * @param gui_scope Whether the call is done from GUI scope. You may not do everything when not in GUI scope. See #InvalidateWindowData() for details. - */ - void OnInvalidateData(int data = 0, bool gui_scope = true) override - { - if (_game_mode == GM_NORMAL && Company::IsValidID(this->slot)) { - this->Close(); - return; - } - - if (!gui_scope) return; - - this->vscroll->SetCount((int)this->info_list->size() + 1); - - /* selected goes from -1 .. length of ai list - 1. */ - this->selected = std::min(this->selected, this->vscroll->GetCount() - 2); - } -}; - -/** Widgets for the AI list window. */ -static const NWidgetPart _nested_ai_list_widgets[] = { - NWidget(NWID_HORIZONTAL), - NWidget(WWT_CLOSEBOX, COLOUR_MAUVE), - NWidget(WWT_CAPTION, COLOUR_MAUVE, WID_AIL_CAPTION), SetDataTip(STR_AI_LIST_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS), - NWidget(WWT_DEFSIZEBOX, COLOUR_MAUVE), - EndContainer(), - NWidget(NWID_HORIZONTAL), - NWidget(WWT_MATRIX, COLOUR_MAUVE, WID_AIL_LIST), SetMinimalSize(188, 112), SetFill(1, 1), SetResize(1, 1), SetMatrixDataTip(1, 0, STR_AI_LIST_TOOLTIP), SetScrollbar(WID_AIL_SCROLLBAR), - NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, WID_AIL_SCROLLBAR), - EndContainer(), - NWidget(WWT_PANEL, COLOUR_MAUVE, WID_AIL_INFO_BG), SetMinimalTextLines(8, WidgetDimensions::unscaled.framerect.Vertical() + WidgetDimensions::unscaled.vsep_normal * 3), SetResize(1, 0), - EndContainer(), - NWidget(NWID_HORIZONTAL), - NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), - NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, WID_AIL_ACCEPT), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_AI_LIST_ACCEPT, STR_AI_LIST_ACCEPT_TOOLTIP), - NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, WID_AIL_CANCEL), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_AI_LIST_CANCEL, STR_AI_LIST_CANCEL_TOOLTIP), - EndContainer(), - NWidget(WWT_RESIZEBOX, COLOUR_MAUVE), - EndContainer(), -}; - -/** Window definition for the ai list window. */ -static WindowDesc _ai_list_desc( - WDP_CENTER, "settings_script_list", 200, 234, - WC_AI_LIST, WC_NONE, - 0, - _nested_ai_list_widgets, lengthof(_nested_ai_list_widgets) -); - -/** - * Open the AI list window to chose an AI for the given company slot. - * @param slot The slot to change the AI of. - */ -void ShowAIListWindow(CompanyID slot) -{ - CloseWindowByClass(WC_AI_LIST); - new AIListWindow(&_ai_list_desc, slot); -} - -/** - * Window for settings the parameters of an AI. - */ -struct AISettingsWindow : public Window { - CompanyID slot; ///< The currently show company's setting. - ScriptConfig *ai_config; ///< The configuration we're modifying. - int clicked_button; ///< The button we clicked. - bool clicked_increase; ///< Whether we clicked the increase or decrease button. - bool clicked_dropdown; ///< Whether the dropdown is open. - bool closing_dropdown; ///< True, if the dropdown list is currently closing. - GUITimer timeout; ///< Timeout for unclicking the button. - int clicked_row; ///< The clicked row of settings. - int line_height; ///< Height of a row in the matrix widget. - Scrollbar *vscroll; ///< Cache of the vertical scrollbar. - typedef std::vector VisibleSettingsList; ///< typdef for a vector of script settings - VisibleSettingsList visible_settings; ///< List of visible AI settings - - /** - * Constructor for the window. - * @param desc The description of the window. - * @param slot The company we're changing the settings for. - */ - AISettingsWindow(WindowDesc *desc, CompanyID slot) : Window(desc), - slot(slot), - clicked_button(-1), - clicked_dropdown(false), - closing_dropdown(false), - timeout(0) - { - this->ai_config = GetConfig(slot); - - this->CreateNestedTree(); - this->vscroll = this->GetScrollbar(WID_AIS_SCROLLBAR); - this->FinishInitNested(slot); // Initializes 'this->line_height' as side effect. - - this->RebuildVisibleSettings(); - } - - /** - * Rebuilds the list of visible settings. AI settings with the flag - * AICONFIG_AI_DEVELOPER set will only be visible if the game setting - * gui.ai_developer_tools is enabled. - */ - void RebuildVisibleSettings() - { - visible_settings.clear(); - - for (const auto &item : *this->ai_config->GetConfigList()) { - bool no_hide = (item.flags & SCRIPTCONFIG_DEVELOPER) == 0; - if (no_hide || _settings_client.gui.ai_developer_tools) { - visible_settings.push_back(&item); - } - } - - this->vscroll->SetCount((int)this->visible_settings.size()); - } - - void SetStringParameters(int widget) const override - { - switch (widget) { - case WID_AIS_CAPTION: - SetDParam(0, (this->slot == OWNER_DEITY) ? STR_AI_SETTINGS_CAPTION_GAMESCRIPT : STR_AI_SETTINGS_CAPTION_AI); - break; - } - } - - void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override - { - if (widget == WID_AIS_BACKGROUND) { - this->line_height = std::max(SETTING_BUTTON_HEIGHT, FONT_HEIGHT_NORMAL) + padding.height; - - resize->width = 1; - resize->height = this->line_height; - size->height = 5 * this->line_height; - } - } - - void DrawWidget(const Rect &r, int widget) const override - { - if (widget != WID_AIS_BACKGROUND) return; - - ScriptConfig *config = this->ai_config; - VisibleSettingsList::const_iterator it = this->visible_settings.begin(); - int i = 0; - for (; !this->vscroll->IsVisible(i); i++) it++; - - Rect ir = r.Shrink(WidgetDimensions::scaled.framerect); - bool rtl = _current_text_dir == TD_RTL; - Rect br = ir.WithWidth(SETTING_BUTTON_WIDTH, rtl); - Rect tr = ir.Indent(SETTING_BUTTON_WIDTH + WidgetDimensions::scaled.hsep_wide, rtl); - - int y = r.top; - int button_y_offset = (this->line_height - SETTING_BUTTON_HEIGHT) / 2; - int text_y_offset = (this->line_height - FONT_HEIGHT_NORMAL) / 2; - for (; this->vscroll->IsVisible(i) && it != visible_settings.end(); i++, it++) { - const ScriptConfigItem &config_item = **it; - int current_value = config->GetSetting((config_item).name); - bool editable = this->IsEditableItem(config_item); - - StringID str; - TextColour colour; - uint idx = 0; - if (StrEmpty(config_item.description)) { - if (this->slot != OWNER_DEITY && !strcmp(config_item.name, "start_date")) { - /* Build-in translation */ - str = STR_AI_SETTINGS_START_DELAY; - colour = TC_LIGHT_BLUE; - } else { - str = STR_JUST_STRING; - colour = TC_ORANGE; - } - } else { - str = STR_AI_SETTINGS_SETTING; - colour = TC_LIGHT_BLUE; - SetDParamStr(idx++, config_item.description); - } - - if ((config_item.flags & SCRIPTCONFIG_BOOLEAN) != 0) { - DrawBoolButton(br.left, y + button_y_offset, current_value != 0, editable); - SetDParam(idx++, current_value == 0 ? STR_CONFIG_SETTING_OFF : STR_CONFIG_SETTING_ON); - } else { - if (config_item.complete_labels) { - DrawDropDownButton(br.left, y + button_y_offset, COLOUR_YELLOW, this->clicked_row == i && clicked_dropdown, editable); - } else { - DrawArrowButtons(br.left, y + button_y_offset, COLOUR_YELLOW, (this->clicked_button == i) ? 1 + (this->clicked_increase != rtl) : 0, editable && current_value > config_item.min_value, editable && current_value < config_item.max_value); - } - if (config_item.labels != nullptr && config_item.labels->Contains(current_value)) { - SetDParam(idx++, STR_JUST_RAW_STRING); - SetDParamStr(idx++, config_item.labels->Find(current_value)->second); - } else { - SetDParam(idx++, STR_JUST_INT); - SetDParam(idx++, current_value); - } - } - - DrawString(tr.left, tr.right, y + text_y_offset, str, colour); - y += this->line_height; - } - } - - void OnPaint() override - { - if (this->closing_dropdown) { - this->closing_dropdown = false; - this->clicked_dropdown = false; - } - this->DrawWidgets(); - } - - void OnClick(Point pt, int widget, int click_count) override - { - switch (widget) { - case WID_AIS_BACKGROUND: { - Rect r = this->GetWidget(widget)->GetCurrentRect().Shrink(WidgetDimensions::scaled.matrix, RectPadding::zero); - int num = (pt.y - r.top) / this->line_height + this->vscroll->GetPosition(); - if (num >= (int)this->visible_settings.size()) break; - - VisibleSettingsList::const_iterator it = this->visible_settings.begin(); - for (int i = 0; i < num; i++) it++; - const ScriptConfigItem config_item = **it; - if (!this->IsEditableItem(config_item)) return; - - if (this->clicked_row != num) { - this->CloseChildWindows(WC_QUERY_STRING); - HideDropDownMenu(this); - this->clicked_row = num; - this->clicked_dropdown = false; - } - - bool bool_item = (config_item.flags & SCRIPTCONFIG_BOOLEAN) != 0; - - int x = pt.x - r.left; - if (_current_text_dir == TD_RTL) x = r.Width() - 1 - x; - - /* One of the arrows is clicked (or green/red rect in case of bool value) */ - int old_val = this->ai_config->GetSetting(config_item.name); - if (!bool_item && IsInsideMM(x, 0, SETTING_BUTTON_WIDTH) && config_item.complete_labels) { - if (this->clicked_dropdown) { - /* unclick the dropdown */ - HideDropDownMenu(this); - this->clicked_dropdown = false; - this->closing_dropdown = false; - } else { - int rel_y = (pt.y - r.top) % this->line_height; - - Rect wi_rect; - wi_rect.left = pt.x - (_current_text_dir == TD_RTL ? SETTING_BUTTON_WIDTH - 1 - x : x); - wi_rect.right = wi_rect.left + SETTING_BUTTON_WIDTH - 1; - wi_rect.top = pt.y - rel_y + (this->line_height - SETTING_BUTTON_HEIGHT) / 2; - wi_rect.bottom = wi_rect.top + SETTING_BUTTON_HEIGHT - 1; - - /* If the mouse is still held but dragged outside of the dropdown list, keep the dropdown open */ - if (pt.y >= wi_rect.top && pt.y <= wi_rect.bottom) { - this->clicked_dropdown = true; - this->closing_dropdown = false; - - DropDownList list; - for (int i = config_item.min_value; i <= config_item.max_value; i++) { - list.emplace_back(new DropDownListCharStringItem(config_item.labels->Find(i)->second, i, false)); - } - - ShowDropDownListAt(this, std::move(list), old_val, -1, wi_rect, COLOUR_ORANGE, true); - } - } - } else if (IsInsideMM(x, 0, SETTING_BUTTON_WIDTH)) { - int new_val = old_val; - if (bool_item) { - new_val = !new_val; - } else if (x >= SETTING_BUTTON_WIDTH / 2) { - /* Increase button clicked */ - new_val += config_item.step_size; - if (new_val > config_item.max_value) new_val = config_item.max_value; - this->clicked_increase = true; - } else { - /* Decrease button clicked */ - new_val -= config_item.step_size; - if (new_val < config_item.min_value) new_val = config_item.min_value; - this->clicked_increase = false; - } - - if (new_val != old_val) { - this->ai_config->SetSetting(config_item.name, new_val); - this->clicked_button = num; - this->timeout.SetInterval(150); - } - } else if (!bool_item && !config_item.complete_labels) { - /* Display a query box so users can enter a custom value. */ - SetDParam(0, old_val); - ShowQueryString(STR_JUST_INT, STR_CONFIG_SETTING_QUERY_CAPTION, 10, this, CS_NUMERAL, QSF_NONE); - } - this->SetDirty(); - break; - } - - case WID_AIS_ACCEPT: - this->Close(); - break; - - case WID_AIS_RESET: - this->ai_config->ResetEditableSettings(_game_mode == GM_MENU || ((this->slot != OWNER_DEITY) && !Company::IsValidID(this->slot))); - this->SetDirty(); - break; - } - } - - void OnQueryTextFinished(char *str) override - { - if (StrEmpty(str)) return; - int32 value = atoi(str); - - SetValue(value); - } - - void OnDropdownSelect(int widget, int index) override - { - assert(this->clicked_dropdown); - SetValue(index); - } - - void OnDropdownClose(Point pt, int widget, int index, bool instant_close) override - { - /* We cannot raise the dropdown button just yet. OnClick needs some hint, whether - * the same dropdown button was clicked again, and then not open the dropdown again. - * So, we only remember that it was closed, and process it on the next OnPaint, which is - * after OnClick. */ - assert(this->clicked_dropdown); - this->closing_dropdown = true; - this->SetDirty(); - } - - void OnResize() override - { - this->vscroll->SetCapacityFromWidget(this, WID_AIS_BACKGROUND); - } - - void OnRealtimeTick(uint delta_ms) override - { - if (this->timeout.Elapsed(delta_ms)) { - this->clicked_button = -1; - this->SetDirty(); - } - } - - /** - * Some data on this window has become invalid. - * @param data Information about the changed data. - * @param gui_scope Whether the call is done from GUI scope. You may not do everything when not in GUI scope. See #InvalidateWindowData() for details. - */ - void OnInvalidateData(int data = 0, bool gui_scope = true) override - { - this->RebuildVisibleSettings(); - HideDropDownMenu(this); - this->CloseChildWindows(WC_QUERY_STRING); - } - -private: - bool IsEditableItem(const ScriptConfigItem &config_item) const - { - return _game_mode == GM_MENU - || _game_mode == GM_EDITOR - || ((this->slot != OWNER_DEITY) && !Company::IsValidID(this->slot)) - || (config_item.flags & SCRIPTCONFIG_INGAME) != 0 - || _settings_client.gui.ai_developer_tools; - } - - void SetValue(int value) - { - VisibleSettingsList::const_iterator it = this->visible_settings.begin(); - for (int i = 0; i < this->clicked_row; i++) it++; - const ScriptConfigItem config_item = **it; - if (_game_mode == GM_NORMAL && ((this->slot == OWNER_DEITY) || Company::IsValidID(this->slot)) && (config_item.flags & SCRIPTCONFIG_INGAME) == 0) return; - this->ai_config->SetSetting(config_item.name, value); - this->SetDirty(); - } -}; - -/** Widgets for the AI settings window. */ -static const NWidgetPart _nested_ai_settings_widgets[] = { - NWidget(NWID_HORIZONTAL), - NWidget(WWT_CLOSEBOX, COLOUR_MAUVE), - NWidget(WWT_CAPTION, COLOUR_MAUVE, WID_AIS_CAPTION), SetDataTip(STR_AI_SETTINGS_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS), - NWidget(WWT_DEFSIZEBOX, COLOUR_MAUVE), - EndContainer(), - NWidget(NWID_HORIZONTAL), - NWidget(WWT_MATRIX, COLOUR_MAUVE, WID_AIS_BACKGROUND), SetMinimalSize(188, 182), SetResize(1, 1), SetFill(1, 0), SetMatrixDataTip(1, 0, STR_NULL), SetScrollbar(WID_AIS_SCROLLBAR), - NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, WID_AIS_SCROLLBAR), - EndContainer(), - NWidget(NWID_HORIZONTAL), - NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), - NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, WID_AIS_ACCEPT), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_AI_SETTINGS_CLOSE, STR_NULL), - NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, WID_AIS_RESET), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_AI_SETTINGS_RESET, STR_NULL), - EndContainer(), - NWidget(WWT_RESIZEBOX, COLOUR_MAUVE), - EndContainer(), -}; - -/** Window definition for the AI settings window. */ -static WindowDesc _ai_settings_desc( - WDP_CENTER, "settings_script", 500, 208, - WC_AI_SETTINGS, WC_NONE, - 0, - _nested_ai_settings_widgets, lengthof(_nested_ai_settings_widgets) -); - -/** - * Open the AI settings window to change the AI settings for an AI. - * @param slot The CompanyID of the AI to change the settings. - */ -static void ShowAISettingsWindow(CompanyID slot) -{ - CloseWindowByClass(WC_AI_LIST); - CloseWindowByClass(WC_AI_SETTINGS); - new AISettingsWindow(&_ai_settings_desc, slot); -} - - -/** Window for displaying the textfile of a AI. */ -struct ScriptTextfileWindow : public TextfileWindow { - CompanyID slot; ///< View the textfile of this CompanyID slot. - - ScriptTextfileWindow(TextfileType file_type, CompanyID slot) : TextfileWindow(file_type), slot(slot) - { - this->OnInvalidateData(); - } - - void SetStringParameters(int widget) const override - { - if (widget == WID_TF_CAPTION) { - SetDParam(0, (slot == OWNER_DEITY) ? STR_CONTENT_TYPE_GAME_SCRIPT : STR_CONTENT_TYPE_AI); - SetDParamStr(1, GetConfig(slot)->GetInfo()->GetName()); - } - } - - void OnInvalidateData(int data = 0, bool gui_scope = true) override - { - const char *textfile = GetConfig(slot)->GetTextfile(file_type, slot); - if (textfile == nullptr) { - this->Close(); - } else { - this->LoadTextfile(textfile, (slot == OWNER_DEITY) ? GAME_DIR : AI_DIR); - } - } -}; - -/** - * Open the AI version of the textfile window. - * @param file_type The type of textfile to display. - * @param slot The slot the Script is using. - */ -void ShowScriptTextfileWindow(TextfileType file_type, CompanyID slot) -{ - CloseWindowById(WC_TEXTFILE, file_type); - new ScriptTextfileWindow(file_type, slot); -} - /** Widgets for the configure AI window. */ static const NWidgetPart _nested_ai_config_widgets[] = { @@ -747,8 +95,8 @@ struct AIConfigWindow : public Window { void Close() override { - CloseWindowByClass(WC_AI_LIST); - CloseWindowByClass(WC_AI_SETTINGS); + CloseWindowByClass(WC_SCRIPT_LIST); + CloseWindowByClass(WC_SCRIPT_SETTINGS); this->Window::Close(); } @@ -824,7 +172,7 @@ struct AIConfigWindow : public Window { void OnClick(Point pt, int widget, int click_count) override { if (widget >= WID_AIC_TEXTFILE && widget < WID_AIC_TEXTFILE + TFT_END) { - if (this->selected_slot == INVALID_COMPANY || GetConfig(this->selected_slot) == nullptr) return; + if (this->selected_slot == INVALID_COMPANY || AIConfig::GetConfig(this->selected_slot) == nullptr) return; ShowScriptTextfileWindow((TextfileType)(widget - WID_AIC_TEXTFILE), this->selected_slot); return; @@ -846,7 +194,7 @@ struct AIConfigWindow : public Window { case WID_AIC_LIST: { // Select a slot this->selected_slot = (CompanyID)this->vscroll->GetScrolledRowFromWidget(pt.y, this, widget); this->InvalidateData(); - if (click_count > 1 && this->selected_slot != INVALID_COMPANY) ShowAIListWindow((CompanyID)this->selected_slot); + if (click_count > 1 && this->selected_slot != INVALID_COMPANY) ShowScriptListWindow((CompanyID)this->selected_slot); break; } @@ -869,11 +217,11 @@ struct AIConfigWindow : public Window { break; case WID_AIC_CHANGE: // choose other AI - ShowAIListWindow((CompanyID)this->selected_slot); + ShowScriptListWindow((CompanyID)this->selected_slot); break; case WID_AIC_CONFIGURE: // change the settings for an AI - ShowAISettingsWindow((CompanyID)this->selected_slot); + ShowScriptSettingsWindow((CompanyID)this->selected_slot); break; case WID_AIC_CLOSE: @@ -906,12 +254,12 @@ struct AIConfigWindow : public Window { this->SetWidgetDisabledState(WID_AIC_DECREASE, GetGameSettings().difficulty.max_no_competitors == 0); this->SetWidgetDisabledState(WID_AIC_INCREASE, GetGameSettings().difficulty.max_no_competitors == MAX_COMPANIES - 1); this->SetWidgetDisabledState(WID_AIC_CHANGE, this->selected_slot == INVALID_COMPANY); - this->SetWidgetDisabledState(WID_AIC_CONFIGURE, this->selected_slot == INVALID_COMPANY || GetConfig(this->selected_slot)->GetConfigList()->size() == 0); + this->SetWidgetDisabledState(WID_AIC_CONFIGURE, this->selected_slot == INVALID_COMPANY || AIConfig::GetConfig(this->selected_slot)->GetConfigList()->size() == 0); this->SetWidgetDisabledState(WID_AIC_MOVE_UP, this->selected_slot == INVALID_COMPANY || !IsEditable((CompanyID)(this->selected_slot - 1))); this->SetWidgetDisabledState(WID_AIC_MOVE_DOWN, this->selected_slot == INVALID_COMPANY || !IsEditable((CompanyID)(this->selected_slot + 1))); for (TextfileType tft = TFT_BEGIN; tft < TFT_END; tft++) { - this->SetWidgetDisabledState(WID_AIC_TEXTFILE + tft, this->selected_slot == INVALID_COMPANY || (GetConfig(this->selected_slot)->GetTextfile(tft, this->selected_slot) == nullptr)); + this->SetWidgetDisabledState(WID_AIC_TEXTFILE + tft, this->selected_slot == INVALID_COMPANY || (AIConfig::GetConfig(this->selected_slot)->GetTextfile(tft, this->selected_slot) == nullptr)); } } }; @@ -923,597 +271,3 @@ void ShowAIConfigWindow() new AIConfigWindow(); } -/** - * Set the widget colour of a button based on the - * state of the script. (dead or alive) - * @param button the button to update. - * @param dead true if the script is dead, otherwise false. - * @param paused true if the script is paused, otherwise false. - * @return true if the colour was changed and the window need to be marked as dirty. - */ -static bool SetScriptButtonColour(NWidgetCore &button, bool dead, bool paused) -{ - /* Dead scripts are indicated with red background and - * paused scripts are indicated with yellow background. */ - Colours colour = dead ? COLOUR_RED : - (paused ? COLOUR_YELLOW : COLOUR_GREY); - if (button.colour != colour) { - button.colour = colour; - return true; - } - return false; -} - -/** - * Window with everything an AI prints via ScriptLog. - */ -struct AIDebugWindow : public Window { - static const uint MAX_BREAK_STR_STRING_LENGTH = 256; ///< Maximum length of the break string. - - static CompanyID ai_debug_company; ///< The AI that is (was last) being debugged. - int redraw_timer; ///< Timer for redrawing the window, otherwise it'll happen every tick. - int last_vscroll_pos; ///< Last position of the scrolling. - bool autoscroll; ///< Whether automatically scrolling should be enabled or not. - bool show_break_box; ///< Whether the break/debug box is visible. - static bool break_check_enabled; ///< Stop an AI when it prints a matching string - static char break_string[MAX_BREAK_STR_STRING_LENGTH]; ///< The string to match to the AI output - QueryString break_editbox; ///< Break editbox - static StringFilter break_string_filter; ///< Log filter for break. - static bool case_sensitive_break_check; ///< Is the matching done case-sensitive - int highlight_row; ///< The output row that matches the given string, or -1 - Scrollbar *vscroll; ///< Cache of the vertical scrollbar. - - ScriptLog::LogData *GetLogPointer() const - { - if (ai_debug_company == OWNER_DEITY) return (ScriptLog::LogData *)Game::GetInstance()->GetLogPointer(); - return (ScriptLog::LogData *)Company::Get(ai_debug_company)->ai_instance->GetLogPointer(); - } - - /** - * Check whether the currently selected AI/GS is dead. - * @return true if dead. - */ - bool IsDead() const - { - if (ai_debug_company == OWNER_DEITY) { - GameInstance *game = Game::GetInstance(); - return game == nullptr || game->IsDead(); - } - return !Company::IsValidAiID(ai_debug_company) || Company::Get(ai_debug_company)->ai_instance->IsDead(); - } - - /** - * Check whether a company is a valid AI company or GS. - * @param company Company to check for validity. - * @return true if company is valid for debugging. - */ - bool IsValidDebugCompany(CompanyID company) const - { - switch (company) { - case INVALID_COMPANY: return false; - case OWNER_DEITY: return Game::GetInstance() != nullptr; - default: return Company::IsValidAiID(company); - } - } - - /** - * Ensure that \c ai_debug_company refers to a valid AI company or GS, or is set to #INVALID_COMPANY. - * If no valid company is selected, it selects the first valid AI or GS if any. - */ - void SelectValidDebugCompany() - { - /* Check if the currently selected company is still active. */ - if (this->IsValidDebugCompany(ai_debug_company)) return; - - ai_debug_company = INVALID_COMPANY; - - for (const Company *c : Company::Iterate()) { - if (c->is_ai) { - ChangeToAI(c->index); - return; - } - } - - /* If no AI is available, see if there is a game script. */ - if (Game::GetInstance() != nullptr) ChangeToAI(OWNER_DEITY); - } - - /** - * Constructor for the window. - * @param desc The description of the window. - * @param number The window number (actually unused). - */ - AIDebugWindow(WindowDesc *desc, WindowNumber number) : Window(desc), break_editbox(MAX_BREAK_STR_STRING_LENGTH) - { - this->CreateNestedTree(); - this->vscroll = this->GetScrollbar(WID_AID_SCROLLBAR); - this->show_break_box = _settings_client.gui.ai_developer_tools; - this->GetWidget(WID_AID_BREAK_STRING_WIDGETS)->SetDisplayedPlane(this->show_break_box ? 0 : SZSP_HORIZONTAL); - this->FinishInitNested(number); - - if (!this->show_break_box) break_check_enabled = false; - - this->last_vscroll_pos = 0; - this->autoscroll = true; - this->highlight_row = -1; - - this->querystrings[WID_AID_BREAK_STR_EDIT_BOX] = &this->break_editbox; - - SetWidgetsDisabledState(!this->show_break_box, WID_AID_BREAK_STR_ON_OFF_BTN, WID_AID_BREAK_STR_EDIT_BOX, WID_AID_MATCH_CASE_BTN, WIDGET_LIST_END); - - /* Restore the break string value from static variable */ - this->break_editbox.text.Assign(this->break_string); - - this->SelectValidDebugCompany(); - this->InvalidateData(-1); - } - - void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override - { - if (widget == WID_AID_LOG_PANEL) { - resize->height = FONT_HEIGHT_NORMAL + WidgetDimensions::scaled.vsep_normal; - size->height = 14 * resize->height + WidgetDimensions::scaled.framerect.Vertical(); - } - } - - void OnPaint() override - { - this->SelectValidDebugCompany(); - - /* Draw standard stuff */ - this->DrawWidgets(); - - if (this->IsShaded()) return; // Don't draw anything when the window is shaded. - - bool dirty = false; - - /* Paint the company icons */ - for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) { - NWidgetCore *button = this->GetWidget(i + WID_AID_COMPANY_BUTTON_START); - - bool valid = Company::IsValidAiID(i); - - /* Check whether the validity of the company changed */ - dirty |= (button->IsDisabled() == valid); - - /* Mark dead/paused AIs by setting the background colour. */ - bool dead = valid && Company::Get(i)->ai_instance->IsDead(); - bool paused = valid && Company::Get(i)->ai_instance->IsPaused(); - /* Re-paint if the button was updated. - * (note that it is intentional that SetScriptButtonColour is always called) */ - dirty |= SetScriptButtonColour(*button, dead, paused); - - /* Draw company icon only for valid AI companies */ - if (!valid) continue; - - byte offset = (i == ai_debug_company) ? 1 : 0; - DrawCompanyIcon(i, button->pos_x + button->current_x / 2 - 7 + offset, this->GetWidget(WID_AID_COMPANY_BUTTON_START + i)->pos_y + 2 + offset); - } - - /* Set button colour for Game Script. */ - GameInstance *game = Game::GetInstance(); - bool valid = game != nullptr; - bool dead = valid && game->IsDead(); - bool paused = valid && game->IsPaused(); - - NWidgetCore *button = this->GetWidget(WID_AID_SCRIPT_GAME); - dirty |= (button->IsDisabled() == valid) || SetScriptButtonColour(*button, dead, paused); - - if (dirty) this->InvalidateData(-1); - - /* If there are no active companies, don't display anything else. */ - if (ai_debug_company == INVALID_COMPANY) return; - - ScriptLog::LogData *log = this->GetLogPointer(); - - int scroll_count = (log == nullptr) ? 0 : log->used; - if (this->vscroll->GetCount() != scroll_count) { - this->vscroll->SetCount(scroll_count); - - /* We need a repaint */ - this->SetWidgetDirty(WID_AID_SCROLLBAR); - } - - if (log == nullptr) return; - - /* Detect when the user scrolls the window. Enable autoscroll when the - * bottom-most line becomes visible. */ - if (this->last_vscroll_pos != this->vscroll->GetPosition()) { - this->autoscroll = this->vscroll->GetPosition() >= log->used - this->vscroll->GetCapacity(); - } - if (this->autoscroll) { - int scroll_pos = std::max(0, log->used - this->vscroll->GetCapacity()); - if (scroll_pos != this->vscroll->GetPosition()) { - this->vscroll->SetPosition(scroll_pos); - - /* We need a repaint */ - this->SetWidgetDirty(WID_AID_SCROLLBAR); - this->SetWidgetDirty(WID_AID_LOG_PANEL); - } - } - this->last_vscroll_pos = this->vscroll->GetPosition(); - } - - void SetStringParameters(int widget) const override - { - switch (widget) { - case WID_AID_NAME_TEXT: - if (ai_debug_company == OWNER_DEITY) { - const GameInfo *info = Game::GetInfo(); - assert(info != nullptr); - SetDParam(0, STR_AI_DEBUG_NAME_AND_VERSION); - SetDParamStr(1, info->GetName()); - SetDParam(2, info->GetVersion()); - } else if (ai_debug_company == INVALID_COMPANY || !Company::IsValidAiID(ai_debug_company)) { - SetDParam(0, STR_EMPTY); - } else { - const AIInfo *info = Company::Get(ai_debug_company)->ai_info; - assert(info != nullptr); - SetDParam(0, STR_AI_DEBUG_NAME_AND_VERSION); - SetDParamStr(1, info->GetName()); - SetDParam(2, info->GetVersion()); - } - break; - } - } - - void DrawWidget(const Rect &r, int widget) const override - { - if (ai_debug_company == INVALID_COMPANY) return; - - switch (widget) { - case WID_AID_LOG_PANEL: { - ScriptLog::LogData *log = this->GetLogPointer(); - if (log == nullptr) return; - - Rect br = r.Shrink(WidgetDimensions::scaled.bevel); - Rect tr = r.Shrink(WidgetDimensions::scaled.framerect); - for (int i = this->vscroll->GetPosition(); this->vscroll->IsVisible(i) && i < log->used; i++) { - int pos = (i + log->pos + 1 - log->used + log->count) % log->count; - if (log->lines[pos] == nullptr) break; - - TextColour colour; - switch (log->type[pos]) { - case ScriptLog::LOG_SQ_INFO: colour = TC_BLACK; break; - case ScriptLog::LOG_SQ_ERROR: colour = TC_WHITE; break; - case ScriptLog::LOG_INFO: colour = TC_BLACK; break; - case ScriptLog::LOG_WARNING: colour = TC_YELLOW; break; - case ScriptLog::LOG_ERROR: colour = TC_RED; break; - default: colour = TC_BLACK; break; - } - - /* Check if the current line should be highlighted */ - if (pos == this->highlight_row) { - GfxFillRect(br.left, tr.top, br.right, tr.top + this->resize.step_height - 1, PC_BLACK); - if (colour == TC_BLACK) colour = TC_WHITE; // Make black text readable by inverting it to white. - } - - DrawString(tr, log->lines[pos], colour, SA_LEFT | SA_FORCE); - tr.top += this->resize.step_height; - } - break; - } - } - } - - /** - * Change all settings to select another AI. - * @param show_ai The new AI to show. - */ - void ChangeToAI(CompanyID show_ai) - { - if (!this->IsValidDebugCompany(show_ai)) return; - - ai_debug_company = show_ai; - - this->highlight_row = -1; // The highlight of one AI make little sense for another AI. - - /* Close AI settings window to prevent confusion */ - CloseWindowByClass(WC_AI_SETTINGS); - - this->InvalidateData(-1); - - this->autoscroll = true; - this->last_vscroll_pos = this->vscroll->GetPosition(); - } - - void OnClick(Point pt, int widget, int click_count) override - { - /* Also called for hotkeys, so check for disabledness */ - if (this->IsWidgetDisabled(widget)) return; - - /* Check which button is clicked */ - if (IsInsideMM(widget, WID_AID_COMPANY_BUTTON_START, WID_AID_COMPANY_BUTTON_END + 1)) { - ChangeToAI((CompanyID)(widget - WID_AID_COMPANY_BUTTON_START)); - } - - switch (widget) { - case WID_AID_SCRIPT_GAME: - ChangeToAI(OWNER_DEITY); - break; - - case WID_AID_RELOAD_TOGGLE: - if (ai_debug_company == OWNER_DEITY) break; - /* First kill the company of the AI, then start a new one. This should start the current AI again */ - Command::Post(CCA_DELETE, ai_debug_company, CRR_MANUAL, INVALID_CLIENT_ID); - Command::Post(CCA_NEW_AI, ai_debug_company, CRR_NONE, INVALID_CLIENT_ID); - break; - - case WID_AID_SETTINGS: - ShowAISettingsWindow(ai_debug_company); - break; - - case WID_AID_BREAK_STR_ON_OFF_BTN: - this->break_check_enabled = !this->break_check_enabled; - this->InvalidateData(-1); - break; - - case WID_AID_MATCH_CASE_BTN: - this->case_sensitive_break_check = !this->case_sensitive_break_check; - this->InvalidateData(-1); - break; - - case WID_AID_CONTINUE_BTN: - /* Unpause current AI / game script and mark the corresponding script button dirty. */ - if (!this->IsDead()) { - if (ai_debug_company == OWNER_DEITY) { - Game::Unpause(); - } else { - AI::Unpause(ai_debug_company); - } - } - - /* If the last AI/Game Script is unpaused, unpause the game too. */ - if ((_pause_mode & PM_PAUSED_NORMAL) == PM_PAUSED_NORMAL) { - bool all_unpaused = !Game::IsPaused(); - if (all_unpaused) { - for (const Company *c : Company::Iterate()) { - if (c->is_ai && AI::IsPaused(c->index)) { - all_unpaused = false; - break; - } - } - if (all_unpaused) { - /* All scripts have been unpaused => unpause the game. */ - Command::Post(PM_PAUSED_NORMAL, false); - } - } - } - - this->highlight_row = -1; - this->InvalidateData(-1); - break; - } - } - - void OnEditboxChanged(int wid) override - { - if (wid == WID_AID_BREAK_STR_EDIT_BOX) { - /* Save the current string to static member so it can be restored next time the window is opened. */ - strecpy(this->break_string, this->break_editbox.text.buf, lastof(this->break_string)); - break_string_filter.SetFilterTerm(this->break_string); - } - } - - /** - * Some data on this window has become invalid. - * @param data Information about the changed data. - * This is the company ID of the AI/GS which wrote a new log message, or -1 in other cases. - * @param gui_scope Whether the call is done from GUI scope. You may not do everything when not in GUI scope. See #InvalidateWindowData() for details. - */ - void OnInvalidateData(int data = 0, bool gui_scope = true) override - { - /* If the log message is related to the active company tab, check the break string. - * This needs to be done in gameloop-scope, so the AI is suspended immediately. */ - if (!gui_scope && data == ai_debug_company && this->IsValidDebugCompany(ai_debug_company) && this->break_check_enabled && !this->break_string_filter.IsEmpty()) { - /* Get the log instance of the active company */ - ScriptLog::LogData *log = this->GetLogPointer(); - - if (log != nullptr) { - this->break_string_filter.ResetState(); - this->break_string_filter.AddLine(log->lines[log->pos]); - if (this->break_string_filter.GetState()) { - /* Pause execution of script. */ - if (!this->IsDead()) { - if (ai_debug_company == OWNER_DEITY) { - Game::Pause(); - } else { - AI::Pause(ai_debug_company); - } - } - - /* Pause the game. */ - if ((_pause_mode & PM_PAUSED_NORMAL) == PM_UNPAUSED) { - Command::Post(PM_PAUSED_NORMAL, true); - } - - /* Highlight row that matched */ - this->highlight_row = log->pos; - } - } - } - - if (!gui_scope) return; - - this->SelectValidDebugCompany(); - - ScriptLog::LogData *log = ai_debug_company != INVALID_COMPANY ? this->GetLogPointer() : nullptr; - this->vscroll->SetCount((log == nullptr) ? 0 : log->used); - - /* Update company buttons */ - for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) { - this->SetWidgetDisabledState(i + WID_AID_COMPANY_BUTTON_START, !Company::IsValidAiID(i)); - this->SetWidgetLoweredState(i + WID_AID_COMPANY_BUTTON_START, ai_debug_company == i); - } - - this->SetWidgetDisabledState(WID_AID_SCRIPT_GAME, Game::GetGameInstance() == nullptr); - this->SetWidgetLoweredState(WID_AID_SCRIPT_GAME, ai_debug_company == OWNER_DEITY); - - this->SetWidgetLoweredState(WID_AID_BREAK_STR_ON_OFF_BTN, this->break_check_enabled); - this->SetWidgetLoweredState(WID_AID_MATCH_CASE_BTN, this->case_sensitive_break_check); - - this->SetWidgetDisabledState(WID_AID_SETTINGS, ai_debug_company == INVALID_COMPANY); - extern CompanyID _local_company; - this->SetWidgetDisabledState(WID_AID_RELOAD_TOGGLE, ai_debug_company == INVALID_COMPANY || ai_debug_company == OWNER_DEITY || ai_debug_company == _local_company); - this->SetWidgetDisabledState(WID_AID_CONTINUE_BTN, ai_debug_company == INVALID_COMPANY || - (ai_debug_company == OWNER_DEITY ? !Game::IsPaused() : !AI::IsPaused(ai_debug_company))); - } - - void OnResize() override - { - this->vscroll->SetCapacityFromWidget(this, WID_AID_LOG_PANEL, WidgetDimensions::scaled.framerect.Vertical()); - } - - static HotkeyList hotkeys; -}; - -CompanyID AIDebugWindow::ai_debug_company = INVALID_COMPANY; -char AIDebugWindow::break_string[MAX_BREAK_STR_STRING_LENGTH] = ""; -bool AIDebugWindow::break_check_enabled = true; -bool AIDebugWindow::case_sensitive_break_check = false; -StringFilter AIDebugWindow::break_string_filter(&AIDebugWindow::case_sensitive_break_check); - -/** Make a number of rows with buttons for each company for the AI debug window. */ -NWidgetBase *MakeCompanyButtonRowsAIDebug(int *biggest_index) -{ - return MakeCompanyButtonRows(biggest_index, WID_AID_COMPANY_BUTTON_START, WID_AID_COMPANY_BUTTON_END, COLOUR_GREY, 8, STR_AI_DEBUG_SELECT_AI_TOOLTIP); -} - -/** - * Handler for global hotkeys of the AIDebugWindow. - * @param hotkey Hotkey - * @return ES_HANDLED if hotkey was accepted. - */ -static EventState AIDebugGlobalHotkeys(int hotkey) -{ - if (_game_mode != GM_NORMAL) return ES_NOT_HANDLED; - Window *w = ShowAIDebugWindow(INVALID_COMPANY); - if (w == nullptr) return ES_NOT_HANDLED; - return w->OnHotkey(hotkey); -} - -static Hotkey aidebug_hotkeys[] = { - Hotkey('1', "company_1", WID_AID_COMPANY_BUTTON_START), - Hotkey('2', "company_2", WID_AID_COMPANY_BUTTON_START + 1), - Hotkey('3', "company_3", WID_AID_COMPANY_BUTTON_START + 2), - Hotkey('4', "company_4", WID_AID_COMPANY_BUTTON_START + 3), - Hotkey('5', "company_5", WID_AID_COMPANY_BUTTON_START + 4), - Hotkey('6', "company_6", WID_AID_COMPANY_BUTTON_START + 5), - Hotkey('7', "company_7", WID_AID_COMPANY_BUTTON_START + 6), - Hotkey('8', "company_8", WID_AID_COMPANY_BUTTON_START + 7), - Hotkey('9', "company_9", WID_AID_COMPANY_BUTTON_START + 8), - Hotkey((uint16)0, "company_10", WID_AID_COMPANY_BUTTON_START + 9), - Hotkey((uint16)0, "company_11", WID_AID_COMPANY_BUTTON_START + 10), - Hotkey((uint16)0, "company_12", WID_AID_COMPANY_BUTTON_START + 11), - Hotkey((uint16)0, "company_13", WID_AID_COMPANY_BUTTON_START + 12), - Hotkey((uint16)0, "company_14", WID_AID_COMPANY_BUTTON_START + 13), - Hotkey((uint16)0, "company_15", WID_AID_COMPANY_BUTTON_START + 14), - Hotkey('S', "settings", WID_AID_SETTINGS), - Hotkey('0', "game_script", WID_AID_SCRIPT_GAME), - Hotkey((uint16)0, "reload", WID_AID_RELOAD_TOGGLE), - Hotkey('B', "break_toggle", WID_AID_BREAK_STR_ON_OFF_BTN), - Hotkey('F', "break_string", WID_AID_BREAK_STR_EDIT_BOX), - Hotkey('C', "match_case", WID_AID_MATCH_CASE_BTN), - Hotkey(WKC_RETURN, "continue", WID_AID_CONTINUE_BTN), - HOTKEY_LIST_END -}; -HotkeyList AIDebugWindow::hotkeys("aidebug", aidebug_hotkeys, AIDebugGlobalHotkeys); - -/** Widgets for the AI debug window. */ -static const NWidgetPart _nested_ai_debug_widgets[] = { - NWidget(NWID_HORIZONTAL), - NWidget(WWT_CLOSEBOX, COLOUR_GREY), - NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_AI_DEBUG, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS), - NWidget(WWT_SHADEBOX, COLOUR_GREY), - NWidget(WWT_DEFSIZEBOX, COLOUR_GREY), - NWidget(WWT_STICKYBOX, COLOUR_GREY), - EndContainer(), - NWidget(WWT_PANEL, COLOUR_GREY, WID_AID_VIEW), - NWidgetFunction(MakeCompanyButtonRowsAIDebug), SetPadding(0, 2, 1, 2), - EndContainer(), - NWidget(NWID_HORIZONTAL), - NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_AID_SCRIPT_GAME), SetMinimalSize(100, 20), SetResize(1, 0), SetDataTip(STR_AI_GAME_SCRIPT, STR_AI_GAME_SCRIPT_TOOLTIP), - NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_AID_NAME_TEXT), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_JUST_STRING, STR_AI_DEBUG_NAME_TOOLTIP), - NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_AID_SETTINGS), SetMinimalSize(100, 20), SetDataTip(STR_AI_DEBUG_SETTINGS, STR_AI_DEBUG_SETTINGS_TOOLTIP), - NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_AID_RELOAD_TOGGLE), SetMinimalSize(100, 20), SetDataTip(STR_AI_DEBUG_RELOAD, STR_AI_DEBUG_RELOAD_TOOLTIP), - EndContainer(), - NWidget(NWID_HORIZONTAL), - NWidget(NWID_VERTICAL), - /* Log panel */ - NWidget(WWT_PANEL, COLOUR_GREY, WID_AID_LOG_PANEL), SetMinimalSize(287, 180), SetResize(1, 1), SetScrollbar(WID_AID_SCROLLBAR), - EndContainer(), - /* Break string widgets */ - NWidget(NWID_SELECTION, INVALID_COLOUR, WID_AID_BREAK_STRING_WIDGETS), - NWidget(NWID_HORIZONTAL), - 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), - NWidget(WWT_PANEL, COLOUR_GREY), - NWidget(NWID_HORIZONTAL), - NWidget(WWT_LABEL, COLOUR_GREY), SetPadding(2, 2, 2, 4), SetDataTip(STR_AI_DEBUG_BREAK_ON_LABEL, 0x0), - NWidget(WWT_EDITBOX, COLOUR_GREY, WID_AID_BREAK_STR_EDIT_BOX), SetFill(1, 1), SetResize(1, 0), SetPadding(2, 2, 2, 2), SetDataTip(STR_AI_DEBUG_BREAK_STR_OSKTITLE, STR_AI_DEBUG_BREAK_STR_TOOLTIP), - EndContainer(), - EndContainer(), - NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_AID_MATCH_CASE_BTN), SetMinimalSize(100, 0), SetFill(0, 1), SetDataTip(STR_AI_DEBUG_MATCH_CASE, STR_AI_DEBUG_MATCH_CASE_TOOLTIP), - 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), - EndContainer(), - EndContainer(), - EndContainer(), - NWidget(NWID_VERTICAL), - NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_AID_SCROLLBAR), - NWidget(WWT_RESIZEBOX, COLOUR_GREY), - EndContainer(), - EndContainer(), -}; - -/** Window definition for the AI debug window. */ -static WindowDesc _ai_debug_desc( - WDP_AUTO, "script_debug", 600, 450, - WC_AI_DEBUG, WC_NONE, - 0, - _nested_ai_debug_widgets, lengthof(_nested_ai_debug_widgets), - &AIDebugWindow::hotkeys -); - -/** - * Open the AI debug window and select the given company. - * @param show_company Display debug information about this AI company. - */ -Window *ShowAIDebugWindow(CompanyID show_company) -{ - if (!_networking || _network_server) { - AIDebugWindow *w = (AIDebugWindow *)BringWindowToFrontById(WC_AI_DEBUG, 0); - if (w == nullptr) w = new AIDebugWindow(&_ai_debug_desc, 0); - if (show_company != INVALID_COMPANY) w->ChangeToAI(show_company); - return w; - } else { - ShowErrorMessage(STR_ERROR_AI_DEBUG_SERVER_ONLY, INVALID_STRING_ID, WL_INFO); - } - - return nullptr; -} - -/** - * Reset the AI windows to their initial state. - */ -void InitializeAIGui() -{ - AIDebugWindow::ai_debug_company = INVALID_COMPANY; -} - -/** Open the AI debug window if one of the AI scripts has crashed. */ -void ShowAIDebugWindowIfAIError() -{ - /* Network clients can't debug AIs. */ - if (_networking && !_network_server) return; - - for (const Company *c : Company::Iterate()) { - if (c->is_ai && c->ai_instance->IsDead()) { - ShowAIDebugWindow(c->index); - break; - } - } - - GameInstance *g = Game::GetGameInstance(); - if (g != nullptr && g->IsDead()) { - ShowAIDebugWindow(OWNER_DEITY); - } -} diff --git a/src/ai/ai_gui.hpp b/src/ai/ai_gui.hpp index be6263ddd5..6eb28c3841 100644 --- a/src/ai/ai_gui.hpp +++ b/src/ai/ai_gui.hpp @@ -10,13 +10,6 @@ #ifndef AI_GUI_HPP #define AI_GUI_HPP -#include "../company_type.h" - -void ShowAIListWindow(CompanyID slot); -Window* ShowAIDebugWindow(CompanyID show_company = INVALID_COMPANY); void ShowAIConfigWindow(); -void ShowScriptTextfileWindow(TextfileType file_type, CompanyID slot); -void ShowAIDebugWindowIfAIError(); -void InitializeAIGui(); #endif /* AI_GUI_HPP */ diff --git a/src/ai/ai_instance.cpp b/src/ai/ai_instance.cpp index 4ffb10f8d0..57528f2877 100644 --- a/src/ai/ai_instance.cpp +++ b/src/ai/ai_instance.cpp @@ -14,11 +14,11 @@ #include "../script/squirrel_class.hpp" #include "ai_config.hpp" -#include "ai_gui.hpp" #include "ai.hpp" #include "../script/script_storage.hpp" #include "../script/script_cmd.h" +#include "../script/script_gui.h" #include "ai_info.hpp" #include "ai_instance.hpp" @@ -64,7 +64,7 @@ void AIInstance::Died() /* Intro is not supposed to use AI, but it may have 'dummy' AI which instant dies. */ if (_game_mode == GM_MENU) return; - ShowAIDebugWindow(_current_company); + ShowScriptDebugWindow(_current_company); const AIInfo *info = AIConfig::GetConfig(_current_company, AIConfig::SSS_FORCE_GAME)->GetInfo(); if (info != nullptr) { diff --git a/src/company_cmd.cpp b/src/company_cmd.cpp index 182f32a4f0..7432bde65b 100644 --- a/src/company_cmd.cpp +++ b/src/company_cmd.cpp @@ -922,8 +922,8 @@ CommandCost CmdCompanyCtrl(DoCommandFlag flags, CompanyCtrlAction cca, CompanyID } InvalidateWindowClassesData(WC_GAME_OPTIONS); - InvalidateWindowClassesData(WC_AI_SETTINGS); - InvalidateWindowClassesData(WC_AI_LIST); + InvalidateWindowClassesData(WC_SCRIPT_SETTINGS); + InvalidateWindowClassesData(WC_SCRIPT_LIST); return CommandCost(); } diff --git a/src/game/game.hpp b/src/game/game.hpp index 98f436d397..fffa04fa05 100644 --- a/src/game/game.hpp +++ b/src/game/game.hpp @@ -12,10 +12,6 @@ #include "../core/string_compare_type.hpp" #include "game_scanner.hpp" -#include - -/** A list that maps AI names to their AIInfo object. */ -typedef std::map ScriptInfoList; #include "../script/api/script_event_types.hpp" diff --git a/src/game/game_core.cpp b/src/game/game_core.cpp index caa57e1322..a4e1e64ddc 100644 --- a/src/game/game_core.cpp +++ b/src/game/game_core.cpp @@ -96,7 +96,7 @@ cur_company.Restore(); - InvalidateWindowData(WC_AI_DEBUG, 0, -1); + InvalidateWindowData(WC_SCRIPT_DEBUG, 0, -1); } /* static */ void Game::Uninitialize(bool keepConfig) @@ -201,9 +201,9 @@ Game::scanner_library->RescanDir(); ResetConfig(); - InvalidateWindowData(WC_AI_LIST, 0, 1); - SetWindowClassesDirty(WC_AI_DEBUG); - InvalidateWindowClassesData(WC_AI_SETTINGS); + InvalidateWindowData(WC_SCRIPT_LIST, 0, 1); + SetWindowClassesDirty(WC_SCRIPT_DEBUG); + InvalidateWindowClassesData(WC_SCRIPT_SETTINGS); InvalidateWindowClassesData(WC_GAME_OPTIONS); } diff --git a/src/game/game_gui.cpp b/src/game/game_gui.cpp index c5285a7547..ad2248a6f5 100644 --- a/src/game/game_gui.cpp +++ b/src/game/game_gui.cpp @@ -8,39 +8,20 @@ /** @file game_gui.cpp %Window for configuring the Game Script */ #include "../stdafx.h" -#include "../table/sprites.h" #include "../error.h" #include "../settings_gui.h" #include "../querystring_gui.h" -#include "../stringfilter_type.h" -#include "../company_base.h" -#include "../company_gui.h" -#include "../strings_func.h" #include "../window_func.h" -#include "../window_type.h" -#include "../gfx_func.h" -#include "../command_func.h" #include "../network/network.h" -#include "../settings_func.h" #include "../network/network_content.h" -#include "../textfile_gui.h" -#include "../widgets/dropdown_type.h" #include "../widgets/dropdown_func.h" -#include "../hotkeys.h" -#include "../core/geometry_func.hpp" -#include "../guitimer_func.h" -#include "../company_cmd.h" -#include "../misc_cmd.h" +#include "game.hpp" #include "game_gui.hpp" -#include "../ai/ai_config.hpp" -#include "../ai/ai_gui.hpp" -#include "../widgets/game_widget.h" +#include "game_config.hpp" +#include "game_info.hpp" +#include "../script/script_gui.h" #include "../table/strings.h" -#include "../game/game.hpp" -#include "../game/game_config.hpp" -#include "../game/game_info.hpp" -#include "../game/game_instance.hpp" #include "../safeguards.h" @@ -125,7 +106,7 @@ struct GSConfigWindow : public Window { void Close() override { - CloseWindowByClass(WC_AI_LIST); + CloseWindowByClass(WC_SCRIPT_LIST); this->Window::Close(); } @@ -267,12 +248,12 @@ struct GSConfigWindow : public Window { switch (widget) { case WID_GSC_GSLIST: { this->InvalidateData(); - if (click_count > 1 && _game_mode != GM_NORMAL) ShowAIListWindow((CompanyID)OWNER_DEITY); + if (click_count > 1 && _game_mode != GM_NORMAL) ShowScriptListWindow((CompanyID)OWNER_DEITY); break; } case WID_GSC_CHANGE: // choose other Game Script - ShowAIListWindow((CompanyID)OWNER_DEITY); + ShowScriptListWindow((CompanyID)OWNER_DEITY); break; case WID_GSC_CONTENT_DOWNLOAD: diff --git a/src/game/game_instance.cpp b/src/game/game_instance.cpp index 09bba13345..facb875b43 100644 --- a/src/game/game_instance.cpp +++ b/src/game/game_instance.cpp @@ -14,7 +14,7 @@ #include "../script/script_storage.hpp" #include "../script/script_cmd.h" -#include "../ai/ai_gui.hpp" +#include "../script/script_gui.h" #include "game_config.hpp" #include "game_info.hpp" #include "game_instance.hpp" @@ -67,7 +67,7 @@ void GameInstance::Died() { ScriptInstance::Died(); - ShowAIDebugWindow(OWNER_DEITY); + ShowScriptDebugWindow(OWNER_DEITY); const GameInfo *info = Game::GetInfo(); if (info != nullptr) { diff --git a/src/misc.cpp b/src/misc.cpp index 467c398d66..a9b3e19759 100644 --- a/src/misc.cpp +++ b/src/misc.cpp @@ -11,7 +11,7 @@ #include "landscape.h" #include "news_func.h" #include "ai/ai.hpp" -#include "ai/ai_gui.hpp" +#include "script/script_gui.h" #include "newgrf.h" #include "newgrf_house.h" #include "economy_func.h" @@ -100,7 +100,7 @@ void InitializeGame(uint size_x, uint size_y, bool reset_date, bool reset_settin InitializeGraphGui(); InitializeObjectGui(); InitializeTownGui(); - InitializeAIGui(); + InitializeScriptGui(); InitializeTrees(); InitializeIndustries(); InitializeObjects(); diff --git a/src/saveload/afterload.cpp b/src/saveload/afterload.cpp index a131e6c635..b22797032e 100644 --- a/src/saveload/afterload.cpp +++ b/src/saveload/afterload.cpp @@ -40,7 +40,7 @@ #include "../company_func.h" #include "../road_cmd.h" #include "../ai/ai.hpp" -#include "../ai/ai_gui.hpp" +#include "../script/script_gui.h" #include "../game/game.hpp" #include "../town.h" #include "../economy_base.h" @@ -552,7 +552,7 @@ static void StartScripts() if (Company::IsValidAiID(c->index)) AI::StartNew(c->index, false); } - ShowAIDebugWindowIfAIError(); + ShowScriptDebugWindowIfScriptError(); } /** diff --git a/src/script/CMakeLists.txt b/src/script/CMakeLists.txt index d1054ca5f8..d945e54ab9 100644 --- a/src/script/CMakeLists.txt +++ b/src/script/CMakeLists.txt @@ -9,6 +9,8 @@ add_files( script_config.cpp script_config.hpp script_fatalerror.hpp + script_gui.h + script_gui.cpp script_info.cpp script_info.hpp script_info_dummy.cpp diff --git a/src/script/api/script_controller.cpp b/src/script/api/script_controller.cpp index 232ce5a3b6..44a4b152b8 100644 --- a/src/script/api/script_controller.cpp +++ b/src/script/api/script_controller.cpp @@ -18,7 +18,7 @@ #include "../script_info.hpp" #include "../script_instance.hpp" #include "script_log.hpp" -#include "../../ai/ai_gui.hpp" +#include "../script_gui.h" #include "../../settings_type.h" #include "../../network/network.h" #include "../../misc_cmd.h" @@ -57,7 +57,7 @@ /* Inform script developer that their script has been paused and * needs manual action to continue. */ - ShowAIDebugWindow(ScriptObject::GetRootCompany()); + ShowScriptDebugWindow(ScriptObject::GetRootCompany()); if ((_pause_mode & PM_PAUSED_NORMAL) == PM_UNPAUSED) { ScriptObject::Command::Do(PM_PAUSED_NORMAL, true); diff --git a/src/script/api/script_log.cpp b/src/script/api/script_log.cpp index 63c4548aaf..12c4533ca2 100644 --- a/src/script/api/script_log.cpp +++ b/src/script/api/script_log.cpp @@ -75,7 +75,7 @@ /* Also still print to debug window */ Debug(script, level, "[{}] [{}] {}", (uint)ScriptObject::GetRootCompany(), logc, log->lines[log->pos]); - InvalidateWindowData(WC_AI_DEBUG, 0, ScriptObject::GetRootCompany()); + InvalidateWindowData(WC_SCRIPT_DEBUG, 0, ScriptObject::GetRootCompany()); } /* static */ void ScriptLog::FreeLogPointer() diff --git a/src/script/script_gui.cpp b/src/script/script_gui.cpp new file mode 100644 index 0000000000..468cdecf59 --- /dev/null +++ b/src/script/script_gui.cpp @@ -0,0 +1,1256 @@ +/* + * This file is part of OpenTTD. + * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2. + * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see . + */ + + /** @file script_gui.cpp %Window for configuring the Scripts */ + +#include "../stdafx.h" +#include "../table/sprites.h" +#include "../error.h" +#include "../settings_gui.h" +#include "../querystring_gui.h" +#include "../stringfilter_type.h" +#include "../company_base.h" +#include "../company_gui.h" +#include "../window_func.h" +#include "../network/network.h" +#include "../widgets/dropdown_func.h" +#include "../hotkeys.h" +#include "../company_cmd.h" +#include "../misc_cmd.h" + +#include "script_gui.h" +#include "script_log.hpp" +#include "script_scanner.hpp" +#include "../ai/ai.hpp" +#include "../ai/ai_config.hpp" +#include "../ai/ai_info.hpp" +#include "../ai/ai_instance.hpp" +#include "../game/game.hpp" +#include "../game/game_config.hpp" +#include "../game/game_info.hpp" +#include "../game/game_instance.hpp" +#include "table/strings.h" + +#include "../safeguards.h" + + +static ScriptConfig *GetConfig(CompanyID slot) +{ + if (slot == OWNER_DEITY) return GameConfig::GetConfig(); + return AIConfig::GetConfig(slot); +} + +/** + * Window that let you choose an available Script. + */ +struct ScriptListWindow : public Window { + const ScriptInfoList *info_list; ///< The list of Scripts. + int selected; ///< The currently selected Script. + CompanyID slot; ///< The company we're selecting a new Script for. + int line_height; ///< Height of a row in the matrix widget. + Scrollbar *vscroll; ///< Cache of the vertical scrollbar. + + /** + * Constructor for the window. + * @param desc The description of the window. + * @param slot The company we're changing the Script for. + */ + ScriptListWindow(WindowDesc *desc, CompanyID slot) : Window(desc), + slot(slot) + { + if (slot == OWNER_DEITY) { + this->info_list = Game::GetUniqueInfoList(); + } else { + this->info_list = AI::GetUniqueInfoList(); + } + + this->CreateNestedTree(); + this->vscroll = this->GetScrollbar(WID_SCRL_SCROLLBAR); + this->FinishInitNested(); // Initializes 'this->line_height' as side effect. + + this->vscroll->SetCount((int)this->info_list->size() + 1); + + /* Try if we can find the currently selected AI */ + this->selected = -1; + if (GetConfig(slot)->HasScript()) { + ScriptInfo *info = GetConfig(slot)->GetInfo(); + int i = 0; + for (const auto &item : *this->info_list) { + if (item.second == info) { + this->selected = i; + break; + } + + i++; + } + } + } + + void SetStringParameters(int widget) const override + { + if (widget != WID_SCRL_CAPTION) return; + + SetDParam(0, (this->slot == OWNER_DEITY) ? STR_AI_LIST_CAPTION_GAMESCRIPT : STR_AI_LIST_CAPTION_AI); + } + + void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override + { + if (widget != WID_SCRL_LIST) return; + + this->line_height = FONT_HEIGHT_NORMAL + padding.height; + + resize->width = 1; + resize->height = this->line_height; + size->height = 5 * this->line_height; + } + + void DrawWidget(const Rect &r, int widget) const override + { + switch (widget) { + case WID_SCRL_LIST: { + /* Draw a list of all available Scripts. */ + Rect tr = r.Shrink(WidgetDimensions::scaled.matrix); + /* First AI in the list is hardcoded to random */ + if (this->vscroll->IsVisible(0)) { + DrawString(tr, this->slot == OWNER_DEITY ? STR_AI_CONFIG_NONE : STR_AI_CONFIG_RANDOM_AI, this->selected == -1 ? TC_WHITE : TC_ORANGE); + tr.top += this->line_height; + } + int i = 0; + for (const auto &item : *this->info_list) { + i++; + if (this->vscroll->IsVisible(i)) { + DrawString(tr, item.second->GetName(), (this->selected == i - 1) ? TC_WHITE : TC_ORANGE); + tr.top += this->line_height; + } + } + break; + } + case WID_SCRL_INFO_BG: { + ScriptInfo *selected_info = nullptr; + int i = 0; + for (const auto &item : *this->info_list) { + i++; + if (this->selected == i - 1) selected_info = static_cast(item.second); + } + /* Some info about the currently selected Script. */ + if (selected_info != nullptr) { + Rect tr = r.Shrink(WidgetDimensions::scaled.frametext, WidgetDimensions::scaled.framerect); + SetDParamStr(0, selected_info->GetAuthor()); + DrawString(tr, STR_AI_LIST_AUTHOR); + tr.top += FONT_HEIGHT_NORMAL + WidgetDimensions::scaled.vsep_normal; + SetDParam(0, selected_info->GetVersion()); + DrawString(tr, STR_AI_LIST_VERSION); + tr.top += FONT_HEIGHT_NORMAL + WidgetDimensions::scaled.vsep_normal; + if (selected_info->GetURL() != nullptr) { + SetDParamStr(0, selected_info->GetURL()); + DrawString(tr, STR_AI_LIST_URL); + tr.top += FONT_HEIGHT_NORMAL + WidgetDimensions::scaled.vsep_normal; + } + SetDParamStr(0, selected_info->GetDescription()); + DrawStringMultiLine(tr, STR_JUST_RAW_STRING, TC_WHITE); + } + break; + } + } + } + + /** + * Changes the Script of the current slot. + */ + void ChangeScript() + { + if (this->selected == -1) { + GetConfig(slot)->Change(nullptr); + } else { + ScriptInfoList::const_iterator it = this->info_list->begin(); + for (int i = 0; i < this->selected; i++) it++; + GetConfig(slot)->Change((*it).second->GetName(), (*it).second->GetVersion()); + } + InvalidateWindowData(WC_GAME_OPTIONS, slot == OWNER_DEITY ? WN_GAME_OPTIONS_GS : WN_GAME_OPTIONS_AI); + InvalidateWindowClassesData(WC_SCRIPT_SETTINGS); + CloseWindowByClass(WC_QUERY_STRING); + InvalidateWindowClassesData(WC_TEXTFILE); + } + + void OnClick(Point pt, int widget, int click_count) override + { + switch (widget) { + case WID_SCRL_LIST: { // Select one of the Scripts + int sel = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_SCRL_LIST) - 1; + if (sel < (int)this->info_list->size()) { + this->selected = sel; + this->SetDirty(); + if (click_count > 1) { + this->ChangeScript(); + this->Close(); + } + } + break; + } + + case WID_SCRL_ACCEPT: { + this->ChangeScript(); + this->Close(); + break; + } + + case WID_SCRL_CANCEL: + this->Close(); + break; + } + } + + void OnResize() override + { + this->vscroll->SetCapacityFromWidget(this, WID_SCRL_LIST); + } + + /** + * Some data on this window has become invalid. + * @param data Information about the changed data. + * @param gui_scope Whether the call is done from GUI scope. You may not do everything when not in GUI scope. See #InvalidateWindowData() for details. + */ + void OnInvalidateData(int data = 0, bool gui_scope = true) override + { + if (_game_mode == GM_NORMAL && Company::IsValidID(this->slot)) { + this->Close(); + return; + } + + if (!gui_scope) return; + + this->vscroll->SetCount((int)this->info_list->size() + 1); + + /* selected goes from -1 .. length of ai list - 1. */ + this->selected = std::min(this->selected, this->vscroll->GetCount() - 2); + } +}; + +/** Widgets for the AI list window. */ +static const NWidgetPart _nested_script_list_widgets[] = { + NWidget(NWID_HORIZONTAL), + NWidget(WWT_CLOSEBOX, COLOUR_MAUVE), + NWidget(WWT_CAPTION, COLOUR_MAUVE, WID_SCRL_CAPTION), SetDataTip(STR_AI_LIST_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS), + NWidget(WWT_DEFSIZEBOX, COLOUR_MAUVE), + EndContainer(), + NWidget(NWID_HORIZONTAL), + NWidget(WWT_MATRIX, COLOUR_MAUVE, WID_SCRL_LIST), SetMinimalSize(188, 112), SetFill(1, 1), SetResize(1, 1), SetMatrixDataTip(1, 0, STR_AI_LIST_TOOLTIP), SetScrollbar(WID_SCRL_SCROLLBAR), + NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, WID_SCRL_SCROLLBAR), + EndContainer(), + NWidget(WWT_PANEL, COLOUR_MAUVE, WID_SCRL_INFO_BG), SetMinimalTextLines(8, WidgetDimensions::unscaled.framerect.Vertical() + WidgetDimensions::unscaled.vsep_normal * 3), SetResize(1, 0), + EndContainer(), + NWidget(NWID_HORIZONTAL), + NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), + NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, WID_SCRL_ACCEPT), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_AI_LIST_ACCEPT, STR_AI_LIST_ACCEPT_TOOLTIP), + NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, WID_SCRL_CANCEL), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_AI_LIST_CANCEL, STR_AI_LIST_CANCEL_TOOLTIP), + EndContainer(), + NWidget(WWT_RESIZEBOX, COLOUR_MAUVE), + EndContainer(), +}; + +/** Window definition for the ai list window. */ +static WindowDesc _script_list_desc( + WDP_CENTER, "settings_script_list", 200, 234, + WC_SCRIPT_LIST, WC_NONE, + 0, + _nested_script_list_widgets, lengthof(_nested_script_list_widgets) +); + +/** + * Open the AI list window to chose an AI for the given company slot. + * @param slot The slot to change the AI of. + */ +void ShowScriptListWindow(CompanyID slot) +{ + CloseWindowByClass(WC_SCRIPT_LIST); + new ScriptListWindow(&_script_list_desc, slot); +} + + +/** + * Window for settings the parameters of an AI. + */ +struct ScriptSettingsWindow : public Window { + CompanyID slot; ///< The currently show company's setting. + ScriptConfig *script_config; ///< The configuration we're modifying. + int clicked_button; ///< The button we clicked. + bool clicked_increase; ///< Whether we clicked the increase or decrease button. + bool clicked_dropdown; ///< Whether the dropdown is open. + bool closing_dropdown; ///< True, if the dropdown list is currently closing. + GUITimer timeout; ///< Timeout for unclicking the button. + int clicked_row; ///< The clicked row of settings. + int line_height; ///< Height of a row in the matrix widget. + Scrollbar *vscroll; ///< Cache of the vertical scrollbar. + typedef std::vector VisibleSettingsList; ///< typdef for a vector of script settings + VisibleSettingsList visible_settings; ///< List of visible AI settings + + /** + * Constructor for the window. + * @param desc The description of the window. + * @param slot The company we're changing the settings for. + */ + ScriptSettingsWindow(WindowDesc *desc, CompanyID slot) : Window(desc), + slot(slot), + clicked_button(-1), + clicked_dropdown(false), + closing_dropdown(false), + timeout(0) + { + this->script_config = GetConfig(slot); + + this->CreateNestedTree(); + this->vscroll = this->GetScrollbar(WID_SCRS_SCROLLBAR); + this->FinishInitNested(slot); // Initializes 'this->line_height' as side effect. + + this->RebuildVisibleSettings(); + } + + /** + * Rebuilds the list of visible settings. AI settings with the flag + * AICONFIG_AI_DEVELOPER set will only be visible if the game setting + * gui.ai_developer_tools is enabled. + */ + void RebuildVisibleSettings() + { + visible_settings.clear(); + + for (const auto &item : *this->script_config->GetConfigList()) { + bool no_hide = (item.flags & SCRIPTCONFIG_DEVELOPER) == 0; + if (no_hide || _settings_client.gui.ai_developer_tools) { + visible_settings.push_back(&item); + } + } + + this->vscroll->SetCount((int)this->visible_settings.size()); + } + + void SetStringParameters(int widget) const override + { + if (widget != WID_SCRS_CAPTION) return; + + SetDParam(0, (this->slot == OWNER_DEITY) ? STR_AI_SETTINGS_CAPTION_GAMESCRIPT : STR_AI_SETTINGS_CAPTION_AI); + } + + void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override + { + if (widget != WID_SCRS_BACKGROUND) return; + + this->line_height = std::max(SETTING_BUTTON_HEIGHT, FONT_HEIGHT_NORMAL) + padding.height; + + resize->width = 1; + resize->height = this->line_height; + size->height = 5 * this->line_height; + } + + void DrawWidget(const Rect &r, int widget) const override + { + if (widget != WID_SCRS_BACKGROUND) return; + + ScriptConfig *config = this->script_config; + VisibleSettingsList::const_iterator it = this->visible_settings.begin(); + int i = 0; + for (; !this->vscroll->IsVisible(i); i++) it++; + + Rect ir = r.Shrink(WidgetDimensions::scaled.framerect); + bool rtl = _current_text_dir == TD_RTL; + Rect br = ir.WithWidth(SETTING_BUTTON_WIDTH, rtl); + Rect tr = ir.Indent(SETTING_BUTTON_WIDTH + WidgetDimensions::scaled.hsep_wide, rtl); + + int y = r.top; + int button_y_offset = (this->line_height - SETTING_BUTTON_HEIGHT) / 2; + int text_y_offset = (this->line_height - FONT_HEIGHT_NORMAL) / 2; + for (; this->vscroll->IsVisible(i) && it != visible_settings.end(); i++, it++) { + const ScriptConfigItem &config_item = **it; + int current_value = config->GetSetting((config_item).name); + bool editable = this->IsEditableItem(config_item); + + StringID str; + TextColour colour; + uint idx = 0; + if (StrEmpty(config_item.description)) { + if (this->slot != OWNER_DEITY && !strcmp(config_item.name, "start_date")) { + /* Build-in translation */ + str = STR_AI_SETTINGS_START_DELAY; + colour = TC_LIGHT_BLUE; + } else { + str = STR_JUST_STRING; + colour = TC_ORANGE; + } + } else { + str = STR_AI_SETTINGS_SETTING; + colour = TC_LIGHT_BLUE; + SetDParamStr(idx++, config_item.description); + } + + if ((config_item.flags & SCRIPTCONFIG_BOOLEAN) != 0) { + DrawBoolButton(br.left, y + button_y_offset, current_value != 0, editable); + SetDParam(idx++, current_value == 0 ? STR_CONFIG_SETTING_OFF : STR_CONFIG_SETTING_ON); + } else { + if (config_item.complete_labels) { + DrawDropDownButton(br.left, y + button_y_offset, COLOUR_YELLOW, this->clicked_row == i && clicked_dropdown, editable); + } else { + DrawArrowButtons(br.left, y + button_y_offset, COLOUR_YELLOW, (this->clicked_button == i) ? 1 + (this->clicked_increase != rtl) : 0, editable && current_value > config_item.min_value, editable && current_value < config_item.max_value); + } + if (config_item.labels != nullptr && config_item.labels->Contains(current_value)) { + SetDParam(idx++, STR_JUST_RAW_STRING); + SetDParamStr(idx++, config_item.labels->Find(current_value)->second); + } else { + SetDParam(idx++, STR_JUST_INT); + SetDParam(idx++, current_value); + } + } + + DrawString(tr.left, tr.right, y + text_y_offset, str, colour); + y += this->line_height; + } + } + + void OnPaint() override + { + if (this->closing_dropdown) { + this->closing_dropdown = false; + this->clicked_dropdown = false; + } + this->DrawWidgets(); + } + + void OnClick(Point pt, int widget, int click_count) override + { + switch (widget) { + case WID_SCRS_BACKGROUND: { + Rect r = this->GetWidget(widget)->GetCurrentRect().Shrink(WidgetDimensions::scaled.matrix, RectPadding::zero); + int num = (pt.y - r.top) / this->line_height + this->vscroll->GetPosition(); + if (num >= (int)this->visible_settings.size()) break; + + VisibleSettingsList::const_iterator it = this->visible_settings.begin(); + for (int i = 0; i < num; i++) it++; + const ScriptConfigItem config_item = **it; + if (!this->IsEditableItem(config_item)) return; + + if (this->clicked_row != num) { + this->CloseChildWindows(WC_QUERY_STRING); + HideDropDownMenu(this); + this->clicked_row = num; + this->clicked_dropdown = false; + } + + bool bool_item = (config_item.flags & SCRIPTCONFIG_BOOLEAN) != 0; + + int x = pt.x - r.left; + if (_current_text_dir == TD_RTL) x = r.Width() - 1 - x; + + /* One of the arrows is clicked (or green/red rect in case of bool value) */ + int old_val = this->script_config->GetSetting(config_item.name); + if (!bool_item && IsInsideMM(x, 0, SETTING_BUTTON_WIDTH) && config_item.complete_labels) { + if (this->clicked_dropdown) { + /* unclick the dropdown */ + HideDropDownMenu(this); + this->clicked_dropdown = false; + this->closing_dropdown = false; + } else { + int rel_y = (pt.y - r.top) % this->line_height; + + Rect wi_rect; + wi_rect.left = pt.x - (_current_text_dir == TD_RTL ? SETTING_BUTTON_WIDTH - 1 - x : x); + wi_rect.right = wi_rect.left + SETTING_BUTTON_WIDTH - 1; + wi_rect.top = pt.y - rel_y + (this->line_height - SETTING_BUTTON_HEIGHT) / 2; + wi_rect.bottom = wi_rect.top + SETTING_BUTTON_HEIGHT - 1; + + /* If the mouse is still held but dragged outside of the dropdown list, keep the dropdown open */ + if (pt.y >= wi_rect.top && pt.y <= wi_rect.bottom) { + this->clicked_dropdown = true; + this->closing_dropdown = false; + + DropDownList list; + for (int i = config_item.min_value; i <= config_item.max_value; i++) { + list.emplace_back(new DropDownListCharStringItem(config_item.labels->Find(i)->second, i, false)); + } + + ShowDropDownListAt(this, std::move(list), old_val, -1, wi_rect, COLOUR_ORANGE, true); + } + } + } else if (IsInsideMM(x, 0, SETTING_BUTTON_WIDTH)) { + int new_val = old_val; + if (bool_item) { + new_val = !new_val; + } else if (x >= SETTING_BUTTON_WIDTH / 2) { + /* Increase button clicked */ + new_val += config_item.step_size; + if (new_val > config_item.max_value) new_val = config_item.max_value; + this->clicked_increase = true; + } else { + /* Decrease button clicked */ + new_val -= config_item.step_size; + if (new_val < config_item.min_value) new_val = config_item.min_value; + this->clicked_increase = false; + } + + if (new_val != old_val) { + this->script_config->SetSetting(config_item.name, new_val); + this->clicked_button = num; + this->timeout.SetInterval(150); + } + } else if (!bool_item && !config_item.complete_labels) { + /* Display a query box so users can enter a custom value. */ + SetDParam(0, old_val); + ShowQueryString(STR_JUST_INT, STR_CONFIG_SETTING_QUERY_CAPTION, 10, this, CS_NUMERAL, QSF_NONE); + } + this->SetDirty(); + break; + } + + case WID_SCRS_ACCEPT: + this->Close(); + break; + + case WID_SCRS_RESET: + this->script_config->ResetEditableSettings(_game_mode == GM_MENU || ((this->slot != OWNER_DEITY) && !Company::IsValidID(this->slot))); + this->SetDirty(); + break; + } + } + + void OnQueryTextFinished(char *str) override + { + if (StrEmpty(str)) return; + int32 value = atoi(str); + + SetValue(value); + } + + void OnDropdownSelect(int widget, int index) override + { + assert(this->clicked_dropdown); + SetValue(index); + } + + void OnDropdownClose(Point pt, int widget, int index, bool instant_close) override + { + /* We cannot raise the dropdown button just yet. OnClick needs some hint, whether + * the same dropdown button was clicked again, and then not open the dropdown again. + * So, we only remember that it was closed, and process it on the next OnPaint, which is + * after OnClick. */ + assert(this->clicked_dropdown); + this->closing_dropdown = true; + this->SetDirty(); + } + + void OnResize() override + { + this->vscroll->SetCapacityFromWidget(this, WID_SCRS_BACKGROUND); + } + + void OnRealtimeTick(uint delta_ms) override + { + if (this->timeout.Elapsed(delta_ms)) { + this->clicked_button = -1; + this->SetDirty(); + } + } + + /** + * Some data on this window has become invalid. + * @param data Information about the changed data. + * @param gui_scope Whether the call is done from GUI scope. You may not do everything when not in GUI scope. See #InvalidateWindowData() for details. + */ + void OnInvalidateData(int data = 0, bool gui_scope = true) override + { + this->RebuildVisibleSettings(); + HideDropDownMenu(this); + this->CloseChildWindows(WC_QUERY_STRING); + } + +private: + bool IsEditableItem(const ScriptConfigItem &config_item) const + { + return _game_mode == GM_MENU + || _game_mode == GM_EDITOR + || ((this->slot != OWNER_DEITY) && !Company::IsValidID(this->slot)) + || (config_item.flags & SCRIPTCONFIG_INGAME) != 0 + || _settings_client.gui.ai_developer_tools; + } + + void SetValue(int value) + { + VisibleSettingsList::const_iterator it = this->visible_settings.begin(); + for (int i = 0; i < this->clicked_row; i++) it++; + const ScriptConfigItem config_item = **it; + if (_game_mode == GM_NORMAL && ((this->slot == OWNER_DEITY) || Company::IsValidID(this->slot)) && (config_item.flags & SCRIPTCONFIG_INGAME) == 0) return; + this->script_config->SetSetting(config_item.name, value); + this->SetDirty(); + } +}; + +/** Widgets for the Script settings window. */ +static const NWidgetPart _nested_script_settings_widgets[] = { + NWidget(NWID_HORIZONTAL), + NWidget(WWT_CLOSEBOX, COLOUR_MAUVE), + NWidget(WWT_CAPTION, COLOUR_MAUVE, WID_SCRS_CAPTION), SetDataTip(STR_AI_SETTINGS_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS), + NWidget(WWT_DEFSIZEBOX, COLOUR_MAUVE), + EndContainer(), + NWidget(NWID_HORIZONTAL), + NWidget(WWT_MATRIX, COLOUR_MAUVE, WID_SCRS_BACKGROUND), SetMinimalSize(188, 182), SetResize(1, 1), SetFill(1, 0), SetMatrixDataTip(1, 0, STR_NULL), SetScrollbar(WID_SCRS_SCROLLBAR), + NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, WID_SCRS_SCROLLBAR), + EndContainer(), + NWidget(NWID_HORIZONTAL), + NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), + NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, WID_SCRS_ACCEPT), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_AI_SETTINGS_CLOSE, STR_NULL), + NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, WID_SCRS_RESET), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_AI_SETTINGS_RESET, STR_NULL), + EndContainer(), + NWidget(WWT_RESIZEBOX, COLOUR_MAUVE), + EndContainer(), +}; + +/** Window definition for the Script settings window. */ +static WindowDesc _script_settings_desc( + WDP_CENTER, "settings_script", 500, 208, + WC_SCRIPT_SETTINGS, WC_NONE, + 0, + _nested_script_settings_widgets, lengthof(_nested_script_settings_widgets) +); + +/** + * Open the Script settings window to change the Script settings for a Script. + * @param slot The CompanyID of the Script to change the settings. + */ +void ShowScriptSettingsWindow(CompanyID slot) +{ + CloseWindowByClass(WC_SCRIPT_LIST); + CloseWindowByClass(WC_SCRIPT_SETTINGS); + new ScriptSettingsWindow(&_script_settings_desc, slot); +} + + +/** Window for displaying the textfile of a AI. */ +struct ScriptTextfileWindow : public TextfileWindow { + CompanyID slot; ///< View the textfile of this CompanyID slot. + + ScriptTextfileWindow(TextfileType file_type, CompanyID slot) : TextfileWindow(file_type), slot(slot) + { + this->OnInvalidateData(); + } + + void SetStringParameters(int widget) const override + { + if (widget == WID_TF_CAPTION) { + SetDParam(0, (slot == OWNER_DEITY) ? STR_CONTENT_TYPE_GAME_SCRIPT : STR_CONTENT_TYPE_AI); + SetDParamStr(1, GetConfig(slot)->GetInfo()->GetName()); + } + } + + void OnInvalidateData(int data = 0, bool gui_scope = true) override + { + const char *textfile = GetConfig(slot)->GetTextfile(file_type, slot); + if (textfile == nullptr) { + this->Close(); + } else { + this->LoadTextfile(textfile, (slot == OWNER_DEITY) ? GAME_DIR : AI_DIR); + } + } +}; + +/** + * Open the Script version of the textfile window. + * @param file_type The type of textfile to display. + * @param slot The slot the Script is using. + */ +void ShowScriptTextfileWindow(TextfileType file_type, CompanyID slot) +{ + CloseWindowById(WC_TEXTFILE, file_type); + new ScriptTextfileWindow(file_type, slot); +} + + +/** + * Set the widget colour of a button based on the + * state of the script. (dead or alive) + * @param button the button to update. + * @param dead true if the script is dead, otherwise false. + * @param paused true if the script is paused, otherwise false. + * @return true if the colour was changed and the window need to be marked as dirty. + */ +static bool SetScriptButtonColour(NWidgetCore &button, bool dead, bool paused) +{ + /* Dead scripts are indicated with red background and + * paused scripts are indicated with yellow background. */ + Colours colour = dead ? COLOUR_RED : + (paused ? COLOUR_YELLOW : COLOUR_GREY); + if (button.colour != colour) { + button.colour = colour; + return true; + } + return false; +} + +/** + * Window with everything an AI prints via ScriptLog. + */ +struct ScriptDebugWindow : public Window { + static const uint MAX_BREAK_STR_STRING_LENGTH = 256; ///< Maximum length of the break string. + + static CompanyID script_debug_company; ///< The AI that is (was last) being debugged. + int redraw_timer; ///< Timer for redrawing the window, otherwise it'll happen every tick. + int last_vscroll_pos; ///< Last position of the scrolling. + bool autoscroll; ///< Whether automatically scrolling should be enabled or not. + bool show_break_box; ///< Whether the break/debug box is visible. + static bool break_check_enabled; ///< Stop an AI when it prints a matching string + static char break_string[MAX_BREAK_STR_STRING_LENGTH]; ///< The string to match to the AI output + QueryString break_editbox; ///< Break editbox + static StringFilter break_string_filter; ///< Log filter for break. + static bool case_sensitive_break_check; ///< Is the matching done case-sensitive + int highlight_row; ///< The output row that matches the given string, or -1 + Scrollbar *vscroll; ///< Cache of the vertical scrollbar. + + ScriptLog::LogData *GetLogPointer() const + { + if (script_debug_company == OWNER_DEITY) return (ScriptLog::LogData *)Game::GetInstance()->GetLogPointer(); + return (ScriptLog::LogData *)Company::Get(script_debug_company)->ai_instance->GetLogPointer(); + } + + /** + * Check whether the currently selected AI/GS is dead. + * @return true if dead. + */ + bool IsDead() const + { + if (script_debug_company == OWNER_DEITY) { + GameInstance *game = Game::GetInstance(); + return game == nullptr || game->IsDead(); + } + return !Company::IsValidAiID(script_debug_company) || Company::Get(script_debug_company)->ai_instance->IsDead(); + } + + /** + * Check whether a company is a valid AI company or GS. + * @param company Company to check for validity. + * @return true if company is valid for debugging. + */ + bool IsValidDebugCompany(CompanyID company) const + { + switch (company) { + case INVALID_COMPANY: return false; + case OWNER_DEITY: return Game::GetInstance() != nullptr; + default: return Company::IsValidAiID(company); + } + } + + /** + * Ensure that \c script_debug_company refers to a valid AI company or GS, or is set to #INVALID_COMPANY. + * If no valid company is selected, it selects the first valid AI or GS if any. + */ + void SelectValidDebugCompany() + { + /* Check if the currently selected company is still active. */ + if (this->IsValidDebugCompany(script_debug_company)) return; + + script_debug_company = INVALID_COMPANY; + + for (const Company *c : Company::Iterate()) { + if (c->is_ai) { + ChangeToScript(c->index); + return; + } + } + + /* If no AI is available, see if there is a game script. */ + if (Game::GetInstance() != nullptr) ChangeToScript(OWNER_DEITY); + } + + /** + * Constructor for the window. + * @param desc The description of the window. + * @param number The window number (actually unused). + */ + ScriptDebugWindow(WindowDesc *desc, WindowNumber number) : Window(desc), break_editbox(MAX_BREAK_STR_STRING_LENGTH) + { + this->CreateNestedTree(); + this->vscroll = this->GetScrollbar(WID_SCRD_SCROLLBAR); + this->show_break_box = _settings_client.gui.ai_developer_tools; + this->GetWidget(WID_SCRD_BREAK_STRING_WIDGETS)->SetDisplayedPlane(this->show_break_box ? 0 : SZSP_HORIZONTAL); + this->FinishInitNested(number); + + if (!this->show_break_box) break_check_enabled = false; + + this->last_vscroll_pos = 0; + this->autoscroll = true; + this->highlight_row = -1; + + this->querystrings[WID_SCRD_BREAK_STR_EDIT_BOX] = &this->break_editbox; + + SetWidgetsDisabledState(!this->show_break_box, WID_SCRD_BREAK_STR_ON_OFF_BTN, WID_SCRD_BREAK_STR_EDIT_BOX, WID_SCRD_MATCH_CASE_BTN, WIDGET_LIST_END); + + /* Restore the break string value from static variable */ + this->break_editbox.text.Assign(this->break_string); + + this->SelectValidDebugCompany(); + this->InvalidateData(-1); + } + + void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override + { + if (widget == WID_SCRD_LOG_PANEL) { + resize->height = FONT_HEIGHT_NORMAL + WidgetDimensions::scaled.vsep_normal; + size->height = 14 * resize->height + WidgetDimensions::scaled.framerect.Vertical(); + } + } + + void OnPaint() override + { + this->SelectValidDebugCompany(); + + /* Draw standard stuff */ + this->DrawWidgets(); + + if (this->IsShaded()) return; // Don't draw anything when the window is shaded. + + bool dirty = false; + + /* Paint the company icons */ + for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) { + NWidgetCore *button = this->GetWidget(i + WID_SCRD_COMPANY_BUTTON_START); + + bool valid = Company::IsValidAiID(i); + + /* Check whether the validity of the company changed */ + dirty |= (button->IsDisabled() == valid); + + /* Mark dead/paused AIs by setting the background colour. */ + bool dead = valid && Company::Get(i)->ai_instance->IsDead(); + bool paused = valid && Company::Get(i)->ai_instance->IsPaused(); + /* Re-paint if the button was updated. + * (note that it is intentional that SetScriptButtonColour is always called) */ + dirty |= SetScriptButtonColour(*button, dead, paused); + + /* Draw company icon only for valid AI companies */ + if (!valid) continue; + + byte offset = (i == script_debug_company) ? 1 : 0; + DrawCompanyIcon(i, button->pos_x + button->current_x / 2 - 7 + offset, this->GetWidget(WID_SCRD_COMPANY_BUTTON_START + i)->pos_y + 2 + offset); + } + + /* Set button colour for Game Script. */ + GameInstance *game = Game::GetInstance(); + bool valid = game != nullptr; + bool dead = valid && game->IsDead(); + bool paused = valid && game->IsPaused(); + + NWidgetCore *button = this->GetWidget(WID_SCRD_SCRIPT_GAME); + dirty |= (button->IsDisabled() == valid) || SetScriptButtonColour(*button, dead, paused); + + if (dirty) this->InvalidateData(-1); + + /* If there are no active companies, don't display anything else. */ + if (script_debug_company == INVALID_COMPANY) return; + + ScriptLog::LogData *log = this->GetLogPointer(); + + int scroll_count = (log == nullptr) ? 0 : log->used; + if (this->vscroll->GetCount() != scroll_count) { + this->vscroll->SetCount(scroll_count); + + /* We need a repaint */ + this->SetWidgetDirty(WID_SCRD_SCROLLBAR); + } + + if (log == nullptr) return; + + /* Detect when the user scrolls the window. Enable autoscroll when the + * bottom-most line becomes visible. */ + if (this->last_vscroll_pos != this->vscroll->GetPosition()) { + this->autoscroll = this->vscroll->GetPosition() >= log->used - this->vscroll->GetCapacity(); + } + if (this->autoscroll) { + int scroll_pos = std::max(0, log->used - this->vscroll->GetCapacity()); + if (scroll_pos != this->vscroll->GetPosition()) { + this->vscroll->SetPosition(scroll_pos); + + /* We need a repaint */ + this->SetWidgetDirty(WID_SCRD_SCROLLBAR); + this->SetWidgetDirty(WID_SCRD_LOG_PANEL); + } + } + this->last_vscroll_pos = this->vscroll->GetPosition(); + } + + void SetStringParameters(int widget) const override + { + if (widget != WID_SCRD_NAME_TEXT) return; + + if (script_debug_company == OWNER_DEITY) { + const GameInfo *info = Game::GetInfo(); + assert(info != nullptr); + SetDParam(0, STR_AI_DEBUG_NAME_AND_VERSION); + SetDParamStr(1, info->GetName()); + SetDParam(2, info->GetVersion()); + } else if (script_debug_company == INVALID_COMPANY || !Company::IsValidAiID(script_debug_company)) { + SetDParam(0, STR_EMPTY); + } else { + const AIInfo *info = Company::Get(script_debug_company)->ai_info; + assert(info != nullptr); + SetDParam(0, STR_AI_DEBUG_NAME_AND_VERSION); + SetDParamStr(1, info->GetName()); + SetDParam(2, info->GetVersion()); + } + } + + void DrawWidget(const Rect &r, int widget) const override + { + if (script_debug_company == INVALID_COMPANY) return; + + if (widget != WID_SCRD_LOG_PANEL) return; + + ScriptLog::LogData *log = this->GetLogPointer(); + if (log == nullptr) return; + + Rect br = r.Shrink(WidgetDimensions::scaled.bevel); + Rect tr = r.Shrink(WidgetDimensions::scaled.framerect); + for (int i = this->vscroll->GetPosition(); this->vscroll->IsVisible(i) && i < log->used; i++) { + int pos = (i + log->pos + 1 - log->used + log->count) % log->count; + if (log->lines[pos] == nullptr) break; + + TextColour colour; + switch (log->type[pos]) { + case ScriptLog::LOG_SQ_INFO: colour = TC_BLACK; break; + case ScriptLog::LOG_SQ_ERROR: colour = TC_WHITE; break; + case ScriptLog::LOG_INFO: colour = TC_BLACK; break; + case ScriptLog::LOG_WARNING: colour = TC_YELLOW; break; + case ScriptLog::LOG_ERROR: colour = TC_RED; break; + default: colour = TC_BLACK; break; + } + + /* Check if the current line should be highlighted */ + if (pos == this->highlight_row) { + GfxFillRect(br.left, tr.top, br.right, tr.top + this->resize.step_height - 1, PC_BLACK); + if (colour == TC_BLACK) colour = TC_WHITE; // Make black text readable by inverting it to white. + } + + DrawString(tr, log->lines[pos], colour, SA_LEFT | SA_FORCE); + tr.top += this->resize.step_height; + } + } + + /** + * Change all settings to select another Script. + * @param show_ai The new AI to show. + */ + void ChangeToScript(CompanyID show_script) + { + if (!this->IsValidDebugCompany(show_script)) return; + + script_debug_company = show_script; + + this->highlight_row = -1; // The highlight of one Script make little sense for another Script. + + /* Close AI settings window to prevent confusion */ + CloseWindowByClass(WC_SCRIPT_SETTINGS); + + this->InvalidateData(-1); + + this->autoscroll = true; + this->last_vscroll_pos = this->vscroll->GetPosition(); + } + + void OnClick(Point pt, int widget, int click_count) override + { + /* Also called for hotkeys, so check for disabledness */ + if (this->IsWidgetDisabled(widget)) return; + + /* Check which button is clicked */ + if (IsInsideMM(widget, WID_SCRD_COMPANY_BUTTON_START, WID_SCRD_COMPANY_BUTTON_END + 1)) { + ChangeToScript((CompanyID)(widget - WID_SCRD_COMPANY_BUTTON_START)); + } + + switch (widget) { + case WID_SCRD_SCRIPT_GAME: + ChangeToScript(OWNER_DEITY); + break; + + case WID_SCRD_RELOAD_TOGGLE: + if (script_debug_company == OWNER_DEITY) break; + /* First kill the company of the AI, then start a new one. This should start the current AI again */ + Command::Post(CCA_DELETE, script_debug_company, CRR_MANUAL, INVALID_CLIENT_ID); + Command::Post(CCA_NEW_AI, script_debug_company, CRR_NONE, INVALID_CLIENT_ID); + break; + + case WID_SCRD_SETTINGS: + ShowScriptSettingsWindow(script_debug_company); + break; + + case WID_SCRD_BREAK_STR_ON_OFF_BTN: + this->break_check_enabled = !this->break_check_enabled; + this->InvalidateData(-1); + break; + + case WID_SCRD_MATCH_CASE_BTN: + this->case_sensitive_break_check = !this->case_sensitive_break_check; + this->InvalidateData(-1); + break; + + case WID_SCRD_CONTINUE_BTN: + /* Unpause current AI / game script and mark the corresponding script button dirty. */ + if (!this->IsDead()) { + if (script_debug_company == OWNER_DEITY) { + Game::Unpause(); + } else { + AI::Unpause(script_debug_company); + } + } + + /* If the last AI/Game Script is unpaused, unpause the game too. */ + if ((_pause_mode & PM_PAUSED_NORMAL) == PM_PAUSED_NORMAL) { + bool all_unpaused = !Game::IsPaused(); + if (all_unpaused) { + for (const Company *c : Company::Iterate()) { + if (c->is_ai && AI::IsPaused(c->index)) { + all_unpaused = false; + break; + } + } + if (all_unpaused) { + /* All scripts have been unpaused => unpause the game. */ + Command::Post(PM_PAUSED_NORMAL, false); + } + } + } + + this->highlight_row = -1; + this->InvalidateData(-1); + break; + } + } + + void OnEditboxChanged(int wid) override + { + if (wid != WID_SCRD_BREAK_STR_EDIT_BOX) return; + + /* Save the current string to static member so it can be restored next time the window is opened. */ + strecpy(this->break_string, this->break_editbox.text.buf, lastof(this->break_string)); + break_string_filter.SetFilterTerm(this->break_string); + } + + /** + * Some data on this window has become invalid. + * @param data Information about the changed data. + * This is the company ID of the AI/GS which wrote a new log message, or -1 in other cases. + * @param gui_scope Whether the call is done from GUI scope. You may not do everything when not in GUI scope. See #InvalidateWindowData() for details. + */ + void OnInvalidateData(int data = 0, bool gui_scope = true) override + { + /* If the log message is related to the active company tab, check the break string. + * This needs to be done in gameloop-scope, so the AI is suspended immediately. */ + if (!gui_scope && data == script_debug_company && this->IsValidDebugCompany(script_debug_company) && this->break_check_enabled && !this->break_string_filter.IsEmpty()) { + /* Get the log instance of the active company */ + ScriptLog::LogData *log = this->GetLogPointer(); + + if (log != nullptr) { + this->break_string_filter.ResetState(); + this->break_string_filter.AddLine(log->lines[log->pos]); + if (this->break_string_filter.GetState()) { + /* Pause execution of script. */ + if (!this->IsDead()) { + if (script_debug_company == OWNER_DEITY) { + Game::Pause(); + } else { + AI::Pause(script_debug_company); + } + } + + /* Pause the game. */ + if ((_pause_mode & PM_PAUSED_NORMAL) == PM_UNPAUSED) { + Command::Post(PM_PAUSED_NORMAL, true); + } + + /* Highlight row that matched */ + this->highlight_row = log->pos; + } + } + } + + if (!gui_scope) return; + + this->SelectValidDebugCompany(); + + ScriptLog::LogData *log = script_debug_company != INVALID_COMPANY ? this->GetLogPointer() : nullptr; + this->vscroll->SetCount((log == nullptr) ? 0 : log->used); + + /* Update company buttons */ + for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) { + this->SetWidgetDisabledState(i + WID_SCRD_COMPANY_BUTTON_START, !Company::IsValidAiID(i)); + this->SetWidgetLoweredState(i + WID_SCRD_COMPANY_BUTTON_START, script_debug_company == i); + } + + this->SetWidgetDisabledState(WID_SCRD_SCRIPT_GAME, Game::GetGameInstance() == nullptr); + this->SetWidgetLoweredState(WID_SCRD_SCRIPT_GAME, script_debug_company == OWNER_DEITY); + + this->SetWidgetLoweredState(WID_SCRD_BREAK_STR_ON_OFF_BTN, this->break_check_enabled); + this->SetWidgetLoweredState(WID_SCRD_MATCH_CASE_BTN, this->case_sensitive_break_check); + + this->SetWidgetDisabledState(WID_SCRD_SETTINGS, script_debug_company == INVALID_COMPANY); + extern CompanyID _local_company; + this->SetWidgetDisabledState(WID_SCRD_RELOAD_TOGGLE, script_debug_company == INVALID_COMPANY || script_debug_company == OWNER_DEITY || script_debug_company == _local_company); + this->SetWidgetDisabledState(WID_SCRD_CONTINUE_BTN, script_debug_company == INVALID_COMPANY || + (script_debug_company == OWNER_DEITY ? !Game::IsPaused() : !AI::IsPaused(script_debug_company))); + } + + void OnResize() override + { + this->vscroll->SetCapacityFromWidget(this, WID_SCRD_LOG_PANEL, WidgetDimensions::scaled.framerect.Vertical()); + } + + static HotkeyList hotkeys; +}; + +CompanyID ScriptDebugWindow::script_debug_company = INVALID_COMPANY; +char ScriptDebugWindow::break_string[MAX_BREAK_STR_STRING_LENGTH] = ""; +bool ScriptDebugWindow::break_check_enabled = true; +bool ScriptDebugWindow::case_sensitive_break_check = false; +StringFilter ScriptDebugWindow::break_string_filter(&ScriptDebugWindow::case_sensitive_break_check); + +/** Make a number of rows with buttons for each company for the Script debug window. */ +NWidgetBase *MakeCompanyButtonRowsScriptDebug(int *biggest_index) +{ + return MakeCompanyButtonRows(biggest_index, WID_SCRD_COMPANY_BUTTON_START, WID_SCRD_COMPANY_BUTTON_END, COLOUR_GREY, 8, STR_AI_DEBUG_SELECT_AI_TOOLTIP); +} + +/** + * Handler for global hotkeys of the ScriptDebugWindow. + * @param hotkey Hotkey + * @return ES_HANDLED if hotkey was accepted. + */ +static EventState ScriptDebugGlobalHotkeys(int hotkey) +{ + if (_game_mode != GM_NORMAL) return ES_NOT_HANDLED; + Window *w = ShowScriptDebugWindow(INVALID_COMPANY); + if (w == nullptr) return ES_NOT_HANDLED; + return w->OnHotkey(hotkey); +} + +static Hotkey scriptdebug_hotkeys[] = { + Hotkey('1', "company_1", WID_SCRD_COMPANY_BUTTON_START), + Hotkey('2', "company_2", WID_SCRD_COMPANY_BUTTON_START + 1), + Hotkey('3', "company_3", WID_SCRD_COMPANY_BUTTON_START + 2), + Hotkey('4', "company_4", WID_SCRD_COMPANY_BUTTON_START + 3), + Hotkey('5', "company_5", WID_SCRD_COMPANY_BUTTON_START + 4), + Hotkey('6', "company_6", WID_SCRD_COMPANY_BUTTON_START + 5), + Hotkey('7', "company_7", WID_SCRD_COMPANY_BUTTON_START + 6), + Hotkey('8', "company_8", WID_SCRD_COMPANY_BUTTON_START + 7), + Hotkey('9', "company_9", WID_SCRD_COMPANY_BUTTON_START + 8), + Hotkey((uint16)0, "company_10", WID_SCRD_COMPANY_BUTTON_START + 9), + Hotkey((uint16)0, "company_11", WID_SCRD_COMPANY_BUTTON_START + 10), + Hotkey((uint16)0, "company_12", WID_SCRD_COMPANY_BUTTON_START + 11), + Hotkey((uint16)0, "company_13", WID_SCRD_COMPANY_BUTTON_START + 12), + Hotkey((uint16)0, "company_14", WID_SCRD_COMPANY_BUTTON_START + 13), + Hotkey((uint16)0, "company_15", WID_SCRD_COMPANY_BUTTON_START + 14), + Hotkey('S', "settings", WID_SCRD_SETTINGS), + Hotkey('0', "game_script", WID_SCRD_SCRIPT_GAME), + Hotkey((uint16)0, "reload", WID_SCRD_RELOAD_TOGGLE), + Hotkey('B', "break_toggle", WID_SCRD_BREAK_STR_ON_OFF_BTN), + Hotkey('F', "break_string", WID_SCRD_BREAK_STR_EDIT_BOX), + Hotkey('C', "match_case", WID_SCRD_MATCH_CASE_BTN), + Hotkey(WKC_RETURN, "continue", WID_SCRD_CONTINUE_BTN), + HOTKEY_LIST_END +}; +HotkeyList ScriptDebugWindow::hotkeys("aidebug", scriptdebug_hotkeys, ScriptDebugGlobalHotkeys); + +/** Widgets for the Script debug window. */ +static const NWidgetPart _nested_script_debug_widgets[] = { + NWidget(NWID_HORIZONTAL), + NWidget(WWT_CLOSEBOX, COLOUR_GREY), + NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_AI_DEBUG, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS), + NWidget(WWT_SHADEBOX, COLOUR_GREY), + NWidget(WWT_DEFSIZEBOX, COLOUR_GREY), + NWidget(WWT_STICKYBOX, COLOUR_GREY), + EndContainer(), + NWidget(WWT_PANEL, COLOUR_GREY, WID_SCRD_VIEW), + NWidgetFunction(MakeCompanyButtonRowsScriptDebug), SetPadding(0, 2, 1, 2), + EndContainer(), + NWidget(NWID_HORIZONTAL), + NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_SCRD_SCRIPT_GAME), SetMinimalSize(100, 20), SetResize(1, 0), SetDataTip(STR_AI_GAME_SCRIPT, STR_AI_GAME_SCRIPT_TOOLTIP), + NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_SCRD_NAME_TEXT), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_JUST_STRING, STR_AI_DEBUG_NAME_TOOLTIP), + NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SCRD_SETTINGS), SetMinimalSize(100, 20), SetDataTip(STR_AI_DEBUG_SETTINGS, STR_AI_DEBUG_SETTINGS_TOOLTIP), + NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SCRD_RELOAD_TOGGLE), SetMinimalSize(100, 20), SetDataTip(STR_AI_DEBUG_RELOAD, STR_AI_DEBUG_RELOAD_TOOLTIP), + EndContainer(), + NWidget(NWID_HORIZONTAL), + NWidget(NWID_VERTICAL), + /* Log panel */ + NWidget(WWT_PANEL, COLOUR_GREY, WID_SCRD_LOG_PANEL), SetMinimalSize(287, 180), SetResize(1, 1), SetScrollbar(WID_SCRD_SCROLLBAR), + EndContainer(), + /* Break string widgets */ + NWidget(NWID_SELECTION, INVALID_COLOUR, WID_SCRD_BREAK_STRING_WIDGETS), + NWidget(NWID_HORIZONTAL), + NWidget(WWT_IMGBTN_2, COLOUR_GREY, WID_SCRD_BREAK_STR_ON_OFF_BTN), SetFill(0, 1), SetDataTip(SPR_FLAG_VEH_STOPPED, STR_AI_DEBUG_BREAK_STR_ON_OFF_TOOLTIP), + NWidget(WWT_PANEL, COLOUR_GREY), + NWidget(NWID_HORIZONTAL), + NWidget(WWT_LABEL, COLOUR_GREY), SetPadding(2, 2, 2, 4), SetDataTip(STR_AI_DEBUG_BREAK_ON_LABEL, 0x0), + NWidget(WWT_EDITBOX, COLOUR_GREY, WID_SCRD_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), + EndContainer(), + EndContainer(), + NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_SCRD_MATCH_CASE_BTN), SetMinimalSize(100, 0), SetFill(0, 1), SetDataTip(STR_AI_DEBUG_MATCH_CASE, STR_AI_DEBUG_MATCH_CASE_TOOLTIP), + NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SCRD_CONTINUE_BTN), SetMinimalSize(100, 0), SetFill(0, 1), SetDataTip(STR_AI_DEBUG_CONTINUE, STR_AI_DEBUG_CONTINUE_TOOLTIP), + EndContainer(), + EndContainer(), + EndContainer(), + NWidget(NWID_VERTICAL), + NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_SCRD_SCROLLBAR), + NWidget(WWT_RESIZEBOX, COLOUR_GREY), + EndContainer(), +EndContainer(), +}; + +/** Window definition for the Script debug window. */ +static WindowDesc _script_debug_desc( + WDP_AUTO, "script_debug", 600, 450, + WC_SCRIPT_DEBUG, WC_NONE, + 0, + _nested_script_debug_widgets, lengthof(_nested_script_debug_widgets), + &ScriptDebugWindow::hotkeys +); + +/** + * Open the Script debug window and select the given company. + * @param show_company Display debug information about this AI company. + */ +Window *ShowScriptDebugWindow(CompanyID show_company) +{ + if (!_networking || _network_server) { + ScriptDebugWindow *w = (ScriptDebugWindow *)BringWindowToFrontById(WC_SCRIPT_DEBUG, 0); + if (w == nullptr) w = new ScriptDebugWindow(&_script_debug_desc, 0); + if (show_company != INVALID_COMPANY) w->ChangeToScript(show_company); + return w; + } else { + ShowErrorMessage(STR_ERROR_AI_DEBUG_SERVER_ONLY, INVALID_STRING_ID, WL_INFO); + } + + return nullptr; +} + +/** + * Reset the Script windows to their initial state. + */ +void InitializeScriptGui() +{ + ScriptDebugWindow::script_debug_company = INVALID_COMPANY; +} + +/** Open the AI debug window if one of the AI scripts has crashed. */ +void ShowScriptDebugWindowIfScriptError() +{ + /* Network clients can't debug AIs. */ + if (_networking && !_network_server) return; + + for (const Company *c : Company::Iterate()) { + if (c->is_ai && c->ai_instance->IsDead()) { + ShowScriptDebugWindow(c->index); + break; + } + } + + GameInstance *g = Game::GetGameInstance(); + if (g != nullptr && g->IsDead()) { + ShowScriptDebugWindow(OWNER_DEITY); + } +} diff --git a/src/script/script_gui.h b/src/script/script_gui.h new file mode 100644 index 0000000000..d892feef3a --- /dev/null +++ b/src/script/script_gui.h @@ -0,0 +1,23 @@ +/* + * This file is part of OpenTTD. + * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2. + * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see . + */ + + /** @file script_gui.hpp %Window for configuring the scripts */ + +#ifndef SCRIPT_GUI_HPP +#define SCRIPT_GUI_HPP + +#include "../company_type.h" +#include "../textfile_type.h" + +void ShowScriptListWindow(CompanyID slot); +Window *ShowScriptDebugWindow(CompanyID show_company = INVALID_COMPANY); +void ShowScriptSettingsWindow(CompanyID slot); +void ShowScriptTextfileWindow(TextfileType file_type, CompanyID slot); +void ShowScriptDebugWindowIfScriptError(); +void InitializeScriptGui(); + +#endif /* SCRIPT_GUI_HPP */ diff --git a/src/table/settings/gui_settings.ini b/src/table/settings/gui_settings.ini index a521408be7..7abeed9809 100644 --- a/src/table/settings/gui_settings.ini +++ b/src/table/settings/gui_settings.ini @@ -779,7 +779,7 @@ cat = SC_EXPERT var = gui.ai_developer_tools flags = SF_NOT_IN_SAVE | SF_NO_NETWORK_SYNC def = false -post_cb = [](auto) { InvalidateWindowClassesData(WC_AI_SETTINGS); } +post_cb = [](auto) { InvalidateWindowClassesData(WC_SCRIPT_SETTINGS); } cat = SC_EXPERT [SDTC_BOOL] diff --git a/src/toolbar_gui.cpp b/src/toolbar_gui.cpp index 9d1aad649a..d03371d428 100644 --- a/src/toolbar_gui.cpp +++ b/src/toolbar_gui.cpp @@ -34,6 +34,7 @@ #include "news_gui.h" #include "ai/ai_gui.hpp" #include "game/game_gui.hpp" +#include "script/script_gui.h" #include "tilehighlight_func.h" #include "smallmap_gui.h" #include "graph_gui.h" @@ -1163,7 +1164,7 @@ static CallBackFunction MenuClickHelp(int index) switch (index) { case 0: return PlaceLandBlockInfo(); case 2: IConsoleSwitch(); break; - case 3: ShowAIDebugWindow(); break; + case 3: ShowScriptDebugWindow(); break; case 4: ShowScreenshotWindow(); break; case 5: ShowFramerateWindow(); break; case 6: ShowAboutWindow(); break; @@ -2073,7 +2074,7 @@ struct MainToolbarWindow : Window { case MTHK_BUILD_AIRPORT: ShowBuildAirToolbar(); break; case MTHK_BUILD_TREES: ShowBuildTreesToolbar(); break; case MTHK_MUSIC: ShowMusicWindow(); break; - case MTHK_AI_DEBUG: ShowAIDebugWindow(); break; + case MTHK_SCRIPT_DEBUG: ShowScriptDebugWindow(); break; case MTHK_SMALL_SCREENSHOT: MakeScreenshotWithConfirm(SC_VIEWPORT); break; case MTHK_ZOOMEDIN_SCREENSHOT: MakeScreenshotWithConfirm(SC_ZOOMEDIN); break; case MTHK_DEFAULTZOOM_SCREENSHOT: MakeScreenshotWithConfirm(SC_DEFAULTZOOM); break; @@ -2187,7 +2188,7 @@ static Hotkey maintoolbar_hotkeys[] = { Hotkey(WKC_SHIFT | WKC_F10, "build_airport", MTHK_BUILD_AIRPORT), Hotkey(WKC_SHIFT | WKC_F11, "build_trees", MTHK_BUILD_TREES), Hotkey(WKC_SHIFT | WKC_F12, "music", MTHK_MUSIC), - Hotkey((uint16)0, "ai_debug", MTHK_AI_DEBUG), + Hotkey((uint16)0, "ai_debug", MTHK_SCRIPT_DEBUG), Hotkey(WKC_CTRL | 'S', "small_screenshot", MTHK_SMALL_SCREENSHOT), Hotkey(WKC_CTRL | 'P', "zoomedin_screenshot", MTHK_ZOOMEDIN_SCREENSHOT), Hotkey(WKC_CTRL | 'D', "defaultzoom_screenshot", MTHK_DEFAULTZOOM_SCREENSHOT), diff --git a/src/toolbar_gui.h b/src/toolbar_gui.h index 5b8000f0c1..cede0a7c19 100644 --- a/src/toolbar_gui.h +++ b/src/toolbar_gui.h @@ -41,7 +41,7 @@ enum MainToolbarHotkeys { MTHK_BUILD_TREES, MTHK_MUSIC, MTHK_LANDINFO, - MTHK_AI_DEBUG, + MTHK_SCRIPT_DEBUG, MTHK_SMALL_SCREENSHOT, MTHK_ZOOMEDIN_SCREENSHOT, MTHK_DEFAULTZOOM_SCREENSHOT, diff --git a/src/widgets/CMakeLists.txt b/src/widgets/CMakeLists.txt index f766d1cff7..33e46b6db5 100644 --- a/src/widgets/CMakeLists.txt +++ b/src/widgets/CMakeLists.txt @@ -44,6 +44,7 @@ add_files( rail_widget.h road_widget.h screenshot_widget.h + script_widget.h settings_widget.h sign_widget.h slider.cpp diff --git a/src/widgets/ai_widget.h b/src/widgets/ai_widget.h index f09ab21210..f50b6632c8 100644 --- a/src/widgets/ai_widget.h +++ b/src/widgets/ai_widget.h @@ -10,28 +10,8 @@ #ifndef WIDGETS_AI_WIDGET_H #define WIDGETS_AI_WIDGET_H -#include "../company_type.h" #include "../textfile_type.h" -/** Widgets of the #AIListWindow class. */ -enum AIListWidgets { - WID_AIL_CAPTION, ///< Caption of the window. - WID_AIL_LIST, ///< The matrix with all available AIs. - WID_AIL_SCROLLBAR, ///< Scrollbar next to the AI list. - WID_AIL_INFO_BG, ///< Panel to draw some AI information on. - WID_AIL_ACCEPT, ///< Accept button. - WID_AIL_CANCEL, ///< Cancel button. -}; - -/** Widgets of the #AISettingsWindow class. */ -enum AISettingsWidgets { - WID_AIS_CAPTION, ///< Caption of the window. - WID_AIS_BACKGROUND, ///< Panel to draw the settings on. - WID_AIS_SCROLLBAR, ///< Scrollbar to scroll through all settings. - WID_AIS_ACCEPT, ///< Accept button. - WID_AIS_RESET, ///< Reset button. -}; - /** Widgets of the #AIConfigWindow class. */ enum AIConfigWidgets { WID_AIC_BACKGROUND, ///< Window background. @@ -49,22 +29,4 @@ enum AIConfigWidgets { WID_AIC_CONTENT_DOWNLOAD = WID_AIC_TEXTFILE + TFT_END, ///< Download content button. }; -/** Widgets of the #AIDebugWindow class. */ -enum AIDebugWidgets { - WID_AID_VIEW, ///< The row of company buttons. - WID_AID_NAME_TEXT, ///< Name of the current selected. - WID_AID_SETTINGS, ///< Settings button. - WID_AID_SCRIPT_GAME, ///< Game Script button. - WID_AID_RELOAD_TOGGLE, ///< Reload button. - WID_AID_LOG_PANEL, ///< Panel where the log is in. - WID_AID_SCROLLBAR, ///< Scrollbar of the log panel. - WID_AID_COMPANY_BUTTON_START, ///< Buttons in the VIEW. - WID_AID_COMPANY_BUTTON_END = WID_AID_COMPANY_BUTTON_START + MAX_COMPANIES - 1, ///< Last possible button in the VIEW. - WID_AID_BREAK_STRING_WIDGETS, ///< The panel to handle the breaking on string. - WID_AID_BREAK_STR_ON_OFF_BTN, ///< Enable breaking on string. - WID_AID_BREAK_STR_EDIT_BOX, ///< Edit box for the string to break on. - WID_AID_MATCH_CASE_BTN, ///< Checkbox to use match caching or not. - WID_AID_CONTINUE_BTN, ///< Continue button. -}; - #endif /* WIDGETS_AI_WIDGET_H */ diff --git a/src/widgets/game_widget.h b/src/widgets/game_widget.h index 0d2d0950b3..f791a43749 100644 --- a/src/widgets/game_widget.h +++ b/src/widgets/game_widget.h @@ -5,7 +5,7 @@ * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see . */ -/** @file ai_widget.h Types related to the ai widgets. */ +/** @file game_widget.h Types related to the GS widgets. */ #ifndef WIDGETS_GS_WIDGET_H #define WIDGETS_GS_WIDGET_H diff --git a/src/widgets/script_widget.h b/src/widgets/script_widget.h new file mode 100644 index 0000000000..9c757e12fa --- /dev/null +++ b/src/widgets/script_widget.h @@ -0,0 +1,52 @@ +/* + * This file is part of OpenTTD. + * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2. + * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see . + */ + +/** @file script_widget.h Types related to the script widgets. */ + +#ifndef WIDGETS_SCRIPT_WIDGET_H +#define WIDGETS_SCRIPT_WIDGET_H + +#include "../company_type.h" + +/** Widgets of the #ScriptListWindow class. */ +enum ScriptListWidgets { + WID_SCRL_CAPTION, ///< Caption of the window. + WID_SCRL_LIST, ///< The matrix with all available Scripts. + WID_SCRL_SCROLLBAR, ///< Scrollbar next to the Script list. + WID_SCRL_INFO_BG, ///< Panel to draw some Script information on. + WID_SCRL_ACCEPT, ///< Accept button. + WID_SCRL_CANCEL, ///< Cancel button. +}; + +/** Widgets of the #ScriptSettingsWindow class. */ +enum ScriptSettingsWidgets { + WID_SCRS_CAPTION, ///< Caption of the window. + WID_SCRS_BACKGROUND, ///< Panel to draw the settings on. + WID_SCRS_SCROLLBAR, ///< Scrollbar to scroll through all settings. + WID_SCRS_ACCEPT, ///< Accept button. + WID_SCRS_RESET, ///< Reset button. +}; + +/** Widgets of the #ScriptDebugWindow class. */ +enum ScriptDebugWidgets { + WID_SCRD_VIEW, ///< The row of company buttons. + WID_SCRD_NAME_TEXT, ///< Name of the current selected. + WID_SCRD_SETTINGS, ///< Settings button. + WID_SCRD_SCRIPT_GAME, ///< Game Script button. + WID_SCRD_RELOAD_TOGGLE, ///< Reload button. + WID_SCRD_LOG_PANEL, ///< Panel where the log is in. + WID_SCRD_SCROLLBAR, ///< Scrollbar of the log panel. + WID_SCRD_COMPANY_BUTTON_START, ///< Buttons in the VIEW. + WID_SCRD_COMPANY_BUTTON_END = WID_SCRD_COMPANY_BUTTON_START + MAX_COMPANIES - 1, ///< Last possible button in the VIEW. + WID_SCRD_BREAK_STRING_WIDGETS, ///< The panel to handle the breaking on string. + WID_SCRD_BREAK_STR_ON_OFF_BTN, ///< Enable breaking on string. + WID_SCRD_BREAK_STR_EDIT_BOX, ///< Edit box for the string to break on. + WID_SCRD_MATCH_CASE_BTN, ///< Checkbox to use match caching or not. + WID_SCRD_CONTINUE_BTN, ///< Continue button. +}; + +#endif /* WIDGETS_SCRIPT_WIDGET_H */ diff --git a/src/window.cpp b/src/window.cpp index 0adcef17be..00ed25c7dc 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -1367,8 +1367,8 @@ static uint GetWindowZPriority(WindowClass wc) case WC_CUSTOM_CURRENCY: case WC_NETWORK_WINDOW: case WC_GRF_PARAMETERS: - case WC_AI_LIST: - case WC_AI_SETTINGS: + case WC_SCRIPT_LIST: + case WC_SCRIPT_SETTINGS: case WC_TEXTFILE: ++z_priority; FALLTHROUGH; diff --git a/src/window_type.h b/src/window_type.h index 8b1c0cd80a..a31150f21d 100644 --- a/src/window_type.h +++ b/src/window_type.h @@ -162,10 +162,10 @@ enum WindowClass { /** - * AI settings; %Window numbers: - * - 0 = #AISettingsWidgets + * Script settings; %Window numbers: + * - 0 = #ScriptSettingsWidgets */ - WC_AI_SETTINGS, + WC_SCRIPT_SETTINGS, /** * NewGRF parameters; %Window numbers: @@ -271,10 +271,10 @@ enum WindowClass { WC_SIGN_LIST, /** - * AI list; %Window numbers: - * - 0 = #AIListWidgets + * Scripts list; %Window numbers: + * - 0 = #ScriptListWidgets */ - WC_AI_LIST, + WC_SCRIPT_LIST, /** * Goals list; %Window numbers: @@ -650,10 +650,10 @@ enum WindowClass { /** - * AI debug window; %Window numbers: - * - 0 = #AIDebugWidgets + * Script debug window; %Window numbers: + * - 0 = #ScriptDebugWidgets */ - WC_AI_DEBUG, + WC_SCRIPT_DEBUG, /** * NewGRF inspect (debug); %Window numbers: