1 /* $Id$ */ |
|
2 |
|
3 #include "stdafx.h" |
|
4 #include "openttd.h" |
|
5 #include "debug.h" |
|
6 #include "functions.h" |
|
7 #include "map.h" |
|
8 #include "tile.h" |
|
9 #include "command.h" |
|
10 #include "ai_new.h" |
|
11 #include "engine.h" |
|
12 #include "station.h" |
|
13 #include "variables.h" |
|
14 |
|
15 // Build HQ |
|
16 // Params: |
|
17 // tile : tile where HQ is going to be build |
|
18 bool AiNew_Build_CompanyHQ(Player *p, TileIndex tile) |
|
19 { |
|
20 if (CmdFailed(DoCommandByTile(tile, 0, 0, DC_AUTO | DC_NO_WATER, CMD_BUILD_COMPANY_HQ))) |
|
21 return false; |
|
22 DoCommandByTile(tile, 0, 0, DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_BUILD_COMPANY_HQ); |
|
23 return true; |
|
24 } |
|
25 |
|
26 |
|
27 // Build station |
|
28 // Params: |
|
29 // type : AI_TRAIN/AI_BUS/AI_TRUCK : indicates the type of station |
|
30 // tile : tile where station is going to be build |
|
31 // length : in case of AI_TRAIN: length of station |
|
32 // numtracks : in case of AI_TRAIN: tracks of station |
|
33 // direction : the direction of the station |
|
34 // flag : flag passed to DoCommand (normally 0 to get the cost or DC_EXEC to build it) |
|
35 int AiNew_Build_Station(Player *p, byte type, TileIndex tile, byte length, byte numtracks, byte direction, byte flag) |
|
36 { |
|
37 if (type == AI_TRAIN) |
|
38 return DoCommandByTile(tile, direction + (numtracks << 8) + (length << 16), 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_RAILROAD_STATION); |
|
39 |
|
40 if (type == AI_BUS) |
|
41 return DoCommandByTile(tile, direction, RS_BUS, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD_STOP); |
|
42 |
|
43 return DoCommandByTile(tile, direction, RS_TRUCK, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD_STOP); |
|
44 } |
|
45 |
|
46 |
|
47 // Builds a brdige. The second best out of the ones available for this player |
|
48 // Params: |
|
49 // tile_a : starting point |
|
50 // tile_b : end point |
|
51 // flag : flag passed to DoCommand |
|
52 int AiNew_Build_Bridge(Player *p, TileIndex tile_a, TileIndex tile_b, byte flag) |
|
53 { |
|
54 int bridge_type, bridge_len, type, type2; |
|
55 |
|
56 // Find a good bridgetype (the best money can buy) |
|
57 bridge_len = GetBridgeLength(tile_a, tile_b); |
|
58 type = type2 = 0; |
|
59 for (bridge_type = MAX_BRIDGES-1; bridge_type >= 0; bridge_type--) { |
|
60 if (CheckBridge_Stuff(bridge_type, bridge_len)) { |
|
61 type2 = type; |
|
62 type = bridge_type; |
|
63 // We found two bridges, exit |
|
64 if (type2 != 0) break; |
|
65 } |
|
66 } |
|
67 // There is only one bridge that can be build.. |
|
68 if (type2 == 0 && type != 0) type2 = type; |
|
69 |
|
70 // Now, simply, build the bridge! |
|
71 if (p->ainew.tbt == AI_TRAIN) |
|
72 return DoCommandByTile(tile_a, tile_b, (0<<8) + type2, flag | DC_AUTO, CMD_BUILD_BRIDGE); |
|
73 |
|
74 return DoCommandByTile(tile_a, tile_b, (0x80 << 8) + type2, flag | DC_AUTO, CMD_BUILD_BRIDGE); |
|
75 } |
|
76 |
|
77 |
|
78 // Build the route part by part |
|
79 // Basicly what this function do, is build that amount of parts of the route |
|
80 // that go in the same direction. It sets 'part' to the last part of the route builded. |
|
81 // The return value is the cost for the builded parts |
|
82 // |
|
83 // Params: |
|
84 // PathFinderInfo : Pointer to the PathFinderInfo used for AiPathFinder |
|
85 // part : Which part we need to build |
|
86 // |
|
87 // TODO: skip already builded road-pieces (e.g.: cityroad) |
|
88 int AiNew_Build_RoutePart(Player *p, Ai_PathFinderInfo *PathFinderInfo, byte flag) |
|
89 { |
|
90 int part = PathFinderInfo->position; |
|
91 byte *route_extra = PathFinderInfo->route_extra; |
|
92 TileIndex *route = PathFinderInfo->route; |
|
93 int dir; |
|
94 int old_dir = -1; |
|
95 int cost = 0; |
|
96 int res; |
|
97 // We need to calculate the direction with the parent of the parent.. so we skip |
|
98 // the first pieces and the last piece |
|
99 if (part < 1) part = 1; |
|
100 // When we are done, stop it |
|
101 if (part >= PathFinderInfo->route_length - 1) { PathFinderInfo->position = -2; return 0; } |
|
102 |
|
103 |
|
104 if (PathFinderInfo->rail_or_road) { |
|
105 // Tunnel code |
|
106 if ((AI_PATHFINDER_FLAG_TUNNEL & route_extra[part]) != 0) { |
|
107 cost += DoCommandByTile(route[part], 0, 0, flag, CMD_BUILD_TUNNEL); |
|
108 PathFinderInfo->position++; |
|
109 // TODO: problems! |
|
110 if (CmdFailed(cost)) { |
|
111 DEBUG(ai,0)("[AiNew - BuildPath] We have a serious problem: tunnel could not be build!"); |
|
112 return 0; |
|
113 } |
|
114 return cost; |
|
115 } |
|
116 // Bridge code |
|
117 if ((AI_PATHFINDER_FLAG_BRIDGE & route_extra[part]) != 0) { |
|
118 cost += AiNew_Build_Bridge(p, route[part], route[part-1], flag); |
|
119 PathFinderInfo->position++; |
|
120 // TODO: problems! |
|
121 if (CmdFailed(cost)) { |
|
122 DEBUG(ai,0)("[AiNew - BuildPath] We have a serious problem: bridge could not be build!"); |
|
123 return 0; |
|
124 } |
|
125 return cost; |
|
126 } |
|
127 |
|
128 // Build normal rail |
|
129 // Keep it doing till we go an other way |
|
130 if (route_extra[part-1] == 0 && route_extra[part] == 0) { |
|
131 while (route_extra[part] == 0) { |
|
132 // Get the current direction |
|
133 dir = AiNew_GetRailDirection(route[part-1], route[part], route[part+1]); |
|
134 // Is it the same as the last one? |
|
135 if (old_dir != -1 && old_dir != dir) break; |
|
136 old_dir = dir; |
|
137 // Build the tile |
|
138 res = DoCommandByTile(route[part], 0, dir, flag, CMD_BUILD_SINGLE_RAIL); |
|
139 if (CmdFailed(res)) { |
|
140 // Problem.. let's just abort it all! |
|
141 p->ainew.state = AI_STATE_NOTHING; |
|
142 return 0; |
|
143 } |
|
144 cost += res; |
|
145 // Go to the next tile |
|
146 part++; |
|
147 // Check if it is still in range.. |
|
148 if (part >= PathFinderInfo->route_length - 1) break; |
|
149 } |
|
150 part--; |
|
151 } |
|
152 // We want to return the last position, so we go back one |
|
153 PathFinderInfo->position = part; |
|
154 } else { |
|
155 // Tunnel code |
|
156 if ((AI_PATHFINDER_FLAG_TUNNEL & route_extra[part]) != 0) { |
|
157 cost += DoCommandByTile(route[part], 0x200, 0, flag, CMD_BUILD_TUNNEL); |
|
158 PathFinderInfo->position++; |
|
159 // TODO: problems! |
|
160 if (CmdFailed(cost)) { |
|
161 DEBUG(ai,0)("[AiNew - BuildPath] We have a serious problem: tunnel could not be build!"); |
|
162 return 0; |
|
163 } |
|
164 return cost; |
|
165 } |
|
166 // Bridge code |
|
167 if ((AI_PATHFINDER_FLAG_BRIDGE & route_extra[part]) != 0) { |
|
168 cost += AiNew_Build_Bridge(p, route[part], route[part+1], flag); |
|
169 PathFinderInfo->position++; |
|
170 // TODO: problems! |
|
171 if (CmdFailed(cost)) { |
|
172 DEBUG(ai,0)("[AiNew - BuildPath] We have a serious problem: bridge could not be build!"); |
|
173 return 0; |
|
174 } |
|
175 return cost; |
|
176 } |
|
177 |
|
178 // Build normal road |
|
179 // Keep it doing till we go an other way |
|
180 // EnsureNoVehicle makes sure we don't build on a tile where a vehicle is. This way |
|
181 // it will wait till the vehicle is gone.. |
|
182 if (route_extra[part-1] == 0 && route_extra[part] == 0 && (flag != DC_EXEC || EnsureNoVehicle(route[part]))) { |
|
183 while (route_extra[part] == 0 && (flag != DC_EXEC || EnsureNoVehicle(route[part]))) { |
|
184 // Get the current direction |
|
185 dir = AiNew_GetRoadDirection(route[part-1], route[part], route[part+1]); |
|
186 // Is it the same as the last one? |
|
187 if (old_dir != -1 && old_dir != dir) break; |
|
188 old_dir = dir; |
|
189 // There is already some road, and it is a bridge.. don't build!!! |
|
190 if (!IsTileType(route[part], MP_TUNNELBRIDGE)) { |
|
191 // Build the tile |
|
192 res = DoCommandByTile(route[part], dir, 0, flag | DC_NO_WATER, CMD_BUILD_ROAD); |
|
193 // Currently, we ignore CMD_ERRORs! |
|
194 if (CmdFailed(res) && flag == DC_EXEC && !IsTileType(route[part], MP_STREET) && !EnsureNoVehicle(route[part])) { |
|
195 // Problem.. let's just abort it all! |
|
196 DEBUG(ai,0)("Darn, the route could not be builded.. aborting!"); |
|
197 p->ainew.state = AI_STATE_NOTHING; |
|
198 return 0; |
|
199 } |
|
200 |
|
201 if (!CmdFailed(res)) cost += res; |
|
202 } |
|
203 // Go to the next tile |
|
204 part++; |
|
205 // Check if it is still in range.. |
|
206 if (part >= PathFinderInfo->route_length - 1) break; |
|
207 } |
|
208 part--; |
|
209 // We want to return the last position, so we go back one |
|
210 } |
|
211 if (!EnsureNoVehicle(route[part]) && flag == DC_EXEC) part--; |
|
212 PathFinderInfo->position = part; |
|
213 } |
|
214 |
|
215 return cost; |
|
216 } |
|
217 |
|
218 |
|
219 // This functions tries to find the best vehicle for this type of cargo |
|
220 // It returns vehicle_id or -1 if not found |
|
221 int AiNew_PickVehicle(Player *p) |
|
222 { |
|
223 if (p->ainew.tbt == AI_TRAIN) { |
|
224 // Not supported yet |
|
225 return -1; |
|
226 } else { |
|
227 int start, count, i, ret = CMD_ERROR; |
|
228 start = _cargoc.ai_roadveh_start[p->ainew.cargo]; |
|
229 count = _cargoc.ai_roadveh_count[p->ainew.cargo]; |
|
230 |
|
231 // Let's check it backwards.. we simply want to best engine available.. |
|
232 for (i=start+count-1;i>=start;i--) { |
|
233 // Is it availiable? |
|
234 // Also, check if the reliability of the vehicle is above the AI_VEHICLE_MIN_RELIABILTY |
|
235 if (!HASBIT(GetEngine(i)->player_avail, _current_player) || GetEngine(i)->reliability * 100 < AI_VEHICLE_MIN_RELIABILTY << 16) continue; |
|
236 // Can we build it? |
|
237 ret = DoCommandByTile(0, i, 0, DC_QUERY_COST, CMD_BUILD_ROAD_VEH); |
|
238 if (!CmdFailed(ret)) break; |
|
239 } |
|
240 // We did not find a vehicle :( |
|
241 if (CmdFailed(ret)) { return -1; } |
|
242 return i; |
|
243 } |
|
244 } |
|
245 |
|
246 |
|
247 // Builds the best vehicle possible |
|
248 int AiNew_Build_Vehicle(Player *p, TileIndex tile, byte flag) |
|
249 { |
|
250 int i = AiNew_PickVehicle(p); |
|
251 if (i == -1) return CMD_ERROR; |
|
252 |
|
253 if (p->ainew.tbt == AI_TRAIN) |
|
254 return CMD_ERROR; |
|
255 |
|
256 return DoCommandByTile(tile, i, 0, flag, CMD_BUILD_ROAD_VEH); |
|
257 } |
|
258 |
|
259 int AiNew_Build_Depot(Player *p, TileIndex tile, byte direction, byte flag) |
|
260 { |
|
261 static const byte _roadbits_by_dir[4] = {2,1,8,4}; |
|
262 int ret, ret2; |
|
263 if (p->ainew.tbt == AI_TRAIN) |
|
264 return DoCommandByTile(tile, 0, direction, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_TRAIN_DEPOT); |
|
265 |
|
266 ret = DoCommandByTile(tile, direction, 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD_DEPOT); |
|
267 if (CmdFailed(ret)) return ret; |
|
268 // Try to build the road from the depot |
|
269 ret2 = DoCommandByTile(tile + TileOffsByDir(direction), _roadbits_by_dir[direction], 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD); |
|
270 // If it fails, ignore it.. |
|
271 if (CmdFailed(ret2)) return ret; |
|
272 return ret + ret2; |
|
273 } |
|