4 |
4 |
5 #include "ai_road.hpp" |
5 #include "ai_road.hpp" |
6 #include "../../road_map.h" |
6 #include "../../road_map.h" |
7 #include "../../station_map.h" |
7 #include "../../station_map.h" |
8 |
8 |
9 bool AIRoad::IsRoadTile(TileIndex tile) |
9 /* static */ bool AIRoad::IsRoadTile(TileIndex tile) |
10 { |
10 { |
11 /* Outside of the map */ |
11 /* Outside of the map */ |
12 if (tile >= ::MapSize()) return false; |
12 if (tile >= ::MapSize()) return false; |
13 |
13 |
14 return (::IsTileType(tile, MP_ROAD) && ::GetRoadTileType(tile) != ROAD_TILE_DEPOT) || |
14 return (::IsTileType(tile, MP_ROAD) && ::GetRoadTileType(tile) != ROAD_TILE_DEPOT) || |
15 this->IsDriveThroughRoadStationTile(tile); |
15 IsDriveThroughRoadStationTile(tile); |
16 } |
16 } |
17 |
17 |
18 bool AIRoad::IsRoadDepotTile(TileIndex tile) |
18 /* static */ bool AIRoad::IsRoadDepotTile(TileIndex tile) |
19 { |
19 { |
20 /* Outside of the map */ |
20 /* Outside of the map */ |
21 if (tile >= ::MapSize()) return false; |
21 if (tile >= ::MapSize()) return false; |
22 |
22 |
23 return ::IsTileType(tile, MP_ROAD) && ::GetRoadTileType(tile) == ROAD_TILE_DEPOT; |
23 return ::IsTileType(tile, MP_ROAD) && ::GetRoadTileType(tile) == ROAD_TILE_DEPOT; |
24 } |
24 } |
25 |
25 |
26 bool AIRoad::IsRoadStationTile(TileIndex tile) |
26 /* static */ bool AIRoad::IsRoadStationTile(TileIndex tile) |
27 { |
27 { |
28 /* Outside of the map */ |
28 /* Outside of the map */ |
29 if (tile >= ::MapSize()) return false; |
29 if (tile >= ::MapSize()) return false; |
30 |
30 |
31 return ::IsRoadStopTile(tile); |
31 return ::IsRoadStopTile(tile); |
32 } |
32 } |
33 |
33 |
34 bool AIRoad::IsDriveThroughRoadStationTile(TileIndex tile) |
34 /* static */ bool AIRoad::IsDriveThroughRoadStationTile(TileIndex tile) |
35 { |
35 { |
36 /* Outside of the map */ |
36 /* Outside of the map */ |
37 if (tile >= ::MapSize()) return false; |
37 if (tile >= ::MapSize()) return false; |
38 |
38 |
39 return ::IsDriveThroughStopTile(tile); |
39 return ::IsDriveThroughStopTile(tile); |
40 } |
40 } |
41 |
41 |
42 bool AIRoad::AreRoadTilesConnected(TileIndex t1, TileIndex t2) |
42 /* static */ bool AIRoad::AreRoadTilesConnected(TileIndex t1, TileIndex t2) |
43 { |
43 { |
44 /* Outside of the map */ |
44 /* Outside of the map */ |
45 if (t1 >= ::MapSize() || t2 >= ::MapSize()) return false; |
45 if (t1 >= ::MapSize() || t2 >= ::MapSize()) return false; |
46 |
46 |
47 /* Tiles not neighbouring */ |
47 /* Tiles not neighbouring */ |
69 if (::IsTileType(tile + ::TileDiffXY( 0, 1), MP_ROAD) && ::GetRoadTileType(tile + ::TileDiffXY( 0, 1)) != ROAD_TILE_DEPOT) neighbour++; |
69 if (::IsTileType(tile + ::TileDiffXY( 0, 1), MP_ROAD) && ::GetRoadTileType(tile + ::TileDiffXY( 0, 1)) != ROAD_TILE_DEPOT) neighbour++; |
70 |
70 |
71 return neighbour; |
71 return neighbour; |
72 } |
72 } |
73 |
73 |
74 TileIndex AIRoad::GetRoadDepotFrontTile(TileIndex depot) |
74 /* static */ TileIndex AIRoad::GetRoadDepotFrontTile(TileIndex depot) |
75 { |
75 { |
76 if (!this->IsRoadDepotTile(depot)) return INVALID_TILE; |
76 if (!IsRoadDepotTile(depot)) return INVALID_TILE; |
77 |
77 |
78 return depot + ::TileOffsByDiagDir(::GetRoadDepotDirection(depot)); |
78 return depot + ::TileOffsByDiagDir(::GetRoadDepotDirection(depot)); |
79 } |
79 } |
80 |
80 |
81 TileIndex AIRoad::GetRoadStationFrontTile(TileIndex station) |
81 /* static */ TileIndex AIRoad::GetRoadStationFrontTile(TileIndex station) |
82 { |
82 { |
83 if (!this->IsRoadStationTile(station)) return INVALID_TILE; |
83 if (!IsRoadStationTile(station)) return INVALID_TILE; |
84 |
84 |
85 return station + ::TileOffsByDiagDir(::GetRoadStopDir(station)); |
85 return station + ::TileOffsByDiagDir(::GetRoadStopDir(station)); |
86 } |
86 } |
87 |
87 |
88 TileIndex AIRoad::GetDriveThroughBackTile(TileIndex station) |
88 /* static */ TileIndex AIRoad::GetDriveThroughBackTile(TileIndex station) |
89 { |
89 { |
90 if (!this->IsDriveThroughRoadStationTile(station)) return INVALID_TILE; |
90 if (!IsDriveThroughRoadStationTile(station)) return INVALID_TILE; |
91 |
91 |
92 return station + ::TileOffsByDiagDir(::ReverseDiagDir(::GetRoadStopDir(station))); |
92 return station + ::TileOffsByDiagDir(::ReverseDiagDir(::GetRoadStopDir(station))); |
93 } |
93 } |
94 |
94 |
95 bool AIRoad::BuildRoad(TileIndex start, TileIndex end) |
95 /* static */ bool AIRoad::BuildRoad(TileIndex start, TileIndex end) |
96 { |
96 { |
97 /* Outside of the map */ |
97 /* Outside of the map */ |
98 if (start >= ::MapSize() || end >= ::MapSize() || start == end) return false; |
98 if (start >= ::MapSize() || end >= ::MapSize() || start == end) return false; |
99 /* Not on one line */ |
99 /* Not on one line */ |
100 if (TileX(start) != TileX(end) && |
100 if (TileX(start) != TileX(end) && |
101 TileY(start) != TileY(end)) return false; |
101 TileY(start) != TileY(end)) return false; |
102 |
102 |
103 return this->DoCommand(end, start, (TileY(start) != TileY(end) ? 4 : 0) | (start < end ? 1 : 2) | (ROADTYPE_ROAD << 3), CMD_BUILD_LONG_ROAD); |
103 return AIObject::DoCommand(end, start, (TileY(start) != TileY(end) ? 4 : 0) | (start < end ? 1 : 2) | (ROADTYPE_ROAD << 3), CMD_BUILD_LONG_ROAD); |
104 } |
104 } |
105 |
105 |
106 bool AIRoad::BuildRoadFull(TileIndex start, TileIndex end) |
106 /* static */ bool AIRoad::BuildRoadFull(TileIndex start, TileIndex end) |
107 { |
107 { |
108 /* Outside of the map */ |
108 /* Outside of the map */ |
109 if (start >= ::MapSize() || end >= ::MapSize() || start == end) return false; |
109 if (start >= ::MapSize() || end >= ::MapSize() || start == end) return false; |
110 /* Not on one line */ |
110 /* Not on one line */ |
111 if (TileX(start) != TileX(end) && |
111 if (TileX(start) != TileX(end) && |
112 TileY(start) != TileY(end)) return false; |
112 TileY(start) != TileY(end)) return false; |
113 |
113 |
114 return this->DoCommand(end, start, (TileY(start) != TileY(end) ? 4 : 0) | (start < end ? 2 : 1), CMD_BUILD_LONG_ROAD); |
114 return AIObject::DoCommand(end, start, (TileY(start) != TileY(end) ? 4 : 0) | (start < end ? 2 : 1), CMD_BUILD_LONG_ROAD); |
115 } |
115 } |
116 |
116 |
117 bool AIRoad::BuildRoadDepot(TileIndex tile, TileIndex front) |
117 /* static */ bool AIRoad::BuildRoadDepot(TileIndex tile, TileIndex front) |
118 { |
118 { |
119 /* Outside of the map */ |
119 /* Outside of the map */ |
120 if (tile >= ::MapSize() || tile == front) return false; |
120 if (tile >= ::MapSize() || tile == front) return false; |
121 |
121 |
122 uint entrance_dir = (TileX(tile) == TileX(front)) ? (TileY(tile) < TileY(front) ? 1 : 3) : (TileX(tile) < TileX(front) ? 2 : 0); |
122 uint entrance_dir = (TileX(tile) == TileX(front)) ? (TileY(tile) < TileY(front) ? 1 : 3) : (TileX(tile) < TileX(front) ? 2 : 0); |
123 |
123 |
124 return this->DoCommand(tile, entrance_dir, ROADTYPE_ROAD << 2, CMD_BUILD_ROAD_DEPOT); |
124 return AIObject::DoCommand(tile, entrance_dir, ROADTYPE_ROAD << 2, CMD_BUILD_ROAD_DEPOT); |
125 } |
125 } |
126 |
126 |
127 bool AIRoad::BuildRoadStation(TileIndex tile, TileIndex front, bool truck, bool drive_through) |
127 /* static */ bool AIRoad::BuildRoadStation(TileIndex tile, TileIndex front, bool truck, bool drive_through) |
128 { |
128 { |
129 /* Outside of the map */ |
129 /* Outside of the map */ |
130 if (tile >= ::MapSize() || tile == front) return false; |
130 if (tile >= ::MapSize() || tile == front) return false; |
131 |
131 |
132 uint entrance_dir; |
132 uint entrance_dir; |
134 entrance_dir = TileY(tile) != TileY(front); |
134 entrance_dir = TileY(tile) != TileY(front); |
135 } else { |
135 } else { |
136 entrance_dir = (TileX(tile) == TileX(front)) ? (TileY(tile) < TileY(front) ? 1 : 3) : (TileX(tile) < TileX(front) ? 2 : 0); |
136 entrance_dir = (TileX(tile) == TileX(front)) ? (TileY(tile) < TileY(front) ? 1 : 3) : (TileX(tile) < TileX(front) ? 2 : 0); |
137 } |
137 } |
138 |
138 |
139 return this->DoCommand(tile, entrance_dir, (drive_through ? 2 : 0) | (truck ? 1 : 0) | (ROADTYPES_ROAD << 2), CMD_BUILD_ROAD_STOP); |
139 return AIObject::DoCommand(tile, entrance_dir, (drive_through ? 2 : 0) | (truck ? 1 : 0) | (ROADTYPES_ROAD << 2), CMD_BUILD_ROAD_STOP); |
140 } |
140 } |
141 |
141 |
142 bool AIRoad::RemoveRoad(TileIndex start, TileIndex end) |
142 /* static */ bool AIRoad::RemoveRoad(TileIndex start, TileIndex end) |
143 { |
143 { |
144 /* Outside of the map */ |
144 /* Outside of the map */ |
145 if (start >= ::MapSize() || end >= ::MapSize()) return false; |
145 if (start >= ::MapSize() || end >= ::MapSize()) return false; |
146 /* Not on one line */ |
146 /* Not on one line */ |
147 if (TileX(start) != TileX(end) && |
147 if (TileX(start) != TileX(end) && |
148 TileY(start) != TileY(end)) return false; |
148 TileY(start) != TileY(end)) return false; |
149 |
149 |
150 return this->DoCommand(end, start, (TileY(start) != TileY(end) ? 4 : 0) | (start < end ? 1 : 2) | (ROADTYPE_ROAD << 3), CMD_REMOVE_LONG_ROAD); |
150 return AIObject::DoCommand(end, start, (TileY(start) != TileY(end) ? 4 : 0) | (start < end ? 1 : 2) | (ROADTYPE_ROAD << 3), CMD_REMOVE_LONG_ROAD); |
151 } |
151 } |
152 |
152 |
153 bool AIRoad::RemoveRoadFull(TileIndex start, TileIndex end) |
153 /* static */ bool AIRoad::RemoveRoadFull(TileIndex start, TileIndex end) |
154 { |
154 { |
155 /* Outside of the map */ |
155 /* Outside of the map */ |
156 if (start >= ::MapSize() || end >= ::MapSize()) return false; |
156 if (start >= ::MapSize() || end >= ::MapSize()) return false; |
157 /* Not on one line */ |
157 /* Not on one line */ |
158 if (TileX(start) != TileX(end) && |
158 if (TileX(start) != TileX(end) && |
159 TileY(start) != TileY(end)) return false; |
159 TileY(start) != TileY(end)) return false; |
160 |
160 |
161 return this->DoCommand(end, start, (TileY(start) != TileY(end) ? 4 : 0) | (start < end ? 2 : 1), CMD_REMOVE_LONG_ROAD); |
161 return AIObject::DoCommand(end, start, (TileY(start) != TileY(end) ? 4 : 0) | (start < end ? 2 : 1), CMD_REMOVE_LONG_ROAD); |
162 } |
162 } |
163 |
163 |
164 bool AIRoad::RemoveRoadDepot(TileIndex tile) |
164 /* static */ bool AIRoad::RemoveRoadDepot(TileIndex tile) |
165 { |
165 { |
166 /* Outside of the map */ |
166 /* Outside of the map */ |
167 if (tile >= ::MapSize()) return false; |
167 if (tile >= ::MapSize()) return false; |
168 |
168 |
169 /* Not a road depot tile */ |
169 /* Not a road depot tile */ |
170 if (!IsTileType(tile, MP_ROAD) || GetRoadTileType(tile) != ROAD_TILE_DEPOT) return false; |
170 if (!IsTileType(tile, MP_ROAD) || GetRoadTileType(tile) != ROAD_TILE_DEPOT) return false; |
171 |
171 |
172 return this->DoCommand(tile, 0, 0, CMD_LANDSCAPE_CLEAR); |
172 return AIObject::DoCommand(tile, 0, 0, CMD_LANDSCAPE_CLEAR); |
173 } |
173 } |
174 |
174 |
175 bool AIRoad::RemoveRoadStation(TileIndex tile) |
175 /* static */ bool AIRoad::RemoveRoadStation(TileIndex tile) |
176 { |
176 { |
177 /* Outside of the map */ |
177 /* Outside of the map */ |
178 if (tile >= ::MapSize()) return false; |
178 if (tile >= ::MapSize()) return false; |
179 |
179 |
180 /* Not a road station tile */ |
180 /* Not a road station tile */ |
181 if (!IsTileType(tile, MP_STATION) || !IsRoadStop(tile)) return false; |
181 if (!IsTileType(tile, MP_STATION) || !IsRoadStop(tile)) return false; |
182 |
182 |
183 return this->DoCommand(tile, 0, GetRoadStopType(tile), CMD_REMOVE_ROAD_STOP); |
183 return AIObject::DoCommand(tile, 0, GetRoadStopType(tile), CMD_REMOVE_ROAD_STOP); |
184 } |
184 } |