src/ai/api/ai_sign.cpp
author rubidium
Sat, 14 Apr 2007 20:38:10 +0000
branchnoai
changeset 9594 5009a30f320a
parent 9561 33b2f192bb34
child 9599 949374e83b78
permissions -rw-r--r--
(svn r9627) [NoAI] -Fix: let the squirrel export script export all needed (and a few more) types of references to structs and classes.
/* $Id$ */

/** @file ai_sign.cpp handles all functions of the AISign class */

#include "ai_sign.hpp"
#include "table/strings.h"
#include "../../command.h"
#include "../../signs.h"
#include "../../strings.h"
#include "../../variables.h" /* For SetDParam */

SignID AISign::GetMaxSignID()
{
	return ::GetMaxSignIndex();
}

int32 AISign::GetSignCount()
{
	return ::GetNumSigns();
}

/* static */ bool AISign::IsValidSign(SignID sign_id)
{
	return ::IsValidSignID(sign_id);
}

char *AISign::GetText(SignID sign_id)
{
	if (!AISign::IsValidSign(sign_id)) return NULL;
	static const int len = 64;
	char *sign_name = MallocT<char>(len);

	::GetString(sign_name, ::GetSign(sign_id)->str, &sign_name[len - 1]);

	return sign_name;
}

TileIndex AISign::GetLocation(SignID sign_id)
{
	if (!AISign::IsValidSign(sign_id)) return INVALID_TILE;
	const Sign *sign = ::GetSign(sign_id);
	return ::TileVirtXY(sign->x, sign->y);
}

bool AISign::RemoveSign(SignID sign_id)
{
	_cmd_text = "";
	return this->DoCommand(0, sign_id, 0, CMD_RENAME_SIGN);
}

SignID AISign::BuildSign(TileIndex location, const char *text)
{
	if (!::IsValidTile(location)) return INVALID_SIGN;

	/* Reset the internal NewSignID in case we are in TestMode */
	AIObject::SetNewSignID(0);

	bool ret = this->DoCommand(location, 0, 0, CMD_PLACE_SIGN);
	if (!ret) return INVALID_SIGN;

	SignID new_sign_id = AIObject::GetNewSignID();
	_cmd_text = text;
	ret = this->DoCommand(0, new_sign_id, 0, CMD_RENAME_SIGN);
	if (!ret) {
		this->RemoveSign(new_sign_id);
		return INVALID_SIGN;
	}
	return new_sign_id;
}