From 8e2efb07561963b172f96c7b3ec91a19d8a6ba5a Mon Sep 17 00:00:00 2001 From: tobspr Date: Mon, 7 Dec 2020 19:15:57 +0100 Subject: [PATCH] Fix belts being too slow sometimes, closes #999 --- src/js/changelog.js | 1 + src/js/game/belt_path.js | 22 +++++++++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/js/changelog.js b/src/js/changelog.js index 80ae69f6..3d3ba951 100644 --- a/src/js/changelog.js +++ b/src/js/changelog.js @@ -3,6 +3,7 @@ export const CHANGELOG = [ version: "1.2.2", date: "07.12.2020", entries: [ + "Fix item readers and some other buildings slowing up belts, especially if they stalled (inspired by Keterr's fix)", "Added the ability to edit constant signals by left clicking them", "You can now add markers in the wire layer (partially by daanbreur)", "Allow to cycle backwards in the toolbar with SHIFT + Tab (idea by EmeraldBlock)", diff --git a/src/js/game/belt_path.js b/src/js/game/belt_path.js index eb55d613..dde81549 100644 --- a/src/js/game/belt_path.js +++ b/src/js/game/belt_path.js @@ -1085,6 +1085,9 @@ export class BeltPath extends BasicSerializableObject { // Reduce the spacing nextDistanceAndItem[_nextDistance] -= clampedProgress; + // Advance all items behind by the progress we made + this.spacingToFirstItem += clampedProgress; + // If the last item can be ejected, eject it and reduce the spacing, because otherwise // we lose velocity if (isFirstItemProcessed && nextDistanceAndItem[_nextDistance] < 1e-7) { @@ -1097,6 +1100,24 @@ export class BeltPath extends BasicSerializableObject { if (this.tryHandOverItem(nextDistanceAndItem[_item], excessVelocity)) { this.items.pop(); + const itemBehind = this.items[lastItemProcessed - 1]; + if (itemBehind && this.numCompressedItemsAfterFirstItem > 0) { + // So, with the next tick we will skip this item, but it actually has the potential + // to process farther -> If we don't advance here, we loose a tiny bit of progress + // every tick which causes the belt to be slower than it actually is. + // Also see #999 + const fixupProgress = Math.max( + 0, + Math.min(remainingVelocity, itemBehind[_nextDistance]) + ); + + // See above + itemBehind[_nextDistance] -= fixupProgress; + remainingVelocity -= fixupProgress; + this.spacingToFirstItem += fixupProgress; + } + + // Reduce the number of compressed items since the first item no longer exists this.numCompressedItemsAfterFirstItem = Math.max( 0, this.numCompressedItemsAfterFirstItem - 1 @@ -1110,7 +1131,6 @@ export class BeltPath extends BasicSerializableObject { } isFirstItemProcessed = false; - this.spacingToFirstItem += clampedProgress; if (remainingVelocity < 1e-7) { break; }