2007-06-01 11:41:02 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
/** @file articulated_vehicles.cpp Implementation of articulated vehicles. */
|
2007-06-01 11:41:02 +00:00
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "train.h"
|
2007-06-11 14:00:16 +00:00
|
|
|
#include "roadveh.h"
|
2009-02-21 12:52:41 +00:00
|
|
|
#include "aircraft.h"
|
2007-06-01 11:41:02 +00:00
|
|
|
#include "newgrf_engine.h"
|
2007-12-27 13:35:39 +00:00
|
|
|
#include "vehicle_func.h"
|
2007-06-01 11:41:02 +00:00
|
|
|
|
2008-07-27 12:35:14 +00:00
|
|
|
static const uint MAX_ARTICULATED_PARTS = 100; ///< Maximum of articulated parts per vehicle, i.e. when to abort calling the articulated vehicle callback.
|
|
|
|
|
2007-09-17 04:23:03 +00:00
|
|
|
uint CountArticulatedParts(EngineID engine_type, bool purchase_window)
|
2007-06-01 11:41:02 +00:00
|
|
|
{
|
2007-11-19 21:02:30 +00:00
|
|
|
if (!HasBit(EngInfo(engine_type)->callbackmask, CBM_VEHICLE_ARTIC_ENGINE)) return 0;
|
2007-06-01 11:41:02 +00:00
|
|
|
|
2009-01-09 14:59:02 +00:00
|
|
|
/* If we can't allocate a vehicle now, we can't allocate it in the command
|
|
|
|
* either, so it doesn't matter how many articulated parts there are. */
|
|
|
|
if (!Vehicle::CanAllocateItem()) return 0;
|
|
|
|
|
2007-09-17 04:23:03 +00:00
|
|
|
Vehicle *v = NULL;;
|
|
|
|
if (!purchase_window) {
|
|
|
|
v = new InvalidVehicle();
|
|
|
|
v->engine_type = engine_type;
|
|
|
|
}
|
|
|
|
|
2007-06-01 11:41:02 +00:00
|
|
|
uint i;
|
2008-07-27 12:35:14 +00:00
|
|
|
for (i = 1; i < MAX_ARTICULATED_PARTS; i++) {
|
2007-09-17 04:23:03 +00:00
|
|
|
uint16 callback = GetVehicleCallback(CBID_VEHICLE_ARTIC_ENGINE, i, 0, engine_type, v);
|
2008-04-21 14:33:33 +00:00
|
|
|
if (callback == CALLBACK_FAILED || GB(callback, 0, 8) == 0xFF) break;
|
2007-06-01 11:41:02 +00:00
|
|
|
}
|
|
|
|
|
2007-09-17 04:23:03 +00:00
|
|
|
delete v;
|
|
|
|
|
2007-06-01 11:41:02 +00:00
|
|
|
return i - 1;
|
|
|
|
}
|
|
|
|
|
2008-02-14 07:25:24 +00:00
|
|
|
|
2008-07-27 15:17:16 +00:00
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
{
|
2009-02-21 12:52:41 +00:00
|
|
|
const Engine *e = GetEngine(engine);
|
|
|
|
CargoID cargo = (e->CanCarryCargo() ? e->GetDefaultCargoType() : (CargoID)CT_INVALID);
|
|
|
|
if (cargo_type != NULL) *cargo_type = cargo;
|
|
|
|
if (cargo == CT_INVALID) return 0;
|
2008-07-27 15:17:16 +00:00
|
|
|
switch (type) {
|
2009-02-21 12:52:41 +00:00
|
|
|
case VEH_TRAIN:
|
|
|
|
return GetEngineProperty(engine, 0x14, e->u.rail.capacity) + (e->u.rail.railveh_type == RAILVEH_MULTIHEAD ? e->u.rail.capacity : 0);
|
2008-07-27 15:17:16 +00:00
|
|
|
|
2009-02-21 12:52:41 +00:00
|
|
|
case VEH_ROAD:
|
|
|
|
return GetEngineProperty(engine, 0x0F, e->u.road.capacity);
|
2008-07-27 15:17:16 +00:00
|
|
|
|
2009-02-21 12:52:41 +00:00
|
|
|
case VEH_SHIP:
|
|
|
|
return GetEngineProperty(engine, 0x0D, e->u.ship.capacity);
|
2008-07-27 15:17:16 +00:00
|
|
|
|
2009-02-21 12:52:41 +00:00
|
|
|
case VEH_AIRCRAFT:
|
|
|
|
return AircraftDefaultCargoCapacity(cargo, &e->u.air);
|
2008-07-27 15:17:16 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2008-02-14 07:25:24 +00:00
|
|
|
uint16 *GetCapacityOfArticulatedParts(EngineID engine, VehicleType type)
|
|
|
|
{
|
|
|
|
static uint16 capacity[NUM_CARGO];
|
|
|
|
memset(capacity, 0, sizeof(capacity));
|
|
|
|
|
2008-07-27 15:17:16 +00:00
|
|
|
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;
|
2008-02-14 07:25:24 +00:00
|
|
|
|
|
|
|
if (!HasBit(EngInfo(engine)->callbackmask, CBM_VEHICLE_ARTIC_ENGINE)) return capacity;
|
|
|
|
|
2008-07-27 12:35:14 +00:00
|
|
|
for (uint i = 1; i < MAX_ARTICULATED_PARTS; i++) {
|
2008-02-14 07:25:24 +00:00
|
|
|
uint16 callback = GetVehicleCallback(CBID_VEHICLE_ARTIC_ENGINE, i, 0, engine, NULL);
|
2008-04-21 14:33:33 +00:00
|
|
|
if (callback == CALLBACK_FAILED || GB(callback, 0, 8) == 0xFF) break;
|
2008-02-14 07:25:24 +00:00
|
|
|
|
2008-04-29 21:31:29 +00:00
|
|
|
EngineID artic_engine = GetNewEngineID(GetEngineGRF(engine), type, GB(callback, 0, 7));
|
2008-02-14 07:25:24 +00:00
|
|
|
|
2008-07-27 15:17:16 +00:00
|
|
|
cargo_capacity = GetVehicleDefaultCapacity(artic_engine, type, &cargo_type);
|
|
|
|
if (cargo_type < NUM_CARGO) capacity[cargo_type] += cargo_capacity;
|
2008-02-14 07:25:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return capacity;
|
|
|
|
}
|
|
|
|
|
2008-07-27 15:17:16 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2008-02-14 07:25:24 +00:00
|
|
|
|
2007-06-11 14:00:16 +00:00
|
|
|
void AddArticulatedParts(Vehicle **vl, VehicleType type)
|
2007-06-01 11:41:02 +00:00
|
|
|
{
|
|
|
|
const Vehicle *v = vl[0];
|
|
|
|
Vehicle *u = vl[0];
|
|
|
|
|
2007-11-19 21:02:30 +00:00
|
|
|
if (!HasBit(EngInfo(v->engine_type)->callbackmask, CBM_VEHICLE_ARTIC_ENGINE)) return;
|
2007-06-01 11:41:02 +00:00
|
|
|
|
2008-07-27 12:35:14 +00:00
|
|
|
for (uint i = 1; i < MAX_ARTICULATED_PARTS; i++) {
|
2007-07-09 19:38:12 +00:00
|
|
|
uint16 callback = GetVehicleCallback(CBID_VEHICLE_ARTIC_ENGINE, i, 0, v->engine_type, v);
|
2008-04-21 14:33:33 +00:00
|
|
|
if (callback == CALLBACK_FAILED || GB(callback, 0, 8) == 0xFF) return;
|
2007-06-01 11:41:02 +00:00
|
|
|
|
|
|
|
/* Attempt to use pre-allocated vehicles until they run out. This can happen
|
|
|
|
* if the callback returns different values depending on the cargo type. */
|
2007-08-30 13:03:56 +00:00
|
|
|
u->SetNext(vl[i]);
|
|
|
|
if (u->Next() == NULL) return;
|
2007-06-01 11:41:02 +00:00
|
|
|
|
2007-09-01 20:09:26 +00:00
|
|
|
Vehicle *previous = u;
|
2007-08-30 13:03:56 +00:00
|
|
|
u = u->Next();
|
2007-06-01 11:41:02 +00:00
|
|
|
|
2008-04-29 21:31:29 +00:00
|
|
|
EngineID engine_type = GetNewEngineID(GetEngineGRF(v->engine_type), type, GB(callback, 0, 7));
|
2007-11-19 21:02:30 +00:00
|
|
|
bool flip_image = HasBit(callback, 7);
|
2007-06-01 11:41:02 +00:00
|
|
|
|
|
|
|
/* get common values from first engine */
|
|
|
|
u->direction = v->direction;
|
|
|
|
u->owner = v->owner;
|
|
|
|
u->tile = v->tile;
|
|
|
|
u->x_pos = v->x_pos;
|
|
|
|
u->y_pos = v->y_pos;
|
|
|
|
u->z_pos = v->z_pos;
|
|
|
|
u->build_year = v->build_year;
|
|
|
|
u->vehstatus = v->vehstatus & ~VS_STOPPED;
|
|
|
|
|
|
|
|
u->cargo_subtype = 0;
|
|
|
|
u->max_speed = 0;
|
|
|
|
u->max_age = 0;
|
|
|
|
u->engine_type = engine_type;
|
|
|
|
u->value = 0;
|
|
|
|
u->subtype = 0;
|
|
|
|
u->cur_image = 0xAC2;
|
|
|
|
u->random_bits = VehicleRandomBits();
|
|
|
|
|
2009-02-21 12:52:41 +00:00
|
|
|
const Engine *e_artic = GetEngine(engine_type);
|
2007-06-11 14:00:16 +00:00
|
|
|
switch (type) {
|
|
|
|
default: NOT_REACHED();
|
|
|
|
|
2009-02-21 12:52:41 +00:00
|
|
|
case VEH_TRAIN:
|
2007-06-11 14:00:16 +00:00
|
|
|
u = new (u) Train();
|
2007-09-05 17:53:14 +00:00
|
|
|
previous->SetNext(u);
|
2007-06-11 14:00:16 +00:00
|
|
|
u->u.rail.track = v->u.rail.track;
|
|
|
|
u->u.rail.railtype = v->u.rail.railtype;
|
|
|
|
u->u.rail.first_engine = v->engine_type;
|
|
|
|
|
2009-02-21 12:52:41 +00:00
|
|
|
u->spritenum = e_artic->u.rail.image_index;
|
|
|
|
if (e_artic->CanCarryCargo()) {
|
|
|
|
u->cargo_type = e_artic->GetDefaultCargoType();
|
|
|
|
u->cargo_cap = e_artic->u.rail.capacity; // Callback 36 is called when the consist is finished
|
|
|
|
} else {
|
|
|
|
u->cargo_type = v->cargo_type; // Needed for livery selection
|
|
|
|
u->cargo_cap = 0;
|
|
|
|
}
|
2007-06-11 14:00:16 +00:00
|
|
|
|
|
|
|
SetArticulatedPart(u);
|
2009-02-21 12:52:41 +00:00
|
|
|
break;
|
2007-06-11 14:00:16 +00:00
|
|
|
|
2009-02-21 12:52:41 +00:00
|
|
|
case VEH_ROAD:
|
2007-06-11 14:00:16 +00:00
|
|
|
u = new (u) RoadVehicle();
|
2007-09-05 17:53:14 +00:00
|
|
|
previous->SetNext(u);
|
2007-06-11 14:00:16 +00:00
|
|
|
u->u.road.first_engine = v->engine_type;
|
|
|
|
u->u.road.cached_veh_length = GetRoadVehLength(u);
|
|
|
|
u->u.road.state = RVSB_IN_DEPOT;
|
|
|
|
|
|
|
|
u->u.road.roadtype = v->u.road.roadtype;
|
|
|
|
u->u.road.compatible_roadtypes = v->u.road.compatible_roadtypes;
|
|
|
|
|
2009-02-21 12:52:41 +00:00
|
|
|
u->spritenum = e_artic->u.road.image_index;
|
|
|
|
if (e_artic->CanCarryCargo()) {
|
|
|
|
u->cargo_type = e_artic->GetDefaultCargoType();
|
|
|
|
u->cargo_cap = e_artic->u.road.capacity; // Callback 36 is called when the consist is finished
|
|
|
|
} else {
|
|
|
|
u->cargo_type = v->cargo_type; // Needed for livery selection
|
|
|
|
u->cargo_cap = 0;
|
|
|
|
}
|
2007-06-11 14:00:16 +00:00
|
|
|
|
|
|
|
SetRoadVehArticPart(u);
|
2009-02-21 12:52:41 +00:00
|
|
|
break;
|
2007-06-11 14:00:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (flip_image) u->spritenum++;
|
|
|
|
|
2007-06-01 11:41:02 +00:00
|
|
|
VehiclePositionChanged(u);
|
|
|
|
}
|
|
|
|
}
|