(svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.

This commit is contained in:
frosch
2008-07-27 15:17:16 +00:00
parent 62c053729a
commit 1f31e117f0
4 changed files with 232 additions and 71 deletions

View File

@@ -35,19 +35,78 @@ uint CountArticulatedParts(EngineID engine_type, bool purchase_window)
}
/**
* Returns the default (non-refitted) capacity of a specific EngineID.
* @param engine the EngineID of iterest
* @param type the type of the engine
* @param cargo_type returns the default cargo type, if needed
* @return capacity
*/
static inline uint16 GetVehicleDefaultCapacity(EngineID engine, VehicleType type, CargoID *cargo_type)
{
switch (type) {
case VEH_TRAIN: {
const RailVehicleInfo *rvi = RailVehInfo(engine);
if (cargo_type != NULL) *cargo_type = rvi->cargo_type;
return GetEngineProperty(engine, 0x14, rvi->capacity) + (rvi->railveh_type == RAILVEH_MULTIHEAD ? rvi->capacity : 0);
}
case VEH_ROAD: {
const RoadVehicleInfo *rvi = RoadVehInfo(engine);
if (cargo_type != NULL) *cargo_type = rvi->cargo_type;
return GetEngineProperty(engine, 0x0F, rvi->capacity);
}
case VEH_SHIP: {
const ShipVehicleInfo *svi = ShipVehInfo(engine);
if (cargo_type != NULL) *cargo_type = svi->cargo_type;
return GetEngineProperty(engine, 0x0D, svi->capacity);
}
case VEH_AIRCRAFT: {
const AircraftVehicleInfo *avi = AircraftVehInfo(engine);
if (cargo_type != NULL) *cargo_type = CT_PASSENGERS;
return avi->passenger_capacity;
}
default: NOT_REACHED();
}
}
/**
* Returns all cargos a vehicle can carry.
* @param engine the EngineID of iterest
* @param type the type of the engine
* @param include_initial_cargo_type if true the default cargo type of the vehicle is included; if false only the refit_mask
* @return bit set of CargoIDs
*/
static inline uint32 GetAvailableVehicleCargoTypes(EngineID engine, VehicleType type, bool include_initial_cargo_type)
{
uint32 cargos = 0;
CargoID initial_cargo_type;
if (GetVehicleDefaultCapacity(engine, type, &initial_cargo_type) > 0) {
if (type != VEH_SHIP || ShipVehInfo(engine)->refittable) {
const EngineInfo *ei = EngInfo(engine);
cargos = ei->refit_mask;
}
if (include_initial_cargo_type && initial_cargo_type < NUM_CARGO) SetBit(cargos, initial_cargo_type);
}
return cargos;
}
uint16 *GetCapacityOfArticulatedParts(EngineID engine, VehicleType type)
{
static uint16 capacity[NUM_CARGO];
memset(capacity, 0, sizeof(capacity));
if (type == VEH_TRAIN) {
const RailVehicleInfo *rvi = RailVehInfo(engine);
capacity[rvi->cargo_type] = GetEngineProperty(engine, 0x14, rvi->capacity);
if (rvi->railveh_type == RAILVEH_MULTIHEAD) capacity[rvi->cargo_type] += rvi->capacity;
} else if (type == VEH_ROAD) {
const RoadVehicleInfo *rvi = RoadVehInfo(engine);
capacity[rvi->cargo_type] = GetEngineProperty(engine, 0x0F, rvi->capacity);
}
CargoID cargo_type;
uint16 cargo_capacity = GetVehicleDefaultCapacity(engine, type, &cargo_type);
if (cargo_type < NUM_CARGO) capacity[cargo_type] = cargo_capacity;
if (type != VEH_TRAIN && type != VEH_ROAD) return capacity;
if (!HasBit(EngInfo(engine)->callbackmask, CBM_VEHICLE_ARTIC_ENGINE)) return capacity;
@@ -57,18 +116,111 @@ uint16 *GetCapacityOfArticulatedParts(EngineID engine, VehicleType type)
EngineID artic_engine = GetNewEngineID(GetEngineGRF(engine), type, GB(callback, 0, 7));
if (type == VEH_TRAIN) {
const RailVehicleInfo *rvi = RailVehInfo(artic_engine);
capacity[rvi->cargo_type] += GetEngineProperty(artic_engine, 0x14, rvi->capacity);
} else if (type == VEH_ROAD) {
const RoadVehicleInfo *rvi = RoadVehInfo(artic_engine);
capacity[rvi->cargo_type] += GetEngineProperty(artic_engine, 0x0F, rvi->capacity);
}
cargo_capacity = GetVehicleDefaultCapacity(artic_engine, type, &cargo_type);
if (cargo_type < NUM_CARGO) capacity[cargo_type] += cargo_capacity;
}
return capacity;
}
/**
* Ors the refit_masks of all articulated parts.
* Note: Vehicles with a default capacity of zero are ignored.
* @param engine the first part
* @param type the vehicle type
* @param include_initial_cargo_type if true the default cargo type of the vehicle is included; if false only the refit_mask
* @return bit mask of CargoIDs which are a refit option for at least one articulated part
*/
uint32 GetUnionOfArticulatedRefitMasks(EngineID engine, VehicleType type, bool include_initial_cargo_type)
{
uint32 cargos = GetAvailableVehicleCargoTypes(engine, type, include_initial_cargo_type);
if (type != VEH_TRAIN && type != VEH_ROAD) return cargos;
if (!HasBit(EngInfo(engine)->callbackmask, CBM_VEHICLE_ARTIC_ENGINE)) return cargos;
for (uint i = 1; i < MAX_ARTICULATED_PARTS; i++) {
uint16 callback = GetVehicleCallback(CBID_VEHICLE_ARTIC_ENGINE, i, 0, engine, NULL);
if (callback == CALLBACK_FAILED || GB(callback, 0, 8) == 0xFF) break;
EngineID artic_engine = GetNewEngineID(GetEngineGRF(engine), type, GB(callback, 0, 7));
cargos |= GetAvailableVehicleCargoTypes(artic_engine, type, include_initial_cargo_type);
}
return cargos;
}
/**
* Ands the refit_masks of all articulated parts.
* Note: Vehicles with a default capacity of zero are ignored.
* @param engine the first part
* @param type the vehicle type
* @param include_initial_cargo_type if true the default cargo type of the vehicle is included; if false only the refit_mask
* @return bit mask of CargoIDs which are a refit option for every articulated part (with default capacity > 0)
*/
uint32 GetIntersectionOfArticulatedRefitMasks(EngineID engine, VehicleType type, bool include_initial_cargo_type)
{
uint32 cargos = UINT32_MAX;
uint32 veh_cargos = GetAvailableVehicleCargoTypes(engine, type, include_initial_cargo_type);
if (veh_cargos != 0) cargos &= veh_cargos;
if (type != VEH_TRAIN && type != VEH_ROAD) return cargos;
if (!HasBit(EngInfo(engine)->callbackmask, CBM_VEHICLE_ARTIC_ENGINE)) return cargos;
for (uint i = 1; i < MAX_ARTICULATED_PARTS; i++) {
uint16 callback = GetVehicleCallback(CBID_VEHICLE_ARTIC_ENGINE, i, 0, engine, NULL);
if (callback == CALLBACK_FAILED || GB(callback, 0, 8) == 0xFF) break;
EngineID artic_engine = GetNewEngineID(GetEngineGRF(engine), type, GB(callback, 0, 7));
veh_cargos = GetAvailableVehicleCargoTypes(artic_engine, type, include_initial_cargo_type);
if (veh_cargos != 0) cargos &= veh_cargos;
}
return cargos;
}
/**
* Tests if all parts of an articulated vehicle are refitted to the same cargo.
* Note: Vehicles not carrying anything are ignored
* @param v the first vehicle in the chain
* @param cargo_type returns the common CargoID if needed. (CT_INVALID if no part is carrying something or they are carrying different things)
* @return true if some parts are carrying different cargos, false if all parts are carrying the same (nothing is also the same)
*/
bool IsArticulatedVehicleCarryingDifferentCargos(const Vehicle *v, CargoID *cargo_type)
{
CargoID first_cargo = CT_INVALID;
do {
if (v->cargo_cap > 0 && v->cargo_type != CT_INVALID) {
if (first_cargo == CT_INVALID) first_cargo = v->cargo_type;
if (first_cargo != v->cargo_type) {
if (cargo_type != NULL) *cargo_type = CT_INVALID;
return true;
}
}
switch (v->type) {
case VEH_TRAIN:
v = (EngineHasArticPart(v) ? GetNextArticPart(v) : NULL);
break;
case VEH_ROAD:
v = (RoadVehHasArticPart(v) ? v->Next() : NULL);
break;
default:
v = NULL;
break;
}
} while (v != NULL);
if (cargo_type != NULL) *cargo_type = first_cargo;
return false;
}
void AddArticulatedParts(Vehicle **vl, VehicleType type)
{