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/>.
8 /** @file ai_gui.cpp %Window for configuring the AIs */
10 #include "../stdafx.h"
11 #include "../table/sprites.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"
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"
47 #include "../safeguards.h"
49 static ScriptConfig
*GetConfig(CompanyID slot
)
51 if (slot
== OWNER_DEITY
) return GameConfig::GetConfig();
52 return AIConfig::GetConfig(slot
);
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.
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
),
73 if (slot
== OWNER_DEITY
) {
74 this->info_list
= Game::GetUniqueInfoList();
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 */
87 if (GetConfig(slot
)->HasScript()) {
88 ScriptInfo
*info
= GetConfig(slot
)->GetInfo();
90 for (const auto &item
: *this->info_list
) {
91 if (item
.second
== info
) {
101 void SetStringParameters(int widget
) const override
104 case WID_AIL_CAPTION
:
105 SetDParam(0, (this->slot
== OWNER_DEITY
) ? STR_AI_LIST_CAPTION_GAMESCRIPT
: STR_AI_LIST_CAPTION_AI
);
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
;
116 resize
->height
= this->line_height
;
117 size
->height
= 5 * this->line_height
;
121 void DrawWidget(const Rect
&r
, int widget
) const override
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
;
133 for (const auto &item
: *this->info_list
) {
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
;
142 case WID_AIL_INFO_BG
: {
143 AIInfo
*selected_info
= nullptr;
145 for (const auto &item
: *this->info_list
) {
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
);
172 * Changes the AI of the current slot.
176 if (this->selected
== -1) {
177 GetConfig(slot
)->Change(nullptr);
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 CloseWindowByClass(WC_QUERY_STRING
);
186 InvalidateWindowClassesData(WC_TEXTFILE
);
189 void OnClick(Point pt
, int widget
, int click_count
) override
192 case WID_AIL_LIST
: { // Select one of the AIs
193 int sel
= this->vscroll
->GetScrolledRowFromWidget(pt
.y
, this, WID_AIL_LIST
) - 1;
194 if (sel
< (int)this->info_list
->size()) {
195 this->selected
= sel
;
197 if (click_count
> 1) {
205 case WID_AIL_ACCEPT
: {
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
)) {
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
),
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
),
254 NWidget(WWT_PANEL
, COLOUR_MAUVE
, WID_AIL_INFO_BG
), SetMinimalTextLines(8, WD_FRAMERECT_TOP
+ WD_FRAMERECT_BOTTOM
), SetResize(1, 0),
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
),
261 NWidget(WWT_RESIZEBOX
, COLOUR_MAUVE
),
265 /** Window definition for the ai list window. */
266 static WindowDesc
_ai_list_desc(
267 WDP_CENTER
, "settings_script_list", 200, 234,
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 CloseWindowByClass(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
),
308 clicked_dropdown(false),
309 closing_dropdown(false),
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
324 case WID_AIS_CAPTION
:
325 SetDParam(0, (this->slot
== OWNER_DEITY
) ? STR_AI_SETTINGS_CAPTION_GAMESCRIPT
: STR_AI_SETTINGS_CAPTION_AI
);
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
;
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();
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
);
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
);
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
;
392 str
= STR_JUST_STRING
;
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
);
405 if (config_item
.complete_labels
) {
406 DrawDropDownButton(buttons_left
, y
+ button_y_offset
, COLOUR_YELLOW
, this->clicked_row
== i
&& clicked_dropdown
, editable
);
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
);
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;
433 void OnClick(Point pt
, int widget
, int click_count
) override
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 this->CloseChildWindows(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
;
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;
468 const NWidgetBase
*wid
= this->GetWidget
<NWidgetBase
>(WID_AIS_BACKGROUND
);
469 int rel_y
= (pt
.y
- (int)wid
->pos_y
) % this->line_height
;
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;
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
;
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;
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
);
525 this->ai_config
->ResetEditableSettings(_game_mode
== GM_MENU
|| ((this->slot
!= OWNER_DEITY
) && !Company::IsValidID(this->slot
)));
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
);
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
);
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
560 assert(this->clicked_dropdown
);
561 this->closing_dropdown
= true;
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;
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 this->CloseChildWindows(WC_QUERY_STRING
);
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
),
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
),
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
),
613 NWidget(WWT_RESIZEBOX
, COLOUR_MAUVE
),
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 CloseWindowByClass(WC_AI_LIST
);
632 CloseWindowByClass(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) {
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 CloseWindowById(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
),
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),
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
),
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
),
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
),
706 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
), SetPIP(7, 0, 7),
707 NWidget(WWT_PUSHTXTBTN
, COLOUR_YELLOW
, WID_AIC_CHANGE
), SetFill(1, 0), SetMinimalSize(93, 0), SetDataTip(STR_AI_CONFIG_CHANGE
, STR_AI_CONFIG_CHANGE_TOOLTIP
),
708 NWidget(WWT_PUSHTXTBTN
, COLOUR_YELLOW
, WID_AIC_CONFIGURE
), SetFill(1, 0), SetMinimalSize(93, 0), SetDataTip(STR_AI_CONFIG_CONFIGURE
, STR_AI_CONFIG_CONFIGURE_TOOLTIP
),
709 NWidget(WWT_PUSHTXTBTN
, COLOUR_YELLOW
, WID_AIC_CLOSE
), SetFill(1, 0), SetMinimalSize(93, 0), SetDataTip(STR_AI_SETTINGS_CLOSE
, STR_NULL
),
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
),
716 NWidget(WWT_PUSHTXTBTN
, COLOUR_YELLOW
, WID_AIC_CONTENT_DOWNLOAD
), SetFill(1, 0), SetMinimalSize(279, 0), SetPadding(0, 7, 9, 7), SetDataTip(STR_INTRO_ONLINE_CONTENT
, STR_INTRO_TOOLTIP_ONLINE_CONTENT
),
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 void Close() override
749 CloseWindowByClass(WC_AI_LIST
);
750 CloseWindowByClass(WC_AI_SETTINGS
);
751 this->Window::Close();
754 void SetStringParameters(int widget
) const override
758 SetDParam(0, GetGameSettings().difficulty
.max_no_competitors
);
761 switch (selected_slot
) {
763 SetDParam(0, STR_AI_CONFIG_CHANGE_GAMESCRIPT
);
766 case INVALID_COMPANY
:
767 SetDParam(0, STR_AI_CONFIG_CHANGE_NONE
);
771 SetDParam(0, STR_AI_CONFIG_CHANGE_AI
);
778 void UpdateWidgetSize(int widget
, Dimension
*size
, const Dimension
&padding
, Dimension
*fill
, Dimension
*resize
) override
781 case WID_AIC_GAMELIST
:
782 this->line_height
= FONT_HEIGHT_NORMAL
+ WD_MATRIX_TOP
+ WD_MATRIX_BOTTOM
;
783 size
->height
= 1 * this->line_height
;
787 this->line_height
= FONT_HEIGHT_NORMAL
+ WD_MATRIX_TOP
+ WD_MATRIX_BOTTOM
;
788 resize
->height
= this->line_height
;
789 size
->height
= 8 * this->line_height
;
792 case WID_AIC_CHANGE
: {
793 SetDParam(0, STR_AI_CONFIG_CHANGE_GAMESCRIPT
);
794 Dimension dim
= GetStringBoundingBox(STR_AI_CONFIG_CHANGE
);
796 SetDParam(0, STR_AI_CONFIG_CHANGE_NONE
);
797 dim
= maxdim(dim
, GetStringBoundingBox(STR_AI_CONFIG_CHANGE
));
799 SetDParam(0, STR_AI_CONFIG_CHANGE_AI
);
800 dim
= maxdim(dim
, GetStringBoundingBox(STR_AI_CONFIG_CHANGE
));
802 dim
.width
+= padding
.width
;
803 dim
.height
+= padding
.height
;
804 *size
= maxdim(*size
, dim
);
811 * Can the AI config in the given company slot be edited?
812 * @param slot The slot to query.
813 * @return True if and only if the given AI Config slot can e edited.
815 static bool IsEditable(CompanyID slot
)
817 if (slot
== OWNER_DEITY
) return _game_mode
!= GM_NORMAL
|| Game::GetInstance() != nullptr;
819 if (_game_mode
!= GM_NORMAL
) {
820 return slot
> 0 && slot
<= GetGameSettings().difficulty
.max_no_competitors
;
822 if (Company::IsValidID(slot
)) return false;
824 int max_slot
= GetGameSettings().difficulty
.max_no_competitors
;
825 for (CompanyID cid
= COMPANY_FIRST
; cid
< (CompanyID
)max_slot
&& cid
< MAX_COMPANIES
; cid
++) {
826 if (Company::IsValidHumanID(cid
)) max_slot
++;
828 return slot
< max_slot
;
831 void DrawWidget(const Rect
&r
, int widget
) const override
834 case WID_AIC_GAMELIST
: {
835 StringID text
= STR_AI_CONFIG_NONE
;
837 if (GameConfig::GetConfig()->GetInfo() != nullptr) {
838 SetDParamStr(0, GameConfig::GetConfig()->GetInfo()->GetName());
839 text
= STR_JUST_RAW_STRING
;
842 DrawString(r
.left
+ 10, r
.right
- 10, r
.top
+ WD_MATRIX_TOP
, text
,
843 (this->selected_slot
== OWNER_DEITY
) ? TC_WHITE
: (IsEditable(OWNER_DEITY
) ? TC_ORANGE
: TC_SILVER
));
850 for (int i
= this->vscroll
->GetPosition(); this->vscroll
->IsVisible(i
) && i
< MAX_COMPANIES
; i
++) {
853 if ((_game_mode
!= GM_NORMAL
&& i
== 0) || (_game_mode
== GM_NORMAL
&& Company::IsValidHumanID(i
))) {
854 text
= STR_AI_CONFIG_HUMAN_PLAYER
;
855 } else if (AIConfig::GetConfig((CompanyID
)i
)->GetInfo() != nullptr) {
856 SetDParamStr(0, AIConfig::GetConfig((CompanyID
)i
)->GetInfo()->GetName());
857 text
= STR_JUST_RAW_STRING
;
859 text
= STR_AI_CONFIG_RANDOM_AI
;
861 DrawString(r
.left
+ 10, r
.right
- 10, y
+ WD_MATRIX_TOP
, text
,
862 (this->selected_slot
== i
) ? TC_WHITE
: (IsEditable((CompanyID
)i
) ? TC_ORANGE
: TC_SILVER
));
863 y
+= this->line_height
;
870 void OnClick(Point pt
, int widget
, int click_count
) override
872 if (widget
>= WID_AIC_TEXTFILE
&& widget
< WID_AIC_TEXTFILE
+ TFT_END
) {
873 if (this->selected_slot
== INVALID_COMPANY
|| GetConfig(this->selected_slot
) == nullptr) return;
875 ShowScriptTextfileWindow((TextfileType
)(widget
- WID_AIC_TEXTFILE
), this->selected_slot
);
880 case WID_AIC_DECREASE
:
881 case WID_AIC_INCREASE
: {
883 if (widget
== WID_AIC_DECREASE
) {
884 new_value
= std::max(0, GetGameSettings().difficulty
.max_no_competitors
- 1);
886 new_value
= std::min(MAX_COMPANIES
- 1, GetGameSettings().difficulty
.max_no_competitors
+ 1);
888 IConsoleSetSetting("difficulty.max_no_competitors", new_value
);
892 case WID_AIC_GAMELIST
: {
893 this->selected_slot
= OWNER_DEITY
;
894 this->InvalidateData();
895 if (click_count
> 1 && this->selected_slot
!= INVALID_COMPANY
&& _game_mode
!= GM_NORMAL
) ShowAIListWindow((CompanyID
)this->selected_slot
);
899 case WID_AIC_LIST
: { // Select a slot
900 this->selected_slot
= (CompanyID
)this->vscroll
->GetScrolledRowFromWidget(pt
.y
, this, widget
);
901 this->InvalidateData();
902 if (click_count
> 1 && this->selected_slot
!= INVALID_COMPANY
) ShowAIListWindow((CompanyID
)this->selected_slot
);
906 case WID_AIC_MOVE_UP
:
907 if (IsEditable(this->selected_slot
) && IsEditable((CompanyID
)(this->selected_slot
- 1))) {
908 Swap(GetGameSettings().ai_config
[this->selected_slot
], GetGameSettings().ai_config
[this->selected_slot
- 1]);
909 this->selected_slot
--;
910 this->vscroll
->ScrollTowards(this->selected_slot
);
911 this->InvalidateData();
915 case WID_AIC_MOVE_DOWN
:
916 if (IsEditable(this->selected_slot
) && IsEditable((CompanyID
)(this->selected_slot
+ 1))) {
917 Swap(GetGameSettings().ai_config
[this->selected_slot
], GetGameSettings().ai_config
[this->selected_slot
+ 1]);
918 this->selected_slot
++;
919 this->vscroll
->ScrollTowards(this->selected_slot
);
920 this->InvalidateData();
924 case WID_AIC_CHANGE
: // choose other AI
925 ShowAIListWindow((CompanyID
)this->selected_slot
);
928 case WID_AIC_CONFIGURE
: // change the settings for an AI
929 ShowAISettingsWindow((CompanyID
)this->selected_slot
);
936 case WID_AIC_CONTENT_DOWNLOAD
:
937 if (!_network_available
) {
938 ShowErrorMessage(STR_NETWORK_ERROR_NOTAVAILABLE
, INVALID_STRING_ID
, WL_ERROR
);
940 ShowNetworkContentListWindow(nullptr, CONTENT_TYPE_AI
, CONTENT_TYPE_GAME
);
947 * Some data on this window has become invalid.
948 * @param data Information about the changed data.
949 * @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.
951 void OnInvalidateData(int data
= 0, bool gui_scope
= true) override
953 if (!IsEditable(this->selected_slot
)) {
954 this->selected_slot
= INVALID_COMPANY
;
957 if (!gui_scope
) return;
959 this->SetWidgetDisabledState(WID_AIC_DECREASE
, GetGameSettings().difficulty
.max_no_competitors
== 0);
960 this->SetWidgetDisabledState(WID_AIC_INCREASE
, GetGameSettings().difficulty
.max_no_competitors
== MAX_COMPANIES
- 1);
961 this->SetWidgetDisabledState(WID_AIC_CHANGE
, (this->selected_slot
== OWNER_DEITY
&& _game_mode
== GM_NORMAL
) || this->selected_slot
== INVALID_COMPANY
);
962 this->SetWidgetDisabledState(WID_AIC_CONFIGURE
, this->selected_slot
== INVALID_COMPANY
|| GetConfig(this->selected_slot
)->GetConfigList()->size() == 0);
963 this->SetWidgetDisabledState(WID_AIC_MOVE_UP
, this->selected_slot
== OWNER_DEITY
|| this->selected_slot
== INVALID_COMPANY
|| !IsEditable((CompanyID
)(this->selected_slot
- 1)));
964 this->SetWidgetDisabledState(WID_AIC_MOVE_DOWN
, this->selected_slot
== OWNER_DEITY
|| this->selected_slot
== INVALID_COMPANY
|| !IsEditable((CompanyID
)(this->selected_slot
+ 1)));
966 for (TextfileType tft
= TFT_BEGIN
; tft
< TFT_END
; tft
++) {
967 this->SetWidgetDisabledState(WID_AIC_TEXTFILE
+ tft
, this->selected_slot
== INVALID_COMPANY
|| (GetConfig(this->selected_slot
)->GetTextfile(tft
, this->selected_slot
) == nullptr));
972 /** Open the AI config window. */
973 void ShowAIConfigWindow()
975 CloseWindowByClass(WC_GAME_OPTIONS
);
976 new AIConfigWindow();
980 * Set the widget colour of a button based on the
981 * state of the script. (dead or alive)
982 * @param button the button to update.
983 * @param dead true if the script is dead, otherwise false.
984 * @param paused true if the script is paused, otherwise false.
985 * @return true if the colour was changed and the window need to be marked as dirty.
987 static bool SetScriptButtonColour(NWidgetCore
&button
, bool dead
, bool paused
)
989 /* Dead scripts are indicated with red background and
990 * paused scripts are indicated with yellow background. */
991 Colours colour
= dead
? COLOUR_RED
:
992 (paused
? COLOUR_YELLOW
: COLOUR_GREY
);
993 if (button
.colour
!= colour
) {
994 button
.colour
= colour
;
1001 * Window with everything an AI prints via ScriptLog.
1003 struct AIDebugWindow
: public Window
{
1004 static const int top_offset
; ///< Offset of the text at the top of the WID_AID_LOG_PANEL.
1005 static const int bottom_offset
; ///< Offset of the text at the bottom of the WID_AID_LOG_PANEL.
1007 static const uint MAX_BREAK_STR_STRING_LENGTH
= 256; ///< Maximum length of the break string.
1009 static CompanyID ai_debug_company
; ///< The AI that is (was last) being debugged.
1010 int redraw_timer
; ///< Timer for redrawing the window, otherwise it'll happen every tick.
1011 int last_vscroll_pos
; ///< Last position of the scrolling.
1012 bool autoscroll
; ///< Whether automatically scrolling should be enabled or not.
1013 bool show_break_box
; ///< Whether the break/debug box is visible.
1014 static bool break_check_enabled
; ///< Stop an AI when it prints a matching string
1015 static char break_string
[MAX_BREAK_STR_STRING_LENGTH
]; ///< The string to match to the AI output
1016 QueryString break_editbox
; ///< Break editbox
1017 static StringFilter break_string_filter
; ///< Log filter for break.
1018 static bool case_sensitive_break_check
; ///< Is the matching done case-sensitive
1019 int highlight_row
; ///< The output row that matches the given string, or -1
1020 Scrollbar
*vscroll
; ///< Cache of the vertical scrollbar.
1022 ScriptLog::LogData
*GetLogPointer() const
1024 if (ai_debug_company
== OWNER_DEITY
) return (ScriptLog::LogData
*)Game::GetInstance()->GetLogPointer();
1025 return (ScriptLog::LogData
*)Company::Get(ai_debug_company
)->ai_instance
->GetLogPointer();
1029 * Check whether the currently selected AI/GS is dead.
1030 * @return true if dead.
1034 if (ai_debug_company
== OWNER_DEITY
) {
1035 GameInstance
*game
= Game::GetInstance();
1036 return game
== nullptr || game
->IsDead();
1038 return !Company::IsValidAiID(ai_debug_company
) || Company::Get(ai_debug_company
)->ai_instance
->IsDead();
1042 * Check whether a company is a valid AI company or GS.
1043 * @param company Company to check for validity.
1044 * @return true if company is valid for debugging.
1046 bool IsValidDebugCompany(CompanyID company
) const
1049 case INVALID_COMPANY
: return false;
1050 case OWNER_DEITY
: return Game::GetInstance() != nullptr;
1051 default: return Company::IsValidAiID(company
);
1056 * Ensure that \c ai_debug_company refers to a valid AI company or GS, or is set to #INVALID_COMPANY.
1057 * If no valid company is selected, it selects the first valid AI or GS if any.
1059 void SelectValidDebugCompany()
1061 /* Check if the currently selected company is still active. */
1062 if (this->IsValidDebugCompany(ai_debug_company
)) return;
1064 ai_debug_company
= INVALID_COMPANY
;
1066 for (const Company
*c
: Company::Iterate()) {
1068 ChangeToAI(c
->index
);
1073 /* If no AI is available, see if there is a game script. */
1074 if (Game::GetInstance() != nullptr) ChangeToAI(OWNER_DEITY
);
1078 * Constructor for the window.
1079 * @param desc The description of the window.
1080 * @param number The window number (actually unused).
1082 AIDebugWindow(WindowDesc
*desc
, WindowNumber number
) : Window(desc
), break_editbox(MAX_BREAK_STR_STRING_LENGTH
)
1084 this->CreateNestedTree();
1085 this->vscroll
= this->GetScrollbar(WID_AID_SCROLLBAR
);
1086 this->show_break_box
= _settings_client
.gui
.ai_developer_tools
;
1087 this->GetWidget
<NWidgetStacked
>(WID_AID_BREAK_STRING_WIDGETS
)->SetDisplayedPlane(this->show_break_box
? 0 : SZSP_HORIZONTAL
);
1088 this->FinishInitNested(number
);
1090 if (!this->show_break_box
) break_check_enabled
= false;
1092 this->last_vscroll_pos
= 0;
1093 this->autoscroll
= true;
1094 this->highlight_row
= -1;
1096 this->querystrings
[WID_AID_BREAK_STR_EDIT_BOX
] = &this->break_editbox
;
1098 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
);
1100 /* Restore the break string value from static variable */
1101 this->break_editbox
.text
.Assign(this->break_string
);
1103 this->SelectValidDebugCompany();
1104 this->InvalidateData(-1);
1107 void UpdateWidgetSize(int widget
, Dimension
*size
, const Dimension
&padding
, Dimension
*fill
, Dimension
*resize
) override
1109 if (widget
== WID_AID_LOG_PANEL
) {
1110 resize
->height
= FONT_HEIGHT_NORMAL
+ WD_PAR_VSEP_NORMAL
;
1111 size
->height
= 14 * resize
->height
+ this->top_offset
+ this->bottom_offset
;
1115 void OnPaint() override
1117 this->SelectValidDebugCompany();
1119 /* Draw standard stuff */
1120 this->DrawWidgets();
1122 if (this->IsShaded()) return; // Don't draw anything when the window is shaded.
1126 /* Paint the company icons */
1127 for (CompanyID i
= COMPANY_FIRST
; i
< MAX_COMPANIES
; i
++) {
1128 NWidgetCore
*button
= this->GetWidget
<NWidgetCore
>(i
+ WID_AID_COMPANY_BUTTON_START
);
1130 bool valid
= Company::IsValidAiID(i
);
1132 /* Check whether the validity of the company changed */
1133 dirty
|= (button
->IsDisabled() == valid
);
1135 /* Mark dead/paused AIs by setting the background colour. */
1136 bool dead
= valid
&& Company::Get(i
)->ai_instance
->IsDead();
1137 bool paused
= valid
&& Company::Get(i
)->ai_instance
->IsPaused();
1138 /* Re-paint if the button was updated.
1139 * (note that it is intentional that SetScriptButtonColour is always called) */
1140 dirty
|= SetScriptButtonColour(*button
, dead
, paused
);
1142 /* Draw company icon only for valid AI companies */
1143 if (!valid
) continue;
1145 byte offset
= (i
== ai_debug_company
) ? 1 : 0;
1146 DrawCompanyIcon(i
, button
->pos_x
+ button
->current_x
/ 2 - 7 + offset
, this->GetWidget
<NWidgetBase
>(WID_AID_COMPANY_BUTTON_START
+ i
)->pos_y
+ 2 + offset
);
1149 /* Set button colour for Game Script. */
1150 GameInstance
*game
= Game::GetInstance();
1151 bool valid
= game
!= nullptr;
1152 bool dead
= valid
&& game
->IsDead();
1153 bool paused
= valid
&& game
->IsPaused();
1155 NWidgetCore
*button
= this->GetWidget
<NWidgetCore
>(WID_AID_SCRIPT_GAME
);
1156 dirty
|= (button
->IsDisabled() == valid
) || SetScriptButtonColour(*button
, dead
, paused
);
1158 if (dirty
) this->InvalidateData(-1);
1160 /* If there are no active companies, don't display anything else. */
1161 if (ai_debug_company
== INVALID_COMPANY
) return;
1163 ScriptLog::LogData
*log
= this->GetLogPointer();
1165 int scroll_count
= (log
== nullptr) ? 0 : log
->used
;
1166 if (this->vscroll
->GetCount() != scroll_count
) {
1167 this->vscroll
->SetCount(scroll_count
);
1169 /* We need a repaint */
1170 this->SetWidgetDirty(WID_AID_SCROLLBAR
);
1173 if (log
== nullptr) return;
1175 /* Detect when the user scrolls the window. Enable autoscroll when the
1176 * bottom-most line becomes visible. */
1177 if (this->last_vscroll_pos
!= this->vscroll
->GetPosition()) {
1178 this->autoscroll
= this->vscroll
->GetPosition() >= log
->used
- this->vscroll
->GetCapacity();
1180 if (this->autoscroll
) {
1181 int scroll_pos
= std::max(0, log
->used
- this->vscroll
->GetCapacity());
1182 if (scroll_pos
!= this->vscroll
->GetPosition()) {
1183 this->vscroll
->SetPosition(scroll_pos
);
1185 /* We need a repaint */
1186 this->SetWidgetDirty(WID_AID_SCROLLBAR
);
1187 this->SetWidgetDirty(WID_AID_LOG_PANEL
);
1190 this->last_vscroll_pos
= this->vscroll
->GetPosition();
1193 void SetStringParameters(int widget
) const override
1196 case WID_AID_NAME_TEXT
:
1197 if (ai_debug_company
== OWNER_DEITY
) {
1198 const GameInfo
*info
= Game::GetInfo();
1199 assert(info
!= nullptr);
1200 SetDParam(0, STR_AI_DEBUG_NAME_AND_VERSION
);
1201 SetDParamStr(1, info
->GetName());
1202 SetDParam(2, info
->GetVersion());
1203 } else if (ai_debug_company
== INVALID_COMPANY
|| !Company::IsValidAiID(ai_debug_company
)) {
1204 SetDParam(0, STR_EMPTY
);
1206 const AIInfo
*info
= Company::Get(ai_debug_company
)->ai_info
;
1207 assert(info
!= nullptr);
1208 SetDParam(0, STR_AI_DEBUG_NAME_AND_VERSION
);
1209 SetDParamStr(1, info
->GetName());
1210 SetDParam(2, info
->GetVersion());
1216 void DrawWidget(const Rect
&r
, int widget
) const override
1218 if (ai_debug_company
== INVALID_COMPANY
) return;
1221 case WID_AID_LOG_PANEL
: {
1222 ScriptLog::LogData
*log
= this->GetLogPointer();
1223 if (log
== nullptr) return;
1225 int y
= this->top_offset
;
1226 for (int i
= this->vscroll
->GetPosition(); this->vscroll
->IsVisible(i
) && i
< log
->used
; i
++) {
1227 int pos
= (i
+ log
->pos
+ 1 - log
->used
+ log
->count
) % log
->count
;
1228 if (log
->lines
[pos
] == nullptr) break;
1231 switch (log
->type
[pos
]) {
1232 case ScriptLog::LOG_SQ_INFO
: colour
= TC_BLACK
; break;
1233 case ScriptLog::LOG_SQ_ERROR
: colour
= TC_RED
; break;
1234 case ScriptLog::LOG_INFO
: colour
= TC_BLACK
; break;
1235 case ScriptLog::LOG_WARNING
: colour
= TC_YELLOW
; break;
1236 case ScriptLog::LOG_ERROR
: colour
= TC_RED
; break;
1237 default: colour
= TC_BLACK
; break;
1240 /* Check if the current line should be highlighted */
1241 if (pos
== this->highlight_row
) {
1242 GfxFillRect(r
.left
+ 1, r
.top
+ y
, r
.right
- 1, r
.top
+ y
+ this->resize
.step_height
- WD_PAR_VSEP_NORMAL
, PC_BLACK
);
1243 if (colour
== TC_BLACK
) colour
= TC_WHITE
; // Make black text readable by inverting it to white.
1246 DrawString(r
.left
+ 7, r
.right
- 7, r
.top
+ y
, log
->lines
[pos
], colour
, SA_LEFT
| SA_FORCE
);
1247 y
+= this->resize
.step_height
;
1255 * Change all settings to select another AI.
1256 * @param show_ai The new AI to show.
1258 void ChangeToAI(CompanyID show_ai
)
1260 if (!this->IsValidDebugCompany(show_ai
)) return;
1262 ai_debug_company
= show_ai
;
1264 this->highlight_row
= -1; // The highlight of one AI make little sense for another AI.
1266 /* Close AI settings window to prevent confusion */
1267 CloseWindowByClass(WC_AI_SETTINGS
);
1269 this->InvalidateData(-1);
1271 this->autoscroll
= true;
1272 this->last_vscroll_pos
= this->vscroll
->GetPosition();
1275 void OnClick(Point pt
, int widget
, int click_count
) override
1277 /* Also called for hotkeys, so check for disabledness */
1278 if (this->IsWidgetDisabled(widget
)) return;
1280 /* Check which button is clicked */
1281 if (IsInsideMM(widget
, WID_AID_COMPANY_BUTTON_START
, WID_AID_COMPANY_BUTTON_END
+ 1)) {
1282 ChangeToAI((CompanyID
)(widget
- WID_AID_COMPANY_BUTTON_START
));
1286 case WID_AID_SCRIPT_GAME
:
1287 ChangeToAI(OWNER_DEITY
);
1290 case WID_AID_RELOAD_TOGGLE
:
1291 if (ai_debug_company
== OWNER_DEITY
) break;
1292 /* First kill the company of the AI, then start a new one. This should start the current AI again */
1293 DoCommandP(0, CCA_DELETE
| ai_debug_company
<< 16 | CRR_MANUAL
<< 24, 0, CMD_COMPANY_CTRL
);
1294 DoCommandP(0, CCA_NEW_AI
| ai_debug_company
<< 16, 0, CMD_COMPANY_CTRL
);
1297 case WID_AID_SETTINGS
:
1298 ShowAISettingsWindow(ai_debug_company
);
1301 case WID_AID_BREAK_STR_ON_OFF_BTN
:
1302 this->break_check_enabled
= !this->break_check_enabled
;
1303 this->InvalidateData(-1);
1306 case WID_AID_MATCH_CASE_BTN
:
1307 this->case_sensitive_break_check
= !this->case_sensitive_break_check
;
1308 this->InvalidateData(-1);
1311 case WID_AID_CONTINUE_BTN
:
1312 /* Unpause current AI / game script and mark the corresponding script button dirty. */
1313 if (!this->IsDead()) {
1314 if (ai_debug_company
== OWNER_DEITY
) {
1317 AI::Unpause(ai_debug_company
);
1321 /* If the last AI/Game Script is unpaused, unpause the game too. */
1322 if ((_pause_mode
& PM_PAUSED_NORMAL
) == PM_PAUSED_NORMAL
) {
1323 bool all_unpaused
= !Game::IsPaused();
1325 for (const Company
*c
: Company::Iterate()) {
1326 if (c
->is_ai
&& AI::IsPaused(c
->index
)) {
1327 all_unpaused
= false;
1332 /* All scripts have been unpaused => unpause the game. */
1333 DoCommandP(0, PM_PAUSED_NORMAL
, 0, CMD_PAUSE
);
1338 this->highlight_row
= -1;
1339 this->InvalidateData(-1);
1344 void OnEditboxChanged(int wid
) override
1346 if (wid
== WID_AID_BREAK_STR_EDIT_BOX
) {
1347 /* Save the current string to static member so it can be restored next time the window is opened. */
1348 strecpy(this->break_string
, this->break_editbox
.text
.buf
, lastof(this->break_string
));
1349 break_string_filter
.SetFilterTerm(this->break_string
);
1354 * Some data on this window has become invalid.
1355 * @param data Information about the changed data.
1356 * This is the company ID of the AI/GS which wrote a new log message, or -1 in other cases.
1357 * @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.
1359 void OnInvalidateData(int data
= 0, bool gui_scope
= true) override
1361 /* If the log message is related to the active company tab, check the break string.
1362 * This needs to be done in gameloop-scope, so the AI is suspended immediately. */
1363 if (!gui_scope
&& data
== ai_debug_company
&& this->IsValidDebugCompany(ai_debug_company
) && this->break_check_enabled
&& !this->break_string_filter
.IsEmpty()) {
1364 /* Get the log instance of the active company */
1365 ScriptLog::LogData
*log
= this->GetLogPointer();
1367 if (log
!= nullptr) {
1368 this->break_string_filter
.ResetState();
1369 this->break_string_filter
.AddLine(log
->lines
[log
->pos
]);
1370 if (this->break_string_filter
.GetState()) {
1371 /* Pause execution of script. */
1372 if (!this->IsDead()) {
1373 if (ai_debug_company
== OWNER_DEITY
) {
1376 AI::Pause(ai_debug_company
);
1380 /* Pause the game. */
1381 if ((_pause_mode
& PM_PAUSED_NORMAL
) == PM_UNPAUSED
) {
1382 DoCommandP(0, PM_PAUSED_NORMAL
, 1, CMD_PAUSE
);
1385 /* Highlight row that matched */
1386 this->highlight_row
= log
->pos
;
1391 if (!gui_scope
) return;
1393 this->SelectValidDebugCompany();
1395 ScriptLog::LogData
*log
= ai_debug_company
!= INVALID_COMPANY
? this->GetLogPointer() : nullptr;
1396 this->vscroll
->SetCount((log
== nullptr) ? 0 : log
->used
);
1398 /* Update company buttons */
1399 for (CompanyID i
= COMPANY_FIRST
; i
< MAX_COMPANIES
; i
++) {
1400 this->SetWidgetDisabledState(i
+ WID_AID_COMPANY_BUTTON_START
, !Company::IsValidAiID(i
));
1401 this->SetWidgetLoweredState(i
+ WID_AID_COMPANY_BUTTON_START
, ai_debug_company
== i
);
1404 this->SetWidgetDisabledState(WID_AID_SCRIPT_GAME
, Game::GetGameInstance() == nullptr);
1405 this->SetWidgetLoweredState(WID_AID_SCRIPT_GAME
, ai_debug_company
== OWNER_DEITY
);
1407 this->SetWidgetLoweredState(WID_AID_BREAK_STR_ON_OFF_BTN
, this->break_check_enabled
);
1408 this->SetWidgetLoweredState(WID_AID_MATCH_CASE_BTN
, this->case_sensitive_break_check
);
1410 this->SetWidgetDisabledState(WID_AID_SETTINGS
, ai_debug_company
== INVALID_COMPANY
);
1411 extern CompanyID _local_company
;
1412 this->SetWidgetDisabledState(WID_AID_RELOAD_TOGGLE
, ai_debug_company
== INVALID_COMPANY
|| ai_debug_company
== OWNER_DEITY
|| ai_debug_company
== _local_company
);
1413 this->SetWidgetDisabledState(WID_AID_CONTINUE_BTN
, ai_debug_company
== INVALID_COMPANY
||
1414 (ai_debug_company
== OWNER_DEITY
? !Game::IsPaused() : !AI::IsPaused(ai_debug_company
)));
1417 void OnResize() override
1419 this->vscroll
->SetCapacityFromWidget(this, WID_AID_LOG_PANEL
);
1422 static HotkeyList hotkeys
;
1425 const int AIDebugWindow::top_offset
= WD_FRAMERECT_TOP
+ 2;
1426 const int AIDebugWindow::bottom_offset
= WD_FRAMERECT_BOTTOM
;
1427 CompanyID
AIDebugWindow::ai_debug_company
= INVALID_COMPANY
;
1428 char AIDebugWindow::break_string
[MAX_BREAK_STR_STRING_LENGTH
] = "";
1429 bool AIDebugWindow::break_check_enabled
= true;
1430 bool AIDebugWindow::case_sensitive_break_check
= false;
1431 StringFilter
AIDebugWindow::break_string_filter(&AIDebugWindow::case_sensitive_break_check
);
1433 /** Make a number of rows with buttons for each company for the AI debug window. */
1434 NWidgetBase
*MakeCompanyButtonRowsAIDebug(int *biggest_index
)
1436 return MakeCompanyButtonRows(biggest_index
, WID_AID_COMPANY_BUTTON_START
, WID_AID_COMPANY_BUTTON_END
, COLOUR_GREY
, 8, STR_AI_DEBUG_SELECT_AI_TOOLTIP
);
1440 * Handler for global hotkeys of the AIDebugWindow.
1441 * @param hotkey Hotkey
1442 * @return ES_HANDLED if hotkey was accepted.
1444 static EventState
AIDebugGlobalHotkeys(int hotkey
)
1446 if (_game_mode
!= GM_NORMAL
) return ES_NOT_HANDLED
;
1447 Window
*w
= ShowAIDebugWindow(INVALID_COMPANY
);
1448 if (w
== nullptr) return ES_NOT_HANDLED
;
1449 return w
->OnHotkey(hotkey
);
1452 static Hotkey aidebug_hotkeys
[] = {
1453 Hotkey('1', "company_1", WID_AID_COMPANY_BUTTON_START
),
1454 Hotkey('2', "company_2", WID_AID_COMPANY_BUTTON_START
+ 1),
1455 Hotkey('3', "company_3", WID_AID_COMPANY_BUTTON_START
+ 2),
1456 Hotkey('4', "company_4", WID_AID_COMPANY_BUTTON_START
+ 3),
1457 Hotkey('5', "company_5", WID_AID_COMPANY_BUTTON_START
+ 4),
1458 Hotkey('6', "company_6", WID_AID_COMPANY_BUTTON_START
+ 5),
1459 Hotkey('7', "company_7", WID_AID_COMPANY_BUTTON_START
+ 6),
1460 Hotkey('8', "company_8", WID_AID_COMPANY_BUTTON_START
+ 7),
1461 Hotkey('9', "company_9", WID_AID_COMPANY_BUTTON_START
+ 8),
1462 Hotkey((uint16
)0, "company_10", WID_AID_COMPANY_BUTTON_START
+ 9),
1463 Hotkey((uint16
)0, "company_11", WID_AID_COMPANY_BUTTON_START
+ 10),
1464 Hotkey((uint16
)0, "company_12", WID_AID_COMPANY_BUTTON_START
+ 11),
1465 Hotkey((uint16
)0, "company_13", WID_AID_COMPANY_BUTTON_START
+ 12),
1466 Hotkey((uint16
)0, "company_14", WID_AID_COMPANY_BUTTON_START
+ 13),
1467 Hotkey((uint16
)0, "company_15", WID_AID_COMPANY_BUTTON_START
+ 14),
1468 Hotkey('S', "settings", WID_AID_SETTINGS
),
1469 Hotkey('0', "game_script", WID_AID_SCRIPT_GAME
),
1470 Hotkey((uint16
)0, "reload", WID_AID_RELOAD_TOGGLE
),
1471 Hotkey('B', "break_toggle", WID_AID_BREAK_STR_ON_OFF_BTN
),
1472 Hotkey('F', "break_string", WID_AID_BREAK_STR_EDIT_BOX
),
1473 Hotkey('C', "match_case", WID_AID_MATCH_CASE_BTN
),
1474 Hotkey(WKC_RETURN
, "continue", WID_AID_CONTINUE_BTN
),
1477 HotkeyList
AIDebugWindow::hotkeys("aidebug", aidebug_hotkeys
, AIDebugGlobalHotkeys
);
1479 /** Widgets for the AI debug window. */
1480 static const NWidgetPart _nested_ai_debug_widgets
[] = {
1481 NWidget(NWID_HORIZONTAL
),
1482 NWidget(WWT_CLOSEBOX
, COLOUR_GREY
),
1483 NWidget(WWT_CAPTION
, COLOUR_GREY
), SetDataTip(STR_AI_DEBUG
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
1484 NWidget(WWT_SHADEBOX
, COLOUR_GREY
),
1485 NWidget(WWT_DEFSIZEBOX
, COLOUR_GREY
),
1486 NWidget(WWT_STICKYBOX
, COLOUR_GREY
),
1488 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_AID_VIEW
),
1489 NWidgetFunction(MakeCompanyButtonRowsAIDebug
), SetPadding(0, 2, 1, 2),
1491 NWidget(NWID_HORIZONTAL
),
1492 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
),
1493 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_AID_NAME_TEXT
), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_JUST_STRING
, STR_AI_DEBUG_NAME_TOOLTIP
),
1494 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_AID_SETTINGS
), SetMinimalSize(100, 20), SetDataTip(STR_AI_DEBUG_SETTINGS
, STR_AI_DEBUG_SETTINGS_TOOLTIP
),
1495 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_AID_RELOAD_TOGGLE
), SetMinimalSize(100, 20), SetDataTip(STR_AI_DEBUG_RELOAD
, STR_AI_DEBUG_RELOAD_TOOLTIP
),
1497 NWidget(NWID_HORIZONTAL
),
1498 NWidget(NWID_VERTICAL
),
1500 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_AID_LOG_PANEL
), SetMinimalSize(287, 180), SetResize(1, 1), SetScrollbar(WID_AID_SCROLLBAR
),
1502 /* Break string widgets */
1503 NWidget(NWID_SELECTION
, INVALID_COLOUR
, WID_AID_BREAK_STRING_WIDGETS
),
1504 NWidget(NWID_HORIZONTAL
),
1505 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
),
1506 NWidget(WWT_PANEL
, COLOUR_GREY
),
1507 NWidget(NWID_HORIZONTAL
),
1508 NWidget(WWT_LABEL
, COLOUR_GREY
), SetPadding(2, 2, 2, 4), SetDataTip(STR_AI_DEBUG_BREAK_ON_LABEL
, 0x0),
1509 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
),
1512 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
),
1513 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
),
1517 NWidget(NWID_VERTICAL
),
1518 NWidget(NWID_VSCROLLBAR
, COLOUR_GREY
, WID_AID_SCROLLBAR
),
1519 NWidget(WWT_RESIZEBOX
, COLOUR_GREY
),
1524 /** Window definition for the AI debug window. */
1525 static WindowDesc
_ai_debug_desc(
1526 WDP_AUTO
, "script_debug", 600, 450,
1527 WC_AI_DEBUG
, WC_NONE
,
1529 _nested_ai_debug_widgets
, lengthof(_nested_ai_debug_widgets
),
1530 &AIDebugWindow::hotkeys
1534 * Open the AI debug window and select the given company.
1535 * @param show_company Display debug information about this AI company.
1537 Window
*ShowAIDebugWindow(CompanyID show_company
)
1539 if (!_networking
|| _network_server
) {
1540 AIDebugWindow
*w
= (AIDebugWindow
*)BringWindowToFrontById(WC_AI_DEBUG
, 0);
1541 if (w
== nullptr) w
= new AIDebugWindow(&_ai_debug_desc
, 0);
1542 if (show_company
!= INVALID_COMPANY
) w
->ChangeToAI(show_company
);
1545 ShowErrorMessage(STR_ERROR_AI_DEBUG_SERVER_ONLY
, INVALID_STRING_ID
, WL_INFO
);
1552 * Reset the AI windows to their initial state.
1554 void InitializeAIGui()
1556 AIDebugWindow::ai_debug_company
= INVALID_COMPANY
;
1559 /** Open the AI debug window if one of the AI scripts has crashed. */
1560 void ShowAIDebugWindowIfAIError()
1562 /* Network clients can't debug AIs. */
1563 if (_networking
&& !_network_server
) return;
1565 for (const Company
*c
: Company::Iterate()) {
1566 if (c
->is_ai
&& c
->ai_instance
->IsDead()) {
1567 ShowAIDebugWindow(c
->index
);
1572 GameInstance
*g
= Game::GetGameInstance();
1573 if (g
!= nullptr && g
->IsDead()) {
1574 ShowAIDebugWindow(OWNER_DEITY
);