tron@2186: /* $Id$ */ tron@2186: belugas@6423: /** @file vehicle.cpp */ belugas@6423: 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" tron@1364: #include "table/sprites.h" tron@507: #include "table/strings.h" tron@2163: #include "functions.h" maedhros@6453: #include "landscape.h" tron@679: #include "map.h" tron@1209: #include "tile.h" truelight@0: #include "vehicle.h" maedhros@6980: #include "timetable.h" truelight@0: #include "gfx.h" truelight@0: #include "viewport.h" truelight@0: #include "news.h" truelight@0: #include "command.h" truelight@0: #include "saveload.h" truelight@0: #include "player.h" truelight@0: #include "engine.h" tron@337: #include "sound.h" celestar@1601: #include "debug.h" matthijs@1752: #include "vehicle_gui.h" matthijs@1758: #include "depot.h" matthijs@1758: #include "station.h" matthijs@1942: #include "rail.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@5469: #include "network/network.h" KUDr@4130: #include "yapf/yapf.h" rubidium@4261: #include "date.h" peter1138@5717: #include "newgrf_callbacks.h" peter1138@4603: #include "newgrf_engine.h" peter1138@4656: #include "newgrf_sound.h" rubidium@5587: #include "helpers.hpp" rubidium@6643: #include "group.h" rubidium@6565: #include "economy.h" peter1138@7097: #include "strings.h" truelight@0: rubidium@6904: #define INVALID_COORD (0x7fffffff) tron@4174: #define GEN_HASH(x, y) ((GB((y), 6, 6) << 6) + GB((x), 7, 6)) truelight@0: bjarni@5792: bjarni@5792: /* Tables used in vehicle.h to find the right command for a certain vehicle type */ bjarni@5792: 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@5792: 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@5792: 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@7400: DEFINE_OLD_POOL_GENERIC(Vehicle, Vehicle) truelight@1279: 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: tron@593: bool VehicleNeedsService(const Vehicle *v) tron@593: { rubidium@7502: if (v->vehstatus & (VS_STOPPED | VS_CRASHED)) return false; rubidium@7502: if (_patches.gotodepot && VehicleHasDepotOrders(v)) return false; rubidium@7502: if (v->current_order.type == OT_LOADING) return false; rubidium@7502: if (v->current_order.type == OT_GOTO_DEPOT && v->current_order.flags & OF_HALT_IN_DEPOT) return false; matthijs@1757: bjarni@4262: if (_patches.no_servicing_if_no_breakdowns && _opt.diff.vehicle_breakdowns == 0) { rubidium@6643: return EngineHasReplacementForPlayer(GetPlayer(v->owner), v->engine_type, v->group_id); /* Vehicles set for autoreplacing needs to go to a depot even if breakdowns are turned off */ bjarni@4262: } bjarni@4262: truelight@812: return _patches.servint_ispercent ? tron@1926: (v->reliability < GetEngine(v->engine_type)->reliability * (100 - v->service_interval) / 100) : tron@593: (v->date_of_last_service + v->service_interval < _date); tron@593: } tron@593: tron@3881: StringID VehicleInTheWayErrMsg(const Vehicle* v) truelight@0: { tron@2631: switch (v->type) { rubidium@6259: case VEH_TRAIN: return STR_8803_TRAIN_IN_THE_WAY; rubidium@6259: case VEH_ROAD: return STR_9000_ROAD_VEHICLE_IN_THE_WAY; rubidium@6259: 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 *EnsureNoVehicleProc(Vehicle *v, void *data) truelight@0: { rubidium@6259: if (v->tile != *(const TileIndex*)data || v->type == VEH_DISASTER) truelight@0: return NULL; truelight@193: tron@3881: _error_message = VehicleInTheWayErrMsg(v); tron@537: return v; truelight@193: } truelight@0: truelight@0: bool EnsureNoVehicle(TileIndex tile) truelight@0: { tron@537: return VehicleFromPos(tile, &tile, EnsureNoVehicleProc) == NULL; truelight@0: } truelight@0: truelight@0: static void *EnsureNoVehicleProcZ(Vehicle *v, void *data) truelight@0: { rubidium@5587: const TileInfo *ti = (const TileInfo*)data; tron@537: rubidium@6259: if (v->tile != ti->tile || v->type == VEH_DISASTER) return NULL; tron@3794: if (v->z_pos > ti->z) return NULL; truelight@0: tron@3881: _error_message = VehicleInTheWayErrMsg(v); tron@537: return v; truelight@0: } truelight@0: darkvater@1082: tron@3794: bool EnsureNoVehicleOnGround(TileIndex tile) truelight@0: { truelight@0: TileInfo ti; tron@537: Darkvater@2871: ti.tile = tile; tron@3794: ti.z = GetTileMaxZ(tile); tron@537: return VehicleFromPos(tile, &ti, EnsureNoVehicleProcZ) == NULL; truelight@0: } truelight@0: truelight@1605: Vehicle *FindVehicleOnTileZ(TileIndex tile, byte z) truelight@1605: { truelight@1605: TileInfo ti; truelight@1605: truelight@1605: ti.tile = tile; truelight@1605: ti.z = z; truelight@1605: rubidium@5587: return (Vehicle*)VehicleFromPos(tile, &ti, EnsureNoVehicleProcZ); truelight@1605: } truelight@1605: rubidium@5940: 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@6106: Swap(x1, x2); tron@6106: Swap(y1, y2); truelight@0: } truelight@919: FOR_ALL_VEHICLES(veh) { rubidium@5940: if (without_crashed && (veh->vehstatus & VS_CRASHED) != 0) continue; rubidium@6491: if ((veh->type == VEH_TRAIN || veh->type == VEH_ROAD) && (z == 0xFF || veh->z_pos == z)) { rubidium@6491: if ((veh->x_pos >> 4) >= x1 && (veh->x_pos >> 4) <= x2 && rubidium@6491: (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: tron@2817: 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); tron@2319: 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: belugas@6423: /** Called after load to update coordinates */ rubidium@6247: void AfterLoadVehicles() truelight@0: { truelight@0: Vehicle *v; truelight@0: truelight@0: FOR_ALL_VEHICLES(v) { rubidium@7497: /* Reinstate the previous pointer */ rubidium@7497: if (v->Next() != NULL) v->Next()->previous = v; rubidium@7497: rubidium@6558: v->UpdateDeltaXY(v->direction); rubidium@6558: truelight@6998: v->fill_percent_te_id = INVALID_TE_ID; celestar@1601: v->first = NULL; rubidium@6259: if (v->type == VEH_TRAIN) v->u.rail.first_engine = INVALID_ENGINE; maedhros@6857: if (v->type == VEH_ROAD) v->u.road.first_engine = INVALID_ENGINE; rubidium@7010: rubidium@7010: v->cargo.InvalidateCache(); celestar@3355: } celestar@3355: celestar@3355: FOR_ALL_VEHICLES(v) { rubidium@7497: /* Fill the first pointers */ rubidium@7497: if (v->Previous() == NULL) { rubidium@7497: for (Vehicle *u = v; u != NULL; u = u->Next()) { rubidium@7497: u->first = v; rubidium@7497: } rubidium@7497: } rubidium@7497: } rubidium@7497: rubidium@7497: FOR_ALL_VEHICLES(v) { rubidium@7497: assert(v->first != NULL); rubidium@7497: maedhros@6857: if (v->type == VEH_TRAIN && (IsFrontEngine(v) || IsFreeWagon(v))) { peter1138@2994: TrainConsistChanged(v); maedhros@6857: } else if (v->type == VEH_ROAD && IsRoadVehFront(v)) { maedhros@6857: RoadVehUpdateCache(v); maedhros@6857: } peter1138@2994: } peter1138@2994: peter1138@2994: FOR_ALL_VEHICLES(v) { truelight@4346: switch (v->type) { rubidium@6683: case VEH_ROAD: rubidium@6685: v->u.road.roadtype = HASBIT(EngInfo(v->engine_type)->misc_flags, EF_ROAD_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD; rubidium@6683: v->u.road.compatible_roadtypes = RoadTypeToRoadTypes(v->u.road.roadtype); rubidium@7134: /* FALL THROUGH */ rubidium@7134: case VEH_TRAIN: rubidium@7134: case VEH_SHIP: rubidium@7134: v->cur_image = v->GetImage(v->direction); rubidium@6683: break; rubidium@6683: rubidium@6259: case VEH_AIRCRAFT: Darkvater@5854: if (IsNormalAircraft(v)) { rubidium@7134: v->cur_image = v->GetImage(v->direction); Darkvater@5866: Darkvater@5866: /* The plane's shadow will have the same image as the plane */ rubidium@7492: Vehicle *shadow = v->Next(); Darkvater@5866: shadow->cur_image = v->cur_image; Darkvater@5866: Darkvater@5866: /* In the case of a helicopter we will update the rotor sprites */ Darkvater@5866: if (v->subtype == AIR_HELICOPTER) { rubidium@7492: Vehicle *rotor = shadow->Next(); Darkvater@5866: rotor->cur_image = GetRotorImage(v); Darkvater@5866: } peter1138@6490: peter1138@6490: 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@7398: Vehicle::Vehicle() truelight@0: { rubidium@7398: this->type = VEH_INVALID; rubidium@7398: this->left_coord = INVALID_COORD; rubidium@7398: this->group_id = DEFAULT_GROUP; rubidium@7398: this->fill_percent_te_id = INVALID_TE_ID; rubidium@7497: this->first = this; 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@6247: byte VehicleRandomBits() peter1138@2804: { peter1138@2804: return GB(Random(), 0, 8); peter1138@2804: } peter1138@2804: rubidium@7398: rubidium@7398: /* static */ bool Vehicle::AllocateList(Vehicle **vl, int num) truelight@0: { rubidium@7398: uint counter = _Vehicle_pool.first_free_index; rubidium@7398: rubidium@7398: for (int i = 0; i != num; i++) { rubidium@7398: Vehicle *v = AllocateRaw(counter); rubidium@7398: rubidium@7398: if (v == NULL) return false; rubidium@7398: v = new (v) InvalidVehicle(); rubidium@7398: bjarni@2606: if (vl != NULL) { bjarni@2606: vl[i] = v; bjarni@2606: } rubidium@7398: counter++; bjarni@2601: } bjarni@2601: bjarni@2606: return true; bjarni@2601: } bjarni@2601: peter1138@6871: /* Size of the hash, 6 = 64 x 64, 7 = 128 x 128. Larger sizes will (in theory) reduce hash peter1138@6871: * lookup times at the expense of memory usage. */ peter1138@6871: const int HASH_BITS = 7; peter1138@6871: const int HASH_SIZE = 1 << HASH_BITS; peter1138@6871: const int HASH_MASK = HASH_SIZE - 1; peter1138@6871: const int TOTAL_HASH_SIZE = 1 << (HASH_BITS * 2); peter1138@6871: const int TOTAL_HASH_MASK = TOTAL_HASH_SIZE - 1; peter1138@6871: peter1138@6871: /* Resolution of the hash, 0 = 1*1 tile, 1 = 2*2 tiles, 2 = 4*4 tiles, etc. peter1138@6871: * Profiling results show that 0 is fastest. */ peter1138@6871: const int HASH_RES = 0; peter1138@6871: peter1138@6871: static Vehicle *_new_vehicle_position_hash[TOTAL_HASH_SIZE]; peter1138@6871: peter1138@6871: static void *VehicleFromHash(int xl, int yl, int xu, int yu, void *data, VehicleFromPosProc *proc) peter1138@6871: { peter1138@6871: for (int y = yl; ; y = (y + (1 << HASH_BITS)) & (HASH_MASK << HASH_BITS)) { peter1138@6871: for (int x = xl; ; x = (x + 1) & HASH_MASK) { peter1138@6871: Vehicle *v = _new_vehicle_position_hash[(x + y) & TOTAL_HASH_MASK]; peter1138@6871: for (; v != NULL; v = v->next_new_hash) { peter1138@6871: void *a = proc(v, data); peter1138@6871: if (a != NULL) return a; peter1138@6871: } peter1138@6871: if (x == xu) break; peter1138@6871: } peter1138@6871: if (y == yu) break; peter1138@6871: } peter1138@6871: peter1138@6871: return NULL; peter1138@6871: } peter1138@6871: peter1138@6871: peter1138@6871: void *VehicleFromPosXY(int x, int y, void *data, VehicleFromPosProc *proc) peter1138@6871: { peter1138@6871: const int COLL_DIST = 6; peter1138@6871: peter1138@6871: /* Hash area to scan is from xl,yl to xu,yu */ peter1138@6871: int xl = GB((x - COLL_DIST) / TILE_SIZE, HASH_RES, HASH_BITS); peter1138@6871: int xu = GB((x + COLL_DIST) / TILE_SIZE, HASH_RES, HASH_BITS); peter1138@6871: int yl = GB((y - COLL_DIST) / TILE_SIZE, HASH_RES, HASH_BITS) << HASH_BITS; peter1138@6871: int yu = GB((y + COLL_DIST) / TILE_SIZE, HASH_RES, HASH_BITS) << HASH_BITS; peter1138@6871: peter1138@6871: return VehicleFromHash(xl, yl, xu, yu, data, proc); peter1138@6871: } peter1138@6871: tron@2651: truelight@0: void *VehicleFromPos(TileIndex tile, void *data, VehicleFromPosProc *proc) truelight@0: { peter1138@6871: int x = GB(TileX(tile), HASH_RES, HASH_BITS); peter1138@6871: int y = GB(TileY(tile), HASH_RES, HASH_BITS) << HASH_BITS; peter1138@6871: peter1138@6871: Vehicle *v = _new_vehicle_position_hash[(x + y) & TOTAL_HASH_MASK]; peter1138@6871: for (; v != NULL; v = v->next_new_hash) { peter1138@6871: if (v->tile != tile) continue; peter1138@6871: peter1138@6871: void *a = proc(v, data); peter1138@6871: if (a != NULL) return a; truelight@0: } peter1138@6871: truelight@0: return NULL; truelight@0: } truelight@0: peter1138@6886: static void UpdateNewVehiclePosHash(Vehicle *v, bool remove) peter1138@6871: { peter1138@6871: Vehicle **old_hash = v->old_new_hash; peter1138@6871: Vehicle **new_hash; peter1138@6871: peter1138@6886: if (remove) { peter1138@6871: new_hash = NULL; peter1138@6871: } else { peter1138@7371: int x = GB(TileX(v->tile), HASH_RES, HASH_BITS); peter1138@7371: int y = GB(TileY(v->tile), HASH_RES, HASH_BITS) << HASH_BITS; peter1138@6871: new_hash = &_new_vehicle_position_hash[(x + y) & TOTAL_HASH_MASK]; peter1138@6871: } peter1138@6871: peter1138@6871: if (old_hash == new_hash) return; peter1138@6871: peter1138@6871: /* Remove from the old position in the hash table */ peter1138@6871: if (old_hash != NULL) { peter1138@6871: Vehicle *last = NULL; peter1138@6871: Vehicle *u = *old_hash; peter1138@6871: while (u != v) { peter1138@6871: last = u; peter1138@6871: u = u->next_new_hash; peter1138@6871: assert(u != NULL); peter1138@6871: } peter1138@6871: peter1138@6871: if (last == NULL) { peter1138@6871: *old_hash = v->next_new_hash; peter1138@6871: } else { peter1138@6871: last->next_new_hash = v->next_new_hash; peter1138@6871: } peter1138@6871: } peter1138@6871: peter1138@6871: /* Insert vehicle at beginning of the new position in the hash table */ peter1138@6871: if (new_hash != NULL) { peter1138@6871: v->next_new_hash = *new_hash; peter1138@6871: *new_hash = v; peter1138@6871: assert(v != v->next_new_hash); peter1138@6871: } peter1138@6871: peter1138@6871: /* Remember current hash position */ peter1138@6871: v->old_new_hash = new_hash; peter1138@6871: } peter1138@6871: peter1138@6871: static Vehicle *_vehicle_position_hash[0x1000]; truelight@0: tron@2817: static void UpdateVehiclePosHash(Vehicle* v, int x, int y) truelight@0: { peter1138@6886: UpdateNewVehiclePosHash(v, x == INVALID_COORD); peter1138@6871: peter1138@5574: Vehicle **old_hash, **new_hash; truelight@0: int old_x = v->left_coord; truelight@0: int old_y = v->top_coord; truelight@0: rubidium@6491: 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@5574: Vehicle *u = *old_hash; peter1138@5574: while (u != v) { truelight@0: last = u; peter1138@5574: u = u->next_hash; peter1138@5574: 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@5574: *new_hash = v; truelight@0: } truelight@0: } truelight@0: rubidium@6247: void ResetVehiclePosHash() Darkvater@5352: { glx@6883: Vehicle *v; glx@6883: FOR_ALL_VEHICLES(v) { v->old_new_hash = NULL; } peter1138@5574: memset(_vehicle_position_hash, 0, sizeof(_vehicle_position_hash)); peter1138@6871: memset(_new_vehicle_position_hash, 0, sizeof(_new_vehicle_position_hash)); Darkvater@5352: } Darkvater@5352: rubidium@6247: void InitializeVehicles() truelight@0: { rubidium@7400: _Vehicle_pool.CleanPool(); rubidium@7400: _Vehicle_pool.AddBlockToPool(); Darkvater@5352: Darkvater@5352: ResetVehiclePosHash(); truelight@0: } truelight@0: truelight@0: Vehicle *GetLastVehicleInChain(Vehicle *v) truelight@0: { rubidium@7492: while (v->Next() != NULL) v = v->Next(); truelight@0: return v; truelight@0: } truelight@0: tron@2630: uint CountVehiclesInChain(const Vehicle* v) truelight@0: { tron@2639: uint count = 0; rubidium@7492: 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@6259: case VEH_AIRCRAFT: return IsNormalAircraft(v); // don't count plane shadows and helicopter rotors rubidium@6259: case VEH_TRAIN: bjarni@4574: return !IsArticulatedPart(v) && // tenders and other articulated parts bjarni@7526: !IsRearDualheaded(v); // rear parts of multiheaded engines maedhros@6857: case VEH_ROAD: return IsRoadVehFront(v); maedhros@6857: case VEH_SHIP: return true; bjarni@4574: default: return false; // Only count player buildable vehicles bjarni@4574: } bjarni@4574: } bjarni@4574: rubidium@7412: void Vehicle::PreDestructor() truelight@0: { rubidium@7413: if (CleaningPool()) return; rubidium@7413: rubidium@7398: if (IsValidStationID(this->last_station_visited)) { rubidium@7398: GetStation(this->last_station_visited)->loading_vehicles.remove(this); rubidium@7398: rubidium@7398: HideFillingPercent(this->fill_percent_te_id); rubidium@7398: this->fill_percent_te_id = INVALID_TE_ID; rubidium@6500: } rubidium@6500: rubidium@7398: if (IsEngineCountable(this)) { rubidium@7398: GetPlayer(this->owner)->num_engines[this->engine_type]--; rubidium@7398: if (this->owner == _local_player) InvalidateAutoreplaceWindow(this->engine_type, this->group_id); rubidium@7398: rubidium@7398: if (IsValidGroupID(this->group_id)) GetGroup(this->group_id)->num_engines[this->engine_type]--; rubidium@7398: if (this->IsPrimaryVehicle()) DecreaseGroupNumVehicle(this->group_id); bjarni@5944: } bjarni@4574: rubidium@7398: if (this->type == VEH_ROAD) ClearSlot(this); rubidium@7398: rubidium@7398: if (this->type != VEH_TRAIN || (this->type == VEH_TRAIN && (IsFrontEngine(this) || IsFreeWagon(this)))) { rubidium@7398: InvalidateWindowData(WC_VEHICLE_DEPOT, this->tile); bjarni@4739: } bjarni@4739: rubidium@7398: this->cargo.Truncate(0); rubidium@7412: 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@7398: if ((this->type == VEH_TRAIN && EngineHasArticPart(this)) || (this->type == VEH_ROAD && RoadVehHasArticPart(this))) { rubidium@7492: delete this->Next(); maedhros@6857: } rubidium@7412: } rubidium@7412: rubidium@7412: Vehicle::~Vehicle() rubidium@7412: { rubidium@7413: DeleteName(this->string_id); rubidium@7413: rubidium@7413: if (CleaningPool()) return; rubidium@7413: rubidium@7499: this->SetNext(NULL); rubidium@7412: UpdateVehiclePosHash(this, INVALID_COORD, 0); rubidium@7412: this->next_hash = NULL; rubidium@7412: this->next_new_hash = NULL; rubidium@7412: rubidium@7412: DeleteVehicleNews(this->index, INVALID_STRING_ID); rubidium@7398: rubidium@7398: new (this) InvalidVehicle(); rubidium@7398: } rubidium@7398: maedhros@6782: /** maedhros@6782: * Deletes all vehicles in a chain. maedhros@6782: * @param v The first vehicle in the chain. maedhros@6782: * maedhros@6782: * @warning This function is not valid for any vehicle containing articulated maedhros@6782: * parts. maedhros@6782: */ truelight@0: void DeleteVehicleChain(Vehicle *v) truelight@0: { maedhros@6857: assert(v->type != VEH_TRAIN && v->type != VEH_ROAD); maedhros@6782: truelight@0: do { truelight@0: Vehicle *u = v; rubidium@7492: v = v->Next(); rubidium@7398: delete u; Darkvater@1765: } while (v != NULL); truelight@0: } truelight@0: belugas@6423: /** 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: { belugas@6423: /* we need to set v->leave_depot_instantly as we have no control of it's contents at this time */ bjarni@2600: if (HASBIT(v->current_order.flags, OFB_HALT_IN_DEPOT) && !HASBIT(v->current_order.flags, OFB_PART_OF_ORDERS) && v->current_order.type == OT_GOTO_DEPOT) { belugas@6423: /* we keep the vehicle in the depot since the user ordered it to stay */ bjarni@2590: v->leave_depot_instantly = false; bjarni@2590: } else { belugas@6423: /* the vehicle do not plan on stopping in the depot, so we stop it to ensure that it will not reserve the path belugas@6423: * out of the depot before we might autoreplace it to a different engine. The new engine would not own the reserved path belugas@6423: * 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@6247: 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: rubidium@6616: Station *st; rubidium@6616: FOR_ALL_STATIONS(st) LoadUnloadStation(st); rubidium@6616: rubidium@6616: Vehicle *v; truelight@0: FOR_ALL_VEHICLES(v) { rubidium@7135: v->Tick(); peter1138@4656: peter1138@4656: switch (v->type) { rubidium@6621: default: break; rubidium@6621: rubidium@6259: case VEH_TRAIN: rubidium@6259: case VEH_ROAD: rubidium@6259: case VEH_AIRCRAFT: rubidium@6259: case VEH_SHIP: rubidium@6259: if (v->type == VEH_TRAIN && IsTrainWagon(v)) continue; rubidium@6259: if (v->type == VEH_AIRCRAFT && v->subtype != AIR_HELICOPTER) continue; maedhros@6857: 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: belugas@6423: /* now we handle all the vehicles that entered a depot this tick */ bjarni@2574: v = _first_veh_in_depot_list; bjarni@2574: while (v != NULL) { bjarni@2574: Vehicle *w = v->depot_list; rubidium@4434: v->depot_list = NULL; // it should always be NULL at the end of each tick bjarni@4676: MaybeReplaceVehicle(v, false, true); bjarni@2574: v = w; 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: { peter1138@6148: 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. belugas@6484: * @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) { peter1138@6350: for (CargoID cid = 0; cid < NUM_CARGO; cid++) { peter1138@6148: 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 belugas@6484: * @param engine_type Which engine to refit bjarni@4544: * @return Price for refitting bjarni@4544: */ rubidium@6943: CommandCost GetRefitCost(EngineID engine_type) bjarni@4544: { rubidium@6950: CommandCost base_cost; bjarni@4544: bjarni@4544: switch (GetEngine(engine_type)->type) { rubidium@6950: case VEH_SHIP: base_cost.AddCost(_price.ship_base); break; rubidium@6950: case VEH_ROAD: base_cost.AddCost(_price.roadveh_base); break; rubidium@6950: case VEH_AIRCRAFT: base_cost.AddCost(_price.aircraft_base); break; rubidium@6259: case VEH_TRAIN: rubidium@6950: base_cost.AddCost(2 * ((RailVehInfo(engine_type)->railveh_type == RAILVEH_WAGON) ? rubidium@6950: _price.build_railwagon : _price.build_railvehicle)); bjarni@4544: break; bjarni@4544: default: NOT_REACHED(); break; bjarni@4544: } rubidium@6950: return CommandCost((EngInfo(engine_type)->refit_cost * base_cost.GetCost()) >> 10); bjarni@4544: } peter1138@3973: Darkvater@2436: static void DoDrawVehicle(const Vehicle *v) truelight@0: { peter1138@5668: SpriteID image = v->cur_image; peter1138@5668: SpriteID pal; truelight@193: rubidium@7333: if (v->vehstatus & VS_DEFPAL) { peter1138@5668: pal = (v->vehstatus & VS_CRASHED) ? PALETTE_CRASH : GetVehiclePalette(v); peter1138@5668: } else { peter1138@5668: pal = PAL_NONE; truelight@193: } truelight@0: peter1138@5668: AddSortableSpriteToDraw(image, pal, v->x_pos + v->x_offs, v->y_pos + v->y_offs, glx@7351: v->sprite_width, v->sprite_height, v->z_height, v->z_pos, (v->vehstatus & VS_SHADOW) != 0); truelight@0: } truelight@0: truelight@0: void ViewportAddVehicles(DrawPixelInfo *dpi) truelight@0: { belugas@6423: /* 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: belugas@6423: /* The hash area to scan */ rubidium@6905: int xl, xu, yl, yu; rubidium@6905: rubidium@6905: if (dpi->width + 70 < (1 << (7 + 6))) { rubidium@6905: xl = GB(l - 70, 7, 6); rubidium@6905: xu = GB(r, 7, 6); rubidium@6905: } else { rubidium@6905: /* scan whole hash row */ rubidium@6905: xl = 0; rubidium@6905: xu = 0x3F; rubidium@6905: } rubidium@6905: rubidium@6905: if (dpi->height + 70 < (1 << (6 + 6))) { rubidium@6905: yl = GB(t - 70, 6, 6) << 6; rubidium@6905: yu = GB(b, 6, 6) << 6; rubidium@6905: } else { rubidium@6905: /* scan whole column */ rubidium@6905: yl = 0; rubidium@6905: yu = 0x3F << 6; rubidium@6905: } rubidium@6905: rubidium@6905: for (int y = yl;; y = (y + (1 << 6)) & (0x3F << 6)) { rubidium@6905: for (int x = xl;; x = (x + 1) & 0x3F) { rubidium@6905: const Vehicle *v = _vehicle_position_hash[x + y]; // already masked & 0xFFF peter1138@5574: peter1138@5574: 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@5574: 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@1371: static void ChimneySmokeInit(Vehicle *v) truelight@0: { truelight@0: uint32 r = Random(); tron@2140: v->cur_image = SPR_CHIMNEY_SMOKE_0 + GB(r, 0, 3); tron@2140: v->progress = GB(r, 16, 3); truelight@0: } truelight@0: tron@1371: static void ChimneySmokeTick(Vehicle *v) truelight@0: { tron@1371: if (v->progress > 0) { tron@1371: v->progress--; tron@1371: } else { tron@1371: TileIndex tile; truelight@0: truelight@0: BeginVehicleMove(v); truelight@193: tron@1980: tile = TileVirtXY(v->x_pos, v->y_pos); tron@1035: if (!IsTileType(tile, MP_INDUSTRY)) { truelight@0: EndVehicleMove(v); rubidium@7398: delete v; truelight@0: return; truelight@0: } truelight@0: tron@1371: if (v->cur_image != SPR_CHIMNEY_SMOKE_7) { tron@1371: v->cur_image++; tron@1371: } else { tron@1371: v->cur_image = SPR_CHIMNEY_SMOKE_0; tron@1371: } truelight@0: v->progress = 7; truelight@0: VehiclePositionChanged(v); truelight@0: EndVehicleMove(v); truelight@0: } truelight@0: } truelight@0: tron@1371: static void SteamSmokeInit(Vehicle *v) truelight@0: { tron@1371: v->cur_image = SPR_STEAM_SMOKE_0; truelight@0: v->progress = 12; truelight@0: } truelight@0: tron@1371: static void SteamSmokeTick(Vehicle *v) truelight@0: { tron@1371: bool moved = false; truelight@193: truelight@0: BeginVehicleMove(v); truelight@193: tron@1371: v->progress++; truelight@193: tron@1371: if ((v->progress & 7) == 0) { truelight@0: v->z_pos++; truelight@0: moved = true; truelight@0: } truelight@0: tron@1371: if ((v->progress & 0xF) == 4) { tron@1371: if (v->cur_image != SPR_STEAM_SMOKE_4) { tron@1371: v->cur_image++; tron@1371: } else { truelight@0: EndVehicleMove(v); rubidium@7398: delete v; truelight@0: return; truelight@0: } truelight@0: moved = true; truelight@0: } truelight@0: truelight@0: if (moved) { truelight@0: VehiclePositionChanged(v); truelight@0: EndVehicleMove(v); truelight@0: } truelight@0: } truelight@0: tron@1371: static void DieselSmokeInit(Vehicle *v) truelight@0: { tron@1371: v->cur_image = SPR_DIESEL_SMOKE_0; truelight@0: v->progress = 0; truelight@0: } truelight@0: tron@1371: static void DieselSmokeTick(Vehicle *v) truelight@0: { tron@1371: v->progress++; tron@1371: tron@1371: if ((v->progress & 3) == 0) { truelight@0: BeginVehicleMove(v); truelight@0: v->z_pos++; truelight@0: VehiclePositionChanged(v); truelight@0: EndVehicleMove(v); truelight@0: } else if ((v->progress & 7) == 1) { truelight@0: BeginVehicleMove(v); tron@1371: if (v->cur_image != SPR_DIESEL_SMOKE_5) { tron@1371: v->cur_image++; tron@1371: VehiclePositionChanged(v); tron@1371: EndVehicleMove(v); tron@1371: } else { truelight@0: EndVehicleMove(v); rubidium@7398: delete v; truelight@0: } truelight@0: } truelight@0: } truelight@0: tron@1371: static void ElectricSparkInit(Vehicle *v) truelight@0: { tron@1371: v->cur_image = SPR_ELECTRIC_SPARK_0; truelight@0: v->progress = 1; truelight@0: } truelight@0: tron@1371: static void ElectricSparkTick(Vehicle *v) truelight@0: { tron@1371: if (v->progress < 2) { tron@1371: v->progress++; tron@1371: } else { truelight@0: v->progress = 0; truelight@0: BeginVehicleMove(v); tron@1371: if (v->cur_image != SPR_ELECTRIC_SPARK_5) { tron@1371: v->cur_image++; tron@1371: VehiclePositionChanged(v); tron@1371: EndVehicleMove(v); tron@1371: } else { truelight@0: EndVehicleMove(v); rubidium@7398: delete v; truelight@0: } truelight@0: } truelight@0: } truelight@0: tron@1371: static void SmokeInit(Vehicle *v) truelight@0: { tron@1371: v->cur_image = SPR_SMOKE_0; truelight@0: v->progress = 12; truelight@0: } truelight@0: tron@1371: static void SmokeTick(Vehicle *v) truelight@0: { tron@1371: bool moved = false; truelight@193: truelight@0: BeginVehicleMove(v); truelight@193: tron@1371: v->progress++; truelight@193: tron@1371: if ((v->progress & 3) == 0) { truelight@0: v->z_pos++; truelight@0: moved = true; truelight@0: } truelight@0: tron@1371: if ((v->progress & 0xF) == 4) { tron@1371: if (v->cur_image != SPR_SMOKE_4) { tron@1371: v->cur_image++; tron@1371: } else { truelight@0: EndVehicleMove(v); rubidium@7398: delete v; truelight@0: return; truelight@0: } truelight@0: moved = true; truelight@0: } truelight@0: truelight@0: if (moved) { truelight@0: VehiclePositionChanged(v); truelight@0: EndVehicleMove(v); truelight@0: } truelight@0: } truelight@0: tron@1371: static void ExplosionLargeInit(Vehicle *v) truelight@0: { tron@1371: v->cur_image = SPR_EXPLOSION_LARGE_0; truelight@0: v->progress = 0; truelight@0: } truelight@0: tron@1371: static void ExplosionLargeTick(Vehicle *v) truelight@0: { tron@1371: v->progress++; tron@1371: if ((v->progress & 3) == 0) { truelight@0: BeginVehicleMove(v); tron@1371: if (v->cur_image != SPR_EXPLOSION_LARGE_F) { tron@1371: v->cur_image++; tron@1371: VehiclePositionChanged(v); tron@1371: EndVehicleMove(v); tron@1371: } else { truelight@0: EndVehicleMove(v); rubidium@7398: delete v; truelight@0: } truelight@0: } truelight@0: } truelight@0: tron@1371: static void BreakdownSmokeInit(Vehicle *v) truelight@0: { tron@1371: v->cur_image = SPR_BREAKDOWN_SMOKE_0; truelight@0: v->progress = 0; truelight@0: } truelight@0: tron@1371: static void BreakdownSmokeTick(Vehicle *v) truelight@0: { tron@1371: v->progress++; tron@1371: if ((v->progress & 7) == 0) { truelight@0: BeginVehicleMove(v); tron@1371: if (v->cur_image != SPR_BREAKDOWN_SMOKE_3) { tron@1371: v->cur_image++; tron@1371: } else { tron@1371: v->cur_image = SPR_BREAKDOWN_SMOKE_0; tron@1371: } truelight@0: VehiclePositionChanged(v); truelight@0: EndVehicleMove(v); truelight@0: } truelight@0: rubidium@7334: v->u.special.animation_state--; rubidium@7334: if (v->u.special.animation_state == 0) { truelight@0: BeginVehicleMove(v); truelight@0: EndVehicleMove(v); rubidium@7398: delete v; truelight@0: } truelight@0: } truelight@0: tron@1371: static void ExplosionSmallInit(Vehicle *v) truelight@0: { tron@1371: v->cur_image = SPR_EXPLOSION_SMALL_0; truelight@0: v->progress = 0; truelight@0: } truelight@0: tron@1371: static void ExplosionSmallTick(Vehicle *v) truelight@0: { tron@1371: v->progress++; tron@1371: if ((v->progress & 3) == 0) { truelight@0: BeginVehicleMove(v); tron@1371: if (v->cur_image != SPR_EXPLOSION_SMALL_B) { tron@1371: v->cur_image++; tron@1371: VehiclePositionChanged(v); tron@1371: EndVehicleMove(v); tron@1371: } else { truelight@0: EndVehicleMove(v); rubidium@7398: delete v; truelight@0: } truelight@0: } truelight@0: } truelight@0: tron@1364: static void BulldozerInit(Vehicle *v) truelight@0: { tron@1364: v->cur_image = SPR_BULLDOZER_NE; truelight@0: v->progress = 0; rubidium@7334: v->u.special.animation_state = 0; rubidium@7334: v->u.special.animation_substate = 0; truelight@0: } truelight@0: rubidium@6248: struct BulldozerMovement { tron@1365: byte direction:2; tron@1364: byte image:2; tron@1364: byte duration:3; rubidium@6248: }; truelight@0: tron@1364: static const BulldozerMovement _bulldozer_movement[] = { tron@1364: { 0, 0, 4 }, tron@1364: { 3, 3, 4 }, tron@1364: { 2, 2, 7 }, tron@1364: { 0, 2, 7 }, tron@1364: { 1, 1, 3 }, tron@1364: { 2, 2, 7 }, tron@1364: { 0, 2, 7 }, tron@1364: { 1, 1, 3 }, tron@1364: { 2, 2, 7 }, tron@1364: { 0, 2, 7 }, tron@1364: { 3, 3, 6 }, tron@1364: { 2, 2, 6 }, tron@1364: { 1, 1, 7 }, tron@1364: { 3, 1, 7 }, tron@1364: { 0, 0, 3 }, tron@1364: { 1, 1, 7 }, tron@1364: { 3, 1, 7 }, tron@1364: { 0, 0, 3 }, tron@1364: { 1, 1, 7 }, tron@1364: { 3, 1, 7 } truelight@0: }; truelight@0: tron@1364: static const struct { tron@1364: int8 x; tron@1364: int8 y; tron@1364: } _inc_by_dir[] = { tron@1364: { -1, 0 }, tron@1364: { 0, 1 }, tron@1364: { 1, 0 }, tron@1364: { 0, -1 } tron@1364: }; truelight@0: tron@1364: static void BulldozerTick(Vehicle *v) truelight@0: { tron@1371: v->progress++; tron@1371: if ((v->progress & 7) == 0) { rubidium@7334: const BulldozerMovement* b = &_bulldozer_movement[v->u.special.animation_state]; truelight@0: truelight@0: BeginVehicleMove(v); truelight@0: tron@1364: v->cur_image = SPR_BULLDOZER_NE + b->image; truelight@0: tron@1364: v->x_pos += _inc_by_dir[b->direction].x; tron@1364: v->y_pos += _inc_by_dir[b->direction].y; truelight@0: rubidium@7334: v->u.special.animation_substate++; rubidium@7334: if (v->u.special.animation_substate >= b->duration) { rubidium@7334: v->u.special.animation_substate = 0; rubidium@7334: v->u.special.animation_state++; rubidium@7334: if (v->u.special.animation_state == lengthof(_bulldozer_movement)) { truelight@0: EndVehicleMove(v); rubidium@7398: delete v; truelight@0: return; truelight@0: } truelight@0: } truelight@0: VehiclePositionChanged(v); truelight@0: EndVehicleMove(v); truelight@0: } truelight@0: } truelight@0: tron@1371: static void BubbleInit(Vehicle *v) truelight@0: { tron@1371: v->cur_image = SPR_BUBBLE_GENERATE_0; truelight@0: v->spritenum = 0; truelight@0: v->progress = 0; truelight@0: } truelight@0: rubidium@6248: struct BubbleMovement { tron@1371: int8 x:4; tron@1371: int8 y:4; tron@1371: int8 z:4; tron@1371: byte image:4; rubidium@6248: }; truelight@0: tron@1371: #define MK(x, y, z, i) { x, y, z, i } tron@1371: #define ME(i) { i, 4, 0, 0 } tron@1371: tron@1371: static const BubbleMovement _bubble_float_sw[] = { rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(1, 0, 1, 1), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(1, 0, 1, 2), tron@1371: ME(1) truelight@0: }; truelight@0: truelight@0: tron@1371: static const BubbleMovement _bubble_float_ne[] = { rubidium@4344: MK( 0, 0, 1, 0), rubidium@4344: MK(-1, 0, 1, 1), rubidium@4344: MK( 0, 0, 1, 0), rubidium@4344: MK(-1, 0, 1, 2), tron@1371: ME(1) truelight@0: }; truelight@0: tron@1371: static const BubbleMovement _bubble_float_se[] = { rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(0, 1, 1, 1), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(0, 1, 1, 2), tron@1371: ME(1) truelight@0: }; truelight@0: tron@1371: static const BubbleMovement _bubble_float_nw[] = { rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(0, -1, 1, 1), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(0, -1, 1, 2), tron@1371: ME(1) truelight@0: }; truelight@0: tron@1371: static const BubbleMovement _bubble_burst[] = { rubidium@4344: MK(0, 0, 1, 2), rubidium@4344: MK(0, 0, 1, 7), rubidium@4344: MK(0, 0, 1, 8), rubidium@4344: MK(0, 0, 1, 9), tron@1371: ME(0) truelight@0: }; truelight@0: tron@1371: static const BubbleMovement _bubble_absorb[] = { rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(0, 0, 1, 1), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(0, 0, 1, 2), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(0, 0, 1, 1), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(0, 0, 1, 2), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(0, 0, 1, 1), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(0, 0, 1, 2), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(0, 0, 1, 1), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(0, 0, 1, 2), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(0, 0, 1, 1), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(0, 0, 1, 2), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(0, 0, 1, 1), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(0, 0, 1, 2), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(0, 0, 1, 1), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(0, 0, 1, 2), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(0, 0, 1, 1), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(0, 0, 1, 2), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(0, 0, 1, 1), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(0, 0, 1, 2), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(0, 0, 1, 1), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(0, 0, 1, 2), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(0, 0, 1, 1), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(0, 0, 1, 2), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(0, 0, 1, 1), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(0, 0, 1, 2), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(0, 0, 1, 1), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(0, 0, 1, 2), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(0, 0, 1, 1), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(0, 0, 1, 2), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(0, 0, 1, 1), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(0, 0, 1, 2), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(0, 0, 1, 1), rubidium@4344: MK(2, 1, 3, 0), rubidium@4344: MK(1, 1, 3, 1), rubidium@4344: MK(2, 1, 3, 0), rubidium@4344: MK(1, 1, 3, 2), rubidium@4344: MK(2, 1, 3, 0), rubidium@4344: MK(1, 1, 3, 1), rubidium@4344: MK(2, 1, 3, 0), rubidium@4344: MK(1, 0, 1, 2), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(1, 0, 1, 1), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(1, 0, 1, 2), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(1, 0, 1, 1), rubidium@4344: MK(0, 0, 1, 0), rubidium@4344: MK(1, 0, 1, 2), tron@1371: ME(2), rubidium@4344: MK(0, 0, 0, 0xA), rubidium@4344: MK(0, 0, 0, 0xB), rubidium@4344: MK(0, 0, 0, 0xC), rubidium@4344: MK(0, 0, 0, 0xD), rubidium@4344: MK(0, 0, 0, 0xE), tron@1371: ME(0) truelight@0: }; tron@1371: #undef ME truelight@0: #undef MK truelight@0: tron@1371: static const BubbleMovement * const _bubble_movement[] = { tron@1371: _bubble_float_sw, tron@1371: _bubble_float_ne, tron@1371: _bubble_float_se, tron@1371: _bubble_float_nw, tron@1371: _bubble_burst, tron@1371: _bubble_absorb, truelight@0: }; truelight@0: tron@1371: static void BubbleTick(Vehicle *v) truelight@0: { truelight@543: /* truelight@543: * Warning: those effects can NOT use Random(), and have to use truelight@543: * InteractiveRandom(), because somehow someone forgot to save truelight@543: * spritenum to the savegame, and so it will cause desyncs in truelight@543: * multiplayer!! (that is: in ToyLand) truelight@543: */ tron@1371: uint et; tron@1371: const BubbleMovement *b; truelight@0: tron@1371: v->progress++; tron@1371: if ((v->progress & 3) != 0) truelight@0: return; truelight@0: truelight@0: BeginVehicleMove(v); truelight@0: truelight@0: if (v->spritenum == 0) { tron@1371: v->cur_image++; tron@1371: if (v->cur_image < SPR_BUBBLE_GENERATE_3) { truelight@0: VehiclePositionChanged(v); truelight@0: EndVehicleMove(v); truelight@0: return; truelight@0: } rubidium@7334: if (v->u.special.animation_substate != 0) { tron@2635: v->spritenum = GB(InteractiveRandom(), 0, 2) + 1; truelight@0: } else { truelight@0: v->spritenum = 6; truelight@0: } truelight@0: et = 0; tron@1371: } else { tron@1371: et = v->engine_type + 1; truelight@0: } truelight@0: tron@1371: b = &_bubble_movement[v->spritenum - 1][et]; truelight@0: tron@1371: if (b->y == 4 && b->x == 0) { truelight@0: EndVehicleMove(v); rubidium@7398: delete v; truelight@0: return; truelight@193: } truelight@193: tron@1371: if (b->y == 4 && b->x == 1) { tron@1371: if (v->z_pos > 180 || CHANCE16I(1, 96, InteractiveRandom())) { truelight@0: v->spritenum = 5; tron@541: SndPlayVehicleFx(SND_2F_POP, v); truelight@0: } truelight@0: et = 0; truelight@193: } truelight@193: tron@1371: if (b->y == 4 && b->x == 2) { tron@1371: TileIndex tile; truelight@0: truelight@0: et++; tron@541: SndPlayVehicleFx(SND_31_EXTRACT, v); truelight@0: tron@1980: tile = TileVirtXY(v->x_pos, v->y_pos); belugas@7466: if (IsTileType(tile, MP_INDUSTRY) && GetIndustryGfx(tile) == GFX_BUBBLE_CATCHER) AddAnimatedTile(tile); truelight@0: } truelight@0: tron@1371: v->engine_type = et; tron@1371: b = &_bubble_movement[v->spritenum - 1][et]; tron@1371: tron@1371: v->x_pos += b->x; tron@1371: v->y_pos += b->y; tron@1371: v->z_pos += b->z; tron@1371: v->cur_image = SPR_BUBBLE_0 + b->image; truelight@0: truelight@0: VehiclePositionChanged(v); truelight@0: EndVehicleMove(v); truelight@0: } truelight@0: truelight@0: truelight@0: typedef void EffectInitProc(Vehicle *v); truelight@0: typedef void EffectTickProc(Vehicle *v); truelight@0: truelight@0: static EffectInitProc * const _effect_init_procs[] = { tron@1371: ChimneySmokeInit, tron@1371: SteamSmokeInit, tron@1371: DieselSmokeInit, tron@1371: ElectricSparkInit, tron@1371: SmokeInit, tron@1371: ExplosionLargeInit, tron@1371: BreakdownSmokeInit, tron@1371: ExplosionSmallInit, tron@1364: BulldozerInit, tron@1371: BubbleInit, truelight@0: }; truelight@0: truelight@0: static EffectTickProc * const _effect_tick_procs[] = { tron@1371: ChimneySmokeTick, tron@1371: SteamSmokeTick, tron@1371: DieselSmokeTick, tron@1371: ElectricSparkTick, tron@1371: SmokeTick, tron@1371: ExplosionLargeTick, tron@1371: BreakdownSmokeTick, tron@1371: ExplosionSmallTick, tron@1364: BulldozerTick, tron@1371: BubbleTick, truelight@0: }; truelight@0: truelight@0: tron@1359: Vehicle *CreateEffectVehicle(int x, int y, int z, EffectVehicle type) truelight@0: { truelight@0: Vehicle *v; truelight@193: rubidium@7398: v = new SpecialVehicle(); truelight@0: if (v != NULL) { truelight@0: v->subtype = type; truelight@0: v->x_pos = x; truelight@0: v->y_pos = y; truelight@0: v->z_pos = z; truelight@0: v->tile = 0; rubidium@6558: v->UpdateDeltaXY(INVALID_DIR); truelight@0: v->vehstatus = VS_UNCLICKABLE; truelight@0: truelight@0: _effect_init_procs[type](v); truelight@0: truelight@0: VehiclePositionChanged(v); truelight@0: BeginVehicleMove(v); truelight@0: EndVehicleMove(v); truelight@0: } truelight@0: return v; truelight@0: } truelight@0: tron@1359: Vehicle *CreateEffectVehicleAbove(int x, int y, int z, EffectVehicle type) truelight@0: { rubidium@5295: int safe_x = clamp(x, 0, MapMaxX() * TILE_SIZE); rubidium@5295: int safe_y = clamp(y, 0, MapMaxY() * TILE_SIZE); rubidium@5295: return CreateEffectVehicle(x, y, GetSlopeZ(safe_x, safe_y) + z, type); truelight@0: } truelight@0: tron@1359: Vehicle *CreateEffectVehicleRel(const Vehicle *v, int x, int y, int z, EffectVehicle type) truelight@0: { truelight@0: return CreateEffectVehicle(v->x_pos + x, v->y_pos + y, v->z_pos + z, type); truelight@0: } truelight@0: rubidium@7135: void SpecialVehicle::Tick() truelight@0: { rubidium@7135: _effect_tick_procs[this->subtype](this); 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: truelight@0: if ( (uint)(x -= vp->left) >= (uint)vp->width || truelight@0: (uint)(y -= vp->top) >= (uint)vp->height) truelight@0: return NULL; truelight@0: truelight@6626: x = ScaleByZoom(x, vp->zoom) + vp->virtual_left; truelight@6626: 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( truelight@0: myabs( ((v->left_coord + v->right_coord)>>1) - x ), truelight@0: myabs( ((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: 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: uint32 r; truelight@0: int chance; truelight@0: truelight@0: /* decrease reliability */ truelight@0: v->reliability = rel = max((rel_old = v->reliability) - v->reliability_spd_dec, 0); truelight@0: if ((rel_old >> 8) != (rel >> 8)) truelight@0: InvalidateWindow(WC_VEHICLE_DETAILS, v->index); truelight@0: tron@2639: if (v->breakdown_ctr != 0 || v->vehstatus & VS_STOPPED || tron@2639: v->cur_speed < 5 || _game_mode == GM_MENU) { tron@2639: return; tron@2639: } truelight@0: truelight@0: r = Random(); truelight@0: truelight@0: /* increase chance of failure */ truelight@0: chance = v->breakdown_chance + 1; truelight@0: 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@6259: if (v->type == VEH_SHIP) rel += 0x6666; truelight@193: truelight@0: /* disabled breakdowns? */ tron@2639: if (_opt.diff.vehicle_breakdowns < 1) return; truelight@0: truelight@0: /* reduced breakdowns? */ truelight@0: if (_opt.diff.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: belugas@6423: /* Do not show getting-old message if autorenew is active */ tron@2639: if (GetPlayer(v->owner)->engine_renew) return; truelight@0: bjarni@5955: SetDParam(0, _vehicle_type_names[v->type]); tron@534: SetDParam(1, v->unitnumber); truelight@0: AddNewsItem(msg, NEWS_FLAGS(NM_SMALL, NF_VIEWPORT|NF_VEHICLE, NT_ADVICE, 0), v->index, 0); truelight@0: } truelight@0: truelight@0: void AgeVehicle(Vehicle *v) truelight@0: { truelight@0: int age; truelight@0: truelight@0: if (v->age < 65535) truelight@0: v->age++; truelight@0: truelight@0: age = v->age - v->max_age; truelight@0: if (age == 366*0 || age == 366*1 || age == 366*2 || age == 366*3 || age == 366*4) truelight@193: 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); truelight@0: } else if (age == 366*1 || age == 366*2 || age == 366*3 || age == 366*4 || age == 366*5) { 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) belugas@6423: * @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: */ rubidium@6943: CommandCost CmdMassStartStopVehicle(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) bjarni@4640: { bjarni@4640: Vehicle **vl = NULL; bjarni@4640: uint16 engine_list_length = 0; bjarni@4640: uint16 engine_count = 0; rubidium@6943: CommandCost return_value = CMD_ERROR; bjarni@4640: uint i; bjarni@4640: uint stop_command; rubidium@6641: VehicleType vehicle_type = (VehicleType)GB(p2, 0, 5); bjarni@4762: bool start_stop = HASBIT(p2, 5); bjarni@4762: bool vehicle_list_window = HASBIT(p2, 6); bjarni@4640: bjarni@4640: switch (vehicle_type) { rubidium@6259: case VEH_TRAIN: stop_command = CMD_START_STOP_TRAIN; break; rubidium@6259: case VEH_ROAD: stop_command = CMD_START_STOP_ROADVEH; break; rubidium@6259: case VEH_SHIP: stop_command = CMD_START_STOP_SHIP; break; rubidium@6259: 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@5748: uint32 id = p1; bjarni@4673: uint16 window_type = p2 & VLW_MASK; bjarni@4673: bjarni@5747: engine_count = GenerateVehicleSortList((const Vehicle***)&vl, &engine_list_length, vehicle_type, _current_player, id, window_type); bjarni@4673: } else { bjarni@4673: /* Get the list of vehicles in the depot */ bjarni@4673: BuildDepotVehicleList(vehicle_type, tile, &vl, &engine_list_length, &engine_count, NULL, NULL, NULL); bjarni@4673: } bjarni@4640: bjarni@4640: for (i = 0; i < engine_count; i++) { bjarni@4640: const Vehicle *v = vl[i]; rubidium@6943: CommandCost ret; bjarni@4640: bjarni@4640: if (!!(v->vehstatus & VS_STOPPED) != start_stop) continue; bjarni@4673: bjarni@4673: if (!vehicle_list_window) { rubidium@6259: 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: bjarni@4640: ret = DoCommand(tile, v->index, 0, flags, stop_command); bjarni@4640: rubidium@6946: if (CmdSucceeded(ret)) { rubidium@6950: 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@4648: free(vl); bjarni@4640: return return_value; bjarni@4640: } bjarni@4640: bjarni@4659: /** Sells all vehicles in a depot belugas@6423: * @param tile Tile of the depot where the depot is belugas@6423: * @param flags type of operation belugas@6423: * @param p1 Vehicle type belugas@6423: * @param p2 unused belugas@6423: */ rubidium@6943: CommandCost CmdDepotSellAllVehicles(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) bjarni@4659: { bjarni@4659: Vehicle **engines = NULL; bjarni@4659: Vehicle **wagons = NULL; bjarni@4659: uint16 engine_list_length = 0; bjarni@4659: uint16 engine_count = 0; bjarni@4659: uint16 wagon_list_length = 0; bjarni@4659: uint16 wagon_count = 0; bjarni@4659: rubidium@6950: CommandCost cost; bjarni@4659: uint i, sell_command, total_number_vehicles; rubidium@6641: VehicleType vehicle_type = (VehicleType)GB(p1, 0, 8); bjarni@4659: bjarni@4659: switch (vehicle_type) { rubidium@6259: case VEH_TRAIN: sell_command = CMD_SELL_RAIL_WAGON; break; rubidium@6259: case VEH_ROAD: sell_command = CMD_SELL_ROAD_VEH; break; rubidium@6259: case VEH_SHIP: sell_command = CMD_SELL_SHIP; break; rubidium@6259: 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 */ bjarni@4659: BuildDepotVehicleList(vehicle_type, tile, &engines, &engine_list_length, &engine_count, bjarni@4659: &wagons, &wagon_list_length, &wagon_count); bjarni@4659: bjarni@4659: total_number_vehicles = engine_count + wagon_count; bjarni@4659: for (i = 0; i < total_number_vehicles; i++) { bjarni@4659: const Vehicle *v; rubidium@6943: CommandCost ret; bjarni@4659: bjarni@4659: if (i < engine_count) { bjarni@4659: v = engines[i]; bjarni@4659: } else { bjarni@4659: v = wagons[i - engine_count]; bjarni@4659: } bjarni@4659: bjarni@4659: ret = DoCommand(tile, v->index, 1, flags, sell_command); bjarni@4659: rubidium@6950: if (CmdSucceeded(ret)) cost.AddCost(ret); bjarni@4659: } bjarni@4659: bjarni@4659: free(engines); bjarni@4659: free(wagons); rubidium@6950: 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 belugas@6423: * @param tile Tile of the depot where the vehicles are belugas@6423: * @param flags type of operation belugas@6423: * @param p1 Type of vehicle belugas@6423: * @param p2 Unused belugas@6423: */ rubidium@6943: CommandCost CmdDepotMassAutoReplace(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) bjarni@4662: { bjarni@4662: Vehicle **vl = NULL; bjarni@4662: uint16 engine_list_length = 0; bjarni@4662: uint16 engine_count = 0; bjarni@4662: uint i, x = 0, y = 0, z = 0; rubidium@6950: CommandCost cost; rubidium@6641: VehicleType vehicle_type = (VehicleType)GB(p1, 0, 8); bjarni@4662: rubidium@7512: if (!IsDepotTile(tile) || !IsTileOwner(tile, _current_player)) return CMD_ERROR; bjarni@4662: bjarni@4662: /* Get the list of vehicles in the depot */ bjarni@4662: BuildDepotVehicleList(vehicle_type, tile, &vl, &engine_list_length, &engine_count, NULL, NULL, NULL); bjarni@4662: bjarni@4662: bjarni@4662: for (i = 0; i < engine_count; i++) { bjarni@4662: Vehicle *v = vl[i]; bjarni@4662: bool stopped = !(v->vehstatus & VS_STOPPED); rubidium@6943: CommandCost ret; bjarni@4662: bjarni@4662: /* Ensure that the vehicle completely in the depot */ rubidium@7490: if (!v->IsInDepot()) continue; bjarni@4662: bjarni@4676: x = v->x_pos; bjarni@4676: y = v->y_pos; bjarni@4676: z = v->z_pos; bjarni@4676: bjarni@4676: if (stopped) { bjarni@4676: v->vehstatus |= VS_STOPPED; // Stop the vehicle bjarni@4676: v->leave_depot_instantly = true; bjarni@4676: } bjarni@4676: ret = MaybeReplaceVehicle(v, !(flags & DC_EXEC), false); bjarni@4662: rubidium@6946: if (CmdSucceeded(ret)) { rubidium@6950: cost.AddCost(ret); bjarni@4662: if (!(flags & DC_EXEC)) break; bjarni@4662: /* There is a problem with autoreplace and newgrf bjarni@4662: * It's impossible to tell the length of a train after it's being replaced before it's actually done bjarni@4662: * Because of this, we can't estimate costs due to wagon removal and we will have to always return 0 and pay manually bjarni@4662: * Since we pay after each vehicle is replaced and MaybeReplaceVehicle() check if the player got enough money bjarni@4662: * we should never reach a condition where the player will end up with negative money from doing this */ bjarni@4662: SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES); bjarni@4662: SubtractMoneyFromPlayer(ret); bjarni@4662: } bjarni@4662: } bjarni@4662: rubidium@6950: if (cost.GetCost() == 0) { bjarni@4662: cost = CMD_ERROR; bjarni@4662: } else { bjarni@4662: if (flags & DC_EXEC) { bjarni@4662: /* Display the cost animation now that DoCommandP() can't do it for us (see previous comments) */ rubidium@6950: if (IsLocalPlayer()) ShowCostOrIncomeAnimation(x, y, z, cost.GetCost()); bjarni@4662: } rubidium@6950: cost = CommandCost(); bjarni@4662: } bjarni@4662: bjarni@4662: free(vl); 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 belugas@6423: * @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: */ rubidium@6943: CommandCost CmdCloneVehicle(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) bjarni@2244: { bjarni@2563: Vehicle *v_front, *v; bjarni@2563: Vehicle *w_front, *w, *w_rear; rubidium@6950: CommandCost cost, total_cost; bjarni@3816: uint32 build_argument = 2; bjarni@2244: truelight@4352: if (!IsValidVehicleID(p1)) return CMD_ERROR; bjarni@2244: v = GetVehicle(p1); bjarni@2563: v_front = v; bjarni@2563: w = NULL; bjarni@2563: w_front = NULL; bjarni@2563: w_rear = NULL; bjarni@2563: maedhros@6444: bjarni@2563: /* bjarni@2563: * v_front is the front engine in the original vehicle maedhros@6444: * 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@6259: if (v->type == VEH_TRAIN && (!IsFrontEngine(v) || v->u.rail.crash_anim_pos >= 4400)) return CMD_ERROR; bjarni@2244: belugas@6423: /* 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@7492: } while ((v = v->Next()) != NULL); bjarni@2601: rubidium@7398: 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 { bjarni@7526: 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: bjarni@5792: 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: rubidium@6950: total_cost.AddCost(cost); bjarni@2563: bjarni@2563: if (flags & DC_EXEC) { tron@2639: w = GetVehicle(_new_vehicle_id); bjarni@2563: rubidium@6259: if (v->type == VEH_TRAIN && HASBIT(v->u.rail.flags, VRF_REVERSE_DIRECTION)) { bjarni@3896: SETBIT(w->u.rail.flags, VRF_REVERSE_DIRECTION); bjarni@3896: } bjarni@2563: rubidium@6259: if (v->type == VEH_TRAIN && !IsFrontEngine(v)) { belugas@6423: /* this s a train car belugas@6423: * add this unit to the end of the train */ rubidium@6943: CommandCost result = DoCommand(0, (w_rear->index << 16) | w->index, 1, flags, CMD_MOVE_RAIL_VEHICLE); bjarni@6838: if (CmdFailed(result)) { bjarni@6838: /* The train can't be joined to make the same consist as the original. bjarni@6838: * Sell what we already made (clean up) and return an error. */ bjarni@6838: DoCommand(w_front->tile, w_front->index, 1, flags, GetCmdSellVeh(w_front)); bjarni@6838: DoCommand(w_front->tile, w->index, 1, flags, GetCmdSellVeh(w)); bjarni@6838: return result; // return error and the message returned from CMD_MOVE_RAIL_VEHICLE bjarni@6838: } bjarni@2563: } else { belugas@6423: /* this is a front engine or not a train. It need orders */ bjarni@2563: w_front = w; bjarni@3679: w->service_interval = v->service_interval; tron@3491: DoCommand(0, (v->index << 16) | w->index, p2 & 1 ? CO_SHARE : CO_COPY, flags, CMD_CLONE_ORDER); 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@6259: } while (v->type == VEH_TRAIN && (v = GetNextVehicle(v)) != NULL); rubidium@6259: rubidium@6259: if (flags & DC_EXEC && v_front->type == VEH_TRAIN) { belugas@6423: /* 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: rubidium@6643: if (flags & DC_EXEC) { rubidium@6643: /* Cloned vehicles belong to the same group */ rubidium@6643: DoCommand(0, v_front->group_id, w_front->index, flags, CMD_ADD_VEHICLE_GROUP); rubidium@6643: } rubidium@6643: rubidium@6643: maedhros@6546: /* Take care of refitting. */ maedhros@6546: w = w_front; maedhros@6546: v = v_front; maedhros@6546: maedhros@6546: /* Both building and refitting are influenced by newgrf callbacks, which maedhros@6546: * makes it impossible to accurately estimate the cloning costs. In maedhros@6546: * particular, it is possible for engines of the same type to be built with maedhros@6546: * different numbers of articulated parts, so when refitting we have to maedhros@6546: * loop over real vehicles first, and then the articulated parts of those maedhros@6546: * vehicles in a different loop. */ maedhros@6546: do { maedhros@6546: do { maedhros@6546: if (flags & DC_EXEC) { maedhros@6546: assert(w != NULL); maedhros@6546: maedhros@6546: if (w->cargo_type != v->cargo_type || w->cargo_subtype != v->cargo_type) { maedhros@6546: cost = DoCommand(0, w->index, v->cargo_type | (v->cargo_subtype << 8) | 1U << 16 , flags, GetCmdRefitVeh(v)); rubidium@6950: if (CmdSucceeded(cost)) total_cost.AddCost(cost); maedhros@6546: } maedhros@6546: maedhros@6546: if (w->type == VEH_TRAIN && EngineHasArticPart(w)) { maedhros@6546: w = GetNextArticPart(w); maedhros@6857: } else if (w->type == VEH_ROAD && RoadVehHasArticPart(w)) { rubidium@7492: w = w->Next(); maedhros@6546: } else { maedhros@6546: break; maedhros@6546: } maedhros@6546: } else { maedhros@6546: CargoID initial_cargo = GetEngineCargoType(v->engine_type); maedhros@6546: maedhros@6546: if (v->cargo_type != initial_cargo && initial_cargo != CT_INVALID) { rubidium@6950: total_cost.AddCost(GetRefitCost(v->engine_type)); maedhros@6546: } maedhros@6546: } maedhros@6857: maedhros@6857: if (v->type == VEH_TRAIN && EngineHasArticPart(v)) { maedhros@6857: v = GetNextArticPart(v); maedhros@6857: } else if (v->type == VEH_ROAD && RoadVehHasArticPart(v)) { rubidium@7492: v = v->Next(); maedhros@6857: } else { maedhros@6857: break; maedhros@6857: } maedhros@6857: } while (v != NULL); maedhros@6546: maedhros@6727: if ((flags & DC_EXEC) && v->type == VEH_TRAIN) w = GetNextVehicle(w); maedhros@6546: } while (v->type == VEH_TRAIN && (v = GetNextVehicle(v)) != NULL); maedhros@6546: maedhros@6546: /* Since we can't estimate the cost of cloning a vehicle accurately we must maedhros@6546: * check whether the player has enough money manually. */ maedhros@6546: if (!CheckPlayerHasMoney(total_cost)) { maedhros@6546: if (flags & DC_EXEC) { maedhros@6546: /* The vehicle has already been bought, so now it must be sold again. */ maedhros@6546: DoCommand(w_front->tile, w_front->index, 1, flags, GetCmdSellVeh(w_front)); maedhros@6546: } maedhros@6546: return CMD_ERROR; maedhros@6546: } maedhros@6546: peter1138@5062: /* Set the expense type last as refitting will make the cost go towards peter1138@5062: * running costs... */ peter1138@5062: SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES); bjarni@2244: return total_cost; bjarni@2244: } bjarni@2244: bjarni@2552: bjarni@4635: /* Extend the list size for BuildDepotVehicleList() */ bjarni@4678: static inline void ExtendVehicleListSize(const Vehicle ***engine_list, uint16 *engine_list_length, uint16 step_size) bjarni@4635: { matthijs@5247: *engine_list_length = min(*engine_list_length + step_size, GetMaxVehicleIndex() + 1); KUDr@5609: *engine_list = ReallocT(*engine_list, *engine_list_length); bjarni@4635: } bjarni@4635: bjarni@4635: /** Generates a list of vehicles inside a depot bjarni@4635: * Will enlarge allocated space for the list if they are too small, so it's ok to call with (pointer to NULL array, pointer to uninitised uint16, pointer to 0) bjarni@4635: * If one of the lists is not needed (say wagons when finding ships), all the pointers regarding that list should be set to NULL belugas@6484: * @param type Type of vehicle bjarni@4635: * @param tile The tile the depot is located in bjarni@4635: * @param ***engine_list Pointer to a pointer to an array of vehicles in the depot (old list is freed and a new one is malloced) bjarni@4635: * @param *engine_list_length Allocated size of engine_list. Needs to be set to 0 when engine_list points to a NULL array bjarni@4635: * @param *engine_count The number of engines stored in the list bjarni@4635: * @param ***wagon_list Pointer to a pointer to an array of free wagons in the depot (old list is freed and a new one is malloced) bjarni@4635: * @param *wagon_list_length Allocated size of wagon_list. Needs to be set to 0 when wagon_list points to a NULL array bjarni@4635: * @param *wagon_count The number of engines stored in the list bjarni@4635: */ rubidium@6641: void BuildDepotVehicleList(VehicleType type, TileIndex tile, Vehicle ***engine_list, uint16 *engine_list_length, uint16 *engine_count, Vehicle ***wagon_list, uint16 *wagon_list_length, uint16 *wagon_count) bjarni@4635: { bjarni@4635: Vehicle *v; bjarni@4635: bjarni@4635: /* This function should never be called without an array to store results */ rubidium@6259: assert(!(engine_list == NULL && type != VEH_TRAIN)); rubidium@6259: assert(!(type == VEH_TRAIN && engine_list == NULL && wagon_list == NULL)); bjarni@4635: bjarni@4635: /* Both array and the length should either be NULL to disable the list or both should not be NULL */ bjarni@4635: assert((engine_list == NULL && engine_list_length == NULL) || (engine_list != NULL && engine_list_length != NULL)); bjarni@4635: assert((wagon_list == NULL && wagon_list_length == NULL) || (wagon_list != NULL && wagon_list_length != NULL)); bjarni@4635: bjarni@4635: assert(!(engine_list != NULL && engine_count == NULL)); bjarni@4635: assert(!(wagon_list != NULL && wagon_count == NULL)); bjarni@4635: bjarni@4635: if (engine_count != NULL) *engine_count = 0; bjarni@4635: if (wagon_count != NULL) *wagon_count = 0; bjarni@4635: bjarni@4635: switch (type) { rubidium@6259: case VEH_TRAIN: bjarni@4635: FOR_ALL_VEHICLES(v) { rubidium@6259: if (v->tile == tile && v->type == VEH_TRAIN && v->u.rail.track == TRACK_BIT_DEPOT) { bjarni@4635: if (IsFrontEngine(v)) { bjarni@4635: if (engine_list == NULL) continue; bjarni@4678: if (*engine_count == *engine_list_length) ExtendVehicleListSize((const Vehicle***)engine_list, engine_list_length, 25); bjarni@4635: (*engine_list)[(*engine_count)++] = v; bjarni@4635: } else if (IsFreeWagon(v)) { bjarni@4635: if (wagon_list == NULL) continue; bjarni@4678: if (*wagon_count == *wagon_list_length) ExtendVehicleListSize((const Vehicle***)wagon_list, wagon_list_length, 25); bjarni@4635: (*wagon_list)[(*wagon_count)++] = v; bjarni@4635: } bjarni@4635: } bjarni@4635: } bjarni@4635: break; bjarni@4635: rubidium@6259: case VEH_ROAD: bjarni@4635: FOR_ALL_VEHICLES(v) { rubidium@7490: if (v->tile == tile && v->type == VEH_ROAD && v->IsInDepot() && IsRoadVehFront(v)) { bjarni@4678: if (*engine_count == *engine_list_length) ExtendVehicleListSize((const Vehicle***)engine_list, engine_list_length, 25); bjarni@4635: (*engine_list)[(*engine_count)++] = v; bjarni@4635: } bjarni@4635: } bjarni@4635: break; bjarni@4635: rubidium@6259: case VEH_SHIP: bjarni@4635: FOR_ALL_VEHICLES(v) { rubidium@7490: if (v->tile == tile && v->type == VEH_SHIP && v->IsInDepot()) { bjarni@4678: if (*engine_count == *engine_list_length) ExtendVehicleListSize((const Vehicle***)engine_list, engine_list_length, 25); bjarni@4635: (*engine_list)[(*engine_count)++] = v; bjarni@4635: } bjarni@4635: } bjarni@4635: break; bjarni@4635: rubidium@6259: case VEH_AIRCRAFT: bjarni@4635: FOR_ALL_VEHICLES(v) { bjarni@4635: if (v->tile == tile && rubidium@6259: v->type == VEH_AIRCRAFT && IsNormalAircraft(v) && rubidium@7490: v->IsInDepot()) { bjarni@4678: if (*engine_count == *engine_list_length) ExtendVehicleListSize((const Vehicle***)engine_list, engine_list_length, 25); bjarni@4635: (*engine_list)[(*engine_count)++] = v; bjarni@4635: } bjarni@4635: } bjarni@4635: break; bjarni@4635: bjarni@4635: default: NOT_REACHED(); bjarni@4635: } bjarni@4635: } bjarni@4635: bjarni@4497: /** bjarni@4678: * @param sort_list list to store the list in. Either NULL or the length length_of_array tells bjarni@4678: * @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 bjarni@4554: * @param type type of vehicle bjarni@4554: * @param owner PlayerID of owner to generate a list for belugas@6484: * @param index This parameter has different meanings depending on window_type belugas@6484: bjarni@4554: * @param window_type tells what kind of window the list is for. Use the VLW flags in vehicle_gui.h bjarni@4554: * @return the number of vehicles added to the list bjarni@4554: */ rubidium@6641: uint GenerateVehicleSortList(const Vehicle ***sort_list, uint16 *length_of_array, VehicleType type, PlayerID owner, uint32 index, uint16 window_type) bjarni@4497: { bjarni@4499: uint n = 0; bjarni@4497: const Vehicle *v; bjarni@4497: bjarni@4497: switch (window_type) { bjarni@4497: case VLW_STATION_LIST: { bjarni@4497: FOR_ALL_VEHICLES(v) { maedhros@6773: if (v->type == type && v->IsPrimaryVehicle()) { bjarni@4497: const Order *order; bjarni@4497: bjarni@4497: FOR_VEHICLE_ORDERS(v, order) { bjarni@5747: if (order->type == OT_GOTO_STATION && order->dest == index) { bjarni@4678: if (n == *length_of_array) ExtendVehicleListSize(sort_list, length_of_array, 50); bjarni@4678: (*sort_list)[n++] = v; bjarni@4497: break; bjarni@4497: } bjarni@4497: } bjarni@4497: } bjarni@4497: } bjarni@4497: break; bjarni@4497: } bjarni@4497: bjarni@4497: case VLW_SHARED_ORDERS: { bjarni@4497: FOR_ALL_VEHICLES(v) { bjarni@4497: /* Find a vehicle with the order in question */ bjarni@5747: if (v->orders != NULL && v->orders->index == index) break; bjarni@4497: } bjarni@4497: bjarni@5747: if (v != NULL && v->orders != NULL && v->orders->index == index) { bjarni@4497: /* Only try to make the list if we found a vehicle using the order in question */ bjarni@4497: for (v = GetFirstVehicleFromSharedList(v); v != NULL; v = v->next_shared) { bjarni@4678: if (n == *length_of_array) ExtendVehicleListSize(sort_list, length_of_array, 25); bjarni@4678: (*sort_list)[n++] = v; bjarni@4497: } bjarni@4497: } bjarni@4497: break; bjarni@4497: } bjarni@4497: bjarni@4497: case VLW_STANDARD: { bjarni@4497: FOR_ALL_VEHICLES(v) { maedhros@6773: if (v->type == type && v->owner == owner && v->IsPrimaryVehicle()) { bjarni@4678: /* TODO find a better estimate on the total number of vehicles for current player */ matthijs@5247: if (n == *length_of_array) ExtendVehicleListSize(sort_list, length_of_array, GetNumVehicles()/4); bjarni@4678: (*sort_list)[n++] = v; bjarni@4497: } bjarni@4497: } bjarni@4497: break; bjarni@4497: } bjarni@4497: bjarni@4681: case VLW_DEPOT_LIST: { bjarni@4681: FOR_ALL_VEHICLES(v) { maedhros@6773: if (v->type == type && v->IsPrimaryVehicle()) { bjarni@4681: const Order *order; bjarni@4681: bjarni@4681: FOR_VEHICLE_ORDERS(v, order) { bjarni@5747: if (order->type == OT_GOTO_DEPOT && order->dest == index) { bjarni@4681: if (n == *length_of_array) ExtendVehicleListSize(sort_list, length_of_array, 25); bjarni@4681: (*sort_list)[n++] = v; bjarni@4681: break; bjarni@4681: } bjarni@4681: } bjarni@4681: } bjarni@4681: } bjarni@4681: break; bjarni@4681: } bjarni@4681: rubidium@6643: case VLW_GROUP_LIST: rubidium@6643: FOR_ALL_VEHICLES(v) { maedhros@6773: if (v->type == type && v->IsPrimaryVehicle() && maedhros@6773: v->owner == owner && v->group_id == index) { rubidium@6643: if (n == *length_of_array) ExtendVehicleListSize(sort_list, length_of_array, GetNumVehicles() / 4); rubidium@6643: rubidium@6643: (*sort_list)[n++] = v; rubidium@6643: } rubidium@6643: } rubidium@6643: break; rubidium@6643: bjarni@4497: default: NOT_REACHED(); break; bjarni@4497: } bjarni@4497: bjarni@4678: if ((n + 100) < *length_of_array) { bjarni@4678: /* We allocated way too much for sort_list. bjarni@4678: * Now we will reduce how much we allocated. bjarni@4678: * We will still make it have room for 50 extra vehicles to prevent having bjarni@4678: * to move the whole array if just one vehicle is added later */ bjarni@4678: *length_of_array = n + 50; KUDr@5609: *sort_list = ReallocT(*sort_list, (*length_of_array) * sizeof((*sort_list)[0])); bjarni@4678: } bjarni@4678: bjarni@4497: return n; bjarni@4497: } bjarni@4497: bjarni@4463: /** 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 belugas@6484: * @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: */ rubidium@6943: CommandCost SendAllVehiclesToDepot(VehicleType type, uint32 flags, bool service, PlayerID owner, uint16 vlw_flag, uint32 id) bjarni@4463: { bjarni@4678: const Vehicle **sort_list = NULL; bjarni@4497: uint n, i; bjarni@4678: uint16 array_length = 0; bjarni@4678: bjarni@5747: n = GenerateVehicleSortList(&sort_list, &array_length, type, owner, id, vlw_flag); bjarni@4465: bjarni@4463: /* Send all the vehicles to a depot */ bjarni@4499: for (i = 0; i < n; i++) { bjarni@4497: const Vehicle *v = sort_list[i]; rubidium@6943: 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 */ rubidium@6946: if (CmdSucceeded(ret) && !(flags & DC_EXEC)) { bjarni@4683: free((void*)sort_list); rubidium@6950: return CommandCost(); bjarni@4463: } bjarni@4463: } bjarni@4465: bjarni@4683: free((void*)sort_list); rubidium@6950: return (flags & DC_EXEC) ? CommandCost() : CMD_ERROR; bjarni@4463: } bjarni@4463: truelight@6998: /** truelight@6998: * Calculates how full a vehicle is. truelight@6998: * @param v The Vehicle to check. For trains, use the first engine. truelight@7014: * @param color The string to show depending on if we are unloading or loading truelight@6998: * @return A percentage of how full the Vehicle is. truelight@6998: */ truelight@7014: uint8 CalcPercentVehicleFilled(Vehicle *v, StringID *color) truelight@6998: { truelight@6998: int count = 0; truelight@6998: int max = 0; truelight@7014: int cars = 0; truelight@7014: int unloading = 0; rubidium@7087: bool loading = false; truelight@7014: truelight@7014: assert(color != NULL); truelight@6998: rubidium@7087: const Vehicle *u = v; rubidium@7087: const Station *st = GetStation(v->last_station_visited); rubidium@7087: truelight@6998: /* Count up max and used */ rubidium@7492: for (; v != NULL; v = v->Next()) { rubidium@7010: count += v->cargo.Count(); truelight@6998: max += v->cargo_cap; truelight@7014: if (v->cargo_cap != 0) { truelight@7014: unloading += HASBIT(v->vehicle_flags, VF_CARGO_UNLOADING) ? 1 : 0; rubidium@7087: loading |= (u->current_order.flags & OF_UNLOAD) == 0 && st->goods[v->cargo_type].days_since_pickup != 255; truelight@7014: cars++; truelight@7014: } truelight@6998: } truelight@6998: rubidium@7087: if (unloading == 0 && loading) *color = STR_PERCENT_UP; rubidium@7087: else if (cars == unloading || !loading) *color = STR_PERCENT_DOWN; rubidium@7087: else *color = STR_PERCENT_UP_DOWN; truelight@7014: truelight@6998: /* Train without capacity */ truelight@6998: if (max == 0) return 100; truelight@6998: truelight@6998: /* Return the percentage */ truelight@6998: return (count * 100) / max; truelight@6998: } truelight@6998: bjarni@4725: void VehicleEnterDepot(Vehicle *v) bjarni@4725: { bjarni@4725: switch (v->type) { rubidium@6259: case VEH_TRAIN: bjarni@4725: InvalidateWindowClasses(WC_TRAINS_LIST); rubidium@7497: if (!IsFrontEngine(v)) v = v->First(); bjarni@4725: UpdateSignalsOnSegment(v->tile, GetRailDepotDirection(v->tile)); bjarni@4725: v->load_unload_time_rem = 0; bjarni@4725: break; bjarni@4725: rubidium@6259: case VEH_ROAD: bjarni@4725: InvalidateWindowClasses(WC_ROADVEH_LIST); rubidium@7497: if (!IsRoadVehFront(v)) v = v->First(); bjarni@4725: break; bjarni@4725: rubidium@6259: case VEH_SHIP: bjarni@4725: InvalidateWindowClasses(WC_SHIPS_LIST); rubidium@5993: v->u.ship.state = TRACK_BIT_DEPOT; bjarni@4725: RecalcShipStuff(v); bjarni@4725: break; bjarni@4725: rubidium@6259: 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@6259: 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: bjarni@4725: if (v->current_order.type == OT_GOTO_DEPOT) { bjarni@4725: Order t; bjarni@4725: bjarni@4725: InvalidateWindow(WC_VEHICLE_VIEW, v->index); bjarni@4725: bjarni@4725: t = v->current_order; bjarni@4725: v->current_order.type = OT_DUMMY; bjarni@4725: v->current_order.flags = 0; bjarni@4725: bjarni@4782: if (t.refit_cargo < NUM_CARGO) { rubidium@6943: CommandCost cost; bjarni@4725: bjarni@4725: _current_player = v->owner; bjarni@5792: cost = DoCommand(v->tile, v->index, t.refit_cargo | t.refit_subtype << 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@5955: SetDParam(0, _vehicle_type_names[v->type]); bjarni@4783: SetDParam(1, v->unitnumber); bjarni@4783: AddNewsItem(STR_ORDER_REFIT_FAILED, NEWS_FLAGS(NM_SMALL, NF_VIEWPORT|NF_VEHICLE, NT_ADVICE, 0), v->index, 0); bjarni@4783: } rubidium@6950: } else if (v->owner == _local_player && cost.GetCost() != 0) { rubidium@6950: ShowCostOrIncomeAnimation(v->x_pos, v->y_pos, v->z_pos, cost.GetCost()); bjarni@4783: } bjarni@4725: } bjarni@4725: bjarni@4725: if (HASBIT(t.flags, OFB_PART_OF_ORDERS)) { bjarni@4725: /* Part of orders */ maedhros@7070: UpdateVehicleTimetable(v, true); bjarni@4725: v->cur_order_index++; bjarni@4725: } else if (HASBIT(t.flags, OFB_HALT_IN_DEPOT)) { 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@6259: case VEH_TRAIN: string = STR_8814_TRAIN_IS_WAITING_IN_DEPOT; break; rubidium@6259: case VEH_ROAD: string = STR_9016_ROAD_VEHICLE_IS_WAITING; break; rubidium@6259: case VEH_SHIP: string = STR_981C_SHIP_IS_WAITING_IN_DEPOT; break; rubidium@6259: 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); rubidium@6492: AddNewsItem(string, NEWS_FLAGS(NM_SMALL, NF_VIEWPORT|NF_VEHICLE, NT_ADVICE, 0), v->index, 0); bjarni@4725: } bjarni@4725: } bjarni@4725: } bjarni@4725: } bjarni@2244: peter1138@7097: static bool IsUniqueVehicleName(const char *name) peter1138@7097: { peter1138@7097: const Vehicle *v; peter1138@7097: char buf[512]; peter1138@7097: peter1138@7097: FOR_ALL_VEHICLES(v) { peter1138@7097: switch (v->type) { peter1138@7097: case VEH_TRAIN: peter1138@7108: if (!IsTrainEngine(v)) continue; peter1138@7108: break; peter1138@7108: peter1138@7097: case VEH_ROAD: peter1138@7358: if (!IsRoadVehFront(v)) continue; peter1138@7108: break; peter1138@7108: peter1138@7097: case VEH_AIRCRAFT: peter1138@7108: if (!IsNormalAircraft(v)) continue; peter1138@7108: break; peter1138@7108: peter1138@7097: case VEH_SHIP: peter1138@7097: break; peter1138@7097: peter1138@7097: default: peter1138@7108: continue; peter1138@7097: } peter1138@7108: peter1138@7108: SetDParam(0, v->index); peter1138@7108: GetString(buf, STR_VEHICLE_NAME, lastof(buf)); peter1138@7108: if (strcmp(buf, name) == 0) return false; peter1138@7097: } peter1138@7097: peter1138@7097: return true; peter1138@7097: } peter1138@7097: Darkvater@1786: /** Give a custom name to your vehicle tron@3491: * @param tile unused belugas@6423: * @param flags type of operation Darkvater@1786: * @param p1 vehicle ID to name Darkvater@1786: * @param p2 unused Darkvater@1786: */ rubidium@6943: CommandCost CmdNameVehicle(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) truelight@0: { truelight@0: Vehicle *v; truelight@0: StringID str; truelight@0: peter1138@7097: if (!IsValidVehicleID(p1) || StrEmpty(_cmd_text)) return CMD_ERROR; bjarni@1237: truelight@919: v = GetVehicle(p1); truelight@0: Darkvater@1786: if (!CheckOwnership(v->owner)) return CMD_ERROR; truelight@0: peter1138@7097: if (!IsUniqueVehicleName(_cmd_text)) return_cmd_error(STR_NAME_MUST_BE_UNIQUE); peter1138@7097: peter1138@7097: str = AllocateName(_cmd_text, 2); Darkvater@1786: if (str == 0) return CMD_ERROR; truelight@193: truelight@0: if (flags & DC_EXEC) { truelight@0: StringID old_str = v->string_id; truelight@0: v->string_id = str; truelight@0: DeleteName(old_str); tron@588: ResortVehicleLists(); truelight@0: MarkWholeScreenDirty(); truelight@0: } else { truelight@0: DeleteName(str); truelight@0: } truelight@0: rubidium@6950: return CommandCost(); truelight@0: } truelight@0: truelight@0: tron@2819: /** Change the service interval of a vehicle tron@3491: * @param tile unused belugas@6423: * @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: */ rubidium@6943: CommandCost CmdChangeServiceInt(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) tron@2819: { tron@2819: Vehicle* v; 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: tron@2819: 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: rubidium@6950: return CommandCost(); tron@2819: } tron@2819: truelight@0: truelight@0: static Rect _old_vehicle_coords; truelight@0: rubidium@7321: void BeginVehicleMove(Vehicle *v) rubidium@7321: { truelight@0: _old_vehicle_coords.left = v->left_coord; truelight@0: _old_vehicle_coords.top = v->top_coord; truelight@0: _old_vehicle_coords.right = v->right_coord; truelight@0: _old_vehicle_coords.bottom = v->bottom_coord; truelight@0: } truelight@0: truelight@0: void EndVehicleMove(Vehicle *v) truelight@0: { truelight@0: MarkAllViewportsDirty( truelight@0: min(_old_vehicle_coords.left,v->left_coord), truelight@0: min(_old_vehicle_coords.top,v->top_coord), truelight@0: max(_old_vehicle_coords.right,v->right_coord)+1, truelight@0: max(_old_vehicle_coords.bottom,v->bottom_coord)+1 truelight@0: ); truelight@0: } truelight@0: truelight@0: /* returns true if staying in the same tile */ tron@6153: 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@6153: GetNewVehiclePosResult gp; tron@6153: gp.x = x; tron@6153: gp.y = y; tron@6153: gp.old_tile = v->tile; tron@6153: gp.new_tile = TileVirtXY(x, y); tron@6153: 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: tron@3157: Direction GetDirectionTowards(const Vehicle* v, int x, int y) truelight@0: { tron@3157: Direction dir; tron@3158: DirDiff dirdiff; truelight@0: int i = 0; truelight@0: truelight@0: if (y >= v->y_pos) { truelight@0: if (y != v->y_pos) i+=3; truelight@0: 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: truelight@0: dir = v->direction; truelight@0: tron@3158: 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: matthijs@1944: Trackdir GetVehicleTrackdir(const Vehicle* v) matthijs@1752: { rubidium@5587: if (v->vehstatus & VS_CRASHED) return INVALID_TRACKDIR; matthijs@1758: tron@2952: switch (v->type) { rubidium@6259: case VEH_TRAIN: belugas@6423: if (v->u.rail.track == TRACK_BIT_DEPOT) // We'll assume the train is facing outwards belugas@6423: return DiagdirToDiagTrackdir(GetRailDepotDirection(v->tile)); // Train in depot belugas@6423: belugas@6423: if (v->u.rail.track == TRACK_BIT_WORMHOLE) // train in tunnel, so just use his direction and assume a diagonal track tron@3161: return DiagdirToDiagTrackdir(DirToDiagDir(v->direction)); Darkvater@1765: rubidium@5587: return TrackDirectionToTrackdir(FindFirstTrack(v->u.rail.track), v->direction); tron@1959: rubidium@6259: case VEH_SHIP: rubidium@7490: if (v->IsInDepot()) belugas@6423: // We'll assume the ship is facing outwards tron@3953: return DiagdirToDiagTrackdir(GetShipDepotDirection(v->tile)); Darkvater@1765: rubidium@5587: return TrackDirectionToTrackdir(FindFirstTrack(v->u.ship.state), v->direction); tron@1959: rubidium@6259: case VEH_ROAD: rubidium@7490: if (v->IsInDepot()) // We'll assume the road vehicle is facing outwards tron@3179: return DiagdirToDiagTrackdir(GetRoadDepotDirection(v->tile)); Darkvater@1765: belugas@6423: if (IsStandardRoadStopTile(v->tile)) // We'll assume the road vehicle is facing outwards belugas@6423: return DiagdirToDiagTrackdir(GetRoadStopDir(v->tile)); // Road vehicle in a station Darkvater@1765: rubidium@6012: if (IsDriveThroughStopTile(v->tile)) return DiagdirToDiagTrackdir(DirToDiagDir(v->direction)); rubidium@6012: Darkvater@4270: /* If vehicle's state is a valid track direction (vehicle is not turning around) return it */ rubidium@6009: 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 */ tron@3161: return DiagdirToDiagTrackdir(DirToDiagDir(v->direction)); tron@1959: rubidium@6259: /* case VEH_AIRCRAFT: case VEH_SPECIAL: case VEH_DISASTER: */ rubidium@5587: default: return INVALID_TRACKDIR; matthijs@1752: } matthijs@1752: } rubidium@5991: rubidium@5991: /** rubidium@5991: * Returns some meta-data over the to be entered tile. rubidium@5991: * @see VehicleEnterTileStatus to see what the bits in the return value mean. rubidium@5991: */ 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: rubidium@6641: UnitID GetFreeUnitNumber(VehicleType type) truelight@0: { truelight@3018: UnitID unit, 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@6259: case VEH_TRAIN: max = _patches.max_trains; break; rubidium@6259: case VEH_ROAD: max = _patches.max_roadveh; break; rubidium@6259: case VEH_SHIP: max = _patches.max_ships; break; rubidium@6259: case VEH_AIRCRAFT: max = _patches.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@5609: cache = MallocT(max + 1); peter1138@2995: } peter1138@2995: belugas@6423: /* Clear the cache */ peter1138@2995: memset(cache, 0, (max + 1) * sizeof(*cache)); peter1138@2995: belugas@6423: /* 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: belugas@6423: /* Find the first unused unit number */ peter1138@2995: for (unit = 1; unit <= max; unit++) { peter1138@2995: if (!cache[unit]) break; peter1138@2995: } peter1138@2995: peter1138@2995: return unit; truelight@0: } truelight@0: peter1138@6516: rubidium@7086: /** rubidium@7086: * Check whether we can build infrastructure for the given rubidium@7086: * vehicle type. This to disable building stations etc. when rubidium@7086: * you are not allowed/able to have the vehicle type yet. rubidium@7086: * @param type the vehicle type to check this for rubidium@7086: * @return true if there is any reason why you may build rubidium@7086: * the infrastructure for the given vehicle type rubidium@7086: */ rubidium@7086: bool CanBuildVehicleInfrastructure(VehicleType type) rubidium@7086: { rubidium@7086: assert(IsPlayerBuildableVehicleType(type)); rubidium@7086: maedhros@7415: if (!IsValidPlayer(_current_player)) return false; rubidium@7086: if (_patches.always_build_infrastructure) return true; rubidium@7086: rubidium@7086: UnitID max; rubidium@7086: switch (type) { rubidium@7086: case VEH_TRAIN: max = _patches.max_trains; break; rubidium@7086: case VEH_ROAD: max = _patches.max_roadveh; break; rubidium@7086: case VEH_SHIP: max = _patches.max_ships; break; rubidium@7086: case VEH_AIRCRAFT: max = _patches.max_aircraft; break; rubidium@7086: default: NOT_REACHED(); rubidium@7086: } rubidium@7086: rubidium@7086: /* We can build vehicle infrastructure when we may build the vehicle type */ rubidium@7086: if (max > 0) { rubidium@7086: rubidium@7086: /* Can we actually build the vehicle type? */ rubidium@7086: EngineID e; rubidium@7086: FOR_ALL_ENGINEIDS_OF_TYPE(e, type) { rubidium@7086: if (HASBIT(GetEngine(e)->player_avail, _local_player)) return true; rubidium@7086: } rubidium@7086: return false; rubidium@7086: } rubidium@7086: rubidium@7086: /* We should be able to build infrastructure when we have the actual vehicle type */ rubidium@7086: const Vehicle *v; rubidium@7086: FOR_ALL_VEHICLES(v) { rubidium@7086: if (v->owner == _local_player && v->type == type) return true; rubidium@7086: } rubidium@7086: rubidium@7086: return false; rubidium@7086: } rubidium@7086: rubidium@7086: peter1138@6516: 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@5717: CargoID cargo_type = v == NULL ? (CargoID)CT_INVALID : v->cargo_type; peter1138@5717: 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. */ peter1138@4616: if (p->livery[LS_DEFAULT].in_use && (_patches.liveries == 2 || (_patches.liveries == 1 && player == _local_player))) { peter1138@4603: /* Determine the livery scheme to use */ peter1138@4603: switch (GetEngine(engine_type)->type) { rubidium@6638: default: NOT_REACHED(); rubidium@6259: case VEH_TRAIN: { tron@5763: const RailVehicleInfo *rvi = RailVehInfo(engine_type); tron@5763: tron@5763: switch (rvi->railtype) { tron@5823: default: NOT_REACHED(); peter1138@4603: case RAILTYPE_RAIL: peter1138@4603: case RAILTYPE_ELECTRIC: peter1138@4603: { peter1138@4603: if (cargo_type == CT_INVALID) cargo_type = rvi->cargo_type; belugas@5868: if (rvi->railveh_type == RAILVEH_WAGON) { peter1138@6658: if (!GetCargo(cargo_type)->is_freight) { peter1138@4604: if (parent_engine_type == INVALID_ENGINE) { peter1138@4604: scheme = LS_PASSENGER_WAGON_STEAM; peter1138@4604: } else { peter1138@4604: switch (RailVehInfo(parent_engine_type)->engclass) { rubidium@6585: default: NOT_REACHED(); rubidium@6581: case EC_STEAM: scheme = LS_PASSENGER_WAGON_STEAM; break; rubidium@6581: case EC_DIESEL: scheme = LS_PASSENGER_WAGON_DIESEL; break; rubidium@6581: case EC_ELECTRIC: scheme = LS_PASSENGER_WAGON_ELECTRIC; break; peter1138@4604: } peter1138@4604: } peter1138@4604: } else { peter1138@4604: scheme = LS_FREIGHT_WAGON; peter1138@4604: } peter1138@4603: } else { peter1138@7273: bool is_mu = HASBIT(EngInfo(engine_type)->misc_flags, EF_RAIL_IS_MU); peter1138@4603: peter1138@4603: switch (rvi->engclass) { rubidium@6585: default: NOT_REACHED(); rubidium@6581: case EC_STEAM: scheme = LS_STEAM; break; rubidium@6581: case EC_DIESEL: scheme = is_mu ? LS_DMU : LS_DIESEL; break; rubidium@6581: case EC_ELECTRIC: scheme = is_mu ? LS_EMU : LS_ELECTRIC; break; peter1138@4603: } peter1138@4603: } peter1138@4603: break; peter1138@4603: } peter1138@4603: peter1138@4603: case RAILTYPE_MONO: scheme = LS_MONORAIL; break; peter1138@4603: case RAILTYPE_MAGLEV: scheme = LS_MAGLEV; break; peter1138@4603: } peter1138@4603: break; peter1138@4603: } peter1138@4603: rubidium@6259: case VEH_ROAD: { peter1138@4603: const RoadVehicleInfo *rvi = RoadVehInfo(engine_type); peter1138@4603: if (cargo_type == CT_INVALID) cargo_type = rvi->cargo_type; peter1138@6724: if (HASBIT(EngInfo(engine_type)->misc_flags, EF_ROAD_TRAM)) { peter1138@6724: /* Tram */ peter1138@6724: scheme = IsCargoInClass(cargo_type, CC_PASSENGERS) ? LS_PASSENGER_TRAM : LS_FREIGHT_TRAM; peter1138@6724: } else { peter1138@6724: /* Bus or truck */ peter1138@6724: scheme = IsCargoInClass(cargo_type, CC_PASSENGERS) ? LS_BUS : LS_TRUCK; peter1138@6724: } peter1138@4603: break; peter1138@4603: } peter1138@4603: rubidium@6259: case VEH_SHIP: { peter1138@4603: const ShipVehicleInfo *svi = ShipVehInfo(engine_type); peter1138@4603: if (cargo_type == CT_INVALID) cargo_type = svi->cargo_type; peter1138@6330: scheme = IsCargoInClass(cargo_type, CC_PASSENGERS) ? LS_PASSENGER_SHIP : LS_FREIGHT_SHIP; peter1138@4603: break; peter1138@4603: } peter1138@4603: rubidium@6259: 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@5855: case AIR_HELI: scheme = LS_HELICOPTER; break; Darkvater@5855: case AIR_CTOL: scheme = LS_SMALL_PLANE; break; Darkvater@5855: 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: peter1138@6516: return &p->livery[scheme]; peter1138@6516: } peter1138@6516: peter1138@6516: peter1138@6516: static SpriteID GetEngineColourMap(EngineID engine_type, PlayerID player, EngineID parent_engine_type, const Vehicle *v) peter1138@6516: { peter1138@6516: SpriteID map = PAL_NONE; peter1138@6516: peter1138@6516: /* Check if we should use the colour map callback */ peter1138@6516: if (HASBIT(EngInfo(engine_type)->callbackmask, CBM_COLOUR_REMAP)) { peter1138@6516: uint16 callback = GetVehicleCallback(CBID_VEHICLE_COLOUR_MAPPING, 0, 0, engine_type, v); peter1138@6516: /* A return value of 0xC000 is stated to "use the default two-color peter1138@6516: * maps" which happens to be the failure action too... */ peter1138@6516: if (callback != CALLBACK_FAILED && callback != 0xC000) { peter1138@6516: map = GB(callback, 0, 14); peter1138@6516: /* If bit 14 is set, then the company colours are applied to the peter1138@6516: * map else it's returned as-is. */ peter1138@6516: if (!HASBIT(callback, 14)) return map; peter1138@6516: } peter1138@6516: } peter1138@6516: peter1138@5717: bool twocc = HASBIT(EngInfo(engine_type)->misc_flags, EF_USES_2CC); peter1138@5717: peter1138@5717: if (map == PAL_NONE) map = twocc ? (SpriteID)SPR_2CCMAP_BASE : (SpriteID)PALETTE_RECOLOR_START; peter1138@5717: peter1138@6516: const Livery *livery = GetEngineLivery(engine_type, player, parent_engine_type, v); peter1138@6516: peter1138@6516: map += livery->colour1; peter1138@6516: if (twocc) map += livery->colour2 * 16; peter1138@3113: peter1138@5668: return map; peter1138@3040: } peter1138@3040: peter1138@5668: SpriteID GetEnginePalette(EngineID engine_type, PlayerID player) peter1138@3105: { peter1138@5717: return GetEngineColourMap(engine_type, player, INVALID_ENGINE, NULL); peter1138@3105: } peter1138@3105: peter1138@5668: SpriteID GetVehiclePalette(const Vehicle *v) peter1138@3105: { rubidium@6259: if (v->type == VEH_TRAIN) { peter1138@4603: return GetEngineColourMap( peter1138@6809: (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@5717: v->owner, v->u.rail.first_engine, v); peter1138@4603: } peter1138@4603: peter1138@5717: return GetEngineColourMap(v->engine_type, v->owner, INVALID_ENGINE, v); peter1138@3105: } peter1138@3105: rubidium@7010: static uint8 _cargo_days; rubidium@7010: static uint16 _cargo_source; rubidium@7010: static uint32 _cargo_source_xy; rubidium@7010: static uint16 _cargo_count; rubidium@7010: static uint16 _cargo_paid_for; rubidium@7010: static Money _cargo_feeder_share; rubidium@7010: static uint32 _cargo_loaded_at_xy; rubidium@7010: rubidium@7493: /** rubidium@7493: * Make it possible to make the saveload tables "friends" of other classes. rubidium@7493: * @param vt the vehicle type. Can be VEH_END for the common vehicle description data rubidium@7493: * @return the saveload description rubidium@7493: */ rubidium@7493: const SaveLoad *GetVehicleDescription(VehicleType vt) rubidium@7493: { belugas@6423: /** Save and load of vehicles */ rubidium@7493: 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@4344: SLE_VAR(Vehicle, string_id, SLE_STRINGID), 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@6561: SLE_CONDNULL(2, 0, 57), rubidium@4344: SLE_VAR(Vehicle, spritenum, SLE_UINT8), rubidium@6561: 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: rubidium@7010: SLE_VAR(Vehicle, cargo_type, SLE_UINT8), rubidium@7010: SLE_CONDVAR(Vehicle, cargo_subtype, SLE_UINT8, 35, SL_MAX_VERSION), rubidium@7010: SLEG_CONDVAR( _cargo_days, SLE_UINT8, 0, 67), rubidium@7010: SLEG_CONDVAR( _cargo_source, SLE_FILE_U8 | SLE_VAR_U16, 0, 6), rubidium@7010: SLEG_CONDVAR( _cargo_source, SLE_UINT16, 7, 67), rubidium@7010: SLEG_CONDVAR( _cargo_source_xy, SLE_UINT32, 44, 67), rubidium@7010: SLE_VAR(Vehicle, cargo_cap, SLE_UINT16), rubidium@7010: SLEG_CONDVAR( _cargo_count, SLE_UINT16, 0, 67), rubidium@7010: 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), 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@6552: SLE_CONDVARX(cpp_offsetof(Vehicle, current_order) + cpp_offsetof(Order, type), SLE_UINT8, 0, 4), rubidium@6552: 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@6552: SLE_CONDVARX(cpp_offsetof(Vehicle, current_order) + cpp_offsetof(Order, type), SLE_UINT8, 5, SL_MAX_VERSION), rubidium@6552: SLE_CONDVARX(cpp_offsetof(Vehicle, current_order) + cpp_offsetof(Order, flags), SLE_UINT8, 5, SL_MAX_VERSION), rubidium@6552: 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@6552: SLE_CONDVARX(cpp_offsetof(Vehicle, current_order) + cpp_offsetof(Order, refit_cargo), SLE_UINT8, 36, SL_MAX_VERSION), rubidium@6552: SLE_CONDVARX(cpp_offsetof(Vehicle, current_order) + cpp_offsetof(Order, refit_subtype), SLE_UINT8, 36, SL_MAX_VERSION), bjarni@4712: maedhros@6980: /* Timetable in current order */ maedhros@6980: SLE_CONDVARX(cpp_offsetof(Vehicle, current_order) + cpp_offsetof(Order, wait_time), SLE_UINT16, 67, SL_MAX_VERSION), maedhros@6980: SLE_CONDVARX(cpp_offsetof(Vehicle, current_order) + cpp_offsetof(Order, travel_time), SLE_UINT16, 67, SL_MAX_VERSION), maedhros@6980: 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: rubidium@7010: SLE_VAR(Vehicle, load_unload_time_rem, SLE_UINT16), rubidium@7010: SLEG_CONDVAR( _cargo_paid_for, SLE_UINT16, 45, SL_MAX_VERSION), rubidium@7010: SLE_CONDVAR(Vehicle, vehicle_flags, SLE_UINT8, 40, SL_MAX_VERSION), rubidium@7010: rubidium@7010: SLE_CONDVAR(Vehicle, profit_this_year, SLE_FILE_I32 | SLE_VAR_I64, 0, 64), rubidium@7010: SLE_CONDVAR(Vehicle, profit_this_year, SLE_INT64, 65, SL_MAX_VERSION), rubidium@7010: SLE_CONDVAR(Vehicle, profit_last_year, SLE_FILE_I32 | SLE_VAR_I64, 0, 64), rubidium@7010: SLE_CONDVAR(Vehicle, profit_last_year, SLE_INT64, 65, SL_MAX_VERSION), rubidium@7010: SLEG_CONDVAR( _cargo_feeder_share, SLE_FILE_I32 | SLE_VAR_I64,51, 64), rubidium@7010: SLEG_CONDVAR( _cargo_feeder_share, SLE_INT64, 65, 67), rubidium@7010: SLEG_CONDVAR( _cargo_loaded_at_xy, SLE_UINT32, 51, 67), rubidium@7010: SLE_CONDVAR(Vehicle, value, SLE_FILE_I32 | SLE_VAR_I64, 0, 64), rubidium@7010: SLE_CONDVAR(Vehicle, value, SLE_INT64, 65, SL_MAX_VERSION), rubidium@4344: rubidium@4344: SLE_VAR(Vehicle, random_bits, SLE_UINT8), rubidium@4344: SLE_VAR(Vehicle, waiting_triggers, SLE_UINT8), rubidium@4344: rubidium@4344: SLE_REF(Vehicle, next_shared, REF_VEHICLE), rubidium@4344: SLE_REF(Vehicle, prev_shared, REF_VEHICLE), truelight@1024: rubidium@6643: SLE_CONDVAR(Vehicle, group_id, SLE_UINT16, 60, SL_MAX_VERSION), rubidium@6643: maedhros@6980: SLE_CONDVAR(Vehicle, current_order_time, SLE_UINT32, 67, SL_MAX_VERSION), maedhros@6980: SLE_CONDVAR(Vehicle, lateness_counter, SLE_INT32, 67, SL_MAX_VERSION), maedhros@6980: belugas@6423: /* 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[] = { rubidium@6817: SLE_WRITEBYTE(Vehicle, type, VEH_TRAIN), rubidium@7493: SLE_VEH_INCLUDEX(), rubidium@6552: SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRail, crash_anim_pos), SLE_UINT16), rubidium@6552: SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRail, force_proceed), SLE_UINT8), rubidium@6552: SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRail, railtype), SLE_UINT8), rubidium@6552: SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRail, track), SLE_UINT8), rubidium@6552: rubidium@6552: SLE_CONDVARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRail, flags), SLE_UINT8, 2, SL_MAX_VERSION), rubidium@6573: SLE_CONDNULL(2, 2, 59), truelight@0: Darkvater@3222: SLE_CONDNULL(2, 2, 19), belugas@6423: /* 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[] = { rubidium@6817: SLE_WRITEBYTE(Vehicle, type, VEH_ROAD), rubidium@7493: SLE_VEH_INCLUDEX(), rubidium@6552: SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRoad, state), SLE_UINT8), rubidium@6552: SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRoad, frame), SLE_UINT8), rubidium@6552: SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRoad, blocked_ctr), SLE_UINT16), rubidium@6552: SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRoad, overtaking), SLE_UINT8), rubidium@6552: SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRoad, overtaking_ctr), SLE_UINT8), rubidium@6552: SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRoad, crashed_ctr), SLE_UINT16), rubidium@6552: SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRoad, reverse_ctr), SLE_UINT8), rubidium@6552: rubidium@6552: 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@6552: SLE_CONDVARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRoad, slot_age), SLE_UINT8, 6, SL_MAX_VERSION), belugas@6423: /* 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[] = { rubidium@6817: SLE_WRITEBYTE(Vehicle, type, VEH_SHIP), rubidium@7493: SLE_VEH_INCLUDEX(), rubidium@6552: SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleShip, state), SLE_UINT8), truelight@0: belugas@6423: /* 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[] = { rubidium@6817: SLE_WRITEBYTE(Vehicle, type, VEH_AIRCRAFT), rubidium@7493: SLE_VEH_INCLUDEX(), rubidium@6552: SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleAir, crashed_counter), SLE_UINT16), rubidium@6552: SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleAir, pos), SLE_UINT8), rubidium@6552: rubidium@6552: SLE_CONDVARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleAir, targetairport), SLE_FILE_U8 | SLE_VAR_U16, 0, 4), rubidium@6552: SLE_CONDVARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleAir, targetairport), SLE_UINT16, 5, SL_MAX_VERSION), rubidium@6552: rubidium@6552: SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleAir, state), SLE_UINT8), rubidium@6552: rubidium@6552: SLE_CONDVARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleAir, previous_pos), SLE_UINT8, 2, SL_MAX_VERSION), truelight@0: belugas@6423: /* 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[] = { rubidium@6817: SLE_WRITEBYTE(Vehicle, type, VEH_SPECIAL), rubidium@6817: 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@6561: SLE_CONDNULL(5, 0, 57), rubidium@4344: SLE_VAR(Vehicle, progress, SLE_UINT8), rubidium@4344: SLE_VAR(Vehicle, vehstatus, SLE_UINT8), rubidium@4344: rubidium@7334: SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleSpecial, animation_state), SLE_UINT16), rubidium@7334: SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleSpecial, animation_substate), SLE_UINT8), truelight@0: belugas@6423: /* 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[] = { rubidium@6817: SLE_WRITEBYTE(Vehicle, type, VEH_DISASTER), rubidium@6817: 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@6561: SLE_CONDNULL(5, 0, 57), rubidium@4344: SLE_VAR(Vehicle, owner, SLE_UINT8), rubidium@4344: SLE_VAR(Vehicle, vehstatus, SLE_UINT8), rubidium@6552: SLE_CONDVARX(cpp_offsetof(Vehicle, current_order) + cpp_offsetof(Order, dest), SLE_FILE_U8 | SLE_VAR_U16, 0, 4), rubidium@6552: 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@7334: SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleDisaster, image_override), SLE_UINT16), rubidium@7334: SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleDisaster, big_ufo_destroyer_target), SLE_UINT16), truelight@0: belugas@6423: /* 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@7398: 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@7493: _common_veh_desc, truelight@0: }; truelight@0: rubidium@7493: return _veh_descs[vt]; rubidium@7493: } rubidium@7493: belugas@6423: /** Will be called when the vehicles need to be saved. */ rubidium@6247: static void Save_VEHS() truelight@0: { truelight@0: Vehicle *v; belugas@6423: /* Write the vehicles */ truelight@0: FOR_ALL_VEHICLES(v) { truelight@4346: SlSetArrayIndex(v->index); rubidium@7493: SlObject(v, GetVehicleDescription(v->type)); truelight@0: } truelight@0: } truelight@0: belugas@6423: /** Will be called when vehicles need to be loaded. */ rubidium@6247: static void Load_VEHS() truelight@0: { truelight@0: int index; truelight@0: Vehicle *v; truelight@0: rubidium@7010: _cargo_count = 0; rubidium@7010: truelight@0: while ((index = SlIterateArray()) != -1) { truelight@1279: Vehicle *v; rubidium@6817: VehicleType vtype = (VehicleType)SlReadByte(); rubidium@7010: rubidium@7010: switch (vtype) { rubidium@7398: case VEH_TRAIN: v = new (index) Train(); break; rubidium@7398: case VEH_ROAD: v = new (index) RoadVehicle(); break; rubidium@7398: case VEH_SHIP: v = new (index) Ship(); break; rubidium@7398: case VEH_AIRCRAFT: v = new (index) Aircraft(); break; rubidium@7398: case VEH_SPECIAL: v = new (index) SpecialVehicle(); break; rubidium@7398: case VEH_DISASTER: v = new (index) DisasterVehicle(); break; rubidium@7398: case VEH_INVALID: v = new (index) InvalidVehicle(); break; rubidium@6621: default: NOT_REACHED(); rubidium@6552: } rubidium@6552: rubidium@7493: SlObject(v, GetVehicleDescription(vtype)); rubidium@7010: rubidium@7010: if (_cargo_count != 0 && IsPlayerBuildableVehicleType(v)) { rubidium@7010: /* Don't construct the packet with station here, because that'll fail with old savegames */ rubidium@7010: CargoPacket *cp = new CargoPacket(); rubidium@7010: cp->source = _cargo_source; rubidium@7010: cp->source_xy = _cargo_source_xy; rubidium@7010: cp->count = _cargo_count; rubidium@7010: cp->days_in_transit = _cargo_days; rubidium@7010: cp->feeder_share = _cargo_feeder_share; rubidium@7010: cp->loaded_at_xy = _cargo_loaded_at_xy; rubidium@7010: v->cargo.Append(cp); rubidium@7010: } rubidium@7010: 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 */ truelight@956: v->current_order.flags = (v->current_order.type & 0xF0) >> 4; rubidium@5587: v->current_order.type.m_val &= 0x0F; truelight@956: } rubidium@6643: rubidium@6643: /* Advanced vehicle lists got added */ rubidium@6643: 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@5587: extern const ChunkHandler _veh_chunk_handlers[] = { truelight@1542: { 'VEHS', Save_VEHS, Load_VEHS, CH_SPARSE_ARRAY | CH_LAST}, truelight@0: }; KUDr@5651: KUDr@5651: void Vehicle::BeginLoading() KUDr@5651: { rubidium@6259: assert(IsTileType(tile, MP_STATION) || type == VEH_SHIP); rubidium@6550: rubidium@6550: if (this->current_order.type == OT_GOTO_STATION && rubidium@6550: this->current_order.dest == this->last_station_visited) { rubidium@6550: /* Arriving at the ordered station. rubidium@6550: * Keep the load/unload flags, as we (obviously) still need them. */ rubidium@6550: this->current_order.flags &= OF_FULL_LOAD | OF_UNLOAD | OF_TRANSFER; rubidium@6550: rubidium@6550: /* Furthermore add the Non Stop flag to mark that this station rubidium@6550: * is the actual destination of the vehicle, which is (for example) rubidium@6550: * necessary to be known for HandleTrainLoading to determine rubidium@6550: * whether the train is lost or not; not marking a train lost rubidium@6550: * that arrives at random stations is bad. */ rubidium@6550: this->current_order.flags |= OF_NON_STOP; maedhros@6980: UpdateVehicleTimetable(this, true); rubidium@6550: } else { rubidium@6550: /* This is just an unordered intermediate stop */ rubidium@6550: this->current_order.flags = 0; rubidium@6550: } rubidium@6550: KUDr@5651: current_order.type = OT_LOADING; rubidium@6500: GetStation(this->last_station_visited)->loading_vehicles.push_back(this); rubidium@6553: rubidium@6562: SET_EXPENSES_TYPE(this->GetExpenseType(true)); rubidium@6565: VehiclePayment(this); rubidium@6565: rubidium@6565: InvalidateWindow(this->GetVehicleListWindowClass(), this->owner); rubidium@6553: InvalidateWindowWidget(WC_VEHICLE_VIEW, this->index, STATUS_BAR); rubidium@6565: InvalidateWindow(WC_VEHICLE_DETAILS, this->index); rubidium@6565: InvalidateWindow(WC_STATION_VIEW, this->last_station_visited); rubidium@6565: peter1138@6823: GetStation(this->last_station_visited)->MarkTilesDirty(true); rubidium@6565: this->MarkDirty(); KUDr@5651: } KUDr@5651: KUDr@5651: void Vehicle::LeaveStation() KUDr@5651: { KUDr@5651: assert(current_order.type == OT_LOADING); maedhros@7076: maedhros@7076: /* Only update the timetable if the vehicle was supposed to stop here. */ maedhros@7076: if (current_order.flags & OF_NON_STOP) UpdateVehicleTimetable(this, false); maedhros@7076: KUDr@5651: current_order.type = OT_LEAVESTATION; KUDr@5651: current_order.flags = 0; maedhros@6501: GetStation(this->last_station_visited)->loading_vehicles.remove(this); maedhros@6980: truelight@6998: HideFillingPercent(this->fill_percent_te_id); truelight@6998: this->fill_percent_te_id = INVALID_TE_ID; KUDr@5651: } rubidium@6558: rubidium@6558: rubidium@6594: void Vehicle::HandleLoading(bool mode) rubidium@6594: { rubidium@6594: switch (this->current_order.type) { rubidium@6594: case OT_LOADING: { maedhros@6980: uint wait_time = max(this->current_order.wait_time - this->lateness_counter, 0); maedhros@6980: rubidium@6616: /* Not the first call for this tick, or still loading */ maedhros@6980: if (mode || !HASBIT(this->vehicle_flags, VF_LOADING_FINISHED) || maedhros@6980: (_patches.timetabling && this->current_order_time < wait_time)) return; rubidium@6594: rubidium@6594: this->PlayLeaveStationSound(); rubidium@6594: rubidium@6594: Order b = this->current_order; rubidium@6594: this->LeaveStation(); rubidium@6594: rubidium@6594: /* If this was not the final order, don't remove it from the list. */ rubidium@6594: if (!(b.flags & OF_NON_STOP)) return; rubidium@6594: break; rubidium@6594: } rubidium@6594: rubidium@6594: case OT_DUMMY: break; rubidium@6594: rubidium@6594: default: return; rubidium@6594: } rubidium@6594: rubidium@6594: this->cur_order_index++; rubidium@6594: InvalidateVehicleOrder(this); rubidium@6594: } rubidium@6594: rubidium@7497: void Vehicle::SetNext(Vehicle *next) rubidium@7497: { rubidium@7497: if (this->next != NULL) { rubidium@7497: /* We had an old next vehicle. Update the first and previous pointers */ rubidium@7497: for (Vehicle *v = this->next; v != NULL; v = v->Next()) { rubidium@7497: v->first = this->next; rubidium@7497: } rubidium@7497: this->next->previous = NULL; rubidium@7497: } rubidium@7497: rubidium@7497: this->next = next; rubidium@7497: rubidium@7497: if (this->next != NULL) { rubidium@7497: /* A new next vehicle. Update the first and previous pointers */ rubidium@7497: if (this->next->previous != NULL) this->next->previous->next = NULL; rubidium@7497: this->next->previous = this; rubidium@7497: for (Vehicle *v = this->next; v != NULL; v = v->Next()) { rubidium@7497: v->first = this->first; rubidium@7497: } rubidium@7497: } rubidium@7497: } rubidium@7497: rubidium@6558: void SpecialVehicle::UpdateDeltaXY(Direction direction) rubidium@6558: { rubidium@6558: this->x_offs = 0; rubidium@6558: this->y_offs = 0; rubidium@6558: this->sprite_width = 1; rubidium@6558: this->sprite_height = 1; rubidium@6558: this->z_height = 1; rubidium@6558: }