src/ai/api/ai_town.cpp
author truebrain
Thu, 24 Apr 2008 23:39:18 +0000
branchnoai
changeset 10339 ce6cd68d9eb8
parent 10194 c9fdeb7450da
child 10360 3234cb59de55
permissions -rw-r--r--
(svn r12880) [NoAI] -Add: introduces ai_types.hpp, which has all NNNId like VehicleID. This simplifies the include-mess, and avoids including tons of _type.h for just a single typedef.
-Note: this is perfectly safe; when a type changes, any sane compiler starts complaining about redefining the typedef to an other type
/* $Id$ */

/** @file ai_town.cpp Implementation of AITown. */

#include "ai_town.hpp"
#include "ai_map.hpp"
#include "../../openttd.h"
#include "../../town.h"
#include "../../strings_func.h"
#include "../../core/alloc_func.hpp"
#include "table/strings.h"

/* static */ TownID AITown::GetMaxTownID()
{
	return ::GetMaxTownIndex();
}

/* static */ int32 AITown::GetTownCount()
{
	return ::GetNumTowns();
}

/* static */ bool AITown::IsValidTown(TownID town_id)
{
	return ::IsValidTownID(town_id);
}

/* static */ char *AITown::GetName(TownID town_id)
{
	if (!IsValidTown(town_id)) return NULL;
	static const int len = 64;
	char *town_name = MallocT<char>(len);

	::SetDParam(0, town_id);
	::GetString(town_name, STR_TOWN, &town_name[len - 1]);

	return town_name;
}

/* static */ int32 AITown::GetPopulation(TownID town_id)
{
	if (!IsValidTown(town_id)) return -1;
	const Town *t = ::GetTown(town_id);
	return t->population;
}

/* static */ TileIndex AITown::GetLocation(TownID town_id)
{
	if (!IsValidTown(town_id)) return INVALID_TILE;
	const Town *t = ::GetTown(town_id);
	return t->xy;
}

/* static */ int32 AITown::GetDistanceManhattanToTile(TownID town_id, TileIndex tile)
{
	return AIMap::DistanceManhattan(tile, GetLocation(town_id));
}

/* static */ int32 AITown::GetDistanceSquareToTile(TownID town_id, TileIndex tile)
{
	return AIMap::DistanceSquare(tile, GetLocation(town_id));
}