(svn r12511) [NoAI] -Add (WIP): AI Debug GUI. For now it is a stripped down copy of performance details. In future it will allow reloading of AI, changing settings, and reading the log of the AI noai
authortruebrain
Mon, 31 Mar 2008 11:31:44 +0000
branchnoai
changeset 9845 c359062b4db0
parent 9844 738b8f69675f
child 9846 6dcf51a0cf7c
(svn r12511) [NoAI] -Add (WIP): AI Debug GUI. For now it is a stripped down copy of performance details. In future it will allow reloading of AI, changing settings, and reading the log of the AI
projects/openttd_vs80.vcproj
projects/openttd_vs90.vcproj
source.list
src/ai/ai_gui.cpp
src/ai/ai_gui.hpp
src/lang/english.txt
src/main_gui.cpp
src/toolbar_gui.cpp
--- a/projects/openttd_vs80.vcproj	Mon Mar 31 10:55:13 2008 +0000
+++ b/projects/openttd_vs80.vcproj	Mon Mar 31 11:31:44 2008 +0000
@@ -2124,6 +2124,14 @@
 				>
 			</File>
 			<File
+				RelativePath=".\..\src\ai\ai_gui.cpp"
+				>
+			</File>
+			<File
+				RelativePath=".\..\src\ai\ai_gui.hpp"
+				>
+			</File>
+			<File
 				RelativePath=".\..\src\ai\ai_squirrel.cpp"
 				>
 			</File>
--- a/projects/openttd_vs90.vcproj	Mon Mar 31 10:55:13 2008 +0000
+++ b/projects/openttd_vs90.vcproj	Mon Mar 31 11:31:44 2008 +0000
@@ -2121,6 +2121,14 @@
 				>
 			</File>
 			<File
+				RelativePath=".\..\src\ai\ai_gui.cpp"
+				>
+			</File>
+			<File
+				RelativePath=".\..\src\ai\ai_gui.hpp"
+				>
+			</File>
+			<File
 				RelativePath=".\..\src\ai\ai_squirrel.cpp"
 				>
 			</File>
--- a/source.list	Mon Mar 31 10:55:13 2008 +0000
+++ b/source.list	Mon Mar 31 11:31:44 2008 +0000
@@ -460,6 +460,8 @@
 ai/ai.h
 #if NO_THREADS
 ai/ai_factory.hpp
+ai/ai_gui.cpp
+ai/ai_gui.hpp
 ai/ai_squirrel.cpp
 ai/ai_squirrel.hpp
 ai/ai_threads.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ai/ai_gui.cpp	Mon Mar 31 11:31:44 2008 +0000
@@ -0,0 +1,193 @@
+/* $Id$ */
+
+/** @file ai_gui.cpp */
+
+#include "../stdafx.h"
+#include "../openttd.h"
+#include "../gui.h"
+#include "../window_gui.h"
+#include "../player_base.h"
+#include "../player_gui.h"
+#include "../economy_func.h"
+#include "../variables.h"
+#include "../cargotype.h"
+#include "../strings_func.h"
+#include "../core/alloc_func.hpp"
+#include "../window_func.h"
+#include "../date_func.h"
+#include "../gfx_func.h"
+#include "../debug.h"
+#include "ai_factory.hpp"
+
+#include "table/strings.h"
+#include "../table/sprites.h"
+
+extern AIFactoryBase *AI_GetPlayerFactory(PlayerID player);
+
+static void AIDebugWndProc(Window *w, WindowEvent *e)
+{
+	static PlayerID _ai_debug_player = INVALID_PLAYER;
+
+	switch (e->event) {
+		case WE_PAINT: {
+			byte x;
+
+			/* Draw standard stuff */
+			DrawWindowWidgets(w);
+
+			/* Check if the currently selected player is still active. */
+			if (_ai_debug_player == INVALID_PLAYER || !GetPlayer(_ai_debug_player)->is_active) {
+				if (_ai_debug_player != INVALID_PLAYER) {
+					/* Raise and disable the widget for the previous selection. */
+					w->RaiseWidget(_ai_debug_player + 13);
+					w->DisableWidget(_ai_debug_player + 13);
+					SetWindowDirty(w);
+
+					_ai_debug_player = INVALID_PLAYER;
+				}
+
+				for (PlayerID i = PLAYER_FIRST; i < MAX_PLAYERS; i++) {
+					if (GetPlayer(i)->is_active && GetPlayer(i)->is_ai) {
+						/* Lower the widget corresponding to this player. */
+						w->LowerWidget(i + 13);
+						w->EnableWidget(4);
+						SetWindowDirty(w);
+
+						_ai_debug_player = i;
+						break;
+					}
+				}
+			}
+
+			/* If there are no active players, don't display anything else. */
+			if (_ai_debug_player == INVALID_PLAYER) break;
+
+			/* Paint the player icons */
+			for (PlayerID i = PLAYER_FIRST; i < MAX_PLAYERS; i++) {
+				if (!GetPlayer(i)->is_active || !GetPlayer(i)->is_ai) {
+					/* Check if we have the player as an active player */
+					if (!w->IsWidgetDisabled(i + 13)) {
+						/* Bah, player gone :( */
+						w->DisableWidget(i + 13);
+						w->DisableWidget(4);
+
+						/* We need a repaint */
+						SetWindowDirty(w);
+					}
+					continue;
+				}
+
+				/* Check if we have the player marked as inactive */
+				if (w->IsWidgetDisabled(i + 13)) {
+					/* New AI! Yippie :p */
+					w->EnableWidget(i + 13);
+					w->EnableWidget(4);
+					/* We need a repaint */
+					SetWindowDirty(w);
+				}
+
+				x = (i == _ai_debug_player) ? 1 : 0;
+				DrawPlayerIcon(i, i * 37 + 13 + x, 16 + x);
+			}
+
+			AIFactoryBase *factory = AI_GetPlayerFactory(_ai_debug_player);
+			DoDrawString(factory->GetAIName(), 7, 34, TC_BLACK);
+
+			break;
+		}
+
+		case WE_CLICK:
+			/* Check which button is clicked */
+			if (IsInsideMM(e->we.click.widget, 13, 21)) {
+				/* Is it no on disable? */
+				if (!w->IsWidgetDisabled(e->we.click.widget)) {
+					w->RaiseWidget(_ai_debug_player + 13);
+					_ai_debug_player = (PlayerID)(e->we.click.widget - 13);
+					w->LowerWidget(_ai_debug_player + 13);
+					SetWindowDirty(w);
+				}
+			}
+			if (e->we.click.widget == 4 && !w->IsWidgetDisabled(e->we.click.widget)) {
+				/* Reload the AI */
+				DEBUG(ai, 0, "Reloading not yet supported");
+			}
+			break;
+
+		case WE_CREATE: {
+			/* Disable the players who are not active or not an AI */
+			for (PlayerID i = PLAYER_FIRST; i < MAX_PLAYERS; i++) {
+				w->SetWidgetDisabledState(i + 13, !GetPlayer(i)->is_active || !GetPlayer(i)->is_ai);
+			}
+			w->DisableWidget(4);
+
+			w->custom[0] = DAY_TICKS;
+			w->custom[1] = 5;
+
+			if (_ai_debug_player != INVALID_PLAYER) w->LowerWidget(_ai_debug_player + 13);
+			SetWindowDirty(w);
+
+			break;
+		}
+
+		case WE_TICK:
+			if (_pause_game != 0) break;
+
+			/* Update the AI logs every 5 days */
+			if (--w->custom[0] == 0) {
+				w->custom[0] = DAY_TICKS;
+				if (--w->custom[1] == 0) {
+					w->custom[1] = 5;
+
+					SetWindowDirty(w);
+				}
+			}
+
+			break;
+
+		case WE_TIMEOUT:
+			w->RaiseWidget(4);
+			SetWindowDirty(w);
+			break;
+	}
+}
+
+static const Widget _ai_debug_widgets[] = {
+{   WWT_CLOSEBOX,   RESIZE_NONE,    14,     0,    10,     0,    13, STR_00C5,                   STR_018B_CLOSE_WINDOW},
+{    WWT_CAPTION,   RESIZE_NONE,    14,    11,   298,     0,    13, STR_AI_DEBUG,               STR_018C_WINDOW_TITLE_DRAG_THIS},
+{      WWT_PANEL,   RESIZE_NONE,    14,     0,   298,    14,    27, 0x0,                        STR_NULL},
+
+{      WWT_PANEL,   RESIZE_NONE,    14,     0,   149,    28,    47, 0x0,                        STR_AI_DEBUG_NAME_TIP},
+{ WWT_PUSHTXTBTN,   RESIZE_NONE,    14,   150,   298,    28,    47, STR_AI_DEBUG_RELOAD,        STR_AI_DEBUG_RELOAD_TIP},
+{      WWT_PANEL,   RESIZE_NONE,    14,     0,   298,    48,   227, 0x0,                        STR_NULL},
+/* As this is WIP, leave the next few so we can work a bit with the GUI */
+{      WWT_EMPTY,   RESIZE_NONE,    14,     0,   298,    88,   107, 0x0,                        STR_NULL},
+{      WWT_EMPTY,   RESIZE_NONE,    14,     0,   298,   108,   127, 0x0,                        STR_NULL},
+{      WWT_EMPTY,   RESIZE_NONE,    14,     0,   298,   128,   147, 0x0,                        STR_NULL},
+{      WWT_EMPTY,   RESIZE_NONE,    14,     0,   298,   148,   167, 0x0,                        STR_NULL},
+{      WWT_EMPTY,   RESIZE_NONE,    14,     0,   298,   168,   187, 0x0,                        STR_NULL},
+{      WWT_EMPTY,   RESIZE_NONE,    14,     0,   298,   188,   207, 0x0,                        STR_NULL},
+{      WWT_EMPTY,   RESIZE_NONE,    14,     0,   298,   208,   227, 0x0,                        STR_NULL},
+
+{      WWT_PANEL,   RESIZE_NONE,    14,     2,    38,    14,    26, 0x0,                        STR_704F_CLICK_HERE_TO_TOGGLE_COMPANY},
+{      WWT_PANEL,   RESIZE_NONE,    14,    39,    75,    14,    26, 0x0,                        STR_704F_CLICK_HERE_TO_TOGGLE_COMPANY},
+{      WWT_PANEL,   RESIZE_NONE,    14,    76,   112,    14,    26, 0x0,                        STR_704F_CLICK_HERE_TO_TOGGLE_COMPANY},
+{      WWT_PANEL,   RESIZE_NONE,    14,   113,   149,    14,    26, 0x0,                        STR_704F_CLICK_HERE_TO_TOGGLE_COMPANY},
+{      WWT_PANEL,   RESIZE_NONE,    14,   150,   186,    14,    26, 0x0,                        STR_704F_CLICK_HERE_TO_TOGGLE_COMPANY},
+{      WWT_PANEL,   RESIZE_NONE,    14,   187,   223,    14,    26, 0x0,                        STR_704F_CLICK_HERE_TO_TOGGLE_COMPANY},
+{      WWT_PANEL,   RESIZE_NONE,    14,   224,   260,    14,    26, 0x0,                        STR_704F_CLICK_HERE_TO_TOGGLE_COMPANY},
+{      WWT_PANEL,   RESIZE_NONE,    14,   261,   297,    14,    26, 0x0,                        STR_704F_CLICK_HERE_TO_TOGGLE_COMPANY},
+{   WIDGETS_END},
+};
+
+static const WindowDesc _ai_debug_desc = {
+	WDP_AUTO, WDP_AUTO, 299, 228, 299, 228,
+	WC_PERFORMANCE_DETAIL, WC_NONE,
+	WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET,
+	_ai_debug_widgets,
+	AIDebugWndProc
+};
+
+void ShowAIDebugWindow()
+{
+	AllocateWindowDescFront(&_ai_debug_desc, 0);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ai/ai_gui.hpp	Mon Mar 31 11:31:44 2008 +0000
@@ -0,0 +1,6 @@
+#ifndef AI_GUI_HPP
+#define AI_GUI_HPP
+
+void ShowAIDebugWindow();
+
+#endif /* AI_GUI_HPP */
--- a/src/lang/english.txt	Mon Mar 31 10:55:13 2008 +0000
+++ b/src/lang/english.txt	Mon Mar 31 11:31:44 2008 +0000
@@ -790,6 +790,7 @@
 STR_02D5_LAND_BLOCK_INFO                                        :Land area information
 STR_02D6                                                        :
 STR_CONSOLE_SETTING                                             :Toggle Console
+STR_AI_DEBUG_MENU                                               :AI Debug
 STR_02D7_SCREENSHOT_CTRL_S                                      :Screenshot (Ctrl-S)
 STR_02D8_GIANT_SCREENSHOT_CTRL_G                                :Giant Screenshot (Ctrl-G)
 STR_02D9_ABOUT_OPENTTD                                          :About 'OpenTTD'
@@ -3505,3 +3506,10 @@
 STR_OSK_KEYBOARD_LAYOUT                                         :`1234567890-=\qwertyuiop[]asdfghjkl;'  zxcvbnm,./ .
 STR_OSK_KEYBOARD_LAYOUT_CAPS                                    :~!@#$%^&*()_+|QWERTYUIOP{{}}ASDFGHJKL:"  ZXCVBNM<>? .
 ########
+
+############ AI GUI
+STR_AI_DEBUG                                                    :{WHITE}AI Debug
+STR_AI_DEBUG_NAME_TIP                                           :{BLACK}Name of the AI
+STR_AI_DEBUG_RELOAD                                             :{BLACK}Reload AI
+STR_AI_DEBUG_RELOAD_TIP                                         :{BLACK}Kill the AI, reload the script, and restart the AI
+########
\ No newline at end of file
--- a/src/main_gui.cpp	Mon Mar 31 10:55:13 2008 +0000
+++ b/src/main_gui.cpp	Mon Mar 31 11:31:44 2008 +0000
@@ -45,6 +45,7 @@
 #include "player_gui.h"
 #include "settings_type.h"
 #include "toolbar_gui.h"
+#include "ai/ai_gui.hpp"
 
 #include "network/network.h"
 #include "network/network_data.h"
@@ -377,9 +378,10 @@
 	switch (index) {
 		case 0: PlaceLandBlockInfo();       break;
 		case 2: IConsoleSwitch();           break;
-		case 3: MenuClickSmallScreenshot(); break;
-		case 4: MenuClickWorldScreenshot(); break;
-		case 5: ShowAboutWindow();          break;
+		case 3: ShowAIDebugWindow();        break;
+		case 4: MenuClickSmallScreenshot(); break;
+		case 5: MenuClickWorldScreenshot(); break;
+		case 6: ShowAboutWindow();          break;
 	}
 }
 
--- a/src/toolbar_gui.cpp	Mon Mar 31 10:55:13 2008 +0000
+++ b/src/toolbar_gui.cpp	Mon Mar 31 11:31:44 2008 +0000
@@ -222,7 +222,7 @@
 
 static void ToolbarHelpClick(Window *w)
 {
-	PopupMainToolbMenu(w, 26, STR_02D5_LAND_BLOCK_INFO, 6, 0);
+	PopupMainToolbMenu(w, 26, STR_02D5_LAND_BLOCK_INFO, 7, 0);
 }
 
 static void ToolbarOptionsClick(Window *w)