src/ai/api/ai_station.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_station.cpp Implementation of AIStation. */

#include "ai_station.hpp"
#include "ai_cargo.hpp"
#include "ai_map.hpp"
#include "../../openttd.h"
#include "../../debug.h"
#include "../../station_map.h"
#include "../../variables.h"
#include "../../strings_func.h"
#include "../../core/alloc_func.hpp"
#include "../../player_func.h"
#include "../../settings_type.h"
#include "table/strings.h"

/* static */ bool AIStation::IsValidStation(StationID station_id)
{
	return ::IsValidStationID(station_id) && ::GetStation(station_id)->owner == _current_player;
}

/* static */ StationID AIStation::GetStationID(TileIndex tile)
{
	if (!::IsTileType(tile, MP_STATION)) return INVALID_STATION;
	return ::GetStationIndex(tile);
}

/* static */ char *AIStation::GetName(StationID station_id)
{
	if (!IsValidStation(station_id)) return NULL;

	static const int len = 64;
	char *station_name = MallocT<char>(len);

	::SetDParam(0, GetStation(station_id)->index);
	::GetString(station_name, STR_STATION, &station_name[len - 1]);
	return station_name;
}

/* static */ TileIndex AIStation::GetLocation(StationID station_id)
{
	if (!IsValidStation(station_id)) return INVALID_TILE;

	return ::GetStation(station_id)->xy;
}

/* static */ int32 AIStation::GetCargoWaiting(StationID station_id, CargoID cargo_id)
{
	if (!IsValidStation(station_id)) return -1;
	if (!AICargo::IsValidCargo(cargo_id)) return -1;

	return ::GetStation(station_id)->goods[cargo_id].cargo.Count();
}

/* static */ int32 AIStation::GetCargoRating(StationID station_id, CargoID cargo_id)
{
	if (!IsValidStation(station_id)) return -1;
	if (!AICargo::IsValidCargo(cargo_id)) return -1;

	return ::GetStation(station_id)->goods[cargo_id].rating * 101 >> 8;
}

/* static */ int32 AIStation::GetCoverageRadius(AIStation::StationType type)
{
	if (type == STATION_AIRPORT) {
		DEBUG(ai, 0, "GetCoverageRadius(): coverage radius of airports needs to be requested via AIAirport::GetAirportCoverageRadius(), as it requires AirportType");
		return -1;
	}
	if (!_patches.modified_catchment) return CA_UNMODIFIED;

	switch (type) {
		case STATION_TRAIN:      return CA_TRAIN;
		case STATION_TRUCK_STOP: return CA_TRUCK;
		case STATION_BUS_STOP:   return CA_BUS;
		case STATION_DOCK:       return CA_DOCK;
		default:                 return CA_NONE;
	}
}

/* static */ int32 AIStation::GetDistanceManhattanToTile(StationID station_id, TileIndex tile)
{
	return AIMap::DistanceManhattan(tile, GetLocation(station_id));
}

/* static */ int32 AIStation::GetDistanceSquareToTile(StationID station_id, TileIndex tile)
{
	return AIMap::DistanceSquare(tile, GetLocation(station_id));
}