Merge branch 'improved_breakdowns' into improved_breakdowns-sx

This commit is contained in:
Jonathan G Rennison
2015-09-27 23:17:58 +01:00
3 changed files with 93 additions and 87 deletions

View File

@@ -1021,6 +1021,14 @@ static bool RoadVehAccelerationModelChanged(int32 p1)
} }
} }
} }
if (_settings_game.vehicle.roadveh_acceleration_model == AM_ORIGINAL || !_settings_game.vehicle.improved_breakdowns) {
RoadVehicle *rv;
FOR_ALL_ROADVEHICLES(rv) {
if (rv->IsFrontEngine()) {
rv->breakdown_chance = 128;
}
}
}
/* These windows show acceleration values only when realistic acceleration is on. They must be redrawn after a setting change. */ /* These windows show acceleration values only when realistic acceleration is on. They must be redrawn after a setting change. */
SetWindowClassesDirty(WC_ENGINE_PREVIEW); SetWindowClassesDirty(WC_ENGINE_PREVIEW);

View File

@@ -484,8 +484,13 @@ void Train::UpdateAcceleration()
assert(weight != 0); assert(weight != 0);
this->acceleration = Clamp(power / weight * 4, 1, 255); this->acceleration = Clamp(power / weight * 4, 1, 255);
/* for non-realistic acceleration, breakdown chance is 128, corrected by the multiengine factor of 3/(n+2) */ if (_settings_game.vehicle.improved_breakdowns) {
this->breakdown_chance = min(128 * 3 / (this->tcache.cached_num_engines + 2), 5); if (_settings_game.vehicle.train_acceleration_model == AM_ORIGINAL) {
this->breakdown_chance = max(128 * 3 / (this->tcache.cached_num_engines + 2), 5);
}
} else {
this->breakdown_chance = 128;
}
} }
/** /**

View File

@@ -116,16 +116,12 @@ void VehicleServiceInDepot(Vehicle *v)
v->reliability = e->reliability; v->reliability = e->reliability;
v->breakdown_ctr = 0; v->breakdown_ctr = 0;
v->vehstatus &= ~VS_AIRCRAFT_BROKEN; v->vehstatus &= ~VS_AIRCRAFT_BROKEN;
/* Prevent vehicles from breaking down directly after exiting the depot. */
v->breakdown_chance /= 4;
SetWindowDirty(WC_VEHICLE_DETAILS, v->index); // ensure that last service date and reliability are updated SetWindowDirty(WC_VEHICLE_DETAILS, v->index); // ensure that last service date and reliability are updated
do { do {
v->date_of_last_service = _date; v->date_of_last_service = _date;
v->breakdowns_since_last_service = 0; v->breakdowns_since_last_service = 0;
v->reliability = v->GetEngine()->reliability; v->reliability = v->GetEngine()->reliability;
/* Prevent vehicles from breaking down directly after exiting the depot. */
v->breakdown_chance /= 4;
v = v->Next(); v = v->Next();
} while (v != NULL && v->HasEngineType()); } while (v != NULL && v->HasEngineType());
} }
@@ -1193,76 +1189,75 @@ static const byte _breakdown_chances[4][4] = {
* @param v the vehicle in question. * @param v the vehicle in question.
* @param r the random number to use. (Note that bits 0..6 are already used) * @param r the random number to use. (Note that bits 0..6 are already used)
*/ */
void void DetermineBreakdownType(Vehicle *v, uint32 r) {
DetermineBreakdownType( Vehicle *v, uint32 r ) {
/* if 'improved breakdowns' is off, just do the classic breakdown */ /* if 'improved breakdowns' is off, just do the classic breakdown */
if ( !_settings_game.vehicle.improved_breakdowns ) { if (!_settings_game.vehicle.improved_breakdowns) {
v->breakdown_type = BREAKDOWN_CRITICAL; v->breakdown_type = BREAKDOWN_CRITICAL;
v->breakdown_severity = 40; //only used by aircraft (321 km/h) v->breakdown_severity = 40; //only used by aircraft (321 km/h)
return; return;
} }
byte rand = GB( r, 8, 8 ); byte rand = GB(r, 8, 8);
const byte *breakdown_type_chance = _breakdown_chances[v->type]; const byte *breakdown_type_chance = _breakdown_chances[v->type];
if ( v->type == VEH_AIRCRAFT ) { if (v->type == VEH_AIRCRAFT) {
if ( rand <= breakdown_type_chance[BREAKDOWN_AIRCRAFT_SPEED] ) { if (rand <= breakdown_type_chance[BREAKDOWN_AIRCRAFT_SPEED]) {
v->breakdown_type = BREAKDOWN_AIRCRAFT_SPEED; v->breakdown_type = BREAKDOWN_AIRCRAFT_SPEED;
/* all speed values here are 1/8th of the real max speed in km/h */ /* all speed values here are 1/8th of the real max speed in km/h */
byte max_speed = min( AircraftVehInfo( v->engine_type )->max_speed >> 3, 255 ); byte max_speed = min(AircraftVehInfo( v->engine_type )->max_speed >> 3, 255);
byte min_speed = min( 15 + ( max_speed >> 2 ), AircraftVehInfo( v->engine_type )->max_speed >> 4 ); byte min_speed = min(15 + (max_speed >> 2), AircraftVehInfo(v->engine_type)->max_speed >> 4);
v->breakdown_severity = min_speed + ( ( ( v->reliability + GB( r, 16, 16 ) ) * ( max_speed - min_speed ) ) >> 17 ); v->breakdown_severity = min_speed + (((v->reliability + GB(r, 16, 16)) * (max_speed - min_speed)) >> 17);
} else if ( rand <= breakdown_type_chance[BREAKDOWN_AIRCRAFT_DEPOT] ) { } else if (rand <= breakdown_type_chance[BREAKDOWN_AIRCRAFT_DEPOT]) {
v->breakdown_type = BREAKDOWN_AIRCRAFT_DEPOT; v->breakdown_type = BREAKDOWN_AIRCRAFT_DEPOT;
} else if ( rand <= breakdown_type_chance[BREAKDOWN_AIRCRAFT_EM_LANDING] ) { } else if (rand <= breakdown_type_chance[BREAKDOWN_AIRCRAFT_EM_LANDING]) {
/* emergency landings only happen when reliability < 87% */ /* emergency landings only happen when reliability < 87% */
if ( v->reliability < 0xDDDD ) { if (v->reliability < 0xDDDD) {
v->breakdown_type = BREAKDOWN_AIRCRAFT_EM_LANDING; v->breakdown_type = BREAKDOWN_AIRCRAFT_EM_LANDING;
} else { } else {
/* try again */ /* try again */
DetermineBreakdownType( v, Random( ) ); DetermineBreakdownType(v, Random());
} }
} else { } else {
NOT_REACHED( ); NOT_REACHED();
} }
return; return;
} }
if ( rand <= breakdown_type_chance[BREAKDOWN_CRITICAL] ) { if (rand <= breakdown_type_chance[BREAKDOWN_CRITICAL]) {
v->breakdown_type = BREAKDOWN_CRITICAL; v->breakdown_type = BREAKDOWN_CRITICAL;
} else if ( rand <= breakdown_type_chance[BREAKDOWN_EM_STOP] ) { } else if (rand <= breakdown_type_chance[BREAKDOWN_EM_STOP]) {
/* Non-front engines cannot have emergency stops */ /* Non-front engines cannot have emergency stops */
if ( v->type == VEH_TRAIN && !( Train::From( v )->IsFrontEngine( ) ) ) { if (v->type == VEH_TRAIN && !(Train::From(v)->IsFrontEngine())) {
return DetermineBreakdownType( v, Random( ) ); return DetermineBreakdownType(v, Random());
} }
v->breakdown_type = BREAKDOWN_EM_STOP; v->breakdown_type = BREAKDOWN_EM_STOP;
v->breakdown_delay >>= 2; //emergency stops don't last long (1/4 of normal) v->breakdown_delay >>= 2; //emergency stops don't last long (1/4 of normal)
} else if ( rand <= breakdown_type_chance[BREAKDOWN_LOW_SPEED] ) { } else if (rand <= breakdown_type_chance[BREAKDOWN_LOW_SPEED]) {
v->breakdown_type = BREAKDOWN_LOW_SPEED; v->breakdown_type = BREAKDOWN_LOW_SPEED;
/* average of random and reliability */ /* average of random and reliability */
uint16 rand2 = ( GB( r, 16, 16 ) + v->reliability ) >> 1; uint16 rand2 = (GB(r, 16, 16) + v->reliability) >> 1;
uint16 max_speed = uint16 max_speed =
( v->type == VEH_TRAIN ) ? (v->type == VEH_TRAIN) ?
GetVehicleProperty( v, PROP_TRAIN_SPEED, RailVehInfo( v->engine_type )->max_speed ) : GetVehicleProperty(v, PROP_TRAIN_SPEED, RailVehInfo(v->engine_type)->max_speed) :
( v->type == VEH_ROAD ) ? (v->type == VEH_ROAD ) ?
GetVehicleProperty( v, PROP_ROADVEH_SPEED, RoadVehInfo( v->engine_type )->max_speed ) : GetVehicleProperty(v, PROP_ROADVEH_SPEED, RoadVehInfo(v->engine_type)->max_speed) :
( v->type == VEH_SHIP ) ? (v->type == VEH_SHIP) ?
GetVehicleProperty( v, PROP_SHIP_SPEED, ShipVehInfo( v->engine_type )->max_speed ) : GetVehicleProperty(v, PROP_SHIP_SPEED, ShipVehInfo(v->engine_type)->max_speed ) :
GetVehicleProperty( v, PROP_AIRCRAFT_SPEED, AircraftVehInfo( v->engine_type )->max_speed ); GetVehicleProperty(v, PROP_AIRCRAFT_SPEED, AircraftVehInfo(v->engine_type)->max_speed);
byte min_speed = min( 41, max_speed >> 2 ); byte min_speed = min(41, max_speed >> 2);
/* we use the min() function here because we want to use the real value of max_speed for the min_speed calculation */ /* we use the min() function here because we want to use the real value of max_speed for the min_speed calculation */
max_speed = min( max_speed, 255 ); max_speed = min(max_speed, 255);
v->breakdown_severity = Clamp( ( max_speed * rand2 ) >> 16, min_speed, max_speed ); v->breakdown_severity = Clamp((max_speed * rand2) >> 16, min_speed, max_speed);
} else if ( rand <= breakdown_type_chance[BREAKDOWN_LOW_POWER] ) { } else if (rand <= breakdown_type_chance[BREAKDOWN_LOW_POWER]) {
v->breakdown_type = BREAKDOWN_LOW_POWER; v->breakdown_type = BREAKDOWN_LOW_POWER;
/** within this type there are two possibilities: (50/50) /** within this type there are two possibilities: (50/50)
* power reduction (10-90%), or no power at all */ * power reduction (10-90%), or no power at all */
if ( GB( r, 7, 1 ) ) { if (GB(r, 7, 1)) {
v->breakdown_severity = Clamp( ( GB( r, 16, 16 ) + v->reliability ) >> 9, 26, 231 ); v->breakdown_severity = Clamp((GB(r, 16, 16) + v->reliability) >> 9, 26, 231);
} else { } else {
v->breakdown_severity = 0; v->breakdown_severity = 0;
} }
} else { } else {
NOT_REACHED( ); NOT_REACHED();
} }
} }
@@ -1300,10 +1295,10 @@ void CheckVehicleBreakdown(Vehicle *v)
* However, because breakdowns are no longer by definition a complete stop, * However, because breakdowns are no longer by definition a complete stop,
* their impact will be significantly less. * their impact will be significantly less.
*/ */
if ( (uint32) ( 0xffff - v->reliability ) * _settings_game.difficulty.vehicle_breakdowns * chance > GB( r1, 0, 24 ) * 10 ) { if ((uint32) (0xffff - v->reliability) * _settings_game.difficulty.vehicle_breakdowns * chance > GB(r1, 0, 24) * 10) {
v->breakdown_ctr = GB( r1, 24, 6 ) + 0xF; v->breakdown_ctr = GB(r1, 24, 6) + 0xF;
v->breakdown_delay = GB( r2, 0, 7 ) + 0x80; v->breakdown_delay = GB(r2, 0, 7) + 0x80;
DetermineBreakdownType( v, r2 ); DetermineBreakdownType(v, r2);
} }
} }
@@ -1341,22 +1336,21 @@ bool Vehicle::HandleBreakdown()
(this->current_order.GetDepotOrderType() & ODTFB_BREAKDOWN) && (this->current_order.GetDepotOrderType() & ODTFB_BREAKDOWN) &&
GetTargetAirportIfValid(Aircraft::From(this)) != NULL)) return false; GetTargetAirportIfValid(Aircraft::From(this)) != NULL)) return false;
FindBreakdownDestination(Aircraft::From(this)); FindBreakdownDestination(Aircraft::From(this));
} else if (this->type == VEH_TRAIN) {
} else if ( this->type == VEH_TRAIN ) { if (this->breakdown_type == BREAKDOWN_LOW_POWER ||
if ( this->breakdown_type == BREAKDOWN_LOW_POWER || this->First()->cur_speed <= ((this->breakdown_type == BREAKDOWN_LOW_SPEED) ? this->breakdown_severity : 0)) {
this->First( )->cur_speed <= ( ( this->breakdown_type == BREAKDOWN_LOW_SPEED ) ? this->breakdown_severity : 0 ) ) { switch (this->breakdown_type) {
switch ( this->breakdown_type ) {
case BREAKDOWN_CRITICAL: case BREAKDOWN_CRITICAL:
if (!PlayVehicleSound(this, VSE_BREAKDOWN)) { if (!PlayVehicleSound(this, VSE_BREAKDOWN)) {
bool train_or_ship = this->type == VEH_TRAIN || this->type == VEH_SHIP; bool train_or_ship = this->type == VEH_TRAIN || this->type == VEH_SHIP;
SndPlayVehicleFx((_settings_game.game_creation.landscape != LT_TOYLAND) ? SndPlayVehicleFx((_settings_game.game_creation.landscape != LT_TOYLAND) ?
(train_or_ship ? SND_10_TRAIN_BREAKDOWN : SND_0F_VEHICLE_BREAKDOWN) : (train_or_ship ? SND_10_TRAIN_BREAKDOWN : SND_0F_VEHICLE_BREAKDOWN) :
(train_or_ship ? SND_3A_COMEDY_BREAKDOWN_2 : SND_35_COMEDY_BREAKDOWN), this); (train_or_ship ? SND_3A_COMEDY_BREAKDOWN_2 : SND_35_COMEDY_BREAKDOWN), this);
} }
if (!(this->vehstatus & VS_HIDDEN)) { if (!(this->vehstatus & VS_HIDDEN) && !HasBit(EngInfo(this->engine_type)->misc_flags, EF_NO_BREAKDOWN_SMOKE) && this->breakdown_delay > 0) {
EffectVehicle *u = CreateEffectVehicleRel(this, 4, 4, 5, EV_BREAKDOWN_SMOKE); EffectVehicle *u = CreateEffectVehicleRel(this, 4, 4, 5, EV_BREAKDOWN_SMOKE);
if (u != NULL) u->animation_state = this->breakdown_delay * 2; if (u != NULL) u->animation_state = this->breakdown_delay * 2;
} }
/* Max Speed reduction*/ /* Max Speed reduction*/
if (_settings_game.vehicle.improved_breakdowns) { if (_settings_game.vehicle.improved_breakdowns) {
if (!HasBit(Train::From(this)->flags,VRF_NEED_REPAIR)) { if (!HasBit(Train::From(this)->flags,VRF_NEED_REPAIR)) {
@@ -1364,15 +1358,13 @@ bool Vehicle::HandleBreakdown()
const RailVehicleInfo *rvi = &e->u.rail; const RailVehicleInfo *rvi = &e->u.rail;
if (rvi->max_speed > this->vcache.cached_max_speed) if (rvi->max_speed > this->vcache.cached_max_speed)
this->vcache.cached_max_speed = rvi->max_speed; this->vcache.cached_max_speed = rvi->max_speed;
} }
this->vcache.cached_max_speed = this->vcache.cached_max_speed = min(this->vcache.cached_max_speed -
min( (this->vcache.cached_max_speed >> 1) / Train::From(this->First())->tcache.cached_num_engines + 1, this->vcache.cached_max_speed);
this->vcache.cached_max_speed - (this->vcache.cached_max_speed >> 1) / Train::From(this->First())->tcache.cached_num_engines + 1,
this->vcache.cached_max_speed);
SetBit(Train::From(this)->flags, VRF_NEED_REPAIR); SetBit(Train::From(this)->flags, VRF_NEED_REPAIR);
Train::From(this->First())->ConsistChanged(CCF_TRACK); Train::From(this->First())->ConsistChanged(CCF_TRACK);
} }
/* FALL THROUGH */ /* FALL THROUGH */
case BREAKDOWN_EM_STOP: case BREAKDOWN_EM_STOP:
CheckBreakdownFlags(Train::From(this->First())); CheckBreakdownFlags(Train::From(this->First()));
SetBit(Train::From(this->First())->flags, VRF_BREAKDOWN_STOPPED); SetBit(Train::From(this->First())->flags, VRF_BREAKDOWN_STOPPED);
@@ -1385,27 +1377,28 @@ bool Vehicle::HandleBreakdown()
SetBit(Train::From(this->First())->flags, VRF_BREAKDOWN_POWER); SetBit(Train::From(this->First())->flags, VRF_BREAKDOWN_POWER);
break; break;
default: NOT_REACHED(); default: NOT_REACHED();
} }
this->First()->MarkDirty(); this->First()->MarkDirty();
SetWindowDirty(WC_VEHICLE_VIEW, this->index); SetWindowDirty(WC_VEHICLE_VIEW, this->index);
SetWindowDirty(WC_VEHICLE_DETAILS, this->index); SetWindowDirty(WC_VEHICLE_DETAILS, this->index);
} else { } else {
this->breakdown_ctr = 2; // wait until slowdown this->breakdown_ctr = 2; // wait until slowdown
this->breakdowns_since_last_service--; this->breakdowns_since_last_service--;
SetBit( Train::From( this )->flags, VRF_BREAKDOWN_BRAKING ); SetBit(Train::From(this)->flags, VRF_BREAKDOWN_BRAKING);
return false; return false;
} }
if ( ( !( this->vehstatus & VS_HIDDEN ) ) && ( ( this->breakdown_type == BREAKDOWN_LOW_SPEED || this->breakdown_type == BREAKDOWN_LOW_POWER ) && ( this->tick_counter & 0x1F ) == 0 ) ) { if ((!(this->vehstatus & VS_HIDDEN)) && (this->breakdown_type == BREAKDOWN_LOW_SPEED || this->breakdown_type == BREAKDOWN_LOW_POWER)
CreateEffectVehicleRel( this, 0, 0, 2, EV_BREAKDOWN_SMOKE ); //some grey clouds to indicate a broken engine && !HasBit(EngInfo(this->engine_type)->misc_flags, EF_NO_BREAKDOWN_SMOKE)) {
} EffectVehicle *u = CreateEffectVehicleRel(this, 0, 0, 2, EV_BREAKDOWN_SMOKE); //some grey clouds to indicate a broken engine
if (u != NULL) u->animation_state = 25;
}
} else { } else {
switch ( this->breakdown_type ) { switch (this->breakdown_type) {
case BREAKDOWN_CRITICAL: case BREAKDOWN_CRITICAL:
if ( !PlayVehicleSound( this, VSE_BREAKDOWN ) ) { if (!PlayVehicleSound(this, VSE_BREAKDOWN)) {
SndPlayVehicleFx( ( _settings_game.game_creation.landscape != LT_TOYLAND ) ? SND_0F_VEHICLE_BREAKDOWN : SND_35_COMEDY_BREAKDOWN, this ); SndPlayVehicleFx((_settings_game.game_creation.landscape != LT_TOYLAND) ? SND_0F_VEHICLE_BREAKDOWN : SND_35_COMEDY_BREAKDOWN, this);
} }
if (!(this->vehstatus & VS_HIDDEN)) { if (!(this->vehstatus & VS_HIDDEN) && !HasBit(EngInfo(this->engine_type)->misc_flags, EF_NO_BREAKDOWN_SMOKE) && this->breakdown_delay > 0) {
EffectVehicle *u = CreateEffectVehicleRel(this, 4, 4, 5, EV_BREAKDOWN_SMOKE); EffectVehicle *u = CreateEffectVehicleRel(this, 4, 4, 5, EV_BREAKDOWN_SMOKE);
if (u != NULL) u->animation_state = this->breakdown_delay * 2; if (u != NULL) u->animation_state = this->breakdown_delay * 2;
} }
@@ -1417,18 +1410,18 @@ bool Vehicle::HandleBreakdown()
case BREAKDOWN_LOW_POWER: case BREAKDOWN_LOW_POWER:
/* do nothing */ /* do nothing */
break; break;
default: NOT_REACHED( ); default: NOT_REACHED();
} }
if ( ( !( this->vehstatus & VS_HIDDEN ) ) && ( if ((!(this->vehstatus & VS_HIDDEN)) &&
( this->breakdown_type == BREAKDOWN_LOW_SPEED || this->breakdown_type == BREAKDOWN_LOW_POWER ) && (this->breakdown_type == BREAKDOWN_LOW_SPEED || this->breakdown_type == BREAKDOWN_LOW_POWER)) {
( this->tick_counter & 0x1F ) == 0 ) ) {
/* Some gray clouds to indicate a broken RV */ /* Some gray clouds to indicate a broken RV */
CreateEffectVehicleRel( this, 0, 0, 2, EV_BREAKDOWN_SMOKE ); EffectVehicle *u = CreateEffectVehicleRel(this, 0, 0, 2, EV_BREAKDOWN_SMOKE);
if (u != NULL) u->animation_state = 25;
} }
this->First()->MarkDirty(); this->First()->MarkDirty();
SetWindowDirty(WC_VEHICLE_VIEW, this->index); SetWindowDirty(WC_VEHICLE_VIEW, this->index);
SetWindowDirty(WC_VEHICLE_DETAILS, this->index); SetWindowDirty(WC_VEHICLE_DETAILS, this->index);
return (this->breakdown_type == BREAKDOWN_CRITICAL || this->breakdown_type == BREAKDOWN_EM_STOP ); return (this->breakdown_type == BREAKDOWN_CRITICAL || this->breakdown_type == BREAKDOWN_EM_STOP);
} }
/* FALL THROUGH */ /* FALL THROUGH */
@@ -1440,16 +1433,16 @@ bool Vehicle::HandleBreakdown()
if ((this->tick_counter & (this->type == VEH_TRAIN ? 3 : 1)) == 0) { if ((this->tick_counter & (this->type == VEH_TRAIN ? 3 : 1)) == 0) {
if (--this->breakdown_delay == 0) { if (--this->breakdown_delay == 0) {
this->breakdown_ctr = 0; this->breakdown_ctr = 0;
if( this->type == VEH_TRAIN ) { if(this->type == VEH_TRAIN) {
CheckBreakdownFlags(Train::From(this->First())); CheckBreakdownFlags(Train::From(this->First()));
this->First()->MarkDirty(); this->First()->MarkDirty();
SetWindowDirty(WC_VEHICLE_VIEW, this->First()->index); SetWindowDirty(WC_VEHICLE_VIEW, this->First()->index);
} else { } else {
this->MarkDirty(); this->MarkDirty();
SetWindowDirty(WC_VEHICLE_VIEW, this->index); SetWindowDirty(WC_VEHICLE_VIEW, this->index);
}
} }
} }
}
return (this->breakdown_type == BREAKDOWN_CRITICAL || this->breakdown_type == BREAKDOWN_EM_STOP); return (this->breakdown_type == BREAKDOWN_CRITICAL || this->breakdown_type == BREAKDOWN_EM_STOP);
default: default: