author | bjarni |
Thu, 27 Jan 2005 21:00:05 +0000 | |
changeset 1196 | 115f46e3807d |
parent 1151 | 614cbcb5b9fe |
child 1197 | 4322cf8d6ae7 |
permissions | -rw-r--r-- |
0 | 1 |
#include "stdafx.h" |
2 |
#include "ttd.h" |
|
507
8aa8100b0b22
(svn r815) Include strings.h only in the files which need it.
tron
parents:
493
diff
changeset
|
3 |
#include "table/strings.h" |
679
e959706a3e4d
(svn r1117) Move map arrays and some related macros into their own files map.c and map.h
tron
parents:
593
diff
changeset
|
4 |
#include "map.h" |
0 | 5 |
#include "vehicle.h" |
6 |
#include "engine.h" |
|
7 |
#include "command.h" |
|
8 |
#include "station.h" |
|
9 |
#include "news.h" |
|
10 |
#include "gfx.h" |
|
11 |
#include "pathfind.h" |
|
12 |
#include "player.h" |
|
337
66647f97e7c0
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
241
diff
changeset
|
13 |
#include "sound.h" |
0 | 14 |
|
15 |
void ShowRoadVehViewWindow(Vehicle *v); |
|
16 |
||
17 |
static const uint16 _roadveh_images[63] = { |
|
18 |
0xCD4, 0xCDC, 0xCE4, 0xCEC, 0xCF4, 0xCFC, 0xD0C, 0xD14, |
|
19 |
0xD24, 0xD1C, 0xD2C, 0xD04, 0xD1C, 0xD24, 0xD6C, 0xD74, |
|
20 |
0xD7C, 0xC14, 0xC1C, 0xC24, 0xC2C, 0xC34, 0xC3C, 0xC4C, |
|
21 |
0xC54, 0xC64, 0xC5C, 0xC6C, 0xC44, 0xC5C, 0xC64, 0xCAC, |
|
22 |
0xCB4, 0xCBC, 0xD94, 0xD9C, 0xDA4, 0xDAC, 0xDB4, 0xDBC, |
|
23 |
0xDCC, 0xDD4, 0xDE4, 0xDDC, 0xDEC, 0xDC4, 0xDDC, 0xDE4, |
|
24 |
0xE2C, 0xE34, 0xE3C, 0xC14, 0xC1C, 0xC2C, 0xC3C, 0xC4C, |
|
25 |
0xC5C, 0xC64, 0xC6C, 0xC74, 0xC84, 0xC94, 0xCA4 |
|
26 |
}; |
|
27 |
||
28 |
static const uint16 _roadveh_full_adder[63] = { |
|
29 |
0, 88, 0, 0, 0, 0, 48, 48, |
|
30 |
48, 48, 0, 0, 64, 64, 0, 16, |
|
31 |
16, 0, 88, 0, 0, 0, 0, 48, |
|
32 |
48, 48, 48, 0, 0, 64, 64, 0, |
|
33 |
16, 16, 0, 88, 0, 0, 0, 0, |
|
34 |
48, 48, 48, 48, 0, 0, 64, 64, |
|
35 |
0, 16, 16, 0, 8, 8, 8, 8, |
|
36 |
0, 0, 0, 8, 8, 8, 8 |
|
37 |
}; |
|
38 |
||
39 |
||
40 |
static const uint16 _road_veh_fp_ax_or[4] = { |
|
41 |
0x100,0x200,1,2, |
|
42 |
}; |
|
43 |
||
44 |
static const uint16 _road_veh_fp_ax_and[4] = { |
|
45 |
0x1009, 0x16, 0x520, 0x2A00 |
|
46 |
}; |
|
47 |
||
48 |
static const byte _road_reverse_table[4] = { |
|
49 |
6, 7, 14, 15 |
|
50 |
}; |
|
51 |
||
52 |
static const uint16 _road_pf_table_3[4] = { |
|
53 |
0x910, 0x1600, 0x2005, 0x2A |
|
54 |
}; |
|
55 |
||
56 |
int GetRoadVehImage(Vehicle *v, byte direction) |
|
57 |
{ |
|
58 |
int img = v->spritenum; |
|
59 |
int image; |
|
60 |
||
61 |
if (is_custom_sprite(img)) { |
|
62 |
image = GetCustomVehicleSprite(v, direction); |
|
63 |
if (image) return image; |
|
64 |
img = _engine_original_sprites[v->engine_type]; |
|
65 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
66 |
|
0 | 67 |
image = direction + _roadveh_images[img]; |
68 |
if (v->cargo_count >= (v->cargo_cap >> 1)) |
|
69 |
image += _roadveh_full_adder[img]; |
|
70 |
return image; |
|
71 |
} |
|
72 |
||
73 |
void DrawRoadVehEngine(int x, int y, int engine, uint32 image_ormod) |
|
74 |
{ |
|
538
5dd99f3b7eca
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
75 |
int spritenum = RoadVehInfo(engine)->image_index; |
378
9f170b63d48e
(svn r567) -newgrf: Support for road vehicles customization (seems to work at
celestar
parents:
374
diff
changeset
|
76 |
|
9f170b63d48e
(svn r567) -newgrf: Support for road vehicles customization (seems to work at
celestar
parents:
374
diff
changeset
|
77 |
if (is_custom_sprite(spritenum)) { |
9f170b63d48e
(svn r567) -newgrf: Support for road vehicles customization (seems to work at
celestar
parents:
374
diff
changeset
|
78 |
int sprite = GetCustomVehicleIcon(engine, 6); |
9f170b63d48e
(svn r567) -newgrf: Support for road vehicles customization (seems to work at
celestar
parents:
374
diff
changeset
|
79 |
|
9f170b63d48e
(svn r567) -newgrf: Support for road vehicles customization (seems to work at
celestar
parents:
374
diff
changeset
|
80 |
if (sprite) { |
9f170b63d48e
(svn r567) -newgrf: Support for road vehicles customization (seems to work at
celestar
parents:
374
diff
changeset
|
81 |
DrawSprite(sprite | image_ormod, x, y); |
9f170b63d48e
(svn r567) -newgrf: Support for road vehicles customization (seems to work at
celestar
parents:
374
diff
changeset
|
82 |
return; |
9f170b63d48e
(svn r567) -newgrf: Support for road vehicles customization (seems to work at
celestar
parents:
374
diff
changeset
|
83 |
} |
9f170b63d48e
(svn r567) -newgrf: Support for road vehicles customization (seems to work at
celestar
parents:
374
diff
changeset
|
84 |
spritenum = _engine_original_sprites[engine]; |
9f170b63d48e
(svn r567) -newgrf: Support for road vehicles customization (seems to work at
celestar
parents:
374
diff
changeset
|
85 |
} |
9f170b63d48e
(svn r567) -newgrf: Support for road vehicles customization (seems to work at
celestar
parents:
374
diff
changeset
|
86 |
DrawSprite((6 + _roadveh_images[spritenum]) | image_ormod, x, y); |
0 | 87 |
} |
88 |
||
89 |
void DrawRoadVehEngineInfo(int engine, int x, int y, int maxw) |
|
90 |
{ |
|
538
5dd99f3b7eca
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
91 |
const RoadVehicleInfo *rvi = RoadVehInfo(engine); |
0 | 92 |
|
538
5dd99f3b7eca
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
93 |
SetDParam(0, ((_price.roadveh_base >> 3) * rvi->base_cost) >> 5); |
5dd99f3b7eca
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
94 |
SetDParam(1, rvi->max_speed * 10 >> 5); |
5dd99f3b7eca
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
95 |
SetDParam(2, rvi->running_cost * _price.roadveh_running >> 8); |
5dd99f3b7eca
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
96 |
|
5dd99f3b7eca
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
97 |
SetDParam(4, rvi->capacity); |
5dd99f3b7eca
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
98 |
SetDParam(3, _cargoc.names_long_p[rvi->cargo_type]); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
99 |
|
0 | 100 |
DrawStringMultiCenter(x, y, STR_902A_COST_SPEED_RUNNING_COST, maxw); |
101 |
} |
|
102 |
||
812
79c99885ad00
(svn r1283) -Add: AutoRenew is now a client-side patch instead of a game-side patch
truelight
parents:
755
diff
changeset
|
103 |
int32 EstimateRoadVehCost(byte engine_type) |
0 | 104 |
{ |
538
5dd99f3b7eca
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
105 |
return ((_price.roadveh_base >> 3) * RoadVehInfo(engine_type)->base_cost) >> 5; |
0 | 106 |
} |
107 |
||
842 | 108 |
// p1 = engine_type |
109 |
// p2 not used |
|
0 | 110 |
int32 CmdBuildRoadVeh(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
111 |
{ |
|
112 |
int32 cost; |
|
113 |
Vehicle *v; |
|
114 |
byte unit_num; |
|
115 |
uint tile = TILE_FROM_XY(x,y); |
|
116 |
Engine *e; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
117 |
|
1196
115f46e3807d
(svn r1700) - Fix: Hacked clients can no longer be used to build vehicles that are not available yet (Hackykid)
bjarni
parents:
1151
diff
changeset
|
118 |
if (!(IsEngineBuildable(p1, VEH_Road))) return CMD_ERROR; |
115f46e3807d
(svn r1700) - Fix: Hacked clients can no longer be used to build vehicles that are not available yet (Hackykid)
bjarni
parents:
1151
diff
changeset
|
119 |
|
0 | 120 |
SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES); |
121 |
||
122 |
cost = EstimateRoadVehCost(p1); |
|
123 |
if (flags & DC_QUERY_COST) |
|
124 |
return cost; |
|
125 |
||
126 |
v = AllocateVehicle(); |
|
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
926
diff
changeset
|
127 |
if (v == NULL || IsOrderPoolFull()) |
0 | 128 |
return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME); |
129 |
||
130 |
/* find the first free roadveh id */ |
|
131 |
unit_num = GetFreeUnitNumber(VEH_Road); |
|
132 |
if (unit_num > _patches.max_roadveh) |
|
133 |
return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME); |
|
134 |
||
135 |
if (flags & DC_EXEC) { |
|
538
5dd99f3b7eca
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
136 |
const RoadVehicleInfo *rvi = RoadVehInfo(p1); |
5dd99f3b7eca
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
137 |
|
0 | 138 |
v->unitnumber = unit_num; |
139 |
v->direction = 0; |
|
140 |
v->owner = _current_player; |
|
141 |
||
142 |
v->tile = tile; |
|
926
bd4312619522
(svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents:
919
diff
changeset
|
143 |
x = TileX(tile) * 16 + 8; |
bd4312619522
(svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents:
919
diff
changeset
|
144 |
y = TileY(tile) * 16 + 8; |
0 | 145 |
v->x_pos = x; |
146 |
v->y_pos = y; |
|
147 |
v->z_pos = GetSlopeZ(x,y); |
|
148 |
v->z_height = 6; |
|
149 |
||
150 |
v->u.road.state = 254; |
|
151 |
v->vehstatus = VS_HIDDEN|VS_STOPPED|VS_DEFPAL; |
|
152 |
||
538
5dd99f3b7eca
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
153 |
v->spritenum = rvi->image_index; |
5dd99f3b7eca
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
154 |
v->cargo_type = rvi->cargo_type; |
5dd99f3b7eca
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
155 |
v->cargo_cap = rvi->capacity; |
0 | 156 |
// v->cargo_count = 0; |
157 |
v->value = cost; |
|
158 |
// v->day_counter = 0; |
|
159 |
// v->next_order_param = v->next_order = 0; |
|
160 |
// v->load_unload_time_rem = 0; |
|
161 |
// v->progress = 0; |
|
162 |
||
163 |
// v->u.road.unk2 = 0; |
|
164 |
// v->u.road.overtaking = 0; |
|
165 |
||
817
4f9377b7fd2b
(svn r1288) -Codechange: changed _map2 to an uint16. It is still saved and loaded as
truelight
parents:
812
diff
changeset
|
166 |
v->last_station_visited = 0xFFFF; |
538
5dd99f3b7eca
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
167 |
v->max_speed = rvi->max_speed; |
0 | 168 |
v->engine_type = (byte)p1; |
169 |
||
170 |
e = &_engines[p1]; |
|
171 |
v->reliability = e->reliability; |
|
172 |
v->reliability_spd_dec = e->reliability_spd_dec; |
|
173 |
v->max_age = e->lifelength * 366; |
|
174 |
_new_roadveh_id = v->index; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
175 |
|
0 | 176 |
v->string_id = STR_SV_ROADVEH_NAME; |
177 |
||
178 |
v->service_interval = _patches.servint_roadveh; |
|
179 |
||
180 |
v->date_of_last_service = _date; |
|
181 |
v->build_year = _cur_year; |
|
182 |
||
183 |
v->type = VEH_Road; |
|
184 |
v->cur_image = 0xC15; |
|
185 |
||
186 |
VehiclePositionChanged(v); |
|
187 |
||
188 |
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile); |
|
588 | 189 |
RebuildVehicleLists(); |
0 | 190 |
InvalidateWindow(WC_COMPANY, v->owner); |
191 |
} |
|
192 |
||
1128
d3ffc98b92ad
(svn r1629) added a counter to tell how many engines you have of each type to the autoreplace vehicle windows and made them show only the vehicles you actually have in the left list.
bjarni
parents:
1095
diff
changeset
|
193 |
InvalidateWindow(WC_REPLACE_VEHICLE, VEH_Road); // updates the replace Road window |
d3ffc98b92ad
(svn r1629) added a counter to tell how many engines you have of each type to the autoreplace vehicle windows and made them show only the vehicles you actually have in the left list.
bjarni
parents:
1095
diff
changeset
|
194 |
|
0 | 195 |
return cost; |
196 |
} |
|
197 |
||
198 |
// p1 = vehicle |
|
199 |
int32 CmdStartStopRoadVeh(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
|
200 |
{ |
|
201 |
Vehicle *v; |
|
202 |
||
919
b0d6c7642f99
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
904
diff
changeset
|
203 |
v = GetVehicle(p1); |
0 | 204 |
|
205 |
if (v->type != VEH_Road || !CheckOwnership(v->owner)) |
|
206 |
return CMD_ERROR; |
|
207 |
||
208 |
if (flags & DC_EXEC) { |
|
209 |
v->vehstatus ^= VS_STOPPED; |
|
755
823358e83abf
(svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents:
679
diff
changeset
|
210 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); |
0 | 211 |
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile); |
212 |
} |
|
213 |
||
214 |
return 0; |
|
215 |
} |
|
216 |
||
919
b0d6c7642f99
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
904
diff
changeset
|
217 |
// p1 = vehicle index in GetVehicle() |
842 | 218 |
// p2 not used |
0 | 219 |
int32 CmdSellRoadVeh(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
220 |
{ |
|
221 |
Vehicle *v; |
|
222 |
||
223 |
SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
224 |
|
919
b0d6c7642f99
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
904
diff
changeset
|
225 |
v = GetVehicle(p1); |
0 | 226 |
|
227 |
if (v->type != VEH_Road || !CheckOwnership(v->owner)) |
|
228 |
return CMD_ERROR; |
|
229 |
||
230 |
if (!IsRoadDepotTile(v->tile) || v->u.road.state != 254 || !(v->vehstatus&VS_STOPPED)) |
|
231 |
return_cmd_error(STR_9013_MUST_BE_STOPPED_INSIDE); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
232 |
|
0 | 233 |
if (flags & DC_EXEC) { |
234 |
// Invalidate depot |
|
235 |
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile); |
|
588 | 236 |
RebuildVehicleLists(); |
0 | 237 |
InvalidateWindow(WC_COMPANY, v->owner); |
238 |
DeleteWindowById(WC_VEHICLE_VIEW, v->index); |
|
239 |
DeleteVehicle(v); |
|
240 |
} |
|
1128
d3ffc98b92ad
(svn r1629) added a counter to tell how many engines you have of each type to the autoreplace vehicle windows and made them show only the vehicles you actually have in the left list.
bjarni
parents:
1095
diff
changeset
|
241 |
InvalidateWindow(WC_REPLACE_VEHICLE, VEH_Road); // updates the replace Road window |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
242 |
|
0 | 243 |
return -(int32)v->value; |
244 |
} |
|
245 |
||
246 |
typedef struct RoadFindDepotData { |
|
247 |
uint best_length; |
|
248 |
uint tile; |
|
249 |
byte owner; |
|
250 |
} RoadFindDepotData; |
|
251 |
||
252 |
static const byte _road_pf_directions[16] = { |
|
253 |
0, 1, 0, 1, 2, 1, 255, 255, |
|
254 |
2, 3, 3, 2, 3, 0, 255, 255, |
|
255 |
}; |
|
256 |
||
257 |
static bool EnumRoadSignalFindDepot(uint tile, RoadFindDepotData *rfdd, int track, uint length, byte *state) |
|
258 |
{ |
|
904
bff808a1c7f0
(svn r1390) Use TileOffsByDir() instead of home brewed table
tron
parents:
900
diff
changeset
|
259 |
tile += TileOffsByDir(_road_pf_directions[track]); |
0 | 260 |
|
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
|
261 |
if (IsTileType(tile, MP_STREET) && |
0 | 262 |
(_map5[tile] & 0xF0) == 0x20 && |
263 |
_map_owner[tile] == rfdd->owner) { |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
264 |
|
0 | 265 |
if (length < rfdd->best_length) { |
266 |
rfdd->best_length = length; |
|
267 |
rfdd->tile = tile; |
|
268 |
} |
|
269 |
} |
|
270 |
return false; |
|
271 |
} |
|
272 |
||
273 |
static int FindClosestRoadDepot(Vehicle *v) |
|
274 |
{ |
|
275 |
uint tile = v->tile; |
|
276 |
int i; |
|
277 |
RoadFindDepotData rfdd; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
278 |
|
0 | 279 |
if (v->u.road.state == 255) { tile = GetVehicleOutOfTunnelTile(v); } |
280 |
||
281 |
rfdd.owner = v->owner; |
|
282 |
rfdd.best_length = (uint)-1; |
|
283 |
||
284 |
/* search in all directions */ |
|
285 |
for(i=0; i!=4; i++) |
|
159
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
156
diff
changeset
|
286 |
FollowTrack(tile, 0x2000 | TRANSPORT_ROAD, i, (TPFEnumProc*)EnumRoadSignalFindDepot, NULL, &rfdd); |
0 | 287 |
|
288 |
if (rfdd.best_length == (uint)-1) |
|
289 |
return -1; |
|
290 |
||
291 |
return GetDepotByTile(rfdd.tile); |
|
292 |
} |
|
293 |
||
294 |
int32 CmdSendRoadVehToDepot(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
|
295 |
{ |
|
919
b0d6c7642f99
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
904
diff
changeset
|
296 |
Vehicle *v = GetVehicle(p1); |
0 | 297 |
int depot; |
298 |
||
299 |
if (v->type != VEH_Road || !CheckOwnership(v->owner)) |
|
300 |
return CMD_ERROR; |
|
301 |
||
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
302 |
if (v->current_order.type == OT_GOTO_DEPOT) { |
0 | 303 |
if (flags & DC_EXEC) { |
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
304 |
if (v->current_order.flags & OF_UNLOAD) |
0 | 305 |
v->cur_order_index++; |
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
306 |
v->current_order.type = OT_DUMMY; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
307 |
v->current_order.flags = 0; |
755
823358e83abf
(svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents:
679
diff
changeset
|
308 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); |
0 | 309 |
} |
310 |
return 0; |
|
311 |
} |
|
312 |
||
313 |
depot = FindClosestRoadDepot(v); |
|
314 |
if (depot < 0) |
|
315 |
return_cmd_error(STR_9019_UNABLE_TO_FIND_LOCAL_DEPOT); |
|
316 |
||
317 |
if (flags & DC_EXEC) { |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
318 |
v->current_order.type = OT_GOTO_DEPOT; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
319 |
v->current_order.flags = OF_NON_STOP | OF_FULL_LOAD; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
320 |
v->current_order.station = (byte)depot; |
0 | 321 |
v->dest_tile = _depots[depot].xy; |
755
823358e83abf
(svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents:
679
diff
changeset
|
322 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); |
0 | 323 |
} |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
324 |
|
0 | 325 |
return 0; |
326 |
} |
|
327 |
||
328 |
int32 CmdTurnRoadVeh(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
|
329 |
{ |
|
330 |
Vehicle *v; |
|
331 |
||
919
b0d6c7642f99
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
904
diff
changeset
|
332 |
v = GetVehicle(p1); |
0 | 333 |
|
334 |
if (v->type != VEH_Road || !CheckOwnership(v->owner)) |
|
335 |
return CMD_ERROR; |
|
336 |
||
337 |
if (v->vehstatus & (VS_HIDDEN|VS_STOPPED) || |
|
338 |
v->u.road.crashed_ctr != 0 || |
|
339 |
v->breakdown_ctr != 0 || |
|
340 |
v->u.road.overtaking != 0 || |
|
341 |
v->cur_speed < 5) { |
|
342 |
_error_message = STR_EMPTY; |
|
343 |
return CMD_ERROR; |
|
344 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
345 |
|
0 | 346 |
if (flags & DC_EXEC) { |
347 |
v->u.road.reverse_ctr = 180; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
348 |
} |
0 | 349 |
|
350 |
return 0; |
|
351 |
} |
|
352 |
||
353 |
int32 CmdChangeRoadVehServiceInt(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
|
354 |
{ |
|
355 |
Vehicle *v; |
|
356 |
||
919
b0d6c7642f99
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
904
diff
changeset
|
357 |
v = GetVehicle(p1); |
0 | 358 |
|
359 |
if (v->type != VEH_Road || !CheckOwnership(v->owner)) |
|
360 |
return CMD_ERROR; |
|
361 |
||
362 |
if (flags & DC_EXEC) { |
|
363 |
v->service_interval = (uint16)p2; |
|
364 |
InvalidateWindowWidget(WC_VEHICLE_DETAILS, v->index, 7); |
|
365 |
} |
|
366 |
||
367 |
return 0; |
|
368 |
} |
|
369 |
||
370 |
||
371 |
static void MarkRoadVehDirty(Vehicle *v) |
|
372 |
{ |
|
373 |
v->cur_image = GetRoadVehImage(v, v->direction); |
|
374 |
MarkAllViewportsDirty(v->left_coord, v->top_coord, v->right_coord + 1, v->bottom_coord + 1); |
|
375 |
} |
|
376 |
||
377 |
static void UpdateRoadVehDeltaXY(Vehicle *v) |
|
378 |
{ |
|
379 |
#define MKIT(a,b,c,d) ((a&0xFF)<<24) | ((b&0xFF)<<16) | ((c&0xFF)<<8) | ((d&0xFF)<<0) |
|
380 |
static const uint32 _delta_xy_table[8] = { |
|
381 |
MKIT(3, 3, -1, -1), |
|
382 |
MKIT(3, 7, -1, -3), |
|
383 |
MKIT(3, 3, -1, -1), |
|
384 |
MKIT(7, 3, -3, -1), |
|
385 |
MKIT(3, 3, -1, -1), |
|
386 |
MKIT(3, 7, -1, -3), |
|
387 |
MKIT(3, 3, -1, -1), |
|
388 |
MKIT(7, 3, -3, -1), |
|
389 |
}; |
|
390 |
#undef MKIT |
|
391 |
uint32 x = _delta_xy_table[v->direction]; |
|
392 |
v->x_offs = (byte)x; |
|
393 |
v->y_offs = (byte)(x>>=8); |
|
394 |
v->sprite_width = (byte)(x>>=8); |
|
395 |
v->sprite_height = (byte)(x>>=8); |
|
396 |
} |
|
397 |
||
398 |
static void ClearCrashedStation(Vehicle *v) |
|
399 |
{ |
|
400 |
uint tile = v->tile; |
|
919
b0d6c7642f99
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
904
diff
changeset
|
401 |
Station *st = GetStation(_map2[tile]); |
0 | 402 |
byte *b, bb; |
403 |
||
404 |
b = (_map5[tile] >= 0x47) ? &st->bus_stop_status : &st->truck_stop_status; |
|
405 |
||
406 |
bb = *b; |
|
407 |
||
408 |
// mark station as not busy |
|
409 |
bb &= ~0x80; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
410 |
|
0 | 411 |
// free parking bay |
412 |
bb |= (v->u.road.state&0x02)?2:1; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
413 |
|
0 | 414 |
*b = bb; |
415 |
} |
|
416 |
||
417 |
static void RoadVehDelete(Vehicle *v) |
|
418 |
{ |
|
419 |
DeleteWindowById(WC_VEHICLE_VIEW, v->index); |
|
420 |
InvalidateWindow(WC_VEHICLE_DETAILS, v->index); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
421 |
|
588 | 422 |
RebuildVehicleLists(); |
0 | 423 |
InvalidateWindow(WC_COMPANY, v->owner); |
424 |
||
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
|
425 |
if (IsTileType(v->tile, MP_STATION)) |
0 | 426 |
ClearCrashedStation(v); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
427 |
|
0 | 428 |
BeginVehicleMove(v); |
429 |
EndVehicleMove(v); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
430 |
|
0 | 431 |
DeleteVehicle(v); |
432 |
} |
|
433 |
||
434 |
static byte SetRoadVehPosition(Vehicle *v, int x, int y) |
|
435 |
{ |
|
436 |
byte new_z, old_z; |
|
437 |
||
438 |
// need this hint so it returns the right z coordinate on bridges. |
|
439 |
_get_z_hint = v->z_pos; |
|
440 |
new_z = GetSlopeZ(v->x_pos=x, v->y_pos=y); |
|
441 |
_get_z_hint = 0; |
|
442 |
||
443 |
old_z = v->z_pos; |
|
444 |
v->z_pos = new_z; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
445 |
|
0 | 446 |
VehiclePositionChanged(v); |
447 |
EndVehicleMove(v); |
|
448 |
return old_z; |
|
449 |
} |
|
450 |
||
451 |
static void RoadVehSetRandomDirection(Vehicle *v) |
|
452 |
{ |
|
453 |
static const int8 _turn_prob[4] = { -1, 0, 0, 1 }; |
|
454 |
uint32 r = Random(); |
|
455 |
v->direction = (v->direction+_turn_prob[r&3])&7; |
|
456 |
BeginVehicleMove(v); |
|
457 |
UpdateRoadVehDeltaXY(v); |
|
458 |
v->cur_image = GetRoadVehImage(v, v->direction); |
|
459 |
SetRoadVehPosition(v, v->x_pos, v->y_pos); |
|
460 |
} |
|
461 |
||
462 |
static void RoadVehIsCrashed(Vehicle *v) |
|
463 |
{ |
|
464 |
v->u.road.crashed_ctr++; |
|
465 |
if (v->u.road.crashed_ctr == 2) { |
|
466 |
CreateEffectVehicleRel(v,4,4,8,EV_CRASHED_SMOKE); |
|
467 |
} else if (v->u.road.crashed_ctr <= 45) { |
|
468 |
if ((v->tick_counter&7)==0) |
|
469 |
RoadVehSetRandomDirection(v); |
|
470 |
} else if (v->u.road.crashed_ctr >= 2220) { |
|
471 |
RoadVehDelete(v); |
|
472 |
} |
|
473 |
} |
|
474 |
||
475 |
static void *EnumCheckRoadVehCrashTrain(Vehicle *v, Vehicle *u) |
|
476 |
{ |
|
477 |
if (v->type != VEH_Train || |
|
478 |
myabs(v->z_pos - u->z_pos) > 6 || |
|
479 |
myabs(v->x_pos - u->x_pos) > 4 || |
|
480 |
myabs(v->y_pos - u->y_pos) > 4) |
|
481 |
return NULL; |
|
482 |
return v; |
|
483 |
} |
|
484 |
||
485 |
static void RoadVehCrash(Vehicle *v) |
|
486 |
{ |
|
487 |
uint16 pass; |
|
488 |
||
489 |
v->u.road.crashed_ctr++; |
|
490 |
v->vehstatus |= VS_CRASHED; |
|
491 |
||
755
823358e83abf
(svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents:
679
diff
changeset
|
492 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); |
0 | 493 |
|
494 |
pass = 1; |
|
495 |
if (v->cargo_type == 0) |
|
496 |
pass += v->cargo_count; |
|
497 |
v->cargo_count = 0; |
|
534
17ab2f22ff74
(svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents:
507
diff
changeset
|
498 |
SetDParam(0, pass); |
0 | 499 |
|
500 |
AddNewsItem(STR_9031_ROAD_VEHICLE_CRASH_DRIVER+(pass!=1), |
|
501 |
NEWS_FLAGS(NM_THIN, NF_VIEWPORT|NF_VEHICLE, NT_ACCIDENT, 0), |
|
502 |
v->index, |
|
503 |
0); |
|
504 |
||
505 |
ModifyStationRatingAround(v->tile, v->owner, -160, 22); |
|
541 | 506 |
SndPlayVehicleFx(SND_12_EXPLOSION, v); |
0 | 507 |
} |
508 |
||
509 |
static void RoadVehCheckTrainCrash(Vehicle *v) |
|
510 |
{ |
|
511 |
uint tile; |
|
512 |
||
513 |
if (v->u.road.state == 255) |
|
514 |
return; |
|
515 |
||
516 |
tile = v->tile; |
|
517 |
||
518 |
// Make sure it's a road/rail crossing |
|
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
|
519 |
if (!IsTileType(tile, MP_STREET) || |
1151
614cbcb5b9fe
(svn r1652) Fix: train and road vehicle lists are now redrawn when vehicles enters/leaves a depot (Hackykid)
bjarni
parents:
1128
diff
changeset
|
520 |
(_map5[tile] & 0xF0) != 0x10) |
0 | 521 |
return; |
522 |
||
523 |
if (VehicleFromPos(tile, v, (VehicleFromPosProc*)EnumCheckRoadVehCrashTrain) != NULL) |
|
524 |
RoadVehCrash(v); |
|
525 |
} |
|
526 |
||
527 |
static void HandleBrokenRoadVeh(Vehicle *v) |
|
528 |
{ |
|
529 |
if (v->breakdown_ctr != 1) { |
|
530 |
v->breakdown_ctr = 1; |
|
531 |
v->cur_speed = 0; |
|
532 |
||
533 |
if (v->breakdowns_since_last_service != 255) |
|
534 |
v->breakdowns_since_last_service++; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
535 |
|
0 | 536 |
InvalidateWindow(WC_VEHICLE_VIEW, v->index); |
537 |
InvalidateWindow(WC_VEHICLE_DETAILS, v->index); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
538 |
|
541 | 539 |
SndPlayVehicleFx((_opt.landscape != LT_CANDY) ? |
540 |
SND_0F_VEHICLE_BREAKDOWN : SND_35_COMEDY_BREAKDOWN, v); |
|
0 | 541 |
|
542 |
if (!(v->vehstatus & VS_HIDDEN)) { |
|
543 |
Vehicle *u = CreateEffectVehicleRel(v, 4, 4, 5, EV_BREAKDOWN_SMOKE); |
|
544 |
if (u) |
|
545 |
u->u.special.unk0 = v->breakdown_delay * 2; |
|
546 |
} |
|
547 |
} |
|
548 |
||
549 |
if (!(v->tick_counter & 1)) { |
|
550 |
if (!--v->breakdown_delay) { |
|
551 |
v->breakdown_ctr = 0; |
|
552 |
InvalidateWindow(WC_VEHICLE_VIEW, v->index); |
|
553 |
} |
|
554 |
} |
|
555 |
} |
|
556 |
||
557 |
static void ProcessRoadVehOrder(Vehicle *v) |
|
558 |
{ |
|
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
926
diff
changeset
|
559 |
const Order *order; |
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
926
diff
changeset
|
560 |
const Station *st; |
0 | 561 |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
562 |
if (v->current_order.type >= OT_GOTO_DEPOT && v->current_order.type <= OT_LEAVESTATION) { |
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
926
diff
changeset
|
563 |
// Let a depot order in the orderlist interrupt. |
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
564 |
if (v->current_order.type != OT_GOTO_DEPOT || |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
565 |
!(v->current_order.flags & OF_UNLOAD)) |
0 | 566 |
return; |
567 |
} |
|
568 |
||
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
569 |
if (v->current_order.type == OT_GOTO_DEPOT && |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
570 |
(v->current_order.flags & (OF_UNLOAD | OF_FULL_LOAD)) == (OF_UNLOAD | OF_FULL_LOAD) && |
593 | 571 |
!VehicleNeedsService(v)) { |
0 | 572 |
v->cur_order_index++; |
573 |
} |
|
574 |
||
575 |
if (v->cur_order_index >= v->num_orders) |
|
576 |
v->cur_order_index = 0; |
|
577 |
||
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
926
diff
changeset
|
578 |
order = GetVehicleOrder(v, v->cur_order_index); |
0 | 579 |
|
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
926
diff
changeset
|
580 |
if (order == NULL) { |
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
581 |
v->current_order.type = OT_NOTHING; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
582 |
v->current_order.flags = 0; |
0 | 583 |
v->dest_tile = 0; |
584 |
return; |
|
585 |
} |
|
586 |
||
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
926
diff
changeset
|
587 |
if (order->type == v->current_order.type && |
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
926
diff
changeset
|
588 |
order->flags == v->current_order.flags && |
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
926
diff
changeset
|
589 |
order->station == v->current_order.station) |
0 | 590 |
return; |
591 |
||
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
926
diff
changeset
|
592 |
v->current_order = *order; |
0 | 593 |
|
594 |
v->dest_tile = 0; |
|
595 |
||
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
926
diff
changeset
|
596 |
if (order->type == OT_GOTO_STATION) { |
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
926
diff
changeset
|
597 |
if (order->station == v->last_station_visited) |
817
4f9377b7fd2b
(svn r1288) -Codechange: changed _map2 to an uint16. It is still saved and loaded as
truelight
parents:
812
diff
changeset
|
598 |
v->last_station_visited = 0xFFFF; |
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
926
diff
changeset
|
599 |
|
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
926
diff
changeset
|
600 |
st = GetStation(order->station); |
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
926
diff
changeset
|
601 |
v->dest_tile = v->cargo_type == CT_PASSENGERS ? st->bus_tile : st->lorry_tile; |
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
926
diff
changeset
|
602 |
} else if (order->type == OT_GOTO_DEPOT) { |
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
926
diff
changeset
|
603 |
v->dest_tile = _depots[order->station].xy; |
0 | 604 |
} |
605 |
||
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
926
diff
changeset
|
606 |
InvalidateVehicleOrder(v); |
0 | 607 |
} |
608 |
||
609 |
static void HandleRoadVehLoading(Vehicle *v) |
|
610 |
{ |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
611 |
if (v->current_order.type == OT_NOTHING) |
0 | 612 |
return; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
613 |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
614 |
if (v->current_order.type != OT_DUMMY) { |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
615 |
if (v->current_order.type != OT_LOADING) |
0 | 616 |
return; |
617 |
||
618 |
if (--v->load_unload_time_rem) |
|
619 |
return; |
|
620 |
||
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
621 |
if (v->current_order.flags & OF_FULL_LOAD && CanFillVehicle(v)) { |
0 | 622 |
SET_EXPENSES_TYPE(EXPENSES_ROADVEH_INC); |
623 |
if (LoadUnloadVehicle(v)) { |
|
624 |
InvalidateWindow(WC_ROADVEH_LIST, v->owner); |
|
625 |
MarkRoadVehDirty(v); |
|
626 |
} |
|
627 |
return; |
|
628 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
629 |
|
0 | 630 |
{ |
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
631 |
Order b = v->current_order; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
632 |
v->current_order.type = OT_LEAVESTATION; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
633 |
v->current_order.flags = 0; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
634 |
if (!(b.flags & OF_NON_STOP)) |
0 | 635 |
return; |
636 |
} |
|
637 |
} |
|
638 |
||
639 |
v->cur_order_index++; |
|
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
926
diff
changeset
|
640 |
InvalidateVehicleOrder(v); |
0 | 641 |
} |
642 |
||
643 |
static void StartRoadVehSound(Vehicle *v) |
|
644 |
{ |
|
541 | 645 |
SoundFx s = RoadVehInfo(v->engine_type)->sfx; |
646 |
if (s == SND_19_BUS_START_PULL_AWAY && (v->tick_counter & 3) == 0) |
|
647 |
s = SND_1A_BUS_START_PULL_AWAY_WITH_HORN; |
|
0 | 648 |
SndPlayVehicleFx(s, v); |
649 |
} |
|
650 |
||
651 |
typedef struct RoadVehFindData { |
|
652 |
int x,y; |
|
653 |
Vehicle *veh; |
|
654 |
byte dir; |
|
655 |
} RoadVehFindData; |
|
656 |
||
1095 | 657 |
static void *EnumCheckRoadVehClose(Vehicle *v, RoadVehFindData *rvf) |
0 | 658 |
{ |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
659 |
static const short _dists[] = { |
63
53cc59601b3c
(svn r64) Fix: Road vehicles don't get stuck any more (Truelight + Celestar
dominik
parents:
19
diff
changeset
|
660 |
-4, -8, -4, -1, 4, 8, 4, 1, |
53cc59601b3c
(svn r64) Fix: Road vehicles don't get stuck any more (Truelight + Celestar
dominik
parents:
19
diff
changeset
|
661 |
-4, -1, 4, 8, 4, 1, -4, -8, |
0 | 662 |
}; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
663 |
|
63
53cc59601b3c
(svn r64) Fix: Road vehicles don't get stuck any more (Truelight + Celestar
dominik
parents:
19
diff
changeset
|
664 |
short x_diff = v->x_pos - rvf->x; |
53cc59601b3c
(svn r64) Fix: Road vehicles don't get stuck any more (Truelight + Celestar
dominik
parents:
19
diff
changeset
|
665 |
short y_diff = v->y_pos - rvf->y; |
0 | 666 |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
667 |
if (rvf->veh == v || |
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
668 |
v->type != VEH_Road || |
0 | 669 |
v->u.road.state == 254 || |
670 |
myabs(v->z_pos - rvf->veh->z_pos) > 6 || |
|
671 |
v->direction != rvf->dir || |
|
63
53cc59601b3c
(svn r64) Fix: Road vehicles don't get stuck any more (Truelight + Celestar
dominik
parents:
19
diff
changeset
|
672 |
(_dists[v->direction] < 0 && (x_diff <= _dists[v->direction] || x_diff > 0)) || |
53cc59601b3c
(svn r64) Fix: Road vehicles don't get stuck any more (Truelight + Celestar
dominik
parents:
19
diff
changeset
|
673 |
(_dists[v->direction] > 0 && (x_diff >= _dists[v->direction] || x_diff < 0)) || |
53cc59601b3c
(svn r64) Fix: Road vehicles don't get stuck any more (Truelight + Celestar
dominik
parents:
19
diff
changeset
|
674 |
(_dists[v->direction+8] < 0 && (y_diff <= _dists[v->direction+8] || y_diff > 0)) || |
53cc59601b3c
(svn r64) Fix: Road vehicles don't get stuck any more (Truelight + Celestar
dominik
parents:
19
diff
changeset
|
675 |
(_dists[v->direction+8] > 0 && (y_diff >= _dists[v->direction+8] || y_diff < 0))) |
1151
614cbcb5b9fe
(svn r1652) Fix: train and road vehicle lists are now redrawn when vehicles enters/leaves a depot (Hackykid)
bjarni
parents:
1128
diff
changeset
|
676 |
return NULL; |
0 | 677 |
|
678 |
return v; |
|
679 |
} |
|
680 |
||
681 |
static Vehicle *RoadVehFindCloseTo(Vehicle *v, int x, int y, byte dir) |
|
682 |
{ |
|
683 |
RoadVehFindData rvf; |
|
684 |
Vehicle *u; |
|
685 |
||
686 |
if (v->u.road.reverse_ctr != 0) |
|
687 |
return NULL; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
688 |
|
0 | 689 |
rvf.x = x; |
690 |
rvf.y = y; |
|
691 |
rvf.dir = dir; |
|
692 |
rvf.veh = v; |
|
693 |
u = VehicleFromPos(TILE_FROM_XY(x,y), &rvf, (VehicleFromPosProc*)EnumCheckRoadVehClose); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
694 |
|
63
53cc59601b3c
(svn r64) Fix: Road vehicles don't get stuck any more (Truelight + Celestar
dominik
parents:
19
diff
changeset
|
695 |
// This code protects a roadvehicle from being blocked for ever |
826 | 696 |
// If more than 1480 / 74 days a road vehicle is blocked, it will |
63
53cc59601b3c
(svn r64) Fix: Road vehicles don't get stuck any more (Truelight + Celestar
dominik
parents:
19
diff
changeset
|
697 |
// drive just through it. The ultimate backup-code of TTD. |
53cc59601b3c
(svn r64) Fix: Road vehicles don't get stuck any more (Truelight + Celestar
dominik
parents:
19
diff
changeset
|
698 |
// It can be disabled. |
0 | 699 |
if (u == NULL) { |
700 |
v->u.road.unk2 = 0; |
|
701 |
return NULL; |
|
702 |
} |
|
703 |
||
704 |
if (++v->u.road.unk2 > 1480) |
|
705 |
return NULL; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
706 |
|
0 | 707 |
return u; |
708 |
} |
|
709 |
||
710 |
static void RoadVehArrivesAt(Vehicle *v, Station *st) |
|
711 |
{ |
|
712 |
if (v->engine_type < 123) { |
|
713 |
/* Check if station was ever visited before */ |
|
714 |
if (!(st->had_vehicle_of_type & HVOT_BUS)) { |
|
715 |
uint32 flags; |
|
716 |
||
717 |
st->had_vehicle_of_type |= HVOT_BUS; |
|
534
17ab2f22ff74
(svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents:
507
diff
changeset
|
718 |
SetDParam(0, st->index); |
0 | 719 |
flags = (v->owner == _local_player) ? NEWS_FLAGS(NM_THIN, NF_VIEWPORT|NF_VEHICLE, NT_ARRIVAL_PLAYER, 0) : NEWS_FLAGS(NM_THIN, NF_VIEWPORT|NF_VEHICLE, NT_ARRIVAL_OTHER, 0); |
720 |
AddNewsItem( |
|
721 |
STR_902F_CITIZENS_CELEBRATE_FIRST, |
|
722 |
flags, |
|
723 |
v->index, |
|
724 |
0); |
|
725 |
} |
|
726 |
} else { |
|
727 |
/* Check if station was ever visited before */ |
|
728 |
if (!(st->had_vehicle_of_type & HVOT_TRUCK)) { |
|
729 |
uint32 flags; |
|
730 |
||
731 |
st->had_vehicle_of_type |= HVOT_TRUCK; |
|
534
17ab2f22ff74
(svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents:
507
diff
changeset
|
732 |
SetDParam(0, st->index); |
0 | 733 |
flags = (v->owner == _local_player) ? NEWS_FLAGS(NM_THIN, NF_VIEWPORT|NF_VEHICLE, NT_ARRIVAL_PLAYER, 0) : NEWS_FLAGS(NM_THIN, NF_VIEWPORT|NF_VEHICLE, NT_ARRIVAL_OTHER, 0); |
734 |
AddNewsItem( |
|
735 |
STR_9030_CITIZENS_CELEBRATE_FIRST, |
|
736 |
flags, |
|
737 |
v->index, |
|
738 |
0); |
|
739 |
} |
|
740 |
} |
|
741 |
} |
|
742 |
||
743 |
static bool RoadVehAccelerate(Vehicle *v) |
|
744 |
{ |
|
745 |
uint spd = v->cur_speed + 1 + ((v->u.road.overtaking != 0)?1:0); |
|
746 |
byte t; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
747 |
|
0 | 748 |
// Clamp |
749 |
spd = min(spd, v->max_speed); |
|
750 |
||
751 |
//updates statusbar only if speed have changed to save CPU time |
|
752 |
if (spd != v->cur_speed) { |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
753 |
v->cur_speed = spd; |
0 | 754 |
if (_patches.vehicle_speed) |
755
823358e83abf
(svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents:
679
diff
changeset
|
755 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); |
0 | 756 |
} |
757 |
||
758 |
// Decrease somewhat when turning |
|
759 |
if (!(v->direction&1)) |
|
760 |
spd = spd * 3 >> 2; |
|
761 |
||
762 |
if (spd == 0) |
|
763 |
return false; |
|
764 |
||
765 |
if ((byte)++spd == 0) |
|
766 |
return true; |
|
767 |
||
768 |
v->progress = (t = v->progress) - (byte)spd; |
|
769 |
||
770 |
return (t < v->progress); |
|
771 |
} |
|
772 |
||
773 |
static byte RoadVehGetNewDirection(Vehicle *v, int x, int y) |
|
774 |
{ |
|
775 |
static const byte _roadveh_new_dir[11] = { |
|
776 |
0, 7, 6, 0, |
|
777 |
1, 0, 5, 0, |
|
778 |
2, 3, 4 |
|
779 |
}; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
780 |
|
0 | 781 |
x = x - v->x_pos + 1; |
782 |
y = y - v->y_pos + 1; |
|
783 |
||
784 |
if ((uint)x > 2 || (uint)y > 2) |
|
785 |
return v->direction; |
|
786 |
return _roadveh_new_dir[y*4+x]; |
|
787 |
} |
|
788 |
||
789 |
static byte RoadVehGetSlidingDirection(Vehicle *v, int x, int y) |
|
790 |
{ |
|
791 |
byte b = RoadVehGetNewDirection(v,x,y); |
|
792 |
byte d = v->direction; |
|
793 |
if (b == d) return d; |
|
794 |
d = (d+1)&7; |
|
795 |
if (b==d) return d; |
|
796 |
d = (d-2)&7; |
|
797 |
if (b==d) return d; |
|
798 |
if (b==((d-1)&7)) return d; |
|
799 |
if (b==((d-2)&7)) return d; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
800 |
return (d+2)&7; |
0 | 801 |
} |
802 |
||
803 |
typedef struct OvertakeData { |
|
804 |
Vehicle *u, *v; |
|
805 |
uint tile; |
|
806 |
byte tilebits; |
|
807 |
} OvertakeData; |
|
808 |
||
1095 | 809 |
static void *EnumFindVehToOvertake(Vehicle *v, OvertakeData *od) |
0 | 810 |
{ |
811 |
if (v->tile != (TileIndex)od->tile || |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
812 |
v->type != VEH_Road || |
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
813 |
v == od->u || |
0 | 814 |
v == od->v) |
815 |
return NULL; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
816 |
return v; |
0 | 817 |
} |
818 |
||
819 |
static bool FindRoadVehToOvertake(OvertakeData *od) |
|
820 |
{ |
|
821 |
uint32 bits; |
|
822 |
||
159
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
156
diff
changeset
|
823 |
bits = GetTileTrackStatus(od->tile, TRANSPORT_ROAD)&0x3F; |
0 | 824 |
|
825 |
if (!(od->tilebits & bits) || (bits&0x3C) || (bits & 0x3F3F0000)) |
|
826 |
return true; |
|
827 |
return VehicleFromPos(od->tile, od, (VehicleFromPosProc*)EnumFindVehToOvertake) != NULL; |
|
828 |
} |
|
829 |
||
830 |
static void RoadVehCheckOvertake(Vehicle *v, Vehicle *u) |
|
831 |
{ |
|
832 |
OvertakeData od; |
|
833 |
byte tt; |
|
834 |
||
835 |
od.v = v; |
|
836 |
od.u = u; |
|
837 |
||
838 |
if (u->max_speed >= v->max_speed && |
|
839 |
!(u->vehstatus&VS_STOPPED) && |
|
840 |
u->cur_speed != 0) |
|
841 |
return; |
|
842 |
||
843 |
if (v->direction != u->direction || !(v->direction&1)) |
|
844 |
return; |
|
845 |
||
846 |
if (v->u.road.state >= 32 || (v->u.road.state&7) > 1 ) |
|
847 |
return; |
|
848 |
||
159
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
156
diff
changeset
|
849 |
tt = (byte)(GetTileTrackStatus(v->tile, TRANSPORT_ROAD) & 0x3F); |
0 | 850 |
if ((tt & 3) == 0) |
851 |
return; |
|
852 |
if ((tt & 0x3C) != 0) |
|
853 |
return; |
|
854 |
||
855 |
if (tt == 3) { |
|
856 |
tt = (v->direction&2)?2:1; |
|
857 |
} |
|
858 |
od.tilebits = tt; |
|
859 |
||
860 |
od.tile = v->tile; |
|
861 |
if (FindRoadVehToOvertake(&od)) |
|
862 |
return; |
|
863 |
||
900 | 864 |
od.tile = v->tile + TileOffsByDir(v->direction >> 1); |
0 | 865 |
if (FindRoadVehToOvertake(&od)) |
866 |
return; |
|
867 |
||
868 |
if (od.u->cur_speed == 0 || od.u->vehstatus&VS_STOPPED) { |
|
869 |
v->u.road.overtaking_ctr = 0x11; |
|
870 |
v->u.road.overtaking = 0x10; |
|
871 |
} else { |
|
872 |
// if (FindRoadVehToOvertake(&od)) |
|
873 |
// return; |
|
874 |
v->u.road.overtaking_ctr = 0; |
|
875 |
v->u.road.overtaking = 0x10; |
|
876 |
} |
|
877 |
} |
|
878 |
||
879 |
static void RoadZPosAffectSpeed(Vehicle *v, byte old_z) |
|
880 |
{ |
|
881 |
if (old_z == v->z_pos) |
|
882 |
return; |
|
883 |
||
884 |
if (old_z < v->z_pos) { |
|
885 |
v->cur_speed = v->cur_speed * 232 >> 8; |
|
886 |
} else { |
|
887 |
uint16 spd = v->cur_speed + 2; |
|
888 |
if (spd <= v->max_speed) |
|
889 |
v->cur_speed = spd; |
|
890 |
} |
|
891 |
} |
|
892 |
||
893 |
static int PickRandomBit(uint bits) |
|
894 |
{ |
|
895 |
uint num = 0; |
|
896 |
uint b = bits; |
|
897 |
uint i; |
|
898 |
||
899 |
do { |
|
900 |
if (b & 1) |
|
901 |
num++; |
|
902 |
} while (b >>= 1); |
|
903 |
||
904 |
num = ((uint16)Random() * num >> 16); |
|
905 |
||
906 |
for(i=0; !((bits & 1) && ((int)--num) < 0); bits>>=1,i++); |
|
907 |
return i; |
|
908 |
} |
|
909 |
||
910 |
typedef struct { |
|
911 |
TileIndex dest; |
|
912 |
uint maxtracklen; |
|
913 |
uint mindist; |
|
914 |
} FindRoadToChooseData; |
|
915 |
||
916 |
static bool EnumRoadTrackFindDist(uint tile, FindRoadToChooseData *frd, int track, uint length, byte *state) |
|
917 |
{ |
|
918 |
uint dist = GetTileDist(tile, frd->dest); |
|
919 |
if (dist <= frd->mindist) { |
|
920 |
if (dist != frd->mindist || length < frd->maxtracklen) { |
|
921 |
frd->maxtracklen = length; |
|
922 |
} |
|
923 |
frd->mindist = dist; |
|
924 |
} |
|
925 |
return false; |
|
926 |
} |
|
927 |
||
928 |
// Returns direction to choose |
|
929 |
// or -1 if the direction is currently blocked |
|
930 |
static int RoadFindPathToDest(Vehicle *v, uint tile, int direction) |
|
931 |
{ |
|
932 |
#define return_track(x) {best_track = x; goto found_best_track; } |
|
933 |
||
934 |
uint16 signal; |
|
935 |
uint bitmask; |
|
936 |
uint desttile; |
|
937 |
FindRoadToChooseData frd; |
|
938 |
int best_track; |
|
939 |
uint best_dist, best_maxlen; |
|
940 |
uint i; |
|
941 |
byte m5; |
|
942 |
||
943 |
{ |
|
944 |
uint32 r; |
|
241
e6e62a5e7f52
(svn r242) -Fix: Pathfinding bug for road vehicles introduced in r160 fixed (blathijs)
darkvater
parents:
193
diff
changeset
|
945 |
r = GetTileTrackStatus(tile, TRANSPORT_ROAD); |
0 | 946 |
signal = (uint16)(r >> 16); |
947 |
bitmask = (uint16)r; |
|
948 |
} |
|
949 |
||
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
|
950 |
if (IsTileType(tile, MP_STREET)) { |
241
e6e62a5e7f52
(svn r242) -Fix: Pathfinding bug for road vehicles introduced in r160 fixed (blathijs)
darkvater
parents:
193
diff
changeset
|
951 |
if ((_map5[tile]&0xF0) == 0x20 && v->owner == _map_owner[tile]) |
e6e62a5e7f52
(svn r242) -Fix: Pathfinding bug for road vehicles introduced in r160 fixed (blathijs)
darkvater
parents:
193
diff
changeset
|
952 |
/* Road crossing */ |
e6e62a5e7f52
(svn r242) -Fix: Pathfinding bug for road vehicles introduced in r160 fixed (blathijs)
darkvater
parents:
193
diff
changeset
|
953 |
bitmask |= _road_veh_fp_ax_or[_map5[tile]&3]; |
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
|
954 |
} else if (IsTileType(tile, MP_STATION)) { |
241
e6e62a5e7f52
(svn r242) -Fix: Pathfinding bug for road vehicles introduced in r160 fixed (blathijs)
darkvater
parents:
193
diff
changeset
|
955 |
if (_map_owner[tile] == OWNER_NONE || _map_owner[tile] == v->owner) { |
e6e62a5e7f52
(svn r242) -Fix: Pathfinding bug for road vehicles introduced in r160 fixed (blathijs)
darkvater
parents:
193
diff
changeset
|
956 |
/* Our station */ |
919
b0d6c7642f99
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
904
diff
changeset
|
957 |
Station *st = GetStation(_map2[tile]); |
241
e6e62a5e7f52
(svn r242) -Fix: Pathfinding bug for road vehicles introduced in r160 fixed (blathijs)
darkvater
parents:
193
diff
changeset
|
958 |
byte val = _map5[tile]; |
e6e62a5e7f52
(svn r242) -Fix: Pathfinding bug for road vehicles introduced in r160 fixed (blathijs)
darkvater
parents:
193
diff
changeset
|
959 |
if (v->cargo_type != CT_PASSENGERS) { |
e6e62a5e7f52
(svn r242) -Fix: Pathfinding bug for road vehicles introduced in r160 fixed (blathijs)
darkvater
parents:
193
diff
changeset
|
960 |
if (IS_BYTE_INSIDE(val, 0x43, 0x47) && (_patches.roadveh_queue || st->truck_stop_status&3)) |
e6e62a5e7f52
(svn r242) -Fix: Pathfinding bug for road vehicles introduced in r160 fixed (blathijs)
darkvater
parents:
193
diff
changeset
|
961 |
bitmask |= _road_veh_fp_ax_or[(val-0x43)&3]; |
e6e62a5e7f52
(svn r242) -Fix: Pathfinding bug for road vehicles introduced in r160 fixed (blathijs)
darkvater
parents:
193
diff
changeset
|
962 |
} else { |
e6e62a5e7f52
(svn r242) -Fix: Pathfinding bug for road vehicles introduced in r160 fixed (blathijs)
darkvater
parents:
193
diff
changeset
|
963 |
if (IS_BYTE_INSIDE(val, 0x47, 0x4B) && (_patches.roadveh_queue || st->bus_stop_status&3)) |
e6e62a5e7f52
(svn r242) -Fix: Pathfinding bug for road vehicles introduced in r160 fixed (blathijs)
darkvater
parents:
193
diff
changeset
|
964 |
bitmask |= _road_veh_fp_ax_or[(val-0x47)&3]; |
e6e62a5e7f52
(svn r242) -Fix: Pathfinding bug for road vehicles introduced in r160 fixed (blathijs)
darkvater
parents:
193
diff
changeset
|
965 |
} |
e6e62a5e7f52
(svn r242) -Fix: Pathfinding bug for road vehicles introduced in r160 fixed (blathijs)
darkvater
parents:
193
diff
changeset
|
966 |
} |
e6e62a5e7f52
(svn r242) -Fix: Pathfinding bug for road vehicles introduced in r160 fixed (blathijs)
darkvater
parents:
193
diff
changeset
|
967 |
} |
e6e62a5e7f52
(svn r242) -Fix: Pathfinding bug for road vehicles introduced in r160 fixed (blathijs)
darkvater
parents:
193
diff
changeset
|
968 |
/* The above lookups should be moved to GetTileTrackStatus in the |
e6e62a5e7f52
(svn r242) -Fix: Pathfinding bug for road vehicles introduced in r160 fixed (blathijs)
darkvater
parents:
193
diff
changeset
|
969 |
* future, but that requires more changes to the pathfinder and other |
e6e62a5e7f52
(svn r242) -Fix: Pathfinding bug for road vehicles introduced in r160 fixed (blathijs)
darkvater
parents:
193
diff
changeset
|
970 |
* stuff, probably even more arguments to GTTS. |
e6e62a5e7f52
(svn r242) -Fix: Pathfinding bug for road vehicles introduced in r160 fixed (blathijs)
darkvater
parents:
193
diff
changeset
|
971 |
*/ |
0 | 972 |
|
159
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
156
diff
changeset
|
973 |
/* remove unreachable tracks */ |
0 | 974 |
bitmask &= _road_veh_fp_ax_and[direction]; |
975 |
if (bitmask == 0) { |
|
976 |
// reverse |
|
977 |
return_track(_road_reverse_table[direction]); |
|
978 |
} |
|
979 |
||
980 |
if (v->u.road.reverse_ctr != 0) { |
|
981 |
v->u.road.reverse_ctr = 0; |
|
982 |
if (v->tile != (TileIndex)tile) { |
|
983 |
return_track(_road_reverse_table[direction]); |
|
984 |
} |
|
985 |
} |
|
986 |
||
987 |
desttile = v->dest_tile; |
|
988 |
if (desttile == 0) { |
|
989 |
// Pick a random direction |
|
990 |
return_track(PickRandomBit(bitmask)); |
|
991 |
} |
|
992 |
||
993 |
// Only one direction to choose between? |
|
994 |
if (!(bitmask & (bitmask - 1))) { |
|
995 |
return_track(FindFirstBit2x64(bitmask)); |
|
996 |
} |
|
997 |
||
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
|
998 |
if (IsTileType(desttile, MP_STREET)) { |
0 | 999 |
m5 = _map5[desttile]; |
1000 |
if ((m5&0xF0) == 0x20) |
|
1001 |
goto do_it; |
|
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
|
1002 |
} else if (IsTileType(desttile, MP_STATION)) { |
0 | 1003 |
m5 = _map5[desttile]; |
1004 |
if (IS_BYTE_INSIDE(m5, 0x43, 0x4B)) { |
|
1005 |
m5 -= 0x43; |
|
1006 |
do_it:; |
|
900 | 1007 |
desttile += TileOffsByDir(m5 & 3); |
0 | 1008 |
if (desttile == tile && bitmask&_road_pf_table_3[m5&3]) { |
1009 |
return_track(FindFirstBit2x64(bitmask&_road_pf_table_3[m5&3])); |
|
1010 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
1011 |
} |
0 | 1012 |
} |
1013 |
// do pathfind |
|
1014 |
frd.dest = desttile; |
|
1015 |
||
1016 |
best_track = -1; |
|
1017 |
best_dist = (uint)-1; |
|
1018 |
best_maxlen = (uint)-1; |
|
1019 |
i = 0; |
|
1020 |
do { |
|
1021 |
if (bitmask & 1) { |
|
1022 |
if (best_track == -1) best_track = i; // in case we don't find the path, just pick a direction |
|
1023 |
frd.maxtracklen = (uint)-1; |
|
1024 |
frd.mindist = (uint)-1; |
|
159
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
156
diff
changeset
|
1025 |
FollowTrack(tile, 0x3000 | TRANSPORT_ROAD, _road_pf_directions[i], (TPFEnumProc*)EnumRoadTrackFindDist, NULL, &frd); |
0 | 1026 |
|
1027 |
if (frd.mindist < best_dist || (frd.mindist==best_dist && frd.maxtracklen < best_maxlen)) { |
|
1028 |
best_dist = frd.mindist; |
|
1029 |
best_maxlen = frd.maxtracklen; |
|
1030 |
best_track = i; |
|
1031 |
} |
|
1032 |
} |
|
1033 |
} while (++i,(bitmask>>=1) != 0); |
|
1034 |
||
1035 |
found_best_track:; |
|
1036 |
||
1037 |
if (HASBIT(signal, best_track)) |
|
1038 |
return -1; |
|
1039 |
||
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
1040 |
return best_track; |
0 | 1041 |
} |
1042 |
||
1043 |
typedef struct RoadDriveEntry { |
|
1044 |
byte x,y; |
|
1045 |
} RoadDriveEntry; |
|
1046 |
||
1047 |
#include "table/roadveh.h" |
|
1048 |
||
1049 |
static const byte _road_veh_data_1[] = { |
|
1050 |
20, 20, 16, 16, 0, 0, 0, 0, |
|
1051 |
19, 19, 15, 15, 0, 0, 0, 0, |
|
1052 |
16, 16, 12, 12, 0, 0, 0, 0, |
|
1053 |
15, 15, 11, 11 |
|
1054 |
}; |
|
1055 |
||
1056 |
static const byte _roadveh_data_2[4] = { 0,1,8,9 }; |
|
1057 |
||
1058 |
static void RoadVehEventHandler(Vehicle *v) |
|
1059 |
{ |
|
1060 |
GetNewVehiclePosResult gp; |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1061 |
byte new_dir, old_dir; |
0 | 1062 |
RoadDriveEntry rd; |
1063 |
int x,y; |
|
1064 |
Station *st; |
|
1065 |
uint32 r; |
|
1066 |
Vehicle *u; |
|
1067 |
||
1068 |
// decrease counters |
|
1069 |
v->tick_counter++; |
|
1070 |
if (v->u.road.reverse_ctr != 0) |
|
1071 |
v->u.road.reverse_ctr--; |
|
1072 |
||
1073 |
// handle crashed |
|
1074 |
if (v->u.road.crashed_ctr != 0) { |
|
1075 |
RoadVehIsCrashed(v); |
|
1076 |
return; |
|
1077 |
} |
|
1078 |
||
1079 |
RoadVehCheckTrainCrash(v); |
|
1080 |
||
1081 |
// road vehicle has broken down? |
|
1082 |
if (v->breakdown_ctr != 0) { |
|
1083 |
if (v->breakdown_ctr <= 2) { |
|
1084 |
HandleBrokenRoadVeh(v); |
|
1085 |
return; |
|
1086 |
} |
|
1087 |
v->breakdown_ctr--; |
|
1088 |
} |
|
1089 |
||
1090 |
// exit if vehicle is stopped |
|
1091 |
if (v->vehstatus & VS_STOPPED) |
|
1092 |
return; |
|
1093 |
||
1094 |
ProcessRoadVehOrder(v); |
|
1095 |
HandleRoadVehLoading(v); |
|
1096 |
||
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1097 |
if (v->current_order.type == OT_LOADING) |
0 | 1098 |
return; |
1099 |
||
1100 |
if (v->u.road.state == 254) { |
|
1101 |
int dir; |
|
1102 |
const RoadDriveEntry*rdp; |
|
1103 |
byte rd2; |
|
1104 |
||
1105 |
v->cur_speed = 0; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
1106 |
|
0 | 1107 |
dir = _map5[v->tile]&3; |
1108 |
v->direction = dir*2+1; |
|
1109 |
||
1110 |
rd2 = _roadveh_data_2[dir]; |
|
1111 |
rdp = _road_drive_data[(_opt.road_side<<4) + rd2]; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
1112 |
|
926
bd4312619522
(svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents:
919
diff
changeset
|
1113 |
x = TileX(v->tile) * 16 + (rdp[6].x & 0xF); |
bd4312619522
(svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents:
919
diff
changeset
|
1114 |
y = TileY(v->tile) * 16 + (rdp[6].y & 0xF); |
0 | 1115 |
|
1116 |
if (RoadVehFindCloseTo(v,x,y,v->direction)) |
|
1117 |
return; |
|
1118 |
||
578
86e352980acd
(svn r998) now vehicles are serviced both when entering and when leaving depots to prevent that vehicles might need service when leaving after a long stay (ln--)
bjarni
parents:
555
diff
changeset
|
1119 |
VehicleServiceInDepot(v); |
86e352980acd
(svn r998) now vehicles are serviced both when entering and when leaving depots to prevent that vehicles might need service when leaving after a long stay (ln--)
bjarni
parents:
555
diff
changeset
|
1120 |
|
0 | 1121 |
StartRoadVehSound(v); |
1122 |
||
1123 |
BeginVehicleMove(v); |
|
1124 |
||
1125 |
v->vehstatus &= ~VS_HIDDEN; |
|
1126 |
v->u.road.state = rd2; |
|
1127 |
v->u.road.frame = 6; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
1128 |
|
0 | 1129 |
v->cur_image = GetRoadVehImage(v, v->direction); |
1130 |
UpdateRoadVehDeltaXY(v); |
|
1131 |
SetRoadVehPosition(v,x,y); |
|
1132 |
||
1133 |
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile); |
|
1134 |
return; |
|
1135 |
} |
|
1136 |
||
1137 |
if (!RoadVehAccelerate(v)) |
|
1138 |
return; |
|
1139 |
||
1140 |
if (v->u.road.overtaking != 0) { |
|
1141 |
if (++v->u.road.overtaking_ctr >= 35) |
|
1142 |
v->u.road.overtaking = 0; |
|
1143 |
} |
|
1144 |
||
1145 |
BeginVehicleMove(v); |
|
1146 |
||
1147 |
if (v->u.road.state == 255) { |
|
1148 |
GetNewVehiclePos(v, &gp); |
|
1149 |
||
1150 |
if (RoadVehFindCloseTo(v, gp.x, gp.y, v->direction)) { |
|
1151 |
v->cur_speed = 0; |
|
1152 |
return; |
|
1153 |
} |
|
1154 |
||
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
|
1155 |
if (IsTileType(gp.new_tile, MP_TUNNELBRIDGE) && |
0 | 1156 |
(_map5[gp.new_tile]&0xF0) == 0 && |
1157 |
(VehicleEnterTile(v, gp.new_tile, gp.x, gp.y)&4)) { |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
1158 |
|
0 | 1159 |
//new_dir = RoadGetNewDirection(v, gp.x, gp.y) |
1160 |
v->cur_image = GetRoadVehImage(v, v->direction); |
|
1161 |
UpdateRoadVehDeltaXY(v); |
|
1162 |
SetRoadVehPosition(v,gp.x,gp.y); |
|
1163 |
return; |
|
1164 |
} |
|
1165 |
||
1166 |
v->x_pos = gp.x; |
|
1167 |
v->y_pos = gp.y; |
|
1168 |
VehiclePositionChanged(v); |
|
1169 |
return; |
|
1170 |
} |
|
1171 |
||
1172 |
rd = _road_drive_data[(v->u.road.state + (_opt.road_side<<4)) ^ v->u.road.overtaking][v->u.road.frame+1]; |
|
1173 |
||
1174 |
// switch to another tile |
|
1175 |
if (rd.x & 0x80) { |
|
900 | 1176 |
uint tile = v->tile + TileOffsByDir(rd.x & 3); |
0 | 1177 |
int dir = RoadFindPathToDest(v, tile, rd.x&3); |
1178 |
int tmp; |
|
1179 |
uint32 r; |
|
1180 |
byte newdir; |
|
1181 |
const RoadDriveEntry *rdp; |
|
1182 |
||
1183 |
if (dir == -1) { |
|
1184 |
v->cur_speed = 0; |
|
1185 |
return; |
|
1186 |
} |
|
1187 |
||
1188 |
again: |
|
1189 |
if ((dir & 7) >= 6) { |
|
1190 |
tile = v->tile; |
|
1191 |
} |
|
1192 |
||
1193 |
tmp = (dir+(_opt.road_side<<4))^v->u.road.overtaking; |
|
1194 |
rdp = _road_drive_data[tmp]; |
|
1195 |
||
1196 |
tmp &= ~0x10; |
|
1197 |
||
926
bd4312619522
(svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents:
919
diff
changeset
|
1198 |
x = TileX(tile) * 16 + rdp[0].x; |
bd4312619522
(svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents:
919
diff
changeset
|
1199 |
y = TileY(tile) * 16 + rdp[0].y; |
0 | 1200 |
|
1201 |
if (RoadVehFindCloseTo(v, x, y, newdir=RoadVehGetSlidingDirection(v, x, y))) |
|
1202 |
return; |
|
1203 |
||
1204 |
r = VehicleEnterTile(v, tile, x, y); |
|
1205 |
if (r & 8) { |
|
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
|
1206 |
if (!IsTileType(tile, MP_TUNNELBRIDGE)) { |
0 | 1207 |
v->cur_speed = 0; |
1208 |
return; |
|
1209 |
} |
|
1210 |
dir = _road_reverse_table[rd.x&3]; |
|
1211 |
goto again; |
|
1212 |
} |
|
1213 |
||
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
|
1214 |
if (IS_BYTE_INSIDE(v->u.road.state, 0x20, 0x30) && IsTileType(v->tile, MP_STATION)) { |
0 | 1215 |
if ((tmp&7) >= 6) { v->cur_speed = 0; return; } |
1216 |
if (IS_BYTE_INSIDE(_map5[v->tile], 0x43, 0x4B)) { |
|
919
b0d6c7642f99
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
904
diff
changeset
|
1217 |
Station *st = GetStation(_map2[v->tile]); |
0 | 1218 |
byte *b; |
1219 |
||
1220 |
if (_map5[v->tile] >= 0x47) { |
|
1221 |
b = &st->bus_stop_status; |
|
1222 |
} else { |
|
1223 |
b = &st->truck_stop_status; |
|
1224 |
} |
|
1225 |
*b = (*b | ((v->u.road.state&2)?2:1)) & 0x7F; |
|
1226 |
} |
|
1227 |
} |
|
1228 |
||
1229 |
if (!(r & 4)) { |
|
1230 |
v->tile = tile; |
|
1231 |
v->u.road.state = (byte)tmp; |
|
1232 |
v->u.road.frame = 0; |
|
1233 |
} |
|
1234 |
if (newdir != v->direction) { |
|
1235 |
v->direction = newdir; |
|
1236 |
v->cur_speed -= v->cur_speed >> 2; |
|
1237 |
} |
|
1238 |
||
1239 |
v->cur_image = GetRoadVehImage(v, newdir); |
|
1240 |
UpdateRoadVehDeltaXY(v); |
|
1241 |
RoadZPosAffectSpeed(v, SetRoadVehPosition(v, x, y)); |
|
1242 |
return; |
|
1243 |
} |
|
1244 |
||
1245 |
if (rd.x & 0x40) { |
|
1246 |
int dir = RoadFindPathToDest(v, v->tile, rd.x&3); |
|
1247 |
uint32 r; |
|
1248 |
int tmp; |
|
1249 |
byte newdir; |
|
1250 |
const RoadDriveEntry *rdp; |
|
1251 |
||
1252 |
if (dir == -1) { |
|
1253 |
v->cur_speed = 0; |
|
1254 |
return; |
|
1255 |
} |
|
1256 |
||
1257 |
tmp = (_opt.road_side<<4) + dir; |
|
1258 |
rdp = _road_drive_data[tmp]; |
|
1259 |
||
926
bd4312619522
(svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents:
919
diff
changeset
|
1260 |
x = TileX(v->tile) * 16 + rdp[1].x; |
bd4312619522
(svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents:
919
diff
changeset
|
1261 |
y = TileY(v->tile) * 16 + rdp[1].y; |
0 | 1262 |
|
1263 |
if (RoadVehFindCloseTo(v, x, y, newdir=RoadVehGetSlidingDirection(v, x, y))) |
|
1264 |
return; |
|
1265 |
||
1266 |
r = VehicleEnterTile(v, v->tile, x, y); |
|
1267 |
if (r & 8) { |
|
1268 |
v->cur_speed = 0; |
|
1269 |
return; |
|
1270 |
} |
|
1271 |
||
1272 |
v->u.road.state = tmp & ~16; |
|
1273 |
v->u.road.frame = 1; |
|
1274 |
||
1275 |
if (newdir != v->direction) { |
|
1276 |
v->direction = newdir; |
|
1277 |
v->cur_speed -= v->cur_speed >> 2; |
|
1278 |
} |
|
1279 |
||
1280 |
v->cur_image = GetRoadVehImage(v, newdir); |
|
1281 |
UpdateRoadVehDeltaXY(v); |
|
1282 |
RoadZPosAffectSpeed(v, SetRoadVehPosition(v, x, y)); |
|
1283 |
return; |
|
1284 |
} |
|
1285 |
||
1286 |
x = (v->x_pos&~15)+(rd.x&15); |
|
1287 |
y = (v->y_pos&~15)+(rd.y&15); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
1288 |
|
0 | 1289 |
new_dir = RoadVehGetSlidingDirection(v, x, y); |
1290 |
||
1291 |
if (!IS_BYTE_INSIDE(v->u.road.state, 0x20, 0x30) && (u=RoadVehFindCloseTo(v, x, y, new_dir)) != NULL) { |
|
1292 |
if (v->u.road.overtaking == 0) |
|
1293 |
RoadVehCheckOvertake(v, u); |
|
1294 |
return; |
|
1295 |
} |
|
1296 |
||
1297 |
old_dir = v->direction; |
|
1298 |
if (new_dir != old_dir) { |
|
1299 |
v->direction = new_dir; |
|
1300 |
v->cur_speed -= (v->cur_speed >> 2); |
|
1301 |
if (old_dir != v->u.road.state) { |
|
1302 |
v->cur_image = GetRoadVehImage(v, new_dir); |
|
1303 |
UpdateRoadVehDeltaXY(v); |
|
1304 |
SetRoadVehPosition(v, v->x_pos, v->y_pos); |
|
1305 |
return; |
|
1306 |
} |
|
1307 |
} |
|
1308 |
||
1309 |
if (v->u.road.state >= 0x20 && |
|
1310 |
_road_veh_data_1[v->u.road.state - 0x20 + (_opt.road_side<<4)] == v->u.road.frame) { |
|
1311 |
byte *b; |
|
1312 |
||
919
b0d6c7642f99
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
904
diff
changeset
|
1313 |
st = GetStation(_map2[v->tile]); |
0 | 1314 |
b = IS_BYTE_INSIDE(_map5[v->tile], 0x43, 0x47) ? &st->truck_stop_status : &st->bus_stop_status; |
1315 |
||
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1316 |
if (v->current_order.type != OT_LEAVESTATION && |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1317 |
v->current_order.type != OT_GOTO_DEPOT) { |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1318 |
Order old_order; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
1319 |
|
0 | 1320 |
*b &= ~0x80; |
1321 |
||
1322 |
v->last_station_visited = _map2[v->tile]; |
|
1323 |
||
1324 |
RoadVehArrivesAt(v, st); |
|
1325 |
||
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1326 |
old_order = v->current_order; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1327 |
v->current_order.type = OT_LOADING; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1328 |
v->current_order.flags = 0; |
0 | 1329 |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1330 |
if (old_order.type == OT_GOTO_STATION && |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1331 |
v->current_order.station == v->last_station_visited) { |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1332 |
v->current_order.flags = |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1333 |
(old_order.flags & (OF_FULL_LOAD | OF_UNLOAD)) | OF_NON_STOP; |
0 | 1334 |
} |
1335 |
||
1336 |
SET_EXPENSES_TYPE(EXPENSES_ROADVEH_INC); |
|
1337 |
if (LoadUnloadVehicle(v)) { |
|
1338 |
InvalidateWindow(WC_ROADVEH_LIST, v->owner); |
|
1339 |
MarkRoadVehDirty(v); |
|
1340 |
} |
|
755
823358e83abf
(svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents:
679
diff
changeset
|
1341 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); |
0 | 1342 |
return; |
1343 |
} |
|
1344 |
||
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1345 |
if (v->current_order.type != OT_GOTO_DEPOT) { |
0 | 1346 |
if (*b&0x80) { |
1347 |
v->cur_speed = 0; |
|
1348 |
return; |
|
1349 |
} |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1350 |
v->current_order.type = OT_NOTHING; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1351 |
v->current_order.flags = 0; |
0 | 1352 |
} |
1353 |
*b |= 0x80; |
|
1354 |
||
1355 |
StartRoadVehSound(v); |
|
755
823358e83abf
(svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents:
679
diff
changeset
|
1356 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); |
0 | 1357 |
} |
1358 |
||
1359 |
r = VehicleEnterTile(v, v->tile, x, y); |
|
1360 |
if (r & 8) { |
|
1361 |
v->cur_speed = 0; |
|
1362 |
return; |
|
1363 |
} |
|
1364 |
||
1365 |
if ((r & 4) == 0) { |
|
1366 |
v->u.road.frame++; |
|
1367 |
} |
|
1368 |
||
1369 |
v->cur_image = GetRoadVehImage(v, v->direction); |
|
1370 |
UpdateRoadVehDeltaXY(v); |
|
1371 |
RoadZPosAffectSpeed(v, SetRoadVehPosition(v, x, y)); |
|
1372 |
} |
|
1373 |
||
1374 |
void RoadVehEnterDepot(Vehicle *v) |
|
1375 |
{ |
|
1376 |
v->u.road.state = 254; |
|
1377 |
v->vehstatus |= VS_HIDDEN; |
|
1378 |
||
1379 |
InvalidateWindow(WC_VEHICLE_DETAILS, v->index); |
|
1380 |
||
842 | 1381 |
MaybeReplaceVehicle(v); |
0 | 1382 |
|
578
86e352980acd
(svn r998) now vehicles are serviced both when entering and when leaving depots to prevent that vehicles might need service when leaving after a long stay (ln--)
bjarni
parents:
555
diff
changeset
|
1383 |
VehicleServiceInDepot(v); |
86e352980acd
(svn r998) now vehicles are serviced both when entering and when leaving depots to prevent that vehicles might need service when leaving after a long stay (ln--)
bjarni
parents:
555
diff
changeset
|
1384 |
|
445
0e3fa3da3899
(svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents:
378
diff
changeset
|
1385 |
TriggerVehicle(v, VEHICLE_TRIGGER_DEPOT); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
1386 |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1387 |
if (v->current_order.type == OT_GOTO_DEPOT) { |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1388 |
Order t; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1389 |
|
0 | 1390 |
InvalidateWindow(WC_VEHICLE_VIEW, v->index); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
1391 |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1392 |
t = v->current_order; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1393 |
v->current_order.type = OT_DUMMY; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1394 |
v->current_order.flags = 0; |
0 | 1395 |
|
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
926
diff
changeset
|
1396 |
// Part of the orderlist? |
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1397 |
if (t.flags & OF_UNLOAD) { |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1398 |
v->cur_order_index++; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1399 |
} else if (t.flags & OF_FULL_LOAD) { |
0 | 1400 |
v->vehstatus |= VS_STOPPED; |
1401 |
if (v->owner == _local_player) { |
|
534
17ab2f22ff74
(svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents:
507
diff
changeset
|
1402 |
SetDParam(0, v->unitnumber); |
0 | 1403 |
AddNewsItem( |
1404 |
STR_9016_ROAD_VEHICLE_IS_WAITING, |
|
1405 |
NEWS_FLAGS(NM_SMALL, NF_VIEWPORT|NF_VEHICLE, NT_ADVICE, 0), |
|
1406 |
v->index, |
|
1407 |
0); |
|
1408 |
} |
|
1409 |
} |
|
1410 |
} |
|
1411 |
||
1412 |
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile); |
|
1151
614cbcb5b9fe
(svn r1652) Fix: train and road vehicle lists are now redrawn when vehicles enters/leaves a depot (Hackykid)
bjarni
parents:
1128
diff
changeset
|
1413 |
InvalidateWindowClasses(WC_ROADVEH_LIST); |
0 | 1414 |
} |
1415 |
||
1416 |
static void AgeRoadVehCargo(Vehicle *v) |
|
1417 |
{ |
|
1418 |
if (_age_cargo_skip_counter != 0) |
|
1419 |
return; |
|
1420 |
if (v->cargo_days != 255) |
|
1421 |
v->cargo_days++; |
|
1422 |
} |
|
1423 |
||
1424 |
void RoadVeh_Tick(Vehicle *v) |
|
1425 |
{ |
|
1426 |
AgeRoadVehCargo(v); |
|
1427 |
RoadVehEventHandler(v); |
|
1428 |
} |
|
1429 |
||
1430 |
static void CheckIfRoadVehNeedsService(Vehicle *v) |
|
1431 |
{ |
|
1432 |
int i; |
|
1433 |
||
76
30511cbc5188
(svn r77) -Fix: [1010788] AI service interval bug (tnx truesatan)
truelight
parents:
63
diff
changeset
|
1434 |
if (_patches.servint_roadveh == 0) |
11
836bc4b37b5b
(svn r12) Change: removed patch no_train_service. Instead you can set the default service interval for any vehicle type to 'disabled'.
dominik
parents:
0
diff
changeset
|
1435 |
return; |
836bc4b37b5b
(svn r12) Change: removed patch no_train_service. Instead you can set the default service interval for any vehicle type to 'disabled'.
dominik
parents:
0
diff
changeset
|
1436 |
|
593 | 1437 |
if (!VehicleNeedsService(v)) |
0 | 1438 |
return; |
1439 |
||
1440 |
if (v->vehstatus & VS_STOPPED) |
|
1441 |
return; |
|
1442 |
||
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
926
diff
changeset
|
1443 |
if (_patches.gotodepot && VehicleHasDepotOrders(v)) |
0 | 1444 |
return; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
1445 |
|
0 | 1446 |
// Don't interfere with a depot visit scheduled by the user, or a |
1447 |
// depot visit by the order list. |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1448 |
if (v->current_order.type == OT_GOTO_DEPOT && |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1449 |
(v->current_order.flags & (OF_FULL_LOAD | OF_UNLOAD)) != 0) |
0 | 1450 |
return; |
1451 |
||
1452 |
i = FindClosestRoadDepot(v); |
|
1453 |
||
1454 |
if (i < 0 || GetTileDist(v->tile, (&_depots[i])->xy) > 12) { |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1455 |
if (v->current_order.type == OT_GOTO_DEPOT) { |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1456 |
v->current_order.type = OT_DUMMY; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1457 |
v->current_order.flags = 0; |
755
823358e83abf
(svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents:
679
diff
changeset
|
1458 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); |
0 | 1459 |
} |
1460 |
return; |
|
1461 |
} |
|
1462 |
||
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1463 |
if (v->current_order.type == OT_GOTO_DEPOT && |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1464 |
v->current_order.flags & OF_NON_STOP && |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1465 |
!CHANCE16(1,20)) |
0 | 1466 |
return; |
1467 |
||
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1468 |
v->current_order.type = OT_GOTO_DEPOT; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1469 |
v->current_order.flags = OF_NON_STOP; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1470 |
v->current_order.station = (byte)i; |
0 | 1471 |
v->dest_tile = (&_depots[i])->xy; |
755
823358e83abf
(svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents:
679
diff
changeset
|
1472 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); |
0 | 1473 |
} |
1474 |
||
1475 |
void OnNewDay_RoadVeh(Vehicle *v) |
|
1476 |
{ |
|
1477 |
int32 cost; |
|
1478 |
Station *st; |
|
1479 |
uint tile; |
|
1480 |
||
1481 |
if ((++v->day_counter & 7) == 0) |
|
1482 |
DecreaseVehicleValue(v); |
|
1483 |
||
1484 |
if (v->u.road.unk2 == 0) |
|
1485 |
CheckVehicleBreakdown(v); |
|
1486 |
||
1487 |
AgeVehicle(v); |
|
1488 |
CheckIfRoadVehNeedsService(v); |
|
1489 |
||
1053
dfb5243315f1
(svn r1554) -Fix: [ 1103187 ] Order Check messages are now validated before
celestar
parents:
1035
diff
changeset
|
1490 |
CheckOrders(v->index, OC_INIT); |
19
6080d2b6a959
(svn r20) Feature: warning when a vehicle has invalid orders (celestar)
dominik
parents:
11
diff
changeset
|
1491 |
|
0 | 1492 |
/* update destination */ |
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1493 |
if (v->current_order.type == OT_GOTO_STATION) { |
919
b0d6c7642f99
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
904
diff
changeset
|
1494 |
st = GetStation(v->current_order.station); |
0 | 1495 |
if ((tile=(v->cargo_type==CT_PASSENGERS ? st->bus_tile : st->lorry_tile)) != 0) |
1496 |
v->dest_tile = tile; |
|
1497 |
} |
|
1498 |
||
1499 |
if (v->vehstatus & VS_STOPPED) |
|
1500 |
return; |
|
1501 |
||
538
5dd99f3b7eca
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
1502 |
cost = RoadVehInfo(v->engine_type)->running_cost * _price.roadveh_running / 364; |
0 | 1503 |
|
1504 |
v->profit_this_year -= cost >> 8; |
|
1505 |
||
1506 |
SET_EXPENSES_TYPE(EXPENSES_ROADVEH_RUN); |
|
1507 |
SubtractMoneyFromPlayerFract(v->owner, cost); |
|
1508 |
||
1509 |
InvalidateWindow(WC_VEHICLE_DETAILS, v->index); |
|
1151
614cbcb5b9fe
(svn r1652) Fix: train and road vehicle lists are now redrawn when vehicles enters/leaves a depot (Hackykid)
bjarni
parents:
1128
diff
changeset
|
1510 |
InvalidateWindowClasses(WC_ROADVEH_LIST); |
0 | 1511 |
} |
1512 |
||
1513 |
void HandleClickOnRoadVeh(Vehicle *v) |
|
1514 |
{ |
|
1515 |
ShowRoadVehViewWindow(v); |
|
1516 |
} |
|
1517 |
||
1093
e8d26c7dc42f
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1053
diff
changeset
|
1518 |
void RoadVehiclesYearlyLoop(void) |
0 | 1519 |
{ |
1520 |
Vehicle *v; |
|
1521 |
||
1522 |
FOR_ALL_VEHICLES(v) { |
|
1523 |
if (v->type == VEH_Road) { |
|
1524 |
v->profit_last_year = v->profit_this_year; |
|
1525 |
v->profit_this_year = 0; |
|
1526 |
InvalidateWindow(WC_VEHICLE_DETAILS, v->index); |
|
1527 |
} |
|
1528 |
} |
|
1529 |
} |
|
1530 |