author | Darkvater |
Sun, 15 Oct 2006 17:01:19 +0000 | |
changeset 4854 | 383ef523793f |
parent 4559 | c853d2440065 |
child 5015 | 8e2f6a5df288 |
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" |
3157
40de8616c04c
(svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents:
3033
diff
changeset
|
8 |
#include "../../road_map.h" |
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
|
9 |
#include "../../tile.h" |
3946
c9e039a60682
(svn r5092) -Fix: There was a gross race condition in the AI code which made it pretty random if the AI could give a new vehicle its orders
tron
parents:
3887
diff
changeset
|
10 |
#include "../../vehicle.h" |
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
|
11 |
#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
|
12 |
#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
|
13 |
#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
|
14 |
#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
|
15 |
#include "../../variables.h" |
3359
d4316b1af327
(svn r4154) -Moved MAX_BRIDGES in bridge.h and made it an enum. This makes two drops ...
celestar
parents:
3157
diff
changeset
|
16 |
#include "../../bridge.h" |
2682
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2639
diff
changeset
|
17 |
#include "../ai.h" |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
18 |
|
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
19 |
// Build HQ |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
20 |
// Params: |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
21 |
// 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
|
22 |
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
|
23 |
{ |
2682
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2639
diff
changeset
|
24 |
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
|
25 |
return false; |
2682
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2639
diff
changeset
|
26 |
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
|
27 |
return true; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
28 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
29 |
|
2366 | 30 |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
31 |
// Build station |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
32 |
// Params: |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
33 |
// 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
|
34 |
// 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
|
35 |
// 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
|
36 |
// 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
|
37 |
// direction : the direction of the station |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
38 |
// 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
|
39 |
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
|
40 |
{ |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
41 |
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
|
42 |
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
|
43 |
|
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
|
44 |
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
|
45 |
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
|
46 |
|
2682
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2639
diff
changeset
|
47 |
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
|
48 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
49 |
|
2366 | 50 |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
51 |
// 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
|
52 |
// Params: |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
53 |
// tile_a : starting point |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
54 |
// tile_b : end point |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
55 |
// flag : flag passed to DoCommand |
1977
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1926
diff
changeset
|
56 |
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
|
57 |
{ |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
58 |
int bridge_type, bridge_len, type, type2; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
59 |
|
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
60 |
// 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
|
61 |
bridge_len = GetBridgeLength(tile_a, tile_b); |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
62 |
type = type2 = 0; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
63 |
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
|
64 |
if (CheckBridge_Stuff(bridge_type, bridge_len)) { |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
65 |
type2 = type; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
66 |
type = bridge_type; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
67 |
// We found two bridges, exit |
2366 | 68 |
if (type2 != 0) break; |
110
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 |
} |
4000
bab1ebc37da0
(svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents:
3946
diff
changeset
|
71 |
// There is only one bridge that can be built |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
72 |
if (type2 == 0 && type != 0) type2 = type; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
73 |
|
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
74 |
// Now, simply, build the bridge! |
4000
bab1ebc37da0
(svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents:
3946
diff
changeset
|
75 |
if (p->ainew.tbt == AI_TRAIN) { |
bab1ebc37da0
(svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents:
3946
diff
changeset
|
76 |
return AI_DoCommand(tile_a, tile_b, (0x00 << 8) + type2, flag | DC_AUTO, CMD_BUILD_BRIDGE); |
bab1ebc37da0
(svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents:
3946
diff
changeset
|
77 |
} else { |
bab1ebc37da0
(svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents:
3946
diff
changeset
|
78 |
return AI_DoCommand(tile_a, tile_b, (0x80 << 8) + type2, flag | DC_AUTO, CMD_BUILD_BRIDGE); |
bab1ebc37da0
(svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents:
3946
diff
changeset
|
79 |
} |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
80 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
81 |
|
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 |
// Build the route part by part |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
84 |
// 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
|
85 |
// 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
|
86 |
// 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
|
87 |
// |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
88 |
// Params: |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
89 |
// PathFinderInfo : Pointer to the PathFinderInfo used for AiPathFinder |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
90 |
// part : Which part we need to build |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
91 |
// |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
92 |
// TODO: skip already builded road-pieces (e.g.: cityroad) |
2366 | 93 |
int AiNew_Build_RoutePart(Player *p, Ai_PathFinderInfo *PathFinderInfo, byte flag) |
94 |
{ |
|
95 |
int part = PathFinderInfo->position; |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
96 |
byte *route_extra = PathFinderInfo->route_extra; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
97 |
TileIndex *route = PathFinderInfo->route; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
98 |
int dir; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
99 |
int old_dir = -1; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
100 |
int cost = 0; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
101 |
int res; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
102 |
// 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
|
103 |
// the first pieces and the last piece |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
104 |
if (part < 1) part = 1; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
105 |
// When we are done, stop it |
4000
bab1ebc37da0
(svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents:
3946
diff
changeset
|
106 |
if (part >= PathFinderInfo->route_length - 1) { |
bab1ebc37da0
(svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents:
3946
diff
changeset
|
107 |
PathFinderInfo->position = -2; |
bab1ebc37da0
(svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents:
3946
diff
changeset
|
108 |
return 0; |
bab1ebc37da0
(svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents:
3946
diff
changeset
|
109 |
} |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
145
diff
changeset
|
110 |
|
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
145
diff
changeset
|
111 |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
112 |
if (PathFinderInfo->rail_or_road) { |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
113 |
// Tunnel code |
2366 | 114 |
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
|
115 |
cost += AI_DoCommand(route[part], 0, 0, flag, CMD_BUILD_TUNNEL); |
2366 | 116 |
PathFinderInfo->position++; |
117 |
// TODO: problems! |
|
118 |
if (CmdFailed(cost)) { |
|
4000
bab1ebc37da0
(svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents:
3946
diff
changeset
|
119 |
DEBUG(ai,0)("[AiNew - BuildPath] We have a serious problem: tunnel could not be built!"); |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
120 |
return 0; |
2366 | 121 |
} |
122 |
return cost; |
|
123 |
} |
|
124 |
// Bridge code |
|
125 |
if ((AI_PATHFINDER_FLAG_BRIDGE & route_extra[part]) != 0) { |
|
126 |
cost += AiNew_Build_Bridge(p, route[part], route[part-1], flag); |
|
127 |
PathFinderInfo->position++; |
|
128 |
// TODO: problems! |
|
129 |
if (CmdFailed(cost)) { |
|
4000
bab1ebc37da0
(svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents:
3946
diff
changeset
|
130 |
DEBUG(ai,0)("[AiNew - BuildPath] We have a serious problem: bridge could not be built!"); |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
131 |
return 0; |
2366 | 132 |
} |
133 |
return cost; |
|
134 |
} |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
135 |
|
2366 | 136 |
// Build normal rail |
137 |
// Keep it doing till we go an other way |
|
4000
bab1ebc37da0
(svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents:
3946
diff
changeset
|
138 |
if (route_extra[part - 1] == 0 && route_extra[part] == 0) { |
2366 | 139 |
while (route_extra[part] == 0) { |
140 |
// Get the current direction |
|
141 |
dir = AiNew_GetRailDirection(route[part-1], route[part], route[part+1]); |
|
142 |
// Is it the same as the last one? |
|
143 |
if (old_dir != -1 && old_dir != dir) break; |
|
144 |
old_dir = dir; |
|
145 |
// Build the tile |
|
2682
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2639
diff
changeset
|
146 |
res = AI_DoCommand(route[part], 0, dir, flag, CMD_BUILD_SINGLE_RAIL); |
2366 | 147 |
if (CmdFailed(res)) { |
148 |
// Problem.. let's just abort it all! |
|
149 |
p->ainew.state = AI_STATE_NOTHING; |
|
150 |
return 0; |
|
151 |
} |
|
152 |
cost += res; |
|
153 |
// Go to the next tile |
|
154 |
part++; |
|
155 |
// Check if it is still in range.. |
|
156 |
if (part >= PathFinderInfo->route_length - 1) break; |
|
157 |
} |
|
158 |
part--; |
|
159 |
} |
|
160 |
// We want to return the last position, so we go back one |
|
161 |
PathFinderInfo->position = part; |
|
162 |
} else { |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
163 |
// Tunnel code |
2366 | 164 |
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
|
165 |
cost += AI_DoCommand(route[part], 0x200, 0, flag, CMD_BUILD_TUNNEL); |
2366 | 166 |
PathFinderInfo->position++; |
167 |
// TODO: problems! |
|
168 |
if (CmdFailed(cost)) { |
|
169 |
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
|
170 |
return 0; |
2366 | 171 |
} |
172 |
return cost; |
|
173 |
} |
|
174 |
// Bridge code |
|
175 |
if ((AI_PATHFINDER_FLAG_BRIDGE & route_extra[part]) != 0) { |
|
176 |
cost += AiNew_Build_Bridge(p, route[part], route[part+1], flag); |
|
177 |
PathFinderInfo->position++; |
|
178 |
// TODO: problems! |
|
179 |
if (CmdFailed(cost)) { |
|
180 |
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
|
181 |
return 0; |
2366 | 182 |
} |
183 |
return cost; |
|
184 |
} |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
185 |
|
2366 | 186 |
// Build normal road |
187 |
// Keep it doing till we go an other way |
|
188 |
// EnsureNoVehicle makes sure we don't build on a tile where a vehicle is. This way |
|
189 |
// it will wait till the vehicle is gone.. |
|
190 |
if (route_extra[part-1] == 0 && route_extra[part] == 0 && (flag != DC_EXEC || EnsureNoVehicle(route[part]))) { |
|
191 |
while (route_extra[part] == 0 && (flag != DC_EXEC || EnsureNoVehicle(route[part]))) { |
|
192 |
// Get the current direction |
|
193 |
dir = AiNew_GetRoadDirection(route[part-1], route[part], route[part+1]); |
|
194 |
// Is it the same as the last one? |
|
195 |
if (old_dir != -1 && old_dir != dir) break; |
|
196 |
old_dir = dir; |
|
197 |
// There is already some road, and it is a bridge.. don't build!!! |
|
198 |
if (!IsTileType(route[part], MP_TUNNELBRIDGE)) { |
|
199 |
// Build the tile |
|
2682
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2639
diff
changeset
|
200 |
res = AI_DoCommand(route[part], dir, 0, flag | DC_NO_WATER, CMD_BUILD_ROAD); |
2366 | 201 |
// Currently, we ignore CMD_ERRORs! |
202 |
if (CmdFailed(res) && flag == DC_EXEC && !IsTileType(route[part], MP_STREET) && !EnsureNoVehicle(route[part])) { |
|
203 |
// Problem.. let's just abort it all! |
|
204 |
DEBUG(ai,0)("Darn, the route could not be builded.. aborting!"); |
|
205 |
p->ainew.state = AI_STATE_NOTHING; |
|
206 |
return 0; |
|
207 |
} |
|
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
|
208 |
|
2366 | 209 |
if (!CmdFailed(res)) cost += res; |
210 |
} |
|
211 |
// Go to the next tile |
|
212 |
part++; |
|
213 |
// Check if it is still in range.. |
|
214 |
if (part >= PathFinderInfo->route_length - 1) break; |
|
215 |
} |
|
216 |
part--; |
|
217 |
// We want to return the last position, so we go back one |
|
218 |
} |
|
219 |
if (!EnsureNoVehicle(route[part]) && flag == DC_EXEC) part--; |
|
220 |
PathFinderInfo->position = part; |
|
221 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
145
diff
changeset
|
222 |
|
2366 | 223 |
return cost; |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
224 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
225 |
|
2366 | 226 |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
227 |
// This functions tries to find the best vehicle for this type of cargo |
3885
327cdee1f0b6
(svn r4941) Replace some ints and magic numbers by proper types and enums
tron
parents:
3359
diff
changeset
|
228 |
// It returns INVALID_ENGINE if not suitable engine is found |
327cdee1f0b6
(svn r4941) Replace some ints and magic numbers by proper types and enums
tron
parents:
3359
diff
changeset
|
229 |
EngineID AiNew_PickVehicle(Player *p) |
2366 | 230 |
{ |
231 |
if (p->ainew.tbt == AI_TRAIN) { |
|
232 |
// Not supported yet |
|
3885
327cdee1f0b6
(svn r4941) Replace some ints and magic numbers by proper types and enums
tron
parents:
3359
diff
changeset
|
233 |
return INVALID_ENGINE; |
2366 | 234 |
} else { |
3885
327cdee1f0b6
(svn r4941) Replace some ints and magic numbers by proper types and enums
tron
parents:
3359
diff
changeset
|
235 |
EngineID start = _cargoc.ai_roadveh_start[p->ainew.cargo]; |
327cdee1f0b6
(svn r4941) Replace some ints and magic numbers by proper types and enums
tron
parents:
3359
diff
changeset
|
236 |
EngineID end = start + _cargoc.ai_roadveh_count[p->ainew.cargo]; |
327cdee1f0b6
(svn r4941) Replace some ints and magic numbers by proper types and enums
tron
parents:
3359
diff
changeset
|
237 |
EngineID i; |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
238 |
|
2366 | 239 |
// Let's check it backwards.. we simply want to best engine available.. |
3885
327cdee1f0b6
(svn r4941) Replace some ints and magic numbers by proper types and enums
tron
parents:
3359
diff
changeset
|
240 |
for (i = end - 1; i >= start; i--) { |
3887
3e44ae3b1e7c
(svn r4943) uint tile -> TileIndex tile, byte player -> PlayerID player
tron
parents:
3885
diff
changeset
|
241 |
const Engine* e = GetEngine(i); |
3885
327cdee1f0b6
(svn r4941) Replace some ints and magic numbers by proper types and enums
tron
parents:
3359
diff
changeset
|
242 |
int32 ret; |
327cdee1f0b6
(svn r4941) Replace some ints and magic numbers by proper types and enums
tron
parents:
3359
diff
changeset
|
243 |
|
2366 | 244 |
// Is it availiable? |
245 |
// Also, check if the reliability of the vehicle is above the AI_VEHICLE_MIN_RELIABILTY |
|
3887
3e44ae3b1e7c
(svn r4943) uint tile -> TileIndex tile, byte player -> PlayerID player
tron
parents:
3885
diff
changeset
|
246 |
if (!HASBIT(e->player_avail, _current_player) || e->reliability * 100 < AI_VEHICLE_MIN_RELIABILTY << 16) continue; |
2366 | 247 |
// 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
|
248 |
ret = AI_DoCommand(0, i, 0, DC_QUERY_COST, CMD_BUILD_ROAD_VEH); |
3885
327cdee1f0b6
(svn r4941) Replace some ints and magic numbers by proper types and enums
tron
parents:
3359
diff
changeset
|
249 |
if (!CmdFailed(ret)) return i; |
2366 | 250 |
} |
251 |
// We did not find a vehicle :( |
|
3885
327cdee1f0b6
(svn r4941) Replace some ints and magic numbers by proper types and enums
tron
parents:
3359
diff
changeset
|
252 |
return INVALID_ENGINE; |
2366 | 253 |
} |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
254 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
255 |
|
2366 | 256 |
|
3946
c9e039a60682
(svn r5092) -Fix: There was a gross race condition in the AI code which made it pretty random if the AI could give a new vehicle its orders
tron
parents:
3887
diff
changeset
|
257 |
void CcAI(bool success, TileIndex tile, uint32 p1, uint32 p2) |
c9e039a60682
(svn r5092) -Fix: There was a gross race condition in the AI code which made it pretty random if the AI could give a new vehicle its orders
tron
parents:
3887
diff
changeset
|
258 |
{ |
c9e039a60682
(svn r5092) -Fix: There was a gross race condition in the AI code which made it pretty random if the AI could give a new vehicle its orders
tron
parents:
3887
diff
changeset
|
259 |
Player* p = GetPlayer(_current_player); |
c9e039a60682
(svn r5092) -Fix: There was a gross race condition in the AI code which made it pretty random if the AI could give a new vehicle its orders
tron
parents:
3887
diff
changeset
|
260 |
|
c9e039a60682
(svn r5092) -Fix: There was a gross race condition in the AI code which made it pretty random if the AI could give a new vehicle its orders
tron
parents:
3887
diff
changeset
|
261 |
if (success) { |
c9e039a60682
(svn r5092) -Fix: There was a gross race condition in the AI code which made it pretty random if the AI could give a new vehicle its orders
tron
parents:
3887
diff
changeset
|
262 |
p->ainew.state = AI_STATE_GIVE_ORDERS; |
c9e039a60682
(svn r5092) -Fix: There was a gross race condition in the AI code which made it pretty random if the AI could give a new vehicle its orders
tron
parents:
3887
diff
changeset
|
263 |
p->ainew.veh_id = _new_vehicle_id; |
c9e039a60682
(svn r5092) -Fix: There was a gross race condition in the AI code which made it pretty random if the AI could give a new vehicle its orders
tron
parents:
3887
diff
changeset
|
264 |
} else { |
c9e039a60682
(svn r5092) -Fix: There was a gross race condition in the AI code which made it pretty random if the AI could give a new vehicle its orders
tron
parents:
3887
diff
changeset
|
265 |
/* XXX this should be handled more gracefully */ |
c9e039a60682
(svn r5092) -Fix: There was a gross race condition in the AI code which made it pretty random if the AI could give a new vehicle its orders
tron
parents:
3887
diff
changeset
|
266 |
p->ainew.state = AI_STATE_NOTHING; |
c9e039a60682
(svn r5092) -Fix: There was a gross race condition in the AI code which made it pretty random if the AI could give a new vehicle its orders
tron
parents:
3887
diff
changeset
|
267 |
} |
c9e039a60682
(svn r5092) -Fix: There was a gross race condition in the AI code which made it pretty random if the AI could give a new vehicle its orders
tron
parents:
3887
diff
changeset
|
268 |
} |
c9e039a60682
(svn r5092) -Fix: There was a gross race condition in the AI code which made it pretty random if the AI could give a new vehicle its orders
tron
parents:
3887
diff
changeset
|
269 |
|
c9e039a60682
(svn r5092) -Fix: There was a gross race condition in the AI code which made it pretty random if the AI could give a new vehicle its orders
tron
parents:
3887
diff
changeset
|
270 |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
271 |
// Builds the best vehicle possible |
1977
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1926
diff
changeset
|
272 |
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
|
273 |
{ |
3885
327cdee1f0b6
(svn r4941) Replace some ints and magic numbers by proper types and enums
tron
parents:
3359
diff
changeset
|
274 |
EngineID i = AiNew_PickVehicle(p); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
145
diff
changeset
|
275 |
|
3885
327cdee1f0b6
(svn r4941) Replace some ints and magic numbers by proper types and enums
tron
parents:
3359
diff
changeset
|
276 |
if (i == INVALID_ENGINE) return CMD_ERROR; |
2639 | 277 |
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
|
278 |
|
3946
c9e039a60682
(svn r5092) -Fix: There was a gross race condition in the AI code which made it pretty random if the AI could give a new vehicle its orders
tron
parents:
3887
diff
changeset
|
279 |
if (flag & DC_EXEC) { |
c9e039a60682
(svn r5092) -Fix: There was a gross race condition in the AI code which made it pretty random if the AI could give a new vehicle its orders
tron
parents:
3887
diff
changeset
|
280 |
return AI_DoCommandCc(tile, i, 0, flag, CMD_BUILD_ROAD_VEH, CcAI); |
c9e039a60682
(svn r5092) -Fix: There was a gross race condition in the AI code which made it pretty random if the AI could give a new vehicle its orders
tron
parents:
3887
diff
changeset
|
281 |
} else { |
c9e039a60682
(svn r5092) -Fix: There was a gross race condition in the AI code which made it pretty random if the AI could give a new vehicle its orders
tron
parents:
3887
diff
changeset
|
282 |
return AI_DoCommand(tile, i, 0, flag, CMD_BUILD_ROAD_VEH); |
c9e039a60682
(svn r5092) -Fix: There was a gross race condition in the AI code which made it pretty random if the AI could give a new vehicle its orders
tron
parents:
3887
diff
changeset
|
283 |
} |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
284 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
285 |
|
3157
40de8616c04c
(svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents:
3033
diff
changeset
|
286 |
int AiNew_Build_Depot(Player* p, TileIndex tile, DiagDirection 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
|
287 |
{ |
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
|
288 |
int ret, ret2; |
4077
d3022f976946
(svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents:
4000
diff
changeset
|
289 |
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
|
290 |
return AI_DoCommand(tile, 0, direction, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_TRAIN_DEPOT); |
4077
d3022f976946
(svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents:
4000
diff
changeset
|
291 |
} else { |
d3022f976946
(svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents:
4000
diff
changeset
|
292 |
ret = AI_DoCommand(tile, direction, 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD_DEPOT); |
d3022f976946
(svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents:
4000
diff
changeset
|
293 |
if (CmdFailed(ret)) return ret; |
d3022f976946
(svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents:
4000
diff
changeset
|
294 |
// Try to build the road from the depot |
4559
c853d2440065
(svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents:
4077
diff
changeset
|
295 |
ret2 = AI_DoCommand(tile + TileOffsByDiagDir(direction), DiagDirToRoadBits(ReverseDiagDir(direction)), 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD); |
4077
d3022f976946
(svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents:
4000
diff
changeset
|
296 |
// If it fails, ignore it.. |
d3022f976946
(svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents:
4000
diff
changeset
|
297 |
if (CmdFailed(ret2)) return ret; |
d3022f976946
(svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents:
4000
diff
changeset
|
298 |
return ret + ret2; |
d3022f976946
(svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents:
4000
diff
changeset
|
299 |
} |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
84
diff
changeset
|
300 |
} |