From 2183fd4dabe72bd96b264bf8cdb3a2a8be625cf8 Mon Sep 17 00:00:00 2001 From: Vít Šefl Date: Sun, 15 Aug 2021 11:17:05 +0200 Subject: Feature: [NewGRF] Maximum curve speed modifier for rail vehicles (#9346) --- src/train_cmd.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'src/train_cmd.cpp') diff --git a/src/train_cmd.cpp b/src/train_cmd.cpp index 17e298c33..1498a5fad 100644 --- a/src/train_cmd.cpp +++ b/src/train_cmd.cpp @@ -115,6 +115,7 @@ void Train::ConsistChanged(ConsistChangeFlags allowed_changes) this->compatible_railtypes = RAILTYPES_NONE; bool train_can_tilt = true; + int min_curve_speed_mod = INT_MAX; for (Train *u = this; u != nullptr; u = u->Next()) { const RailVehicleInfo *rvi_u = RailVehInfo(u->engine_type); @@ -146,6 +147,7 @@ void Train::ConsistChanged(ConsistChangeFlags allowed_changes) const RailVehicleInfo *rvi_u = &e_u->u.rail; if (!HasBit(e_u->info.misc_flags, EF_RAIL_TILTS)) train_can_tilt = false; + min_curve_speed_mod = std::min(min_curve_speed_mod, u->GetCurveSpeedModifier()); /* Cache wagon override sprite group. nullptr is returned if there is none */ u->tcache.cached_override = GetWagonOverrideSpriteSet(u->engine_type, u->cargo_type, u->gcache.first_engine); @@ -229,6 +231,7 @@ void Train::ConsistChanged(ConsistChangeFlags allowed_changes) /* store consist weight/max speed in cache */ this->vcache.cached_max_speed = max_speed; this->tcache.cached_tilt = train_can_tilt; + this->tcache.cached_curve_speed_mod = min_curve_speed_mod; this->tcache.cached_max_curve_speed = this->GetCurveSpeedLimit(); /* recalculate cached weights and power too (we do this *after* the rest, so it is known which wagons are powered and need extra weight added) */ @@ -357,6 +360,11 @@ int Train::GetCurveSpeedLimit() const /* Apply max_speed bonus of 20% for a tilting train */ max_speed += max_speed / 5; } + + /* Apply max_speed modifier (cached value is fixed-point binary with 8 fractional bits) + * and clamp the result to an acceptable range. */ + max_speed += (max_speed * this->tcache.cached_curve_speed_mod) >> 8; + max_speed = Clamp(max_speed, 2, absolute_max_speed); } return max_speed; -- cgit v1.2.3-54-g00ecf