OpenTTD
ai_gui.cpp
Go to the documentation of this file.
1 /* $Id: ai_gui.cpp 27581 2016-05-22 12:00:36Z frosch $ */
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 {
61  int selected;
65 
71  AIListWindow(WindowDesc *desc, CompanyID slot) : Window(desc),
72  slot(slot)
73  {
74  if (slot == OWNER_DEITY) {
76  } else {
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) {
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);
152  SetDParam(0, selected_info->GetVersion());
153  DrawString(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, y, STR_AI_LIST_VERSION);
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);
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  }
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  {
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 
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(),
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(),
251  EndContainer(),
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 
263  WDP_CENTER, "settings_script_list", 200, 234,
265  0,
266  _nested_ai_list_widgets, lengthof(_nested_ai_list_widgets)
267 );
268 
273 static void ShowAIListWindow(CompanyID slot)
274 {
276  new AIListWindow(&_ai_list_desc, slot);
277 }
278 
282 struct AISettingsWindow : public Window {
289  int timeout;
293  typedef std::vector<const ScriptConfigItem *> VisibleSettingsList;
294  VisibleSettingsList visible_settings;
295 
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  this->RebuildVisibleSettings();
310 
311  this->CreateNestedTree();
312  this->vscroll = this->GetScrollbar(WID_AIS_SCROLLBAR);
313  this->FinishInitNested(slot); // Initializes 'this->line_height' as side effect.
314 
315  this->SetWidgetDisabledState(WID_AIS_RESET, _game_mode != GM_MENU && Company::IsValidID(this->slot));
316 
317  this->vscroll->SetCount((int)this->visible_settings.size());
318  }
319 
320  virtual void SetStringParameters(int widget) const
321  {
322  switch (widget) {
323  case WID_AIS_CAPTION:
324  SetDParam(0, (this->slot == OWNER_DEITY) ? STR_AI_SETTINGS_CAPTION_GAMESCRIPT : STR_AI_SETTINGS_CAPTION_AI);
325  break;
326  }
327  }
328 
335  {
336  visible_settings.clear();
337 
338  ScriptConfigItemList::const_iterator it = this->ai_config->GetConfigList()->begin();
339  for (; it != this->ai_config->GetConfigList()->end(); it++) {
340  bool no_hide = (it->flags & SCRIPTCONFIG_DEVELOPER) == 0;
341  if (no_hide || _settings_client.gui.ai_developer_tools) {
342  visible_settings.push_back(&(*it));
343  }
344  }
345  }
346 
347  virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
348  {
349  if (widget == WID_AIS_BACKGROUND) {
351 
352  resize->width = 1;
353  resize->height = this->line_height;
354  size->height = 5 * this->line_height;
355  }
356  }
357 
358  virtual void DrawWidget(const Rect &r, int widget) const
359  {
360  if (widget != WID_AIS_BACKGROUND) return;
361 
362  ScriptConfig *config = this->ai_config;
363  VisibleSettingsList::const_iterator it = this->visible_settings.begin();
364  int i = 0;
365  for (; !this->vscroll->IsVisible(i); i++) it++;
366 
367  bool rtl = _current_text_dir == TD_RTL;
368  uint buttons_left = rtl ? r.right - SETTING_BUTTON_WIDTH - 3 : r.left + 4;
369  uint text_left = r.left + (rtl ? WD_FRAMERECT_LEFT : SETTING_BUTTON_WIDTH + 8);
370  uint text_right = r.right - (rtl ? SETTING_BUTTON_WIDTH + 8 : WD_FRAMERECT_RIGHT);
371 
372 
373  int y = r.top;
374  int button_y_offset = (this->line_height - SETTING_BUTTON_HEIGHT) / 2;
375  int text_y_offset = (this->line_height - FONT_HEIGHT_NORMAL) / 2;
376  for (; this->vscroll->IsVisible(i) && it != visible_settings.end(); i++, it++) {
377  const ScriptConfigItem &config_item = **it;
378  int current_value = config->GetSetting((config_item).name);
379  bool editable = _game_mode == GM_MENU || ((this->slot != OWNER_DEITY) && !Company::IsValidID(this->slot)) || (config_item.flags & SCRIPTCONFIG_INGAME) != 0;
380 
381  StringID str;
382  TextColour colour;
383  uint idx = 0;
384  if (StrEmpty(config_item.description)) {
385  if (!strcmp(config_item.name, "start_date")) {
386  /* Build-in translation */
387  str = STR_AI_SETTINGS_START_DELAY;
388  colour = TC_LIGHT_BLUE;
389  } else {
390  str = STR_JUST_STRING;
391  colour = TC_ORANGE;
392  }
393  } else {
394  str = STR_AI_SETTINGS_SETTING;
395  colour = TC_LIGHT_BLUE;
396  SetDParamStr(idx++, config_item.description);
397  }
398 
399  if ((config_item.flags & SCRIPTCONFIG_BOOLEAN) != 0) {
400  DrawBoolButton(buttons_left, y + button_y_offset, current_value != 0, editable);
401  SetDParam(idx++, current_value == 0 ? STR_CONFIG_SETTING_OFF : STR_CONFIG_SETTING_ON);
402  } else {
403  if (config_item.complete_labels) {
404  DrawDropDownButton(buttons_left, y + button_y_offset, COLOUR_YELLOW, this->clicked_row == i && clicked_dropdown, editable);
405  } else {
406  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);
407  }
408  if (config_item.labels != NULL && config_item.labels->Contains(current_value)) {
409  SetDParam(idx++, STR_JUST_RAW_STRING);
410  SetDParamStr(idx++, config_item.labels->Find(current_value)->second);
411  } else {
412  SetDParam(idx++, STR_JUST_INT);
413  SetDParam(idx++, current_value);
414  }
415  }
416 
417  DrawString(text_left, text_right, y + text_y_offset, str, colour);
418  y += this->line_height;
419  }
420  }
421 
422  virtual void OnPaint()
423  {
424  if (this->closing_dropdown) {
425  this->closing_dropdown = false;
426  this->clicked_dropdown = false;
427  }
428  this->DrawWidgets();
429  }
430 
431  virtual void OnClick(Point pt, int widget, int click_count)
432  {
433  switch (widget) {
434  case WID_AIS_BACKGROUND: {
435  const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_AIS_BACKGROUND);
436  int num = (pt.y - wid->pos_y) / this->line_height + this->vscroll->GetPosition();
437  if (num >= (int)this->visible_settings.size()) break;
438 
439  VisibleSettingsList::const_iterator it = this->visible_settings.begin();
440  for (int i = 0; i < num; i++) it++;
441  const ScriptConfigItem config_item = **it;
442  if (_game_mode == GM_NORMAL && ((this->slot == OWNER_DEITY) || Company::IsValidID(this->slot)) && (config_item.flags & SCRIPTCONFIG_INGAME) == 0) return;
443 
444  if (this->clicked_row != num) {
446  HideDropDownMenu(this);
447  this->clicked_row = num;
448  this->clicked_dropdown = false;
449  }
450 
451  bool bool_item = (config_item.flags & SCRIPTCONFIG_BOOLEAN) != 0;
452 
453  int x = pt.x - wid->pos_x;
454  if (_current_text_dir == TD_RTL) x = wid->current_x - 1 - x;
455  x -= 4;
456 
457  /* One of the arrows is clicked (or green/red rect in case of bool value) */
458  int old_val = this->ai_config->GetSetting(config_item.name);
459  if (!bool_item && IsInsideMM(x, 0, SETTING_BUTTON_WIDTH) && config_item.complete_labels) {
460  if (this->clicked_dropdown) {
461  /* unclick the dropdown */
462  HideDropDownMenu(this);
463  this->clicked_dropdown = false;
464  this->closing_dropdown = false;
465  } else {
466  const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_AIS_BACKGROUND);
467  int rel_y = (pt.y - (int)wid->pos_y) % this->line_height;
468 
469  Rect wi_rect;
470  wi_rect.left = pt.x - (_current_text_dir == TD_RTL ? SETTING_BUTTON_WIDTH - 1 - x : x);
471  wi_rect.right = wi_rect.left + SETTING_BUTTON_WIDTH - 1;
472  wi_rect.top = pt.y - rel_y + (this->line_height - SETTING_BUTTON_HEIGHT) / 2;
473  wi_rect.bottom = wi_rect.top + SETTING_BUTTON_HEIGHT - 1;
474 
475  /* For dropdowns we also have to check the y position thoroughly, the mouse may not above the just opening dropdown */
476  if (pt.y >= wi_rect.top && pt.y <= wi_rect.bottom) {
477  this->clicked_dropdown = true;
478  this->closing_dropdown = false;
479 
480  DropDownList *list = new DropDownList();
481  for (int i = config_item.min_value; i <= config_item.max_value; i++) {
482  *list->Append() = new DropDownListCharStringItem(config_item.labels->Find(i)->second, i, false);
483  }
484 
485  ShowDropDownListAt(this, list, old_val, -1, wi_rect, COLOUR_ORANGE, true);
486  }
487  }
488  } else if (IsInsideMM(x, 0, SETTING_BUTTON_WIDTH)) {
489  int new_val = old_val;
490  if (bool_item) {
491  new_val = !new_val;
492  } else if (x >= SETTING_BUTTON_WIDTH / 2) {
493  /* Increase button clicked */
494  new_val += config_item.step_size;
495  if (new_val > config_item.max_value) new_val = config_item.max_value;
496  this->clicked_increase = true;
497  } else {
498  /* Decrease button clicked */
499  new_val -= config_item.step_size;
500  if (new_val < config_item.min_value) new_val = config_item.min_value;
501  this->clicked_increase = false;
502  }
503 
504  if (new_val != old_val) {
505  this->ai_config->SetSetting(config_item.name, new_val);
506  this->clicked_button = num;
507  this->timeout = 5;
508  }
509  } else if (!bool_item && !config_item.complete_labels) {
510  /* Display a query box so users can enter a custom value. */
511  SetDParam(0, old_val);
512  ShowQueryString(STR_JUST_INT, STR_CONFIG_SETTING_QUERY_CAPTION, 10, this, CS_NUMERAL, QSF_NONE);
513  }
514  this->SetDirty();
515  break;
516  }
517 
518  case WID_AIS_ACCEPT:
519  delete this;
520  break;
521 
522  case WID_AIS_RESET:
523  if (_game_mode == GM_MENU || !Company::IsValidID(this->slot)) {
524  this->ai_config->ResetSettings();
525  this->SetDirty();
526  }
527  break;
528  }
529  }
530 
531  virtual void OnQueryTextFinished(char *str)
532  {
533  if (StrEmpty(str)) return;
534  ScriptConfigItemList::const_iterator it = this->ai_config->GetConfigList()->begin();
535  for (int i = 0; i < this->clicked_row; i++) it++;
536  if (_game_mode == GM_NORMAL && ((this->slot == OWNER_DEITY) || Company::IsValidID(this->slot)) && (it->flags & SCRIPTCONFIG_INGAME) == 0) return;
537  int32 value = atoi(str);
538  this->ai_config->SetSetting((*it).name, value);
539  this->SetDirty();
540  }
541 
542  virtual void OnDropdownSelect(int widget, int index)
543  {
544  assert(this->clicked_dropdown);
545  ScriptConfigItemList::const_iterator it = this->ai_config->GetConfigList()->begin();
546  for (int i = 0; i < this->clicked_row; i++) it++;
547  if (_game_mode == GM_NORMAL && ((this->slot == OWNER_DEITY) || Company::IsValidID(this->slot)) && (it->flags & SCRIPTCONFIG_INGAME) == 0) return;
548  this->ai_config->SetSetting((*it).name, index);
549  this->SetDirty();
550  }
551 
552  virtual void OnDropdownClose(Point pt, int widget, int index, bool instant_close)
553  {
554  /* We cannot raise the dropdown button just yet. OnClick needs some hint, whether
555  * the same dropdown button was clicked again, and then not open the dropdown again.
556  * So, we only remember that it was closed, and process it on the next OnPaint, which is
557  * after OnClick. */
558  assert(this->clicked_dropdown);
559  this->closing_dropdown = true;
560  this->SetDirty();
561  }
562 
563  virtual void OnResize()
564  {
566  }
567 
568  virtual void OnTick()
569  {
570  if (--this->timeout == 0) {
571  this->clicked_button = -1;
572  this->SetDirty();
573  }
574  }
575 
581  virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
582  {
583  this->RebuildVisibleSettings();
584  }
585 };
586 
590  NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
591  NWidget(WWT_CAPTION, COLOUR_MAUVE, WID_AIS_CAPTION), SetDataTip(STR_AI_SETTINGS_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
592  NWidget(WWT_DEFSIZEBOX, COLOUR_MAUVE),
593  EndContainer(),
595  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),
596  NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, WID_AIS_SCROLLBAR),
597  EndContainer(),
600  NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, WID_AIS_ACCEPT), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_AI_SETTINGS_CLOSE, STR_NULL),
601  NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, WID_AIS_RESET), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_AI_SETTINGS_RESET, STR_NULL),
602  EndContainer(),
603  NWidget(WWT_RESIZEBOX, COLOUR_MAUVE),
604  EndContainer(),
605 };
606 
609  WDP_CENTER, "settings_script", 500, 208,
611  0,
612  _nested_ai_settings_widgets, lengthof(_nested_ai_settings_widgets)
613 );
614 
620 {
624 }
625 
626 
630 
631  ScriptTextfileWindow(TextfileType file_type, CompanyID slot) : TextfileWindow(file_type), slot(slot)
632  {
633  const char *textfile = GetConfig(slot)->GetTextfile(file_type, slot);
634  this->LoadTextfile(textfile, (slot == OWNER_DEITY) ? GAME_DIR : AI_DIR);
635  }
636 
637  /* virtual */ void SetStringParameters(int widget) const
638  {
639  if (widget == WID_TF_CAPTION) {
640  SetDParam(0, (slot == OWNER_DEITY) ? STR_CONTENT_TYPE_GAME_SCRIPT : STR_CONTENT_TYPE_AI);
641  SetDParamStr(1, GetConfig(slot)->GetName());
642  }
643  }
644 };
645 
652 {
654  new ScriptTextfileWindow(file_type, slot);
655 }
656 
657 
661  NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
662  NWidget(WWT_CAPTION, COLOUR_MAUVE), SetDataTip(STR_AI_CONFIG_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
663  EndContainer(),
664  NWidget(WWT_PANEL, COLOUR_MAUVE, WID_AIC_BACKGROUND),
665  NWidget(NWID_VERTICAL), SetPIP(4, 4, 4),
666  NWidget(NWID_HORIZONTAL), SetPIP(7, 0, 7),
667  NWidget(WWT_PUSHARROWBTN, COLOUR_YELLOW, WID_AIC_DECREASE), SetFill(0, 1), SetDataTip(AWV_DECREASE, STR_NULL),
668  NWidget(WWT_PUSHARROWBTN, COLOUR_YELLOW, WID_AIC_INCREASE), SetFill(0, 1), SetDataTip(AWV_INCREASE, STR_NULL),
670  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),
671  EndContainer(),
673  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),
674  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),
675  EndContainer(),
676  EndContainer(),
677  NWidget(WWT_FRAME, COLOUR_MAUVE), SetDataTip(STR_AI_CONFIG_AI, STR_NULL), SetPadding(0, 5, 0, 5),
679  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),
680  NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, WID_AIC_SCROLLBAR),
681  EndContainer(),
682  EndContainer(),
684  NWidget(WWT_FRAME, COLOUR_MAUVE), SetDataTip(STR_AI_CONFIG_GAMESCRIPT, STR_NULL), SetPadding(0, 5, 4, 5),
685  NWidget(WWT_MATRIX, COLOUR_MAUVE, WID_AIC_GAMELIST), SetMinimalSize(288, 14), SetFill(1, 0), SetMatrixDataTip(1, 1, STR_AI_CONFIG_GAMELIST_TOOLTIP),
686  EndContainer(),
688  NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_AIC_CHANGE), SetFill(1, 0), SetMinimalSize(93, 12), SetDataTip(STR_AI_CONFIG_CHANGE, STR_AI_CONFIG_CHANGE_TOOLTIP),
689  NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_AIC_CONFIGURE), SetFill(1, 0), SetMinimalSize(93, 12), SetDataTip(STR_AI_CONFIG_CONFIGURE, STR_AI_CONFIG_CONFIGURE_TOOLTIP),
690  NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_AIC_CLOSE), SetFill(1, 0), SetMinimalSize(93, 12), SetDataTip(STR_AI_SETTINGS_CLOSE, STR_NULL),
691  EndContainer(),
693  NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_AIC_TEXTFILE + TFT_README), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_TEXTFILE_VIEW_README, STR_NULL),
694  NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_AIC_TEXTFILE + TFT_CHANGELOG), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_TEXTFILE_VIEW_CHANGELOG, STR_NULL),
695  NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_AIC_TEXTFILE + TFT_LICENSE), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_TEXTFILE_VIEW_LICENCE, STR_NULL),
696  EndContainer(),
697  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),
698  EndContainer(),
699 };
700 
703  WDP_CENTER, "settings_script_config", 0, 0,
705  0,
706  _nested_ai_config_widgets, lengthof(_nested_ai_config_widgets)
707 );
708 
712 struct AIConfigWindow : public Window {
716 
718  {
719  this->InitNested(WN_GAME_OPTIONS_AI); // Initializes 'this->line_height' as a side effect.
720  this->vscroll = this->GetScrollbar(WID_AIC_SCROLLBAR);
722  NWidgetCore *nwi = this->GetWidget<NWidgetCore>(WID_AIC_LIST);
723  this->vscroll->SetCapacity(nwi->current_y / this->line_height);
725  this->OnInvalidateData(0);
726  }
727 
728  ~AIConfigWindow()
729  {
732  }
733 
734  virtual void SetStringParameters(int widget) const
735  {
736  switch (widget) {
737  case WID_AIC_NUMBER:
738  SetDParam(0, GetGameSettings().difficulty.max_no_competitors);
739  break;
740  case WID_AIC_CHANGE:
741  switch (selected_slot) {
742  case OWNER_DEITY:
743  SetDParam(0, STR_AI_CONFIG_CHANGE_GAMESCRIPT);
744  break;
745 
746  case INVALID_COMPANY:
747  SetDParam(0, STR_AI_CONFIG_CHANGE_NONE);
748  break;
749 
750  default:
751  SetDParam(0, STR_AI_CONFIG_CHANGE_AI);
752  break;
753  }
754  break;
755  }
756  }
757 
758  virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
759  {
760  switch (widget) {
761  case WID_AIC_GAMELIST:
763  size->height = 1 * this->line_height;
764  break;
765 
766  case WID_AIC_LIST:
768  size->height = 8 * this->line_height;
769  break;
770 
771  case WID_AIC_CHANGE: {
772  SetDParam(0, STR_AI_CONFIG_CHANGE_GAMESCRIPT);
773  Dimension dim = GetStringBoundingBox(STR_AI_CONFIG_CHANGE);
774 
775  SetDParam(0, STR_AI_CONFIG_CHANGE_NONE);
776  dim = maxdim(dim, GetStringBoundingBox(STR_AI_CONFIG_CHANGE));
777 
778  SetDParam(0, STR_AI_CONFIG_CHANGE_AI);
779  dim = maxdim(dim, GetStringBoundingBox(STR_AI_CONFIG_CHANGE));
780 
781  dim.width += padding.width;
782  dim.height += padding.height;
783  *size = maxdim(*size, dim);
784  break;
785  }
786  }
787  }
788 
794  static bool IsEditable(CompanyID slot)
795  {
796  if (slot == OWNER_DEITY) return _game_mode != GM_NORMAL || Game::GetInstance() != NULL;
797 
798  if (_game_mode != GM_NORMAL) {
799  return slot > 0 && slot <= GetGameSettings().difficulty.max_no_competitors;
800  }
801  if (Company::IsValidID(slot) || slot < 0) return false;
802 
804  for (CompanyID cid = COMPANY_FIRST; cid < (CompanyID)max_slot && cid < MAX_COMPANIES; cid++) {
805  if (Company::IsValidHumanID(cid)) max_slot++;
806  }
807  return slot < max_slot;
808  }
809 
810  virtual void DrawWidget(const Rect &r, int widget) const
811  {
812  switch (widget) {
813  case WID_AIC_GAMELIST: {
814  StringID text = STR_AI_CONFIG_NONE;
815 
816  if (GameConfig::GetConfig()->GetInfo() != NULL) {
817  SetDParamStr(0, GameConfig::GetConfig()->GetInfo()->GetName());
818  text = STR_JUST_RAW_STRING;
819  }
820 
821  DrawString(r.left + 10, r.right - 10, r.top + WD_MATRIX_TOP, text,
822  (this->selected_slot == OWNER_DEITY) ? TC_WHITE : (IsEditable(OWNER_DEITY) ? TC_ORANGE : TC_SILVER));
823 
824  break;
825  }
826 
827  case WID_AIC_LIST: {
828  int y = r.top;
829  for (int i = this->vscroll->GetPosition(); this->vscroll->IsVisible(i) && i < MAX_COMPANIES; i++) {
830  StringID text;
831 
832  if ((_game_mode != GM_NORMAL && i == 0) || (_game_mode == GM_NORMAL && Company::IsValidHumanID(i))) {
833  text = STR_AI_CONFIG_HUMAN_PLAYER;
834  } else if (AIConfig::GetConfig((CompanyID)i)->GetInfo() != NULL) {
835  SetDParamStr(0, AIConfig::GetConfig((CompanyID)i)->GetInfo()->GetName());
836  text = STR_JUST_RAW_STRING;
837  } else {
838  text = STR_AI_CONFIG_RANDOM_AI;
839  }
840  DrawString(r.left + 10, r.right - 10, y + WD_MATRIX_TOP, text,
841  (this->selected_slot == i) ? TC_WHITE : (IsEditable((CompanyID)i) ? TC_ORANGE : TC_SILVER));
842  y += this->line_height;
843  }
844  break;
845  }
846  }
847  }
848 
849  virtual void OnClick(Point pt, int widget, int click_count)
850  {
851  if (widget >= WID_AIC_TEXTFILE && widget < WID_AIC_TEXTFILE + TFT_END) {
852  if (this->selected_slot == INVALID_COMPANY || GetConfig(this->selected_slot) == NULL) return;
853 
855  return;
856  }
857 
858  switch (widget) {
859  case WID_AIC_DECREASE:
860  case WID_AIC_INCREASE: {
861  int new_value;
862  if (widget == WID_AIC_DECREASE) {
863  new_value = max(0, GetGameSettings().difficulty.max_no_competitors - 1);
864  } else {
865  new_value = min(MAX_COMPANIES - 1, GetGameSettings().difficulty.max_no_competitors + 1);
866  }
867  IConsoleSetSetting("difficulty.max_no_competitors", new_value);
868  this->InvalidateData();
869  break;
870  }
871 
872  case WID_AIC_GAMELIST: {
873  this->selected_slot = OWNER_DEITY;
874  this->InvalidateData();
875  if (click_count > 1 && this->selected_slot != INVALID_COMPANY && _game_mode != GM_NORMAL) ShowAIListWindow((CompanyID)this->selected_slot);
876  break;
877  }
878 
879  case WID_AIC_LIST: { // Select a slot
880  this->selected_slot = (CompanyID)this->vscroll->GetScrolledRowFromWidget(pt.y, this, widget, 0, this->line_height);
881  this->InvalidateData();
882  if (click_count > 1 && this->selected_slot != INVALID_COMPANY) ShowAIListWindow((CompanyID)this->selected_slot);
883  break;
884  }
885 
886  case WID_AIC_MOVE_UP:
887  if (IsEditable(this->selected_slot) && IsEditable((CompanyID)(this->selected_slot - 1))) {
888  Swap(GetGameSettings().ai_config[this->selected_slot], GetGameSettings().ai_config[this->selected_slot - 1]);
889  this->selected_slot--;
890  this->vscroll->ScrollTowards(this->selected_slot);
891  this->InvalidateData();
892  }
893  break;
894 
895  case WID_AIC_MOVE_DOWN:
896  if (IsEditable(this->selected_slot) && IsEditable((CompanyID)(this->selected_slot + 1))) {
897  Swap(GetGameSettings().ai_config[this->selected_slot], GetGameSettings().ai_config[this->selected_slot + 1]);
898  this->selected_slot++;
899  this->vscroll->ScrollTowards(this->selected_slot);
900  this->InvalidateData();
901  }
902  break;
903 
904  case WID_AIC_CHANGE: // choose other AI
906  break;
907 
908  case WID_AIC_CONFIGURE: // change the settings for an AI
909  ShowAISettingsWindow((CompanyID)this->selected_slot);
910  break;
911 
912  case WID_AIC_CLOSE:
913  delete this;
914  break;
915 
917  if (!_network_available) {
918  ShowErrorMessage(STR_NETWORK_ERROR_NOTAVAILABLE, INVALID_STRING_ID, WL_ERROR);
919  } else {
920 #if defined(ENABLE_NETWORK)
922 #endif
923  }
924  break;
925  }
926  }
927 
933  virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
934  {
935  if (!IsEditable(this->selected_slot)) {
937  }
938 
939  if (!gui_scope) return;
940 
941  this->SetWidgetDisabledState(WID_AIC_DECREASE, GetGameSettings().difficulty.max_no_competitors == 0);
942  this->SetWidgetDisabledState(WID_AIC_INCREASE, GetGameSettings().difficulty.max_no_competitors == MAX_COMPANIES - 1);
943  this->SetWidgetDisabledState(WID_AIC_CHANGE, (this->selected_slot == OWNER_DEITY && _game_mode == GM_NORMAL) || this->selected_slot == INVALID_COMPANY);
944  this->SetWidgetDisabledState(WID_AIC_CONFIGURE, this->selected_slot == INVALID_COMPANY || GetConfig(this->selected_slot)->GetConfigList()->size() == 0);
947 
948  for (TextfileType tft = TFT_BEGIN; tft < TFT_END; tft++) {
949  this->SetWidgetDisabledState(WID_AIC_TEXTFILE + tft, this->selected_slot == INVALID_COMPANY || (GetConfig(this->selected_slot)->GetTextfile(tft, this->selected_slot) == NULL));
950  }
951  }
952 };
953 
956 {
958  new AIConfigWindow();
959 }
960 
969 static bool SetScriptButtonColour(NWidgetCore &button, bool dead, bool paused)
970 {
971  /* Dead scripts are indicated with red background and
972  * paused scripts are indicated with yellow background. */
973  Colours colour = dead ? COLOUR_RED :
974  (paused ? COLOUR_YELLOW : COLOUR_GREY);
975  if (button.colour != colour) {
976  button.colour = colour;
977  return true;
978  }
979  return false;
980 }
981 
985 struct AIDebugWindow : public Window {
986  static const int top_offset;
987  static const int bottom_offset;
988 
989  static const uint MAX_BREAK_STR_STRING_LENGTH = 256;
990 
994  bool autoscroll;
996  static bool break_check_enabled;
1003 
1004  ScriptLog::LogData *GetLogPointer() const
1005  {
1006  if (ai_debug_company == OWNER_DEITY) return (ScriptLog::LogData *)Game::GetInstance()->GetLogPointer();
1007  return (ScriptLog::LogData *)Company::Get(ai_debug_company)->ai_instance->GetLogPointer();
1008  }
1009 
1014  bool IsDead() const
1015  {
1016  if (ai_debug_company == OWNER_DEITY) {
1017  GameInstance *game = Game::GetInstance();
1018  return game == NULL || game->IsDead();
1019  }
1020  return !Company::IsValidAiID(ai_debug_company) || Company::Get(ai_debug_company)->ai_instance->IsDead();
1021  }
1022 
1028  bool IsValidDebugCompany(CompanyID company) const
1029  {
1030  switch (company) {
1031  case INVALID_COMPANY: return false;
1032  case OWNER_DEITY: return Game::GetInstance() != NULL;
1033  default: return Company::IsValidAiID(company);
1034  }
1035  }
1036 
1042  {
1043  /* Check if the currently selected company is still active. */
1044  if (this->IsValidDebugCompany(ai_debug_company)) return;
1045 
1047 
1048  const Company *c;
1049  FOR_ALL_COMPANIES(c) {
1050  if (c->is_ai) {
1051  ChangeToAI(c->index);
1052  return;
1053  }
1054  }
1055 
1056  /* If no AI is available, see if there is a game script. */
1057  if (Game::GetInstance() != NULL) ChangeToAI(OWNER_DEITY);
1058  }
1059 
1066  {
1067  this->CreateNestedTree();
1068  this->vscroll = this->GetScrollbar(WID_AID_SCROLLBAR);
1070  this->GetWidget<NWidgetStacked>(WID_AID_BREAK_STRING_WIDGETS)->SetDisplayedPlane(this->show_break_box ? 0 : SZSP_HORIZONTAL);
1071  this->FinishInitNested(number);
1072 
1073  if (!this->show_break_box) break_check_enabled = false;
1074 
1075  this->last_vscroll_pos = 0;
1076  this->autoscroll = true;
1077  this->highlight_row = -1;
1078 
1080 
1082 
1083  /* Restore the break string value from static variable */
1084  this->break_editbox.text.Assign(this->break_string);
1085 
1086  this->SelectValidDebugCompany();
1087  this->InvalidateData(-1);
1088  }
1089 
1090  virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
1091  {
1092  if (widget == WID_AID_LOG_PANEL) {
1093  resize->height = FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
1094  size->height = 14 * resize->height + this->top_offset + this->bottom_offset;
1095  }
1096  }
1097 
1098  virtual void OnPaint()
1099  {
1100  this->SelectValidDebugCompany();
1101 
1102  /* Draw standard stuff */
1103  this->DrawWidgets();
1104 
1105  if (this->IsShaded()) return; // Don't draw anything when the window is shaded.
1106 
1107  bool dirty = false;
1108 
1109  /* Paint the company icons */
1110  for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
1111  NWidgetCore *button = this->GetWidget<NWidgetCore>(i + WID_AID_COMPANY_BUTTON_START);
1112 
1113  bool valid = Company::IsValidAiID(i);
1114 
1115  /* Check whether the validity of the company changed */
1116  dirty |= (button->IsDisabled() == valid);
1117 
1118  /* Mark dead/paused AIs by setting the background colour. */
1119  bool dead = valid && Company::Get(i)->ai_instance->IsDead();
1120  bool paused = valid && Company::Get(i)->ai_instance->IsPaused();
1121  /* Re-paint if the button was updated.
1122  * (note that it is intentional that SetScriptButtonColour is always called) */
1123  dirty |= SetScriptButtonColour(*button, dead, paused);
1124 
1125  /* Draw company icon only for valid AI companies */
1126  if (!valid) continue;
1127 
1128  byte offset = (i == ai_debug_company) ? 1 : 0;
1129  DrawCompanyIcon(i, button->pos_x + button->current_x / 2 - 7 + offset, this->GetWidget<NWidgetBase>(WID_AID_COMPANY_BUTTON_START + i)->pos_y + 2 + offset);
1130  }
1131 
1132  /* Set button colour for Game Script. */
1133  GameInstance *game = Game::GetInstance();
1134  bool valid = game != NULL;
1135  bool dead = valid && game->IsDead();
1136  bool paused = valid && game->IsPaused();
1137 
1138  NWidgetCore *button = this->GetWidget<NWidgetCore>(WID_AID_SCRIPT_GAME);
1139  dirty |= (button->IsDisabled() == valid) || SetScriptButtonColour(*button, dead, paused);
1140 
1141  if (dirty) this->InvalidateData(-1);
1142 
1143  /* If there are no active companies, don't display anything else. */
1144  if (ai_debug_company == INVALID_COMPANY) return;
1145 
1146  ScriptLog::LogData *log = this->GetLogPointer();
1147 
1148  int scroll_count = (log == NULL) ? 0 : log->used;
1149  if (this->vscroll->GetCount() != scroll_count) {
1150  this->vscroll->SetCount(scroll_count);
1151 
1152  /* We need a repaint */
1154  }
1155 
1156  if (log == NULL) return;
1157 
1158  /* Detect when the user scrolls the window. Enable autoscroll when the
1159  * bottom-most line becomes visible. */
1160  if (this->last_vscroll_pos != this->vscroll->GetPosition()) {
1161  this->autoscroll = this->vscroll->GetPosition() >= log->used - this->vscroll->GetCapacity();
1162  }
1163  if (this->autoscroll) {
1164  int scroll_pos = max(0, log->used - this->vscroll->GetCapacity());
1165  if (scroll_pos != this->vscroll->GetPosition()) {
1166  this->vscroll->SetPosition(scroll_pos);
1167 
1168  /* We need a repaint */
1171  }
1172  }
1173  this->last_vscroll_pos = this->vscroll->GetPosition();
1174  }
1175 
1176  virtual void SetStringParameters(int widget) const
1177  {
1178  switch (widget) {
1179  case WID_AID_NAME_TEXT:
1180  if (ai_debug_company == OWNER_DEITY) {
1181  const GameInfo *info = Game::GetInfo();
1182  assert(info != NULL);
1183  SetDParam(0, STR_AI_DEBUG_NAME_AND_VERSION);
1184  SetDParamStr(1, info->GetName());
1185  SetDParam(2, info->GetVersion());
1187  SetDParam(0, STR_EMPTY);
1188  } else {
1189  const AIInfo *info = Company::Get(ai_debug_company)->ai_info;
1190  assert(info != NULL);
1191  SetDParam(0, STR_AI_DEBUG_NAME_AND_VERSION);
1192  SetDParamStr(1, info->GetName());
1193  SetDParam(2, info->GetVersion());
1194  }
1195  break;
1196  }
1197  }
1198 
1199  virtual void DrawWidget(const Rect &r, int widget) const
1200  {
1201  if (ai_debug_company == INVALID_COMPANY) return;
1202 
1203  switch (widget) {
1204  case WID_AID_LOG_PANEL: {
1205  ScriptLog::LogData *log = this->GetLogPointer();
1206  if (log == NULL) return;
1207 
1208  int y = this->top_offset;
1209  for (int i = this->vscroll->GetPosition(); this->vscroll->IsVisible(i) && i < log->used; i++) {
1210  int pos = (i + log->pos + 1 - log->used + log->count) % log->count;
1211  if (log->lines[pos] == NULL) break;
1212 
1213  TextColour colour;
1214  switch (log->type[pos]) {
1215  case ScriptLog::LOG_SQ_INFO: colour = TC_BLACK; break;
1216  case ScriptLog::LOG_SQ_ERROR: colour = TC_RED; break;
1217  case ScriptLog::LOG_INFO: colour = TC_BLACK; break;
1218  case ScriptLog::LOG_WARNING: colour = TC_YELLOW; break;
1219  case ScriptLog::LOG_ERROR: colour = TC_RED; break;
1220  default: colour = TC_BLACK; break;
1221  }
1222 
1223  /* Check if the current line should be highlighted */
1224  if (pos == this->highlight_row) {
1225  GfxFillRect(r.left + 1, r.top + y, r.right - 1, r.top + y + this->resize.step_height - WD_PAR_VSEP_NORMAL, PC_BLACK);
1226  if (colour == TC_BLACK) colour = TC_WHITE; // Make black text readable by inverting it to white.
1227  }
1228 
1229  DrawString(r.left + 7, r.right - 7, r.top + y, log->lines[pos], colour, SA_LEFT | SA_FORCE);
1230  y += this->resize.step_height;
1231  }
1232  break;
1233  }
1234  }
1235  }
1236 
1241  void ChangeToAI(CompanyID show_ai)
1242  {
1243  if (!this->IsValidDebugCompany(show_ai)) return;
1244 
1245  ai_debug_company = show_ai;
1246 
1247  this->highlight_row = -1; // The highlight of one AI make little sense for another AI.
1248 
1249  /* Close AI settings window to prevent confusion */
1251 
1252  this->InvalidateData(-1);
1253 
1254  this->autoscroll = true;
1255  this->last_vscroll_pos = this->vscroll->GetPosition();
1256  }
1257 
1258  virtual void OnClick(Point pt, int widget, int click_count)
1259  {
1260  /* Also called for hotkeys, so check for disabledness */
1261  if (this->IsWidgetDisabled(widget)) return;
1262 
1263  /* Check which button is clicked */
1266  }
1267 
1268  switch (widget) {
1269  case WID_AID_SCRIPT_GAME:
1271  break;
1272 
1273  case WID_AID_RELOAD_TOGGLE:
1274  if (ai_debug_company == OWNER_DEITY) break;
1275  /* First kill the company of the AI, then start a new one. This should start the current AI again */
1277  DoCommandP(0, 1 | ai_debug_company << 16, 0, CMD_COMPANY_CTRL);
1278  break;
1279 
1280  case WID_AID_SETTINGS:
1282  break;
1283 
1286  this->InvalidateData(-1);
1287  break;
1288 
1291  this->InvalidateData(-1);
1292  break;
1293 
1294  case WID_AID_CONTINUE_BTN:
1295  /* Unpause current AI / game script and mark the corresponding script button dirty. */
1296  if (!this->IsDead()) {
1297  if (ai_debug_company == OWNER_DEITY) {
1298  Game::Unpause();
1299  } else {
1301  }
1302  }
1303 
1304  /* If the last AI/Game Script is unpaused, unpause the game too. */
1305  if ((_pause_mode & PM_PAUSED_NORMAL) == PM_PAUSED_NORMAL) {
1306  bool all_unpaused = !Game::IsPaused();
1307  if (all_unpaused) {
1308  Company *c;
1309  FOR_ALL_COMPANIES(c) {
1310  if (c->is_ai && AI::IsPaused(c->index)) {
1311  all_unpaused = false;
1312  break;
1313  }
1314  }
1315  if (all_unpaused) {
1316  /* All scripts have been unpaused => unpause the game. */
1317  DoCommandP(0, PM_PAUSED_NORMAL, 0, CMD_PAUSE);
1318  }
1319  }
1320  }
1321 
1322  this->highlight_row = -1;
1323  this->InvalidateData(-1);
1324  break;
1325  }
1326  }
1327 
1328  virtual void OnEditboxChanged(int wid)
1329  {
1330  if (wid == WID_AID_BREAK_STR_EDIT_BOX) {
1331  /* Save the current string to static member so it can be restored next time the window is opened. */
1332  strecpy(this->break_string, this->break_editbox.text.buf, lastof(this->break_string));
1334  }
1335  }
1336 
1343  virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
1344  {
1345  /* If the log message is related to the active company tab, check the break string.
1346  * This needs to be done in gameloop-scope, so the AI is suspended immediately. */
1347  if (!gui_scope && data == ai_debug_company && this->IsValidDebugCompany(ai_debug_company) && this->break_check_enabled && !this->break_string_filter.IsEmpty()) {
1348  /* Get the log instance of the active company */
1349  ScriptLog::LogData *log = this->GetLogPointer();
1350 
1351  if (log != NULL) {
1353  this->break_string_filter.AddLine(log->lines[log->pos]);
1354  if (this->break_string_filter.GetState()) {
1355  /* Pause execution of script. */
1356  if (!this->IsDead()) {
1357  if (ai_debug_company == OWNER_DEITY) {
1358  Game::Pause();
1359  } else {
1361  }
1362  }
1363 
1364  /* Pause the game. */
1366  DoCommandP(0, PM_PAUSED_NORMAL, 1, CMD_PAUSE);
1367  }
1368 
1369  /* Highlight row that matched */
1370  this->highlight_row = log->pos;
1371  }
1372  }
1373  }
1374 
1375  if (!gui_scope) return;
1376 
1377  this->SelectValidDebugCompany();
1378 
1379  ScriptLog::LogData *log = ai_debug_company != INVALID_COMPANY ? this->GetLogPointer() : NULL;
1380  this->vscroll->SetCount((log == NULL) ? 0 : log->used);
1381 
1382  /* Update company buttons */
1383  for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
1386  }
1387 
1390 
1393 
1398  }
1399 
1400  virtual void OnResize()
1401  {
1403  }
1404 
1405  static HotkeyList hotkeys;
1406 };
1407 
1411 char AIDebugWindow::break_string[MAX_BREAK_STR_STRING_LENGTH] = "";
1414 StringFilter AIDebugWindow::break_string_filter(&AIDebugWindow::case_sensitive_break_check);
1415 
1418 {
1419  return MakeCompanyButtonRows(biggest_index, WID_AID_COMPANY_BUTTON_START, WID_AID_COMPANY_BUTTON_END, 8, STR_AI_DEBUG_SELECT_AI_TOOLTIP);
1420 }
1421 
1428 {
1429  if (_game_mode != GM_NORMAL) return ES_NOT_HANDLED;
1431  if (w == NULL) return ES_NOT_HANDLED;
1432  return w->OnHotkey(hotkey);
1433 }
1434 
1435 static Hotkey aidebug_hotkeys[] = {
1436  Hotkey('1', "company_1", WID_AID_COMPANY_BUTTON_START),
1437  Hotkey('2', "company_2", WID_AID_COMPANY_BUTTON_START + 1),
1438  Hotkey('3', "company_3", WID_AID_COMPANY_BUTTON_START + 2),
1439  Hotkey('4', "company_4", WID_AID_COMPANY_BUTTON_START + 3),
1440  Hotkey('5', "company_5", WID_AID_COMPANY_BUTTON_START + 4),
1441  Hotkey('6', "company_6", WID_AID_COMPANY_BUTTON_START + 5),
1442  Hotkey('7', "company_7", WID_AID_COMPANY_BUTTON_START + 6),
1443  Hotkey('8', "company_8", WID_AID_COMPANY_BUTTON_START + 7),
1444  Hotkey('9', "company_9", WID_AID_COMPANY_BUTTON_START + 8),
1445  Hotkey((uint16)0, "company_10", WID_AID_COMPANY_BUTTON_START + 9),
1446  Hotkey((uint16)0, "company_11", WID_AID_COMPANY_BUTTON_START + 10),
1447  Hotkey((uint16)0, "company_12", WID_AID_COMPANY_BUTTON_START + 11),
1448  Hotkey((uint16)0, "company_13", WID_AID_COMPANY_BUTTON_START + 12),
1449  Hotkey((uint16)0, "company_14", WID_AID_COMPANY_BUTTON_START + 13),
1450  Hotkey((uint16)0, "company_15", WID_AID_COMPANY_BUTTON_START + 14),
1451  Hotkey('S', "settings", WID_AID_SETTINGS),
1452  Hotkey('0', "game_script", WID_AID_SCRIPT_GAME),
1453  Hotkey((uint16)0, "reload", WID_AID_RELOAD_TOGGLE),
1454  Hotkey('B', "break_toggle", WID_AID_BREAK_STR_ON_OFF_BTN),
1455  Hotkey('F', "break_string", WID_AID_BREAK_STR_EDIT_BOX),
1456  Hotkey('C', "match_case", WID_AID_MATCH_CASE_BTN),
1457  Hotkey(WKC_RETURN, "continue", WID_AID_CONTINUE_BTN),
1458  HOTKEY_LIST_END
1459 };
1460 HotkeyList AIDebugWindow::hotkeys("aidebug", aidebug_hotkeys, AIDebugGlobalHotkeys);
1461 
1465  NWidget(WWT_CLOSEBOX, COLOUR_GREY),
1466  NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_AI_DEBUG, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1467  NWidget(WWT_SHADEBOX, COLOUR_GREY),
1468  NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
1469  NWidget(WWT_STICKYBOX, COLOUR_GREY),
1470  EndContainer(),
1471  NWidget(WWT_PANEL, COLOUR_GREY, WID_AID_VIEW),
1473  EndContainer(),
1475  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),
1476  NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_AID_NAME_TEXT), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_JUST_STRING, STR_AI_DEBUG_NAME_TOOLTIP),
1477  NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_AID_SETTINGS), SetMinimalSize(100, 20), SetDataTip(STR_AI_DEBUG_SETTINGS, STR_AI_DEBUG_SETTINGS_TOOLTIP),
1478  NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_AID_RELOAD_TOGGLE), SetMinimalSize(100, 20), SetDataTip(STR_AI_DEBUG_RELOAD, STR_AI_DEBUG_RELOAD_TOOLTIP),
1479  EndContainer(),
1482  /* Log panel */
1484  EndContainer(),
1485  /* Break string widgets */
1488  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),
1489  NWidget(WWT_PANEL, COLOUR_GREY),
1491  NWidget(WWT_LABEL, COLOUR_GREY), SetPadding(2, 2, 2, 4), SetDataTip(STR_AI_DEBUG_BREAK_ON_LABEL, 0x0),
1492  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),
1493  EndContainer(),
1494  EndContainer(),
1495  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),
1496  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),
1497  EndContainer(),
1498  EndContainer(),
1499  EndContainer(),
1501  NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_AID_SCROLLBAR),
1502  NWidget(WWT_RESIZEBOX, COLOUR_GREY),
1503  EndContainer(),
1504  EndContainer(),
1505 };
1506 
1508 static WindowDesc _ai_debug_desc(
1509  WDP_AUTO, "script_debug", 600, 450,
1511  0,
1512  _nested_ai_debug_widgets, lengthof(_nested_ai_debug_widgets),
1513  &AIDebugWindow::hotkeys
1514 );
1515 
1521 {
1522  if (!_networking || _network_server) {
1524  if (w == NULL) w = new AIDebugWindow(&_ai_debug_desc, 0);
1525  if (show_company != INVALID_COMPANY) w->ChangeToAI(show_company);
1526  return w;
1527  } else {
1528  ShowErrorMessage(STR_ERROR_AI_DEBUG_SERVER_ONLY, INVALID_STRING_ID, WL_INFO);
1529  }
1530 
1531  return NULL;
1532 }
1533 
1538 {
1539  AIDebugWindow::ai_debug_company = INVALID_COMPANY;
1540 }
1541 
1544 {
1545  /* Network clients can't debug AIs. */
1546  if (_networking && !_network_server) return;
1547 
1548  Company *c;
1549  FOR_ALL_COMPANIES(c) {
1550  if (c->is_ai && c->ai_instance->IsDead()) {
1552  break;
1553  }
1554  }
1555 
1557  if (g != NULL && g->IsDead()) {
1559  }
1560 }