From 87c1111b63102e4f7427f875d30eabc62b4ccbc8 Mon Sep 17 00:00:00 2001 From: Jordan Irwin Date: Mon, 15 Jul 2024 09:34:42 -0700 Subject: [PATCH] Don't load sounds for events that are too far to be heard https://github.com/arianne/stendhal/issues/742 --- doc/CHANGES.txt | 1 + src/js/stendhal/entity/User.ts | 21 +++++++++++++++++++++ src/js/stendhal/event/SoundEvent.ts | 9 +++++++++ 3 files changed, 31 insertions(+) diff --git a/doc/CHANGES.txt b/doc/CHANGES.txt index a6521a3f6f..5f864c97df 100644 --- a/doc/CHANGES.txt +++ b/doc/CHANGES.txt @@ -24,6 +24,7 @@ Changelog - added support for animated entity overlay effects - added transformation effect to vampires - cleaned up bestiary window layout +- performance optimization: sound events too far away to be heard are not loaded 1.47 diff --git a/src/js/stendhal/entity/User.ts b/src/js/stendhal/entity/User.ts index 75fc51b642..353ee34152 100644 --- a/src/js/stendhal/entity/User.ts +++ b/src/js/stendhal/entity/User.ts @@ -136,6 +136,27 @@ export class User extends Player { && (Math.abs(this["y"] - entity["y"]) < hearingRange)); } + /** + * Checks if player is within range to hear a sound event. + * + * @param {number} radius + * Distance at which sound can be heard. + * @param {Entity} entity + * Entity emitting sound event. + * @returns {boolean} + * `true` if sound should be loaded (when event radius is more than -1 & user position is within + * radial distance from event origin). + */ + isInSoundRange(radius: number, entity: Entity): boolean { + if (entity === this) { + return true; + } + if (radius < 0) { + return false; + } + return Math.abs(this["x"] - entity["x"]) + Math.abs(this["y"] - entity["y"]) <= radius; + } + /** * Actions when player leaves a zone. */ diff --git a/src/js/stendhal/event/SoundEvent.ts b/src/js/stendhal/event/SoundEvent.ts index f50ff89ee3..7a0654847b 100644 --- a/src/js/stendhal/event/SoundEvent.ts +++ b/src/js/stendhal/event/SoundEvent.ts @@ -15,6 +15,7 @@ import { RPEvent } from "./RPEvent"; import { SoundID } from "../data/sound/SoundID"; import { SoundManager } from "../data/sound/SoundManager"; +declare var marauroa: any; declare var stendhal: any; @@ -28,10 +29,18 @@ export class SoundEvent extends RPEvent { execute(entity: any) { + if (!marauroa.me) { + return; + } let radius = SoundManager.DEFAULT_RADIUS; if (typeof(this["radius"]) === "number") { radius = this["radius"]; } + if (!marauroa.me.isInSoundRange(radius, entity)) { + // too far away to hear so don't load + return; + } + let volume = 1; // Adjust by the server specified volume, if any if (this.hasOwnProperty("volume")) {