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