tron@2186: /* $Id$ */ tron@2186: rubidium@9111: /** @file articulated_vehicles.cpp Implementation of articulated vehicles. */ belugas@6422: truelight@0: #include "stdafx.h" Darkvater@1891: #include "openttd.h" maedhros@6772: #include "articulated_vehicles.h" bjarni@2676: #include "train.h" maedhros@6857: #include "roadveh.h" peter1138@2982: #include "newgrf_callbacks.h" peter1138@2962: #include "newgrf_engine.h" rubidium@8144: #include "vehicle_func.h" truelight@0: frosch@9723: static const uint MAX_ARTICULATED_PARTS = 100; ///< Maximum of articulated parts per vehicle, i.e. when to abort calling the articulated vehicle callback. frosch@9723: rubidium@7595: uint CountArticulatedParts(EngineID engine_type, bool purchase_window) peter1138@2602: { skidd13@7928: if (!HasBit(EngInfo(engine_type)->callbackmask, CBM_VEHICLE_ARTIC_ENGINE)) return 0; tron@6150: rubidium@7595: Vehicle *v = NULL;; rubidium@7595: if (!purchase_window) { rubidium@7595: v = new InvalidVehicle(); rubidium@7595: v->engine_type = engine_type; rubidium@7595: } rubidium@7595: peter1138@2602: uint i; frosch@9723: for (i = 1; i < MAX_ARTICULATED_PARTS; i++) { rubidium@7595: uint16 callback = GetVehicleCallback(CBID_VEHICLE_ARTIC_ENGINE, i, 0, engine_type, v); rubidium@9020: if (callback == CALLBACK_FAILED || GB(callback, 0, 8) == 0xFF) break; peter1138@2602: } peter1138@2602: rubidium@7595: delete v; rubidium@7595: peter1138@2608: return i - 1; peter1138@2602: } peter1138@2602: peter1138@8559: frosch@9725: /** frosch@9725: * Returns the default (non-refitted) capacity of a specific EngineID. frosch@9725: * @param engine the EngineID of iterest frosch@9725: * @param type the type of the engine frosch@9725: * @param cargo_type returns the default cargo type, if needed frosch@9725: * @return capacity frosch@9725: */ frosch@9725: static inline uint16 GetVehicleDefaultCapacity(EngineID engine, VehicleType type, CargoID *cargo_type) frosch@9725: { frosch@9725: switch (type) { frosch@9725: case VEH_TRAIN: { frosch@9725: const RailVehicleInfo *rvi = RailVehInfo(engine); frosch@9725: if (cargo_type != NULL) *cargo_type = rvi->cargo_type; frosch@9725: return GetEngineProperty(engine, 0x14, rvi->capacity) + (rvi->railveh_type == RAILVEH_MULTIHEAD ? rvi->capacity : 0); frosch@9725: } frosch@9725: frosch@9725: case VEH_ROAD: { frosch@9725: const RoadVehicleInfo *rvi = RoadVehInfo(engine); frosch@9725: if (cargo_type != NULL) *cargo_type = rvi->cargo_type; frosch@9725: return GetEngineProperty(engine, 0x0F, rvi->capacity); frosch@9725: } frosch@9725: frosch@9725: case VEH_SHIP: { frosch@9725: const ShipVehicleInfo *svi = ShipVehInfo(engine); frosch@9725: if (cargo_type != NULL) *cargo_type = svi->cargo_type; frosch@9725: return GetEngineProperty(engine, 0x0D, svi->capacity); frosch@9725: } frosch@9725: frosch@9725: case VEH_AIRCRAFT: { frosch@9725: const AircraftVehicleInfo *avi = AircraftVehInfo(engine); frosch@9725: if (cargo_type != NULL) *cargo_type = CT_PASSENGERS; frosch@9725: return avi->passenger_capacity; frosch@9725: } frosch@9725: frosch@9725: default: NOT_REACHED(); frosch@9725: } frosch@9725: frosch@9725: } frosch@9725: frosch@9725: /** frosch@9725: * Returns all cargos a vehicle can carry. frosch@9725: * @param engine the EngineID of iterest frosch@9725: * @param type the type of the engine frosch@9725: * @param include_initial_cargo_type if true the default cargo type of the vehicle is included; if false only the refit_mask frosch@9725: * @return bit set of CargoIDs frosch@9725: */ frosch@9725: static inline uint32 GetAvailableVehicleCargoTypes(EngineID engine, VehicleType type, bool include_initial_cargo_type) frosch@9725: { frosch@9725: uint32 cargos = 0; frosch@9725: CargoID initial_cargo_type; frosch@9725: frosch@9725: if (GetVehicleDefaultCapacity(engine, type, &initial_cargo_type) > 0) { frosch@9725: if (type != VEH_SHIP || ShipVehInfo(engine)->refittable) { frosch@9725: const EngineInfo *ei = EngInfo(engine); frosch@9725: cargos = ei->refit_mask; frosch@9725: } frosch@9725: if (include_initial_cargo_type && initial_cargo_type < NUM_CARGO) SetBit(cargos, initial_cargo_type); frosch@9725: } frosch@9725: frosch@9725: return cargos; frosch@9725: } frosch@9725: peter1138@8559: uint16 *GetCapacityOfArticulatedParts(EngineID engine, VehicleType type) peter1138@8559: { peter1138@8559: static uint16 capacity[NUM_CARGO]; peter1138@8559: memset(capacity, 0, sizeof(capacity)); peter1138@8559: frosch@9725: CargoID cargo_type; frosch@9725: uint16 cargo_capacity = GetVehicleDefaultCapacity(engine, type, &cargo_type); frosch@9725: if (cargo_type < NUM_CARGO) capacity[cargo_type] = cargo_capacity; frosch@9725: frosch@9725: if (type != VEH_TRAIN && type != VEH_ROAD) return capacity; peter1138@8559: peter1138@8559: if (!HasBit(EngInfo(engine)->callbackmask, CBM_VEHICLE_ARTIC_ENGINE)) return capacity; peter1138@8559: frosch@9723: for (uint i = 1; i < MAX_ARTICULATED_PARTS; i++) { peter1138@8559: uint16 callback = GetVehicleCallback(CBID_VEHICLE_ARTIC_ENGINE, i, 0, engine, NULL); rubidium@9020: if (callback == CALLBACK_FAILED || GB(callback, 0, 8) == 0xFF) break; peter1138@8559: peter1138@9070: EngineID artic_engine = GetNewEngineID(GetEngineGRF(engine), type, GB(callback, 0, 7)); peter1138@8559: frosch@9725: cargo_capacity = GetVehicleDefaultCapacity(artic_engine, type, &cargo_type); frosch@9725: if (cargo_type < NUM_CARGO) capacity[cargo_type] += cargo_capacity; peter1138@8559: } peter1138@8559: peter1138@8559: return capacity; peter1138@8559: } peter1138@8559: frosch@9725: /** frosch@9725: * Ors the refit_masks of all articulated parts. frosch@9725: * Note: Vehicles with a default capacity of zero are ignored. frosch@9725: * @param engine the first part frosch@9725: * @param type the vehicle type frosch@9725: * @param include_initial_cargo_type if true the default cargo type of the vehicle is included; if false only the refit_mask frosch@9725: * @return bit mask of CargoIDs which are a refit option for at least one articulated part frosch@9725: */ frosch@9725: uint32 GetUnionOfArticulatedRefitMasks(EngineID engine, VehicleType type, bool include_initial_cargo_type) frosch@9725: { frosch@9725: uint32 cargos = GetAvailableVehicleCargoTypes(engine, type, include_initial_cargo_type); frosch@9725: frosch@9725: if (type != VEH_TRAIN && type != VEH_ROAD) return cargos; frosch@9725: frosch@9725: if (!HasBit(EngInfo(engine)->callbackmask, CBM_VEHICLE_ARTIC_ENGINE)) return cargos; frosch@9725: frosch@9725: for (uint i = 1; i < MAX_ARTICULATED_PARTS; i++) { frosch@9725: uint16 callback = GetVehicleCallback(CBID_VEHICLE_ARTIC_ENGINE, i, 0, engine, NULL); frosch@9725: if (callback == CALLBACK_FAILED || GB(callback, 0, 8) == 0xFF) break; frosch@9725: frosch@9725: EngineID artic_engine = GetNewEngineID(GetEngineGRF(engine), type, GB(callback, 0, 7)); frosch@9725: cargos |= GetAvailableVehicleCargoTypes(artic_engine, type, include_initial_cargo_type); frosch@9725: } frosch@9725: frosch@9725: return cargos; frosch@9725: } frosch@9725: frosch@9725: /** frosch@9725: * Ands the refit_masks of all articulated parts. frosch@9725: * Note: Vehicles with a default capacity of zero are ignored. frosch@9725: * @param engine the first part frosch@9725: * @param type the vehicle type frosch@9725: * @param include_initial_cargo_type if true the default cargo type of the vehicle is included; if false only the refit_mask frosch@9725: * @return bit mask of CargoIDs which are a refit option for every articulated part (with default capacity > 0) frosch@9725: */ frosch@9725: uint32 GetIntersectionOfArticulatedRefitMasks(EngineID engine, VehicleType type, bool include_initial_cargo_type) frosch@9725: { frosch@9725: uint32 cargos = UINT32_MAX; frosch@9725: frosch@9725: uint32 veh_cargos = GetAvailableVehicleCargoTypes(engine, type, include_initial_cargo_type); frosch@9725: if (veh_cargos != 0) cargos &= veh_cargos; frosch@9725: frosch@9725: if (type != VEH_TRAIN && type != VEH_ROAD) return cargos; frosch@9725: frosch@9725: if (!HasBit(EngInfo(engine)->callbackmask, CBM_VEHICLE_ARTIC_ENGINE)) return cargos; frosch@9725: frosch@9725: for (uint i = 1; i < MAX_ARTICULATED_PARTS; i++) { frosch@9725: uint16 callback = GetVehicleCallback(CBID_VEHICLE_ARTIC_ENGINE, i, 0, engine, NULL); frosch@9725: if (callback == CALLBACK_FAILED || GB(callback, 0, 8) == 0xFF) break; frosch@9725: frosch@9725: EngineID artic_engine = GetNewEngineID(GetEngineGRF(engine), type, GB(callback, 0, 7)); frosch@9725: veh_cargos = GetAvailableVehicleCargoTypes(artic_engine, type, include_initial_cargo_type); frosch@9725: if (veh_cargos != 0) cargos &= veh_cargos; frosch@9725: } frosch@9725: frosch@9725: return cargos; frosch@9725: } frosch@9725: frosch@9725: frosch@9725: /** frosch@9725: * Tests if all parts of an articulated vehicle are refitted to the same cargo. frosch@9725: * Note: Vehicles not carrying anything are ignored frosch@9725: * @param v the first vehicle in the chain frosch@9725: * @param cargo_type returns the common CargoID if needed. (CT_INVALID if no part is carrying something or they are carrying different things) frosch@9725: * @return true if some parts are carrying different cargos, false if all parts are carrying the same (nothing is also the same) frosch@9725: */ frosch@9725: bool IsArticulatedVehicleCarryingDifferentCargos(const Vehicle *v, CargoID *cargo_type) frosch@9725: { frosch@9725: CargoID first_cargo = CT_INVALID; frosch@9725: frosch@9725: do { frosch@9725: if (v->cargo_cap > 0 && v->cargo_type != CT_INVALID) { frosch@9725: if (first_cargo == CT_INVALID) first_cargo = v->cargo_type; frosch@9725: if (first_cargo != v->cargo_type) { frosch@9725: if (cargo_type != NULL) *cargo_type = CT_INVALID; frosch@9725: return true; frosch@9725: } frosch@9725: } frosch@9725: frosch@9725: switch (v->type) { frosch@9725: case VEH_TRAIN: frosch@9725: v = (EngineHasArticPart(v) ? GetNextArticPart(v) : NULL); frosch@9725: break; frosch@9725: frosch@9725: case VEH_ROAD: frosch@9725: v = (RoadVehHasArticPart(v) ? v->Next() : NULL); frosch@9725: break; frosch@9725: frosch@9725: default: frosch@9725: v = NULL; frosch@9725: break; frosch@9725: } frosch@9725: } while (v != NULL); frosch@9725: frosch@9725: if (cargo_type != NULL) *cargo_type = first_cargo; frosch@9725: return false; frosch@9725: } frosch@9725: peter1138@8559: maedhros@6857: void AddArticulatedParts(Vehicle **vl, VehicleType type) peter1138@2602: { tron@6150: const Vehicle *v = vl[0]; tron@6150: Vehicle *u = vl[0]; peter1138@2602: skidd13@7928: if (!HasBit(EngInfo(v->engine_type)->callbackmask, CBM_VEHICLE_ARTIC_ENGINE)) return; peter1138@2602: frosch@9723: for (uint i = 1; i < MAX_ARTICULATED_PARTS; i++) { rubidium@7215: uint16 callback = GetVehicleCallback(CBID_VEHICLE_ARTIC_ENGINE, i, 0, v->engine_type, v); rubidium@9020: if (callback == CALLBACK_FAILED || GB(callback, 0, 8) == 0xFF) return; peter1138@2602: peter1138@4831: /* Attempt to use pre-allocated vehicles until they run out. This can happen peter1138@4831: * if the callback returns different values depending on the cargo type. */ rubidium@7492: u->SetNext(vl[i]); rubidium@7492: if (u->Next() == NULL) return; peter1138@4831: rubidium@7518: Vehicle *previous = u; rubidium@7492: u = u->Next(); peter1138@2602: peter1138@9070: EngineID engine_type = GetNewEngineID(GetEngineGRF(v->engine_type), type, GB(callback, 0, 7)); skidd13@7928: bool flip_image = HasBit(callback, 7); peter1138@2602: belugas@6422: /* get common values from first engine */ peter1138@2602: u->direction = v->direction; peter1138@2602: u->owner = v->owner; peter1138@2602: u->tile = v->tile; peter1138@2602: u->x_pos = v->x_pos; peter1138@2602: u->y_pos = v->y_pos; peter1138@2602: u->z_pos = v->z_pos; peter1138@2602: u->build_year = v->build_year; peter1138@2602: u->vehstatus = v->vehstatus & ~VS_STOPPED; peter1138@2602: peter1138@3870: u->cargo_subtype = 0; peter1138@2602: u->max_speed = 0; peter1138@2602: u->max_age = 0; peter1138@2602: u->engine_type = engine_type; peter1138@2602: u->value = 0; bjarni@2676: u->subtype = 0; peter1138@2602: u->cur_image = 0xAC2; peter1138@2804: u->random_bits = VehicleRandomBits(); peter1138@2602: maedhros@6857: switch (type) { maedhros@6857: default: NOT_REACHED(); maedhros@6857: maedhros@6857: case VEH_TRAIN: { maedhros@6857: const RailVehicleInfo *rvi_artic = RailVehInfo(engine_type); maedhros@6857: maedhros@6857: u = new (u) Train(); rubidium@7528: previous->SetNext(u); maedhros@6857: u->u.rail.track = v->u.rail.track; maedhros@6857: u->u.rail.railtype = v->u.rail.railtype; maedhros@6857: u->u.rail.first_engine = v->engine_type; maedhros@6857: maedhros@6857: u->spritenum = rvi_artic->image_index; maedhros@6857: u->cargo_type = rvi_artic->cargo_type; maedhros@6857: u->cargo_cap = rvi_artic->capacity; maedhros@6857: maedhros@6857: SetArticulatedPart(u); maedhros@6857: } break; maedhros@6857: maedhros@6857: case VEH_ROAD: { maedhros@6857: const RoadVehicleInfo *rvi_artic = RoadVehInfo(engine_type); maedhros@6857: maedhros@6857: u = new (u) RoadVehicle(); rubidium@7528: previous->SetNext(u); maedhros@6857: u->u.road.first_engine = v->engine_type; maedhros@6857: u->u.road.cached_veh_length = GetRoadVehLength(u); maedhros@6857: u->u.road.state = RVSB_IN_DEPOT; maedhros@6857: maedhros@6857: u->u.road.roadtype = v->u.road.roadtype; maedhros@6857: u->u.road.compatible_roadtypes = v->u.road.compatible_roadtypes; maedhros@6857: maedhros@6857: u->spritenum = rvi_artic->image_index; maedhros@6857: u->cargo_type = rvi_artic->cargo_type; maedhros@6857: u->cargo_cap = rvi_artic->capacity; maedhros@6857: maedhros@6857: SetRoadVehArticPart(u); maedhros@6857: } break; maedhros@6857: } maedhros@6857: maedhros@6857: if (flip_image) u->spritenum++; maedhros@6857: peter1138@2602: VehiclePositionChanged(u); peter1138@2602: } peter1138@2602: }