src/ai/api/ai_tile.cpp
author truebrain
Wed, 26 Mar 2008 15:17:40 +0000
branchnoai
changeset 9823 0b7f816cf46f
parent 9814 be51ea0adc29
child 9833 89a64246458f
permissions -rw-r--r--
(svn r12431) [NoAI] -Add: added AIEventSubsidiaryOffer, which keeps you informed about new Subsidiaries
9617
df9cedf12aab (svn r9786) [NoAI] -Fix: NeighbourRoad -> NeighbourRoadCount
truelight
parents:
diff changeset
     1
/* $Id$ */
df9cedf12aab (svn r9786) [NoAI] -Fix: NeighbourRoad -> NeighbourRoadCount
truelight
parents:
diff changeset
     2
df9cedf12aab (svn r9786) [NoAI] -Fix: NeighbourRoad -> NeighbourRoadCount
truelight
parents:
diff changeset
     3
/** @file ai_tile.cpp handles the functions of the AITile class */
df9cedf12aab (svn r9786) [NoAI] -Fix: NeighbourRoad -> NeighbourRoadCount
truelight
parents:
diff changeset
     4
df9cedf12aab (svn r9786) [NoAI] -Fix: NeighbourRoad -> NeighbourRoadCount
truelight
parents:
diff changeset
     5
#include "ai_tile.hpp"
9814
be51ea0adc29 (svn r12411) [NoAI] -Change [API CHANGE]: order of params of CargoIncome is changed
truebrain
parents: 9801
diff changeset
     6
#include "ai_map.hpp"
9723
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9708
diff changeset
     7
#include "../../tile_map.h"
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9708
diff changeset
     8
#include "../../map_func.h"
9617
df9cedf12aab (svn r9786) [NoAI] -Fix: NeighbourRoad -> NeighbourRoadCount
truelight
parents:
diff changeset
     9
#include "../../variables.h"
df9cedf12aab (svn r9786) [NoAI] -Fix: NeighbourRoad -> NeighbourRoadCount
truelight
parents:
diff changeset
    10
#include "../../station.h"
9723
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9708
diff changeset
    11
#include "../../command_type.h"
9724
b39bc69bb2f2 (svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents: 9723
diff changeset
    12
#include "../../settings_type.h"
9769
015b6674c8ad (svn r12259) [NoAI] -Fix: depots were considered buildable (tnx Progman)
truebrain
parents: 9737
diff changeset
    13
#include "../../road_map.h"
9617
df9cedf12aab (svn r9786) [NoAI] -Fix: NeighbourRoad -> NeighbourRoadCount
truelight
parents:
diff changeset
    14
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
    15
/* static */ bool AITile::IsBuildable(TileIndex tile)
9617
df9cedf12aab (svn r9786) [NoAI] -Fix: NeighbourRoad -> NeighbourRoadCount
truelight
parents:
diff changeset
    16
{
9801
03a3eebd7fb7 (svn r12307) [NoAI] -Codechange: as followup on r12303 (trunk), use ::IsValidTile to check if a tile is inside the map, instead of all our custom ways
truebrain
parents: 9769
diff changeset
    17
	if (!::IsValidTile(tile)) return false;
9617
df9cedf12aab (svn r9786) [NoAI] -Fix: NeighbourRoad -> NeighbourRoadCount
truelight
parents:
diff changeset
    18
df9cedf12aab (svn r9786) [NoAI] -Fix: NeighbourRoad -> NeighbourRoadCount
truelight
parents:
diff changeset
    19
	switch (::GetTileType(tile)) {
9728
d7bd32dc6c59 (svn r12151) [NoAI] -Fix: use the correct types for returned values
glx
parents: 9724
diff changeset
    20
		default: return true;
9617
df9cedf12aab (svn r9786) [NoAI] -Fix: NeighbourRoad -> NeighbourRoadCount
truelight
parents:
diff changeset
    21
		case MP_VOID:
df9cedf12aab (svn r9786) [NoAI] -Fix: NeighbourRoad -> NeighbourRoadCount
truelight
parents:
diff changeset
    22
		case MP_HOUSE:
df9cedf12aab (svn r9786) [NoAI] -Fix: NeighbourRoad -> NeighbourRoadCount
truelight
parents:
diff changeset
    23
		case MP_STATION:
df9cedf12aab (svn r9786) [NoAI] -Fix: NeighbourRoad -> NeighbourRoadCount
truelight
parents:
diff changeset
    24
		case MP_INDUSTRY:
9697
a6a9379988f6 (svn r10938) [NoAI] -Fix r10937: regression update
truelight
parents: 9658
diff changeset
    25
		case MP_UNMOVABLE:
9728
d7bd32dc6c59 (svn r12151) [NoAI] -Fix: use the correct types for returned values
glx
parents: 9724
diff changeset
    26
		case MP_WATER: return false;
9769
015b6674c8ad (svn r12259) [NoAI] -Fix: depots were considered buildable (tnx Progman)
truebrain
parents: 9737
diff changeset
    27
015b6674c8ad (svn r12259) [NoAI] -Fix: depots were considered buildable (tnx Progman)
truebrain
parents: 9737
diff changeset
    28
		case MP_ROAD:
015b6674c8ad (svn r12259) [NoAI] -Fix: depots were considered buildable (tnx Progman)
truebrain
parents: 9737
diff changeset
    29
			/* Depots aren't considered buildable */
015b6674c8ad (svn r12259) [NoAI] -Fix: depots were considered buildable (tnx Progman)
truebrain
parents: 9737
diff changeset
    30
			if (::GetRoadTileType(tile) == ROAD_TILE_DEPOT) return false;
015b6674c8ad (svn r12259) [NoAI] -Fix: depots were considered buildable (tnx Progman)
truebrain
parents: 9737
diff changeset
    31
			return true;
9617
df9cedf12aab (svn r9786) [NoAI] -Fix: NeighbourRoad -> NeighbourRoadCount
truelight
parents:
diff changeset
    32
	}
df9cedf12aab (svn r9786) [NoAI] -Fix: NeighbourRoad -> NeighbourRoadCount
truelight
parents:
diff changeset
    33
}
df9cedf12aab (svn r9786) [NoAI] -Fix: NeighbourRoad -> NeighbourRoadCount
truelight
parents:
diff changeset
    34
9814
be51ea0adc29 (svn r12411) [NoAI] -Change [API CHANGE]: order of params of CargoIncome is changed
truebrain
parents: 9801
diff changeset
    35
/* static */ bool AITile::IsBuildableRectangle(TileIndex tile, uint width, uint height)
be51ea0adc29 (svn r12411) [NoAI] -Change [API CHANGE]: order of params of CargoIncome is changed
truebrain
parents: 9801
diff changeset
    36
{
be51ea0adc29 (svn r12411) [NoAI] -Change [API CHANGE]: order of params of CargoIncome is changed
truebrain
parents: 9801
diff changeset
    37
	uint tx, ty;
be51ea0adc29 (svn r12411) [NoAI] -Change [API CHANGE]: order of params of CargoIncome is changed
truebrain
parents: 9801
diff changeset
    38
be51ea0adc29 (svn r12411) [NoAI] -Change [API CHANGE]: order of params of CargoIncome is changed
truebrain
parents: 9801
diff changeset
    39
	tx = AIMap::GetTileX(tile);
be51ea0adc29 (svn r12411) [NoAI] -Change [API CHANGE]: order of params of CargoIncome is changed
truebrain
parents: 9801
diff changeset
    40
	ty = AIMap::GetTileY(tile);
be51ea0adc29 (svn r12411) [NoAI] -Change [API CHANGE]: order of params of CargoIncome is changed
truebrain
parents: 9801
diff changeset
    41
be51ea0adc29 (svn r12411) [NoAI] -Change [API CHANGE]: order of params of CargoIncome is changed
truebrain
parents: 9801
diff changeset
    42
	for (uint x = tx; x < width + tx; x++) {
be51ea0adc29 (svn r12411) [NoAI] -Change [API CHANGE]: order of params of CargoIncome is changed
truebrain
parents: 9801
diff changeset
    43
		for (uint y = ty; y < height + ty; y++) {
be51ea0adc29 (svn r12411) [NoAI] -Change [API CHANGE]: order of params of CargoIncome is changed
truebrain
parents: 9801
diff changeset
    44
			if (!IsBuildable(AIMap::GetTileIndex(x, y))) return false;
be51ea0adc29 (svn r12411) [NoAI] -Change [API CHANGE]: order of params of CargoIncome is changed
truebrain
parents: 9801
diff changeset
    45
		}
be51ea0adc29 (svn r12411) [NoAI] -Change [API CHANGE]: order of params of CargoIncome is changed
truebrain
parents: 9801
diff changeset
    46
	}
be51ea0adc29 (svn r12411) [NoAI] -Change [API CHANGE]: order of params of CargoIncome is changed
truebrain
parents: 9801
diff changeset
    47
be51ea0adc29 (svn r12411) [NoAI] -Change [API CHANGE]: order of params of CargoIncome is changed
truebrain
parents: 9801
diff changeset
    48
	return true;
be51ea0adc29 (svn r12411) [NoAI] -Change [API CHANGE]: order of params of CargoIncome is changed
truebrain
parents: 9801
diff changeset
    49
}
be51ea0adc29 (svn r12411) [NoAI] -Change [API CHANGE]: order of params of CargoIncome is changed
truebrain
parents: 9801
diff changeset
    50
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
    51
/* static */ bool AITile::IsWater(TileIndex tile)
9698
1d50fe99b7e9 (svn r10939) [NoAI] -Add: added AITileList valuator Water
truelight
parents: 9697
diff changeset
    52
{
9801
03a3eebd7fb7 (svn r12307) [NoAI] -Codechange: as followup on r12303 (trunk), use ::IsValidTile to check if a tile is inside the map, instead of all our custom ways
truebrain
parents: 9769
diff changeset
    53
	if (!::IsValidTile(tile)) return false;
9698
1d50fe99b7e9 (svn r10939) [NoAI] -Add: added AITileList valuator Water
truelight
parents: 9697
diff changeset
    54
1d50fe99b7e9 (svn r10939) [NoAI] -Add: added AITileList valuator Water
truelight
parents: 9697
diff changeset
    55
	return ::GetTileType(tile) == MP_WATER;
1d50fe99b7e9 (svn r10939) [NoAI] -Add: added AITileList valuator Water
truelight
parents: 9697
diff changeset
    56
}
1d50fe99b7e9 (svn r10939) [NoAI] -Add: added AITileList valuator Water
truelight
parents: 9697
diff changeset
    57
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
    58
/* static */ int32 AITile::GetSlope(TileIndex tile)
9617
df9cedf12aab (svn r9786) [NoAI] -Fix: NeighbourRoad -> NeighbourRoadCount
truelight
parents:
diff changeset
    59
{
9801
03a3eebd7fb7 (svn r12307) [NoAI] -Codechange: as followup on r12303 (trunk), use ::IsValidTile to check if a tile is inside the map, instead of all our custom ways
truebrain
parents: 9769
diff changeset
    60
	if (!::IsValidTile(tile)) return false;
9617
df9cedf12aab (svn r9786) [NoAI] -Fix: NeighbourRoad -> NeighbourRoadCount
truelight
parents:
diff changeset
    61
9700
e442ce398e83 (svn r10941) [NoAI] -Add: added AITile::GetHeight and AITileList valuator Height
truelight
parents: 9698
diff changeset
    62
	return ::GetTileSlope(tile, NULL);
e442ce398e83 (svn r10941) [NoAI] -Add: added AITile::GetHeight and AITileList valuator Height
truelight
parents: 9698
diff changeset
    63
}
e442ce398e83 (svn r10941) [NoAI] -Add: added AITile::GetHeight and AITileList valuator Height
truelight
parents: 9698
diff changeset
    64
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
    65
/* static */ int32 AITile::GetHeight(TileIndex tile)
9700
e442ce398e83 (svn r10941) [NoAI] -Add: added AITile::GetHeight and AITileList valuator Height
truelight
parents: 9698
diff changeset
    66
{
9801
03a3eebd7fb7 (svn r12307) [NoAI] -Codechange: as followup on r12303 (trunk), use ::IsValidTile to check if a tile is inside the map, instead of all our custom ways
truebrain
parents: 9769
diff changeset
    67
	if (!::IsValidTile(tile)) return false;
9700
e442ce398e83 (svn r10941) [NoAI] -Add: added AITile::GetHeight and AITileList valuator Height
truelight
parents: 9698
diff changeset
    68
e442ce398e83 (svn r10941) [NoAI] -Add: added AITile::GetHeight and AITileList valuator Height
truelight
parents: 9698
diff changeset
    69
	return ::TileHeight(tile);
9617
df9cedf12aab (svn r9786) [NoAI] -Fix: NeighbourRoad -> NeighbourRoadCount
truelight
parents:
diff changeset
    70
}
df9cedf12aab (svn r9786) [NoAI] -Fix: NeighbourRoad -> NeighbourRoadCount
truelight
parents:
diff changeset
    71
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
    72
/* static */ int32 AITile::GetCargoAcceptance(TileIndex tile, CargoID cargo_type, uint width, uint height, uint radius)
9617
df9cedf12aab (svn r9786) [NoAI] -Fix: NeighbourRoad -> NeighbourRoadCount
truelight
parents:
diff changeset
    73
{
9801
03a3eebd7fb7 (svn r12307) [NoAI] -Codechange: as followup on r12303 (trunk), use ::IsValidTile to check if a tile is inside the map, instead of all our custom ways
truebrain
parents: 9769
diff changeset
    74
	if (!::IsValidTile(tile)) return false;
9617
df9cedf12aab (svn r9786) [NoAI] -Fix: NeighbourRoad -> NeighbourRoadCount
truelight
parents:
diff changeset
    75
df9cedf12aab (svn r9786) [NoAI] -Fix: NeighbourRoad -> NeighbourRoadCount
truelight
parents:
diff changeset
    76
	AcceptedCargo accepts;
9736
183b38e0a480 (svn r12215) [NoAI] -Codechange: added '::' if a function comes from non-AI-API functions (was inconsistant till now)
truebrain
parents: 9735
diff changeset
    77
	::GetAcceptanceAroundTiles(accepts, tile, width, height, _patches.modified_catchment ? radius : (uint)CA_UNMODIFIED);
9617
df9cedf12aab (svn r9786) [NoAI] -Fix: NeighbourRoad -> NeighbourRoadCount
truelight
parents:
diff changeset
    78
	return accepts[cargo_type];
df9cedf12aab (svn r9786) [NoAI] -Fix: NeighbourRoad -> NeighbourRoadCount
truelight
parents:
diff changeset
    79
}
9708
a63a756fd080 (svn r11274) [NoAI] -Add: added LowerTile and RaiseTile to 'terraform' the map a bit (dynaxo)
truelight
parents: 9700
diff changeset
    80
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
    81
/* static */ int32 AITile::GetCargoProduction(TileIndex tile, CargoID cargo_type, uint width, uint height, uint radius)
9729
c264c78a3567 (svn r12152) [NoAI] -Add [FS#1772]: add AITile.GetCargoProduction() function (Morloth)
glx
parents: 9728
diff changeset
    82
{
9801
03a3eebd7fb7 (svn r12307) [NoAI] -Codechange: as followup on r12303 (trunk), use ::IsValidTile to check if a tile is inside the map, instead of all our custom ways
truebrain
parents: 9769
diff changeset
    83
	if (!::IsValidTile(tile)) return false;
9729
c264c78a3567 (svn r12152) [NoAI] -Add [FS#1772]: add AITile.GetCargoProduction() function (Morloth)
glx
parents: 9728
diff changeset
    84
c264c78a3567 (svn r12152) [NoAI] -Add [FS#1772]: add AITile.GetCargoProduction() function (Morloth)
glx
parents: 9728
diff changeset
    85
	AcceptedCargo produced;
9736
183b38e0a480 (svn r12215) [NoAI] -Codechange: added '::' if a function comes from non-AI-API functions (was inconsistant till now)
truebrain
parents: 9735
diff changeset
    86
	::GetProductionAroundTiles(produced, tile, width, height, _patches.modified_catchment ? radius : (uint)CA_UNMODIFIED);
9729
c264c78a3567 (svn r12152) [NoAI] -Add [FS#1772]: add AITile.GetCargoProduction() function (Morloth)
glx
parents: 9728
diff changeset
    87
	return produced[cargo_type];
c264c78a3567 (svn r12152) [NoAI] -Add [FS#1772]: add AITile.GetCargoProduction() function (Morloth)
glx
parents: 9728
diff changeset
    88
}
c264c78a3567 (svn r12152) [NoAI] -Add [FS#1772]: add AITile.GetCargoProduction() function (Morloth)
glx
parents: 9728
diff changeset
    89
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
    90
/* static */ bool AITile::RaiseTile(TileIndex tile, int32 slope)
9708
a63a756fd080 (svn r11274) [NoAI] -Add: added LowerTile and RaiseTile to 'terraform' the map a bit (dynaxo)
truelight
parents: 9700
diff changeset
    91
{
9801
03a3eebd7fb7 (svn r12307) [NoAI] -Codechange: as followup on r12303 (trunk), use ::IsValidTile to check if a tile is inside the map, instead of all our custom ways
truebrain
parents: 9769
diff changeset
    92
	if (!::IsValidTile(tile)) return false;
9708
a63a756fd080 (svn r11274) [NoAI] -Add: added LowerTile and RaiseTile to 'terraform' the map a bit (dynaxo)
truelight
parents: 9700
diff changeset
    93
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
    94
	return AIObject::DoCommand(tile, slope, 1, CMD_TERRAFORM_LAND);
9708
a63a756fd080 (svn r11274) [NoAI] -Add: added LowerTile and RaiseTile to 'terraform' the map a bit (dynaxo)
truelight
parents: 9700
diff changeset
    95
}
a63a756fd080 (svn r11274) [NoAI] -Add: added LowerTile and RaiseTile to 'terraform' the map a bit (dynaxo)
truelight
parents: 9700
diff changeset
    96
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
    97
/* static */ bool AITile::LowerTile(TileIndex tile, int32 slope)
9708
a63a756fd080 (svn r11274) [NoAI] -Add: added LowerTile and RaiseTile to 'terraform' the map a bit (dynaxo)
truelight
parents: 9700
diff changeset
    98
{
9801
03a3eebd7fb7 (svn r12307) [NoAI] -Codechange: as followup on r12303 (trunk), use ::IsValidTile to check if a tile is inside the map, instead of all our custom ways
truebrain
parents: 9769
diff changeset
    99
	if (!::IsValidTile(tile)) return false;
9708
a63a756fd080 (svn r11274) [NoAI] -Add: added LowerTile and RaiseTile to 'terraform' the map a bit (dynaxo)
truelight
parents: 9700
diff changeset
   100
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
   101
	return AIObject::DoCommand(tile, slope, 0, CMD_TERRAFORM_LAND);
9708
a63a756fd080 (svn r11274) [NoAI] -Add: added LowerTile and RaiseTile to 'terraform' the map a bit (dynaxo)
truelight
parents: 9700
diff changeset
   102
}
9814
be51ea0adc29 (svn r12411) [NoAI] -Change [API CHANGE]: order of params of CargoIncome is changed
truebrain
parents: 9801
diff changeset
   103
be51ea0adc29 (svn r12411) [NoAI] -Change [API CHANGE]: order of params of CargoIncome is changed
truebrain
parents: 9801
diff changeset
   104
/* static */ int32 AITile::GetDistanceManhattanToTile(TileIndex tile_from, TileIndex tile_to)
be51ea0adc29 (svn r12411) [NoAI] -Change [API CHANGE]: order of params of CargoIncome is changed
truebrain
parents: 9801
diff changeset
   105
{
be51ea0adc29 (svn r12411) [NoAI] -Change [API CHANGE]: order of params of CargoIncome is changed
truebrain
parents: 9801
diff changeset
   106
	return AIMap::DistanceManhattan(tile_from, tile_to);
be51ea0adc29 (svn r12411) [NoAI] -Change [API CHANGE]: order of params of CargoIncome is changed
truebrain
parents: 9801
diff changeset
   107
}
be51ea0adc29 (svn r12411) [NoAI] -Change [API CHANGE]: order of params of CargoIncome is changed
truebrain
parents: 9801
diff changeset
   108
be51ea0adc29 (svn r12411) [NoAI] -Change [API CHANGE]: order of params of CargoIncome is changed
truebrain
parents: 9801
diff changeset
   109
/* static */ int32 AITile::GetDistanceSquareToTile(TileIndex tile_from, TileIndex tile_to)
be51ea0adc29 (svn r12411) [NoAI] -Change [API CHANGE]: order of params of CargoIncome is changed
truebrain
parents: 9801
diff changeset
   110
{
be51ea0adc29 (svn r12411) [NoAI] -Change [API CHANGE]: order of params of CargoIncome is changed
truebrain
parents: 9801
diff changeset
   111
	return AIMap::DistanceSquare(tile_from, tile_to);
be51ea0adc29 (svn r12411) [NoAI] -Change [API CHANGE]: order of params of CargoIncome is changed
truebrain
parents: 9801
diff changeset
   112
}