tron@2186: /* $Id$ */
tron@2186:
rubidium@10455: /** @file vehicle.cpp Base implementations of all vehicles. */
glx@9574:
truelight@0: #include "stdafx.h"
Darkvater@1891: #include "openttd.h"
tron@3957: #include "road_map.h"
tron@3959: #include "roadveh.h"
tron@3961: #include "ship.h"
tron@1349: #include "spritecache.h"
rubidium@9723: #include "tile_cmd.h"
rubidium@9599: #include "landscape.h"
glx@9629: #include "timetable.h"
rubidium@9723: #include "viewport_func.h"
rubidium@9723: #include "gfx_func.h"
rubidium@9826: #include "news_func.h"
rubidium@9723: #include "command_func.h"
truelight@0: #include "saveload.h"
rubidium@9724: #include "player_func.h"
celestar@1601: #include "debug.h"
matthijs@1752: #include "vehicle_gui.h"
rubidium@9723: #include "rail_type.h"
bjarni@2676: #include "train.h"
bjarni@4662: #include "aircraft.h"
peter1138@3428: #include "industry_map.h"
celestar@3404: #include "station_map.h"
tron@3957: #include "water_map.h"
rubidium@5720: #include "network/network.h"
KUDr@4130: #include "yapf/yapf.h"
peter1138@5968: #include "newgrf_callbacks.h"
peter1138@4603: #include "newgrf_engine.h"
peter1138@4656: #include "newgrf_sound.h"
rubidium@10355: #include "newgrf_station.h"
glx@9624: #include "group.h"
rubidium@9837: #include "order_func.h"
rubidium@9723: #include "strings_func.h"
rubidium@9723: #include "zoom_func.h"
rubidium@9723: #include "functions.h"
rubidium@9723: #include "date_func.h"
rubidium@9723: #include "window_func.h"
rubidium@9723: #include "vehicle_func.h"
rubidium@9724: #include "signal_func.h"
rubidium@9723: #include "sound_func.h"
rubidium@9723: #include "variables.h"
rubidium@9723: #include "autoreplace_func.h"
rubidium@9723: #include "autoreplace_gui.h"
rubidium@9723: #include "string_func.h"
rubidium@9724: #include "settings_type.h"
rubidium@10142: #include "oldpool_func.h"
rubidium@10249: #include "depot_map.h"
glx@10294: #include "animated_tile_func.h"
glx@10294: #include "effectvehicle_base.h"
truebrain@10370: #include "ai/ai.h"
rubidium@10715: #include "core/alloc_func.hpp"
rubidium@9724:
rubidium@9724: #include "table/sprites.h"
rubidium@9724: #include "table/strings.h"
truelight@0:
rubidium@9628: #define INVALID_COORD (0x7fffffff)
tron@4174: #define GEN_HASH(x, y) ((GB((y), 6, 6) << 6) + GB((x), 7, 6))
truelight@0:
rubidium@9723: VehicleID _vehicle_id_ctr_day;
rubidium@10715: const Vehicle *_place_clicked_vehicle;
rubidium@9723: VehicleID _new_vehicle_id;
rubidium@9723: uint16 _returned_refit_capacity;
rubidium@9723:
bjarni@6043:
bjarni@6043: /* Tables used in vehicle.h to find the right command for a certain vehicle type */
bjarni@6043: const uint32 _veh_build_proc_table[] = {
bjarni@2552: CMD_BUILD_RAIL_VEHICLE,
bjarni@2552: CMD_BUILD_ROAD_VEH,
bjarni@2552: CMD_BUILD_SHIP,
bjarni@2552: CMD_BUILD_AIRCRAFT,
bjarni@2552: };
bjarni@6043: const uint32 _veh_sell_proc_table[] = {
bjarni@2552: CMD_SELL_RAIL_WAGON,
bjarni@2552: CMD_SELL_ROAD_VEH,
bjarni@2552: CMD_SELL_SHIP,
bjarni@2552: CMD_SELL_AIRCRAFT,
bjarni@2552: };
tron@2753:
bjarni@6043: const uint32 _veh_refit_proc_table[] = {
bjarni@2552: CMD_REFIT_RAIL_VEHICLE,
peter1138@3990: CMD_REFIT_ROAD_VEH,
bjarni@2552: CMD_REFIT_SHIP,
bjarni@2552: CMD_REFIT_AIRCRAFT,
bjarni@2552: };
bjarni@2552:
bjarni@4451: const uint32 _send_to_depot_proc_table[] = {
Darkvater@4495: CMD_SEND_TRAIN_TO_DEPOT,
bjarni@4451: CMD_SEND_ROADVEH_TO_DEPOT,
bjarni@4451: CMD_SEND_SHIP_TO_DEPOT,
bjarni@4451: CMD_SEND_AIRCRAFT_TO_HANGAR,
bjarni@4451: };
bjarni@4451:
bjarni@2552:
truelight@1279: /* Initialize the vehicle-pool */
rubidium@9694: DEFINE_OLD_POOL_GENERIC(Vehicle, Vehicle)
truelight@1279:
glx@9800: /** Function to tell if a vehicle needs to be autorenewed
glx@9800: * @param *p The vehicle owner
glx@9800: * @return true if the vehicle is old enough for replacement
glx@9800: */
glx@9800: bool Vehicle::NeedsAutorenewing(const Player *p) const
glx@9800: {
glx@9800: /* We can always generate the Player pointer when we have the vehicle.
glx@9800: * However this takes time and since the Player pointer is often present
glx@9800: * when this function is called then it's faster to pass the pointer as an
glx@9800: * argument rather than finding it again. */
glx@9800: assert(p == GetPlayer(this->owner));
glx@9800:
glx@9800: if (!p->engine_renew) return false;
glx@9800: if (this->age - this->max_age < (p->engine_renew_months * 30)) return false;
glx@10294: if (this->age == 0) return false; // rail cars don't age and lacks a max age
glx@9800:
glx@9800: return true;
glx@9800: }
glx@9800:
bjarni@578: void VehicleServiceInDepot(Vehicle *v)
bjarni@578: {
bjarni@578: v->date_of_last_service = _date;
bjarni@578: v->breakdowns_since_last_service = 0;
tron@1926: v->reliability = GetEngine(v->engine_type)->reliability;
bjarni@4725: InvalidateWindow(WC_VEHICLE_DETAILS, v->index); // ensure that last service date and reliability are updated
bjarni@578: }
truelight@0:
rubidium@10142: bool Vehicle::NeedsServicing() const
tron@593: {
rubidium@10142: if (this->vehstatus & (VS_STOPPED | VS_CRASHED)) return false;
matthijs@1757:
rubidium@10715: if (_settings.order.no_servicing_if_no_breakdowns && _settings.difficulty.vehicle_breakdowns == 0) {
rubidium@10142: /* Vehicles set for autoreplacing needs to go to a depot even if breakdowns are turned off.
rubidium@10142: * Note: If servicing is enabled, we postpone replacement till next service. */
rubidium@10142: return EngineHasReplacementForPlayer(GetPlayer(this->owner), this->engine_type, this->group_id);
bjarni@4262: }
bjarni@4262:
rubidium@10715: return _settings.vehicle.servint_ispercent ?
rubidium@10142: (this->reliability < GetEngine(this->engine_type)->reliability * (100 - this->service_interval) / 100) :
rubidium@10142: (this->date_of_last_service + this->service_interval < _date);
rubidium@10142: }
rubidium@10142:
rubidium@10142: bool Vehicle::NeedsAutomaticServicing() const
rubidium@10142: {
rubidium@10715: if (_settings.order.gotodepot && VehicleHasDepotOrders(this)) return false;
rubidium@10142: if (this->current_order.IsType(OT_LOADING)) return false;
rubidium@10181: if (this->current_order.IsType(OT_GOTO_DEPOT) && this->current_order.GetDepotOrderType() != ODTFB_SERVICE) return false;
rubidium@10142: return NeedsServicing();
tron@593: }
tron@593:
tron@3881: StringID VehicleInTheWayErrMsg(const Vehicle* v)
truelight@0: {
tron@2631: switch (v->type) {
rubidium@6585: case VEH_TRAIN: return STR_8803_TRAIN_IN_THE_WAY;
rubidium@6585: case VEH_ROAD: return STR_9000_ROAD_VEHICLE_IN_THE_WAY;
rubidium@6585: case VEH_AIRCRAFT: return STR_A015_AIRCRAFT_IN_THE_WAY;
tron@3881: default: return STR_980E_SHIP_IN_THE_WAY;
tron@2631: }
truelight@0: }
truelight@0:
truelight@0: static void *EnsureNoVehicleProcZ(Vehicle *v, void *data)
truelight@0: {
rubidium@9723: byte z = *(byte*)data;
rubidium@9723:
rubidium@9723: if (v->type == VEH_DISASTER || (v->type == VEH_AIRCRAFT && v->subtype == AIR_SHADOW)) return NULL;
rubidium@9723: if (v->z_pos > z) return NULL;
truelight@0:
tron@3881: _error_message = VehicleInTheWayErrMsg(v);
tron@537: return v;
truelight@0: }
truelight@0:
rubidium@9723: Vehicle *FindVehicleOnTileZ(TileIndex tile, byte z)
rubidium@9723: {
rubidium@9723: return (Vehicle*)VehicleFromPos(tile, &z, &EnsureNoVehicleProcZ);
rubidium@9723: }
darkvater@1082:
tron@3794: bool EnsureNoVehicleOnGround(TileIndex tile)
truelight@0: {
rubidium@9723: return FindVehicleOnTileZ(tile, GetTileMaxZ(tile)) == NULL;
truelight@1605: }
truelight@1605:
rubidium@6191: Vehicle *FindVehicleBetween(TileIndex from, TileIndex to, byte z, bool without_crashed)
truelight@0: {
tron@926: int x1 = TileX(from);
tron@926: int y1 = TileY(from);
tron@926: int x2 = TileX(to);
tron@926: int y2 = TileY(to);
truelight@0: Vehicle *veh;
truelight@0:
truelight@0: /* Make sure x1 < x2 or y1 < y2 */
truelight@0: if (x1 > x2 || y1 > y2) {
tron@6432: Swap(x1, x2);
tron@6432: Swap(y1, y2);
truelight@0: }
truelight@919: FOR_ALL_VEHICLES(veh) {
rubidium@6191: if (without_crashed && (veh->vehstatus & VS_CRASHED) != 0) continue;
rubidium@9601: if ((veh->type == VEH_TRAIN || veh->type == VEH_ROAD) && (z == 0xFF || veh->z_pos == z)) {
rubidium@9601: if ((veh->x_pos >> 4) >= x1 && (veh->x_pos >> 4) <= x2 &&
rubidium@9601: (veh->y_pos >> 4) >= y1 && (veh->y_pos >> 4) <= y2) {
truelight@0: return veh;
truelight@0: }
truelight@0: }
truelight@0: }
truelight@0: return NULL;
truelight@0: }
truelight@0:
tron@2817:
rubidium@9723: /** Procedure called for every vehicle found in tunnel/bridge in the hash map */
rubidium@9723: static void *GetVehicleTunnelBridgeProc(Vehicle *v, void *data)
rubidium@9723: {
rubidium@9723: if (v->type != VEH_TRAIN && v->type != VEH_ROAD) return NULL;
rubidium@9723:
rubidium@9723: _error_message = VehicleInTheWayErrMsg(v);
rubidium@9723: return v;
rubidium@9723: }
rubidium@9723:
rubidium@9723: /**
rubidium@9723: * Finds vehicle in tunnel / bridge
rubidium@9723: * @param tile first end
rubidium@9723: * @param endtile second end
rubidium@9723: * @return pointer to vehicle found
rubidium@9723: */
rubidium@9723: Vehicle *GetVehicleTunnelBridge(TileIndex tile, TileIndex endtile)
rubidium@9723: {
rubidium@9723: Vehicle *v = (Vehicle*)VehicleFromPos(tile, NULL, &GetVehicleTunnelBridgeProc);
rubidium@9723: if (v != NULL) return v;
rubidium@9723:
rubidium@9723: return (Vehicle*)VehicleFromPos(endtile, NULL, &GetVehicleTunnelBridgeProc);
rubidium@9723: }
rubidium@9723:
rubidium@9723:
rubidium@10142: static void UpdateVehiclePosHash(Vehicle *v, int x, int y);
tron@2817:
truelight@0: void VehiclePositionChanged(Vehicle *v)
truelight@0: {
truelight@0: int img = v->cur_image;
truelight@0: Point pt = RemapCoords(v->x_pos + v->x_offs, v->y_pos + v->y_offs, v->z_pos);
rubidium@10142: const Sprite *spr = GetSprite(img);
truelight@0:
tron@2319: pt.x += spr->x_offs;
tron@2319: pt.y += spr->y_offs;
truelight@0:
truelight@0: UpdateVehiclePosHash(v, pt.x, pt.y);
truelight@0:
truelight@0: v->left_coord = pt.x;
truelight@0: v->top_coord = pt.y;
tron@2319: v->right_coord = pt.x + spr->width + 2;
tron@2319: v->bottom_coord = pt.y + spr->height + 2;
truelight@0: }
truelight@0:
glx@9574: /** Called after load to update coordinates */
rubidium@9723: void AfterLoadVehicles(bool clear_te_id)
truelight@0: {
truelight@0: Vehicle *v;
truelight@0:
truelight@0: FOR_ALL_VEHICLES(v) {
rubidium@9701: /* Reinstate the previous pointer */
rubidium@9701: if (v->Next() != NULL) v->Next()->previous = v;
rubidium@9701:
rubidium@9620: v->UpdateDeltaXY(v->direction);
rubidium@9620:
rubidium@9723: if (clear_te_id) v->fill_percent_te_id = INVALID_TE_ID;
celestar@1601: v->first = NULL;
rubidium@6585: if (v->type == VEH_TRAIN) v->u.rail.first_engine = INVALID_ENGINE;
glx@9627: if (v->type == VEH_ROAD) v->u.road.first_engine = INVALID_ENGINE;
glx@9629:
glx@9629: v->cargo.InvalidateCache();
celestar@3355: }
celestar@3355:
celestar@3355: FOR_ALL_VEHICLES(v) {
rubidium@9701: /* Fill the first pointers */
rubidium@9701: if (v->Previous() == NULL) {
rubidium@9701: for (Vehicle *u = v; u != NULL; u = u->Next()) {
rubidium@9701: u->first = v;
rubidium@9701: }
rubidium@9701: }
rubidium@9701: }
rubidium@9701:
rubidium@9701: FOR_ALL_VEHICLES(v) {
rubidium@9701: assert(v->first != NULL);
rubidium@9701:
glx@9627: if (v->type == VEH_TRAIN && (IsFrontEngine(v) || IsFreeWagon(v))) {
rubidium@9724: if (IsFrontEngine(v)) v->u.rail.last_speed = v->cur_speed; // update displayed train speed
peter1138@2994: TrainConsistChanged(v);
glx@9627: } else if (v->type == VEH_ROAD && IsRoadVehFront(v)) {
glx@9627: RoadVehUpdateCache(v);
glx@9627: }
peter1138@2994: }
peter1138@2994:
peter1138@2994: FOR_ALL_VEHICLES(v) {
truelight@4346: switch (v->type) {
glx@9624: case VEH_ROAD:
rubidium@9722: v->u.road.roadtype = HasBit(EngInfo(v->engine_type)->misc_flags, EF_ROAD_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD;
glx@9624: v->u.road.compatible_roadtypes = RoadTypeToRoadTypes(v->u.road.roadtype);
rubidium@9631: /* FALL THROUGH */
rubidium@9631: case VEH_TRAIN:
rubidium@9631: case VEH_SHIP:
rubidium@9631: v->cur_image = v->GetImage(v->direction);
glx@9624: break;
glx@9624:
rubidium@6585: case VEH_AIRCRAFT:
Darkvater@6105: if (IsNormalAircraft(v)) {
rubidium@9631: v->cur_image = v->GetImage(v->direction);
Darkvater@6117:
Darkvater@6117: /* The plane's shadow will have the same image as the plane */
rubidium@9701: Vehicle *shadow = v->Next();
Darkvater@6117: shadow->cur_image = v->cur_image;
Darkvater@6117:
Darkvater@6117: /* In the case of a helicopter we will update the rotor sprites */
Darkvater@6117: if (v->subtype == AIR_HELICOPTER) {
rubidium@9701: Vehicle *rotor = shadow->Next();
Darkvater@6117: rotor->cur_image = GetRotorImage(v);
Darkvater@6117: }
rubidium@9601:
rubidium@9601: UpdateAircraftCache(v);
truelight@4346: }
truelight@4346: break;
truelight@4346: default: break;
truelight@0: }
truelight@4346:
truelight@4346: v->left_coord = INVALID_COORD;
truelight@4346: VehiclePositionChanged(v);
truelight@0: }
truelight@0: }
truelight@0:
rubidium@9694: Vehicle::Vehicle()
truelight@0: {
rubidium@9694: this->type = VEH_INVALID;
rubidium@9694: this->left_coord = INVALID_COORD;
rubidium@9694: this->group_id = DEFAULT_GROUP;
rubidium@9694: this->fill_percent_te_id = INVALID_TE_ID;
rubidium@9701: this->first = this;
rubidium@9722: this->colormap = PAL_NONE;
truelight@0: }
truelight@0:
peter1138@2804: /**
peter1138@2804: * Get a value for a vehicle's random_bits.
peter1138@2804: * @return A random value from 0 to 255.
peter1138@2804: */
rubidium@6573: byte VehicleRandomBits()
peter1138@2804: {
peter1138@2804: return GB(Random(), 0, 8);
peter1138@2804: }
peter1138@2804:
rubidium@9694:
rubidium@9694: /* static */ bool Vehicle::AllocateList(Vehicle **vl, int num)
truelight@0: {
rubidium@9694: uint counter = _Vehicle_pool.first_free_index;
rubidium@9694:
rubidium@9694: for (int i = 0; i != num; i++) {
rubidium@9694: Vehicle *v = AllocateRaw(counter);
rubidium@9694:
rubidium@9694: if (v == NULL) return false;
rubidium@9694: v = new (v) InvalidVehicle();
rubidium@9694:
bjarni@2606: if (vl != NULL) {
bjarni@2606: vl[i] = v;
bjarni@2606: }
rubidium@9694: counter++;
bjarni@2601: }
bjarni@2601:
bjarni@2606: return true;
bjarni@2601: }
bjarni@2601:
glx@9627: /* Size of the hash, 6 = 64 x 64, 7 = 128 x 128. Larger sizes will (in theory) reduce hash
glx@9627: * lookup times at the expense of memory usage. */
glx@9627: const int HASH_BITS = 7;
glx@9627: const int HASH_SIZE = 1 << HASH_BITS;
glx@9627: const int HASH_MASK = HASH_SIZE - 1;
glx@9627: const int TOTAL_HASH_SIZE = 1 << (HASH_BITS * 2);
glx@9627: const int TOTAL_HASH_MASK = TOTAL_HASH_SIZE - 1;
glx@9627:
glx@9627: /* Resolution of the hash, 0 = 1*1 tile, 1 = 2*2 tiles, 2 = 4*4 tiles, etc.
glx@9627: * Profiling results show that 0 is fastest. */
glx@9627: const int HASH_RES = 0;
glx@9627:
glx@9627: static Vehicle *_new_vehicle_position_hash[TOTAL_HASH_SIZE];
glx@9627:
glx@9627: static void *VehicleFromHash(int xl, int yl, int xu, int yu, void *data, VehicleFromPosProc *proc)
glx@9627: {
glx@9627: for (int y = yl; ; y = (y + (1 << HASH_BITS)) & (HASH_MASK << HASH_BITS)) {
glx@9627: for (int x = xl; ; x = (x + 1) & HASH_MASK) {
glx@9627: Vehicle *v = _new_vehicle_position_hash[(x + y) & TOTAL_HASH_MASK];
glx@9627: for (; v != NULL; v = v->next_new_hash) {
glx@9627: void *a = proc(v, data);
glx@9627: if (a != NULL) return a;
glx@9627: }
glx@9627: if (x == xu) break;
glx@9627: }
glx@9627: if (y == yu) break;
glx@9627: }
glx@9627:
glx@9627: return NULL;
glx@9627: }
glx@9627:
glx@9627:
glx@9627: void *VehicleFromPosXY(int x, int y, void *data, VehicleFromPosProc *proc)
glx@9627: {
glx@9627: const int COLL_DIST = 6;
glx@9627:
glx@9627: /* Hash area to scan is from xl,yl to xu,yu */
glx@9627: int xl = GB((x - COLL_DIST) / TILE_SIZE, HASH_RES, HASH_BITS);
glx@9627: int xu = GB((x + COLL_DIST) / TILE_SIZE, HASH_RES, HASH_BITS);
glx@9627: int yl = GB((y - COLL_DIST) / TILE_SIZE, HASH_RES, HASH_BITS) << HASH_BITS;
glx@9627: int yu = GB((y + COLL_DIST) / TILE_SIZE, HASH_RES, HASH_BITS) << HASH_BITS;
glx@9627:
glx@9627: return VehicleFromHash(xl, yl, xu, yu, data, proc);
glx@9627: }
glx@9627:
tron@2651:
truelight@0: void *VehicleFromPos(TileIndex tile, void *data, VehicleFromPosProc *proc)
truelight@0: {
glx@9627: int x = GB(TileX(tile), HASH_RES, HASH_BITS);
glx@9627: int y = GB(TileY(tile), HASH_RES, HASH_BITS) << HASH_BITS;
glx@9627:
glx@9627: Vehicle *v = _new_vehicle_position_hash[(x + y) & TOTAL_HASH_MASK];
glx@9627: for (; v != NULL; v = v->next_new_hash) {
glx@9627: if (v->tile != tile) continue;
glx@9627:
glx@9627: void *a = proc(v, data);
glx@9627: if (a != NULL) return a;
truelight@0: }
glx@9627:
truelight@0: return NULL;
truelight@0: }
truelight@0:
rubidium@9628: static void UpdateNewVehiclePosHash(Vehicle *v, bool remove)
glx@9627: {
glx@9627: Vehicle **old_hash = v->old_new_hash;
glx@9627: Vehicle **new_hash;
glx@9627:
rubidium@9628: if (remove) {
glx@9627: new_hash = NULL;
glx@9627: } else {
rubidium@9694: int x = GB(TileX(v->tile), HASH_RES, HASH_BITS);
rubidium@9694: int y = GB(TileY(v->tile), HASH_RES, HASH_BITS) << HASH_BITS;
glx@9627: new_hash = &_new_vehicle_position_hash[(x + y) & TOTAL_HASH_MASK];
glx@9627: }
glx@9627:
glx@9627: if (old_hash == new_hash) return;
glx@9627:
glx@9627: /* Remove from the old position in the hash table */
glx@9627: if (old_hash != NULL) {
glx@9627: Vehicle *last = NULL;
glx@9627: Vehicle *u = *old_hash;
glx@9627: while (u != v) {
glx@9627: last = u;
glx@9627: u = u->next_new_hash;
glx@9627: assert(u != NULL);
glx@9627: }
glx@9627:
glx@9627: if (last == NULL) {
glx@9627: *old_hash = v->next_new_hash;
glx@9627: } else {
glx@9627: last->next_new_hash = v->next_new_hash;
glx@9627: }
glx@9627: }
glx@9627:
glx@9627: /* Insert vehicle at beginning of the new position in the hash table */
glx@9627: if (new_hash != NULL) {
glx@9627: v->next_new_hash = *new_hash;
glx@9627: *new_hash = v;
glx@9627: assert(v != v->next_new_hash);
glx@9627: }
glx@9627:
glx@9627: /* Remember current hash position */
glx@9627: v->old_new_hash = new_hash;
glx@9627: }
glx@9627:
glx@9627: static Vehicle *_vehicle_position_hash[0x1000];
truelight@0:
rubidium@10142: static void UpdateVehiclePosHash(Vehicle *v, int x, int y)
truelight@0: {
rubidium@9628: UpdateNewVehiclePosHash(v, x == INVALID_COORD);
glx@9627:
peter1138@5825: Vehicle **old_hash, **new_hash;
truelight@0: int old_x = v->left_coord;
truelight@0: int old_y = v->top_coord;
truelight@0:
rubidium@9601: new_hash = (x == INVALID_COORD) ? NULL : &_vehicle_position_hash[GEN_HASH(x, y)];
truelight@0: old_hash = (old_x == INVALID_COORD) ? NULL : &_vehicle_position_hash[GEN_HASH(old_x, old_y)];
truelight@193:
tron@2639: if (old_hash == new_hash) return;
truelight@0:
truelight@0: /* remove from hash table? */
truelight@0: if (old_hash != NULL) {
truelight@0: Vehicle *last = NULL;
peter1138@5825: Vehicle *u = *old_hash;
peter1138@5825: while (u != v) {
truelight@0: last = u;
peter1138@5825: u = u->next_hash;
peter1138@5825: assert(u != NULL);
truelight@0: }
truelight@0:
tron@2639: if (last == NULL) {
truelight@0: *old_hash = v->next_hash;
tron@2639: } else {
truelight@0: last->next_hash = v->next_hash;
tron@2639: }
truelight@0: }
truelight@0:
truelight@0: /* insert into hash table? */
truelight@0: if (new_hash != NULL) {
truelight@0: v->next_hash = *new_hash;
peter1138@5825: *new_hash = v;
truelight@0: }
truelight@0: }
truelight@0:
rubidium@6573: void ResetVehiclePosHash()
Darkvater@5352: {
rubidium@9628: Vehicle *v;
rubidium@9628: FOR_ALL_VEHICLES(v) { v->old_new_hash = NULL; }
peter1138@5825: memset(_vehicle_position_hash, 0, sizeof(_vehicle_position_hash));
glx@9627: memset(_new_vehicle_position_hash, 0, sizeof(_new_vehicle_position_hash));
Darkvater@5352: }
Darkvater@5352:
rubidium@9722: void ResetVehicleColorMap()
rubidium@9722: {
rubidium@9722: Vehicle *v;
rubidium@9722: FOR_ALL_VEHICLES(v) { v->colormap = PAL_NONE; }
rubidium@9722: }
rubidium@9722:
rubidium@6573: void InitializeVehicles()
truelight@0: {
rubidium@9694: _Vehicle_pool.CleanPool();
rubidium@9694: _Vehicle_pool.AddBlockToPool();
Darkvater@5352:
Darkvater@5352: ResetVehiclePosHash();
truelight@0: }
truelight@0:
truelight@0: Vehicle *GetLastVehicleInChain(Vehicle *v)
truelight@0: {
rubidium@9701: while (v->Next() != NULL) v = v->Next();
truelight@0: return v;
truelight@0: }
truelight@0:
rubidium@10715: const Vehicle *GetLastVehicleInChain(const Vehicle *v)
rubidium@10715: {
rubidium@10715: while (v->Next() != NULL) v = v->Next();
rubidium@10715: return v;
rubidium@10715: }
rubidium@10715:
tron@2630: uint CountVehiclesInChain(const Vehicle* v)
truelight@0: {
tron@2639: uint count = 0;
rubidium@9701: do count++; while ((v = v->Next()) != NULL);
truelight@0: return count;
truelight@0: }
truelight@0:
bjarni@4574: /** Check if a vehicle is counted in num_engines in each player struct
bjarni@4574: * @param *v Vehicle to test
bjarni@4574: * @return true if the vehicle is counted in num_engines
bjarni@4574: */
bjarni@4574: bool IsEngineCountable(const Vehicle *v)
bjarni@4574: {
bjarni@4574: switch (v->type) {
rubidium@6585: case VEH_AIRCRAFT: return IsNormalAircraft(v); // don't count plane shadows and helicopter rotors
rubidium@6585: case VEH_TRAIN:
bjarni@4574: return !IsArticulatedPart(v) && // tenders and other articulated parts
rubidium@9703: !IsRearDualheaded(v); // rear parts of multiheaded engines
glx@9627: case VEH_ROAD: return IsRoadVehFront(v);
glx@9627: case VEH_SHIP: return true;
bjarni@4574: default: return false; // Only count player buildable vehicles
bjarni@4574: }
bjarni@4574: }
bjarni@4574:
rubidium@9701: void Vehicle::PreDestructor()
truelight@0: {
rubidium@9701: if (CleaningPool()) return;
rubidium@9701:
rubidium@9694: if (IsValidStationID(this->last_station_visited)) {
rubidium@9694: GetStation(this->last_station_visited)->loading_vehicles.remove(this);
rubidium@9694:
rubidium@9694: HideFillingPercent(this->fill_percent_te_id);
rubidium@9694: this->fill_percent_te_id = INVALID_TE_ID;
rubidium@9601: }
rubidium@9601:
rubidium@9694: if (IsEngineCountable(this)) {
rubidium@9694: GetPlayer(this->owner)->num_engines[this->engine_type]--;
rubidium@9694: if (this->owner == _local_player) InvalidateAutoreplaceWindow(this->engine_type, this->group_id);
rubidium@9694:
rubidium@9694: if (IsValidGroupID(this->group_id)) GetGroup(this->group_id)->num_engines[this->engine_type]--;
rubidium@9694: if (this->IsPrimaryVehicle()) DecreaseGroupNumVehicle(this->group_id);
bjarni@6195: }
bjarni@4574:
rubidium@9694: if (this->type == VEH_ROAD) ClearSlot(this);
rubidium@9694:
rubidium@9694: if (this->type != VEH_TRAIN || (this->type == VEH_TRAIN && (IsFrontEngine(this) || IsFreeWagon(this)))) {
rubidium@9694: InvalidateWindowData(WC_VEHICLE_DEPOT, this->tile);
bjarni@4739: }
bjarni@4739:
rubidium@9694: this->cargo.Truncate(0);
rubidium@9701: DeleteVehicleOrders(this);
truelight@4404:
truelight@4404: /* Now remove any artic part. This will trigger an other
truelight@4404: * destroy vehicle, which on his turn can remove any
truelight@4404: * other artic parts. */
rubidium@9694: if ((this->type == VEH_TRAIN && EngineHasArticPart(this)) || (this->type == VEH_ROAD && RoadVehHasArticPart(this))) {
rubidium@9701: delete this->Next();
glx@9627: }
rubidium@9722:
glx@10645: extern void StopGlobalFollowVehicle(const Vehicle *v);
glx@10645: StopGlobalFollowVehicle(this);
rubidium@9701: }
rubidium@9701:
rubidium@9701: Vehicle::~Vehicle()
rubidium@9701: {
rubidium@9724: free(this->name);
rubidium@9701:
rubidium@9701: if (CleaningPool()) return;
rubidium@9701:
rubidium@9701: this->SetNext(NULL);
rubidium@9701: UpdateVehiclePosHash(this, INVALID_COORD, 0);
rubidium@9701: this->next_hash = NULL;
rubidium@9701: this->next_new_hash = NULL;
rubidium@9701:
rubidium@9701: DeleteVehicleNews(this->index, INVALID_STRING_ID);
rubidium@9694:
rubidium@9694: new (this) InvalidVehicle();
rubidium@9694: }
rubidium@9694:
glx@9626: /**
glx@9626: * Deletes all vehicles in a chain.
glx@9626: * @param v The first vehicle in the chain.
glx@9626: */
truelight@0: void DeleteVehicleChain(Vehicle *v)
truelight@0: {
truelight@9718: assert(v->First() == v);
glx@9626:
truelight@0: do {
truelight@0: Vehicle *u = v;
rubidium@9724: /* sometimes, eg. for disaster vehicles, when company bankrupts, when removing crashed/flooded vehicles,
rubidium@9724: * it may happen that vehicle chain is deleted when visible */
rubidium@9724: if (!(v->vehstatus & VS_HIDDEN)) MarkSingleVehicleDirty(v);
rubidium@9701: v = v->Next();
rubidium@9694: delete u;
Darkvater@1765: } while (v != NULL);
truelight@0: }
truelight@0:
glx@9574: /** head of the linked list to tell what vehicles that visited a depot in a tick */
tron@2630: static Vehicle* _first_veh_in_depot_list;
bjarni@2574:
bjarni@2574: /** Adds a vehicle to the list of vehicles, that visited a depot this tick
rubidium@4549: * @param *v vehicle to add
rubidium@4549: */
bjarni@2574: void VehicleEnteredDepotThisTick(Vehicle *v)
bjarni@2574: {
glx@9732: /* We need to set v->leave_depot_instantly as we have no control of it's contents at this time.
glx@9732: * Vehicle should stop in the depot if it was in 'stopping' state - train intered depot while slowing down. */
rubidium@10142: if (((v->current_order.GetDepotActionType() & ODATFB_HALT) && !(v->current_order.GetDepotOrderType() & ODTFB_PART_OF_ORDERS) && v->current_order.IsType(OT_GOTO_DEPOT)) ||
glx@9732: (v->vehstatus & VS_STOPPED)) {
glx@9574: /* we keep the vehicle in the depot since the user ordered it to stay */
bjarni@2590: v->leave_depot_instantly = false;
bjarni@2590: } else {
glx@9574: /* the vehicle do not plan on stopping in the depot, so we stop it to ensure that it will not reserve the path
glx@9574: * out of the depot before we might autoreplace it to a different engine. The new engine would not own the reserved path
glx@9574: * we store that we stopped the vehicle, so autoreplace can start it again */
bjarni@2579: v->vehstatus |= VS_STOPPED;
bjarni@2579: v->leave_depot_instantly = true;
bjarni@2579: }
bjarni@2579:
bjarni@2574: if (_first_veh_in_depot_list == NULL) {
bjarni@2574: _first_veh_in_depot_list = v;
bjarni@2574: } else {
bjarni@2574: Vehicle *w = _first_veh_in_depot_list;
bjarni@2574: while (w->depot_list != NULL) w = w->depot_list;
bjarni@2574: w->depot_list = v;
bjarni@2574: }
bjarni@2574: }
truelight@0:
rubidium@6573: void CallVehicleTicks()
truelight@0: {
rubidium@4434: _first_veh_in_depot_list = NULL; // now we are sure it's initialized at the start of each tick
bjarni@2574:
glx@9624: Station *st;
glx@9624: FOR_ALL_STATIONS(st) LoadUnloadStation(st);
glx@9624:
glx@9624: Vehicle *v;
truelight@0: FOR_ALL_VEHICLES(v) {
rubidium@9631: v->Tick();
peter1138@4656:
peter1138@4656: switch (v->type) {
glx@9624: default: break;
glx@9624:
rubidium@6585: case VEH_TRAIN:
rubidium@6585: case VEH_ROAD:
rubidium@6585: case VEH_AIRCRAFT:
rubidium@6585: case VEH_SHIP:
rubidium@6585: if (v->type == VEH_TRAIN && IsTrainWagon(v)) continue;
rubidium@6585: if (v->type == VEH_AIRCRAFT && v->subtype != AIR_HELICOPTER) continue;
glx@9627: if (v->type == VEH_ROAD && !IsRoadVehFront(v)) continue;
peter1138@4656:
peter1138@4656: v->motion_counter += (v->direction & 1) ? (v->cur_speed * 3) / 4 : v->cur_speed;
peter1138@4656: /* Play a running sound if the motion counter passes 256 (Do we not skip sounds?) */
peter1138@4656: if (GB(v->motion_counter, 0, 8) < v->cur_speed) PlayVehicleSound(v, VSE_RUNNING);
peter1138@4656:
peter1138@4656: /* Play an alterate running sound every 16 ticks */
peter1138@4656: if (GB(v->tick_counter, 0, 4) == 0) PlayVehicleSound(v, v->cur_speed > 0 ? VSE_RUNNING_16 : VSE_STOPPED_16);
peter1138@4656: }
bjarni@2574: }
bjarni@2574:
glx@9574: /* now we handle all the vehicles that entered a depot this tick */
bjarni@2574: v = _first_veh_in_depot_list;
glx@10294: if (v != NULL) {
glx@10294: while (v != NULL) {
glx@10294: /* Autoreplace needs the current player set as the vehicle owner */
glx@10294: _current_player = v->owner;
glx@10294:
glx@10294: /* Buffer v->depot_list and clear it.
glx@10294: * Autoreplace might clear this so it has to be buffered. */
glx@10294: Vehicle *w = v->depot_list;
glx@10294: v->depot_list = NULL; // it should always be NULL at the end of each tick
glx@10294:
glx@10294: /* Start vehicle if we stopped them in VehicleEnteredDepotThisTick()
glx@10294: * We need to stop them between VehicleEnteredDepotThisTick() and here or we risk that
glx@10294: * they are already leaving the depot again before being replaced. */
glx@10294: if (v->leave_depot_instantly) {
glx@10294: v->leave_depot_instantly = false;
glx@10294: v->vehstatus &= ~VS_STOPPED;
glx@10294: }
rubidium@10455: MaybeReplaceVehicle(v, DC_EXEC, true);
glx@10294: v = w;
glx@10294: }
glx@10294: _current_player = OWNER_NONE;
truelight@0: }
truelight@0: }
truelight@0:
peter1138@2704: /** Check if a given engine type can be refitted to a given cargo
peter1138@2704: * @param engine_type Engine type to check
Darkvater@1802: * @param cid_to check refit to this cargo-type
Darkvater@1802: * @return true if it is possible, false otherwise
Darkvater@1802: */
peter1138@2704: bool CanRefitTo(EngineID engine_type, CargoID cid_to)
Darkvater@1802: {
rubidium@9722: return HasBit(EngInfo(engine_type)->refit_mask, cid_to);
Darkvater@1802: }
Darkvater@1802:
peter1138@3973: /** Find the first cargo type that an engine can be refitted to.
rubidium@9601: * @param engine_type Which engine to find cargo for.
peter1138@3973: * @return A climate dependent cargo type. CT_INVALID is returned if not refittable.
peter1138@3973: */
peter1138@3973: CargoID FindFirstRefittableCargo(EngineID engine_type)
peter1138@3973: {
peter1138@3973: uint32 refit_mask = EngInfo(engine_type)->refit_mask;
peter1138@3973:
peter1138@3973: if (refit_mask != 0) {
glx@9505: for (CargoID cid = 0; cid < NUM_CARGO; cid++) {
rubidium@9722: if (HasBit(refit_mask, cid)) return cid;
peter1138@3973: }
peter1138@3973: }
peter1138@3973:
peter1138@3973: return CT_INVALID;
peter1138@3973: }
peter1138@3973:
bjarni@4544: /** Learn the price of refitting a certain engine
rubidium@9601: * @param engine_type Which engine to refit
bjarni@4544: * @return Price for refitting
bjarni@4544: */
glx@9629: CommandCost GetRefitCost(EngineID engine_type)
bjarni@4544: {
rubidium@9723: Money base_cost;
rubidium@9723: ExpensesType expense_type;
bjarni@4544: switch (GetEngine(engine_type)->type) {
rubidium@9723: case VEH_SHIP:
rubidium@9723: base_cost = _price.ship_base;
rubidium@9723: expense_type = EXPENSES_SHIP_RUN;
rubidium@9723: break;
rubidium@9723:
rubidium@9723: case VEH_ROAD:
rubidium@9723: base_cost = _price.roadveh_base;
rubidium@9723: expense_type = EXPENSES_ROADVEH_RUN;
rubidium@9723: break;
rubidium@9723:
rubidium@9723: case VEH_AIRCRAFT:
rubidium@9723: base_cost = _price.aircraft_base;
rubidium@9723: expense_type = EXPENSES_AIRCRAFT_RUN;
rubidium@9723: break;
rubidium@9723:
rubidium@6585: case VEH_TRAIN:
rubidium@9723: base_cost = 2 * ((RailVehInfo(engine_type)->railveh_type == RAILVEH_WAGON) ?
rubidium@9723: _price.build_railwagon : _price.build_railvehicle);
rubidium@9723: expense_type = EXPENSES_TRAIN_RUN;
bjarni@4544: break;
rubidium@9723:
rubidium@9723: default: NOT_REACHED();
bjarni@4544: }
rubidium@9723: return CommandCost(expense_type, (EngInfo(engine_type)->refit_cost * base_cost) >> 10);
bjarni@4544: }
peter1138@3973:
Darkvater@2436: static void DoDrawVehicle(const Vehicle *v)
truelight@0: {
peter1138@5919: SpriteID image = v->cur_image;
rubidium@10142: SpriteID pal = PAL_NONE;
rubidium@10142:
rubidium@10142: if (v->vehstatus & VS_DEFPAL) pal = (v->vehstatus & VS_CRASHED) ? PALETTE_CRASH : GetVehiclePalette(v);
truelight@0:
peter1138@5919: AddSortableSpriteToDraw(image, pal, v->x_pos + v->x_offs, v->y_pos + v->y_offs,
rubidium@9869: v->x_extent, v->y_extent, v->z_extent, v->z_pos, (v->vehstatus & VS_SHADOW) != 0);
truelight@0: }
truelight@0:
truelight@0: void ViewportAddVehicles(DrawPixelInfo *dpi)
truelight@0: {
glx@9574: /* The bounding rectangle */
tron@4174: const int l = dpi->left;
tron@4174: const int r = dpi->left + dpi->width;
tron@4174: const int t = dpi->top;
tron@4174: const int b = dpi->top + dpi->height;
tron@4174:
glx@9574: /* The hash area to scan */
rubidium@9628: int xl, xu, yl, yu;
rubidium@9628:
rubidium@9628: if (dpi->width + 70 < (1 << (7 + 6))) {
rubidium@9628: xl = GB(l - 70, 7, 6);
rubidium@9628: xu = GB(r, 7, 6);
rubidium@9628: } else {
rubidium@9628: /* scan whole hash row */
rubidium@9628: xl = 0;
rubidium@9628: xu = 0x3F;
rubidium@9628: }
rubidium@9628:
rubidium@9628: if (dpi->height + 70 < (1 << (6 + 6))) {
rubidium@9628: yl = GB(t - 70, 6, 6) << 6;
rubidium@9628: yu = GB(b, 6, 6) << 6;
rubidium@9628: } else {
rubidium@9628: /* scan whole column */
rubidium@9628: yl = 0;
rubidium@9628: yu = 0x3F << 6;
rubidium@9628: }
rubidium@9628:
rubidium@9628: for (int y = yl;; y = (y + (1 << 6)) & (0x3F << 6)) {
rubidium@9628: for (int x = xl;; x = (x + 1) & 0x3F) {
rubidium@9628: const Vehicle *v = _vehicle_position_hash[x + y]; // already masked & 0xFFF
peter1138@5825:
peter1138@5825: while (v != NULL) {
truelight@193: if (!(v->vehstatus & VS_HIDDEN) &&
tron@4174: l <= v->right_coord &&
tron@4174: t <= v->bottom_coord &&
tron@4174: r >= v->left_coord &&
tron@4174: b >= v->top_coord) {
truelight@0: DoDrawVehicle(v);
truelight@0: }
peter1138@5825: v = v->next_hash;
truelight@0: }
truelight@0:
tron@4174: if (x == xu) break;
truelight@0: }
tron@4174:
tron@4174: if (y == yu) break;
truelight@0: }
truelight@0: }
truelight@0:
tron@2116: Vehicle *CheckClickOnVehicle(const ViewPort *vp, int x, int y)
truelight@0: {
truelight@0: Vehicle *found = NULL, *v;
truelight@0: uint dist, best_dist = (uint)-1;
truelight@0:
rubidium@10142: if ((uint)(x -= vp->left) >= (uint)vp->width || (uint)(y -= vp->top) >= (uint)vp->height) return NULL;
truelight@0:
glx@9624: x = ScaleByZoom(x, vp->zoom) + vp->virtual_left;
glx@9624: y = ScaleByZoom(y, vp->zoom) + vp->virtual_top;
truelight@0:
truelight@0: FOR_ALL_VEHICLES(v) {
truelight@4346: if ((v->vehstatus & (VS_HIDDEN|VS_UNCLICKABLE)) == 0 &&
truelight@0: x >= v->left_coord && x <= v->right_coord &&
truelight@0: y >= v->top_coord && y <= v->bottom_coord) {
truelight@193:
truelight@0: dist = max(
rubidium@10142: abs(((v->left_coord + v->right_coord) >> 1) - x),
rubidium@10142: abs(((v->top_coord + v->bottom_coord) >> 1) - y)
truelight@0: );
truelight@0:
truelight@0: if (dist < best_dist) {
truelight@0: found = v;
truelight@0: best_dist = dist;
truelight@0: }
truelight@0: }
truelight@0: }
truelight@0:
truelight@0: return found;
truelight@0: }
truelight@0:
rubidium@9722: void CheckVehicle32Day(Vehicle *v)
rubidium@9722: {
rubidium@9722: if ((v->day_counter & 0x1F) != 0) return;
rubidium@9722:
rubidium@9722: uint16 callback = GetVehicleCallback(CBID_VEHICLE_32DAY_CALLBACK, 0, 0, v->engine_type, v);
rubidium@9722: if (callback == CALLBACK_FAILED) return;
rubidium@9722: if (HasBit(callback, 0)) TriggerVehicle(v, VEHICLE_TRIGGER_CALLBACK_32); // Trigger vehicle trigger 10
rubidium@9722: if (HasBit(callback, 1)) v->colormap = PAL_NONE; // Update colormap via callback 2D
rubidium@9722: }
truelight@0:
truelight@0: void DecreaseVehicleValue(Vehicle *v)
truelight@0: {
truelight@0: v->value -= v->value >> 8;
truelight@0: InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
truelight@0: }
truelight@0:
truelight@0: static const byte _breakdown_chance[64] = {
rubidium@4344: 3, 3, 3, 3, 3, 3, 3, 3,
rubidium@4344: 4, 4, 5, 5, 6, 6, 7, 7,
rubidium@4344: 8, 8, 9, 9, 10, 10, 11, 11,
rubidium@4344: 12, 13, 13, 13, 13, 14, 15, 16,
rubidium@4344: 17, 19, 21, 25, 28, 31, 34, 37,
rubidium@4344: 40, 44, 48, 52, 56, 60, 64, 68,
rubidium@4344: 72, 80, 90, 100, 110, 120, 130, 140,
truelight@0: 150, 170, 190, 210, 230, 250, 250, 250,
truelight@0: };
truelight@0:
truelight@0: void CheckVehicleBreakdown(Vehicle *v)
truelight@0: {
truelight@0: int rel, rel_old;
truelight@0:
truelight@0: /* decrease reliability */
truelight@0: v->reliability = rel = max((rel_old = v->reliability) - v->reliability_spd_dec, 0);
rubidium@10249: if ((rel_old >> 8) != (rel >> 8)) InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
truelight@0:
tron@2639: if (v->breakdown_ctr != 0 || v->vehstatus & VS_STOPPED ||
rubidium@10715: _settings.difficulty.vehicle_breakdowns < 1 ||
tron@2639: v->cur_speed < 5 || _game_mode == GM_MENU) {
tron@2639: return;
tron@2639: }
truelight@0:
rubidium@10142: uint32 r = Random();
truelight@0:
truelight@0: /* increase chance of failure */
rubidium@10142: int chance = v->breakdown_chance + 1;
rubidium@10249: if (Chance16I(1, 25, r)) chance += 25;
truelight@0: v->breakdown_chance = min(255, chance);
truelight@0:
truelight@0: /* calculate reliability value to use in comparison */
truelight@0: rel = v->reliability;
rubidium@6585: if (v->type == VEH_SHIP) rel += 0x6666;
truelight@193:
truelight@0: /* reduced breakdowns? */
rubidium@10715: if (_settings.difficulty.vehicle_breakdowns == 1) rel += 0x6666;
truelight@0:
truelight@0: /* check if to break down */
truelight@0: if (_breakdown_chance[(uint)min(rel, 0xffff) >> 10] <= v->breakdown_chance) {
tron@2140: v->breakdown_ctr = GB(r, 16, 6) + 0x3F;
tron@2140: v->breakdown_delay = GB(r, 24, 7) + 0x80;
truelight@0: v->breakdown_chance = 0;
truelight@0: }
truelight@0: }
truelight@0:
truelight@0: static const StringID _vehicle_type_names[4] = {
truelight@0: STR_019F_TRAIN,
truelight@0: STR_019C_ROAD_VEHICLE,
truelight@0: STR_019E_SHIP,
truelight@0: STR_019D_AIRCRAFT,
truelight@0: };
truelight@0:
truelight@0: static void ShowVehicleGettingOld(Vehicle *v, StringID msg)
truelight@0: {
tron@2639: if (v->owner != _local_player) return;
truelight@812:
rubidium@9703: /* Do not show getting-old message if autorenew is active (and it can replace the vehicle) */
rubidium@9703: if (GetPlayer(v->owner)->engine_renew && GetEngine(v->engine_type)->player_avail != 0) return;
truelight@0:
bjarni@6206: SetDParam(0, _vehicle_type_names[v->type]);
tron@534: SetDParam(1, v->unitnumber);
glx@10645: AddNewsItem(msg, NS_ADVICE, v->index, 0);
truelight@0: }
truelight@0:
truelight@0: void AgeVehicle(Vehicle *v)
truelight@0: {
rubidium@9703: if (v->age < 65535) v->age++;
rubidium@9703:
rubidium@9703: int age = v->age - v->max_age;
rubidium@9703: if (age == 366*0 || age == 366*1 || age == 366*2 || age == 366*3 || age == 366*4) v->reliability_spd_dec <<= 1;
truelight@193:
truelight@0: InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
truelight@0:
truelight@0: if (age == -366) {
truelight@0: ShowVehicleGettingOld(v, STR_01A0_IS_GETTING_OLD);
truelight@0: } else if (age == 0) {
truelight@0: ShowVehicleGettingOld(v, STR_01A1_IS_GETTING_VERY_OLD);
rubidium@9703: } else if (age > 0 && (age % 366) == 0) {
truelight@0: ShowVehicleGettingOld(v, STR_01A2_IS_GETTING_VERY_OLD_AND);
truelight@0: }
truelight@0: }
truelight@0:
bjarni@4640: /** Starts or stops a lot of vehicles
bjarni@4673: * @param tile Tile of the depot where the vehicles are started/stopped (only used for depots)
glx@9574: * @param flags type of operation
bjarni@4762: * @param p1 Station/Order/Depot ID (only used for vehicle list windows)
bjarni@4673: * @param p2 bitmask
bjarni@4762: * - bit 0-4 Vehicle type
bjarni@4762: * - bit 5 false = start vehicles, true = stop vehicles
bjarni@4762: * - bit 6 if set, then it's a vehicle list window, not a depot and Tile is ignored in this case
bjarni@4673: * - bit 8-11 Vehicle List Window type (ignored unless bit 1 is set)
bjarni@4640: */
glx@9629: CommandCost CmdMassStartStopVehicle(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
bjarni@4640: {
rubidium@10715: VehicleList list;
glx@9629: CommandCost return_value = CMD_ERROR;
bjarni@4640: uint stop_command;
glx@9624: VehicleType vehicle_type = (VehicleType)GB(p2, 0, 5);
rubidium@9722: bool start_stop = HasBit(p2, 5);
rubidium@9722: bool vehicle_list_window = HasBit(p2, 6);
bjarni@4640:
bjarni@4640: switch (vehicle_type) {
rubidium@6585: case VEH_TRAIN: stop_command = CMD_START_STOP_TRAIN; break;
rubidium@6585: case VEH_ROAD: stop_command = CMD_START_STOP_ROADVEH; break;
rubidium@6585: case VEH_SHIP: stop_command = CMD_START_STOP_SHIP; break;
rubidium@6585: case VEH_AIRCRAFT: stop_command = CMD_START_STOP_AIRCRAFT; break;
bjarni@4640: default: return CMD_ERROR;
bjarni@4640: }
bjarni@4640:
bjarni@4673: if (vehicle_list_window) {
bjarni@5999: uint32 id = p1;
bjarni@4673: uint16 window_type = p2 & VLW_MASK;
bjarni@4673:
rubidium@10715: GenerateVehicleSortList(&list, vehicle_type, _current_player, id, window_type);
bjarni@4673: } else {
bjarni@4673: /* Get the list of vehicles in the depot */
rubidium@10715: BuildDepotVehicleList(vehicle_type, tile, &list, NULL);
bjarni@4673: }
bjarni@4640:
rubidium@10715: for (uint i = 0; i < list.Length(); i++) {
rubidium@10715: const Vehicle *v = list[i];
bjarni@4640:
bjarni@4640: if (!!(v->vehstatus & VS_STOPPED) != start_stop) continue;
bjarni@4673:
bjarni@4673: if (!vehicle_list_window) {
rubidium@6585: if (vehicle_type == VEH_TRAIN) {
bjarni@4673: if (CheckTrainInDepot(v, false) == -1) continue;
bjarni@4673: } else {
bjarni@4673: if (!(v->vehstatus & VS_HIDDEN)) continue;
bjarni@4673: }
bjarni@4648: }
bjarni@4648:
rubidium@10142: CommandCost ret = DoCommand(tile, v->index, 0, flags, stop_command);
bjarni@4640:
glx@9629: if (CmdSucceeded(ret)) {
glx@9629: return_value = CommandCost();
bjarni@4640: /* We know that the command is valid for at least one vehicle.
bjarni@4640: * If we haven't set DC_EXEC, then there is no point in continueing because it will be valid */
bjarni@4640: if (!(flags & DC_EXEC)) break;
bjarni@4640: }
bjarni@4640: }
bjarni@4640:
bjarni@4640: return return_value;
bjarni@4640: }
bjarni@4640:
bjarni@4659: /** Sells all vehicles in a depot
glx@9574: * @param tile Tile of the depot where the depot is
glx@9574: * @param flags type of operation
glx@9574: * @param p1 Vehicle type
glx@9574: * @param p2 unused
glx@9574: */
glx@9629: CommandCost CmdDepotSellAllVehicles(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
bjarni@4659: {
rubidium@10715: VehicleList list;
bjarni@4659:
rubidium@9724: CommandCost cost(EXPENSES_NEW_VEHICLES);
rubidium@10715: uint sell_command;
glx@9624: VehicleType vehicle_type = (VehicleType)GB(p1, 0, 8);
bjarni@4659:
bjarni@4659: switch (vehicle_type) {
rubidium@6585: case VEH_TRAIN: sell_command = CMD_SELL_RAIL_WAGON; break;
rubidium@6585: case VEH_ROAD: sell_command = CMD_SELL_ROAD_VEH; break;
rubidium@6585: case VEH_SHIP: sell_command = CMD_SELL_SHIP; break;
rubidium@6585: case VEH_AIRCRAFT: sell_command = CMD_SELL_AIRCRAFT; break;
bjarni@4659: default: return CMD_ERROR;
bjarni@4659: }
bjarni@4659:
bjarni@4659: /* Get the list of vehicles in the depot */
rubidium@10715: BuildDepotVehicleList(vehicle_type, tile, &list, &list);
rubidium@10715:
rubidium@10715: for (uint i = 0; i < list.Length(); i++) {
rubidium@10715: CommandCost ret = DoCommand(tile, list[i]->index, 1, flags, sell_command);
glx@9629: if (CmdSucceeded(ret)) cost.AddCost(ret);
bjarni@4659: }
bjarni@4659:
glx@9629: if (cost.GetCost() == 0) return CMD_ERROR; // no vehicles to sell
bjarni@4659: return cost;
bjarni@4659: }
bjarni@4659:
bjarni@4662: /** Autoreplace all vehicles in the depot
rubidium@9826: * Note: this command can make incorrect cost estimations
rubidium@9826: * Luckily the final price can only drop, not increase. This is due to the fact that
rubidium@9826: * estimation can't predict wagon removal so it presumes worst case which is no income from selling wagons.
glx@9574: * @param tile Tile of the depot where the vehicles are
glx@9574: * @param flags type of operation
glx@9574: * @param p1 Type of vehicle
rubidium@9826: * @param p2 If bit 0 is set, then either replace all or nothing (instead of replacing until money runs out)
glx@9574: */
glx@9629: CommandCost CmdDepotMassAutoReplace(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
bjarni@4662: {
rubidium@10715: VehicleList list;
rubidium@9826: CommandCost cost = CommandCost(EXPENSES_NEW_VEHICLES);
glx@9624: VehicleType vehicle_type = (VehicleType)GB(p1, 0, 8);
rubidium@9826: bool all_or_nothing = HasBit(p2, 0);
bjarni@4662:
rubidium@9701: if (!IsDepotTile(tile) || !IsTileOwner(tile, _current_player)) return CMD_ERROR;
bjarni@4662:
bjarni@4662: /* Get the list of vehicles in the depot */
rubidium@10715: BuildDepotVehicleList(vehicle_type, tile, &list, &list);
rubidium@10715:
rubidium@10715: for (uint i = 0; i < list.Length(); i++) {
rubidium@10715: Vehicle *v = (Vehicle*)list[i];
bjarni@4662:
bjarni@4662: /* Ensure that the vehicle completely in the depot */
rubidium@9701: if (!v->IsInDepot()) continue;
bjarni@4662:
glx@10294: CommandCost ret = MaybeReplaceVehicle(v, flags, false);
bjarni@4662:
glx@9629: if (CmdSucceeded(ret)) {
glx@9629: cost.AddCost(ret);
rubidium@9826: } else {
rubidium@9826: if (all_or_nothing) {
rubidium@9826: /* We failed to replace a vehicle even though we set all or nothing.
rubidium@9826: * We should never reach this if DC_EXEC is set since then it should
rubidium@9826: * have failed the estimation guess. */
rubidium@9826: assert(!(flags & DC_EXEC));
rubidium@10715: /* Now we will have to return an error. */
rubidium@10715: return CMD_ERROR;
rubidium@9826: }
bjarni@4662: }
bjarni@4662: }
bjarni@4662:
glx@9629: if (cost.GetCost() == 0) {
rubidium@9826: /* Either we didn't replace anything or something went wrong.
rubidium@9826: * Either way we want to return an error and not execute this command. */
bjarni@4662: cost = CMD_ERROR;
bjarni@4662: }
bjarni@4662:
bjarni@4662: return cost;
bjarni@4662: }
bjarni@4662:
bjarni@2244: /** Clone a vehicle. If it is a train, it will clone all the cars too
rubidium@4549: * @param tile tile of the depot where the cloned vehicle is build
glx@9574: * @param flags type of operation
rubidium@4549: * @param p1 the original vehicle's index
rubidium@4549: * @param p2 1 = shared orders, else copied orders
rubidium@4549: */
glx@9629: CommandCost CmdCloneVehicle(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
bjarni@2244: {
rubidium@10142: CommandCost total_cost(EXPENSES_NEW_VEHICLES);
bjarni@3816: uint32 build_argument = 2;
bjarni@2244:
truelight@4352: if (!IsValidVehicleID(p1)) return CMD_ERROR;
rubidium@10142:
rubidium@10142: Vehicle *v = GetVehicle(p1);
rubidium@10142: Vehicle *v_front = v;
rubidium@10142: Vehicle *w = NULL;
rubidium@10142: Vehicle *w_front = NULL;
rubidium@10142: Vehicle *w_rear = NULL;
rubidium@9599:
bjarni@2563: /*
bjarni@2563: * v_front is the front engine in the original vehicle
rubidium@9599: * v is the car/vehicle of the original vehicle, that is currently being copied
bjarni@2563: * w_front is the front engine of the cloned vehicle
bjarni@2563: * w is the car/vehicle currently being cloned
bjarni@2563: * w_rear is the rear end of the cloned train. It's used to add more cars and is only used by trains
bjarni@2563: */
bjarni@2244:
tron@2639: if (!CheckOwnership(v->owner)) return CMD_ERROR;
bjarni@2244:
rubidium@6585: if (v->type == VEH_TRAIN && (!IsFrontEngine(v) || v->u.rail.crash_anim_pos >= 4400)) return CMD_ERROR;
bjarni@2244:
glx@9574: /* check that we can allocate enough vehicles */
bjarni@2601: if (!(flags & DC_EXEC)) {
bjarni@2601: int veh_counter = 0;
bjarni@2601: do {
bjarni@2601: veh_counter++;
rubidium@9701: } while ((v = v->Next()) != NULL);
bjarni@2601:
rubidium@9694: if (!Vehicle::AllocateList(NULL, veh_counter)) {
bjarni@2606: return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME);
bjarni@2601: }
bjarni@2601: }
bjarni@2601:
bjarni@2601: v = v_front;
bjarni@2601:
bjarni@2563: do {
rubidium@9703: if (v->type == VEH_TRAIN && IsRearDualheaded(v)) {
bjarni@2676: /* we build the rear ends of multiheaded trains with the front ones */
bjarni@2676: continue;
bjarni@2676: }
bjarni@2676:
rubidium@10142: CommandCost cost = DoCommand(tile, v->engine_type, build_argument, flags, GetCmdBuildVeh(v));
bjarni@3819: build_argument = 3; // ensure that we only assign a number to the first engine
bjarni@2563:
bjarni@2563: if (CmdFailed(cost)) return cost;
bjarni@2563:
glx@9629: total_cost.AddCost(cost);
bjarni@2563:
bjarni@2563: if (flags & DC_EXEC) {
tron@2639: w = GetVehicle(_new_vehicle_id);
bjarni@2563:
rubidium@9722: if (v->type == VEH_TRAIN && HasBit(v->u.rail.flags, VRF_REVERSE_DIRECTION)) {
rubidium@9722: SetBit(w->u.rail.flags, VRF_REVERSE_DIRECTION);
bjarni@3896: }
bjarni@2563:
rubidium@6585: if (v->type == VEH_TRAIN && !IsFrontEngine(v)) {
glx@9574: /* this s a train car
glx@9574: * add this unit to the end of the train */
glx@9629: CommandCost result = DoCommand(0, (w_rear->index << 16) | w->index, 1, flags, CMD_MOVE_RAIL_VEHICLE);
glx@9626: if (CmdFailed(result)) {
glx@9626: /* The train can't be joined to make the same consist as the original.
glx@9626: * Sell what we already made (clean up) and return an error. */
glx@9626: DoCommand(w_front->tile, w_front->index, 1, flags, GetCmdSellVeh(w_front));
glx@9626: DoCommand(w_front->tile, w->index, 1, flags, GetCmdSellVeh(w));
glx@9626: return result; // return error and the message returned from CMD_MOVE_RAIL_VEHICLE
glx@9626: }
bjarni@2563: } else {
rubidium@9722: /* this is a front engine or not a train. */
bjarni@2563: w_front = w;
bjarni@3679: w->service_interval = v->service_interval;
bjarni@2563: }
rubidium@4434: w_rear = w; // trains needs to know the last car in the train, so they can add more in next loop
bjarni@2563: }
rubidium@6585: } while (v->type == VEH_TRAIN && (v = GetNextVehicle(v)) != NULL);
rubidium@6585:
rubidium@6585: if (flags & DC_EXEC && v_front->type == VEH_TRAIN) {
glx@9574: /* for trains this needs to be the front engine due to the callback function */
tron@3948: _new_vehicle_id = w_front->index;
bjarni@2244: }
peter1138@5062:
glx@9624: if (flags & DC_EXEC) {
glx@9624: /* Cloned vehicles belong to the same group */
glx@9624: DoCommand(0, v_front->group_id, w_front->index, flags, CMD_ADD_VEHICLE_GROUP);
glx@9624: }
glx@9624:
glx@9624:
rubidium@9620: /* Take care of refitting. */
rubidium@9620: w = w_front;
rubidium@9620: v = v_front;
rubidium@9620:
rubidium@9620: /* Both building and refitting are influenced by newgrf callbacks, which
rubidium@9620: * makes it impossible to accurately estimate the cloning costs. In
rubidium@9620: * particular, it is possible for engines of the same type to be built with
rubidium@9620: * different numbers of articulated parts, so when refitting we have to
rubidium@9620: * loop over real vehicles first, and then the articulated parts of those
rubidium@9620: * vehicles in a different loop. */
rubidium@9620: do {
rubidium@9620: do {
rubidium@9620: if (flags & DC_EXEC) {
rubidium@9620: assert(w != NULL);
rubidium@9620:
rubidium@9722: if (w->cargo_type != v->cargo_type || w->cargo_subtype != v->cargo_subtype) {
rubidium@10142: CommandCost cost = DoCommand(0, w->index, v->cargo_type | (v->cargo_subtype << 8) | 1U << 16 , flags, GetCmdRefitVeh(v));
glx@9629: if (CmdSucceeded(cost)) total_cost.AddCost(cost);
rubidium@9620: }
rubidium@9620:
rubidium@9620: if (w->type == VEH_TRAIN && EngineHasArticPart(w)) {
rubidium@9620: w = GetNextArticPart(w);
glx@9627: } else if (w->type == VEH_ROAD && RoadVehHasArticPart(w)) {
rubidium@9701: w = w->Next();
rubidium@9620: } else {
rubidium@9620: break;
rubidium@9620: }
rubidium@9620: } else {
rubidium@9620: CargoID initial_cargo = GetEngineCargoType(v->engine_type);
rubidium@9620:
rubidium@9620: if (v->cargo_type != initial_cargo && initial_cargo != CT_INVALID) {
glx@9629: total_cost.AddCost(GetRefitCost(v->engine_type));
rubidium@9620: }
rubidium@9620: }
glx@9627:
glx@9627: if (v->type == VEH_TRAIN && EngineHasArticPart(v)) {
glx@9627: v = GetNextArticPart(v);
glx@9627: } else if (v->type == VEH_ROAD && RoadVehHasArticPart(v)) {
rubidium@9701: v = v->Next();
glx@9627: } else {
glx@9627: break;
glx@9627: }
glx@9627: } while (v != NULL);
rubidium@9620:
rubidium@9625: if ((flags & DC_EXEC) && v->type == VEH_TRAIN) w = GetNextVehicle(w);
rubidium@9620: } while (v->type == VEH_TRAIN && (v = GetNextVehicle(v)) != NULL);
rubidium@9620:
rubidium@9722: if (flags & DC_EXEC) {
rubidium@9722: /*
rubidium@9722: * Set the orders of the vehicle. Cannot do it earlier as we need
rubidium@9722: * the vehicle refitted before doing this, otherwise the moved
rubidium@9722: * cargo types might not match (passenger vs non-passenger)
rubidium@9722: */
rubidium@9722: DoCommand(0, (v_front->index << 16) | w_front->index, p2 & 1 ? CO_SHARE : CO_COPY, flags, CMD_CLONE_ORDER);
rubidium@9722: }
rubidium@9722:
rubidium@9620: /* Since we can't estimate the cost of cloning a vehicle accurately we must
rubidium@9620: * check whether the player has enough money manually. */
rubidium@9620: if (!CheckPlayerHasMoney(total_cost)) {
rubidium@9620: if (flags & DC_EXEC) {
rubidium@9620: /* The vehicle has already been bought, so now it must be sold again. */
rubidium@9620: DoCommand(w_front->tile, w_front->index, 1, flags, GetCmdSellVeh(w_front));
rubidium@9620: }
rubidium@9620: return CMD_ERROR;
rubidium@9620: }
rubidium@9620:
bjarni@2244: return total_cost;
bjarni@2244: }
bjarni@2244:
rubidium@10715: /**
rubidium@10715: * Generate a list of vehicles inside a depot.
rubidium@10715: * @param type Type of vehicle
rubidium@10715: * @param tile The tile the depot is located on
rubidium@10715: * @param engines Pointer to list to add vehicles to
rubidium@10715: * @param wagons Pointer to list to add wagons to (can be NULL)
bjarni@4635: */
rubidium@10715: void BuildDepotVehicleList(VehicleType type, TileIndex tile, VehicleList *engines, VehicleList *wagons)
bjarni@4635: {
rubidium@10715: engines->Clear();
rubidium@10715: if (wagons != NULL && wagons != engines) wagons->Clear();
rubidium@10715:
rubidium@10715: const Vehicle *v;
rubidium@10715: FOR_ALL_VEHICLES(v) {
rubidium@10715: /* General tests for all vehicle types */
rubidium@10715: if (v->type != type) continue;
rubidium@10715: if (v->tile != tile) continue;
rubidium@10715:
rubidium@10715: switch (type) {
rubidium@10715: case VEH_TRAIN:
rubidium@10715: if (v->u.rail.track != TRACK_BIT_DEPOT) continue;
rubidium@10715: if (wagons != NULL && IsFreeWagon(v)) {
rubidium@10715: *wagons->Append() = v;
rubidium@10715: continue;
bjarni@4635: }
rubidium@10715: break;
rubidium@10715:
rubidium@10715: default:
rubidium@10715: if (!v->IsInDepot()) continue;
rubidium@10715: break;
rubidium@10715: }
rubidium@10715:
rubidium@10715: if (!v->IsPrimaryVehicle()) continue;
rubidium@10715:
rubidium@10715: *engines->Append() = v;
bjarni@4635: }
rubidium@10715:
rubidium@10715: /* Ensure the lists are not wasting too much space. If the lists are fresh
rubidium@10715: * (i.e. built within a command) then this will actually do nothing. */
rubidium@10715: engines->Compact();
rubidium@10715: if (wagons != NULL && wagons != engines) wagons->Compact();
bjarni@4635: }
bjarni@4635:
bjarni@4497: /**
rubidium@10142: * @param sort_list list to store the list in. Either NULL or the length length_of_array tells
rubidium@10142: * @param length_of_array informs the length allocated for sort_list. This is not the same as the number of vehicles in the list. Needs to be 0 when sort_list is NULL
rubidium@10142: * @param type type of vehicle
rubidium@10142: * @param owner PlayerID of owner to generate a list for
rubidium@10142: * @param index This parameter has different meanings depending on window_type
rubidium@10142: *
rubidium@10142: * - VLW_STATION_LIST: index of station to generate a list for
rubidium@10142: * - VLW_SHARED_ORDERS: index of order to generate a list for
-
rubidium@10142: *
- VLW_STANDARD: not used
-
rubidium@10142: *
- VLW_DEPOT_LIST: TileIndex of the depot/hangar to make the list for
rubidium@10142: * - VLW_GROUP_LIST: index of group to generate a list for
rubidium@10142: *
rubidium@10142: * @param window_type tells what kind of window the list is for. Use the VLW flags in vehicle_gui.h
rubidium@10142: * @return the number of vehicles added to the list
rubidium@10142: */
glx@9624: uint GenerateVehicleSortList(const Vehicle ***sort_list, uint16 *length_of_array, VehicleType type, PlayerID owner, uint32 index, uint16 window_type)
bjarni@4497: {
rubidium@10715: VehicleList list;
rubidium@10715: GenerateVehicleSortList(&list, type, owner, index, window_type);
rubidium@10715:
rubidium@10715: if (list.Length() > 0) {
rubidium@10715: *sort_list = ReallocT(*sort_list, list.Length());
rubidium@10715: memcpy(*sort_list, list.Begin(), sizeof(list.Begin()) * list.Length());
rubidium@10715: }
rubidium@10715:
rubidium@10715: return list.Length();
rubidium@10715: }
rubidium@10715:
rubidium@10715: /**
rubidium@10715: * Generate a list of vehicles based on window type.
rubidium@10715: * @param list Pointer to list to add vehicles to
rubidium@10715: * @param type Type of vehicle
rubidium@10715: * @param owner Player to generate list for
rubidium@10715: * @param index This parameter has different meanings depending on window_type
rubidium@10715: *
rubidium@10715: * - VLW_STATION_LIST: index of station to generate a list for
rubidium@10715: * - VLW_SHARED_ORDERS: index of order to generate a list for
-
rubidium@10715: *
- VLW_STANDARD: not used
-
rubidium@10715: *
- VLW_DEPOT_LIST: TileIndex of the depot/hangar to make the list for
rubidium@10715: * - VLW_GROUP_LIST: index of group to generate a list for
rubidium@10715: *
rubidium@10715: * @param window_type The type of window the list is for, using the VLW_ flags in vehicle_gui.h
rubidium@10715: */
rubidium@10715: void GenerateVehicleSortList(VehicleList *list, VehicleType type, PlayerID owner, uint32 index, uint16 window_type)
rubidium@10715: {
rubidium@10715: list->Clear();
rubidium@10715:
bjarni@4497: const Vehicle *v;
bjarni@4497:
bjarni@4497: switch (window_type) {
rubidium@10715: case VLW_STATION_LIST:
bjarni@4497: FOR_ALL_VEHICLES(v) {
rubidium@9625: if (v->type == type && v->IsPrimaryVehicle()) {
bjarni@4497: const Order *order;
bjarni@4497:
bjarni@4497: FOR_VEHICLE_ORDERS(v, order) {
rubidium@9869: if (order->IsType(OT_GOTO_STATION) && order->GetDestination() == index) {
rubidium@10715: *list->Append() = v;
bjarni@4497: break;
bjarni@4497: }
bjarni@4497: }
bjarni@4497: }
bjarni@4497: }
bjarni@4497: break;
rubidium@10715:
rubidium@10715: case VLW_SHARED_ORDERS:
bjarni@4497: FOR_ALL_VEHICLES(v) {
bjarni@4497: /* Find a vehicle with the order in question */
rubidium@10715: if (v->orders != NULL && v->orders->index == index) {
rubidium@10715: /* Add all vehicles from this vehicle's shared order list */
rubidium@10715: for (v = GetFirstVehicleFromSharedList(v); v != NULL; v = v->next_shared) {
rubidium@10715: *list->Append() = v;
rubidium@10715: }
rubidium@10715: break;
bjarni@4497: }
bjarni@4497: }
bjarni@4497: break;
rubidium@10715:
rubidium@10715: case VLW_STANDARD:
bjarni@4497: FOR_ALL_VEHICLES(v) {
rubidium@9625: if (v->type == type && v->owner == owner && v->IsPrimaryVehicle()) {
rubidium@10715: *list->Append() = v;
bjarni@4497: }
bjarni@4497: }
bjarni@4497: break;
rubidium@10715:
rubidium@10715: case VLW_DEPOT_LIST:
bjarni@4681: FOR_ALL_VEHICLES(v) {
rubidium@9625: if (v->type == type && v->IsPrimaryVehicle()) {
bjarni@4681: const Order *order;
bjarni@4681:
bjarni@4681: FOR_VEHICLE_ORDERS(v, order) {
rubidium@9869: if (order->IsType(OT_GOTO_DEPOT) && order->GetDestination() == index) {
rubidium@10715: *list->Append() = v;
bjarni@4681: break;
bjarni@4681: }
bjarni@4681: }
bjarni@4681: }
bjarni@4681: }
bjarni@4681: break;
bjarni@4681:
rubidium@10249: case VLW_GROUP_LIST:
glx@9624: FOR_ALL_VEHICLES(v) {
rubidium@9625: if (v->type == type && v->IsPrimaryVehicle() &&
rubidium@9625: v->owner == owner && v->group_id == index) {
rubidium@10715: *list->Append() = v;
glx@9624: }
glx@9624: }
glx@9624: break;
glx@9624:
bjarni@4497: default: NOT_REACHED(); break;
bjarni@4497: }
bjarni@4497:
rubidium@10715: list->Compact();
bjarni@4497: }
bjarni@4497:
rubidium@10142: /**
rubidium@10142: * Send all vehicles of type to depots
rubidium@4549: * @param type type of vehicle
rubidium@4549: * @param flags the flags used for DoCommand()
rubidium@4549: * @param service should the vehicles only get service in the depots
rubidium@4549: * @param owner PlayerID of owner of the vehicles to send
rubidium@9601: * @param vlw_flag tells what kind of list requested the goto depot
rubidium@4549: * @return 0 for success and CMD_ERROR if no vehicle is able to go to depot
rubidium@4549: */
glx@9629: CommandCost SendAllVehiclesToDepot(VehicleType type, uint32 flags, bool service, PlayerID owner, uint16 vlw_flag, uint32 id)
bjarni@4463: {
rubidium@10715: VehicleList list;
rubidium@10715:
rubidium@10715: GenerateVehicleSortList(&list, type, owner, id, vlw_flag);
bjarni@4465:
bjarni@4463: /* Send all the vehicles to a depot */
rubidium@10715: for (uint i = 0; i < list.Length(); i++) {
rubidium@10715: const Vehicle *v = list[i];
glx@9629: CommandCost ret = DoCommand(v->tile, v->index, (service ? 1 : 0) | DEPOT_DONT_CANCEL, flags, GetCmdSendToDepot(type));
Darkvater@4560:
Darkvater@4560: /* Return 0 if DC_EXEC is not set this is a valid goto depot command)
Darkvater@4560: * In this case we know that at least one vehicle can be sent to a depot
Darkvater@4560: * and we will issue the command. We can now safely quit the loop, knowing
Darkvater@4560: * it will succeed at least once. With DC_EXEC we really need to send them to the depot */
glx@9629: if (CmdSucceeded(ret) && !(flags & DC_EXEC)) {
glx@9629: return CommandCost();
bjarni@4463: }
bjarni@4463: }
bjarni@4465:
glx@9629: return (flags & DC_EXEC) ? CommandCost() : CMD_ERROR;
bjarni@4463: }
bjarni@4463:
glx@9629: /**
glx@9629: * Calculates how full a vehicle is.
glx@9629: * @param v The Vehicle to check. For trains, use the first engine.
glx@9629: * @param color The string to show depending on if we are unloading or loading
glx@9629: * @return A percentage of how full the Vehicle is.
glx@9629: */
glx@10645: uint8 CalcPercentVehicleFilled(const Vehicle *v, StringID *color)
glx@9629: {
glx@9629: int count = 0;
glx@9629: int max = 0;
glx@9629: int cars = 0;
glx@9629: int unloading = 0;
rubidium@9631: bool loading = false;
glx@9629:
rubidium@9631: const Vehicle *u = v;
rubidium@10142: const Station *st = v->last_station_visited != INVALID_STATION ? GetStation(v->last_station_visited) : NULL;
rubidium@9631:
glx@9629: /* Count up max and used */
rubidium@9701: for (; v != NULL; v = v->Next()) {
glx@9629: count += v->cargo.Count();
glx@9629: max += v->cargo_cap;
rubidium@10142: if (v->cargo_cap != 0 && color != NULL) {
rubidium@9722: unloading += HasBit(v->vehicle_flags, VF_CARGO_UNLOADING) ? 1 : 0;
rubidium@10142: loading |= !(u->current_order.GetUnloadType() & OUFB_UNLOAD) && st->goods[v->cargo_type].days_since_pickup != 255;
glx@9629: cars++;
glx@9629: }
glx@9629: }
glx@9629:
rubidium@10142: if (color != NULL) {
rubidium@10142: if (unloading == 0 && loading) {
rubidium@10142: *color = STR_PERCENT_UP;
rubidium@10142: } else if (cars == unloading || !loading) {
rubidium@10142: *color = STR_PERCENT_DOWN;
rubidium@10142: } else {
rubidium@10142: *color = STR_PERCENT_UP_DOWN;
rubidium@10142: }
rubidium@10142: }
glx@9629:
glx@9629: /* Train without capacity */
glx@9629: if (max == 0) return 100;
glx@9629:
glx@9629: /* Return the percentage */
glx@9629: return (count * 100) / max;
glx@9629: }
glx@9629:
bjarni@4725: void VehicleEnterDepot(Vehicle *v)
bjarni@4725: {
bjarni@4725: switch (v->type) {
rubidium@6585: case VEH_TRAIN:
bjarni@4725: InvalidateWindowClasses(WC_TRAINS_LIST);
rubidium@9701: if (!IsFrontEngine(v)) v = v->First();
rubidium@9724: UpdateSignalsOnSegment(v->tile, INVALID_DIAGDIR, v->owner);
bjarni@4725: v->load_unload_time_rem = 0;
rubidium@10181: ClrBit(v->u.rail.flags, VRF_TOGGLE_REVERSE);
rubidium@9826: TrainConsistChanged(v);
bjarni@4725: break;
bjarni@4725:
rubidium@6585: case VEH_ROAD:
bjarni@4725: InvalidateWindowClasses(WC_ROADVEH_LIST);
rubidium@9701: if (!IsRoadVehFront(v)) v = v->First();
bjarni@4725: break;
bjarni@4725:
rubidium@6585: case VEH_SHIP:
bjarni@4725: InvalidateWindowClasses(WC_SHIPS_LIST);
rubidium@6319: v->u.ship.state = TRACK_BIT_DEPOT;
bjarni@4725: RecalcShipStuff(v);
bjarni@4725: break;
bjarni@4725:
rubidium@6585: case VEH_AIRCRAFT:
bjarni@4725: InvalidateWindowClasses(WC_AIRCRAFT_LIST);
bjarni@4725: HandleAircraftEnterHangar(v);
bjarni@4725: break;
bjarni@4725: default: NOT_REACHED();
bjarni@4725: }
bjarni@4725:
rubidium@6585: if (v->type != VEH_TRAIN) {
bjarni@4739: /* Trains update the vehicle list when the first unit enters the depot and calls VehicleEnterDepot() when the last unit enters.
bjarni@4739: * We only increase the number of vehicles when the first one enters, so we will not need to search for more vehicles in the depot */
bjarni@4739: InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
bjarni@4739: }
bjarni@4725: InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
bjarni@4725:
bjarni@4725: v->vehstatus |= VS_HIDDEN;
bjarni@4725: v->cur_speed = 0;
bjarni@4725:
bjarni@4725: VehicleServiceInDepot(v);
bjarni@4725:
bjarni@4725: TriggerVehicle(v, VEHICLE_TRIGGER_DEPOT);
bjarni@4725:
rubidium@9869: if (v->current_order.IsType(OT_GOTO_DEPOT)) {
bjarni@4725: InvalidateWindow(WC_VEHICLE_VIEW, v->index);
bjarni@4725:
rubidium@10142: Order t = v->current_order;
rubidium@9869: v->current_order.MakeDummy();
rubidium@9869:
rubidium@9869: if (t.IsRefit()) {
bjarni@4725: _current_player = v->owner;
rubidium@10142: CommandCost cost = DoCommand(v->tile, v->index, t.GetRefitCargo() | t.GetRefitSubtype() << 8, DC_EXEC, GetCmdRefitVeh(v));
bjarni@4783:
bjarni@4783: if (CmdFailed(cost)) {
bjarni@4783: v->leave_depot_instantly = false; // We ensure that the vehicle stays in the depot
bjarni@4783: if (v->owner == _local_player) {
bjarni@4783: /* Notify the user that we stopped the vehicle */
bjarni@6206: SetDParam(0, _vehicle_type_names[v->type]);
bjarni@4783: SetDParam(1, v->unitnumber);
glx@10645: AddNewsItem(STR_ORDER_REFIT_FAILED, NS_ADVICE, v->index, 0);
bjarni@4783: }
glx@9629: } else if (v->owner == _local_player && cost.GetCost() != 0) {
glx@9629: ShowCostOrIncomeAnimation(v->x_pos, v->y_pos, v->z_pos, cost.GetCost());
bjarni@4783: }
bjarni@4725: }
bjarni@4725:
rubidium@10142: if (t.GetDepotOrderType() & ODTFB_PART_OF_ORDERS) {
bjarni@4725: /* Part of orders */
glx@9629: UpdateVehicleTimetable(v, true);
bjarni@4725: v->cur_order_index++;
rubidium@10142: } else if (t.GetDepotActionType() & ODATFB_HALT) {
bjarni@4725: /* Force depot visit */
bjarni@4725: v->vehstatus |= VS_STOPPED;
bjarni@4725: if (v->owner == _local_player) {
bjarni@4725: StringID string;
bjarni@4725:
bjarni@4725: switch (v->type) {
rubidium@6585: case VEH_TRAIN: string = STR_8814_TRAIN_IS_WAITING_IN_DEPOT; break;
rubidium@6585: case VEH_ROAD: string = STR_9016_ROAD_VEHICLE_IS_WAITING; break;
rubidium@6585: case VEH_SHIP: string = STR_981C_SHIP_IS_WAITING_IN_DEPOT; break;
rubidium@6585: case VEH_AIRCRAFT: string = STR_A014_AIRCRAFT_IS_WAITING_IN; break;
bjarni@4725: default: NOT_REACHED(); string = STR_EMPTY; // Set the string to something to avoid a compiler warning
bjarni@4725: }
bjarni@4725:
bjarni@4725: SetDParam(0, v->unitnumber);
glx@10645: AddNewsItem(string, NS_ADVICE, v->index, 0);
bjarni@4725: }
truebrain@10370: AI_Event(v->owner, new AIEventVehicleWaitingInDepot(v->index));
bjarni@4725: }
bjarni@4725: }
bjarni@4725: }
bjarni@2244:
rubidium@9631: static bool IsUniqueVehicleName(const char *name)
rubidium@9631: {
rubidium@9631: const Vehicle *v;
rubidium@9631: char buf[512];
rubidium@9631:
rubidium@9631: FOR_ALL_VEHICLES(v) {
rubidium@9631: switch (v->type) {
rubidium@9631: case VEH_TRAIN:
rubidium@9631: if (!IsTrainEngine(v)) continue;
rubidium@9631: break;
rubidium@9631:
rubidium@9631: case VEH_ROAD:
rubidium@9694: if (!IsRoadVehFront(v)) continue;
rubidium@9631: break;
rubidium@9631:
rubidium@9631: case VEH_AIRCRAFT:
rubidium@9631: if (!IsNormalAircraft(v)) continue;
rubidium@9631: break;
rubidium@9631:
rubidium@9631: case VEH_SHIP:
rubidium@9631: break;
rubidium@9631:
rubidium@9631: default:
rubidium@9631: continue;
rubidium@9631: }
rubidium@9631:
rubidium@9631: SetDParam(0, v->index);
rubidium@9631: GetString(buf, STR_VEHICLE_NAME, lastof(buf));
rubidium@9631: if (strcmp(buf, name) == 0) return false;
rubidium@9631: }
rubidium@9631:
rubidium@9631: return true;
rubidium@9631: }
rubidium@9631:
Darkvater@1786: /** Give a custom name to your vehicle
tron@3491: * @param tile unused
glx@9574: * @param flags type of operation
Darkvater@1786: * @param p1 vehicle ID to name
Darkvater@1786: * @param p2 unused
Darkvater@1786: */
glx@9629: CommandCost CmdNameVehicle(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
truelight@0: {
rubidium@9631: if (!IsValidVehicleID(p1) || StrEmpty(_cmd_text)) return CMD_ERROR;
bjarni@1237:
rubidium@10142: Vehicle *v = GetVehicle(p1);
truelight@0:
Darkvater@1786: if (!CheckOwnership(v->owner)) return CMD_ERROR;
truelight@0:
rubidium@9631: if (!IsUniqueVehicleName(_cmd_text)) return_cmd_error(STR_NAME_MUST_BE_UNIQUE);
rubidium@9631:
truelight@0: if (flags & DC_EXEC) {
rubidium@9724: free(v->name);
rubidium@9724: v->name = strdup(_cmd_text);
glx@10645: InvalidateWindowClassesData(WC_TRAINS_LIST, 1);
truelight@0: MarkWholeScreenDirty();
truelight@0: }
truelight@0:
glx@9629: return CommandCost();
truelight@0: }
truelight@0:
truelight@0:
tron@2819: /** Change the service interval of a vehicle
tron@3491: * @param tile unused
glx@9574: * @param flags type of operation
tron@2819: * @param p1 vehicle ID that is being service-interval-changed
tron@2819: * @param p2 new service interval
tron@2819: */
glx@9629: CommandCost CmdChangeServiceInt(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
tron@2819: {
tron@2819: uint16 serv_int = GetServiceIntervalClamped(p2); /* Double check the service interval from the user-input */
tron@2819:
truelight@4352: if (serv_int != p2 || !IsValidVehicleID(p1)) return CMD_ERROR;
tron@2819:
rubidium@10142: Vehicle *v = GetVehicle(p1);
tron@2819:
truelight@4352: if (!CheckOwnership(v->owner)) return CMD_ERROR;
tron@2819:
tron@2819: if (flags & DC_EXEC) {
tron@2819: v->service_interval = serv_int;
tron@2819: InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
tron@2819: }
tron@2819:
glx@9629: return CommandCost();
tron@2819: }
tron@2819:
truelight@0:
rubidium@9724: static Rect _old_vehicle_coords; ///< coords of vehicle before it has moved
rubidium@9724:
rubidium@9724: /**
rubidium@9724: * Stores the vehicle image coords for later call to EndVehicleMove()
rubidium@9724: * @param v vehicle which image's coords to store
rubidium@9724: * @see _old_vehicle_coords
rubidium@9724: * @see EndVehicleMove()
rubidium@9724: */
rubidium@9724: void BeginVehicleMove(const Vehicle *v)
rubidium@9694: {
rubidium@9724: _old_vehicle_coords.left = v->left_coord;
rubidium@9724: _old_vehicle_coords.top = v->top_coord;
rubidium@9724: _old_vehicle_coords.right = v->right_coord;
truelight@0: _old_vehicle_coords.bottom = v->bottom_coord;
truelight@0: }
truelight@0:
rubidium@9724: /**
rubidium@9724: * Marks screen dirty after a vehicle has moved
rubidium@9724: * @param v vehicle which is marked dirty
rubidium@9724: * @see _old_vehicle_coords
rubidium@9724: * @see BeginVehicleMove()
rubidium@9724: */
rubidium@9724: void EndVehicleMove(const Vehicle *v)
truelight@0: {
truelight@0: MarkAllViewportsDirty(
rubidium@9724: min(_old_vehicle_coords.left, v->left_coord),
rubidium@9724: min(_old_vehicle_coords.top, v->top_coord),
rubidium@9724: max(_old_vehicle_coords.right, v->right_coord) + 1,
rubidium@9724: max(_old_vehicle_coords.bottom, v->bottom_coord) + 1
truelight@0: );
truelight@0: }
truelight@0:
rubidium@9724: /**
rubidium@9724: * Marks viewports dirty where the vehicle's image is
rubidium@9724: * In fact, it equals
rubidium@9724: * BeginVehicleMove(v); EndVehicleMove(v);
rubidium@9724: * @param v vehicle to mark dirty
rubidium@9724: * @see BeginVehicleMove()
rubidium@9724: * @see EndVehicleMove()
rubidium@9724: */
rubidium@9724: void MarkSingleVehicleDirty(const Vehicle *v)
rubidium@9724: {
rubidium@9724: MarkAllViewportsDirty(v->left_coord, v->top_coord, v->right_coord + 1, v->bottom_coord + 1);
rubidium@9724: }
rubidium@9724:
truelight@0: /* returns true if staying in the same tile */
tron@6479: GetNewVehiclePosResult GetNewVehiclePos(const Vehicle *v)
truelight@0: {
truelight@0: static const int8 _delta_coord[16] = {
truelight@0: -1,-1,-1, 0, 1, 1, 1, 0, /* x */
truelight@0: -1, 0, 1, 1, 1, 0,-1,-1, /* y */
truelight@0: };
truelight@0:
truelight@0: int x = v->x_pos + _delta_coord[v->direction];
truelight@0: int y = v->y_pos + _delta_coord[v->direction + 8];
truelight@0:
tron@6479: GetNewVehiclePosResult gp;
tron@6479: gp.x = x;
tron@6479: gp.y = y;
tron@6479: gp.old_tile = v->tile;
tron@6479: gp.new_tile = TileVirtXY(x, y);
tron@6479: return gp;
truelight@0: }
truelight@0:
tron@3157: static const Direction _new_direction_table[] = {
tron@3157: DIR_N , DIR_NW, DIR_W ,
tron@3157: DIR_NE, DIR_SE, DIR_SW,
tron@3157: DIR_E , DIR_SE, DIR_S
truelight@0: };
truelight@0:
rubidium@10142: Direction GetDirectionTowards(const Vehicle *v, int x, int y)
truelight@0: {
truelight@0: int i = 0;
truelight@0:
truelight@0: if (y >= v->y_pos) {
rubidium@10142: if (y != v->y_pos) i += 3;
rubidium@10142: i += 3;
truelight@0: }
truelight@0:
truelight@0: if (x >= v->x_pos) {
truelight@0: if (x != v->x_pos) i++;
truelight@0: i++;
truelight@0: }
truelight@0:
rubidium@10142: Direction dir = v->direction;
rubidium@10142:
rubidium@10142: DirDiff dirdiff = DirDifference(_new_direction_table[i], dir);
tron@3158: if (dirdiff == DIRDIFF_SAME) return dir;
tron@3158: return ChangeDir(dir, dirdiff > DIRDIFF_REVERSE ? DIRDIFF_45LEFT : DIRDIFF_45RIGHT);
truelight@0: }
truelight@0:
rubidium@10142: Trackdir GetVehicleTrackdir(const Vehicle *v)
matthijs@1752: {
rubidium@5838: if (v->vehstatus & VS_CRASHED) return INVALID_TRACKDIR;
matthijs@1758:
tron@2952: switch (v->type) {
rubidium@6585: case VEH_TRAIN:
glx@9574: if (v->u.rail.track == TRACK_BIT_DEPOT) // We'll assume the train is facing outwards
glx@10645: return DiagDirToDiagTrackdir(GetRailDepotDirection(v->tile)); // Train in depot
glx@9574:
glx@9574: if (v->u.rail.track == TRACK_BIT_WORMHOLE) // train in tunnel, so just use his direction and assume a diagonal track
glx@10645: return DiagDirToDiagTrackdir(DirToDiagDir(v->direction));
Darkvater@1765:
rubidium@5838: return TrackDirectionToTrackdir(FindFirstTrack(v->u.rail.track), v->direction);
tron@1959:
rubidium@6585: case VEH_SHIP:
rubidium@9701: if (v->IsInDepot())
glx@9574: // We'll assume the ship is facing outwards
glx@10645: return DiagDirToDiagTrackdir(GetShipDepotDirection(v->tile));
Darkvater@1765:
rubidium@5838: return TrackDirectionToTrackdir(FindFirstTrack(v->u.ship.state), v->direction);
tron@1959:
rubidium@6585: case VEH_ROAD:
rubidium@9701: if (v->IsInDepot()) // We'll assume the road vehicle is facing outwards
glx@10645: return DiagDirToDiagTrackdir(GetRoadDepotDirection(v->tile));
Darkvater@1765:
glx@9574: if (IsStandardRoadStopTile(v->tile)) // We'll assume the road vehicle is facing outwards
glx@10645: return DiagDirToDiagTrackdir(GetRoadStopDir(v->tile)); // Road vehicle in a station
glx@10645:
glx@10645: if (IsDriveThroughStopTile(v->tile)) return DiagDirToDiagTrackdir(DirToDiagDir(v->direction));
rubidium@6338:
Darkvater@4270: /* If vehicle's state is a valid track direction (vehicle is not turning around) return it */
rubidium@6335: if (!IsReversingRoadTrackdir((Trackdir)v->u.road.state)) return (Trackdir)v->u.road.state;
Darkvater@4270:
Darkvater@4270: /* Vehicle is turning around, get the direction from vehicle's direction */
glx@10645: return DiagDirToDiagTrackdir(DirToDiagDir(v->direction));
tron@1959:
glx@10294: /* case VEH_AIRCRAFT: case VEH_EFFECT: case VEH_DISASTER: */
rubidium@5838: default: return INVALID_TRACKDIR;
matthijs@1752: }
matthijs@1752: }
rubidium@6317:
rubidium@6317: /**
rubidium@6317: * Returns some meta-data over the to be entered tile.
rubidium@6317: * @see VehicleEnterTileStatus to see what the bits in the return value mean.
rubidium@6317: */
tron@1977: uint32 VehicleEnterTile(Vehicle *v, TileIndex tile, int x, int y)
truelight@193: {
tron@3657: return _tile_type_procs[GetTileType(tile)]->vehicle_enter_tile_proc(v, tile, x, y);
truelight@0: }
truelight@0:
glx@9624: UnitID GetFreeUnitNumber(VehicleType type)
truelight@0: {
rubidium@10142: UnitID max = 0;
peter1138@2995: const Vehicle *u;
peter1138@2995: static bool *cache = NULL;
peter1138@2995: static UnitID gmax = 0;
truelight@0:
peter1138@2995: switch (type) {
rubidium@10715: case VEH_TRAIN: max = _settings.vehicle.max_trains; break;
rubidium@10715: case VEH_ROAD: max = _settings.vehicle.max_roadveh; break;
rubidium@10715: case VEH_SHIP: max = _settings.vehicle.max_ships; break;
rubidium@10715: case VEH_AIRCRAFT: max = _settings.vehicle.max_aircraft; break;
peter1138@2996: default: NOT_REACHED();
peter1138@2995: }
peter1138@2995:
bjarni@4096: if (max == 0) {
bjarni@4096: /* we can't build any of this kind of vehicle, so we just return 1 instead of looking for a free number
bjarni@4096: * a max of 0 will cause the following code to write to a NULL pointer
bjarni@4096: * We know that 1 is bigger than the max allowed vehicle number, so it's the same as returning something, that is too big
bjarni@4096: */
bjarni@4096: return 1;
bjarni@4096: }
bjarni@4096:
peter1138@2995: if (max > gmax) {
peter1138@2995: gmax = max;
peter1138@2995: free(cache);
KUDr@5860: cache = MallocT(max + 1);
peter1138@2995: }
peter1138@2995:
glx@9574: /* Clear the cache */
peter1138@2995: memset(cache, 0, (max + 1) * sizeof(*cache));
peter1138@2995:
glx@9574: /* Fill the cache */
truelight@0: FOR_ALL_VEHICLES(u) {
peter1138@2995: if (u->type == type && u->owner == _current_player && u->unitnumber != 0 && u->unitnumber <= max)
peter1138@2995: cache[u->unitnumber] = true;
truelight@0: }
peter1138@2995:
glx@9574: /* Find the first unused unit number */
rubidium@10142: UnitID unit = 1;
rubidium@10142: for (; unit <= max; unit++) {
peter1138@2995: if (!cache[unit]) break;
peter1138@2995: }
peter1138@2995:
peter1138@2995: return unit;
truelight@0: }
truelight@0:
rubidium@9601:
rubidium@9631: /**
rubidium@9631: * Check whether we can build infrastructure for the given
rubidium@9631: * vehicle type. This to disable building stations etc. when
rubidium@9631: * you are not allowed/able to have the vehicle type yet.
rubidium@9631: * @param type the vehicle type to check this for
rubidium@9631: * @return true if there is any reason why you may build
rubidium@9631: * the infrastructure for the given vehicle type
rubidium@9631: */
rubidium@9631: bool CanBuildVehicleInfrastructure(VehicleType type)
rubidium@9631: {
rubidium@9631: assert(IsPlayerBuildableVehicleType(type));
rubidium@9631:
rubidium@9701: if (!IsValidPlayer(_current_player)) return false;
rubidium@10715: if (_settings.gui.always_build_infrastructure) return true;
rubidium@9631:
rubidium@9631: UnitID max;
rubidium@9631: switch (type) {
rubidium@10715: case VEH_TRAIN: max = _settings.vehicle.max_trains; break;
rubidium@10715: case VEH_ROAD: max = _settings.vehicle.max_roadveh; break;
rubidium@10715: case VEH_SHIP: max = _settings.vehicle.max_ships; break;
rubidium@10715: case VEH_AIRCRAFT: max = _settings.vehicle.max_aircraft; break;
rubidium@9631: default: NOT_REACHED();
rubidium@9631: }
rubidium@9631:
rubidium@9631: /* We can build vehicle infrastructure when we may build the vehicle type */
rubidium@9631: if (max > 0) {
rubidium@9631: /* Can we actually build the vehicle type? */
rubidium@10455: const Engine *e;
rubidium@10455: FOR_ALL_ENGINES_OF_TYPE(e, type) {
rubidium@10455: if (HasBit(e->player_avail, _local_player)) return true;
rubidium@9631: }
rubidium@9631: return false;
rubidium@9631: }
rubidium@9631:
rubidium@9631: /* We should be able to build infrastructure when we have the actual vehicle type */
rubidium@9631: const Vehicle *v;
rubidium@9631: FOR_ALL_VEHICLES(v) {
rubidium@9631: if (v->owner == _local_player && v->type == type) return true;
rubidium@9631: }
rubidium@9631:
rubidium@9631: return false;
rubidium@9631: }
rubidium@9631:
rubidium@9631:
rubidium@9601: const Livery *GetEngineLivery(EngineID engine_type, PlayerID player, EngineID parent_engine_type, const Vehicle *v)
peter1138@3040: {
peter1138@4603: const Player *p = GetPlayer(player);
peter1138@4603: LiveryScheme scheme = LS_DEFAULT;
peter1138@5968: CargoID cargo_type = v == NULL ? (CargoID)CT_INVALID : v->cargo_type;
peter1138@5968:
peter1138@4603: /* The default livery is always available for use, but its in_use flag determines
peter1138@4603: * whether any _other_ liveries are in use. */
rubidium@10715: if (p->livery[LS_DEFAULT].in_use && (_settings.gui.liveries == 2 || (_settings.gui.liveries == 1 && player == _local_player))) {
peter1138@4603: /* Determine the livery scheme to use */
peter1138@4603: switch (GetEngine(engine_type)->type) {
glx@9624: default: NOT_REACHED();
rubidium@6585: case VEH_TRAIN: {
tron@6014: const RailVehicleInfo *rvi = RailVehInfo(engine_type);
tron@6014:
rubidium@9724: if (cargo_type == CT_INVALID) cargo_type = rvi->cargo_type;
rubidium@9724: if (rvi->railveh_type == RAILVEH_WAGON) {
rubidium@9724: if (!GetCargo(cargo_type)->is_freight) {
rubidium@9724: if (parent_engine_type == INVALID_ENGINE) {
rubidium@9724: scheme = LS_PASSENGER_WAGON_STEAM;
peter1138@4603: } else {
rubidium@9724: switch (RailVehInfo(parent_engine_type)->engclass) {
rubidium@9620: default: NOT_REACHED();
rubidium@9724: case EC_STEAM: scheme = LS_PASSENGER_WAGON_STEAM; break;
rubidium@9724: case EC_DIESEL: scheme = LS_PASSENGER_WAGON_DIESEL; break;
rubidium@9724: case EC_ELECTRIC: scheme = LS_PASSENGER_WAGON_ELECTRIC; break;
rubidium@9724: case EC_MONORAIL: scheme = LS_PASSENGER_WAGON_MONORAIL; break;
rubidium@9724: case EC_MAGLEV: scheme = LS_PASSENGER_WAGON_MAGLEV; break;
peter1138@4603: }
peter1138@4603: }
rubidium@9724: } else {
rubidium@9724: scheme = LS_FREIGHT_WAGON;
peter1138@4603: }
rubidium@9724: } else {
rubidium@9724: bool is_mu = HasBit(EngInfo(engine_type)->misc_flags, EF_RAIL_IS_MU);
rubidium@9724:
rubidium@9724: switch (rvi->engclass) {
rubidium@9724: default: NOT_REACHED();
rubidium@9724: case EC_STEAM: scheme = LS_STEAM; break;
rubidium@9724: case EC_DIESEL: scheme = is_mu ? LS_DMU : LS_DIESEL; break;
rubidium@9724: case EC_ELECTRIC: scheme = is_mu ? LS_EMU : LS_ELECTRIC; break;
rubidium@9724: case EC_MONORAIL: scheme = LS_MONORAIL; break;
rubidium@9724: case EC_MAGLEV: scheme = LS_MAGLEV; break;
rubidium@9724: }
peter1138@4603: }
peter1138@4603: break;
peter1138@4603: }
peter1138@4603:
rubidium@6585: case VEH_ROAD: {
peter1138@4603: const RoadVehicleInfo *rvi = RoadVehInfo(engine_type);
peter1138@4603: if (cargo_type == CT_INVALID) cargo_type = rvi->cargo_type;
rubidium@9722: if (HasBit(EngInfo(engine_type)->misc_flags, EF_ROAD_TRAM)) {
rubidium@9625: /* Tram */
rubidium@9625: scheme = IsCargoInClass(cargo_type, CC_PASSENGERS) ? LS_PASSENGER_TRAM : LS_FREIGHT_TRAM;
rubidium@9625: } else {
rubidium@9625: /* Bus or truck */
rubidium@9625: scheme = IsCargoInClass(cargo_type, CC_PASSENGERS) ? LS_BUS : LS_TRUCK;
rubidium@9625: }
peter1138@4603: break;
peter1138@4603: }
peter1138@4603:
rubidium@6585: case VEH_SHIP: {
peter1138@4603: const ShipVehicleInfo *svi = ShipVehInfo(engine_type);
peter1138@4603: if (cargo_type == CT_INVALID) cargo_type = svi->cargo_type;
truelight@9476: scheme = IsCargoInClass(cargo_type, CC_PASSENGERS) ? LS_PASSENGER_SHIP : LS_FREIGHT_SHIP;
peter1138@4603: break;
peter1138@4603: }
peter1138@4603:
rubidium@6585: case VEH_AIRCRAFT: {
peter1138@4603: const AircraftVehicleInfo *avi = AircraftVehInfo(engine_type);
peter1138@4603: if (cargo_type == CT_INVALID) cargo_type = CT_PASSENGERS;
peter1138@4603: switch (avi->subtype) {
Darkvater@6106: case AIR_HELI: scheme = LS_HELICOPTER; break;
Darkvater@6106: case AIR_CTOL: scheme = LS_SMALL_PLANE; break;
Darkvater@6106: case AIR_CTOL | AIR_FAST: scheme = LS_LARGE_PLANE; break;
peter1138@4603: }
peter1138@4603: break;
peter1138@4603: }
peter1138@4603: }
peter1138@4603:
peter1138@4603: /* Switch back to the default scheme if the resolved scheme is not in use */
peter1138@4603: if (!p->livery[scheme].in_use) scheme = LS_DEFAULT;
peter1138@4603: }
peter1138@4603:
rubidium@9601: return &p->livery[scheme];
rubidium@9601: }
rubidium@9601:
rubidium@9601:
rubidium@9601: static SpriteID GetEngineColourMap(EngineID engine_type, PlayerID player, EngineID parent_engine_type, const Vehicle *v)
rubidium@9601: {
rubidium@9722: SpriteID map = (v != NULL) ? v->colormap : PAL_NONE;
rubidium@9722:
rubidium@9722: /* Return cached value if any */
rubidium@9722: if (map != PAL_NONE) return map;
rubidium@9601:
rubidium@9601: /* Check if we should use the colour map callback */
rubidium@9722: if (HasBit(EngInfo(engine_type)->callbackmask, CBM_VEHICLE_COLOUR_REMAP)) {
rubidium@9601: uint16 callback = GetVehicleCallback(CBID_VEHICLE_COLOUR_MAPPING, 0, 0, engine_type, v);
rubidium@9601: /* A return value of 0xC000 is stated to "use the default two-color
rubidium@9601: * maps" which happens to be the failure action too... */
rubidium@9601: if (callback != CALLBACK_FAILED && callback != 0xC000) {
rubidium@9601: map = GB(callback, 0, 14);
rubidium@9601: /* If bit 14 is set, then the company colours are applied to the
rubidium@9601: * map else it's returned as-is. */
rubidium@9722: if (!HasBit(callback, 14)) {
rubidium@9722: /* Update cache */
rubidium@9722: if (v != NULL) ((Vehicle*)v)->colormap = map;
rubidium@9722: return map;
rubidium@9722: }
rubidium@9601: }
rubidium@9601: }
rubidium@9601:
rubidium@9722: bool twocc = HasBit(EngInfo(engine_type)->misc_flags, EF_USES_2CC);
peter1138@5968:
peter1138@5968: if (map == PAL_NONE) map = twocc ? (SpriteID)SPR_2CCMAP_BASE : (SpriteID)PALETTE_RECOLOR_START;
peter1138@5968:
rubidium@9601: const Livery *livery = GetEngineLivery(engine_type, player, parent_engine_type, v);
rubidium@9601:
rubidium@9601: map += livery->colour1;
rubidium@9601: if (twocc) map += livery->colour2 * 16;
peter1138@3113:
rubidium@9722: /* Update cache */
rubidium@9722: if (v != NULL) ((Vehicle*)v)->colormap = map;
peter1138@5919: return map;
peter1138@3040: }
peter1138@3040:
peter1138@5919: SpriteID GetEnginePalette(EngineID engine_type, PlayerID player)
peter1138@3105: {
peter1138@5968: return GetEngineColourMap(engine_type, player, INVALID_ENGINE, NULL);
peter1138@3105: }
peter1138@3105:
peter1138@5919: SpriteID GetVehiclePalette(const Vehicle *v)
peter1138@3105: {
rubidium@6585: if (v->type == VEH_TRAIN) {
peter1138@4603: return GetEngineColourMap(
glx@9626: (v->u.rail.first_engine != INVALID_ENGINE && (UsesWagonOverride(v) || (IsArticulatedPart(v) && RailVehInfo(v->engine_type)->railveh_type != RAILVEH_WAGON))) ?
peter1138@4603: v->u.rail.first_engine : v->engine_type,
peter1138@5968: v->owner, v->u.rail.first_engine, v);
peter1138@4603: }
peter1138@4603:
peter1138@5968: return GetEngineColourMap(v->engine_type, v->owner, INVALID_ENGINE, v);
peter1138@3105: }
peter1138@3105:
glx@9629: static uint8 _cargo_days;
glx@9629: static uint16 _cargo_source;
glx@9629: static uint32 _cargo_source_xy;
glx@9629: static uint16 _cargo_count;
glx@9629: static uint16 _cargo_paid_for;
glx@9629: static Money _cargo_feeder_share;
glx@9629: static uint32 _cargo_loaded_at_xy;
glx@9629:
rubidium@9701: /**
rubidium@9701: * Make it possible to make the saveload tables "friends" of other classes.
rubidium@9701: * @param vt the vehicle type. Can be VEH_END for the common vehicle description data
rubidium@9701: * @return the saveload description
rubidium@9701: */
rubidium@9701: const SaveLoad *GetVehicleDescription(VehicleType vt)
rubidium@9701: {
glx@9574: /** Save and load of vehicles */
rubidium@9701: static const SaveLoad _common_veh_desc[] = {
rubidium@4344: SLE_VAR(Vehicle, subtype, SLE_UINT8),
rubidium@4344:
rubidium@4344: SLE_REF(Vehicle, next, REF_VEHICLE_OLD),
rubidium@9724: SLE_CONDVAR(Vehicle, name, SLE_NAME, 0, 83),
rubidium@9724: SLE_CONDSTR(Vehicle, name, SLE_STR, 0, 84, SL_MAX_VERSION),
rubidium@4344: SLE_CONDVAR(Vehicle, unitnumber, SLE_FILE_U8 | SLE_VAR_U16, 0, 7),
rubidium@4344: SLE_CONDVAR(Vehicle, unitnumber, SLE_UINT16, 8, SL_MAX_VERSION),
rubidium@4344: SLE_VAR(Vehicle, owner, SLE_UINT8),
rubidium@4344: SLE_CONDVAR(Vehicle, tile, SLE_FILE_U16 | SLE_VAR_U32, 0, 5),
rubidium@4344: SLE_CONDVAR(Vehicle, tile, SLE_UINT32, 6, SL_MAX_VERSION),
rubidium@4344: SLE_CONDVAR(Vehicle, dest_tile, SLE_FILE_U16 | SLE_VAR_U32, 0, 5),
rubidium@4344: SLE_CONDVAR(Vehicle, dest_tile, SLE_UINT32, 6, SL_MAX_VERSION),
rubidium@4344:
rubidium@4344: SLE_CONDVAR(Vehicle, x_pos, SLE_FILE_U16 | SLE_VAR_U32, 0, 5),
rubidium@4344: SLE_CONDVAR(Vehicle, x_pos, SLE_UINT32, 6, SL_MAX_VERSION),
rubidium@4344: SLE_CONDVAR(Vehicle, y_pos, SLE_FILE_U16 | SLE_VAR_U32, 0, 5),
rubidium@4344: SLE_CONDVAR(Vehicle, y_pos, SLE_UINT32, 6, SL_MAX_VERSION),
rubidium@4344: SLE_VAR(Vehicle, z_pos, SLE_UINT8),
rubidium@4344: SLE_VAR(Vehicle, direction, SLE_UINT8),
rubidium@4344:
rubidium@9620: SLE_CONDNULL(2, 0, 57),
rubidium@4344: SLE_VAR(Vehicle, spritenum, SLE_UINT8),
rubidium@9620: SLE_CONDNULL(5, 0, 57),
rubidium@4344: SLE_VAR(Vehicle, engine_type, SLE_UINT16),
rubidium@4344:
rubidium@4344: SLE_VAR(Vehicle, max_speed, SLE_UINT16),
rubidium@4344: SLE_VAR(Vehicle, cur_speed, SLE_UINT16),
rubidium@4344: SLE_VAR(Vehicle, subspeed, SLE_UINT8),
rubidium@4344: SLE_VAR(Vehicle, acceleration, SLE_UINT8),
rubidium@4344: SLE_VAR(Vehicle, progress, SLE_UINT8),
rubidium@4344:
rubidium@4344: SLE_VAR(Vehicle, vehstatus, SLE_UINT8),
rubidium@4344: SLE_CONDVAR(Vehicle, last_station_visited, SLE_FILE_U8 | SLE_VAR_U16, 0, 4),
rubidium@4344: SLE_CONDVAR(Vehicle, last_station_visited, SLE_UINT16, 5, SL_MAX_VERSION),
rubidium@4344:
glx@9629: SLE_VAR(Vehicle, cargo_type, SLE_UINT8),
glx@9629: SLE_CONDVAR(Vehicle, cargo_subtype, SLE_UINT8, 35, SL_MAX_VERSION),
glx@9629: SLEG_CONDVAR( _cargo_days, SLE_UINT8, 0, 67),
glx@9629: SLEG_CONDVAR( _cargo_source, SLE_FILE_U8 | SLE_VAR_U16, 0, 6),
glx@9629: SLEG_CONDVAR( _cargo_source, SLE_UINT16, 7, 67),
glx@9629: SLEG_CONDVAR( _cargo_source_xy, SLE_UINT32, 44, 67),
glx@9629: SLE_VAR(Vehicle, cargo_cap, SLE_UINT16),
glx@9629: SLEG_CONDVAR( _cargo_count, SLE_UINT16, 0, 67),
glx@9629: SLE_CONDLST(Vehicle, cargo, REF_CARGO_PACKET, 68, SL_MAX_VERSION),
rubidium@4344:
rubidium@4344: SLE_VAR(Vehicle, day_counter, SLE_UINT8),
rubidium@4344: SLE_VAR(Vehicle, tick_counter, SLE_UINT8),
glx@9732: SLE_CONDVAR(Vehicle, running_ticks, SLE_UINT8, 88, SL_MAX_VERSION),
rubidium@4344:
rubidium@4344: SLE_VAR(Vehicle, cur_order_index, SLE_UINT8),
rubidium@4344: SLE_VAR(Vehicle, num_orders, SLE_UINT8),
truelight@956:
truelight@956: /* This next line is for version 4 and prior compatibility.. it temporarily reads
truelight@956: type and flags (which were both 4 bits) into type. Later on this is
truelight@956: converted correctly */
rubidium@9620: SLE_CONDVARX(cpp_offsetof(Vehicle, current_order) + cpp_offsetof(Order, type), SLE_UINT8, 0, 4),
rubidium@9620: SLE_CONDVARX(cpp_offsetof(Vehicle, current_order) + cpp_offsetof(Order, dest), SLE_FILE_U8 | SLE_VAR_U16, 0, 4),
truelight@956:
truelight@956: /* Orders for version 5 and on */
rubidium@9620: SLE_CONDVARX(cpp_offsetof(Vehicle, current_order) + cpp_offsetof(Order, type), SLE_UINT8, 5, SL_MAX_VERSION),
rubidium@9620: SLE_CONDVARX(cpp_offsetof(Vehicle, current_order) + cpp_offsetof(Order, flags), SLE_UINT8, 5, SL_MAX_VERSION),
rubidium@9620: SLE_CONDVARX(cpp_offsetof(Vehicle, current_order) + cpp_offsetof(Order, dest), SLE_UINT16, 5, SL_MAX_VERSION),
rubidium@4344:
bjarni@4712: /* Refit in current order */
rubidium@9620: SLE_CONDVARX(cpp_offsetof(Vehicle, current_order) + cpp_offsetof(Order, refit_cargo), SLE_UINT8, 36, SL_MAX_VERSION),
rubidium@9620: SLE_CONDVARX(cpp_offsetof(Vehicle, current_order) + cpp_offsetof(Order, refit_subtype), SLE_UINT8, 36, SL_MAX_VERSION),
bjarni@4712:
glx@9629: /* Timetable in current order */
glx@9629: SLE_CONDVARX(cpp_offsetof(Vehicle, current_order) + cpp_offsetof(Order, wait_time), SLE_UINT16, 67, SL_MAX_VERSION),
glx@9629: SLE_CONDVARX(cpp_offsetof(Vehicle, current_order) + cpp_offsetof(Order, travel_time), SLE_UINT16, 67, SL_MAX_VERSION),
glx@9629:
rubidium@4344: SLE_REF(Vehicle, orders, REF_ORDER),
rubidium@4344:
rubidium@4344: SLE_CONDVAR(Vehicle, age, SLE_FILE_U16 | SLE_VAR_I32, 0, 30),
rubidium@4344: SLE_CONDVAR(Vehicle, age, SLE_INT32, 31, SL_MAX_VERSION),
rubidium@4344: SLE_CONDVAR(Vehicle, max_age, SLE_FILE_U16 | SLE_VAR_I32, 0, 30),
rubidium@4344: SLE_CONDVAR(Vehicle, max_age, SLE_INT32, 31, SL_MAX_VERSION),
rubidium@4344: SLE_CONDVAR(Vehicle, date_of_last_service, SLE_FILE_U16 | SLE_VAR_I32, 0, 30),
rubidium@4344: SLE_CONDVAR(Vehicle, date_of_last_service, SLE_INT32, 31, SL_MAX_VERSION),
rubidium@4344: SLE_CONDVAR(Vehicle, service_interval, SLE_FILE_U16 | SLE_VAR_I32, 0, 30),
rubidium@4344: SLE_CONDVAR(Vehicle, service_interval, SLE_INT32, 31, SL_MAX_VERSION),
rubidium@4344: SLE_VAR(Vehicle, reliability, SLE_UINT16),
rubidium@4344: SLE_VAR(Vehicle, reliability_spd_dec, SLE_UINT16),
rubidium@4344: SLE_VAR(Vehicle, breakdown_ctr, SLE_UINT8),
rubidium@4344: SLE_VAR(Vehicle, breakdown_delay, SLE_UINT8),
rubidium@4344: SLE_VAR(Vehicle, breakdowns_since_last_service, SLE_UINT8),
rubidium@4344: SLE_VAR(Vehicle, breakdown_chance, SLE_UINT8),
rubidium@4344: SLE_CONDVAR(Vehicle, build_year, SLE_FILE_U8 | SLE_VAR_I32, 0, 30),
rubidium@4344: SLE_CONDVAR(Vehicle, build_year, SLE_INT32, 31, SL_MAX_VERSION),
rubidium@4344:
glx@9629: SLE_VAR(Vehicle, load_unload_time_rem, SLE_UINT16),
glx@9629: SLEG_CONDVAR( _cargo_paid_for, SLE_UINT16, 45, SL_MAX_VERSION),
glx@9629: SLE_CONDVAR(Vehicle, vehicle_flags, SLE_UINT8, 40, SL_MAX_VERSION),
glx@9629:
glx@9629: SLE_CONDVAR(Vehicle, profit_this_year, SLE_FILE_I32 | SLE_VAR_I64, 0, 64),
glx@9629: SLE_CONDVAR(Vehicle, profit_this_year, SLE_INT64, 65, SL_MAX_VERSION),
glx@9629: SLE_CONDVAR(Vehicle, profit_last_year, SLE_FILE_I32 | SLE_VAR_I64, 0, 64),
glx@9629: SLE_CONDVAR(Vehicle, profit_last_year, SLE_INT64, 65, SL_MAX_VERSION),
glx@9629: SLEG_CONDVAR( _cargo_feeder_share, SLE_FILE_I32 | SLE_VAR_I64,51, 64),
glx@9629: SLEG_CONDVAR( _cargo_feeder_share, SLE_INT64, 65, 67),
glx@9629: SLEG_CONDVAR( _cargo_loaded_at_xy, SLE_UINT32, 51, 67),
glx@9629: SLE_CONDVAR(Vehicle, value, SLE_FILE_I32 | SLE_VAR_I64, 0, 64),
glx@9629: SLE_CONDVAR(Vehicle, value, SLE_INT64, 65, SL_MAX_VERSION),
rubidium@4344:
glx@9732: SLE_CONDVAR(Vehicle, random_bits, SLE_UINT8, 2, SL_MAX_VERSION),
glx@9732: SLE_CONDVAR(Vehicle, waiting_triggers, SLE_UINT8, 2, SL_MAX_VERSION),
glx@9732:
glx@9732: SLE_CONDREF(Vehicle, next_shared, REF_VEHICLE, 2, SL_MAX_VERSION),
glx@9732: SLE_CONDREF(Vehicle, prev_shared, REF_VEHICLE, 2, SL_MAX_VERSION),
truelight@1024:
glx@9624: SLE_CONDVAR(Vehicle, group_id, SLE_UINT16, 60, SL_MAX_VERSION),
glx@9624:
glx@9629: SLE_CONDVAR(Vehicle, current_order_time, SLE_UINT32, 67, SL_MAX_VERSION),
glx@9629: SLE_CONDVAR(Vehicle, lateness_counter, SLE_INT32, 67, SL_MAX_VERSION),
glx@9629:
glx@9574: /* reserve extra space in savegame here. (currently 10 bytes) */
rubidium@4344: SLE_CONDNULL(10, 2, SL_MAX_VERSION),
truelight@193:
truelight@0: SLE_END()
truelight@0: };
truelight@0:
truelight@0:
Darkvater@1881: static const SaveLoad _train_desc[] = {
glx@9626: SLE_WRITEBYTE(Vehicle, type, VEH_TRAIN),
rubidium@9701: SLE_VEH_INCLUDEX(),
rubidium@9620: SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRail, crash_anim_pos), SLE_UINT16),
rubidium@9620: SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRail, force_proceed), SLE_UINT8),
rubidium@9620: SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRail, railtype), SLE_UINT8),
rubidium@9620: SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRail, track), SLE_UINT8),
rubidium@9620:
rubidium@9620: SLE_CONDVARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRail, flags), SLE_UINT8, 2, SL_MAX_VERSION),
rubidium@9620: SLE_CONDNULL(2, 2, 59),
truelight@0:
Darkvater@3222: SLE_CONDNULL(2, 2, 19),
glx@9574: /* reserve extra space in savegame here. (currently 11 bytes) */
Darkvater@3222: SLE_CONDNULL(11, 2, SL_MAX_VERSION),
truelight@0:
truelight@0: SLE_END()
truelight@0: };
truelight@0:
Darkvater@1881: static const SaveLoad _roadveh_desc[] = {
glx@9626: SLE_WRITEBYTE(Vehicle, type, VEH_ROAD),
rubidium@9701: SLE_VEH_INCLUDEX(),
rubidium@9620: SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRoad, state), SLE_UINT8),
rubidium@9620: SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRoad, frame), SLE_UINT8),
rubidium@9620: SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRoad, blocked_ctr), SLE_UINT16),
rubidium@9620: SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRoad, overtaking), SLE_UINT8),
rubidium@9620: SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRoad, overtaking_ctr), SLE_UINT8),
rubidium@9620: SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRoad, crashed_ctr), SLE_UINT16),
rubidium@9620: SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRoad, reverse_ctr), SLE_UINT8),
rubidium@9620:
rubidium@9620: SLE_CONDREFX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRoad, slot), REF_ROADSTOPS, 6, SL_MAX_VERSION),
rubidium@4344: SLE_CONDNULL(1, 6, SL_MAX_VERSION),
rubidium@9620: SLE_CONDVARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRoad, slot_age), SLE_UINT8, 6, SL_MAX_VERSION),
glx@9574: /* reserve extra space in savegame here. (currently 16 bytes) */
rubidium@4344: SLE_CONDNULL(16, 2, SL_MAX_VERSION),
truelight@0:
truelight@0: SLE_END()
truelight@0: };
truelight@0:
Darkvater@1881: static const SaveLoad _ship_desc[] = {
glx@9626: SLE_WRITEBYTE(Vehicle, type, VEH_SHIP),
rubidium@9701: SLE_VEH_INCLUDEX(),
rubidium@9620: SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleShip, state), SLE_UINT8),
truelight@0:
glx@9574: /* reserve extra space in savegame here. (currently 16 bytes) */
Darkvater@3222: SLE_CONDNULL(16, 2, SL_MAX_VERSION),
truelight@0:
truelight@0: SLE_END()
truelight@0: };
truelight@0:
Darkvater@1881: static const SaveLoad _aircraft_desc[] = {
glx@9626: SLE_WRITEBYTE(Vehicle, type, VEH_AIRCRAFT),
rubidium@9701: SLE_VEH_INCLUDEX(),
rubidium@9620: SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleAir, crashed_counter), SLE_UINT16),
rubidium@9620: SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleAir, pos), SLE_UINT8),
rubidium@9620:
rubidium@9620: SLE_CONDVARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleAir, targetairport), SLE_FILE_U8 | SLE_VAR_U16, 0, 4),
rubidium@9620: SLE_CONDVARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleAir, targetairport), SLE_UINT16, 5, SL_MAX_VERSION),
rubidium@9620:
rubidium@9620: SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleAir, state), SLE_UINT8),
rubidium@9620:
rubidium@9620: SLE_CONDVARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleAir, previous_pos), SLE_UINT8, 2, SL_MAX_VERSION),
truelight@0:
glx@9574: /* reserve extra space in savegame here. (currently 15 bytes) */
rubidium@4344: SLE_CONDNULL(15, 2, SL_MAX_VERSION),
truelight@0:
truelight@0: SLE_END()
truelight@0: };
truelight@0:
Darkvater@1881: static const SaveLoad _special_desc[] = {
glx@10294: SLE_WRITEBYTE(Vehicle, type, VEH_EFFECT),
truelight@0:
rubidium@4344: SLE_VAR(Vehicle, subtype, SLE_UINT8),
rubidium@4344:
rubidium@4344: SLE_CONDVAR(Vehicle, tile, SLE_FILE_U16 | SLE_VAR_U32, 0, 5),
rubidium@4344: SLE_CONDVAR(Vehicle, tile, SLE_UINT32, 6, SL_MAX_VERSION),
rubidium@4344:
rubidium@4344: SLE_CONDVAR(Vehicle, x_pos, SLE_FILE_I16 | SLE_VAR_I32, 0, 5),
rubidium@4344: SLE_CONDVAR(Vehicle, x_pos, SLE_INT32, 6, SL_MAX_VERSION),
rubidium@4344: SLE_CONDVAR(Vehicle, y_pos, SLE_FILE_I16 | SLE_VAR_I32, 0, 5),
rubidium@4344: SLE_CONDVAR(Vehicle, y_pos, SLE_INT32, 6, SL_MAX_VERSION),
rubidium@4344: SLE_VAR(Vehicle, z_pos, SLE_UINT8),
rubidium@4344:
rubidium@4344: SLE_VAR(Vehicle, cur_image, SLE_UINT16),
rubidium@9620: SLE_CONDNULL(5, 0, 57),
rubidium@4344: SLE_VAR(Vehicle, progress, SLE_UINT8),
rubidium@4344: SLE_VAR(Vehicle, vehstatus, SLE_UINT8),
rubidium@4344:
glx@10294: SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleEffect, animation_state), SLE_UINT16),
glx@10294: SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleEffect, animation_substate), SLE_UINT8),
truelight@0:
glx@9574: /* reserve extra space in savegame here. (currently 16 bytes) */
Darkvater@3222: SLE_CONDNULL(16, 2, SL_MAX_VERSION),
truelight@0:
truelight@0: SLE_END()
truelight@0: };
truelight@0:
Darkvater@1881: static const SaveLoad _disaster_desc[] = {
glx@9626: SLE_WRITEBYTE(Vehicle, type, VEH_DISASTER),
rubidium@4344:
rubidium@4344: SLE_REF(Vehicle, next, REF_VEHICLE_OLD),
rubidium@4344:
rubidium@4344: SLE_VAR(Vehicle, subtype, SLE_UINT8),
rubidium@4344: SLE_CONDVAR(Vehicle, tile, SLE_FILE_U16 | SLE_VAR_U32, 0, 5),
rubidium@4344: SLE_CONDVAR(Vehicle, tile, SLE_UINT32, 6, SL_MAX_VERSION),
rubidium@4344: SLE_CONDVAR(Vehicle, dest_tile, SLE_FILE_U16 | SLE_VAR_U32, 0, 5),
rubidium@4344: SLE_CONDVAR(Vehicle, dest_tile, SLE_UINT32, 6, SL_MAX_VERSION),
rubidium@4344:
rubidium@4344: SLE_CONDVAR(Vehicle, x_pos, SLE_FILE_I16 | SLE_VAR_I32, 0, 5),
rubidium@4344: SLE_CONDVAR(Vehicle, x_pos, SLE_INT32, 6, SL_MAX_VERSION),
rubidium@4344: SLE_CONDVAR(Vehicle, y_pos, SLE_FILE_I16 | SLE_VAR_I32, 0, 5),
rubidium@4344: SLE_CONDVAR(Vehicle, y_pos, SLE_INT32, 6, SL_MAX_VERSION),
rubidium@4344: SLE_VAR(Vehicle, z_pos, SLE_UINT8),
rubidium@4344: SLE_VAR(Vehicle, direction, SLE_UINT8),
rubidium@4344:
rubidium@9620: SLE_CONDNULL(5, 0, 57),
rubidium@4344: SLE_VAR(Vehicle, owner, SLE_UINT8),
rubidium@4344: SLE_VAR(Vehicle, vehstatus, SLE_UINT8),
rubidium@9620: SLE_CONDVARX(cpp_offsetof(Vehicle, current_order) + cpp_offsetof(Order, dest), SLE_FILE_U8 | SLE_VAR_U16, 0, 4),
rubidium@9620: SLE_CONDVARX(cpp_offsetof(Vehicle, current_order) + cpp_offsetof(Order, dest), SLE_UINT16, 5, SL_MAX_VERSION),
rubidium@4344:
rubidium@4344: SLE_VAR(Vehicle, cur_image, SLE_UINT16),
rubidium@4344: SLE_CONDVAR(Vehicle, age, SLE_FILE_U16 | SLE_VAR_I32, 0, 30),
rubidium@4344: SLE_CONDVAR(Vehicle, age, SLE_INT32, 31, SL_MAX_VERSION),
rubidium@4344: SLE_VAR(Vehicle, tick_counter, SLE_UINT8),
rubidium@4344:
rubidium@9694: SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleDisaster, image_override), SLE_UINT16),
rubidium@9694: SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleDisaster, big_ufo_destroyer_target), SLE_UINT16),
truelight@0:
glx@9574: /* reserve extra space in savegame here. (currently 16 bytes) */
rubidium@4344: SLE_CONDNULL(16, 2, SL_MAX_VERSION),
truelight@0:
truelight@0: SLE_END()
truelight@0: };
truelight@0:
truelight@0:
rubidium@9694: static const SaveLoad *_veh_descs[] = {
truelight@0: _train_desc,
truelight@0: _roadveh_desc,
truelight@0: _ship_desc,
truelight@0: _aircraft_desc,
truelight@0: _special_desc,
truelight@0: _disaster_desc,
rubidium@9701: _common_veh_desc,
truelight@0: };
truelight@0:
rubidium@9701: return _veh_descs[vt];
rubidium@9701: }
rubidium@9701:
glx@9574: /** Will be called when the vehicles need to be saved. */
rubidium@6573: static void Save_VEHS()
truelight@0: {
truelight@0: Vehicle *v;
glx@9574: /* Write the vehicles */
truelight@0: FOR_ALL_VEHICLES(v) {
truelight@4346: SlSetArrayIndex(v->index);
rubidium@9701: SlObject(v, GetVehicleDescription(v->type));
truelight@0: }
truelight@0: }
truelight@0:
glx@9574: /** Will be called when vehicles need to be loaded. */
rubidium@9869: void Load_VEHS()
truelight@0: {
truelight@0: int index;
truelight@0: Vehicle *v;
truelight@0:
glx@9629: _cargo_count = 0;
glx@9629:
truelight@0: while ((index = SlIterateArray()) != -1) {
truelight@1279: Vehicle *v;
glx@9626: VehicleType vtype = (VehicleType)SlReadByte();
glx@9629:
glx@9629: switch (vtype) {
rubidium@9694: case VEH_TRAIN: v = new (index) Train(); break;
rubidium@9694: case VEH_ROAD: v = new (index) RoadVehicle(); break;
rubidium@9694: case VEH_SHIP: v = new (index) Ship(); break;
rubidium@9694: case VEH_AIRCRAFT: v = new (index) Aircraft(); break;
glx@10294: case VEH_EFFECT: v = new (index) EffectVehicle(); break;
rubidium@9694: case VEH_DISASTER: v = new (index) DisasterVehicle(); break;
rubidium@9694: case VEH_INVALID: v = new (index) InvalidVehicle(); break;
glx@9624: default: NOT_REACHED();
rubidium@9620: }
rubidium@9620:
rubidium@9701: SlObject(v, GetVehicleDescription(vtype));
glx@9629:
glx@9629: if (_cargo_count != 0 && IsPlayerBuildableVehicleType(v)) {
glx@9629: /* Don't construct the packet with station here, because that'll fail with old savegames */
glx@9629: CargoPacket *cp = new CargoPacket();
glx@9629: cp->source = _cargo_source;
glx@9629: cp->source_xy = _cargo_source_xy;
glx@9629: cp->count = _cargo_count;
glx@9629: cp->days_in_transit = _cargo_days;
glx@9629: cp->feeder_share = _cargo_feeder_share;
glx@9629: cp->loaded_at_xy = _cargo_loaded_at_xy;
glx@9629: v->cargo.Append(cp);
glx@9629: }
glx@9629:
tron@2549: /* Old savegames used 'last_station_visited = 0xFF' */
truelight@2685: if (CheckSavegameVersion(5) && v->last_station_visited == 0xFF)
tron@2469: v->last_station_visited = INVALID_STATION;
truelight@956:
truelight@2685: if (CheckSavegameVersion(5)) {
truelight@956: /* Convert the current_order.type (which is a mix of type and flags, because
rubidium@4549: * in those versions, they both were 4 bits big) to type and flags */
rubidium@9869: v->current_order.flags = GB(v->current_order.type, 4, 4);
rubidium@10142: v->current_order.type &= 0x0F;
truelight@956: }
glx@9624:
glx@9624: /* Advanced vehicle lists got added */
glx@9624: if (CheckSavegameVersion(60)) v->group_id = DEFAULT_GROUP;
truelight@0: }
truelight@0:
truelight@1024: /* Check for shared order-lists (we now use pointers for that) */
truelight@2685: if (CheckSavegameVersionOldStyle(5, 2)) {
truelight@1024: FOR_ALL_VEHICLES(v) {
truelight@1024: Vehicle *u;
truelight@1024:
truelight@1024: FOR_ALL_VEHICLES_FROM(u, v->index + 1) {
truelight@1024: /* If a vehicle has the same orders, add the link to eachother
rubidium@4549: * in both vehicles */
truelight@1024: if (v->orders == u->orders) {
truelight@1024: v->next_shared = u;
truelight@1024: u->prev_shared = v;
truelight@1024: break;
truelight@1024: }
truelight@1024: }
truelight@1024: }
truelight@1024: }
truelight@0: }
truelight@0:
rubidium@5838: extern const ChunkHandler _veh_chunk_handlers[] = {
truelight@1542: { 'VEHS', Save_VEHS, Load_VEHS, CH_SPARSE_ARRAY | CH_LAST},
truelight@0: };
KUDr@5902:
KUDr@5902: void Vehicle::BeginLoading()
KUDr@5902: {
rubidium@6585: assert(IsTileType(tile, MP_STATION) || type == VEH_SHIP);
rubidium@9620:
rubidium@9869: if (this->current_order.IsType(OT_GOTO_STATION) &&
rubidium@9869: this->current_order.GetDestination() == this->last_station_visited) {
rubidium@10142: current_order.MakeLoading(true);
rubidium@10142: UpdateVehicleTimetable(this, true);
rubidium@10142:
rubidium@9620: /* Furthermore add the Non Stop flag to mark that this station
rubidium@9620: * is the actual destination of the vehicle, which is (for example)
rubidium@9620: * necessary to be known for HandleTrainLoading to determine
rubidium@9620: * whether the train is lost or not; not marking a train lost
rubidium@9620: * that arrives at random stations is bad. */
rubidium@10142: this->current_order.SetNonStopType(ONSF_NO_STOP_AT_ANY_STATION);
rubidium@10142:
rubidium@9620: } else {
rubidium@9869: current_order.MakeLoading(false);
rubidium@9620: }
rubidium@9620:
rubidium@9601: GetStation(this->last_station_visited)->loading_vehicles.push_back(this);
rubidium@9620:
rubidium@9620: VehiclePayment(this);
rubidium@9620:
rubidium@10355: InvalidateWindow(GetWindowClassForVehicleType(this->type), this->owner);
rubidium@9724: InvalidateWindowWidget(WC_VEHICLE_VIEW, this->index, VVW_WIDGET_START_STOP_VEH);
rubidium@9620: InvalidateWindow(WC_VEHICLE_DETAILS, this->index);
rubidium@9620: InvalidateWindow(WC_STATION_VIEW, this->last_station_visited);
rubidium@9620:
glx@9626: GetStation(this->last_station_visited)->MarkTilesDirty(true);
rubidium@9620: this->MarkDirty();
KUDr@5902: }
KUDr@5902:
KUDr@5902: void Vehicle::LeaveStation()
KUDr@5902: {
rubidium@9869: assert(current_order.IsType(OT_LOADING));
glx@9629:
glx@9629: /* Only update the timetable if the vehicle was supposed to stop here. */
rubidium@10142: if (current_order.GetNonStopType() != ONSF_STOP_EVERYWHERE) UpdateVehicleTimetable(this, false);
rubidium@9869:
rubidium@9869: current_order.MakeLeaveStation();
rubidium@10355: Station *st = GetStation(this->last_station_visited);
rubidium@10355: st->loading_vehicles.remove(this);
glx@9629:
glx@9629: HideFillingPercent(this->fill_percent_te_id);
glx@9629: this->fill_percent_te_id = INVALID_TE_ID;
rubidium@10355:
rubidium@10355: /* Trigger station animation for trains only */
rubidium@10355: if (this->type == VEH_TRAIN && IsTileType(this->tile, MP_STATION)) StationAnimationTrigger(st, this->tile, STAT_ANIM_TRAIN_DEPARTS);
KUDr@5902: }
rubidium@9620:
rubidium@9620:
rubidium@9620: void Vehicle::HandleLoading(bool mode)
rubidium@9620: {
rubidium@9869: switch (this->current_order.GetType()) {
rubidium@9620: case OT_LOADING: {
glx@9629: uint wait_time = max(this->current_order.wait_time - this->lateness_counter, 0);
glx@9629:
glx@9624: /* Not the first call for this tick, or still loading */
rubidium@9722: if (mode || !HasBit(this->vehicle_flags, VF_LOADING_FINISHED) ||
rubidium@10715: (_settings.order.timetabling && this->current_order_time < wait_time)) return;
rubidium@9620:
rubidium@9620: this->PlayLeaveStationSound();
rubidium@9620:
rubidium@10142: bool at_destination_station = this->current_order.GetNonStopType() != ONSF_STOP_EVERYWHERE;
rubidium@9620: this->LeaveStation();
rubidium@9620:
rubidium@9620: /* If this was not the final order, don't remove it from the list. */
rubidium@10142: if (!at_destination_station) return;
rubidium@9620: break;
rubidium@9620: }
rubidium@9620:
rubidium@9620: case OT_DUMMY: break;
rubidium@9620:
rubidium@9620: default: return;
rubidium@9620: }
rubidium@9620:
rubidium@9620: this->cur_order_index++;
rubidium@9620: InvalidateVehicleOrder(this);
rubidium@9620: }
rubidium@9620:
rubidium@10142: CommandCost Vehicle::SendToDepot(uint32 flags, DepotCommand command)
rubidium@10142: {
rubidium@10142: if (!CheckOwnership(this->owner)) return CMD_ERROR;
rubidium@10142: if (this->vehstatus & VS_CRASHED) return CMD_ERROR;
rubidium@10142: if (this->IsInDepot()) return CMD_ERROR;
rubidium@10142:
rubidium@10142: if (this->current_order.IsType(OT_GOTO_DEPOT)) {
rubidium@10142: bool halt_in_depot = this->current_order.GetDepotActionType() & ODATFB_HALT;
rubidium@10142: if (!!(command & DEPOT_SERVICE) == halt_in_depot) {
rubidium@10142: /* We called with a different DEPOT_SERVICE setting.
rubidium@10142: * Now we change the setting to apply the new one and let the vehicle head for the same depot.
rubidium@10142: * Note: the if is (true for requesting service == true for ordered to stop in depot) */
rubidium@10142: if (flags & DC_EXEC) {
rubidium@10142: this->current_order.SetDepotOrderType(ODTF_MANUAL);
rubidium@10142: this->current_order.SetDepotActionType(halt_in_depot ? ODATF_SERVICE_ONLY : ODATFB_HALT);
rubidium@10142: InvalidateWindowWidget(WC_VEHICLE_VIEW, this->index, VVW_WIDGET_START_STOP_VEH);
rubidium@10142: }
rubidium@10142: return CommandCost();
rubidium@10142: }
rubidium@10142:
rubidium@10142: if (command & DEPOT_DONT_CANCEL) return CMD_ERROR; // Requested no cancelation of depot orders
rubidium@10142: if (flags & DC_EXEC) {
rubidium@10142: /* If the orders to 'goto depot' are in the orders list (forced servicing),
rubidium@10142: * then skip to the next order; effectively cancelling this forced service */
rubidium@10142: if (this->current_order.GetDepotOrderType() & ODTFB_PART_OF_ORDERS) this->cur_order_index++;
rubidium@10142:
rubidium@10142: this->current_order.MakeDummy();
rubidium@10142: InvalidateWindowWidget(WC_VEHICLE_VIEW, this->index, VVW_WIDGET_START_STOP_VEH);
rubidium@10142: }
rubidium@10142: return CommandCost();
rubidium@10142: }
rubidium@10142:
rubidium@10142: /* check if at a standstill (not stopped only) in a depot
rubidium@10142: * the check is down here to make it possible to alter stop/service for trains entering the depot */
rubidium@10249: if (this->type == VEH_TRAIN && IsRailDepotTile(this->tile) && this->cur_speed == 0) return CMD_ERROR;
rubidium@10142:
rubidium@10142: TileIndex location;
rubidium@10142: DestinationID destination;
rubidium@10142: bool reverse;
rubidium@10142: static const StringID no_depot[] = {STR_883A_UNABLE_TO_FIND_ROUTE_TO, STR_9019_UNABLE_TO_FIND_LOCAL_DEPOT, STR_981A_UNABLE_TO_FIND_LOCAL_DEPOT, STR_A012_CAN_T_SEND_AIRCRAFT_TO};
rubidium@10142: if (!this->FindClosestDepot(&location, &destination, &reverse)) return_cmd_error(no_depot[this->type]);
rubidium@10142:
rubidium@10142: if (flags & DC_EXEC) {
rubidium@10142: if (this->current_order.IsType(OT_LOADING)) this->LeaveStation();
rubidium@10142:
rubidium@10142: this->dest_tile = location;
rubidium@10142: this->current_order.MakeGoToDepot(destination, ODTF_MANUAL);
rubidium@10142: if (!(command & DEPOT_SERVICE)) this->current_order.SetDepotActionType(ODATFB_HALT);
rubidium@10142: InvalidateWindowWidget(WC_VEHICLE_VIEW, this->index, VVW_WIDGET_START_STOP_VEH);
rubidium@10142:
rubidium@10142: /* If there is no depot in front, reverse automatically (trains only) */
rubidium@10142: if (this->type == VEH_TRAIN && reverse) DoCommand(this->tile, this->index, 0, DC_EXEC, CMD_REVERSE_TRAIN_DIRECTION);
rubidium@10142:
rubidium@10142: if (this->type == VEH_AIRCRAFT && this->u.air.state == FLYING && this->u.air.targetairport != destination) {
rubidium@10142: /* The aircraft is now heading for a different hangar than the next in the orders */
rubidium@10142: extern void AircraftNextAirportPos_and_Order(Vehicle *v);
rubidium@10142: AircraftNextAirportPos_and_Order(this);
rubidium@10142: }
rubidium@10142: }
rubidium@10142:
rubidium@10142: return CommandCost();
rubidium@10142:
rubidium@10142: }
rubidium@10142:
rubidium@9701: void Vehicle::SetNext(Vehicle *next)
rubidium@9701: {
rubidium@9701: if (this->next != NULL) {
rubidium@9701: /* We had an old next vehicle. Update the first and previous pointers */
rubidium@9701: for (Vehicle *v = this->next; v != NULL; v = v->Next()) {
rubidium@9701: v->first = this->next;
rubidium@9701: }
rubidium@9701: this->next->previous = NULL;
rubidium@9701: }
rubidium@9701:
rubidium@9701: this->next = next;
rubidium@9701:
rubidium@9701: if (this->next != NULL) {
rubidium@9701: /* A new next vehicle. Update the first and previous pointers */
rubidium@9701: if (this->next->previous != NULL) this->next->previous->next = NULL;
rubidium@9701: this->next->previous = this;
rubidium@9701: for (Vehicle *v = this->next; v != NULL; v = v->Next()) {
rubidium@9701: v->first = this->first;
rubidium@9701: }
rubidium@9701: }
rubidium@9701: }
rubidium@9620:
rubidium@10455: /** Backs up a chain of vehicles
rubidium@10455: * @param v The vehicle to back up
rubidium@10455: */
rubidium@10455: void BackuppedVehicle::BackupVehicle(Vehicle *v)
rubidium@10455: {
rubidium@10455: int length = CountVehiclesInChain(v);
rubidium@10455:
rubidium@10513: size_t cargo_packages_count = 1;
rubidium@10455: for (const Vehicle *v_count = v; v_count != NULL; v_count=v_count->Next()) {
rubidium@10455: /* Now we count how many cargo packets we need to store.
rubidium@10455: * We started with an offset by one because we also need an end of array marker. */
rubidium@10455: cargo_packages_count += v_count->cargo.packets.size();
rubidium@10455: }
rubidium@10455:
rubidium@10455: vehicles = MallocT(length);
rubidium@10455: cargo_packets = MallocT(cargo_packages_count);
rubidium@10455:
rubidium@10455: /* Now we make some pointers to iterate over the arrays. */
rubidium@10455: Vehicle *copy = vehicles;
rubidium@10455: CargoPacket *cargo = cargo_packets;
rubidium@10455:
rubidium@10455: Vehicle *original = v;
rubidium@10455:
rubidium@10455: for (; 0 < length; original = original->Next(), copy++, length--) {
rubidium@10455: /* First we need to copy the vehicle itself.
rubidium@10455: * However there is an issue as the cargo list isn't copied.
rubidium@10455: * To avoid restoring invalid pointers we start by swapping the cargo list with an empty one. */
rubidium@10455: CargoList::List empty_packets;
rubidium@10455: original->cargo.packets.swap(empty_packets);
rubidium@10455: memcpy(copy, original, sizeof(Vehicle));
rubidium@10455:
rubidium@10455: /* No need to do anything else if the cargo list is empty.
rubidium@10455: * It really doesn't matter if we swap an empty list with an empty list. */
rubidium@10455: if (original->cargo.Empty()) continue;
rubidium@10455:
rubidium@10455: /* And now we swap the cargo lists back. The vehicle now has it's cargo again. */
rubidium@10455: original->cargo.packets.swap(empty_packets);
rubidium@10455:
rubidium@10455: /* The vehicle contains some cargo so we will back up the cargo as well.
rubidium@10455: * We only need to store the packets and not which vehicle they came from.
rubidium@10455: * We will still be able to put them together with the right vehicle when restoring. */
rubidium@10455: const CargoList::List *packets = original->cargo.Packets();
rubidium@10455: for (CargoList::List::const_iterator it = packets->begin(); it != packets->end(); it++) {
rubidium@10455: memcpy(cargo, (*it), sizeof(CargoPacket));
rubidium@10455: cargo++;
rubidium@10455: }
rubidium@10455: }
rubidium@10455: /* We should end with a 0 packet so restoring can detect the end of the array. */
rubidium@10455: memset(cargo, 0, sizeof(CargoPacket));
rubidium@10455: }
rubidium@10455:
rubidium@10455: /** Restore a backed up row of vehicles
rubidium@10455: * @param *v The array of vehicles to restore
rubidium@10455: * @param *p The owner of the vehicle
rubidium@10455: */
rubidium@10455: Vehicle* BackuppedVehicle::RestoreBackupVehicle(Vehicle *v, Player *p)
rubidium@10455: {
rubidium@10455: Vehicle *backup = v;
rubidium@10455: CargoPacket *cargo = cargo_packets;
rubidium@10455:
rubidium@10455: assert(v->owner == p->index);
rubidium@10455:
rubidium@10455: /* Cache the result of the vehicle type check since it will not change
rubidium@10455: * and we need this check once for every run though the loop. */
rubidium@10455: bool is_road_veh = v->type == VEH_ROAD;
rubidium@10455:
rubidium@10455: while (true) {
rubidium@10455: Vehicle *dest = GetVehicle(backup->index);
rubidium@10455: /* The vehicle should be free since we are restoring something we just sold. */
rubidium@10455: assert(!dest->IsValid());
rubidium@10455: memcpy(dest, backup, sizeof(Vehicle));
rubidium@10455:
rubidium@10455: /* We decreased the engine count when we sold the engines so we will increase it again. */
glx@10645: if (IsEngineCountable(backup)) {
glx@10645: p->num_engines[backup->engine_type]++;
glx@10645: if (IsValidGroupID(backup->group_id)) GetGroup(backup->group_id)->num_engines[backup->engine_type]++;
glx@10645: if (backup->IsPrimaryVehicle()) IncreaseGroupNumVehicle(backup->group_id);
glx@10645: }
rubidium@10455:
rubidium@10455: /* Update hash. */
rubidium@10455: Vehicle *dummy = dest;
rubidium@10455: dest->old_new_hash = &dummy;
rubidium@10455: dest->left_coord = INVALID_COORD;
rubidium@10455: UpdateVehiclePosHash(dest, INVALID_COORD, 0);
rubidium@10455:
rubidium@10455: if (is_road_veh) {
rubidium@10455: /* Removed the slot in the road vehicles as the slot is gone.
rubidium@10455: * We don't want a pointer to a slot that's gone. */
rubidium@10455: dest->u.road.slot = NULL;
rubidium@10455: }
rubidium@10455:
rubidium@10455: if (!dest->cargo.Empty()) {
rubidium@10455: /* The vehicle in question contains some cargo.
rubidium@10455: * However we lost the list so we will have to recreate it.
rubidium@10455: * We know that the packets are stored in the same order as the vehicles so
rubidium@10455: * the one cargo_packets points to and maybe some following ones belongs to
rubidium@10455: * the current vehicle.
rubidium@10455: * Now all we have to do is to add the packets to a list and keep track of how
rubidium@10455: * much cargo we restore and once we reached the cached cargo hold we recovered
rubidium@10455: * everything for this vehicle. */
rubidium@10455: uint cargo_count = 0;
rubidium@10455: for(; cargo_count < dest->cargo.Count(); cargo++) {
rubidium@10455: dest->cargo.packets.push_back(GetCargoPacket(cargo->index));
rubidium@10455: cargo_count += cargo->count;
rubidium@10455: }
rubidium@10455: /* This design should always end up with the right amount of cargo. */
rubidium@10455: assert(cargo_count == dest->cargo.Count());
rubidium@10455: }
rubidium@10455:
rubidium@10455: if (backup->Next() == NULL) break;
rubidium@10455: backup++;
rubidium@10455: }
rubidium@10455: return GetVehicle(v->index);
rubidium@10455: }
rubidium@10455:
rubidium@10455: /** Restores a backed up vehicle
rubidium@10455: * @param *v A vehicle we should sell and take the windows from (NULL for not using this)
rubidium@10455: * @param *p The owner of the vehicle
rubidium@10455: * @return The vehicle we restored (front for trains) or v if we didn't have anything to restore
rubidium@10455: */
rubidium@10455: Vehicle *BackuppedVehicle::Restore(Vehicle *v, Player *p)
rubidium@10455: {
rubidium@10455: if (!ContainsBackup()) return v;
rubidium@10455: if (v != NULL) {
glx@10645: ChangeVehicleViewWindow(v->index, INVALID_VEHICLE);
rubidium@10455: DoCommand(0, v->index, 1, DC_EXEC, GetCmdSellVeh(v));
rubidium@10455: }
rubidium@10455: v = RestoreBackupVehicle(this->vehicles, p);
glx@10645: ChangeVehicleViewWindow(INVALID_VEHICLE, v->index);
rubidium@10455: if (orders != NULL) RestoreVehicleOrdersBruteForce(v, orders);
rubidium@10455: if (economy != NULL) economy->Restore();
rubidium@10455: /* If we stored cargo as well then we should restore it. */
rubidium@10455: cargo_packets->RestoreBackup();
rubidium@10455: return v;
rubidium@10455: }
rubidium@10455:
rubidium@10455: /** Backs up a vehicle
rubidium@10455: * This should never be called when the object already contains a backup
rubidium@10455: * @param v the vehicle to backup
rubidium@10455: * @param p If it's set to the vehicle's owner then economy is backed up. If NULL then economy backup will be skipped.
rubidium@10455: */
rubidium@10455: void BackuppedVehicle::Backup(Vehicle *v, Player *p)
rubidium@10455: {
rubidium@10455: assert(!ContainsBackup());
rubidium@10455: if (p != NULL) {
rubidium@10455: assert(p->index == v->owner);
rubidium@10455: economy = new PlayerMoneyBackup(p);
rubidium@10455: }
rubidium@10455: BackupVehicle(v);
rubidium@10455: if (orders != NULL) BackupVehicleOrders(v, orders);
rubidium@10455: }
rubidium@10455:
rubidium@9723: void StopAllVehicles()
rubidium@9723: {
rubidium@9723: Vehicle *v;
rubidium@9723: FOR_ALL_VEHICLES(v) {
rubidium@9723: /* Code ripped from CmdStartStopTrain. Can't call it, because of
rubidium@9723: * ownership problems, so we'll duplicate some code, for now */
rubidium@9723: v->vehstatus |= VS_STOPPED;
rubidium@9724: InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
rubidium@9723: InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
rubidium@9723: }
rubidium@9723: }