00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "stdafx.h"
00013 #include "train.h"
00014 #include "roadveh.h"
00015 #include "vehicle_func.h"
00016 #include "engine_func.h"
00017 #include "company_func.h"
00018
00019 #include "table/strings.h"
00020 #include "table/sprites.h"
00021
00022 static const uint MAX_ARTICULATED_PARTS = 100;
00023
00032 static EngineID GetNextArticulatedPart(uint index, EngineID front_type, Vehicle *front = NULL, bool *mirrored = NULL)
00033 {
00034 assert(front == NULL || front->engine_type == front_type);
00035
00036 uint16 callback = GetVehicleCallback(CBID_VEHICLE_ARTIC_ENGINE, index, 0, front_type, front);
00037 if (callback == CALLBACK_FAILED || GB(callback, 0, 8) == 0xFF) return INVALID_ENGINE;
00038
00039 if (mirrored != NULL) *mirrored = HasBit(callback, 7);
00040 return GetNewEngineID(GetEngineGRF(front_type), Engine::Get(front_type)->type, GB(callback, 0, 7));
00041 }
00042
00043 uint CountArticulatedParts(EngineID engine_type, bool purchase_window)
00044 {
00045 if (!HasBit(EngInfo(engine_type)->callback_mask, CBM_VEHICLE_ARTIC_ENGINE)) return 0;
00046
00047
00048
00049 if (!Vehicle::CanAllocateItem()) return 0;
00050
00051 Vehicle *v = NULL;
00052 if (!purchase_window) {
00053 v = new Vehicle();
00054 v->engine_type = engine_type;
00055 v->owner = _current_company;
00056 }
00057
00058 uint i;
00059 for (i = 1; i < MAX_ARTICULATED_PARTS; i++) {
00060 if (GetNextArticulatedPart(i, engine_type, v) == INVALID_ENGINE) break;
00061 }
00062
00063 delete v;
00064
00065 return i - 1;
00066 }
00067
00068
00075 static inline uint16 GetVehicleDefaultCapacity(EngineID engine, CargoID *cargo_type)
00076 {
00077 const Engine *e = Engine::Get(engine);
00078 CargoID cargo = (e->CanCarryCargo() ? e->GetDefaultCargoType() : (CargoID)CT_INVALID);
00079 if (cargo_type != NULL) *cargo_type = cargo;
00080 if (cargo == CT_INVALID) return 0;
00081 return e->GetDisplayDefaultCapacity();
00082 }
00083
00090 static inline uint32 GetAvailableVehicleCargoTypes(EngineID engine, bool include_initial_cargo_type)
00091 {
00092 uint32 cargos = 0;
00093 CargoID initial_cargo_type;
00094
00095 if (GetVehicleDefaultCapacity(engine, &initial_cargo_type) > 0) {
00096 const EngineInfo *ei = EngInfo(engine);
00097 cargos = ei->refit_mask;
00098 if (include_initial_cargo_type && initial_cargo_type < NUM_CARGO) SetBit(cargos, initial_cargo_type);
00099 }
00100
00101 return cargos;
00102 }
00103
00104 CargoArray GetCapacityOfArticulatedParts(EngineID engine)
00105 {
00106 CargoArray capacity;
00107 const Engine *e = Engine::Get(engine);
00108
00109 CargoID cargo_type;
00110 uint16 cargo_capacity = GetVehicleDefaultCapacity(engine, &cargo_type);
00111 if (cargo_type < NUM_CARGO) capacity[cargo_type] = cargo_capacity;
00112
00113 if (!e->IsGroundVehicle()) return capacity;
00114
00115 if (!HasBit(e->info.callback_mask, CBM_VEHICLE_ARTIC_ENGINE)) return capacity;
00116
00117 for (uint i = 1; i < MAX_ARTICULATED_PARTS; i++) {
00118 EngineID artic_engine = GetNextArticulatedPart(i, engine);
00119 if (artic_engine == INVALID_ENGINE) break;
00120
00121 cargo_capacity = GetVehicleDefaultCapacity(artic_engine, &cargo_type);
00122 if (cargo_type < NUM_CARGO) capacity[cargo_type] += cargo_capacity;
00123 }
00124
00125 return capacity;
00126 }
00127
00133 bool IsArticulatedVehicleRefittable(EngineID engine)
00134 {
00135 if (IsEngineRefittable(engine)) return true;
00136
00137 const Engine *e = Engine::Get(engine);
00138 if (!e->IsGroundVehicle()) return false;
00139
00140 if (!HasBit(e->info.callback_mask, CBM_VEHICLE_ARTIC_ENGINE)) return false;
00141
00142 for (uint i = 1; i < MAX_ARTICULATED_PARTS; i++) {
00143 EngineID artic_engine = GetNextArticulatedPart(i, engine);
00144 if (artic_engine == INVALID_ENGINE) break;
00145
00146 if (IsEngineRefittable(artic_engine)) return true;
00147 }
00148
00149 return false;
00150 }
00151
00159 void GetArticulatedRefitMasks(EngineID engine, bool include_initial_cargo_type, uint32 *union_mask, uint32 *intersection_mask)
00160 {
00161 const Engine *e = Engine::Get(engine);
00162 uint32 veh_cargos = GetAvailableVehicleCargoTypes(engine, include_initial_cargo_type);
00163 *union_mask = veh_cargos;
00164 *intersection_mask = (veh_cargos != 0) ? veh_cargos : UINT32_MAX;
00165
00166 if (!e->IsGroundVehicle()) return;
00167 if (!HasBit(e->info.callback_mask, CBM_VEHICLE_ARTIC_ENGINE)) return;
00168
00169 for (uint i = 1; i < MAX_ARTICULATED_PARTS; i++) {
00170 EngineID artic_engine = GetNextArticulatedPart(i, engine);
00171 if (artic_engine == INVALID_ENGINE) break;
00172
00173 veh_cargos = GetAvailableVehicleCargoTypes(artic_engine, include_initial_cargo_type);
00174 *union_mask |= veh_cargos;
00175 if (veh_cargos != 0) *intersection_mask &= veh_cargos;
00176 }
00177 }
00178
00185 uint32 GetUnionOfArticulatedRefitMasks(EngineID engine, bool include_initial_cargo_type)
00186 {
00187 uint32 union_mask, intersection_mask;
00188 GetArticulatedRefitMasks(engine, include_initial_cargo_type, &union_mask, &intersection_mask);
00189 return union_mask;
00190 }
00191
00198 uint32 GetIntersectionOfArticulatedRefitMasks(EngineID engine, bool include_initial_cargo_type)
00199 {
00200 uint32 union_mask, intersection_mask;
00201 GetArticulatedRefitMasks(engine, include_initial_cargo_type, &union_mask, &intersection_mask);
00202 return intersection_mask;
00203 }
00204
00205
00213 bool IsArticulatedVehicleCarryingDifferentCargos(const Vehicle *v, CargoID *cargo_type)
00214 {
00215 CargoID first_cargo = CT_INVALID;
00216
00217 do {
00218 if (v->cargo_cap > 0 && v->cargo_type != CT_INVALID) {
00219 if (first_cargo == CT_INVALID) first_cargo = v->cargo_type;
00220 if (first_cargo != v->cargo_type) {
00221 if (cargo_type != NULL) *cargo_type = CT_INVALID;
00222 return true;
00223 }
00224 }
00225
00226 v = v->HasArticulatedPart() ? v->GetNextArticulatedPart() : NULL;
00227 } while (v != NULL);
00228
00229 if (cargo_type != NULL) *cargo_type = first_cargo;
00230 return false;
00231 }
00232
00241 void CheckConsistencyOfArticulatedVehicle(const Vehicle *v)
00242 {
00243 const Engine *engine = Engine::Get(v->engine_type);
00244
00245 uint32 purchase_refit_union, purchase_refit_intersection;
00246 GetArticulatedRefitMasks(v->engine_type, true, &purchase_refit_union, &purchase_refit_intersection);
00247 CargoArray purchase_default_capacity = GetCapacityOfArticulatedParts(v->engine_type);
00248
00249 uint32 real_refit_union = 0;
00250 uint32 real_refit_intersection = UINT_MAX;
00251 CargoArray real_default_capacity;
00252
00253 do {
00254 uint32 refit_mask = GetAvailableVehicleCargoTypes(v->engine_type, true);
00255 real_refit_union |= refit_mask;
00256 if (refit_mask != 0) real_refit_intersection &= refit_mask;
00257
00258 assert(v->cargo_type < NUM_CARGO);
00259 real_default_capacity[v->cargo_type] += v->cargo_cap;
00260
00261 v = v->HasArticulatedPart() ? v->GetNextArticulatedPart() : NULL;
00262 } while (v != NULL);
00263
00264
00265 bool carries_more = false;
00266 for (CargoID cid = 0; cid < NUM_CARGO; cid++) {
00267 if (real_default_capacity[cid] != 0 && purchase_default_capacity[cid] == 0) {
00268 carries_more = true;
00269 break;
00270 }
00271 }
00272
00273
00274 if (real_refit_union != purchase_refit_union || real_refit_intersection != purchase_refit_intersection || carries_more) {
00275 ShowNewGrfVehicleError(engine->index, STR_NEWGRF_BUGGY, STR_NEWGRF_BUGGY_ARTICULATED_CARGO, GBUG_VEH_REFIT, false);
00276 }
00277 }
00278
00279 void AddArticulatedParts(Vehicle *first)
00280 {
00281 VehicleType type = first->type;
00282 if (!HasBit(EngInfo(first->engine_type)->callback_mask, CBM_VEHICLE_ARTIC_ENGINE)) return;
00283
00284 Vehicle *v = first;
00285 for (uint i = 1; i < MAX_ARTICULATED_PARTS; i++) {
00286 bool flip_image;
00287 EngineID engine_type = GetNextArticulatedPart(i, first->engine_type, first, &flip_image);
00288 if (engine_type == INVALID_ENGINE) return;
00289
00290
00291
00292 if (!Vehicle::CanAllocateItem()) return;
00293
00294 GroundVehicleCache *gcache = v->GetGroundVehicleCache();
00295 gcache->first_engine = v->engine_type;
00296
00297 const Engine *e_artic = Engine::Get(engine_type);
00298 switch (type) {
00299 default: NOT_REACHED();
00300
00301 case VEH_TRAIN: {
00302 Train *front = Train::From(first);
00303 Train *t = new Train();
00304 v->SetNext(t);
00305 v = t;
00306
00307 t->subtype = 0;
00308 t->track = front->track;
00309 t->railtype = front->railtype;
00310
00311 t->spritenum = e_artic->u.rail.image_index;
00312 if (e_artic->CanCarryCargo()) {
00313 t->cargo_type = e_artic->GetDefaultCargoType();
00314 t->cargo_cap = e_artic->u.rail.capacity;
00315 } else {
00316 t->cargo_type = front->cargo_type;
00317 t->cargo_cap = 0;
00318 }
00319
00320 t->SetArticulatedPart();
00321 break;
00322 }
00323
00324 case VEH_ROAD: {
00325 RoadVehicle *front = RoadVehicle::From(first);
00326 RoadVehicle *rv = new RoadVehicle();
00327 v->SetNext(rv);
00328 v = rv;
00329
00330 rv->subtype = 0;
00331 gcache->cached_veh_length = VEHICLE_LENGTH;
00332 rv->state = RVSB_IN_DEPOT;
00333
00334 rv->roadtype = front->roadtype;
00335 rv->compatible_roadtypes = front->compatible_roadtypes;
00336
00337 rv->spritenum = e_artic->u.road.image_index;
00338 if (e_artic->CanCarryCargo()) {
00339 rv->cargo_type = e_artic->GetDefaultCargoType();
00340 rv->cargo_cap = e_artic->u.road.capacity;
00341 } else {
00342 rv->cargo_type = front->cargo_type;
00343 rv->cargo_cap = 0;
00344 }
00345
00346 rv->SetArticulatedPart();
00347 break;
00348 }
00349 }
00350
00351
00352 v->direction = first->direction;
00353 v->owner = first->owner;
00354 v->tile = first->tile;
00355 v->x_pos = first->x_pos;
00356 v->y_pos = first->y_pos;
00357 v->z_pos = first->z_pos;
00358 v->build_year = first->build_year;
00359 v->vehstatus = first->vehstatus & ~VS_STOPPED;
00360
00361 v->cargo_subtype = 0;
00362 v->max_age = 0;
00363 v->engine_type = engine_type;
00364 v->value = 0;
00365 v->cur_image = SPR_IMG_QUERY;
00366 v->random_bits = VehicleRandomBits();
00367
00368 if (flip_image) v->spritenum++;
00369
00370 VehicleMove(v, false);
00371 }
00372 }