author | matthijs |
Wed, 22 Mar 2006 22:26:16 +0000 | |
branch | 0.4.5 |
changeset 9958 | bed516c67d61 |
parent 2682 | 94ca0b4dc53f |
child 3033 | e4f7c60a1742 |
permissions | -rw-r--r-- |
2186 | 1 |
/* $Id$ */ |
2 |
||
2381
de9053fe2a2c
(svn r2907) -Codechange: splitted the AIs to their own directory. AINew becomes 'trolly', AIOld becomes 'default', both in their own dir in the 'ai' dir. More AIs to come.
truelight
parents:
2366
diff
changeset
|
3 |
#include "../../stdafx.h" |
de9053fe2a2c
(svn r2907) -Codechange: splitted the AIs to their own directory. AINew becomes 'trolly', AIOld becomes 'default', both in their own dir in the 'ai' dir. More AIs to come.
truelight
parents:
2366
diff
changeset
|
4 |
#include "../../openttd.h" |
de9053fe2a2c
(svn r2907) -Codechange: splitted the AIs to their own directory. AINew becomes 'trolly', AIOld becomes 'default', both in their own dir in the 'ai' dir. More AIs to come.
truelight
parents:
2366
diff
changeset
|
5 |
#include "../../debug.h" |
de9053fe2a2c
(svn r2907) -Codechange: splitted the AIs to their own directory. AINew becomes 'trolly', AIOld becomes 'default', both in their own dir in the 'ai' dir. More AIs to come.
truelight
parents:
2366
diff
changeset
|
6 |
#include "../../functions.h" |
de9053fe2a2c
(svn r2907) -Codechange: splitted the AIs to their own directory. AINew becomes 'trolly', AIOld becomes 'default', both in their own dir in the 'ai' dir. More AIs to come.
truelight
parents:
2366
diff
changeset
|
7 |
#include "../../map.h" |
de9053fe2a2c
(svn r2907) -Codechange: splitted the AIs to their own directory. AINew becomes 'trolly', AIOld becomes 'default', both in their own dir in the 'ai' dir. More AIs to come.
truelight
parents:
2366
diff
changeset
|
8 |
#include "../../tile.h" |
de9053fe2a2c
(svn r2907) -Codechange: splitted the AIs to their own directory. AINew becomes 'trolly', AIOld becomes 'default', both in their own dir in the 'ai' dir. More AIs to come.
truelight
parents:
2366
diff
changeset
|
9 |
#include "../../command.h" |
de9053fe2a2c
(svn r2907) -Codechange: splitted the AIs to their own directory. AINew becomes 'trolly', AIOld becomes 'default', both in their own dir in the 'ai' dir. More AIs to come.
truelight
parents:
2366
diff
changeset
|
10 |
#include "trolly.h" |
de9053fe2a2c
(svn r2907) -Codechange: splitted the AIs to their own directory. AINew becomes 'trolly', AIOld becomes 'default', both in their own dir in the 'ai' dir. More AIs to come.
truelight
parents:
2366
diff
changeset
|
11 |
#include "../../engine.h" |
de9053fe2a2c
(svn r2907) -Codechange: splitted the AIs to their own directory. AINew becomes 'trolly', AIOld becomes 'default', both in their own dir in the 'ai' dir. More AIs to come.
truelight
parents:
2366
diff
changeset
|
12 |
#include "../../station.h" |
de9053fe2a2c
(svn r2907) -Codechange: splitted the AIs to their own directory. AINew becomes 'trolly', AIOld becomes 'default', both in their own dir in the 'ai' dir. More AIs to come.
truelight
parents:
2366
diff
changeset
|
13 |
#include "../../variables.h" |
2682
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2639
diff
changeset
|
14 |
#include "../ai.h" |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
15 |
|
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
16 |
// Build HQ |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
17 |
// Params: |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
18 |
// tile : tile where HQ is going to be build |
1977
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1926
diff
changeset
|
19 |
bool AiNew_Build_CompanyHQ(Player *p, TileIndex tile) |
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1926
diff
changeset
|
20 |
{ |
2682
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2639
diff
changeset
|
21 |
if (CmdFailed(AI_DoCommand(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
|
22 |
return false; |
2682
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2639
diff
changeset
|
23 |
AI_DoCommand(tile, 0, 0, DC_EXEC | 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
|
24 |
return true; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
25 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
26 |
|
2366 | 27 |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
28 |
// Build station |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
29 |
// Params: |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
30 |
// 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
|
31 |
// 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
|
32 |
// 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
|
33 |
// 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
|
34 |
// direction : the direction of the station |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
35 |
// flag : flag passed to DoCommand (normally 0 to get the cost or DC_EXEC to build it) |
1977
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1926
diff
changeset
|
36 |
int AiNew_Build_Station(Player *p, byte type, TileIndex tile, byte length, byte numtracks, byte direction, byte flag) |
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1926
diff
changeset
|
37 |
{ |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
38 |
if (type == AI_TRAIN) |
2682
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2639
diff
changeset
|
39 |
return AI_DoCommand(tile, direction + (numtracks << 8) + (length << 16), 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_RAILROAD_STATION); |
1713
d970350410b2
(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 |
|
d970350410b2
(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
|
41 |
if (type == AI_BUS) |
2682
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2639
diff
changeset
|
42 |
return AI_DoCommand(tile, direction, RS_BUS, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD_STOP); |
1713
d970350410b2
(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 |
|
2682
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2639
diff
changeset
|
44 |
return AI_DoCommand(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
|
45 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
46 |
|
2366 | 47 |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
48 |
// 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
|
49 |
// Params: |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
50 |
// tile_a : starting point |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
51 |
// tile_b : end point |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
52 |
// flag : flag passed to DoCommand |
1977
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1926
diff
changeset
|
53 |
int AiNew_Build_Bridge(Player *p, TileIndex tile_a, TileIndex tile_b, byte flag) |
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1926
diff
changeset
|
54 |
{ |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
55 |
int bridge_type, bridge_len, type, type2; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
56 |
|
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
57 |
// 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
|
58 |
bridge_len = GetBridgeLength(tile_a, tile_b); |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
59 |
type = type2 = 0; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
60 |
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
|
61 |
if (CheckBridge_Stuff(bridge_type, bridge_len)) { |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
62 |
type2 = type; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
63 |
type = bridge_type; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
64 |
// We found two bridges, exit |
2366 | 65 |
if (type2 != 0) break; |
110
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 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
68 |
// 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
|
69 |
if (type2 == 0 && type != 0) type2 = type; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
70 |
|
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
71 |
// Now, simply, build the bridge! |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
72 |
if (p->ainew.tbt == AI_TRAIN) |
2682
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2639
diff
changeset
|
73 |
return AI_DoCommand(tile_a, tile_b, (0<<8) + type2, flag | DC_AUTO, CMD_BUILD_BRIDGE); |
1713
d970350410b2
(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 |
|
2682
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2639
diff
changeset
|
75 |
return AI_DoCommand(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
|
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 |
|
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
79 |
// Build the route part by part |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
80 |
// 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
|
81 |
// 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
|
82 |
// 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
|
83 |
// |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
84 |
// Params: |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
85 |
// PathFinderInfo : Pointer to the PathFinderInfo used for AiPathFinder |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
86 |
// part : Which part we need to build |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
87 |
// |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
88 |
// TODO: skip already builded road-pieces (e.g.: cityroad) |
2366 | 89 |
int AiNew_Build_RoutePart(Player *p, Ai_PathFinderInfo *PathFinderInfo, byte flag) |
90 |
{ |
|
91 |
int part = PathFinderInfo->position; |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
92 |
byte *route_extra = PathFinderInfo->route_extra; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
93 |
TileIndex *route = PathFinderInfo->route; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
94 |
int dir; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
95 |
int old_dir = -1; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
96 |
int cost = 0; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
97 |
int res; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
98 |
// 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
|
99 |
// the first pieces and the last piece |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
100 |
if (part < 1) part = 1; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
101 |
// When we are done, stop it |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
102 |
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
|
103 |
|
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
145
diff
changeset
|
104 |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
105 |
if (PathFinderInfo->rail_or_road) { |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
106 |
// Tunnel code |
2366 | 107 |
if ((AI_PATHFINDER_FLAG_TUNNEL & route_extra[part]) != 0) { |
2682
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2639
diff
changeset
|
108 |
cost += AI_DoCommand(route[part], 0, 0, flag, CMD_BUILD_TUNNEL); |
2366 | 109 |
PathFinderInfo->position++; |
110 |
// TODO: problems! |
|
111 |
if (CmdFailed(cost)) { |
|
112 |
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
|
113 |
return 0; |
2366 | 114 |
} |
115 |
return cost; |
|
116 |
} |
|
117 |
// Bridge code |
|
118 |
if ((AI_PATHFINDER_FLAG_BRIDGE & route_extra[part]) != 0) { |
|
119 |
cost += AiNew_Build_Bridge(p, route[part], route[part-1], flag); |
|
120 |
PathFinderInfo->position++; |
|
121 |
// TODO: problems! |
|
122 |
if (CmdFailed(cost)) { |
|
123 |
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
|
124 |
return 0; |
2366 | 125 |
} |
126 |
return cost; |
|
127 |
} |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
128 |
|
2366 | 129 |
// Build normal rail |
130 |
// Keep it doing till we go an other way |
|
131 |
if (route_extra[part-1] == 0 && route_extra[part] == 0) { |
|
132 |
while (route_extra[part] == 0) { |
|
133 |
// Get the current direction |
|
134 |
dir = AiNew_GetRailDirection(route[part-1], route[part], route[part+1]); |
|
135 |
// Is it the same as the last one? |
|
136 |
if (old_dir != -1 && old_dir != dir) break; |
|
137 |
old_dir = dir; |
|
138 |
// Build the tile |
|
2682
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2639
diff
changeset
|
139 |
res = AI_DoCommand(route[part], 0, dir, flag, CMD_BUILD_SINGLE_RAIL); |
2366 | 140 |
if (CmdFailed(res)) { |
141 |
// Problem.. let's just abort it all! |
|
142 |
p->ainew.state = AI_STATE_NOTHING; |
|
143 |
return 0; |
|
144 |
} |
|
145 |
cost += res; |
|
146 |
// Go to the next tile |
|
147 |
part++; |
|
148 |
// Check if it is still in range.. |
|
149 |
if (part >= PathFinderInfo->route_length - 1) break; |
|
150 |
} |
|
151 |
part--; |
|
152 |
} |
|
153 |
// We want to return the last position, so we go back one |
|
154 |
PathFinderInfo->position = part; |
|
155 |
} else { |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
156 |
// Tunnel code |
2366 | 157 |
if ((AI_PATHFINDER_FLAG_TUNNEL & route_extra[part]) != 0) { |
2682
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2639
diff
changeset
|
158 |
cost += AI_DoCommand(route[part], 0x200, 0, flag, CMD_BUILD_TUNNEL); |
2366 | 159 |
PathFinderInfo->position++; |
160 |
// TODO: problems! |
|
161 |
if (CmdFailed(cost)) { |
|
162 |
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
|
163 |
return 0; |
2366 | 164 |
} |
165 |
return cost; |
|
166 |
} |
|
167 |
// Bridge code |
|
168 |
if ((AI_PATHFINDER_FLAG_BRIDGE & route_extra[part]) != 0) { |
|
169 |
cost += AiNew_Build_Bridge(p, route[part], route[part+1], flag); |
|
170 |
PathFinderInfo->position++; |
|
171 |
// TODO: problems! |
|
172 |
if (CmdFailed(cost)) { |
|
173 |
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
|
174 |
return 0; |
2366 | 175 |
} |
176 |
return cost; |
|
177 |
} |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
178 |
|
2366 | 179 |
// Build normal road |
180 |
// Keep it doing till we go an other way |
|
181 |
// EnsureNoVehicle makes sure we don't build on a tile where a vehicle is. This way |
|
182 |
// it will wait till the vehicle is gone.. |
|
183 |
if (route_extra[part-1] == 0 && route_extra[part] == 0 && (flag != DC_EXEC || EnsureNoVehicle(route[part]))) { |
|
184 |
while (route_extra[part] == 0 && (flag != DC_EXEC || EnsureNoVehicle(route[part]))) { |
|
185 |
// Get the current direction |
|
186 |
dir = AiNew_GetRoadDirection(route[part-1], route[part], route[part+1]); |
|
187 |
// Is it the same as the last one? |
|
188 |
if (old_dir != -1 && old_dir != dir) break; |
|
189 |
old_dir = dir; |
|
190 |
// There is already some road, and it is a bridge.. don't build!!! |
|
191 |
if (!IsTileType(route[part], MP_TUNNELBRIDGE)) { |
|
192 |
// Build the tile |
|
2682
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2639
diff
changeset
|
193 |
res = AI_DoCommand(route[part], dir, 0, flag | DC_NO_WATER, CMD_BUILD_ROAD); |
2366 | 194 |
// Currently, we ignore CMD_ERRORs! |
195 |
if (CmdFailed(res) && flag == DC_EXEC && !IsTileType(route[part], MP_STREET) && !EnsureNoVehicle(route[part])) { |
|
196 |
// Problem.. let's just abort it all! |
|
197 |
DEBUG(ai,0)("Darn, the route could not be builded.. aborting!"); |
|
198 |
p->ainew.state = AI_STATE_NOTHING; |
|
199 |
return 0; |
|
200 |
} |
|
1713
d970350410b2
(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
|
201 |
|
2366 | 202 |
if (!CmdFailed(res)) cost += res; |
203 |
} |
|
204 |
// Go to the next tile |
|
205 |
part++; |
|
206 |
// Check if it is still in range.. |
|
207 |
if (part >= PathFinderInfo->route_length - 1) break; |
|
208 |
} |
|
209 |
part--; |
|
210 |
// We want to return the last position, so we go back one |
|
211 |
} |
|
212 |
if (!EnsureNoVehicle(route[part]) && flag == DC_EXEC) part--; |
|
213 |
PathFinderInfo->position = part; |
|
214 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
145
diff
changeset
|
215 |
|
2366 | 216 |
return cost; |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
217 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
218 |
|
2366 | 219 |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
220 |
// 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
|
221 |
// It returns vehicle_id or -1 if not found |
2366 | 222 |
int AiNew_PickVehicle(Player *p) |
223 |
{ |
|
224 |
if (p->ainew.tbt == AI_TRAIN) { |
|
225 |
// Not supported yet |
|
226 |
return -1; |
|
227 |
} else { |
|
228 |
int start, count, i, ret = CMD_ERROR; |
|
229 |
start = _cargoc.ai_roadveh_start[p->ainew.cargo]; |
|
230 |
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
|
231 |
|
2366 | 232 |
// Let's check it backwards.. we simply want to best engine available.. |
2549 | 233 |
for (i = start + count - 1; i >= start; i--) { |
2366 | 234 |
// Is it availiable? |
235 |
// Also, check if the reliability of the vehicle is above the AI_VEHICLE_MIN_RELIABILTY |
|
236 |
if (!HASBIT(GetEngine(i)->player_avail, _current_player) || GetEngine(i)->reliability * 100 < AI_VEHICLE_MIN_RELIABILTY << 16) continue; |
|
237 |
// Can we build it? |
|
2682
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2639
diff
changeset
|
238 |
ret = AI_DoCommand(0, i, 0, DC_QUERY_COST, CMD_BUILD_ROAD_VEH); |
2366 | 239 |
if (!CmdFailed(ret)) break; |
240 |
} |
|
241 |
// We did not find a vehicle :( |
|
2549 | 242 |
if (CmdFailed(ret)) return -1; |
2366 | 243 |
return i; |
244 |
} |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
245 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
246 |
|
2366 | 247 |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
248 |
// Builds the best vehicle possible |
1977
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1926
diff
changeset
|
249 |
int AiNew_Build_Vehicle(Player *p, TileIndex tile, byte flag) |
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1926
diff
changeset
|
250 |
{ |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
251 |
int i = AiNew_PickVehicle(p); |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
252 |
if (i == -1) return CMD_ERROR; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
145
diff
changeset
|
253 |
|
2639 | 254 |
if (p->ainew.tbt == AI_TRAIN) return CMD_ERROR; |
1713
d970350410b2
(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 |
|
2682
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2639
diff
changeset
|
256 |
return AI_DoCommand(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
4392ae3d8e31
(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
d970350410b2
(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
d970350410b2
(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) |
2682
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2639
diff
changeset
|
264 |
return AI_DoCommand(tile, 0, direction, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_TRAIN_DEPOT); |
1713
d970350410b2
(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 |
|
2682
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2639
diff
changeset
|
266 |
ret = AI_DoCommand(tile, direction, 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD_DEPOT); |
2366 | 267 |
if (CmdFailed(ret)) return ret; |
268 |
// Try to build the road from the depot |
|
2682
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2639
diff
changeset
|
269 |
ret2 = AI_DoCommand(tile + TileOffsByDir(direction), _roadbits_by_dir[direction], 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD); |
2366 | 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 |
} |