2005-07-24 16:12:37 +02:00
|
|
|
/* $Id$ */
|
|
|
|
|
2004-08-09 19:04:08 +02:00
|
|
|
#include "stdafx.h"
|
2005-06-02 21:30:21 +02:00
|
|
|
#include "openttd.h"
|
2005-02-05 16:58:59 +01:00
|
|
|
#include "debug.h"
|
2005-07-22 09:02:20 +02:00
|
|
|
#include "functions.h"
|
2006-03-05 11:19:33 +01:00
|
|
|
#include "road_map.h"
|
2006-06-05 12:23:18 +02:00
|
|
|
#include "roadveh.h"
|
2006-03-24 09:55:08 +01:00
|
|
|
#include "station_map.h"
|
2004-11-25 11:47:30 +01:00
|
|
|
#include "table/strings.h"
|
2004-12-15 23:18:54 +01:00
|
|
|
#include "map.h"
|
2005-01-29 13:19:05 +01:00
|
|
|
#include "tile.h"
|
2004-08-09 19:04:08 +02:00
|
|
|
#include "vehicle.h"
|
|
|
|
#include "engine.h"
|
|
|
|
#include "command.h"
|
|
|
|
#include "station.h"
|
|
|
|
#include "news.h"
|
|
|
|
#include "pathfind.h"
|
2005-01-31 12:23:10 +01:00
|
|
|
#include "npf.h"
|
2004-08-09 19:04:08 +02:00
|
|
|
#include "player.h"
|
2004-11-06 00:12:33 +01:00
|
|
|
#include "sound.h"
|
2005-02-06 11:18:47 +01:00
|
|
|
#include "depot.h"
|
2006-12-27 13:38:02 +01:00
|
|
|
#include "bridge.h"
|
2006-03-12 16:04:03 +01:00
|
|
|
#include "tunnel_map.h"
|
2006-12-27 13:38:02 +01:00
|
|
|
#include "bridge_map.h"
|
2005-05-03 01:59:11 +02:00
|
|
|
#include "vehicle_gui.h"
|
2006-06-09 09:45:26 +02:00
|
|
|
#include "newgrf_callbacks.h"
|
2006-02-03 13:55:21 +01:00
|
|
|
#include "newgrf_engine.h"
|
2006-08-09 23:11:45 +02:00
|
|
|
#include "newgrf_text.h"
|
2006-09-27 20:17:01 +02:00
|
|
|
#include "newgrf_sound.h"
|
2006-05-27 18:12:16 +02:00
|
|
|
#include "yapf/yapf.h"
|
2006-08-14 16:21:15 +02:00
|
|
|
#include "date.h"
|
2004-08-09 19:04:08 +02: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 23:21:29 +01: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 19:04:08 +02:00
|
|
|
};
|
|
|
|
|
2007-02-13 21:57:02 +01: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 19:04:08 +02:00
|
|
|
};
|
|
|
|
|
2007-02-13 21:57:02 +01:00
|
|
|
static const Trackdir _road_reverse_table[DIAGDIR_END] = {
|
|
|
|
TRACKDIR_RVREV_NE, TRACKDIR_RVREV_SE, TRACKDIR_RVREV_SW, TRACKDIR_RVREV_NW
|
2004-08-09 19:04:08 +02:00
|
|
|
};
|
|
|
|
|
2007-02-13 21:57:02 +01: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 19:04:08 +02:00
|
|
|
};
|
|
|
|
|
2007-02-13 23:27:27 +01: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
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Checks whether the trackdir means that we are reversing */
|
|
|
|
static bool IsReversingRoadTrackdir(Trackdir dir)
|
|
|
|
{
|
|
|
|
return (dir & 0x07) >= 6;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Checks whether the given trackdir is a straight road */
|
|
|
|
static bool IsStraightRoadTrackdir(Trackdir dir)
|
|
|
|
{
|
|
|
|
return (dir & 0x06) == 0;
|
|
|
|
}
|
|
|
|
|
2006-03-08 07:55:33 +01:00
|
|
|
int GetRoadVehImage(const Vehicle* v, Direction direction)
|
2004-08-09 19:04:08 +02:00
|
|
|
{
|
|
|
|
int img = v->spritenum;
|
|
|
|
int image;
|
|
|
|
|
|
|
|
if (is_custom_sprite(img)) {
|
|
|
|
image = GetCustomVehicleSprite(v, direction);
|
2006-02-06 10:18:04 +01:00
|
|
|
if (image != 0) return image;
|
2005-09-26 21:01:49 +02:00
|
|
|
img = orig_road_vehicle_info[v->engine_type - ROAD_ENGINES_INDEX].image_index;
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
2004-09-10 21:02:27 +02:00
|
|
|
|
2004-08-09 19:04:08 +02:00
|
|
|
image = direction + _roadveh_images[img];
|
2006-02-06 10:18:04 +01:00
|
|
|
if (v->cargo_count >= v->cargo_cap / 2) image += _roadveh_full_adder[img];
|
2004-08-09 19:04:08 +02:00
|
|
|
return image;
|
|
|
|
}
|
|
|
|
|
2007-01-14 20:57:49 +01:00
|
|
|
void DrawRoadVehEngine(int x, int y, EngineID engine, SpriteID pal)
|
2004-08-09 19:04:08 +02:00
|
|
|
{
|
2004-12-03 22:57:05 +01:00
|
|
|
int spritenum = RoadVehInfo(engine)->image_index;
|
2004-11-13 19:01:33 +01:00
|
|
|
|
|
|
|
if (is_custom_sprite(spritenum)) {
|
2006-03-12 18:42:04 +01:00
|
|
|
int sprite = GetCustomVehicleIcon(engine, DIR_W);
|
2004-11-13 19:01:33 +01:00
|
|
|
|
2006-02-13 22:15:00 +01:00
|
|
|
if (sprite != 0) {
|
2007-01-14 20:57:49 +01:00
|
|
|
DrawSprite(sprite, pal, x, y);
|
2004-11-13 19:01:33 +01:00
|
|
|
return;
|
|
|
|
}
|
2005-09-26 21:01:49 +02:00
|
|
|
spritenum = orig_road_vehicle_info[engine - ROAD_ENGINES_INDEX].image_index;
|
2004-11-13 19:01:33 +01:00
|
|
|
}
|
2007-01-14 20:57:49 +01:00
|
|
|
DrawSprite(6 + _roadveh_images[spritenum], pal, x, y);
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
2006-01-05 13:40:50 +01:00
|
|
|
static int32 EstimateRoadVehCost(EngineID engine_type)
|
2004-08-09 19:04:08 +02:00
|
|
|
{
|
2004-12-03 22:57:05 +01:00
|
|
|
return ((_price.roadveh_base >> 3) * RoadVehInfo(engine_type)->base_cost) >> 5;
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
2005-05-12 02:11:37 +02:00
|
|
|
/** Build a road vehicle.
|
2006-04-10 09:15:58 +02:00
|
|
|
* @param tile tile of depot where road vehicle is built
|
2005-05-12 02:11:37 +02:00
|
|
|
* @param p1 bus/truck type being built (engine)
|
2006-05-11 15:31:14 +02:00
|
|
|
* @param p2 bit 0 when set, the unitnumber will be 0, otherwise it will be a free number
|
2005-05-12 02:11:37 +02:00
|
|
|
*/
|
2006-04-10 09:15:58 +02:00
|
|
|
int32 CmdBuildRoadVeh(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
|
2004-08-09 19:04:08 +02:00
|
|
|
{
|
|
|
|
int32 cost;
|
|
|
|
Vehicle *v;
|
2005-02-04 15:24:23 +01:00
|
|
|
UnitID unit_num;
|
2004-08-09 19:04:08 +02:00
|
|
|
Engine *e;
|
2004-09-10 21:02:27 +02:00
|
|
|
|
2006-12-03 16:48:21 +01:00
|
|
|
if (!IsEngineBuildable(p1, VEH_Road, _current_player)) return_cmd_error(STR_ENGINE_NOT_BUILDABLE);
|
2005-01-27 22:00:05 +01:00
|
|
|
|
2004-08-09 19:04:08 +02:00
|
|
|
SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
|
|
|
|
|
|
|
|
cost = EstimateRoadVehCost(p1);
|
2005-05-12 02:11:37 +02:00
|
|
|
if (flags & DC_QUERY_COST) return cost;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2005-03-06 17:53:00 +01: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 13:56:32 +02:00
|
|
|
if (!IsTileOwner(tile, _current_player)) return CMD_ERROR;
|
2005-03-06 17:53:00 +01:00
|
|
|
|
2004-08-09 19:04:08 +02:00
|
|
|
v = AllocateVehicle();
|
2007-02-03 10:05:43 +01:00
|
|
|
if (v == NULL) return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME);
|
2004-08-09 19:04:08 +02:00
|
|
|
|
|
|
|
/* find the first free roadveh id */
|
2006-06-29 10:58:08 +02:00
|
|
|
unit_num = HASBIT(p2, 0) ? 0 : GetFreeUnitNumber(VEH_Road);
|
2004-08-09 19:04:08 +02:00
|
|
|
if (unit_num > _patches.max_roadveh)
|
|
|
|
return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME);
|
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
2006-04-10 09:15:58 +02:00
|
|
|
int x;
|
|
|
|
int y;
|
|
|
|
|
2004-12-03 22:57:05 +01:00
|
|
|
const RoadVehicleInfo *rvi = RoadVehInfo(p1);
|
|
|
|
|
2004-08-09 19:04:08 +02:00
|
|
|
v->unitnumber = unit_num;
|
2007-01-10 19:56:51 +01:00
|
|
|
v->direction = INVALID_DIR;
|
2004-08-09 19:04:08 +02:00
|
|
|
v->owner = _current_player;
|
|
|
|
|
|
|
|
v->tile = tile;
|
2006-04-23 21:35:36 +02:00
|
|
|
x = TileX(tile) * TILE_SIZE + TILE_SIZE / 2;
|
|
|
|
y = TileY(tile) * TILE_SIZE + TILE_SIZE / 2;
|
2004-08-09 19:04:08 +02:00
|
|
|
v->x_pos = x;
|
|
|
|
v->y_pos = y;
|
|
|
|
v->z_pos = GetSlopeZ(x,y);
|
|
|
|
v->z_height = 6;
|
|
|
|
|
2007-02-13 23:27:27 +01:00
|
|
|
v->u.road.state = RVSB_IN_DEPOT;
|
2004-08-09 19:04:08 +02:00
|
|
|
v->vehstatus = VS_HIDDEN|VS_STOPPED|VS_DEFPAL;
|
|
|
|
|
2004-12-03 22:57:05 +01:00
|
|
|
v->spritenum = rvi->image_index;
|
|
|
|
v->cargo_type = rvi->cargo_type;
|
2006-05-19 12:04:03 +02:00
|
|
|
v->cargo_subtype = 0;
|
2004-12-03 22:57:05 +01:00
|
|
|
v->cargo_cap = rvi->capacity;
|
2004-08-09 19:04:08 +02:00
|
|
|
// v->cargo_count = 0;
|
|
|
|
v->value = cost;
|
|
|
|
// 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 17:16:43 +01:00
|
|
|
v->last_station_visited = INVALID_STATION;
|
2004-12-03 22:57:05 +01:00
|
|
|
v->max_speed = rvi->max_speed;
|
2004-08-09 19:04:08 +02:00
|
|
|
v->engine_type = (byte)p1;
|
|
|
|
|
2005-06-07 20:13:49 +02:00
|
|
|
e = GetEngine(p1);
|
2004-08-09 19:04:08 +02:00
|
|
|
v->reliability = e->reliability;
|
|
|
|
v->reliability_spd_dec = e->reliability_spd_dec;
|
|
|
|
v->max_age = e->lifelength * 366;
|
2005-10-29 23:54:28 +02:00
|
|
|
_new_vehicle_id = v->index;
|
2004-09-10 21:02:27 +02:00
|
|
|
|
2004-08-09 19:04:08 +02:00
|
|
|
v->string_id = STR_SV_ROADVEH_NAME;
|
|
|
|
|
|
|
|
v->service_interval = _patches.servint_roadveh;
|
|
|
|
|
|
|
|
v->date_of_last_service = _date;
|
2006-08-20 21:05:28 +02:00
|
|
|
v->build_year = _cur_year;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
|
|
|
v->type = VEH_Road;
|
|
|
|
v->cur_image = 0xC15;
|
2005-12-28 23:29:59 +01:00
|
|
|
v->random_bits = VehicleRandomBits();
|
2004-08-09 19:04:08 +02:00
|
|
|
|
|
|
|
VehiclePositionChanged(v);
|
|
|
|
|
2006-10-05 14:59:28 +02:00
|
|
|
InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
|
2004-12-10 19:16:08 +01:00
|
|
|
RebuildVehicleLists();
|
2004-08-09 19:04:08 +02:00
|
|
|
InvalidateWindow(WC_COMPANY, v->owner);
|
2005-11-09 00:18:09 +01:00
|
|
|
if (IsLocalPlayer())
|
2007-02-06 12:11:12 +01:00
|
|
|
InvalidateAutoreplaceWindow(VEH_Road); // updates the replace Road window
|
|
|
|
|
|
|
|
GetPlayer(_current_player)->num_engines[p1]++;
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return cost;
|
|
|
|
}
|
|
|
|
|
2005-05-12 02:11:37 +02:00
|
|
|
/** Start/Stop a road vehicle.
|
2006-04-10 09:15:58 +02:00
|
|
|
* @param tile unused
|
2005-05-12 02:11:37 +02:00
|
|
|
* @param p1 road vehicle ID to start/stop
|
|
|
|
* @param p2 unused
|
|
|
|
*/
|
2006-04-10 09:15:58 +02:00
|
|
|
int32 CmdStartStopRoadVeh(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
|
2004-08-09 19:04:08 +02:00
|
|
|
{
|
|
|
|
Vehicle *v;
|
2006-08-09 23:11:45 +02:00
|
|
|
uint16 callback;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2006-08-22 20:15:17 +02:00
|
|
|
if (!IsValidVehicleID(p1)) return CMD_ERROR;
|
2005-01-30 21:50:06 +01:00
|
|
|
|
2005-01-06 23:31:58 +01:00
|
|
|
v = GetVehicle(p1);
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2005-05-12 02:11:37 +02:00
|
|
|
if (v->type != VEH_Road || !CheckOwnership(v->owner)) return CMD_ERROR;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2006-08-09 23:11:45 +02: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 19:04:08 +02:00
|
|
|
if (flags & DC_EXEC) {
|
2006-06-05 12:23:18 +02:00
|
|
|
if (IsRoadVehInDepotStopped(v)) {
|
2006-03-04 12:01:35 +01:00
|
|
|
DeleteVehicleNews(p1, STR_9016_ROAD_VEHICLE_IS_WAITING);
|
|
|
|
}
|
|
|
|
|
2004-08-09 19:04:08 +02:00
|
|
|
v->vehstatus ^= VS_STOPPED;
|
2004-12-22 00:27:58 +01:00
|
|
|
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
|
2004-08-09 19:04:08 +02:00
|
|
|
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-03-02 09:55:12 +01:00
|
|
|
void ClearSlot(Vehicle *v)
|
2005-04-09 00:44:06 +02:00
|
|
|
{
|
2006-03-02 09:55:12 +01:00
|
|
|
RoadStop *rs = v->u.road.slot;
|
|
|
|
if (v->u.road.slot == NULL) return;
|
|
|
|
|
2005-04-09 00:44:06 +02:00
|
|
|
v->u.road.slot = NULL;
|
|
|
|
v->u.road.slot_age = 0;
|
2006-03-02 09:55:12 +01:00
|
|
|
|
2006-04-03 16:25:32 +02:00
|
|
|
assert(rs->num_vehicles != 0);
|
|
|
|
rs->num_vehicles--;
|
|
|
|
|
2006-12-26 18:36:18 +01:00
|
|
|
DEBUG(ms, 3, "Clearing slot at 0x%X", rs->xy);
|
2005-04-09 00:44:06 +02:00
|
|
|
}
|
|
|
|
|
2005-05-12 02:11:37 +02:00
|
|
|
/** Sell a road vehicle.
|
2006-04-10 09:15:58 +02:00
|
|
|
* @param tile unused
|
2005-05-12 02:11:37 +02:00
|
|
|
* @param p1 vehicle ID to be sold
|
|
|
|
* @param p2 unused
|
|
|
|
*/
|
2006-04-10 09:15:58 +02:00
|
|
|
int32 CmdSellRoadVeh(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
|
2004-08-09 19:04:08 +02:00
|
|
|
{
|
|
|
|
Vehicle *v;
|
|
|
|
|
2006-08-22 20:15:17 +02:00
|
|
|
if (!IsValidVehicleID(p1)) return CMD_ERROR;
|
2004-09-10 21:02:27 +02:00
|
|
|
|
2005-01-06 23:31:58 +01:00
|
|
|
v = GetVehicle(p1);
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2005-05-12 02:11:37 +02:00
|
|
|
if (v->type != VEH_Road || !CheckOwnership(v->owner)) return CMD_ERROR;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2005-01-30 21:50:06 +01:00
|
|
|
SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
|
|
|
|
|
2006-06-05 12:23:18 +02:00
|
|
|
if (!IsRoadVehInDepotStopped(v)) {
|
2004-08-09 19:04:08 +02:00
|
|
|
return_cmd_error(STR_9013_MUST_BE_STOPPED_INSIDE);
|
2006-02-18 15:41:24 +01:00
|
|
|
}
|
2004-09-10 21:02:27 +02:00
|
|
|
|
2004-08-09 19:04:08 +02:00
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
// Invalidate depot
|
|
|
|
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
|
2004-12-10 19:16:08 +01:00
|
|
|
RebuildVehicleLists();
|
2004-08-09 19:04:08 +02:00
|
|
|
InvalidateWindow(WC_COMPANY, v->owner);
|
|
|
|
DeleteWindowById(WC_VEHICLE_VIEW, v->index);
|
2006-12-05 23:59:42 +01:00
|
|
|
DeleteDepotHighlightOfVehicle(v);
|
2004-08-09 19:04:08 +02:00
|
|
|
DeleteVehicle(v);
|
|
|
|
}
|
2004-09-10 21:02:27 +02:00
|
|
|
|
2004-08-09 19:04:08 +02:00
|
|
|
return -(int32)v->value;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct RoadFindDepotData {
|
|
|
|
uint best_length;
|
2005-06-24 14:38:35 +02:00
|
|
|
TileIndex tile;
|
2007-01-10 19:56:51 +01:00
|
|
|
OwnerByte owner;
|
2004-08-09 19:04:08 +02:00
|
|
|
} RoadFindDepotData;
|
|
|
|
|
2006-03-12 18:42:04 +01:00
|
|
|
static const DiagDirection _road_pf_directions[] = {
|
2007-01-10 19:56:51 +01: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 19:04:08 +02:00
|
|
|
};
|
|
|
|
|
2007-01-10 19:56:51 +01:00
|
|
|
static bool EnumRoadSignalFindDepot(TileIndex tile, void* data, Trackdir trackdir, uint length, byte* state)
|
2004-08-09 19:04:08 +02:00
|
|
|
{
|
2007-01-10 19:56:51 +01:00
|
|
|
RoadFindDepotData* rfdd = (RoadFindDepotData*)data;
|
2006-03-03 22:29:13 +01:00
|
|
|
|
2007-01-10 19:56:51 +01:00
|
|
|
tile += TileOffsByDiagDir(_road_pf_directions[trackdir]);
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2005-01-16 12:24:58 +01:00
|
|
|
if (IsTileType(tile, MP_STREET) &&
|
2006-05-09 10:25:31 +02:00
|
|
|
GetRoadTileType(tile) == ROAD_TILE_DEPOT &&
|
2006-03-03 23:21:29 +01:00
|
|
|
IsTileOwner(tile, rfdd->owner) &&
|
|
|
|
length < rfdd->best_length) {
|
|
|
|
rfdd->best_length = length;
|
|
|
|
rfdd->tile = tile;
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-02-18 15:41:24 +01:00
|
|
|
static const Depot* FindClosestRoadDepot(const Vehicle* v)
|
2004-08-09 19:04:08 +02:00
|
|
|
{
|
2005-06-24 14:38:35 +02:00
|
|
|
TileIndex tile = v->tile;
|
2004-09-10 21:02:27 +02:00
|
|
|
|
2006-05-27 18:12:16 +02:00
|
|
|
if (_patches.yapf.road_use_yapf) {
|
|
|
|
Depot* ret = YapfFindNearestRoadDepot(v);
|
|
|
|
return ret;
|
|
|
|
} else if (_patches.new_pathfinding_all) {
|
2005-01-31 12:23:10 +01: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 20:04:02 +02:00
|
|
|
Trackdir trackdir = GetVehicleTrackdir(v);
|
2005-05-02 19:52:35 +02:00
|
|
|
|
2005-07-03 15:02:54 +02:00
|
|
|
ftd = NPFRouteToDepotBreadthFirst(v->tile, trackdir, TRANSPORT_ROAD, v->owner, INVALID_RAILTYPE);
|
2006-02-13 22:15:00 +01:00
|
|
|
if (ftd.best_bird_dist == 0) {
|
2005-01-31 12:23:10 +01:00
|
|
|
return GetDepotByTile(ftd.node.tile); /* Target found */
|
2006-02-13 22:15:00 +01:00
|
|
|
} else {
|
2005-02-06 11:18:47 +01:00
|
|
|
return NULL; /* Target not found */
|
2006-02-13 22:15:00 +01:00
|
|
|
}
|
2005-01-31 12:23:10 +01: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 21:28:28 +01:00
|
|
|
|
2005-01-31 12:23:10 +01:00
|
|
|
rfdd.owner = v->owner;
|
|
|
|
rfdd.best_length = (uint)-1;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2005-01-31 12:23:10 +01:00
|
|
|
/* search in all directions */
|
2007-01-10 19:56:51 +01:00
|
|
|
for (DiagDirection i = DIAGDIR_BEGIN; i != DIAGDIR_END; i++) {
|
2006-03-03 22:29:13 +01:00
|
|
|
FollowTrack(tile, 0x2000 | TRANSPORT_ROAD, i, EnumRoadSignalFindDepot, NULL, &rfdd);
|
2006-02-18 15:41:24 +01:00
|
|
|
}
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2006-02-13 22:15:00 +01:00
|
|
|
if (rfdd.best_length == (uint)-1) return NULL;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2005-01-31 12:23:10 +01:00
|
|
|
return GetDepotByTile(rfdd.tile);
|
|
|
|
}
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
2005-05-12 02:11:37 +02:00
|
|
|
/** Send a road vehicle to the depot.
|
2006-04-10 09:15:58 +02:00
|
|
|
* @param tile unused
|
2005-05-12 02:11:37 +02:00
|
|
|
* @param p1 vehicle ID to send to the depot
|
2006-08-30 01:39:57 +02:00
|
|
|
* @param p2 various bitmasked elements
|
2006-09-01 12:24:15 +02:00
|
|
|
* - p2 bit 0-3 - DEPOT_ flags (see vehicle.h)
|
|
|
|
* - p2 bit 8-10 - VLW flag (for mass goto depot)
|
2005-05-12 02:11:37 +02:00
|
|
|
*/
|
2006-04-10 09:15:58 +02:00
|
|
|
int32 CmdSendRoadVehToDepot(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
|
2004-08-09 19:04:08 +02:00
|
|
|
{
|
2005-01-30 21:50:06 +01:00
|
|
|
Vehicle *v;
|
2005-05-12 02:11:37 +02:00
|
|
|
const Depot *dep;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2006-09-01 12:24:15 +02:00
|
|
|
if (p2 & DEPOT_MASS_SEND) {
|
|
|
|
/* Mass goto depot requested */
|
2006-09-04 17:16:58 +02:00
|
|
|
if (!ValidVLWFlags(p2 & VLW_MASK)) return CMD_ERROR;
|
|
|
|
return SendAllVehiclesToDepot(VEH_Road, flags, p2 & DEPOT_SERVICE, _current_player, (p2 & VLW_MASK), p1);
|
2006-08-30 23:39:01 +02:00
|
|
|
}
|
|
|
|
|
2006-09-01 12:24:15 +02:00
|
|
|
if (!IsValidVehicleID(p1)) return CMD_ERROR;
|
2005-01-30 21:50:06 +01:00
|
|
|
|
|
|
|
v = GetVehicle(p1);
|
|
|
|
|
2006-09-01 12:24:15 +02:00
|
|
|
if (v->type != VEH_Road || !CheckOwnership(v->owner)) return CMD_ERROR;
|
2006-08-30 01:39:57 +02:00
|
|
|
|
2006-09-01 12:24:15 +02:00
|
|
|
if (v->vehstatus & VS_CRASHED) return CMD_ERROR;
|
2005-05-03 21:31:33 +02:00
|
|
|
|
2006-09-03 00:47:45 +02:00
|
|
|
if (IsRoadVehInDepot(v)) return CMD_ERROR;
|
|
|
|
|
2005-05-12 02:11:37 +02:00
|
|
|
/* If the current orders are already goto-depot */
|
2004-12-05 13:43:04 +01:00
|
|
|
if (v->current_order.type == OT_GOTO_DEPOT) {
|
2006-09-02 11:35:03 +02:00
|
|
|
if (!!(p2 & DEPOT_SERVICE) == HASBIT(v->current_order.flags, OFB_HALT_IN_DEPOT)) {
|
2006-09-01 14:37:03 +02:00
|
|
|
/* We called with a different DEPOT_SERVICE setting.
|
2006-09-04 22:40:33 +02: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 14:37:03 +02:00
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
TOGGLEBIT(v->current_order.flags, OFB_HALT_IN_DEPOT);
|
|
|
|
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-09-01 12:24:15 +02:00
|
|
|
if (p2 & DEPOT_DONT_CANCEL) return CMD_ERROR; // Requested no cancelation of depot orders
|
2004-08-09 19:04:08 +02:00
|
|
|
if (flags & DC_EXEC) {
|
2005-05-12 02:11:37 +02: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 */
|
2005-03-20 09:43:29 +01:00
|
|
|
if (HASBIT(v->current_order.flags, OFB_PART_OF_ORDERS))
|
2004-08-09 19:04:08 +02:00
|
|
|
v->cur_order_index++;
|
2005-03-20 09:43:29 +01:00
|
|
|
|
2004-12-05 13:43:04 +01:00
|
|
|
v->current_order.type = OT_DUMMY;
|
|
|
|
v->current_order.flags = 0;
|
2004-12-22 00:27:58 +01:00
|
|
|
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-05-12 02:11:37 +02:00
|
|
|
dep = FindClosestRoadDepot(v);
|
2006-09-01 12:24:15 +02:00
|
|
|
if (dep == NULL) return_cmd_error(STR_9019_UNABLE_TO_FIND_LOCAL_DEPOT);
|
2004-08-09 19:04:08 +02:00
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
2006-03-16 17:47:39 +01:00
|
|
|
ClearSlot(v);
|
2004-12-05 13:43:04 +01:00
|
|
|
v->current_order.type = OT_GOTO_DEPOT;
|
2006-08-27 11:28:52 +02:00
|
|
|
v->current_order.flags = OF_NON_STOP;
|
2006-09-01 12:24:15 +02:00
|
|
|
if (!(p2 & DEPOT_SERVICE)) SETBIT(v->current_order.flags, OFB_HALT_IN_DEPOT);
|
2006-12-06 00:57:04 +01:00
|
|
|
v->current_order.refit_cargo = CT_INVALID;
|
2006-09-03 10:25:27 +02:00
|
|
|
v->current_order.dest = dep->index;
|
2005-05-12 02:11:37 +02:00
|
|
|
v->dest_tile = dep->xy;
|
2004-12-22 00:27:58 +01:00
|
|
|
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
2004-09-10 21:02:27 +02:00
|
|
|
|
2004-08-09 19:04:08 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-05-12 02:11:37 +02:00
|
|
|
/** Turn a roadvehicle around.
|
2006-04-10 09:15:58 +02:00
|
|
|
* @param tile unused
|
2005-05-12 02:11:37 +02:00
|
|
|
* @param p1 vehicle ID to turn
|
|
|
|
* @param p2 unused
|
|
|
|
*/
|
2006-04-10 09:15:58 +02:00
|
|
|
int32 CmdTurnRoadVeh(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
|
2004-08-09 19:04:08 +02:00
|
|
|
{
|
|
|
|
Vehicle *v;
|
|
|
|
|
2006-08-22 20:15:17 +02:00
|
|
|
if (!IsValidVehicleID(p1)) return CMD_ERROR;
|
2005-01-30 21:50:06 +01:00
|
|
|
|
2005-01-06 23:31:58 +01:00
|
|
|
v = GetVehicle(p1);
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2005-05-12 02:11:37 +02:00
|
|
|
if (v->type != VEH_Road || !CheckOwnership(v->owner)) return CMD_ERROR;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2006-12-27 13:38:02 +01:00
|
|
|
if (v->vehstatus & VS_STOPPED ||
|
2004-08-09 19:04:08 +02:00
|
|
|
v->u.road.crashed_ctr != 0 ||
|
|
|
|
v->breakdown_ctr != 0 ||
|
|
|
|
v->u.road.overtaking != 0 ||
|
2007-02-13 23:27:27 +01:00
|
|
|
v->u.road.state == RVSB_WORMHOLE ||
|
2006-12-27 13:38:02 +01:00
|
|
|
IsRoadVehInDepot(v) ||
|
2004-08-09 19:04:08 +02:00
|
|
|
v->cur_speed < 5) {
|
|
|
|
return CMD_ERROR;
|
|
|
|
}
|
2004-09-10 21:02:27 +02:00
|
|
|
|
2006-12-27 13:38:02 +01: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 23:21:29 +01:00
|
|
|
if (flags & DC_EXEC) v->u.road.reverse_ctr = 180;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void MarkRoadVehDirty(Vehicle *v)
|
|
|
|
{
|
|
|
|
v->cur_image = GetRoadVehImage(v, v->direction);
|
|
|
|
MarkAllViewportsDirty(v->left_coord, v->top_coord, v->right_coord + 1, v->bottom_coord + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void UpdateRoadVehDeltaXY(Vehicle *v)
|
|
|
|
{
|
|
|
|
#define MKIT(a,b,c,d) ((a&0xFF)<<24) | ((b&0xFF)<<16) | ((c&0xFF)<<8) | ((d&0xFF)<<0)
|
|
|
|
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
|
|
|
|
uint32 x = _delta_xy_table[v->direction];
|
2005-07-21 08:31:02 +02:00
|
|
|
v->x_offs = GB(x, 0, 8);
|
|
|
|
v->y_offs = GB(x, 8, 8);
|
|
|
|
v->sprite_width = GB(x, 16, 8);
|
|
|
|
v->sprite_height = GB(x, 24, 8);
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void ClearCrashedStation(Vehicle *v)
|
|
|
|
{
|
2005-11-17 11:12:21 +01:00
|
|
|
RoadStop *rs = GetRoadStopByTile(v->tile, GetRoadStopType(v->tile));
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2007-02-13 01:25:42 +01:00
|
|
|
/* Mark the station entrance as not busy */
|
|
|
|
rs->SetEntranceBusy(false);
|
2004-09-10 21:02:27 +02:00
|
|
|
|
2007-02-13 01:25:42 +01:00
|
|
|
/* Free the parking bay */
|
2007-02-13 23:27:27 +01:00
|
|
|
rs->FreeBay(HASBIT(v->u.road.state, RVS_USING_SECOND_BAY) ? 1 : 0);
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void RoadVehDelete(Vehicle *v)
|
|
|
|
{
|
|
|
|
DeleteWindowById(WC_VEHICLE_VIEW, v->index);
|
2004-09-10 21:02:27 +02:00
|
|
|
|
2004-12-10 19:16:08 +01:00
|
|
|
RebuildVehicleLists();
|
2004-08-09 19:04:08 +02:00
|
|
|
InvalidateWindow(WC_COMPANY, v->owner);
|
|
|
|
|
2006-02-13 22:15:00 +01:00
|
|
|
if (IsTileType(v->tile, MP_STATION)) ClearCrashedStation(v);
|
2004-09-10 21:02:27 +02:00
|
|
|
|
2004-08-09 19:04:08 +02:00
|
|
|
BeginVehicleMove(v);
|
|
|
|
EndVehicleMove(v);
|
2004-09-10 21:02:27 +02:00
|
|
|
|
2004-08-09 19:04:08 +02:00
|
|
|
DeleteVehicle(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
static byte SetRoadVehPosition(Vehicle *v, int x, int y)
|
|
|
|
{
|
|
|
|
byte new_z, old_z;
|
|
|
|
|
|
|
|
// need this hint so it returns the right z coordinate on bridges.
|
2006-02-13 22:15:00 +01:00
|
|
|
v->x_pos = x;
|
|
|
|
v->y_pos = y;
|
|
|
|
new_z = GetSlopeZ(x, y);
|
2004-08-09 19:04:08 +02:00
|
|
|
|
|
|
|
old_z = v->z_pos;
|
|
|
|
v->z_pos = new_z;
|
2004-09-10 21:02:27 +02:00
|
|
|
|
2004-08-09 19:04:08 +02:00
|
|
|
VehiclePositionChanged(v);
|
|
|
|
EndVehicleMove(v);
|
|
|
|
return old_z;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void RoadVehSetRandomDirection(Vehicle *v)
|
|
|
|
{
|
2006-03-08 09:16:31 +01:00
|
|
|
static const DirDiff delta[] = {
|
|
|
|
DIRDIFF_45LEFT, DIRDIFF_SAME, DIRDIFF_SAME, DIRDIFF_45RIGHT
|
|
|
|
};
|
|
|
|
|
2004-08-09 19:04:08 +02:00
|
|
|
uint32 r = Random();
|
2006-03-08 09:16:31 +01:00
|
|
|
|
|
|
|
v->direction = ChangeDir(v->direction, delta[r & 3]);
|
2004-08-09 19:04:08 +02:00
|
|
|
BeginVehicleMove(v);
|
|
|
|
UpdateRoadVehDeltaXY(v);
|
|
|
|
v->cur_image = GetRoadVehImage(v, v->direction);
|
|
|
|
SetRoadVehPosition(v, v->x_pos, v->y_pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void RoadVehIsCrashed(Vehicle *v)
|
|
|
|
{
|
|
|
|
v->u.road.crashed_ctr++;
|
|
|
|
if (v->u.road.crashed_ctr == 2) {
|
2005-02-12 16:53:32 +01:00
|
|
|
CreateEffectVehicleRel(v, 4, 4, 8, EV_EXPLOSION_LARGE);
|
2004-08-09 19:04:08 +02:00
|
|
|
} else if (v->u.road.crashed_ctr <= 45) {
|
2006-02-13 22:15:00 +01:00
|
|
|
if ((v->tick_counter & 7) == 0) RoadVehSetRandomDirection(v);
|
2004-08-09 19:04:08 +02:00
|
|
|
} else if (v->u.road.crashed_ctr >= 2220) {
|
|
|
|
RoadVehDelete(v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-03-03 23:21:29 +01:00
|
|
|
static void* EnumCheckRoadVehCrashTrain(Vehicle* v, void* data)
|
2004-08-09 19:04:08 +02:00
|
|
|
{
|
2007-01-10 19:56:51 +01:00
|
|
|
const Vehicle* u = (Vehicle*)data;
|
2006-03-03 23:21:29 +01:00
|
|
|
|
2006-02-06 10:18:04 +01:00
|
|
|
return
|
|
|
|
v->type == VEH_Train &&
|
|
|
|
myabs(v->z_pos - u->z_pos) <= 6 &&
|
|
|
|
myabs(v->x_pos - u->x_pos) <= 4 &&
|
|
|
|
myabs(v->y_pos - u->y_pos) <= 4 ?
|
|
|
|
v : NULL;
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void RoadVehCrash(Vehicle *v)
|
|
|
|
{
|
|
|
|
uint16 pass;
|
|
|
|
|
|
|
|
v->u.road.crashed_ctr++;
|
|
|
|
v->vehstatus |= VS_CRASHED;
|
2006-03-16 17:47:39 +01:00
|
|
|
ClearSlot(v);
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2004-12-22 00:27:58 +01:00
|
|
|
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
|
2004-08-09 19:04:08 +02:00
|
|
|
|
|
|
|
pass = 1;
|
2006-02-06 10:18:04 +01:00
|
|
|
if (v->cargo_type == CT_PASSENGERS) pass += v->cargo_count;
|
2004-08-09 19:04:08 +02:00
|
|
|
v->cargo_count = 0;
|
|
|
|
|
2006-02-01 07:32:03 +01:00
|
|
|
SetDParam(0, pass);
|
2005-08-01 15:01:14 +02:00
|
|
|
AddNewsItem(
|
|
|
|
(pass == 1) ?
|
|
|
|
STR_9031_ROAD_VEHICLE_CRASH_DRIVER : STR_9032_ROAD_VEHICLE_CRASH_DIE,
|
2004-08-09 19:04:08 +02:00
|
|
|
NEWS_FLAGS(NM_THIN, NF_VIEWPORT|NF_VEHICLE, NT_ACCIDENT, 0),
|
|
|
|
v->index,
|
2006-02-18 15:41:24 +01:00
|
|
|
0
|
|
|
|
);
|
2004-08-09 19:04:08 +02:00
|
|
|
|
|
|
|
ModifyStationRatingAround(v->tile, v->owner, -160, 22);
|
2004-12-04 10:26:39 +01:00
|
|
|
SndPlayVehicleFx(SND_12_EXPLOSION, v);
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void RoadVehCheckTrainCrash(Vehicle *v)
|
|
|
|
{
|
2005-06-24 14:38:35 +02:00
|
|
|
TileIndex tile;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2007-02-13 23:27:27 +01:00
|
|
|
if (v->u.road.state == RVSB_WORMHOLE) return;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
|
|
|
tile = v->tile;
|
|
|
|
|
2006-04-15 22:07:42 +02:00
|
|
|
if (!IsLevelCrossingTile(tile)) return;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2006-03-03 23:21:29 +01:00
|
|
|
if (VehicleFromPos(tile, v, EnumCheckRoadVehCrashTrain) != NULL)
|
2004-08-09 19:04:08 +02:00
|
|
|
RoadVehCrash(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
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 21:02:27 +02:00
|
|
|
|
2004-08-09 19:04:08 +02:00
|
|
|
InvalidateWindow(WC_VEHICLE_VIEW, v->index);
|
|
|
|
InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
|
2004-09-10 21:02:27 +02:00
|
|
|
|
2006-09-27 20:17:01 +02:00
|
|
|
if (!PlayVehicleSound(v, VSE_BREAKDOWN)) {
|
|
|
|
SndPlayVehicleFx((_opt.landscape != LT_CANDY) ?
|
|
|
|
SND_0F_VEHICLE_BREAKDOWN : SND_35_COMEDY_BREAKDOWN, v);
|
|
|
|
}
|
2004-08-09 19:04:08 +02:00
|
|
|
|
|
|
|
if (!(v->vehstatus & VS_HIDDEN)) {
|
|
|
|
Vehicle *u = CreateEffectVehicleRel(v, 4, 4, 5, EV_BREAKDOWN_SMOKE);
|
2006-02-06 10:18:04 +01:00
|
|
|
if (u != NULL) u->u.special.unk0 = v->breakdown_delay * 2;
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-02-06 10:18:04 +01:00
|
|
|
if ((v->tick_counter & 1) == 0) {
|
|
|
|
if (--v->breakdown_delay == 0) {
|
2004-08-09 19:04:08 +02:00
|
|
|
v->breakdown_ctr = 0;
|
|
|
|
InvalidateWindow(WC_VEHICLE_VIEW, v->index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ProcessRoadVehOrder(Vehicle *v)
|
|
|
|
{
|
2005-01-15 20:06:22 +01:00
|
|
|
const Order *order;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2006-02-10 07:03:51 +01:00
|
|
|
switch (v->current_order.type) {
|
|
|
|
case OT_GOTO_DEPOT:
|
|
|
|
// Let a depot order in the orderlist interrupt.
|
|
|
|
if (!(v->current_order.flags & OF_PART_OF_ORDERS)) return;
|
|
|
|
if (v->current_order.flags & OF_SERVICE_IF_NEEDED &&
|
|
|
|
!VehicleNeedsService(v)) {
|
|
|
|
v->cur_order_index++;
|
|
|
|
}
|
|
|
|
break;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2006-02-10 07:03:51 +01:00
|
|
|
case OT_LOADING:
|
|
|
|
case OT_LEAVESTATION:
|
|
|
|
return;
|
2006-08-22 19:13:49 +02:00
|
|
|
|
|
|
|
default: break;
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
2006-02-10 07:03:51 +01:00
|
|
|
if (v->cur_order_index >= v->num_orders) v->cur_order_index = 0;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2005-01-15 20:06:22 +01:00
|
|
|
order = GetVehicleOrder(v, v->cur_order_index);
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2005-01-15 20:06:22 +01:00
|
|
|
if (order == NULL) {
|
2004-12-05 13:43:04 +01:00
|
|
|
v->current_order.type = OT_NOTHING;
|
|
|
|
v->current_order.flags = 0;
|
2004-08-09 19:04:08 +02:00
|
|
|
v->dest_tile = 0;
|
2006-03-02 09:55:12 +01:00
|
|
|
ClearSlot(v);
|
2004-08-09 19:04:08 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-09-03 10:25:27 +02:00
|
|
|
if (order->type == v->current_order.type &&
|
|
|
|
order->flags == v->current_order.flags &&
|
|
|
|
order->dest == v->current_order.dest) {
|
2004-08-09 19:04:08 +02:00
|
|
|
return;
|
2006-02-01 07:32:03 +01:00
|
|
|
}
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2005-01-15 20:06:22 +01:00
|
|
|
v->current_order = *order;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2006-07-26 12:50:46 +02:00
|
|
|
switch (order->type) {
|
|
|
|
case OT_GOTO_STATION: {
|
|
|
|
const RoadStop* rs;
|
2005-01-29 20:41:44 +01:00
|
|
|
|
2006-09-03 10:25:27 +02:00
|
|
|
if (order->dest == v->last_station_visited) {
|
2006-07-26 12:50:46 +02:00
|
|
|
v->last_station_visited = INVALID_STATION;
|
|
|
|
}
|
2005-01-29 20:41:44 +01:00
|
|
|
|
2007-01-31 05:34:56 +01:00
|
|
|
rs = GetStation(order->dest)->GetPrimaryRoadStop(
|
2007-01-25 11:06:58 +01:00
|
|
|
v->cargo_type == CT_PASSENGERS ? RoadStop::BUS : RoadStop::TRUCK
|
2006-07-26 12:50:46 +02:00
|
|
|
);
|
2005-01-30 18:46:57 +01:00
|
|
|
|
2006-07-26 12:50:46 +02:00
|
|
|
if (rs != NULL) {
|
|
|
|
TileIndex dest = rs->xy;
|
|
|
|
uint mindist = DistanceManhattan(v->tile, rs->xy);
|
2005-01-29 20:41:44 +01:00
|
|
|
|
2006-07-26 12:50:46 +02:00
|
|
|
for (rs = rs->next; rs != NULL; rs = rs->next) {
|
|
|
|
uint dist = DistanceManhattan(v->tile, rs->xy);
|
2006-03-03 22:18:19 +01:00
|
|
|
|
2006-07-26 12:50:46 +02:00
|
|
|
if (dist < mindist) {
|
|
|
|
mindist = dist;
|
|
|
|
dest = rs->xy;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
v->dest_tile = dest;
|
|
|
|
} else {
|
|
|
|
// There is no stop left at the station, so don't even TRY to go there
|
|
|
|
v->cur_order_index++;
|
|
|
|
v->dest_tile = 0;
|
2006-03-03 22:18:19 +01:00
|
|
|
}
|
2006-07-26 12:50:46 +02:00
|
|
|
break;
|
2005-01-29 20:41:44 +01:00
|
|
|
}
|
2006-07-26 12:50:46 +02:00
|
|
|
|
|
|
|
case OT_GOTO_DEPOT:
|
2006-09-03 10:25:27 +02:00
|
|
|
v->dest_tile = GetDepot(order->dest)->xy;
|
2006-07-26 12:50:46 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
v->dest_tile = 0;
|
|
|
|
break;
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
2005-01-15 20:06:22 +01:00
|
|
|
InvalidateVehicleOrder(v);
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void HandleRoadVehLoading(Vehicle *v)
|
|
|
|
{
|
2006-02-10 07:03:51 +01:00
|
|
|
switch (v->current_order.type) {
|
|
|
|
case OT_LOADING: {
|
|
|
|
Order b;
|
2004-09-10 21:02:27 +02:00
|
|
|
|
2006-02-10 07:03:51 +01:00
|
|
|
if (--v->load_unload_time_rem != 0) return;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2006-12-02 17:56:32 +01:00
|
|
|
if (CanFillVehicle(v) && (v->current_order.flags & OF_FULL_LOAD ||
|
|
|
|
(_patches.gradual_loading && !HASBIT(v->load_status, LS_LOADING_FINISHED)))) {
|
2006-02-10 07:03:51 +01:00
|
|
|
SET_EXPENSES_TYPE(EXPENSES_ROADVEH_INC);
|
2006-12-02 17:56:32 +01:00
|
|
|
if (LoadUnloadVehicle(v, false)) {
|
2006-02-10 07:03:51 +01:00
|
|
|
InvalidateWindow(WC_ROADVEH_LIST, v->owner);
|
|
|
|
MarkRoadVehDirty(v);
|
|
|
|
}
|
|
|
|
return;
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
2004-09-10 21:02:27 +02:00
|
|
|
|
2006-02-10 07:03:51 +01:00
|
|
|
b = v->current_order;
|
2007-01-13 19:55:54 +01:00
|
|
|
v->LeaveStation();
|
2006-02-10 07:03:51 +01:00
|
|
|
if (!(b.flags & OF_NON_STOP)) return;
|
|
|
|
break;
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
2006-02-10 07:03:51 +01:00
|
|
|
|
|
|
|
case OT_DUMMY: break;
|
|
|
|
|
|
|
|
default: return;
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
v->cur_order_index++;
|
2005-01-15 20:06:22 +01:00
|
|
|
InvalidateVehicleOrder(v);
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
2006-02-13 22:15:00 +01:00
|
|
|
static void StartRoadVehSound(const Vehicle* v)
|
2004-08-09 19:04:08 +02:00
|
|
|
{
|
2006-09-27 20:17:01 +02: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 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct RoadVehFindData {
|
2006-02-13 22:15:00 +01:00
|
|
|
int x;
|
|
|
|
int y;
|
|
|
|
const Vehicle* veh;
|
2006-03-08 07:55:33 +01:00
|
|
|
Direction dir;
|
2004-08-09 19:04:08 +02:00
|
|
|
} RoadVehFindData;
|
|
|
|
|
2006-03-03 23:21:29 +01:00
|
|
|
static void* EnumCheckRoadVehClose(Vehicle *v, void* data)
|
2004-08-09 19:04:08 +02:00
|
|
|
{
|
2006-03-03 23:21:29 +01: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 19:56:51 +01:00
|
|
|
const RoadVehFindData* rvf = (RoadVehFindData*)data;
|
2004-09-10 21:02:27 +02:00
|
|
|
|
2004-08-16 15:31:18 +02:00
|
|
|
short x_diff = v->x_pos - rvf->x;
|
|
|
|
short y_diff = v->y_pos - rvf->y;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2006-03-03 23:21:29 +01:00
|
|
|
return
|
|
|
|
rvf->veh != v &&
|
|
|
|
v->type == VEH_Road &&
|
2006-06-05 12:23:18 +02:00
|
|
|
!IsRoadVehInDepot(v) &&
|
2006-03-03 23:21:29 +01:00
|
|
|
myabs(v->z_pos - rvf->veh->z_pos) < 6 &&
|
|
|
|
v->direction == rvf->dir &&
|
|
|
|
(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 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
2006-03-08 07:55:33 +01:00
|
|
|
static Vehicle* RoadVehFindCloseTo(Vehicle* v, int x, int y, Direction dir)
|
2004-08-09 19:04:08 +02:00
|
|
|
{
|
|
|
|
RoadVehFindData rvf;
|
|
|
|
Vehicle *u;
|
|
|
|
|
2006-02-13 22:15:00 +01:00
|
|
|
if (v->u.road.reverse_ctr != 0) return NULL;
|
2004-09-10 21:02:27 +02:00
|
|
|
|
2004-08-09 19:04:08 +02:00
|
|
|
rvf.x = x;
|
|
|
|
rvf.y = y;
|
|
|
|
rvf.dir = dir;
|
|
|
|
rvf.veh = v;
|
2007-01-10 19:56:51 +01:00
|
|
|
u = (Vehicle*)VehicleFromPos(TileVirtXY(x, y), &rvf, EnumCheckRoadVehClose);
|
2004-09-10 21:02:27 +02:00
|
|
|
|
2004-08-16 15:31:18 +02:00
|
|
|
// This code protects a roadvehicle from being blocked for ever
|
2004-12-29 14:13:29 +01:00
|
|
|
// If more than 1480 / 74 days a road vehicle is blocked, it will
|
2004-08-16 15:31:18 +02:00
|
|
|
// drive just through it. The ultimate backup-code of TTD.
|
|
|
|
// It can be disabled.
|
2004-08-09 19:04:08 +02:00
|
|
|
if (u == NULL) {
|
2006-02-11 11:45:20 +01:00
|
|
|
v->u.road.blocked_ctr = 0;
|
2004-08-09 19:04:08 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2006-02-18 15:41:24 +01:00
|
|
|
if (++v->u.road.blocked_ctr > 1480) return NULL;
|
2004-09-10 21:02:27 +02:00
|
|
|
|
2004-08-09 19:04:08 +02:00
|
|
|
return u;
|
|
|
|
}
|
|
|
|
|
2005-11-13 14:43:55 +01:00
|
|
|
static void RoadVehArrivesAt(const Vehicle* v, Station* st)
|
2004-08-09 19:04:08 +02:00
|
|
|
{
|
2005-10-23 16:38:09 +02:00
|
|
|
if (v->cargo_type == CT_PASSENGERS) {
|
2004-08-09 19:04:08 +02: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 23:53:07 +01:00
|
|
|
SetDParam(0, st->index);
|
2004-08-09 19:04:08 +02: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(
|
|
|
|
STR_902F_CITIZENS_CELEBRATE_FIRST,
|
|
|
|
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 23:53:07 +01:00
|
|
|
SetDParam(0, st->index);
|
2004-08-09 19:04:08 +02: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(
|
|
|
|
STR_9030_CITIZENS_CELEBRATE_FIRST,
|
|
|
|
flags,
|
|
|
|
v->index,
|
2006-02-18 15:41:24 +01:00
|
|
|
0
|
|
|
|
);
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool RoadVehAccelerate(Vehicle *v)
|
|
|
|
{
|
2006-02-01 07:32:03 +01:00
|
|
|
uint spd = v->cur_speed + 1 + (v->u.road.overtaking != 0 ? 1 : 0);
|
2004-08-09 19:04:08 +02:00
|
|
|
byte t;
|
2004-09-10 21:02:27 +02:00
|
|
|
|
2004-08-09 19:04:08 +02:00
|
|
|
// Clamp
|
|
|
|
spd = min(spd, v->max_speed);
|
2007-02-13 23:27:27 +01:00
|
|
|
if (v->u.road.state == RVSB_WORMHOLE) spd = min(spd, SetSpeedLimitOnBridge(v));
|
2004-08-09 19:04:08 +02:00
|
|
|
|
|
|
|
//updates statusbar only if speed have changed to save CPU time
|
|
|
|
if (spd != v->cur_speed) {
|
2004-09-10 21:02:27 +02:00
|
|
|
v->cur_speed = spd;
|
2006-02-01 07:32:03 +01:00
|
|
|
if (_patches.vehicle_speed) {
|
2004-12-22 00:27:58 +01:00
|
|
|
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
|
2006-02-01 07:32:03 +01:00
|
|
|
}
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Decrease somewhat when turning
|
2006-02-01 07:32:03 +01:00
|
|
|
if (!(v->direction & 1)) spd = spd * 3 >> 2;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2006-02-01 07:32:03 +01:00
|
|
|
if (spd == 0) return false;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2006-02-01 07:32:03 +01:00
|
|
|
if ((byte)++spd == 0) return true;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
|
|
|
v->progress = (t = v->progress) - (byte)spd;
|
|
|
|
|
|
|
|
return (t < v->progress);
|
|
|
|
}
|
|
|
|
|
2006-03-08 07:55:33 +01:00
|
|
|
static Direction RoadVehGetNewDirection(const Vehicle* v, int x, int y)
|
2004-08-09 19:04:08 +02:00
|
|
|
{
|
2006-03-08 07:55:33 +01:00
|
|
|
static const Direction _roadveh_new_dir[] = {
|
2007-01-10 19:56:51 +01:00
|
|
|
DIR_N , DIR_NW, DIR_W , INVALID_DIR,
|
|
|
|
DIR_NE, DIR_N , DIR_SW, INVALID_DIR,
|
2006-03-08 07:55:33 +01:00
|
|
|
DIR_E , DIR_SE, DIR_S
|
2004-08-09 19:04:08 +02:00
|
|
|
};
|
2004-09-10 21:02:27 +02:00
|
|
|
|
2004-08-09 19:04:08 +02:00
|
|
|
x = x - v->x_pos + 1;
|
|
|
|
y = y - v->y_pos + 1;
|
|
|
|
|
2006-02-13 22:15:00 +01:00
|
|
|
if ((uint)x > 2 || (uint)y > 2) return v->direction;
|
|
|
|
return _roadveh_new_dir[y * 4 + x];
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
2006-03-08 07:55:33 +01:00
|
|
|
static Direction RoadVehGetSlidingDirection(const Vehicle* v, int x, int y)
|
2004-08-09 19:04:08 +02:00
|
|
|
{
|
2007-01-10 19:56:51 +01:00
|
|
|
Direction new_dir = RoadVehGetNewDirection(v, x, y);
|
|
|
|
Direction old_dir = v->direction;
|
2006-03-08 08:50:26 +01:00
|
|
|
DirDiff delta;
|
2006-03-08 07:55:33 +01:00
|
|
|
|
2007-01-10 19:56:51 +01: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 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct OvertakeData {
|
2006-03-03 23:21:29 +01:00
|
|
|
const Vehicle* u;
|
|
|
|
const Vehicle* v;
|
2005-06-24 14:38:35 +02:00
|
|
|
TileIndex tile;
|
2004-08-09 19:04:08 +02:00
|
|
|
byte tilebits;
|
|
|
|
} OvertakeData;
|
|
|
|
|
2006-03-03 23:21:29 +01:00
|
|
|
static void* EnumFindVehToOvertake(Vehicle* v, void* data)
|
2004-08-09 19:04:08 +02:00
|
|
|
{
|
2007-01-10 19:56:51 +01:00
|
|
|
const OvertakeData* od = (OvertakeData*)data;
|
2006-03-03 23:21:29 +01:00
|
|
|
|
2006-02-13 22:15:00 +01:00
|
|
|
return
|
2006-07-25 21:18:31 +02:00
|
|
|
v->tile == od->tile && v->type == VEH_Road && v != od->u && v != od->v ?
|
2006-02-13 22:15:00 +01:00
|
|
|
v : NULL;
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool FindRoadVehToOvertake(OvertakeData *od)
|
|
|
|
{
|
|
|
|
uint32 bits;
|
|
|
|
|
2006-02-18 15:41:24 +01:00
|
|
|
bits = GetTileTrackStatus(od->tile, TRANSPORT_ROAD) & 0x3F;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2006-03-03 23:21:29 +01:00
|
|
|
if (!(od->tilebits & bits) || (bits & 0x3C) || (bits & 0x3F3F0000))
|
2004-08-09 19:04:08 +02:00
|
|
|
return true;
|
2006-03-03 23:21:29 +01:00
|
|
|
return VehicleFromPos(od->tile, od, EnumFindVehToOvertake) != NULL;
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void RoadVehCheckOvertake(Vehicle *v, Vehicle *u)
|
|
|
|
{
|
|
|
|
OvertakeData od;
|
|
|
|
byte tt;
|
|
|
|
|
|
|
|
od.v = v;
|
|
|
|
od.u = u;
|
|
|
|
|
|
|
|
if (u->max_speed >= v->max_speed &&
|
2006-04-03 16:25:32 +02:00
|
|
|
!(u->vehstatus & VS_STOPPED) &&
|
2006-02-01 07:32:03 +01:00
|
|
|
u->cur_speed != 0) {
|
|
|
|
return;
|
|
|
|
}
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2006-02-18 15:41:24 +01:00
|
|
|
if (v->direction != u->direction || !(v->direction & 1)) return;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2007-02-13 23:27:27 +01:00
|
|
|
/* Check if vehicle is in a road stop, depot, tunnel or bridge or not on a straight road */
|
|
|
|
if (v->u.road.state >= RVS_IN_ROAD_STOP || !IsStraightRoadTrackdir((Trackdir)(v->u.road.state & RVSB_TRACKDIR_MASK))) return;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2005-07-21 08:31:02 +02:00
|
|
|
tt = GetTileTrackStatus(v->tile, TRANSPORT_ROAD) & 0x3F;
|
2006-02-01 07:32:03 +01:00
|
|
|
if ((tt & 3) == 0) return;
|
|
|
|
if ((tt & 0x3C) != 0) return;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2006-02-01 07:32:03 +01:00
|
|
|
if (tt == 3) tt = (v->direction & 2) ? 2 : 1;
|
2004-08-09 19:04:08 +02:00
|
|
|
od.tilebits = tt;
|
|
|
|
|
|
|
|
od.tile = v->tile;
|
2006-02-01 07:32:03 +01:00
|
|
|
if (FindRoadVehToOvertake(&od)) return;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2006-09-06 01:21:41 +02:00
|
|
|
od.tile = v->tile + TileOffsByDiagDir(DirToDiagDir(v->direction));
|
2006-02-01 07:32:03 +01:00
|
|
|
if (FindRoadVehToOvertake(&od)) return;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2006-04-03 16:25:32 +02:00
|
|
|
if (od.u->cur_speed == 0 || od.u->vehstatus& VS_STOPPED) {
|
2004-08-09 19:04:08 +02:00
|
|
|
v->u.road.overtaking_ctr = 0x11;
|
|
|
|
v->u.road.overtaking = 0x10;
|
|
|
|
} else {
|
2006-02-01 07:32:03 +01:00
|
|
|
// if (FindRoadVehToOvertake(&od)) return;
|
2004-08-09 19:04:08 +02:00
|
|
|
v->u.road.overtaking_ctr = 0;
|
|
|
|
v->u.road.overtaking = 0x10;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void RoadZPosAffectSpeed(Vehicle *v, byte old_z)
|
|
|
|
{
|
2006-02-13 22:15:00 +01:00
|
|
|
if (old_z == v->z_pos) return;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
|
|
|
if (old_z < v->z_pos) {
|
2006-02-13 22:15:00 +01:00
|
|
|
v->cur_speed = v->cur_speed * 232 / 256; // slow down by ~10%
|
2004-08-09 19:04:08 +02:00
|
|
|
} else {
|
|
|
|
uint16 spd = v->cur_speed + 2;
|
2006-02-13 22:15:00 +01:00
|
|
|
if (spd <= v->max_speed) v->cur_speed = spd;
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int PickRandomBit(uint bits)
|
|
|
|
{
|
|
|
|
uint num = 0;
|
|
|
|
uint b = bits;
|
|
|
|
uint i;
|
|
|
|
|
|
|
|
do {
|
2006-02-01 07:32:03 +01:00
|
|
|
if (b & 1) num++;
|
2004-08-09 19:04:08 +02:00
|
|
|
} while (b >>= 1);
|
|
|
|
|
2005-11-14 10:21:05 +01:00
|
|
|
num = RandomRange(num);
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2006-02-13 22:15:00 +01:00
|
|
|
for (i = 0; !(bits & 1) || (int)--num >= 0; bits >>= 1, i++) {}
|
2004-08-09 19:04:08 +02:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
TileIndex dest;
|
|
|
|
uint maxtracklen;
|
|
|
|
uint mindist;
|
|
|
|
} FindRoadToChooseData;
|
|
|
|
|
2007-01-10 19:56:51 +01:00
|
|
|
static bool EnumRoadTrackFindDist(TileIndex tile, void* data, Trackdir trackdir, uint length, byte* state)
|
2004-08-09 19:04:08 +02:00
|
|
|
{
|
2007-01-10 19:56:51 +01:00
|
|
|
FindRoadToChooseData* frd = (FindRoadToChooseData*)data;
|
2005-01-31 08:23:15 +01:00
|
|
|
uint dist = DistanceManhattan(tile, frd->dest);
|
2006-03-03 23:21:29 +01:00
|
|
|
|
2004-08-09 19:04:08 +02:00
|
|
|
if (dist <= frd->mindist) {
|
|
|
|
if (dist != frd->mindist || length < frd->maxtracklen) {
|
|
|
|
frd->maxtracklen = length;
|
|
|
|
}
|
|
|
|
frd->mindist = dist;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-05-27 18:12:16 +02:00
|
|
|
static inline NPFFoundTargetData PerfNPFRouteToStationOrTile(TileIndex tile, Trackdir trackdir, NPFFindStationOrTileData* target, TransportType type, Owner owner, RailTypeMask railtypes)
|
|
|
|
{
|
|
|
|
|
|
|
|
void* perf = NpfBeginInterval();
|
|
|
|
NPFFoundTargetData ret = NPFRouteToStationOrTile(tile, trackdir, target, type, owner, railtypes);
|
|
|
|
int t = NpfEndInterval(perf);
|
2006-12-26 18:36:18 +01: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 18:12:16 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-02-13 21:57:02 +01:00
|
|
|
/**
|
|
|
|
* Returns direction to for a road vehicle to take or
|
|
|
|
* INVALID_TRACKDIR if the direction is currently blocked
|
|
|
|
* @param v the vehicle to do the pathfinding for
|
|
|
|
* @param tile the where to start the pathfinding
|
|
|
|
* @param enterdir the direction the vehicle enters the tile from
|
|
|
|
* @return the trackdir to take
|
|
|
|
*/
|
|
|
|
static Trackdir RoadFindPathToDest(Vehicle* v, TileIndex tile, DiagDirection enterdir)
|
2004-08-09 19:04:08 +02:00
|
|
|
{
|
2007-02-13 21:57:02 +01:00
|
|
|
#define return_track(x) { best_track = (Trackdir)x; goto found_best_track; }
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2005-06-24 14:38:35 +02:00
|
|
|
TileIndex desttile;
|
2004-08-09 19:04:08 +02:00
|
|
|
FindRoadToChooseData frd;
|
2007-02-13 21:57:02 +01:00
|
|
|
Trackdir best_track;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2007-02-13 21:57:02 +01:00
|
|
|
uint32 r = GetTileTrackStatus(tile, TRANSPORT_ROAD);
|
|
|
|
TrackdirBits signal = (TrackdirBits)GB(r, 16, 16);
|
|
|
|
TrackdirBits trackdirs = (TrackdirBits)GB(r, 0, 16);
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2005-01-16 12:24:58 +01:00
|
|
|
if (IsTileType(tile, MP_STREET)) {
|
2006-08-29 21:46:31 +02:00
|
|
|
if (GetRoadTileType(tile) == ROAD_TILE_DEPOT && (!IsTileOwner(tile, v->owner) || GetRoadDepotDirection(tile) == enterdir)) {
|
|
|
|
/* Road depot owned by another player or with the wrong orientation */
|
2007-02-13 21:57:02 +01:00
|
|
|
trackdirs = TRACKDIR_BIT_NONE;
|
2005-10-23 15:04:44 +02:00
|
|
|
}
|
2006-03-31 21:10:54 +02:00
|
|
|
} else if (IsTileType(tile, MP_STATION) && IsRoadStopTile(tile)) {
|
2006-08-29 21:46:31 +02:00
|
|
|
if (!IsTileOwner(tile, v->owner) || GetRoadStopDir(tile) == enterdir) {
|
|
|
|
/* different station owner or wrong orientation */
|
2007-02-13 21:57:02 +01:00
|
|
|
trackdirs = TRACKDIR_BIT_NONE;
|
2006-06-02 21:29:11 +02:00
|
|
|
} else {
|
2004-09-14 03:21:07 +02:00
|
|
|
/* Our station */
|
2007-01-25 11:06:58 +01:00
|
|
|
RoadStop::Type rstype = (v->cargo_type == CT_PASSENGERS) ? RoadStop::BUS : RoadStop::TRUCK;
|
2006-03-03 23:21:29 +01:00
|
|
|
|
2006-06-02 21:29:11 +02:00
|
|
|
if (GetRoadStopType(tile) != rstype) {
|
2007-02-13 21:57:02 +01:00
|
|
|
/* Wrong station type */
|
|
|
|
trackdirs = TRACKDIR_BIT_NONE;
|
2006-06-02 21:29:11 +02:00
|
|
|
} else {
|
2007-02-13 21:57:02 +01:00
|
|
|
/* Proper station type, check if there is free loading bay */
|
2007-02-02 20:07:20 +01:00
|
|
|
if (!_patches.roadveh_queue &&
|
2007-02-13 01:25:42 +01:00
|
|
|
!GetRoadStopByTile(tile, rstype)->HasFreeBay()) {
|
2007-02-13 21:57:02 +01:00
|
|
|
/* Station is full and RV queuing is off */
|
|
|
|
trackdirs = TRACKDIR_BIT_NONE;
|
2006-02-27 20:43:19 +01:00
|
|
|
}
|
2004-09-14 03:21:07 +02: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 18:15:22 +02:00
|
|
|
|
2007-02-13 21:57:02 +01:00
|
|
|
/* Remove tracks unreachable from the enter dir */
|
|
|
|
trackdirs &= _road_enter_dir_to_reachable_trackdirs[enterdir];
|
|
|
|
if (trackdirs == TRACKDIR_BIT_NONE) {
|
2005-01-31 12:23:10 +01:00
|
|
|
/* No reachable tracks, so we'll reverse */
|
|
|
|
return_track(_road_reverse_table[enterdir]);
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (v->u.road.reverse_ctr != 0) {
|
2005-01-31 12:23:10 +01:00
|
|
|
/* What happens here?? */
|
2004-08-09 19:04:08 +02:00
|
|
|
v->u.road.reverse_ctr = 0;
|
2005-06-27 08:57:24 +02:00
|
|
|
if (v->tile != tile) {
|
2005-01-31 12:23:10 +01:00
|
|
|
return_track(_road_reverse_table[enterdir]);
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
desttile = v->dest_tile;
|
|
|
|
if (desttile == 0) {
|
2007-02-13 21:57:02 +01:00
|
|
|
/* We've got no destination, pick a random track */
|
|
|
|
return_track(PickRandomBit(trackdirs));
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
2007-02-13 21:57:02 +01:00
|
|
|
/* Only one track to choose between? */
|
|
|
|
if (!(KillFirstBit2x64(trackdirs))) {
|
|
|
|
return_track(FindFirstBit2x64(trackdirs));
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
2006-05-27 18:12:16 +02:00
|
|
|
if (_patches.yapf.road_use_yapf) {
|
|
|
|
Trackdir trackdir = YapfChooseRoadTrack(v, tile, enterdir);
|
|
|
|
if (trackdir != INVALID_TRACKDIR) return_track(trackdir);
|
2007-02-13 21:57:02 +01:00
|
|
|
return_track(PickRandomBit(trackdirs));
|
2006-05-27 18:12:16 +02:00
|
|
|
} else if (_patches.new_pathfinding_all) {
|
2005-01-31 12:23:10 +01:00
|
|
|
NPFFindStationOrTileData fstd;
|
|
|
|
NPFFoundTargetData ftd;
|
2007-01-10 19:56:51 +01:00
|
|
|
Trackdir trackdir;
|
2005-01-31 12:23:10 +01: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 20:04:02 +02:00
|
|
|
trackdir = DiagdirToDiagTrackdir(enterdir);
|
2005-01-31 12:23:10 +01:00
|
|
|
//debug("Finding path. Enterdir: %d, Trackdir: %d", enterdir, trackdir);
|
|
|
|
|
2006-09-06 01:21:41 +02:00
|
|
|
ftd = PerfNPFRouteToStationOrTile(tile - TileOffsByDiagDir(enterdir), trackdir, &fstd, TRANSPORT_ROAD, v->owner, INVALID_RAILTYPE);
|
2007-02-13 21:57:02 +01:00
|
|
|
if (ftd.best_trackdir == INVALID_TRACKDIR) {
|
2005-04-15 15:48:08 +02:00
|
|
|
/* We are already at our target. Just do something */
|
2005-01-31 12:23:10 +01:00
|
|
|
//TODO: maybe display error?
|
|
|
|
//TODO: go straight ahead if possible?
|
2007-02-13 21:57:02 +01:00
|
|
|
return_track(FindFirstBit2x64(trackdirs));
|
2005-01-31 12:23:10 +01:00
|
|
|
} else {
|
2005-04-15 15:48:08 +02: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 12:23:10 +01:00
|
|
|
return_track(ftd.best_trackdir);
|
|
|
|
}
|
|
|
|
} else {
|
2006-03-08 16:29:23 +01:00
|
|
|
DiagDirection dir;
|
|
|
|
|
2005-01-31 12:23:10 +01:00
|
|
|
if (IsTileType(desttile, MP_STREET)) {
|
2006-05-09 10:25:31 +02:00
|
|
|
if (GetRoadTileType(desttile) == ROAD_TILE_DEPOT) {
|
2006-03-08 16:29:23 +01:00
|
|
|
dir = GetRoadDepotDirection(desttile);
|
|
|
|
goto do_it;
|
|
|
|
}
|
2005-01-31 12:23:10 +01:00
|
|
|
} else if (IsTileType(desttile, MP_STATION)) {
|
2006-03-26 21:20:15 +02:00
|
|
|
if (IsRoadStop(desttile)) {
|
2006-03-31 21:10:54 +02:00
|
|
|
dir = GetRoadStopDir(desttile);
|
2004-08-09 19:04:08 +02:00
|
|
|
do_it:;
|
2005-01-31 12:23:10 +01: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-06 01:21:41 +02:00
|
|
|
desttile += TileOffsByDiagDir(dir);
|
2007-02-13 21:57:02 +01:00
|
|
|
if (desttile == tile && trackdirs & _road_exit_dir_to_incoming_trackdirs[dir]) {
|
2005-01-31 12:23:10 +01:00
|
|
|
/* If we are already in front of the
|
|
|
|
* station/depot and we can get in from here,
|
|
|
|
* we enter */
|
2007-02-13 21:57:02 +01:00
|
|
|
return_track(FindFirstBit2x64(trackdirs & _road_exit_dir_to_incoming_trackdirs[dir]));
|
2005-01-31 12:23:10 +01:00
|
|
|
}
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
2004-09-10 21:02:27 +02:00
|
|
|
}
|
2007-02-13 21:57:02 +01:00
|
|
|
/* Do some pathfinding */
|
2005-01-31 12:23:10 +01:00
|
|
|
frd.dest = desttile;
|
|
|
|
|
2007-02-13 21:57:02 +01: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++) {
|
|
|
|
if (HASBIT(bitmask, i)) {
|
|
|
|
if (best_track == INVALID_TRACKDIR) best_track = (Trackdir)i; // in case we don't find the path, just pick a track
|
2005-01-31 12:23:10 +01:00
|
|
|
frd.maxtracklen = (uint)-1;
|
|
|
|
frd.mindist = (uint)-1;
|
2006-04-21 22:08:25 +02:00
|
|
|
FollowTrack(tile, 0x2000 | TRANSPORT_ROAD, _road_pf_directions[i], EnumRoadTrackFindDist, NULL, &frd);
|
2005-01-31 12:23:10 +01:00
|
|
|
|
2007-02-13 21:57:02 +01:00
|
|
|
if (frd.mindist < best_dist || (frd.mindist == best_dist && frd.maxtracklen < best_maxlen)) {
|
2005-01-31 12:23:10 +01:00
|
|
|
best_dist = frd.mindist;
|
|
|
|
best_maxlen = frd.maxtracklen;
|
2007-02-13 21:57:02 +01:00
|
|
|
best_track = (Trackdir)i;
|
2005-01-31 12:23:10 +01:00
|
|
|
}
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
2007-02-13 21:57:02 +01:00
|
|
|
}
|
2005-01-31 12:23:10 +01:00
|
|
|
}
|
2004-08-09 19:04:08 +02:00
|
|
|
|
|
|
|
found_best_track:;
|
|
|
|
|
2007-02-13 21:57:02 +01:00
|
|
|
if (HASBIT(signal, best_track)) return INVALID_TRACKDIR;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2004-09-10 21:02:27 +02:00
|
|
|
return best_track;
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
2006-03-02 09:55:12 +01:00
|
|
|
static uint RoadFindPathToStop(const Vehicle *v, TileIndex tile)
|
2005-01-29 20:41:44 +01:00
|
|
|
{
|
2006-08-15 01:11:59 +02:00
|
|
|
uint dist;
|
2006-05-30 12:53:27 +02:00
|
|
|
if (_patches.yapf.road_use_yapf) {
|
|
|
|
// use YAPF
|
|
|
|
dist = YapfRoadVehDistanceToTile(v, tile);
|
|
|
|
} else {
|
|
|
|
// use NPF
|
|
|
|
NPFFindStationOrTileData fstd;
|
2007-01-10 19:56:51 +01:00
|
|
|
Trackdir trackdir = GetVehicleTrackdir(v);
|
2007-02-13 21:57:02 +01:00
|
|
|
assert(trackdir != INVALID_TRACKDIR);
|
2005-01-29 20:41:44 +01:00
|
|
|
|
2006-05-30 12:53:27 +02:00
|
|
|
fstd.dest_coords = tile;
|
|
|
|
fstd.station_index = INVALID_STATION; // indicates that the destination is a tile, not a station
|
2005-01-29 20:41:44 +01:00
|
|
|
|
2006-05-30 12:53:27 +02:00
|
|
|
dist = NPFRouteToStationOrTile(v->tile, trackdir, &fstd, TRANSPORT_ROAD, v->owner, INVALID_RAILTYPE).best_path_dist;
|
|
|
|
// change units from NPF_TILE_LENGTH to # of tiles
|
|
|
|
if (dist != UINT_MAX)
|
|
|
|
dist = (dist + NPF_TILE_LENGTH - 1) / NPF_TILE_LENGTH;
|
|
|
|
}
|
|
|
|
return dist;
|
2005-01-29 20:41:44 +01:00
|
|
|
}
|
|
|
|
|
2007-02-14 02:00:57 +01:00
|
|
|
enum {
|
|
|
|
RDE_NEXT_TILE = 0x80,
|
|
|
|
RDE_TURNED = 0x40,
|
|
|
|
};
|
|
|
|
|
2004-08-09 19:04:08 +02:00
|
|
|
typedef struct RoadDriveEntry {
|
|
|
|
byte x,y;
|
|
|
|
} RoadDriveEntry;
|
|
|
|
|
|
|
|
#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
|
|
|
|
};
|
|
|
|
|
2005-02-22 20:38:44 +01:00
|
|
|
static void RoadVehController(Vehicle *v)
|
2004-08-09 19:04:08 +02:00
|
|
|
{
|
2006-03-08 07:55:33 +01:00
|
|
|
Direction new_dir;
|
|
|
|
Direction old_dir;
|
2004-08-09 19:04:08 +02:00
|
|
|
RoadDriveEntry rd;
|
|
|
|
int x,y;
|
|
|
|
uint32 r;
|
|
|
|
|
|
|
|
// decrease counters
|
|
|
|
v->tick_counter++;
|
2006-02-13 22:15:00 +01:00
|
|
|
if (v->u.road.reverse_ctr != 0) v->u.road.reverse_ctr--;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
|
|
|
// handle crashed
|
|
|
|
if (v->u.road.crashed_ctr != 0) {
|
|
|
|
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--;
|
|
|
|
}
|
|
|
|
|
2006-04-03 16:25:32 +02:00
|
|
|
if (v->vehstatus & VS_STOPPED) return;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
|
|
|
ProcessRoadVehOrder(v);
|
|
|
|
HandleRoadVehLoading(v);
|
|
|
|
|
2006-02-01 07:32:03 +01:00
|
|
|
if (v->current_order.type == OT_LOADING) return;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2006-06-05 12:23:18 +02:00
|
|
|
if (IsRoadVehInDepot(v)) {
|
2007-02-12 23:46:10 +01:00
|
|
|
/* Vehicle is about to leave a depot */
|
2006-03-06 21:28:28 +01:00
|
|
|
DiagDirection dir;
|
2006-02-18 15:41:24 +01:00
|
|
|
const RoadDriveEntry* rdp;
|
2007-02-13 21:57:02 +01:00
|
|
|
Trackdir tdir;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
|
|
|
v->cur_speed = 0;
|
2004-09-10 21:02:27 +02:00
|
|
|
|
2006-03-08 16:29:23 +01:00
|
|
|
dir = GetRoadDepotDirection(v->tile);
|
2006-03-06 21:28:28 +01:00
|
|
|
v->direction = DiagDirToDir(dir);
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2007-02-13 21:57:02 +01:00
|
|
|
tdir = _roadveh_depot_exit_trackdir[dir];
|
|
|
|
rdp = _road_drive_data[(_opt.road_side << 4) + tdir];
|
2004-09-10 21:02:27 +02:00
|
|
|
|
2006-04-03 07:32:11 +02:00
|
|
|
x = TileX(v->tile) * TILE_SIZE + (rdp[6].x & 0xF);
|
|
|
|
y = TileY(v->tile) * TILE_SIZE + (rdp[6].y & 0xF);
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2006-03-03 23:21:29 +01:00
|
|
|
if (RoadVehFindCloseTo(v, x, y, v->direction) != NULL) return;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2004-12-09 22:46:56 +01:00
|
|
|
VehicleServiceInDepot(v);
|
|
|
|
|
2004-08-09 19:04:08 +02:00
|
|
|
StartRoadVehSound(v);
|
|
|
|
|
|
|
|
BeginVehicleMove(v);
|
|
|
|
|
|
|
|
v->vehstatus &= ~VS_HIDDEN;
|
2007-02-13 21:57:02 +01:00
|
|
|
v->u.road.state = tdir;
|
2004-08-09 19:04:08 +02:00
|
|
|
v->u.road.frame = 6;
|
2004-09-10 21:02:27 +02:00
|
|
|
|
2004-08-09 19:04:08 +02:00
|
|
|
v->cur_image = GetRoadVehImage(v, v->direction);
|
|
|
|
UpdateRoadVehDeltaXY(v);
|
|
|
|
SetRoadVehPosition(v,x,y);
|
|
|
|
|
2006-10-05 14:59:28 +02:00
|
|
|
InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
|
2004-08-09 19:04:08 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-02-12 23:46:10 +01:00
|
|
|
/* Check if vehicle needs to proceed, return if it doesn't */
|
2006-02-01 07:32:03 +01:00
|
|
|
if (!RoadVehAccelerate(v)) return;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
|
|
|
if (v->u.road.overtaking != 0) {
|
|
|
|
if (++v->u.road.overtaking_ctr >= 35)
|
2006-01-06 18:25:39 +01: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 23:27:27 +01: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 18:25:39 +01:00
|
|
|
v->u.road.overtaking = 0;
|
2006-02-18 15:41:24 +01:00
|
|
|
}
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
2007-02-12 23:46:10 +01:00
|
|
|
/* Save old vehicle position to use at end of move to set viewport area dirty */
|
2004-08-09 19:04:08 +02:00
|
|
|
BeginVehicleMove(v);
|
|
|
|
|
2007-02-13 23:27:27 +01:00
|
|
|
if (v->u.road.state == RVSB_WORMHOLE) {
|
2007-02-12 23:46:10 +01:00
|
|
|
/* Vehicle is on a bridge or in a tunnel */
|
2006-03-03 23:21:29 +01:00
|
|
|
GetNewVehiclePosResult gp;
|
|
|
|
|
2004-08-09 19:04:08 +02:00
|
|
|
GetNewVehiclePos(v, &gp);
|
|
|
|
|
2007-02-06 10:29:56 +01:00
|
|
|
const Vehicle *u = RoadVehFindCloseTo(v, gp.x, gp.y, v->direction);
|
|
|
|
if (u != NULL && u->cur_speed < v->cur_speed) {
|
|
|
|
v->cur_speed = u->cur_speed;
|
2004-08-09 19:04:08 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-02-13 11:26:53 +01: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 23:46:10 +01:00
|
|
|
/* Vehicle has just entered a bridge or tunnel */
|
2004-08-09 19:04:08 +02:00
|
|
|
v->cur_image = GetRoadVehImage(v, v->direction);
|
|
|
|
UpdateRoadVehDeltaXY(v);
|
|
|
|
SetRoadVehPosition(v,gp.x,gp.y);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
v->x_pos = gp.x;
|
|
|
|
v->y_pos = gp.y;
|
|
|
|
VehiclePositionChanged(v);
|
2006-12-27 13:38:02 +01:00
|
|
|
if (!(v->vehstatus & VS_HIDDEN)) EndVehicleMove(v);
|
2004-08-09 19:04:08 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-02-12 23:46:10 +01:00
|
|
|
/* Get move position data for next frame */
|
2007-02-13 23:27:27 +01:00
|
|
|
rd = _road_drive_data[(v->u.road.state + (_opt.road_side << RVS_DRIVE_SIDE)) ^ v->u.road.overtaking][v->u.road.frame + 1];
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2007-02-14 02:00:57 +01:00
|
|
|
if (rd.x & RDE_NEXT_TILE) {
|
2007-02-12 23:46:10 +01:00
|
|
|
/* Vehicle is moving to the next tile */
|
2006-09-06 01:21:41 +02:00
|
|
|
TileIndex tile = v->tile + TileOffsByDiagDir(rd.x & 3);
|
2007-02-13 21:57:02 +01:00
|
|
|
Trackdir dir = RoadFindPathToDest(v, tile, (DiagDirection)(rd.x & 3));
|
2004-08-09 19:04:08 +02:00
|
|
|
uint32 r;
|
2006-03-08 07:55:33 +01:00
|
|
|
Direction newdir;
|
2004-08-09 19:04:08 +02:00
|
|
|
const RoadDriveEntry *rdp;
|
|
|
|
|
2007-02-13 21:57:02 +01:00
|
|
|
if (dir == INVALID_TRACKDIR) {
|
2007-02-12 23:46:10 +01:00
|
|
|
/* No path was found to destination */
|
2004-08-09 19:04:08 +02:00
|
|
|
v->cur_speed = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
again:
|
2007-02-13 23:27:27 +01:00
|
|
|
if (IsReversingRoadTrackdir(dir)) {
|
2005-01-31 12:23:10 +01:00
|
|
|
/* Turning around */
|
2004-08-09 19:04:08 +02:00
|
|
|
tile = v->tile;
|
|
|
|
}
|
|
|
|
|
2007-02-12 23:46:10 +01:00
|
|
|
/* Get position data for first frame on the new tile */
|
2006-01-06 18:25:39 +01:00
|
|
|
rdp = _road_drive_data[(dir + (_opt.road_side << 4)) ^ v->u.road.overtaking];
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2006-04-03 07:32:11 +02:00
|
|
|
x = TileX(tile) * TILE_SIZE + rdp[0].x;
|
|
|
|
y = TileY(tile) * TILE_SIZE + rdp[0].y;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2006-02-13 22:15:00 +01:00
|
|
|
newdir = RoadVehGetSlidingDirection(v, x, y);
|
2006-03-03 23:21:29 +01:00
|
|
|
if (RoadVehFindCloseTo(v, x, y, newdir) != NULL) return;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
|
|
|
r = VehicleEnterTile(v, tile, x, y);
|
2007-02-13 11:26:53 +01:00
|
|
|
if (HASBIT(r, VETS_CANNOT_ENTER)) {
|
2007-02-12 23:46:10 +01:00
|
|
|
/* Vehicle cannot enter the tile */
|
2005-01-16 12:24:58 +01:00
|
|
|
if (!IsTileType(tile, MP_TUNNELBRIDGE)) {
|
2004-08-09 19:04:08 +02:00
|
|
|
v->cur_speed = 0;
|
|
|
|
return;
|
|
|
|
}
|
2007-02-12 23:46:10 +01:00
|
|
|
/* Try an about turn to re-enter the previous tile */
|
2007-02-13 23:27:27 +01:00
|
|
|
dir = _road_reverse_table[rd.x & 3];
|
2004-08-09 19:04:08 +02:00
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
|
2007-02-13 23:27:27 +01:00
|
|
|
if (IS_BYTE_INSIDE(v->u.road.state, RVSB_IN_ROAD_STOP, RVSB_IN_ROAD_STOP_END) && IsTileType(v->tile, MP_STATION)) {
|
|
|
|
if (IsReversingRoadTrackdir(dir)) {
|
2007-02-12 23:46:10 +01: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 15:41:24 +01:00
|
|
|
v->cur_speed = 0;
|
|
|
|
return;
|
|
|
|
}
|
2006-03-26 21:20:15 +02:00
|
|
|
if (IsRoadStop(v->tile)) {
|
2007-02-12 23:46:10 +01:00
|
|
|
/* The tile that the vehicle is leaving is a road stop */
|
2005-01-29 20:41:44 +01:00
|
|
|
RoadStop *rs = GetRoadStopByTile(v->tile, GetRoadStopType(v->tile));
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2007-02-12 23:46:10 +01:00
|
|
|
/* Vehicle is leaving a road stop tile, mark bay as free and clear the usage bit */
|
2007-02-13 23:27:27 +01:00
|
|
|
rs->FreeBay(HASBIT(v->u.road.state, RVS_USING_SECOND_BAY) ? 1 : 0);
|
2007-02-13 01:25:42 +01:00
|
|
|
rs->SetEntranceBusy(false);
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-13 11:26:53 +01:00
|
|
|
if (!HASBIT(r, VETS_ENTERED_WORMHOLE)) {
|
2007-02-12 23:46:10 +01:00
|
|
|
/* Set vehicle to first frame on new tile */
|
2004-08-09 19:04:08 +02:00
|
|
|
v->tile = tile;
|
2006-01-06 18:25:39 +01:00
|
|
|
v->u.road.state = (byte)dir;
|
2004-08-09 19:04:08 +02:00
|
|
|
v->u.road.frame = 0;
|
|
|
|
}
|
|
|
|
if (newdir != v->direction) {
|
|
|
|
v->direction = newdir;
|
|
|
|
v->cur_speed -= v->cur_speed >> 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
v->cur_image = GetRoadVehImage(v, newdir);
|
|
|
|
UpdateRoadVehDeltaXY(v);
|
|
|
|
RoadZPosAffectSpeed(v, SetRoadVehPosition(v, x, y));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-02-14 02:00:57 +01:00
|
|
|
if (rd.x & RDE_TURNED) {
|
2007-02-12 23:46:10 +01:00
|
|
|
/* Vehicle has finished turning around, it will now head back onto the same tile */
|
2007-02-13 21:57:02 +01:00
|
|
|
Trackdir dir = RoadFindPathToDest(v, v->tile, (DiagDirection)(rd.x & 3));
|
2004-08-09 19:04:08 +02:00
|
|
|
uint32 r;
|
|
|
|
int tmp;
|
2006-03-08 07:55:33 +01:00
|
|
|
Direction newdir;
|
2004-08-09 19:04:08 +02:00
|
|
|
const RoadDriveEntry *rdp;
|
|
|
|
|
2007-02-13 21:57:02 +01:00
|
|
|
if (dir == INVALID_TRACKDIR) {
|
2007-02-12 23:46:10 +01:00
|
|
|
/* No path was found to destination */
|
2004-08-09 19:04:08 +02:00
|
|
|
v->cur_speed = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-02-18 15:41:24 +01:00
|
|
|
tmp = (_opt.road_side << 4) + dir;
|
2004-08-09 19:04:08 +02:00
|
|
|
rdp = _road_drive_data[tmp];
|
|
|
|
|
2006-04-03 07:32:11 +02:00
|
|
|
x = TileX(v->tile) * TILE_SIZE + rdp[1].x;
|
|
|
|
y = TileY(v->tile) * TILE_SIZE + rdp[1].y;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2006-02-13 22:15:00 +01:00
|
|
|
newdir = RoadVehGetSlidingDirection(v, x, y);
|
2006-03-03 23:21:29 +01:00
|
|
|
if (RoadVehFindCloseTo(v, x, y, newdir) != NULL) return;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
|
|
|
r = VehicleEnterTile(v, v->tile, x, y);
|
2007-02-13 11:26:53 +01:00
|
|
|
if (HASBIT(r, VETS_CANNOT_ENTER)) {
|
2007-02-12 23:46:10 +01:00
|
|
|
/* Vehicle cannot enter the tile */
|
2004-08-09 19:04:08 +02:00
|
|
|
v->cur_speed = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-02-12 23:46:10 +01:00
|
|
|
/* Set vehicle to second frame on the tile */
|
2007-02-14 01:50:34 +01:00
|
|
|
v->u.road.state = tmp & ~(1 << RVS_DRIVE_SIDE);
|
2004-08-09 19:04:08 +02:00
|
|
|
v->u.road.frame = 1;
|
|
|
|
|
|
|
|
if (newdir != v->direction) {
|
|
|
|
v->direction = newdir;
|
|
|
|
v->cur_speed -= v->cur_speed >> 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
v->cur_image = GetRoadVehImage(v, newdir);
|
|
|
|
UpdateRoadVehDeltaXY(v);
|
|
|
|
RoadZPosAffectSpeed(v, SetRoadVehPosition(v, x, y));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-02-12 23:46:10 +01:00
|
|
|
/* Calculate new position for the vehicle */
|
2006-02-18 15:41:24 +01:00
|
|
|
x = (v->x_pos & ~15) + (rd.x & 15);
|
|
|
|
y = (v->y_pos & ~15) + (rd.y & 15);
|
2004-09-10 21:02:27 +02:00
|
|
|
|
2004-08-09 19:04:08 +02:00
|
|
|
new_dir = RoadVehGetSlidingDirection(v, x, y);
|
|
|
|
|
2007-02-13 23:27:27 +01:00
|
|
|
if (!IS_BYTE_INSIDE(v->u.road.state, RVSB_IN_ROAD_STOP, RVSB_IN_ROAD_STOP_END)) {
|
2007-02-12 23:46:10 +01:00
|
|
|
/* Vehicle is not in a road stop.
|
|
|
|
* Check for another vehicle to overtake */
|
2006-03-03 23:21:29 +01:00
|
|
|
Vehicle* u = RoadVehFindCloseTo(v, x, y, new_dir);
|
|
|
|
|
|
|
|
if (u != NULL) {
|
2007-02-12 23:46:10 +01:00
|
|
|
/* There is a vehicle in front overtake it if possible */
|
2006-03-03 23:21:29 +01:00
|
|
|
if (v->u.road.overtaking == 0) RoadVehCheckOvertake(v, u);
|
|
|
|
return;
|
|
|
|
}
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
old_dir = v->direction;
|
|
|
|
if (new_dir != old_dir) {
|
2007-02-12 23:46:10 +01:00
|
|
|
/* The vehicle's direction has changed. */
|
2004-08-09 19:04:08 +02:00
|
|
|
v->direction = new_dir;
|
|
|
|
v->cur_speed -= (v->cur_speed >> 2);
|
|
|
|
if (old_dir != v->u.road.state) {
|
2007-02-12 23:46:10 +01:00
|
|
|
/* The vehicle is in a road stop */
|
2004-08-09 19:04:08 +02:00
|
|
|
v->cur_image = GetRoadVehImage(v, new_dir);
|
|
|
|
UpdateRoadVehDeltaXY(v);
|
|
|
|
SetRoadVehPosition(v, v->x_pos, v->y_pos);
|
2007-02-12 23:46:10 +01:00
|
|
|
/* Note, return here means that the frame counter is not incremented
|
|
|
|
* for vehicles changing direction in a road stop. This causes frames to
|
|
|
|
* be repeated. Is this intended? */
|
2004-08-09 19:04:08 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-13 23:27:27 +01:00
|
|
|
if (v->u.road.state >= RVSB_IN_ROAD_STOP &&
|
|
|
|
_road_veh_data_1[v->u.road.state - RVSB_IN_ROAD_STOP + (_opt.road_side << RVS_DRIVE_SIDE)] == v->u.road.frame) {
|
2005-01-29 20:41:44 +01:00
|
|
|
RoadStop *rs = GetRoadStopByTile(v->tile, GetRoadStopType(v->tile));
|
2006-03-24 09:55:08 +01:00
|
|
|
Station* st = GetStationByTile(v->tile);
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2007-02-12 23:46:10 +01: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 13:43:04 +01:00
|
|
|
if (v->current_order.type != OT_LEAVESTATION &&
|
|
|
|
v->current_order.type != OT_GOTO_DEPOT) {
|
2007-02-12 23:46:10 +01:00
|
|
|
/* Vehicle has arrived at a bay in a road stop */
|
2004-12-05 13:43:04 +01:00
|
|
|
Order old_order;
|
2004-09-10 21:02:27 +02:00
|
|
|
|
2007-02-12 23:46:10 +01:00
|
|
|
/* Clear road stop busy bit to allow another vehicle to enter or leave */
|
2007-02-13 01:25:42 +01:00
|
|
|
rs->SetEntranceBusy(false);
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2006-03-24 09:55:08 +01:00
|
|
|
v->last_station_visited = GetStationIndex(v->tile);
|
2004-08-09 19:04:08 +02:00
|
|
|
|
|
|
|
RoadVehArrivesAt(v, st);
|
|
|
|
|
2004-12-05 13:43:04 +01:00
|
|
|
old_order = v->current_order;
|
2007-01-13 19:55:54 +01:00
|
|
|
v->BeginLoading();
|
2004-12-05 13:43:04 +01:00
|
|
|
v->current_order.flags = 0;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2004-12-05 13:43:04 +01:00
|
|
|
if (old_order.type == OT_GOTO_STATION &&
|
2006-09-03 10:25:27 +02:00
|
|
|
v->current_order.dest == v->last_station_visited) {
|
2004-12-05 13:43:04 +01:00
|
|
|
v->current_order.flags =
|
2005-06-15 18:58:15 +02:00
|
|
|
(old_order.flags & (OF_FULL_LOAD | OF_UNLOAD | OF_TRANSFER)) | OF_NON_STOP;
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SET_EXPENSES_TYPE(EXPENSES_ROADVEH_INC);
|
2006-12-02 17:56:32 +01:00
|
|
|
if (LoadUnloadVehicle(v, true)) {
|
2004-08-09 19:04:08 +02:00
|
|
|
InvalidateWindow(WC_ROADVEH_LIST, v->owner);
|
|
|
|
MarkRoadVehDirty(v);
|
|
|
|
}
|
2004-12-22 00:27:58 +01:00
|
|
|
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
|
2004-08-09 19:04:08 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-02-12 23:46:10 +01:00
|
|
|
/* Vehicle is ready to leave a bay in a road stop */
|
2004-12-05 13:43:04 +01:00
|
|
|
if (v->current_order.type != OT_GOTO_DEPOT) {
|
2007-02-13 01:25:42 +01:00
|
|
|
if (rs->IsEntranceBusy()) {
|
|
|
|
/* Road stop entrance is busy, so wait */
|
2004-08-09 19:04:08 +02:00
|
|
|
v->cur_speed = 0;
|
|
|
|
return;
|
|
|
|
}
|
2004-12-05 13:43:04 +01:00
|
|
|
v->current_order.type = OT_NOTHING;
|
|
|
|
v->current_order.flags = 0;
|
2006-03-02 09:55:12 +01:00
|
|
|
ClearSlot(v);
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
2007-02-12 23:46:10 +01:00
|
|
|
/* Set road stop busy bit to prevent another vehicle trying to enter or leave */
|
2007-02-13 01:25:42 +01:00
|
|
|
rs->SetEntranceBusy(true);
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2005-01-29 20:41:44 +01:00
|
|
|
if (rs == v->u.road.slot) {
|
2007-02-12 23:46:10 +01:00
|
|
|
/* We are leaving the correct station */
|
2006-03-02 09:55:12 +01:00
|
|
|
ClearSlot(v);
|
2005-01-29 20:41:44 +01:00
|
|
|
} else if (v->u.road.slot != NULL) {
|
2007-02-12 23:46:10 +01:00
|
|
|
/* We are leaving the wrong station */
|
2005-01-29 20:41:44 +01:00
|
|
|
//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 18:36:18 +01:00
|
|
|
DEBUG(ms, 0, "Vehicle %d (index %d) arrived at wrong stop", v->unitnumber, v->index);
|
2006-03-03 23:21:29 +01:00
|
|
|
if (v->tile != v->dest_tile) {
|
2006-12-26 18:36:18 +01:00
|
|
|
DEBUG(ms, 2, " current tile 0x%X is not destination tile 0x%X. Route problem", v->tile, v->dest_tile);
|
2006-03-03 23:21:29 +01:00
|
|
|
}
|
|
|
|
if (v->dest_tile != v->u.road.slot->xy) {
|
2006-12-26 18:36:18 +01: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 23:21:29 +01:00
|
|
|
}
|
2006-03-02 09:55:12 +01:00
|
|
|
if (v->current_order.type != OT_GOTO_STATION) {
|
2006-12-26 18:36:18 +01:00
|
|
|
DEBUG(ms, 2, " current order type (%d) is not OT_GOTO_STATION", v->current_order.type);
|
2006-03-02 09:55:12 +01:00
|
|
|
} else {
|
2006-09-03 10:25:27 +02:00
|
|
|
if (v->current_order.dest != st->index)
|
2006-12-26 18:36:18 +01:00
|
|
|
DEBUG(ms, 2, " current station %d is not target station in current_order.station (%d)",
|
2006-09-03 10:25:27 +02:00
|
|
|
st->index, v->current_order.dest);
|
2006-03-02 09:55:12 +01:00
|
|
|
}
|
|
|
|
|
2006-12-26 18:36:18 +01:00
|
|
|
DEBUG(ms, 2, " force a slot clearing");
|
2006-03-02 09:55:12 +01:00
|
|
|
ClearSlot(v);
|
2005-01-29 20:41:44 +01:00
|
|
|
}
|
|
|
|
|
2004-08-09 19:04:08 +02:00
|
|
|
StartRoadVehSound(v);
|
2004-12-22 00:27:58 +01:00
|
|
|
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
2007-02-12 23:46:10 +01:00
|
|
|
/* Check tile position conditions - i.e. stop position in depot,
|
|
|
|
* entry onto bridge or into tunnel */
|
2004-08-09 19:04:08 +02:00
|
|
|
r = VehicleEnterTile(v, v->tile, x, y);
|
2007-02-13 11:26:53 +01:00
|
|
|
if (HASBIT(r, VETS_CANNOT_ENTER)) {
|
2007-02-12 23:46:10 +01:00
|
|
|
/* Vehicle cannot continue */
|
2004-08-09 19:04:08 +02:00
|
|
|
v->cur_speed = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-02-12 23:46:10 +01:00
|
|
|
/* Move to next frame unless vehicle arrived at a stop position
|
|
|
|
* in a depot or entered a tunnel/bridge */
|
2007-02-13 11:26:53 +01:00
|
|
|
if (!HASBIT(r, VETS_ENTERED_WORMHOLE)) v->u.road.frame++;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
|
|
|
v->cur_image = GetRoadVehImage(v, v->direction);
|
|
|
|
UpdateRoadVehDeltaXY(v);
|
|
|
|
RoadZPosAffectSpeed(v, SetRoadVehPosition(v, x, y));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void AgeRoadVehCargo(Vehicle *v)
|
|
|
|
{
|
2006-02-13 22:15:00 +01:00
|
|
|
if (_age_cargo_skip_counter != 0) return;
|
|
|
|
if (v->cargo_days != 255) v->cargo_days++;
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void RoadVeh_Tick(Vehicle *v)
|
|
|
|
{
|
|
|
|
AgeRoadVehCargo(v);
|
2005-02-22 20:38:44 +01:00
|
|
|
RoadVehController(v);
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void CheckIfRoadVehNeedsService(Vehicle *v)
|
|
|
|
{
|
2006-02-18 15:41:24 +01:00
|
|
|
const Depot* depot;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2006-02-13 22:15:00 +01:00
|
|
|
if (_patches.servint_roadveh == 0) return;
|
|
|
|
if (!VehicleNeedsService(v)) return;
|
2006-04-03 16:25:32 +02:00
|
|
|
if (v->vehstatus & VS_STOPPED) return;
|
2006-02-13 22:15:00 +01:00
|
|
|
if (_patches.gotodepot && VehicleHasDepotOrders(v)) return;
|
2004-09-10 21:02:27 +02:00
|
|
|
|
2004-08-09 19:04:08 +02:00
|
|
|
// Don't interfere with a depot visit scheduled by the user, or a
|
|
|
|
// depot visit by the order list.
|
2004-12-05 13:43:04 +01:00
|
|
|
if (v->current_order.type == OT_GOTO_DEPOT &&
|
2005-03-20 09:43:29 +01:00
|
|
|
(v->current_order.flags & (OF_HALT_IN_DEPOT | OF_PART_OF_ORDERS)) != 0)
|
2004-08-09 19:04:08 +02:00
|
|
|
return;
|
|
|
|
|
2006-02-13 22:15:00 +01:00
|
|
|
// If we already got a slot at a stop, use that FIRST, and go to a depot later
|
|
|
|
if (v->u.road.slot != NULL) return;
|
2005-01-29 20:41:44 +01:00
|
|
|
|
2006-09-03 13:49:38 +02:00
|
|
|
if (IsRoadVehInDepot(v)) {
|
|
|
|
VehicleServiceInDepot(v);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-04-21 22:10:53 +02:00
|
|
|
// XXX If we already have a depot order, WHY do we search over and over?
|
2005-02-06 11:18:47 +01:00
|
|
|
depot = FindClosestRoadDepot(v);
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2005-02-06 11:18:47 +01:00
|
|
|
if (depot == NULL || DistanceManhattan(v->tile, depot->xy) > 12) {
|
2005-03-19 22:16:22 +01:00
|
|
|
if (v->current_order.type == OT_GOTO_DEPOT) {
|
2004-12-05 13:43:04 +01:00
|
|
|
v->current_order.type = OT_DUMMY;
|
|
|
|
v->current_order.flags = 0;
|
2004-12-22 00:27:58 +01:00
|
|
|
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-12-05 13:43:04 +01:00
|
|
|
if (v->current_order.type == OT_GOTO_DEPOT &&
|
|
|
|
v->current_order.flags & OF_NON_STOP &&
|
2006-02-18 15:41:24 +01:00
|
|
|
!CHANCE16(1, 20)) {
|
2004-08-09 19:04:08 +02:00
|
|
|
return;
|
2006-02-18 15:41:24 +01:00
|
|
|
}
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2004-12-05 13:43:04 +01:00
|
|
|
v->current_order.type = OT_GOTO_DEPOT;
|
|
|
|
v->current_order.flags = OF_NON_STOP;
|
2006-09-03 10:25:27 +02:00
|
|
|
v->current_order.dest = depot->index;
|
2005-02-06 11:18:47 +01:00
|
|
|
v->dest_tile = depot->xy;
|
2004-12-22 00:27:58 +01:00
|
|
|
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void OnNewDay_RoadVeh(Vehicle *v)
|
|
|
|
{
|
|
|
|
int32 cost;
|
|
|
|
|
2006-02-13 22:15:00 +01:00
|
|
|
if ((++v->day_counter & 7) == 0) DecreaseVehicleValue(v);
|
|
|
|
if (v->u.road.blocked_ctr == 0) CheckVehicleBreakdown(v);
|
2004-08-09 19:04:08 +02:00
|
|
|
|
|
|
|
AgeVehicle(v);
|
|
|
|
CheckIfRoadVehNeedsService(v);
|
|
|
|
|
2006-03-04 12:15:44 +01:00
|
|
|
CheckOrders(v);
|
2004-08-11 12:15:38 +02:00
|
|
|
|
2006-03-02 09:55:12 +01:00
|
|
|
//Current slot has expired
|
2006-04-08 16:18:39 +02:00
|
|
|
if (v->current_order.type == OT_GOTO_STATION && v->u.road.slot != NULL && v->u.road.slot_age-- == 0) {
|
2006-12-26 18:36:18 +01:00
|
|
|
DEBUG(ms, 3, "Slot expired for vehicle %d (index %d) at stop 0x%X",
|
2006-04-03 16:25:32 +02:00
|
|
|
v->unitnumber, v->index, v->u.road.slot->xy);
|
2006-03-02 09:55:12 +01:00
|
|
|
ClearSlot(v);
|
|
|
|
}
|
|
|
|
|
2006-03-16 17:47:39 +01:00
|
|
|
if (v->vehstatus & VS_STOPPED) return;
|
|
|
|
|
2004-08-09 19:04:08 +02:00
|
|
|
/* update destination */
|
2006-04-03 16:25:32 +02:00
|
|
|
if (v->current_order.type == OT_GOTO_STATION && v->u.road.slot == NULL && !(v->vehstatus & VS_CRASHED)) {
|
2006-09-03 10:25:27 +02:00
|
|
|
Station* st = GetStation(v->current_order.dest);
|
2007-01-31 05:34:56 +01:00
|
|
|
RoadStop* rs = st->GetPrimaryRoadStop(v->cargo_type == CT_PASSENGERS ? RoadStop::BUS : RoadStop::TRUCK);
|
2006-06-27 23:25:53 +02:00
|
|
|
RoadStop* best = NULL;
|
2006-03-02 09:55:12 +01:00
|
|
|
|
|
|
|
if (rs != NULL) {
|
2007-02-02 17:51:10 +01: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 16:25:32 +02:00
|
|
|
uint dist, badness;
|
|
|
|
uint minbadness = UINT_MAX;
|
2006-03-02 09:55:12 +01:00
|
|
|
|
2006-12-26 18:36:18 +01:00
|
|
|
DEBUG(ms, 2, "Attempting to obtain a slot for vehicle %d (index %d) at station %d (0x%X)",
|
2006-06-27 23:25:53 +02:00
|
|
|
v->unitnumber, v->index, st->index, st->xy
|
|
|
|
);
|
2006-03-02 09:55:12 +01:00
|
|
|
/* Now we find the nearest road stop that has a free slot */
|
2006-04-03 16:25:32 +02:00
|
|
|
for (; rs != NULL; rs = rs->next) {
|
|
|
|
dist = RoadFindPathToStop(v, rs->xy);
|
|
|
|
if (dist == UINT_MAX) {
|
2006-12-26 18:36:18 +01:00
|
|
|
DEBUG(ms, 4, " stop 0x%X is unreachable, not treating further", rs->xy);
|
2006-03-02 09:55:12 +01:00
|
|
|
continue;
|
|
|
|
}
|
2006-05-30 12:53:27 +02:00
|
|
|
badness = (rs->num_vehicles + 1) * (rs->num_vehicles + 1) + dist;
|
2006-04-03 16:25:32 +02:00
|
|
|
|
2006-12-26 18:36:18 +01: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 09:55:12 +01:00
|
|
|
|
2006-04-03 16:25:32 +02:00
|
|
|
if (badness < minbadness) {
|
|
|
|
best = rs;
|
|
|
|
minbadness = badness;
|
2006-03-02 09:55:12 +01:00
|
|
|
}
|
2006-02-18 15:41:24 +01:00
|
|
|
}
|
2005-07-17 14:29:33 +02:00
|
|
|
|
2006-04-03 16:25:32 +02:00
|
|
|
if (best != NULL) {
|
|
|
|
best->num_vehicles++;
|
2006-12-26 18:36:18 +01:00
|
|
|
DEBUG(ms, 3, "Assigned to stop 0x%X", best->xy);
|
2006-03-02 09:55:12 +01:00
|
|
|
|
2006-04-03 16:25:32 +02:00
|
|
|
v->u.road.slot = best;
|
|
|
|
v->dest_tile = best->xy;
|
2006-03-02 09:55:12 +01:00
|
|
|
v->u.road.slot_age = 14;
|
|
|
|
} else {
|
2006-12-26 18:36:18 +01:00
|
|
|
DEBUG(ms, 3, "Could not find a suitable stop");
|
2005-07-17 15:45:43 +02:00
|
|
|
}
|
2006-03-02 09:55:12 +01:00
|
|
|
} else {
|
2006-12-26 18:36:18 +01:00
|
|
|
DEBUG(ms, 5, "Distance from station too far. Postponing slotting for vehicle %d (index %d) at station %d, (0x%X)",
|
2006-03-02 09:55:12 +01:00
|
|
|
v->unitnumber, v->index, st->index, st->xy);
|
2005-02-05 13:10:09 +01:00
|
|
|
}
|
2006-03-02 09:55:12 +01:00
|
|
|
} else {
|
2006-12-26 18:36:18 +01:00
|
|
|
DEBUG(ms, 4, "No road stop for vehicle %d (index %d) at station %d (0x%X)",
|
2006-03-02 09:55:12 +01:00
|
|
|
v->unitnumber, v->index, st->index, st->xy);
|
2005-01-29 20:41:44 +01:00
|
|
|
}
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
2004-12-03 22:57:05 +01:00
|
|
|
cost = RoadVehInfo(v->engine_type)->running_cost * _price.roadveh_running / 364;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
|
|
|
v->profit_this_year -= cost >> 8;
|
|
|
|
|
|
|
|
SET_EXPENSES_TYPE(EXPENSES_ROADVEH_RUN);
|
|
|
|
SubtractMoneyFromPlayerFract(v->owner, cost);
|
|
|
|
|
|
|
|
InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
|
2005-01-24 23:24:47 +01:00
|
|
|
InvalidateWindowClasses(WC_ROADVEH_LIST);
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-01-22 21:23:18 +01:00
|
|
|
void RoadVehiclesYearlyLoop(void)
|
2004-08-09 19:04:08 +02:00
|
|
|
{
|
|
|
|
Vehicle *v;
|
|
|
|
|
|
|
|
FOR_ALL_VEHICLES(v) {
|
|
|
|
if (v->type == VEH_Road) {
|
|
|
|
v->profit_last_year = v->profit_this_year;
|
|
|
|
v->profit_this_year = 0;
|
|
|
|
InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-06-09 09:45:26 +02:00
|
|
|
|
|
|
|
/** Refit a road vehicle to the specified cargo type
|
|
|
|
* @param tile unused
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
int32 CmdRefitRoadVeh(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
|
|
|
|
{
|
|
|
|
Vehicle *v;
|
|
|
|
int32 cost;
|
|
|
|
CargoID new_cid = GB(p2, 0, 8);
|
|
|
|
byte new_subtype = GB(p2, 8, 8);
|
|
|
|
uint16 capacity = CALLBACK_FAILED;
|
|
|
|
|
2006-08-22 20:15:17 +02:00
|
|
|
if (!IsValidVehicleID(p1)) return CMD_ERROR;
|
2006-06-09 09:45:26 +02:00
|
|
|
|
|
|
|
v = GetVehicle(p1);
|
|
|
|
|
|
|
|
if (v->type != VEH_Road || !CheckOwnership(v->owner)) return CMD_ERROR;
|
|
|
|
if (!IsRoadVehInDepotStopped(v)) return_cmd_error(STR_9013_MUST_BE_STOPPED_INSIDE);
|
|
|
|
|
|
|
|
if (new_cid > NUM_CARGO || !CanRefitTo(v->engine_type, new_cid)) return CMD_ERROR;
|
|
|
|
|
|
|
|
SET_EXPENSES_TYPE(EXPENSES_ROADVEH_RUN);
|
|
|
|
|
|
|
|
if (HASBIT(EngInfo(v->engine_type)->callbackmask, CBM_REFIT_CAPACITY)) {
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 = 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_returned_refit_capacity = capacity;
|
|
|
|
|
|
|
|
cost = 0;
|
2006-10-14 17:15:56 +02:00
|
|
|
if (IsHumanPlayer(v->owner) && new_cid != v->cargo_type) {
|
2006-09-04 11:07:52 +02:00
|
|
|
cost = GetRefitCost(v->engine_type);
|
2006-06-09 09:45:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
v->cargo_cap = capacity;
|
|
|
|
v->cargo_count = (v->cargo_type == new_cid) ? min(capacity, v->cargo_count) : 0;
|
|
|
|
v->cargo_type = new_cid;
|
|
|
|
v->cargo_subtype = new_subtype;
|
|
|
|
InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
|
|
|
|
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
|
|
|
|
RebuildVehicleLists();
|
|
|
|
}
|
|
|
|
|
|
|
|
return cost;
|
|
|
|
}
|