2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
/** @file roadveh_cmd.cpp Handling of road vehicles. */
|
2007-03-28 20:41:35 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "stdafx.h"
|
2006-06-05 10:23:18 +00:00
|
|
|
#include "roadveh.h"
|
2007-12-21 21:50:46 +00:00
|
|
|
#include "command_func.h"
|
2008-03-28 08:53:36 +00:00
|
|
|
#include "news_func.h"
|
2009-12-02 10:44:38 +00:00
|
|
|
#include "pathfinder/npf/npf_func.h"
|
2009-12-01 22:45:39 +00:00
|
|
|
#include "station_base.h"
|
2008-09-30 20:51:04 +00:00
|
|
|
#include "company_func.h"
|
2007-06-11 14:00:16 +00:00
|
|
|
#include "articulated_vehicles.h"
|
2006-09-27 18:17:01 +00:00
|
|
|
#include "newgrf_sound.h"
|
2009-12-01 22:45:39 +00:00
|
|
|
#include "pathfinder/yapf/yapf.h"
|
2007-12-21 19:49:27 +00:00
|
|
|
#include "strings_func.h"
|
2007-12-16 15:38:51 +00:00
|
|
|
#include "tunnelbridge_map.h"
|
2007-12-26 13:50:40 +00:00
|
|
|
#include "date_func.h"
|
2007-12-27 13:35:39 +00:00
|
|
|
#include "vehicle_func.h"
|
2007-12-29 09:24:26 +00:00
|
|
|
#include "sound_func.h"
|
2009-01-12 17:11:45 +00:00
|
|
|
#include "ai/ai.hpp"
|
2011-12-19 20:59:36 +00:00
|
|
|
#include "game/game.hpp"
|
2009-10-04 17:16:41 +00:00
|
|
|
#include "depot_map.h"
|
2008-04-20 11:12:07 +00:00
|
|
|
#include "effectvehicle_func.h"
|
2009-06-23 21:44:48 +00:00
|
|
|
#include "roadstop_base.h"
|
2009-11-16 17:58:46 +00:00
|
|
|
#include "spritecache.h"
|
2010-01-15 16:41:15 +00:00
|
|
|
#include "core/random_func.hpp"
|
|
|
|
#include "company_base.h"
|
2010-05-31 20:22:57 +00:00
|
|
|
#include "core/backup_type.hpp"
|
2015-08-06 21:24:24 +00:00
|
|
|
#include "infrastructure_func.h"
|
2011-11-08 17:27:55 +00:00
|
|
|
#include "newgrf.h"
|
2011-11-24 12:38:48 +00:00
|
|
|
#include "zoom_func.h"
|
2018-07-19 19:17:07 +00:00
|
|
|
#include "framerate_type.h"
|
2017-02-19 12:47:22 +00:00
|
|
|
#include "scope_info.h"
|
|
|
|
#include "string_func.h"
|
2007-12-16 15:38:51 +00:00
|
|
|
|
2008-01-13 01:21:35 +00:00
|
|
|
#include "table/strings.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "safeguards.h"
|
|
|
|
|
2013-11-26 16:08:58 +00:00
|
|
|
static const uint16 _roadveh_images[] = {
|
2004-08-09 17:04:08 +00:00
|
|
|
0xCD4, 0xCDC, 0xCE4, 0xCEC, 0xCF4, 0xCFC, 0xD0C, 0xD14,
|
|
|
|
0xD24, 0xD1C, 0xD2C, 0xD04, 0xD1C, 0xD24, 0xD6C, 0xD74,
|
|
|
|
0xD7C, 0xC14, 0xC1C, 0xC24, 0xC2C, 0xC34, 0xC3C, 0xC4C,
|
|
|
|
0xC54, 0xC64, 0xC5C, 0xC6C, 0xC44, 0xC5C, 0xC64, 0xCAC,
|
|
|
|
0xCB4, 0xCBC, 0xD94, 0xD9C, 0xDA4, 0xDAC, 0xDB4, 0xDBC,
|
|
|
|
0xDCC, 0xDD4, 0xDE4, 0xDDC, 0xDEC, 0xDC4, 0xDDC, 0xDE4,
|
|
|
|
0xE2C, 0xE34, 0xE3C, 0xC14, 0xC1C, 0xC2C, 0xC3C, 0xC4C,
|
|
|
|
0xC5C, 0xC64, 0xC6C, 0xC74, 0xC84, 0xC94, 0xCA4
|
|
|
|
};
|
|
|
|
|
2013-11-26 16:08:58 +00:00
|
|
|
static const uint16 _roadveh_full_adder[] = {
|
2006-03-03 22:21:29 +00:00
|
|
|
0, 88, 0, 0, 0, 0, 48, 48,
|
|
|
|
48, 48, 0, 0, 64, 64, 0, 16,
|
|
|
|
16, 0, 88, 0, 0, 0, 0, 48,
|
|
|
|
48, 48, 48, 0, 0, 64, 64, 0,
|
|
|
|
16, 16, 0, 88, 0, 0, 0, 0,
|
|
|
|
48, 48, 48, 48, 0, 0, 64, 64,
|
|
|
|
0, 16, 16, 0, 8, 8, 8, 8,
|
|
|
|
0, 0, 0, 8, 8, 8, 8
|
2004-08-09 17:04:08 +00:00
|
|
|
};
|
2013-11-26 16:08:58 +00:00
|
|
|
assert_compile(lengthof(_roadveh_images) == lengthof(_roadveh_full_adder));
|
|
|
|
|
|
|
|
template <>
|
|
|
|
bool IsValidImageIndex<VEH_ROAD>(uint8 image_index)
|
|
|
|
{
|
|
|
|
return image_index < lengthof(_roadveh_images);
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-13 20:57:02 +00:00
|
|
|
static const Trackdir _road_reverse_table[DIAGDIR_END] = {
|
|
|
|
TRACKDIR_RVREV_NE, TRACKDIR_RVREV_SE, TRACKDIR_RVREV_SW, TRACKDIR_RVREV_NW
|
2004-08-09 17:04:08 +00:00
|
|
|
};
|
|
|
|
|
2009-12-02 17:37:02 +00:00
|
|
|
/**
|
|
|
|
* Check whether a roadvehicle is a bus
|
|
|
|
* @return true if bus
|
|
|
|
*/
|
|
|
|
bool RoadVehicle::IsBus() const
|
|
|
|
{
|
2011-01-19 18:44:13 +00:00
|
|
|
assert(this->IsFrontEngine());
|
2009-12-02 17:37:02 +00:00
|
|
|
return IsCargoInClass(this->cargo_type, CC_PASSENGERS);
|
|
|
|
}
|
|
|
|
|
2009-07-18 12:34:19 +00:00
|
|
|
/**
|
|
|
|
* Get the width of a road vehicle image in the GUI.
|
|
|
|
* @param offset Additional offset for positioning the sprite; set to NULL if not needed
|
|
|
|
* @return Width in pixels
|
|
|
|
*/
|
|
|
|
int RoadVehicle::GetDisplayImageWidth(Point *offset) const
|
|
|
|
{
|
|
|
|
int reference_width = ROADVEHINFO_DEFAULT_VEHICLE_WIDTH;
|
|
|
|
|
|
|
|
if (offset != NULL) {
|
2015-02-01 20:54:24 +00:00
|
|
|
offset->x = ScaleGUITrad(reference_width) / 2;
|
2009-07-18 12:34:19 +00:00
|
|
|
offset->y = 0;
|
|
|
|
}
|
2015-02-01 20:54:24 +00:00
|
|
|
return ScaleGUITrad(this->gcache.cached_veh_length * reference_width / VEHICLE_LENGTH);
|
2009-07-18 12:34:19 +00:00
|
|
|
}
|
|
|
|
|
2016-10-16 14:57:56 +00:00
|
|
|
static void GetRoadVehIcon(EngineID engine, EngineImageType image_type, VehicleSpriteSeq *result)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-10-06 19:17:07 +00:00
|
|
|
const Engine *e = Engine::Get(engine);
|
|
|
|
uint8 spritenum = e->u.road.image_index;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-04-21 20:50:58 +00:00
|
|
|
if (is_custom_sprite(spritenum)) {
|
2016-10-16 14:57:56 +00:00
|
|
|
GetCustomVehicleIcon(engine, DIR_W, image_type, result);
|
|
|
|
if (result->IsValid()) return;
|
2008-04-21 20:50:58 +00:00
|
|
|
|
2009-10-06 19:17:07 +00:00
|
|
|
spritenum = e->original_image_index;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2013-11-26 16:08:58 +00:00
|
|
|
assert(IsValidImageIndex<VEH_ROAD>(spritenum));
|
2016-10-16 14:57:56 +00:00
|
|
|
result->Set(DIR_W + _roadveh_images[spritenum]);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2016-10-16 14:57:56 +00:00
|
|
|
void RoadVehicle::GetImage(Direction direction, EngineImageType image_type, VehicleSpriteSeq *result) const
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-04-21 20:50:58 +00:00
|
|
|
uint8 spritenum = this->spritenum;
|
2004-11-13 18:01:33 +00:00
|
|
|
|
|
|
|
if (is_custom_sprite(spritenum)) {
|
2016-10-16 14:57:56 +00:00
|
|
|
GetCustomVehicleSprite(this, (Direction)(direction + 4 * IS_CUSTOM_SECONDHEAD_SPRITE(spritenum)), image_type, result);
|
|
|
|
if (result->IsValid()) return;
|
2004-11-13 18:01:33 +00:00
|
|
|
|
2011-11-01 00:21:08 +00:00
|
|
|
spritenum = this->GetEngine()->original_image_index;
|
2004-11-13 18:01:33 +00:00
|
|
|
}
|
2008-04-21 20:50:58 +00:00
|
|
|
|
2013-11-26 16:08:58 +00:00
|
|
|
assert(IsValidImageIndex<VEH_ROAD>(spritenum));
|
2016-10-16 14:57:56 +00:00
|
|
|
SpriteID sprite = direction + _roadveh_images[spritenum];
|
2008-04-21 20:50:58 +00:00
|
|
|
|
2013-04-13 13:42:08 +00:00
|
|
|
if (this->cargo.StoredCount() >= this->cargo_cap / 2U) sprite += _roadveh_full_adder[spritenum];
|
2008-04-21 20:50:58 +00:00
|
|
|
|
2016-10-16 14:57:56 +00:00
|
|
|
result->Set(sprite);
|
2008-04-21 20:50:58 +00:00
|
|
|
}
|
|
|
|
|
2010-08-15 14:06:43 +00:00
|
|
|
/**
|
|
|
|
* Draw a road vehicle engine.
|
|
|
|
* @param left Left edge to draw within.
|
|
|
|
* @param right Right edge to draw within.
|
|
|
|
* @param preferred_x Preferred position of the engine.
|
|
|
|
* @param y Vertical position of the engine.
|
|
|
|
* @param engine Engine to draw
|
|
|
|
* @param pal Palette to use.
|
|
|
|
*/
|
2011-11-01 16:51:47 +00:00
|
|
|
void DrawRoadVehEngine(int left, int right, int preferred_x, int y, EngineID engine, PaletteID pal, EngineImageType image_type)
|
2008-04-21 20:50:58 +00:00
|
|
|
{
|
2016-10-16 14:57:56 +00:00
|
|
|
VehicleSpriteSeq seq;
|
|
|
|
GetRoadVehIcon(engine, image_type, &seq);
|
|
|
|
|
2018-01-19 19:13:01 +00:00
|
|
|
Rect16 rect = seq.GetBounds();
|
2015-02-01 20:54:24 +00:00
|
|
|
preferred_x = Clamp(preferred_x,
|
2016-10-16 14:58:38 +00:00
|
|
|
left - UnScaleGUI(rect.left),
|
|
|
|
right - UnScaleGUI(rect.right));
|
|
|
|
|
|
|
|
seq.Draw(preferred_x, y, pal, pal == PALETTE_CRASH);
|
2012-12-23 01:00:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the size of the sprite of a road vehicle sprite heading west (used for lists).
|
|
|
|
* @param engine The engine to get the sprite from.
|
|
|
|
* @param[out] width The width of the sprite.
|
|
|
|
* @param[out] height The height of the sprite.
|
|
|
|
* @param[out] xoffs Number of pixels to shift the sprite to the right.
|
|
|
|
* @param[out] yoffs Number of pixels to shift the sprite downwards.
|
|
|
|
* @param image_type Context the sprite is used in.
|
|
|
|
*/
|
|
|
|
void GetRoadVehSpriteSize(EngineID engine, uint &width, uint &height, int &xoffs, int &yoffs, EngineImageType image_type)
|
|
|
|
{
|
2016-10-16 14:57:56 +00:00
|
|
|
VehicleSpriteSeq seq;
|
|
|
|
GetRoadVehIcon(engine, image_type, &seq);
|
|
|
|
|
2018-01-19 19:13:01 +00:00
|
|
|
Rect16 rect = seq.GetBounds();
|
2012-12-23 01:00:25 +00:00
|
|
|
|
2016-10-16 14:58:38 +00:00
|
|
|
width = UnScaleGUI(rect.right - rect.left + 1);
|
|
|
|
height = UnScaleGUI(rect.bottom - rect.top + 1);
|
|
|
|
xoffs = UnScaleGUI(rect.left);
|
|
|
|
yoffs = UnScaleGUI(rect.top);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2011-01-29 21:37:11 +00:00
|
|
|
/**
|
|
|
|
* Get length of a road vehicle.
|
|
|
|
* @param v Road vehicle to query length.
|
|
|
|
* @return Length of the given road vehicle.
|
|
|
|
*/
|
2009-10-20 19:55:09 +00:00
|
|
|
static uint GetRoadVehLength(const RoadVehicle *v)
|
2007-06-11 14:00:16 +00:00
|
|
|
{
|
2011-11-08 17:27:46 +00:00
|
|
|
const Engine *e = v->GetEngine();
|
2011-11-08 18:21:56 +00:00
|
|
|
uint length = VEHICLE_LENGTH;
|
2007-06-11 14:00:16 +00:00
|
|
|
|
2011-11-08 17:27:55 +00:00
|
|
|
uint16 veh_len = CALLBACK_FAILED;
|
|
|
|
if (e->GetGRF() != NULL && e->GetGRF()->grf_version >= 8) {
|
|
|
|
/* Use callback 36 */
|
|
|
|
veh_len = GetVehicleProperty(v, PROP_ROADVEH_SHORTEN_FACTOR, CALLBACK_FAILED);
|
2012-11-12 20:13:02 +00:00
|
|
|
if (veh_len != CALLBACK_FAILED && veh_len >= VEHICLE_LENGTH) ErrorUnknownCallbackResult(e->GetGRFID(), CBID_VEHICLE_LENGTH, veh_len);
|
2011-11-08 17:27:55 +00:00
|
|
|
} else {
|
|
|
|
/* Use callback 11 */
|
|
|
|
veh_len = GetVehicleCallback(CBID_VEHICLE_LENGTH, 0, 0, v->engine_type, v);
|
|
|
|
}
|
2011-11-08 18:21:56 +00:00
|
|
|
if (veh_len == CALLBACK_FAILED) veh_len = e->u.road.shorten_factor;
|
|
|
|
if (veh_len != 0) {
|
2011-03-05 21:52:45 +00:00
|
|
|
length -= Clamp(veh_len, 0, VEHICLE_LENGTH - 1);
|
2007-06-11 14:00:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2011-01-29 21:37:11 +00:00
|
|
|
/**
|
|
|
|
* Update the cache of a road vehicle.
|
|
|
|
* @param v Road vehicle needing an update of its cache.
|
2011-11-04 15:04:29 +00:00
|
|
|
* @param same_length should length of vehicles stay the same?
|
2011-01-29 21:37:11 +00:00
|
|
|
* @pre \a v must be first road vehicle.
|
|
|
|
*/
|
2011-11-04 15:04:29 +00:00
|
|
|
void RoadVehUpdateCache(RoadVehicle *v, bool same_length)
|
2007-06-11 14:00:16 +00:00
|
|
|
{
|
|
|
|
assert(v->type == VEH_ROAD);
|
2011-01-19 18:44:13 +00:00
|
|
|
assert(v->IsFrontEngine());
|
2007-06-11 14:00:16 +00:00
|
|
|
|
2009-05-31 12:03:14 +00:00
|
|
|
v->InvalidateNewGRFCacheOfChain();
|
|
|
|
|
2010-12-14 21:33:53 +00:00
|
|
|
v->gcache.cached_total_length = 0;
|
2009-12-18 21:34:06 +00:00
|
|
|
|
2009-05-22 20:18:45 +00:00
|
|
|
for (RoadVehicle *u = v; u != NULL; u = u->Next()) {
|
2007-08-30 21:11:12 +00:00
|
|
|
/* Check the v->first cache. */
|
|
|
|
assert(u->First() == v);
|
2007-06-11 14:00:16 +00:00
|
|
|
|
|
|
|
/* Update the 'first engine' */
|
2010-12-14 21:33:53 +00:00
|
|
|
u->gcache.first_engine = (v == u) ? INVALID_ENGINE : v->engine_type;
|
2007-06-11 14:00:16 +00:00
|
|
|
|
|
|
|
/* Update the length of the vehicle. */
|
2011-11-04 15:04:29 +00:00
|
|
|
uint veh_len = GetRoadVehLength(u);
|
|
|
|
/* Verify length hasn't changed. */
|
|
|
|
if (same_length && veh_len != u->gcache.cached_veh_length) VehicleLengthChanged(u);
|
|
|
|
|
|
|
|
u->gcache.cached_veh_length = veh_len;
|
2010-12-14 21:33:53 +00:00
|
|
|
v->gcache.cached_total_length += u->gcache.cached_veh_length;
|
2009-01-27 19:44:36 +00:00
|
|
|
|
2010-11-18 14:17:55 +00:00
|
|
|
/* Update visual effect */
|
2013-12-25 11:37:10 +00:00
|
|
|
u->UpdateVisualEffect();
|
2010-11-18 14:17:55 +00:00
|
|
|
|
2011-08-03 20:55:08 +00:00
|
|
|
/* Update cargo aging period. */
|
|
|
|
u->vcache.cached_cargo_age_period = GetVehicleProperty(u, PROP_ROADVEH_CARGO_AGE_PERIOD, EngInfo(u->engine_type)->cargo_age_period);
|
2007-06-11 14:00:16 +00:00
|
|
|
}
|
2010-11-06 13:05:11 +00:00
|
|
|
|
2010-11-06 13:11:24 +00:00
|
|
|
uint max_speed = GetVehicleProperty(v, PROP_ROADVEH_SPEED, 0);
|
|
|
|
v->vcache.cached_max_speed = (max_speed != 0) ? max_speed * 4 : RoadVehInfo(v->engine_type)->max_speed;
|
2007-06-11 14:00:16 +00:00
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Build a road vehicle.
|
2010-08-17 23:15:55 +00:00
|
|
|
* @param tile tile of the depot where road vehicle is built.
|
|
|
|
* @param flags type of operation.
|
|
|
|
* @param e the engine to build.
|
|
|
|
* @param data unused.
|
|
|
|
* @param ret[out] the vehicle that has been built.
|
|
|
|
* @return the cost of this operation or an error.
|
2005-05-12 00:11:37 +00:00
|
|
|
*/
|
2010-08-17 23:15:55 +00:00
|
|
|
CommandCost CmdBuildRoadVehicle(TileIndex tile, DoCommandFlag flags, const Engine *e, uint16 data, Vehicle **ret)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-10-06 19:17:07 +00:00
|
|
|
if (HasTileRoadType(tile, ROADTYPE_TRAM) != HasBit(e->info.misc_flags, EF_ROAD_TRAM)) return_cmd_error(STR_ERROR_DEPOT_WRONG_DEPOT_TYPE);
|
2007-05-26 20:30:55 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (flags & DC_EXEC) {
|
2009-10-06 19:17:07 +00:00
|
|
|
const RoadVehicleInfo *rvi = &e->u.road;
|
2004-12-03 21:57:05 +00:00
|
|
|
|
2009-05-22 20:18:45 +00:00
|
|
|
RoadVehicle *v = new RoadVehicle();
|
2010-08-17 23:15:55 +00:00
|
|
|
*ret = v;
|
2007-06-11 14:00:16 +00:00
|
|
|
v->direction = DiagDirToDir(GetRoadDepotDirection(tile));
|
2008-09-30 20:39:50 +00:00
|
|
|
v->owner = _current_company;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
v->tile = tile;
|
2009-05-13 21:38:23 +00:00
|
|
|
int x = TileX(tile) * TILE_SIZE + TILE_SIZE / 2;
|
|
|
|
int y = TileY(tile) * TILE_SIZE + TILE_SIZE / 2;
|
2004-08-09 17:04:08 +00:00
|
|
|
v->x_pos = x;
|
|
|
|
v->y_pos = y;
|
2011-11-04 10:18:13 +00:00
|
|
|
v->z_pos = GetSlopePixelZ(x, y);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-05-22 20:22:20 +00:00
|
|
|
v->state = RVSB_IN_DEPOT;
|
2007-04-18 22:10:36 +00:00
|
|
|
v->vehstatus = VS_HIDDEN | VS_STOPPED | VS_DEFPAL;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-12-03 21:57:05 +00:00
|
|
|
v->spritenum = rvi->image_index;
|
2009-02-21 12:52:41 +00:00
|
|
|
v->cargo_type = e->GetDefaultCargoType();
|
2004-12-03 21:57:05 +00:00
|
|
|
v->cargo_cap = rvi->capacity;
|
2013-05-19 14:22:04 +00:00
|
|
|
v->refit_cap = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-02-02 16:16:43 +00:00
|
|
|
v->last_station_visited = INVALID_STATION;
|
2013-05-19 14:22:04 +00:00
|
|
|
v->last_loading_station = INVALID_STATION;
|
2010-08-17 23:15:55 +00:00
|
|
|
v->engine_type = e->index;
|
2010-12-14 21:33:53 +00:00
|
|
|
v->gcache.first_engine = INVALID_ENGINE; // needs to be set before first callback
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
v->reliability = e->reliability;
|
|
|
|
v->reliability_spd_dec = e->reliability_spd_dec;
|
2016-01-31 22:55:25 +00:00
|
|
|
v->breakdown_chance_factor = 128;
|
2009-06-16 13:52:18 +00:00
|
|
|
v->max_age = e->GetLifeLengthInDays();
|
2005-10-29 21:54:28 +00:00
|
|
|
_new_vehicle_id = v->index;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2013-02-14 17:04:01 +00:00
|
|
|
v->SetServiceInterval(Company::Get(v->owner)->settings.vehicle.servint_roadveh);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
v->date_of_last_service = _date;
|
2006-08-20 19:05:28 +00:00
|
|
|
v->build_year = _cur_year;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2016-10-16 14:57:56 +00:00
|
|
|
v->sprite_seq.Set(SPR_IMG_QUERY);
|
2005-12-28 22:29:59 +00:00
|
|
|
v->random_bits = VehicleRandomBits();
|
2011-01-19 18:44:13 +00:00
|
|
|
v->SetFrontEngine();
|
2007-06-11 14:00:16 +00:00
|
|
|
|
2009-10-06 19:17:07 +00:00
|
|
|
v->roadtype = HasBit(e->info.misc_flags, EF_ROAD_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD;
|
2009-05-22 20:22:20 +00:00
|
|
|
v->compatible_roadtypes = RoadTypeToRoadTypes(v->roadtype);
|
2011-03-05 21:52:45 +00:00
|
|
|
v->gcache.cached_veh_length = VEHICLE_LENGTH;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-11-20 13:35:54 +00:00
|
|
|
if (e->flags & ENGINE_EXCLUSIVE_PREVIEW) SetBit(v->vehicle_flags, VF_BUILT_AS_PROTOTYPE);
|
2013-02-14 17:24:55 +00:00
|
|
|
v->SetServiceIntervalIsPercent(Company::Get(_current_company)->settings.vehicle.servint_ispercent);
|
2016-05-06 22:33:12 +00:00
|
|
|
SB(v->vehicle_flags, VF_AUTOMATE_TIMETABLE, 1, Company::Get(_current_company)->settings.vehicle.auto_timetable_by_default);
|
2016-05-07 00:15:46 +00:00
|
|
|
SB(v->vehicle_flags, VF_TIMETABLE_SEPARATION, 1, Company::Get(_current_company)->settings.vehicle.auto_separation_by_default);
|
2007-02-28 17:59:05 +00:00
|
|
|
|
2009-10-06 19:30:31 +00:00
|
|
|
AddArticulatedParts(v);
|
2009-05-31 12:03:14 +00:00
|
|
|
v->InvalidateNewGRFCacheOfChain();
|
2007-06-11 14:00:16 +00:00
|
|
|
|
2009-03-09 18:56:50 +00:00
|
|
|
/* Call various callbacks after the whole consist has been constructed */
|
2009-05-22 20:18:45 +00:00
|
|
|
for (RoadVehicle *u = v; u != NULL; u = u->Next()) {
|
2011-11-09 16:38:50 +00:00
|
|
|
u->cargo_cap = u->GetEngine()->DetermineCapacity(u);
|
2013-05-19 14:22:04 +00:00
|
|
|
u->refit_cap = 0;
|
2009-05-31 12:03:14 +00:00
|
|
|
v->InvalidateNewGRFCache();
|
|
|
|
u->InvalidateNewGRFCache();
|
2009-01-07 17:50:20 +00:00
|
|
|
}
|
2009-10-20 20:01:56 +00:00
|
|
|
RoadVehUpdateCache(v);
|
2010-03-06 12:56:57 +00:00
|
|
|
/* Initialize cached values for realistic acceleration. */
|
|
|
|
if (_settings_game.vehicle.roadveh_acceleration_model != AM_ORIGINAL) v->CargoChanged();
|
2009-01-07 17:50:20 +00:00
|
|
|
|
2014-09-20 15:31:26 +00:00
|
|
|
v->UpdatePosition();
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-03-13 21:28:40 +00:00
|
|
|
CheckConsistencyOfArticulatedVehicle(v);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2010-08-17 23:15:55 +00:00
|
|
|
return CommandCost();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-12-02 10:44:38 +00:00
|
|
|
static FindDepotData FindClosestRoadDepot(const RoadVehicle *v, int max_distance)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-12-02 10:44:38 +00:00
|
|
|
if (IsRoadDepotTile(v->tile)) return FindDepotData(v->tile, 0);
|
2009-08-31 19:16:18 +00:00
|
|
|
|
2008-05-29 15:13:28 +00:00
|
|
|
switch (_settings_game.pf.pathfinder_for_roadvehs) {
|
2009-12-20 16:19:47 +00:00
|
|
|
case VPF_NPF: return NPFRoadVehicleFindNearestDepot(v, max_distance);
|
|
|
|
case VPF_YAPF: return YapfRoadVehicleFindNearestDepot(v, max_distance);
|
2006-03-06 20:28:28 +00:00
|
|
|
|
2009-12-02 10:44:38 +00:00
|
|
|
default: NOT_REACHED();
|
2005-01-31 11:23:10 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-04-11 08:14:43 +00:00
|
|
|
bool RoadVehicle::FindClosestDepot(TileIndex *location, DestinationID *destination, bool *reverse)
|
|
|
|
{
|
2009-12-02 10:44:38 +00:00
|
|
|
FindDepotData rfdd = FindClosestRoadDepot(this, 0);
|
2009-08-31 19:16:18 +00:00
|
|
|
if (rfdd.best_length == UINT_MAX) return false;
|
2008-04-11 08:14:43 +00:00
|
|
|
|
2009-08-31 19:16:18 +00:00
|
|
|
if (location != NULL) *location = rfdd.tile;
|
2009-09-10 14:37:55 +00:00
|
|
|
if (destination != NULL) *destination = GetDepotIndex(rfdd.tile);
|
2008-04-11 08:14:43 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Turn a roadvehicle around.
|
2006-04-10 07:15:58 +00:00
|
|
|
* @param tile unused
|
2007-04-17 21:09:38 +00:00
|
|
|
* @param flags operation to perform
|
2005-05-12 00:11:37 +00:00
|
|
|
* @param p1 vehicle ID to turn
|
|
|
|
* @param p2 unused
|
2009-09-18 14:23:58 +00:00
|
|
|
* @param text unused
|
|
|
|
* @return the cost of this operation or an error
|
2005-05-12 00:11:37 +00:00
|
|
|
*/
|
2009-02-09 21:20:05 +00:00
|
|
|
CommandCost CmdTurnRoadVeh(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-05-26 22:10:13 +00:00
|
|
|
RoadVehicle *v = RoadVehicle::GetIfValid(p1);
|
2010-03-13 17:11:28 +00:00
|
|
|
if (v == NULL) return CMD_ERROR;
|
|
|
|
|
2012-07-07 15:35:28 +00:00
|
|
|
if (!v->IsPrimaryVehicle()) return CMD_ERROR;
|
|
|
|
|
2015-08-06 21:24:24 +00:00
|
|
|
CommandCost ret = CheckVehicleControlAllowed(v);
|
2010-03-13 17:11:28 +00:00
|
|
|
if (ret.Failed()) return ret;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-06-01 11:43:36 +00:00
|
|
|
if ((v->vehstatus & VS_STOPPED) ||
|
|
|
|
(v->vehstatus & VS_CRASHED) ||
|
2009-05-22 20:22:20 +00:00
|
|
|
v->overtaking != 0 ||
|
|
|
|
v->state == RVSB_WORMHOLE ||
|
2007-08-29 21:49:08 +00:00
|
|
|
v->IsInDepot() ||
|
2010-06-07 20:38:16 +00:00
|
|
|
v->current_order.IsType(OT_LOADING)) {
|
2004-08-09 17:04:08 +00:00
|
|
|
return CMD_ERROR;
|
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2008-02-14 15:59:16 +00:00
|
|
|
if (IsNormalRoadTile(v->tile) && GetDisallowedRoadDirections(v->tile) != DRD_NONE) return CMD_ERROR;
|
2007-05-31 15:15:00 +00:00
|
|
|
|
2007-12-16 19:30:42 +00:00
|
|
|
if (IsTileType(v->tile, MP_TUNNELBRIDGE) && DirToDiagDir(v->direction) == GetTunnelBridgeDirection(v->tile)) return CMD_ERROR;
|
2006-12-27 12:38:02 +00:00
|
|
|
|
2009-05-22 20:22:20 +00:00
|
|
|
if (flags & DC_EXEC) v->reverse_ctr = 180;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-06-18 19:53:50 +00:00
|
|
|
return CommandCost();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-29 22:33:51 +00:00
|
|
|
void RoadVehicle::MarkDirty()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2010-05-03 23:36:17 +00:00
|
|
|
for (RoadVehicle *v = this; v != NULL; v = v->Next()) {
|
2013-08-04 14:02:27 +00:00
|
|
|
v->colourmap = PAL_NONE;
|
2016-07-08 18:07:25 +00:00
|
|
|
v->cur_image_valid_dir = INVALID_DIR;
|
2013-08-04 14:02:27 +00:00
|
|
|
v->UpdateViewport(true, false);
|
2007-08-06 13:44:50 +00:00
|
|
|
}
|
2010-03-06 12:56:57 +00:00
|
|
|
this->CargoChanged();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2018-05-22 17:43:34 +00:00
|
|
|
void RoadVehicle::UpdateDeltaXY()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2012-06-07 15:41:14 +00:00
|
|
|
static const int8 _delta_xy_table[8][10] = {
|
|
|
|
/* 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 */
|
|
|
|
{3, 3, -1, -1, 0, 0, -1, -1, -1, -1}, // N
|
|
|
|
{3, 7, -1, -3, 0, -1, 0, -1, 0, 0}, // NE
|
|
|
|
{3, 3, -1, -1, 0, 0, 1, -1, 1, -1}, // E
|
|
|
|
{7, 3, -3, -1, -1, 0, 0, 0, 1, 0}, // SE
|
|
|
|
{3, 3, -1, -1, 0, 0, 1, 1, 1, 1}, // S
|
|
|
|
{3, 7, -1, -3, 0, -1, 0, 0, 0, 1}, // SW
|
|
|
|
{3, 3, -1, -1, 0, 0, -1, 1, -1, 1}, // W
|
|
|
|
{7, 3, -3, -1, -1, 0, -1, 0, 0, 0}, // NW
|
2004-08-09 17:04:08 +00:00
|
|
|
};
|
2007-05-01 16:35:14 +00:00
|
|
|
|
2012-06-07 15:41:14 +00:00
|
|
|
int shorten = VEHICLE_LENGTH - this->gcache.cached_veh_length;
|
2018-05-22 17:43:34 +00:00
|
|
|
if (!IsDiagonalDirection(this->direction)) shorten >>= 1;
|
2012-06-07 15:41:14 +00:00
|
|
|
|
2018-05-22 17:43:34 +00:00
|
|
|
const int8 *bb = _delta_xy_table[this->direction];
|
2012-06-07 15:41:14 +00:00
|
|
|
this->x_bb_offs = bb[5] + bb[9] * shorten;
|
|
|
|
this->y_bb_offs = bb[4] + bb[8] * shorten;;
|
2012-06-06 14:03:22 +00:00
|
|
|
this->x_offs = bb[3];
|
|
|
|
this->y_offs = bb[2];
|
2012-06-07 15:41:14 +00:00
|
|
|
this->x_extent = bb[1] + bb[7] * shorten;
|
|
|
|
this->y_extent = bb[0] + bb[6] * shorten;
|
2008-04-01 14:03:20 +00:00
|
|
|
this->z_extent = 6;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2018-01-04 19:18:24 +00:00
|
|
|
/**
|
|
|
|
* Calculates the maximum speed of the vehicle, taking into account speed reductions following critical breakdowns
|
|
|
|
* @return Maximum speed of the vehicle.
|
|
|
|
*/
|
|
|
|
int RoadVehicle::GetEffectiveMaxSpeed() const
|
|
|
|
{
|
|
|
|
int max_speed = this->vcache.cached_max_speed;
|
|
|
|
|
|
|
|
for (uint i = 0; i < this->critical_breakdown_count; i++) {
|
|
|
|
max_speed = min(max_speed - (max_speed / 3) + 1, max_speed);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* clamp speed to be no less than lower of 5mph and 1/8 of base speed */
|
|
|
|
return max<uint16>(max_speed, min<uint16>(10, (this->vcache.cached_max_speed + 7) >> 3));
|
|
|
|
}
|
|
|
|
|
2010-03-06 12:56:57 +00:00
|
|
|
/**
|
|
|
|
* Calculates the maximum speed of the vehicle under its current conditions.
|
|
|
|
* @return Maximum speed of the vehicle.
|
|
|
|
*/
|
2011-12-20 17:57:56 +00:00
|
|
|
inline int RoadVehicle::GetCurrentMaxSpeed() const
|
2010-03-06 12:56:57 +00:00
|
|
|
{
|
2018-01-04 19:18:24 +00:00
|
|
|
int max_speed = this->GetEffectiveMaxSpeed();
|
2010-03-06 12:56:57 +00:00
|
|
|
|
|
|
|
/* Limit speed to 50% while reversing, 75% in curves. */
|
|
|
|
for (const RoadVehicle *u = this; u != NULL; u = u->Next()) {
|
2013-04-08 19:55:24 +00:00
|
|
|
if (_settings_game.vehicle.roadveh_acceleration_model == AM_REALISTIC) {
|
|
|
|
if (this->state <= RVSB_TRACKDIR_MASK && IsReversingRoadTrackdir((Trackdir)this->state)) {
|
|
|
|
max_speed = this->vcache.cached_max_speed / 2;
|
|
|
|
break;
|
|
|
|
} else if ((u->direction & 1) == 0) {
|
|
|
|
max_speed = this->vcache.cached_max_speed * 3 / 4;
|
|
|
|
}
|
2010-03-06 12:56:57 +00:00
|
|
|
}
|
2012-12-20 19:43:58 +00:00
|
|
|
|
|
|
|
/* Vehicle is on the middle part of a bridge. */
|
|
|
|
if (u->state == RVSB_WORMHOLE && !(u->vehstatus & VS_HIDDEN)) {
|
|
|
|
max_speed = min(max_speed, GetBridgeSpec(GetBridgeType(u->tile))->speed * 2);
|
|
|
|
}
|
2010-03-06 12:56:57 +00:00
|
|
|
}
|
|
|
|
|
2014-05-01 14:48:44 +00:00
|
|
|
return min(max_speed, this->current_order.GetMaxSpeed() * 2);
|
2010-03-06 12:56:57 +00:00
|
|
|
}
|
|
|
|
|
2011-02-05 16:20:55 +00:00
|
|
|
/**
|
|
|
|
* Delete last vehicle of a chain road vehicles.
|
|
|
|
* @param v First roadvehicle.
|
|
|
|
*/
|
2009-05-22 20:18:45 +00:00
|
|
|
static void DeleteLastRoadVeh(RoadVehicle *v)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2013-06-04 15:04:59 +00:00
|
|
|
RoadVehicle *first = v->First();
|
2007-06-11 14:00:16 +00:00
|
|
|
Vehicle *u = v;
|
2007-08-30 13:03:56 +00:00
|
|
|
for (; v->Next() != NULL; v = v->Next()) u = v;
|
|
|
|
u->SetNext(NULL);
|
2013-06-04 15:04:59 +00:00
|
|
|
v->last_station_visited = first->last_station_visited; // for PreDestructor
|
2007-06-11 14:00:16 +00:00
|
|
|
|
2009-12-04 20:52:19 +00:00
|
|
|
/* Only leave the road stop when we're really gone. */
|
|
|
|
if (IsInsideMM(v->state, RVSB_IN_ROAD_STOP, RVSB_IN_ROAD_STOP_END)) RoadStop::GetByTile(v->tile, GetRoadStopType(v->tile))->Leave(v);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-08-03 19:36:00 +00:00
|
|
|
delete v;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-05-22 20:18:45 +00:00
|
|
|
static void RoadVehSetRandomDirection(RoadVehicle *v)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-03-08 08:16:31 +00:00
|
|
|
static const DirDiff delta[] = {
|
|
|
|
DIRDIFF_45LEFT, DIRDIFF_SAME, DIRDIFF_SAME, DIRDIFF_45RIGHT
|
|
|
|
};
|
|
|
|
|
2007-06-11 14:00:16 +00:00
|
|
|
do {
|
|
|
|
uint32 r = Random();
|
2006-03-08 08:16:31 +00:00
|
|
|
|
2007-06-11 14:00:16 +00:00
|
|
|
v->direction = ChangeDir(v->direction, delta[r & 3]);
|
2011-02-02 22:40:04 +00:00
|
|
|
v->UpdateViewport(true, true);
|
2007-08-30 13:03:56 +00:00
|
|
|
} while ((v = v->Next()) != NULL);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2011-02-05 16:20:55 +00:00
|
|
|
/**
|
|
|
|
* Road vehicle chain has crashed.
|
|
|
|
* @param v First roadvehicle.
|
|
|
|
* @return whether the chain still exists.
|
|
|
|
*/
|
2009-05-22 20:18:45 +00:00
|
|
|
static bool RoadVehIsCrashed(RoadVehicle *v)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-05-22 20:22:20 +00:00
|
|
|
v->crashed_ctr++;
|
|
|
|
if (v->crashed_ctr == 2) {
|
2005-02-12 15:53:32 +00:00
|
|
|
CreateEffectVehicleRel(v, 4, 4, 8, EV_EXPLOSION_LARGE);
|
2009-05-22 20:22:20 +00:00
|
|
|
} else if (v->crashed_ctr <= 45) {
|
2006-02-13 21:15:00 +00:00
|
|
|
if ((v->tick_counter & 7) == 0) RoadVehSetRandomDirection(v);
|
2009-05-22 20:22:20 +00:00
|
|
|
} else if (v->crashed_ctr >= 2220 && !(v->tick_counter & 0x1F)) {
|
2009-05-22 13:53:14 +00:00
|
|
|
bool ret = v->Next() != NULL;
|
2007-06-11 14:00:16 +00:00
|
|
|
DeleteLastRoadVeh(v);
|
2009-05-22 13:53:14 +00:00
|
|
|
return ret;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2009-05-22 13:53:14 +00:00
|
|
|
|
|
|
|
return true;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2017-06-06 19:20:33 +00:00
|
|
|
struct CheckRoadVehCrashTrainInfo {
|
|
|
|
const Vehicle *u;
|
|
|
|
bool found = false;
|
|
|
|
|
|
|
|
CheckRoadVehCrashTrainInfo(const Vehicle *u_)
|
|
|
|
: u(u_) { }
|
|
|
|
};
|
|
|
|
|
2011-02-05 16:20:55 +00:00
|
|
|
/**
|
|
|
|
* Check routine whether a road and a train vehicle have collided.
|
|
|
|
* @param v %Train vehicle to test.
|
2017-06-06 19:20:33 +00:00
|
|
|
* @param data Info including road vehicle to test.
|
2011-02-05 16:20:55 +00:00
|
|
|
* @return %Train vehicle if the vehicles collided, else \c NULL.
|
|
|
|
*/
|
2008-08-01 15:07:31 +00:00
|
|
|
static Vehicle *EnumCheckRoadVehCrashTrain(Vehicle *v, void *data)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2017-06-06 19:20:33 +00:00
|
|
|
CheckRoadVehCrashTrainInfo *info = (CheckRoadVehCrashTrainInfo*) data;
|
|
|
|
|
|
|
|
if (v->type == VEH_TRAIN &&
|
|
|
|
abs(v->z_pos - info->u->z_pos) <= 6 &&
|
|
|
|
abs(v->x_pos - info->u->x_pos) <= 4 &&
|
|
|
|
abs(v->y_pos - info->u->y_pos) <= 4) {
|
|
|
|
info->found = true;
|
|
|
|
extern void TrainRoadVehicleCrashBreakdown(Vehicle *v);
|
|
|
|
TrainRoadVehicleCrashBreakdown(v);
|
|
|
|
return v;
|
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-12-04 20:29:46 +00:00
|
|
|
uint RoadVehicle::Crash(bool flooded)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2011-01-21 16:20:55 +00:00
|
|
|
uint pass = this->GroundVehicleBase::Crash(flooded);
|
2011-01-19 18:44:13 +00:00
|
|
|
if (this->IsFrontEngine()) {
|
2009-12-04 20:52:19 +00:00
|
|
|
pass += 1; // driver
|
|
|
|
|
|
|
|
/* If we're in a drive through road stop we ought to leave it */
|
|
|
|
if (IsInsideMM(this->state, RVSB_IN_DT_ROAD_STOP, RVSB_IN_DT_ROAD_STOP_END)) {
|
|
|
|
RoadStop::GetByTile(this->tile, GetRoadStopType(this->tile))->Leave(this);
|
|
|
|
}
|
|
|
|
}
|
2009-12-04 20:29:46 +00:00
|
|
|
this->crashed_ctr = flooded ? 2000 : 1; // max 2220, disappear pretty fast when flooded
|
|
|
|
return pass;
|
|
|
|
}
|
2007-06-11 14:00:16 +00:00
|
|
|
|
2009-12-04 20:29:46 +00:00
|
|
|
static void RoadVehCrash(RoadVehicle *v)
|
|
|
|
{
|
|
|
|
uint pass = v->Crash();
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
AI::NewEvent(v->owner, new ScriptEventVehicleCrashed(v->index, v->tile, ScriptEventVehicleCrashed::CRASH_RV_LEVEL_CROSSING));
|
2011-12-19 20:59:36 +00:00
|
|
|
Game::NewEvent(new ScriptEventVehicleCrashed(v->index, v->tile, ScriptEventVehicleCrashed::CRASH_RV_LEVEL_CROSSING));
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2006-02-01 06:32:03 +00:00
|
|
|
SetDParam(0, pass);
|
2009-05-24 16:52:42 +00:00
|
|
|
AddVehicleNewsItem(
|
2005-08-01 13:01:14 +00:00
|
|
|
(pass == 1) ?
|
2009-07-22 22:44:56 +00:00
|
|
|
STR_NEWS_ROAD_VEHICLE_CRASH_DRIVER : STR_NEWS_ROAD_VEHICLE_CRASH,
|
2012-05-26 14:16:03 +00:00
|
|
|
NT_ACCIDENT,
|
2009-05-24 16:52:42 +00:00
|
|
|
v->index
|
2006-02-18 14:41:24 +00:00
|
|
|
);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
ModifyStationRatingAround(v->tile, v->owner, -160, 22);
|
2012-12-23 21:09:09 +00:00
|
|
|
if (_settings_client.sound.disaster) SndPlayVehicleFx(SND_12_EXPLOSION, v);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-05-22 20:18:45 +00:00
|
|
|
static bool RoadVehCheckTrainCrash(RoadVehicle *v)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-05-22 20:18:45 +00:00
|
|
|
for (RoadVehicle *u = v; u != NULL; u = u->Next()) {
|
2009-05-22 20:22:20 +00:00
|
|
|
if (u->state == RVSB_WORMHOLE) continue;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-06-11 14:00:16 +00:00
|
|
|
TileIndex tile = u->tile;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-06-11 14:00:16 +00:00
|
|
|
if (!IsLevelCrossingTile(tile)) continue;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2017-06-06 19:20:33 +00:00
|
|
|
CheckRoadVehCrashTrainInfo info(u);
|
|
|
|
FindVehicleOnPosXY(v->x_pos, v->y_pos, &info, EnumCheckRoadVehCrashTrain);
|
|
|
|
if (info.found) {
|
2007-06-11 14:00:16 +00:00
|
|
|
RoadVehCrash(v);
|
2009-01-06 14:45:38 +00:00
|
|
|
return true;
|
2007-06-11 14:00:16 +00:00
|
|
|
}
|
|
|
|
}
|
2009-01-06 14:45:38 +00:00
|
|
|
|
|
|
|
return false;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-04-05 10:55:50 +00:00
|
|
|
TileIndex RoadVehicle::GetOrderStationLocation(StationID station)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-04-05 12:01:34 +00:00
|
|
|
if (station == this->last_station_visited) this->last_station_visited = INVALID_STATION;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-12-02 18:18:56 +00:00
|
|
|
const Station *st = Station::Get(station);
|
|
|
|
if (!CanVehicleUseStation(this, st)) {
|
2008-04-05 10:55:50 +00:00
|
|
|
/* There is no stop left at the station, so don't even TRY to go there */
|
2011-01-31 20:44:15 +00:00
|
|
|
this->IncrementRealOrderIndex();
|
2008-04-05 10:55:50 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2009-12-02 18:18:56 +00:00
|
|
|
|
|
|
|
return st->xy;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-05-22 20:18:45 +00:00
|
|
|
static void StartRoadVehSound(const RoadVehicle *v)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-09-27 18:17:01 +00:00
|
|
|
if (!PlayVehicleSound(v, VSE_START)) {
|
2009-05-17 17:17:48 +00:00
|
|
|
SoundID s = RoadVehInfo(v->engine_type)->sfx;
|
2010-07-24 10:14:39 +00:00
|
|
|
if (s == SND_19_BUS_START_PULL_AWAY && (v->tick_counter & 3) == 0) {
|
2006-09-27 18:17:01 +00:00
|
|
|
s = SND_1A_BUS_START_PULL_AWAY_WITH_HORN;
|
2010-07-24 10:14:39 +00:00
|
|
|
}
|
2006-09-27 18:17:01 +00:00
|
|
|
SndPlayVehicleFx(s, v);
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-03-07 12:11:48 +00:00
|
|
|
struct RoadVehFindData {
|
2006-02-13 21:15:00 +00:00
|
|
|
int x;
|
|
|
|
int y;
|
2008-09-07 11:23:10 +00:00
|
|
|
const Vehicle *veh;
|
|
|
|
Vehicle *best;
|
|
|
|
uint best_diff;
|
2006-03-08 06:55:33 +00:00
|
|
|
Direction dir;
|
2007-03-07 12:11:48 +00:00
|
|
|
};
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-08-01 15:07:31 +00:00
|
|
|
static Vehicle *EnumCheckRoadVehClose(Vehicle *v, void *data)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-03-03 22:21:29 +00:00
|
|
|
static const int8 dist_x[] = { -4, -8, -4, -1, 4, 8, 4, 1 };
|
|
|
|
static const int8 dist_y[] = { -4, -1, 4, 8, 4, 1, -4, -8 };
|
|
|
|
|
2008-09-07 11:23:10 +00:00
|
|
|
RoadVehFindData *rvf = (RoadVehFindData*)data;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-16 13:31:18 +00:00
|
|
|
short x_diff = v->x_pos - rvf->x;
|
|
|
|
short y_diff = v->y_pos - rvf->y;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-09-07 11:23:10 +00:00
|
|
|
if (v->type == VEH_ROAD &&
|
|
|
|
!v->IsInDepot() &&
|
|
|
|
abs(v->z_pos - rvf->veh->z_pos) < 6 &&
|
|
|
|
v->direction == rvf->dir &&
|
|
|
|
rvf->veh->First() != v->First() &&
|
|
|
|
(dist_x[v->direction] >= 0 || (x_diff > dist_x[v->direction] && x_diff <= 0)) &&
|
|
|
|
(dist_x[v->direction] <= 0 || (x_diff < dist_x[v->direction] && x_diff >= 0)) &&
|
|
|
|
(dist_y[v->direction] >= 0 || (y_diff > dist_y[v->direction] && y_diff <= 0)) &&
|
|
|
|
(dist_y[v->direction] <= 0 || (y_diff < dist_y[v->direction] && y_diff >= 0))) {
|
|
|
|
uint diff = abs(x_diff) + abs(y_diff);
|
|
|
|
|
|
|
|
if (diff < rvf->best_diff || (diff == rvf->best_diff && v->index < rvf->best->index)) {
|
|
|
|
rvf->best = v;
|
|
|
|
rvf->best_diff = diff;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-12-08 00:15:15 +00:00
|
|
|
static RoadVehicle *RoadVehFindCloseTo(RoadVehicle *v, int x, int y, Direction dir, bool update_blocked_ctr = true)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
RoadVehFindData rvf;
|
2009-05-22 20:18:45 +00:00
|
|
|
RoadVehicle *front = v->First();
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-05-22 20:22:20 +00:00
|
|
|
if (front->reverse_ctr != 0) return NULL;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
rvf.x = x;
|
|
|
|
rvf.y = y;
|
|
|
|
rvf.dir = dir;
|
|
|
|
rvf.veh = v;
|
2008-09-07 11:23:10 +00:00
|
|
|
rvf.best_diff = UINT_MAX;
|
|
|
|
|
2009-05-22 20:22:20 +00:00
|
|
|
if (front->state == RVSB_WORMHOLE) {
|
2008-09-07 11:23:10 +00:00
|
|
|
FindVehicleOnPos(v->tile, &rvf, EnumCheckRoadVehClose);
|
|
|
|
FindVehicleOnPos(GetOtherTunnelBridgeEnd(v->tile), &rvf, EnumCheckRoadVehClose);
|
2007-10-31 22:13:41 +00:00
|
|
|
} else {
|
2008-09-07 11:23:10 +00:00
|
|
|
FindVehicleOnPosXY(x, y, &rvf, EnumCheckRoadVehClose);
|
2007-10-31 22:13:41 +00:00
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-03-28 20:41:35 +00:00
|
|
|
/* This code protects a roadvehicle from being blocked for ever
|
|
|
|
* If more than 1480 / 74 days a road vehicle is blocked, it will
|
|
|
|
* drive just through it. The ultimate backup-code of TTD.
|
|
|
|
* It can be disabled. */
|
2008-09-07 11:23:10 +00:00
|
|
|
if (rvf.best_diff == UINT_MAX) {
|
2009-05-22 20:22:20 +00:00
|
|
|
front->blocked_ctr = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-12-08 00:15:15 +00:00
|
|
|
if (update_blocked_ctr && ++front->blocked_ctr > 1480) return NULL;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2017-05-08 20:54:15 +00:00
|
|
|
RoadVehicle *rv = RoadVehicle::From(rvf.best);
|
|
|
|
if (rv != NULL && front->IsRoadVehicleOnLevelCrossing() && (rv->First()->cur_speed == 0 || rv->First()->IsRoadVehicleStopped())) return NULL;
|
|
|
|
|
|
|
|
return rv;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2010-11-05 16:34:22 +00:00
|
|
|
/**
|
|
|
|
* A road vehicle arrives at a station. If it is the first time, create a news item.
|
|
|
|
* @param v Road vehicle that arrived.
|
|
|
|
* @param st Station where the road vehicle arrived.
|
|
|
|
*/
|
2009-05-22 20:18:45 +00:00
|
|
|
static void RoadVehArrivesAt(const RoadVehicle *v, Station *st)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-12-02 17:37:02 +00:00
|
|
|
if (v->IsBus()) {
|
2004-08-09 17:04:08 +00:00
|
|
|
/* Check if station was ever visited before */
|
|
|
|
if (!(st->had_vehicle_of_type & HVOT_BUS)) {
|
|
|
|
st->had_vehicle_of_type |= HVOT_BUS;
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(0, st->index);
|
2009-05-24 16:52:42 +00:00
|
|
|
AddVehicleNewsItem(
|
2009-07-22 22:44:56 +00:00
|
|
|
v->roadtype == ROADTYPE_ROAD ? STR_NEWS_FIRST_BUS_ARRIVAL : STR_NEWS_FIRST_PASSENGER_TRAM_ARRIVAL,
|
2012-05-26 14:16:03 +00:00
|
|
|
(v->owner == _local_company) ? NT_ARRIVAL_COMPANY : NT_ARRIVAL_OTHER,
|
2004-08-09 17:04:08 +00:00
|
|
|
v->index,
|
2008-09-13 10:19:51 +00:00
|
|
|
st->index
|
|
|
|
);
|
2011-11-29 23:15:35 +00:00
|
|
|
AI::NewEvent(v->owner, new ScriptEventStationFirstVehicle(st->index, v->index));
|
2011-12-19 20:59:36 +00:00
|
|
|
Game::NewEvent(new ScriptEventStationFirstVehicle(st->index, v->index));
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Check if station was ever visited before */
|
|
|
|
if (!(st->had_vehicle_of_type & HVOT_TRUCK)) {
|
|
|
|
st->had_vehicle_of_type |= HVOT_TRUCK;
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(0, st->index);
|
2009-05-24 16:52:42 +00:00
|
|
|
AddVehicleNewsItem(
|
2009-07-22 22:44:56 +00:00
|
|
|
v->roadtype == ROADTYPE_ROAD ? STR_NEWS_FIRST_TRUCK_ARRIVAL : STR_NEWS_FIRST_CARGO_TRAM_ARRIVAL,
|
2012-05-26 14:16:03 +00:00
|
|
|
(v->owner == _local_company) ? NT_ARRIVAL_COMPANY : NT_ARRIVAL_OTHER,
|
2004-08-09 17:04:08 +00:00
|
|
|
v->index,
|
2008-09-13 10:19:51 +00:00
|
|
|
st->index
|
2006-02-18 14:41:24 +00:00
|
|
|
);
|
2011-11-29 23:15:35 +00:00
|
|
|
AI::NewEvent(v->owner, new ScriptEventStationFirstVehicle(st->index, v->index));
|
2011-12-19 20:59:36 +00:00
|
|
|
Game::NewEvent(new ScriptEventStationFirstVehicle(st->index, v->index));
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-04 13:07:47 +00:00
|
|
|
/**
|
|
|
|
* This function looks at the vehicle and updates its speed (cur_speed
|
|
|
|
* and subspeed) variables. Furthermore, it returns the distance that
|
|
|
|
* the vehicle can drive this tick. #Vehicle::GetAdvanceDistance() determines
|
|
|
|
* the distance to drive before moving a step on the map.
|
|
|
|
* @return distance to drive.
|
|
|
|
*/
|
2011-01-26 17:35:03 +00:00
|
|
|
int RoadVehicle::UpdateSpeed()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2011-01-26 17:35:03 +00:00
|
|
|
switch (_settings_game.vehicle.roadveh_acceleration_model) {
|
|
|
|
default: NOT_REACHED();
|
|
|
|
case AM_ORIGINAL:
|
|
|
|
return this->DoUpdateSpeed(this->overtaking != 0 ? 512 : 256, 0, this->GetCurrentMaxSpeed());
|
2009-01-06 14:45:38 +00:00
|
|
|
|
2011-01-26 17:35:03 +00:00
|
|
|
case AM_REALISTIC:
|
|
|
|
return this->DoUpdateSpeed(this->GetAcceleration() + (this->overtaking != 0 ? 256 : 0), this->GetAccelerationStatus() == AS_BRAKE ? 0 : 4, this->GetCurrentMaxSpeed());
|
2009-01-06 14:45:38 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-05-22 20:18:45 +00:00
|
|
|
static Direction RoadVehGetNewDirection(const RoadVehicle *v, int x, int y)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-03-08 06:55:33 +00:00
|
|
|
static const Direction _roadveh_new_dir[] = {
|
2007-01-10 18:56:51 +00:00
|
|
|
DIR_N , DIR_NW, DIR_W , INVALID_DIR,
|
|
|
|
DIR_NE, DIR_N , DIR_SW, INVALID_DIR,
|
2006-03-08 06:55:33 +00:00
|
|
|
DIR_E , DIR_SE, DIR_S
|
2004-08-09 17:04:08 +00:00
|
|
|
};
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
x = x - v->x_pos + 1;
|
|
|
|
y = y - v->y_pos + 1;
|
|
|
|
|
2006-02-13 21:15:00 +00:00
|
|
|
if ((uint)x > 2 || (uint)y > 2) return v->direction;
|
|
|
|
return _roadveh_new_dir[y * 4 + x];
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-05-22 20:18:45 +00:00
|
|
|
static Direction RoadVehGetSlidingDirection(const RoadVehicle *v, int x, int y)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-01-10 18:56:51 +00:00
|
|
|
Direction new_dir = RoadVehGetNewDirection(v, x, y);
|
|
|
|
Direction old_dir = v->direction;
|
2006-03-08 07:50:26 +00:00
|
|
|
DirDiff delta;
|
2006-03-08 06:55:33 +00:00
|
|
|
|
2007-01-10 18:56:51 +00:00
|
|
|
if (new_dir == old_dir) return old_dir;
|
|
|
|
delta = (DirDifference(new_dir, old_dir) > DIRDIFF_REVERSE ? DIRDIFF_45LEFT : DIRDIFF_45RIGHT);
|
|
|
|
return ChangeDir(old_dir, delta);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-03-07 12:11:48 +00:00
|
|
|
struct OvertakeData {
|
2009-05-22 20:18:45 +00:00
|
|
|
const RoadVehicle *u;
|
|
|
|
const RoadVehicle *v;
|
2005-06-24 12:38:35 +00:00
|
|
|
TileIndex tile;
|
2008-02-20 14:07:45 +00:00
|
|
|
Trackdir trackdir;
|
2007-03-07 12:11:48 +00:00
|
|
|
};
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-01-10 00:31:47 +00:00
|
|
|
static Vehicle *EnumFindVehBlockingOvertake(Vehicle *v, void *data)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-01-10 00:31:47 +00:00
|
|
|
const OvertakeData *od = (OvertakeData*)data;
|
2006-03-03 22:21:29 +00:00
|
|
|
|
2010-11-20 15:44:24 +00:00
|
|
|
return (v->type == VEH_ROAD && v->First() == v && v != od->u && v != od->v) ? v : NULL;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-02-20 14:07:45 +00:00
|
|
|
/**
|
|
|
|
* Check if overtaking is possible on a piece of track
|
|
|
|
*
|
|
|
|
* @param od Information about the tile and the involved vehicles
|
|
|
|
* @return true if we have to abort overtaking
|
|
|
|
*/
|
|
|
|
static bool CheckRoadBlockedForOvertaking(OvertakeData *od)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-05-22 20:22:20 +00:00
|
|
|
TrackStatus ts = GetTileTrackStatus(od->tile, TRANSPORT_ROAD, od->v->compatible_roadtypes);
|
2008-02-20 17:49:50 +00:00
|
|
|
TrackdirBits trackdirbits = TrackStatusToTrackdirBits(ts);
|
|
|
|
TrackdirBits red_signals = TrackStatusToRedSignals(ts); // barred level crossing
|
2008-02-20 14:07:45 +00:00
|
|
|
TrackBits trackbits = TrackdirBitsToTrackBits(trackdirbits);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-02-20 14:07:45 +00:00
|
|
|
/* Track does not continue along overtaking direction || track has junction || levelcrossing is barred */
|
|
|
|
if (!HasBit(trackdirbits, od->trackdir) || (trackbits & ~TRACK_BIT_CROSS) || (red_signals != TRACKDIR_BIT_NONE)) return true;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-02-20 14:07:45 +00:00
|
|
|
/* Are there more vehicles on the tile except the two vehicles involved in overtaking */
|
2008-09-07 11:23:10 +00:00
|
|
|
return HasVehicleOnPos(od->tile, od, EnumFindVehBlockingOvertake);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-05-22 20:18:45 +00:00
|
|
|
static void RoadVehCheckOvertake(RoadVehicle *v, RoadVehicle *u)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
OvertakeData od;
|
|
|
|
|
|
|
|
od.v = v;
|
|
|
|
od.u = u;
|
|
|
|
|
2007-05-25 11:01:44 +00:00
|
|
|
/* Trams can't overtake other trams */
|
2009-05-22 20:22:20 +00:00
|
|
|
if (v->roadtype == ROADTYPE_TRAM) return;
|
2007-05-25 11:01:44 +00:00
|
|
|
|
2007-10-04 20:16:35 +00:00
|
|
|
/* Don't overtake in stations */
|
2009-12-20 11:22:36 +00:00
|
|
|
if (IsTileType(v->tile, MP_STATION) || IsTileType(u->tile, MP_STATION)) return;
|
2007-10-04 20:16:35 +00:00
|
|
|
|
2007-06-11 14:00:16 +00:00
|
|
|
/* For now, articulated road vehicles can't overtake anything. */
|
2009-07-02 09:06:15 +00:00
|
|
|
if (v->HasArticulatedPart()) return;
|
2007-06-11 14:00:16 +00:00
|
|
|
|
2015-08-02 21:58:41 +00:00
|
|
|
/* Don't overtake if the vehicle is broken or about to break down */
|
|
|
|
if (v->breakdown_ctr != 0) return;
|
|
|
|
|
2008-02-20 14:07:45 +00:00
|
|
|
/* Vehicles are not driving in same direction || direction is not a diagonal direction */
|
2006-02-18 14:41:24 +00:00
|
|
|
if (v->direction != u->direction || !(v->direction & 1)) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-13 22:27:27 +00:00
|
|
|
/* Check if vehicle is in a road stop, depot, tunnel or bridge or not on a straight road */
|
2009-05-22 20:22:20 +00:00
|
|
|
if (v->state >= RVSB_IN_ROAD_STOP || !IsStraightRoadTrackdir((Trackdir)(v->state & RVSB_TRACKDIR_MASK))) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2015-01-01 22:39:35 +00:00
|
|
|
/* Can't overtake a vehicle that is moving faster than us. If the vehicle in front is
|
2015-03-16 20:01:14 +00:00
|
|
|
* accelerating, take the maximum speed for the comparison, else the current speed.
|
|
|
|
* Original acceleration always accelerates, so always use the maximum speed. */
|
|
|
|
int u_speed = (_settings_game.vehicle.roadveh_acceleration_model == AM_ORIGINAL || u->GetAcceleration() > 0) ? u->GetCurrentMaxSpeed() : u->cur_speed;
|
2015-01-01 22:39:35 +00:00
|
|
|
if (u_speed >= v->GetCurrentMaxSpeed() &&
|
|
|
|
!(u->vehstatus & VS_STOPPED) &&
|
|
|
|
u->cur_speed != 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-05-14 18:31:21 +00:00
|
|
|
od.trackdir = DiagDirToDiagTrackdir(DirToDiagDir(v->direction));
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-02-20 14:07:45 +00:00
|
|
|
/* Are the current and the next tile suitable for overtaking?
|
|
|
|
* - Does the track continue along od.trackdir
|
|
|
|
* - No junctions
|
|
|
|
* - No barred levelcrossing
|
|
|
|
* - No other vehicles in the way
|
|
|
|
*/
|
2004-08-09 17:04:08 +00:00
|
|
|
od.tile = v->tile;
|
2008-02-20 14:07:45 +00:00
|
|
|
if (CheckRoadBlockedForOvertaking(&od)) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-09-05 23:21:41 +00:00
|
|
|
od.tile = v->tile + TileOffsByDiagDir(DirToDiagDir(v->direction));
|
2008-02-20 14:07:45 +00:00
|
|
|
if (CheckRoadBlockedForOvertaking(&od)) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2011-02-02 22:06:14 +00:00
|
|
|
/* When the vehicle in front of us is stopped we may only take
|
|
|
|
* half the time to pass it than when the vehicle is moving. */
|
2017-05-08 20:54:15 +00:00
|
|
|
v->overtaking_ctr = (od.u->cur_speed == 0 || od.u->IsRoadVehicleStopped()) ? RV_OVERTAKE_TIMEOUT / 2 : 0;
|
2011-02-02 22:06:14 +00:00
|
|
|
v->overtaking = RVSB_DRIVE_SIDE;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2014-09-21 12:44:38 +00:00
|
|
|
static void RoadZPosAffectSpeed(RoadVehicle *v, int old_z)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2010-03-06 12:56:57 +00:00
|
|
|
if (old_z == v->z_pos || _settings_game.vehicle.roadveh_acceleration_model != AM_ORIGINAL) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (old_z < v->z_pos) {
|
2006-02-13 21:15:00 +00:00
|
|
|
v->cur_speed = v->cur_speed * 232 / 256; // slow down by ~10%
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
|
|
|
uint16 spd = v->cur_speed + 2;
|
2010-11-06 13:05:11 +00:00
|
|
|
if (spd <= v->vcache.cached_max_speed) v->cur_speed = spd;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int PickRandomBit(uint bits)
|
|
|
|
{
|
|
|
|
uint i;
|
2007-11-04 22:47:34 +00:00
|
|
|
uint num = RandomRange(CountBits(bits));
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-02-13 21:15:00 +00:00
|
|
|
for (i = 0; !(bits & 1) || (int)--num >= 0; bits >>= 1, i++) {}
|
2004-08-09 17:04:08 +00:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2007-02-13 20:57:02 +00:00
|
|
|
/**
|
|
|
|
* Returns direction to for a road vehicle to take or
|
|
|
|
* INVALID_TRACKDIR if the direction is currently blocked
|
2007-04-17 21:09:38 +00:00
|
|
|
* @param v the Vehicle to do the pathfinding for
|
2007-02-13 20:57:02 +00:00
|
|
|
* @param tile the where to start the pathfinding
|
|
|
|
* @param enterdir the direction the vehicle enters the tile from
|
2007-04-17 21:09:38 +00:00
|
|
|
* @return the Trackdir to take
|
2007-02-13 20:57:02 +00:00
|
|
|
*/
|
2009-05-22 20:18:45 +00:00
|
|
|
static Trackdir RoadFindPathToDest(RoadVehicle *v, TileIndex tile, DiagDirection enterdir)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-02-13 20:57:02 +00:00
|
|
|
#define return_track(x) { best_track = (Trackdir)x; goto found_best_track; }
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-06-24 12:38:35 +00:00
|
|
|
TileIndex desttile;
|
2007-02-13 20:57:02 +00:00
|
|
|
Trackdir best_track;
|
2010-12-13 21:56:25 +00:00
|
|
|
bool path_found = true;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-05-22 20:22:20 +00:00
|
|
|
TrackStatus ts = GetTileTrackStatus(tile, TRANSPORT_ROAD, v->compatible_roadtypes);
|
2008-02-20 17:49:50 +00:00
|
|
|
TrackdirBits red_signals = TrackStatusToRedSignals(ts); // crossing
|
|
|
|
TrackdirBits trackdirs = TrackStatusToTrackdirBits(ts);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-07-29 23:42:59 +00:00
|
|
|
if (IsTileType(tile, MP_ROAD)) {
|
2015-08-06 21:24:24 +00:00
|
|
|
if (IsRoadDepot(tile) && (!IsInfraTileUsageAllowed(VEH_ROAD, v->owner, tile) || GetRoadDepotDirection(tile) == enterdir || (GetRoadTypes(tile) & v->compatible_roadtypes) == 0)) {
|
2008-09-30 20:39:50 +00:00
|
|
|
/* Road depot owned by another company or with the wrong orientation */
|
2007-02-13 20:57:02 +00:00
|
|
|
trackdirs = TRACKDIR_BIT_NONE;
|
2005-10-23 13:04:44 +00:00
|
|
|
}
|
2007-02-14 16:37:16 +00:00
|
|
|
} else if (IsTileType(tile, MP_STATION) && IsStandardRoadStopTile(tile)) {
|
|
|
|
/* Standard road stop (drive-through stops are treated as normal road) */
|
2007-08-24 19:19:18 +00:00
|
|
|
|
2015-08-06 21:24:24 +00:00
|
|
|
if (!IsInfraTileUsageAllowed(VEH_ROAD, v->owner, tile) || GetRoadStopDir(tile) == enterdir || v->HasArticulatedPart()) {
|
2007-08-24 19:19:18 +00:00
|
|
|
/* different station owner or wrong orientation or the vehicle has articulated parts */
|
2007-02-13 20:57:02 +00:00
|
|
|
trackdirs = TRACKDIR_BIT_NONE;
|
2006-06-02 19:29:11 +00:00
|
|
|
} else {
|
2004-09-14 01:21:07 +00:00
|
|
|
/* Our station */
|
2009-12-02 17:37:02 +00:00
|
|
|
RoadStopType rstype = v->IsBus() ? ROADSTOP_BUS : ROADSTOP_TRUCK;
|
2006-03-03 22:21:29 +00:00
|
|
|
|
2006-06-02 19:29:11 +00:00
|
|
|
if (GetRoadStopType(tile) != rstype) {
|
2007-02-13 20:57:02 +00:00
|
|
|
/* Wrong station type */
|
|
|
|
trackdirs = TRACKDIR_BIT_NONE;
|
2006-06-02 19:29:11 +00:00
|
|
|
} else {
|
2007-02-13 20:57:02 +00:00
|
|
|
/* Proper station type, check if there is free loading bay */
|
2008-05-29 15:13:28 +00:00
|
|
|
if (!_settings_game.pf.roadveh_queue && IsStandardRoadStopTile(tile) &&
|
2009-06-26 10:45:20 +00:00
|
|
|
!RoadStop::GetByTile(tile, rstype)->HasFreeBay()) {
|
2007-02-13 20:57:02 +00:00
|
|
|
/* Station is full and RV queuing is off */
|
|
|
|
trackdirs = TRACKDIR_BIT_NONE;
|
2006-02-27 19:43:19 +00:00
|
|
|
}
|
2004-09-14 01:21:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* The above lookups should be moved to GetTileTrackStatus in the
|
|
|
|
* future, but that requires more changes to the pathfinder and other
|
|
|
|
* stuff, probably even more arguments to GTTS.
|
|
|
|
*/
|
2004-09-05 16:15:22 +00:00
|
|
|
|
2007-02-13 20:57:02 +00:00
|
|
|
/* Remove tracks unreachable from the enter dir */
|
2017-03-20 18:14:22 +00:00
|
|
|
trackdirs &= DiagdirReachesTrackdirs(enterdir);
|
2007-02-13 20:57:02 +00:00
|
|
|
if (trackdirs == TRACKDIR_BIT_NONE) {
|
2005-01-31 11:23:10 +00:00
|
|
|
/* No reachable tracks, so we'll reverse */
|
|
|
|
return_track(_road_reverse_table[enterdir]);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-05-22 20:22:20 +00:00
|
|
|
if (v->reverse_ctr != 0) {
|
2008-09-21 11:26:50 +00:00
|
|
|
bool reverse = true;
|
2009-05-22 20:22:20 +00:00
|
|
|
if (v->roadtype == ROADTYPE_TRAM) {
|
2008-09-21 11:26:50 +00:00
|
|
|
/* Trams may only reverse on a tile if it contains at least the straight
|
|
|
|
* trackbits or when it is a valid turning tile (i.e. one roadbit) */
|
|
|
|
RoadBits rb = GetAnyRoadBits(tile, ROADTYPE_TRAM);
|
|
|
|
RoadBits straight = AxisToRoadBits(DiagDirToAxis(enterdir));
|
|
|
|
reverse = ((rb & straight) == straight) ||
|
|
|
|
(rb == DiagDirToRoadBits(enterdir));
|
|
|
|
}
|
|
|
|
if (reverse) {
|
2009-05-22 20:22:20 +00:00
|
|
|
v->reverse_ctr = 0;
|
2008-09-21 11:26:50 +00:00
|
|
|
if (v->tile != tile) {
|
|
|
|
return_track(_road_reverse_table[enterdir]);
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
desttile = v->dest_tile;
|
|
|
|
if (desttile == 0) {
|
2007-02-13 20:57:02 +00:00
|
|
|
/* We've got no destination, pick a random track */
|
|
|
|
return_track(PickRandomBit(trackdirs));
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-02-13 20:57:02 +00:00
|
|
|
/* Only one track to choose between? */
|
2007-11-05 17:50:10 +00:00
|
|
|
if (KillFirstBit(trackdirs) == TRACKDIR_BIT_NONE) {
|
2007-02-13 20:57:02 +00:00
|
|
|
return_track(FindFirstBit2x64(trackdirs));
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-05-29 15:13:28 +00:00
|
|
|
switch (_settings_game.pf.pathfinder_for_roadvehs) {
|
2010-12-13 21:56:25 +00:00
|
|
|
case VPF_NPF: best_track = NPFRoadVehicleChooseTrack(v, tile, enterdir, trackdirs, path_found); break;
|
|
|
|
case VPF_YAPF: best_track = YapfRoadVehicleChooseTrack(v, tile, enterdir, trackdirs, path_found); break;
|
2008-02-13 17:54:11 +00:00
|
|
|
|
2009-12-02 10:44:38 +00:00
|
|
|
default: NOT_REACHED();
|
2005-01-31 11:23:10 +00:00
|
|
|
}
|
2010-12-13 21:56:25 +00:00
|
|
|
v->HandlePathfindingResult(path_found);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
found_best_track:;
|
|
|
|
|
2008-02-20 17:49:50 +00:00
|
|
|
if (HasBit(red_signals, best_track)) return INVALID_TRACKDIR;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-09-10 19:02:27 +00:00
|
|
|
return best_track;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-03-07 12:11:48 +00:00
|
|
|
struct RoadDriveEntry {
|
|
|
|
byte x, y;
|
|
|
|
};
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-02-07 17:01:06 +00:00
|
|
|
#include "table/roadveh_movement.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-05-22 20:18:45 +00:00
|
|
|
static bool RoadVehLeaveDepot(RoadVehicle *v, bool first)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2012-07-07 15:37:29 +00:00
|
|
|
/* Don't leave unless v and following wagons are in the depot. */
|
2009-05-22 20:18:45 +00:00
|
|
|
for (const RoadVehicle *u = v; u != NULL; u = u->Next()) {
|
2009-05-22 20:22:20 +00:00
|
|
|
if (u->state != RVSB_IN_DEPOT || u->tile != v->tile) return false;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-06-11 14:00:16 +00:00
|
|
|
DiagDirection dir = GetRoadDepotDirection(v->tile);
|
|
|
|
v->direction = DiagDirToDir(dir);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2017-03-20 18:16:23 +00:00
|
|
|
Trackdir tdir = DiagDirToDiagTrackdir(dir);
|
2009-05-22 20:22:20 +00:00
|
|
|
const RoadDriveEntry *rdp = _road_drive_data[v->roadtype][(_settings_game.vehicle.road_side << RVS_DRIVE_SIDE) + tdir];
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-06-11 14:00:16 +00:00
|
|
|
int x = TileX(v->tile) * TILE_SIZE + (rdp[RVC_DEPOT_START_FRAME].x & 0xF);
|
|
|
|
int y = TileY(v->tile) * TILE_SIZE + (rdp[RVC_DEPOT_START_FRAME].y & 0xF);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-06-11 14:00:16 +00:00
|
|
|
if (first) {
|
2011-04-10 10:47:21 +00:00
|
|
|
/* We are leaving a depot, but have to go to the exact same one; re-enter */
|
|
|
|
if (v->current_order.IsType(OT_GOTO_DEPOT) && v->tile == v->dest_tile) {
|
|
|
|
VehicleEnterDepot(v);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-12-08 00:15:15 +00:00
|
|
|
if (RoadVehFindCloseTo(v, x, y, v->direction, false) != NULL) return true;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-06-11 14:00:16 +00:00
|
|
|
VehicleServiceInDepot(v);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-06-11 14:00:16 +00:00
|
|
|
StartRoadVehSound(v);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-06-11 14:00:16 +00:00
|
|
|
/* Vehicle is about to leave a depot */
|
2004-08-09 17:04:08 +00:00
|
|
|
v->cur_speed = 0;
|
2007-06-11 14:00:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
v->vehstatus &= ~VS_HIDDEN;
|
2016-09-09 22:44:13 +00:00
|
|
|
v->cur_image_valid_dir = INVALID_DIR;
|
2009-05-22 20:22:20 +00:00
|
|
|
v->state = tdir;
|
|
|
|
v->frame = RVC_DEPOT_START_FRAME;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2010-03-06 12:55:56 +00:00
|
|
|
v->x_pos = x;
|
|
|
|
v->y_pos = y;
|
2014-09-20 15:31:26 +00:00
|
|
|
v->UpdatePosition();
|
2010-03-06 12:55:56 +00:00
|
|
|
v->UpdateInclination(true, true);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-06-11 14:00:16 +00:00
|
|
|
InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-06-11 14:00:16 +00:00
|
|
|
return true;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-05-22 20:18:45 +00:00
|
|
|
static Trackdir FollowPreviousRoadVehicle(const RoadVehicle *v, const RoadVehicle *prev, TileIndex tile, DiagDirection entry_dir, bool already_reversed)
|
2007-06-11 14:00:16 +00:00
|
|
|
{
|
2007-08-06 12:46:49 +00:00
|
|
|
if (prev->tile == v->tile && !already_reversed) {
|
2007-06-11 14:00:16 +00:00
|
|
|
/* If the previous vehicle is on the same tile as this vehicle is
|
|
|
|
* then it must have reversed. */
|
|
|
|
return _road_reverse_table[entry_dir];
|
|
|
|
}
|
2004-12-09 21:46:56 +00:00
|
|
|
|
2009-05-22 20:22:20 +00:00
|
|
|
byte prev_state = prev->state;
|
2007-06-11 14:00:16 +00:00
|
|
|
Trackdir dir;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-06-11 14:00:16 +00:00
|
|
|
if (prev_state == RVSB_WORMHOLE || prev_state == RVSB_IN_DEPOT) {
|
|
|
|
DiagDirection diag_dir = INVALID_DIAGDIR;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-12-16 19:30:42 +00:00
|
|
|
if (IsTileType(tile, MP_TUNNELBRIDGE)) {
|
2007-12-16 15:38:51 +00:00
|
|
|
diag_dir = GetTunnelBridgeDirection(tile);
|
2008-02-14 15:59:16 +00:00
|
|
|
} else if (IsRoadDepotTile(tile)) {
|
2007-06-11 14:00:16 +00:00
|
|
|
diag_dir = ReverseDiagDir(GetRoadDepotDirection(tile));
|
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-06-11 14:00:16 +00:00
|
|
|
if (diag_dir == INVALID_DIAGDIR) return INVALID_TRACKDIR;
|
2008-05-14 18:31:21 +00:00
|
|
|
dir = DiagDirToDiagTrackdir(diag_dir);
|
2008-01-12 22:10:00 +00:00
|
|
|
} else {
|
2007-11-12 18:28:55 +00:00
|
|
|
if (already_reversed && prev->tile != tile) {
|
|
|
|
/*
|
|
|
|
* The vehicle has reversed, but did not go straight back.
|
2010-08-15 14:06:43 +00:00
|
|
|
* It immediately turn onto another tile. This means that
|
2007-11-12 18:28:55 +00:00
|
|
|
* the roadstate of the previous vehicle cannot be used
|
|
|
|
* as the direction we have to go with this vehicle.
|
|
|
|
*
|
|
|
|
* Next table is build in the following way:
|
|
|
|
* - first row for when the vehicle in front went to the northern or
|
|
|
|
* western tile, second for southern and eastern.
|
|
|
|
* - columns represent the entry direction.
|
|
|
|
* - cell values are determined by the Trackdir one has to take from
|
|
|
|
* the entry dir (column) to the tile in north or south by only
|
|
|
|
* going over the trackdirs used for turning 90 degrees, i.e.
|
|
|
|
* TRACKDIR_{UPPER,RIGHT,LOWER,LEFT}_{N,E,S,W}.
|
|
|
|
*/
|
2009-09-20 23:11:01 +00:00
|
|
|
static const Trackdir reversed_turn_lookup[2][DIAGDIR_END] = {
|
2007-11-12 18:28:55 +00:00
|
|
|
{ TRACKDIR_UPPER_W, TRACKDIR_RIGHT_N, TRACKDIR_LEFT_N, TRACKDIR_UPPER_E },
|
|
|
|
{ TRACKDIR_RIGHT_S, TRACKDIR_LOWER_W, TRACKDIR_LOWER_E, TRACKDIR_LEFT_S }};
|
|
|
|
dir = reversed_turn_lookup[prev->tile < tile ? 0 : 1][ReverseDiagDir(entry_dir)];
|
2008-01-12 22:10:00 +00:00
|
|
|
} else if (HasBit(prev_state, RVS_IN_DT_ROAD_STOP)) {
|
|
|
|
dir = (Trackdir)(prev_state & RVSB_ROAD_STOP_TRACKDIR_MASK);
|
|
|
|
} else if (prev_state < TRACKDIR_END) {
|
2007-11-12 18:28:55 +00:00
|
|
|
dir = (Trackdir)prev_state;
|
2008-01-12 22:10:00 +00:00
|
|
|
} else {
|
|
|
|
return INVALID_TRACKDIR;
|
2007-11-12 18:28:55 +00:00
|
|
|
}
|
2007-06-11 14:00:16 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-06-11 14:00:16 +00:00
|
|
|
/* Do some sanity checking. */
|
|
|
|
static const RoadBits required_roadbits[] = {
|
|
|
|
ROAD_X, ROAD_Y, ROAD_NW | ROAD_NE, ROAD_SW | ROAD_SE,
|
|
|
|
ROAD_NW | ROAD_SW, ROAD_NE | ROAD_SE, ROAD_X, ROAD_Y
|
|
|
|
};
|
|
|
|
RoadBits required = required_roadbits[dir & 0x07];
|
|
|
|
|
2017-06-21 22:40:19 +00:00
|
|
|
if ((required & GetAnyRoadBits(tile, v->roadtype, false)) == ROAD_NONE) {
|
2007-06-11 14:00:16 +00:00
|
|
|
dir = INVALID_TRACKDIR;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-06-11 14:00:16 +00:00
|
|
|
return dir;
|
|
|
|
}
|
|
|
|
|
2007-11-17 12:42:15 +00:00
|
|
|
/**
|
|
|
|
* Can a tram track build without destruction on the given tile?
|
2008-09-30 20:39:50 +00:00
|
|
|
* @param c the company that would be building the tram tracks
|
2007-11-17 12:42:15 +00:00
|
|
|
* @param t the tile to build on.
|
2007-12-11 20:28:53 +00:00
|
|
|
* @param r the road bits needed.
|
2007-11-17 12:42:15 +00:00
|
|
|
* @return true when a track track can be build on 't'
|
|
|
|
*/
|
2008-09-30 20:39:50 +00:00
|
|
|
static bool CanBuildTramTrackOnTile(CompanyID c, TileIndex t, RoadBits r)
|
2007-11-17 12:42:15 +00:00
|
|
|
{
|
2008-09-30 20:39:50 +00:00
|
|
|
/* The 'current' company is not necessarily the owner of the vehicle. */
|
2010-06-05 12:16:12 +00:00
|
|
|
Backup<CompanyByte> cur_company(_current_company, c, FILE_LINE);
|
2007-11-17 12:42:15 +00:00
|
|
|
|
2013-02-03 20:18:41 +00:00
|
|
|
CommandCost ret = DoCommand(t, ROADTYPE_TRAM << 4 | r, 0, DC_NO_WATER, CMD_BUILD_ROAD);
|
2007-11-17 12:42:15 +00:00
|
|
|
|
2010-05-31 20:22:57 +00:00
|
|
|
cur_company.Restore();
|
2010-01-18 22:57:21 +00:00
|
|
|
return ret.Succeeded();
|
2007-11-17 12:42:15 +00:00
|
|
|
}
|
|
|
|
|
2013-12-22 11:55:07 +00:00
|
|
|
bool IndividualRoadVehicleController(RoadVehicle *v, const RoadVehicle *prev)
|
2007-06-11 14:00:16 +00:00
|
|
|
{
|
2017-02-19 12:47:22 +00:00
|
|
|
SCOPE_INFO_FMT([&], "IndividualRoadVehicleController: %s, %s", scope_dumper().VehicleInfo(v), scope_dumper().VehicleInfo(prev));
|
2009-05-22 20:22:20 +00:00
|
|
|
if (v->overtaking != 0) {
|
2007-12-04 21:53:09 +00:00
|
|
|
if (IsTileType(v->tile, MP_STATION)) {
|
|
|
|
/* Force us to be not overtaking! */
|
2009-05-22 20:22:20 +00:00
|
|
|
v->overtaking = 0;
|
2011-02-02 22:06:14 +00:00
|
|
|
} else if (++v->overtaking_ctr >= RV_OVERTAKE_TIMEOUT) {
|
2006-01-06 17:25:39 +00:00
|
|
|
/* If overtaking just aborts at a random moment, we can have a out-of-bound problem,
|
|
|
|
* if the vehicle started a corner. To protect that, only allow an abort of
|
2007-02-13 22:27:27 +00:00
|
|
|
* overtake if we are on straight roads */
|
2009-05-22 20:22:20 +00:00
|
|
|
if (v->state < RVSB_IN_ROAD_STOP && IsStraightRoadTrackdir((Trackdir)v->state)) {
|
|
|
|
v->overtaking = 0;
|
2006-02-18 14:41:24 +00:00
|
|
|
}
|
2007-12-04 21:53:09 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-06-11 14:00:16 +00:00
|
|
|
/* If this vehicle is in a depot and we've reached this point it must be
|
|
|
|
* one of the articulated parts. It will stay in the depot until activated
|
|
|
|
* by the previous vehicle in the chain when it gets to the right place. */
|
2007-08-29 21:49:08 +00:00
|
|
|
if (v->IsInDepot()) return true;
|
2007-06-11 14:00:16 +00:00
|
|
|
|
2017-02-05 18:07:10 +00:00
|
|
|
bool no_advance_tile = false;
|
|
|
|
|
2009-05-22 20:22:20 +00:00
|
|
|
if (v->state == RVSB_WORMHOLE) {
|
2007-02-14 10:33:36 +00:00
|
|
|
/* Vehicle is entering a depot or is on a bridge or in a tunnel */
|
2007-02-25 10:49:13 +00:00
|
|
|
GetNewVehiclePosResult gp = GetNewVehiclePos(v);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2011-01-19 18:44:13 +00:00
|
|
|
if (v->IsFrontEngine()) {
|
2008-01-13 12:08:35 +00:00
|
|
|
const Vehicle *u = RoadVehFindCloseTo(v, gp.x, gp.y, v->direction);
|
|
|
|
if (u != NULL) {
|
|
|
|
v->cur_speed = u->First()->cur_speed;
|
|
|
|
return false;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-01-23 14:51:36 +00:00
|
|
|
if (IsTileType(gp.new_tile, MP_TUNNELBRIDGE) && HasBit(VehicleEnterTile(v, gp.new_tile, gp.x, gp.y), VETS_ENTERED_WORMHOLE)) {
|
2017-02-05 18:07:10 +00:00
|
|
|
if (IsRoadCustomBridgeHeadTile(gp.new_tile)) {
|
|
|
|
v->frame = 15;
|
|
|
|
no_advance_tile = true;
|
|
|
|
} else {
|
|
|
|
/* Vehicle has just entered a bridge or tunnel */
|
|
|
|
v->x_pos = gp.x;
|
|
|
|
v->y_pos = gp.y;
|
|
|
|
v->UpdatePosition();
|
|
|
|
v->UpdateInclination(true, true);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else {
|
2010-03-06 12:55:56 +00:00
|
|
|
v->x_pos = gp.x;
|
|
|
|
v->y_pos = gp.y;
|
2014-09-20 15:31:26 +00:00
|
|
|
v->UpdatePosition();
|
2017-03-05 18:05:04 +00:00
|
|
|
RoadZPosAffectSpeed(v, v->UpdateInclination(false, false, true));
|
2017-02-06 01:53:06 +00:00
|
|
|
if (v->IsDrawn()) v->Vehicle::UpdateViewport(true);
|
2007-06-11 14:00:16 +00:00
|
|
|
return true;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-14 16:37:16 +00:00
|
|
|
/* Get move position data for next frame.
|
|
|
|
* For a drive-through road stop use 'straight road' move data.
|
2009-05-22 20:22:20 +00:00
|
|
|
* In this case v->state is masked to give the road stop entry direction. */
|
|
|
|
RoadDriveEntry rd = _road_drive_data[v->roadtype][(
|
|
|
|
(HasBit(v->state, RVS_IN_DT_ROAD_STOP) ? v->state & RVSB_ROAD_STOP_TRACKDIR_MASK : v->state) +
|
|
|
|
(_settings_game.vehicle.road_side << RVS_DRIVE_SIDE)) ^ v->overtaking][v->frame + 1];
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-14 01:00:57 +00:00
|
|
|
if (rd.x & RDE_NEXT_TILE) {
|
2017-02-05 18:07:10 +00:00
|
|
|
TileIndex tile = v->tile;
|
|
|
|
if (!no_advance_tile) tile += TileOffsByDiagDir((DiagDirection)(rd.x & 3));
|
2007-06-11 14:00:16 +00:00
|
|
|
Trackdir dir;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2011-01-19 18:44:13 +00:00
|
|
|
if (v->IsFrontEngine()) {
|
2007-06-11 14:00:16 +00:00
|
|
|
/* If this is the front engine, look for the right path. */
|
|
|
|
dir = RoadFindPathToDest(v, tile, (DiagDirection)(rd.x & 3));
|
2017-02-05 18:07:10 +00:00
|
|
|
} else if (no_advance_tile) {
|
|
|
|
/* Follow previous vehicle out of custom bridge wormhole */
|
|
|
|
dir = (Trackdir) prev->state;
|
2007-06-11 14:00:16 +00:00
|
|
|
} else {
|
2007-08-06 12:46:49 +00:00
|
|
|
dir = FollowPreviousRoadVehicle(v, prev, tile, (DiagDirection)(rd.x & 3), false);
|
2007-06-11 14:00:16 +00:00
|
|
|
}
|
|
|
|
|
2007-02-13 20:57:02 +00:00
|
|
|
if (dir == INVALID_TRACKDIR) {
|
2011-01-19 18:44:13 +00:00
|
|
|
if (!v->IsFrontEngine()) error("Disconnecting road vehicle.");
|
2004-08-09 17:04:08 +00:00
|
|
|
v->cur_speed = 0;
|
2007-06-11 14:00:16 +00:00
|
|
|
return false;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
again:
|
2007-11-17 12:42:15 +00:00
|
|
|
uint start_frame = RVC_DEFAULT_START_FRAME;
|
2007-02-13 22:27:27 +00:00
|
|
|
if (IsReversingRoadTrackdir(dir)) {
|
2011-02-02 22:13:13 +00:00
|
|
|
/* When turning around we can't be overtaking. */
|
|
|
|
v->overtaking = 0;
|
|
|
|
|
2017-02-05 18:07:10 +00:00
|
|
|
if (no_advance_tile) {
|
|
|
|
DEBUG(misc, 0, "Road vehicle attempted to turn around on a single road piece bridge head");
|
|
|
|
}
|
|
|
|
|
2005-01-31 11:23:10 +00:00
|
|
|
/* Turning around */
|
2009-05-22 20:22:20 +00:00
|
|
|
if (v->roadtype == ROADTYPE_TRAM) {
|
2007-11-17 12:42:15 +00:00
|
|
|
/* Determine the road bits the tram needs to be able to turn around
|
|
|
|
* using the 'big' corner loop. */
|
|
|
|
RoadBits needed;
|
2007-05-25 22:07:40 +00:00
|
|
|
switch (dir) {
|
|
|
|
default: NOT_REACHED();
|
|
|
|
case TRACKDIR_RVREV_NE: needed = ROAD_SW; break;
|
|
|
|
case TRACKDIR_RVREV_SE: needed = ROAD_NW; break;
|
|
|
|
case TRACKDIR_RVREV_SW: needed = ROAD_NE; break;
|
|
|
|
case TRACKDIR_RVREV_NW: needed = ROAD_SE; break;
|
|
|
|
}
|
2017-02-05 18:07:10 +00:00
|
|
|
auto tile_turn_ok = [&]() -> bool {
|
|
|
|
if (IsNormalRoadTile(tile)) {
|
|
|
|
return !HasRoadWorks(tile) && (needed & GetRoadBits(tile, ROADTYPE_TRAM)) != ROAD_NONE;
|
|
|
|
} else if (IsRoadCustomBridgeHeadTile(tile)) {
|
|
|
|
return (needed & GetCustomBridgeHeadRoadBits(tile, ROADTYPE_TRAM)) != ROAD_NONE;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
2017-02-16 22:11:41 +00:00
|
|
|
if ((v->Previous() != NULL && v->Previous()->tile == tile) || (v->IsFrontEngine() && tile_turn_ok())) {
|
2007-11-17 12:42:15 +00:00
|
|
|
/*
|
|
|
|
* Taking the 'big' corner for trams only happens when:
|
|
|
|
* - The previous vehicle in this (articulated) tram chain is
|
|
|
|
* already on the 'next' tile, we just follow them regardless of
|
|
|
|
* anything. When it is NOT on the 'next' tile, the tram started
|
|
|
|
* doing a reversing turn when the piece of tram track on the next
|
|
|
|
* tile did not exist yet. Do not use the big tram loop as that is
|
|
|
|
* going to cause the tram to split up.
|
|
|
|
* - Or the front of the tram can drive over the next tile.
|
|
|
|
*/
|
2011-01-19 18:44:13 +00:00
|
|
|
} else if (!v->IsFrontEngine() || !CanBuildTramTrackOnTile(v->owner, tile, needed) || ((~needed & GetAnyRoadBits(v->tile, ROADTYPE_TRAM, false)) == ROAD_NONE)) {
|
2007-11-17 12:42:15 +00:00
|
|
|
/*
|
|
|
|
* Taking the 'small' corner for trams only happens when:
|
|
|
|
* - We are not the from vehicle of an articulated tram.
|
2008-09-30 20:39:50 +00:00
|
|
|
* - Or when the company cannot build on the next tile.
|
2007-11-17 12:42:15 +00:00
|
|
|
*
|
|
|
|
* The 'small' corner means that the vehicle is on the end of a
|
|
|
|
* tram track and needs to start turning there. To do this properly
|
|
|
|
* the tram needs to start at an offset in the tram turning 'code'
|
|
|
|
* for 'big' corners. It furthermore does not go to the next tile,
|
|
|
|
* so that needs to be fixed too.
|
|
|
|
*/
|
|
|
|
tile = v->tile;
|
|
|
|
start_frame = RVC_TURN_AROUND_START_FRAME_SHORT_TRAM;
|
|
|
|
} else {
|
2008-09-30 20:39:50 +00:00
|
|
|
/* The company can build on the next tile, so wait till (s)he does. */
|
2007-05-25 22:07:40 +00:00
|
|
|
v->cur_speed = 0;
|
2007-06-11 14:00:16 +00:00
|
|
|
return false;
|
2007-05-25 22:07:40 +00:00
|
|
|
}
|
2008-02-14 15:59:16 +00:00
|
|
|
} else if (IsNormalRoadTile(v->tile) && GetDisallowedRoadDirections(v->tile) != DRD_NONE) {
|
2007-05-31 15:15:00 +00:00
|
|
|
v->cur_speed = 0;
|
2007-06-11 14:00:16 +00:00
|
|
|
return false;
|
2007-05-25 22:07:40 +00:00
|
|
|
} else {
|
|
|
|
tile = v->tile;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-02-12 22:46:10 +00:00
|
|
|
/* Get position data for first frame on the new tile */
|
2009-05-22 20:22:20 +00:00
|
|
|
const RoadDriveEntry *rdp = _road_drive_data[v->roadtype][(dir + (_settings_game.vehicle.road_side << RVS_DRIVE_SIDE)) ^ v->overtaking];
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-03-11 15:00:40 +00:00
|
|
|
int x = TileX(tile) * TILE_SIZE + rdp[start_frame].x;
|
|
|
|
int y = TileY(tile) * TILE_SIZE + rdp[start_frame].y;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-03-11 15:00:40 +00:00
|
|
|
Direction new_dir = RoadVehGetSlidingDirection(v, x, y);
|
2011-01-19 18:44:13 +00:00
|
|
|
if (v->IsFrontEngine()) {
|
2009-03-11 15:00:40 +00:00
|
|
|
Vehicle *u = RoadVehFindCloseTo(v, x, y, new_dir);
|
2007-11-19 18:58:32 +00:00
|
|
|
if (u != NULL) {
|
|
|
|
v->cur_speed = u->First()->cur_speed;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-03-11 15:00:40 +00:00
|
|
|
uint32 r = VehicleEnterTile(v, tile, x, y);
|
2007-11-19 21:02:30 +00:00
|
|
|
if (HasBit(r, VETS_CANNOT_ENTER)) {
|
2005-01-16 11:24:58 +00:00
|
|
|
if (!IsTileType(tile, MP_TUNNELBRIDGE)) {
|
2004-08-09 17:04:08 +00:00
|
|
|
v->cur_speed = 0;
|
2007-06-11 14:00:16 +00:00
|
|
|
return false;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2007-02-12 22:46:10 +00:00
|
|
|
/* Try an about turn to re-enter the previous tile */
|
2007-02-13 22:27:27 +00:00
|
|
|
dir = _road_reverse_table[rd.x & 3];
|
2004-08-09 17:04:08 +00:00
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
|
2009-05-22 20:22:20 +00:00
|
|
|
if (IsInsideMM(v->state, RVSB_IN_ROAD_STOP, RVSB_IN_DT_ROAD_STOP_END) && IsTileType(v->tile, MP_STATION)) {
|
|
|
|
if (IsReversingRoadTrackdir(dir) && IsInsideMM(v->state, RVSB_IN_ROAD_STOP, RVSB_IN_ROAD_STOP_END)) {
|
2007-02-12 22:46:10 +00:00
|
|
|
/* New direction is trying to turn vehicle around.
|
|
|
|
* We can't turn at the exit of a road stop so wait.*/
|
2006-02-18 14:41:24 +00:00
|
|
|
v->cur_speed = 0;
|
2007-06-11 14:00:16 +00:00
|
|
|
return false;
|
2006-02-18 14:41:24 +00:00
|
|
|
}
|
2009-12-18 21:35:55 +00:00
|
|
|
|
|
|
|
/* If we are a drive through road stop and the next tile is of
|
|
|
|
* the same road stop and the next tile isn't this one (i.e. we
|
|
|
|
* are not reversing), then keep the reservation and state.
|
|
|
|
* This way we will not be shortly unregister from the road
|
|
|
|
* stop. It also makes it possible to load when on the edge of
|
|
|
|
* two road stops; otherwise you could get vehicles that should
|
|
|
|
* be loading but are not actually loading. */
|
|
|
|
if (IsDriveThroughStopTile(v->tile) &&
|
|
|
|
RoadStop::IsDriveThroughRoadStopContinuation(v->tile, tile) &&
|
|
|
|
v->tile != tile) {
|
|
|
|
/* So, keep 'our' state */
|
|
|
|
dir = (Trackdir)v->state;
|
|
|
|
} else if (IsRoadStop(v->tile)) {
|
|
|
|
/* We're not continuing our drive through road stop, so leave. */
|
|
|
|
RoadStop::GetByTile(v->tile, GetRoadStopType(v->tile))->Leave(v);
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-11-19 21:02:30 +00:00
|
|
|
if (!HasBit(r, VETS_ENTERED_WORMHOLE)) {
|
2016-09-09 22:44:13 +00:00
|
|
|
v->cur_image_valid_dir = INVALID_DIR;
|
2004-08-09 17:04:08 +00:00
|
|
|
v->tile = tile;
|
2009-05-22 20:22:20 +00:00
|
|
|
v->state = (byte)dir;
|
|
|
|
v->frame = start_frame;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2009-03-11 15:00:40 +00:00
|
|
|
if (new_dir != v->direction) {
|
|
|
|
v->direction = new_dir;
|
2010-03-06 12:56:57 +00:00
|
|
|
if (_settings_game.vehicle.roadveh_acceleration_model == AM_ORIGINAL) v->cur_speed -= v->cur_speed >> 2;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2010-03-06 12:55:56 +00:00
|
|
|
v->x_pos = x;
|
|
|
|
v->y_pos = y;
|
2014-09-20 15:31:26 +00:00
|
|
|
v->UpdatePosition();
|
2010-03-06 12:55:56 +00:00
|
|
|
RoadZPosAffectSpeed(v, v->UpdateInclination(true, true));
|
2007-06-11 14:00:16 +00:00
|
|
|
return true;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-02-14 01:00:57 +00:00
|
|
|
if (rd.x & RDE_TURNED) {
|
2007-02-12 22:46:10 +00:00
|
|
|
/* Vehicle has finished turning around, it will now head back onto the same tile */
|
2007-08-06 12:46:49 +00:00
|
|
|
Trackdir dir;
|
2007-11-17 12:42:15 +00:00
|
|
|
uint turn_around_start_frame = RVC_TURN_AROUND_START_FRAME;
|
|
|
|
|
2017-06-21 22:40:19 +00:00
|
|
|
if (v->roadtype == ROADTYPE_TRAM && !IsRoadDepotTile(v->tile) && HasExactlyOneBit(GetAnyRoadBits(v->tile, ROADTYPE_TRAM, false))) {
|
2007-11-17 12:42:15 +00:00
|
|
|
/*
|
|
|
|
* The tram is turning around with one tram 'roadbit'. This means that
|
|
|
|
* it is using the 'big' corner 'drive data'. However, to support the
|
|
|
|
* trams to take a small corner, there is a 'turned' marker in the middle
|
|
|
|
* of the turning 'drive data'. When the tram took the long corner, we
|
|
|
|
* will still use the 'big' corner drive data, but we advance it one
|
|
|
|
* frame. We furthermore set the driving direction so the turning is
|
|
|
|
* going to be properly shown.
|
|
|
|
*/
|
|
|
|
turn_around_start_frame = RVC_START_FRAME_AFTER_LONG_TRAM;
|
2008-09-21 11:26:50 +00:00
|
|
|
switch (rd.x & 0x3) {
|
2007-11-17 12:42:15 +00:00
|
|
|
default: NOT_REACHED();
|
2008-09-21 11:26:50 +00:00
|
|
|
case DIAGDIR_NW: dir = TRACKDIR_RVREV_SE; break;
|
|
|
|
case DIAGDIR_NE: dir = TRACKDIR_RVREV_SW; break;
|
|
|
|
case DIAGDIR_SE: dir = TRACKDIR_RVREV_NW; break;
|
|
|
|
case DIAGDIR_SW: dir = TRACKDIR_RVREV_NE; break;
|
2007-11-17 12:42:15 +00:00
|
|
|
}
|
2007-08-06 12:46:49 +00:00
|
|
|
} else {
|
2011-01-19 18:44:13 +00:00
|
|
|
if (v->IsFrontEngine()) {
|
2007-11-17 12:42:15 +00:00
|
|
|
/* If this is the front engine, look for the right path. */
|
|
|
|
dir = RoadFindPathToDest(v, v->tile, (DiagDirection)(rd.x & 3));
|
|
|
|
} else {
|
|
|
|
dir = FollowPreviousRoadVehicle(v, prev, v->tile, (DiagDirection)(rd.x & 3), true);
|
|
|
|
}
|
2007-08-06 12:46:49 +00:00
|
|
|
}
|
|
|
|
|
2007-02-13 20:57:02 +00:00
|
|
|
if (dir == INVALID_TRACKDIR) {
|
2004-08-09 17:04:08 +00:00
|
|
|
v->cur_speed = 0;
|
2007-06-11 14:00:16 +00:00
|
|
|
return false;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-05-22 20:22:20 +00:00
|
|
|
const RoadDriveEntry *rdp = _road_drive_data[v->roadtype][(_settings_game.vehicle.road_side << RVS_DRIVE_SIDE) + dir];
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-03-11 15:00:40 +00:00
|
|
|
int x = TileX(v->tile) * TILE_SIZE + rdp[turn_around_start_frame].x;
|
|
|
|
int y = TileY(v->tile) * TILE_SIZE + rdp[turn_around_start_frame].y;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-03-11 15:00:40 +00:00
|
|
|
Direction new_dir = RoadVehGetSlidingDirection(v, x, y);
|
2011-01-19 18:44:13 +00:00
|
|
|
if (v->IsFrontEngine() && RoadVehFindCloseTo(v, x, y, new_dir) != NULL) return false;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-03-11 15:00:40 +00:00
|
|
|
uint32 r = VehicleEnterTile(v, v->tile, x, y);
|
2007-11-19 21:02:30 +00:00
|
|
|
if (HasBit(r, VETS_CANNOT_ENTER)) {
|
2004-08-09 17:04:08 +00:00
|
|
|
v->cur_speed = 0;
|
2007-06-11 14:00:16 +00:00
|
|
|
return false;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2016-09-09 22:44:13 +00:00
|
|
|
v->cur_image_valid_dir = INVALID_DIR;
|
2009-05-22 20:22:20 +00:00
|
|
|
v->state = dir;
|
|
|
|
v->frame = turn_around_start_frame;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-03-11 15:00:40 +00:00
|
|
|
if (new_dir != v->direction) {
|
|
|
|
v->direction = new_dir;
|
2010-03-06 12:56:57 +00:00
|
|
|
if (_settings_game.vehicle.roadveh_acceleration_model == AM_ORIGINAL) v->cur_speed -= v->cur_speed >> 2;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2010-03-06 12:55:56 +00:00
|
|
|
v->x_pos = x;
|
|
|
|
v->y_pos = y;
|
2014-09-20 15:31:26 +00:00
|
|
|
v->UpdatePosition();
|
2010-03-06 12:55:56 +00:00
|
|
|
RoadZPosAffectSpeed(v, v->UpdateInclination(true, true));
|
2007-06-11 14:00:16 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This vehicle is not in a wormhole and it hasn't entered a new tile. If
|
|
|
|
* it's on a depot tile, check if it's time to activate the next vehicle in
|
|
|
|
* the chain yet. */
|
2008-02-14 15:59:16 +00:00
|
|
|
if (v->Next() != NULL && IsRoadDepotTile(v->tile)) {
|
2010-12-14 21:33:53 +00:00
|
|
|
if (v->frame == v->gcache.cached_veh_length + RVC_DEPOT_START_FRAME) {
|
2007-08-30 13:03:56 +00:00
|
|
|
RoadVehLeaveDepot(v->Next(), false);
|
2007-06-11 14:00:16 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-02-12 22:46:10 +00:00
|
|
|
/* Calculate new position for the vehicle */
|
2009-03-11 15:00:40 +00:00
|
|
|
int x = (v->x_pos & ~15) + (rd.x & 15);
|
|
|
|
int y = (v->y_pos & ~15) + (rd.y & 15);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2009-03-11 15:00:40 +00:00
|
|
|
Direction new_dir = RoadVehGetSlidingDirection(v, x, y);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2011-01-19 18:44:13 +00:00
|
|
|
if (v->IsFrontEngine() && !IsInsideMM(v->state, RVSB_IN_ROAD_STOP, RVSB_IN_ROAD_STOP_END)) {
|
2007-02-12 22:46:10 +00:00
|
|
|
/* Vehicle is not in a road stop.
|
|
|
|
* Check for another vehicle to overtake */
|
2009-05-22 20:18:45 +00:00
|
|
|
RoadVehicle *u = RoadVehFindCloseTo(v, x, y, new_dir);
|
2006-03-03 22:21:29 +00:00
|
|
|
|
|
|
|
if (u != NULL) {
|
2007-10-31 22:15:19 +00:00
|
|
|
u = u->First();
|
2007-02-12 22:46:10 +00:00
|
|
|
/* There is a vehicle in front overtake it if possible */
|
2009-05-22 20:22:20 +00:00
|
|
|
if (v->overtaking == 0) RoadVehCheckOvertake(v, u);
|
|
|
|
if (v->overtaking == 0) v->cur_speed = u->cur_speed;
|
2009-12-04 20:52:19 +00:00
|
|
|
|
|
|
|
/* In case an RV is stopped in a road stop, why not try to load? */
|
|
|
|
if (v->cur_speed == 0 && IsInsideMM(v->state, RVSB_IN_DT_ROAD_STOP, RVSB_IN_DT_ROAD_STOP_END) &&
|
|
|
|
v->current_order.ShouldStopAtStation(v, GetStationIndex(v->tile)) &&
|
2015-08-06 21:24:24 +00:00
|
|
|
IsInfraTileUsageAllowed(VEH_ROAD, v->owner, v->tile) && !v->current_order.IsType(OT_LEAVESTATION) &&
|
2009-12-04 20:52:19 +00:00
|
|
|
GetRoadStopType(v->tile) == (v->IsBus() ? ROADSTOP_BUS : ROADSTOP_TRUCK)) {
|
|
|
|
Station *st = Station::GetByTile(v->tile);
|
|
|
|
v->last_station_visited = st->index;
|
|
|
|
RoadVehArrivesAt(v, st);
|
|
|
|
v->BeginLoading();
|
|
|
|
}
|
2007-06-11 14:00:16 +00:00
|
|
|
return false;
|
2006-03-03 22:21:29 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-03-11 15:00:40 +00:00
|
|
|
Direction old_dir = v->direction;
|
2004-08-09 17:04:08 +00:00
|
|
|
if (new_dir != old_dir) {
|
|
|
|
v->direction = new_dir;
|
2010-03-06 12:56:57 +00:00
|
|
|
if (_settings_game.vehicle.roadveh_acceleration_model == AM_ORIGINAL) v->cur_speed -= v->cur_speed >> 2;
|
2013-12-08 21:22:03 +00:00
|
|
|
|
2013-12-22 11:55:07 +00:00
|
|
|
/* Delay the vehicle in curves by making it require one additional frame per turning direction (two in total).
|
2013-12-08 21:22:03 +00:00
|
|
|
* A vehicle has to spend at least 9 frames on a tile, so the following articulated part can follow.
|
|
|
|
* (The following part may only be one tile behind, and the front part is moved before the following ones.)
|
2013-12-22 11:55:07 +00:00
|
|
|
* The short (inner) curve has 8 frames, this elongates it to 10. */
|
|
|
|
v->UpdateInclination(false, true);
|
|
|
|
return true;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-02-14 16:37:16 +00:00
|
|
|
/* If the vehicle is in a normal road stop and the frame equals the stop frame OR
|
|
|
|
* if the vehicle is in a drive-through road stop and this is the destination station
|
|
|
|
* and it's the correct type of stop (bus or truck) and the frame equals the stop frame...
|
|
|
|
* (the station test and stop type test ensure that other vehicles, using the road stop as
|
|
|
|
* a through route, do not stop) */
|
2011-01-19 18:44:13 +00:00
|
|
|
if (v->IsFrontEngine() && ((IsInsideMM(v->state, RVSB_IN_ROAD_STOP, RVSB_IN_ROAD_STOP_END) &&
|
2010-11-20 09:09:57 +00:00
|
|
|
_road_stop_stop_frame[v->state - RVSB_IN_ROAD_STOP + (_settings_game.vehicle.road_side << RVS_DRIVE_SIDE)] == v->frame) ||
|
2009-05-22 20:22:20 +00:00
|
|
|
(IsInsideMM(v->state, RVSB_IN_DT_ROAD_STOP, RVSB_IN_DT_ROAD_STOP_END) &&
|
2008-04-13 16:54:19 +00:00
|
|
|
v->current_order.ShouldStopAtStation(v, GetStationIndex(v->tile)) &&
|
2015-08-06 21:24:24 +00:00
|
|
|
IsInfraTileUsageAllowed(VEH_ROAD, v->owner, v->tile) &&
|
2009-12-02 17:37:02 +00:00
|
|
|
GetRoadStopType(v->tile) == (v->IsBus() ? ROADSTOP_BUS : ROADSTOP_TRUCK) &&
|
2009-05-22 20:22:20 +00:00
|
|
|
v->frame == RVC_DRIVE_THROUGH_STOP_FRAME))) {
|
2007-02-14 16:37:16 +00:00
|
|
|
|
2009-06-26 10:45:20 +00:00
|
|
|
RoadStop *rs = RoadStop::GetByTile(v->tile, GetRoadStopType(v->tile));
|
2009-06-24 17:39:54 +00:00
|
|
|
Station *st = Station::GetByTile(v->tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-12 22:46:10 +00:00
|
|
|
/* Vehicle is at the stop position (at a bay) in a road stop.
|
|
|
|
* Note, if vehicle is loading/unloading it has already been handled,
|
|
|
|
* so if we get here the vehicle has just arrived or is just ready to leave. */
|
2010-11-20 09:09:57 +00:00
|
|
|
if (!HasBit(v->state, RVS_ENTERED_STOP)) {
|
2007-02-12 22:46:10 +00:00
|
|
|
/* Vehicle has arrived at a bay in a road stop */
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-02-14 16:37:16 +00:00
|
|
|
if (IsDriveThroughStopTile(v->tile)) {
|
2018-04-29 11:50:52 +00:00
|
|
|
TileIndex next_tile = TileAddByDir(v->tile, v->direction);
|
2007-02-14 16:37:16 +00:00
|
|
|
|
2009-12-04 21:01:28 +00:00
|
|
|
/* Check if next inline bay is free and has compatible road. */
|
|
|
|
if (RoadStop::IsDriveThroughRoadStopContinuation(v->tile, next_tile) && (GetRoadTypes(next_tile) & v->compatible_roadtypes) != 0) {
|
2009-12-04 20:52:19 +00:00
|
|
|
v->frame++;
|
2010-03-06 12:55:56 +00:00
|
|
|
v->x_pos = x;
|
|
|
|
v->y_pos = y;
|
2014-09-20 15:31:26 +00:00
|
|
|
v->UpdatePosition();
|
2010-03-06 12:55:56 +00:00
|
|
|
RoadZPosAffectSpeed(v, v->UpdateInclination(true, false));
|
2009-12-04 20:52:19 +00:00
|
|
|
return true;
|
2007-02-14 16:37:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-13 00:25:42 +00:00
|
|
|
rs->SetEntranceBusy(false);
|
2010-11-20 09:09:57 +00:00
|
|
|
SetBit(v->state, RVS_ENTERED_STOP);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-04-13 16:54:19 +00:00
|
|
|
v->last_station_visited = st->index;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-06-30 15:13:40 +00:00
|
|
|
if (IsDriveThroughStopTile(v->tile) || (v->current_order.IsType(OT_GOTO_STATION) && v->current_order.GetDestination() == st->index)) {
|
2008-04-13 16:54:19 +00:00
|
|
|
RoadVehArrivesAt(v, st);
|
|
|
|
v->BeginLoading();
|
2008-06-30 15:13:40 +00:00
|
|
|
return false;
|
2008-04-13 16:54:19 +00:00
|
|
|
}
|
2008-06-30 15:13:40 +00:00
|
|
|
} else {
|
|
|
|
/* Vehicle is ready to leave a bay in a road stop */
|
2007-02-13 00:25:42 +00:00
|
|
|
if (rs->IsEntranceBusy()) {
|
2007-02-14 10:33:36 +00:00
|
|
|
/* Road stop entrance is busy, so wait as there is nowhere else to go */
|
2004-08-09 17:04:08 +00:00
|
|
|
v->cur_speed = 0;
|
2007-06-11 14:00:16 +00:00
|
|
|
return false;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2010-11-20 09:09:57 +00:00
|
|
|
if (v->current_order.IsType(OT_LEAVESTATION)) v->current_order.Free();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2007-02-14 10:33:36 +00:00
|
|
|
|
2007-02-14 16:37:16 +00:00
|
|
|
if (IsStandardRoadStopTile(v->tile)) rs->SetEntranceBusy(true);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
StartRoadVehSound(v);
|
2011-12-16 16:58:55 +00:00
|
|
|
SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-02-12 22:46:10 +00:00
|
|
|
/* Check tile position conditions - i.e. stop position in depot,
|
|
|
|
* entry onto bridge or into tunnel */
|
2009-03-11 15:00:40 +00:00
|
|
|
uint32 r = VehicleEnterTile(v, v->tile, x, y);
|
2007-11-19 21:02:30 +00:00
|
|
|
if (HasBit(r, VETS_CANNOT_ENTER)) {
|
2004-08-09 17:04:08 +00:00
|
|
|
v->cur_speed = 0;
|
2007-06-11 14:00:16 +00:00
|
|
|
return false;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-10-28 23:34:42 +00:00
|
|
|
if (v->current_order.IsType(OT_LEAVESTATION) && IsDriveThroughStopTile(v->tile)) {
|
|
|
|
v->current_order.Free();
|
|
|
|
}
|
2008-04-13 16:54:19 +00:00
|
|
|
|
2007-02-12 22:46:10 +00:00
|
|
|
/* Move to next frame unless vehicle arrived at a stop position
|
|
|
|
* in a depot or entered a tunnel/bridge */
|
2009-05-22 20:22:20 +00:00
|
|
|
if (!HasBit(r, VETS_ENTERED_WORMHOLE)) v->frame++;
|
2010-03-06 12:55:56 +00:00
|
|
|
v->x_pos = x;
|
|
|
|
v->y_pos = y;
|
2014-09-20 15:31:26 +00:00
|
|
|
v->UpdatePosition();
|
2017-03-05 18:05:04 +00:00
|
|
|
RoadZPosAffectSpeed(v, v->UpdateInclination(false, true, v->state == RVSB_WORMHOLE));
|
2007-06-11 14:00:16 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-05-22 20:18:45 +00:00
|
|
|
static bool RoadVehController(RoadVehicle *v)
|
2007-06-11 14:00:16 +00:00
|
|
|
{
|
|
|
|
/* decrease counters */
|
2007-06-20 19:17:22 +00:00
|
|
|
v->current_order_time++;
|
2009-05-22 20:22:20 +00:00
|
|
|
if (v->reverse_ctr != 0) v->reverse_ctr--;
|
2007-06-11 14:00:16 +00:00
|
|
|
|
|
|
|
/* handle crashed */
|
2010-07-02 19:54:29 +00:00
|
|
|
if (v->vehstatus & VS_CRASHED || RoadVehCheckTrainCrash(v)) {
|
2009-05-22 13:53:14 +00:00
|
|
|
return RoadVehIsCrashed(v);
|
2007-06-11 14:00:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* road vehicle has broken down? */
|
2010-08-28 14:14:37 +00:00
|
|
|
if (v->HandleBreakdown()) return true;
|
2017-05-08 20:54:15 +00:00
|
|
|
if (v->IsRoadVehicleStopped()) {
|
|
|
|
v->cur_speed = 0;
|
|
|
|
v->SetLastSpeed();
|
|
|
|
return true;
|
|
|
|
}
|
2007-06-11 14:00:16 +00:00
|
|
|
|
2008-04-05 10:55:50 +00:00
|
|
|
ProcessOrders(v);
|
2007-06-11 14:00:16 +00:00
|
|
|
v->HandleLoading();
|
|
|
|
|
2009-05-22 13:53:14 +00:00
|
|
|
if (v->current_order.IsType(OT_LOADING)) return true;
|
2007-06-11 14:00:16 +00:00
|
|
|
|
2015-08-03 00:06:05 +00:00
|
|
|
v->HandleWaiting(false);
|
|
|
|
if (v->current_order.IsType(OT_WAITING)) return true;
|
|
|
|
|
2009-05-22 13:53:14 +00:00
|
|
|
if (v->IsInDepot() && RoadVehLeaveDepot(v, true)) return true;
|
2007-06-11 14:00:16 +00:00
|
|
|
|
2010-11-18 14:17:55 +00:00
|
|
|
v->ShowVisualEffect();
|
|
|
|
|
2009-01-06 14:45:38 +00:00
|
|
|
/* Check how far the vehicle needs to proceed */
|
2011-01-26 17:35:03 +00:00
|
|
|
int j = v->UpdateSpeed();
|
2009-01-06 14:45:38 +00:00
|
|
|
|
2010-07-04 13:07:47 +00:00
|
|
|
int adv_spd = v->GetAdvanceDistance();
|
2010-05-03 19:55:03 +00:00
|
|
|
bool blocked = false;
|
2009-01-06 14:45:38 +00:00
|
|
|
while (j >= adv_spd) {
|
|
|
|
j -= adv_spd;
|
2007-06-11 14:00:16 +00:00
|
|
|
|
2009-05-22 20:18:45 +00:00
|
|
|
RoadVehicle *u = v;
|
|
|
|
for (RoadVehicle *prev = NULL; u != NULL; prev = u, u = u->Next()) {
|
2010-05-03 19:55:03 +00:00
|
|
|
if (!IndividualRoadVehicleController(u, prev)) {
|
|
|
|
blocked = true;
|
|
|
|
break;
|
|
|
|
}
|
2009-01-06 14:45:38 +00:00
|
|
|
}
|
2010-05-03 19:55:03 +00:00
|
|
|
if (blocked) break;
|
2009-01-06 14:45:38 +00:00
|
|
|
|
2010-07-04 13:07:47 +00:00
|
|
|
/* Determine distance to next map position */
|
|
|
|
adv_spd = v->GetAdvanceDistance();
|
2009-01-06 14:45:38 +00:00
|
|
|
|
|
|
|
/* Test for a collision, but only if another movement will occur. */
|
|
|
|
if (j >= adv_spd && RoadVehCheckTrainCrash(v)) break;
|
2007-06-11 14:00:16 +00:00
|
|
|
}
|
2009-01-06 14:45:38 +00:00
|
|
|
|
2011-01-26 17:31:44 +00:00
|
|
|
v->SetLastSpeed();
|
|
|
|
|
2009-05-22 20:18:45 +00:00
|
|
|
for (RoadVehicle *u = v; u != NULL; u = u->Next()) {
|
2015-07-29 23:32:55 +00:00
|
|
|
if (!(u->IsDrawn())) continue;
|
2009-03-11 23:23:08 +00:00
|
|
|
|
2009-08-08 18:45:12 +00:00
|
|
|
u->UpdateViewport(false, false);
|
2009-03-11 23:23:08 +00:00
|
|
|
}
|
|
|
|
|
2010-05-03 19:55:03 +00:00
|
|
|
/* If movement is blocked, set 'progress' to its maximum, so the roadvehicle does
|
|
|
|
* not accelerate again before it can actually move. I.e. make sure it tries to advance again
|
|
|
|
* on next tick to discover whether it is still blocked. */
|
|
|
|
if (v->progress == 0) v->progress = blocked ? adv_spd - 1 : j;
|
2009-05-22 13:53:14 +00:00
|
|
|
|
|
|
|
return true;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-11-08 13:02:05 +00:00
|
|
|
Money RoadVehicle::GetRunningCost() const
|
|
|
|
{
|
2011-11-01 00:21:08 +00:00
|
|
|
const Engine *e = this->GetEngine();
|
2009-11-24 13:12:34 +00:00
|
|
|
if (e->u.road.running_cost_class == INVALID_PRICE) return 0;
|
2009-11-08 13:02:05 +00:00
|
|
|
|
2009-11-24 13:12:34 +00:00
|
|
|
uint cost_factor = GetVehicleProperty(this, PROP_ROADVEH_RUNNING_COST_FACTOR, e->u.road.running_cost);
|
2009-11-08 13:02:05 +00:00
|
|
|
if (cost_factor == 0) return 0;
|
|
|
|
|
2011-11-01 00:23:41 +00:00
|
|
|
return GetPrice(e->u.road.running_cost_class, cost_factor, e->GetGRF());
|
2009-11-08 13:02:05 +00:00
|
|
|
}
|
|
|
|
|
2009-05-22 13:53:14 +00:00
|
|
|
bool RoadVehicle::Tick()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2018-07-19 19:17:07 +00:00
|
|
|
PerformanceAccumulator framerate(PFE_GL_ROADVEHS);
|
|
|
|
|
2013-05-06 13:45:58 +00:00
|
|
|
this->tick_counter++;
|
|
|
|
|
2011-01-19 18:44:13 +00:00
|
|
|
if (this->IsFrontEngine()) {
|
2017-05-08 20:54:15 +00:00
|
|
|
if (!(this->IsRoadVehicleStopped())) this->running_ticks++;
|
2009-05-22 13:53:14 +00:00
|
|
|
return RoadVehController(this);
|
2008-02-13 19:24:40 +00:00
|
|
|
}
|
2009-05-22 13:53:14 +00:00
|
|
|
|
|
|
|
return true;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-05-22 20:18:45 +00:00
|
|
|
static void CheckIfRoadVehNeedsService(RoadVehicle *v)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-03-28 20:41:35 +00:00
|
|
|
/* If we already got a slot at a stop, use that FIRST, and go to a depot later */
|
2009-12-02 18:18:56 +00:00
|
|
|
if (Company::Get(v->owner)->settings.vehicle.servint_roadveh == 0 || !v->NeedsAutomaticServicing()) return;
|
2012-07-07 15:39:46 +00:00
|
|
|
if (v->IsChainInDepot()) {
|
2006-09-03 11:49:38 +00:00
|
|
|
VehicleServiceInDepot(v);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-12-13 10:48:44 +00:00
|
|
|
uint max_penalty;
|
|
|
|
switch (_settings_game.pf.pathfinder_for_roadvehs) {
|
|
|
|
case VPF_NPF: max_penalty = _settings_game.pf.npf.maximum_go_to_depot_penalty; break;
|
|
|
|
case VPF_YAPF: max_penalty = _settings_game.pf.yapf.maximum_go_to_depot_penalty; break;
|
|
|
|
default: NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
|
|
|
FindDepotData rfdd = FindClosestRoadDepot(v, max_penalty);
|
2009-08-31 19:16:18 +00:00
|
|
|
/* Only go to the depot if it is not too far out of our way. */
|
2009-12-13 10:48:44 +00:00
|
|
|
if (rfdd.best_length == UINT_MAX || rfdd.best_length > max_penalty) {
|
2008-04-05 23:36:54 +00:00
|
|
|
if (v->current_order.IsType(OT_GOTO_DEPOT)) {
|
2009-08-31 19:16:18 +00:00
|
|
|
/* If we were already heading for a depot but it has
|
|
|
|
* suddenly moved farther away, we continue our normal
|
|
|
|
* schedule? */
|
2008-04-05 23:36:54 +00:00
|
|
|
v->current_order.MakeDummy();
|
2011-12-16 16:58:55 +00:00
|
|
|
SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-09-10 14:37:55 +00:00
|
|
|
DepotID depot = GetDepotIndex(rfdd.tile);
|
2009-08-31 19:16:18 +00:00
|
|
|
|
2008-04-05 23:36:54 +00:00
|
|
|
if (v->current_order.IsType(OT_GOTO_DEPOT) &&
|
2008-04-07 08:59:04 +00:00
|
|
|
v->current_order.GetNonStopType() & ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS &&
|
2007-11-25 15:35:25 +00:00
|
|
|
!Chance16(1, 20)) {
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
2006-02-18 14:41:24 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2011-05-18 12:19:58 +00:00
|
|
|
SetBit(v->gv_flags, GVF_SUPPRESS_IMPLICIT_ORDERS);
|
2009-09-10 14:37:55 +00:00
|
|
|
v->current_order.MakeGoToDepot(depot, ODTFB_SERVICE);
|
2009-08-31 19:16:18 +00:00
|
|
|
v->dest_tile = rfdd.tile;
|
2011-12-16 16:58:55 +00:00
|
|
|
SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-02-01 22:02:14 +00:00
|
|
|
void RoadVehicle::OnNewDay()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2011-08-22 21:33:58 +00:00
|
|
|
AgeVehicle(this);
|
|
|
|
|
2011-01-19 18:44:13 +00:00
|
|
|
if (!this->IsFrontEngine()) return;
|
2007-06-11 14:00:16 +00:00
|
|
|
|
2008-02-01 22:02:14 +00:00
|
|
|
if ((++this->day_counter & 7) == 0) DecreaseVehicleValue(this);
|
2009-05-22 20:22:20 +00:00
|
|
|
if (this->blocked_ctr == 0) CheckVehicleBreakdown(this);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-02-01 22:02:14 +00:00
|
|
|
CheckIfRoadVehNeedsService(this);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-02-01 22:02:14 +00:00
|
|
|
CheckOrders(this);
|
2004-08-11 10:15:38 +00:00
|
|
|
|
2008-02-13 19:24:40 +00:00
|
|
|
if (this->running_ticks == 0) return;
|
|
|
|
|
2009-01-22 21:33:08 +00:00
|
|
|
CommandCost cost(EXPENSES_ROADVEH_RUN, this->GetRunningCost() * this->running_ticks / (DAYS_IN_YEAR * DAY_TICKS));
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-02-13 19:24:40 +00:00
|
|
|
this->profit_this_year -= cost.GetCost();
|
|
|
|
this->running_ticks = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
SubtractMoneyFromCompanyFract(this->owner, cost);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowDirty(WC_VEHICLE_DETAILS, this->index);
|
|
|
|
SetWindowClassesDirty(WC_ROADVEH_LIST);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-05-22 18:17:20 +00:00
|
|
|
Trackdir RoadVehicle::GetVehicleTrackdir() const
|
|
|
|
{
|
|
|
|
if (this->vehstatus & VS_CRASHED) return INVALID_TRACKDIR;
|
|
|
|
|
|
|
|
if (this->IsInDepot()) {
|
|
|
|
/* We'll assume the road vehicle is facing outwards */
|
|
|
|
return DiagDirToDiagTrackdir(GetRoadDepotDirection(this->tile));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsStandardRoadStopTile(this->tile)) {
|
|
|
|
/* We'll assume the road vehicle is facing outwards */
|
|
|
|
return DiagDirToDiagTrackdir(GetRoadStopDir(this->tile)); // Road vehicle in a station
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Drive through road stops / wormholes (tunnels) */
|
2009-05-22 20:22:20 +00:00
|
|
|
if (this->state > RVSB_TRACKDIR_MASK) return DiagDirToDiagTrackdir(DirToDiagDir(this->direction));
|
2009-05-22 18:17:20 +00:00
|
|
|
|
|
|
|
/* If vehicle's state is a valid track direction (vehicle is not turning around) return it,
|
|
|
|
* otherwise transform it into a valid track direction */
|
2009-05-22 20:22:20 +00:00
|
|
|
return (Trackdir)((IsReversingRoadTrackdir((Trackdir)this->state)) ? (this->state - 6) : this->state);
|
2009-05-22 18:17:20 +00:00
|
|
|
}
|