author | truebrain |
Tue, 29 Apr 2008 21:34:17 +0000 | |
branch | noai |
changeset 10383 | bda80b3e9c25 |
parent 10381 | 4ad8b98acd82 |
child 10668 | 495789401303 |
permissions | -rw-r--r-- |
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 |
|
9833
89a64246458f
(svn r12496) [NoAI] -Documentation: give the .cpp files a nice uniform format too
truebrain
parents:
9814
diff
changeset
|
3 |
/** @file ai_station.cpp Implementation of AIStation. */ |
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
|
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" |
9814
be51ea0adc29
(svn r12411) [NoAI] -Change [API CHANGE]: order of params of CargoIncome is changed
truebrain
parents:
9736
diff
changeset
|
7 |
#include "ai_map.hpp" |
10360
3234cb59de55
(svn r12901) [NoAI] -Add: added AITown.IsWithinTownRadius, AIStation.IsWithinTownRadius and AITile.GetOwner (Yexo)
truebrain
parents:
10339
diff
changeset
|
8 |
#include "ai_town.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
|
9 |
#include "../../openttd.h" |
10381
4ad8b98acd82
(svn r12922) [NoAI] -Add: added AIStation::SetName() (Yexo)
truebrain
parents:
10361
diff
changeset
|
10 |
#include "../../command_func.h" |
9670
820b77e19bb3
(svn r10609) [NoAI] -Add: added GetAirportWidth / GetAirportHeight
truelight
parents:
9666
diff
changeset
|
11 |
#include "../../debug.h" |
9666
5765eb70736d
(svn r10580) [NoAI] -Add: added GetStationID to get the StationID based on a tile
truelight
parents:
9648
diff
changeset
|
12 |
#include "../../station_map.h" |
9670
820b77e19bb3
(svn r10609) [NoAI] -Add: added GetAirportWidth / GetAirportHeight
truelight
parents:
9666
diff
changeset
|
13 |
#include "../../variables.h" |
10381
4ad8b98acd82
(svn r12922) [NoAI] -Add: added AIStation::SetName() (Yexo)
truebrain
parents:
10361
diff
changeset
|
14 |
#include "../../string_func.h" |
9723
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9696
diff
changeset
|
15 |
#include "../../strings_func.h" |
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9696
diff
changeset
|
16 |
#include "../../core/alloc_func.hpp" |
9724
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
17 |
#include "../../player_func.h" |
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
18 |
#include "../../settings_type.h" |
9696
4384ed3de1f0
(svn r10937) [NoAI] -Add: added AIStation::GetName on request by Nickman
truelight
parents:
9670
diff
changeset
|
19 |
#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
|
20 |
|
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
|
21 |
/* 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
|
22 |
{ |
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
|
23 |
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
|
24 |
} |
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
|
25 |
|
9666
5765eb70736d
(svn r10580) [NoAI] -Add: added GetStationID to get the StationID based on a tile
truelight
parents:
9648
diff
changeset
|
26 |
/* 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
|
27 |
{ |
5765eb70736d
(svn r10580) [NoAI] -Add: added GetStationID to get the StationID based on a tile
truelight
parents:
9648
diff
changeset
|
28 |
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
|
29 |
return ::GetStationIndex(tile); |
5765eb70736d
(svn r10580) [NoAI] -Add: added GetStationID to get the StationID based on a tile
truelight
parents:
9648
diff
changeset
|
30 |
} |
5765eb70736d
(svn r10580) [NoAI] -Add: added GetStationID to get the StationID based on a tile
truelight
parents:
9648
diff
changeset
|
31 |
|
9696
4384ed3de1f0
(svn r10937) [NoAI] -Add: added AIStation::GetName on request by Nickman
truelight
parents:
9670
diff
changeset
|
32 |
/* static */ char *AIStation::GetName(StationID station_id) |
4384ed3de1f0
(svn r10937) [NoAI] -Add: added AIStation::GetName on request by Nickman
truelight
parents:
9670
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:
9732
diff
changeset
|
34 |
if (!IsValidStation(station_id)) return NULL; |
9696
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 |
static const int len = 64; |
4384ed3de1f0
(svn r10937) [NoAI] -Add: added AIStation::GetName on request by Nickman
truelight
parents:
9670
diff
changeset
|
37 |
char *station_name = MallocT<char>(len); |
4384ed3de1f0
(svn r10937) [NoAI] -Add: added AIStation::GetName on request by Nickman
truelight
parents:
9670
diff
changeset
|
38 |
|
9736
183b38e0a480
(svn r12215) [NoAI] -Codechange: added '::' if a function comes from non-AI-API functions (was inconsistant till now)
truebrain
parents:
9732
diff
changeset
|
39 |
::SetDParam(0, GetStation(station_id)->index); |
183b38e0a480
(svn r12215) [NoAI] -Codechange: added '::' if a function comes from non-AI-API functions (was inconsistant till now)
truebrain
parents:
9732
diff
changeset
|
40 |
::GetString(station_name, STR_STATION, &station_name[len - 1]); |
9696
4384ed3de1f0
(svn r10937) [NoAI] -Add: added AIStation::GetName on request by Nickman
truelight
parents:
9670
diff
changeset
|
41 |
return station_name; |
4384ed3de1f0
(svn r10937) [NoAI] -Add: added AIStation::GetName on request by Nickman
truelight
parents:
9670
diff
changeset
|
42 |
} |
4384ed3de1f0
(svn r10937) [NoAI] -Add: added AIStation::GetName on request by Nickman
truelight
parents:
9670
diff
changeset
|
43 |
|
10381
4ad8b98acd82
(svn r12922) [NoAI] -Add: added AIStation::SetName() (Yexo)
truebrain
parents:
10361
diff
changeset
|
44 |
/* static */ bool AIStation::SetName(StationID station_id, const char *name) |
4ad8b98acd82
(svn r12922) [NoAI] -Add: added AIStation::SetName() (Yexo)
truebrain
parents:
10361
diff
changeset
|
45 |
{ |
4ad8b98acd82
(svn r12922) [NoAI] -Add: added AIStation::SetName() (Yexo)
truebrain
parents:
10361
diff
changeset
|
46 |
EnforcePrecondition(false, IsValidStation(station_id)); |
4ad8b98acd82
(svn r12922) [NoAI] -Add: added AIStation::SetName() (Yexo)
truebrain
parents:
10361
diff
changeset
|
47 |
EnforcePrecondition(false, !::StrEmpty(name)); |
4ad8b98acd82
(svn r12922) [NoAI] -Add: added AIStation::SetName() (Yexo)
truebrain
parents:
10361
diff
changeset
|
48 |
|
4ad8b98acd82
(svn r12922) [NoAI] -Add: added AIStation::SetName() (Yexo)
truebrain
parents:
10361
diff
changeset
|
49 |
_cmd_text = name; |
4ad8b98acd82
(svn r12922) [NoAI] -Add: added AIStation::SetName() (Yexo)
truebrain
parents:
10361
diff
changeset
|
50 |
return AIObject::DoCommand(0, 0, station_id, CMD_RENAME_STATION); |
4ad8b98acd82
(svn r12922) [NoAI] -Add: added AIStation::SetName() (Yexo)
truebrain
parents:
10361
diff
changeset
|
51 |
} |
4ad8b98acd82
(svn r12922) [NoAI] -Add: added AIStation::SetName() (Yexo)
truebrain
parents:
10361
diff
changeset
|
52 |
|
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
|
53 |
/* 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
|
54 |
{ |
9736
183b38e0a480
(svn r12215) [NoAI] -Codechange: added '::' if a function comes from non-AI-API functions (was inconsistant till now)
truebrain
parents:
9732
diff
changeset
|
55 |
if (!IsValidStation(station_id)) return INVALID_TILE; |
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
|
56 |
|
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
|
57 |
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
|
58 |
} |
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
|
59 |
|
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
|
60 |
/* 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
|
61 |
{ |
9736
183b38e0a480
(svn r12215) [NoAI] -Codechange: added '::' if a function comes from non-AI-API functions (was inconsistant till now)
truebrain
parents:
9732
diff
changeset
|
62 |
if (!IsValidStation(station_id)) return -1; |
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
|
63 |
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
|
64 |
|
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
|
65 |
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
|
66 |
} |
9648
760b7b504e37
(svn r10543) [NoAI] -Add: added AIStationListCargoRating as valuator for a station list
truelight
parents:
9638
diff
changeset
|
67 |
|
760b7b504e37
(svn r10543) [NoAI] -Add: added AIStationListCargoRating as valuator for a station list
truelight
parents:
9638
diff
changeset
|
68 |
/* 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
|
69 |
{ |
9736
183b38e0a480
(svn r12215) [NoAI] -Codechange: added '::' if a function comes from non-AI-API functions (was inconsistant till now)
truebrain
parents:
9732
diff
changeset
|
70 |
if (!IsValidStation(station_id)) return -1; |
9648
760b7b504e37
(svn r10543) [NoAI] -Add: added AIStationListCargoRating as valuator for a station list
truelight
parents:
9638
diff
changeset
|
71 |
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
|
72 |
|
760b7b504e37
(svn r10543) [NoAI] -Add: added AIStationListCargoRating as valuator for a station list
truelight
parents:
9638
diff
changeset
|
73 |
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
|
74 |
} |
9670
820b77e19bb3
(svn r10609) [NoAI] -Add: added GetAirportWidth / GetAirportHeight
truelight
parents:
9666
diff
changeset
|
75 |
|
820b77e19bb3
(svn r10609) [NoAI] -Add: added GetAirportWidth / GetAirportHeight
truelight
parents:
9666
diff
changeset
|
76 |
/* static */ int32 AIStation::GetCoverageRadius(AIStation::StationType type) |
820b77e19bb3
(svn r10609) [NoAI] -Add: added GetAirportWidth / GetAirportHeight
truelight
parents:
9666
diff
changeset
|
77 |
{ |
820b77e19bb3
(svn r10609) [NoAI] -Add: added GetAirportWidth / GetAirportHeight
truelight
parents:
9666
diff
changeset
|
78 |
if (type == STATION_AIRPORT) { |
820b77e19bb3
(svn r10609) [NoAI] -Add: added GetAirportWidth / GetAirportHeight
truelight
parents:
9666
diff
changeset
|
79 |
DEBUG(ai, 0, "GetCoverageRadius(): coverage radius of airports needs to be requested via AIAirport::GetAirportCoverageRadius(), as it requires AirportType"); |
10194
c9fdeb7450da
(svn r12727) [NoAI] -Fix (API CHANGE): return -1 to indicate invalidity, instead of 0 or -1, depending on the class
truebrain
parents:
9837
diff
changeset
|
80 |
return -1; |
9670
820b77e19bb3
(svn r10609) [NoAI] -Add: added GetAirportWidth / GetAirportHeight
truelight
parents:
9666
diff
changeset
|
81 |
} |
9732 | 82 |
if (!_patches.modified_catchment) return CA_UNMODIFIED; |
9670
820b77e19bb3
(svn r10609) [NoAI] -Add: added GetAirportWidth / GetAirportHeight
truelight
parents:
9666
diff
changeset
|
83 |
|
820b77e19bb3
(svn r10609) [NoAI] -Add: added GetAirportWidth / GetAirportHeight
truelight
parents:
9666
diff
changeset
|
84 |
switch (type) { |
820b77e19bb3
(svn r10609) [NoAI] -Add: added GetAirportWidth / GetAirportHeight
truelight
parents:
9666
diff
changeset
|
85 |
case STATION_TRAIN: return CA_TRAIN; |
820b77e19bb3
(svn r10609) [NoAI] -Add: added GetAirportWidth / GetAirportHeight
truelight
parents:
9666
diff
changeset
|
86 |
case STATION_TRUCK_STOP: return CA_TRUCK; |
820b77e19bb3
(svn r10609) [NoAI] -Add: added GetAirportWidth / GetAirportHeight
truelight
parents:
9666
diff
changeset
|
87 |
case STATION_BUS_STOP: return CA_BUS; |
820b77e19bb3
(svn r10609) [NoAI] -Add: added GetAirportWidth / GetAirportHeight
truelight
parents:
9666
diff
changeset
|
88 |
case STATION_DOCK: return CA_DOCK; |
820b77e19bb3
(svn r10609) [NoAI] -Add: added GetAirportWidth / GetAirportHeight
truelight
parents:
9666
diff
changeset
|
89 |
default: return CA_NONE; |
820b77e19bb3
(svn r10609) [NoAI] -Add: added GetAirportWidth / GetAirportHeight
truelight
parents:
9666
diff
changeset
|
90 |
} |
820b77e19bb3
(svn r10609) [NoAI] -Add: added GetAirportWidth / GetAirportHeight
truelight
parents:
9666
diff
changeset
|
91 |
} |
9814
be51ea0adc29
(svn r12411) [NoAI] -Change [API CHANGE]: order of params of CargoIncome is changed
truebrain
parents:
9736
diff
changeset
|
92 |
|
be51ea0adc29
(svn r12411) [NoAI] -Change [API CHANGE]: order of params of CargoIncome is changed
truebrain
parents:
9736
diff
changeset
|
93 |
/* static */ int32 AIStation::GetDistanceManhattanToTile(StationID station_id, TileIndex tile) |
be51ea0adc29
(svn r12411) [NoAI] -Change [API CHANGE]: order of params of CargoIncome is changed
truebrain
parents:
9736
diff
changeset
|
94 |
{ |
be51ea0adc29
(svn r12411) [NoAI] -Change [API CHANGE]: order of params of CargoIncome is changed
truebrain
parents:
9736
diff
changeset
|
95 |
return AIMap::DistanceManhattan(tile, GetLocation(station_id)); |
be51ea0adc29
(svn r12411) [NoAI] -Change [API CHANGE]: order of params of CargoIncome is changed
truebrain
parents:
9736
diff
changeset
|
96 |
} |
be51ea0adc29
(svn r12411) [NoAI] -Change [API CHANGE]: order of params of CargoIncome is changed
truebrain
parents:
9736
diff
changeset
|
97 |
|
be51ea0adc29
(svn r12411) [NoAI] -Change [API CHANGE]: order of params of CargoIncome is changed
truebrain
parents:
9736
diff
changeset
|
98 |
/* static */ int32 AIStation::GetDistanceSquareToTile(StationID station_id, TileIndex tile) |
be51ea0adc29
(svn r12411) [NoAI] -Change [API CHANGE]: order of params of CargoIncome is changed
truebrain
parents:
9736
diff
changeset
|
99 |
{ |
be51ea0adc29
(svn r12411) [NoAI] -Change [API CHANGE]: order of params of CargoIncome is changed
truebrain
parents:
9736
diff
changeset
|
100 |
return AIMap::DistanceSquare(tile, GetLocation(station_id)); |
be51ea0adc29
(svn r12411) [NoAI] -Change [API CHANGE]: order of params of CargoIncome is changed
truebrain
parents:
9736
diff
changeset
|
101 |
} |
10360
3234cb59de55
(svn r12901) [NoAI] -Add: added AITown.IsWithinTownRadius, AIStation.IsWithinTownRadius and AITile.GetOwner (Yexo)
truebrain
parents:
10339
diff
changeset
|
102 |
|
10361
4cdffd48480f
(svn r12902) [NoAI] -Fix r12901 [API CHANGE]: renamed IsWithinTownRadius to IsWithinTownInfluence, as that reflects the meaning much better
truebrain
parents:
10360
diff
changeset
|
103 |
/* static */ bool AIStation::IsWithinTownInfluence(StationID station_id, TownID town_id) |
10360
3234cb59de55
(svn r12901) [NoAI] -Add: added AITown.IsWithinTownRadius, AIStation.IsWithinTownRadius and AITile.GetOwner (Yexo)
truebrain
parents:
10339
diff
changeset
|
104 |
{ |
10361
4cdffd48480f
(svn r12902) [NoAI] -Fix r12901 [API CHANGE]: renamed IsWithinTownRadius to IsWithinTownInfluence, as that reflects the meaning much better
truebrain
parents:
10360
diff
changeset
|
105 |
return AITown::IsWithinTownInfluence(town_id, GetLocation(station_id)); |
10360
3234cb59de55
(svn r12901) [NoAI] -Add: added AITown.IsWithinTownRadius, AIStation.IsWithinTownRadius and AITile.GetOwner (Yexo)
truebrain
parents:
10339
diff
changeset
|
106 |
} |