OpenTTD: src/ai/ai_gui.cpp Source File

OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

ai_gui.cpp
Go to the documentation of this file.
1 /* $Id: ai_gui.cpp 27787 2017-03-12 18:19:01Z peter1138 $ */
2
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 */
9
12 #include "../stdafx.h"
13 #include "../table/sprites.h"
14 #include "../error.h"
15 #include "../settings_gui.h"
16 #include "../querystring_gui.h"
17 #include "../stringfilter_type.h"
18 #include "../company_base.h"
19 #include "../company_gui.h"
20 #include "../strings_func.h"
21 #include "../window_func.h"
22 #include "../gfx_func.h"
23 #include "../command_func.h"
24 #include "../network/network.h"
25 #include "../settings_func.h"
26 #include "../network/network_content.h"
27 #include "../textfile_gui.h"
28 #include "../widgets/dropdown_type.h"
29 #include "../widgets/dropdown_func.h"
30 #include "../hotkeys.h"
31 #include "../core/geometry_func.hpp"
32
33 #include "ai.hpp"
34 #include "ai_gui.hpp"
35 #include "../script/api/script_log.hpp"
36 #include "ai_config.hpp"
37 #include "ai_info.hpp"
38 #include "ai_instance.hpp"
39 #include "../game/game.hpp"
40 #include "../game/game_config.hpp"
41 #include "../game/game_info.hpp"
42 #include "../game/game_instance.hpp"
43
44 #include "table/strings.h"
45
46 #include <vector>
47
48 #include "../safeguards.h"
49
50 static ScriptConfig *GetConfig(CompanyID slot)
51 {
52 if (slot == OWNER_DEITY) return GameConfig::GetConfig();
53 return AIConfig::GetConfig(slot);
54 }
55
59 struct AIListWindow : public Window {
60 const ScriptInfoList *info_list;
61 int selected;
62 CompanyID slot;
63 int line_height;
64 Scrollbar *vscroll;
65
71 AIListWindow(WindowDesc *desc, CompanyID slot) : Window(desc),
72 slot(slot)
73 {
74 if (slot == OWNER_DEITY) {
75 this->info_list = Game::GetUniqueInfoList();
76 } else {
77 this->info_list = AI::GetUniqueInfoList();
78 }
79
80 this->CreateNestedTree();
81 this->vscroll = this->GetScrollbar(WID_AIL_SCROLLBAR);
82 this->FinishInitNested(); // Initializes 'this->line_height' as side effect.
83
84 this->vscroll->SetCount((int)this->info_list->size() + 1);
85
86 /* Try if we can find the currently selected AI */
87 this->selected = -1;
88 if (GetConfig(slot)->HasScript()) {
89 ScriptInfo *info = GetConfig(slot)->GetInfo();
90 int i = 0;
91 for (ScriptInfoList::const_iterator it = this->info_list->begin(); it != this->info_list->end(); it++, i++) {
92 if ((*it).second == info) {
93 this->selected = i;
94 break;
95 }
96 }
97 }
98 }
99
100 virtual void SetStringParameters(int widget) const
101 {
102 switch (widget) {
103 case WID_AIL_CAPTION:
104 SetDParam(0, (this->slot == OWNER_DEITY) ? STR_AI_LIST_CAPTION_GAMESCRIPT : STR_AI_LIST_CAPTION_AI);
105 break;
106 }
107 }
108
109 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
110 {
111 if (widget == WID_AIL_LIST) {
112 this->line_height = FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM;
113
114 resize->width = 1;
115 resize->height = this->line_height;
116 size->height = 5 * this->line_height;
117 }
118 }
119
120 virtual void DrawWidget(const Rect &r, int widget) const
121 {
122 switch (widget) {
123 case WID_AIL_LIST: {
124 /* Draw a list of all available AIs. */
125 int y = this->GetWidget<NWidgetBase>(WID_AIL_LIST)->pos_y;
126 /* First AI in the list is hardcoded to random */
127 if (this->vscroll->IsVisible(0)) {
128 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);
129 y += this->line_height;
130 }
131 ScriptInfoList::const_iterator it = this->info_list->begin();
132 for (int i = 1; it != this->info_list->end(); i++, it++) {
133 if (this->vscroll->IsVisible(i)) {
134 DrawString(r.left + WD_MATRIX_LEFT, r.right - WD_MATRIX_RIGHT, y + WD_MATRIX_TOP, (*it).second->GetName(), (this->selected == i - 1) ? TC_WHITE : TC_ORANGE);
135 y += this->line_height;
136 }
137 }
138 break;
139 }
140 case WID_AIL_INFO_BG: {
141 AIInfo *selected_info = NULL;
142 ScriptInfoList::const_iterator it = this->info_list->begin();
143 for (int i = 1; selected_info == NULL && it != this->info_list->end(); i++, it++) {
144 if (this->selected == i - 1) selected_info = static_cast<AIInfo *>((*it).second);
145 }
146 /* Some info about the currently selected AI. */
147 if (selected_info != NULL) {
148 int y = r.top + WD_FRAMERECT_TOP;
149 SetDParamStr(0, selected_info->GetAuthor());
150 DrawString(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, y, STR_AI_LIST_AUTHOR);
151 y += FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
152 SetDParam(0, selected_info->GetVersion());
153 DrawString(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, y, STR_AI_LIST_VERSION);
154 y += FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
155 if (selected_info->GetURL() != NULL) {
156 SetDParamStr(0, selected_info->GetURL());
157 DrawString(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, y, STR_AI_LIST_URL);
158 y += FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
159 }
160 SetDParamStr(0, selected_info->GetDescription());
161 DrawStringMultiLine(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, y, r.bottom - WD_FRAMERECT_BOTTOM, STR_JUST_RAW_STRING, TC_WHITE);
162 }
163 break;
164 }
165 }
166 }
167
171 void ChangeAI()
172 {
173 if (this->selected == -1) {
174 GetConfig(slot)->Change(NULL);
175 } else {
176 ScriptInfoList::const_iterator it = this->info_list->begin();
177 for (int i = 0; i < this->selected; i++) it++;
178 GetConfig(slot)->Change((*it).second->GetName(), (*it).second->GetVersion());
179 }
180 InvalidateWindowData(WC_GAME_OPTIONS, WN_GAME_OPTIONS_AI);
181 InvalidateWindowClassesData(WC_AI_SETTINGS);
182 DeleteWindowByClass(WC_QUERY_STRING);
183 }
184
185 virtual void OnClick(Point pt, int widget, int click_count)
186 {
187 switch (widget) {
188 case WID_AIL_LIST: { // Select one of the AIs
189 int sel = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_AIL_LIST, 0, this->line_height) - 1;
190 if (sel < (int)this->info_list->size()) {
191 this->selected = sel;
192 this->SetDirty();
193 if (click_count > 1) {
194 this->ChangeAI();
195 delete this;
196 }
197 }
198 break;
199 }
200
201 case WID_AIL_ACCEPT: {
202 this->ChangeAI();
203 delete this;
204 break;
205 }
206
207 case WID_AIL_CANCEL:
208 delete this;
209 break;
210 }
211 }
212
213 virtual void OnResize()
214 {
215 this->vscroll->SetCapacityFromWidget(this, WID_AIL_LIST);
216 }
217
223 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
224 {
225 if (_game_mode == GM_NORMAL && Company::IsValidID(this->slot)) {
226 delete this;
227 return;
228 }
229
230 if (!gui_scope) return;
231
232 this->vscroll->SetCount((int)this->info_list->size() + 1);
233
234 /* selected goes from -1 .. length of ai list - 1. */
235 this->selected = min(this->selected, this->vscroll->GetCount() - 2);
236 }
237 };
238
240 static const NWidgetPart _nested_ai_list_widgets[] = {
241 NWidget(NWID_HORIZONTAL),
242 NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
243 NWidget(WWT_CAPTION, COLOUR_MAUVE, WID_AIL_CAPTION), SetDataTip(STR_AI_LIST_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
244 NWidget(WWT_DEFSIZEBOX, COLOUR_MAUVE),
245 EndContainer(),
246 NWidget(NWID_HORIZONTAL),
247 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),
248 NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, WID_AIL_SCROLLBAR),
249 EndContainer(),
250 NWidget(WWT_PANEL, COLOUR_MAUVE, WID_AIL_INFO_BG), SetMinimalTextLines(8, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM), SetResize(1, 0),
251 EndContainer(),
252 NWidget(NWID_HORIZONTAL),
253 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
254 NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, WID_AIL_ACCEPT), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_AI_LIST_ACCEPT, STR_AI_LIST_ACCEPT_TOOLTIP),
255 NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, WID_AIL_CANCEL), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_AI_LIST_CANCEL, STR_AI_LIST_CANCEL_TOOLTIP),
256 EndContainer(),
257 NWidget(WWT_RESIZEBOX, COLOUR_MAUVE),
258 EndContainer(),
259 };
260
262 static WindowDesc _ai_list_desc(
263 WDP_CENTER, "settings_script_list", 200, 234,
264 WC_AI_LIST, WC_NONE,
265 0,
266 _nested_ai_list_widgets, lengthof(_nested_ai_list_widgets)
267 );
268
273 static void ShowAIListWindow(CompanyID slot)
274 {
275 DeleteWindowByClass(WC_AI_LIST);
276 new AIListWindow(&_ai_list_desc, slot);
277 }
278
282 struct AISettingsWindow : public Window {
283 CompanyID slot;
284 ScriptConfig *ai_config;
285 int clicked_button;
286 bool clicked_increase;
287 bool clicked_dropdown;
288 bool closing_dropdown;
289 int timeout;
290 int clicked_row;
291 int line_height;
292 Scrollbar *vscroll;
293 typedef std::vector<const ScriptConfigItem *> VisibleSettingsList;
294 VisibleSettingsList visible_settings;
295
301 AISettingsWindow(WindowDesc *desc, CompanyID slot) : Window(desc),
302 slot(slot),
303 clicked_button(-1),
304 clicked_dropdown(false),
305 closing_dropdown(false),
306 timeout(0)
307 {
308 this->ai_config = GetConfig(slot);
309
310 this->CreateNestedTree();
311 this->vscroll = this->GetScrollbar(WID_AIS_SCROLLBAR);
312 this->FinishInitNested(slot); // Initializes 'this->line_height' as side effect.
313
314 this->SetWidgetDisabledState(WID_AIS_RESET, _game_mode != GM_MENU && Company::IsValidID(this->slot));
315
316 this->RebuildVisibleSettings();
317 }
318
319 virtual void SetStringParameters(int widget) const
320 {
321 switch (widget) {
322 case WID_AIS_CAPTION:
323 SetDParam(0, (this->slot == OWNER_DEITY) ? STR_AI_SETTINGS_CAPTION_GAMESCRIPT : STR_AI_SETTINGS_CAPTION_AI);
324 break;
325 }
326 }
327
333 void RebuildVisibleSettings()
334 {
335 visible_settings.clear();
336
337 ScriptConfigItemList::const_iterator it = this->ai_config->GetConfigList()->begin();
338 for (; it != this->ai_config->GetConfigList()->end(); it++) {
339 bool no_hide = (it->flags & SCRIPTCONFIG_DEVELOPER) == 0;
340 if (no_hide || _settings_client.gui.ai_developer_tools) {
341 visible_settings.push_back(&(*it));
342 }
343 }
344
345 this->vscroll->SetCount((int)this->visible_settings.size());
346 }
347
348 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
349 {
350 if (widget == WID_AIS_BACKGROUND) {
351 this->line_height = max(SETTING_BUTTON_HEIGHT, FONT_HEIGHT_NORMAL) + WD_MATRIX_TOP + WD_MATRIX_BOTTOM;
352
353 resize->width = 1;
354 resize->height = this->line_height;
355 size->height = 5 * this->line_height;
356 }
357 }
358
359 virtual void DrawWidget(const Rect &r, int widget) const
360 {
361 if (widget != WID_AIS_BACKGROUND) return;
362
363 ScriptConfig *config = this->ai_config;
364 VisibleSettingsList::const_iterator it = this->visible_settings.begin();
365 int i = 0;
366 for (; !this->vscroll->IsVisible(i); i++) it++;
367
368 bool rtl = _current_text_dir == TD_RTL;
369 uint buttons_left = rtl ? r.right - SETTING_BUTTON_WIDTH - 3 : r.left + 4;
370 uint text_left = r.left + (rtl ? WD_FRAMERECT_LEFT : SETTING_BUTTON_WIDTH + 8);
371 uint text_right = r.right - (rtl ? SETTING_BUTTON_WIDTH + 8 : WD_FRAMERECT_RIGHT);
372
373
374 int y = r.top;
375 int button_y_offset = (this->line_height - SETTING_BUTTON_HEIGHT) / 2;
376 int text_y_offset = (this->line_height - FONT_HEIGHT_NORMAL) / 2;
377 for (; this->vscroll->IsVisible(i) && it != visible_settings.end(); i++, it++) {
378 const ScriptConfigItem &config_item = **it;
379 int current_value = config->GetSetting((config_item).name);
380 bool editable = _game_mode == GM_MENU || ((this->slot != OWNER_DEITY) && !Company::IsValidID(this->slot)) || (config_item.flags & SCRIPTCONFIG_INGAME) != 0;
381
382 StringID str;
383 TextColour colour;
384 uint idx = 0;
385 if (StrEmpty(config_item.description)) {
386 if (!strcmp(config_item.name, "start_date")) {
387 /* Build-in translation */
388 str = STR_AI_SETTINGS_START_DELAY;
389 colour = TC_LIGHT_BLUE;
390 } else {
391 str = STR_JUST_STRING;
392 colour = TC_ORANGE;
393 }
394 } else {
395 str = STR_AI_SETTINGS_SETTING;
396 colour = TC_LIGHT_BLUE;
397 SetDParamStr(idx++, config_item.description);
398 }
399
400 if ((config_item.flags & SCRIPTCONFIG_BOOLEAN) != 0) {
401 DrawBoolButton(buttons_left, y + button_y_offset, current_value != 0, editable);
402 SetDParam(idx++, current_value == 0 ? STR_CONFIG_SETTING_OFF : STR_CONFIG_SETTING_ON);
403 } else {
404 if (config_item.complete_labels) {
405 DrawDropDownButton(buttons_left, y + button_y_offset, COLOUR_YELLOW, this->clicked_row == i && clicked_dropdown, editable);
406 } else {
407 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);
408 }
409 if (config_item.labels != NULL && config_item.labels->Contains(current_value)) {
410 SetDParam(idx++, STR_JUST_RAW_STRING);
411 SetDParamStr(idx++, config_item.labels->Find(current_value)->second);
412 } else {
413 SetDParam(idx++, STR_JUST_INT);
414 SetDParam(idx++, current_value);
415 }
416 }
417
418 DrawString(text_left, text_right, y + text_y_offset, str, colour);
419 y += this->line_height;
420 }
421 }
422
423 virtual void OnPaint()
424 {
425 if (this->closing_dropdown) {
426 this->closing_dropdown = false;
427 this->clicked_dropdown = false;
428 }
429 this->DrawWidgets();
430 }
431
432 virtual void OnClick(Point pt, int widget, int click_count)
433 {
434 switch (widget) {
435 case WID_AIS_BACKGROUND: {
436 const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_AIS_BACKGROUND);
437 int num = (pt.y - wid->pos_y) / this->line_height + this->vscroll->GetPosition();
438 if (num >= (int)this->visible_settings.size()) break;
439
440 VisibleSettingsList::const_iterator it = this->visible_settings.begin();
441 for (int i = 0; i < num; i++) it++;
442 const ScriptConfigItem config_item = **it;
443 if (_game_mode == GM_NORMAL && ((this->slot == OWNER_DEITY) || Company::IsValidID(this->slot)) && (config_item.flags & SCRIPTCONFIG_INGAME) == 0) return;
444
445 if (this->clicked_row != num) {
446 DeleteChildWindows(WC_QUERY_STRING);
447 HideDropDownMenu(this);
448 this->clicked_row = num;
449 this->clicked_dropdown = false;
450 }
451
452 bool bool_item = (config_item.flags & SCRIPTCONFIG_BOOLEAN) != 0;
453
454 int x = pt.x - wid->pos_x;
455 if (_current_text_dir == TD_RTL) x = wid->current_x - 1 - x;
456 x -= 4;
457
458 /* One of the arrows is clicked (or green/red rect in case of bool value) */
459 int old_val = this->ai_config->GetSetting(config_item.name);
460 if (!bool_item && IsInsideMM(x, 0, SETTING_BUTTON_WIDTH) && config_item.complete_labels) {
461 if (this->clicked_dropdown) {
462 /* unclick the dropdown */
463 HideDropDownMenu(this);
464 this->clicked_dropdown = false;
465 this->closing_dropdown = false;
466 } else {
467 const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_AIS_BACKGROUND);
468 int rel_y = (pt.y - (int)wid->pos_y) % this->line_height;
469
470 Rect wi_rect;
471 wi_rect.left = pt.x - (_current_text_dir == TD_RTL ? SETTING_BUTTON_WIDTH - 1 - x : x);
472 wi_rect.right = wi_rect.left + SETTING_BUTTON_WIDTH - 1;
473 wi_rect.top = pt.y - rel_y + (this->line_height - SETTING_BUTTON_HEIGHT) / 2;
474 wi_rect.bottom = wi_rect.top + SETTING_BUTTON_HEIGHT - 1;
475
476 /* For dropdowns we also have to check the y position thoroughly, the mouse may not above the just opening dropdown */
477 if (pt.y >= wi_rect.top && pt.y <= wi_rect.bottom) {
478 this->clicked_dropdown = true;
479 this->closing_dropdown = false;
480
481 DropDownList *list = new DropDownList();
482 for (int i = config_item.min_value; i <= config_item.max_value; i++) {
483 *list->Append() = new DropDownListCharStringItem(config_item.labels->Find(i)->second, i, false);
484 }
485
486 ShowDropDownListAt(this, list, old_val, -1, wi_rect, COLOUR_ORANGE, true);
487 }
488 }
489 } else if (IsInsideMM(x, 0, SETTING_BUTTON_WIDTH)) {
490 int new_val = old_val;
491 if (bool_item) {
492 new_val = !new_val;
493 } else if (x >= SETTING_BUTTON_WIDTH / 2) {
494 /* Increase button clicked */
495 new_val += config_item.step_size;
496 if (new_val > config_item.max_value) new_val = config_item.max_value;
497 this->clicked_increase = true;
498 } else {
499 /* Decrease button clicked */
500 new_val -= config_item.step_size;
501 if (new_val < config_item.min_value) new_val = config_item.min_value;
502 this->clicked_increase = false;
503 }
504
505 if (new_val != old_val) {
506 this->ai_config->SetSetting(config_item.name, new_val);
507 this->clicked_button = num;
508 this->timeout = 5;
509 }
510 } else if (!bool_item && !config_item.complete_labels) {
511 /* Display a query box so users can enter a custom value. */
512 SetDParam(0, old_val);
513 ShowQueryString(STR_JUST_INT, STR_CONFIG_SETTING_QUERY_CAPTION, 10, this, CS_NUMERAL, QSF_NONE);
514 }
515 this->SetDirty();
516 break;
517 }
518
519 case WID_AIS_ACCEPT:
520 delete this;
521 break;
522
523 case WID_AIS_RESET:
524 if (_game_mode == GM_MENU || !Company::IsValidID(this->slot)) {
525 this->ai_config->ResetSettings();
526 this->SetDirty();
527 }
528 break;
529 }
530 }
531
532 virtual void OnQueryTextFinished(char *str)
533 {
534 if (StrEmpty(str)) return;
535 VisibleSettingsList::const_iterator it = this->visible_settings.begin();
536 for (int i = 0; i < this->clicked_row; i++) it++;
537 const ScriptConfigItem config_item = **it;
538 if (_game_mode == GM_NORMAL && ((this->slot == OWNER_DEITY) || Company::IsValidID(this->slot)) && (config_item.flags & SCRIPTCONFIG_INGAME) == 0) return;
539 int32 value = atoi(str);
540 this->ai_config->SetSetting(config_item.name, value);
541 this->SetDirty();
542 }
543
544 virtual void OnDropdownSelect(int widget, int index)
545 {
546 assert(this->clicked_dropdown);
547 VisibleSettingsList::const_iterator it = this->visible_settings.begin();
548 for (int i = 0; i < this->clicked_row; i++) it++;
549 const ScriptConfigItem config_item = **it;
550 if (_game_mode == GM_NORMAL && ((this->slot == OWNER_DEITY) || Company::IsValidID(this->slot)) && (config_item.flags & SCRIPTCONFIG_INGAME) == 0) return;
551 this->ai_config->SetSetting(config_item.name, index);
552 this->SetDirty();
553 }
554
555 virtual void OnDropdownClose(Point pt, int widget, int index, bool instant_close)
556 {
557 /* We cannot raise the dropdown button just yet. OnClick needs some hint, whether
558 * the same dropdown button was clicked again, and then not open the dropdown again.
559 * So, we only remember that it was closed, and process it on the next OnPaint, which is
560 * after OnClick. */
561 assert(this->clicked_dropdown);
562 this->closing_dropdown = true;
563 this->SetDirty();
564 }
565
566 virtual void OnResize()
567 {
568 this->vscroll->SetCapacityFromWidget(this, WID_AIS_BACKGROUND);
569 }
570
571 virtual void OnTick()
572 {
573 if (--this->timeout == 0) {
574 this->clicked_button = -1;
575 this->SetDirty();
576 }
577 }
578
584 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
585 {
586 this->RebuildVisibleSettings();
587 }
588 };
589
591 static const NWidgetPart _nested_ai_settings_widgets[] = {
592 NWidget(NWID_HORIZONTAL),
593 NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
594 NWidget(WWT_CAPTION, COLOUR_MAUVE, WID_AIS_CAPTION), SetDataTip(STR_AI_SETTINGS_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
595 NWidget(WWT_DEFSIZEBOX, COLOUR_MAUVE),
596 EndContainer(),
597 NWidget(NWID_HORIZONTAL),
598 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),
599 NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, WID_AIS_SCROLLBAR),
600 EndContainer(),
601 NWidget(NWID_HORIZONTAL),
602 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
603 NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, WID_AIS_ACCEPT), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_AI_SETTINGS_CLOSE, STR_NULL),
604 NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, WID_AIS_RESET), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_AI_SETTINGS_RESET, STR_NULL),
605 EndContainer(),
606 NWidget(WWT_RESIZEBOX, COLOUR_MAUVE),
607 EndContainer(),
608 };
609
611 static WindowDesc _ai_settings_desc(
612 WDP_CENTER, "settings_script", 500, 208,
613 WC_AI_SETTINGS, WC_NONE,
614 0,
615 _nested_ai_settings_widgets, lengthof(_nested_ai_settings_widgets)
616 );
617
622 static void ShowAISettingsWindow(CompanyID slot)
623 {
624 DeleteWindowByClass(WC_AI_LIST);
625 DeleteWindowByClass(WC_AI_SETTINGS);
626 new AISettingsWindow(&_ai_settings_desc, slot);
627 }
628
629
631 struct ScriptTextfileWindow : public TextfileWindow {
632 CompanyID slot;
633
634 ScriptTextfileWindow(TextfileType file_type, CompanyID slot) : TextfileWindow(file_type), slot(slot)
635 {
636 const char *textfile = GetConfig(slot)->GetTextfile(file_type, slot);
637 this->LoadTextfile(textfile, (slot == OWNER_DEITY) ? GAME_DIR : AI_DIR);
638 }
639
640 /* virtual */ void SetStringParameters(int widget) const
641 {
642 if (widget == WID_TF_CAPTION) {
643 SetDParam(0, (slot == OWNER_DEITY) ? STR_CONTENT_TYPE_GAME_SCRIPT : STR_CONTENT_TYPE_AI);
644 SetDParamStr(1, GetConfig(slot)->GetName());
645 }
646 }
647 };
648
654 void ShowScriptTextfileWindow(TextfileType file_type, CompanyID slot)
655 {
656 DeleteWindowByClass(WC_TEXTFILE);
657 new ScriptTextfileWindow(file_type, slot);
658 }
659
660
662 static const NWidgetPart _nested_ai_config_widgets[] = {
663 NWidget(NWID_HORIZONTAL),
664 NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
665 NWidget(WWT_CAPTION, COLOUR_MAUVE), SetDataTip(STR_AI_CONFIG_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
666 EndContainer(),
667 NWidget(WWT_PANEL, COLOUR_MAUVE, WID_AIC_BACKGROUND),
668 NWidget(NWID_VERTICAL), SetPIP(4, 4, 4),
669 NWidget(NWID_HORIZONTAL), SetPIP(7, 0, 7),
670 NWidget(WWT_PUSHARROWBTN, COLOUR_YELLOW, WID_AIC_DECREASE), SetFill(0, 1), SetDataTip(AWV_DECREASE, STR_NULL),
671 NWidget(WWT_PUSHARROWBTN, COLOUR_YELLOW, WID_AIC_INCREASE), SetFill(0, 1), SetDataTip(AWV_INCREASE, STR_NULL),
672 NWidget(NWID_SPACER), SetMinimalSize(6, 0),
673 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),
674 EndContainer(),
675 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(7, 0, 7),
676 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),
677 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),
678 EndContainer(),
679 EndContainer(),
680 NWidget(WWT_FRAME, COLOUR_MAUVE), SetDataTip(STR_AI_CONFIG_AI, STR_NULL), SetPadding(0, 5, 0, 5),
681 NWidget(NWID_HORIZONTAL),
682 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),
683 NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, WID_AIC_SCROLLBAR),
684 EndContainer(),
685 EndContainer(),
686 NWidget(NWID_SPACER), SetMinimalSize(0, 9),
687 NWidget(WWT_FRAME, COLOUR_MAUVE), SetDataTip(STR_AI_CONFIG_GAMESCRIPT, STR_NULL), SetPadding(0, 5, 4, 5),
688 NWidget(WWT_MATRIX, COLOUR_MAUVE, WID_AIC_GAMELIST), SetMinimalSize(288, 14), SetFill(1, 0), SetMatrixDataTip(1, 1, STR_AI_CONFIG_GAMELIST_TOOLTIP),
689 EndContainer(),
690 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(7, 0, 7),
691 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_AIC_CHANGE), SetFill(1, 0), SetMinimalSize(93, 12), SetDataTip(STR_AI_CONFIG_CHANGE, STR_AI_CONFIG_CHANGE_TOOLTIP),
692 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_AIC_CONFIGURE), SetFill(1, 0), SetMinimalSize(93, 12), SetDataTip(STR_AI_CONFIG_CONFIGURE, STR_AI_CONFIG_CONFIGURE_TOOLTIP),
693 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_AIC_CLOSE), SetFill(1, 0), SetMinimalSize(93, 12), SetDataTip(STR_AI_SETTINGS_CLOSE, STR_NULL),
694 EndContainer(),
695 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(7, 0, 7),
696 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_AIC_TEXTFILE + TFT_README), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_TEXTFILE_VIEW_README, STR_NULL),
697 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_AIC_TEXTFILE + TFT_CHANGELOG), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_TEXTFILE_VIEW_CHANGELOG, STR_NULL),
698 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_AIC_TEXTFILE + TFT_LICENSE), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_TEXTFILE_VIEW_LICENCE, STR_NULL),
699 EndContainer(),
700 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),
701 EndContainer(),
702 };
703
705 static WindowDesc _ai_config_desc(
706 WDP_CENTER, "settings_script_config", 0, 0,
707 WC_GAME_OPTIONS, WC_NONE,
708 0,
709 _nested_ai_config_widgets, lengthof(_nested_ai_config_widgets)
710 );
711
715 struct AIConfigWindow : public Window {
716 CompanyID selected_slot;
717 int line_height;
718 Scrollbar *vscroll;
719
720 AIConfigWindow() : Window(&_ai_config_desc)
721 {
722 this->InitNested(WN_GAME_OPTIONS_AI); // Initializes 'this->line_height' as a side effect.
723 this->vscroll = this->GetScrollbar(WID_AIC_SCROLLBAR);
724 this->selected_slot = INVALID_COMPANY;
725 NWidgetCore *nwi = this->GetWidget<NWidgetCore>(WID_AIC_LIST);
726 this->vscroll->SetCapacity(nwi->current_y / this->line_height);
727 this->vscroll->SetCount(MAX_COMPANIES);
728 this->OnInvalidateData(0);
729 }
730
731 ~AIConfigWindow()
732 {
733 DeleteWindowByClass(WC_AI_LIST);
734 DeleteWindowByClass(WC_AI_SETTINGS);
735 }
736
737 virtual void SetStringParameters(int widget) const
738 {
739 switch (widget) {
740 case WID_AIC_NUMBER:
741 SetDParam(0, GetGameSettings().difficulty.max_no_competitors);
742 break;
743 case WID_AIC_CHANGE:
744 switch (selected_slot) {
745 case OWNER_DEITY:
746 SetDParam(0, STR_AI_CONFIG_CHANGE_GAMESCRIPT);
747 break;
748
749 case INVALID_COMPANY:
750 SetDParam(0, STR_AI_CONFIG_CHANGE_NONE);
751 break;
752
753 default:
754 SetDParam(0, STR_AI_CONFIG_CHANGE_AI);
755 break;
756 }
757 break;
758 }
759 }
760
761 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
762 {
763 switch (widget) {
764 case WID_AIC_GAMELIST:
765 this->line_height = FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM;
766 size->height = 1 * this->line_height;
767 break;
768
769 case WID_AIC_LIST:
770 this->line_height = FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM;
771 size->height = 8 * this->line_height;
772 break;
773
774 case WID_AIC_CHANGE: {
775 SetDParam(0, STR_AI_CONFIG_CHANGE_GAMESCRIPT);
776 Dimension dim = GetStringBoundingBox(STR_AI_CONFIG_CHANGE);
777
778 SetDParam(0, STR_AI_CONFIG_CHANGE_NONE);
779 dim = maxdim(dim, GetStringBoundingBox(STR_AI_CONFIG_CHANGE));
780
781 SetDParam(0, STR_AI_CONFIG_CHANGE_AI);
782 dim = maxdim(dim, GetStringBoundingBox(STR_AI_CONFIG_CHANGE));
783
784 dim.width += padding.width;
785 dim.height += padding.height;
786 *size = maxdim(*size, dim);
787 break;
788 }
789 }
790 }
791
797 static bool IsEditable(CompanyID slot)
798 {
799 if (slot == OWNER_DEITY) return _game_mode != GM_NORMAL || Game::GetInstance() != NULL;
800
801 if (_game_mode != GM_NORMAL) {
802 return slot > 0 && slot <= GetGameSettings().difficulty.max_no_competitors;
803 }
804 if (Company::IsValidID(slot) || slot < 0) return false;
805
806 int max_slot = GetGameSettings().difficulty.max_no_competitors;
807 for (CompanyID cid = COMPANY_FIRST; cid < (CompanyID)max_slot && cid < MAX_COMPANIES; cid++) {
808 if (Company::IsValidHumanID(cid)) max_slot++;
809 }
810 return slot < max_slot;
811 }
812
813 virtual void DrawWidget(const Rect &r, int widget) const
814 {
815 switch (widget) {
816 case WID_AIC_GAMELIST: {
817 StringID text = STR_AI_CONFIG_NONE;
818
819 if (GameConfig::GetConfig()->GetInfo() != NULL) {
820 SetDParamStr(0, GameConfig::GetConfig()->GetInfo()->GetName());
821 text = STR_JUST_RAW_STRING;
822 }
823
824 DrawString(r.left + 10, r.right - 10, r.top + WD_MATRIX_TOP, text,
825 (this->selected_slot == OWNER_DEITY) ? TC_WHITE : (IsEditable(OWNER_DEITY) ? TC_ORANGE : TC_SILVER));
826
827 break;
828 }
829
830 case WID_AIC_LIST: {
831 int y = r.top;
832 for (int i = this->vscroll->GetPosition(); this->vscroll->IsVisible(i) && i < MAX_COMPANIES; i++) {
833 StringID text;
834
835 if ((_game_mode != GM_NORMAL && i == 0) || (_game_mode == GM_NORMAL && Company::IsValidHumanID(i))) {
836 text = STR_AI_CONFIG_HUMAN_PLAYER;
837 } else if (AIConfig::GetConfig((CompanyID)i)->GetInfo() != NULL) {
838 SetDParamStr(0, AIConfig::GetConfig((CompanyID)i)->GetInfo()->GetName());
839 text = STR_JUST_RAW_STRING;
840 } else {
841 text = STR_AI_CONFIG_RANDOM_AI;
842 }
843 DrawString(r.left + 10, r.right - 10, y + WD_MATRIX_TOP, text,
844 (this->selected_slot == i) ? TC_WHITE : (IsEditable((CompanyID)i) ? TC_ORANGE : TC_SILVER));
845 y += this->line_height;
846 }
847 break;
848 }
849 }
850 }
851
852 virtual void OnClick(Point pt, int widget, int click_count)
853 {
854 if (widget >= WID_AIC_TEXTFILE && widget < WID_AIC_TEXTFILE + TFT_END) {
855 if (this->selected_slot == INVALID_COMPANY || GetConfig(this->selected_slot) == NULL) return;
856
857 ShowScriptTextfileWindow((TextfileType)(widget - WID_AIC_TEXTFILE), this->selected_slot);
858 return;
859 }
860
861 switch (widget) {
862 case WID_AIC_DECREASE:
863 case WID_AIC_INCREASE: {
864 int new_value;
865 if (widget == WID_AIC_DECREASE) {
866 new_value = max(0, GetGameSettings().difficulty.max_no_competitors - 1);
867 } else {
868 new_value = min(MAX_COMPANIES - 1, GetGameSettings().difficulty.max_no_competitors + 1);
869 }
870 IConsoleSetSetting("difficulty.max_no_competitors", new_value);
871 this->InvalidateData();
872 break;
873 }
874
875 case WID_AIC_GAMELIST: {
876 this->selected_slot = OWNER_DEITY;
877 this->InvalidateData();
878 if (click_count > 1 && this->selected_slot != INVALID_COMPANY && _game_mode != GM_NORMAL) ShowAIListWindow((CompanyID)this->selected_slot);
879 break;
880 }
881
882 case WID_AIC_LIST: { // Select a slot
883 this->selected_slot = (CompanyID)this->vscroll->GetScrolledRowFromWidget(pt.y, this, widget, 0, this->line_height);
884 this->InvalidateData();
885 if (click_count > 1 && this->selected_slot != INVALID_COMPANY) ShowAIListWindow((CompanyID)this->selected_slot);
886 break;
887 }
888
889 case WID_AIC_MOVE_UP:
890 if (IsEditable(this->selected_slot) && IsEditable((CompanyID)(this->selected_slot - 1))) {
891 Swap(GetGameSettings().ai_config[this->selected_slot], GetGameSettings().ai_config[this->selected_slot - 1]);
892 this->selected_slot--;
893 this->vscroll->ScrollTowards(this->selected_slot);
894 this->InvalidateData();
895 }
896 break;
897
898 case WID_AIC_MOVE_DOWN:
899 if (IsEditable(this->selected_slot) && IsEditable((CompanyID)(this->selected_slot + 1))) {
900 Swap(GetGameSettings().ai_config[this->selected_slot], GetGameSettings().ai_config[this->selected_slot + 1]);
901 this->selected_slot++;
902 this->vscroll->ScrollTowards(this->selected_slot);
903 this->InvalidateData();
904 }
905 break;
906
907 case WID_AIC_CHANGE: // choose other AI
908 ShowAIListWindow((CompanyID)this->selected_slot);
909 break;
910
911 case WID_AIC_CONFIGURE: // change the settings for an AI
912 ShowAISettingsWindow((CompanyID)this->selected_slot);
913 break;
914
915 case WID_AIC_CLOSE:
916 delete this;
917 break;
918
919 case WID_AIC_CONTENT_DOWNLOAD:
920 if (!_network_available) {
921 ShowErrorMessage(STR_NETWORK_ERROR_NOTAVAILABLE, INVALID_STRING_ID, WL_ERROR);
922 } else {
923 #if defined(ENABLE_NETWORK)
924 ShowNetworkContentListWindow(NULL, CONTENT_TYPE_AI, CONTENT_TYPE_GAME);
925 #endif
926 }
927 break;
928 }
929 }
930
936 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
937 {
938 if (!IsEditable(this->selected_slot)) {
939 this->selected_slot = INVALID_COMPANY;
940 }
941
942 if (!gui_scope) return;
943
944 this->SetWidgetDisabledState(WID_AIC_DECREASE, GetGameSettings().difficulty.max_no_competitors == 0);
945 this->SetWidgetDisabledState(WID_AIC_INCREASE, GetGameSettings().difficulty.max_no_competitors == MAX_COMPANIES - 1);
946 this->SetWidgetDisabledState(WID_AIC_CHANGE, (this->selected_slot == OWNER_DEITY && _game_mode == GM_NORMAL) || this->selected_slot == INVALID_COMPANY);
947 this->SetWidgetDisabledState(WID_AIC_CONFIGURE, this->selected_slot == INVALID_COMPANY || GetConfig(this->selected_slot)->GetConfigList()->size() == 0);
948 this->SetWidgetDisabledState(WID_AIC_MOVE_UP, this->selected_slot == OWNER_DEITY || this->selected_slot == INVALID_COMPANY || !IsEditable((CompanyID)(this->selected_slot - 1)));
949 this->SetWidgetDisabledState(WID_AIC_MOVE_DOWN, this->selected_slot == OWNER_DEITY || this->selected_slot == INVALID_COMPANY || !IsEditable((CompanyID)(this->selected_slot + 1)));
950
951 for (TextfileType tft = TFT_BEGIN; tft < TFT_END; tft++) {
952 this->SetWidgetDisabledState(WID_AIC_TEXTFILE + tft, this->selected_slot == INVALID_COMPANY || (GetConfig(this->selected_slot)->GetTextfile(tft, this->selected_slot) == NULL));
953 }
954 }
955 };
956
958 void ShowAIConfigWindow()
959 {
960 DeleteWindowByClass(WC_GAME_OPTIONS);
961 new AIConfigWindow();
962 }
963
972 static bool SetScriptButtonColour(NWidgetCore &button, bool dead, bool paused)
973 {
974 /* Dead scripts are indicated with red background and
975 * paused scripts are indicated with yellow background. */
976 Colours colour = dead ? COLOUR_RED :
977 (paused ? COLOUR_YELLOW : COLOUR_GREY);
978 if (button.colour != colour) {
979 button.colour = colour;
980 return true;
981 }
982 return false;
983 }
984
988 struct AIDebugWindow : public Window {
989 static const int top_offset;
990 static const int bottom_offset;
991
992 static const uint MAX_BREAK_STR_STRING_LENGTH = 256;
993
994 static CompanyID ai_debug_company;
995 int redraw_timer;
996 int last_vscroll_pos;
997 bool autoscroll;
998 bool show_break_box;
999 static bool break_check_enabled;
1000 static char break_string[MAX_BREAK_STR_STRING_LENGTH];
1001 QueryString break_editbox;
1002 static StringFilter break_string_filter;
1003 static bool case_sensitive_break_check;
1004 int highlight_row;
1005 Scrollbar *vscroll;
1006
1007 ScriptLog::LogData *GetLogPointer() const
1008 {
1009 if (ai_debug_company == OWNER_DEITY) return (ScriptLog::LogData *)Game::GetInstance()->GetLogPointer();
1010 return (ScriptLog::LogData *)Company::Get(ai_debug_company)->ai_instance->GetLogPointer();
1011 }
1012
1017 bool IsDead() const
1018 {
1019 if (ai_debug_company == OWNER_DEITY) {
1020 GameInstance *game = Game::GetInstance();
1021 return game == NULL || game->IsDead();
1022 }
1023 return !Company::IsValidAiID(ai_debug_company) || Company::Get(ai_debug_company)->ai_instance->IsDead();
1024 }
1025
1031 bool IsValidDebugCompany(CompanyID company) const
1032 {
1033 switch (company) {
1034 case INVALID_COMPANY: return false;
1035 case OWNER_DEITY: return Game::GetInstance() != NULL;
1036 default: return Company::IsValidAiID(company);
1037 }
1038 }
1039
1044 void SelectValidDebugCompany()
1045 {
1046 /* Check if the currently selected company is still active. */
1047 if (this->IsValidDebugCompany(ai_debug_company)) return;
1048
1049 ai_debug_company = INVALID_COMPANY;
1050
1051 const Company *c;
1052 FOR_ALL_COMPANIES(c) {
1053 if (c->is_ai) {
1054 ChangeToAI(c->index);
1055 return;
1056 }
1057 }
1058
1059 /* If no AI is available, see if there is a game script. */
1060 if (Game::GetInstance() != NULL) ChangeToAI(OWNER_DEITY);
1061 }
1062
1068 AIDebugWindow(WindowDesc *desc, WindowNumber number) : Window(desc), break_editbox(MAX_BREAK_STR_STRING_LENGTH)
1069 {
1070 this->CreateNestedTree();
1071 this->vscroll = this->GetScrollbar(WID_AID_SCROLLBAR);
1072 this->show_break_box = _settings_client.gui.ai_developer_tools;
1073 this->GetWidget<NWidgetStacked>(WID_AID_BREAK_STRING_WIDGETS)->SetDisplayedPlane(this->show_break_box ? 0 : SZSP_HORIZONTAL);
1074 this->FinishInitNested(number);
1075
1076 if (!this->show_break_box) break_check_enabled = false;
1077
1078 this->last_vscroll_pos = 0;
1079 this->autoscroll = true;
1080 this->highlight_row = -1;
1081
1082 this->querystrings[WID_AID_BREAK_STR_EDIT_BOX] = &this->break_editbox;
1083
1084 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);
1085
1086 /* Restore the break string value from static variable */
1087 this->break_editbox.text.Assign(this->break_string);
1088
1089 this->SelectValidDebugCompany();
1090 this->InvalidateData(-1);
1091 }
1092
1093 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
1094 {
1095 if (widget == WID_AID_LOG_PANEL) {
1096 resize->height = FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
1097 size->height = 14 * resize->height + this->top_offset + this->bottom_offset;
1098 }
1099 }
1100
1101 virtual void OnPaint()
1102 {
1103 this->SelectValidDebugCompany();
1104
1105 /* Draw standard stuff */
1106 this->DrawWidgets();
1107
1108 if (this->IsShaded()) return; // Don't draw anything when the window is shaded.
1109
1110 bool dirty = false;
1111
1112 /* Paint the company icons */
1113 for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
1114 NWidgetCore *button = this->GetWidget<NWidgetCore>(i + WID_AID_COMPANY_BUTTON_START);
1115
1116 bool valid = Company::IsValidAiID(i);
1117
1118 /* Check whether the validity of the company changed */
1119 dirty |= (button->IsDisabled() == valid);
1120
1121 /* Mark dead/paused AIs by setting the background colour. */
1122 bool dead = valid && Company::Get(i)->ai_instance->IsDead();
1123 bool paused = valid && Company::Get(i)->ai_instance->IsPaused();
1124 /* Re-paint if the button was updated.
1125 * (note that it is intentional that SetScriptButtonColour is always called) */
1126 dirty |= SetScriptButtonColour(*button, dead, paused);
1127
1128 /* Draw company icon only for valid AI companies */
1129 if (!valid) continue;
1130
1131 byte offset = (i == ai_debug_company) ? 1 : 0;
1132 DrawCompanyIcon(i, button->pos_x + button->current_x / 2 - 7 + offset, this->GetWidget<NWidgetBase>(WID_AID_COMPANY_BUTTON_START + i)->pos_y + 2 + offset);
1133 }
1134
1135 /* Set button colour for Game Script. */
1136 GameInstance *game = Game::GetInstance();
1137 bool valid = game != NULL;
1138 bool dead = valid && game->IsDead();
1139 bool paused = valid && game->IsPaused();
1140
1141 NWidgetCore *button = this->GetWidget<NWidgetCore>(WID_AID_SCRIPT_GAME);
1142 dirty |= (button->IsDisabled() == valid) || SetScriptButtonColour(*button, dead, paused);
1143
1144 if (dirty) this->InvalidateData(-1);
1145
1146 /* If there are no active companies, don't display anything else. */
1147 if (ai_debug_company == INVALID_COMPANY) return;
1148
1149 ScriptLog::LogData *log = this->GetLogPointer();
1150
1151 int scroll_count = (log == NULL) ? 0 : log->used;
1152 if (this->vscroll->GetCount() != scroll_count) {
1153 this->vscroll->SetCount(scroll_count);
1154
1155 /* We need a repaint */
1156 this->SetWidgetDirty(WID_AID_SCROLLBAR);
1157 }
1158
1159 if (log == NULL) return;
1160
1161 /* Detect when the user scrolls the window. Enable autoscroll when the
1162 * bottom-most line becomes visible. */
1163 if (this->last_vscroll_pos != this->vscroll->GetPosition()) {
1164 this->autoscroll = this->vscroll->GetPosition() >= log->used - this->vscroll->GetCapacity();
1165 }
1166 if (this->autoscroll) {
1167 int scroll_pos = max(0, log->used - this->vscroll->GetCapacity());
1168 if (scroll_pos != this->vscroll->GetPosition()) {
1169 this->vscroll->SetPosition(scroll_pos);
1170
1171 /* We need a repaint */
1172 this->SetWidgetDirty(WID_AID_SCROLLBAR);
1173 this->SetWidgetDirty(WID_AID_LOG_PANEL);
1174 }
1175 }
1176 this->last_vscroll_pos = this->vscroll->GetPosition();
1177 }
1178
1179 virtual void SetStringParameters(int widget) const
1180 {
1181 switch (widget) {
1182 case WID_AID_NAME_TEXT:
1183 if (ai_debug_company == OWNER_DEITY) {
1184 const GameInfo *info = Game::GetInfo();
1185 assert(info != NULL);
1186 SetDParam(0, STR_AI_DEBUG_NAME_AND_VERSION);
1187 SetDParamStr(1, info->GetName());
1188 SetDParam(2, info->GetVersion());
1189 } else if (ai_debug_company == INVALID_COMPANY || !Company::IsValidAiID(ai_debug_company)) {
1190 SetDParam(0, STR_EMPTY);
1191 } else {
1192 const AIInfo *info = Company::Get(ai_debug_company)->ai_info;
1193 assert(info != NULL);
1194 SetDParam(0, STR_AI_DEBUG_NAME_AND_VERSION);
1195 SetDParamStr(1, info->GetName());
1196 SetDParam(2, info->GetVersion());
1197 }
1198 break;
1199 }
1200 }
1201
1202 virtual void DrawWidget(const Rect &r, int widget) const
1203 {
1204 if (ai_debug_company == INVALID_COMPANY) return;
1205
1206 switch (widget) {
1207 case WID_AID_LOG_PANEL: {
1208 ScriptLog::LogData *log = this->GetLogPointer();
1209 if (log == NULL) return;
1210
1211 int y = this->top_offset;
1212 for (int i = this->vscroll->GetPosition(); this->vscroll->IsVisible(i) && i < log->used; i++) {
1213 int pos = (i + log->pos + 1 - log->used + log->count) % log->count;
1214 if (log->lines[pos] == NULL) break;
1215
1216 TextColour colour;
1217 switch (log->type[pos]) {
1218 case ScriptLog::LOG_SQ_INFO: colour = TC_BLACK; break;
1219 case ScriptLog::LOG_SQ_ERROR: colour = TC_RED; break;
1220 case ScriptLog::LOG_INFO: colour = TC_BLACK; break;
1221 case ScriptLog::LOG_WARNING: colour = TC_YELLOW; break;
1222 case ScriptLog::LOG_ERROR: colour = TC_RED; break;
1223 default: colour = TC_BLACK; break;
1224 }
1225
1226 /* Check if the current line should be highlighted */
1227 if (pos == this->highlight_row) {
1228 GfxFillRect(r.left + 1, r.top + y, r.right - 1, r.top + y + this->resize.step_height - WD_PAR_VSEP_NORMAL, PC_BLACK);
1229 if (colour == TC_BLACK) colour = TC_WHITE; // Make black text readable by inverting it to white.
1230 }
1231
1232 DrawString(r.left + 7, r.right - 7, r.top + y, log->lines[pos], colour, SA_LEFT | SA_FORCE);
1233 y += this->resize.step_height;
1234 }
1235 break;
1236 }
1237 }
1238 }
1239
1244 void ChangeToAI(CompanyID show_ai)
1245 {
1246 if (!this->IsValidDebugCompany(show_ai)) return;
1247
1248 ai_debug_company = show_ai;
1249
1250 this->highlight_row = -1; // The highlight of one AI make little sense for another AI.
1251
1252 /* Close AI settings window to prevent confusion */
1253 DeleteWindowByClass(WC_AI_SETTINGS);
1254
1255 this->InvalidateData(-1);
1256
1257 this->autoscroll = true;
1258 this->last_vscroll_pos = this->vscroll->GetPosition();
1259 }
1260
1261 virtual void OnClick(Point pt, int widget, int click_count)
1262 {
1263 /* Also called for hotkeys, so check for disabledness */
1264 if (this->IsWidgetDisabled(widget)) return;
1265
1266 /* Check which button is clicked */
1267 if (IsInsideMM(widget, WID_AID_COMPANY_BUTTON_START, WID_AID_COMPANY_BUTTON_END + 1)) {
1268 ChangeToAI((CompanyID)(widget - WID_AID_COMPANY_BUTTON_START));
1269 }
1270
1271 switch (widget) {
1272 case WID_AID_SCRIPT_GAME:
1273 ChangeToAI(OWNER_DEITY);
1274 break;
1275
1276 case WID_AID_RELOAD_TOGGLE:
1277 if (ai_debug_company == OWNER_DEITY) break;
1278 /* First kill the company of the AI, then start a new one. This should start the current AI again */
1279 DoCommandP(0, 2 | ai_debug_company << 16, CRR_MANUAL, CMD_COMPANY_CTRL);
1280 DoCommandP(0, 1 | ai_debug_company << 16, 0, CMD_COMPANY_CTRL);
1281 break;
1282
1283 case WID_AID_SETTINGS:
1284 ShowAISettingsWindow(ai_debug_company);
1285 break;
1286
1287 case WID_AID_BREAK_STR_ON_OFF_BTN:
1288 this->break_check_enabled = !this->break_check_enabled;
1289 this->InvalidateData(-1);
1290 break;
1291
1292 case WID_AID_MATCH_CASE_BTN:
1293 this->case_sensitive_break_check = !this->case_sensitive_break_check;
1294 this->InvalidateData(-1);
1295 break;
1296
1297 case WID_AID_CONTINUE_BTN:
1298 /* Unpause current AI / game script and mark the corresponding script button dirty. */
1299 if (!this->IsDead()) {
1300 if (ai_debug_company == OWNER_DEITY) {
1301 Game::Unpause();
1302 } else {
1303 AI::Unpause(ai_debug_company);
1304 }
1305 }
1306
1307 /* If the last AI/Game Script is unpaused, unpause the game too. */
1308 if ((_pause_mode & PM_PAUSED_NORMAL) == PM_PAUSED_NORMAL) {
1309 bool all_unpaused = !Game::IsPaused();
1310 if (all_unpaused) {
1311 Company *c;
1312 FOR_ALL_COMPANIES(c) {
1313 if (c->is_ai && AI::IsPaused(c->index)) {
1314 all_unpaused = false;
1315 break;
1316 }
1317 }
1318 if (all_unpaused) {
1319 /* All scripts have been unpaused => unpause the game. */
1320 DoCommandP(0, PM_PAUSED_NORMAL, 0, CMD_PAUSE);
1321 }
1322 }
1323 }
1324
1325 this->highlight_row = -1;
1326 this->InvalidateData(-1);
1327 break;
1328 }
1329 }
1330
1331 virtual void OnEditboxChanged(int wid)
1332 {
1333 if (wid == WID_AID_BREAK_STR_EDIT_BOX) {
1334 /* Save the current string to static member so it can be restored next time the window is opened. */
1335 strecpy(this->break_string, this->break_editbox.text.buf, lastof(this->break_string));
1336 break_string_filter.SetFilterTerm(this->break_string);
1337 }
1338 }
1339
1346 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
1347 {
1348 /* If the log message is related to the active company tab, check the break string.
1349 * This needs to be done in gameloop-scope, so the AI is suspended immediately. */
1350 if (!gui_scope && data == ai_debug_company && this->IsValidDebugCompany(ai_debug_company) && this->break_check_enabled && !this->break_string_filter.IsEmpty()) {
1351 /* Get the log instance of the active company */
1352 ScriptLog::LogData *log = this->GetLogPointer();
1353
1354 if (log != NULL) {
1355 this->break_string_filter.ResetState();
1356 this->break_string_filter.AddLine(log->lines[log->pos]);
1357 if (this->break_string_filter.GetState()) {
1358 /* Pause execution of script. */
1359 if (!this->IsDead()) {
1360 if (ai_debug_company == OWNER_DEITY) {
1361 Game::Pause();
1362 } else {
1363 AI::Pause(ai_debug_company);
1364 }
1365 }
1366
1367 /* Pause the game. */
1368 if ((_pause_mode & PM_PAUSED_NORMAL) == PM_UNPAUSED) {
1369 DoCommandP(0, PM_PAUSED_NORMAL, 1, CMD_PAUSE);
1370 }
1371
1372 /* Highlight row that matched */
1373 this->highlight_row = log->pos;
1374 }
1375 }
1376 }
1377
1378 if (!gui_scope) return;
1379
1380 this->SelectValidDebugCompany();
1381
1382 ScriptLog::LogData *log = ai_debug_company != INVALID_COMPANY ? this->GetLogPointer() : NULL;
1383 this->vscroll->SetCount((log == NULL) ? 0 : log->used);
1384
1385 /* Update company buttons */
1386 for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
1387 this->SetWidgetDisabledState(i + WID_AID_COMPANY_BUTTON_START, !Company::IsValidAiID(i));
1388 this->SetWidgetLoweredState(i + WID_AID_COMPANY_BUTTON_START, ai_debug_company == i);
1389 }
1390
1391 this->SetWidgetDisabledState(WID_AID_SCRIPT_GAME, Game::GetGameInstance() == NULL);
1392 this->SetWidgetLoweredState(WID_AID_SCRIPT_GAME, ai_debug_company == OWNER_DEITY);
1393
1394 this->SetWidgetLoweredState(WID_AID_BREAK_STR_ON_OFF_BTN, this->break_check_enabled);
1395 this->SetWidgetLoweredState(WID_AID_MATCH_CASE_BTN, this->case_sensitive_break_check);
1396
1397 this->SetWidgetDisabledState(WID_AID_SETTINGS, ai_debug_company == INVALID_COMPANY);
1398 this->SetWidgetDisabledState(WID_AID_RELOAD_TOGGLE, ai_debug_company == INVALID_COMPANY || ai_debug_company == OWNER_DEITY);
1399 this->SetWidgetDisabledState(WID_AID_CONTINUE_BTN, ai_debug_company == INVALID_COMPANY ||
1400 (ai_debug_company == OWNER_DEITY ? !Game::IsPaused() : !AI::IsPaused(ai_debug_company)));
1401 }
1402
1403 virtual void OnResize()
1404 {
1405 this->vscroll->SetCapacityFromWidget(this, WID_AID_LOG_PANEL);
1406 }
1407
1408 static HotkeyList hotkeys;
1409 };
1410
1411 const int AIDebugWindow::top_offset = WD_FRAMERECT_TOP + 2;
1412 const int AIDebugWindow::bottom_offset = WD_FRAMERECT_BOTTOM;
1413 CompanyID AIDebugWindow::ai_debug_company = INVALID_COMPANY;
1414 char AIDebugWindow::break_string[MAX_BREAK_STR_STRING_LENGTH] = "";
1415 bool AIDebugWindow::break_check_enabled = true;
1416 bool AIDebugWindow::case_sensitive_break_check = false;
1417 StringFilter AIDebugWindow::break_string_filter(&AIDebugWindow::case_sensitive_break_check);
1418
1420 NWidgetBase *MakeCompanyButtonRowsAIDebug(int *biggest_index)
1421 {
1422 return MakeCompanyButtonRows(biggest_index, WID_AID_COMPANY_BUTTON_START, WID_AID_COMPANY_BUTTON_END, 8, STR_AI_DEBUG_SELECT_AI_TOOLTIP);
1423 }
1424
1430 static EventState AIDebugGlobalHotkeys(int hotkey)
1431 {
1432 if (_game_mode != GM_NORMAL) return ES_NOT_HANDLED;
1433 Window *w = ShowAIDebugWindow(INVALID_COMPANY);
1434 if (w == NULL) return ES_NOT_HANDLED;
1435 return w->OnHotkey(hotkey);
1436 }
1437
1438 static Hotkey aidebug_hotkeys[] = {
1439 Hotkey('1', "company_1", WID_AID_COMPANY_BUTTON_START),
1440 Hotkey('2', "company_2", WID_AID_COMPANY_BUTTON_START + 1),
1441 Hotkey('3', "company_3", WID_AID_COMPANY_BUTTON_START + 2),
1442 Hotkey('4', "company_4", WID_AID_COMPANY_BUTTON_START + 3),
1443 Hotkey('5', "company_5", WID_AID_COMPANY_BUTTON_START + 4),
1444 Hotkey('6', "company_6", WID_AID_COMPANY_BUTTON_START + 5),
1445 Hotkey('7', "company_7", WID_AID_COMPANY_BUTTON_START + 6),
1446 Hotkey('8', "company_8", WID_AID_COMPANY_BUTTON_START + 7),
1447 Hotkey('9', "company_9", WID_AID_COMPANY_BUTTON_START + 8),
1448 Hotkey((uint16)0, "company_10", WID_AID_COMPANY_BUTTON_START + 9),
1449 Hotkey((uint16)0, "company_11", WID_AID_COMPANY_BUTTON_START + 10),
1450 Hotkey((uint16)0, "company_12", WID_AID_COMPANY_BUTTON_START + 11),
1451 Hotkey((uint16)0, "company_13", WID_AID_COMPANY_BUTTON_START + 12),
1452 Hotkey((uint16)0, "company_14", WID_AID_COMPANY_BUTTON_START + 13),
1453 Hotkey((uint16)0, "company_15", WID_AID_COMPANY_BUTTON_START + 14),
1454 Hotkey('S', "settings", WID_AID_SETTINGS),
1455 Hotkey('0', "game_script", WID_AID_SCRIPT_GAME),
1456 Hotkey((uint16)0, "reload", WID_AID_RELOAD_TOGGLE),
1457 Hotkey('B', "break_toggle", WID_AID_BREAK_STR_ON_OFF_BTN),
1458 Hotkey('F', "break_string", WID_AID_BREAK_STR_EDIT_BOX),
1459 Hotkey('C', "match_case", WID_AID_MATCH_CASE_BTN),
1460 Hotkey(WKC_RETURN, "continue", WID_AID_CONTINUE_BTN),
1461 HOTKEY_LIST_END
1462 };
1463 HotkeyList AIDebugWindow::hotkeys("aidebug", aidebug_hotkeys, AIDebugGlobalHotkeys);
1464
1466 static const NWidgetPart _nested_ai_debug_widgets[] = {
1467 NWidget(NWID_HORIZONTAL),
1468 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
1469 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_AI_DEBUG, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1470 NWidget(WWT_SHADEBOX, COLOUR_GREY),
1471 NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
1472 NWidget(WWT_STICKYBOX, COLOUR_GREY),
1473 EndContainer(),
1474 NWidget(WWT_PANEL, COLOUR_GREY, WID_AID_VIEW),
1475 NWidgetFunction(MakeCompanyButtonRowsAIDebug), SetPadding(0, 2, 1, 2),
1476 EndContainer(),
1477 NWidget(NWID_HORIZONTAL),
1478 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),
1479 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_AID_NAME_TEXT), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_JUST_STRING, STR_AI_DEBUG_NAME_TOOLTIP),
1480 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_AID_SETTINGS), SetMinimalSize(100, 20), SetDataTip(STR_AI_DEBUG_SETTINGS, STR_AI_DEBUG_SETTINGS_TOOLTIP),
1481 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_AID_RELOAD_TOGGLE), SetMinimalSize(100, 20), SetDataTip(STR_AI_DEBUG_RELOAD, STR_AI_DEBUG_RELOAD_TOOLTIP),
1482 EndContainer(),
1483 NWidget(NWID_HORIZONTAL),
1484 NWidget(NWID_VERTICAL),
1485 /* Log panel */
1486 NWidget(WWT_PANEL, COLOUR_GREY, WID_AID_LOG_PANEL), SetMinimalSize(287, 180), SetResize(1, 1), SetScrollbar(WID_AID_SCROLLBAR),
1487 EndContainer(),
1488 /* Break string widgets */
1489 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_AID_BREAK_STRING_WIDGETS),
1490 NWidget(NWID_HORIZONTAL),
1491 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),
1492 NWidget(WWT_PANEL, COLOUR_GREY),
1493 NWidget(NWID_HORIZONTAL),
1494 NWidget(WWT_LABEL, COLOUR_GREY), SetPadding(2, 2, 2, 4), SetDataTip(STR_AI_DEBUG_BREAK_ON_LABEL, 0x0),
1495 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),
1496 EndContainer(),
1497 EndContainer(),
1498 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),
1499 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),
1500 EndContainer(),
1501 EndContainer(),
1502 EndContainer(),
1503 NWidget(NWID_VERTICAL),
1504 NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_AID_SCROLLBAR),
1505 NWidget(WWT_RESIZEBOX, COLOUR_GREY),
1506 EndContainer(),
1507 EndContainer(),
1508 };
1509
1511 static WindowDesc _ai_debug_desc(
1512 WDP_AUTO, "script_debug", 600, 450,
1513 WC_AI_DEBUG, WC_NONE,
1514 0,
1515 _nested_ai_debug_widgets, lengthof(_nested_ai_debug_widgets),
1516 &AIDebugWindow::hotkeys
1517 );
1518
1523 Window *ShowAIDebugWindow(CompanyID show_company)
1524 {
1525 if (!_networking || _network_server) {
1526 AIDebugWindow *w = (AIDebugWindow *)BringWindowToFrontById(WC_AI_DEBUG, 0);
1527 if (w == NULL) w = new AIDebugWindow(&_ai_debug_desc, 0);
1528 if (show_company != INVALID_COMPANY) w->ChangeToAI(show_company);
1529 return w;
1530 } else {
1531 ShowErrorMessage(STR_ERROR_AI_DEBUG_SERVER_ONLY, INVALID_STRING_ID, WL_INFO);
1532 }
1533
1534 return NULL;
1535 }
1536
1540 void InitializeAIGui()
1541 {
1542 AIDebugWindow::ai_debug_company = INVALID_COMPANY;
1543 }
1544
1546 void ShowAIDebugWindowIfAIError()
1547 {
1548 /* Network clients can't debug AIs. */
1549 if (_networking && !_network_server) return;
1550
1551 Company *c;
1552 FOR_ALL_COMPANIES(c) {
1553 if (c->is_ai && c->ai_instance->IsDead()) {
1554 ShowAIDebugWindow(c->index);
1555 break;
1556 }
1557 }
1558
1559 GameInstance *g = Game::GetGameInstance();
1560 if (g != NULL && g->IsDead()) {
1561 ShowAIDebugWindow(OWNER_DEITY);
1562 }
1563 }