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 #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
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
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
00078
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
00168 bool IsArticulatedVehicleRefittable(EngineID engine)
00169 {
00170 if (IsEngineRefittable(engine)) return true;
00171
00172 const Engine *e = Engine::Get(engine);
00173 if (!e->IsGroundVehicle()) return false;
00174
00175 if (!HasBit(e->info.callback_mask, CBM_VEHICLE_ARTIC_ENGINE)) return false;
00176
00177 for (uint i = 1; i < MAX_ARTICULATED_PARTS; i++) {
00178 EngineID artic_engine = GetNextArticulatedPart(i, engine);
00179 if (artic_engine == INVALID_ENGINE) break;
00180
00181 if (IsEngineRefittable(artic_engine)) return true;
00182 }
00183
00184 return false;
00185 }
00186
00194 void GetArticulatedRefitMasks(EngineID engine, bool include_initial_cargo_type, uint32 *union_mask, uint32 *intersection_mask)
00195 {
00196 const Engine *e = Engine::Get(engine);
00197 uint32 veh_cargoes = GetAvailableVehicleCargoTypes(engine, include_initial_cargo_type);
00198 *union_mask = veh_cargoes;
00199 *intersection_mask = (veh_cargoes != 0) ? veh_cargoes : UINT32_MAX;
00200
00201 if (!e->IsGroundVehicle()) return;
00202 if (!HasBit(e->info.callback_mask, CBM_VEHICLE_ARTIC_ENGINE)) return;
00203
00204 for (uint i = 1; i < MAX_ARTICULATED_PARTS; i++) {
00205 EngineID artic_engine = GetNextArticulatedPart(i, engine);
00206 if (artic_engine == INVALID_ENGINE) break;
00207
00208 veh_cargoes = GetAvailableVehicleCargoTypes(artic_engine, include_initial_cargo_type);
00209 *union_mask |= veh_cargoes;
00210 if (veh_cargoes != 0) *intersection_mask &= veh_cargoes;
00211 }
00212 }
00213
00220 uint32 GetUnionOfArticulatedRefitMasks(EngineID engine, bool include_initial_cargo_type)
00221 {
00222 uint32 union_mask, intersection_mask;
00223 GetArticulatedRefitMasks(engine, include_initial_cargo_type, &union_mask, &intersection_mask);
00224 return union_mask;
00225 }
00226
00233 uint32 GetIntersectionOfArticulatedRefitMasks(EngineID engine, bool include_initial_cargo_type)
00234 {
00235 uint32 union_mask, intersection_mask;
00236 GetArticulatedRefitMasks(engine, include_initial_cargo_type, &union_mask, &intersection_mask);
00237 return intersection_mask;
00238 }
00239
00240
00248 bool IsArticulatedVehicleCarryingDifferentCargoes(const Vehicle *v, CargoID *cargo_type)
00249 {
00250 CargoID first_cargo = CT_INVALID;
00251
00252 do {
00253 if (v->cargo_type != CT_INVALID && v->GetEngine()->CanCarryCargo()) {
00254 if (first_cargo == CT_INVALID) first_cargo = v->cargo_type;
00255 if (first_cargo != v->cargo_type) {
00256 if (cargo_type != NULL) *cargo_type = CT_INVALID;
00257 return true;
00258 }
00259 }
00260
00261 v = v->HasArticulatedPart() ? v->GetNextArticulatedPart() : NULL;
00262 } while (v != NULL);
00263
00264 if (cargo_type != NULL) *cargo_type = first_cargo;
00265 return false;
00266 }
00267
00276 void CheckConsistencyOfArticulatedVehicle(const Vehicle *v)
00277 {
00278 const Engine *engine = v->GetEngine();
00279
00280 uint32 purchase_refit_union, purchase_refit_intersection;
00281 GetArticulatedRefitMasks(v->engine_type, true, &purchase_refit_union, &purchase_refit_intersection);
00282 CargoArray purchase_default_capacity = GetCapacityOfArticulatedParts(v->engine_type);
00283
00284 uint32 real_refit_union = 0;
00285 uint32 real_refit_intersection = UINT_MAX;
00286 CargoArray real_default_capacity;
00287
00288 do {
00289 uint32 refit_mask = GetAvailableVehicleCargoTypes(v->engine_type, true);
00290 real_refit_union |= refit_mask;
00291 if (refit_mask != 0) real_refit_intersection &= refit_mask;
00292
00293 assert(v->cargo_type < NUM_CARGO);
00294 real_default_capacity[v->cargo_type] += v->cargo_cap;
00295
00296 v = v->HasArticulatedPart() ? v->GetNextArticulatedPart() : NULL;
00297 } while (v != NULL);
00298
00299
00300 bool carries_more = false;
00301 for (CargoID cid = 0; cid < NUM_CARGO; cid++) {
00302 if (real_default_capacity[cid] != 0 && purchase_default_capacity[cid] == 0) {
00303 carries_more = true;
00304 break;
00305 }
00306 }
00307
00308
00309 if (real_refit_union != purchase_refit_union || real_refit_intersection != purchase_refit_intersection || carries_more) {
00310 ShowNewGrfVehicleError(engine->index, STR_NEWGRF_BUGGY, STR_NEWGRF_BUGGY_ARTICULATED_CARGO, GBUG_VEH_REFIT, false);
00311 }
00312 }
00313
00318 void AddArticulatedParts(Vehicle *first)
00319 {
00320 VehicleType type = first->type;
00321 if (!HasBit(EngInfo(first->engine_type)->callback_mask, CBM_VEHICLE_ARTIC_ENGINE)) return;
00322
00323 Vehicle *v = first;
00324 for (uint i = 1; i < MAX_ARTICULATED_PARTS; i++) {
00325 bool flip_image;
00326 EngineID engine_type = GetNextArticulatedPart(i, first->engine_type, first, &flip_image);
00327 if (engine_type == INVALID_ENGINE) return;
00328
00329
00330
00331 if (!Vehicle::CanAllocateItem()) return;
00332
00333 GroundVehicleCache *gcache = v->GetGroundVehicleCache();
00334 gcache->first_engine = v->engine_type;
00335
00336 const Engine *e_artic = Engine::Get(engine_type);
00337 switch (type) {
00338 default: NOT_REACHED();
00339
00340 case VEH_TRAIN: {
00341 Train *front = Train::From(first);
00342 Train *t = new Train();
00343 v->SetNext(t);
00344 v = t;
00345
00346 t->subtype = 0;
00347 t->track = front->track;
00348 t->railtype = front->railtype;
00349
00350 t->spritenum = e_artic->u.rail.image_index;
00351 if (e_artic->CanCarryCargo()) {
00352 t->cargo_type = e_artic->GetDefaultCargoType();
00353 t->cargo_cap = e_artic->u.rail.capacity;
00354 } else {
00355 t->cargo_type = front->cargo_type;
00356 t->cargo_cap = 0;
00357 }
00358 t->refit_cap = 0;
00359
00360 t->SetArticulatedPart();
00361 break;
00362 }
00363
00364 case VEH_ROAD: {
00365 RoadVehicle *front = RoadVehicle::From(first);
00366 RoadVehicle *rv = new RoadVehicle();
00367 v->SetNext(rv);
00368 v = rv;
00369
00370 rv->subtype = 0;
00371 gcache->cached_veh_length = VEHICLE_LENGTH;
00372 rv->state = RVSB_IN_DEPOT;
00373
00374 rv->roadtype = front->roadtype;
00375 rv->compatible_roadtypes = front->compatible_roadtypes;
00376
00377 rv->spritenum = e_artic->u.road.image_index;
00378 if (e_artic->CanCarryCargo()) {
00379 rv->cargo_type = e_artic->GetDefaultCargoType();
00380 rv->cargo_cap = e_artic->u.road.capacity;
00381 } else {
00382 rv->cargo_type = front->cargo_type;
00383 rv->cargo_cap = 0;
00384 }
00385 rv->refit_cap = 0;
00386
00387 rv->SetArticulatedPart();
00388 break;
00389 }
00390 }
00391
00392
00393 v->direction = first->direction;
00394 v->owner = first->owner;
00395 v->tile = first->tile;
00396 v->x_pos = first->x_pos;
00397 v->y_pos = first->y_pos;
00398 v->z_pos = first->z_pos;
00399 v->build_year = first->build_year;
00400 v->vehstatus = first->vehstatus & ~VS_STOPPED;
00401
00402 v->cargo_subtype = 0;
00403 v->max_age = 0;
00404 v->engine_type = engine_type;
00405 v->value = 0;
00406 v->cur_image = SPR_IMG_QUERY;
00407 v->random_bits = VehicleRandomBits();
00408
00409 if (flip_image) v->spritenum++;
00410
00411 VehicleUpdatePosition(v);
00412 }
00413 }