4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
10 /** @file roadveh_cmd.cpp Handling of road vehicles. */
14 #include "command_func.h"
15 #include "news_func.h"
16 #include "station_base.h"
17 #include "company_func.h"
18 #include "articulated_vehicles.h"
19 #include "newgrf_sound.h"
20 #include "pathfinder/yapf/yapf.h"
21 #include "strings_func.h"
23 #include "map/depot.h"
24 #include "map/tunnelbridge.h"
25 #include "date_func.h"
26 #include "vehicle_func.h"
27 #include "sound_func.h"
29 #include "game/game.hpp"
30 #include "effectvehicle_func.h"
31 #include "roadstop_base.h"
32 #include "spritecache.h"
33 #include "core/random_func.hpp"
34 #include "company_base.h"
35 #include "core/backup_type.hpp"
37 #include "zoom_func.h"
39 #include "station_func.h"
41 #include "table/strings.h"
43 static const uint16 _roadveh_images
[] = {
44 0xCD4, 0xCDC, 0xCE4, 0xCEC, 0xCF4, 0xCFC, 0xD0C, 0xD14,
45 0xD24, 0xD1C, 0xD2C, 0xD04, 0xD1C, 0xD24, 0xD6C, 0xD74,
46 0xD7C, 0xC14, 0xC1C, 0xC24, 0xC2C, 0xC34, 0xC3C, 0xC4C,
47 0xC54, 0xC64, 0xC5C, 0xC6C, 0xC44, 0xC5C, 0xC64, 0xCAC,
48 0xCB4, 0xCBC, 0xD94, 0xD9C, 0xDA4, 0xDAC, 0xDB4, 0xDBC,
49 0xDCC, 0xDD4, 0xDE4, 0xDDC, 0xDEC, 0xDC4, 0xDDC, 0xDE4,
50 0xE2C, 0xE34, 0xE3C, 0xC14, 0xC1C, 0xC2C, 0xC3C, 0xC4C,
51 0xC5C, 0xC64, 0xC6C, 0xC74, 0xC84, 0xC94, 0xCA4
54 static const uint16 _roadveh_full_adder
[] = {
55 0, 88, 0, 0, 0, 0, 48, 48,
56 48, 48, 0, 0, 64, 64, 0, 16,
57 16, 0, 88, 0, 0, 0, 0, 48,
58 48, 48, 48, 0, 0, 64, 64, 0,
59 16, 16, 0, 88, 0, 0, 0, 0,
60 48, 48, 48, 48, 0, 0, 64, 64,
61 0, 16, 16, 0, 8, 8, 8, 8,
64 assert_compile(lengthof(_roadveh_images
) == lengthof(_roadveh_full_adder
));
67 bool IsValidImageIndex
<VEH_ROAD
>(uint8 image_index
)
69 return image_index
< lengthof(_roadveh_images
);
72 static const Trackdir _road_reverse_table
[DIAGDIR_END
] = {
73 TRACKDIR_RVREV_NE
, TRACKDIR_RVREV_SE
, TRACKDIR_RVREV_SW
, TRACKDIR_RVREV_NW
78 * Check whether a roadvehicle is a bus
81 bool RoadVehicle::IsBus() const
83 assert(this->IsFrontEngine());
84 return IsCargoInClass(this->cargo_type
, CC_PASSENGERS
);
88 * Get the width of a road vehicle image in the GUI.
89 * @param offset Additional offset for positioning the sprite; set to NULL if not needed
90 * @return Width in pixels
92 int RoadVehicle::GetDisplayImageWidth(Point
*offset
) const
94 int reference_width
= ROADVEHINFO_DEFAULT_VEHICLE_WIDTH
;
97 offset
->x
= reference_width
/ 2;
100 return this->gcache
.cached_veh_length
* reference_width
/ VEHICLE_LENGTH
;
103 static SpriteID
GetRoadVehIcon(EngineID engine
, EngineImageType image_type
)
105 const Engine
*e
= Engine::Get(engine
);
106 uint8 spritenum
= e
->u
.road
.image_index
;
108 if (is_custom_sprite(spritenum
)) {
109 SpriteID sprite
= GetCustomVehicleIcon(engine
, DIR_W
, image_type
);
110 if (sprite
!= 0) return sprite
;
112 spritenum
= e
->original_image_index
;
115 assert(IsValidImageIndex
<VEH_ROAD
>(spritenum
));
116 return DIR_W
+ _roadveh_images
[spritenum
];
119 SpriteID
RoadVehicle::GetImage(Direction direction
, EngineImageType image_type
) const
121 uint8 spritenum
= this->spritenum
;
124 if (is_custom_sprite(spritenum
)) {
125 sprite
= GetCustomVehicleSprite(this, (Direction
)(direction
+ 4 * IS_CUSTOM_SECONDHEAD_SPRITE(spritenum
)), image_type
);
126 if (sprite
!= 0) return sprite
;
128 spritenum
= this->GetEngine()->original_image_index
;
131 assert(IsValidImageIndex
<VEH_ROAD
>(spritenum
));
132 sprite
= direction
+ _roadveh_images
[spritenum
];
134 if (this->cargo
.StoredCount() >= this->cargo_cap
/ 2U) sprite
+= _roadveh_full_adder
[spritenum
];
140 * Draw a road vehicle engine.
141 * @param left Left edge to draw within.
142 * @param right Right edge to draw within.
143 * @param preferred_x Preferred position of the engine.
144 * @param y Vertical position of the engine.
145 * @param engine Engine to draw
146 * @param pal Palette to use.
148 void DrawRoadVehEngine(int left
, int right
, int preferred_x
, int y
, EngineID engine
, PaletteID pal
, EngineImageType image_type
)
150 SpriteID sprite
= GetRoadVehIcon(engine
, image_type
);
151 const Sprite
*real_sprite
= GetSprite(sprite
, ST_NORMAL
);
152 preferred_x
= Clamp(preferred_x
, left
- UnScaleByZoom(real_sprite
->x_offs
, ZOOM_LVL_GUI
), right
- UnScaleByZoom(real_sprite
->width
, ZOOM_LVL_GUI
) - UnScaleByZoom(real_sprite
->x_offs
, ZOOM_LVL_GUI
));
153 DrawSprite(sprite
, pal
, preferred_x
, y
);
157 * Get the size of the sprite of a road vehicle sprite heading west (used for lists).
158 * @param engine The engine to get the sprite from.
159 * @param[out] width The width of the sprite.
160 * @param[out] height The height of the sprite.
161 * @param[out] xoffs Number of pixels to shift the sprite to the right.
162 * @param[out] yoffs Number of pixels to shift the sprite downwards.
163 * @param image_type Context the sprite is used in.
165 void GetRoadVehSpriteSize(EngineID engine
, uint
&width
, uint
&height
, int &xoffs
, int &yoffs
, EngineImageType image_type
)
167 const Sprite
*spr
= GetSprite(GetRoadVehIcon(engine
, image_type
), ST_NORMAL
);
169 width
= UnScaleByZoom(spr
->width
, ZOOM_LVL_GUI
);
170 height
= UnScaleByZoom(spr
->height
, ZOOM_LVL_GUI
);
171 xoffs
= UnScaleByZoom(spr
->x_offs
, ZOOM_LVL_GUI
);
172 yoffs
= UnScaleByZoom(spr
->y_offs
, ZOOM_LVL_GUI
);
176 * Get length of a road vehicle.
177 * @param v Road vehicle to query length.
178 * @return Length of the given road vehicle.
180 static uint
GetRoadVehLength(const RoadVehicle
*v
)
182 const Engine
*e
= v
->GetEngine();
183 uint length
= VEHICLE_LENGTH
;
185 uint16 veh_len
= CALLBACK_FAILED
;
186 if (e
->GetGRF() != NULL
&& e
->GetGRF()->grf_version
>= 8) {
187 /* Use callback 36 */
188 veh_len
= GetVehicleProperty(v
, PROP_ROADVEH_SHORTEN_FACTOR
, CALLBACK_FAILED
);
189 if (veh_len
!= CALLBACK_FAILED
&& veh_len
>= VEHICLE_LENGTH
) ErrorUnknownCallbackResult(e
->GetGRFID(), CBID_VEHICLE_LENGTH
, veh_len
);
191 /* Use callback 11 */
192 veh_len
= GetVehicleCallback(CBID_VEHICLE_LENGTH
, 0, 0, v
->engine_type
, v
);
194 if (veh_len
== CALLBACK_FAILED
) veh_len
= e
->u
.road
.shorten_factor
;
196 length
-= Clamp(veh_len
, 0, VEHICLE_LENGTH
- 1);
203 * Update the cache of a road vehicle.
204 * @param v Road vehicle needing an update of its cache.
205 * @param same_length should length of vehicles stay the same?
206 * @pre \a v must be first road vehicle.
208 void RoadVehUpdateCache(RoadVehicle
*v
, bool same_length
)
210 assert(v
->type
== VEH_ROAD
);
211 assert(v
->IsFrontEngine());
213 v
->InvalidateNewGRFCacheOfChain();
215 v
->gcache
.cached_total_length
= 0;
217 for (RoadVehicle
*u
= v
; u
!= NULL
; u
= u
->Next()) {
218 /* Check the v->first cache. */
219 assert(u
->First() == v
);
221 /* Update the 'first engine' */
222 u
->gcache
.first_engine
= (v
== u
) ? INVALID_ENGINE
: v
->engine_type
;
224 /* Update the length of the vehicle. */
225 uint veh_len
= GetRoadVehLength(u
);
226 /* Verify length hasn't changed. */
227 if (same_length
&& veh_len
!= u
->gcache
.cached_veh_length
) VehicleLengthChanged(u
);
229 u
->gcache
.cached_veh_length
= veh_len
;
230 v
->gcache
.cached_total_length
+= u
->gcache
.cached_veh_length
;
232 /* Update visual effect */
233 u
->UpdateVisualEffect();
235 /* Update cargo aging period. */
236 u
->vcache
.cached_cargo_age_period
= GetVehicleProperty(u
, PROP_ROADVEH_CARGO_AGE_PERIOD
, EngInfo(u
->engine_type
)->cargo_age_period
);
239 uint max_speed
= GetVehicleProperty(v
, PROP_ROADVEH_SPEED
, 0);
240 v
->vcache
.cached_max_speed
= (max_speed
!= 0) ? max_speed
* 4 : RoadVehInfo(v
->engine_type
)->max_speed
;
244 * Build a road vehicle.
245 * @param tile tile of the depot where road vehicle is built.
246 * @param flags type of operation.
247 * @param e the engine to build.
248 * @param data unused.
249 * @param ret[out] the vehicle that has been built.
250 * @return the cost of this operation or an error.
252 CommandCost
CmdBuildRoadVehicle(TileIndex tile
, DoCommandFlag flags
, const Engine
*e
, uint16 data
, Vehicle
**ret
)
254 if (HasTileRoadType(tile
, ROADTYPE_TRAM
) != HasBit(e
->info
.misc_flags
, EF_ROAD_TRAM
)) return_cmd_error(STR_ERROR_DEPOT_WRONG_DEPOT_TYPE
);
256 if (flags
& DC_EXEC
) {
257 const RoadVehicleInfo
*rvi
= &e
->u
.road
;
259 RoadVehicle
*v
= new RoadVehicle();
261 v
->direction
= DiagDirToDir(GetGroundDepotDirection(tile
));
262 v
->owner
= _current_company
;
265 int x
= TileX(tile
) * TILE_SIZE
+ TILE_SIZE
/ 2;
266 int y
= TileY(tile
) * TILE_SIZE
+ TILE_SIZE
/ 2;
269 v
->z_pos
= GetSlopePixelZ(x
, y
);
271 v
->state
= RVSB_IN_DEPOT
;
272 v
->vehstatus
= VS_HIDDEN
| VS_STOPPED
| VS_DEFPAL
;
274 v
->spritenum
= rvi
->image_index
;
275 v
->cargo_type
= e
->GetDefaultCargoType();
276 v
->cargo_cap
= rvi
->capacity
;
279 v
->last_station_visited
= INVALID_STATION
;
280 v
->last_loading_station
= INVALID_STATION
;
281 v
->engine_type
= e
->index
;
282 v
->gcache
.first_engine
= INVALID_ENGINE
; // needs to be set before first callback
284 v
->reliability
= e
->reliability
;
285 v
->reliability_spd_dec
= e
->reliability_spd_dec
;
286 v
->max_age
= e
->GetLifeLengthInDays();
287 _new_vehicle_id
= v
->index
;
289 v
->SetServiceInterval(Company::Get(v
->owner
)->settings
.vehicle
.servint_roadveh
);
291 v
->date_of_last_service
= _date
;
292 v
->build_year
= _cur_year
;
294 v
->cur_image
= SPR_IMG_QUERY
;
295 v
->random_bits
= VehicleRandomBits();
298 v
->roadtype
= HasBit(e
->info
.misc_flags
, EF_ROAD_TRAM
) ? ROADTYPE_TRAM
: ROADTYPE_ROAD
;
299 v
->compatible_roadtypes
= RoadTypeToRoadTypes(v
->roadtype
);
300 v
->gcache
.cached_veh_length
= VEHICLE_LENGTH
;
302 if (e
->flags
& ENGINE_EXCLUSIVE_PREVIEW
) SetBit(v
->vehicle_flags
, VF_BUILT_AS_PROTOTYPE
);
303 v
->SetServiceIntervalIsPercent(Company::Get(_current_company
)->settings
.vehicle
.servint_ispercent
);
305 AddArticulatedParts(v
);
306 v
->InvalidateNewGRFCacheOfChain();
308 /* Call various callbacks after the whole consist has been constructed */
309 for (RoadVehicle
*u
= v
; u
!= NULL
; u
= u
->Next()) {
310 u
->cargo_cap
= u
->GetEngine()->DetermineCapacity(u
);
312 v
->InvalidateNewGRFCache();
313 u
->InvalidateNewGRFCache();
315 RoadVehUpdateCache(v
);
316 /* Initialize cached values for realistic acceleration. */
317 if (_settings_game
.vehicle
.roadveh_acceleration_model
!= AM_ORIGINAL
) v
->CargoChanged();
319 VehicleUpdatePosition(v
);
321 CheckConsistencyOfArticulatedVehicle(v
);
324 return CommandCost();
327 static TileIndex
FindClosestRoadDepot(const RoadVehicle
*v
, bool nearby
)
329 if (IsRoadDepotTile(v
->tile
) && v
->state
== DiagDirToDiagTrackdir(ReverseDiagDir(GetGroundDepotDirection(v
->tile
)))) {
333 return YapfRoadVehicleFindNearestDepot(v
,
334 nearby
? _settings_game
.pf
.yapf
.maximum_go_to_depot_penalty
: 0);
337 bool RoadVehicle::FindClosestDepot(TileIndex
*location
, DestinationID
*destination
, bool *reverse
)
339 TileIndex rfdd
= FindClosestRoadDepot(this, false);
340 if (rfdd
== INVALID_TILE
) return false;
342 if (location
!= NULL
) *location
= rfdd
;
343 if (destination
!= NULL
) *destination
= GetDepotIndex(rfdd
);
349 * Turn a roadvehicle around.
351 * @param flags operation to perform
352 * @param p1 vehicle ID to turn
355 * @return the cost of this operation or an error
357 CommandCost
CmdTurnRoadVeh(TileIndex tile
, DoCommandFlag flags
, uint32 p1
, uint32 p2
, const char *text
)
359 RoadVehicle
*v
= RoadVehicle::GetIfValid(p1
);
360 if (v
== NULL
) return CMD_ERROR
;
362 if (!v
->IsPrimaryVehicle()) return CMD_ERROR
;
364 CommandCost ret
= CheckOwnership(v
->owner
);
365 if (ret
.Failed()) return ret
;
367 if ((v
->vehstatus
& VS_STOPPED
) ||
368 (v
->vehstatus
& VS_CRASHED
) ||
369 v
->breakdown_ctr
!= 0 ||
370 v
->overtaking
!= 0 ||
371 v
->state
== RVSB_WORMHOLE
||
373 v
->current_order
.IsType(OT_LOADING
)) {
377 if (IsNormalRoadTile(v
->tile
) && GetDisallowedRoadDirections(v
->tile
) != DRD_NONE
) return CMD_ERROR
;
379 if ((IsTunnelTile(v
->tile
) || IsBridgeHeadTile(v
->tile
)) && DirToDiagDir(v
->direction
) == GetTunnelBridgeDirection(v
->tile
)) return CMD_ERROR
;
381 if (flags
& DC_EXEC
) v
->reverse_ctr
= 180;
383 return CommandCost();
387 void RoadVehicle::MarkDirty()
389 for (RoadVehicle
*v
= this; v
!= NULL
; v
= v
->Next()) {
390 v
->colourmap
= PAL_NONE
;
391 v
->UpdateViewport(true, false);
393 this->CargoChanged();
396 void RoadVehicle::UpdateDeltaXY(Direction direction
)
398 static const int8 _delta_xy_table
[8][10] = {
399 /* y_extent, x_extent, y_offs, x_offs, y_bb_offs, x_bb_offs, y_extent_shorten, x_extent_shorten, y_bb_offs_shorten, x_bb_offs_shorten */
400 {3, 3, -1, -1, 0, 0, -1, -1, -1, -1}, // N
401 {3, 7, -1, -3, 0, -1, 0, -1, 0, 0}, // NE
402 {3, 3, -1, -1, 0, 0, 1, -1, 1, -1}, // E
403 {7, 3, -3, -1, -1, 0, 0, 0, 1, 0}, // SE
404 {3, 3, -1, -1, 0, 0, 1, 1, 1, 1}, // S
405 {3, 7, -1, -3, 0, -1, 0, 0, 0, 1}, // SW
406 {3, 3, -1, -1, 0, 0, -1, 1, -1, 1}, // W
407 {7, 3, -3, -1, -1, 0, -1, 0, 0, 0}, // NW
410 int shorten
= VEHICLE_LENGTH
- this->gcache
.cached_veh_length
;
411 if (!IsDiagonalDirection(direction
)) shorten
>>= 1;
413 const int8
*bb
= _delta_xy_table
[direction
];
414 this->x_bb_offs
= bb
[5] + bb
[9] * shorten
;
415 this->y_bb_offs
= bb
[4] + bb
[8] * shorten
;;
416 this->x_offs
= bb
[3];
417 this->y_offs
= bb
[2];
418 this->x_extent
= bb
[1] + bb
[7] * shorten
;
419 this->y_extent
= bb
[0] + bb
[6] * shorten
;
424 * Calculates the maximum speed of the vehicle under its current conditions.
425 * @return Maximum speed of the vehicle.
427 inline int RoadVehicle::GetCurrentMaxSpeed() const
429 int max_speed
= this->vcache
.cached_max_speed
;
431 /* Limit speed to 50% while reversing, 75% in curves. */
432 for (const RoadVehicle
*u
= this; u
!= NULL
; u
= u
->Next()) {
433 if (_settings_game
.vehicle
.roadveh_acceleration_model
== AM_REALISTIC
) {
434 if (this->state
<= RVSB_TRACKDIR_MASK
&& IsReversingRoadTrackdir((Trackdir
)this->state
)) {
435 max_speed
= this->vcache
.cached_max_speed
/ 2;
437 } else if ((u
->direction
& 1) == 0) {
438 max_speed
= this->vcache
.cached_max_speed
* 3 / 4;
442 /* Vehicle is on the middle part of a bridge. */
443 if (u
->state
== RVSB_WORMHOLE
&& !(u
->vehstatus
& VS_HIDDEN
)) {
444 max_speed
= min(max_speed
, GetBridgeSpec(GetRoadBridgeType(u
->tile
))->speed
* 2);
448 return min(max_speed
, this->current_order
.max_speed
* 2);
452 * Delete last vehicle of a chain road vehicles.
453 * @param v First roadvehicle.
455 static void DeleteLastRoadVeh(RoadVehicle
*v
)
457 RoadVehicle
*first
= v
->First();
459 for (; v
->Next() != NULL
; v
= v
->Next()) u
= v
;
461 v
->last_station_visited
= first
->last_station_visited
; // for PreDestructor
463 /* Only leave the road stop when we're really gone. */
464 if (IsInsideMM(v
->state
, RVSB_IN_ROAD_STOP
, RVSB_IN_ROAD_STOP_END
)) RoadStop::GetByTile(v
->tile
, GetRoadStopType(v
->tile
))->Leave(v
);
469 static void RoadVehSetRandomDirection(RoadVehicle
*v
)
471 static const DirDiff delta
[] = {
472 DIRDIFF_45LEFT
, DIRDIFF_SAME
, DIRDIFF_SAME
, DIRDIFF_45RIGHT
478 v
->direction
= ChangeDir(v
->direction
, delta
[r
& 3]);
479 v
->UpdateViewport(true, true);
480 } while ((v
= v
->Next()) != NULL
);
484 * Road vehicle chain has crashed.
485 * @param v First roadvehicle.
486 * @return whether the chain still exists.
488 static bool RoadVehIsCrashed(RoadVehicle
*v
)
491 if (v
->crashed_ctr
== 2) {
492 CreateEffectVehicleRel(v
, 4, 4, 8, EV_EXPLOSION_LARGE
);
493 } else if (v
->crashed_ctr
<= 45) {
494 if ((v
->tick_counter
& 7) == 0) RoadVehSetRandomDirection(v
);
495 } else if (v
->crashed_ctr
>= 2220 && !(v
->tick_counter
& 0x1F)) {
496 bool ret
= v
->Next() != NULL
;
497 DeleteLastRoadVeh(v
);
505 * Check routine whether a road and a train vehicle have collided.
506 * @param v %Train vehicle to test.
507 * @param data Road vehicle to test.
508 * @return %Train vehicle if the vehicles collided, else \c NULL.
510 static Vehicle
*EnumCheckRoadVehCrashTrain(Vehicle
*v
, void *data
)
512 const Vehicle
*u
= (Vehicle
*)data
;
514 return (v
->type
== VEH_TRAIN
&&
515 abs(v
->z_pos
- u
->z_pos
) <= 6 &&
516 abs(v
->x_pos
- u
->x_pos
) <= 4 &&
517 abs(v
->y_pos
- u
->y_pos
) <= 4) ? v
: NULL
;
520 uint
RoadVehicle::Crash(bool flooded
)
522 uint pass
= this->GroundVehicleBase::Crash(flooded
);
523 if (this->IsFrontEngine()) {
526 /* If we're in a drive through road stop we ought to leave it */
527 if (IsInsideMM(this->state
, RVSB_IN_DT_ROAD_STOP
, RVSB_IN_DT_ROAD_STOP_END
)) {
528 RoadStop::GetByTile(this->tile
, GetRoadStopType(this->tile
))->Leave(this);
531 this->crashed_ctr
= flooded
? 2000 : 1; // max 2220, disappear pretty fast when flooded
535 static void RoadVehCrash(RoadVehicle
*v
)
537 uint pass
= v
->Crash();
539 AI::NewEvent(v
->owner
, new ScriptEventVehicleCrashed(v
->index
, v
->tile
, ScriptEventVehicleCrashed::CRASH_RV_LEVEL_CROSSING
));
540 Game::NewEvent(new ScriptEventVehicleCrashed(v
->index
, v
->tile
, ScriptEventVehicleCrashed::CRASH_RV_LEVEL_CROSSING
));
545 STR_NEWS_ROAD_VEHICLE_CRASH_DRIVER
: STR_NEWS_ROAD_VEHICLE_CRASH
,
550 ModifyStationRatingAround(v
->tile
, v
->owner
, -160, 22);
551 if (_settings_client
.sound
.disaster
) SndPlayVehicleFx(SND_12_EXPLOSION
, v
);
554 static bool RoadVehCheckTrainCrash(RoadVehicle
*v
)
556 for (RoadVehicle
*u
= v
; u
!= NULL
; u
= u
->Next()) {
557 if (u
->state
== RVSB_WORMHOLE
) continue;
559 TileIndex tile
= u
->tile
;
561 if (!IsLevelCrossingTile(tile
)) continue;
563 if (HasVehicleOnPosXY(v
->x_pos
, v
->y_pos
, u
, EnumCheckRoadVehCrashTrain
)) {
572 TileIndex
RoadVehicle::GetOrderStationLocation(StationID station
)
574 if (station
== this->last_station_visited
) this->last_station_visited
= INVALID_STATION
;
576 const Station
*st
= Station::Get(station
);
577 if (!CanVehicleUseStation(this, st
)) {
578 /* There is no stop left at the station, so don't even TRY to go there */
579 this->IncrementRealOrderIndex();
586 static void StartRoadVehSound(const RoadVehicle
*v
)
588 if (!PlayVehicleSound(v
, VSE_START
)) {
589 SoundID s
= RoadVehInfo(v
->engine_type
)->sfx
;
590 if (s
== SND_19_BUS_START_PULL_AWAY
&& (v
->tick_counter
& 3) == 0) {
591 s
= SND_1A_BUS_START_PULL_AWAY_WITH_HORN
;
593 SndPlayVehicleFx(s
, v
);
597 struct RoadVehFindData
{
606 static void RoadVehFindCloseToCheck (RoadVehFindData
*rvf
, Vehicle
*v
)
608 static const int8 dist_x
[] = { -4, -8, -4, -1, 4, 8, 4, 1 };
609 static const int8 dist_y
[] = { -4, -1, 4, 8, 4, 1, -4, -8 };
611 if (v
->type
== VEH_ROAD
&&
613 abs(v
->z_pos
- rvf
->veh
->z_pos
) < 6 &&
614 v
->direction
== rvf
->dir
&&
615 rvf
->veh
->First() != v
->First()) {
617 short x_diff
= v
->x_pos
- rvf
->x
;
618 short y_diff
= v
->y_pos
- rvf
->y
;
620 if ((dist_x
[v
->direction
] > 0 ?
621 (x_diff
>= 0 && x_diff
< dist_x
[v
->direction
]) :
622 (x_diff
<= 0 && x_diff
> dist_x
[v
->direction
])) &&
623 (dist_y
[v
->direction
] > 0 ?
624 (y_diff
>= 0 && y_diff
< dist_y
[v
->direction
]) :
625 (y_diff
<= 0 && y_diff
> dist_y
[v
->direction
]))) {
626 uint diff
= abs(x_diff
) + abs(y_diff
);
628 if (diff
< rvf
->best_diff
|| (diff
== rvf
->best_diff
&& v
->index
< rvf
->best
->index
)) {
630 rvf
->best_diff
= diff
;
636 static Vehicle
*EnumCheckRoadVehClose(Vehicle
*v
, void *data
)
638 RoadVehFindCloseToCheck ((RoadVehFindData
*)data
, v
);
643 static RoadVehicle
*RoadVehFindCloseTo(RoadVehicle
*v
, int x
, int y
, Direction dir
, bool update_blocked_ctr
= true)
646 RoadVehicle
*front
= v
->First();
648 if (front
->reverse_ctr
!= 0) return NULL
;
654 rvf
.best_diff
= UINT_MAX
;
656 if (front
->state
== RVSB_WORMHOLE
) {
657 VehicleTileIterator
iter1 (v
->tile
);
658 while (!iter1
.finished()) {
659 RoadVehFindCloseToCheck (&rvf
, iter1
.next());
661 VehicleTileIterator
iter2 (GetOtherTunnelBridgeEnd(v
->tile
));
662 while (!iter2
.finished()) {
663 RoadVehFindCloseToCheck (&rvf
, iter2
.next());
666 FindVehicleOnPosXY(x
, y
, &rvf
, EnumCheckRoadVehClose
);
669 /* This code protects a roadvehicle from being blocked for ever
670 * If more than 1480 / 74 days a road vehicle is blocked, it will
671 * drive just through it. The ultimate backup-code of TTD.
672 * It can be disabled. */
673 if (rvf
.best_diff
== UINT_MAX
) {
674 front
->blocked_ctr
= 0;
678 if (update_blocked_ctr
&& ++front
->blocked_ctr
> 1480) return NULL
;
680 return RoadVehicle::From(rvf
.best
);
684 * A road vehicle arrives at a station. If it is the first time, create a news item.
685 * @param v Road vehicle that arrived.
686 * @param st Station where the road vehicle arrived.
688 static void RoadVehArrivesAt(const RoadVehicle
*v
, Station
*st
)
691 /* Check if station was ever visited before */
692 if (!(st
->had_vehicle_of_type
& HVOT_BUS
)) {
693 st
->had_vehicle_of_type
|= HVOT_BUS
;
694 SetDParam(0, st
->index
);
696 v
->roadtype
== ROADTYPE_ROAD
? STR_NEWS_FIRST_BUS_ARRIVAL
: STR_NEWS_FIRST_PASSENGER_TRAM_ARRIVAL
,
697 (v
->owner
== _local_company
) ? NT_ARRIVAL_COMPANY
: NT_ARRIVAL_OTHER
,
701 AI::NewEvent(v
->owner
, new ScriptEventStationFirstVehicle(st
->index
, v
->index
));
702 Game::NewEvent(new ScriptEventStationFirstVehicle(st
->index
, v
->index
));
705 /* Check if station was ever visited before */
706 if (!(st
->had_vehicle_of_type
& HVOT_TRUCK
)) {
707 st
->had_vehicle_of_type
|= HVOT_TRUCK
;
708 SetDParam(0, st
->index
);
710 v
->roadtype
== ROADTYPE_ROAD
? STR_NEWS_FIRST_TRUCK_ARRIVAL
: STR_NEWS_FIRST_CARGO_TRAM_ARRIVAL
,
711 (v
->owner
== _local_company
) ? NT_ARRIVAL_COMPANY
: NT_ARRIVAL_OTHER
,
715 AI::NewEvent(v
->owner
, new ScriptEventStationFirstVehicle(st
->index
, v
->index
));
716 Game::NewEvent(new ScriptEventStationFirstVehicle(st
->index
, v
->index
));
722 * This function looks at the vehicle and updates its speed (cur_speed
723 * and subspeed) variables. Furthermore, it returns the distance that
724 * the vehicle can drive this tick. #Vehicle::GetAdvanceDistance() determines
725 * the distance to drive before moving a step on the map.
726 * @return distance to drive.
728 int RoadVehicle::UpdateSpeed()
730 switch (_settings_game
.vehicle
.roadveh_acceleration_model
) {
731 default: NOT_REACHED();
733 return this->DoUpdateSpeed(this->overtaking
!= 0 ? 512 : 256, 0, this->GetCurrentMaxSpeed());
736 return this->DoUpdateSpeed(this->GetAcceleration() + (this->overtaking
!= 0 ? 256 : 0), this->GetAccelerationStatus() == AS_BRAKE
? 0 : 4, this->GetCurrentMaxSpeed());
741 /** The returned bits of VehicleEnterTile. */
742 enum VehicleEnterTileStatus
{
743 VETS_CONTINUE
= 0, ///< The vehicle can continue normally
744 VETS_ENTERED_WORMHOLE
= 1, ///< The vehicle either entered a bridge, tunnel or depot tile
745 VETS_CANNOT_ENTER
= 2, ///< The vehicle cannot enter the tile
747 DECLARE_ENUM_AS_BIT_SET(VehicleEnterTileStatus
)
749 static VehicleEnterTileStatus
RoadVehEnter_Road(RoadVehicle
*v
, TileIndex tile
, int x
, int y
)
751 if (IsTileSubtype(tile
, TT_TRACK
)) return VETS_CONTINUE
;
753 assert(abs((int)(GetSlopePixelZ(x
, y
) - v
->z_pos
)) < 3);
755 /* modify speed of vehicle */
756 uint16 spd
= GetBridgeSpec(GetRoadBridgeType(tile
))->speed
* 2;
757 Vehicle
*first
= v
->First();
758 first
->cur_speed
= min(first
->cur_speed
, spd
);
760 return VETS_CONTINUE
;
763 extern const byte _tunnel_visibility_frame
[DIAGDIR_END
];
765 static VehicleEnterTileStatus
RoadVehEnter_Misc(RoadVehicle
*u
, TileIndex tile
, int x
, int y
)
767 switch (GetTileSubtype(tile
)) {
770 case TT_MISC_TUNNEL
: {
771 assert(abs((int)GetSlopePixelZ(x
, y
) - u
->z_pos
) < 3);
773 /* Direction into the wormhole */
774 const DiagDirection dir
= GetTunnelBridgeDirection(tile
);
776 if (u
->direction
== DiagDirToDir(dir
)) {
777 uint frame
= DistanceFromTileEdge(ReverseDiagDir(dir
), x
& 0xF, y
& 0xF);
778 if (frame
== _tunnel_visibility_frame
[dir
]) {
779 /* Frame should be equal to the next frame number in the RV's movement */
780 assert((int)frame
== u
->frame
+ 1);
781 u
->vehstatus
|= VS_HIDDEN
;
783 } else if (u
->direction
== ReverseDir(DiagDirToDir(dir
))) {
784 uint frame
= DistanceFromTileEdge(dir
, x
& 0xF, y
& 0xF);
785 if (frame
== TILE_SIZE
- _tunnel_visibility_frame
[dir
]) {
786 assert((int)frame
== u
->frame
+ 1);
787 u
->vehstatus
&= ~VS_HIDDEN
;
795 if (!IsRoadDepot(tile
)) break;
797 if (u
->frame
== RVC_DEPOT_STOP_FRAME
&&
798 u
->state
== DiagDirToDiagTrackdir(ReverseDiagDir(GetGroundDepotDirection(tile
)))) {
799 u
->state
= RVSB_IN_DEPOT
;
800 u
->vehstatus
|= VS_HIDDEN
;
801 u
->direction
= ReverseDir(u
->direction
);
802 if (u
->Next() == NULL
) VehicleEnterDepot(u
->First());
805 InvalidateWindowData(WC_VEHICLE_DEPOT
, u
->tile
);
806 return VETS_ENTERED_WORMHOLE
;
812 return VETS_CONTINUE
;
815 static VehicleEnterTileStatus
RoadVehEnter_Station(RoadVehicle
*v
, TileIndex tile
, int x
, int y
)
817 if (v
->state
< RVSB_IN_ROAD_STOP
&& !IsReversingRoadTrackdir((Trackdir
)v
->state
) && v
->frame
== 0) {
818 if (IsRoadStop(tile
) && v
->IsFrontEngine()) {
819 /* Attempt to allocate a parking bay in a road stop */
820 return RoadStop::GetByTile(tile
, GetRoadStopType(tile
))->Enter(v
) ? VETS_CONTINUE
: VETS_CANNOT_ENTER
;
824 return VETS_CONTINUE
;
828 * Call the tile callback function for a road vehicle entering a tile
829 * @param v Road vehicle entering the tile
830 * @param tile Tile entered
831 * @param x X position
832 * @param y Y position
833 * @return Some meta-data over the to be entered tile.
834 * @see VehicleEnterTileStatus to see what the bits in the return value mean.
836 static VehicleEnterTileStatus
RoadVehEnterTile(RoadVehicle
*v
, TileIndex tile
, int x
, int y
)
838 switch (GetTileType(tile
)) {
839 default: NOT_REACHED();
842 return RoadVehEnter_Road(v
, tile
, x
, y
);
845 return RoadVehEnter_Misc(v
, tile
, x
, y
);
848 return RoadVehEnter_Station(v
, tile
, x
, y
);
852 static Direction
RoadVehGetNewDirection(const RoadVehicle
*v
, int x
, int y
)
854 static const Direction _roadveh_new_dir
[] = {
855 DIR_N
, DIR_NW
, DIR_W
, INVALID_DIR
,
856 DIR_NE
, DIR_N
, DIR_SW
, INVALID_DIR
,
857 DIR_E
, DIR_SE
, DIR_S
860 x
= x
- v
->x_pos
+ 1;
861 y
= y
- v
->y_pos
+ 1;
863 if ((uint
)x
> 2 || (uint
)y
> 2) return v
->direction
;
864 return _roadveh_new_dir
[y
* 4 + x
];
867 static Direction
RoadVehGetSlidingDirection(const RoadVehicle
*v
, int x
, int y
)
869 Direction new_dir
= RoadVehGetNewDirection(v
, x
, y
);
870 Direction old_dir
= v
->direction
;
873 if (new_dir
== old_dir
) return old_dir
;
874 delta
= (DirDifference(new_dir
, old_dir
) > DIRDIFF_REVERSE
? DIRDIFF_45LEFT
: DIRDIFF_45RIGHT
);
875 return ChangeDir(old_dir
, delta
);
878 struct OvertakeData
{
879 const RoadVehicle
*u
;
880 const RoadVehicle
*v
;
886 * Check if overtaking is possible on a piece of track
888 * @param od Information about the tile and the involved vehicles
889 * @return true if we have to abort overtaking
891 static bool CheckRoadBlockedForOvertaking(OvertakeData
*od
)
893 TrackStatus ts
= GetTileRoadStatus(od
->tile
, od
->v
->compatible_roadtypes
);
894 TrackdirBits trackdirbits
= TrackStatusToTrackdirBits(ts
);
895 TrackdirBits red_signals
= TrackStatusToRedSignals(ts
); // barred level crossing
896 TrackBits trackbits
= TrackdirBitsToTrackBits(trackdirbits
);
898 /* Track does not continue along overtaking direction || track has junction || levelcrossing is barred */
899 if (!HasBit(trackdirbits
, od
->trackdir
) || (trackbits
& ~TRACK_BIT_CROSS
) || (red_signals
!= TRACKDIR_BIT_NONE
)) return true;
901 /* Are there more vehicles on the tile except the two vehicles involved in overtaking */
902 VehicleTileFinder
iter (od
->tile
);
903 while (!iter
.finished()) {
904 Vehicle
*v
= iter
.next();
905 if (v
->type
== VEH_ROAD
&& v
->First() == v
&& v
!= od
->u
&& v
!= od
->v
) {
909 return iter
.was_found();
912 static void RoadVehCheckOvertake(RoadVehicle
*v
, RoadVehicle
*u
)
919 if (u
->vcache
.cached_max_speed
>= v
->vcache
.cached_max_speed
&&
920 !(u
->vehstatus
& VS_STOPPED
) &&
925 /* Trams can't overtake other trams */
926 if (v
->roadtype
== ROADTYPE_TRAM
) return;
928 /* Don't overtake in stations */
929 if (IsStationTile(v
->tile
) || IsStationTile(u
->tile
)) return;
931 /* For now, articulated road vehicles can't overtake anything. */
932 if (v
->HasArticulatedPart()) return;
934 /* Vehicles are not driving in same direction || direction is not a diagonal direction */
935 if (v
->direction
!= u
->direction
|| !(v
->direction
& 1)) return;
937 /* Check if vehicle is in a road stop, depot, tunnel or bridge or not on a straight road */
938 if (v
->state
>= RVSB_IN_ROAD_STOP
|| !IsStraightRoadTrackdir((Trackdir
)(v
->state
& RVSB_TRACKDIR_MASK
))) return;
940 od
.trackdir
= DiagDirToDiagTrackdir(DirToDiagDir(v
->direction
));
942 /* Are the current and the next tile suitable for overtaking?
943 * - Does the track continue along od.trackdir
945 * - No barred levelcrossing
946 * - No other vehicles in the way
949 if (CheckRoadBlockedForOvertaking(&od
)) return;
951 od
.tile
= v
->tile
+ TileOffsByDiagDir(DirToDiagDir(v
->direction
));
952 if (CheckRoadBlockedForOvertaking(&od
)) return;
954 /* When the vehicle in front of us is stopped we may only take
955 * half the time to pass it than when the vehicle is moving. */
956 v
->overtaking_ctr
= (od
.u
->cur_speed
== 0 || (od
.u
->vehstatus
& VS_STOPPED
)) ? RV_OVERTAKE_TIMEOUT
/ 2 : 0;
960 static void RoadZPosAffectSpeed(RoadVehicle
*v
, byte old_z
)
962 if (old_z
== v
->z_pos
|| _settings_game
.vehicle
.roadveh_acceleration_model
!= AM_ORIGINAL
) return;
964 if (old_z
< v
->z_pos
) {
965 v
->cur_speed
= v
->cur_speed
* 232 / 256; // slow down by ~10%
967 uint16 spd
= v
->cur_speed
+ 2;
968 if (spd
<= v
->vcache
.cached_max_speed
) v
->cur_speed
= spd
;
972 static int PickRandomBit(uint bits
)
975 uint num
= RandomRange(CountBits(bits
));
977 for (i
= 0; !(bits
& 1) || (int)--num
>= 0; bits
>>= 1, i
++) {}
982 * Returns direction to for a road vehicle to take or
983 * INVALID_TRACKDIR if the direction is currently blocked
984 * @param v the Vehicle to do the pathfinding for
985 * @param tile the where to start the pathfinding
986 * @param enterdir the direction the vehicle enters the tile from
987 * @return the Trackdir to take
989 static Trackdir
RoadFindPathToDest(RoadVehicle
*v
, TileIndex tile
, DiagDirection enterdir
)
991 #define return_track(x) { best_track = (Trackdir)x; goto found_best_track; }
995 bool path_found
= true;
997 TrackStatus ts
= GetTileRoadStatus(tile
, v
->compatible_roadtypes
);
998 TrackdirBits red_signals
= TrackStatusToRedSignals(ts
); // crossing
999 TrackdirBits trackdirs
= TrackStatusToTrackdirBits(ts
);
1001 if (IsRoadDepotTile(tile
)) {
1002 if ((!IsTileOwner(tile
, v
->owner
) || GetGroundDepotDirection(tile
) == enterdir
|| (GetRoadTypes(tile
) & v
->compatible_roadtypes
) == 0)) {
1003 /* Road depot owned by another company or with the wrong orientation */
1004 trackdirs
= TRACKDIR_BIT_NONE
;
1006 } else if (IsStationTile(tile
) && IsStandardRoadStopTile(tile
)) {
1007 /* Standard road stop (drive-through stops are treated as normal road) */
1009 if (!IsTileOwner(tile
, v
->owner
) || GetRoadStopDir(tile
) == enterdir
|| v
->HasArticulatedPart()) {
1010 /* different station owner or wrong orientation or the vehicle has articulated parts */
1011 trackdirs
= TRACKDIR_BIT_NONE
;
1014 RoadStopType rstype
= v
->IsBus() ? ROADSTOP_BUS
: ROADSTOP_TRUCK
;
1016 if (GetRoadStopType(tile
) != rstype
) {
1017 /* Wrong station type */
1018 trackdirs
= TRACKDIR_BIT_NONE
;
1020 /* Proper station type, check if there is free loading bay */
1021 if (!_settings_game
.pf
.roadveh_queue
&& IsStandardRoadStopTile(tile
) &&
1022 !RoadStop::GetByTile(tile
, rstype
)->HasFreeBay()) {
1023 /* Station is full and RV queuing is off */
1024 trackdirs
= TRACKDIR_BIT_NONE
;
1029 /* The above lookups should be moved to GetTileTrackStatus in the
1030 * future, but that requires more changes to the pathfinder and other
1031 * stuff, probably even more arguments to GTTS.
1034 /* Remove tracks unreachable from the enter dir */
1035 trackdirs
&= DiagdirReachesTrackdirs(enterdir
);
1036 if (trackdirs
== TRACKDIR_BIT_NONE
) {
1037 /* No reachable tracks, so we'll reverse */
1038 return_track(_road_reverse_table
[enterdir
]);
1041 if (v
->reverse_ctr
!= 0) {
1042 bool reverse
= true;
1043 if (v
->roadtype
== ROADTYPE_TRAM
) {
1044 /* Trams may only reverse on a tile if it contains at least the straight
1045 * trackbits or when it is a valid turning tile (i.e. one roadbit) */
1046 RoadBits rb
= GetAnyRoadBits(tile
, ROADTYPE_TRAM
);
1047 RoadBits straight
= AxisToRoadBits(DiagDirToAxis(enterdir
));
1048 reverse
= ((rb
& straight
) == straight
) ||
1049 (rb
== DiagDirToRoadBits(enterdir
));
1053 if (v
->tile
!= tile
) {
1054 return_track(_road_reverse_table
[enterdir
]);
1059 desttile
= v
->dest_tile
;
1060 if (desttile
== 0) {
1061 /* We've got no destination, pick a random track */
1062 return_track(PickRandomBit(trackdirs
));
1065 /* Only one track to choose between? */
1066 if (KillFirstBit(trackdirs
) == TRACKDIR_BIT_NONE
) {
1067 return_track(FindFirstBit2x64(trackdirs
));
1070 best_track
= YapfRoadVehicleChooseTrack(v
, tile
, enterdir
, trackdirs
, path_found
);
1071 v
->HandlePathfindingResult(path_found
);
1075 if (HasBit(red_signals
, best_track
)) return INVALID_TRACKDIR
;
1080 struct RoadDriveEntry
{
1084 #include "table/roadveh_movement.h"
1086 static bool RoadVehLeaveDepot(RoadVehicle
*v
, bool first
)
1088 /* Don't leave unless v and following wagons are in the depot. */
1089 for (const RoadVehicle
*u
= v
; u
!= NULL
; u
= u
->Next()) {
1090 if (u
->state
!= RVSB_IN_DEPOT
|| u
->tile
!= v
->tile
) return false;
1093 DiagDirection dir
= GetGroundDepotDirection(v
->tile
);
1094 v
->direction
= DiagDirToDir(dir
);
1096 Trackdir tdir
= DiagDirToDiagTrackdir(dir
);
1097 const RoadDriveEntry
*rdp
= _road_drive_data
[_settings_game
.vehicle
.road_side
][tdir
];
1099 int x
= TileX(v
->tile
) * TILE_SIZE
+ rdp
[RVC_DEPOT_START_FRAME
].x
;
1100 int y
= TileY(v
->tile
) * TILE_SIZE
+ rdp
[RVC_DEPOT_START_FRAME
].y
;
1103 /* We are leaving a depot, but have to go to the exact same one; re-enter */
1104 if (v
->current_order
.IsType(OT_GOTO_DEPOT
) && v
->tile
== v
->dest_tile
) {
1105 VehicleEnterDepot(v
);
1109 if (RoadVehFindCloseTo(v
, x
, y
, v
->direction
, false) != NULL
) return true;
1111 VehicleServiceInDepot(v
);
1113 StartRoadVehSound(v
);
1115 /* Vehicle is about to leave a depot */
1119 v
->vehstatus
&= ~VS_HIDDEN
;
1121 v
->frame
= RVC_DEPOT_START_FRAME
;
1125 VehicleUpdatePosition(v
);
1126 v
->UpdateInclination(true, true);
1128 InvalidateWindowData(WC_VEHICLE_DEPOT
, v
->tile
);
1133 static Trackdir
FollowPreviousRoadVehicle(const RoadVehicle
*v
, const RoadVehicle
*prev
, TileIndex tile
, DiagDirection entry_dir
)
1136 if (prev
->tile
!= tile
) {
1138 * The previous vehicle has already left the tile; follow
1139 * the trackdir that heads in its direction.
1141 DiagDirection exitdir
= DiagdirBetweenTiles(tile
, prev
->tile
);
1142 assert(IsValidDiagDirection(exitdir
));
1143 dir
= EnterdirExitdirToTrackdir(entry_dir
, exitdir
);
1145 byte prev_state
= prev
->state
;
1147 assert(prev_state
!= RVSB_WORMHOLE
);
1149 if (prev_state
== RVSB_IN_DEPOT
) {
1150 dir
= DiagDirToDiagTrackdir(ReverseDiagDir(GetGroundDepotDirection(tile
)));
1151 } else if (HasBit(prev_state
, RVS_IN_DT_ROAD_STOP
)) {
1152 dir
= (Trackdir
)(prev_state
& RVSB_ROAD_STOP_TRACKDIR_MASK
);
1154 assert(prev_state
< TRACKDIR_END
);
1155 dir
= (Trackdir
)prev_state
;
1157 /* Some bends are so short that the vehicle ahead has
1158 * already left the tile when we reach it; this is
1159 * caught by the conditional prev->tile != tile above.
1160 * However, if the vehicle ahead turned around at the
1161 * tile edge instead of moving forward, it is still
1162 * in this tile but has switched to a reversing
1163 * trackdir. In such a case, we must not use its
1164 * trackdir, but head in the direction of the tile
1165 * side at which it is reversing. */
1166 if (IsReversingRoadTrackdir(dir
)) {
1167 DiagDirection side
= TrackdirToExitdir(dir
);
1168 assert (entry_dir
!= side
);
1169 side
= ReverseDiagDir(side
);
1170 if (entry_dir
!= side
) dir
= EnterdirExitdirToTrackdir(entry_dir
, side
);
1175 /* Do some sanity checking. */
1176 if (!IsReversingRoadTrackdir(dir
)) {
1177 static const RoadBits required_roadbits
[TRACK_END
] = {
1178 ROAD_X
, ROAD_Y
, ROAD_N
, ROAD_S
, ROAD_W
, ROAD_E
,
1181 RoadBits required
= required_roadbits
[TrackdirToTrack(dir
)];
1183 assert((required
& GetAnyRoadBits(tile
, v
->roadtype
, true)) != ROAD_NONE
);
1190 * Can a tram track build without destruction on the given tile?
1191 * @param c the company that would be building the tram tracks
1192 * @param t the tile to build on.
1193 * @param r the road bits needed.
1194 * @return true when a track track can be build on 't'
1196 static bool CanBuildTramTrackOnTile(CompanyID c
, TileIndex t
, RoadBits r
)
1198 /* The 'current' company is not necessarily the owner of the vehicle. */
1199 Backup
<CompanyByte
> cur_company(_current_company
, c
, FILE_LINE
);
1201 CommandCost ret
= DoCommand(t
, ROADTYPE_TRAM
<< 4 | r
, 0, DC_NO_WATER
, CMD_BUILD_ROAD
);
1203 cur_company
.Restore();
1204 return ret
.Succeeded();
1208 * Make a road vehicle enter a wormhole.
1209 * @param v The road vehicle
1210 * @param end The other wormhole end
1211 * @param is_bridge Whether the wormhole is a bridge (as opposed to a tunnel)
1212 * @return Whether the vehicle moved at all
1213 * @pre The vehicle must be at the last frame of a wormhole head tile
1215 static bool IndividualRoadVehicleControllerEnterWormhole(RoadVehicle
*v
, TileIndex end
, bool is_bridge
)
1217 VehiclePos gp
= GetNewVehiclePos(v
);
1219 /* this should really bring us to a new virtual tile */
1220 assert(gp
.new_tile
!= v
->tile
);
1222 if (v
->IsFrontEngine()) {
1223 const Vehicle
*u
= RoadVehFindCloseTo (v
, gp
.x
, gp
.y
, v
->direction
);
1225 v
->cur_speed
= u
->First()->cur_speed
;
1231 v
->state
= RVSB_WORMHOLE
;
1236 ClrBit(v
->gv_flags
, GVF_GOINGUP_BIT
);
1237 ClrBit(v
->gv_flags
, GVF_GOINGDOWN_BIT
);
1239 RoadVehicle
*first
= v
->First();
1240 first
->cur_speed
= min(first
->cur_speed
, GetBridgeSpec(GetRoadBridgeType(end
))->speed
* 2);
1242 VehicleUpdatePositionAndViewport(v
);
1244 VehicleUpdatePosition(v
);
1250 static bool IndividualRoadVehicleController(RoadVehicle
*v
, const RoadVehicle
*prev
)
1252 if (v
->overtaking
!= 0) {
1253 if (IsStationTile(v
->tile
)) {
1254 /* Force us to be not overtaking! */
1256 } else if (++v
->overtaking_ctr
>= RV_OVERTAKE_TIMEOUT
) {
1257 /* If overtaking just aborts at a random moment, we can have a out-of-bound problem,
1258 * if the vehicle started a corner. To protect that, only allow an abort of
1259 * overtake if we are on straight roads */
1260 if (v
->state
< RVSB_IN_ROAD_STOP
&& IsStraightRoadTrackdir((Trackdir
)v
->state
)) {
1266 /* If this vehicle is in a depot and we've reached this point it must be
1267 * one of the articulated parts. It will stay in the depot until activated
1268 * by the previous vehicle in the chain when it gets to the right place. */
1269 if (v
->IsInDepot()) return true;
1272 TileIndex tile
; // only used when entering a new tile
1274 if (v
->state
== RVSB_WORMHOLE
) {
1275 /* Vehicle is entering a depot or is on a bridge or in a tunnel */
1276 VehiclePos gp
= GetNewVehiclePos(v
);
1278 if (v
->IsFrontEngine()) {
1279 const Vehicle
*u
= RoadVehFindCloseTo(v
, gp
.x
, gp
.y
, v
->direction
);
1281 v
->cur_speed
= u
->First()->cur_speed
;
1286 if (gp
.new_tile
!= v
->tile
) {
1287 /* Still in the wormhole */
1290 VehicleUpdatePosition(v
);
1291 if ((v
->vehstatus
& VS_HIDDEN
) == 0) VehicleUpdateViewport(v
, true);
1295 /* Vehicle has just exited a bridge or tunnel */
1296 rd
.x
= RDE_NEXT_TILE
;
1297 rd
.y
= ReverseDiagDir(GetTunnelBridgeDirection(gp
.new_tile
));
1300 /* Get move position data for next frame.
1301 * For a drive-through road stop use 'straight road' move data.
1302 * In this case v->state is masked to give the road stop entry direction. */
1303 rd
= _road_drive_data
[_settings_game
.vehicle
.road_side
^ v
->overtaking
]
1304 [HasBit(v
->state
, RVS_IN_DT_ROAD_STOP
) ? (v
->state
& RVSB_ROAD_STOP_TRACKDIR_MASK
) : v
->state
]
1307 if (rd
.x
== RDE_NEXT_TILE
) {
1308 DiagDirection enterdir
= (DiagDirection
)(rd
.y
);
1309 tile
= v
->tile
+ TileOffsByDiagDir(enterdir
);
1311 if (IsTunnelTile(v
->tile
) && GetTunnelBridgeDirection(v
->tile
) == enterdir
) {
1312 TileIndex end_tile
= GetOtherTunnelEnd(v
->tile
);
1313 if (end_tile
!= tile
) {
1314 /* Entering a tunnel */
1315 return IndividualRoadVehicleControllerEnterWormhole (v
, end_tile
, false);
1317 } else if (IsRoadBridgeTile(v
->tile
) && GetTunnelBridgeDirection(v
->tile
) == enterdir
) {
1318 TileIndex end_tile
= GetOtherBridgeEnd(v
->tile
);
1319 if (end_tile
!= tile
) {
1320 /* Entering a bridge */
1321 return IndividualRoadVehicleControllerEnterWormhole (v
, end_tile
, true);
1327 if (rd
.x
== RDE_NEXT_TILE
) {
1328 DiagDirection enterdir
= (DiagDirection
)(rd
.y
);
1332 if (v
->IsFrontEngine()) {
1333 /* If this is the front engine, look for the right path. */
1334 dir
= RoadFindPathToDest(v
, tile
, enterdir
);
1336 if (dir
== INVALID_TRACKDIR
) {
1341 dir
= FollowPreviousRoadVehicle(v
, prev
, tile
, enterdir
);
1344 uint start_frame
= RVC_DEFAULT_START_FRAME
;
1345 if (IsReversingRoadTrackdir(dir
)) {
1346 /* When turning around we can't be overtaking. */
1348 bool use_long_corner
;
1350 /* Turning around */
1351 if (v
->roadtype
== ROADTYPE_ROAD
) {
1353 if (IsNormalRoadTile(v
->tile
) && GetDisallowedRoadDirections(v
->tile
) != DRD_NONE
) {
1357 use_long_corner
= false;
1358 } else if (v
->IsFrontEngine()) {
1359 /* Tram front vehicle. */
1361 /* Determine the road bits the tram needs to be able to turn around
1362 * using the 'big' corner loop. */
1365 default: NOT_REACHED();
1366 case TRACKDIR_RVREV_NE
: needed
= ROAD_SW
; break;
1367 case TRACKDIR_RVREV_SE
: needed
= ROAD_NW
; break;
1368 case TRACKDIR_RVREV_SW
: needed
= ROAD_NE
; break;
1369 case TRACKDIR_RVREV_NW
: needed
= ROAD_SE
; break;
1371 if (IsNormalRoadTile(tile
) && !HasRoadWorks(tile
) &&
1372 (needed
& GetRoadBits(tile
, ROADTYPE_TRAM
)) != ROAD_NONE
) {
1374 * Taking the 'big' corner for trams only happens when
1375 * the front of the tram can drive over the next tile.
1377 use_long_corner
= true;
1378 } else if (!CanBuildTramTrackOnTile(v
->owner
, tile
, needed
) || ((~needed
& GetAnyRoadBits(v
->tile
, ROADTYPE_TRAM
, false)) == ROAD_NONE
)) {
1380 * Taking the 'small' corner for trams only happens when
1381 * the company cannot build on the next tile.
1383 use_long_corner
= false;
1385 /* The company can build on the next tile, so wait till (s)he does. */
1390 /* Tram, not front vehicle. Just follow the previous vehicle. */
1391 use_long_corner
= (v
->Previous()->tile
== tile
);
1394 if (use_long_corner
) {
1395 start_frame
= RVC_LONG_TURN_START_FRAME
;
1398 * The 'small' corner means that the vehicle is on the end of a
1399 * tram track and needs to start turning there. It therefore
1400 * does not go to the next tile, so that needs to be fixed.
1403 start_frame
= RVC_SHORT_TURN_START_FRAME
;
1407 /* Get position data for first frame on the new tile */
1408 const RoadDriveEntry
*rdp
= _road_drive_data
[_settings_game
.vehicle
.road_side
^ v
->overtaking
][dir
];
1410 int x
= TileX(tile
) * TILE_SIZE
+ rdp
[start_frame
].x
;
1411 int y
= TileY(tile
) * TILE_SIZE
+ rdp
[start_frame
].y
;
1413 Direction new_dir
= RoadVehGetSlidingDirection(v
, x
, y
);
1414 if (v
->IsFrontEngine()) {
1415 Vehicle
*u
= RoadVehFindCloseTo(v
, x
, y
, new_dir
);
1417 v
->cur_speed
= u
->First()->cur_speed
;
1422 uint32 r
= RoadVehEnterTile(v
, tile
, x
, y
);
1423 if (r
== VETS_CANNOT_ENTER
) {
1424 assert(IsRoadStopTile(tile
));
1429 if (IsInsideMM(v
->state
, RVSB_IN_ROAD_STOP
, RVSB_IN_DT_ROAD_STOP_END
) && IsStationTile(v
->tile
)) {
1430 if (IsReversingRoadTrackdir(dir
) && IsInsideMM(v
->state
, RVSB_IN_ROAD_STOP
, RVSB_IN_ROAD_STOP_END
)) {
1431 /* New direction is trying to turn vehicle around.
1432 * We can't turn at the exit of a road stop so wait.*/
1437 /* If we are a drive through road stop and the next tile is of
1438 * the same road stop and the next tile isn't this one (i.e. we
1439 * are not reversing), then keep the reservation and state.
1440 * This way we will not be shortly unregister from the road
1441 * stop. It also makes it possible to load when on the edge of
1442 * two road stops; otherwise you could get vehicles that should
1443 * be loading but are not actually loading. */
1444 if (IsDriveThroughStopTile(v
->tile
) &&
1445 RoadStop::IsDriveThroughRoadStopContinuation(v
->tile
, tile
) &&
1447 /* So, keep 'our' state */
1448 dir
= (Trackdir
)v
->state
;
1449 } else if (IsRoadStop(v
->tile
)) {
1450 /* We're not continuing our drive through road stop, so leave. */
1451 RoadStop::GetByTile(v
->tile
, GetRoadStopType(v
->tile
))->Leave(v
);
1455 if (r
!= VETS_ENTERED_WORMHOLE
) {
1457 v
->state
= (byte
)dir
;
1458 v
->frame
= start_frame
;
1460 if (new_dir
!= v
->direction
) {
1461 v
->direction
= new_dir
;
1462 if (_settings_game
.vehicle
.roadveh_acceleration_model
== AM_ORIGINAL
) v
->cur_speed
-= v
->cur_speed
>> 2;
1466 VehicleUpdatePosition(v
);
1467 RoadZPosAffectSpeed(v
, v
->UpdateInclination(true, true));
1471 if (rd
.x
== RDE_TURNED
) {
1472 /* Vehicle has finished turning around, it will now head back onto the same tile */
1473 DiagDirection enterdir
= (DiagDirection
)(rd
.y
);
1476 if (v
->roadtype
== ROADTYPE_TRAM
&& IsNormalRoadTile(v
->tile
) && HasExactlyOneBit(GetRoadBits(v
->tile
, ROADTYPE_TRAM
))) {
1478 * The tram is turning around with one tram 'roadbit'. This means that
1479 * it is using the 'big' corner 'drive data'. When the tram reaches the
1480 * 'turned' marker, we switch it to the corresponding straight drive data.
1482 dir
= DiagDirToDiagTrackdir(enterdir
);
1484 if (v
->IsFrontEngine()) {
1485 /* If this is the front engine, look for the right path. */
1486 dir
= RoadFindPathToDest(v
, v
->tile
, enterdir
);
1488 if (dir
== INVALID_TRACKDIR
) {
1493 dir
= FollowPreviousRoadVehicle(v
, prev
, v
->tile
, enterdir
);
1497 const RoadDriveEntry
*rdp
= _road_drive_data
[_settings_game
.vehicle
.road_side
][dir
];
1499 int x
= TileX(v
->tile
) * TILE_SIZE
+ rdp
[RVC_AFTER_TURN_START_FRAME
].x
;
1500 int y
= TileY(v
->tile
) * TILE_SIZE
+ rdp
[RVC_AFTER_TURN_START_FRAME
].y
;
1502 Direction new_dir
= RoadVehGetSlidingDirection(v
, x
, y
);
1503 if (v
->IsFrontEngine() && RoadVehFindCloseTo(v
, x
, y
, new_dir
) != NULL
) return false;
1505 uint32 r
= RoadVehEnterTile(v
, v
->tile
, x
, y
);
1506 if (r
== VETS_CANNOT_ENTER
) {
1512 v
->frame
= RVC_AFTER_TURN_START_FRAME
;
1514 if (new_dir
!= v
->direction
) {
1515 v
->direction
= new_dir
;
1516 if (_settings_game
.vehicle
.roadveh_acceleration_model
== AM_ORIGINAL
) v
->cur_speed
-= v
->cur_speed
>> 2;
1521 VehicleUpdatePosition(v
);
1522 RoadZPosAffectSpeed(v
, v
->UpdateInclination(true, true));
1526 /* This vehicle is not in a wormhole and it hasn't entered a new tile. If
1527 * it's on a depot tile, check if it's time to activate the next vehicle in
1529 if (v
->Next() != NULL
&& IsRoadDepotTile(v
->tile
)) {
1530 if (v
->frame
== v
->gcache
.cached_veh_length
+ RVC_DEPOT_START_FRAME
) {
1531 RoadVehLeaveDepot(v
->Next(), false);
1535 /* Calculate new position for the vehicle */
1536 int x
= (v
->x_pos
& ~15) + rd
.x
;
1537 int y
= (v
->y_pos
& ~15) + rd
.y
;
1539 Direction new_dir
= RoadVehGetSlidingDirection(v
, x
, y
);
1541 if (v
->IsFrontEngine() && !IsInsideMM(v
->state
, RVSB_IN_ROAD_STOP
, RVSB_IN_ROAD_STOP_END
)) {
1542 /* Vehicle is not in a road stop.
1543 * Check for another vehicle to overtake */
1544 RoadVehicle
*u
= RoadVehFindCloseTo(v
, x
, y
, new_dir
);
1548 /* There is a vehicle in front overtake it if possible */
1549 if (v
->overtaking
== 0) RoadVehCheckOvertake(v
, u
);
1550 if (v
->overtaking
== 0) v
->cur_speed
= u
->cur_speed
;
1552 /* In case an RV is stopped in a road stop, why not try to load? */
1553 if (v
->cur_speed
== 0 && IsInsideMM(v
->state
, RVSB_IN_DT_ROAD_STOP
, RVSB_IN_DT_ROAD_STOP_END
) &&
1554 v
->current_order
.ShouldStopAtStation(v
, GetStationIndex(v
->tile
)) &&
1555 v
->owner
== GetTileOwner(v
->tile
) && !v
->current_order
.IsType(OT_LEAVESTATION
) &&
1556 GetRoadStopType(v
->tile
) == (v
->IsBus() ? ROADSTOP_BUS
: ROADSTOP_TRUCK
)) {
1557 Station
*st
= Station::GetByTile(v
->tile
);
1558 v
->last_station_visited
= st
->index
;
1559 RoadVehArrivesAt(v
, st
);
1566 Direction old_dir
= v
->direction
;
1567 if (new_dir
!= old_dir
) {
1568 v
->direction
= new_dir
;
1569 if (_settings_game
.vehicle
.roadveh_acceleration_model
== AM_ORIGINAL
) v
->cur_speed
-= v
->cur_speed
>> 2;
1570 if (HasBit(v
->state
, RVS_IN_ROAD_STOP
) && _road_stop_stop_frame
[_settings_game
.vehicle
.road_side
][v
->state
& RVSB_TRACKDIR_MASK
] == v
->frame
) {
1571 /* The vehicle is in a road stop, and at its stopping
1572 * frame. Make a delay here until the vehicle has
1574 v
->UpdateInclination(false, true);
1579 /* If the vehicle is in a normal road stop and the frame equals the stop frame OR
1580 * if the vehicle is in a drive-through road stop and this is the destination station
1581 * and it's the correct type of stop (bus or truck) and the frame equals the stop frame...
1582 * (the station test and stop type test ensure that other vehicles, using the road stop as
1583 * a through route, do not stop) */
1584 if (v
->IsFrontEngine() && ((HasBit(v
->state
, RVS_IN_ROAD_STOP
) &&
1585 _road_stop_stop_frame
[_settings_game
.vehicle
.road_side
][v
->state
& RVSB_TRACKDIR_MASK
] == v
->frame
) ||
1586 (HasBit(v
->state
, RVS_IN_DT_ROAD_STOP
) &&
1587 v
->current_order
.ShouldStopAtStation(v
, GetStationIndex(v
->tile
)) &&
1588 v
->owner
== GetTileOwner(v
->tile
) &&
1589 GetRoadStopType(v
->tile
) == (v
->IsBus() ? ROADSTOP_BUS
: ROADSTOP_TRUCK
) &&
1590 v
->frame
== RVC_DRIVE_THROUGH_STOP_FRAME
))) {
1592 RoadStop
*rs
= RoadStop::GetByTile(v
->tile
, GetRoadStopType(v
->tile
));
1593 Station
*st
= Station::GetByTile(v
->tile
);
1595 /* Vehicle is at the stop position (at a bay) in a road stop.
1596 * Note, if vehicle is loading/unloading it has already been handled,
1597 * so if we get here the vehicle has just arrived or is just ready to leave. */
1598 if (!HasBit(v
->state
, RVS_ENTERED_STOP
)) {
1599 /* Vehicle has arrived at a bay in a road stop */
1601 if (IsDriveThroughStopTile(v
->tile
)) {
1602 TileIndex next_tile
= TILE_ADD(v
->tile
, TileOffsByDir(v
->direction
));
1604 /* Check if next inline bay is free and has compatible road. */
1605 if (RoadStop::IsDriveThroughRoadStopContinuation(v
->tile
, next_tile
) && (GetRoadTypes(next_tile
) & v
->compatible_roadtypes
) != 0) {
1609 VehicleUpdatePosition(v
);
1610 RoadZPosAffectSpeed(v
, v
->UpdateInclination(true, false));
1615 rs
->SetEntranceBusy(false);
1616 SetBit(v
->state
, RVS_ENTERED_STOP
);
1618 v
->last_station_visited
= st
->index
;
1620 if (IsDriveThroughStopTile(v
->tile
) || (v
->current_order
.IsType(OT_GOTO_STATION
) && v
->current_order
.GetDestination() == st
->index
)) {
1621 RoadVehArrivesAt(v
, st
);
1626 /* Vehicle is ready to leave a bay in a road stop */
1627 if (rs
->IsEntranceBusy()) {
1628 /* Road stop entrance is busy, so wait as there is nowhere else to go */
1632 if (v
->current_order
.IsType(OT_LEAVESTATION
)) v
->current_order
.Free();
1635 if (IsStandardRoadStopTile(v
->tile
)) rs
->SetEntranceBusy(true);
1637 StartRoadVehSound(v
);
1638 SetWindowWidgetDirty(WC_VEHICLE_VIEW
, v
->index
, WID_VV_START_STOP
);
1641 /* Check tile position conditions - i.e. stop position in depot,
1642 * entry onto bridge or into tunnel */
1643 uint32 r
= RoadVehEnterTile(v
, v
->tile
, x
, y
);
1644 if (r
== VETS_CANNOT_ENTER
) {
1649 if (v
->current_order
.IsType(OT_LEAVESTATION
) && IsDriveThroughStopTile(v
->tile
)) {
1650 v
->current_order
.Free();
1653 /* Move to next frame unless vehicle arrived at a stop position
1654 * in a depot or entered a tunnel/bridge */
1655 if (r
!= VETS_ENTERED_WORMHOLE
) v
->frame
++;
1658 VehicleUpdatePosition(v
);
1659 RoadZPosAffectSpeed(v
, v
->UpdateInclination(false, true));
1663 static bool RoadVehController(RoadVehicle
*v
)
1665 /* decrease counters */
1666 v
->current_order_time
++;
1667 if (v
->reverse_ctr
!= 0) v
->reverse_ctr
--;
1669 /* handle crashed */
1670 if (v
->vehstatus
& VS_CRASHED
|| RoadVehCheckTrainCrash(v
)) {
1671 return RoadVehIsCrashed(v
);
1674 /* road vehicle has broken down? */
1675 if (v
->HandleBreakdown()) return true;
1676 if (v
->vehstatus
& VS_STOPPED
) return true;
1681 if (v
->current_order
.IsType(OT_LOADING
)) return true;
1683 if (v
->IsInDepot() && RoadVehLeaveDepot(v
, true)) return true;
1685 v
->ShowVisualEffect();
1687 /* Check how far the vehicle needs to proceed */
1688 int j
= v
->UpdateSpeed();
1690 int adv_spd
= v
->GetAdvanceDistance();
1691 bool blocked
= false;
1692 while (j
>= adv_spd
) {
1696 for (RoadVehicle
*prev
= NULL
; u
!= NULL
; prev
= u
, u
= u
->Next()) {
1697 if (!IndividualRoadVehicleController(u
, prev
)) {
1704 /* Determine distance to next map position */
1705 adv_spd
= v
->GetAdvanceDistance();
1707 /* Test for a collision, but only if another movement will occur. */
1708 if (j
>= adv_spd
&& RoadVehCheckTrainCrash(v
)) break;
1713 for (RoadVehicle
*u
= v
; u
!= NULL
; u
= u
->Next()) {
1714 if ((u
->vehstatus
& VS_HIDDEN
) != 0) continue;
1716 u
->UpdateViewport(false, false);
1719 /* If movement is blocked, set 'progress' to its maximum, so the roadvehicle does
1720 * not accelerate again before it can actually move. I.e. make sure it tries to advance again
1721 * on next tick to discover whether it is still blocked. */
1722 if (v
->progress
== 0) v
->progress
= blocked
? adv_spd
- 1 : j
;
1727 Money
RoadVehicle::GetRunningCost() const
1729 const Engine
*e
= this->GetEngine();
1730 if (e
->u
.road
.running_cost_class
== INVALID_PRICE
) return 0;
1732 uint cost_factor
= GetVehicleProperty(this, PROP_ROADVEH_RUNNING_COST_FACTOR
, e
->u
.road
.running_cost
);
1733 if (cost_factor
== 0) return 0;
1735 return GetPrice(e
->u
.road
.running_cost_class
, cost_factor
, e
->GetGRF());
1738 bool RoadVehicle::Tick()
1740 this->tick_counter
++;
1742 if (this->IsFrontEngine()) {
1743 if (!(this->vehstatus
& VS_STOPPED
)) this->running_ticks
++;
1744 return RoadVehController(this);
1750 static void CheckIfRoadVehNeedsService(RoadVehicle
*v
)
1752 /* If we already got a slot at a stop, use that FIRST, and go to a depot later */
1753 if (Company::Get(v
->owner
)->settings
.vehicle
.servint_roadveh
== 0 || !v
->NeedsAutomaticServicing()) return;
1754 if (v
->IsChainInDepot()) {
1755 VehicleServiceInDepot(v
);
1759 /* Only go to the depot if it is not too far out of our way. */
1760 TileIndex rfdd
= FindClosestRoadDepot(v
, true);
1761 if (rfdd
== INVALID_TILE
) {
1762 if (v
->current_order
.IsType(OT_GOTO_DEPOT
)) {
1763 /* If we were already heading for a depot but it has
1764 * suddenly moved farther away, we continue our normal
1766 v
->current_order
.MakeDummy();
1767 SetWindowWidgetDirty(WC_VEHICLE_VIEW
, v
->index
, WID_VV_START_STOP
);
1772 DepotID depot
= GetDepotIndex(rfdd
);
1774 if (v
->current_order
.IsType(OT_GOTO_DEPOT
) &&
1775 v
->current_order
.GetNonStopType() & ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS
&&
1780 SetBit(v
->gv_flags
, GVF_SUPPRESS_IMPLICIT_ORDERS
);
1781 v
->current_order
.MakeGoToDepot(depot
, ODTFB_SERVICE
);
1782 v
->dest_tile
= rfdd
;
1783 SetWindowWidgetDirty(WC_VEHICLE_VIEW
, v
->index
, WID_VV_START_STOP
);
1786 void RoadVehicle::OnNewDay()
1790 if (!this->IsFrontEngine()) return;
1792 if ((++this->day_counter
& 7) == 0) DecreaseVehicleValue(this);
1793 if (this->blocked_ctr
== 0) CheckVehicleBreakdown(this);
1795 CheckIfRoadVehNeedsService(this);
1799 if (this->running_ticks
== 0) return;
1801 CommandCost
cost(EXPENSES_ROADVEH_RUN
, this->GetRunningCost() * this->running_ticks
/ (DAYS_IN_YEAR
* DAY_TICKS
));
1803 this->profit_this_year
-= cost
.GetCost();
1804 this->running_ticks
= 0;
1806 SubtractMoneyFromCompanyFract(this->owner
, cost
);
1808 SetWindowDirty(WC_VEHICLE_DETAILS
, this->index
);
1809 SetWindowClassesDirty(WC_ROADVEH_LIST
);
1812 RoadPathPos
RoadVehicle::GetPos() const
1814 if (this->vehstatus
& VS_CRASHED
) return RoadPathPos();
1818 if (this->IsInDepot()) {
1819 /* We'll assume the road vehicle is facing outwards */
1820 td
= DiagDirToDiagTrackdir(GetGroundDepotDirection(this->tile
));
1821 } else if (IsStandardRoadStopTile(this->tile
)) {
1822 /* We'll assume the road vehicle is facing outwards */
1823 td
= DiagDirToDiagTrackdir(GetRoadStopDir(this->tile
)); // Road vehicle in a station
1824 } else if (this->state
> RVSB_TRACKDIR_MASK
) {
1825 /* Drive through road stops / wormholes (tunnels) */
1826 td
= DiagDirToDiagTrackdir(DirToDiagDir(this->direction
));
1828 /* If vehicle's state is a valid track direction (vehicle is not turning around) return it,
1829 * otherwise transform it into a valid track direction */
1830 td
= (Trackdir
)((IsReversingRoadTrackdir((Trackdir
)this->state
)) ? (this->state
- 6) : this->state
);
1833 return RoadPathPos(this->tile
, td
);