18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-21 05:04:58 +01:00
Files
maszyna/McZapkie
maj00r 1b26caca24 fix: inverted clamp bounds give the upper bound again, as the old clamp did (#412)
* fix: inverted clamp bounds give the upper bound again, as the old clamp did

The project's own clamp() of 26.05 applied the lower bound first and the
upper one last, so bounds given the wrong way round always gave the upper
one. Its calls became std::clamp (undefined for such bounds) and, where the
bounds can cross, safe_clamp, which swapped them instead. Neither gives the
old result.

- safe_clamp is the old clamp again, min(max(value, lo), hi). This restores
  26.05 at every place it is used, among them the AI's dynamic brake test
  (its demand was fAccThreshold x1.21, the swap made it x1.01), the
  generator voltage, the heavy cargo acceleration of the AI and the EP
  correction of the brake.
- The cab camera is kept inside the cab with safe_clamp. The bounds come
  from the .mmd as written and some cabs have them reversed (the B cars of
  SA134 and 21xM, ET40, 754); with std::clamp the camera jumped between
  the two bounds every frame on MSVC, as its result is fed back.

* fix: every call of the old clamp gives what it gave up to 26.05.1

d1f16411 (#99) made every call of the project's clamp() std::clamp. Where the bounds come from the data or
are computed at runtime they can cross, and then std::clamp gives another result (undefined behaviour;
MSVC gives the lower bound unless the value is above the upper one) where the old clamp gave the upper
bound. These calls use safe_clamp, the old clamp, too:

- Mover: the pantograph pressure (MaxPress), the compressor switch (CompressorListPosNo), the coupler
  distance (DmaxB, DmaxC), the engine revolutions, the eimic ramp (CtrlDelay), the eimic limit
  (eimic_max), the speed control ramp (PowerUpSpeed, PowerDownSpeed), the load (MaxLoad), the magnet of
  the security system (Dim.L);
- DynObj: the coupler distance of the buffers (DmaxB), the axle distance (Dim.L);
- Train: the brake handle positions (BrakeCtrlPosNo, the range of the handle);
- the legacy renderer: the mouse pick (the window size).

With the bounds in order nothing changes. Calls whose bounds the code keeps in order (constants,
SplineFidelity 1-4, the season table, the guarded output calibration) give the same result with
std::clamp and stay as they are.

The load offset takes its arguments in the 26.05.1 order again; fa2ed089 swapped them, which with the
old clamp differs only for a negative load.

The bump at a rail joint gives the old result again. Up to 26.05.1 its clamps had 0 in place of the
value (since 96f43e11 and 4d72d352), and the old clamp made that the headroom Vmax - (Vel + 0.32 Vmax),
negative above 68% of the top speed, capped from above only; d464628a and 03bfbb13 kept it at 0 or
above. Only damaged track has the bump: a damage flag in the .scn, a derailer set by an event, a track
made on a derailment.

The interior light of the cab in the 3.3 renderer is computed in double again, as before d1f16411.

The call added since (stepped_uc_value, 00955513) keeps what master does: bounds given the other way
round are put in order.

* fix: the alpha of a particle in the legacy renderer as up to 26.05.1, the NaN cases of the clamp tested

Up to 26.05.1 the alpha was clamp<std::uint8_t>( particle.opacity * 255, 0, 255 ): the float was
made a byte first, so the clamp did nothing and an opacity over 1 (a smoke max_opacity over 1)
wrapped round, 1.2 giving 50. The move to std::clamp made it 255. It's made a byte as then again,
through int: the byte the x86-64 compilers gave, without the undefined behaviour of that conversion
(MSVC makes the same two instructions of both). openglparticles.cpp goes with feat/gl46-migration;
its opengl33 counterpart, std::clamp( opacity, 0, 1 ), already gives what 26.05.1 gave.

The tests pin what the old clamp gave with NaN: a NaN value stays NaN, a NaN bound doesn't clamp.
The checks named after the AI braking test and the bump at a rail joint say they test the helper with
the arguments of those calls, not the calls.

* test: the includes of the clamp tests before the project's headers

<cmath> and <limits> go up, after the test headers, so the change no longer touches the lines
#416 adds its includes after. With both merged <cmath> is included twice, which is harmless.

---------

Co-authored-by: maj00r <maj00r@host>
2026-11-20 03:39:29 +01:00
..