/** * Checks whether a NewGRF wants to play a different vehicle sound effect. * @param v Vehicle to play sound effect for. * @param event Trigger for the sound effect. * @return false if the default sound effect shall be played instead. */ bool PlayVehicleSound(const Vehicle *v, VehicleSoundEvent event) { const GRFFile *file = GetEngineGRF(v->engine_type); uint16 callback; /* If the engine has no GRF ID associated it can't ever play any new sounds */ if (file == NULL) return false; /* Check that the vehicle type uses the sound effect callback */ if (!HasBit(EngInfo(v->engine_type)->callback_mask, CBM_VEHICLE_SOUND_EFFECT)) return false; callback = GetVehicleCallback(CBID_VEHICLE_SOUND_EFFECT, event, 0, v->engine_type, v); /* Play default sound if callback fails */ if (callback == CALLBACK_FAILED) return false; if (callback >= ORIGINAL_SAMPLE_COUNT) { callback -= ORIGINAL_SAMPLE_COUNT; /* Play no sound if result is out of range */ if (callback > file->num_sounds) return true; callback += file->sound_offset; } assert(callback < GetNumSounds()); SndPlayVehicleFx(callback, v); return true; }
/** * Returns all cargos a vehicle can carry. * @param engine the EngineID of iterest * @param include_initial_cargo_type if true the default cargo type of the vehicle is included; if false only the refit_mask * @return bit set of CargoIDs */ static inline uint32 GetAvailableVehicleCargoTypes(EngineID engine, bool include_initial_cargo_type) { uint32 cargos = 0; CargoID initial_cargo_type; if (GetVehicleDefaultCapacity(engine, &initial_cargo_type) > 0) { const EngineInfo *ei = EngInfo(engine); cargos = ei->refit_mask; if (include_initial_cargo_type && initial_cargo_type < NUM_CARGO) SetBit(cargos, initial_cargo_type); } return cargos; }
uint CountArticulatedParts(EngineID engine_type, bool purchase_window) { if (!HasBit(EngInfo(engine_type)->callback_mask, CBM_VEHICLE_ARTIC_ENGINE)) return 0; /* If we can't allocate a vehicle now, we can't allocate it in the command * either, so it doesn't matter how many articulated parts there are. */ if (!Vehicle::CanAllocateItem()) return 0; Vehicle *v = NULL; if (!purchase_window) { v = new Vehicle(); v->engine_type = engine_type; } uint i; for (i = 1; i < MAX_ARTICULATED_PARTS; i++) { if (GetNextArticPart(i, engine_type, v) == INVALID_ENGINE) break; } delete v; return i - 1; }
void AddArticulatedParts(Vehicle *first) { VehicleType type = first->type; if (!HasBit(EngInfo(first->engine_type)->callback_mask, CBM_VEHICLE_ARTIC_ENGINE)) return; Vehicle *v = first; for (uint i = 1; i < MAX_ARTICULATED_PARTS; i++) { bool flip_image; EngineID engine_type = GetNextArticPart(i, first->engine_type, first, &flip_image); if (engine_type == INVALID_ENGINE) return; /* In the (very rare) case the GRF reported wrong number of articulated parts * and we run out of available vehicles, bail out. */ if (!Vehicle::CanAllocateItem()) return; const Engine *e_artic = Engine::Get(engine_type); switch (type) { default: NOT_REACHED(); case VEH_TRAIN: { Train *front = Train::From(first); Train *t = new Train(); v->SetNext(t); v = t; t->subtype = 0; t->track = front->track; t->railtype = front->railtype; t->tcache.first_engine = front->engine_type; // needs to be set before first callback t->spritenum = e_artic->u.rail.image_index; if (e_artic->CanCarryCargo()) { t->cargo_type = e_artic->GetDefaultCargoType(); t->cargo_cap = e_artic->u.rail.capacity; // Callback 36 is called when the consist is finished } else { t->cargo_type = front->cargo_type; // Needed for livery selection t->cargo_cap = 0; } t->SetArticulatedPart(); } break; case VEH_ROAD: { RoadVehicle *front = RoadVehicle::From(first); RoadVehicle *rv = new RoadVehicle(); v->SetNext(rv); v = rv; rv->subtype = 0; rv->rcache.first_engine = front->engine_type; // needs to be set before first callback rv->rcache.cached_veh_length = 8; // Callback is called when the consist is finished rv->state = RVSB_IN_DEPOT; rv->roadtype = front->roadtype; rv->compatible_roadtypes = front->compatible_roadtypes; rv->spritenum = e_artic->u.road.image_index; if (e_artic->CanCarryCargo()) { rv->cargo_type = e_artic->GetDefaultCargoType(); rv->cargo_cap = e_artic->u.road.capacity; // Callback 36 is called when the consist is finished } else { rv->cargo_type = front->cargo_type; // Needed for livery selection rv->cargo_cap = 0; } rv->SetArticulatedPart(); } break; } /* get common values from first engine */ v->direction = first->direction; v->owner = first->owner; v->tile = first->tile; v->x_pos = first->x_pos; v->y_pos = first->y_pos; v->z_pos = first->z_pos; v->build_year = first->build_year; v->vehstatus = first->vehstatus & ~VS_STOPPED; v->cargo_subtype = 0; v->max_speed = 0; v->max_age = 0; v->engine_type = engine_type; v->value = 0; v->cur_image = SPR_IMG_QUERY; v->random_bits = VehicleRandomBits(); if (flip_image) v->spritenum++; VehicleMove(v, false); } }
/** * Update the caches of this ship. */ void Ship::UpdateCache() { const ShipVehicleInfo *svi = ShipVehInfo(this->engine_type); /* Get speed fraction for the current water type. Aqueducts are always canals. */ bool is_ocean = GetEffectiveWaterClass(this->tile) == WATER_CLASS_SEA; uint raw_speed = GetVehicleProperty(this, PROP_SHIP_SPEED, svi->max_speed); this->vcache.cached_max_speed = svi->ApplyWaterClassSpeedFrac(raw_speed, is_ocean); /* Update cargo aging period. */ this->vcache.cached_cargo_age_period = GetVehicleProperty(this, PROP_SHIP_CARGO_AGE_PERIOD, EngInfo(this->engine_type)->cargo_age_period); this->UpdateVisualEffect(); }
/** * Does a NewGRF report that this should be an articulated vehicle? * @param engine_type The engine to check. * @return True iff the articulated engine callback flag is set. */ bool IsArticulatedEngine(EngineID engine_type) { return HasBit(EngInfo(engine_type)->callback_mask, CBM_VEHICLE_ARTIC_ENGINE); }