From bd3b28551e9a45cba654af770f10f06795f65206 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Thu, 28 Dec 2023 18:11:26 +0000 Subject: [PATCH] Codechange: Replace reliability magic numbers with constants. These are derived as a percentage of UINT16_MAX. --- src/engine.cpp | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/engine.cpp b/src/engine.cpp index 1481bb6b92..0293eba1f8 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -717,12 +717,23 @@ void StartupOneEngine(Engine *e, TimerGameCalendar::Date aging_date, uint32_t se e->type ^ e->GetGRFID()); - r = Random(); - e->reliability_start = GB(r, 16, 14) + 0x7AE0; - e->reliability_max = GB(r, 0, 14) + 0xBFFF; + /* Base reliability defined as a percentage of UINT16_MAX. */ + const uint16_t RELIABILITY_START = UINT16_MAX * 48 / 100; + const uint16_t RELIABILITY_MAX = UINT16_MAX * 75 / 100; + const uint16_t RELIABILITY_FINAL = UINT16_MAX * 25 / 100; + + static_assert(RELIABILITY_START == 0x7AE0); + static_assert(RELIABILITY_MAX == 0xBFFF); + static_assert(RELIABILITY_FINAL == 0x3FFF); r = Random(); - e->reliability_final = GB(r, 16, 14) + 0x3FFF; + /* 14 bits gives a value between 0 and 16383, which is up to an additional 25%p reliability on top of the base reliability. */ + e->reliability_start = GB(r, 16, 14) + RELIABILITY_START; + e->reliability_max = GB(r, 0, 14) + RELIABILITY_MAX; + + r = Random(); + e->reliability_final = GB(r, 16, 14) + RELIABILITY_FINAL; + e->duration_phase_1 = GB(r, 0, 5) + 7; e->duration_phase_2 = std::max(0, int(GB(r, 5, 4)) + ei->base_life.base() * 12 - 96); e->duration_phase_3 = GB(r, 9, 7) + 120;