src/ai/api/ai_station.cpp
author truelight
Sun, 19 Aug 2007 13:16:06 +0000
branchnoai
changeset 9696 4384ed3de1f0
parent 9670 820b77e19bb3
child 9723 eee46cb39750
permissions -rw-r--r--
(svn r10937) [NoAI] -Add: added AIStation::GetName on request by Nickman
[NoAI] -Fix: AICompant::GetCompanyName returned \0 for invalid company instead of NULL
9638
f6d169a5a45a (svn r10529) [NoAI] -Add: added AIStation to gather information about stations directly by Id, which is used now by AIStationList
truelight
parents:
diff changeset
     1
/* $Id$ */
f6d169a5a45a (svn r10529) [NoAI] -Add: added AIStation to gather information about stations directly by Id, which is used now by AIStationList
truelight
parents:
diff changeset
     2
f6d169a5a45a (svn r10529) [NoAI] -Add: added AIStation to gather information about stations directly by Id, which is used now by AIStationList
truelight
parents:
diff changeset
     3
/** @file ai_station.cpp handles the functions of the AIStation class */
f6d169a5a45a (svn r10529) [NoAI] -Add: added AIStation to gather information about stations directly by Id, which is used now by AIStationList
truelight
parents:
diff changeset
     4
f6d169a5a45a (svn r10529) [NoAI] -Add: added AIStation to gather information about stations directly by Id, which is used now by AIStationList
truelight
parents:
diff changeset
     5
#include "ai_station.hpp"
f6d169a5a45a (svn r10529) [NoAI] -Add: added AIStation to gather information about stations directly by Id, which is used now by AIStationList
truelight
parents:
diff changeset
     6
#include "ai_cargo.hpp"
9670
820b77e19bb3 (svn r10609) [NoAI] -Add: added GetAirportWidth / GetAirportHeight
truelight
parents: 9666
diff changeset
     7
#include "../../debug.h"
9638
f6d169a5a45a (svn r10529) [NoAI] -Add: added AIStation to gather information about stations directly by Id, which is used now by AIStationList
truelight
parents:
diff changeset
     8
#include "../../station.h"
9666
5765eb70736d (svn r10580) [NoAI] -Add: added GetStationID to get the StationID based on a tile
truelight
parents: 9648
diff changeset
     9
#include "../../station_map.h"
9670
820b77e19bb3 (svn r10609) [NoAI] -Add: added GetAirportWidth / GetAirportHeight
truelight
parents: 9666
diff changeset
    10
#include "../../variables.h"
9696
4384ed3de1f0 (svn r10937) [NoAI] -Add: added AIStation::GetName on request by Nickman
truelight
parents: 9670
diff changeset
    11
#include "../../strings.h"
4384ed3de1f0 (svn r10937) [NoAI] -Add: added AIStation::GetName on request by Nickman
truelight
parents: 9670
diff changeset
    12
#include "table/strings.h"
9638
f6d169a5a45a (svn r10529) [NoAI] -Add: added AIStation to gather information about stations directly by Id, which is used now by AIStationList
truelight
parents:
diff changeset
    13
f6d169a5a45a (svn r10529) [NoAI] -Add: added AIStation to gather information about stations directly by Id, which is used now by AIStationList
truelight
parents:
diff changeset
    14
/* static */ bool AIStation::IsValidStation(StationID station_id)
f6d169a5a45a (svn r10529) [NoAI] -Add: added AIStation to gather information about stations directly by Id, which is used now by AIStationList
truelight
parents:
diff changeset
    15
{
f6d169a5a45a (svn r10529) [NoAI] -Add: added AIStation to gather information about stations directly by Id, which is used now by AIStationList
truelight
parents:
diff changeset
    16
	return ::IsValidStationID(station_id) && ::GetStation(station_id)->owner == _current_player;
f6d169a5a45a (svn r10529) [NoAI] -Add: added AIStation to gather information about stations directly by Id, which is used now by AIStationList
truelight
parents:
diff changeset
    17
}
f6d169a5a45a (svn r10529) [NoAI] -Add: added AIStation to gather information about stations directly by Id, which is used now by AIStationList
truelight
parents:
diff changeset
    18
9666
5765eb70736d (svn r10580) [NoAI] -Add: added GetStationID to get the StationID based on a tile
truelight
parents: 9648
diff changeset
    19
/* static */ StationID AIStation::GetStationID(TileIndex tile)
5765eb70736d (svn r10580) [NoAI] -Add: added GetStationID to get the StationID based on a tile
truelight
parents: 9648
diff changeset
    20
{
5765eb70736d (svn r10580) [NoAI] -Add: added GetStationID to get the StationID based on a tile
truelight
parents: 9648
diff changeset
    21
	if (!::IsTileType(tile, MP_STATION)) return INVALID_STATION;
5765eb70736d (svn r10580) [NoAI] -Add: added GetStationID to get the StationID based on a tile
truelight
parents: 9648
diff changeset
    22
	return ::GetStationIndex(tile);
5765eb70736d (svn r10580) [NoAI] -Add: added GetStationID to get the StationID based on a tile
truelight
parents: 9648
diff changeset
    23
}
5765eb70736d (svn r10580) [NoAI] -Add: added GetStationID to get the StationID based on a tile
truelight
parents: 9648
diff changeset
    24
9696
4384ed3de1f0 (svn r10937) [NoAI] -Add: added AIStation::GetName on request by Nickman
truelight
parents: 9670
diff changeset
    25
/* static */ char *AIStation::GetName(StationID station_id)
4384ed3de1f0 (svn r10937) [NoAI] -Add: added AIStation::GetName on request by Nickman
truelight
parents: 9670
diff changeset
    26
{
4384ed3de1f0 (svn r10937) [NoAI] -Add: added AIStation::GetName on request by Nickman
truelight
parents: 9670
diff changeset
    27
	if (!AIStation::IsValidStation(station_id)) return NULL;
4384ed3de1f0 (svn r10937) [NoAI] -Add: added AIStation::GetName on request by Nickman
truelight
parents: 9670
diff changeset
    28
4384ed3de1f0 (svn r10937) [NoAI] -Add: added AIStation::GetName on request by Nickman
truelight
parents: 9670
diff changeset
    29
	static const int len = 64;
4384ed3de1f0 (svn r10937) [NoAI] -Add: added AIStation::GetName on request by Nickman
truelight
parents: 9670
diff changeset
    30
	char *station_name = MallocT<char>(len);
4384ed3de1f0 (svn r10937) [NoAI] -Add: added AIStation::GetName on request by Nickman
truelight
parents: 9670
diff changeset
    31
4384ed3de1f0 (svn r10937) [NoAI] -Add: added AIStation::GetName on request by Nickman
truelight
parents: 9670
diff changeset
    32
	SetDParam(0, GetStation(station_id)->index);
4384ed3de1f0 (svn r10937) [NoAI] -Add: added AIStation::GetName on request by Nickman
truelight
parents: 9670
diff changeset
    33
	GetString(station_name, STR_STATION, &station_name[len - 1]);
4384ed3de1f0 (svn r10937) [NoAI] -Add: added AIStation::GetName on request by Nickman
truelight
parents: 9670
diff changeset
    34
	return station_name;
4384ed3de1f0 (svn r10937) [NoAI] -Add: added AIStation::GetName on request by Nickman
truelight
parents: 9670
diff changeset
    35
}
4384ed3de1f0 (svn r10937) [NoAI] -Add: added AIStation::GetName on request by Nickman
truelight
parents: 9670
diff changeset
    36
9638
f6d169a5a45a (svn r10529) [NoAI] -Add: added AIStation to gather information about stations directly by Id, which is used now by AIStationList
truelight
parents:
diff changeset
    37
/* static */ TileIndex AIStation::GetLocation(StationID station_id)
f6d169a5a45a (svn r10529) [NoAI] -Add: added AIStation to gather information about stations directly by Id, which is used now by AIStationList
truelight
parents:
diff changeset
    38
{
f6d169a5a45a (svn r10529) [NoAI] -Add: added AIStation to gather information about stations directly by Id, which is used now by AIStationList
truelight
parents:
diff changeset
    39
	if (!AIStation::IsValidStation(station_id)) return INVALID_TILE;
f6d169a5a45a (svn r10529) [NoAI] -Add: added AIStation to gather information about stations directly by Id, which is used now by AIStationList
truelight
parents:
diff changeset
    40
f6d169a5a45a (svn r10529) [NoAI] -Add: added AIStation to gather information about stations directly by Id, which is used now by AIStationList
truelight
parents:
diff changeset
    41
	return ::GetStation(station_id)->xy;
f6d169a5a45a (svn r10529) [NoAI] -Add: added AIStation to gather information about stations directly by Id, which is used now by AIStationList
truelight
parents:
diff changeset
    42
}
f6d169a5a45a (svn r10529) [NoAI] -Add: added AIStation to gather information about stations directly by Id, which is used now by AIStationList
truelight
parents:
diff changeset
    43
f6d169a5a45a (svn r10529) [NoAI] -Add: added AIStation to gather information about stations directly by Id, which is used now by AIStationList
truelight
parents:
diff changeset
    44
/* static */ int32 AIStation::GetCargoWaiting(StationID station_id, CargoID cargo_id)
f6d169a5a45a (svn r10529) [NoAI] -Add: added AIStation to gather information about stations directly by Id, which is used now by AIStationList
truelight
parents:
diff changeset
    45
{
f6d169a5a45a (svn r10529) [NoAI] -Add: added AIStation to gather information about stations directly by Id, which is used now by AIStationList
truelight
parents:
diff changeset
    46
	if (!AIStation::IsValidStation(station_id)) return -1;
f6d169a5a45a (svn r10529) [NoAI] -Add: added AIStation to gather information about stations directly by Id, which is used now by AIStationList
truelight
parents:
diff changeset
    47
	if (!AICargo::IsValidCargo(cargo_id)) return -1;
f6d169a5a45a (svn r10529) [NoAI] -Add: added AIStation to gather information about stations directly by Id, which is used now by AIStationList
truelight
parents:
diff changeset
    48
f6d169a5a45a (svn r10529) [NoAI] -Add: added AIStation to gather information about stations directly by Id, which is used now by AIStationList
truelight
parents:
diff changeset
    49
	return ::GetStation(station_id)->goods[cargo_id].cargo.Count();
f6d169a5a45a (svn r10529) [NoAI] -Add: added AIStation to gather information about stations directly by Id, which is used now by AIStationList
truelight
parents:
diff changeset
    50
}
9648
760b7b504e37 (svn r10543) [NoAI] -Add: added AIStationListCargoRating as valuator for a station list
truelight
parents: 9638
diff changeset
    51
760b7b504e37 (svn r10543) [NoAI] -Add: added AIStationListCargoRating as valuator for a station list
truelight
parents: 9638
diff changeset
    52
/* static */ int32 AIStation::GetCargoRating(StationID station_id, CargoID cargo_id)
760b7b504e37 (svn r10543) [NoAI] -Add: added AIStationListCargoRating as valuator for a station list
truelight
parents: 9638
diff changeset
    53
{
760b7b504e37 (svn r10543) [NoAI] -Add: added AIStationListCargoRating as valuator for a station list
truelight
parents: 9638
diff changeset
    54
	if (!AIStation::IsValidStation(station_id)) return -1;
760b7b504e37 (svn r10543) [NoAI] -Add: added AIStationListCargoRating as valuator for a station list
truelight
parents: 9638
diff changeset
    55
	if (!AICargo::IsValidCargo(cargo_id)) return -1;
760b7b504e37 (svn r10543) [NoAI] -Add: added AIStationListCargoRating as valuator for a station list
truelight
parents: 9638
diff changeset
    56
760b7b504e37 (svn r10543) [NoAI] -Add: added AIStationListCargoRating as valuator for a station list
truelight
parents: 9638
diff changeset
    57
	return ::GetStation(station_id)->goods[cargo_id].rating * 101 >> 8;
760b7b504e37 (svn r10543) [NoAI] -Add: added AIStationListCargoRating as valuator for a station list
truelight
parents: 9638
diff changeset
    58
}
9670
820b77e19bb3 (svn r10609) [NoAI] -Add: added GetAirportWidth / GetAirportHeight
truelight
parents: 9666
diff changeset
    59
820b77e19bb3 (svn r10609) [NoAI] -Add: added GetAirportWidth / GetAirportHeight
truelight
parents: 9666
diff changeset
    60
/* static */ int32 AIStation::GetCoverageRadius(AIStation::StationType type)
820b77e19bb3 (svn r10609) [NoAI] -Add: added GetAirportWidth / GetAirportHeight
truelight
parents: 9666
diff changeset
    61
{
820b77e19bb3 (svn r10609) [NoAI] -Add: added GetAirportWidth / GetAirportHeight
truelight
parents: 9666
diff changeset
    62
	if (type == STATION_AIRPORT) {
820b77e19bb3 (svn r10609) [NoAI] -Add: added GetAirportWidth / GetAirportHeight
truelight
parents: 9666
diff changeset
    63
		DEBUG(ai, 0, "GetCoverageRadius(): coverage radius of airports needs to be requested via AIAirport::GetAirportCoverageRadius(), as it requires AirportType");
820b77e19bb3 (svn r10609) [NoAI] -Add: added GetAirportWidth / GetAirportHeight
truelight
parents: 9666
diff changeset
    64
		return 0;
820b77e19bb3 (svn r10609) [NoAI] -Add: added GetAirportWidth / GetAirportHeight
truelight
parents: 9666
diff changeset
    65
	}
820b77e19bb3 (svn r10609) [NoAI] -Add: added GetAirportWidth / GetAirportHeight
truelight
parents: 9666
diff changeset
    66
	if (!_patches.modified_catchment) return 3;
820b77e19bb3 (svn r10609) [NoAI] -Add: added GetAirportWidth / GetAirportHeight
truelight
parents: 9666
diff changeset
    67
820b77e19bb3 (svn r10609) [NoAI] -Add: added GetAirportWidth / GetAirportHeight
truelight
parents: 9666
diff changeset
    68
	switch (type) {
820b77e19bb3 (svn r10609) [NoAI] -Add: added GetAirportWidth / GetAirportHeight
truelight
parents: 9666
diff changeset
    69
		case STATION_TRAIN:      return CA_TRAIN;
820b77e19bb3 (svn r10609) [NoAI] -Add: added GetAirportWidth / GetAirportHeight
truelight
parents: 9666
diff changeset
    70
		case STATION_TRUCK_STOP: return CA_TRUCK;
820b77e19bb3 (svn r10609) [NoAI] -Add: added GetAirportWidth / GetAirportHeight
truelight
parents: 9666
diff changeset
    71
		case STATION_BUS_STOP:   return CA_BUS;
820b77e19bb3 (svn r10609) [NoAI] -Add: added GetAirportWidth / GetAirportHeight
truelight
parents: 9666
diff changeset
    72
		case STATION_DOCK:       return CA_DOCK;
820b77e19bb3 (svn r10609) [NoAI] -Add: added GetAirportWidth / GetAirportHeight
truelight
parents: 9666
diff changeset
    73
		default:                 return CA_NONE;
820b77e19bb3 (svn r10609) [NoAI] -Add: added GetAirportWidth / GetAirportHeight
truelight
parents: 9666
diff changeset
    74
	}
820b77e19bb3 (svn r10609) [NoAI] -Add: added GetAirportWidth / GetAirportHeight
truelight
parents: 9666
diff changeset
    75
}