From c8d0342ecb91a87ab69bad18d76211e89e3e0f08 Mon Sep 17 00:00:00 2001 From: Tijmen Zwaan Date: Sat, 15 Aug 2026 02:01:07 +0200 Subject: [PATCH] Slightly more correct sorting of haps for cps changes --- packages/core/cyclist.mjs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/packages/core/cyclist.mjs b/packages/core/cyclist.mjs index cbb9c6b0f..0468f7524 100644 --- a/packages/core/cyclist.mjs +++ b/packages/core/cyclist.mjs @@ -74,7 +74,23 @@ export class Cyclist { .queryArc(begin, end, { _cps: this.cps, cyclist: 'cyclist' }) // Sort the haps based on their onset, so cps changes within the // tick duration happen in the correct order. - .sort((a, b) => a.whole.begin.valueOf() - b.whole.begin.valueOf()); + .sort((a, b) => { + const aBegin = a.whole.begin.valueOf(); + const bBegin = b.whole.begin.valueOf(); + // If haps happen at the same time, trigger the haps that cause + // cps changes first, so the durations of the simultaneous haps + // are adjusted accordingly. + if (aBegin === bBegin) { + if (a.value.cps !== undefined) { + return b.value.cps !== undefined ? 0 : 1; + } + if (b.value.cps !== undefined) { + return -1; + } + return 0; + } + return aBegin - bBegin; + }); haps.forEach((hap) => { if (hap.hasOnset()) {