author | tron |
Fri, 24 Mar 2006 08:55:08 +0000 | |
changeset 3315 | 1f65f8260092 |
parent 3179 | 774b1e6553a7 |
child 3338 | 2c60ed0dcce3 |
permissions | -rw-r--r-- |
2549 | 1 |
/* $Id$ */ |
1490
6a13118e99f5
(svn r1994) Fix: 1159456 - hopefully prevent the new AI to get stuck in an infinite loop forlorny trying to build its haven. Unintentionally bundled with trivial tidyup.
pasky
parents:
1377
diff
changeset
|
2 |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
3 |
/* |
2549 | 4 |
* This AI was created as a direct reaction to the big demand for some good AIs |
5 |
* in OTTD. Too bad it never left alpha-stage, and it is considered dead in its |
|
6 |
* current form. |
|
7 |
* By the time of writing this, we, the creator of this AI and a good friend of |
|
8 |
* mine, are designing a whole new AI-system that allows us to create AIs |
|
9 |
* easier and without all the fuzz we encountered while I was working on this |
|
10 |
* AI. By the time that system is finished, you can expect that this AI will |
|
11 |
* dissapear, because it is pretty obselete and bad programmed. |
|
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:
2365
diff
changeset
|
12 |
* |
2549 | 13 |
* Meanwhile I wish you all much fun with this AI; if you are interested as |
14 |
* AI-developer in this AI, I advise you not stare too long to some code, some |
|
15 |
* things in here really are... strange ;) But in either way: enjoy :) |
|
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:
2365
diff
changeset
|
16 |
* |
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:
2365
diff
changeset
|
17 |
* -- TrueLight :: 2005-09-01 |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
18 |
*/ |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
19 |
|
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:
2365
diff
changeset
|
20 |
#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:
2365
diff
changeset
|
21 |
#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:
2365
diff
changeset
|
22 |
#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:
2365
diff
changeset
|
23 |
#include "../../functions.h" |
3144
426b825578f9
(svn r3763) Adapt to the new 'map accessors go in foo_map.h'-scheme
tron
parents:
3071
diff
changeset
|
24 |
#include "../../road_map.h" |
3315
1f65f8260092
(svn r4079) Add GetSation{Index,ByTile}() to get the station index resp. the station from a tile
tron
parents:
3179
diff
changeset
|
25 |
#include "../../station_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:
2365
diff
changeset
|
26 |
#include "../../table/strings.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:
2365
diff
changeset
|
27 |
#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:
2365
diff
changeset
|
28 |
#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:
2365
diff
changeset
|
29 |
#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:
2365
diff
changeset
|
30 |
#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:
2365
diff
changeset
|
31 |
#include "../../town.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:
2365
diff
changeset
|
32 |
#include "../../industry.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:
2365
diff
changeset
|
33 |
#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:
2365
diff
changeset
|
34 |
#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:
2365
diff
changeset
|
35 |
#include "../../gui.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:
2365
diff
changeset
|
36 |
#include "../../depot.h" |
2682
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2635
diff
changeset
|
37 |
#include "../ai.h" |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
38 |
|
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
39 |
// This function is called after StartUp. It is the init of an AI |
2365 | 40 |
static void AiNew_State_FirstTime(Player *p) |
41 |
{ |
|
42 |
// This assert is used to protect those function from misuse |
|
43 |
// You have quickly a small mistake in the state-array |
|
44 |
// With that, everything would go wrong. Finding that, is almost impossible |
|
45 |
// With this assert, that problem can never happen. |
|
46 |
assert(p->ainew.state == AI_STATE_FIRST_TIME); |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
47 |
// We first have to init some things |
145 | 48 |
|
2684
8aba54c245cc
(svn r3226) -Fix: GPMI implementation had minor glitches
truelight
parents:
2682
diff
changeset
|
49 |
if (_current_player == 1 || _ai.network_client) { |
2749
46aba09ca365
(svn r3294) - Fix: use INVALID_STRING_ID instead of -1.
Darkvater
parents:
2737
diff
changeset
|
50 |
ShowErrorMessage(INVALID_STRING_ID, TEMP_AI_IN_PROGRESS, 0, 0); |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
51 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
52 |
|
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
53 |
// The PathFinder (AyStar) |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
54 |
// TODO: Maybe when an AI goes bankrupt, this is de-init |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
55 |
// or when coming from a savegame.. should be checked out! |
2365 | 56 |
p->ainew.path_info.start_tile_tl = 0; |
57 |
p->ainew.path_info.start_tile_br = 0; |
|
58 |
p->ainew.path_info.end_tile_tl = 0; |
|
59 |
p->ainew.path_info.end_tile_br = 0; |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
60 |
p->ainew.pathfinder = new_AyStar_AiPathFinder(12, &p->ainew.path_info); |
145 | 61 |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
62 |
p->ainew.idle = 0; |
145 | 63 |
p->ainew.last_vehiclecheck_date = _date; |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
64 |
|
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
65 |
// We ALWAYS start with a bus route.. just some basic money ;) |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
66 |
p->ainew.action = AI_ACTION_BUS_ROUTE; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
67 |
|
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
68 |
// Let's popup the news, and after that, start building.. |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
69 |
p->ainew.state = AI_STATE_WAKE_UP; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
70 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
71 |
|
2365 | 72 |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
73 |
// This function just waste some time |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
74 |
// It keeps it more real. The AI can build on such tempo no normal user |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
75 |
// can ever keep up with that. The competitor_speed already delays a bit |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
76 |
// but after the AI finished a track it really needs to go to sleep. |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
77 |
// |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
78 |
// Let's say, we sleep between one and three days if the AI is put on Very Fast. |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
79 |
// This means that on Very Slow it will be between 16 and 48 days.. slow enough? |
2365 | 80 |
static void AiNew_State_Nothing(Player *p) |
81 |
{ |
|
82 |
assert(p->ainew.state == AI_STATE_NOTHING); |
|
83 |
// If we are done idling, start over again |
|
2682
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2635
diff
changeset
|
84 |
if (p->ainew.idle == 0) p->ainew.idle = AI_RandomRange(DAY_TICKS * 2) + DAY_TICKS; |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
85 |
if (--p->ainew.idle == 0) { |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
86 |
// We are done idling.. what you say? Let's do something! |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
87 |
// I mean.. the next tick ;) |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
88 |
p->ainew.state = AI_STATE_WAKE_UP; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
89 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
90 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
91 |
|
2365 | 92 |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
93 |
// This function picks out a task we are going to do. |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
94 |
// Currently supported: |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
95 |
// - Make new route |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
96 |
// - Check route |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
97 |
// - Build HQ |
2365 | 98 |
static void AiNew_State_WakeUp(Player *p) |
99 |
{ |
|
1490
6a13118e99f5
(svn r1994) Fix: 1159456 - hopefully prevent the new AI to get stuck in an infinite loop forlorny trying to build its haven. Unintentionally bundled with trivial tidyup.
pasky
parents:
1377
diff
changeset
|
100 |
int32 money; |
6a13118e99f5
(svn r1994) Fix: 1159456 - hopefully prevent the new AI to get stuck in an infinite loop forlorny trying to build its haven. Unintentionally bundled with trivial tidyup.
pasky
parents:
1377
diff
changeset
|
101 |
int c; |
6a13118e99f5
(svn r1994) Fix: 1159456 - hopefully prevent the new AI to get stuck in an infinite loop forlorny trying to build its haven. Unintentionally bundled with trivial tidyup.
pasky
parents:
1377
diff
changeset
|
102 |
assert(p->ainew.state == AI_STATE_WAKE_UP); |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
103 |
// First, check if we have a HQ |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
104 |
if (p->location_of_house == 0) { |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
105 |
// We have no HQ yet, build one on a random place |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
106 |
// Random till we found a place for it! |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
107 |
// TODO: this should not be on a random place.. |
2682
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2635
diff
changeset
|
108 |
AiNew_Build_CompanyHQ(p, AI_Random() % MapSize()); |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
109 |
// Enough for now, but we want to come back here the next time |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
110 |
// so we do not change any status |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
111 |
return; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
112 |
} |
145 | 113 |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
114 |
money = p->player_money - AI_MINIMUM_MONEY; |
145 | 115 |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
116 |
// Let's pick an action! |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
117 |
if (p->ainew.action == AI_ACTION_NONE) { |
2682
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2635
diff
changeset
|
118 |
c = AI_Random() & 0xFF; |
2365 | 119 |
if (p->current_loan > 0 && |
120 |
p->old_economy[1].income > AI_MINIMUM_INCOME_FOR_LOAN && |
|
121 |
c < 10) { |
|
122 |
p->ainew.action = AI_ACTION_REPAY_LOAN; |
|
145 | 123 |
} else if (p->ainew.last_vehiclecheck_date + AI_DAYS_BETWEEN_VEHICLE_CHECKS < _date) { |
124 |
// Check all vehicles once in a while |
|
125 |
p->ainew.action = AI_ACTION_CHECK_ALL_VEHICLES; |
|
126 |
p->ainew.last_vehiclecheck_date = _date; |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
127 |
} else if (c < 100 && !_patches.ai_disable_veh_roadveh) { |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
128 |
// Do we have any spots for road-vehicles left open? |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
129 |
if (GetFreeUnitNumber(VEH_Road) <= _patches.max_roadveh) { |
2365 | 130 |
if (c < 85) |
131 |
p->ainew.action = AI_ACTION_TRUCK_ROUTE; |
|
132 |
else |
|
133 |
p->ainew.action = AI_ACTION_BUS_ROUTE; |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
134 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
135 |
}/* else if (c < 200 && !_patches.ai_disable_veh_train) { |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
136 |
if (GetFreeUnitNumber(VEH_Train) <= _patches.max_trains) { |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
137 |
p->ainew.action = AI_ACTION_TRAIN_ROUTE; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
138 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
139 |
}*/ |
145 | 140 |
|
141 |
p->ainew.counter = 0; |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
142 |
} |
145 | 143 |
|
144 |
if (p->ainew.counter++ > AI_MAX_TRIES_FOR_SAME_ROUTE) { |
|
145 |
p->ainew.action = AI_ACTION_NONE; |
|
146 |
return; |
|
147 |
} |
|
148 |
||
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
149 |
if (_patches.ai_disable_veh_roadveh && ( |
2365 | 150 |
p->ainew.action == AI_ACTION_BUS_ROUTE || |
151 |
p->ainew.action == AI_ACTION_TRUCK_ROUTE |
|
152 |
)) { |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
153 |
p->ainew.action = AI_ACTION_NONE; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
154 |
return; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
155 |
} |
145 | 156 |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
157 |
if (_patches.ai_disable_veh_roadveh && ( |
2365 | 158 |
p->ainew.action == AI_ACTION_BUS_ROUTE || |
159 |
p->ainew.action == AI_ACTION_TRUCK_ROUTE |
|
160 |
)) { |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
161 |
p->ainew.action = AI_ACTION_NONE; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
162 |
return; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
163 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
164 |
|
2365 | 165 |
if (p->ainew.action == AI_ACTION_REPAY_LOAN && |
166 |
money > AI_MINIMUM_LOAN_REPAY_MONEY) { |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
167 |
// We start repaying some money.. |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
168 |
p->ainew.state = AI_STATE_REPAY_MONEY; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
169 |
return; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
170 |
} |
145 | 171 |
|
172 |
if (p->ainew.action == AI_ACTION_CHECK_ALL_VEHICLES) { |
|
173 |
p->ainew.state = AI_STATE_CHECK_ALL_VEHICLES; |
|
174 |
return; |
|
175 |
} |
|
176 |
||
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
177 |
// It is useless to start finding a route if we don't have enough money |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
178 |
// to build the route anyway.. |
2365 | 179 |
if (p->ainew.action == AI_ACTION_BUS_ROUTE && |
180 |
money > AI_MINIMUM_BUS_ROUTE_MONEY) { |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
181 |
if (GetFreeUnitNumber(VEH_Road) > _patches.max_roadveh) { |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
182 |
p->ainew.action = AI_ACTION_NONE; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
183 |
return; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
184 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
185 |
p->ainew.cargo = AI_NEED_CARGO; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
186 |
p->ainew.state = AI_STATE_LOCATE_ROUTE; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
187 |
p->ainew.tbt = AI_BUS; // Bus-route |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
188 |
return; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
189 |
} |
2365 | 190 |
if (p->ainew.action == AI_ACTION_TRUCK_ROUTE && |
191 |
money > AI_MINIMUM_TRUCK_ROUTE_MONEY) { |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
192 |
if (GetFreeUnitNumber(VEH_Road) > _patches.max_roadveh) { |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
193 |
p->ainew.action = AI_ACTION_NONE; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
194 |
return; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
195 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
196 |
p->ainew.cargo = AI_NEED_CARGO; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
197 |
p->ainew.last_id = 0; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
198 |
p->ainew.state = AI_STATE_LOCATE_ROUTE; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
199 |
p->ainew.tbt = AI_TRUCK; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
200 |
return; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
201 |
} |
145 | 202 |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
203 |
p->ainew.state = AI_STATE_NOTHING; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
204 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
205 |
|
2365 | 206 |
|
207 |
static void AiNew_State_ActionDone(Player *p) |
|
208 |
{ |
|
1490
6a13118e99f5
(svn r1994) Fix: 1159456 - hopefully prevent the new AI to get stuck in an infinite loop forlorny trying to build its haven. Unintentionally bundled with trivial tidyup.
pasky
parents:
1377
diff
changeset
|
209 |
p->ainew.action = AI_ACTION_NONE; |
6a13118e99f5
(svn r1994) Fix: 1159456 - hopefully prevent the new AI to get stuck in an infinite loop forlorny trying to build its haven. Unintentionally bundled with trivial tidyup.
pasky
parents:
1377
diff
changeset
|
210 |
p->ainew.state = AI_STATE_NOTHING; |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
211 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
212 |
|
2365 | 213 |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
214 |
// Check if a city or industry is good enough to start a route there |
2365 | 215 |
static bool AiNew_Check_City_or_Industry(Player *p, int ic, byte type) |
216 |
{ |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
217 |
if (type == AI_CITY) { |
919
b0d6c7642f99
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
900
diff
changeset
|
218 |
Town *t = GetTown(ic); |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
219 |
Station *st; |
1377
2a418162176e
(svn r1881) -Fix: [ 1119308 ] Max passengers / mail variables are now 32 bit
celestar
parents:
1330
diff
changeset
|
220 |
uint count = 0; |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
221 |
int j = 0; |
145 | 222 |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
223 |
// We don't like roadconstructions, don't even true such a city |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
224 |
if (t->road_build_months != 0) return false; |
145 | 225 |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
226 |
// Check if the rating in a city is high enough |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
227 |
// If not, take a chance if we want to continue |
2682
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2635
diff
changeset
|
228 |
if (t->ratings[_current_player] < 0 && AI_CHANCE16(1,4)) return false; |
145 | 229 |
|
2682
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2635
diff
changeset
|
230 |
if (t->max_pass - t->act_pass < AI_CHECKCITY_NEEDED_CARGO && !AI_CHANCE16(1,AI_CHECKCITY_CITY_CHANCE)) return false; |
145 | 231 |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
232 |
// Check if we have build a station in this town the last 6 months |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
233 |
// else we don't do it. This is done, because stat updates can be slow |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
234 |
// and sometimes it takes up to 4 months before the stats are corectly. |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
235 |
// This way we don't get 12 busstations in one city of 100 population ;) |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
236 |
FOR_ALL_STATIONS(st) { |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
237 |
// Is it an active station |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
238 |
if (st->xy == 0) continue; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
239 |
// Do we own it? |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
240 |
if (st->owner == _current_player) { |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
241 |
// Are we talking busses? |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
242 |
if (p->ainew.tbt == AI_BUS && (FACIL_BUS_STOP & st->facilities) != FACIL_BUS_STOP) continue; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
243 |
// Is it the same city as we are in now? |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
244 |
if (st->town != t) continue; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
245 |
// When was this station build? |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
246 |
if (_date - st->build_date < AI_CHECKCITY_DATE_BETWEEN) return false; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
247 |
// Cound the amount of stations in this city that we own |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
248 |
count++; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
249 |
} else { |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
250 |
// We do not own it, request some info about the station |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
251 |
// we want to know if this station gets the same good. If so, |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
252 |
// we want to know its rating. If it is too high, we are not going |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
253 |
// to build there |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
254 |
if (!st->goods[CT_PASSENGERS].last_speed) continue; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
255 |
// Is it around our city |
1245
768d9bc95aaa
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1209
diff
changeset
|
256 |
if (DistanceManhattan(st->xy, t->xy) > 10) continue; |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
257 |
// It does take this cargo.. what is his rating? |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
258 |
if (st->goods[CT_PASSENGERS].rating < AI_CHECKCITY_CARGO_RATING) continue; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
259 |
j++; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
260 |
// When this is the first station, we build a second with no problem ;) |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
261 |
if (j == 1) continue; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
262 |
// The rating is high.. second station... |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
263 |
// a little chance that we still continue |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
264 |
// But if there are 3 stations of this size, we never go on... |
2682
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2635
diff
changeset
|
265 |
if (j == 2 && AI_CHANCE16(1, AI_CHECKCITY_CARGO_RATING_CHANCE)) continue; |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
266 |
// We don't like this station :( |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
267 |
return false; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
268 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
269 |
} |
145 | 270 |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
271 |
// We are about to add one... |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
272 |
count++; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
273 |
// Check if we the city can provide enough cargo for this amount of stations.. |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
274 |
if (count * AI_CHECKCITY_CARGO_PER_STATION > t->max_pass) return false; |
145 | 275 |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
276 |
// All check are okay, so we can build here! |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
277 |
return true; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
278 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
279 |
if (type == AI_INDUSTRY) { |
919
b0d6c7642f99
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
900
diff
changeset
|
280 |
Industry *i = GetIndustry(ic); |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
281 |
Station *st; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
282 |
int count = 0; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
283 |
int j = 0; |
145 | 284 |
|
2682
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2635
diff
changeset
|
285 |
if (i->town != NULL && i->town->ratings[_current_player] < 0 && AI_CHANCE16(1,4)) return false; |
145 | 286 |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
287 |
// No limits on delevering stations! |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
288 |
// Or for industry that does not give anything yet |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
289 |
if (i->produced_cargo[0] == 0xFF || i->total_production[0] == 0) return true; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
290 |
|
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
291 |
if (i->total_production[0] - i->total_transported[0] < AI_CHECKCITY_NEEDED_CARGO) return false; |
145 | 292 |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
293 |
// Check if we have build a station in this town the last 6 months |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
294 |
// else we don't do it. This is done, because stat updates can be slow |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
295 |
// and sometimes it takes up to 4 months before the stats are corectly. |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
296 |
FOR_ALL_STATIONS(st) { |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
297 |
// Is it an active station |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
298 |
if (st->xy == 0) continue; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
299 |
|
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
300 |
// Do we own it? |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
301 |
if (st->owner == _current_player) { |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
302 |
// Are we talking trucks? |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
303 |
if (p->ainew.tbt == AI_TRUCK && (FACIL_TRUCK_STOP & st->facilities) != FACIL_TRUCK_STOP) continue; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
304 |
// Is it the same city as we are in now? |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
305 |
if (st->town != i->town) continue; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
306 |
// When was this station build? |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
307 |
if (_date - st->build_date < AI_CHECKCITY_DATE_BETWEEN) return false; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
308 |
// Cound the amount of stations in this city that we own |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
309 |
count++; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
310 |
} else { |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
311 |
// We do not own it, request some info about the station |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
312 |
// we want to know if this station gets the same good. If so, |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
313 |
// we want to know its rating. If it is too high, we are not going |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
314 |
// to build there |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
315 |
if (i->produced_cargo[0] == 0xFF) continue; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
316 |
// It does not take this cargo |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
317 |
if (!st->goods[i->produced_cargo[0]].last_speed) continue; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
318 |
// Is it around our industry |
1245
768d9bc95aaa
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1209
diff
changeset
|
319 |
if (DistanceManhattan(st->xy, i->xy) > 5) continue; |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
320 |
// It does take this cargo.. what is his rating? |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
321 |
if (st->goods[i->produced_cargo[0]].rating < AI_CHECKCITY_CARGO_RATING) continue; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
322 |
j++; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
323 |
// The rating is high.. a little chance that we still continue |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
324 |
// But if there are 2 stations of this size, we never go on... |
2682
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2635
diff
changeset
|
325 |
if (j == 1 && AI_CHANCE16(1, AI_CHECKCITY_CARGO_RATING_CHANCE)) continue; |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
326 |
// We don't like this station :( |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
327 |
return false; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
328 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
329 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
330 |
|
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
331 |
// We are about to add one... |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
332 |
count++; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
333 |
// Check if we the city can provide enough cargo for this amount of stations.. |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
334 |
if (count * AI_CHECKCITY_CARGO_PER_STATION > i->total_production[0]) return false; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
335 |
|
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
336 |
// All check are okay, so we can build here! |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
337 |
return true; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
338 |
} |
145 | 339 |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
340 |
return true; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
341 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
342 |
|
145 | 343 |
|
2365 | 344 |
// This functions tries to locate a good route |
345 |
static void AiNew_State_LocateRoute(Player *p) |
|
346 |
{ |
|
347 |
assert(p->ainew.state == AI_STATE_LOCATE_ROUTE); |
|
348 |
// For now, we only support PASSENGERS, CITY and BUSSES |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
349 |
|
2365 | 350 |
// We don't have a route yet |
351 |
if (p->ainew.cargo == AI_NEED_CARGO) { |
|
352 |
p->ainew.new_cost = 0; // No cost yet |
|
353 |
p->ainew.temp = -1; |
|
354 |
// Reset the counter |
|
355 |
p->ainew.counter = 0; |
|
356 |
||
357 |
p->ainew.from_ic = -1; |
|
358 |
p->ainew.to_ic = -1; |
|
359 |
if (p->ainew.tbt == AI_BUS) { |
|
360 |
// For now we only have a passenger route |
|
361 |
p->ainew.cargo = CT_PASSENGERS; |
|
362 |
||
363 |
// Find a route to cities |
|
364 |
p->ainew.from_type = AI_CITY; |
|
365 |
p->ainew.to_type = AI_CITY; |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
366 |
} else if (p->ainew.tbt == AI_TRUCK) { |
2365 | 367 |
p->ainew.cargo = AI_NO_CARGO; |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
368 |
|
2365 | 369 |
p->ainew.from_type = AI_INDUSTRY; |
370 |
p->ainew.to_type = AI_INDUSTRY; |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
371 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
372 |
|
2365 | 373 |
// Now we are doing initing, we wait one tick |
374 |
return; |
|
375 |
} |
|
145 | 376 |
|
2365 | 377 |
// Increase the counter and abort if it is taking too long! |
378 |
p->ainew.counter++; |
|
379 |
if (p->ainew.counter > AI_LOCATE_ROUTE_MAX_COUNTER) { |
|
380 |
// Switch back to doing nothing! |
|
381 |
p->ainew.state = AI_STATE_NOTHING; |
|
382 |
return; |
|
383 |
} |
|
145 | 384 |
|
2365 | 385 |
// We are going to locate a city from where we are going to connect |
386 |
if (p->ainew.from_ic == -1) { |
|
387 |
if (p->ainew.temp == -1) { |
|
388 |
// First, we pick a random spot to search from |
|
389 |
if (p->ainew.from_type == AI_CITY) |
|
2682
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2635
diff
changeset
|
390 |
p->ainew.temp = AI_RandomRange(_total_towns); |
2365 | 391 |
else |
2682
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2635
diff
changeset
|
392 |
p->ainew.temp = AI_RandomRange(_total_industries); |
2365 | 393 |
} |
145 | 394 |
|
2365 | 395 |
if (!AiNew_Check_City_or_Industry(p, p->ainew.temp, p->ainew.from_type)) { |
396 |
// It was not a valid city |
|
397 |
// increase the temp with one, and return. We will come back later here |
|
398 |
// to try again |
|
399 |
p->ainew.temp++; |
|
400 |
if (p->ainew.from_type == AI_CITY) { |
|
401 |
if (p->ainew.temp >= (int)_total_towns) p->ainew.temp = 0; |
|
402 |
} else { |
|
403 |
if (p->ainew.temp >= _total_industries) p->ainew.temp = 0; |
|
404 |
} |
|
145 | 405 |
|
2365 | 406 |
// Don't do an attempt if we are trying the same id as the last time... |
407 |
if (p->ainew.last_id == p->ainew.temp) return; |
|
408 |
p->ainew.last_id = p->ainew.temp; |
|
409 |
||
410 |
return; |
|
411 |
} |
|
412 |
||
413 |
// We found a good city/industry, save the data of it |
|
414 |
p->ainew.from_ic = p->ainew.temp; |
|
415 |
||
416 |
// Start the next tick with finding a to-city |
|
417 |
p->ainew.temp = -1; |
|
418 |
return; |
|
419 |
} |
|
420 |
||
421 |
// Find a to-city |
|
422 |
if (p->ainew.temp == -1) { |
|
423 |
// First, we pick a random spot to search to |
|
424 |
if (p->ainew.to_type == AI_CITY) |
|
2682
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2635
diff
changeset
|
425 |
p->ainew.temp = AI_RandomRange(_total_towns); |
2365 | 426 |
else |
2682
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2635
diff
changeset
|
427 |
p->ainew.temp = AI_RandomRange(_total_industries); |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
428 |
} |
145 | 429 |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
430 |
// The same city is not allowed |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
431 |
// Also check if the city is valid |
2365 | 432 |
if (p->ainew.temp != p->ainew.from_ic && AiNew_Check_City_or_Industry(p, p->ainew.temp, p->ainew.to_type)) { |
433 |
// Maybe it is valid.. |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
434 |
|
2365 | 435 |
// We need to know if they are not to far apart from eachother.. |
436 |
// We do that by checking how much cargo we have to move and how long the route |
|
437 |
// is. |
|
145 | 438 |
|
2365 | 439 |
if (p->ainew.from_type == AI_CITY && p->ainew.tbt == AI_BUS) { |
440 |
int max_cargo = GetTown(p->ainew.from_ic)->max_pass + GetTown(p->ainew.temp)->max_pass; |
|
441 |
max_cargo -= GetTown(p->ainew.from_ic)->act_pass + GetTown(p->ainew.temp)->act_pass; |
|
442 |
// max_cargo is now the amount of cargo we can move between the two cities |
|
443 |
// If it is more than the distance, we allow it |
|
444 |
if (DistanceManhattan(GetTown(p->ainew.from_ic)->xy, GetTown(p->ainew.temp)->xy) <= max_cargo * AI_LOCATEROUTE_BUS_CARGO_DISTANCE) { |
|
445 |
// We found a good city/industry, save the data of it |
|
446 |
p->ainew.to_ic = p->ainew.temp; |
|
447 |
p->ainew.state = AI_STATE_FIND_STATION; |
|
448 |
||
449 |
DEBUG(ai,1)( |
|
450 |
"[AiNew - LocateRoute] Found bus-route of %d tiles long (from %d to %d)", |
|
451 |
DistanceManhattan(GetTown(p->ainew.from_ic)->xy, GetTown(p->ainew.temp)->xy), |
|
452 |
p->ainew.from_ic, |
|
453 |
p->ainew.temp |
|
454 |
); |
|
455 |
||
456 |
p->ainew.from_tile = 0; |
|
457 |
p->ainew.to_tile = 0; |
|
458 |
||
459 |
return; |
|
460 |
} |
|
461 |
} else if (p->ainew.tbt == AI_TRUCK) { |
|
462 |
bool found = false; |
|
463 |
int max_cargo = 0; |
|
464 |
int i; |
|
465 |
// TODO: in max_cargo, also check other cargo (beside [0]) |
|
466 |
// First we check if the from_ic produces cargo that this ic accepts |
|
467 |
if (GetIndustry(p->ainew.from_ic)->produced_cargo[0] != 0xFF && GetIndustry(p->ainew.from_ic)->total_production[0] != 0) { |
|
468 |
for (i=0;i<3;i++) { |
|
469 |
if (GetIndustry(p->ainew.temp)->accepts_cargo[i] == 0xFF) break; |
|
919
b0d6c7642f99
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
900
diff
changeset
|
470 |
if (GetIndustry(p->ainew.from_ic)->produced_cargo[0] == GetIndustry(p->ainew.temp)->accepts_cargo[i]) { |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
471 |
// Found a compatbiel industry |
919
b0d6c7642f99
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
900
diff
changeset
|
472 |
max_cargo = GetIndustry(p->ainew.from_ic)->total_production[0] - GetIndustry(p->ainew.from_ic)->total_transported[0]; |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
473 |
found = true; |
2365 | 474 |
p->ainew.from_deliver = true; |
475 |
p->ainew.to_deliver = false; |
|
476 |
break; |
|
477 |
} |
|
478 |
} |
|
479 |
} |
|
480 |
if (!found && GetIndustry(p->ainew.temp)->produced_cargo[0] != 0xFF && GetIndustry(p->ainew.temp)->total_production[0] != 0) { |
|
481 |
// If not check if the current ic produces cargo that the from_ic accepts |
|
482 |
for (i=0;i<3;i++) { |
|
483 |
if (GetIndustry(p->ainew.from_ic)->accepts_cargo[i] == 0xFF) break; |
|
919
b0d6c7642f99
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
900
diff
changeset
|
484 |
if (GetIndustry(p->ainew.temp)->produced_cargo[0] == GetIndustry(p->ainew.from_ic)->accepts_cargo[i]) { |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
485 |
// Found a compatbiel industry |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
486 |
found = true; |
919
b0d6c7642f99
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
900
diff
changeset
|
487 |
max_cargo = GetIndustry(p->ainew.temp)->total_production[0] - GetIndustry(p->ainew.from_ic)->total_transported[0]; |
2365 | 488 |
p->ainew.from_deliver = false; |
489 |
p->ainew.to_deliver = true; |
|
490 |
break; |
|
491 |
} |
|
492 |
} |
|
493 |
} |
|
494 |
if (found) { |
|
495 |
// Yeah, they are compatible!!! |
|
496 |
// Check the length against the amount of goods |
|
497 |
if (DistanceManhattan(GetIndustry(p->ainew.from_ic)->xy, GetIndustry(p->ainew.temp)->xy) > AI_LOCATEROUTE_TRUCK_MIN_DISTANCE && |
|
498 |
DistanceManhattan(GetIndustry(p->ainew.from_ic)->xy, GetIndustry(p->ainew.temp)->xy) <= max_cargo * AI_LOCATEROUTE_TRUCK_CARGO_DISTANCE) { |
|
499 |
p->ainew.to_ic = p->ainew.temp; |
|
500 |
if (p->ainew.from_deliver) { |
|
501 |
p->ainew.cargo = GetIndustry(p->ainew.from_ic)->produced_cargo[0]; |
|
502 |
} else { |
|
503 |
p->ainew.cargo = GetIndustry(p->ainew.temp)->produced_cargo[0]; |
|
504 |
} |
|
505 |
p->ainew.state = AI_STATE_FIND_STATION; |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
506 |
|
2365 | 507 |
DEBUG(ai,1)( |
508 |
"[AiNew - LocateRoute] Found truck-route of %d tiles long (from %d to %d)", |
|
509 |
DistanceManhattan(GetIndustry(p->ainew.from_ic)->xy, GetIndustry(p->ainew.temp)->xy), |
|
510 |
p->ainew.from_ic, |
|
511 |
p->ainew.temp |
|
512 |
); |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
513 |
|
2365 | 514 |
p->ainew.from_tile = 0; |
515 |
p->ainew.to_tile = 0; |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
516 |
|
2365 | 517 |
return; |
518 |
} |
|
519 |
} |
|
520 |
} |
|
521 |
} |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
522 |
|
1260
c60e76928e5c
(svn r1764) -Add: dynamic towns, you can now have up to 64k towns (let me know when
truelight
parents:
1253
diff
changeset
|
523 |
// It was not a valid city |
c60e76928e5c
(svn r1764) -Add: dynamic towns, you can now have up to 64k towns (let me know when
truelight
parents:
1253
diff
changeset
|
524 |
// increase the temp with one, and return. We will come back later here |
c60e76928e5c
(svn r1764) -Add: dynamic towns, you can now have up to 64k towns (let me know when
truelight
parents:
1253
diff
changeset
|
525 |
// to try again |
c60e76928e5c
(svn r1764) -Add: dynamic towns, you can now have up to 64k towns (let me know when
truelight
parents:
1253
diff
changeset
|
526 |
p->ainew.temp++; |
c60e76928e5c
(svn r1764) -Add: dynamic towns, you can now have up to 64k towns (let me know when
truelight
parents:
1253
diff
changeset
|
527 |
if (p->ainew.to_type == AI_CITY) { |
c60e76928e5c
(svn r1764) -Add: dynamic towns, you can now have up to 64k towns (let me know when
truelight
parents:
1253
diff
changeset
|
528 |
if (p->ainew.temp >= (int)_total_towns) p->ainew.temp = 0; |
c60e76928e5c
(svn r1764) -Add: dynamic towns, you can now have up to 64k towns (let me know when
truelight
parents:
1253
diff
changeset
|
529 |
} else { |
c60e76928e5c
(svn r1764) -Add: dynamic towns, you can now have up to 64k towns (let me know when
truelight
parents:
1253
diff
changeset
|
530 |
if (p->ainew.temp >= _total_industries) p->ainew.temp = 0; |
c60e76928e5c
(svn r1764) -Add: dynamic towns, you can now have up to 64k towns (let me know when
truelight
parents:
1253
diff
changeset
|
531 |
} |
145 | 532 |
|
2365 | 533 |
// Don't do an attempt if we are trying the same id as the last time... |
534 |
if (p->ainew.last_id == p->ainew.temp) return; |
|
535 |
p->ainew.last_id = p->ainew.temp; |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
536 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
537 |
|
2365 | 538 |
|
826 | 539 |
// Check if there are not more than a certain amount of vehicles pointed to a certain |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
540 |
// station. This to prevent 10 busses going to one station, which gives... problems ;) |
2365 | 541 |
static bool AiNew_CheckVehicleStation(Player *p, Station *st) |
542 |
{ |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
543 |
int count = 0; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
544 |
Vehicle *v; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
545 |
|
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
546 |
// Also check if we don't have already a lot of busses to this city... |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
547 |
FOR_ALL_VEHICLES(v) { |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
548 |
if (v->owner == _current_player) { |
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
926
diff
changeset
|
549 |
const Order *order; |
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
926
diff
changeset
|
550 |
|
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
926
diff
changeset
|
551 |
FOR_VEHICLE_ORDERS(v, order) { |
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
926
diff
changeset
|
552 |
if (order->type == OT_GOTO_STATION && GetStation(order->station) == st) { |
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
926
diff
changeset
|
553 |
// This vehicle has this city in its list |
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
926
diff
changeset
|
554 |
count++; |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
555 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
556 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
557 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
558 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
559 |
|
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
560 |
if (count > AI_CHECK_MAX_VEHICLE_PER_STATION) return false; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
561 |
return true; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
562 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
563 |
|
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
564 |
// This function finds a good spot for a station |
2365 | 565 |
static void AiNew_State_FindStation(Player *p) |
566 |
{ |
|
567 |
TileIndex tile; |
|
568 |
Station *st; |
|
569 |
int i, count = 0; |
|
570 |
TileIndex new_tile = 0; |
|
571 |
byte direction = 0; |
|
572 |
Town *town = NULL; |
|
573 |
Industry *industry = NULL; |
|
574 |
assert(p->ainew.state == AI_STATE_FIND_STATION); |
|
145 | 575 |
|
2365 | 576 |
if (p->ainew.from_tile == 0) { |
577 |
// First we scan for a station in the from-city |
|
578 |
if (p->ainew.from_type == AI_CITY) { |
|
579 |
town = GetTown(p->ainew.from_ic); |
|
580 |
tile = town->xy; |
|
581 |
} else { |
|
582 |
industry = GetIndustry(p->ainew.from_ic); |
|
583 |
tile = industry->xy; |
|
584 |
} |
|
585 |
} else if (p->ainew.to_tile == 0) { |
|
586 |
// Second we scan for a station in the to-city |
|
587 |
if (p->ainew.to_type == AI_CITY) { |
|
588 |
town = GetTown(p->ainew.to_ic); |
|
589 |
tile = town->xy; |
|
590 |
} else { |
|
591 |
industry = GetIndustry(p->ainew.to_ic); |
|
592 |
tile = industry->xy; |
|
593 |
} |
|
594 |
} else { |
|
595 |
// Unsupported request |
|
596 |
// Go to FIND_PATH |
|
597 |
p->ainew.temp = -1; |
|
598 |
p->ainew.state = AI_STATE_FIND_PATH; |
|
599 |
return; |
|
600 |
} |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
601 |
|
2365 | 602 |
// First, we are going to look at the stations that already exist inside the city |
603 |
// If there is enough cargo left in the station, we take that station |
|
604 |
// If that is not possible, and there are more than 2 stations in the city, abort |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
605 |
i = AiNew_PickVehicle(p); |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
606 |
// Euhmz, this should not happen _EVER_ |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
607 |
// Quit finding a route... |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
608 |
if (i == -1) { p->ainew.state = AI_STATE_NOTHING; return; } |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
609 |
|
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
610 |
FOR_ALL_STATIONS(st) { |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
611 |
if (st->xy != 0) { |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
612 |
if (st->owner == _current_player) { |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
613 |
if (p->ainew.tbt == AI_BUS && (FACIL_BUS_STOP & st->facilities) == FACIL_BUS_STOP) { |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
614 |
if (st->town == town) { |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
615 |
// Check how much cargo there is left in the station |
538
5dd99f3b7eca
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
507
diff
changeset
|
616 |
if ((st->goods[p->ainew.cargo].waiting_acceptance & 0xFFF) > RoadVehInfo(i)->capacity * AI_STATION_REUSE_MULTIPLER) { |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
617 |
if (AiNew_CheckVehicleStation(p, st)) { |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
618 |
// We did found a station that was good enough! |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
619 |
new_tile = st->xy; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
620 |
// Cheap way to get the direction of the station... |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
621 |
// Bus stations save it as 0x47 .. 0x4A, so decrease it with 0x47, and tada! |
2049
ad0d49c916d4
(svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents:
1981
diff
changeset
|
622 |
direction = _m[st->xy].m5 - 0x47; |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
623 |
break; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
624 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
625 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
626 |
count++; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
627 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
628 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
629 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
630 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
631 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
632 |
// We are going to add a new station... |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
633 |
if (new_tile == 0) count++; |
826 | 634 |
// No more than 2 stations allowed in a city |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
635 |
// This is because only the best 2 stations of one cargo do get any cargo |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
636 |
if (count > 2) { |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
637 |
p->ainew.state = AI_STATE_NOTHING; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
638 |
return; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
639 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
640 |
|
2365 | 641 |
if (new_tile == 0 && p->ainew.tbt == AI_BUS) { |
642 |
uint x, y, i = 0; |
|
643 |
int r; |
|
644 |
uint best; |
|
645 |
uint accepts[NUM_CARGO]; |
|
646 |
TileIndex found_spot[AI_FINDSTATION_TILE_RANGE*AI_FINDSTATION_TILE_RANGE*4]; |
|
647 |
uint found_best[AI_FINDSTATION_TILE_RANGE*AI_FINDSTATION_TILE_RANGE*4]; |
|
648 |
// To find a good spot we scan a range from the center, a get the point |
|
649 |
// where we get the most cargo and where it is buildable. |
|
650 |
// TODO: also check for station of myself and make sure we are not |
|
651 |
// taking eachothers passangers away (bad result when it does not) |
|
652 |
for (x = TileX(tile) - AI_FINDSTATION_TILE_RANGE; x <= TileX(tile) + AI_FINDSTATION_TILE_RANGE; x++) { |
|
653 |
for (y = TileY(tile) - AI_FINDSTATION_TILE_RANGE; y <= TileY(tile) + AI_FINDSTATION_TILE_RANGE; y++) { |
|
654 |
new_tile = TileXY(x, y); |
|
655 |
if (IsTileType(new_tile, MP_CLEAR) || IsTileType(new_tile, MP_TREES)) { |
|
656 |
// This tile we can build on! |
|
657 |
// Check acceptance |
|
658 |
// XXX - Get the catchment area |
|
659 |
GetAcceptanceAroundTiles(accepts, new_tile, 1, 1, 4); |
|
660 |
// >> 3 == 0 means no cargo |
|
661 |
if (accepts[p->ainew.cargo] >> 3 == 0) continue; |
|
662 |
// See if we can build the station |
|
663 |
r = AiNew_Build_Station(p, p->ainew.tbt, new_tile, 0, 0, 0, DC_QUERY_COST); |
|
2737
f16e0a808897
(svn r3282) - Codechange: Replace tests against CMD_ERROR with CmdFailed()
peter1138
parents:
2684
diff
changeset
|
664 |
if (CmdFailed(r)) continue; |
2365 | 665 |
// We can build it, so add it to found_spot |
666 |
found_spot[i] = new_tile; |
|
667 |
found_best[i++] = accepts[p->ainew.cargo]; |
|
668 |
} |
|
669 |
} |
|
670 |
} |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
671 |
|
2365 | 672 |
// If i is still zero, we did not found anything :( |
673 |
if (i == 0) { |
|
674 |
p->ainew.state = AI_STATE_NOTHING; |
|
675 |
return; |
|
676 |
} |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
677 |
|
2365 | 678 |
// Go through all the found_best and check which has the highest value |
679 |
best = 0; |
|
680 |
new_tile = 0; |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
681 |
|
2365 | 682 |
for (x=0;x<i;x++) { |
683 |
if (found_best[x] > best || |
|
684 |
(found_best[x] == best && DistanceManhattan(tile, new_tile) > DistanceManhattan(tile, found_spot[x]))) { |
|
685 |
new_tile = found_spot[x]; |
|
686 |
best = found_best[x]; |
|
687 |
} |
|
688 |
} |
|
145 | 689 |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
690 |
// See how much it is going to cost us... |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
691 |
r = AiNew_Build_Station(p, p->ainew.tbt, new_tile, 0, 0, 0, DC_QUERY_COST); |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
692 |
p->ainew.new_cost += r; |
145 | 693 |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
694 |
direction = AI_PATHFINDER_NO_DIRECTION; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
695 |
} else if (new_tile == 0 && p->ainew.tbt == AI_TRUCK) { |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
696 |
// Truck station locater works differently.. a station can be on any place |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
697 |
// as long as it is in range. So we give back code AI_STATION_RANGE |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
698 |
// so the pathfinder routine can work it out! |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
699 |
new_tile = AI_STATION_RANGE; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
700 |
direction = AI_PATHFINDER_NO_DIRECTION; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
701 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
702 |
|
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
703 |
if (p->ainew.from_tile == 0) { |
2365 | 704 |
p->ainew.from_tile = new_tile; |
705 |
p->ainew.from_direction = direction; |
|
706 |
// Now we found thisone, go in for to_tile |
|
707 |
return; |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
708 |
} else if (p->ainew.to_tile == 0) { |
2365 | 709 |
p->ainew.to_tile = new_tile; |
710 |
p->ainew.to_direction = direction; |
|
711 |
// K, done placing stations! |
|
712 |
p->ainew.temp = -1; |
|
713 |
p->ainew.state = AI_STATE_FIND_PATH; |
|
714 |
return; |
|
715 |
} |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
716 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
717 |
|
2365 | 718 |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
719 |
// We try to find a path between 2 points |
2365 | 720 |
static void AiNew_State_FindPath(Player *p) |
721 |
{ |
|
722 |
int r; |
|
723 |
assert(p->ainew.state == AI_STATE_FIND_PATH); |
|
145 | 724 |
|
2365 | 725 |
// First time, init some data |
726 |
if (p->ainew.temp == -1) { |
|
727 |
// Init path_info |
|
728 |
if (p->ainew.from_tile == AI_STATION_RANGE) { |
|
729 |
// For truck routes we take a range around the industry |
|
730 |
p->ainew.path_info.start_tile_tl = GetIndustry(p->ainew.from_ic)->xy - TileDiffXY(1, 1); |
|
731 |
p->ainew.path_info.start_tile_br = GetIndustry(p->ainew.from_ic)->xy + TileDiffXY(GetIndustry(p->ainew.from_ic)->width, GetIndustry(p->ainew.from_ic)->height) + TileDiffXY(1, 1); |
|
732 |
p->ainew.path_info.start_direction = p->ainew.from_direction; |
|
733 |
} else { |
|
734 |
p->ainew.path_info.start_tile_tl = p->ainew.from_tile; |
|
735 |
p->ainew.path_info.start_tile_br = p->ainew.from_tile; |
|
736 |
p->ainew.path_info.start_direction = p->ainew.from_direction; |
|
737 |
} |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
738 |
|
2365 | 739 |
if (p->ainew.to_tile == AI_STATION_RANGE) { |
740 |
p->ainew.path_info.end_tile_tl = GetIndustry(p->ainew.to_ic)->xy - TileDiffXY(1, 1); |
|
741 |
p->ainew.path_info.end_tile_br = GetIndustry(p->ainew.to_ic)->xy + TileDiffXY(GetIndustry(p->ainew.to_ic)->width, GetIndustry(p->ainew.to_ic)->height) + TileDiffXY(1, 1); |
|
742 |
p->ainew.path_info.end_direction = p->ainew.to_direction; |
|
743 |
} else { |
|
744 |
p->ainew.path_info.end_tile_tl = p->ainew.to_tile; |
|
745 |
p->ainew.path_info.end_tile_br = p->ainew.to_tile; |
|
746 |
p->ainew.path_info.end_direction = p->ainew.to_direction; |
|
747 |
} |
|
145 | 748 |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
749 |
if (p->ainew.tbt == AI_TRAIN) |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
750 |
p->ainew.path_info.rail_or_road = true; |
2365 | 751 |
else |
752 |
p->ainew.path_info.rail_or_road = false; |
|
145 | 753 |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
754 |
// First, clean the pathfinder with our new begin and endpoints |
2365 | 755 |
clean_AyStar_AiPathFinder(p->ainew.pathfinder, &p->ainew.path_info); |
145 | 756 |
|
2365 | 757 |
p->ainew.temp = 0; |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
758 |
} |
145 | 759 |
|
2365 | 760 |
// Start the pathfinder |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
761 |
r = p->ainew.pathfinder->main(p->ainew.pathfinder); |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
762 |
// If it return: no match, stop it... |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
763 |
if (r == AYSTAR_NO_PATH) { |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
764 |
DEBUG(ai,1)("[AiNew] PathFinder found no route!"); |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
765 |
// Start all over again... |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
766 |
p->ainew.state = AI_STATE_NOTHING; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
767 |
return; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
768 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
769 |
if (r == AYSTAR_FOUND_END_NODE) { |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
770 |
// We found the end-point |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
771 |
p->ainew.temp = -1; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
772 |
p->ainew.state = AI_STATE_FIND_DEPOT; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
773 |
return; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
774 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
775 |
// In any other case, we are still busy finding the route... |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
776 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
777 |
|
2365 | 778 |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
779 |
// This function tries to locate a good place for a depot! |
2365 | 780 |
static void AiNew_State_FindDepot(Player *p) |
781 |
{ |
|
782 |
// To place the depot, we walk through the route, and if we find a lovely spot (MP_CLEAR, MP_TREES), we place it there.. |
|
783 |
// Simple, easy, works! |
|
784 |
// To make the depot stand in the middle of the route, we start from the center.. |
|
785 |
// But first we walk through the route see if we can find a depot that is ours |
|
786 |
// this keeps things nice ;) |
|
2635 | 787 |
int g, i, r; |
3153
301c1d71122b
(svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents:
3144
diff
changeset
|
788 |
DiagDirection j; |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
789 |
TileIndex tile; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
790 |
assert(p->ainew.state == AI_STATE_FIND_DEPOT); |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
791 |
|
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
792 |
p->ainew.depot_tile = 0; |
145 | 793 |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
794 |
for (i=2;i<p->ainew.path_info.route_length-2;i++) { |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
795 |
tile = p->ainew.path_info.route[i]; |
900 | 796 |
for (j = 0; j < 4; j++) { |
3153
301c1d71122b
(svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents:
3144
diff
changeset
|
797 |
TileIndex t = tile + TileOffsByDir(j); |
301c1d71122b
(svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents:
3144
diff
changeset
|
798 |
|
301c1d71122b
(svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents:
3144
diff
changeset
|
799 |
if (IsTileType(t, MP_STREET) && |
301c1d71122b
(svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents:
3144
diff
changeset
|
800 |
GetRoadType(t) == ROAD_DEPOT && |
301c1d71122b
(svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents:
3144
diff
changeset
|
801 |
IsTileOwner(t, _current_player) && |
3179 | 802 |
GetRoadDepotDirection(t) == ReverseDiagDir(j)) { |
3153
301c1d71122b
(svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents:
3144
diff
changeset
|
803 |
p->ainew.depot_tile = t; |
301c1d71122b
(svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents:
3144
diff
changeset
|
804 |
p->ainew.depot_direction = ReverseDiagDir(j); |
301c1d71122b
(svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents:
3144
diff
changeset
|
805 |
p->ainew.state = AI_STATE_VERIFY_ROUTE; |
301c1d71122b
(svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents:
3144
diff
changeset
|
806 |
return; |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
807 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
808 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
809 |
} |
145 | 810 |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
811 |
// This routine let depot finding start in the middle, and work his way to the stations |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
812 |
// It makes depot placing nicer :) |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
813 |
i = p->ainew.path_info.route_length / 2; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
814 |
g = 1; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
815 |
while (i > 1 && i < p->ainew.path_info.route_length - 2) { |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
816 |
i += g; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
817 |
g *= -1; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
818 |
(g < 0?g--:g++); |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
819 |
|
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
820 |
if (p->ainew.path_info.route_extra[i] != 0 || p->ainew.path_info.route_extra[i+1] != 0) { |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
821 |
// Bridge or tunnel.. we can't place a depot there |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
822 |
continue; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
823 |
} |
145 | 824 |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
825 |
tile = p->ainew.path_info.route[i]; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
826 |
|
900 | 827 |
for (j = 0; j < 4; j++) { |
3153
301c1d71122b
(svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents:
3144
diff
changeset
|
828 |
TileIndex t = tile + TileOffsByDir(j); |
301c1d71122b
(svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents:
3144
diff
changeset
|
829 |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
830 |
// It may not be placed on the road/rail itself |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
831 |
// And because it is not build yet, we can't see it on the tile.. |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
832 |
// So check the surrounding tiles :) |
3153
301c1d71122b
(svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents:
3144
diff
changeset
|
833 |
if (t == p->ainew.path_info.route[i - 1] || |
301c1d71122b
(svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents:
3144
diff
changeset
|
834 |
t == p->ainew.path_info.route[i + 1]) { |
2365 | 835 |
continue; |
3153
301c1d71122b
(svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents:
3144
diff
changeset
|
836 |
} |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
837 |
// Not around a bridge? |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
838 |
if (p->ainew.path_info.route_extra[i] != 0) continue; |
1035
0a170deb6e33
(svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents:
1024
diff
changeset
|
839 |
if (IsTileType(tile, MP_TUNNELBRIDGE)) continue; |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
840 |
// Is the terrain clear? |
3153
301c1d71122b
(svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents:
3144
diff
changeset
|
841 |
if (IsTileType(t, MP_CLEAR) || IsTileType(t, MP_TREES)) { |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
842 |
// If the current tile is on a slope (tileh != 0) then we do not allow this |
3055
25fd76668365
(svn r3644) Don't use FindLandscapeHeightByTile() when it's overkill - often it was just a complicated way of writing GetTileSlope(tile, NULL)
tron
parents:
2749
diff
changeset
|
843 |
if (GetTileSlope(tile, NULL) != 0) continue; |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
844 |
// Check if everything went okay.. |
3153
301c1d71122b
(svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents:
3144
diff
changeset
|
845 |
r = AiNew_Build_Depot(p, t, ReverseDiagDir(j), 0); |
2737
f16e0a808897
(svn r3282) - Codechange: Replace tests against CMD_ERROR with CmdFailed()
peter1138
parents:
2684
diff
changeset
|
846 |
if (CmdFailed(r)) continue; |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
847 |
// Found a spot! |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
848 |
p->ainew.new_cost += r; |
3153
301c1d71122b
(svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents:
3144
diff
changeset
|
849 |
p->ainew.depot_tile = t; |
301c1d71122b
(svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents:
3144
diff
changeset
|
850 |
p->ainew.depot_direction = ReverseDiagDir(j); // Reverse direction |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
851 |
p->ainew.state = AI_STATE_VERIFY_ROUTE; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
852 |
return; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
853 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
854 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
855 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
856 |
|
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
857 |
// Failed to find a depot? |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
858 |
p->ainew.state = AI_STATE_NOTHING; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
859 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
860 |
|
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
861 |
|
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
862 |
// This function calculates how many vehicles there are needed on this |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
863 |
// traject. |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
864 |
// It works pretty simple: get the length, see how much we move around |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
865 |
// and hussle that, and you know how many vehicles there are needed. |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
866 |
// It returns the cost for the vehicles |
2365 | 867 |
static int AiNew_HowManyVehicles(Player *p) |
868 |
{ |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
869 |
if (p->ainew.tbt == AI_BUS) { |
2365 | 870 |
// For bus-routes we look at the time before we are back in the station |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
871 |
int i, length, tiles_a_day; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
872 |
int amount; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
873 |
i = AiNew_PickVehicle(p); |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
874 |
if (i == -1) return 0; |
2365 | 875 |
// Passenger run.. how long is the route? |
876 |
length = p->ainew.path_info.route_length; |
|
877 |
// Calculating tiles a day a vehicle moves is not easy.. this is how it must be done! |
|
878 |
tiles_a_day = RoadVehInfo(i)->max_speed * DAY_TICKS / 256 / 16; |
|
879 |
// We want a vehicle in a station once a month at least, so, calculate it! |
|
880 |
// (the * 2 is because we have 2 stations ;)) |
|
881 |
amount = length * 2 * 2 / tiles_a_day / 30; |
|
882 |
if (amount == 0) amount = 1; |
|
883 |
return amount; |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
884 |
} else if (p->ainew.tbt == AI_TRUCK) { |
2365 | 885 |
// For truck-routes we look at the cargo |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
886 |
int i, length, amount, tiles_a_day; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
887 |
int max_cargo; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
888 |
i = AiNew_PickVehicle(p); |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
889 |
if (i == -1) return 0; |
2365 | 890 |
// Passenger run.. how long is the route? |
891 |
length = p->ainew.path_info.route_length; |
|
892 |
// Calculating tiles a day a vehicle moves is not easy.. this is how it must be done! |
|
893 |
tiles_a_day = RoadVehInfo(i)->max_speed * DAY_TICKS / 256 / 16; |
|
894 |
if (p->ainew.from_deliver) |
|
895 |
max_cargo = GetIndustry(p->ainew.from_ic)->total_production[0]; |
|
896 |
else |
|
897 |
max_cargo = GetIndustry(p->ainew.to_ic)->total_production[0]; |
|
145 | 898 |
|
2365 | 899 |
// This is because moving 60% is more than we can dream of! |
900 |
max_cargo *= 0.6; |
|
901 |
// We want all the cargo to be gone in a month.. so, we know the cargo it delivers |
|
902 |
// we know what the vehicle takes with him, and we know the time it takes him |
|
903 |
// to get back here.. now let's do some math! |
|
904 |
amount = 2 * length * max_cargo / tiles_a_day / 30 / RoadVehInfo(i)->capacity; |
|
905 |
amount += 1; |
|
906 |
return amount; |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
907 |
} else { |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
908 |
// Currently not supported |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
909 |
return 0; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
910 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
911 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
912 |
|
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
913 |
|
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
914 |
// This function checks: |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
915 |
// - If the route went okay |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
916 |
// - Calculates the amount of money needed to build the route |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
917 |
// - Calculates how much vehicles needed for the route |
2365 | 918 |
static void AiNew_State_VerifyRoute(Player *p) |
919 |
{ |
|
920 |
int res, i; |
|
921 |
assert(p->ainew.state == AI_STATE_VERIFY_ROUTE); |
|
145 | 922 |
|
2365 | 923 |
// Let's calculate the cost of the path.. |
924 |
// new_cost already contains the cost of the stations |
|
925 |
p->ainew.path_info.position = -1; |
|
145 | 926 |
|
2365 | 927 |
do { |
928 |
p->ainew.path_info.position++; |
|
929 |
p->ainew.new_cost += AiNew_Build_RoutePart(p, &p->ainew.path_info, DC_QUERY_COST); |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
930 |
} while (p->ainew.path_info.position != -2); |
145 | 931 |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
932 |
// Now we know the price of build station + path. Now check how many vehicles |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
933 |
// we need and what the price for that will be |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
934 |
res = AiNew_HowManyVehicles(p); |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
935 |
// If res == 0, no vehicle was found, or an other problem did occour |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
936 |
if (res == 0) { |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
937 |
p->ainew.state = AI_STATE_NOTHING; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
938 |
return; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
939 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
940 |
p->ainew.amount_veh = res; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
941 |
p->ainew.cur_veh = 0; |
145 | 942 |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
943 |
// Check how much it it going to cost us.. |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
944 |
for (i=0;i<res;i++) { |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
945 |
p->ainew.new_cost += AiNew_Build_Vehicle(p, 0, DC_QUERY_COST); |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
946 |
} |
145 | 947 |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
948 |
// Now we know how much the route is going to cost us |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
949 |
// Check if we have enough money for it! |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
950 |
if (p->ainew.new_cost > p->player_money - AI_MINIMUM_MONEY) { |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
951 |
// Too bad.. |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
952 |
DEBUG(ai,1)("[AiNew] Can't pay for this route (%d)", p->ainew.new_cost); |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
953 |
p->ainew.state = AI_STATE_NOTHING; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
954 |
return; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
955 |
} |
145 | 956 |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
957 |
// Now we can build the route, check the direction of the stations! |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
958 |
if (p->ainew.from_direction == AI_PATHFINDER_NO_DIRECTION) { |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
959 |
p->ainew.from_direction = AiNew_GetDirection(p->ainew.path_info.route[p->ainew.path_info.route_length-1], p->ainew.path_info.route[p->ainew.path_info.route_length-2]); |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
960 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
961 |
if (p->ainew.to_direction == AI_PATHFINDER_NO_DIRECTION) { |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
962 |
p->ainew.to_direction = AiNew_GetDirection(p->ainew.path_info.route[0], p->ainew.path_info.route[1]); |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
963 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
964 |
if (p->ainew.from_tile == AI_STATION_RANGE) |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
965 |
p->ainew.from_tile = p->ainew.path_info.route[p->ainew.path_info.route_length-1]; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
966 |
if (p->ainew.to_tile == AI_STATION_RANGE) |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
967 |
p->ainew.to_tile = p->ainew.path_info.route[0]; |
145 | 968 |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
969 |
p->ainew.state = AI_STATE_BUILD_STATION; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
970 |
p->ainew.temp = 0; |
145 | 971 |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
972 |
DEBUG(ai,1)("[AiNew] The route is set and buildable.. going to build it!"); |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
973 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
974 |
|
2365 | 975 |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
976 |
// Build the stations |
2365 | 977 |
static void AiNew_State_BuildStation(Player *p) |
978 |
{ |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
979 |
int res = 0; |
2365 | 980 |
assert(p->ainew.state == AI_STATE_BUILD_STATION); |
981 |
if (p->ainew.temp == 0) { |
|
982 |
if (!IsTileType(p->ainew.from_tile, MP_STATION)) |
|
983 |
res = AiNew_Build_Station(p, p->ainew.tbt, p->ainew.from_tile, 0, 0, p->ainew.from_direction, DC_EXEC); |
|
984 |
} else { |
|
985 |
if (!IsTileType(p->ainew.to_tile, MP_STATION)) |
|
986 |
res = AiNew_Build_Station(p, p->ainew.tbt, p->ainew.to_tile, 0, 0, p->ainew.to_direction, DC_EXEC); |
|
987 |
p->ainew.state = AI_STATE_BUILD_PATH; |
|
988 |
} |
|
2737
f16e0a808897
(svn r3282) - Codechange: Replace tests against CMD_ERROR with CmdFailed()
peter1138
parents:
2684
diff
changeset
|
989 |
if (CmdFailed(res)) { |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
990 |
DEBUG(ai,0)("[AiNew - BuildStation] Strange but true... station can not be build!"); |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
991 |
p->ainew.state = AI_STATE_NOTHING; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
992 |
// If the first station _was_ build, destroy it |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
993 |
if (p->ainew.temp != 0) |
2682
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2635
diff
changeset
|
994 |
AI_DoCommand(p->ainew.from_tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR); |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
995 |
return; |
2365 | 996 |
} |
997 |
p->ainew.temp++; |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
998 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
999 |
|
2365 | 1000 |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1001 |
// Build the path |
2365 | 1002 |
static void AiNew_State_BuildPath(Player *p) |
1003 |
{ |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1004 |
assert(p->ainew.state == AI_STATE_BUILD_PATH); |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1005 |
// p->ainew.temp is set to -1 when this function is called for the first time |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1006 |
if (p->ainew.temp == -1) { |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1007 |
DEBUG(ai,1)("[AiNew] Starting to build the path.."); |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1008 |
// Init the counter |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1009 |
p->ainew.counter = (4 - _opt.diff.competitor_speed) * AI_BUILDPATH_PAUSE + 1; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1010 |
// Set the position to the startingplace (-1 because in a minute we do ++) |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1011 |
p->ainew.path_info.position = -1; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1012 |
// And don't do this again |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1013 |
p->ainew.temp = 0; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1014 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1015 |
// Building goes very fast on normal rate, so we are going to slow it down.. |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1016 |
// By let the counter count from AI_BUILDPATH_PAUSE to 0, we have a nice way :) |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1017 |
if (--p->ainew.counter != 0) return; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1018 |
p->ainew.counter = (4 - _opt.diff.competitor_speed) * AI_BUILDPATH_PAUSE + 1; |
145 | 1019 |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1020 |
// Increase the building position |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1021 |
p->ainew.path_info.position++; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1022 |
// Build route |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1023 |
AiNew_Build_RoutePart(p, &p->ainew.path_info, DC_EXEC); |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1024 |
if (p->ainew.path_info.position == -2) { |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1025 |
// This means we are done building! |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1026 |
|
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1027 |
if (p->ainew.tbt == AI_TRUCK && !_patches.roadveh_queue) { |
2365 | 1028 |
// If they not queue, they have to go up and down to try again at a station... |
1029 |
// We don't want that, so try building some road left or right of the station |
|
1030 |
int dir1, dir2, dir3; |
|
1031 |
TileIndex tile; |
|
1032 |
int i, ret; |
|
1033 |
for (i=0;i<2;i++) { |
|
1034 |
if (i == 0) { |
|
1035 |
tile = p->ainew.from_tile + TileOffsByDir(p->ainew.from_direction); |
|
1036 |
dir1 = p->ainew.from_direction - 1; |
|
1037 |
if (dir1 < 0) dir1 = 3; |
|
1038 |
dir2 = p->ainew.from_direction + 1; |
|
1039 |
if (dir2 > 3) dir2 = 0; |
|
1040 |
dir3 = p->ainew.from_direction; |
|
1041 |
} else { |
|
1042 |
tile = p->ainew.to_tile + TileOffsByDir(p->ainew.to_direction); |
|
1043 |
dir1 = p->ainew.to_direction - 1; |
|
1044 |
if (dir1 < 0) dir1 = 3; |
|
1045 |
dir2 = p->ainew.to_direction + 1; |
|
1046 |
if (dir2 > 3) dir2 = 0; |
|
1047 |
dir3 = p->ainew.to_direction; |
|
1048 |
} |
|
145 | 1049 |
|
3157
40de8616c04c
(svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents:
3153
diff
changeset
|
1050 |
ret = AI_DoCommand(tile, DiagDirToRoadBits(ReverseDiagDir(dir1)), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD); |
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:
1490
diff
changeset
|
1051 |
if (!CmdFailed(ret)) { |
900 | 1052 |
dir1 = TileOffsByDir(dir1); |
1035
0a170deb6e33
(svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents:
1024
diff
changeset
|
1053 |
if (IsTileType(tile + dir1, MP_CLEAR) || IsTileType(tile + dir1, MP_TREES)) { |
2682
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2635
diff
changeset
|
1054 |
ret = AI_DoCommand(tile+dir1, AiNew_GetRoadDirection(tile, tile+dir1, tile+dir1+dir1), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD); |
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:
1490
diff
changeset
|
1055 |
if (!CmdFailed(ret)) { |
1035
0a170deb6e33
(svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents:
1024
diff
changeset
|
1056 |
if (IsTileType(tile + dir1 + dir1, MP_CLEAR) || IsTileType(tile + dir1 + dir1, MP_TREES)) |
2682
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2635
diff
changeset
|
1057 |
AI_DoCommand(tile+dir1+dir1, AiNew_GetRoadDirection(tile+dir1, tile+dir1+dir1, tile+dir1+dir1+dir1), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD); |
146
7632a1ddadc0
(svn r147) -Fix [NewAI]: small fix for road-building when RoadQueueing is disabled
truelight
parents:
145
diff
changeset
|
1058 |
} |
7632a1ddadc0
(svn r147) -Fix [NewAI]: small fix for road-building when RoadQueueing is disabled
truelight
parents:
145
diff
changeset
|
1059 |
} |
7632a1ddadc0
(svn r147) -Fix [NewAI]: small fix for road-building when RoadQueueing is disabled
truelight
parents:
145
diff
changeset
|
1060 |
} |
145 | 1061 |
|
3157
40de8616c04c
(svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents:
3153
diff
changeset
|
1062 |
ret = AI_DoCommand(tile, DiagDirToRoadBits(ReverseDiagDir(dir2)), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD); |
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:
1490
diff
changeset
|
1063 |
if (!CmdFailed(ret)) { |
900 | 1064 |
dir2 = TileOffsByDir(dir2); |
1035
0a170deb6e33
(svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents:
1024
diff
changeset
|
1065 |
if (IsTileType(tile + dir2, MP_CLEAR) || IsTileType(tile + dir2, MP_TREES)) { |
2682
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2635
diff
changeset
|
1066 |
ret = AI_DoCommand(tile+dir2, AiNew_GetRoadDirection(tile, tile+dir2, tile+dir2+dir2), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD); |
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:
1490
diff
changeset
|
1067 |
if (!CmdFailed(ret)) { |
1035
0a170deb6e33
(svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents:
1024
diff
changeset
|
1068 |
if (IsTileType(tile + dir2 + dir2, MP_CLEAR) || IsTileType(tile + dir2 + dir2, MP_TREES)) |
2682
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2635
diff
changeset
|
1069 |
AI_DoCommand(tile+dir2+dir2, AiNew_GetRoadDirection(tile+dir2, tile+dir2+dir2, tile+dir2+dir2+dir2), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD); |
146
7632a1ddadc0
(svn r147) -Fix [NewAI]: small fix for road-building when RoadQueueing is disabled
truelight
parents:
145
diff
changeset
|
1070 |
} |
7632a1ddadc0
(svn r147) -Fix [NewAI]: small fix for road-building when RoadQueueing is disabled
truelight
parents:
145
diff
changeset
|
1071 |
} |
7632a1ddadc0
(svn r147) -Fix [NewAI]: small fix for road-building when RoadQueueing is disabled
truelight
parents:
145
diff
changeset
|
1072 |
} |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1073 |
|
3157
40de8616c04c
(svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents:
3153
diff
changeset
|
1074 |
ret = AI_DoCommand(tile, DiagDirToRoadBits(dir3), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD); |
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:
1490
diff
changeset
|
1075 |
if (!CmdFailed(ret)) { |
900 | 1076 |
dir3 = TileOffsByDir(dir3); |
1035
0a170deb6e33
(svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents:
1024
diff
changeset
|
1077 |
if (IsTileType(tile + dir3, MP_CLEAR) || IsTileType(tile + dir3, MP_TREES)) { |
2682
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2635
diff
changeset
|
1078 |
ret = AI_DoCommand(tile+dir3, AiNew_GetRoadDirection(tile, tile+dir3, tile+dir3+dir3), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD); |
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:
1490
diff
changeset
|
1079 |
if (!CmdFailed(ret)) { |
1035
0a170deb6e33
(svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents:
1024
diff
changeset
|
1080 |
if (IsTileType(tile + dir3 + dir3, MP_CLEAR) || IsTileType(tile + dir3 + dir3, MP_TREES)) |
2682
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2635
diff
changeset
|
1081 |
AI_DoCommand(tile+dir3+dir3, AiNew_GetRoadDirection(tile+dir3, tile+dir3+dir3, tile+dir3+dir3+dir3), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD); |
146
7632a1ddadc0
(svn r147) -Fix [NewAI]: small fix for road-building when RoadQueueing is disabled
truelight
parents:
145
diff
changeset
|
1082 |
} |
7632a1ddadc0
(svn r147) -Fix [NewAI]: small fix for road-building when RoadQueueing is disabled
truelight
parents:
145
diff
changeset
|
1083 |
} |
7632a1ddadc0
(svn r147) -Fix [NewAI]: small fix for road-building when RoadQueueing is disabled
truelight
parents:
145
diff
changeset
|
1084 |
} |
2365 | 1085 |
} |
1086 |
} |
|
145 | 1087 |
|
1088 |
||
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1089 |
DEBUG(ai,1)("[AiNew] Done building the path (cost: %d)", p->ainew.new_cost); |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1090 |
p->ainew.state = AI_STATE_BUILD_DEPOT; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1091 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1092 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1093 |
|
2365 | 1094 |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1095 |
// Builds the depot |
2365 | 1096 |
static void AiNew_State_BuildDepot(Player *p) |
1097 |
{ |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1098 |
int res = 0; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1099 |
assert(p->ainew.state == AI_STATE_BUILD_DEPOT); |
145 | 1100 |
|
3071
a6acfe74e0a7
(svn r3660) Convert further road bits and type references to the functions/enums
tron
parents:
3055
diff
changeset
|
1101 |
if (IsTileType(p->ainew.depot_tile, MP_STREET) && GetRoadType(p->ainew.depot_tile) == ROAD_DEPOT) { |
1901
fb05044cf5c3
(svn r2407) Use {Get,Is}TileOwner to get/check the owner of a tile and fix some bogus reads of _map_owner
tron
parents:
1891
diff
changeset
|
1102 |
if (IsTileOwner(p->ainew.depot_tile, _current_player)) { |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1103 |
// The depot is already builded! |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1104 |
p->ainew.state = AI_STATE_BUILD_VEHICLE; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1105 |
return; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1106 |
} else { |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1107 |
// There is a depot, but not of our team! :( |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1108 |
p->ainew.state = AI_STATE_NOTHING; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1109 |
return; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1110 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1111 |
} |
145 | 1112 |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1113 |
// There is a bus on the tile we want to build road on... idle till he is gone! (BAD PERSON! :p) |
900 | 1114 |
if (!EnsureNoVehicle(p->ainew.depot_tile + TileOffsByDir(p->ainew.depot_direction))) |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1115 |
return; |
145 | 1116 |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1117 |
res = AiNew_Build_Depot(p, p->ainew.depot_tile, p->ainew.depot_direction, DC_EXEC); |
2737
f16e0a808897
(svn r3282) - Codechange: Replace tests against CMD_ERROR with CmdFailed()
peter1138
parents:
2684
diff
changeset
|
1118 |
if (CmdFailed(res)) { |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1119 |
DEBUG(ai,0)("[AiNew - BuildDepot] Strange but true... depot can not be build!"); |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1120 |
p->ainew.state = AI_STATE_NOTHING; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1121 |
return; |
2365 | 1122 |
} |
145 | 1123 |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1124 |
p->ainew.state = AI_STATE_BUILD_VEHICLE; |
2682
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2635
diff
changeset
|
1125 |
p->ainew.idle = 10; |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1126 |
p->ainew.veh_main_id = (VehicleID)-1; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1127 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1128 |
|
145 | 1129 |
|
2365 | 1130 |
// Build vehicles |
1131 |
static void AiNew_State_BuildVehicle(Player *p) |
|
1132 |
{ |
|
1133 |
int res; |
|
1134 |
assert(p->ainew.state == AI_STATE_BUILD_VEHICLE); |
|
1135 |
||
1136 |
// Check if we need to build a vehicle |
|
1137 |
if (p->ainew.amount_veh == 0) { |
|
1138 |
// Nope, we are done! |
|
1139 |
// This means: we are all done! The route is open.. go back to NOTHING |
|
1140 |
// He will idle some time and it will all start over again.. :) |
|
1141 |
p->ainew.state = AI_STATE_ACTION_DONE; |
|
1142 |
return; |
|
1143 |
} |
|
1144 |
if (--p->ainew.idle != 0) return; |
|
1145 |
// It is realistic that the AI can only build 1 vehicle a day.. |
|
1146 |
// This makes sure of that! |
|
1147 |
p->ainew.idle = AI_BUILD_VEHICLE_TIME_BETWEEN; |
|
1148 |
||
1149 |
// Build the vehicle |
|
1150 |
res = AiNew_Build_Vehicle(p, p->ainew.depot_tile, DC_EXEC); |
|
2737
f16e0a808897
(svn r3282) - Codechange: Replace tests against CMD_ERROR with CmdFailed()
peter1138
parents:
2684
diff
changeset
|
1151 |
if (CmdFailed(res)) { |
2365 | 1152 |
// This happens when the AI can't build any more vehicles! |
1153 |
p->ainew.state = AI_STATE_NOTHING; |
|
1154 |
return; |
|
1155 |
} |
|
1156 |
// Increase the current counter |
|
1157 |
p->ainew.cur_veh++; |
|
1158 |
// Decrease the total counter |
|
1159 |
p->ainew.amount_veh--; |
|
1160 |
// Go give some orders! |
|
1161 |
p->ainew.state = AI_STATE_GIVE_ORDERS; |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1162 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1163 |
|
2365 | 1164 |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1165 |
// Put the stations in the order list |
2365 | 1166 |
static void AiNew_State_GiveOrders(Player *p) |
1167 |
{ |
|
1168 |
int idx; |
|
1169 |
Order order; |
|
145 | 1170 |
|
2365 | 1171 |
assert(p->ainew.state == AI_STATE_GIVE_ORDERS); |
145 | 1172 |
|
2682
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2635
diff
changeset
|
1173 |
// Get the new ID |
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2635
diff
changeset
|
1174 |
/* XXX -- Because this AI isn't using any event-system, this is VERY dangerous! |
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2635
diff
changeset
|
1175 |
* There is no way telling if the vehicle is already bought (or delayed by the |
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2635
diff
changeset
|
1176 |
* network), and if bought, if not an other vehicle is bought in between.. in |
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2635
diff
changeset
|
1177 |
* other words, there is absolutely no way knowing if this id is the true |
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2635
diff
changeset
|
1178 |
* id.. soon this will all change, but for now, we needed something to test |
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2635
diff
changeset
|
1179 |
* on ;) -- TrueLight -- 21-11-2005 */ |
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2635
diff
changeset
|
1180 |
if (p->ainew.tbt == AI_TRAIN) { |
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2635
diff
changeset
|
1181 |
} else { |
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2635
diff
changeset
|
1182 |
p->ainew.veh_id = _new_roadveh_id; |
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2635
diff
changeset
|
1183 |
} |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1184 |
|
2682
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2635
diff
changeset
|
1185 |
if (p->ainew.veh_main_id != (VehicleID)-1) { |
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2635
diff
changeset
|
1186 |
AI_DoCommand(0, p->ainew.veh_id + (p->ainew.veh_main_id << 16), 0, DC_EXEC, CMD_CLONE_ORDER); |
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2635
diff
changeset
|
1187 |
|
2365 | 1188 |
p->ainew.state = AI_STATE_START_VEHICLE; |
1189 |
return; |
|
1190 |
} else { |
|
1191 |
p->ainew.veh_main_id = p->ainew.veh_id; |
|
1192 |
} |
|
1193 |
||
2682
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2635
diff
changeset
|
1194 |
// Very handy for AI, goto depot.. but yeah, it needs to be activated ;) |
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2635
diff
changeset
|
1195 |
if (_patches.gotodepot) { |
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2635
diff
changeset
|
1196 |
idx = 0; |
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2635
diff
changeset
|
1197 |
order.type = OT_GOTO_DEPOT; |
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2635
diff
changeset
|
1198 |
order.flags = OF_UNLOAD; |
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2635
diff
changeset
|
1199 |
order.station = GetDepotByTile(p->ainew.depot_tile)->index; |
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2635
diff
changeset
|
1200 |
AI_DoCommand(0, p->ainew.veh_id + (idx << 16), PackOrder(&order), DC_EXEC, CMD_INSERT_ORDER); |
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2635
diff
changeset
|
1201 |
} |
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2635
diff
changeset
|
1202 |
|
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2635
diff
changeset
|
1203 |
idx = 0; |
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2635
diff
changeset
|
1204 |
order.type = OT_GOTO_STATION; |
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2635
diff
changeset
|
1205 |
order.flags = 0; |
3315
1f65f8260092
(svn r4079) Add GetSation{Index,ByTile}() to get the station index resp. the station from a tile
tron
parents:
3179
diff
changeset
|
1206 |
order.station = GetStationIndex(p->ainew.to_tile); |
2682
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2635
diff
changeset
|
1207 |
if (p->ainew.tbt == AI_TRUCK && p->ainew.to_deliver) |
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2635
diff
changeset
|
1208 |
order.flags |= OF_FULL_LOAD; |
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2635
diff
changeset
|
1209 |
AI_DoCommand(0, p->ainew.veh_id + (idx << 16), PackOrder(&order), DC_EXEC, CMD_INSERT_ORDER); |
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2635
diff
changeset
|
1210 |
|
2365 | 1211 |
idx = 0; |
1212 |
order.type = OT_GOTO_STATION; |
|
1213 |
order.flags = 0; |
|
3315
1f65f8260092
(svn r4079) Add GetSation{Index,ByTile}() to get the station index resp. the station from a tile
tron
parents:
3179
diff
changeset
|
1214 |
order.station = GetStationIndex(p->ainew.from_tile); |
2365 | 1215 |
if (p->ainew.tbt == AI_TRUCK && p->ainew.from_deliver) |
1216 |
order.flags |= OF_FULL_LOAD; |
|
2682
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2635
diff
changeset
|
1217 |
AI_DoCommand(0, p->ainew.veh_id + (idx << 16), PackOrder(&order), DC_EXEC, CMD_INSERT_ORDER); |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1218 |
|
2365 | 1219 |
// Start the engines! |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1220 |
p->ainew.state = AI_STATE_START_VEHICLE; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1221 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1222 |
|
2365 | 1223 |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1224 |
// Start the vehicle |
2365 | 1225 |
static void AiNew_State_StartVehicle(Player *p) |
1226 |
{ |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1227 |
assert(p->ainew.state == AI_STATE_START_VEHICLE); |
145 | 1228 |
|
2682
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2635
diff
changeset
|
1229 |
// Skip the first order if it is a second vehicle |
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2635
diff
changeset
|
1230 |
// This to make vehicles go different ways.. |
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2635
diff
changeset
|
1231 |
if (p->ainew.cur_veh & 1) |
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2635
diff
changeset
|
1232 |
AI_DoCommand(0, p->ainew.veh_id, 0, DC_EXEC, CMD_SKIP_ORDER); |
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2635
diff
changeset
|
1233 |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1234 |
// 3, 2, 1... go! (give START_STOP command ;)) |
2682
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2635
diff
changeset
|
1235 |
AI_DoCommand(0, p->ainew.veh_id, 0, DC_EXEC, CMD_START_STOP_ROADVEH); |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1236 |
// Try to build an other vehicle (that function will stop building when needed) |
2682
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2635
diff
changeset
|
1237 |
p->ainew.idle = 10; |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1238 |
p->ainew.state = AI_STATE_BUILD_VEHICLE; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1239 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1240 |
|
2365 | 1241 |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1242 |
// Repays money |
2365 | 1243 |
static void AiNew_State_RepayMoney(Player *p) |
1244 |
{ |
|
1245 |
int i; |
|
1246 |
for (i=0;i<AI_LOAN_REPAY;i++) |
|
2682
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2635
diff
changeset
|
1247 |
AI_DoCommand(0, 0, 0, DC_EXEC, CMD_DECREASE_LOAN); |
2365 | 1248 |
p->ainew.state = AI_STATE_ACTION_DONE; |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1249 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1250 |
|
2365 | 1251 |
|
1252 |
static void AiNew_CheckVehicle(Player *p, Vehicle *v) |
|
1253 |
{ |
|
145 | 1254 |
// When a vehicle is under the 6 months, we don't check for anything |
1255 |
if (v->age < 180) return; |
|
1256 |
||
1257 |
// When a vehicle is older then 1 year, it should make money... |
|
1258 |
if (v->age > 360) { |
|
826 | 1259 |
// If both years together are not more than AI_MINIMUM_ROUTE_PROFIT, |
145 | 1260 |
// it is not worth the line I guess... |
1261 |
if (v->profit_last_year + v->profit_this_year < AI_MINIMUM_ROUTE_PROFIT || |
|
2365 | 1262 |
(v->reliability * 100 >> 16) < 40) { |
145 | 1263 |
// There is a possibility that the route is fucked up... |
1264 |
if (v->cargo_days > AI_VEHICLE_LOST_DAYS) { |
|
1265 |
// The vehicle is lost.. check the route, or else, get the vehicle |
|
1266 |
// back to a depot |
|
1267 |
// TODO: make this piece of code |
|
1268 |
} |
|
1269 |
||
1270 |
||
1271 |
// We are already sending him back |
|
1272 |
if (AiNew_GetSpecialVehicleFlag(p, v) & AI_VEHICLEFLAG_SELL) { |
|
1330
8a67d04016ce
(svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents:
1313
diff
changeset
|
1273 |
if (v->type == VEH_Road && IsTileDepotType(v->tile, TRANSPORT_ROAD) && |
2365 | 1274 |
(v->vehstatus&VS_STOPPED)) { |
145 | 1275 |
// We are at the depot, sell the vehicle |
2682
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2635
diff
changeset
|
1276 |
AI_DoCommand(0, v->index, 0, DC_EXEC, CMD_SELL_ROAD_VEH); |
145 | 1277 |
} |
1278 |
return; |
|
1279 |
} |
|
1280 |
||
1281 |
if (!AiNew_SetSpecialVehicleFlag(p, v, AI_VEHICLEFLAG_SELL)) return; |
|
1282 |
{ |
|
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:
1490
diff
changeset
|
1283 |
int ret = 0; |
145 | 1284 |
if (v->type == VEH_Road) |
2682
94ca0b4dc53f
(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents:
2635
diff
changeset
|
1285 |
ret = AI_DoCommand(0, v->index, 0, DC_EXEC, CMD_SEND_ROADVEH_TO_DEPOT); |
145 | 1286 |
// This means we can not find a depot :s |
2365 | 1287 |
// if (CmdFailed(ret)) |
145 | 1288 |
} |
1289 |
} |
|
1290 |
} |
|
1291 |
} |
|
1292 |
||
2365 | 1293 |
|
145 | 1294 |
// Checks all vehicles if they are still valid and make money and stuff |
2365 | 1295 |
static void AiNew_State_CheckAllVehicles(Player *p) |
1296 |
{ |
|
145 | 1297 |
Vehicle *v; |
1298 |
||
1299 |
FOR_ALL_VEHICLES(v) { |
|
1300 |
if (v->type == 0) continue; |
|
1301 |
if (v->owner != p->index) continue; |
|
1302 |
// Currently, we only know how to handle road-vehicles |
|
1303 |
if (v->type != VEH_Road) continue; |
|
1304 |
||
1305 |
AiNew_CheckVehicle(p, v); |
|
1306 |
} |
|
1307 |
||
1308 |
p->ainew.state = AI_STATE_ACTION_DONE; |
|
1309 |
} |
|
1310 |
||
2365 | 1311 |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1312 |
// Using the technique simular to the original AI |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1313 |
// Keeps things logical |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1314 |
// It really should be in the same order as the AI_STATE's are! |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1315 |
static AiNew_StateFunction* const _ainew_state[] = { |
2365 | 1316 |
NULL, |
1317 |
AiNew_State_FirstTime, |
|
1318 |
AiNew_State_Nothing, |
|
1319 |
AiNew_State_WakeUp, |
|
1320 |
AiNew_State_LocateRoute, |
|
1321 |
AiNew_State_FindStation, |
|
1322 |
AiNew_State_FindPath, |
|
1323 |
AiNew_State_FindDepot, |
|
1324 |
AiNew_State_VerifyRoute, |
|
1325 |
AiNew_State_BuildStation, |
|
1326 |
AiNew_State_BuildPath, |
|
1327 |
AiNew_State_BuildDepot, |
|
1328 |
AiNew_State_BuildVehicle, |
|
1329 |
AiNew_State_GiveOrders, |
|
1330 |
AiNew_State_StartVehicle, |
|
1331 |
AiNew_State_RepayMoney, |
|
145 | 1332 |
AiNew_State_CheckAllVehicles, |
2365 | 1333 |
AiNew_State_ActionDone, |
1334 |
NULL, |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1335 |
}; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1336 |
|
2365 | 1337 |
static void AiNew_OnTick(Player *p) |
1338 |
{ |
|
1339 |
if (_ainew_state[p->ainew.state] != NULL) |
|
1340 |
_ainew_state[p->ainew.state](p); |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1341 |
} |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1342 |
|
2365 | 1343 |
|
1344 |
void AiNewDoGameLoop(Player *p) |
|
1345 |
{ |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1346 |
if (p->ainew.state == AI_STATE_STARTUP) { |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1347 |
// The AI just got alive! |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1348 |
p->ainew.state = AI_STATE_FIRST_TIME; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1349 |
p->ainew.tick = 0; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1350 |
|
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1351 |
// Only startup the AI |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1352 |
return; |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1353 |
} |
145 | 1354 |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1355 |
// We keep a ticker. We use it for competitor_speed |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1356 |
p->ainew.tick++; |
145 | 1357 |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1358 |
// If we come here, we can do a tick.. do so! |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1359 |
AiNew_OnTick(p); |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
95
diff
changeset
|
1360 |
} |