src/ai/api/ai_town.cpp
author truebrain
Fri, 25 Apr 2008 15:51:12 +0000
branchnoai
changeset 10360 3234cb59de55
parent 10339 ce6cd68d9eb8
child 10361 4cdffd48480f
permissions -rw-r--r--
(svn r12901) [NoAI] -Add: added AITown.IsWithinTownRadius, AIStation.IsWithinTownRadius and AITile.GetOwner (Yexo)
9380
f4c7eb98b43d (svn r9169) [NoAI] -Add: some wrapper functions for town related queries.
rubidium
parents:
diff changeset
     1
/* $Id$ */
f4c7eb98b43d (svn r9169) [NoAI] -Add: some wrapper functions for town related queries.
rubidium
parents:
diff changeset
     2
9833
89a64246458f (svn r12496) [NoAI] -Documentation: give the .cpp files a nice uniform format too
truebrain
parents: 9814
diff changeset
     3
/** @file ai_town.cpp Implementation of AITown. */
9380
f4c7eb98b43d (svn r9169) [NoAI] -Add: some wrapper functions for town related queries.
rubidium
parents:
diff changeset
     4
9430
9e0a193b2bec (svn r9234) [NoAI] -Codechange: move away from the 'much' subdirectory approach for the API implementation.
rubidium
parents: 9427
diff changeset
     5
#include "ai_town.hpp"
9814
be51ea0adc29 (svn r12411) [NoAI] -Change [API CHANGE]: order of params of CargoIncome is changed
truebrain
parents: 9737
diff changeset
     6
#include "ai_map.hpp"
10339
ce6cd68d9eb8 (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.
truebrain
parents: 10194
diff changeset
     7
#include "../../openttd.h"
9430
9e0a193b2bec (svn r9234) [NoAI] -Codechange: move away from the 'much' subdirectory approach for the API implementation.
rubidium
parents: 9427
diff changeset
     8
#include "../../town.h"
9723
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9582
diff changeset
     9
#include "../../strings_func.h"
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9582
diff changeset
    10
#include "../../core/alloc_func.hpp"
9405
df6f3b4b0038 (svn r9202) [NoAI] -Add: some initial functions for cargo and industries.
rubidium
parents: 9380
diff changeset
    11
#include "table/strings.h"
9380
f4c7eb98b43d (svn r9169) [NoAI] -Add: some wrapper functions for town related queries.
rubidium
parents:
diff changeset
    12
9737
ee408edf3851 (svn r12216) [NoAI] -Codechange: made most functions 'static', which removes the need to create an instance to get, for example, engine information, and therefor heavily simplifying AI creation (Morloth)
truebrain
parents: 9736
diff changeset
    13
/* static */ TownID AITown::GetMaxTownID()
9380
f4c7eb98b43d (svn r9169) [NoAI] -Add: some wrapper functions for town related queries.
rubidium
parents:
diff changeset
    14
{
f4c7eb98b43d (svn r9169) [NoAI] -Add: some wrapper functions for town related queries.
rubidium
parents:
diff changeset
    15
	return ::GetMaxTownIndex();
f4c7eb98b43d (svn r9169) [NoAI] -Add: some wrapper functions for town related queries.
rubidium
parents:
diff changeset
    16
}
f4c7eb98b43d (svn r9169) [NoAI] -Add: some wrapper functions for town related queries.
rubidium
parents:
diff changeset
    17
9737
ee408edf3851 (svn r12216) [NoAI] -Codechange: made most functions 'static', which removes the need to create an instance to get, for example, engine information, and therefor heavily simplifying AI creation (Morloth)
truebrain
parents: 9736
diff changeset
    18
/* static */ int32 AITown::GetTownCount()
9380
f4c7eb98b43d (svn r9169) [NoAI] -Add: some wrapper functions for town related queries.
rubidium
parents:
diff changeset
    19
{
f4c7eb98b43d (svn r9169) [NoAI] -Add: some wrapper functions for town related queries.
rubidium
parents:
diff changeset
    20
	return ::GetNumTowns();
f4c7eb98b43d (svn r9169) [NoAI] -Add: some wrapper functions for town related queries.
rubidium
parents:
diff changeset
    21
}
f4c7eb98b43d (svn r9169) [NoAI] -Add: some wrapper functions for town related queries.
rubidium
parents:
diff changeset
    22
9497
f6678533ccba (svn r9369) [NoAI] -Codechange: make some IsValidXXX() function static, so they can be used by the other classes without the need for an instance.
rubidium
parents: 9459
diff changeset
    23
/* static */ bool AITown::IsValidTown(TownID town_id)
9380
f4c7eb98b43d (svn r9169) [NoAI] -Add: some wrapper functions for town related queries.
rubidium
parents:
diff changeset
    24
{
f4c7eb98b43d (svn r9169) [NoAI] -Add: some wrapper functions for town related queries.
rubidium
parents:
diff changeset
    25
	return ::IsValidTownID(town_id);
f4c7eb98b43d (svn r9169) [NoAI] -Add: some wrapper functions for town related queries.
rubidium
parents:
diff changeset
    26
}
f4c7eb98b43d (svn r9169) [NoAI] -Add: some wrapper functions for town related queries.
rubidium
parents:
diff changeset
    27
9737
ee408edf3851 (svn r12216) [NoAI] -Codechange: made most functions 'static', which removes the need to create an instance to get, for example, engine information, and therefor heavily simplifying AI creation (Morloth)
truebrain
parents: 9736
diff changeset
    28
/* static */ char *AITown::GetName(TownID town_id)
9380
f4c7eb98b43d (svn r9169) [NoAI] -Add: some wrapper functions for town related queries.
rubidium
parents:
diff changeset
    29
{
9736
183b38e0a480 (svn r12215) [NoAI] -Codechange: added '::' if a function comes from non-AI-API functions (was inconsistant till now)
truebrain
parents: 9723
diff changeset
    30
	if (!IsValidTown(town_id)) return NULL;
9380
f4c7eb98b43d (svn r9169) [NoAI] -Add: some wrapper functions for town related queries.
rubidium
parents:
diff changeset
    31
	static const int len = 64;
f4c7eb98b43d (svn r9169) [NoAI] -Add: some wrapper functions for town related queries.
rubidium
parents:
diff changeset
    32
	char *town_name = MallocT<char>(len);
f4c7eb98b43d (svn r9169) [NoAI] -Add: some wrapper functions for town related queries.
rubidium
parents:
diff changeset
    33
9736
183b38e0a480 (svn r12215) [NoAI] -Codechange: added '::' if a function comes from non-AI-API functions (was inconsistant till now)
truebrain
parents: 9723
diff changeset
    34
	::SetDParam(0, town_id);
183b38e0a480 (svn r12215) [NoAI] -Codechange: added '::' if a function comes from non-AI-API functions (was inconsistant till now)
truebrain
parents: 9723
diff changeset
    35
	::GetString(town_name, STR_TOWN, &town_name[len - 1]);
9380
f4c7eb98b43d (svn r9169) [NoAI] -Add: some wrapper functions for town related queries.
rubidium
parents:
diff changeset
    36
f4c7eb98b43d (svn r9169) [NoAI] -Add: some wrapper functions for town related queries.
rubidium
parents:
diff changeset
    37
	return town_name;
f4c7eb98b43d (svn r9169) [NoAI] -Add: some wrapper functions for town related queries.
rubidium
parents:
diff changeset
    38
}
f4c7eb98b43d (svn r9169) [NoAI] -Add: some wrapper functions for town related queries.
rubidium
parents:
diff changeset
    39
9737
ee408edf3851 (svn r12216) [NoAI] -Codechange: made most functions 'static', which removes the need to create an instance to get, for example, engine information, and therefor heavily simplifying AI creation (Morloth)
truebrain
parents: 9736
diff changeset
    40
/* static */ int32 AITown::GetPopulation(TownID town_id)
9380
f4c7eb98b43d (svn r9169) [NoAI] -Add: some wrapper functions for town related queries.
rubidium
parents:
diff changeset
    41
{
10194
c9fdeb7450da (svn r12727) [NoAI] -Fix (API CHANGE): return -1 to indicate invalidity, instead of 0 or -1, depending on the class
truebrain
parents: 9833
diff changeset
    42
	if (!IsValidTown(town_id)) return -1;
9380
f4c7eb98b43d (svn r9169) [NoAI] -Add: some wrapper functions for town related queries.
rubidium
parents:
diff changeset
    43
	const Town *t = ::GetTown(town_id);
f4c7eb98b43d (svn r9169) [NoAI] -Add: some wrapper functions for town related queries.
rubidium
parents:
diff changeset
    44
	return t->population;
f4c7eb98b43d (svn r9169) [NoAI] -Add: some wrapper functions for town related queries.
rubidium
parents:
diff changeset
    45
}
f4c7eb98b43d (svn r9169) [NoAI] -Add: some wrapper functions for town related queries.
rubidium
parents:
diff changeset
    46
9737
ee408edf3851 (svn r12216) [NoAI] -Codechange: made most functions 'static', which removes the need to create an instance to get, for example, engine information, and therefor heavily simplifying AI creation (Morloth)
truebrain
parents: 9736
diff changeset
    47
/* static */ TileIndex AITown::GetLocation(TownID town_id)
9380
f4c7eb98b43d (svn r9169) [NoAI] -Add: some wrapper functions for town related queries.
rubidium
parents:
diff changeset
    48
{
9736
183b38e0a480 (svn r12215) [NoAI] -Codechange: added '::' if a function comes from non-AI-API functions (was inconsistant till now)
truebrain
parents: 9723
diff changeset
    49
	if (!IsValidTown(town_id)) return INVALID_TILE;
9380
f4c7eb98b43d (svn r9169) [NoAI] -Add: some wrapper functions for town related queries.
rubidium
parents:
diff changeset
    50
	const Town *t = ::GetTown(town_id);
f4c7eb98b43d (svn r9169) [NoAI] -Add: some wrapper functions for town related queries.
rubidium
parents:
diff changeset
    51
	return t->xy;
f4c7eb98b43d (svn r9169) [NoAI] -Add: some wrapper functions for town related queries.
rubidium
parents:
diff changeset
    52
}
9814
be51ea0adc29 (svn r12411) [NoAI] -Change [API CHANGE]: order of params of CargoIncome is changed
truebrain
parents: 9737
diff changeset
    53
be51ea0adc29 (svn r12411) [NoAI] -Change [API CHANGE]: order of params of CargoIncome is changed
truebrain
parents: 9737
diff changeset
    54
/* static */ int32 AITown::GetDistanceManhattanToTile(TownID town_id, TileIndex tile)
be51ea0adc29 (svn r12411) [NoAI] -Change [API CHANGE]: order of params of CargoIncome is changed
truebrain
parents: 9737
diff changeset
    55
{
be51ea0adc29 (svn r12411) [NoAI] -Change [API CHANGE]: order of params of CargoIncome is changed
truebrain
parents: 9737
diff changeset
    56
	return AIMap::DistanceManhattan(tile, GetLocation(town_id));
be51ea0adc29 (svn r12411) [NoAI] -Change [API CHANGE]: order of params of CargoIncome is changed
truebrain
parents: 9737
diff changeset
    57
}
be51ea0adc29 (svn r12411) [NoAI] -Change [API CHANGE]: order of params of CargoIncome is changed
truebrain
parents: 9737
diff changeset
    58
be51ea0adc29 (svn r12411) [NoAI] -Change [API CHANGE]: order of params of CargoIncome is changed
truebrain
parents: 9737
diff changeset
    59
/* static */ int32 AITown::GetDistanceSquareToTile(TownID town_id, TileIndex tile)
be51ea0adc29 (svn r12411) [NoAI] -Change [API CHANGE]: order of params of CargoIncome is changed
truebrain
parents: 9737
diff changeset
    60
{
be51ea0adc29 (svn r12411) [NoAI] -Change [API CHANGE]: order of params of CargoIncome is changed
truebrain
parents: 9737
diff changeset
    61
	return AIMap::DistanceSquare(tile, GetLocation(town_id));
be51ea0adc29 (svn r12411) [NoAI] -Change [API CHANGE]: order of params of CargoIncome is changed
truebrain
parents: 9737
diff changeset
    62
}
10360
3234cb59de55 (svn r12901) [NoAI] -Add: added AITown.IsWithinTownRadius, AIStation.IsWithinTownRadius and AITile.GetOwner (Yexo)
truebrain
parents: 10339
diff changeset
    63
3234cb59de55 (svn r12901) [NoAI] -Add: added AITown.IsWithinTownRadius, AIStation.IsWithinTownRadius and AITile.GetOwner (Yexo)
truebrain
parents: 10339
diff changeset
    64
/* static */ bool AITown::IsWithinTownRadius(TownID town_id, TileIndex tile)
3234cb59de55 (svn r12901) [NoAI] -Add: added AITown.IsWithinTownRadius, AIStation.IsWithinTownRadius and AITile.GetOwner (Yexo)
truebrain
parents: 10339
diff changeset
    65
{
3234cb59de55 (svn r12901) [NoAI] -Add: added AITown.IsWithinTownRadius, AIStation.IsWithinTownRadius and AITile.GetOwner (Yexo)
truebrain
parents: 10339
diff changeset
    66
	const Town *t = ::GetTown(town_id);
3234cb59de55 (svn r12901) [NoAI] -Add: added AITown.IsWithinTownRadius, AIStation.IsWithinTownRadius and AITile.GetOwner (Yexo)
truebrain
parents: 10339
diff changeset
    67
	return ((uint32)GetDistanceSquareToTile(town_id, tile) <= t->squared_town_zone_radius[0]);
3234cb59de55 (svn r12901) [NoAI] -Add: added AITown.IsWithinTownRadius, AIStation.IsWithinTownRadius and AITile.GetOwner (Yexo)
truebrain
parents: 10339
diff changeset
    68
}