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