articulated_vehicles.cpp

Go to the documentation of this file.
00001 /* $Id$ */
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 
00063 uint CountArticulatedParts(EngineID engine_type, bool purchase_window)
00064 {
00065   if (!HasBit(EngInfo(engine_type)->callback_mask, CBM_VEHICLE_ARTIC_ENGINE)) return 0;
00066 
00067   /* If we can't allocate a vehicle now, we can't allocate it in the command
00068    * either, so it doesn't matter how many articulated parts there are. */
00069   if (!Vehicle::CanAllocateItem()) return 0;
00070 
00071   Vehicle *v = NULL;
00072   if (!purchase_window) {
00073     v = new Vehicle();
00074     v->engine_type = engine_type;
00075     v->owner = _current_company;
00076   }
00077 
00078   uint i;
00079   for (i = 1; i < MAX_ARTICULATED_PARTS; i++) {
00080     if (GetNextArticulatedPart(i, engine_type, v) == INVALID_ENGINE) break;
00081   }
00082 
00083   delete v;
00084 
00085   return i - 1;
00086 }
00087 
00088 
00095 static inline uint16 GetVehicleDefaultCapacity(EngineID engine, CargoID *cargo_type)
00096 {
00097   const Engine *e = Engine::Get(engine);
00098   CargoID cargo = (e->CanCarryCargo() ? e->GetDefaultCargoType() : (CargoID)CT_INVALID);
00099   if (cargo_type != NULL) *cargo_type = cargo;
00100   if (cargo == CT_INVALID) return 0;
00101   return e->GetDisplayDefaultCapacity();
00102 }
00103 
00110 static inline uint32 GetAvailableVehicleCargoTypes(EngineID engine, bool include_initial_cargo_type)
00111 {
00112   const Engine *e = Engine::Get(engine);
00113   if (!e->CanCarryCargo()) return 0;
00114 
00115   uint32 cargoes = e->info.refit_mask;
00116 
00117   if (include_initial_cargo_type) {
00118     SetBit(cargoes, e->GetDefaultCargoType());
00119   }
00120 
00121   return cargoes;
00122 }
00123 
00129 CargoArray GetCapacityOfArticulatedParts(EngineID engine)
00130 {
00131   CargoArray capacity;
00132   const Engine *e = Engine::Get(engine);
00133 
00134   CargoID cargo_type;
00135   uint16 cargo_capacity = GetVehicleDefaultCapacity(engine, &cargo_type);
00136   if (cargo_type < NUM_CARGO) capacity[cargo_type] = cargo_capacity;
00137 
00138   if (!e->IsGroundVehicle()) return capacity;
00139 
00140   if (!HasBit(e->info.callback_mask, CBM_VEHICLE_ARTIC_ENGINE)) return capacity;
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     cargo_capacity = GetVehicleDefaultCapacity(artic_engine, &cargo_type);
00147     if (cargo_type < NUM_CARGO) capacity[cargo_type] += cargo_capacity;
00148   }
00149 
00150   return capacity;
00151 }
00152 
00158 bool IsArticulatedVehicleRefittable(EngineID engine)
00159 {
00160   if (IsEngineRefittable(engine)) return true;
00161 
00162   const Engine *e = Engine::Get(engine);
00163   if (!e->IsGroundVehicle()) return false;
00164 
00165   if (!HasBit(e->info.callback_mask, CBM_VEHICLE_ARTIC_ENGINE)) return false;
00166 
00167   for (uint i = 1; i < MAX_ARTICULATED_PARTS; i++) {
00168     EngineID artic_engine = GetNextArticulatedPart(i, engine);
00169     if (artic_engine == INVALID_ENGINE) break;
00170 
00171     if (IsEngineRefittable(artic_engine)) return true;
00172   }
00173 
00174   return false;
00175 }
00176 
00184 void GetArticulatedRefitMasks(EngineID engine, bool include_initial_cargo_type, uint32 *union_mask, uint32 *intersection_mask)
00185 {
00186   const Engine *e = Engine::Get(engine);
00187   uint32 veh_cargoes = GetAvailableVehicleCargoTypes(engine, include_initial_cargo_type);
00188   *union_mask = veh_cargoes;
00189   *intersection_mask = (veh_cargoes != 0) ? veh_cargoes : UINT32_MAX;
00190 
00191   if (!e->IsGroundVehicle()) return;
00192   if (!HasBit(e->info.callback_mask, CBM_VEHICLE_ARTIC_ENGINE)) return;
00193 
00194   for (uint i = 1; i < MAX_ARTICULATED_PARTS; i++) {
00195     EngineID artic_engine = GetNextArticulatedPart(i, engine);
00196     if (artic_engine == INVALID_ENGINE) break;
00197 
00198     veh_cargoes = GetAvailableVehicleCargoTypes(artic_engine, include_initial_cargo_type);
00199     *union_mask |= veh_cargoes;
00200     if (veh_cargoes != 0) *intersection_mask &= veh_cargoes;
00201   }
00202 }
00203 
00210 uint32 GetUnionOfArticulatedRefitMasks(EngineID engine, bool include_initial_cargo_type)
00211 {
00212   uint32 union_mask, intersection_mask;
00213   GetArticulatedRefitMasks(engine, include_initial_cargo_type, &union_mask, &intersection_mask);
00214   return union_mask;
00215 }
00216 
00223 uint32 GetIntersectionOfArticulatedRefitMasks(EngineID engine, bool include_initial_cargo_type)
00224 {
00225   uint32 union_mask, intersection_mask;
00226   GetArticulatedRefitMasks(engine, include_initial_cargo_type, &union_mask, &intersection_mask);
00227   return intersection_mask;
00228 }
00229 
00230 
00238 bool IsArticulatedVehicleCarryingDifferentCargoes(const Vehicle *v, CargoID *cargo_type)
00239 {
00240   CargoID first_cargo = CT_INVALID;
00241 
00242   do {
00243     if (v->cargo_type != CT_INVALID && v->GetEngine()->CanCarryCargo()) {
00244       if (first_cargo == CT_INVALID) first_cargo = v->cargo_type;
00245       if (first_cargo != v->cargo_type) {
00246         if (cargo_type != NULL) *cargo_type = CT_INVALID;
00247         return true;
00248       }
00249     }
00250 
00251     v = v->HasArticulatedPart() ? v->GetNextArticulatedPart() : NULL;
00252   } while (v != NULL);
00253 
00254   if (cargo_type != NULL) *cargo_type = first_cargo;
00255   return false;
00256 }
00257 
00266 void CheckConsistencyOfArticulatedVehicle(const Vehicle *v)
00267 {
00268   const Engine *engine = v->GetEngine();
00269 
00270   uint32 purchase_refit_union, purchase_refit_intersection;
00271   GetArticulatedRefitMasks(v->engine_type, true, &purchase_refit_union, &purchase_refit_intersection);
00272   CargoArray purchase_default_capacity = GetCapacityOfArticulatedParts(v->engine_type);
00273 
00274   uint32 real_refit_union = 0;
00275   uint32 real_refit_intersection = UINT_MAX;
00276   CargoArray real_default_capacity;
00277 
00278   do {
00279     uint32 refit_mask = GetAvailableVehicleCargoTypes(v->engine_type, true);
00280     real_refit_union |= refit_mask;
00281     if (refit_mask != 0) real_refit_intersection &= refit_mask;
00282 
00283     assert(v->cargo_type < NUM_CARGO);
00284     real_default_capacity[v->cargo_type] += v->cargo_cap;
00285 
00286     v = v->HasArticulatedPart() ? v->GetNextArticulatedPart() : NULL;
00287   } while (v != NULL);
00288 
00289   /* Check whether the vehicle carries more cargoes than expected */
00290   bool carries_more = false;
00291   for (CargoID cid = 0; cid < NUM_CARGO; cid++) {
00292     if (real_default_capacity[cid] != 0 && purchase_default_capacity[cid] == 0) {
00293       carries_more = true;
00294       break;
00295     }
00296   }
00297 
00298   /* show a warning once for each GRF after each game load */
00299   if (real_refit_union != purchase_refit_union || real_refit_intersection != purchase_refit_intersection || carries_more) {
00300     ShowNewGrfVehicleError(engine->index, STR_NEWGRF_BUGGY, STR_NEWGRF_BUGGY_ARTICULATED_CARGO, GBUG_VEH_REFIT, false);
00301   }
00302 }
00303 
00308 void AddArticulatedParts(Vehicle *first)
00309 {
00310   VehicleType type = first->type;
00311   if (!HasBit(EngInfo(first->engine_type)->callback_mask, CBM_VEHICLE_ARTIC_ENGINE)) return;
00312 
00313   Vehicle *v = first;
00314   for (uint i = 1; i < MAX_ARTICULATED_PARTS; i++) {
00315     bool flip_image;
00316     EngineID engine_type = GetNextArticulatedPart(i, first->engine_type, first, &flip_image);
00317     if (engine_type == INVALID_ENGINE) return;
00318 
00319     /* In the (very rare) case the GRF reported wrong number of articulated parts
00320      * and we run out of available vehicles, bail out. */
00321     if (!Vehicle::CanAllocateItem()) return;
00322 
00323     GroundVehicleCache *gcache = v->GetGroundVehicleCache();
00324     gcache->first_engine = v->engine_type; // Needs to be set before first callback
00325 
00326     const Engine *e_artic = Engine::Get(engine_type);
00327     switch (type) {
00328       default: NOT_REACHED();
00329 
00330       case VEH_TRAIN: {
00331         Train *front = Train::From(first);
00332         Train *t = new Train();
00333         v->SetNext(t);
00334         v = t;
00335 
00336         t->subtype = 0;
00337         t->track = front->track;
00338         t->railtype = front->railtype;
00339 
00340         t->spritenum = e_artic->u.rail.image_index;
00341         if (e_artic->CanCarryCargo()) {
00342           t->cargo_type = e_artic->GetDefaultCargoType();
00343           t->cargo_cap = e_artic->u.rail.capacity;  // Callback 36 is called when the consist is finished
00344         } else {
00345           t->cargo_type = front->cargo_type; // Needed for livery selection
00346           t->cargo_cap = 0;
00347         }
00348 
00349         t->SetArticulatedPart();
00350         break;
00351       }
00352 
00353       case VEH_ROAD: {
00354         RoadVehicle *front = RoadVehicle::From(first);
00355         RoadVehicle *rv = new RoadVehicle();
00356         v->SetNext(rv);
00357         v = rv;
00358 
00359         rv->subtype = 0;
00360         gcache->cached_veh_length = VEHICLE_LENGTH; // Callback is called when the consist is finished
00361         rv->state = RVSB_IN_DEPOT;
00362 
00363         rv->roadtype = front->roadtype;
00364         rv->compatible_roadtypes = front->compatible_roadtypes;
00365 
00366         rv->spritenum = e_artic->u.road.image_index;
00367         if (e_artic->CanCarryCargo()) {
00368           rv->cargo_type = e_artic->GetDefaultCargoType();
00369           rv->cargo_cap = e_artic->u.road.capacity;  // Callback 36 is called when the consist is finished
00370         } else {
00371           rv->cargo_type = front->cargo_type; // Needed for livery selection
00372           rv->cargo_cap = 0;
00373         }
00374 
00375         rv->SetArticulatedPart();
00376         break;
00377       }
00378     }
00379 
00380     /* get common values from first engine */
00381     v->direction = first->direction;
00382     v->owner = first->owner;
00383     v->tile = first->tile;
00384     v->x_pos = first->x_pos;
00385     v->y_pos = first->y_pos;
00386     v->z_pos = first->z_pos;
00387     v->build_year = first->build_year;
00388     v->vehstatus = first->vehstatus & ~VS_STOPPED;
00389 
00390     v->cargo_subtype = 0;
00391     v->max_age = 0;
00392     v->engine_type = engine_type;
00393     v->value = 0;
00394     v->cur_image = SPR_IMG_QUERY;
00395     v->random_bits = VehicleRandomBits();
00396 
00397     if (flip_image) v->spritenum++;
00398 
00399     VehicleUpdatePosition(v);
00400   }
00401 }