From 1648a40aa860b54ceba4d1bf7c17b6259e55c169 Mon Sep 17 00:00:00 2001 From: Terncode Date: Wed, 2 Dec 2026 20:48:03 +0100 Subject: [PATCH] Fix boat bobbing flicker from time overflow --- src/ts/client/worldMap.ts | 5 ++--- src/ts/common/tileUtils.ts | 5 ++--- src/ts/common/utils.ts | 5 +++++ src/ts/tests/common/utils.spec.ts | 23 ++++++++++++++++++++++- 4 files changed, 31 insertions(+), 7 deletions(-) diff --git a/src/ts/client/worldMap.ts b/src/ts/client/worldMap.ts index 8947fd7..b257f65 100644 --- a/src/ts/client/worldMap.ts +++ b/src/ts/client/worldMap.ts @@ -2,7 +2,7 @@ import { Entity, Point, TileType, Rect, MapInfo, Camera, Region, MapState, defaultMapState, Pony, EntityFlags, WorldMap, EntityState, } from '../common/interfaces'; -import { contains, removeItem, boundsIntersect, array, pushUniq, containsPoint, removeItemFast } from '../common/utils'; +import { contains, removeItem, boundsIntersect, array, pushUniq, containsPoint, removeItemFast, getTimeFrame } from '../common/utils'; import { isBoundsVisible, } from '../common/camera'; import { getRegionElevation, setRegionElevation, @@ -487,8 +487,7 @@ export function updateEntities(game: PonyTownGame, gameTime: number, delta: numb if ((flags & EntityFlags.Bobbing) !== 0) { const bobs = entity.bobs!; - const frame = (((gameTime / 1000) * entity.bobsFps!) | 0) % bobs.length; - entity.z = toWorldZ(bobs[frame]); + entity.z = toWorldZ(bobs[getTimeFrame(gameTime, entity.bobsFps!, bobs.length)]); } else if ((flags & EntityFlags.StaticY) === 0) { entity.z = getMapHeightAt(map, entity.x, entity.y, gameTime); diff --git a/src/ts/common/tileUtils.ts b/src/ts/common/tileUtils.ts index caf65a4..fe759e4 100644 --- a/src/ts/common/tileUtils.ts +++ b/src/ts/common/tileUtils.ts @@ -6,7 +6,7 @@ import { import * as sprites from '../generated/sprites'; import { getRegionTile, getRegionElevation, setRegionTile, getRegionTileIndex, setRegionTileDirty, doRelativeToRegion, getRegionGlobal } from './region'; import { tileWidth, tileHeight, tileElevation, WATER_FPS, REGION_SIZE, WATER_HEIGHT } from './constants'; -import { clamp, toInt, at, invalidEnumReturn } from './utils'; +import { clamp, toInt, at, invalidEnumReturn, getTimeFrame } from './utils'; import { isAreaVisible } from './camera'; import { WHITE } from './colors'; import { releasePalette } from '../graphics/paletteManager'; @@ -604,8 +604,7 @@ export function getTileHeight( return 0.5; } else if (typeNumber === TileTypeNumber.Boat) { - const frame = ((gameTime / 1000) * WATER_FPS) | 0; - return waterHeight[frame % waterHeight.length]; + return waterHeight[getTimeFrame(gameTime, WATER_FPS, waterHeight.length)]; } return 0; diff --git a/src/ts/common/utils.ts b/src/ts/common/utils.ts index d841140..03c1a4c 100644 --- a/src/ts/common/utils.ts +++ b/src/ts/common/utils.ts @@ -168,6 +168,11 @@ export function toInt(value: any): number { return value | 0; } +export function getTimeFrame(timeMs: number, fps: number, frames: number): number { + const frame = Math.floor((timeMs / 1000) * fps) % frames; + return frame < 0 ? frame + frames : frame; +} + export function dispose(obj: T | undefined): undefined { if (obj) { obj.dispose(); diff --git a/src/ts/tests/common/utils.spec.ts b/src/ts/tests/common/utils.spec.ts index 3613dac..777475d 100644 --- a/src/ts/tests/common/utils.spec.ts +++ b/src/ts/tests/common/utils.spec.ts @@ -5,7 +5,7 @@ import { spy, assert, SinonFakeTimers, useFakeTimers } from 'sinon'; import { arraysEqual, removeItem, removeById, dispose, fromNow, clamp, normalize, toInt, hasFlag, setFlag, at, findById, distance, contains, maxDate, minDate, - flatten, includes, isCommand, processCommand, collidersIntersect, formatDuration, att + flatten, includes, isCommand, processCommand, collidersIntersect, formatDuration, att, getTimeFrame } from '../../common/utils'; import { rect } from '../../common/rect'; @@ -132,6 +132,27 @@ describe('utils', () => { }); }); + describe('getTimeFrame()', () => { + it('returns frame for given time', () => { + expect(getTimeFrame(0, 6, 4)).equal(0); + expect(getTimeFrame(500, 6, 4)).equal(3); + expect(getTimeFrame(1000, 6, 4)).equal(2); + }); + + it('returns valid frame for timestamps that overflow 32 bit integers', () => { + const time = Date.UTC(2026, 11, 3); + + for (let i = 0; i < 12; i++) { + const t = time + i * 1000 / 6; + expect(getTimeFrame(t, 6, 4)).equal(Math.floor(t / 1000 * 6) % 4); + } + }); + + it('returns positive frame for negative time', () => { + expect(getTimeFrame(-1000, 6, 4)).equal(2); + }); + }); + describe('toInt()', () => { it('returns given integer value', () => { expect(toInt(1)).equal(1);