Add: allow making heightmap screenshot via console
[openttd-github.git] / src / ai / ai_gui.cpp
blob8db22e41b5c8fdc2401c97adeb16445809c07813
1 /*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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 <http://www.gnu.org/licenses/>.
6 */
8 /** @file ai_gui.cpp %Window for configuring the AIs */
10 #include "../stdafx.h"
11 #include "../table/sprites.h"
12 #include "../error.h"
13 #include "../settings_gui.h"
14 #include "../querystring_gui.h"
15 #include "../stringfilter_type.h"
16 #include "../company_base.h"
17 #include "../company_gui.h"
18 #include "../strings_func.h"
19 #include "../window_func.h"
20 #include "../gfx_func.h"
21 #include "../command_func.h"
22 #include "../network/network.h"
23 #include "../settings_func.h"
24 #include "../network/network_content.h"
25 #include "../textfile_gui.h"
26 #include "../widgets/dropdown_type.h"
27 #include "../widgets/dropdown_func.h"
28 #include "../hotkeys.h"
29 #include "../core/geometry_func.hpp"
30 #include "../guitimer_func.h"
32 #include "ai.hpp"
33 #include "ai_gui.hpp"
34 #include "../script/api/script_log.hpp"
35 #include "ai_config.hpp"
36 #include "ai_info.hpp"
37 #include "ai_instance.hpp"
38 #include "../game/game.hpp"
39 #include "../game/game_config.hpp"
40 #include "../game/game_info.hpp"
41 #include "../game/game_instance.hpp"
43 #include "table/strings.h"
45 #include <vector>
47 #include "../safeguards.h"
49 static ScriptConfig *GetConfig(CompanyID slot)
51 if (slot == OWNER_DEITY) return GameConfig::GetConfig();
52 return AIConfig::GetConfig(slot);
55 /**
56 * Window that let you choose an available AI.
58 struct AIListWindow : public Window {
59 const ScriptInfoList *info_list; ///< The list of Scripts.
60 int selected; ///< The currently selected Script.
61 CompanyID slot; ///< The company we're selecting a new Script for.
62 int line_height; ///< Height of a row in the matrix widget.
63 Scrollbar *vscroll; ///< Cache of the vertical scrollbar.
65 /**
66 * Constructor for the window.
67 * @param desc The description of the window.
68 * @param slot The company we're changing the AI for.
70 AIListWindow(WindowDesc *desc, CompanyID slot) : Window(desc),
71 slot(slot)
73 if (slot == OWNER_DEITY) {
74 this->info_list = Game::GetUniqueInfoList();
75 } else {
76 this->info_list = AI::GetUniqueInfoList();
79 this->CreateNestedTree();
80 this->vscroll = this->GetScrollbar(WID_AIL_SCROLLBAR);
81 this->FinishInitNested(); // Initializes 'this->line_height' as side effect.
83 this->vscroll->SetCount((int)this->info_list->size() + 1);
85 /* Try if we can find the currently selected AI */
86 this->selected = -1;
87 if (GetConfig(slot)->HasScript()) {
88 ScriptInfo *info = GetConfig(slot)->GetInfo();
89 int i = 0;
90 for (const auto &item : *this->info_list) {
91 if (item.second == info) {
92 this->selected = i;
93 break;
96 i++;
101 void SetStringParameters(int widget) const override
103 switch (widget) {
104 case WID_AIL_CAPTION:
105 SetDParam(0, (this->slot == OWNER_DEITY) ? STR_AI_LIST_CAPTION_GAMESCRIPT : STR_AI_LIST_CAPTION_AI);
106 break;
110 void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
112 if (widget == WID_AIL_LIST) {
113 this->line_height = FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM;
115 resize->width = 1;
116 resize->height = this->line_height;
117 size->height = 5 * this->line_height;
121 void DrawWidget(const Rect &r, int widget) const override
123 switch (widget) {
124 case WID_AIL_LIST: {
125 /* Draw a list of all available AIs. */
126 int y = this->GetWidget<NWidgetBase>(WID_AIL_LIST)->pos_y;
127 /* First AI in the list is hardcoded to random */
128 if (this->vscroll->IsVisible(0)) {
129 DrawString(r.left + WD_MATRIX_LEFT, r.right - WD_MATRIX_LEFT, y + WD_MATRIX_TOP, this->slot == OWNER_DEITY ? STR_AI_CONFIG_NONE : STR_AI_CONFIG_RANDOM_AI, this->selected == -1 ? TC_WHITE : TC_ORANGE);
130 y += this->line_height;
132 int i = 0;
133 for (const auto &item : *this->info_list) {
134 i++;
135 if (this->vscroll->IsVisible(i)) {
136 DrawString(r.left + WD_MATRIX_LEFT, r.right - WD_MATRIX_RIGHT, y + WD_MATRIX_TOP, item.second->GetName(), (this->selected == i - 1) ? TC_WHITE : TC_ORANGE);
137 y += this->line_height;
140 break;
142 case WID_AIL_INFO_BG: {
143 AIInfo *selected_info = nullptr;
144 int i = 0;
145 for (const auto &item : *this->info_list) {
146 i++;
147 if (this->selected == i - 1) selected_info = static_cast<AIInfo *>(item.second);
149 /* Some info about the currently selected AI. */
150 if (selected_info != nullptr) {
151 int y = r.top + WD_FRAMERECT_TOP;
152 SetDParamStr(0, selected_info->GetAuthor());
153 DrawString(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, y, STR_AI_LIST_AUTHOR);
154 y += FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
155 SetDParam(0, selected_info->GetVersion());
156 DrawString(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, y, STR_AI_LIST_VERSION);
157 y += FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
158 if (selected_info->GetURL() != nullptr) {
159 SetDParamStr(0, selected_info->GetURL());
160 DrawString(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, y, STR_AI_LIST_URL);
161 y += FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
163 SetDParamStr(0, selected_info->GetDescription());
164 DrawStringMultiLine(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, y, r.bottom - WD_FRAMERECT_BOTTOM, STR_JUST_RAW_STRING, TC_WHITE);
166 break;
172 * Changes the AI of the current slot.
174 void ChangeAI()
176 if (this->selected == -1) {
177 GetConfig(slot)->Change(nullptr);
178 } else {
179 ScriptInfoList::const_iterator it = this->info_list->begin();
180 for (int i = 0; i < this->selected; i++) it++;
181 GetConfig(slot)->Change((*it).second->GetName(), (*it).second->GetVersion());
183 InvalidateWindowData(WC_GAME_OPTIONS, WN_GAME_OPTIONS_AI);
184 InvalidateWindowClassesData(WC_AI_SETTINGS);
185 DeleteWindowByClass(WC_QUERY_STRING);
186 InvalidateWindowClassesData(WC_TEXTFILE);
189 void OnClick(Point pt, int widget, int click_count) override
191 switch (widget) {
192 case WID_AIL_LIST: { // Select one of the AIs
193 int sel = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_AIL_LIST, 0, this->line_height) - 1;
194 if (sel < (int)this->info_list->size()) {
195 this->selected = sel;
196 this->SetDirty();
197 if (click_count > 1) {
198 this->ChangeAI();
199 delete this;
202 break;
205 case WID_AIL_ACCEPT: {
206 this->ChangeAI();
207 delete this;
208 break;
211 case WID_AIL_CANCEL:
212 delete this;
213 break;
217 void OnResize() override
219 this->vscroll->SetCapacityFromWidget(this, WID_AIL_LIST);
223 * Some data on this window has become invalid.
224 * @param data Information about the changed data.
225 * @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.
227 void OnInvalidateData(int data = 0, bool gui_scope = true) override
229 if (_game_mode == GM_NORMAL && Company::IsValidID(this->slot)) {
230 delete this;
231 return;
234 if (!gui_scope) return;
236 this->vscroll->SetCount((int)this->info_list->size() + 1);
238 /* selected goes from -1 .. length of ai list - 1. */
239 this->selected = std::min(this->selected, this->vscroll->GetCount() - 2);
243 /** Widgets for the AI list window. */
244 static const NWidgetPart _nested_ai_list_widgets[] = {
245 NWidget(NWID_HORIZONTAL),
246 NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
247 NWidget(WWT_CAPTION, COLOUR_MAUVE, WID_AIL_CAPTION), SetDataTip(STR_AI_LIST_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
248 NWidget(WWT_DEFSIZEBOX, COLOUR_MAUVE),
249 EndContainer(),
250 NWidget(NWID_HORIZONTAL),
251 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),
252 NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, WID_AIL_SCROLLBAR),
253 EndContainer(),
254 NWidget(WWT_PANEL, COLOUR_MAUVE, WID_AIL_INFO_BG), SetMinimalTextLines(8, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM), SetResize(1, 0),
255 EndContainer(),
256 NWidget(NWID_HORIZONTAL),
257 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
258 NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, WID_AIL_ACCEPT), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_AI_LIST_ACCEPT, STR_AI_LIST_ACCEPT_TOOLTIP),
259 NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, WID_AIL_CANCEL), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_AI_LIST_CANCEL, STR_AI_LIST_CANCEL_TOOLTIP),
260 EndContainer(),
261 NWidget(WWT_RESIZEBOX, COLOUR_MAUVE),
262 EndContainer(),
265 /** Window definition for the ai list window. */
266 static WindowDesc _ai_list_desc(
267 WDP_CENTER, "settings_script_list", 200, 234,
268 WC_AI_LIST, WC_NONE,
270 _nested_ai_list_widgets, lengthof(_nested_ai_list_widgets)
274 * Open the AI list window to chose an AI for the given company slot.
275 * @param slot The slot to change the AI of.
277 static void ShowAIListWindow(CompanyID slot)
279 DeleteWindowByClass(WC_AI_LIST);
280 new AIListWindow(&_ai_list_desc, slot);
284 * Window for settings the parameters of an AI.
286 struct AISettingsWindow : public Window {
287 CompanyID slot; ///< The currently show company's setting.
288 ScriptConfig *ai_config; ///< The configuration we're modifying.
289 int clicked_button; ///< The button we clicked.
290 bool clicked_increase; ///< Whether we clicked the increase or decrease button.
291 bool clicked_dropdown; ///< Whether the dropdown is open.
292 bool closing_dropdown; ///< True, if the dropdown list is currently closing.
293 GUITimer timeout; ///< Timeout for unclicking the button.
294 int clicked_row; ///< The clicked row of settings.
295 int line_height; ///< Height of a row in the matrix widget.
296 Scrollbar *vscroll; ///< Cache of the vertical scrollbar.
297 typedef std::vector<const ScriptConfigItem *> VisibleSettingsList;
298 VisibleSettingsList visible_settings; ///< List of visible AI settings
301 * Constructor for the window.
302 * @param desc The description of the window.
303 * @param slot The company we're changing the settings for.
305 AISettingsWindow(WindowDesc *desc, CompanyID slot) : Window(desc),
306 slot(slot),
307 clicked_button(-1),
308 clicked_dropdown(false),
309 closing_dropdown(false),
310 timeout(0)
312 this->ai_config = GetConfig(slot);
314 this->CreateNestedTree();
315 this->vscroll = this->GetScrollbar(WID_AIS_SCROLLBAR);
316 this->FinishInitNested(slot); // Initializes 'this->line_height' as side effect.
318 this->RebuildVisibleSettings();
321 void SetStringParameters(int widget) const override
323 switch (widget) {
324 case WID_AIS_CAPTION:
325 SetDParam(0, (this->slot == OWNER_DEITY) ? STR_AI_SETTINGS_CAPTION_GAMESCRIPT : STR_AI_SETTINGS_CAPTION_AI);
326 break;
331 * Rebuilds the list of visible settings. AI settings with the flag
332 * AICONFIG_AI_DEVELOPER set will only be visible if the game setting
333 * gui.ai_developer_tools is enabled.
335 void RebuildVisibleSettings()
337 visible_settings.clear();
339 for (const auto &item : *this->ai_config->GetConfigList()) {
340 bool no_hide = (item.flags & SCRIPTCONFIG_DEVELOPER) == 0;
341 if (no_hide || _settings_client.gui.ai_developer_tools) {
342 visible_settings.push_back(&item);
346 this->vscroll->SetCount((int)this->visible_settings.size());
349 void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
351 if (widget == WID_AIS_BACKGROUND) {
352 this->line_height = std::max(SETTING_BUTTON_HEIGHT, FONT_HEIGHT_NORMAL) + WD_MATRIX_TOP + WD_MATRIX_BOTTOM;
354 resize->width = 1;
355 resize->height = this->line_height;
356 size->height = 5 * this->line_height;
360 void DrawWidget(const Rect &r, int widget) const override
362 if (widget != WID_AIS_BACKGROUND) return;
364 ScriptConfig *config = this->ai_config;
365 VisibleSettingsList::const_iterator it = this->visible_settings.begin();
366 int i = 0;
367 for (; !this->vscroll->IsVisible(i); i++) it++;
369 bool rtl = _current_text_dir == TD_RTL;
370 uint buttons_left = rtl ? r.right - SETTING_BUTTON_WIDTH - 3 : r.left + 4;
371 uint text_left = r.left + (rtl ? WD_FRAMERECT_LEFT : SETTING_BUTTON_WIDTH + 8);
372 uint text_right = r.right - (rtl ? SETTING_BUTTON_WIDTH + 8 : WD_FRAMERECT_RIGHT);
375 int y = r.top;
376 int button_y_offset = (this->line_height - SETTING_BUTTON_HEIGHT) / 2;
377 int text_y_offset = (this->line_height - FONT_HEIGHT_NORMAL) / 2;
378 for (; this->vscroll->IsVisible(i) && it != visible_settings.end(); i++, it++) {
379 const ScriptConfigItem &config_item = **it;
380 int current_value = config->GetSetting((config_item).name);
381 bool editable = this->IsEditableItem(config_item);
383 StringID str;
384 TextColour colour;
385 uint idx = 0;
386 if (StrEmpty(config_item.description)) {
387 if (!strcmp(config_item.name, "start_date")) {
388 /* Build-in translation */
389 str = STR_AI_SETTINGS_START_DELAY;
390 colour = TC_LIGHT_BLUE;
391 } else {
392 str = STR_JUST_STRING;
393 colour = TC_ORANGE;
395 } else {
396 str = STR_AI_SETTINGS_SETTING;
397 colour = TC_LIGHT_BLUE;
398 SetDParamStr(idx++, config_item.description);
401 if ((config_item.flags & SCRIPTCONFIG_BOOLEAN) != 0) {
402 DrawBoolButton(buttons_left, y + button_y_offset, current_value != 0, editable);
403 SetDParam(idx++, current_value == 0 ? STR_CONFIG_SETTING_OFF : STR_CONFIG_SETTING_ON);
404 } else {
405 if (config_item.complete_labels) {
406 DrawDropDownButton(buttons_left, y + button_y_offset, COLOUR_YELLOW, this->clicked_row == i && clicked_dropdown, editable);
407 } else {
408 DrawArrowButtons(buttons_left, y + button_y_offset, COLOUR_YELLOW, (this->clicked_button == i) ? 1 + (this->clicked_increase != rtl) : 0, editable && current_value > config_item.min_value, editable && current_value < config_item.max_value);
410 if (config_item.labels != nullptr && config_item.labels->Contains(current_value)) {
411 SetDParam(idx++, STR_JUST_RAW_STRING);
412 SetDParamStr(idx++, config_item.labels->Find(current_value)->second);
413 } else {
414 SetDParam(idx++, STR_JUST_INT);
415 SetDParam(idx++, current_value);
419 DrawString(text_left, text_right, y + text_y_offset, str, colour);
420 y += this->line_height;
424 void OnPaint() override
426 if (this->closing_dropdown) {
427 this->closing_dropdown = false;
428 this->clicked_dropdown = false;
430 this->DrawWidgets();
433 void OnClick(Point pt, int widget, int click_count) override
435 switch (widget) {
436 case WID_AIS_BACKGROUND: {
437 const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_AIS_BACKGROUND);
438 int num = (pt.y - wid->pos_y) / this->line_height + this->vscroll->GetPosition();
439 if (num >= (int)this->visible_settings.size()) break;
441 VisibleSettingsList::const_iterator it = this->visible_settings.begin();
442 for (int i = 0; i < num; i++) it++;
443 const ScriptConfigItem config_item = **it;
444 if (!this->IsEditableItem(config_item)) return;
446 if (this->clicked_row != num) {
447 DeleteChildWindows(WC_QUERY_STRING);
448 HideDropDownMenu(this);
449 this->clicked_row = num;
450 this->clicked_dropdown = false;
453 bool bool_item = (config_item.flags & SCRIPTCONFIG_BOOLEAN) != 0;
455 int x = pt.x - wid->pos_x;
456 if (_current_text_dir == TD_RTL) x = wid->current_x - 1 - x;
457 x -= 4;
459 /* One of the arrows is clicked (or green/red rect in case of bool value) */
460 int old_val = this->ai_config->GetSetting(config_item.name);
461 if (!bool_item && IsInsideMM(x, 0, SETTING_BUTTON_WIDTH) && config_item.complete_labels) {
462 if (this->clicked_dropdown) {
463 /* unclick the dropdown */
464 HideDropDownMenu(this);
465 this->clicked_dropdown = false;
466 this->closing_dropdown = false;
467 } else {
468 const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_AIS_BACKGROUND);
469 int rel_y = (pt.y - (int)wid->pos_y) % this->line_height;
471 Rect wi_rect;
472 wi_rect.left = pt.x - (_current_text_dir == TD_RTL ? SETTING_BUTTON_WIDTH - 1 - x : x);
473 wi_rect.right = wi_rect.left + SETTING_BUTTON_WIDTH - 1;
474 wi_rect.top = pt.y - rel_y + (this->line_height - SETTING_BUTTON_HEIGHT) / 2;
475 wi_rect.bottom = wi_rect.top + SETTING_BUTTON_HEIGHT - 1;
477 /* For dropdowns we also have to check the y position thoroughly, the mouse may not above the just opening dropdown */
478 if (pt.y >= wi_rect.top && pt.y <= wi_rect.bottom) {
479 this->clicked_dropdown = true;
480 this->closing_dropdown = false;
482 DropDownList list;
483 for (int i = config_item.min_value; i <= config_item.max_value; i++) {
484 list.emplace_back(new DropDownListCharStringItem(config_item.labels->Find(i)->second, i, false));
487 ShowDropDownListAt(this, std::move(list), old_val, -1, wi_rect, COLOUR_ORANGE, true);
490 } else if (IsInsideMM(x, 0, SETTING_BUTTON_WIDTH)) {
491 int new_val = old_val;
492 if (bool_item) {
493 new_val = !new_val;
494 } else if (x >= SETTING_BUTTON_WIDTH / 2) {
495 /* Increase button clicked */
496 new_val += config_item.step_size;
497 if (new_val > config_item.max_value) new_val = config_item.max_value;
498 this->clicked_increase = true;
499 } else {
500 /* Decrease button clicked */
501 new_val -= config_item.step_size;
502 if (new_val < config_item.min_value) new_val = config_item.min_value;
503 this->clicked_increase = false;
506 if (new_val != old_val) {
507 this->ai_config->SetSetting(config_item.name, new_val);
508 this->clicked_button = num;
509 this->timeout.SetInterval(150);
511 } else if (!bool_item && !config_item.complete_labels) {
512 /* Display a query box so users can enter a custom value. */
513 SetDParam(0, old_val);
514 ShowQueryString(STR_JUST_INT, STR_CONFIG_SETTING_QUERY_CAPTION, 10, this, CS_NUMERAL, QSF_NONE);
516 this->SetDirty();
517 break;
520 case WID_AIS_ACCEPT:
521 delete this;
522 break;
524 case WID_AIS_RESET:
525 this->ai_config->ResetEditableSettings(_game_mode == GM_MENU || ((this->slot != OWNER_DEITY) && !Company::IsValidID(this->slot)));
526 this->SetDirty();
527 break;
531 void OnQueryTextFinished(char *str) override
533 if (StrEmpty(str)) return;
534 VisibleSettingsList::const_iterator it = this->visible_settings.begin();
535 for (int i = 0; i < this->clicked_row; i++) it++;
536 const ScriptConfigItem config_item = **it;
537 if (_game_mode == GM_NORMAL && ((this->slot == OWNER_DEITY) || Company::IsValidID(this->slot)) && (config_item.flags & SCRIPTCONFIG_INGAME) == 0) return;
538 int32 value = atoi(str);
539 this->ai_config->SetSetting(config_item.name, value);
540 this->SetDirty();
543 void OnDropdownSelect(int widget, int index) override
545 assert(this->clicked_dropdown);
546 VisibleSettingsList::const_iterator it = this->visible_settings.begin();
547 for (int i = 0; i < this->clicked_row; i++) it++;
548 const ScriptConfigItem config_item = **it;
549 if (_game_mode == GM_NORMAL && ((this->slot == OWNER_DEITY) || Company::IsValidID(this->slot)) && (config_item.flags & SCRIPTCONFIG_INGAME) == 0) return;
550 this->ai_config->SetSetting(config_item.name, index);
551 this->SetDirty();
554 void OnDropdownClose(Point pt, int widget, int index, bool instant_close) override
556 /* We cannot raise the dropdown button just yet. OnClick needs some hint, whether
557 * the same dropdown button was clicked again, and then not open the dropdown again.
558 * So, we only remember that it was closed, and process it on the next OnPaint, which is
559 * after OnClick. */
560 assert(this->clicked_dropdown);
561 this->closing_dropdown = true;
562 this->SetDirty();
565 void OnResize() override
567 this->vscroll->SetCapacityFromWidget(this, WID_AIS_BACKGROUND);
570 void OnRealtimeTick(uint delta_ms) override
572 if (this->timeout.Elapsed(delta_ms)) {
573 this->clicked_button = -1;
574 this->SetDirty();
579 * Some data on this window has become invalid.
580 * @param data Information about the changed data.
581 * @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.
583 void OnInvalidateData(int data = 0, bool gui_scope = true) override
585 this->RebuildVisibleSettings();
586 HideDropDownMenu(this);
587 DeleteChildWindows(WC_QUERY_STRING);
590 private:
591 bool IsEditableItem(const ScriptConfigItem &config_item) const
593 return _game_mode == GM_MENU || ((this->slot != OWNER_DEITY) && !Company::IsValidID(this->slot)) || (config_item.flags & SCRIPTCONFIG_INGAME) != 0;
597 /** Widgets for the AI settings window. */
598 static const NWidgetPart _nested_ai_settings_widgets[] = {
599 NWidget(NWID_HORIZONTAL),
600 NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
601 NWidget(WWT_CAPTION, COLOUR_MAUVE, WID_AIS_CAPTION), SetDataTip(STR_AI_SETTINGS_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
602 NWidget(WWT_DEFSIZEBOX, COLOUR_MAUVE),
603 EndContainer(),
604 NWidget(NWID_HORIZONTAL),
605 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),
606 NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, WID_AIS_SCROLLBAR),
607 EndContainer(),
608 NWidget(NWID_HORIZONTAL),
609 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
610 NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, WID_AIS_ACCEPT), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_AI_SETTINGS_CLOSE, STR_NULL),
611 NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, WID_AIS_RESET), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_AI_SETTINGS_RESET, STR_NULL),
612 EndContainer(),
613 NWidget(WWT_RESIZEBOX, COLOUR_MAUVE),
614 EndContainer(),
617 /** Window definition for the AI settings window. */
618 static WindowDesc _ai_settings_desc(
619 WDP_CENTER, "settings_script", 500, 208,
620 WC_AI_SETTINGS, WC_NONE,
622 _nested_ai_settings_widgets, lengthof(_nested_ai_settings_widgets)
626 * Open the AI settings window to change the AI settings for an AI.
627 * @param slot The CompanyID of the AI to change the settings.
629 static void ShowAISettingsWindow(CompanyID slot)
631 DeleteWindowByClass(WC_AI_LIST);
632 DeleteWindowByClass(WC_AI_SETTINGS);
633 new AISettingsWindow(&_ai_settings_desc, slot);
637 /** Window for displaying the textfile of a AI. */
638 struct ScriptTextfileWindow : public TextfileWindow {
639 CompanyID slot; ///< View the textfile of this CompanyID slot.
641 ScriptTextfileWindow(TextfileType file_type, CompanyID slot) : TextfileWindow(file_type), slot(slot)
643 this->OnInvalidateData();
646 void SetStringParameters(int widget) const override
648 if (widget == WID_TF_CAPTION) {
649 SetDParam(0, (slot == OWNER_DEITY) ? STR_CONTENT_TYPE_GAME_SCRIPT : STR_CONTENT_TYPE_AI);
650 SetDParamStr(1, GetConfig(slot)->GetInfo()->GetName());
654 void OnInvalidateData(int data = 0, bool gui_scope = true) override
656 const char *textfile = GetConfig(slot)->GetTextfile(file_type, slot);
657 if (textfile == nullptr) {
658 delete this;
659 } else {
660 this->LoadTextfile(textfile, (slot == OWNER_DEITY) ? GAME_DIR : AI_DIR);
666 * Open the AI version of the textfile window.
667 * @param file_type The type of textfile to display.
668 * @param slot The slot the Script is using.
670 void ShowScriptTextfileWindow(TextfileType file_type, CompanyID slot)
672 DeleteWindowById(WC_TEXTFILE, file_type);
673 new ScriptTextfileWindow(file_type, slot);
677 /** Widgets for the configure AI window. */
678 static const NWidgetPart _nested_ai_config_widgets[] = {
679 NWidget(NWID_HORIZONTAL),
680 NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
681 NWidget(WWT_CAPTION, COLOUR_MAUVE), SetDataTip(STR_AI_CONFIG_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
682 EndContainer(),
683 NWidget(WWT_PANEL, COLOUR_MAUVE, WID_AIC_BACKGROUND),
684 NWidget(NWID_VERTICAL), SetPIP(4, 4, 4),
685 NWidget(NWID_HORIZONTAL), SetPIP(7, 0, 7),
686 NWidget(WWT_PUSHARROWBTN, COLOUR_YELLOW, WID_AIC_DECREASE), SetFill(0, 1), SetDataTip(AWV_DECREASE, STR_NULL),
687 NWidget(WWT_PUSHARROWBTN, COLOUR_YELLOW, WID_AIC_INCREASE), SetFill(0, 1), SetDataTip(AWV_INCREASE, STR_NULL),
688 NWidget(NWID_SPACER), SetMinimalSize(6, 0),
689 NWidget(WWT_TEXT, COLOUR_MAUVE, WID_AIC_NUMBER), SetDataTip(STR_DIFFICULTY_LEVEL_SETTING_MAXIMUM_NO_COMPETITORS, STR_NULL), SetFill(1, 0), SetPadding(1, 0, 0, 0),
690 EndContainer(),
691 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(7, 0, 7),
692 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_AIC_MOVE_UP), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_AI_CONFIG_MOVE_UP, STR_AI_CONFIG_MOVE_UP_TOOLTIP),
693 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_AIC_MOVE_DOWN), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_AI_CONFIG_MOVE_DOWN, STR_AI_CONFIG_MOVE_DOWN_TOOLTIP),
694 EndContainer(),
695 EndContainer(),
696 NWidget(WWT_FRAME, COLOUR_MAUVE), SetDataTip(STR_AI_CONFIG_AI, STR_NULL), SetPadding(0, 5, 0, 5),
697 NWidget(NWID_HORIZONTAL),
698 NWidget(WWT_MATRIX, COLOUR_MAUVE, WID_AIC_LIST), SetMinimalSize(288, 112), SetFill(1, 0), SetMatrixDataTip(1, 8, STR_AI_CONFIG_AILIST_TOOLTIP), SetScrollbar(WID_AIC_SCROLLBAR),
699 NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, WID_AIC_SCROLLBAR),
700 EndContainer(),
701 EndContainer(),
702 NWidget(NWID_SPACER), SetMinimalSize(0, 9),
703 NWidget(WWT_FRAME, COLOUR_MAUVE), SetDataTip(STR_AI_CONFIG_GAMESCRIPT, STR_NULL), SetPadding(0, 5, 4, 5),
704 NWidget(WWT_MATRIX, COLOUR_MAUVE, WID_AIC_GAMELIST), SetMinimalSize(288, 14), SetFill(1, 0), SetMatrixDataTip(1, 1, STR_AI_CONFIG_GAMELIST_TOOLTIP),
705 EndContainer(),
706 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(7, 0, 7),
707 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_AIC_CHANGE), SetFill(1, 0), SetMinimalSize(93, 12), SetDataTip(STR_AI_CONFIG_CHANGE, STR_AI_CONFIG_CHANGE_TOOLTIP),
708 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_AIC_CONFIGURE), SetFill(1, 0), SetMinimalSize(93, 12), SetDataTip(STR_AI_CONFIG_CONFIGURE, STR_AI_CONFIG_CONFIGURE_TOOLTIP),
709 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_AIC_CLOSE), SetFill(1, 0), SetMinimalSize(93, 12), SetDataTip(STR_AI_SETTINGS_CLOSE, STR_NULL),
710 EndContainer(),
711 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(7, 0, 7),
712 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_AIC_TEXTFILE + TFT_README), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_TEXTFILE_VIEW_README, STR_NULL),
713 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_AIC_TEXTFILE + TFT_CHANGELOG), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_TEXTFILE_VIEW_CHANGELOG, STR_NULL),
714 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_AIC_TEXTFILE + TFT_LICENSE), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_TEXTFILE_VIEW_LICENCE, STR_NULL),
715 EndContainer(),
716 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_AIC_CONTENT_DOWNLOAD), SetFill(1, 0), SetMinimalSize(279, 12), SetPadding(0, 7, 9, 7), SetDataTip(STR_INTRO_ONLINE_CONTENT, STR_INTRO_TOOLTIP_ONLINE_CONTENT),
717 EndContainer(),
720 /** Window definition for the configure AI window. */
721 static WindowDesc _ai_config_desc(
722 WDP_CENTER, "settings_script_config", 0, 0,
723 WC_GAME_OPTIONS, WC_NONE,
725 _nested_ai_config_widgets, lengthof(_nested_ai_config_widgets)
729 * Window to configure which AIs will start.
731 struct AIConfigWindow : public Window {
732 CompanyID selected_slot; ///< The currently selected AI slot or \c INVALID_COMPANY.
733 int line_height; ///< Height of a single AI-name line.
734 Scrollbar *vscroll; ///< Cache of the vertical scrollbar.
736 AIConfigWindow() : Window(&_ai_config_desc)
738 this->InitNested(WN_GAME_OPTIONS_AI); // Initializes 'this->line_height' as a side effect.
739 this->vscroll = this->GetScrollbar(WID_AIC_SCROLLBAR);
740 this->selected_slot = INVALID_COMPANY;
741 NWidgetCore *nwi = this->GetWidget<NWidgetCore>(WID_AIC_LIST);
742 this->vscroll->SetCapacity(nwi->current_y / this->line_height);
743 this->vscroll->SetCount(MAX_COMPANIES);
744 this->OnInvalidateData(0);
747 ~AIConfigWindow()
749 DeleteWindowByClass(WC_AI_LIST);
750 DeleteWindowByClass(WC_AI_SETTINGS);
753 void SetStringParameters(int widget) const override
755 switch (widget) {
756 case WID_AIC_NUMBER:
757 SetDParam(0, GetGameSettings().difficulty.max_no_competitors);
758 break;
759 case WID_AIC_CHANGE:
760 switch (selected_slot) {
761 case OWNER_DEITY:
762 SetDParam(0, STR_AI_CONFIG_CHANGE_GAMESCRIPT);
763 break;
765 case INVALID_COMPANY:
766 SetDParam(0, STR_AI_CONFIG_CHANGE_NONE);
767 break;
769 default:
770 SetDParam(0, STR_AI_CONFIG_CHANGE_AI);
771 break;
773 break;
777 void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
779 switch (widget) {
780 case WID_AIC_GAMELIST:
781 this->line_height = FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM;
782 size->height = 1 * this->line_height;
783 break;
785 case WID_AIC_LIST:
786 this->line_height = FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM;
787 size->height = 8 * this->line_height;
788 break;
790 case WID_AIC_CHANGE: {
791 SetDParam(0, STR_AI_CONFIG_CHANGE_GAMESCRIPT);
792 Dimension dim = GetStringBoundingBox(STR_AI_CONFIG_CHANGE);
794 SetDParam(0, STR_AI_CONFIG_CHANGE_NONE);
795 dim = maxdim(dim, GetStringBoundingBox(STR_AI_CONFIG_CHANGE));
797 SetDParam(0, STR_AI_CONFIG_CHANGE_AI);
798 dim = maxdim(dim, GetStringBoundingBox(STR_AI_CONFIG_CHANGE));
800 dim.width += padding.width;
801 dim.height += padding.height;
802 *size = maxdim(*size, dim);
803 break;
809 * Can the AI config in the given company slot be edited?
810 * @param slot The slot to query.
811 * @return True if and only if the given AI Config slot can e edited.
813 static bool IsEditable(CompanyID slot)
815 if (slot == OWNER_DEITY) return _game_mode != GM_NORMAL || Game::GetInstance() != nullptr;
817 if (_game_mode != GM_NORMAL) {
818 return slot > 0 && slot <= GetGameSettings().difficulty.max_no_competitors;
820 if (Company::IsValidID(slot)) return false;
822 int max_slot = GetGameSettings().difficulty.max_no_competitors;
823 for (CompanyID cid = COMPANY_FIRST; cid < (CompanyID)max_slot && cid < MAX_COMPANIES; cid++) {
824 if (Company::IsValidHumanID(cid)) max_slot++;
826 return slot < max_slot;
829 void DrawWidget(const Rect &r, int widget) const override
831 switch (widget) {
832 case WID_AIC_GAMELIST: {
833 StringID text = STR_AI_CONFIG_NONE;
835 if (GameConfig::GetConfig()->GetInfo() != nullptr) {
836 SetDParamStr(0, GameConfig::GetConfig()->GetInfo()->GetName());
837 text = STR_JUST_RAW_STRING;
840 DrawString(r.left + 10, r.right - 10, r.top + WD_MATRIX_TOP, text,
841 (this->selected_slot == OWNER_DEITY) ? TC_WHITE : (IsEditable(OWNER_DEITY) ? TC_ORANGE : TC_SILVER));
843 break;
846 case WID_AIC_LIST: {
847 int y = r.top;
848 for (int i = this->vscroll->GetPosition(); this->vscroll->IsVisible(i) && i < MAX_COMPANIES; i++) {
849 StringID text;
851 if ((_game_mode != GM_NORMAL && i == 0) || (_game_mode == GM_NORMAL && Company::IsValidHumanID(i))) {
852 text = STR_AI_CONFIG_HUMAN_PLAYER;
853 } else if (AIConfig::GetConfig((CompanyID)i)->GetInfo() != nullptr) {
854 SetDParamStr(0, AIConfig::GetConfig((CompanyID)i)->GetInfo()->GetName());
855 text = STR_JUST_RAW_STRING;
856 } else {
857 text = STR_AI_CONFIG_RANDOM_AI;
859 DrawString(r.left + 10, r.right - 10, y + WD_MATRIX_TOP, text,
860 (this->selected_slot == i) ? TC_WHITE : (IsEditable((CompanyID)i) ? TC_ORANGE : TC_SILVER));
861 y += this->line_height;
863 break;
868 void OnClick(Point pt, int widget, int click_count) override
870 if (widget >= WID_AIC_TEXTFILE && widget < WID_AIC_TEXTFILE + TFT_END) {
871 if (this->selected_slot == INVALID_COMPANY || GetConfig(this->selected_slot) == nullptr) return;
873 ShowScriptTextfileWindow((TextfileType)(widget - WID_AIC_TEXTFILE), this->selected_slot);
874 return;
877 switch (widget) {
878 case WID_AIC_DECREASE:
879 case WID_AIC_INCREASE: {
880 int new_value;
881 if (widget == WID_AIC_DECREASE) {
882 new_value = std::max(0, GetGameSettings().difficulty.max_no_competitors - 1);
883 } else {
884 new_value = std::min(MAX_COMPANIES - 1, GetGameSettings().difficulty.max_no_competitors + 1);
886 IConsoleSetSetting("difficulty.max_no_competitors", new_value);
887 break;
890 case WID_AIC_GAMELIST: {
891 this->selected_slot = OWNER_DEITY;
892 this->InvalidateData();
893 if (click_count > 1 && this->selected_slot != INVALID_COMPANY && _game_mode != GM_NORMAL) ShowAIListWindow((CompanyID)this->selected_slot);
894 break;
897 case WID_AIC_LIST: { // Select a slot
898 this->selected_slot = (CompanyID)this->vscroll->GetScrolledRowFromWidget(pt.y, this, widget, 0, this->line_height);
899 this->InvalidateData();
900 if (click_count > 1 && this->selected_slot != INVALID_COMPANY) ShowAIListWindow((CompanyID)this->selected_slot);
901 break;
904 case WID_AIC_MOVE_UP:
905 if (IsEditable(this->selected_slot) && IsEditable((CompanyID)(this->selected_slot - 1))) {
906 Swap(GetGameSettings().ai_config[this->selected_slot], GetGameSettings().ai_config[this->selected_slot - 1]);
907 this->selected_slot--;
908 this->vscroll->ScrollTowards(this->selected_slot);
909 this->InvalidateData();
911 break;
913 case WID_AIC_MOVE_DOWN:
914 if (IsEditable(this->selected_slot) && IsEditable((CompanyID)(this->selected_slot + 1))) {
915 Swap(GetGameSettings().ai_config[this->selected_slot], GetGameSettings().ai_config[this->selected_slot + 1]);
916 this->selected_slot++;
917 this->vscroll->ScrollTowards(this->selected_slot);
918 this->InvalidateData();
920 break;
922 case WID_AIC_CHANGE: // choose other AI
923 ShowAIListWindow((CompanyID)this->selected_slot);
924 break;
926 case WID_AIC_CONFIGURE: // change the settings for an AI
927 ShowAISettingsWindow((CompanyID)this->selected_slot);
928 break;
930 case WID_AIC_CLOSE:
931 delete this;
932 break;
934 case WID_AIC_CONTENT_DOWNLOAD:
935 if (!_network_available) {
936 ShowErrorMessage(STR_NETWORK_ERROR_NOTAVAILABLE, INVALID_STRING_ID, WL_ERROR);
937 } else {
938 ShowNetworkContentListWindow(nullptr, CONTENT_TYPE_AI, CONTENT_TYPE_GAME);
940 break;
945 * Some data on this window has become invalid.
946 * @param data Information about the changed data.
947 * @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.
949 void OnInvalidateData(int data = 0, bool gui_scope = true) override
951 if (!IsEditable(this->selected_slot)) {
952 this->selected_slot = INVALID_COMPANY;
955 if (!gui_scope) return;
957 this->SetWidgetDisabledState(WID_AIC_DECREASE, GetGameSettings().difficulty.max_no_competitors == 0);
958 this->SetWidgetDisabledState(WID_AIC_INCREASE, GetGameSettings().difficulty.max_no_competitors == MAX_COMPANIES - 1);
959 this->SetWidgetDisabledState(WID_AIC_CHANGE, (this->selected_slot == OWNER_DEITY && _game_mode == GM_NORMAL) || this->selected_slot == INVALID_COMPANY);
960 this->SetWidgetDisabledState(WID_AIC_CONFIGURE, this->selected_slot == INVALID_COMPANY || GetConfig(this->selected_slot)->GetConfigList()->size() == 0);
961 this->SetWidgetDisabledState(WID_AIC_MOVE_UP, this->selected_slot == OWNER_DEITY || this->selected_slot == INVALID_COMPANY || !IsEditable((CompanyID)(this->selected_slot - 1)));
962 this->SetWidgetDisabledState(WID_AIC_MOVE_DOWN, this->selected_slot == OWNER_DEITY || this->selected_slot == INVALID_COMPANY || !IsEditable((CompanyID)(this->selected_slot + 1)));
964 for (TextfileType tft = TFT_BEGIN; tft < TFT_END; tft++) {
965 this->SetWidgetDisabledState(WID_AIC_TEXTFILE + tft, this->selected_slot == INVALID_COMPANY || (GetConfig(this->selected_slot)->GetTextfile(tft, this->selected_slot) == nullptr));
970 /** Open the AI config window. */
971 void ShowAIConfigWindow()
973 DeleteWindowByClass(WC_GAME_OPTIONS);
974 new AIConfigWindow();
978 * Set the widget colour of a button based on the
979 * state of the script. (dead or alive)
980 * @param button the button to update.
981 * @param dead true if the script is dead, otherwise false.
982 * @param paused true if the script is paused, otherwise false.
983 * @return true if the colour was changed and the window need to be marked as dirty.
985 static bool SetScriptButtonColour(NWidgetCore &button, bool dead, bool paused)
987 /* Dead scripts are indicated with red background and
988 * paused scripts are indicated with yellow background. */
989 Colours colour = dead ? COLOUR_RED :
990 (paused ? COLOUR_YELLOW : COLOUR_GREY);
991 if (button.colour != colour) {
992 button.colour = colour;
993 return true;
995 return false;
999 * Window with everything an AI prints via ScriptLog.
1001 struct AIDebugWindow : public Window {
1002 static const int top_offset; ///< Offset of the text at the top of the WID_AID_LOG_PANEL.
1003 static const int bottom_offset; ///< Offset of the text at the bottom of the WID_AID_LOG_PANEL.
1005 static const uint MAX_BREAK_STR_STRING_LENGTH = 256; ///< Maximum length of the break string.
1007 static CompanyID ai_debug_company; ///< The AI that is (was last) being debugged.
1008 int redraw_timer; ///< Timer for redrawing the window, otherwise it'll happen every tick.
1009 int last_vscroll_pos; ///< Last position of the scrolling.
1010 bool autoscroll; ///< Whether automatically scrolling should be enabled or not.
1011 bool show_break_box; ///< Whether the break/debug box is visible.
1012 static bool break_check_enabled; ///< Stop an AI when it prints a matching string
1013 static char break_string[MAX_BREAK_STR_STRING_LENGTH]; ///< The string to match to the AI output
1014 QueryString break_editbox; ///< Break editbox
1015 static StringFilter break_string_filter; ///< Log filter for break.
1016 static bool case_sensitive_break_check; ///< Is the matching done case-sensitive
1017 int highlight_row; ///< The output row that matches the given string, or -1
1018 Scrollbar *vscroll; ///< Cache of the vertical scrollbar.
1020 ScriptLog::LogData *GetLogPointer() const
1022 if (ai_debug_company == OWNER_DEITY) return (ScriptLog::LogData *)Game::GetInstance()->GetLogPointer();
1023 return (ScriptLog::LogData *)Company::Get(ai_debug_company)->ai_instance->GetLogPointer();
1027 * Check whether the currently selected AI/GS is dead.
1028 * @return true if dead.
1030 bool IsDead() const
1032 if (ai_debug_company == OWNER_DEITY) {
1033 GameInstance *game = Game::GetInstance();
1034 return game == nullptr || game->IsDead();
1036 return !Company::IsValidAiID(ai_debug_company) || Company::Get(ai_debug_company)->ai_instance->IsDead();
1040 * Check whether a company is a valid AI company or GS.
1041 * @param company Company to check for validity.
1042 * @return true if company is valid for debugging.
1044 bool IsValidDebugCompany(CompanyID company) const
1046 switch (company) {
1047 case INVALID_COMPANY: return false;
1048 case OWNER_DEITY: return Game::GetInstance() != nullptr;
1049 default: return Company::IsValidAiID(company);
1054 * Ensure that \c ai_debug_company refers to a valid AI company or GS, or is set to #INVALID_COMPANY.
1055 * If no valid company is selected, it selects the first valid AI or GS if any.
1057 void SelectValidDebugCompany()
1059 /* Check if the currently selected company is still active. */
1060 if (this->IsValidDebugCompany(ai_debug_company)) return;
1062 ai_debug_company = INVALID_COMPANY;
1064 for (const Company *c : Company::Iterate()) {
1065 if (c->is_ai) {
1066 ChangeToAI(c->index);
1067 return;
1071 /* If no AI is available, see if there is a game script. */
1072 if (Game::GetInstance() != nullptr) ChangeToAI(OWNER_DEITY);
1076 * Constructor for the window.
1077 * @param desc The description of the window.
1078 * @param number The window number (actually unused).
1080 AIDebugWindow(WindowDesc *desc, WindowNumber number) : Window(desc), break_editbox(MAX_BREAK_STR_STRING_LENGTH)
1082 this->CreateNestedTree();
1083 this->vscroll = this->GetScrollbar(WID_AID_SCROLLBAR);
1084 this->show_break_box = _settings_client.gui.ai_developer_tools;
1085 this->GetWidget<NWidgetStacked>(WID_AID_BREAK_STRING_WIDGETS)->SetDisplayedPlane(this->show_break_box ? 0 : SZSP_HORIZONTAL);
1086 this->FinishInitNested(number);
1088 if (!this->show_break_box) break_check_enabled = false;
1090 this->last_vscroll_pos = 0;
1091 this->autoscroll = true;
1092 this->highlight_row = -1;
1094 this->querystrings[WID_AID_BREAK_STR_EDIT_BOX] = &this->break_editbox;
1096 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);
1098 /* Restore the break string value from static variable */
1099 this->break_editbox.text.Assign(this->break_string);
1101 this->SelectValidDebugCompany();
1102 this->InvalidateData(-1);
1105 void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
1107 if (widget == WID_AID_LOG_PANEL) {
1108 resize->height = FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
1109 size->height = 14 * resize->height + this->top_offset + this->bottom_offset;
1113 void OnPaint() override
1115 this->SelectValidDebugCompany();
1117 /* Draw standard stuff */
1118 this->DrawWidgets();
1120 if (this->IsShaded()) return; // Don't draw anything when the window is shaded.
1122 bool dirty = false;
1124 /* Paint the company icons */
1125 for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
1126 NWidgetCore *button = this->GetWidget<NWidgetCore>(i + WID_AID_COMPANY_BUTTON_START);
1128 bool valid = Company::IsValidAiID(i);
1130 /* Check whether the validity of the company changed */
1131 dirty |= (button->IsDisabled() == valid);
1133 /* Mark dead/paused AIs by setting the background colour. */
1134 bool dead = valid && Company::Get(i)->ai_instance->IsDead();
1135 bool paused = valid && Company::Get(i)->ai_instance->IsPaused();
1136 /* Re-paint if the button was updated.
1137 * (note that it is intentional that SetScriptButtonColour is always called) */
1138 dirty |= SetScriptButtonColour(*button, dead, paused);
1140 /* Draw company icon only for valid AI companies */
1141 if (!valid) continue;
1143 byte offset = (i == ai_debug_company) ? 1 : 0;
1144 DrawCompanyIcon(i, button->pos_x + button->current_x / 2 - 7 + offset, this->GetWidget<NWidgetBase>(WID_AID_COMPANY_BUTTON_START + i)->pos_y + 2 + offset);
1147 /* Set button colour for Game Script. */
1148 GameInstance *game = Game::GetInstance();
1149 bool valid = game != nullptr;
1150 bool dead = valid && game->IsDead();
1151 bool paused = valid && game->IsPaused();
1153 NWidgetCore *button = this->GetWidget<NWidgetCore>(WID_AID_SCRIPT_GAME);
1154 dirty |= (button->IsDisabled() == valid) || SetScriptButtonColour(*button, dead, paused);
1156 if (dirty) this->InvalidateData(-1);
1158 /* If there are no active companies, don't display anything else. */
1159 if (ai_debug_company == INVALID_COMPANY) return;
1161 ScriptLog::LogData *log = this->GetLogPointer();
1163 int scroll_count = (log == nullptr) ? 0 : log->used;
1164 if (this->vscroll->GetCount() != scroll_count) {
1165 this->vscroll->SetCount(scroll_count);
1167 /* We need a repaint */
1168 this->SetWidgetDirty(WID_AID_SCROLLBAR);
1171 if (log == nullptr) return;
1173 /* Detect when the user scrolls the window. Enable autoscroll when the
1174 * bottom-most line becomes visible. */
1175 if (this->last_vscroll_pos != this->vscroll->GetPosition()) {
1176 this->autoscroll = this->vscroll->GetPosition() >= log->used - this->vscroll->GetCapacity();
1178 if (this->autoscroll) {
1179 int scroll_pos = std::max(0, log->used - this->vscroll->GetCapacity());
1180 if (scroll_pos != this->vscroll->GetPosition()) {
1181 this->vscroll->SetPosition(scroll_pos);
1183 /* We need a repaint */
1184 this->SetWidgetDirty(WID_AID_SCROLLBAR);
1185 this->SetWidgetDirty(WID_AID_LOG_PANEL);
1188 this->last_vscroll_pos = this->vscroll->GetPosition();
1191 void SetStringParameters(int widget) const override
1193 switch (widget) {
1194 case WID_AID_NAME_TEXT:
1195 if (ai_debug_company == OWNER_DEITY) {
1196 const GameInfo *info = Game::GetInfo();
1197 assert(info != nullptr);
1198 SetDParam(0, STR_AI_DEBUG_NAME_AND_VERSION);
1199 SetDParamStr(1, info->GetName());
1200 SetDParam(2, info->GetVersion());
1201 } else if (ai_debug_company == INVALID_COMPANY || !Company::IsValidAiID(ai_debug_company)) {
1202 SetDParam(0, STR_EMPTY);
1203 } else {
1204 const AIInfo *info = Company::Get(ai_debug_company)->ai_info;
1205 assert(info != nullptr);
1206 SetDParam(0, STR_AI_DEBUG_NAME_AND_VERSION);
1207 SetDParamStr(1, info->GetName());
1208 SetDParam(2, info->GetVersion());
1210 break;
1214 void DrawWidget(const Rect &r, int widget) const override
1216 if (ai_debug_company == INVALID_COMPANY) return;
1218 switch (widget) {
1219 case WID_AID_LOG_PANEL: {
1220 ScriptLog::LogData *log = this->GetLogPointer();
1221 if (log == nullptr) return;
1223 int y = this->top_offset;
1224 for (int i = this->vscroll->GetPosition(); this->vscroll->IsVisible(i) && i < log->used; i++) {
1225 int pos = (i + log->pos + 1 - log->used + log->count) % log->count;
1226 if (log->lines[pos] == nullptr) break;
1228 TextColour colour;
1229 switch (log->type[pos]) {
1230 case ScriptLog::LOG_SQ_INFO: colour = TC_BLACK; break;
1231 case ScriptLog::LOG_SQ_ERROR: colour = TC_RED; break;
1232 case ScriptLog::LOG_INFO: colour = TC_BLACK; break;
1233 case ScriptLog::LOG_WARNING: colour = TC_YELLOW; break;
1234 case ScriptLog::LOG_ERROR: colour = TC_RED; break;
1235 default: colour = TC_BLACK; break;
1238 /* Check if the current line should be highlighted */
1239 if (pos == this->highlight_row) {
1240 GfxFillRect(r.left + 1, r.top + y, r.right - 1, r.top + y + this->resize.step_height - WD_PAR_VSEP_NORMAL, PC_BLACK);
1241 if (colour == TC_BLACK) colour = TC_WHITE; // Make black text readable by inverting it to white.
1244 DrawString(r.left + 7, r.right - 7, r.top + y, log->lines[pos], colour, SA_LEFT | SA_FORCE);
1245 y += this->resize.step_height;
1247 break;
1253 * Change all settings to select another AI.
1254 * @param show_ai The new AI to show.
1256 void ChangeToAI(CompanyID show_ai)
1258 if (!this->IsValidDebugCompany(show_ai)) return;
1260 ai_debug_company = show_ai;
1262 this->highlight_row = -1; // The highlight of one AI make little sense for another AI.
1264 /* Close AI settings window to prevent confusion */
1265 DeleteWindowByClass(WC_AI_SETTINGS);
1267 this->InvalidateData(-1);
1269 this->autoscroll = true;
1270 this->last_vscroll_pos = this->vscroll->GetPosition();
1273 void OnClick(Point pt, int widget, int click_count) override
1275 /* Also called for hotkeys, so check for disabledness */
1276 if (this->IsWidgetDisabled(widget)) return;
1278 /* Check which button is clicked */
1279 if (IsInsideMM(widget, WID_AID_COMPANY_BUTTON_START, WID_AID_COMPANY_BUTTON_END + 1)) {
1280 ChangeToAI((CompanyID)(widget - WID_AID_COMPANY_BUTTON_START));
1283 switch (widget) {
1284 case WID_AID_SCRIPT_GAME:
1285 ChangeToAI(OWNER_DEITY);
1286 break;
1288 case WID_AID_RELOAD_TOGGLE:
1289 if (ai_debug_company == OWNER_DEITY) break;
1290 /* First kill the company of the AI, then start a new one. This should start the current AI again */
1291 DoCommandP(0, CCA_DELETE | ai_debug_company << 16 | CRR_MANUAL << 24, 0, CMD_COMPANY_CTRL);
1292 DoCommandP(0, CCA_NEW_AI | ai_debug_company << 16, 0, CMD_COMPANY_CTRL);
1293 break;
1295 case WID_AID_SETTINGS:
1296 ShowAISettingsWindow(ai_debug_company);
1297 break;
1299 case WID_AID_BREAK_STR_ON_OFF_BTN:
1300 this->break_check_enabled = !this->break_check_enabled;
1301 this->InvalidateData(-1);
1302 break;
1304 case WID_AID_MATCH_CASE_BTN:
1305 this->case_sensitive_break_check = !this->case_sensitive_break_check;
1306 this->InvalidateData(-1);
1307 break;
1309 case WID_AID_CONTINUE_BTN:
1310 /* Unpause current AI / game script and mark the corresponding script button dirty. */
1311 if (!this->IsDead()) {
1312 if (ai_debug_company == OWNER_DEITY) {
1313 Game::Unpause();
1314 } else {
1315 AI::Unpause(ai_debug_company);
1319 /* If the last AI/Game Script is unpaused, unpause the game too. */
1320 if ((_pause_mode & PM_PAUSED_NORMAL) == PM_PAUSED_NORMAL) {
1321 bool all_unpaused = !Game::IsPaused();
1322 if (all_unpaused) {
1323 for (const Company *c : Company::Iterate()) {
1324 if (c->is_ai && AI::IsPaused(c->index)) {
1325 all_unpaused = false;
1326 break;
1329 if (all_unpaused) {
1330 /* All scripts have been unpaused => unpause the game. */
1331 DoCommandP(0, PM_PAUSED_NORMAL, 0, CMD_PAUSE);
1336 this->highlight_row = -1;
1337 this->InvalidateData(-1);
1338 break;
1342 void OnEditboxChanged(int wid) override
1344 if (wid == WID_AID_BREAK_STR_EDIT_BOX) {
1345 /* Save the current string to static member so it can be restored next time the window is opened. */
1346 strecpy(this->break_string, this->break_editbox.text.buf, lastof(this->break_string));
1347 break_string_filter.SetFilterTerm(this->break_string);
1352 * Some data on this window has become invalid.
1353 * @param data Information about the changed data.
1354 * This is the company ID of the AI/GS which wrote a new log message, or -1 in other cases.
1355 * @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.
1357 void OnInvalidateData(int data = 0, bool gui_scope = true) override
1359 /* If the log message is related to the active company tab, check the break string.
1360 * This needs to be done in gameloop-scope, so the AI is suspended immediately. */
1361 if (!gui_scope && data == ai_debug_company && this->IsValidDebugCompany(ai_debug_company) && this->break_check_enabled && !this->break_string_filter.IsEmpty()) {
1362 /* Get the log instance of the active company */
1363 ScriptLog::LogData *log = this->GetLogPointer();
1365 if (log != nullptr) {
1366 this->break_string_filter.ResetState();
1367 this->break_string_filter.AddLine(log->lines[log->pos]);
1368 if (this->break_string_filter.GetState()) {
1369 /* Pause execution of script. */
1370 if (!this->IsDead()) {
1371 if (ai_debug_company == OWNER_DEITY) {
1372 Game::Pause();
1373 } else {
1374 AI::Pause(ai_debug_company);
1378 /* Pause the game. */
1379 if ((_pause_mode & PM_PAUSED_NORMAL) == PM_UNPAUSED) {
1380 DoCommandP(0, PM_PAUSED_NORMAL, 1, CMD_PAUSE);
1383 /* Highlight row that matched */
1384 this->highlight_row = log->pos;
1389 if (!gui_scope) return;
1391 this->SelectValidDebugCompany();
1393 ScriptLog::LogData *log = ai_debug_company != INVALID_COMPANY ? this->GetLogPointer() : nullptr;
1394 this->vscroll->SetCount((log == nullptr) ? 0 : log->used);
1396 /* Update company buttons */
1397 for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
1398 this->SetWidgetDisabledState(i + WID_AID_COMPANY_BUTTON_START, !Company::IsValidAiID(i));
1399 this->SetWidgetLoweredState(i + WID_AID_COMPANY_BUTTON_START, ai_debug_company == i);
1402 this->SetWidgetDisabledState(WID_AID_SCRIPT_GAME, Game::GetGameInstance() == nullptr);
1403 this->SetWidgetLoweredState(WID_AID_SCRIPT_GAME, ai_debug_company == OWNER_DEITY);
1405 this->SetWidgetLoweredState(WID_AID_BREAK_STR_ON_OFF_BTN, this->break_check_enabled);
1406 this->SetWidgetLoweredState(WID_AID_MATCH_CASE_BTN, this->case_sensitive_break_check);
1408 this->SetWidgetDisabledState(WID_AID_SETTINGS, ai_debug_company == INVALID_COMPANY);
1409 extern CompanyID _local_company;
1410 this->SetWidgetDisabledState(WID_AID_RELOAD_TOGGLE, ai_debug_company == INVALID_COMPANY || ai_debug_company == OWNER_DEITY || ai_debug_company == _local_company);
1411 this->SetWidgetDisabledState(WID_AID_CONTINUE_BTN, ai_debug_company == INVALID_COMPANY ||
1412 (ai_debug_company == OWNER_DEITY ? !Game::IsPaused() : !AI::IsPaused(ai_debug_company)));
1415 void OnResize() override
1417 this->vscroll->SetCapacityFromWidget(this, WID_AID_LOG_PANEL);
1420 static HotkeyList hotkeys;
1423 const int AIDebugWindow::top_offset = WD_FRAMERECT_TOP + 2;
1424 const int AIDebugWindow::bottom_offset = WD_FRAMERECT_BOTTOM;
1425 CompanyID AIDebugWindow::ai_debug_company = INVALID_COMPANY;
1426 char AIDebugWindow::break_string[MAX_BREAK_STR_STRING_LENGTH] = "";
1427 bool AIDebugWindow::break_check_enabled = true;
1428 bool AIDebugWindow::case_sensitive_break_check = false;
1429 StringFilter AIDebugWindow::break_string_filter(&AIDebugWindow::case_sensitive_break_check);
1431 /** Make a number of rows with buttons for each company for the AI debug window. */
1432 NWidgetBase *MakeCompanyButtonRowsAIDebug(int *biggest_index)
1434 return MakeCompanyButtonRows(biggest_index, WID_AID_COMPANY_BUTTON_START, WID_AID_COMPANY_BUTTON_END, 8, STR_AI_DEBUG_SELECT_AI_TOOLTIP);
1438 * Handler for global hotkeys of the AIDebugWindow.
1439 * @param hotkey Hotkey
1440 * @return ES_HANDLED if hotkey was accepted.
1442 static EventState AIDebugGlobalHotkeys(int hotkey)
1444 if (_game_mode != GM_NORMAL) return ES_NOT_HANDLED;
1445 Window *w = ShowAIDebugWindow(INVALID_COMPANY);
1446 if (w == nullptr) return ES_NOT_HANDLED;
1447 return w->OnHotkey(hotkey);
1450 static Hotkey aidebug_hotkeys[] = {
1451 Hotkey('1', "company_1", WID_AID_COMPANY_BUTTON_START),
1452 Hotkey('2', "company_2", WID_AID_COMPANY_BUTTON_START + 1),
1453 Hotkey('3', "company_3", WID_AID_COMPANY_BUTTON_START + 2),
1454 Hotkey('4', "company_4", WID_AID_COMPANY_BUTTON_START + 3),
1455 Hotkey('5', "company_5", WID_AID_COMPANY_BUTTON_START + 4),
1456 Hotkey('6', "company_6", WID_AID_COMPANY_BUTTON_START + 5),
1457 Hotkey('7', "company_7", WID_AID_COMPANY_BUTTON_START + 6),
1458 Hotkey('8', "company_8", WID_AID_COMPANY_BUTTON_START + 7),
1459 Hotkey('9', "company_9", WID_AID_COMPANY_BUTTON_START + 8),
1460 Hotkey((uint16)0, "company_10", WID_AID_COMPANY_BUTTON_START + 9),
1461 Hotkey((uint16)0, "company_11", WID_AID_COMPANY_BUTTON_START + 10),
1462 Hotkey((uint16)0, "company_12", WID_AID_COMPANY_BUTTON_START + 11),
1463 Hotkey((uint16)0, "company_13", WID_AID_COMPANY_BUTTON_START + 12),
1464 Hotkey((uint16)0, "company_14", WID_AID_COMPANY_BUTTON_START + 13),
1465 Hotkey((uint16)0, "company_15", WID_AID_COMPANY_BUTTON_START + 14),
1466 Hotkey('S', "settings", WID_AID_SETTINGS),
1467 Hotkey('0', "game_script", WID_AID_SCRIPT_GAME),
1468 Hotkey((uint16)0, "reload", WID_AID_RELOAD_TOGGLE),
1469 Hotkey('B', "break_toggle", WID_AID_BREAK_STR_ON_OFF_BTN),
1470 Hotkey('F', "break_string", WID_AID_BREAK_STR_EDIT_BOX),
1471 Hotkey('C', "match_case", WID_AID_MATCH_CASE_BTN),
1472 Hotkey(WKC_RETURN, "continue", WID_AID_CONTINUE_BTN),
1473 HOTKEY_LIST_END
1475 HotkeyList AIDebugWindow::hotkeys("aidebug", aidebug_hotkeys, AIDebugGlobalHotkeys);
1477 /** Widgets for the AI debug window. */
1478 static const NWidgetPart _nested_ai_debug_widgets[] = {
1479 NWidget(NWID_HORIZONTAL),
1480 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
1481 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_AI_DEBUG, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1482 NWidget(WWT_SHADEBOX, COLOUR_GREY),
1483 NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
1484 NWidget(WWT_STICKYBOX, COLOUR_GREY),
1485 EndContainer(),
1486 NWidget(WWT_PANEL, COLOUR_GREY, WID_AID_VIEW),
1487 NWidgetFunction(MakeCompanyButtonRowsAIDebug), SetPadding(0, 2, 1, 2),
1488 EndContainer(),
1489 NWidget(NWID_HORIZONTAL),
1490 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),
1491 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_AID_NAME_TEXT), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_JUST_STRING, STR_AI_DEBUG_NAME_TOOLTIP),
1492 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_AID_SETTINGS), SetMinimalSize(100, 20), SetDataTip(STR_AI_DEBUG_SETTINGS, STR_AI_DEBUG_SETTINGS_TOOLTIP),
1493 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_AID_RELOAD_TOGGLE), SetMinimalSize(100, 20), SetDataTip(STR_AI_DEBUG_RELOAD, STR_AI_DEBUG_RELOAD_TOOLTIP),
1494 EndContainer(),
1495 NWidget(NWID_HORIZONTAL),
1496 NWidget(NWID_VERTICAL),
1497 /* Log panel */
1498 NWidget(WWT_PANEL, COLOUR_GREY, WID_AID_LOG_PANEL), SetMinimalSize(287, 180), SetResize(1, 1), SetScrollbar(WID_AID_SCROLLBAR),
1499 EndContainer(),
1500 /* Break string widgets */
1501 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_AID_BREAK_STRING_WIDGETS),
1502 NWidget(NWID_HORIZONTAL),
1503 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),
1504 NWidget(WWT_PANEL, COLOUR_GREY),
1505 NWidget(NWID_HORIZONTAL),
1506 NWidget(WWT_LABEL, COLOUR_GREY), SetPadding(2, 2, 2, 4), SetDataTip(STR_AI_DEBUG_BREAK_ON_LABEL, 0x0),
1507 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),
1508 EndContainer(),
1509 EndContainer(),
1510 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),
1511 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),
1512 EndContainer(),
1513 EndContainer(),
1514 EndContainer(),
1515 NWidget(NWID_VERTICAL),
1516 NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_AID_SCROLLBAR),
1517 NWidget(WWT_RESIZEBOX, COLOUR_GREY),
1518 EndContainer(),
1519 EndContainer(),
1522 /** Window definition for the AI debug window. */
1523 static WindowDesc _ai_debug_desc(
1524 WDP_AUTO, "script_debug", 600, 450,
1525 WC_AI_DEBUG, WC_NONE,
1527 _nested_ai_debug_widgets, lengthof(_nested_ai_debug_widgets),
1528 &AIDebugWindow::hotkeys
1532 * Open the AI debug window and select the given company.
1533 * @param show_company Display debug information about this AI company.
1535 Window *ShowAIDebugWindow(CompanyID show_company)
1537 if (!_networking || _network_server) {
1538 AIDebugWindow *w = (AIDebugWindow *)BringWindowToFrontById(WC_AI_DEBUG, 0);
1539 if (w == nullptr) w = new AIDebugWindow(&_ai_debug_desc, 0);
1540 if (show_company != INVALID_COMPANY) w->ChangeToAI(show_company);
1541 return w;
1542 } else {
1543 ShowErrorMessage(STR_ERROR_AI_DEBUG_SERVER_ONLY, INVALID_STRING_ID, WL_INFO);
1546 return nullptr;
1550 * Reset the AI windows to their initial state.
1552 void InitializeAIGui()
1554 AIDebugWindow::ai_debug_company = INVALID_COMPANY;
1557 /** Open the AI debug window if one of the AI scripts has crashed. */
1558 void ShowAIDebugWindowIfAIError()
1560 /* Network clients can't debug AIs. */
1561 if (_networking && !_network_server) return;
1563 for (const Company *c : Company::Iterate()) {
1564 if (c->is_ai && c->ai_instance->IsDead()) {
1565 ShowAIDebugWindow(c->index);
1566 break;
1570 GameInstance *g = Game::GetGameInstance();
1571 if (g != nullptr && g->IsDead()) {
1572 ShowAIDebugWindow(OWNER_DEITY);