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"
|
2005-05-02 23:59:11 +00:00
|
|
|
#include "vehicle_gui.h"
|
2007-06-11 14:00:16 +00:00
|
|
|
#include "articulated_vehicles.h"
|
2006-02-03 12:55:21 +00:00
|
|
|
#include "newgrf_engine.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-25 11:26:07 +00:00
|
|
|
#include "functions.h"
|
|
|
|
#include "window_func.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"
|
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"
|
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
|
|
|
|
|
|
|
static const uint16 _roadveh_images[63] = {
|
|
|
|
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
|
|
|
|
};
|
|
|
|
|
|
|
|
static const uint16 _roadveh_full_adder[63] = {
|
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
|
|
|
};
|
|
|
|
|
2007-02-13 20:57:02 +00:00
|
|
|
/** 'Convert' the DiagDirection where a road vehicle enters to the trackdirs it can drive onto */
|
|
|
|
static const TrackdirBits _road_enter_dir_to_reachable_trackdirs[DIAGDIR_END] = {
|
|
|
|
TRACKDIR_BIT_LEFT_N | TRACKDIR_BIT_LOWER_E | TRACKDIR_BIT_X_NE, // Enter from north east
|
|
|
|
TRACKDIR_BIT_LEFT_S | TRACKDIR_BIT_UPPER_E | TRACKDIR_BIT_Y_SE, // Enter from south east
|
|
|
|
TRACKDIR_BIT_UPPER_W | TRACKDIR_BIT_X_SW | TRACKDIR_BIT_RIGHT_S, // Enter from south west
|
|
|
|
TRACKDIR_BIT_RIGHT_N | TRACKDIR_BIT_LOWER_W | TRACKDIR_BIT_Y_NW // Enter from north west
|
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
|
|
|
};
|
|
|
|
|
2007-02-13 22:27:27 +00:00
|
|
|
/** Converts the exit direction of a depot to trackdir the vehicle is going to drive to */
|
|
|
|
static const Trackdir _roadveh_depot_exit_trackdir[DIAGDIR_END] = {
|
|
|
|
TRACKDIR_X_NE, TRACKDIR_Y_SE, TRACKDIR_X_SW, TRACKDIR_Y_NW
|
|
|
|
};
|
|
|
|
|
2009-12-02 17:37:02 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether a roadvehicle is a bus
|
|
|
|
* @return true if bus
|
|
|
|
*/
|
|
|
|
bool RoadVehicle::IsBus() const
|
|
|
|
{
|
|
|
|
assert(this->IsRoadVehFront());
|
|
|
|
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) {
|
|
|
|
offset->x = reference_width / 2;
|
|
|
|
offset->y = 0;
|
|
|
|
}
|
|
|
|
return this->rcache.cached_veh_length * reference_width / 8;
|
|
|
|
}
|
|
|
|
|
2008-04-21 20:50:58 +00:00
|
|
|
static SpriteID GetRoadVehIcon(EngineID engine)
|
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)) {
|
|
|
|
SpriteID sprite = GetCustomVehicleIcon(engine, DIR_W);
|
|
|
|
if (sprite != 0) return sprite;
|
|
|
|
|
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
|
|
|
|
2009-05-26 08:53:16 +00:00
|
|
|
return DIR_W + _roadveh_images[spritenum];
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-04-21 20:50:58 +00:00
|
|
|
SpriteID RoadVehicle::GetImage(Direction direction) const
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-04-21 20:50:58 +00:00
|
|
|
uint8 spritenum = this->spritenum;
|
|
|
|
SpriteID sprite;
|
2004-11-13 18:01:33 +00:00
|
|
|
|
|
|
|
if (is_custom_sprite(spritenum)) {
|
2008-04-21 20:50:58 +00:00
|
|
|
sprite = GetCustomVehicleSprite(this, (Direction)(direction + 4 * IS_CUSTOM_SECONDHEAD_SPRITE(spritenum)));
|
|
|
|
if (sprite != 0) return sprite;
|
2004-11-13 18:01:33 +00:00
|
|
|
|
2009-06-16 13:31:06 +00:00
|
|
|
spritenum = Engine::Get(this->engine_type)->original_image_index;
|
2004-11-13 18:01:33 +00:00
|
|
|
}
|
2008-04-21 20:50:58 +00:00
|
|
|
|
|
|
|
sprite = direction + _roadveh_images[spritenum];
|
|
|
|
|
|
|
|
if (this->cargo.Count() >= this->cargo_cap / 2U) sprite += _roadveh_full_adder[spritenum];
|
|
|
|
|
|
|
|
return sprite;
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*/
|
2010-01-21 01:38:13 +00:00
|
|
|
void DrawRoadVehEngine(int left, int right, int preferred_x, int y, EngineID engine, PaletteID pal)
|
2008-04-21 20:50:58 +00:00
|
|
|
{
|
2009-11-16 17:58:46 +00:00
|
|
|
SpriteID sprite = GetRoadVehIcon(engine);
|
|
|
|
const Sprite *real_sprite = GetSprite(sprite, ST_NORMAL);
|
|
|
|
preferred_x = Clamp(preferred_x, left - real_sprite->x_offs, right - real_sprite->width - real_sprite->x_offs);
|
|
|
|
DrawSprite(sprite, pal, preferred_x, y);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-10-20 19:55:09 +00:00
|
|
|
static uint GetRoadVehLength(const RoadVehicle *v)
|
2007-06-11 14:00:16 +00:00
|
|
|
{
|
2009-10-20 19:55:09 +00:00
|
|
|
uint length = 8;
|
2007-06-11 14:00:16 +00:00
|
|
|
|
2007-07-09 19:38:12 +00:00
|
|
|
uint16 veh_len = GetVehicleCallback(CBID_VEHICLE_LENGTH, 0, 0, v->engine_type, v);
|
2007-06-11 14:00:16 +00:00
|
|
|
if (veh_len != CALLBACK_FAILED) {
|
2007-11-19 18:38:10 +00:00
|
|
|
length -= Clamp(veh_len, 0, 7);
|
2007-06-11 14:00:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2009-05-22 20:18:45 +00:00
|
|
|
void RoadVehUpdateCache(RoadVehicle *v)
|
2007-06-11 14:00:16 +00:00
|
|
|
{
|
|
|
|
assert(v->type == VEH_ROAD);
|
2009-07-02 08:59:27 +00:00
|
|
|
assert(v->IsRoadVehFront());
|
2007-06-11 14:00:16 +00:00
|
|
|
|
2009-05-31 12:03:14 +00:00
|
|
|
v->InvalidateNewGRFCacheOfChain();
|
|
|
|
|
2009-12-18 21:34:06 +00:00
|
|
|
v->rcache.cached_total_length = 0;
|
|
|
|
|
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' */
|
2009-05-23 09:10:56 +00:00
|
|
|
u->rcache.first_engine = (v == u) ? INVALID_ENGINE : v->engine_type;
|
2007-06-11 14:00:16 +00:00
|
|
|
|
|
|
|
/* Update the length of the vehicle. */
|
2009-05-23 09:10:56 +00:00
|
|
|
u->rcache.cached_veh_length = GetRoadVehLength(u);
|
2009-12-18 21:34:06 +00:00
|
|
|
v->rcache.cached_total_length += u->rcache.cached_veh_length;
|
2009-01-27 19:44:36 +00:00
|
|
|
|
|
|
|
/* Invalidate the vehicle colour map */
|
2009-02-09 02:57:15 +00:00
|
|
|
u->colourmap = PAL_NONE;
|
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;
|
2007-04-18 22:10:36 +00:00
|
|
|
v->z_pos = GetSlopeZ(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;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-02-02 16:16:43 +00:00
|
|
|
v->last_station_visited = INVALID_STATION;
|
2004-12-03 21:57:05 +00:00
|
|
|
v->max_speed = rvi->max_speed;
|
2010-08-17 23:15:55 +00:00
|
|
|
v->engine_type = e->index;
|
2009-10-03 14:46:48 +00:00
|
|
|
v->rcache.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;
|
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
|
|
|
|
2009-05-26 21:59:49 +00:00
|
|
|
v->service_interval = 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
|
|
|
|
2009-05-13 21:45:56 +00:00
|
|
|
v->cur_image = SPR_IMG_QUERY;
|
2005-12-28 22:29:59 +00:00
|
|
|
v->random_bits = VehicleRandomBits();
|
2009-07-02 08:59:27 +00:00
|
|
|
v->SetRoadVehFront();
|
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);
|
2009-05-23 09:10:56 +00:00
|
|
|
v->rcache.cached_veh_length = 8;
|
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);
|
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()) {
|
2009-10-28 18:31:16 +00:00
|
|
|
u->cargo_cap = GetVehicleCapacity(u);
|
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
|
|
|
|
2009-03-11 20:43:14 +00:00
|
|
|
VehicleMove(v, false);
|
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
|
|
|
}
|
|
|
|
|
2008-08-17 11:12:56 +00:00
|
|
|
bool RoadVehicle::IsStoppedInDepot() const
|
2007-06-11 14:00:16 +00:00
|
|
|
{
|
2008-08-17 11:12:56 +00:00
|
|
|
TileIndex tile = this->tile;
|
2007-06-11 14:00:16 +00:00
|
|
|
|
2008-04-17 18:24:45 +00:00
|
|
|
if (!IsRoadDepotTile(tile)) return false;
|
2009-07-02 08:59:27 +00:00
|
|
|
if (this->IsRoadVehFront() && !(this->vehstatus & VS_STOPPED)) return false;
|
2007-06-11 14:00:16 +00:00
|
|
|
|
2009-05-22 20:18:45 +00:00
|
|
|
for (const RoadVehicle *v = this; v != NULL; v = v->Next()) {
|
2009-05-22 20:22:20 +00:00
|
|
|
if (v->state != RVSB_IN_DEPOT || v->tile != tile) return false;
|
2007-06-11 14:00:16 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
/**
|
|
|
|
* Send a road vehicle to the depot.
|
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 send to the depot
|
2006-08-29 23:39:57 +00:00
|
|
|
* @param p2 various bitmasked elements
|
2006-09-01 10:24:15 +00:00
|
|
|
* - p2 bit 0-3 - DEPOT_ flags (see vehicle.h)
|
|
|
|
* - p2 bit 8-10 - VLW flag (for mass goto depot)
|
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 CmdSendRoadVehToDepot(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-09-01 10:24:15 +00:00
|
|
|
if (p2 & DEPOT_MASS_SEND) {
|
|
|
|
/* Mass goto depot requested */
|
2006-09-04 15:16:58 +00:00
|
|
|
if (!ValidVLWFlags(p2 & VLW_MASK)) return CMD_ERROR;
|
2008-09-30 20:39:50 +00:00
|
|
|
return SendAllVehiclesToDepot(VEH_ROAD, flags, p2 & DEPOT_SERVICE, _current_company, (p2 & VLW_MASK), p1);
|
2006-08-30 21:39:01 +00:00
|
|
|
}
|
|
|
|
|
2009-05-26 22:45:48 +00:00
|
|
|
RoadVehicle *v = RoadVehicle::GetIfValid(p1);
|
|
|
|
if (v == NULL) return CMD_ERROR;
|
2007-04-20 11:39:01 +00:00
|
|
|
|
2008-04-11 08:40:10 +00:00
|
|
|
return v->SendToDepot(flags, (DepotCommand)(p2 & DEPOT_COMMAND_MASK));
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
CommandCost ret = CheckOwnership(v->owner);
|
|
|
|
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) ||
|
2004-08-09 17:04:08 +00:00
|
|
|
v->breakdown_ctr != 0 ||
|
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()) {
|
2009-08-08 18:45:12 +00:00
|
|
|
v->UpdateViewport(false, 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
|
|
|
}
|
|
|
|
|
2007-05-01 16:35:14 +00:00
|
|
|
void RoadVehicle::UpdateDeltaXY(Direction direction)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-05-01 16:35:14 +00:00
|
|
|
#define MKIT(a, b, c, d) ((a & 0xFF) << 24) | ((b & 0xFF) << 16) | ((c & 0xFF) << 8) | ((d & 0xFF) << 0)
|
2004-08-09 17:04:08 +00:00
|
|
|
static const uint32 _delta_xy_table[8] = {
|
|
|
|
MKIT(3, 3, -1, -1),
|
|
|
|
MKIT(3, 7, -1, -3),
|
|
|
|
MKIT(3, 3, -1, -1),
|
|
|
|
MKIT(7, 3, -3, -1),
|
|
|
|
MKIT(3, 3, -1, -1),
|
|
|
|
MKIT(3, 7, -1, -3),
|
|
|
|
MKIT(3, 3, -1, -1),
|
|
|
|
MKIT(7, 3, -3, -1),
|
|
|
|
};
|
|
|
|
#undef MKIT
|
2007-05-01 16:35:14 +00:00
|
|
|
|
|
|
|
uint32 x = _delta_xy_table[direction];
|
|
|
|
this->x_offs = GB(x, 0, 8);
|
|
|
|
this->y_offs = GB(x, 8, 8);
|
2008-04-01 14:03:20 +00:00
|
|
|
this->x_extent = GB(x, 16, 8);
|
|
|
|
this->y_extent = GB(x, 24, 8);
|
|
|
|
this->z_extent = 6;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
FORCEINLINE int RoadVehicle::GetCurrentMaxSpeed() const
|
|
|
|
{
|
|
|
|
if (_settings_game.vehicle.roadveh_acceleration_model == AM_ORIGINAL) return this->max_speed;
|
|
|
|
|
|
|
|
int max_speed = this->max_speed;
|
|
|
|
|
|
|
|
/* Limit speed to 50% while reversing, 75% in curves. */
|
|
|
|
for (const RoadVehicle *u = this; u != NULL; u = u->Next()) {
|
|
|
|
if (this->state <= RVSB_TRACKDIR_MASK && IsReversingRoadTrackdir((Trackdir)this->state)) {
|
|
|
|
max_speed = this->max_speed / 2;
|
|
|
|
break;
|
|
|
|
} else if ((u->direction & 1) == 0) {
|
|
|
|
max_speed = this->max_speed * 3 / 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return max_speed;
|
|
|
|
}
|
|
|
|
|
2009-05-22 20:18:45 +00:00
|
|
|
static void DeleteLastRoadVeh(RoadVehicle *v)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
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);
|
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]);
|
2010-03-06 12:55:56 +00:00
|
|
|
v->UpdateInclination(false, true);
|
2007-08-30 13:03:56 +00:00
|
|
|
} while ((v = v->Next()) != NULL);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2008-08-01 15:07:31 +00:00
|
|
|
static Vehicle *EnumCheckRoadVehCrashTrain(Vehicle *v, void *data)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-08-01 15:07:31 +00:00
|
|
|
const Vehicle *u = (Vehicle*)data;
|
2006-03-03 22:21:29 +00:00
|
|
|
|
2006-02-06 09:18:04 +00:00
|
|
|
return
|
2007-03-08 16:27:54 +00:00
|
|
|
v->type == VEH_TRAIN &&
|
2007-11-19 18:58:04 +00:00
|
|
|
abs(v->z_pos - u->z_pos) <= 6 &&
|
|
|
|
abs(v->x_pos - u->x_pos) <= 4 &&
|
|
|
|
abs(v->y_pos - u->y_pos) <= 4 ?
|
2006-02-06 09:18:04 +00:00
|
|
|
v : 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
|
|
|
{
|
2009-12-04 20:29:46 +00:00
|
|
|
uint pass = Vehicle::Crash(flooded);
|
2009-12-04 20:52:19 +00:00
|
|
|
if (this->IsRoadVehFront()) {
|
|
|
|
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
|
|
|
|
2009-02-05 17:28:37 +00:00
|
|
|
AI::NewEvent(v->owner, new AIEventVehicleCrashed(v->index, v->tile, AIEventVehicleCrashed::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,
|
2009-05-24 16:52:42 +00:00
|
|
|
NS_ACCIDENT,
|
|
|
|
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);
|
2004-12-04 09:26:39 +00:00
|
|
|
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
|
|
|
|
2008-09-07 11:23:10 +00:00
|
|
|
if (HasVehicleOnPosXY(v->x_pos, v->y_pos, u, EnumCheckRoadVehCrashTrain)) {
|
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 */
|
2009-05-09 13:37:18 +00:00
|
|
|
this->IncrementOrderIndex();
|
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
|
|
|
|
2009-06-06 16:54:22 +00:00
|
|
|
return RoadVehicle::From(rvf.best);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
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,
|
2008-09-30 20:39:50 +00:00
|
|
|
(v->owner == _local_company) ? NS_ARRIVAL_COMPANY : NS_ARRIVAL_OTHER,
|
2004-08-09 17:04:08 +00:00
|
|
|
v->index,
|
2008-09-13 10:19:51 +00:00
|
|
|
st->index
|
|
|
|
);
|
2009-01-12 17:11:45 +00:00
|
|
|
AI::NewEvent(v->owner, new AIEventStationFirstVehicle(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,
|
2008-09-30 20:39:50 +00:00
|
|
|
(v->owner == _local_company) ? NS_ARRIVAL_COMPANY : NS_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
|
|
|
);
|
2009-01-12 17:11:45 +00:00
|
|
|
AI::NewEvent(v->owner, new AIEventStationFirstVehicle(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.
|
|
|
|
*/
|
2009-05-22 20:18:45 +00:00
|
|
|
static int RoadVehAccelerate(RoadVehicle *v)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-01-06 14:45:38 +00:00
|
|
|
uint oldspeed = v->cur_speed;
|
2010-03-06 12:56:57 +00:00
|
|
|
uint accel = v->overtaking != 0 ? 256 : 0;
|
|
|
|
accel += (_settings_game.vehicle.roadveh_acceleration_model == AM_ORIGINAL) ? 256 : v->GetAcceleration();
|
2009-01-06 14:45:38 +00:00
|
|
|
uint spd = v->subspeed + accel;
|
|
|
|
|
|
|
|
v->subspeed = (uint8)spd;
|
|
|
|
|
2010-03-06 12:56:57 +00:00
|
|
|
int tempmax = v->GetCurrentMaxSpeed();
|
|
|
|
if (v->cur_speed > tempmax) {
|
2009-01-06 14:45:38 +00:00
|
|
|
tempmax = v->cur_speed - (v->cur_speed / 10) - 1;
|
|
|
|
}
|
|
|
|
|
2010-03-06 12:56:57 +00:00
|
|
|
/* Force a minimum speed of 1 km/h when realistic acceleration is on. */
|
|
|
|
int min_speed = (_settings_game.vehicle.roadveh_acceleration_model == AM_ORIGINAL) ? 0 : 4;
|
|
|
|
v->cur_speed = spd = Clamp(v->cur_speed + ((int)spd >> 8), min_speed, tempmax);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2009-01-06 14:45:38 +00:00
|
|
|
/* Apply bridge speed limit */
|
2009-05-22 20:22:20 +00:00
|
|
|
if (v->state == RVSB_WORMHOLE && !(v->vehstatus & VS_HIDDEN)) {
|
2009-01-06 14:45:38 +00:00
|
|
|
v->cur_speed = min(v->cur_speed, GetBridgeSpec(GetBridgeType(v->tile))->speed * 2);
|
2007-02-24 18:44:30 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-01-06 14:45:38 +00:00
|
|
|
/* Update statusbar only if speed has changed to save CPU time */
|
|
|
|
if (oldspeed != v->cur_speed) {
|
2008-05-29 15:13:28 +00:00
|
|
|
if (_settings_client.gui.vehicle_speed) {
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
|
2006-02-01 06:32:03 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2010-07-04 13:07:47 +00:00
|
|
|
int scaled_spd = v->GetAdvanceSpeed(spd);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-01-06 14:45:38 +00:00
|
|
|
scaled_spd += v->progress;
|
|
|
|
v->progress = 0;
|
|
|
|
return scaled_spd;
|
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
|
|
|
|
2006-02-13 21:15:00 +00:00
|
|
|
return
|
2007-12-15 23:11:18 +00:00
|
|
|
v->type == VEH_ROAD && v->First() == v && v != od->u && v != od->v ?
|
2006-02-13 21:15:00 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
if (u->max_speed >= v->max_speed &&
|
2006-04-03 14:25:32 +00:00
|
|
|
!(u->vehstatus & VS_STOPPED) &&
|
2006-02-01 06:32:03 +00:00
|
|
|
u->cur_speed != 0) {
|
|
|
|
return;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
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
|
|
|
|
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
|
|
|
|
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
|
|
|
|
2009-06-01 11:43:36 +00:00
|
|
|
if (od.u->cur_speed == 0 || (od.u->vehstatus & VS_STOPPED)) {
|
2009-05-22 20:22:20 +00:00
|
|
|
v->overtaking_ctr = 0x11;
|
|
|
|
v->overtaking = 0x10;
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
2008-02-20 14:07:45 +00:00
|
|
|
// if (CheckRoadBlockedForOvertaking(&od)) return;
|
2009-05-22 20:22:20 +00:00
|
|
|
v->overtaking_ctr = 0;
|
|
|
|
v->overtaking = 0x10;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-22 20:18:45 +00:00
|
|
|
static void RoadZPosAffectSpeed(RoadVehicle *v, byte 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;
|
2006-02-13 21:15:00 +00:00
|
|
|
if (spd <= v->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;
|
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)) {
|
2009-05-22 20:22:20 +00:00
|
|
|
if (IsRoadDepot(tile) && (!IsTileOwner(tile, v->owner) || 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
|
|
|
|
2009-07-02 09:06:15 +00:00
|
|
|
if (!IsTileOwner(tile, v->owner) || 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 */
|
|
|
|
trackdirs &= _road_enter_dir_to_reachable_trackdirs[enterdir];
|
|
|
|
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) {
|
2009-12-02 10:44:38 +00:00
|
|
|
case VPF_NPF: return_track(NPFRoadVehicleChooseTrack(v, tile, enterdir, trackdirs));
|
|
|
|
case VPF_YAPF: return_track(YapfRoadVehicleChooseTrack(v, tile, enterdir, trackdirs));
|
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
|
|
|
}
|
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
|
|
|
|
|
|
|
static const byte _road_veh_data_1[] = {
|
|
|
|
20, 20, 16, 16, 0, 0, 0, 0,
|
|
|
|
19, 19, 15, 15, 0, 0, 0, 0,
|
|
|
|
16, 16, 12, 12, 0, 0, 0, 0,
|
|
|
|
15, 15, 11, 11
|
|
|
|
};
|
|
|
|
|
2009-05-22 20:18:45 +00:00
|
|
|
static bool RoadVehLeaveDepot(RoadVehicle *v, bool first)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-06-11 14:00:16 +00:00
|
|
|
/* Don't leave if not all the 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
|
|
|
|
2007-06-11 14:00:16 +00:00
|
|
|
Trackdir tdir = _roadveh_depot_exit_trackdir[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) {
|
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;
|
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;
|
|
|
|
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];
|
|
|
|
|
2009-05-22 20:22:20 +00:00
|
|
|
if ((required & GetAnyRoadBits(tile, v->roadtype, true)) == 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
|
|
|
|
2009-02-09 21:20:05 +00:00
|
|
|
CommandCost ret = DoCommand(t, ROADTYPE_TRAM << 4 | r, 0, DC_NONE, 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
|
|
|
}
|
|
|
|
|
2009-05-22 20:18:45 +00:00
|
|
|
static bool IndividualRoadVehicleController(RoadVehicle *v, const RoadVehicle *prev)
|
2007-06-11 14:00:16 +00:00
|
|
|
{
|
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;
|
|
|
|
} else if (++v->overtaking_ctr >= 35) {
|
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
|
|
|
|
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
|
|
|
|
2009-07-02 08:59:27 +00:00
|
|
|
if (v->IsRoadVehFront()) {
|
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)) {
|
2007-02-12 22:46:10 +00:00
|
|
|
/* Vehicle has just entered a bridge or tunnel */
|
2010-03-06 12:55:56 +00:00
|
|
|
v->x_pos = gp.x;
|
|
|
|
v->y_pos = gp.y;
|
|
|
|
v->UpdateInclination(true, true);
|
2007-06-11 14:00:16 +00:00
|
|
|
return true;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
v->x_pos = gp.x;
|
|
|
|
v->y_pos = gp.y;
|
2009-03-11 20:43:14 +00:00
|
|
|
VehicleMove(v, !(v->vehstatus & VS_HIDDEN));
|
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) {
|
2007-07-24 13:03:24 +00:00
|
|
|
TileIndex tile = v->tile + TileOffsByDiagDir((DiagDirection)(rd.x & 3));
|
2007-06-11 14:00:16 +00:00
|
|
|
Trackdir dir;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-07-02 08:59:27 +00:00
|
|
|
if (v->IsRoadVehFront()) {
|
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));
|
|
|
|
} 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) {
|
2009-07-02 08:59:27 +00:00
|
|
|
if (!v->IsRoadVehFront()) 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)) {
|
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;
|
|
|
|
}
|
2007-11-17 12:42:15 +00:00
|
|
|
if ((v->Previous() != NULL && v->Previous()->tile == tile) ||
|
2009-07-02 08:59:27 +00:00
|
|
|
(v->IsRoadVehFront() && IsNormalRoadTile(tile) && !HasRoadWorks(tile) &&
|
2007-11-17 12:42:15 +00:00
|
|
|
(needed & GetRoadBits(tile, ROADTYPE_TRAM)) != ROAD_NONE)) {
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2009-07-02 08:59:27 +00:00
|
|
|
} else if (!v->IsRoadVehFront() || !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);
|
2009-07-02 08:59:27 +00:00
|
|
|
if (v->IsRoadVehFront()) {
|
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)) {
|
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;
|
|
|
|
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;
|
|
|
|
|
2007-11-18 15:06:01 +00:00
|
|
|
RoadBits tram;
|
2010-02-05 17:05:58 +00:00
|
|
|
if (v->roadtype == ROADTYPE_TRAM && !IsRoadDepotTile(v->tile) && HasExactlyOneBit(tram = GetAnyRoadBits(v->tile, ROADTYPE_TRAM, true))) {
|
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 {
|
2009-07-02 08:59:27 +00:00
|
|
|
if (v->IsRoadVehFront()) {
|
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);
|
2009-07-02 08:59:27 +00:00
|
|
|
if (v->IsRoadVehFront() && 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
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
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)) {
|
2009-05-23 09:10:56 +00:00
|
|
|
if (v->frame == v->rcache.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
|
|
|
|
2009-07-02 08:59:27 +00:00
|
|
|
if (v->IsRoadVehFront() && !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)) &&
|
|
|
|
v->owner == GetTileOwner(v->tile) && !v->current_order.IsType(OT_LEAVESTATION) &&
|
|
|
|
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;
|
2009-05-22 20:22:20 +00:00
|
|
|
if (old_dir != v->state) {
|
2007-02-12 22:46:10 +00:00
|
|
|
/* The vehicle is in a road stop */
|
2010-03-06 12:55:56 +00:00
|
|
|
v->UpdateInclination(false, true);
|
2007-02-12 22:46:10 +00:00
|
|
|
/* Note, return here means that the frame counter is not incremented
|
|
|
|
* for vehicles changing direction in a road stop. This causes frames to
|
2007-02-14 10:33:36 +00:00
|
|
|
* be repeated. (XXX) Is this intended? */
|
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
|
|
|
/* 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) */
|
2009-07-02 08:59:27 +00:00
|
|
|
if (v->IsRoadVehFront() && ((IsInsideMM(v->state, RVSB_IN_ROAD_STOP, RVSB_IN_ROAD_STOP_END) &&
|
2009-05-22 20:22:20 +00:00
|
|
|
_road_veh_data_1[v->state - RVSB_IN_ROAD_STOP + (_settings_game.vehicle.road_side << RVS_DRIVE_SIDE)] == v->frame) ||
|
|
|
|
(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)) &&
|
2009-03-01 21:48:03 +00:00
|
|
|
v->owner == GetTileOwner(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-07-12 19:36:06 +00:00
|
|
|
if (!v->current_order.IsType(OT_LEAVESTATION)) {
|
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)) {
|
|
|
|
TileIndex next_tile = TILE_ADD(v->tile, TileOffsByDir(v->direction));
|
|
|
|
|
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;
|
|
|
|
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);
|
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-07-12 19:36:06 +00:00
|
|
|
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);
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
|
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;
|
|
|
|
RoadZPosAffectSpeed(v, v->UpdateInclination(false, true));
|
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 */
|
|
|
|
v->tick_counter++;
|
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;
|
2009-05-22 13:53:14 +00:00
|
|
|
if (v->vehstatus & VS_STOPPED) 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
|
|
|
|
2009-05-22 13:53:14 +00:00
|
|
|
if (v->IsInDepot() && RoadVehLeaveDepot(v, true)) return true;
|
2007-06-11 14:00:16 +00:00
|
|
|
|
2009-01-06 14:45:38 +00:00
|
|
|
/* Check how far the vehicle needs to proceed */
|
|
|
|
int j = RoadVehAccelerate(v);
|
|
|
|
|
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
|
|
|
|
2009-05-22 20:18:45 +00:00
|
|
|
for (RoadVehicle *u = v; u != NULL; u = u->Next()) {
|
2009-03-11 23:23:08 +00:00
|
|
|
if ((u->vehstatus & VS_HIDDEN) != 0) continue;
|
|
|
|
|
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
|
|
|
|
{
|
2009-11-24 13:12:34 +00:00
|
|
|
const Engine *e = Engine::Get(this->engine_type);
|
|
|
|
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;
|
|
|
|
|
2010-08-10 15:54:53 +00:00
|
|
|
return GetPrice(e->u.road.running_cost_class, cost_factor, e->grf_prop.grffile);
|
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
|
|
|
{
|
2009-07-02 08:59:27 +00:00
|
|
|
if (this->IsRoadVehFront()) {
|
2008-02-13 19:24:40 +00:00
|
|
|
if (!(this->vehstatus & VS_STOPPED)) 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;
|
2007-08-29 21:49:08 +00:00
|
|
|
if (v->IsInDepot()) {
|
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();
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
|
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
|
|
|
|
2008-04-05 23:36:54 +00:00
|
|
|
if (v->current_order.IsType(OT_LOADING)) v->LeaveStation();
|
2007-05-14 08:07:20 +00:00
|
|
|
|
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;
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
|
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
|
|
|
{
|
2009-07-02 08:59:27 +00:00
|
|
|
if (!this->IsRoadVehFront()) 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
|
|
|
AgeVehicle(this);
|
|
|
|
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
|
|
|
}
|