articulated_vehicles.cpp

Go to the documentation of this file.
00001 /* $Id: articulated_vehicles.cpp 26972 2014-10-06 20:10:07Z frosch $ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
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 #include "newgrf.h"
00019 
00020 #include "table/strings.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   const Engine *front_engine = Engine::Get(front_type);
00037 
00038   uint16 callback = GetVehicleCallback(CBID_VEHICLE_ARTIC_ENGINE, index, 0, front_type, front);
00039   if (callback == CALLBACK_FAILED) return INVALID_ENGINE;
00040 
00041   if (front_engine->GetGRF()->grf_version < 8) {
00042     /* 8 bits, bit 7 for mirroring */
00043     callback = GB(callback, 0, 8);
00044     if (callback == 0xFF) return INVALID_ENGINE;
00045     if (mirrored != NULL) *mirrored = HasBit(callback, 7);
00046     callback = GB(callback, 0, 7);
00047   } else {
00048     /* 15 bits, bit 14 for mirroring */
00049     if (callback == 0x7FFF) return INVALID_ENGINE;
00050     if (mirrored != NULL) *mirrored = HasBit(callback, 14);
00051     callback = GB(callback, 0, 14);
00052   }
00053 
00054   return GetNewEngineID(front_engine->GetGRF(), front_engine->type, callback);
00055 }
00056 
00062 bool IsArticulatedEngine(EngineID engine_type)
00063 {
00064   return HasBit(EngInfo(engine_type)->callback_mask, CBM_VEHICLE_ARTIC_ENGINE);
00065 }
00066 
00073 uint CountArticulatedParts(EngineID engine_type, bool purchase_window)
00074 {
00075   if (!HasBit(EngInfo(engine_type)->callback_mask, CBM_VEHICLE_ARTIC_ENGINE)) return 0;
00076 
00077   /* If we can't allocate a vehicle now, we can't allocate it in the command
00078    * either, so it doesn't matter how many articulated parts there are. */
00079   if (!Vehicle::CanAllocateItem()) return 0;
00080 
00081   Vehicle *v = NULL;
00082   if (!purchase_window) {
00083     v = new Vehicle();
00084     v->engine_type = engine_type;
00085     v->owner = _current_company;
00086   }
00087 
00088   uint i;
00089   for (i = 1; i < MAX_ARTICULATED_PARTS; i++) {
00090     if (GetNextArticulatedPart(i, engine_type, v) == INVALID_ENGINE) break;
00091   }
00092 
00093   delete v;
00094 
00095   return i - 1;
00096 }
00097 
00098 
00105 static inline uint16 GetVehicleDefaultCapacity(EngineID engine, CargoID *cargo_type)
00106 {
00107   const Engine *e = Engine::Get(engine);
00108   CargoID cargo = (e->CanCarryCargo() ? e->GetDefaultCargoType() : (CargoID)CT_INVALID);
00109   if (cargo_type != NULL) *cargo_type = cargo;
00110   if (cargo == CT_INVALID) return 0;
00111   return e->GetDisplayDefaultCapacity();
00112 }
00113 
00120 static inline uint32 GetAvailableVehicleCargoTypes(EngineID engine, bool include_initial_cargo_type)
00121 {
00122   const Engine *e = Engine::Get(engine);
00123   if (!e->CanCarryCargo()) return 0;
00124 
00125   uint32 cargoes = e->info.refit_mask;
00126 
00127   if (include_initial_cargo_type) {
00128     SetBit(cargoes, e->GetDefaultCargoType());
00129   }
00130 
00131   return cargoes;
00132 }
00133 
00139 CargoArray GetCapacityOfArticulatedParts(EngineID engine)
00140 {
00141   CargoArray capacity;
00142   const Engine *e = Engine::Get(engine);
00143 
00144   CargoID cargo_type;
00145   uint16 cargo_capacity = GetVehicleDefaultCapacity(engine, &cargo_type);
00146   if (cargo_type < NUM_CARGO) capacity[cargo_type] = cargo_capacity;
00147 
00148   if (!e->IsGroundVehicle()) return capacity;
00149 
00150   if (!HasBit(e->info.callback_mask, CBM_VEHICLE_ARTIC_ENGINE)) return capacity;
00151 
00152   for (uint i = 1; i < MAX_ARTICULATED_PARTS; i++) {
00153     EngineID artic_engine = GetNextArticulatedPart(i, engine);
00154     if (artic_engine == INVALID_ENGINE) break;
00155 
00156     cargo_capacity = GetVehicleDefaultCapacity(artic_engine, &cargo_type);
00157     if (cargo_type < NUM_CARGO) capacity[cargo_type] += cargo_capacity;
00158   }
00159 
00160   return capacity;
00161 }
00162 
00170 void GetArticulatedVehicleCargoesAndRefits(EngineID engine, CargoArray *cargoes, uint32 *refits)
00171 {
00172   cargoes->Clear();
00173   *refits = 0;
00174 
00175   const Engine *e = Engine::Get(engine);
00176 
00177   CargoID cargo_type;
00178   uint16 cargo_capacity = GetVehicleDefaultCapacity(engine, &cargo_type);
00179   if (cargo_type < NUM_CARGO && cargo_capacity > 0) {
00180     (*cargoes)[cargo_type] += cargo_capacity;
00181     if (IsEngineRefittable(engine)) SetBit(*refits, cargo_type);
00182   }
00183 
00184   if (!e->IsGroundVehicle() || !HasBit(e->info.callback_mask, CBM_VEHICLE_ARTIC_ENGINE)) return;
00185 
00186   for (uint i = 1; i < MAX_ARTICULATED_PARTS; i++) {
00187     EngineID artic_engine = GetNextArticulatedPart(i, engine);
00188     if (artic_engine == INVALID_ENGINE) break;
00189 
00190     cargo_capacity = GetVehicleDefaultCapacity(artic_engine, &cargo_type);
00191     if (cargo_type < NUM_CARGO && cargo_capacity > 0) {
00192       (*cargoes)[cargo_type] += cargo_capacity;
00193       if (IsEngineRefittable(artic_engine)) SetBit(*refits, cargo_type);
00194     }
00195   }
00196 }
00197 
00203 bool IsArticulatedVehicleRefittable(EngineID engine)
00204 {
00205   if (IsEngineRefittable(engine)) return true;
00206 
00207   const Engine *e = Engine::Get(engine);
00208   if (!e->IsGroundVehicle()) return false;
00209 
00210   if (!HasBit(e->info.callback_mask, CBM_VEHICLE_ARTIC_ENGINE)) return false;
00211 
00212   for (uint i = 1; i < MAX_ARTICULATED_PARTS; i++) {
00213     EngineID artic_engine = GetNextArticulatedPart(i, engine);
00214     if (artic_engine == INVALID_ENGINE) break;
00215 
00216     if (IsEngineRefittable(artic_engine)) return true;
00217   }
00218 
00219   return false;
00220 }
00221 
00229 void GetArticulatedRefitMasks(EngineID engine, bool include_initial_cargo_type, uint32 *union_mask, uint32 *intersection_mask)
00230 {
00231   const Engine *e = Engine::Get(engine);
00232   uint32 veh_cargoes = GetAvailableVehicleCargoTypes(engine, include_initial_cargo_type);
00233   *union_mask = veh_cargoes;
00234   *intersection_mask = (veh_cargoes != 0) ? veh_cargoes : UINT32_MAX;
00235 
00236   if (!e->IsGroundVehicle()) return;
00237   if (!HasBit(e->info.callback_mask, CBM_VEHICLE_ARTIC_ENGINE)) return;
00238 
00239   for (uint i = 1; i < MAX_ARTICULATED_PARTS; i++) {
00240     EngineID artic_engine = GetNextArticulatedPart(i, engine);
00241     if (artic_engine == INVALID_ENGINE) break;
00242 
00243     veh_cargoes = GetAvailableVehicleCargoTypes(artic_engine, include_initial_cargo_type);
00244     *union_mask |= veh_cargoes;
00245     if (veh_cargoes != 0) *intersection_mask &= veh_cargoes;
00246   }
00247 }
00248 
00255 uint32 GetUnionOfArticulatedRefitMasks(EngineID engine, bool include_initial_cargo_type)
00256 {
00257   uint32 union_mask, intersection_mask;
00258   GetArticulatedRefitMasks(engine, include_initial_cargo_type, &union_mask, &intersection_mask);
00259   return union_mask;
00260 }
00261 
00268 uint32 GetIntersectionOfArticulatedRefitMasks(EngineID engine, bool include_initial_cargo_type)
00269 {
00270   uint32 union_mask, intersection_mask;
00271   GetArticulatedRefitMasks(engine, include_initial_cargo_type, &union_mask, &intersection_mask);
00272   return intersection_mask;
00273 }
00274 
00275 
00283 bool IsArticulatedVehicleCarryingDifferentCargoes(const Vehicle *v, CargoID *cargo_type)
00284 {
00285   CargoID first_cargo = CT_INVALID;
00286 
00287   do {
00288     if (v->cargo_type != CT_INVALID && v->GetEngine()->CanCarryCargo()) {
00289       if (first_cargo == CT_INVALID) first_cargo = v->cargo_type;
00290       if (first_cargo != v->cargo_type) {
00291         if (cargo_type != NULL) *cargo_type = CT_INVALID;
00292         return true;
00293       }
00294     }
00295 
00296     v = v->HasArticulatedPart() ? v->GetNextArticulatedPart() : NULL;
00297   } while (v != NULL);
00298 
00299   if (cargo_type != NULL) *cargo_type = first_cargo;
00300   return false;
00301 }
00302 
00311 void CheckConsistencyOfArticulatedVehicle(const Vehicle *v)
00312 {
00313   const Engine *engine = v->GetEngine();
00314 
00315   uint32 purchase_refit_union, purchase_refit_intersection;
00316   GetArticulatedRefitMasks(v->engine_type, true, &purchase_refit_union, &purchase_refit_intersection);
00317   CargoArray purchase_default_capacity = GetCapacityOfArticulatedParts(v->engine_type);
00318 
00319   uint32 real_refit_union = 0;
00320   uint32 real_refit_intersection = UINT_MAX;
00321   CargoArray real_default_capacity;
00322 
00323   do {
00324     uint32 refit_mask = GetAvailableVehicleCargoTypes(v->engine_type, true);
00325     real_refit_union |= refit_mask;
00326     if (refit_mask != 0) real_refit_intersection &= refit_mask;
00327 
00328     assert(v->cargo_type < NUM_CARGO);
00329     real_default_capacity[v->cargo_type] += v->cargo_cap;
00330 
00331     v = v->HasArticulatedPart() ? v->GetNextArticulatedPart() : NULL;
00332   } while (v != NULL);
00333 
00334   /* Check whether the vehicle carries more cargoes than expected */
00335   bool carries_more = false;
00336   for (CargoID cid = 0; cid < NUM_CARGO; cid++) {
00337     if (real_default_capacity[cid] != 0 && purchase_default_capacity[cid] == 0) {
00338       carries_more = true;
00339       break;
00340     }
00341   }
00342 
00343   /* show a warning once for each GRF after each game load */
00344   if (real_refit_union != purchase_refit_union || real_refit_intersection != purchase_refit_intersection || carries_more) {
00345     ShowNewGrfVehicleError(engine->index, STR_NEWGRF_BUGGY, STR_NEWGRF_BUGGY_ARTICULATED_CARGO, GBUG_VEH_REFIT, false);
00346   }
00347 }
00348 
00353 void AddArticulatedParts(Vehicle *first)
00354 {
00355   VehicleType type = first->type;
00356   if (!HasBit(EngInfo(first->engine_type)->callback_mask, CBM_VEHICLE_ARTIC_ENGINE)) return;
00357 
00358   Vehicle *v = first;
00359   for (uint i = 1; i < MAX_ARTICULATED_PARTS; i++) {
00360     bool flip_image;
00361     EngineID engine_type = GetNextArticulatedPart(i, first->engine_type, first, &flip_image);
00362     if (engine_type == INVALID_ENGINE) return;
00363 
00364     /* In the (very rare) case the GRF reported wrong number of articulated parts
00365      * and we run out of available vehicles, bail out. */
00366     if (!Vehicle::CanAllocateItem()) return;
00367 
00368     GroundVehicleCache *gcache = v->GetGroundVehicleCache();
00369     gcache->first_engine = v->engine_type; // Needs to be set before first callback
00370 
00371     const Engine *e_artic = Engine::Get(engine_type);
00372     switch (type) {
00373       default: NOT_REACHED();
00374 
00375       case VEH_TRAIN: {
00376         Train *front = Train::From(first);
00377         Train *t = new Train();
00378         v->SetNext(t);
00379         v = t;
00380 
00381         t->subtype = 0;
00382         t->track = front->track;
00383         t->railtype = front->railtype;
00384 
00385         t->spritenum = e_artic->u.rail.image_index;
00386         if (e_artic->CanCarryCargo()) {
00387           t->cargo_type = e_artic->GetDefaultCargoType();
00388           t->cargo_cap = e_artic->u.rail.capacity;  // Callback 36 is called when the consist is finished
00389         } else {
00390           t->cargo_type = front->cargo_type; // Needed for livery selection
00391           t->cargo_cap = 0;
00392         }
00393         t->refit_cap = 0;
00394 
00395         t->SetArticulatedPart();
00396         break;
00397       }
00398 
00399       case VEH_ROAD: {
00400         RoadVehicle *front = RoadVehicle::From(first);
00401         RoadVehicle *rv = new RoadVehicle();
00402         v->SetNext(rv);
00403         v = rv;
00404 
00405         rv->subtype = 0;
00406         gcache->cached_veh_length = VEHICLE_LENGTH; // Callback is called when the consist is finished
00407         rv->state = RVSB_IN_DEPOT;
00408 
00409         rv->roadtype = front->roadtype;
00410         rv->compatible_roadtypes = front->compatible_roadtypes;
00411 
00412         rv->spritenum = e_artic->u.road.image_index;
00413         if (e_artic->CanCarryCargo()) {
00414           rv->cargo_type = e_artic->GetDefaultCargoType();
00415           rv->cargo_cap = e_artic->u.road.capacity;  // Callback 36 is called when the consist is finished
00416         } else {
00417           rv->cargo_type = front->cargo_type; // Needed for livery selection
00418           rv->cargo_cap = 0;
00419         }
00420         rv->refit_cap = 0;
00421 
00422         rv->SetArticulatedPart();
00423         break;
00424       }
00425     }
00426 
00427     /* get common values from first engine */
00428     v->direction = first->direction;
00429     v->owner = first->owner;
00430     v->tile = first->tile;
00431     v->x_pos = first->x_pos;
00432     v->y_pos = first->y_pos;
00433     v->z_pos = first->z_pos;
00434     v->build_year = first->build_year;
00435     v->vehstatus = first->vehstatus & ~VS_STOPPED;
00436 
00437     v->cargo_subtype = 0;
00438     v->max_age = 0;
00439     v->engine_type = engine_type;
00440     v->value = 0;
00441     v->cur_image = SPR_IMG_QUERY;
00442     v->random_bits = VehicleRandomBits();
00443 
00444     if (flip_image) v->spritenum++;
00445 
00446     VehicleUpdatePosition(v);
00447   }
00448 }