# articulated_vehicles.cpp

Go to the documentation of this file.

    00001 /* $Id: articulated_vehicles.cpp 19132 2010-02-14 16:31:35Z alberth $ */
    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 "newgrf_engine.h"
    00016 #include "vehicle_func.h"
    00017 #include "engine_base.h"
    00018 #include "engine_func.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 GetNextArticPart(uint index, EngineID front_type, Vehicle *front = NULL, bool *mirrored = NULL)
    00034 {
    00035   assert(front == NULL || front->engine_type == front_type);
    00036
    00037   uint16 callback = GetVehicleCallback(CBID_VEHICLE_ARTIC_ENGINE, index, 0, front_type, front);
    00038   if (callback == CALLBACK_FAILED || GB(callback, 0, 8) == 0xFF) return INVALID_ENGINE;
    00039
    00040   if (mirrored != NULL) *mirrored = HasBit(callback, 7);
    00041   return GetNewEngineID(GetEngineGRF(front_type), Engine::Get(front_type)->type, GB(callback, 0, 7));
    00042 }
    00043
    00044 uint CountArticulatedParts(EngineID engine_type, bool purchase_window)
    00045 {
    00046   if (!HasBit(EngInfo(engine_type)->callback_mask, CBM_VEHICLE_ARTIC_ENGINE)) return 0;
    00047
    00048   /* If we can't allocate a vehicle now, we can't allocate it in the command
    00049    * either, so it doesn't matter how many articulated parts there are. */
    00050   if (!Vehicle::CanAllocateItem()) return 0;
    00051
    00052   Vehicle *v = NULL;
    00053   if (!purchase_window) {
    00054     v = new Vehicle();
    00055     v->engine_type = engine_type;
    00056   }
    00057
    00058   uint i;
    00059   for (i = 1; i < MAX_ARTICULATED_PARTS; i++) {
    00060     if (GetNextArticPart(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->type != VEH_TRAIN && e->type != VEH_ROAD) 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 = GetNextArticPart(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->type != VEH_TRAIN && e->type != VEH_ROAD) 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 = GetNextArticPart(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->type != VEH_TRAIN && e->type != VEH_ROAD) 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 = GetNextArticPart(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     switch (v->type) {
    00227       case VEH_TRAIN:
    00228         v = Train::From(v)->HasArticulatedPart() ? Train::From(v)->GetNextArticPart() : NULL;
    00229         break;
    00230
    00231       case VEH_ROAD:
    00232         v = RoadVehicle::From(v)->HasArticulatedPart() ? v->Next() : NULL;
    00233         break;
    00234
    00235       default:
    00236         v = NULL;
    00237         break;
    00238     }
    00239   } while (v != NULL);
    00240
    00241   if (cargo_type != NULL) *cargo_type = first_cargo;
    00242   return false;
    00243 }
    00244
    00253 void CheckConsistencyOfArticulatedVehicle(const Vehicle *v)
    00254 {
    00255   const Engine *engine = Engine::Get(v->engine_type);
    00256
    00257   uint32 purchase_refit_union, purchase_refit_intersection;
    00258   GetArticulatedRefitMasks(v->engine_type, true, &purchase_refit_union, &purchase_refit_intersection);
    00259   CargoArray purchase_default_capacity = GetCapacityOfArticulatedParts(v->engine_type);
    00260
    00261   uint32 real_refit_union = 0;
    00262   uint32 real_refit_intersection = UINT_MAX;
    00263   CargoArray real_default_capacity;
    00264
    00265   do {
    00266     uint32 refit_mask = GetAvailableVehicleCargoTypes(v->engine_type, true);
    00267     real_refit_union |= refit_mask;
    00268     if (refit_mask != 0) real_refit_intersection &= refit_mask;
    00269
    00270     assert(v->cargo_type < NUM_CARGO);
    00271     real_default_capacity[v->cargo_type] += v->cargo_cap;
    00272
    00273     switch (v->type) {
    00274       case VEH_TRAIN:
    00275         v = Train::From(v)->HasArticulatedPart() ? Train::From(v)->GetNextArticPart() : NULL;
    00276         break;
    00277
    00278       case VEH_ROAD:
    00279         v = RoadVehicle::From(v)->HasArticulatedPart() ? v->Next() : NULL;
    00280         break;
    00281
    00282       default:
    00283         v = NULL;
    00284         break;
    00285     }
    00286   } while (v != NULL);
    00287
    00288   /* Check whether the vehicle carries more cargos than expected */
    00289   bool carries_more = false;
    00290   for (CargoID cid = 0; cid < NUM_CARGO; cid++) {
    00291     if (real_default_capacity[cid] != 0 && purchase_default_capacity[cid] == 0) {
    00292       carries_more = true;
    00293       break;
    00294     }
    00295   }
    00296
    00297   /* show a warning once for each GRF after each game load */
    00298   if (real_refit_union != purchase_refit_union || real_refit_intersection != purchase_refit_intersection || carries_more) {
    00299     ShowNewGrfVehicleError(engine->index, STR_NEWGRF_BUGGY, STR_NEWGRF_BUGGY_ARTICULATED_CARGO, GBUG_VEH_REFIT, false);
    00300   }
    00301 }
    00302
    00303 void AddArticulatedParts(Vehicle *first)
    00304 {
    00305   VehicleType type = first->type;
    00306   if (!HasBit(EngInfo(first->engine_type)->callback_mask, CBM_VEHICLE_ARTIC_ENGINE)) return;
    00307
    00308   Vehicle *v = first;
    00309   for (uint i = 1; i < MAX_ARTICULATED_PARTS; i++) {
    00310     bool flip_image;
    00311     EngineID engine_type = GetNextArticPart(i, first->engine_type, first, &flip_image);
    00312     if (engine_type == INVALID_ENGINE) return;
    00313
    00314     /* In the (very rare) case the GRF reported wrong number of articulated parts
    00315      * and we run out of available vehicles, bail out. */
    00316     if (!Vehicle::CanAllocateItem()) return;
    00317
    00318     const Engine *e_artic = Engine::Get(engine_type);
    00319     switch (type) {
    00320       default: NOT_REACHED();
    00321
    00322       case VEH_TRAIN: {
    00323         Train *front = Train::From(first);
    00324         Train *t = new Train();
    00325         v->SetNext(t);
    00326         v = t;
    00327
    00328         t->subtype = 0;
    00329         t->track = front->track;
    00330         t->railtype = front->railtype;
    00331         t->tcache.first_engine = front->engine_type; // needs to be set before first callback
    00332
    00333         t->spritenum = e_artic->u.rail.image_index;
    00334         if (e_artic->CanCarryCargo()) {
    00335           t->cargo_type = e_artic->GetDefaultCargoType();
    00336           t->cargo_cap = e_artic->u.rail.capacity;  // Callback 36 is called when the consist is finished
    00337         } else {
    00338           t->cargo_type = front->cargo_type; // Needed for livery selection
    00339           t->cargo_cap = 0;
    00340         }
    00341
    00342         t->SetArticulatedPart();
    00343       } break;
    00344
    00345       case VEH_ROAD: {
    00346         RoadVehicle *front = RoadVehicle::From(first);
    00347         RoadVehicle *rv = new RoadVehicle();
    00348         v->SetNext(rv);
    00349         v = rv;
    00350
    00351         rv->subtype = 0;
    00352         rv->rcache.first_engine = front->engine_type; // needs to be set before first callback
    00353         rv->rcache.cached_veh_length = 8; // Callback is called when the consist is finished
    00354         rv->state = RVSB_IN_DEPOT;
    00355
    00356         rv->roadtype = front->roadtype;
    00357         rv->compatible_roadtypes = front->compatible_roadtypes;
    00358
    00359         rv->spritenum = e_artic->u.road.image_index;
    00360         if (e_artic->CanCarryCargo()) {
    00361           rv->cargo_type = e_artic->GetDefaultCargoType();
    00362           rv->cargo_cap = e_artic->u.road.capacity;  // Callback 36 is called when the consist is finished
    00363         } else {
    00364           rv->cargo_type = front->cargo_type; // Needed for livery selection
    00365           rv->cargo_cap = 0;
    00366         }
    00367
    00368         rv->SetArticulatedPart();
    00369       } break;
    00370     }
    00371
    00372     /* get common values from first engine */
    00373     v->direction = first->direction;
    00374     v->owner = first->owner;
    00375     v->tile = first->tile;
    00376     v->x_pos = first->x_pos;
    00377     v->y_pos = first->y_pos;
    00378     v->z_pos = first->z_pos;
    00379     v->build_year = first->build_year;
    00380     v->vehstatus = first->vehstatus & ~VS_STOPPED;
    00381
    00382     v->cargo_subtype = 0;
    00383     v->max_speed = 0;
    00384     v->max_age = 0;
    00385     v->engine_type = engine_type;
    00386     v->value = 0;
    00387     v->cur_image = SPR_IMG_QUERY;
    00388     v->random_bits = VehicleRandomBits();
    00389
    00390     if (flip_image) v->spritenum++;
    00391
    00392     VehicleMove(v, false);
    00393   }
    00394 }