diff --git a/src/Area.js b/src/Area.js index c4f2a4ba..cb1e7568 100644 --- a/src/Area.js +++ b/src/Area.js @@ -179,6 +179,16 @@ class Area extends GameEntity { room.hydrate(state); } } + + /** + * Get all possible broadcast targets within an area. This includes all npcs, + * players, rooms, and the area itself + * @return {Array} + */ + getBroadcastTargets() { + const roomTargets = [...this.rooms].reduce((acc, [, room]) => acc.concat(room.getBroadcastTargets()), []); + return [this, ...roomTargets]; + } } module.exports = Area; diff --git a/src/AreaAudience.js b/src/AreaAudience.js index 77039ab6..a505c45f 100644 --- a/src/AreaAudience.js +++ b/src/AreaAudience.js @@ -13,16 +13,8 @@ class AreaAudience extends ChannelAudience { return []; } - // It would be more elegant to just pass the area but that could be very - // inefficient as it's much more likely that there are fewer players than - // there are rooms in the area - const players = this.state.PlayerManager.filter(player => - player.room && - (player.room.area === this.sender.room.area) && - (player !== this.sender) - ); - - return players.concat(this.sender.room.area.npcs); + const { area } = this.sender.room; + return area.getBroadcastTargets().filter(target => target !== this.sender); } } diff --git a/src/AreaFactory.js b/src/AreaFactory.js index 1bef84fc..a99f8fb1 100644 --- a/src/AreaFactory.js +++ b/src/AreaFactory.js @@ -26,7 +26,7 @@ class AreaFactory extends EntityFactory { const area = new Area(definition.bundle, entityRef, definition.manifest); if (this.scripts.has(entityRef)) { - this.scripts.get(entityRef).attach(entity); + this.scripts.get(entityRef).attach(area); } return area; diff --git a/src/BundleManager.js b/src/BundleManager.js index 1cec8c77..042520b5 100644 --- a/src/BundleManager.js +++ b/src/BundleManager.js @@ -270,13 +270,13 @@ class BundleManager { const scriptPath = this._getAreaScriptPath(bundle, areaName); if (manifest.script) { - const scriptPath = `${scriptPath}/${area.script}.js`; - if (!fs.existsSync(scriptPath)) { - Logger.warn(`\t\t\t[${areaName}] has non-existent script "${area.script}"`); + const areaScriptPath = `${scriptPath}/${manifest.script}.js`; + if (!fs.existsSync(areaScriptPath)) { + Logger.warn(`\t\t\t[${areaName}] has non-existent script "${manifest.script}"`); } - Logger.verbose(`\t\t\tLoading Item Script [${entityRef}] ${item.script}`); - this.loadEntityScript(this.state.AreaFactory, entityRef, scriptPath); + Logger.verbose(`\t\t\tLoading Area Script for [${areaName}]: ${manifest.script}`); + this.loadEntityScript(this.state.AreaFactory, areaName, areaScriptPath); } Logger.verbose(`\t\tLOAD: Quests...`);