author | tron |
Fri, 26 Aug 2005 20:56:48 +0000 | |
changeset 2366 | 346cbe2de606 |
parent 2186 | db48cf29b983 |
permissions | -rw-r--r-- |
2186 | 1 |
/* $Id$ */ |
2 |
||
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
3 |
#include "stdafx.h" |
1891
862800791170
(svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents:
1713
diff
changeset
|
4 |
#include "openttd.h" |
1299
39c06aba09aa
(svn r1803) Move debugging stuff into files of it's own
tron
parents:
1217
diff
changeset
|
5 |
#include "debug.h" |
2163
b17b313113a0
(svn r2673) Include functions.h directly, not globally via openttd.h
tron
parents:
2159
diff
changeset
|
6 |
#include "functions.h" |
679
04ca2cd69420
(svn r1117) Move map arrays and some related macros into their own files map.c and map.h
tron
parents:
193
diff
changeset
|
7 |
#include "map.h" |
1209
2e00193652b2
(svn r1713) Split off several functions which query/set information about a single tile from map.h and put them into a seperate file tile.h
tron
parents:
1035
diff
changeset
|
8 |
#include "tile.h" |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
9 |
#include "command.h" |
2096
32de4f127e79
(svn r2606) -Codechange: renamed ai.c to ai_old.c, and ai.h to ai_new.h to make room
truelight
parents:
1977
diff
changeset
|
10 |
#include "ai_new.h" |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
11 |
#include "engine.h" |
1217
59c024cfaf54
(svn r1721) -Feature: It is now possible to build multiple road stations (up to 8) on
celestar
parents:
1209
diff
changeset
|
12 |
#include "station.h" |
2159
f6284cf5fab0
(svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents:
2096
diff
changeset
|
13 |
#include "variables.h" |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
14 |
|
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
15 |
// Build HQ |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
16 |
// Params: |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
17 |
// tile : tile where HQ is going to be build |
1977
37bbebf94434
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1926
diff
changeset
|
18 |
bool AiNew_Build_CompanyHQ(Player *p, TileIndex tile) |
37bbebf94434
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1926
diff
changeset
|
19 |
{ |
1713
659ca3025cc5
(svn r2217) - Fix: [ 1184201 ] AI orders its vehicles to a competitor's truck stop. Added a CmdFailed() check to all command returns of the AI instead of the simple == / != CMD_ERROR check. This should fix the problem.
Darkvater
parents:
1299
diff
changeset
|
20 |
if (CmdFailed(DoCommandByTile(tile, 0, 0, DC_AUTO | DC_NO_WATER, CMD_BUILD_COMPANY_HQ))) |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
21 |
return false; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
22 |
DoCommandByTile(tile, 0, 0, DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_BUILD_COMPANY_HQ); |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
23 |
return true; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
24 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
25 |
|
2366 | 26 |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
27 |
// Build station |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
28 |
// Params: |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
29 |
// type : AI_TRAIN/AI_BUS/AI_TRUCK : indicates the type of station |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
30 |
// tile : tile where station is going to be build |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
31 |
// length : in case of AI_TRAIN: length of station |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
32 |
// numtracks : in case of AI_TRAIN: tracks of station |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
33 |
// direction : the direction of the station |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
34 |
// flag : flag passed to DoCommand (normally 0 to get the cost or DC_EXEC to build it) |
1977
37bbebf94434
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1926
diff
changeset
|
35 |
int AiNew_Build_Station(Player *p, byte type, TileIndex tile, byte length, byte numtracks, byte direction, byte flag) |
37bbebf94434
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1926
diff
changeset
|
36 |
{ |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
37 |
if (type == AI_TRAIN) |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
38 |
return DoCommandByTile(tile, direction + (numtracks << 8) + (length << 16), 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_RAILROAD_STATION); |
1713
659ca3025cc5
(svn r2217) - Fix: [ 1184201 ] AI orders its vehicles to a competitor's truck stop. Added a CmdFailed() check to all command returns of the AI instead of the simple == / != CMD_ERROR check. This should fix the problem.
Darkvater
parents:
1299
diff
changeset
|
39 |
|
659ca3025cc5
(svn r2217) - Fix: [ 1184201 ] AI orders its vehicles to a competitor's truck stop. Added a CmdFailed() check to all command returns of the AI instead of the simple == / != CMD_ERROR check. This should fix the problem.
Darkvater
parents:
1299
diff
changeset
|
40 |
if (type == AI_BUS) |
1217
59c024cfaf54
(svn r1721) -Feature: It is now possible to build multiple road stations (up to 8) on
celestar
parents:
1209
diff
changeset
|
41 |
return DoCommandByTile(tile, direction, RS_BUS, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD_STOP); |
1713
659ca3025cc5
(svn r2217) - Fix: [ 1184201 ] AI orders its vehicles to a competitor's truck stop. Added a CmdFailed() check to all command returns of the AI instead of the simple == / != CMD_ERROR check. This should fix the problem.
Darkvater
parents:
1299
diff
changeset
|
42 |
|
659ca3025cc5
(svn r2217) - Fix: [ 1184201 ] AI orders its vehicles to a competitor's truck stop. Added a CmdFailed() check to all command returns of the AI instead of the simple == / != CMD_ERROR check. This should fix the problem.
Darkvater
parents:
1299
diff
changeset
|
43 |
return DoCommandByTile(tile, direction, RS_TRUCK, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD_STOP); |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
44 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
45 |
|
2366 | 46 |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
47 |
// Builds a brdige. The second best out of the ones available for this player |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
48 |
// Params: |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
49 |
// tile_a : starting point |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
50 |
// tile_b : end point |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
51 |
// flag : flag passed to DoCommand |
1977
37bbebf94434
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1926
diff
changeset
|
52 |
int AiNew_Build_Bridge(Player *p, TileIndex tile_a, TileIndex tile_b, byte flag) |
37bbebf94434
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1926
diff
changeset
|
53 |
{ |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
54 |
int bridge_type, bridge_len, type, type2; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
55 |
|
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
56 |
// Find a good bridgetype (the best money can buy) |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
57 |
bridge_len = GetBridgeLength(tile_a, tile_b); |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
58 |
type = type2 = 0; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
59 |
for (bridge_type = MAX_BRIDGES-1; bridge_type >= 0; bridge_type--) { |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
60 |
if (CheckBridge_Stuff(bridge_type, bridge_len)) { |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
61 |
type2 = type; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
62 |
type = bridge_type; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
63 |
// We found two bridges, exit |
2366 | 64 |
if (type2 != 0) break; |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
65 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
66 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
67 |
// There is only one bridge that can be build.. |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
68 |
if (type2 == 0 && type != 0) type2 = type; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
69 |
|
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
70 |
// Now, simply, build the bridge! |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
71 |
if (p->ainew.tbt == AI_TRAIN) |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
72 |
return DoCommandByTile(tile_a, tile_b, (0<<8) + type2, flag | DC_AUTO, CMD_BUILD_BRIDGE); |
1713
659ca3025cc5
(svn r2217) - Fix: [ 1184201 ] AI orders its vehicles to a competitor's truck stop. Added a CmdFailed() check to all command returns of the AI instead of the simple == / != CMD_ERROR check. This should fix the problem.
Darkvater
parents:
1299
diff
changeset
|
73 |
|
659ca3025cc5
(svn r2217) - Fix: [ 1184201 ] AI orders its vehicles to a competitor's truck stop. Added a CmdFailed() check to all command returns of the AI instead of the simple == / != CMD_ERROR check. This should fix the problem.
Darkvater
parents:
1299
diff
changeset
|
74 |
return DoCommandByTile(tile_a, tile_b, (0x80 << 8) + type2, flag | DC_AUTO, CMD_BUILD_BRIDGE); |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
75 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
76 |
|
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
77 |
|
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
78 |
// Build the route part by part |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
79 |
// Basicly what this function do, is build that amount of parts of the route |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
80 |
// that go in the same direction. It sets 'part' to the last part of the route builded. |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
81 |
// The return value is the cost for the builded parts |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
82 |
// |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
83 |
// Params: |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
84 |
// PathFinderInfo : Pointer to the PathFinderInfo used for AiPathFinder |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
85 |
// part : Which part we need to build |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
86 |
// |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
87 |
// TODO: skip already builded road-pieces (e.g.: cityroad) |
2366 | 88 |
int AiNew_Build_RoutePart(Player *p, Ai_PathFinderInfo *PathFinderInfo, byte flag) |
89 |
{ |
|
90 |
int part = PathFinderInfo->position; |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
91 |
byte *route_extra = PathFinderInfo->route_extra; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
92 |
TileIndex *route = PathFinderInfo->route; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
93 |
int dir; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
94 |
int old_dir = -1; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
95 |
int cost = 0; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
96 |
int res; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
97 |
// We need to calculate the direction with the parent of the parent.. so we skip |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
98 |
// the first pieces and the last piece |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
99 |
if (part < 1) part = 1; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
100 |
// When we are done, stop it |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
101 |
if (part >= PathFinderInfo->route_length - 1) { PathFinderInfo->position = -2; return 0; } |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
145
diff
changeset
|
102 |
|
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
145
diff
changeset
|
103 |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
104 |
if (PathFinderInfo->rail_or_road) { |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
105 |
// Tunnel code |
2366 | 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!"); |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
112 |
return 0; |
2366 | 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!"); |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
123 |
return 0; |
2366 | 124 |
} |
125 |
return cost; |
|
126 |
} |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
127 |
|
2366 | 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 { |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
155 |
// Tunnel code |
2366 | 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!"); |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
162 |
return 0; |
2366 | 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!"); |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
173 |
return 0; |
2366 | 174 |
} |
175 |
return cost; |
|
176 |
} |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
177 |
|
2366 | 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 |
} |
|
1713
659ca3025cc5
(svn r2217) - Fix: [ 1184201 ] AI orders its vehicles to a competitor's truck stop. Added a CmdFailed() check to all command returns of the AI instead of the simple == / != CMD_ERROR check. This should fix the problem.
Darkvater
parents:
1299
diff
changeset
|
200 |
|
2366 | 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 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
145
diff
changeset
|
214 |
|
2366 | 215 |
return cost; |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
216 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
217 |
|
2366 | 218 |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
219 |
// This functions tries to find the best vehicle for this type of cargo |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
220 |
// It returns vehicle_id or -1 if not found |
2366 | 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]; |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
230 |
|
2366 | 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 |
} |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
244 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
245 |
|
2366 | 246 |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
247 |
// Builds the best vehicle possible |
1977
37bbebf94434
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1926
diff
changeset
|
248 |
int AiNew_Build_Vehicle(Player *p, TileIndex tile, byte flag) |
37bbebf94434
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1926
diff
changeset
|
249 |
{ |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
250 |
int i = AiNew_PickVehicle(p); |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
251 |
if (i == -1) return CMD_ERROR; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
145
diff
changeset
|
252 |
|
1713
659ca3025cc5
(svn r2217) - Fix: [ 1184201 ] AI orders its vehicles to a competitor's truck stop. Added a CmdFailed() check to all command returns of the AI instead of the simple == / != CMD_ERROR check. This should fix the problem.
Darkvater
parents:
1299
diff
changeset
|
253 |
if (p->ainew.tbt == AI_TRAIN) |
2366 | 254 |
return CMD_ERROR; |
1713
659ca3025cc5
(svn r2217) - Fix: [ 1184201 ] AI orders its vehicles to a competitor's truck stop. Added a CmdFailed() check to all command returns of the AI instead of the simple == / != CMD_ERROR check. This should fix the problem.
Darkvater
parents:
1299
diff
changeset
|
255 |
|
659ca3025cc5
(svn r2217) - Fix: [ 1184201 ] AI orders its vehicles to a competitor's truck stop. Added a CmdFailed() check to all command returns of the AI instead of the simple == / != CMD_ERROR check. This should fix the problem.
Darkvater
parents:
1299
diff
changeset
|
256 |
return DoCommandByTile(tile, i, 0, flag, CMD_BUILD_ROAD_VEH); |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
257 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
258 |
|
1977
37bbebf94434
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1926
diff
changeset
|
259 |
int AiNew_Build_Depot(Player *p, TileIndex tile, byte direction, byte flag) |
1713
659ca3025cc5
(svn r2217) - Fix: [ 1184201 ] AI orders its vehicles to a competitor's truck stop. Added a CmdFailed() check to all command returns of the AI instead of the simple == / != CMD_ERROR check. This should fix the problem.
Darkvater
parents:
1299
diff
changeset
|
260 |
{ |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
261 |
static const byte _roadbits_by_dir[4] = {2,1,8,4}; |
1713
659ca3025cc5
(svn r2217) - Fix: [ 1184201 ] AI orders its vehicles to a competitor's truck stop. Added a CmdFailed() check to all command returns of the AI instead of the simple == / != CMD_ERROR check. This should fix the problem.
Darkvater
parents:
1299
diff
changeset
|
262 |
int ret, ret2; |
2366 | 263 |
if (p->ainew.tbt == AI_TRAIN) |
264 |
return DoCommandByTile(tile, 0, direction, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_TRAIN_DEPOT); |
|
1713
659ca3025cc5
(svn r2217) - Fix: [ 1184201 ] AI orders its vehicles to a competitor's truck stop. Added a CmdFailed() check to all command returns of the AI instead of the simple == / != CMD_ERROR check. This should fix the problem.
Darkvater
parents:
1299
diff
changeset
|
265 |
|
2366 | 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; |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
273 |
} |