2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2007-03-28 20:41:35 +00:00
|
|
|
/** @file roadveh_cmd.cpp */
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "stdafx.h"
|
2005-06-02 19:30:21 +00:00
|
|
|
#include "openttd.h"
|
2005-02-05 15:58:59 +00:00
|
|
|
#include "debug.h"
|
2005-07-22 07:02:20 +00:00
|
|
|
#include "functions.h"
|
2007-04-12 13:07:15 +00:00
|
|
|
#include "landscape.h"
|
2006-03-05 10:19:33 +00:00
|
|
|
#include "road_map.h"
|
2006-06-05 10:23:18 +00:00
|
|
|
#include "roadveh.h"
|
2006-03-24 08:55:08 +00:00
|
|
|
#include "station_map.h"
|
2004-11-25 10:47:30 +00:00
|
|
|
#include "table/strings.h"
|
2007-07-16 09:16:58 +00:00
|
|
|
#include "strings.h"
|
2004-12-15 22:18:54 +00:00
|
|
|
#include "map.h"
|
2005-01-29 12:19:05 +00:00
|
|
|
#include "tile.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "vehicle.h"
|
2007-06-20 19:17:22 +00:00
|
|
|
#include "timetable.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "engine.h"
|
|
|
|
#include "command.h"
|
|
|
|
#include "station.h"
|
|
|
|
#include "news.h"
|
|
|
|
#include "pathfind.h"
|
2005-01-31 11:23:10 +00:00
|
|
|
#include "npf.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "player.h"
|
2004-11-05 23:12:33 +00:00
|
|
|
#include "sound.h"
|
2005-02-06 10:18:47 +00:00
|
|
|
#include "depot.h"
|
2006-12-27 12:38:02 +00:00
|
|
|
#include "bridge.h"
|
2006-03-12 15:04:03 +00:00
|
|
|
#include "tunnel_map.h"
|
2006-12-27 12:38:02 +00:00
|
|
|
#include "bridge_map.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-06-09 07:45:26 +00:00
|
|
|
#include "newgrf_callbacks.h"
|
2006-02-03 12:55:21 +00:00
|
|
|
#include "newgrf_engine.h"
|
2006-08-09 21:11:45 +00:00
|
|
|
#include "newgrf_text.h"
|
2006-09-27 18:17:01 +00:00
|
|
|
#include "newgrf_sound.h"
|
2006-05-27 16:12:16 +00:00
|
|
|
#include "yapf/yapf.h"
|
2006-08-14 14:21:15 +00:00
|
|
|
#include "date.h"
|
2007-03-18 22:07:44 +00:00
|
|
|
#include "cargotype.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 20:57:02 +00:00
|
|
|
/** 'Convert' the DiagDirection where a road vehicle should exit to
|
|
|
|
* the trackdirs it can use to drive to the exit direction*/
|
|
|
|
static const TrackdirBits _road_exit_dir_to_incoming_trackdirs[DIAGDIR_END] = {
|
|
|
|
TRACKDIR_BIT_LOWER_W | TRACKDIR_BIT_X_SW | TRACKDIR_BIT_LEFT_S,
|
|
|
|
TRACKDIR_BIT_LEFT_N | TRACKDIR_BIT_UPPER_W | TRACKDIR_BIT_Y_NW,
|
|
|
|
TRACKDIR_BIT_RIGHT_N | TRACKDIR_BIT_UPPER_E | TRACKDIR_BIT_X_NE,
|
|
|
|
TRACKDIR_BIT_RIGHT_S | TRACKDIR_BIT_LOWER_E | TRACKDIR_BIT_Y_SE
|
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
|
|
|
|
};
|
|
|
|
|
2007-07-01 19:11:47 +00:00
|
|
|
int RoadVehicle::GetImage(Direction direction) const
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-07-01 19:11:47 +00:00
|
|
|
int img = this->spritenum;
|
2004-08-09 17:04:08 +00:00
|
|
|
int image;
|
|
|
|
|
|
|
|
if (is_custom_sprite(img)) {
|
2007-07-01 19:11:47 +00:00
|
|
|
image = GetCustomVehicleSprite(this, (Direction)(direction + 4 * IS_CUSTOM_SECONDHEAD_SPRITE(img)));
|
2006-02-06 09:18:04 +00:00
|
|
|
if (image != 0) return image;
|
2007-07-01 19:11:47 +00:00
|
|
|
img = orig_road_vehicle_info[this->engine_type - ROAD_ENGINES_INDEX].image_index;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
image = direction + _roadveh_images[img];
|
2007-07-01 19:11:47 +00:00
|
|
|
if (this->cargo.Count() >= this->cargo_cap / 2U) image += _roadveh_full_adder[img];
|
2004-08-09 17:04:08 +00:00
|
|
|
return image;
|
|
|
|
}
|
|
|
|
|
2007-01-14 19:57:49 +00:00
|
|
|
void DrawRoadVehEngine(int x, int y, EngineID engine, SpriteID pal)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2004-12-03 21:57:05 +00:00
|
|
|
int spritenum = RoadVehInfo(engine)->image_index;
|
2004-11-13 18:01:33 +00:00
|
|
|
|
|
|
|
if (is_custom_sprite(spritenum)) {
|
2006-03-12 17:42:04 +00:00
|
|
|
int sprite = GetCustomVehicleIcon(engine, DIR_W);
|
2004-11-13 18:01:33 +00:00
|
|
|
|
2006-02-13 21:15:00 +00:00
|
|
|
if (sprite != 0) {
|
2007-01-14 19:57:49 +00:00
|
|
|
DrawSprite(sprite, pal, x, y);
|
2004-11-13 18:01:33 +00:00
|
|
|
return;
|
|
|
|
}
|
2005-09-26 19:01:49 +00:00
|
|
|
spritenum = orig_road_vehicle_info[engine - ROAD_ENGINES_INDEX].image_index;
|
2004-11-13 18:01:33 +00:00
|
|
|
}
|
2007-01-14 19:57:49 +00:00
|
|
|
DrawSprite(6 + _roadveh_images[spritenum], pal, x, y);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-06-18 10:48:15 +00:00
|
|
|
static CommandCost EstimateRoadVehCost(EngineID engine_type)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-06-18 19:53:50 +00:00
|
|
|
return CommandCost(((_price.roadveh_base >> 3) * GetEngineProperty(engine_type, 0x11, RoadVehInfo(engine_type)->base_cost)) >> 5);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-06-11 14:00:16 +00:00
|
|
|
byte GetRoadVehLength(const Vehicle *v)
|
|
|
|
{
|
|
|
|
byte length = 8;
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RoadVehUpdateCache(Vehicle *v)
|
|
|
|
{
|
|
|
|
assert(v->type == VEH_ROAD);
|
|
|
|
assert(IsRoadVehFront(v));
|
|
|
|
|
2007-08-30 13:03:56 +00:00
|
|
|
for (Vehicle *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' */
|
|
|
|
u->u.road.first_engine = (v == u) ? INVALID_ENGINE : v->engine_type;
|
|
|
|
|
|
|
|
/* Update the length of the vehicle. */
|
|
|
|
u->u.road.cached_veh_length = GetRoadVehLength(u);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-05-12 00:11:37 +00:00
|
|
|
/** Build a road vehicle.
|
2006-04-10 07:15:58 +00:00
|
|
|
* @param tile tile of depot where road vehicle is built
|
2007-04-17 21:09:38 +00:00
|
|
|
* @param flags operation to perform
|
2005-05-12 00:11:37 +00:00
|
|
|
* @param p1 bus/truck type being built (engine)
|
2006-05-11 13:31:14 +00:00
|
|
|
* @param p2 bit 0 when set, the unitnumber will be 0, otherwise it will be a free number
|
2005-05-12 00:11:37 +00:00
|
|
|
*/
|
2007-06-18 10:48:15 +00:00
|
|
|
CommandCost CmdBuildRoadVeh(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-06-18 10:48:15 +00:00
|
|
|
CommandCost cost;
|
2004-08-09 17:04:08 +00:00
|
|
|
Vehicle *v;
|
2005-02-04 14:24:23 +00:00
|
|
|
UnitID unit_num;
|
2004-08-09 17:04:08 +00:00
|
|
|
Engine *e;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-04-02 11:35:08 +00:00
|
|
|
if (!IsEngineBuildable(p1, VEH_ROAD, _current_player)) return_cmd_error(STR_ROAD_VEHICLE_NOT_AVAILABLE);
|
2005-01-27 21:00:05 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
|
|
|
|
|
|
|
|
cost = EstimateRoadVehCost(p1);
|
2005-05-12 00:11:37 +00:00
|
|
|
if (flags & DC_QUERY_COST) return cost;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-03-06 16:53:00 +00:00
|
|
|
/* The ai_new queries the vehicle cost before building the route,
|
|
|
|
* so we must check against cheaters no sooner than now. --pasky */
|
|
|
|
if (!IsTileDepotType(tile, TRANSPORT_ROAD)) return CMD_ERROR;
|
2005-06-04 11:56:32 +00:00
|
|
|
if (!IsTileOwner(tile, _current_player)) return CMD_ERROR;
|
2005-03-06 16:53:00 +00:00
|
|
|
|
2007-11-19 21:02:30 +00:00
|
|
|
if (HasBit(GetRoadTypes(tile), ROADTYPE_TRAM) != HasBit(EngInfo(p1)->misc_flags, EF_ROAD_TRAM)) return_cmd_error(STR_DEPOT_WRONG_DEPOT_TYPE);
|
2007-05-26 20:30:55 +00:00
|
|
|
|
2007-09-17 04:23:03 +00:00
|
|
|
uint num_vehicles = 1 + CountArticulatedParts(p1, false);
|
2007-06-11 14:00:16 +00:00
|
|
|
|
2007-09-17 04:23:03 +00:00
|
|
|
/* Allow for the front and the articulated parts, plus one to "terminate" the list. */
|
|
|
|
Vehicle **vl = (Vehicle**)alloca(sizeof(*vl) * (num_vehicles + 1));
|
|
|
|
memset(vl, 0, sizeof(*vl) * (num_vehicles + 1));
|
2007-06-11 14:00:16 +00:00
|
|
|
|
2007-08-03 19:36:00 +00:00
|
|
|
if (!Vehicle::AllocateList(vl, num_vehicles)) {
|
2007-06-11 14:00:16 +00:00
|
|
|
return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME);
|
|
|
|
}
|
|
|
|
|
|
|
|
v = vl[0];
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
/* find the first free roadveh id */
|
2007-11-19 21:02:30 +00:00
|
|
|
unit_num = HasBit(p2, 0) ? 0 : GetFreeUnitNumber(VEH_ROAD);
|
2004-08-09 17:04:08 +00:00
|
|
|
if (unit_num > _patches.max_roadveh)
|
|
|
|
return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME);
|
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
2006-04-10 07:15:58 +00:00
|
|
|
int x;
|
|
|
|
int y;
|
|
|
|
|
2004-12-03 21:57:05 +00:00
|
|
|
const RoadVehicleInfo *rvi = RoadVehInfo(p1);
|
|
|
|
|
2007-10-21 16:45:00 +00:00
|
|
|
v = new (v) RoadVehicle();
|
2004-08-09 17:04:08 +00:00
|
|
|
v->unitnumber = unit_num;
|
2007-06-11 14:00:16 +00:00
|
|
|
v->direction = DiagDirToDir(GetRoadDepotDirection(tile));
|
2004-08-09 17:04:08 +00:00
|
|
|
v->owner = _current_player;
|
|
|
|
|
|
|
|
v->tile = tile;
|
2006-04-23 19:35:36 +00:00
|
|
|
x = TileX(tile) * TILE_SIZE + TILE_SIZE / 2;
|
|
|
|
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
|
|
|
|
2007-02-13 22:27:27 +00:00
|
|
|
v->u.road.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;
|
|
|
|
v->cargo_type = rvi->cargo_type;
|
2006-05-19 10:04:03 +00:00
|
|
|
v->cargo_subtype = 0;
|
2004-12-03 21:57:05 +00:00
|
|
|
v->cargo_cap = rvi->capacity;
|
2004-08-09 17:04:08 +00:00
|
|
|
// v->cargo_count = 0;
|
2007-06-18 19:53:50 +00:00
|
|
|
v->value = cost.GetCost();
|
2004-08-09 17:04:08 +00:00
|
|
|
// v->day_counter = 0;
|
|
|
|
// v->next_order_param = v->next_order = 0;
|
|
|
|
// v->load_unload_time_rem = 0;
|
|
|
|
// v->progress = 0;
|
|
|
|
|
|
|
|
// v->u.road.unk2 = 0;
|
|
|
|
// v->u.road.overtaking = 0;
|
|
|
|
|
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;
|
2004-08-09 17:04:08 +00:00
|
|
|
v->engine_type = (byte)p1;
|
|
|
|
|
2005-06-07 18:13:49 +00:00
|
|
|
e = GetEngine(p1);
|
2004-08-09 17:04:08 +00:00
|
|
|
v->reliability = e->reliability;
|
|
|
|
v->reliability_spd_dec = e->reliability_spd_dec;
|
|
|
|
v->max_age = e->lifelength * 366;
|
2005-10-29 21:54:28 +00:00
|
|
|
_new_vehicle_id = v->index;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
v->string_id = STR_SV_ROADVEH_NAME;
|
|
|
|
|
|
|
|
v->service_interval = _patches.servint_roadveh;
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
v->cur_image = 0xC15;
|
2005-12-28 22:29:59 +00:00
|
|
|
v->random_bits = VehicleRandomBits();
|
2007-06-11 14:00:16 +00:00
|
|
|
SetRoadVehFront(v);
|
|
|
|
|
2007-11-19 21:02:30 +00:00
|
|
|
v->u.road.roadtype = HasBit(EngInfo(v->engine_type)->misc_flags, EF_ROAD_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD;
|
2007-06-11 14:00:16 +00:00
|
|
|
v->u.road.compatible_roadtypes = RoadTypeToRoadTypes(v->u.road.roadtype);
|
|
|
|
v->u.road.cached_veh_length = GetRoadVehLength(v);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-28 17:59:05 +00:00
|
|
|
v->vehicle_flags = 0;
|
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
|
|
|
|
2007-05-12 07:05:34 +00:00
|
|
|
v->cargo_cap = GetVehicleProperty(v, 0x0F, rvi->capacity);
|
|
|
|
|
2007-06-11 14:00:16 +00:00
|
|
|
AddArticulatedParts(vl, VEH_ROAD);
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
VehiclePositionChanged(v);
|
|
|
|
|
2006-10-05 12:59:28 +00:00
|
|
|
InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
|
2004-12-10 18:16:08 +00:00
|
|
|
RebuildVehicleLists();
|
2004-08-09 17:04:08 +00:00
|
|
|
InvalidateWindow(WC_COMPANY, v->owner);
|
2005-11-08 23:18:09 +00:00
|
|
|
if (IsLocalPlayer())
|
2007-08-07 23:07:10 +00:00
|
|
|
InvalidateAutoreplaceWindow(v->engine_type, v->group_id); // updates the replace Road window
|
2007-02-06 11:11:12 +00:00
|
|
|
|
|
|
|
GetPlayer(_current_player)->num_engines[p1]++;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-06-18 19:53:50 +00:00
|
|
|
return CommandCost(cost);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-05-12 00:11:37 +00:00
|
|
|
/** Start/Stop a road vehicle.
|
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 road vehicle ID to start/stop
|
|
|
|
* @param p2 unused
|
|
|
|
*/
|
2007-06-18 10:48:15 +00:00
|
|
|
CommandCost CmdStartStopRoadVeh(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
Vehicle *v;
|
2006-08-09 21:11:45 +00:00
|
|
|
uint16 callback;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-08-22 18:15:17 +00:00
|
|
|
if (!IsValidVehicleID(p1)) return CMD_ERROR;
|
2005-01-30 20:50:06 +00:00
|
|
|
|
2005-01-06 22:31:58 +00:00
|
|
|
v = GetVehicle(p1);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-03-08 16:27:54 +00:00
|
|
|
if (v->type != VEH_ROAD || !CheckOwnership(v->owner)) return CMD_ERROR;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-08-09 21:11:45 +00:00
|
|
|
/* Check if this road veh can be started/stopped. The callback will fail or
|
|
|
|
* return 0xFF if it can. */
|
|
|
|
callback = GetVehicleCallback(CBID_VEHICLE_START_STOP_CHECK, 0, 0, v->engine_type, v);
|
|
|
|
if (callback != CALLBACK_FAILED && callback != 0xFF) {
|
|
|
|
StringID error = GetGRFStringID(GetEngineGRFID(v->engine_type), 0xD000 + callback);
|
|
|
|
return_cmd_error(error);
|
|
|
|
}
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (flags & DC_EXEC) {
|
2007-08-29 21:49:08 +00:00
|
|
|
if (v->IsStoppedInDepot()) {
|
2006-03-04 11:01:35 +00:00
|
|
|
DeleteVehicleNews(p1, STR_9016_ROAD_VEHICLE_IS_WAITING);
|
|
|
|
}
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
v->vehstatus ^= VS_STOPPED;
|
2007-05-25 22:31:34 +00:00
|
|
|
v->cur_speed = 0;
|
2004-12-21 23:27:58 +00:00
|
|
|
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
|
2004-08-09 17:04:08 +00:00
|
|
|
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
|
|
|
|
}
|
|
|
|
|
2007-06-18 19:53:50 +00:00
|
|
|
return CommandCost();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2006-03-02 08:55:12 +00:00
|
|
|
void ClearSlot(Vehicle *v)
|
2005-04-08 22:44:06 +00:00
|
|
|
{
|
2006-03-02 08:55:12 +00:00
|
|
|
RoadStop *rs = v->u.road.slot;
|
|
|
|
if (v->u.road.slot == NULL) return;
|
|
|
|
|
2005-04-08 22:44:06 +00:00
|
|
|
v->u.road.slot = NULL;
|
|
|
|
v->u.road.slot_age = 0;
|
2006-03-02 08:55:12 +00:00
|
|
|
|
2006-04-03 14:25:32 +00:00
|
|
|
assert(rs->num_vehicles != 0);
|
|
|
|
rs->num_vehicles--;
|
|
|
|
|
2006-12-26 17:36:18 +00:00
|
|
|
DEBUG(ms, 3, "Clearing slot at 0x%X", rs->xy);
|
2005-04-08 22:44:06 +00:00
|
|
|
}
|
|
|
|
|
2007-06-11 14:00:16 +00:00
|
|
|
static bool CheckRoadVehInDepotStopped(const Vehicle *v)
|
|
|
|
{
|
|
|
|
TileIndex tile = v->tile;
|
|
|
|
|
2007-08-16 18:01:27 +00:00
|
|
|
if (!IsTileDepotType(tile, TRANSPORT_ROAD)) return false;
|
|
|
|
if (IsRoadVehFront(v) && !(v->vehstatus & VS_STOPPED)) return false;
|
2007-06-11 14:00:16 +00:00
|
|
|
|
2007-08-30 13:03:56 +00:00
|
|
|
for (; v != NULL; v = v->Next()) {
|
2007-06-11 14:00:16 +00:00
|
|
|
if (v->u.road.state != RVSB_IN_DEPOT || v->tile != tile) return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-05-12 00:11:37 +00:00
|
|
|
/** Sell a road vehicle.
|
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 be sold
|
|
|
|
* @param p2 unused
|
|
|
|
*/
|
2007-06-18 10:48:15 +00:00
|
|
|
CommandCost CmdSellRoadVeh(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
Vehicle *v;
|
|
|
|
|
2006-08-22 18:15:17 +00:00
|
|
|
if (!IsValidVehicleID(p1)) return CMD_ERROR;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2005-01-06 22:31:58 +00:00
|
|
|
v = GetVehicle(p1);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-03-08 16:27:54 +00:00
|
|
|
if (v->type != VEH_ROAD || !CheckOwnership(v->owner)) return CMD_ERROR;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-10-08 20:06:37 +00:00
|
|
|
if (HASBITS(v->vehstatus, VS_CRASHED)) return_cmd_error(STR_CAN_T_SELL_DESTROYED_VEHICLE);
|
|
|
|
|
2005-01-30 20:50:06 +00:00
|
|
|
SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
|
|
|
|
|
2007-06-11 14:00:16 +00:00
|
|
|
if (!CheckRoadVehInDepotStopped(v)) {
|
2004-08-09 17:04:08 +00:00
|
|
|
return_cmd_error(STR_9013_MUST_BE_STOPPED_INSIDE);
|
2006-02-18 14:41:24 +00:00
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-10-20 20:15:34 +00:00
|
|
|
CommandCost ret(-v->value);
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
// Invalidate depot
|
|
|
|
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
|
2004-12-10 18:16:08 +00:00
|
|
|
RebuildVehicleLists();
|
2004-08-09 17:04:08 +00:00
|
|
|
InvalidateWindow(WC_COMPANY, v->owner);
|
|
|
|
DeleteWindowById(WC_VEHICLE_VIEW, v->index);
|
2006-12-05 22:59:42 +00:00
|
|
|
DeleteDepotHighlightOfVehicle(v);
|
2007-08-03 19:36:00 +00:00
|
|
|
delete v;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-10-20 20:15:34 +00:00
|
|
|
return ret;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-03-07 12:11:48 +00:00
|
|
|
struct RoadFindDepotData {
|
2004-08-09 17:04:08 +00:00
|
|
|
uint best_length;
|
2005-06-24 12:38:35 +00:00
|
|
|
TileIndex tile;
|
2007-01-10 18:56:51 +00:00
|
|
|
OwnerByte owner;
|
2007-03-07 12:11:48 +00:00
|
|
|
};
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-03-12 17:42:04 +00:00
|
|
|
static const DiagDirection _road_pf_directions[] = {
|
2007-01-10 18:56:51 +00:00
|
|
|
DIAGDIR_NE, DIAGDIR_SE, DIAGDIR_NE, DIAGDIR_SE, DIAGDIR_SW, DIAGDIR_SE, INVALID_DIAGDIR, INVALID_DIAGDIR,
|
|
|
|
DIAGDIR_SW, DIAGDIR_NW, DIAGDIR_NW, DIAGDIR_SW, DIAGDIR_NW, DIAGDIR_NE, INVALID_DIAGDIR, INVALID_DIAGDIR
|
2004-08-09 17:04:08 +00:00
|
|
|
};
|
|
|
|
|
2007-01-10 18:56:51 +00:00
|
|
|
static bool EnumRoadSignalFindDepot(TileIndex tile, void* data, Trackdir trackdir, uint length, byte* state)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-01-10 18:56:51 +00:00
|
|
|
RoadFindDepotData* rfdd = (RoadFindDepotData*)data;
|
2006-03-03 21:29:13 +00:00
|
|
|
|
2007-01-10 18:56:51 +00:00
|
|
|
tile += TileOffsByDiagDir(_road_pf_directions[trackdir]);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-07-29 23:42:59 +00:00
|
|
|
if (IsTileType(tile, MP_ROAD) &&
|
2006-05-09 08:25:31 +00:00
|
|
|
GetRoadTileType(tile) == ROAD_TILE_DEPOT &&
|
2006-03-03 22:21:29 +00:00
|
|
|
IsTileOwner(tile, rfdd->owner) &&
|
|
|
|
length < rfdd->best_length) {
|
|
|
|
rfdd->best_length = length;
|
|
|
|
rfdd->tile = tile;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-02-18 14:41:24 +00:00
|
|
|
static const Depot* FindClosestRoadDepot(const Vehicle* v)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-06-24 12:38:35 +00:00
|
|
|
TileIndex tile = v->tile;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2006-05-27 16:12:16 +00:00
|
|
|
if (_patches.yapf.road_use_yapf) {
|
|
|
|
Depot* ret = YapfFindNearestRoadDepot(v);
|
|
|
|
return ret;
|
|
|
|
} else if (_patches.new_pathfinding_all) {
|
2005-01-31 11:23:10 +00:00
|
|
|
NPFFoundTargetData ftd;
|
|
|
|
/* See where we are now */
|
(svn r2448) General cleanup of rail related code, more to follow.
* Add: rail.[ch] for rail-related enums and wrapper functions.
* Codechange: Removed dozens of magic numbers with below enums.
* Codechange: Rewrote CheckTrackCombination().
* Add: TILE_SIZE, TILE_PIXELS and TILE_HEIGHT constants.
* Add: enums RailTileType, RailTileSubtype, SignalType to mask against the map arrays.
* Add: enums Track, TrackBits, Trackdir, TrackdirBits for railway track data. (Note that the old RAIL_BIT constants are replaced by TRACK_BIT ones).
* Add: enums Direction and DiagDirection
* Codechange: Moved a bunch of track(dir) related lookup arrays from npf.[ch] to rail.[ch].
* Codechange: move RailType enum from tile.h to rail.h.
* Add: Wrapper functions for masking signal status in the map arrays: SignalAlongTrackdir, SignalAgainstTrackdir and SignalOnTrack.
* Add: Wrapper functions to access rail tiles, using above enums
* Add: Wrapper functions to modify tracks, trackdirs, directions, etc.
* Add: Wrapper functions for all lookup arrays in rail.[ch] (Arrays are still used in parts of the code)
* Codechange: Renamed some variables and arguments to better represent what they contain (railbit -> track, bits -> trackdirbits, etc.).
* Codechange: Don't use FindLandscapeHeight() in CmdRemoveSingleRail(), since it returns way too much info. Use GetTileSlope() instead.
* Codechange: [NPF] Removed some unused globals and code from npf.c.
2005-06-16 18:04:02 +00:00
|
|
|
Trackdir trackdir = GetVehicleTrackdir(v);
|
2005-05-02 17:52:35 +00:00
|
|
|
|
2007-06-26 20:15:33 +00:00
|
|
|
ftd = NPFRouteToDepotBreadthFirstTwoWay(v->tile, trackdir, v->tile, ReverseTrackdir(trackdir), TRANSPORT_ROAD, v->u.road.compatible_roadtypes, v->owner, INVALID_RAILTYPE, 0);
|
2006-02-13 21:15:00 +00:00
|
|
|
if (ftd.best_bird_dist == 0) {
|
2005-01-31 11:23:10 +00:00
|
|
|
return GetDepotByTile(ftd.node.tile); /* Target found */
|
2006-02-13 21:15:00 +00:00
|
|
|
} else {
|
2005-02-06 10:18:47 +00:00
|
|
|
return NULL; /* Target not found */
|
2006-02-13 21:15:00 +00:00
|
|
|
}
|
2005-01-31 11:23:10 +00:00
|
|
|
/* We do not search in two directions here, why should we? We can't reverse right now can we? */
|
|
|
|
} else {
|
|
|
|
RoadFindDepotData rfdd;
|
2006-03-06 20:28:28 +00:00
|
|
|
|
2005-01-31 11:23:10 +00:00
|
|
|
rfdd.owner = v->owner;
|
|
|
|
rfdd.best_length = (uint)-1;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-01-31 11:23:10 +00:00
|
|
|
/* search in all directions */
|
2007-01-10 18:56:51 +00:00
|
|
|
for (DiagDirection i = DIAGDIR_BEGIN; i != DIAGDIR_END; i++) {
|
2007-05-24 22:41:50 +00:00
|
|
|
FollowTrack(tile, 0x2000 | TRANSPORT_ROAD, v->u.road.compatible_roadtypes, i, EnumRoadSignalFindDepot, NULL, &rfdd);
|
2006-02-18 14:41:24 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-02-13 21:15:00 +00:00
|
|
|
if (rfdd.best_length == (uint)-1) return NULL;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-01-31 11:23:10 +00:00
|
|
|
return GetDepotByTile(rfdd.tile);
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-05-12 00:11:37 +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)
|
2005-05-12 00:11:37 +00:00
|
|
|
*/
|
2007-06-18 10:48:15 +00:00
|
|
|
CommandCost CmdSendRoadVehToDepot(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-01-30 20:50:06 +00:00
|
|
|
Vehicle *v;
|
2005-05-12 00:11:37 +00:00
|
|
|
const Depot *dep;
|
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;
|
2007-03-08 16:27:54 +00:00
|
|
|
return SendAllVehiclesToDepot(VEH_ROAD, flags, p2 & DEPOT_SERVICE, _current_player, (p2 & VLW_MASK), p1);
|
2006-08-30 21:39:01 +00:00
|
|
|
}
|
|
|
|
|
2006-09-01 10:24:15 +00:00
|
|
|
if (!IsValidVehicleID(p1)) return CMD_ERROR;
|
2005-01-30 20:50:06 +00:00
|
|
|
|
|
|
|
v = GetVehicle(p1);
|
|
|
|
|
2007-03-08 16:27:54 +00:00
|
|
|
if (v->type != VEH_ROAD || !CheckOwnership(v->owner)) return CMD_ERROR;
|
2006-08-29 23:39:57 +00:00
|
|
|
|
2006-09-01 10:24:15 +00:00
|
|
|
if (v->vehstatus & VS_CRASHED) return CMD_ERROR;
|
2005-05-03 19:31:33 +00:00
|
|
|
|
2007-08-29 21:49:08 +00:00
|
|
|
if (v->IsInDepot()) return CMD_ERROR;
|
2006-09-02 22:47:45 +00:00
|
|
|
|
2005-05-12 00:11:37 +00:00
|
|
|
/* If the current orders are already goto-depot */
|
2004-12-05 12:43:04 +00:00
|
|
|
if (v->current_order.type == OT_GOTO_DEPOT) {
|
2007-11-19 21:02:30 +00:00
|
|
|
if (!!(p2 & DEPOT_SERVICE) == HasBit(v->current_order.flags, OFB_HALT_IN_DEPOT)) {
|
2006-09-01 12:37:03 +00:00
|
|
|
/* We called with a different DEPOT_SERVICE setting.
|
2006-09-04 20:40:33 +00:00
|
|
|
* Now we change the setting to apply the new one and let the vehicle head for the same depot.
|
|
|
|
* Note: the if is (true for requesting service == true for ordered to stop in depot) */
|
2006-09-01 12:37:03 +00:00
|
|
|
if (flags & DC_EXEC) {
|
2007-11-19 21:32:20 +00:00
|
|
|
ClrBit(v->current_order.flags, OFB_PART_OF_ORDERS);
|
2007-11-20 14:11:19 +00:00
|
|
|
ToggleBit(v->current_order.flags, OFB_HALT_IN_DEPOT);
|
2006-09-01 12:37:03 +00:00
|
|
|
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
|
|
|
|
}
|
2007-06-18 19:53:50 +00:00
|
|
|
return CommandCost();
|
2006-09-01 12:37:03 +00:00
|
|
|
}
|
|
|
|
|
2006-09-01 10:24:15 +00:00
|
|
|
if (p2 & DEPOT_DONT_CANCEL) return CMD_ERROR; // Requested no cancelation of depot orders
|
2004-08-09 17:04:08 +00:00
|
|
|
if (flags & DC_EXEC) {
|
2005-05-12 00:11:37 +00:00
|
|
|
/* If the orders to 'goto depot' are in the orders list (forced servicing),
|
|
|
|
* then skip to the next order; effectively cancelling this forced service */
|
2007-11-19 21:02:30 +00:00
|
|
|
if (HasBit(v->current_order.flags, OFB_PART_OF_ORDERS))
|
2004-08-09 17:04:08 +00:00
|
|
|
v->cur_order_index++;
|
2005-03-20 08:43:29 +00:00
|
|
|
|
2004-12-05 12:43:04 +00:00
|
|
|
v->current_order.type = OT_DUMMY;
|
|
|
|
v->current_order.flags = 0;
|
2004-12-21 23:27:58 +00:00
|
|
|
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
|
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
|
|
|
}
|
|
|
|
|
2005-05-12 00:11:37 +00:00
|
|
|
dep = FindClosestRoadDepot(v);
|
2006-09-01 10:24:15 +00:00
|
|
|
if (dep == NULL) return_cmd_error(STR_9019_UNABLE_TO_FIND_LOCAL_DEPOT);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
2007-04-20 11:39:01 +00:00
|
|
|
if (v->current_order.type == OT_LOADING) v->LeaveStation();
|
|
|
|
|
2006-03-16 16:47:39 +00:00
|
|
|
ClearSlot(v);
|
2004-12-05 12:43:04 +00:00
|
|
|
v->current_order.type = OT_GOTO_DEPOT;
|
2006-08-27 09:28:52 +00:00
|
|
|
v->current_order.flags = OF_NON_STOP;
|
2007-11-20 13:35:54 +00:00
|
|
|
if (!(p2 & DEPOT_SERVICE)) SetBit(v->current_order.flags, OFB_HALT_IN_DEPOT);
|
2006-12-05 23:57:04 +00:00
|
|
|
v->current_order.refit_cargo = CT_INVALID;
|
2006-09-03 08:25:27 +00:00
|
|
|
v->current_order.dest = dep->index;
|
2005-05-12 00:11:37 +00:00
|
|
|
v->dest_tile = dep->xy;
|
2004-12-21 23:27:58 +00:00
|
|
|
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-06-18 19:53:50 +00:00
|
|
|
return CommandCost();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-05-12 00:11:37 +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
|
|
|
|
*/
|
2007-06-18 10:48:15 +00:00
|
|
|
CommandCost CmdTurnRoadVeh(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
Vehicle *v;
|
|
|
|
|
2006-08-22 18:15:17 +00:00
|
|
|
if (!IsValidVehicleID(p1)) return CMD_ERROR;
|
2005-01-30 20:50:06 +00:00
|
|
|
|
2005-01-06 22:31:58 +00:00
|
|
|
v = GetVehicle(p1);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-03-08 16:27:54 +00:00
|
|
|
if (v->type != VEH_ROAD || !CheckOwnership(v->owner)) return CMD_ERROR;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-12-27 12:38:02 +00:00
|
|
|
if (v->vehstatus & VS_STOPPED ||
|
2007-08-26 20:16:02 +00:00
|
|
|
v->vehstatus & VS_CRASHED ||
|
2004-08-09 17:04:08 +00:00
|
|
|
v->breakdown_ctr != 0 ||
|
|
|
|
v->u.road.overtaking != 0 ||
|
2007-02-13 22:27:27 +00:00
|
|
|
v->u.road.state == RVSB_WORMHOLE ||
|
2007-08-29 21:49:08 +00:00
|
|
|
v->IsInDepot() ||
|
2004-08-09 17:04:08 +00:00
|
|
|
v->cur_speed < 5) {
|
|
|
|
return CMD_ERROR;
|
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-07-29 23:42:59 +00:00
|
|
|
if (IsTileType(v->tile, MP_ROAD) && GetRoadTileType(v->tile) == ROAD_TILE_NORMAL && GetDisallowedRoadDirections(v->tile) != DRD_NONE) return CMD_ERROR;
|
2007-05-31 15:15:00 +00:00
|
|
|
|
2006-12-27 12:38:02 +00:00
|
|
|
if (IsTunnelTile(v->tile) && DirToDiagDir(v->direction) == GetTunnelDirection(v->tile)) return CMD_ERROR;
|
|
|
|
if (IsBridgeTile(v->tile) && DirToDiagDir(v->direction) == GetBridgeRampDirection(v->tile)) return CMD_ERROR;
|
|
|
|
|
2006-03-03 22:21:29 +00:00
|
|
|
if (flags & DC_EXEC) v->u.road.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
|
|
|
{
|
2007-08-30 13:03:56 +00:00
|
|
|
for (Vehicle *v = this; v != NULL; v = v->Next()) {
|
2007-08-06 13:44:50 +00:00
|
|
|
v->cur_image = v->GetImage(v->direction);
|
|
|
|
MarkAllViewportsDirty(v->left_coord, v->top_coord, v->right_coord + 1, v->bottom_coord + 1);
|
|
|
|
}
|
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);
|
|
|
|
this->sprite_width = GB(x, 16, 8);
|
|
|
|
this->sprite_height = GB(x, 24, 8);
|
|
|
|
this->z_height = 6;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void ClearCrashedStation(Vehicle *v)
|
|
|
|
{
|
2005-11-17 10:12:21 +00:00
|
|
|
RoadStop *rs = GetRoadStopByTile(v->tile, GetRoadStopType(v->tile));
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-13 00:25:42 +00:00
|
|
|
/* Mark the station entrance as not busy */
|
|
|
|
rs->SetEntranceBusy(false);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-02-13 00:25:42 +00:00
|
|
|
/* Free the parking bay */
|
2007-11-19 21:02:30 +00:00
|
|
|
rs->FreeBay(HasBit(v->u.road.state, RVS_USING_SECOND_BAY));
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-06-11 14:00:16 +00:00
|
|
|
static void DeleteLastRoadVeh(Vehicle *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
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
DeleteWindowById(WC_VEHICLE_VIEW, v->index);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-12-10 18:16:08 +00:00
|
|
|
RebuildVehicleLists();
|
2004-08-09 17:04:08 +00:00
|
|
|
InvalidateWindow(WC_COMPANY, v->owner);
|
|
|
|
|
2006-02-13 21:15:00 +00:00
|
|
|
if (IsTileType(v->tile, MP_STATION)) ClearCrashedStation(v);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
BeginVehicleMove(v);
|
|
|
|
EndVehicleMove(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
|
|
|
}
|
|
|
|
|
|
|
|
static byte SetRoadVehPosition(Vehicle *v, int x, int y)
|
|
|
|
{
|
|
|
|
byte new_z, old_z;
|
|
|
|
|
2007-03-28 20:41:35 +00:00
|
|
|
/* need this hint so it returns the right z coordinate on bridges. */
|
2006-02-13 21:15:00 +00:00
|
|
|
v->x_pos = x;
|
|
|
|
v->y_pos = y;
|
|
|
|
new_z = GetSlopeZ(x, y);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
old_z = v->z_pos;
|
|
|
|
v->z_pos = new_z;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
VehiclePositionChanged(v);
|
|
|
|
EndVehicleMove(v);
|
|
|
|
return old_z;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void RoadVehSetRandomDirection(Vehicle *v)
|
|
|
|
{
|
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]);
|
|
|
|
BeginVehicleMove(v);
|
|
|
|
v->UpdateDeltaXY(v->direction);
|
2007-07-01 19:11:47 +00:00
|
|
|
v->cur_image = v->GetImage(v->direction);
|
2007-06-11 14:00:16 +00:00
|
|
|
SetRoadVehPosition(v, v->x_pos, v->y_pos);
|
2007-08-30 13:03:56 +00:00
|
|
|
} while ((v = v->Next()) != NULL);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void RoadVehIsCrashed(Vehicle *v)
|
|
|
|
{
|
|
|
|
v->u.road.crashed_ctr++;
|
|
|
|
if (v->u.road.crashed_ctr == 2) {
|
2005-02-12 15:53:32 +00:00
|
|
|
CreateEffectVehicleRel(v, 4, 4, 8, EV_EXPLOSION_LARGE);
|
2004-08-09 17:04:08 +00:00
|
|
|
} else if (v->u.road.crashed_ctr <= 45) {
|
2006-02-13 21:15:00 +00:00
|
|
|
if ((v->tick_counter & 7) == 0) RoadVehSetRandomDirection(v);
|
2007-06-11 14:00:16 +00:00
|
|
|
} else if (v->u.road.crashed_ctr >= 2220 && !(v->tick_counter & 0x1F)) {
|
|
|
|
DeleteLastRoadVeh(v);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-03-03 22:21:29 +00:00
|
|
|
static void* EnumCheckRoadVehCrashTrain(Vehicle* v, void* data)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-01-10 18:56:51 +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
|
|
|
}
|
|
|
|
|
|
|
|
static void RoadVehCrash(Vehicle *v)
|
|
|
|
{
|
2007-06-11 14:00:16 +00:00
|
|
|
uint16 pass = 1;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
v->u.road.crashed_ctr++;
|
2007-06-11 14:00:16 +00:00
|
|
|
|
2007-08-30 13:03:56 +00:00
|
|
|
for (Vehicle *u = v; u != NULL; u = u->Next()) {
|
2007-06-22 11:58:59 +00:00
|
|
|
if (IsCargoInClass(u->cargo_type, CC_PASSENGERS)) pass += u->cargo.Count();
|
2007-06-11 14:00:16 +00:00
|
|
|
|
|
|
|
u->vehstatus |= VS_CRASHED;
|
|
|
|
|
|
|
|
MarkAllViewportsDirty(u->left_coord, u->top_coord, u->right_coord + 1, u->bottom_coord + 1);
|
|
|
|
}
|
|
|
|
|
2006-03-16 16:47:39 +00:00
|
|
|
ClearSlot(v);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-12-21 23:27:58 +00:00
|
|
|
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-02-01 06:32:03 +00:00
|
|
|
SetDParam(0, pass);
|
2005-08-01 13:01:14 +00:00
|
|
|
AddNewsItem(
|
|
|
|
(pass == 1) ?
|
|
|
|
STR_9031_ROAD_VEHICLE_CRASH_DRIVER : STR_9032_ROAD_VEHICLE_CRASH_DIE,
|
2004-08-09 17:04:08 +00:00
|
|
|
NEWS_FLAGS(NM_THIN, NF_VIEWPORT|NF_VEHICLE, NT_ACCIDENT, 0),
|
|
|
|
v->index,
|
2006-02-18 14:41:24 +00:00
|
|
|
0
|
|
|
|
);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
static void RoadVehCheckTrainCrash(Vehicle *v)
|
|
|
|
{
|
2007-08-30 13:03:56 +00:00
|
|
|
for (Vehicle *u = v; u != NULL; u = u->Next()) {
|
2007-06-11 14:00:16 +00:00
|
|
|
if (u->u.road.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
|
|
|
|
2007-06-12 11:22:32 +00:00
|
|
|
if (VehicleFromPosXY(v->x_pos, v->y_pos, u, EnumCheckRoadVehCrashTrain) != NULL) {
|
2007-06-11 14:00:16 +00:00
|
|
|
RoadVehCrash(v);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void HandleBrokenRoadVeh(Vehicle *v)
|
|
|
|
{
|
|
|
|
if (v->breakdown_ctr != 1) {
|
|
|
|
v->breakdown_ctr = 1;
|
|
|
|
v->cur_speed = 0;
|
|
|
|
|
|
|
|
if (v->breakdowns_since_last_service != 255)
|
|
|
|
v->breakdowns_since_last_service++;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
InvalidateWindow(WC_VEHICLE_VIEW, v->index);
|
|
|
|
InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2006-09-27 18:17:01 +00:00
|
|
|
if (!PlayVehicleSound(v, VSE_BREAKDOWN)) {
|
2007-03-22 03:42:43 +00:00
|
|
|
SndPlayVehicleFx((_opt.landscape != LT_TOYLAND) ?
|
2006-09-27 18:17:01 +00:00
|
|
|
SND_0F_VEHICLE_BREAKDOWN : SND_35_COMEDY_BREAKDOWN, v);
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (!(v->vehstatus & VS_HIDDEN)) {
|
|
|
|
Vehicle *u = CreateEffectVehicleRel(v, 4, 4, 5, EV_BREAKDOWN_SMOKE);
|
2007-07-26 15:37:19 +00:00
|
|
|
if (u != NULL) u->u.special.animation_state = v->breakdown_delay * 2;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-02-06 09:18:04 +00:00
|
|
|
if ((v->tick_counter & 1) == 0) {
|
|
|
|
if (--v->breakdown_delay == 0) {
|
2004-08-09 17:04:08 +00:00
|
|
|
v->breakdown_ctr = 0;
|
|
|
|
InvalidateWindow(WC_VEHICLE_VIEW, v->index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ProcessRoadVehOrder(Vehicle *v)
|
|
|
|
{
|
2005-01-15 19:06:22 +00:00
|
|
|
const Order *order;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-02-10 06:03:51 +00:00
|
|
|
switch (v->current_order.type) {
|
|
|
|
case OT_GOTO_DEPOT:
|
2007-03-28 20:41:35 +00:00
|
|
|
/* Let a depot order in the orderlist interrupt. */
|
2006-02-10 06:03:51 +00:00
|
|
|
if (!(v->current_order.flags & OF_PART_OF_ORDERS)) return;
|
|
|
|
if (v->current_order.flags & OF_SERVICE_IF_NEEDED &&
|
|
|
|
!VehicleNeedsService(v)) {
|
2007-06-20 19:17:22 +00:00
|
|
|
UpdateVehicleTimetable(v, true);
|
2007-06-25 23:14:13 +00:00
|
|
|
v->cur_order_index++;
|
2006-02-10 06:03:51 +00:00
|
|
|
}
|
|
|
|
break;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-02-10 06:03:51 +00:00
|
|
|
case OT_LOADING:
|
|
|
|
case OT_LEAVESTATION:
|
|
|
|
return;
|
2006-08-22 17:13:49 +00:00
|
|
|
|
|
|
|
default: break;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2006-02-10 06:03:51 +00:00
|
|
|
if (v->cur_order_index >= v->num_orders) v->cur_order_index = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-01-15 19:06:22 +00:00
|
|
|
order = GetVehicleOrder(v, v->cur_order_index);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-01-15 19:06:22 +00:00
|
|
|
if (order == NULL) {
|
2007-03-08 21:39:34 +00:00
|
|
|
v->current_order.Free();
|
2004-08-09 17:04:08 +00:00
|
|
|
v->dest_tile = 0;
|
2006-03-02 08:55:12 +00:00
|
|
|
ClearSlot(v);
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-09-03 08:25:27 +00:00
|
|
|
if (order->type == v->current_order.type &&
|
|
|
|
order->flags == v->current_order.flags &&
|
|
|
|
order->dest == v->current_order.dest) {
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
2006-02-01 06:32:03 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-01-15 19:06:22 +00:00
|
|
|
v->current_order = *order;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-07-26 10:50:46 +00:00
|
|
|
switch (order->type) {
|
|
|
|
case OT_GOTO_STATION: {
|
2006-09-03 08:25:27 +00:00
|
|
|
if (order->dest == v->last_station_visited) {
|
2006-07-26 10:50:46 +00:00
|
|
|
v->last_station_visited = INVALID_STATION;
|
|
|
|
}
|
2005-01-29 19:41:44 +00:00
|
|
|
|
2007-08-24 19:19:18 +00:00
|
|
|
const RoadStop *rs = GetStation(order->dest)->GetPrimaryRoadStop(v);
|
2005-01-30 17:46:57 +00:00
|
|
|
|
2007-05-30 18:22:56 +00:00
|
|
|
TileIndex dest = INVALID_TILE;
|
2006-07-26 10:50:46 +00:00
|
|
|
if (rs != NULL) {
|
2007-05-30 18:22:56 +00:00
|
|
|
uint mindist = MAX_UVALUE(uint);
|
|
|
|
|
2007-08-24 19:19:18 +00:00
|
|
|
for (; rs != NULL; rs = rs->GetNextRoadStop(v)) {
|
2006-07-26 10:50:46 +00:00
|
|
|
uint dist = DistanceManhattan(v->tile, rs->xy);
|
2006-03-03 21:18:19 +00:00
|
|
|
|
2006-07-26 10:50:46 +00:00
|
|
|
if (dist < mindist) {
|
|
|
|
mindist = dist;
|
|
|
|
dest = rs->xy;
|
|
|
|
}
|
|
|
|
}
|
2007-05-30 18:22:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (dest != INVALID_TILE) {
|
|
|
|
v->dest_tile = dest;
|
2006-07-26 10:50:46 +00:00
|
|
|
} else {
|
2007-03-28 20:41:35 +00:00
|
|
|
/* There is no stop left at the station, so don't even TRY to go there */
|
2006-07-26 10:50:46 +00:00
|
|
|
v->cur_order_index++;
|
|
|
|
v->dest_tile = 0;
|
2006-03-03 21:18:19 +00:00
|
|
|
}
|
2006-07-26 10:50:46 +00:00
|
|
|
break;
|
2005-01-29 19:41:44 +00:00
|
|
|
}
|
2006-07-26 10:50:46 +00:00
|
|
|
|
|
|
|
case OT_GOTO_DEPOT:
|
2006-09-03 08:25:27 +00:00
|
|
|
v->dest_tile = GetDepot(order->dest)->xy;
|
2006-07-26 10:50:46 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
v->dest_tile = 0;
|
|
|
|
break;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-01-15 19:06:22 +00:00
|
|
|
InvalidateVehicleOrder(v);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2006-02-13 21:15:00 +00:00
|
|
|
static void StartRoadVehSound(const Vehicle* v)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-09-27 18:17:01 +00:00
|
|
|
if (!PlayVehicleSound(v, VSE_START)) {
|
|
|
|
SoundFx s = RoadVehInfo(v->engine_type)->sfx;
|
|
|
|
if (s == SND_19_BUS_START_PULL_AWAY && (v->tick_counter & 3) == 0)
|
|
|
|
s = SND_1A_BUS_START_PULL_AWAY_WITH_HORN;
|
|
|
|
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;
|
|
|
|
const Vehicle* veh;
|
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
|
|
|
|
2006-03-03 22:21:29 +00:00
|
|
|
static void* 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 };
|
|
|
|
|
2007-01-10 18:56:51 +00:00
|
|
|
const 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
|
|
|
|
2006-03-03 22:21:29 +00:00
|
|
|
return
|
2007-03-08 16:27:54 +00:00
|
|
|
v->type == VEH_ROAD &&
|
2007-08-29 21:49:08 +00:00
|
|
|
!v->IsInDepot() &&
|
2007-11-19 18:58:04 +00:00
|
|
|
abs(v->z_pos - rvf->veh->z_pos) < 6 &&
|
2006-03-03 22:21:29 +00:00
|
|
|
v->direction == rvf->dir &&
|
2007-08-30 21:11:12 +00:00
|
|
|
rvf->veh->First() != v->First() &&
|
2006-03-03 22:21:29 +00:00
|
|
|
(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)) ?
|
|
|
|
v : NULL;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2006-03-08 06:55:33 +00:00
|
|
|
static Vehicle* RoadVehFindCloseTo(Vehicle* v, int x, int y, Direction dir)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
RoadVehFindData rvf;
|
|
|
|
Vehicle *u;
|
2007-10-31 22:13:41 +00:00
|
|
|
Vehicle *front = v->First();
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-10-31 22:13:41 +00:00
|
|
|
if (front->u.road.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;
|
2007-10-31 22:13:41 +00:00
|
|
|
if (front->u.road.state == RVSB_WORMHOLE) {
|
|
|
|
u = (Vehicle*)VehicleFromPos(v->tile, &rvf, EnumCheckRoadVehClose);
|
|
|
|
} else {
|
|
|
|
u = (Vehicle*)VehicleFromPosXY(x, y, &rvf, EnumCheckRoadVehClose);
|
|
|
|
}
|
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. */
|
2004-08-09 17:04:08 +00:00
|
|
|
if (u == NULL) {
|
2007-10-31 22:13:41 +00:00
|
|
|
front->u.road.blocked_ctr = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-10-31 22:13:41 +00:00
|
|
|
if (++front->u.road.blocked_ctr > 1480) return NULL;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
return u;
|
|
|
|
}
|
|
|
|
|
2005-11-13 13:43:55 +00:00
|
|
|
static void RoadVehArrivesAt(const Vehicle* v, Station* st)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-03-18 22:07:44 +00:00
|
|
|
if (IsCargoInClass(v->cargo_type, CC_PASSENGERS)) {
|
2004-08-09 17:04:08 +00:00
|
|
|
/* Check if station was ever visited before */
|
|
|
|
if (!(st->had_vehicle_of_type & HVOT_BUS)) {
|
|
|
|
uint32 flags;
|
|
|
|
|
|
|
|
st->had_vehicle_of_type |= HVOT_BUS;
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(0, st->index);
|
2004-08-09 17:04:08 +00:00
|
|
|
flags = (v->owner == _local_player) ? NEWS_FLAGS(NM_THIN, NF_VIEWPORT|NF_VEHICLE, NT_ARRIVAL_PLAYER, 0) : NEWS_FLAGS(NM_THIN, NF_VIEWPORT|NF_VEHICLE, NT_ARRIVAL_OTHER, 0);
|
|
|
|
AddNewsItem(
|
2007-05-25 22:07:40 +00:00
|
|
|
v->u.road.roadtype == ROADTYPE_ROAD ? STR_902F_CITIZENS_CELEBRATE_FIRST : STR_902F_CITIZENS_CELEBRATE_FIRST_TRAM,
|
2004-08-09 17:04:08 +00:00
|
|
|
flags,
|
|
|
|
v->index,
|
|
|
|
0);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Check if station was ever visited before */
|
|
|
|
if (!(st->had_vehicle_of_type & HVOT_TRUCK)) {
|
|
|
|
uint32 flags;
|
|
|
|
|
|
|
|
st->had_vehicle_of_type |= HVOT_TRUCK;
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(0, st->index);
|
2004-08-09 17:04:08 +00:00
|
|
|
flags = (v->owner == _local_player) ? NEWS_FLAGS(NM_THIN, NF_VIEWPORT|NF_VEHICLE, NT_ARRIVAL_PLAYER, 0) : NEWS_FLAGS(NM_THIN, NF_VIEWPORT|NF_VEHICLE, NT_ARRIVAL_OTHER, 0);
|
|
|
|
AddNewsItem(
|
2007-05-25 22:07:40 +00:00
|
|
|
v->u.road.roadtype == ROADTYPE_ROAD ? STR_9030_CITIZENS_CELEBRATE_FIRST : STR_9030_CITIZENS_CELEBRATE_FIRST_TRAM,
|
2004-08-09 17:04:08 +00:00
|
|
|
flags,
|
|
|
|
v->index,
|
2006-02-18 14:41:24 +00:00
|
|
|
0
|
|
|
|
);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool RoadVehAccelerate(Vehicle *v)
|
|
|
|
{
|
2006-02-01 06:32:03 +00:00
|
|
|
uint spd = v->cur_speed + 1 + (v->u.road.overtaking != 0 ? 1 : 0);
|
2004-08-09 17:04:08 +00:00
|
|
|
byte t;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-03-28 20:41:35 +00:00
|
|
|
/* Clamp */
|
2004-08-09 17:04:08 +00:00
|
|
|
spd = min(spd, v->max_speed);
|
2007-02-24 18:44:30 +00:00
|
|
|
if (v->u.road.state == RVSB_WORMHOLE && !(v->vehstatus & VS_HIDDEN)) {
|
|
|
|
spd = min(spd, GetBridge(GetBridgeType(v->tile))->speed * 2);
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-03-28 20:41:35 +00:00
|
|
|
/* updates statusbar only if speed have changed to save CPU time */
|
2004-08-09 17:04:08 +00:00
|
|
|
if (spd != v->cur_speed) {
|
2004-09-10 19:02:27 +00:00
|
|
|
v->cur_speed = spd;
|
2006-02-01 06:32:03 +00:00
|
|
|
if (_patches.vehicle_speed) {
|
2004-12-21 23:27:58 +00:00
|
|
|
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
|
2006-02-01 06:32:03 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-03-28 20:41:35 +00:00
|
|
|
/* Decrease somewhat when turning */
|
2006-02-01 06:32:03 +00:00
|
|
|
if (!(v->direction & 1)) spd = spd * 3 >> 2;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-02-01 06:32:03 +00:00
|
|
|
if (spd == 0) return false;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-02-01 06:32:03 +00:00
|
|
|
if ((byte)++spd == 0) return true;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
v->progress = (t = v->progress) - (byte)spd;
|
|
|
|
|
|
|
|
return (t < v->progress);
|
|
|
|
}
|
|
|
|
|
2006-03-08 06:55:33 +00:00
|
|
|
static Direction RoadVehGetNewDirection(const Vehicle* 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
|
|
|
}
|
|
|
|
|
2006-03-08 06:55:33 +00:00
|
|
|
static Direction RoadVehGetSlidingDirection(const Vehicle* 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 {
|
2006-03-03 22:21:29 +00:00
|
|
|
const Vehicle* u;
|
|
|
|
const Vehicle* v;
|
2005-06-24 12:38:35 +00:00
|
|
|
TileIndex tile;
|
2007-10-04 20:16:35 +00:00
|
|
|
uint16 tilebits;
|
2007-03-07 12:11:48 +00:00
|
|
|
};
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-03-03 22:21:29 +00:00
|
|
|
static void* EnumFindVehToOvertake(Vehicle* v, void* data)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-01-10 18:56:51 +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-10-31 22:15:19 +00:00
|
|
|
v->tile == od->tile && 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
|
|
|
}
|
|
|
|
|
|
|
|
static bool FindRoadVehToOvertake(OvertakeData *od)
|
|
|
|
{
|
|
|
|
uint32 bits;
|
|
|
|
|
2007-10-04 20:16:35 +00:00
|
|
|
bits = GetTileTrackStatus(od->tile, TRANSPORT_ROAD, od->v->u.road.compatible_roadtypes);
|
|
|
|
bits |= bits >> 8;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-10-04 20:16:35 +00:00
|
|
|
if (!(od->tilebits & bits) || (bits & 0x3C3C) || (bits & 0x3F3F0000))
|
2004-08-09 17:04:08 +00:00
|
|
|
return true;
|
2006-03-03 22:21:29 +00:00
|
|
|
return VehicleFromPos(od->tile, od, EnumFindVehToOvertake) != NULL;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void RoadVehCheckOvertake(Vehicle *v, Vehicle *u)
|
|
|
|
{
|
|
|
|
OvertakeData od;
|
2007-10-04 20:16:35 +00:00
|
|
|
uint16 tt;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
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 */
|
|
|
|
if (v->u.road.roadtype == ROADTYPE_TRAM) return;
|
|
|
|
|
2007-10-04 20:16:35 +00:00
|
|
|
/* Don't overtake in stations */
|
|
|
|
if (IsTileType(v->tile, MP_STATION)) return;
|
|
|
|
|
2007-06-11 14:00:16 +00:00
|
|
|
/* For now, articulated road vehicles can't overtake anything. */
|
|
|
|
if (RoadVehHasArticPart(v)) return;
|
|
|
|
|
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 */
|
2007-02-23 22:14:14 +00:00
|
|
|
if (v->u.road.state >= RVSB_IN_ROAD_STOP || !IsStraightRoadTrackdir((Trackdir)(v->u.road.state & RVSB_TRACKDIR_MASK))) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-10-04 20:16:35 +00:00
|
|
|
tt = GetTileTrackStatus(v->tile, TRANSPORT_ROAD, v->u.road.compatible_roadtypes);
|
|
|
|
tt |= tt >> 8;
|
|
|
|
tt &= 0x3F;
|
|
|
|
|
2006-02-01 06:32:03 +00:00
|
|
|
if ((tt & 3) == 0) return;
|
|
|
|
if ((tt & 0x3C) != 0) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-02-01 06:32:03 +00:00
|
|
|
if (tt == 3) tt = (v->direction & 2) ? 2 : 1;
|
2004-08-09 17:04:08 +00:00
|
|
|
od.tilebits = tt;
|
|
|
|
|
|
|
|
od.tile = v->tile;
|
2006-02-01 06:32:03 +00:00
|
|
|
if (FindRoadVehToOvertake(&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));
|
2006-02-01 06:32:03 +00:00
|
|
|
if (FindRoadVehToOvertake(&od)) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-04-03 14:25:32 +00:00
|
|
|
if (od.u->cur_speed == 0 || od.u->vehstatus& VS_STOPPED) {
|
2004-08-09 17:04:08 +00:00
|
|
|
v->u.road.overtaking_ctr = 0x11;
|
|
|
|
v->u.road.overtaking = 0x10;
|
|
|
|
} else {
|
2006-02-01 06:32:03 +00:00
|
|
|
// if (FindRoadVehToOvertake(&od)) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
v->u.road.overtaking_ctr = 0;
|
|
|
|
v->u.road.overtaking = 0x10;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void RoadZPosAffectSpeed(Vehicle *v, byte old_z)
|
|
|
|
{
|
2006-02-13 21:15:00 +00:00
|
|
|
if (old_z == v->z_pos) 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-03-07 12:11:48 +00:00
|
|
|
struct FindRoadToChooseData {
|
2004-08-09 17:04:08 +00:00
|
|
|
TileIndex dest;
|
|
|
|
uint maxtracklen;
|
|
|
|
uint mindist;
|
2007-03-07 12:11:48 +00:00
|
|
|
};
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-01-10 18:56:51 +00:00
|
|
|
static bool EnumRoadTrackFindDist(TileIndex tile, void* data, Trackdir trackdir, uint length, byte* state)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-01-10 18:56:51 +00:00
|
|
|
FindRoadToChooseData* frd = (FindRoadToChooseData*)data;
|
2005-01-31 07:23:15 +00:00
|
|
|
uint dist = DistanceManhattan(tile, frd->dest);
|
2006-03-03 22:21:29 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (dist <= frd->mindist) {
|
|
|
|
if (dist != frd->mindist || length < frd->maxtracklen) {
|
|
|
|
frd->maxtracklen = length;
|
|
|
|
}
|
|
|
|
frd->mindist = dist;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-05-24 22:41:50 +00:00
|
|
|
static inline NPFFoundTargetData PerfNPFRouteToStationOrTile(TileIndex tile, Trackdir trackdir, NPFFindStationOrTileData* target, TransportType type, uint sub_type, Owner owner, RailTypeMask railtypes)
|
2006-05-27 16:12:16 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
void* perf = NpfBeginInterval();
|
2007-05-24 22:41:50 +00:00
|
|
|
NPFFoundTargetData ret = NPFRouteToStationOrTile(tile, trackdir, target, type, sub_type, owner, railtypes);
|
2006-05-27 16:12:16 +00:00
|
|
|
int t = NpfEndInterval(perf);
|
2006-12-26 17:36:18 +00:00
|
|
|
DEBUG(yapf, 4, "[NPFR] %d us - %d rounds - %d open - %d closed -- ", t, 0, _aystar_stats_open_size, _aystar_stats_closed_size);
|
2006-05-27 16:12:16 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|
|
|
|
static Trackdir RoadFindPathToDest(Vehicle* 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;
|
2004-08-09 17:04:08 +00:00
|
|
|
FindRoadToChooseData frd;
|
2007-02-13 20:57:02 +00:00
|
|
|
Trackdir best_track;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-08-24 19:19:18 +00:00
|
|
|
uint32 r = GetTileTrackStatus(tile, TRANSPORT_ROAD, v->u.road.compatible_roadtypes);
|
2007-02-13 20:57:02 +00:00
|
|
|
TrackdirBits signal = (TrackdirBits)GB(r, 16, 16);
|
|
|
|
TrackdirBits trackdirs = (TrackdirBits)GB(r, 0, 16);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-07-29 23:42:59 +00:00
|
|
|
if (IsTileType(tile, MP_ROAD)) {
|
2007-05-25 08:13:01 +00:00
|
|
|
if (GetRoadTileType(tile) == ROAD_TILE_DEPOT && (!IsTileOwner(tile, v->owner) || GetRoadDepotDirection(tile) == enterdir || (GetRoadTypes(tile) & v->u.road.compatible_roadtypes) == 0)) {
|
2006-08-29 19:46:31 +00:00
|
|
|
/* Road depot owned by another player 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
|
|
|
|
|
|
|
if (!IsTileOwner(tile, v->owner) || GetRoadStopDir(tile) == enterdir || RoadVehHasArticPart(v)) {
|
|
|
|
/* 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 */
|
2007-03-18 22:07:44 +00:00
|
|
|
RoadStop::Type rstype = IsCargoInClass(v->cargo_type, CC_PASSENGERS) ? 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 */
|
2007-08-24 19:19:18 +00:00
|
|
|
if (!_patches.roadveh_queue && IsStandardRoadStopTile(tile) &&
|
2007-02-13 00:25:42 +00:00
|
|
|
!GetRoadStopByTile(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
|
|
|
}
|
|
|
|
|
|
|
|
if (v->u.road.reverse_ctr != 0) {
|
2005-01-31 11:23:10 +00:00
|
|
|
/* What happens here?? */
|
2004-08-09 17:04:08 +00:00
|
|
|
v->u.road.reverse_ctr = 0;
|
2005-06-27 06:57:24 +00:00
|
|
|
if (v->tile != tile) {
|
2005-01-31 11:23:10 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2006-05-27 16:12:16 +00:00
|
|
|
if (_patches.yapf.road_use_yapf) {
|
|
|
|
Trackdir trackdir = YapfChooseRoadTrack(v, tile, enterdir);
|
|
|
|
if (trackdir != INVALID_TRACKDIR) return_track(trackdir);
|
2007-02-13 20:57:02 +00:00
|
|
|
return_track(PickRandomBit(trackdirs));
|
2006-05-27 16:12:16 +00:00
|
|
|
} else if (_patches.new_pathfinding_all) {
|
2005-01-31 11:23:10 +00:00
|
|
|
NPFFindStationOrTileData fstd;
|
|
|
|
NPFFoundTargetData ftd;
|
2007-01-10 18:56:51 +00:00
|
|
|
Trackdir trackdir;
|
2005-01-31 11:23:10 +00:00
|
|
|
|
|
|
|
NPFFillWithOrderData(&fstd, v);
|
(svn r2448) General cleanup of rail related code, more to follow.
* Add: rail.[ch] for rail-related enums and wrapper functions.
* Codechange: Removed dozens of magic numbers with below enums.
* Codechange: Rewrote CheckTrackCombination().
* Add: TILE_SIZE, TILE_PIXELS and TILE_HEIGHT constants.
* Add: enums RailTileType, RailTileSubtype, SignalType to mask against the map arrays.
* Add: enums Track, TrackBits, Trackdir, TrackdirBits for railway track data. (Note that the old RAIL_BIT constants are replaced by TRACK_BIT ones).
* Add: enums Direction and DiagDirection
* Codechange: Moved a bunch of track(dir) related lookup arrays from npf.[ch] to rail.[ch].
* Codechange: move RailType enum from tile.h to rail.h.
* Add: Wrapper functions for masking signal status in the map arrays: SignalAlongTrackdir, SignalAgainstTrackdir and SignalOnTrack.
* Add: Wrapper functions to access rail tiles, using above enums
* Add: Wrapper functions to modify tracks, trackdirs, directions, etc.
* Add: Wrapper functions for all lookup arrays in rail.[ch] (Arrays are still used in parts of the code)
* Codechange: Renamed some variables and arguments to better represent what they contain (railbit -> track, bits -> trackdirbits, etc.).
* Codechange: Don't use FindLandscapeHeight() in CmdRemoveSingleRail(), since it returns way too much info. Use GetTileSlope() instead.
* Codechange: [NPF] Removed some unused globals and code from npf.c.
2005-06-16 18:04:02 +00:00
|
|
|
trackdir = DiagdirToDiagTrackdir(enterdir);
|
2005-01-31 11:23:10 +00:00
|
|
|
//debug("Finding path. Enterdir: %d, Trackdir: %d", enterdir, trackdir);
|
|
|
|
|
2007-05-24 22:41:50 +00:00
|
|
|
ftd = PerfNPFRouteToStationOrTile(tile - TileOffsByDiagDir(enterdir), trackdir, &fstd, TRANSPORT_ROAD, v->u.road.compatible_roadtypes, v->owner, INVALID_RAILTYPE);
|
2007-02-13 20:57:02 +00:00
|
|
|
if (ftd.best_trackdir == INVALID_TRACKDIR) {
|
2007-03-28 20:41:35 +00:00
|
|
|
/* We are already at our target. Just do something
|
|
|
|
* @todo: maybe display error?
|
|
|
|
* @todo: go straight ahead if possible? */
|
2007-02-13 20:57:02 +00:00
|
|
|
return_track(FindFirstBit2x64(trackdirs));
|
2005-01-31 11:23:10 +00:00
|
|
|
} else {
|
2005-04-15 13:48:08 +00:00
|
|
|
/* If ftd.best_bird_dist is 0, we found our target and ftd.best_trackdir contains
|
|
|
|
the direction we need to take to get there, if ftd.best_bird_dist is not 0,
|
|
|
|
we did not find our target, but ftd.best_trackdir contains the direction leading
|
|
|
|
to the tile closest to our target. */
|
2005-01-31 11:23:10 +00:00
|
|
|
return_track(ftd.best_trackdir);
|
|
|
|
}
|
|
|
|
} else {
|
2006-03-08 15:29:23 +00:00
|
|
|
DiagDirection dir;
|
|
|
|
|
2007-07-29 23:42:59 +00:00
|
|
|
if (IsTileType(desttile, MP_ROAD)) {
|
2006-05-09 08:25:31 +00:00
|
|
|
if (GetRoadTileType(desttile) == ROAD_TILE_DEPOT) {
|
2006-03-08 15:29:23 +00:00
|
|
|
dir = GetRoadDepotDirection(desttile);
|
|
|
|
goto do_it;
|
|
|
|
}
|
2005-01-31 11:23:10 +00:00
|
|
|
} else if (IsTileType(desttile, MP_STATION)) {
|
2007-02-14 16:37:16 +00:00
|
|
|
/* For drive-through stops we can head for the actual station tile */
|
|
|
|
if (IsStandardRoadStopTile(desttile)) {
|
2006-03-31 19:10:54 +00:00
|
|
|
dir = GetRoadStopDir(desttile);
|
2004-08-09 17:04:08 +00:00
|
|
|
do_it:;
|
2005-01-31 11:23:10 +00:00
|
|
|
/* When we are heading for a depot or station, we just
|
|
|
|
* pretend we are heading for the tile in front, we'll
|
|
|
|
* see from there */
|
2006-09-05 23:21:41 +00:00
|
|
|
desttile += TileOffsByDiagDir(dir);
|
2007-02-13 20:57:02 +00:00
|
|
|
if (desttile == tile && trackdirs & _road_exit_dir_to_incoming_trackdirs[dir]) {
|
2005-01-31 11:23:10 +00:00
|
|
|
/* If we are already in front of the
|
|
|
|
* station/depot and we can get in from here,
|
|
|
|
* we enter */
|
2007-02-13 20:57:02 +00:00
|
|
|
return_track(FindFirstBit2x64(trackdirs & _road_exit_dir_to_incoming_trackdirs[dir]));
|
2005-01-31 11:23:10 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
}
|
2007-02-13 20:57:02 +00:00
|
|
|
/* Do some pathfinding */
|
2005-01-31 11:23:10 +00:00
|
|
|
frd.dest = desttile;
|
|
|
|
|
2007-02-13 20:57:02 +00:00
|
|
|
best_track = INVALID_TRACKDIR;
|
|
|
|
uint best_dist = (uint)-1;
|
|
|
|
uint best_maxlen = (uint)-1;
|
|
|
|
uint bitmask = (uint)trackdirs;
|
|
|
|
for (int i = 0; bitmask != 0; bitmask >>= 1, i++) {
|
2007-11-19 21:02:30 +00:00
|
|
|
if (HasBit(bitmask, 0)) {
|
2007-02-13 20:57:02 +00:00
|
|
|
if (best_track == INVALID_TRACKDIR) best_track = (Trackdir)i; // in case we don't find the path, just pick a track
|
2005-01-31 11:23:10 +00:00
|
|
|
frd.maxtracklen = (uint)-1;
|
|
|
|
frd.mindist = (uint)-1;
|
2007-05-24 22:41:50 +00:00
|
|
|
FollowTrack(tile, 0x2000 | TRANSPORT_ROAD, v->u.road.compatible_roadtypes, _road_pf_directions[i], EnumRoadTrackFindDist, NULL, &frd);
|
2005-01-31 11:23:10 +00:00
|
|
|
|
2007-02-13 20:57:02 +00:00
|
|
|
if (frd.mindist < best_dist || (frd.mindist == best_dist && frd.maxtracklen < best_maxlen)) {
|
2005-01-31 11:23:10 +00:00
|
|
|
best_dist = frd.mindist;
|
|
|
|
best_maxlen = frd.maxtracklen;
|
2007-02-13 20:57:02 +00:00
|
|
|
best_track = (Trackdir)i;
|
2005-01-31 11:23:10 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2007-02-13 20:57:02 +00:00
|
|
|
}
|
2005-01-31 11:23:10 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
found_best_track:;
|
|
|
|
|
2007-11-19 21:02:30 +00:00
|
|
|
if (HasBit(signal, 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
|
|
|
}
|
|
|
|
|
2006-03-02 08:55:12 +00:00
|
|
|
static uint RoadFindPathToStop(const Vehicle *v, TileIndex tile)
|
2005-01-29 19:41:44 +00:00
|
|
|
{
|
2006-08-14 23:11:59 +00:00
|
|
|
uint dist;
|
2006-05-30 10:53:27 +00:00
|
|
|
if (_patches.yapf.road_use_yapf) {
|
2007-03-28 20:41:35 +00:00
|
|
|
/* use YAPF */
|
2006-05-30 10:53:27 +00:00
|
|
|
dist = YapfRoadVehDistanceToTile(v, tile);
|
|
|
|
} else {
|
2007-03-28 20:41:35 +00:00
|
|
|
/* use NPF */
|
2006-05-30 10:53:27 +00:00
|
|
|
NPFFindStationOrTileData fstd;
|
2007-01-10 18:56:51 +00:00
|
|
|
Trackdir trackdir = GetVehicleTrackdir(v);
|
2007-02-13 20:57:02 +00:00
|
|
|
assert(trackdir != INVALID_TRACKDIR);
|
2005-01-29 19:41:44 +00:00
|
|
|
|
2006-05-30 10:53:27 +00:00
|
|
|
fstd.dest_coords = tile;
|
|
|
|
fstd.station_index = INVALID_STATION; // indicates that the destination is a tile, not a station
|
2005-01-29 19:41:44 +00:00
|
|
|
|
2007-05-24 22:41:50 +00:00
|
|
|
dist = NPFRouteToStationOrTile(v->tile, trackdir, &fstd, TRANSPORT_ROAD, v->u.road.compatible_roadtypes, v->owner, INVALID_RAILTYPE).best_path_dist;
|
2007-03-28 20:41:35 +00:00
|
|
|
/* change units from NPF_TILE_LENGTH to # of tiles */
|
2006-05-30 10:53:27 +00:00
|
|
|
if (dist != UINT_MAX)
|
|
|
|
dist = (dist + NPF_TILE_LENGTH - 1) / NPF_TILE_LENGTH;
|
|
|
|
}
|
|
|
|
return dist;
|
2005-01-29 19:41:44 +00:00
|
|
|
}
|
|
|
|
|
2007-02-14 01:00:57 +00:00
|
|
|
enum {
|
|
|
|
RDE_NEXT_TILE = 0x80,
|
|
|
|
RDE_TURNED = 0x40,
|
2007-02-14 10:33:36 +00:00
|
|
|
|
|
|
|
/* Start frames for when a vehicle enters a tile/changes its state.
|
|
|
|
* The start frame is different for vehicles that turned around or
|
2007-11-17 12:42:15 +00:00
|
|
|
* are leaving the depot as the do not start at the edge of the tile.
|
|
|
|
* For trams there are a few different start frames as there are two
|
|
|
|
* places where trams can turn. */
|
|
|
|
RVC_DEFAULT_START_FRAME = 0,
|
|
|
|
RVC_TURN_AROUND_START_FRAME = 1,
|
|
|
|
RVC_DEPOT_START_FRAME = 6,
|
|
|
|
RVC_START_FRAME_AFTER_LONG_TRAM = 22,
|
|
|
|
RVC_TURN_AROUND_START_FRAME_SHORT_TRAM = 16,
|
2007-02-14 16:37:16 +00:00
|
|
|
/* Stop frame for a vehicle in a drive-through stop */
|
2007-11-17 12:42:15 +00:00
|
|
|
RVC_DRIVE_THROUGH_STOP_FRAME = 7
|
2007-02-14 01:00:57 +00:00
|
|
|
};
|
|
|
|
|
2007-03-07 12:11:48 +00:00
|
|
|
struct RoadDriveEntry {
|
|
|
|
byte x, y;
|
|
|
|
};
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
#include "table/roadveh.h"
|
|
|
|
|
|
|
|
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
|
|
|
|
};
|
|
|
|
|
2007-06-11 14:00:16 +00:00
|
|
|
static bool RoadVehLeaveDepot(Vehicle *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. */
|
2007-08-30 13:03:56 +00:00
|
|
|
for (const Vehicle *u = v; u != NULL; u = u->Next()) {
|
2007-06-11 14:00:16 +00:00
|
|
|
if (u->u.road.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];
|
|
|
|
const RoadDriveEntry *rdp = _road_drive_data[v->u.road.roadtype][(_opt.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) {
|
|
|
|
if (RoadVehFindCloseTo(v, x, y, v->direction) != 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
|
|
|
}
|
|
|
|
|
|
|
|
BeginVehicleMove(v);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-06-11 14:00:16 +00:00
|
|
|
v->vehstatus &= ~VS_HIDDEN;
|
|
|
|
v->u.road.state = tdir;
|
|
|
|
v->u.road.frame = RVC_DEPOT_START_FRAME;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-07-01 19:11:47 +00:00
|
|
|
v->cur_image = v->GetImage(v->direction);
|
2007-06-11 14:00:16 +00:00
|
|
|
v->UpdateDeltaXY(v->direction);
|
|
|
|
SetRoadVehPosition(v,x,y);
|
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
|
|
|
|
2007-08-06 12:46:49 +00:00
|
|
|
static Trackdir FollowPreviousRoadVehicle(const Vehicle *v, const Vehicle *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
|
|
|
|
2007-06-11 14:00:16 +00:00
|
|
|
byte prev_state = prev->u.road.state;
|
|
|
|
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-06-11 14:00:16 +00:00
|
|
|
if (IsTunnelTile(tile)) {
|
|
|
|
diag_dir = GetTunnelDirection(tile);
|
|
|
|
} else if (IsBridgeTile(tile)) {
|
|
|
|
diag_dir = GetBridgeRampDirection(tile);
|
2007-07-29 23:42:59 +00:00
|
|
|
} else if (IsTileType(tile, MP_ROAD) && GetRoadTileType(tile) == ROAD_TILE_DEPOT) {
|
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;
|
|
|
|
dir = DiagdirToDiagTrackdir(diag_dir);
|
2007-11-19 21:02:30 +00:00
|
|
|
} else if (HasBit(prev_state, RVS_IN_DT_ROAD_STOP)) {
|
2007-06-11 14:00:16 +00:00
|
|
|
dir = (Trackdir)(prev_state & RVSB_ROAD_STOP_TRACKDIR_MASK);
|
|
|
|
} else if (prev_state < TRACKDIR_END) {
|
2007-11-12 18:28:55 +00:00
|
|
|
if (already_reversed && prev->tile != tile) {
|
|
|
|
/*
|
|
|
|
* The vehicle has reversed, but did not go straight back.
|
|
|
|
* It immediatelly turn onto another tile. This means that
|
|
|
|
* 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}.
|
|
|
|
*/
|
|
|
|
Trackdir reversed_turn_lookup[2][DIAGDIR_END] = {
|
|
|
|
{ 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)];
|
|
|
|
} else {
|
|
|
|
dir = (Trackdir)prev_state;
|
|
|
|
}
|
2007-06-11 14:00:16 +00:00
|
|
|
} else {
|
|
|
|
return INVALID_TRACKDIR;
|
|
|
|
}
|
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];
|
|
|
|
|
|
|
|
if ((required & GetAnyRoadBits(tile, v->u.road.roadtype)) == ROAD_NONE) {
|
|
|
|
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?
|
|
|
|
* @param t the tile to build on.
|
|
|
|
* @return true when a track track can be build on 't'
|
|
|
|
*/
|
|
|
|
static bool CanBuildTramTrackOnTile(TileIndex t)
|
|
|
|
{
|
|
|
|
switch (GetTileType(t)) {
|
|
|
|
case MP_CLEAR:
|
|
|
|
case MP_TREES:
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case MP_ROAD:
|
|
|
|
return GetRoadTileType(t) == ROAD_TILE_NORMAL;
|
|
|
|
|
|
|
|
case MP_WATER:
|
|
|
|
return IsCoast(t);
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-11 14:00:16 +00:00
|
|
|
static bool IndividualRoadVehicleController(Vehicle *v, const Vehicle *prev)
|
|
|
|
{
|
|
|
|
Direction new_dir;
|
|
|
|
Direction old_dir;
|
|
|
|
RoadDriveEntry rd;
|
|
|
|
int x,y;
|
|
|
|
uint32 r;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (v->u.road.overtaking != 0) {
|
|
|
|
if (++v->u.road.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 */
|
|
|
|
if (v->u.road.state < RVSB_IN_ROAD_STOP && IsStraightRoadTrackdir((Trackdir)v->u.road.state)) {
|
2006-01-06 17:25:39 +00:00
|
|
|
v->u.road.overtaking = 0;
|
2006-02-18 14:41:24 +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
|
|
|
|
2007-02-12 22:46:10 +00:00
|
|
|
/* Save old vehicle position to use at end of move to set viewport area dirty */
|
2004-08-09 17:04:08 +00:00
|
|
|
BeginVehicleMove(v);
|
|
|
|
|
2007-02-13 22:27:27 +00:00
|
|
|
if (v->u.road.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
|
|
|
|
2007-02-06 09:29:56 +00:00
|
|
|
const Vehicle *u = RoadVehFindCloseTo(v, gp.x, gp.y, v->direction);
|
2007-10-31 22:15:19 +00:00
|
|
|
if (u != NULL && u->First()->cur_speed < v->cur_speed) {
|
|
|
|
v->cur_speed = u->First()->cur_speed;
|
2007-06-11 14:00:16 +00:00
|
|
|
return false;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-11-19 21:02:30 +00:00
|
|
|
if ((IsTunnelTile(gp.new_tile) || IsBridgeTile(gp.new_tile)) && 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 */
|
2007-07-01 19:11:47 +00:00
|
|
|
v->cur_image = v->GetImage(v->direction);
|
2007-05-01 16:35:14 +00:00
|
|
|
v->UpdateDeltaXY(v->direction);
|
2004-08-09 17:04:08 +00:00
|
|
|
SetRoadVehPosition(v,gp.x,gp.y);
|
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;
|
|
|
|
VehiclePositionChanged(v);
|
2006-12-27 12:38:02 +00:00
|
|
|
if (!(v->vehstatus & VS_HIDDEN)) EndVehicleMove(v);
|
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.
|
|
|
|
* In this case v->u.road.state is masked to give the road stop entry direction. */
|
2007-05-25 22:07:40 +00:00
|
|
|
rd = _road_drive_data[v->u.road.roadtype][(
|
2007-11-19 21:02:30 +00:00
|
|
|
(HasBit(v->u.road.state, RVS_IN_DT_ROAD_STOP) ? v->u.road.state & RVSB_ROAD_STOP_TRACKDIR_MASK : v->u.road.state) +
|
2007-02-14 16:37:16 +00:00
|
|
|
(_opt.road_side << RVS_DRIVE_SIDE)) ^ v->u.road.overtaking][v->u.road.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
|
|
|
uint32 r;
|
2006-03-08 06:55:33 +00:00
|
|
|
Direction newdir;
|
2004-08-09 17:04:08 +00:00
|
|
|
const RoadDriveEntry *rdp;
|
|
|
|
|
2007-06-11 14:00:16 +00:00
|
|
|
if (IsRoadVehFront(v)) {
|
|
|
|
/* 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) {
|
2007-06-11 14:00:16 +00:00
|
|
|
if (!IsRoadVehFront(v)) 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 */
|
2007-05-25 22:07:40 +00:00
|
|
|
if (v->u.road.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) ||
|
|
|
|
(IsRoadVehFront(v) && IsTileType(tile, MP_ROAD) &&
|
|
|
|
GetRoadTileType(tile) == ROAD_TILE_NORMAL && !HasRoadWorks(tile) &&
|
|
|
|
(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.
|
|
|
|
*/
|
|
|
|
} else if (!IsRoadVehFront(v) || !CanBuildTramTrackOnTile(tile)) {
|
|
|
|
/*
|
|
|
|
* Taking the 'small' corner for trams only happens when:
|
|
|
|
* - We are not the from vehicle of an articulated tram.
|
|
|
|
* - Or when the player cannot build on the next tile.
|
|
|
|
*
|
|
|
|
* 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 {
|
|
|
|
/* The player 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
|
|
|
}
|
2007-07-29 23:42:59 +00:00
|
|
|
} else if (IsTileType(v->tile, MP_ROAD) && GetRoadTileType(v->tile) == ROAD_TILE_NORMAL && 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 */
|
2007-05-25 22:07:40 +00:00
|
|
|
rdp = _road_drive_data[v->u.road.roadtype][(dir + (_opt.road_side << RVS_DRIVE_SIDE)) ^ v->u.road.overtaking];
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-11-17 12:42:15 +00:00
|
|
|
x = TileX(tile) * TILE_SIZE + rdp[start_frame].x;
|
|
|
|
y = TileY(tile) * TILE_SIZE + rdp[start_frame].y;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-02-13 21:15:00 +00:00
|
|
|
newdir = RoadVehGetSlidingDirection(v, x, y);
|
2007-11-19 18:58:32 +00:00
|
|
|
if (IsRoadVehFront(v)) {
|
|
|
|
Vehicle *u = RoadVehFindCloseTo(v, x, y, newdir);
|
|
|
|
if (u != NULL) {
|
|
|
|
v->cur_speed = u->First()->cur_speed;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2007-11-24 10:38:43 +00:00
|
|
|
if (IsInsideMM(v->u.road.state, RVSB_IN_ROAD_STOP, RVSB_IN_DT_ROAD_STOP_END) && IsTileType(v->tile, MP_STATION)) {
|
|
|
|
if (IsReversingRoadTrackdir(dir) && IsInsideMM(v->u.road.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
|
|
|
}
|
2006-03-26 19:20:15 +00:00
|
|
|
if (IsRoadStop(v->tile)) {
|
2005-01-29 19:41:44 +00:00
|
|
|
RoadStop *rs = GetRoadStopByTile(v->tile, GetRoadStopType(v->tile));
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-14 16:37:16 +00:00
|
|
|
/* Vehicle is leaving a road stop tile, mark bay as free
|
|
|
|
* For drive-through stops, only do it if the vehicle stopped here */
|
2007-11-19 21:02:30 +00:00
|
|
|
if (IsStandardRoadStopTile(v->tile) || HasBit(v->u.road.state, RVS_IS_STOPPING)) {
|
|
|
|
rs->FreeBay(HasBit(v->u.road.state, RVS_USING_SECOND_BAY));
|
2007-11-19 21:32:20 +00:00
|
|
|
ClrBit(v->u.road.state, RVS_IS_STOPPING);
|
2007-02-14 16:37:16 +00:00
|
|
|
}
|
|
|
|
if (IsStandardRoadStopTile(v->tile)) rs->SetEntranceBusy(false);
|
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;
|
2006-01-06 17:25:39 +00:00
|
|
|
v->u.road.state = (byte)dir;
|
2007-11-17 12:42:15 +00:00
|
|
|
v->u.road.frame = start_frame;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
if (newdir != v->direction) {
|
|
|
|
v->direction = newdir;
|
|
|
|
v->cur_speed -= v->cur_speed >> 2;
|
|
|
|
}
|
|
|
|
|
2007-07-01 19:11:47 +00:00
|
|
|
v->cur_image = v->GetImage(newdir);
|
2007-05-01 16:35:14 +00:00
|
|
|
v->UpdateDeltaXY(v->direction);
|
2004-08-09 17:04:08 +00:00
|
|
|
RoadZPosAffectSpeed(v, SetRoadVehPosition(v, x, y));
|
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;
|
2004-08-09 17:04:08 +00:00
|
|
|
uint32 r;
|
2006-03-08 06:55:33 +00:00
|
|
|
Direction newdir;
|
2004-08-09 17:04:08 +00:00
|
|
|
const RoadDriveEntry *rdp;
|
|
|
|
|
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;
|
|
|
|
if (v->u.road.roadtype == ROADTYPE_TRAM && CountBits(tram = GetRoadBits(v->tile, ROADTYPE_TRAM)) == 1) {
|
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;
|
|
|
|
switch (tram) {
|
|
|
|
default: NOT_REACHED();
|
|
|
|
case ROAD_SW: dir = TRACKDIR_RVREV_NE; break;
|
|
|
|
case ROAD_NW: dir = TRACKDIR_RVREV_SE; break;
|
|
|
|
case ROAD_NE: dir = TRACKDIR_RVREV_SW; break;
|
|
|
|
case ROAD_SE: dir = TRACKDIR_RVREV_NW; break;
|
|
|
|
}
|
2007-08-06 12:46:49 +00:00
|
|
|
} else {
|
2007-11-17 12:42:15 +00:00
|
|
|
if (IsRoadVehFront(v)) {
|
|
|
|
/* 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
|
|
|
}
|
|
|
|
|
2007-05-25 22:07:40 +00:00
|
|
|
rdp = _road_drive_data[v->u.road.roadtype][(_opt.road_side << RVS_DRIVE_SIDE) + dir];
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-11-17 12:42:15 +00:00
|
|
|
x = TileX(v->tile) * TILE_SIZE + rdp[turn_around_start_frame].x;
|
|
|
|
y = TileY(v->tile) * TILE_SIZE + rdp[turn_around_start_frame].y;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-02-13 21:15:00 +00:00
|
|
|
newdir = RoadVehGetSlidingDirection(v, x, y);
|
2007-06-11 14:00:16 +00:00
|
|
|
if (IsRoadVehFront(v) && RoadVehFindCloseTo(v, x, y, newdir) != NULL) return false;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2007-02-14 10:33:36 +00:00
|
|
|
v->u.road.state = dir;
|
2007-11-17 12:42:15 +00:00
|
|
|
v->u.road.frame = turn_around_start_frame;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (newdir != v->direction) {
|
|
|
|
v->direction = newdir;
|
|
|
|
v->cur_speed -= v->cur_speed >> 2;
|
|
|
|
}
|
|
|
|
|
2007-07-01 19:11:47 +00:00
|
|
|
v->cur_image = v->GetImage(newdir);
|
2007-05-01 16:35:14 +00:00
|
|
|
v->UpdateDeltaXY(v->direction);
|
2004-08-09 17:04:08 +00:00
|
|
|
RoadZPosAffectSpeed(v, SetRoadVehPosition(v, x, y));
|
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. */
|
2007-08-30 13:03:56 +00:00
|
|
|
if (v->Next() != NULL &&
|
2007-07-29 23:42:59 +00:00
|
|
|
IsTileType(v->tile, MP_ROAD) && GetRoadTileType(v->tile) == ROAD_TILE_DEPOT) {
|
2007-06-11 14:00:16 +00:00
|
|
|
|
|
|
|
if (v->u.road.frame == v->u.road.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 */
|
2006-02-18 14:41:24 +00:00
|
|
|
x = (v->x_pos & ~15) + (rd.x & 15);
|
|
|
|
y = (v->y_pos & ~15) + (rd.y & 15);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
new_dir = RoadVehGetSlidingDirection(v, x, y);
|
|
|
|
|
2007-11-24 10:38:43 +00:00
|
|
|
if (IsRoadVehFront(v) && !IsInsideMM(v->u.road.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 */
|
2006-03-03 22:21:29 +00:00
|
|
|
Vehicle* u = RoadVehFindCloseTo(v, x, y, new_dir);
|
|
|
|
|
|
|
|
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 */
|
2006-03-03 22:21:29 +00:00
|
|
|
if (v->u.road.overtaking == 0) RoadVehCheckOvertake(v, u);
|
2007-10-04 20:16:35 +00:00
|
|
|
if (v->u.road.overtaking == 0) v->cur_speed = u->cur_speed;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
old_dir = v->direction;
|
|
|
|
if (new_dir != old_dir) {
|
|
|
|
v->direction = new_dir;
|
|
|
|
v->cur_speed -= (v->cur_speed >> 2);
|
|
|
|
if (old_dir != v->u.road.state) {
|
2007-02-12 22:46:10 +00:00
|
|
|
/* The vehicle is in a road stop */
|
2007-07-01 19:11:47 +00:00
|
|
|
v->cur_image = v->GetImage(new_dir);
|
2007-05-01 16:35:14 +00:00
|
|
|
v->UpdateDeltaXY(v->direction);
|
2004-08-09 17:04:08 +00:00
|
|
|
SetRoadVehPosition(v, v->x_pos, v->y_pos);
|
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) */
|
2007-11-24 10:38:43 +00:00
|
|
|
if (IsRoadVehFront(v) && ((IsInsideMM(v->u.road.state, RVSB_IN_ROAD_STOP, RVSB_IN_ROAD_STOP_END) &&
|
2007-02-14 16:37:16 +00:00
|
|
|
_road_veh_data_1[v->u.road.state - RVSB_IN_ROAD_STOP + (_opt.road_side << RVS_DRIVE_SIDE)] == v->u.road.frame) ||
|
2007-11-24 10:38:43 +00:00
|
|
|
(IsInsideMM(v->u.road.state, RVSB_IN_DT_ROAD_STOP, RVSB_IN_DT_ROAD_STOP_END) &&
|
2007-02-14 16:37:16 +00:00
|
|
|
v->current_order.dest == GetStationIndex(v->tile) &&
|
2007-03-18 22:07:44 +00:00
|
|
|
GetRoadStopType(v->tile) == (IsCargoInClass(v->cargo_type, CC_PASSENGERS) ? RoadStop::BUS : RoadStop::TRUCK) &&
|
2007-06-11 14:00:16 +00:00
|
|
|
v->u.road.frame == RVC_DRIVE_THROUGH_STOP_FRAME))) {
|
2007-02-14 16:37:16 +00:00
|
|
|
|
2005-01-29 19:41:44 +00:00
|
|
|
RoadStop *rs = GetRoadStopByTile(v->tile, GetRoadStopType(v->tile));
|
2006-03-24 08:55:08 +00:00
|
|
|
Station* st = GetStationByTile(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. */
|
2004-12-05 12:43:04 +00:00
|
|
|
if (v->current_order.type != OT_LEAVESTATION &&
|
|
|
|
v->current_order.type != OT_GOTO_DEPOT) {
|
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));
|
2007-03-18 22:07:44 +00:00
|
|
|
RoadStop::Type type = IsCargoInClass(v->cargo_type, CC_PASSENGERS) ? RoadStop::BUS : RoadStop::TRUCK;
|
2007-02-14 16:37:16 +00:00
|
|
|
|
|
|
|
/* Check if next inline bay is free */
|
|
|
|
if (IsDriveThroughStopTile(next_tile) && (GetRoadStopType(next_tile) == type)) {
|
|
|
|
RoadStop *rs_n = GetRoadStopByTile(next_tile, type);
|
|
|
|
|
2007-11-19 21:02:30 +00:00
|
|
|
if (rs_n->IsFreeBay(HasBit(v->u.road.state, RVS_USING_SECOND_BAY))) {
|
2007-02-14 16:37:16 +00:00
|
|
|
/* Bay in next stop along is free - use it */
|
|
|
|
ClearSlot(v);
|
|
|
|
rs_n->num_vehicles++;
|
|
|
|
v->u.road.slot = rs_n;
|
|
|
|
v->dest_tile = rs_n->xy;
|
|
|
|
v->u.road.slot_age = 14;
|
|
|
|
|
|
|
|
v->u.road.frame++;
|
|
|
|
RoadZPosAffectSpeed(v, SetRoadVehPosition(v, x, y));
|
2007-06-11 14:00:16 +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
|
|
|
|
2006-03-24 08:55:08 +00:00
|
|
|
v->last_station_visited = GetStationIndex(v->tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
RoadVehArrivesAt(v, st);
|
2007-01-13 18:55:54 +00:00
|
|
|
v->BeginLoading();
|
2004-08-09 17:04:08 +00:00
|
|
|
|
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
|
|
|
/* Vehicle is ready to leave a bay in a road stop */
|
2004-12-05 12:43:04 +00:00
|
|
|
if (v->current_order.type != OT_GOTO_DEPOT) {
|
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
|
|
|
}
|
2007-03-08 21:39:34 +00:00
|
|
|
v->current_order.Free();
|
2006-03-02 08:55:12 +00:00
|
|
|
ClearSlot(v);
|
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
|
|
|
|
2005-01-29 19:41:44 +00:00
|
|
|
if (rs == v->u.road.slot) {
|
2007-02-12 22:46:10 +00:00
|
|
|
/* We are leaving the correct station */
|
2006-03-02 08:55:12 +00:00
|
|
|
ClearSlot(v);
|
2005-01-29 19:41:44 +00:00
|
|
|
} else if (v->u.road.slot != NULL) {
|
2007-03-28 20:41:35 +00:00
|
|
|
/* We are leaving the wrong station
|
|
|
|
* XXX The question is .. what to do? Actually we shouldn't be here
|
|
|
|
* but I guess we need to clear the slot */
|
2006-12-26 17:36:18 +00:00
|
|
|
DEBUG(ms, 0, "Vehicle %d (index %d) arrived at wrong stop", v->unitnumber, v->index);
|
2006-03-03 22:21:29 +00:00
|
|
|
if (v->tile != v->dest_tile) {
|
2006-12-26 17:36:18 +00:00
|
|
|
DEBUG(ms, 2, " current tile 0x%X is not destination tile 0x%X. Route problem", v->tile, v->dest_tile);
|
2006-03-03 22:21:29 +00:00
|
|
|
}
|
|
|
|
if (v->dest_tile != v->u.road.slot->xy) {
|
2006-12-26 17:36:18 +00:00
|
|
|
DEBUG(ms, 2, " stop tile 0x%X is not destination tile 0x%X. Multistop desync", v->u.road.slot->xy, v->dest_tile);
|
2006-03-03 22:21:29 +00:00
|
|
|
}
|
2006-03-02 08:55:12 +00:00
|
|
|
if (v->current_order.type != OT_GOTO_STATION) {
|
2006-12-26 17:36:18 +00:00
|
|
|
DEBUG(ms, 2, " current order type (%d) is not OT_GOTO_STATION", v->current_order.type);
|
2006-03-02 08:55:12 +00:00
|
|
|
} else {
|
2006-09-03 08:25:27 +00:00
|
|
|
if (v->current_order.dest != st->index)
|
2006-12-26 17:36:18 +00:00
|
|
|
DEBUG(ms, 2, " current station %d is not target station in current_order.station (%d)",
|
2006-09-03 08:25:27 +00:00
|
|
|
st->index, v->current_order.dest);
|
2006-03-02 08:55:12 +00:00
|
|
|
}
|
|
|
|
|
2006-12-26 17:36:18 +00:00
|
|
|
DEBUG(ms, 2, " force a slot clearing");
|
2006-03-02 08:55:12 +00:00
|
|
|
ClearSlot(v);
|
2005-01-29 19:41:44 +00:00
|
|
|
}
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
StartRoadVehSound(v);
|
2004-12-21 23:27:58 +00:00
|
|
|
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
|
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 */
|
2004-08-09 17:04:08 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
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 */
|
2007-11-19 21:02:30 +00:00
|
|
|
if (!HasBit(r, VETS_ENTERED_WORMHOLE)) v->u.road.frame++;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-07-01 19:11:47 +00:00
|
|
|
v->cur_image = v->GetImage(v->direction);
|
2007-05-01 16:35:14 +00:00
|
|
|
v->UpdateDeltaXY(v->direction);
|
2004-08-09 17:04:08 +00:00
|
|
|
RoadZPosAffectSpeed(v, SetRoadVehPosition(v, x, y));
|
2007-06-11 14:00:16 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void RoadVehController(Vehicle *v)
|
|
|
|
{
|
|
|
|
/* decrease counters */
|
|
|
|
v->tick_counter++;
|
2007-06-20 19:17:22 +00:00
|
|
|
v->current_order_time++;
|
2007-06-11 14:00:16 +00:00
|
|
|
if (v->u.road.reverse_ctr != 0) v->u.road.reverse_ctr--;
|
|
|
|
|
|
|
|
/* handle crashed */
|
2007-08-26 20:16:02 +00:00
|
|
|
if (v->vehstatus & VS_CRASHED) {
|
2007-06-11 14:00:16 +00:00
|
|
|
RoadVehIsCrashed(v);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
RoadVehCheckTrainCrash(v);
|
|
|
|
|
|
|
|
/* road vehicle has broken down? */
|
|
|
|
if (v->breakdown_ctr != 0) {
|
|
|
|
if (v->breakdown_ctr <= 2) {
|
|
|
|
HandleBrokenRoadVeh(v);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
v->breakdown_ctr--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (v->vehstatus & VS_STOPPED) return;
|
|
|
|
|
|
|
|
ProcessRoadVehOrder(v);
|
|
|
|
v->HandleLoading();
|
|
|
|
|
|
|
|
if (v->current_order.type == OT_LOADING) return;
|
|
|
|
|
2007-08-29 21:49:08 +00:00
|
|
|
if (v->IsInDepot() && RoadVehLeaveDepot(v, true)) return;
|
2007-06-11 14:00:16 +00:00
|
|
|
|
|
|
|
/* Check if vehicle needs to proceed, return if it doesn't */
|
|
|
|
if (!RoadVehAccelerate(v)) return;
|
|
|
|
|
2007-08-30 13:03:56 +00:00
|
|
|
for (Vehicle *prev = NULL; v != NULL; prev = v, v = v->Next()) {
|
2007-06-11 14:00:16 +00:00
|
|
|
if (!IndividualRoadVehicleController(v, prev)) break;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void AgeRoadVehCargo(Vehicle *v)
|
|
|
|
{
|
2006-02-13 21:15:00 +00:00
|
|
|
if (_age_cargo_skip_counter != 0) return;
|
2007-06-22 11:58:59 +00:00
|
|
|
v->cargo.AgeCargo();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-07-01 19:24:54 +00:00
|
|
|
void RoadVehicle::Tick()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-07-01 19:24:54 +00:00
|
|
|
AgeRoadVehCargo(this);
|
2007-06-11 14:00:16 +00:00
|
|
|
|
2007-07-01 19:24:54 +00:00
|
|
|
if (IsRoadVehFront(this)) RoadVehController(this);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void CheckIfRoadVehNeedsService(Vehicle *v)
|
|
|
|
{
|
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 */
|
2007-08-31 17:13:39 +00:00
|
|
|
if (v->u.road.slot != NULL || _patches.servint_roadveh == 0 || !VehicleNeedsService(v)) return;
|
2007-08-29 21:49:08 +00:00
|
|
|
if (v->IsInDepot()) {
|
2006-09-03 11:49:38 +00:00
|
|
|
VehicleServiceInDepot(v);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-03-28 20:41:35 +00:00
|
|
|
/* XXX If we already have a depot order, WHY do we search over and over? */
|
2007-08-31 17:13:39 +00:00
|
|
|
const Depot *depot = FindClosestRoadDepot(v);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-02-06 10:18:47 +00:00
|
|
|
if (depot == NULL || DistanceManhattan(v->tile, depot->xy) > 12) {
|
2005-03-19 21:16:22 +00:00
|
|
|
if (v->current_order.type == OT_GOTO_DEPOT) {
|
2004-12-05 12:43:04 +00:00
|
|
|
v->current_order.type = OT_DUMMY;
|
|
|
|
v->current_order.flags = 0;
|
2004-12-21 23:27:58 +00:00
|
|
|
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-12-05 12:43:04 +00:00
|
|
|
if (v->current_order.type == OT_GOTO_DEPOT &&
|
|
|
|
v->current_order.flags & OF_NON_STOP &&
|
2006-02-18 14:41:24 +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
|
|
|
|
2007-05-14 08:07:20 +00:00
|
|
|
if (v->current_order.type == OT_LOADING) v->LeaveStation();
|
|
|
|
ClearSlot(v);
|
|
|
|
|
2004-12-05 12:43:04 +00:00
|
|
|
v->current_order.type = OT_GOTO_DEPOT;
|
|
|
|
v->current_order.flags = OF_NON_STOP;
|
2006-09-03 08:25:27 +00:00
|
|
|
v->current_order.dest = depot->index;
|
2005-02-06 10:18:47 +00:00
|
|
|
v->dest_tile = depot->xy;
|
2004-12-21 23:27:58 +00:00
|
|
|
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void OnNewDay_RoadVeh(Vehicle *v)
|
|
|
|
{
|
2007-06-18 10:48:15 +00:00
|
|
|
CommandCost cost;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-06-11 14:00:16 +00:00
|
|
|
if (!IsRoadVehFront(v)) return;
|
|
|
|
|
2006-02-13 21:15:00 +00:00
|
|
|
if ((++v->day_counter & 7) == 0) DecreaseVehicleValue(v);
|
|
|
|
if (v->u.road.blocked_ctr == 0) CheckVehicleBreakdown(v);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
AgeVehicle(v);
|
|
|
|
CheckIfRoadVehNeedsService(v);
|
|
|
|
|
2006-03-04 11:15:44 +00:00
|
|
|
CheckOrders(v);
|
2004-08-11 10:15:38 +00:00
|
|
|
|
2007-03-28 20:41:35 +00:00
|
|
|
/* Current slot has expired */
|
2006-04-08 14:18:39 +00:00
|
|
|
if (v->current_order.type == OT_GOTO_STATION && v->u.road.slot != NULL && v->u.road.slot_age-- == 0) {
|
2006-12-26 17:36:18 +00:00
|
|
|
DEBUG(ms, 3, "Slot expired for vehicle %d (index %d) at stop 0x%X",
|
2006-04-03 14:25:32 +00:00
|
|
|
v->unitnumber, v->index, v->u.road.slot->xy);
|
2006-03-02 08:55:12 +00:00
|
|
|
ClearSlot(v);
|
|
|
|
}
|
|
|
|
|
2006-03-16 16:47:39 +00:00
|
|
|
if (v->vehstatus & VS_STOPPED) return;
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
/* update destination */
|
2006-04-03 14:25:32 +00:00
|
|
|
if (v->current_order.type == OT_GOTO_STATION && v->u.road.slot == NULL && !(v->vehstatus & VS_CRASHED)) {
|
2007-08-24 19:19:18 +00:00
|
|
|
Station *st = GetStation(v->current_order.dest);
|
|
|
|
RoadStop *rs = st->GetPrimaryRoadStop(v);
|
|
|
|
RoadStop *best = NULL;
|
2006-03-02 08:55:12 +00:00
|
|
|
|
|
|
|
if (rs != NULL) {
|
2007-02-02 16:51:10 +00:00
|
|
|
/* We try to obtain a slot if:
|
|
|
|
* 1) we're reasonably close to the primary road stop
|
|
|
|
* or
|
|
|
|
* 2) we're somewhere close to the station rectangle (to make sure we do assign
|
|
|
|
* slots even if the station and its road stops are incredibly spread out)
|
|
|
|
*/
|
|
|
|
if (DistanceManhattan(v->tile, rs->xy) < 16 || st->rect.PtInExtendedRect(TileX(v->tile), TileY(v->tile), 2)) {
|
2006-04-03 14:25:32 +00:00
|
|
|
uint dist, badness;
|
|
|
|
uint minbadness = UINT_MAX;
|
2006-03-02 08:55:12 +00:00
|
|
|
|
2006-12-26 17:36:18 +00:00
|
|
|
DEBUG(ms, 2, "Attempting to obtain a slot for vehicle %d (index %d) at station %d (0x%X)",
|
2006-06-27 21:25:53 +00:00
|
|
|
v->unitnumber, v->index, st->index, st->xy
|
|
|
|
);
|
2006-03-02 08:55:12 +00:00
|
|
|
/* Now we find the nearest road stop that has a free slot */
|
2007-08-24 19:19:18 +00:00
|
|
|
for (; rs != NULL; rs = rs->GetNextRoadStop(v)) {
|
2006-04-03 14:25:32 +00:00
|
|
|
dist = RoadFindPathToStop(v, rs->xy);
|
|
|
|
if (dist == UINT_MAX) {
|
2006-12-26 17:36:18 +00:00
|
|
|
DEBUG(ms, 4, " stop 0x%X is unreachable, not treating further", rs->xy);
|
2006-03-02 08:55:12 +00:00
|
|
|
continue;
|
|
|
|
}
|
2006-05-30 10:53:27 +00:00
|
|
|
badness = (rs->num_vehicles + 1) * (rs->num_vehicles + 1) + dist;
|
2006-04-03 14:25:32 +00:00
|
|
|
|
2006-12-26 17:36:18 +00:00
|
|
|
DEBUG(ms, 4, " stop 0x%X has %d vehicle%s waiting", rs->xy, rs->num_vehicles, rs->num_vehicles == 1 ? "":"s");
|
|
|
|
DEBUG(ms, 4, " distance is %u", dist);
|
|
|
|
DEBUG(ms, 4, " badness %u", badness);
|
2006-03-02 08:55:12 +00:00
|
|
|
|
2006-04-03 14:25:32 +00:00
|
|
|
if (badness < minbadness) {
|
|
|
|
best = rs;
|
|
|
|
minbadness = badness;
|
2006-03-02 08:55:12 +00:00
|
|
|
}
|
2006-02-18 14:41:24 +00:00
|
|
|
}
|
2005-07-17 12:29:33 +00:00
|
|
|
|
2006-04-03 14:25:32 +00:00
|
|
|
if (best != NULL) {
|
|
|
|
best->num_vehicles++;
|
2006-12-26 17:36:18 +00:00
|
|
|
DEBUG(ms, 3, "Assigned to stop 0x%X", best->xy);
|
2006-03-02 08:55:12 +00:00
|
|
|
|
2006-04-03 14:25:32 +00:00
|
|
|
v->u.road.slot = best;
|
|
|
|
v->dest_tile = best->xy;
|
2006-03-02 08:55:12 +00:00
|
|
|
v->u.road.slot_age = 14;
|
|
|
|
} else {
|
2006-12-26 17:36:18 +00:00
|
|
|
DEBUG(ms, 3, "Could not find a suitable stop");
|
2005-07-17 13:45:43 +00:00
|
|
|
}
|
2006-03-02 08:55:12 +00:00
|
|
|
} else {
|
2006-12-26 17:36:18 +00:00
|
|
|
DEBUG(ms, 5, "Distance from station too far. Postponing slotting for vehicle %d (index %d) at station %d, (0x%X)",
|
2006-03-02 08:55:12 +00:00
|
|
|
v->unitnumber, v->index, st->index, st->xy);
|
2005-02-05 12:10:09 +00:00
|
|
|
}
|
2006-03-02 08:55:12 +00:00
|
|
|
} else {
|
2006-12-26 17:36:18 +00:00
|
|
|
DEBUG(ms, 4, "No road stop for vehicle %d (index %d) at station %d (0x%X)",
|
2006-03-02 08:55:12 +00:00
|
|
|
v->unitnumber, v->index, st->index, st->xy);
|
2005-01-29 19:41:44 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2004-12-03 21:57:05 +00:00
|
|
|
cost = RoadVehInfo(v->engine_type)->running_cost * _price.roadveh_running / 364;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-06-18 19:53:50 +00:00
|
|
|
v->profit_this_year -= cost.GetCost() >> 8;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
SET_EXPENSES_TYPE(EXPENSES_ROADVEH_RUN);
|
2007-06-18 19:53:50 +00:00
|
|
|
SubtractMoneyFromPlayerFract(v->owner, CommandCost(cost));
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
|
2005-01-24 22:24:47 +00:00
|
|
|
InvalidateWindowClasses(WC_ROADVEH_LIST);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
void RoadVehiclesYearlyLoop()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
Vehicle *v;
|
|
|
|
|
|
|
|
FOR_ALL_VEHICLES(v) {
|
2007-03-08 16:27:54 +00:00
|
|
|
if (v->type == VEH_ROAD) {
|
2004-08-09 17:04:08 +00:00
|
|
|
v->profit_last_year = v->profit_this_year;
|
|
|
|
v->profit_this_year = 0;
|
|
|
|
InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-06-09 07:45:26 +00:00
|
|
|
|
|
|
|
/** Refit a road vehicle to the specified cargo type
|
|
|
|
* @param tile unused
|
2007-04-17 21:09:38 +00:00
|
|
|
* @param flags operation to perform
|
2006-06-09 07:45:26 +00:00
|
|
|
* @param p1 Vehicle ID of the vehicle to refit
|
|
|
|
* @param p2 Bitstuffed elements
|
|
|
|
* - p2 = (bit 0-7) - the new cargo type to refit to
|
|
|
|
* - p2 = (bit 8-15) - the new cargo subtype to refit to
|
2007-08-06 13:05:45 +00:00
|
|
|
* - p2 = (bit 16) - refit only this vehicle
|
2007-04-29 08:43:00 +00:00
|
|
|
* @return cost of refit or error
|
2006-06-09 07:45:26 +00:00
|
|
|
*/
|
2007-06-18 10:48:15 +00:00
|
|
|
CommandCost CmdRefitRoadVeh(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
|
2006-06-09 07:45:26 +00:00
|
|
|
{
|
|
|
|
Vehicle *v;
|
2007-06-18 10:48:15 +00:00
|
|
|
CommandCost cost;
|
2006-06-09 07:45:26 +00:00
|
|
|
CargoID new_cid = GB(p2, 0, 8);
|
|
|
|
byte new_subtype = GB(p2, 8, 8);
|
2007-11-19 21:02:30 +00:00
|
|
|
bool only_this = HasBit(p2, 16);
|
2006-06-09 07:45:26 +00:00
|
|
|
uint16 capacity = CALLBACK_FAILED;
|
2007-08-06 13:05:45 +00:00
|
|
|
uint total_capacity = 0;
|
2006-06-09 07:45:26 +00:00
|
|
|
|
2006-08-22 18:15:17 +00:00
|
|
|
if (!IsValidVehicleID(p1)) return CMD_ERROR;
|
2006-06-09 07:45:26 +00:00
|
|
|
|
|
|
|
v = GetVehicle(p1);
|
|
|
|
|
2007-03-08 16:27:54 +00:00
|
|
|
if (v->type != VEH_ROAD || !CheckOwnership(v->owner)) return CMD_ERROR;
|
2007-06-11 14:00:16 +00:00
|
|
|
if (!CheckRoadVehInDepotStopped(v)) return_cmd_error(STR_9013_MUST_BE_STOPPED_INSIDE);
|
2006-06-09 07:45:26 +00:00
|
|
|
|
2007-08-06 13:05:45 +00:00
|
|
|
if (new_cid >= NUM_CARGO) return CMD_ERROR;
|
2006-06-09 07:45:26 +00:00
|
|
|
|
|
|
|
SET_EXPENSES_TYPE(EXPENSES_ROADVEH_RUN);
|
|
|
|
|
2007-08-30 13:03:56 +00:00
|
|
|
for (; v != NULL; v = v->Next()) {
|
2007-08-06 13:05:45 +00:00
|
|
|
/* XXX: We refit all the attached wagons en-masse if they can be
|
|
|
|
* refitted. This is how TTDPatch does it. TODO: Have some nice
|
|
|
|
* [Refit] button near each wagon. */
|
|
|
|
if (!CanRefitTo(v->engine_type, new_cid)) continue;
|
|
|
|
|
|
|
|
if (v->cargo_cap == 0) continue;
|
|
|
|
|
2007-11-19 21:02:30 +00:00
|
|
|
if (HasBit(EngInfo(v->engine_type)->callbackmask, CBM_VEHICLE_REFIT_CAPACITY)) {
|
2007-08-06 13:05:45 +00:00
|
|
|
/* Back up the cargo type */
|
|
|
|
CargoID temp_cid = v->cargo_type;
|
|
|
|
byte temp_subtype = v->cargo_subtype;
|
|
|
|
v->cargo_type = new_cid;
|
|
|
|
v->cargo_subtype = new_subtype;
|
|
|
|
|
|
|
|
/* Check the refit capacity callback */
|
|
|
|
capacity = GetVehicleCallback(CBID_VEHICLE_REFIT_CAPACITY, 0, 0, v->engine_type, v);
|
|
|
|
|
|
|
|
/* Restore the original cargo type */
|
|
|
|
v->cargo_type = temp_cid;
|
|
|
|
v->cargo_subtype = temp_subtype;
|
2006-06-09 07:45:26 +00:00
|
|
|
}
|
2007-08-06 13:05:45 +00:00
|
|
|
|
|
|
|
if (capacity == CALLBACK_FAILED) {
|
|
|
|
/* callback failed or not used, use default capacity */
|
|
|
|
const RoadVehicleInfo *rvi = RoadVehInfo(v->engine_type);
|
|
|
|
|
|
|
|
CargoID old_cid = rvi->cargo_type;
|
|
|
|
/* normally, the capacity depends on the cargo type, a vehicle can
|
|
|
|
* carry twice as much mail/goods as normal cargo, and four times as
|
|
|
|
* many passengers
|
|
|
|
*/
|
|
|
|
capacity = GetVehicleProperty(v, 0x0F, rvi->capacity);
|
|
|
|
switch (old_cid) {
|
|
|
|
case CT_PASSENGERS: break;
|
|
|
|
case CT_MAIL:
|
|
|
|
case CT_GOODS: capacity *= 2; break;
|
|
|
|
default: capacity *= 4; break;
|
|
|
|
}
|
|
|
|
switch (new_cid) {
|
|
|
|
case CT_PASSENGERS: break;
|
|
|
|
case CT_MAIL:
|
|
|
|
case CT_GOODS: capacity /= 2; break;
|
|
|
|
default: capacity /= 4; break;
|
|
|
|
}
|
2006-06-09 07:45:26 +00:00
|
|
|
}
|
|
|
|
|
2007-08-06 13:05:45 +00:00
|
|
|
if (capacity == 0) continue;
|
2006-06-09 07:45:26 +00:00
|
|
|
|
2007-08-06 13:05:45 +00:00
|
|
|
total_capacity += capacity;
|
|
|
|
|
|
|
|
if (IsHumanPlayer(v->owner) && new_cid != v->cargo_type) {
|
|
|
|
cost.AddCost(GetRefitCost(v->engine_type));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
v->cargo_cap = capacity;
|
|
|
|
v->cargo.Truncate((v->cargo_type == new_cid) ? capacity : 0);
|
|
|
|
v->cargo_type = new_cid;
|
|
|
|
v->cargo_subtype = new_subtype;
|
|
|
|
InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
|
|
|
|
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
|
|
|
|
RebuildVehicleLists();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (only_this) break;
|
2006-06-09 07:45:26 +00:00
|
|
|
}
|
|
|
|
|
2007-08-06 13:05:45 +00:00
|
|
|
_returned_refit_capacity = total_capacity;
|
|
|
|
|
2006-06-09 07:45:26 +00:00
|
|
|
return cost;
|
|
|
|
}
|