src/ai/ai_gui.cpp
author glx
Mon, 19 May 2008 15:13:58 +0000
branchnoai
changeset 10645 8cbdb511a674
parent 10643 970417eef395
child 10650 30fc5395b1b8
permissions -rw-r--r--
(svn r13189) [NoAI] -Sync: with trunk r13055:13185.
/* $Id$ */

/** @file ai_gui.cpp */

#include "../stdafx.h"
#include "../openttd.h"
#include "../gui.h"
#include "../window_gui.h"
#include "../player_func.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 "../command_func.h"

#include "ai.h"
#include "api/ai_types.hpp"
#include "api/ai_controller.hpp"
#include "api/ai_object.hpp"
#include "api/ai_log.hpp"
#include "ai_info.hpp"

#include "table/strings.h"
#include "../table/sprites.h"

extern AIInfo *AI_GetPlayerInfo(PlayerID player);

struct AIDebugWindow : public Window {
	enum AIDebugWindowWidgets {
		AID_WIDGET_CLOSEBOX = 0,
		AID_WIDGET_CAPTION,
		AID_WIDGET_VIEW,
		AID_WIDGET_NAME_TEXT,
		AID_WIDGET_RELOAD_TOGGLE,
		AID_WIDGET_LOG_PANEL,
		AID_WIDGET_UNUSED_1,
		AID_WIDGET_UNUSED_2,
		AID_WIDGET_UNUSED_3,
		AID_WIDGET_UNUSED_4,
		AID_WIDGET_UNUSED_5,
		AID_WIDGET_UNUSED_6,
		AID_WIDGET_UNUSED_7,

		AID_WIDGET_COMPANY_BUTTON_START,
		AID_WIDGET_COMPANY_BUTTON_END = AID_WIDGET_COMPANY_BUTTON_START + 8,
	};

	static PlayerID ai_debug_player;
	int redraw_timer;

	AIDebugWindow(const WindowDesc *desc, WindowNumber number) : Window(desc, number)
	{
		/* Disable the players who are not active or not an AI */
		for (PlayerID i = PLAYER_FIRST; i < MAX_PLAYERS; i++) {
			this->SetWidgetDisabledState(i + AID_WIDGET_COMPANY_BUTTON_START, !GetPlayer(i)->is_active || !GetPlayer(i)->is_ai);
		}
		this->DisableWidget(AID_WIDGET_RELOAD_TOGGLE);

		this->redraw_timer = 5 * DAY_TICKS;

		if (ai_debug_player != INVALID_PLAYER) this->LowerWidget(ai_debug_player + AID_WIDGET_COMPANY_BUTTON_START);
		this->SetDirty();

		this->FindWindowPlacementAndResize(desc);
	}

	virtual void OnPaint()
	{
		/* Draw standard stuff */
		this->DrawWidgets();

		/* 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. */
				this->RaiseWidget(ai_debug_player + AID_WIDGET_COMPANY_BUTTON_START);
				this->DisableWidget(ai_debug_player + AID_WIDGET_COMPANY_BUTTON_START);
				this->SetDirty();

				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. */
					this->LowerWidget(i + AID_WIDGET_COMPANY_BUTTON_START);
					this->EnableWidget(AID_WIDGET_RELOAD_TOGGLE);
					this->SetDirty();

					ai_debug_player = i;
					break;
				}
			}
		}

		/* If there are no active players, don't display anything else. */
		if (ai_debug_player == INVALID_PLAYER) return;

		/* 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 (!this->IsWidgetDisabled(i + AID_WIDGET_COMPANY_BUTTON_START)) {
					/* Bah, player gone :( */
					this->DisableWidget(i + AID_WIDGET_COMPANY_BUTTON_START);
					this->DisableWidget(AID_WIDGET_RELOAD_TOGGLE);

					/* We need a repaint */
					this->SetDirty();
				}
				continue;
			}

			/* Check if we have the player marked as inactive */
			if (this->IsWidgetDisabled(i + AID_WIDGET_COMPANY_BUTTON_START)) {
				/* New AI! Yippie :p */
				this->EnableWidget(i + AID_WIDGET_COMPANY_BUTTON_START);
				this->EnableWidget(AID_WIDGET_RELOAD_TOGGLE);
				/* We need a repaint */
				this->SetDirty();
			}

			byte x = (i == ai_debug_player) ? 1 : 0;
			DrawPlayerIcon(i, i * 37 + 13 + x, 16 + x);
		}

		AIInfo *info = AI_GetPlayerInfo(ai_debug_player);
		if (info == NULL) {
			DoDrawString("(unknown)", 7, 34, TC_BLACK);
			this->DisableWidget(AID_WIDGET_RELOAD_TOGGLE);
		} else {
			DoDrawString(info->GetName(), 7, 34, TC_BLACK);
		}

		PlayerID old_cp = _current_player;
		_current_player = ai_debug_player;

		if (AIObject::GetLogPointer() == NULL) {
			_current_player = old_cp;
			return;
		}
		AILog::LogData *log = (AILog::LogData *)AIObject::GetLogPointer();

		for (int i = 0; i < log->count; i++) {
			uint pos = (log->count + log->pos - i) % log->count;
			if (log->type[pos] == 0) break;

			int y = 12 * i;
			if (y >= this->widget[AID_WIDGET_LOG_PANEL].bottom - this->widget[AID_WIDGET_LOG_PANEL].top - 12) break;

			uint colour;
			switch (log->type[pos]) {
				case AILog::LOG_SQ_INFO:  colour = TC_BLACK;  break;
				case AILog::LOG_SQ_ERROR: colour = TC_RED;    break;
				case AILog::LOG_INFO:     colour = TC_BLACK;  break;
				case AILog::LOG_WARNING:  colour = TC_YELLOW; break;
				case AILog::LOG_ERROR:    colour = TC_RED;    break;
				default:                  colour = TC_BLACK;  break;
			}

			DoDrawStringTruncated(log->lines[pos], 7, this->widget[AID_WIDGET_LOG_PANEL].top + 6 + y, colour, this->widget[AID_WIDGET_LOG_PANEL].right - this->widget[AID_WIDGET_LOG_PANEL].left - 14);
		}

		_current_player = old_cp;
	}

	virtual void OnClick(Point pt, int widget)
	{
		/* Check which button is clicked */
		if (IsInsideMM(widget, AID_WIDGET_COMPANY_BUTTON_START, AID_WIDGET_COMPANY_BUTTON_END)) {
			/* Is it no on disable? */
			if (!this->IsWidgetDisabled(widget)) {
				this->RaiseWidget(ai_debug_player + AID_WIDGET_COMPANY_BUTTON_START);
				ai_debug_player = (PlayerID)(widget - AID_WIDGET_COMPANY_BUTTON_START);
				this->LowerWidget(ai_debug_player + AID_WIDGET_COMPANY_BUTTON_START);
				this->SetDirty();
			}
		}
		if (widget == AID_WIDGET_RELOAD_TOGGLE && !this->IsWidgetDisabled(widget)) {
			/* Set the current AI as forced next AI */
			AIInfo *info = AI_GetPlayerInfo(ai_debug_player);
			AI_ForceAI(info->GetName());

			/* First kill the company of the AI, then start a new one. This should start the current AI again */
			DoCommandP(0, 2, ai_debug_player, NULL, CMD_PLAYER_CTRL);
			DoCommandP(0, 1, 0, NULL, CMD_PLAYER_CTRL);
		}
	}

	virtual void OnTick()
	{
		if (_pause_game != 0) return;

		/* Update the AI logs every 5 days */
		if (--this->redraw_timer == 0) {
			this->redraw_timer = 5 * DAY_TICKS;
			this->SetDirty();
		}
	}

	virtual void OnTimeout()
	{
		this->RaiseWidget(AID_WIDGET_RELOAD_TOGGLE);
		this->SetDirty();
	}
};

PlayerID AIDebugWindow::ai_debug_player = INVALID_PLAYER;

static const Widget _ai_debug_widgets[] = {
{   WWT_CLOSEBOX,   RESIZE_NONE,    14,     0,    10,     0,    13, STR_00C5,                   STR_018B_CLOSE_WINDOW},                 // AID_WIDGET_CLOSEBOX
{    WWT_CAPTION,   RESIZE_NONE,    14,    11,   298,     0,    13, STR_AI_DEBUG,               STR_018C_WINDOW_TITLE_DRAG_THIS},       // AID_WIDGET_CAPTION
{      WWT_PANEL,   RESIZE_NONE,    14,     0,   298,    14,    27, 0x0,                        STR_NULL},                              // AID_WIDGET_VIEW

{      WWT_PANEL,   RESIZE_NONE,    14,     0,   149,    28,    47, 0x0,                        STR_AI_DEBUG_NAME_TIP},                 // AID_WIDGET_NAME_TEXT
{ WWT_PUSHTXTBTN,   RESIZE_NONE,    14,   150,   298,    28,    47, STR_AI_DEBUG_RELOAD,        STR_AI_DEBUG_RELOAD_TIP},               // AID_WIDGET_RELOAD_TOGGLE
{      WWT_PANEL,   RESIZE_NONE,    14,     0,   298,    48,   227, 0x0,                        STR_NULL},                              // AID_WIDGET_LOG_PANEL
/* 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},                              // AID_WIDGET_UNUSED_1
{      WWT_EMPTY,   RESIZE_NONE,    14,     0,   298,   108,   127, 0x0,                        STR_NULL},                              // AID_WIDGET_UNUSED_2
{      WWT_EMPTY,   RESIZE_NONE,    14,     0,   298,   128,   147, 0x0,                        STR_NULL},                              // AID_WIDGET_UNUSED_3
{      WWT_EMPTY,   RESIZE_NONE,    14,     0,   298,   148,   167, 0x0,                        STR_NULL},                              // AID_WIDGET_UNUSED_4
{      WWT_EMPTY,   RESIZE_NONE,    14,     0,   298,   168,   187, 0x0,                        STR_NULL},                              // AID_WIDGET_UNUSED_5
{      WWT_EMPTY,   RESIZE_NONE,    14,     0,   298,   188,   207, 0x0,                        STR_NULL},                              // AID_WIDGET_UNUSED_6
{      WWT_EMPTY,   RESIZE_NONE,    14,     0,   298,   208,   227, 0x0,                        STR_NULL},                              // AID_WIDGET_UNUSED_7

{      WWT_PANEL,   RESIZE_NONE,    14,     2,    38,    14,    26, 0x0,                        STR_704F_CLICK_HERE_TO_TOGGLE_COMPANY}, // AID_WIDGET_COMPANY_BUTTON_START
{      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}, // AID_WIDGET_COMPANY_BUTTON_END
{   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
};

void ShowAIDebugWindow()
{
	AllocateWindowDescFront<AIDebugWindow>(&_ai_debug_desc, 0);
}