diff --git a/src/engine/action/action-pipeline.ts b/src/engine/action/action-pipeline.ts index 3cfe638e..3b319928 100644 --- a/src/engine/action/action-pipeline.ts +++ b/src/engine/action/action-pipeline.ts @@ -6,6 +6,7 @@ import { LandscapeObject } from '@runejs/filestore'; import { Actor, Player } from '@engine/world/actor'; import { ActionHook, TaskExecutor } from '@engine/action'; import { Position } from '@engine/world'; +import { isPlayer } from '@engine/world/actor/util'; /** @@ -200,7 +201,7 @@ export class ActionPipeline { } public get paused(): boolean { - if(this.actor instanceof Player) { + if(isPlayer(this.actor)) { if(this.actor.interfaceState.widgetOpen()) { return true; } diff --git a/src/engine/action/hook/task.ts b/src/engine/action/hook/task.ts index 3677c966..0738a485 100644 --- a/src/engine/action/hook/task.ts +++ b/src/engine/action/hook/task.ts @@ -6,6 +6,7 @@ import { logger } from '@runejs/common'; import { ActionHook, ActionStrength } from '@engine/action'; import { World } from '@engine/world'; import { Actor, Player, Npc } from '@engine/world/actor'; +import { isNpc, isPlayer } from '@engine/world/actor/util'; export type TaskSessionData = { [key: string]: any }; @@ -149,8 +150,8 @@ export class TaskExecutor { public getDetails(): TaskDetails { return { actor: this.actor, - player: this.actor.isPlayer() ? this.actor : undefined, - npc: this.actor.isNpc() ? this.actor : undefined, + player: isPlayer(this.actor) ? this.actor : undefined, + npc: isNpc(this.actor) ? this.actor : undefined, actionData: this.actionData, session: this.session }; diff --git a/src/engine/world/actor/actor.ts b/src/engine/world/actor/actor.ts index 7d48c853..621d7159 100644 --- a/src/engine/world/actor/actor.ts +++ b/src/engine/world/actor/actor.ts @@ -17,6 +17,7 @@ import { ObjectConfig } from '@runejs/filestore'; import { QueueableTask } from '@engine/action/pipe/task/queueable-task'; import { Npc } from '@engine/world/actor/npc'; import { Player } from '@engine/world/actor/player'; +import { isNpc } from '@engine/world/actor/util'; export type ActorType = 'player' | 'npc'; @@ -357,7 +358,7 @@ export abstract class Actor { return; } - if(this.isNpc()) { + if(isNpc(this)) { const nearbyPlayers = activeWorld.findNearbyPlayers(this.position, 24, this.instance.instanceId); if(nearbyPlayers.length === 0) { // No need for this actor to move if there are no players nearby to witness it, save some memory. :) @@ -485,14 +486,6 @@ export abstract class Actor { this.scheduler.tick(); } - public isPlayer(): this is Player { - return this.type === 'player'; - } - - public isNpc(): this is Npc { - return this.type === 'npc'; - } - public get position(): Position { return this._position; } diff --git a/src/engine/world/actor/npc.ts b/src/engine/world/actor/npc.ts index b86dc41e..7cdea91f 100644 --- a/src/engine/world/actor/npc.ts +++ b/src/engine/world/actor/npc.ts @@ -10,6 +10,7 @@ import { Actor } from './actor'; import { Player } from './player'; import { SkillName } from './skills'; import { logger } from '@runejs/common'; +import { isPlayer } from '@engine/world/actor/util'; /** * Represents a non-player character within the game world. @@ -135,7 +136,7 @@ export class Npc extends Actor { return; } - if(assailant instanceof Player) { + if(isPlayer(assailant)) { const itemDrops = calculateNpcDrops(assailant, npcDetails); itemDrops.forEach(drop => { const droppedItem = findItem(drop.itemKey); @@ -151,7 +152,7 @@ export class Npc extends Actor { } activeWorld.globalInstance.spawnWorldItem({ itemId: droppedItem.gameId, amount: drop.amount }, - deathPosition, { owner: assailant instanceof Player ? assailant : undefined, expires: 300 }); + deathPosition, { owner: assailant, expires: 300 }); }) } }); diff --git a/src/engine/world/actor/pathfinding.ts b/src/engine/world/actor/pathfinding.ts index c02eb914..634dd257 100644 --- a/src/engine/world/actor/pathfinding.ts +++ b/src/engine/world/actor/pathfinding.ts @@ -6,6 +6,7 @@ import { logger } from '@runejs/common'; import { WorldInstance } from '@engine/world/instances'; import { Tile } from '@engine/world/map/chunk-manager'; import { activeWorld } from '@engine/world'; +import { isPlayer } from '@engine/world/actor/util'; class Point { @@ -447,7 +448,7 @@ export class Pathfinding { } private get instance(): WorldInstance { - return this.actor instanceof Player ? this.actor.instance : activeWorld.globalInstance; + return isPlayer(this.actor) ? this.actor.instance : activeWorld.globalInstance; } } diff --git a/src/engine/world/actor/player/player.ts b/src/engine/world/actor/player/player.ts index 634c900b..15097d5f 100644 --- a/src/engine/world/actor/player/player.ts +++ b/src/engine/world/actor/player/player.ts @@ -1048,7 +1048,7 @@ export class Player extends Actor { * Transform's the player's appearance into the specified NPC. * @param npc The NPC to copy the appearance of, or null to reset. */ - public transformInto(npc: Npc | NpcDetails | string | number | null): void { + public transformInto(npc: NpcDetails | string | number | null): void { if(!npc) { delete this.savedMetadata.npcTransformation; this.updateFlags.appearanceUpdateRequired = true; @@ -1062,8 +1062,6 @@ export class Player extends Actor { } else { npc = parseInt(npc, 10); } - } else if(npc instanceof Npc) { - npc = npc.id; } else { npc = npc.gameId; } diff --git a/src/engine/world/actor/player/sync/actor-sync.ts b/src/engine/world/actor/player/sync/actor-sync.ts index bbacf0c0..83da3b39 100644 --- a/src/engine/world/actor/player/sync/actor-sync.ts +++ b/src/engine/world/actor/player/sync/actor-sync.ts @@ -7,6 +7,7 @@ import { QuadtreeKey } from '@engine/world'; import { Actor } from '@engine/world/actor/actor'; import { Player } from '../player'; import { activeWorld } from '@engine/world'; +import { isNpc, isPlayer } from '@engine/world/actor/util'; /** @@ -31,7 +32,7 @@ export function registerNewActors(packet: Packet, player: Player, trackedActors: for(const newActor of newActors) { const nearbyActor = newActor.actor; - if(nearbyActor instanceof Player) { + if(isPlayer(nearbyActor)) { if(player.equals(nearbyActor)) { // Other player is actually this player! continue; @@ -41,7 +42,7 @@ export function registerNewActors(packet: Packet, player: Player, trackedActors: // Other player is no longer in the game world continue; } - } else if(nearbyActor instanceof Npc) { + } else if(isNpc(nearbyActor)) { if(!activeWorld.npcExists(nearbyActor)) { // Npc is no longer in the game world continue; @@ -86,7 +87,7 @@ export function syncTrackedActors(packet: Packet, playerPosition: Position, appe const trackedActor: Actor = trackedActors[i]; let exists = true; - if(trackedActor.isPlayer()) { + if(isPlayer(trackedActor)) { if(!activeWorld.playerOnline(trackedActor)) { exists = false; } diff --git a/src/engine/world/actor/player/sync/npc-sync-task.ts b/src/engine/world/actor/player/sync/npc-sync-task.ts index db8a8c54..12a79268 100644 --- a/src/engine/world/actor/player/sync/npc-sync-task.ts +++ b/src/engine/world/actor/player/sync/npc-sync-task.ts @@ -5,6 +5,7 @@ import { Npc } from '@engine/world/actor/npc'; import { registerNewActors, SyncTask, syncTrackedActors } from './actor-sync'; import { Player } from '../player'; import { activeWorld } from '@engine/world'; +import { isPlayer } from '@engine/world/actor/util'; /** @@ -146,7 +147,7 @@ export class NpcSyncTask extends SyncTask { } else { let worldIndex = actor.worldIndex; - if(actor instanceof Player) { + if(isPlayer(actor)) { // Client checks if index is less than 32768. // If it is, it looks for an NPC. // If it isn't, it looks for a player (subtracting 32768 to find the index). diff --git a/src/engine/world/actor/player/sync/player-sync-task.ts b/src/engine/world/actor/player/sync/player-sync-task.ts index 11333c87..8e0fa881 100644 --- a/src/engine/world/actor/player/sync/player-sync-task.ts +++ b/src/engine/world/actor/player/sync/player-sync-task.ts @@ -8,6 +8,7 @@ import { EquipmentSlot, EquipmentType, ItemDetails } from '@engine/config/item-c import { appendMovement, registerNewActors, SyncTask, syncTrackedActors } from './actor-sync'; import { Player } from '../player'; import { activeWorld } from '@engine/world'; +import { isPlayer } from '@engine/world/actor/util'; /** @@ -173,7 +174,7 @@ export class PlayerSyncTask extends SyncTask { const actor = updateFlags.faceActor; let worldIndex = actor.worldIndex; - if(actor instanceof Player) { + if(isPlayer(actor)) { // Client checks if index is less than 32768. // If it is, it looks for an NPC. // If it isn't, it looks for a player (subtracting 32768 to find the index). diff --git a/src/engine/world/actor/skills.ts b/src/engine/world/actor/skills.ts index 724515da..e74aaa82 100644 --- a/src/engine/world/actor/skills.ts +++ b/src/engine/world/actor/skills.ts @@ -4,6 +4,7 @@ import { gfxIds } from '@engine/world/config'; import { Actor } from './actor'; import { Player } from './player'; import { QueueableTask } from '@engine/action/pipe/task/queueable-task'; +import { isPlayer } from '@engine/world/actor/util'; export enum Skill { ATTACK, @@ -226,14 +227,14 @@ export class Skills extends SkillShortcuts { this.setExp(skill, finalExp); - if(this.actor.isPlayer()) { + if(isPlayer(this.actor)) { this.actor.outgoingPackets.updateSkill(this.getSkillId(skill), finalLevel, finalExp); } if(currentLevel !== finalLevel) { this.setLevel(skill, finalLevel); - if(this.actor.isPlayer()) { + if(isPlayer(this.actor)) { const achievementDetails = skillDetails[this.getSkillId(skill)]; if(!achievementDetails) { return; @@ -262,7 +263,7 @@ export class Skills extends SkillShortcuts { } public showLevelUpDialogue(skill: number | SkillName, level: number): void { - if(!(this.actor.isPlayer())) { + if(!(isPlayer(this.actor))) { return; } diff --git a/src/engine/world/actor/util.ts b/src/engine/world/actor/util.ts new file mode 100644 index 00000000..2a64fa53 --- /dev/null +++ b/src/engine/world/actor/util.ts @@ -0,0 +1,6 @@ +import type { Npc } from './npc'; +import type { Actor } from './actor'; +import type { Player } from './player/player'; + +export const isPlayer = (actor: Actor): actor is Player => actor.type === 'player'; +export const isNpc = (actor: Actor): actor is Npc => actor.type === 'npc'; diff --git a/src/engine/world/actor/walking-queue.ts b/src/engine/world/actor/walking-queue.ts index 0e9902a3..b6f41c35 100644 --- a/src/engine/world/actor/walking-queue.ts +++ b/src/engine/world/actor/walking-queue.ts @@ -6,6 +6,7 @@ import { regionChangeActionFactory } from '@engine/action'; import { Subject } from 'rxjs'; import { activeWorld } from '@engine/world'; import { logger } from '@runejs/common'; +import { isNpc, isPlayer } from '@engine/world/actor/util'; /** @@ -180,7 +181,7 @@ export class WalkingQueue { let runDir = -1; // @TODO npc running - if(this.actor instanceof Player) { + if(isPlayer(this.actor)) { if(this.actor.settings.runEnabled && this.queue.length !== 0) { const runPosition = this.queue.shift(); @@ -217,7 +218,7 @@ export class WalkingQueue { this.movementEvent.next(this.actor.position); - if(this.actor instanceof Player) { + if(isPlayer(this.actor)) { const mapDiffX = this.actor.position.x - (lastMapRegionUpdatePosition.chunkX * 8); const mapDiffY = this.actor.position.y - (lastMapRegionUpdatePosition.chunkY * 8); if(mapDiffX < 16 || mapDiffX > 87 || mapDiffY < 16 || mapDiffY > 87) { @@ -227,12 +228,12 @@ export class WalkingQueue { } if(!oldChunk.equals(newChunk)) { - if(this.actor instanceof Player) { + if(isPlayer(this.actor)) { this.actor.metadata.updateChunk = { newChunk, oldChunk }; this.actor.actionPipeline.call('region_change', regionChangeActionFactory( this.actor, originalPosition, this.actor.position)); - } else if(this.actor instanceof Npc) { + } else if(isNpc(this.actor)) { oldChunk.removeNpc(this.actor); newChunk.addNpc(this.actor); } diff --git a/src/engine/world/world.ts b/src/engine/world/world.ts index 99955739..21a43ce0 100644 --- a/src/engine/world/world.ts +++ b/src/engine/world/world.ts @@ -15,6 +15,7 @@ import { TravelLocations, ExamineCache, parseScenerySpawns } from '@engine/world import { loadPlugins } from '@engine/plugins'; import { TaskScheduler, Task } from '@engine/task'; import { Isaac } from '@engine/net'; +import { isPlayer } from '@engine/world/actor/util'; export interface QuadtreeKey { @@ -100,7 +101,7 @@ export class World { const objectChunk = this.chunkManager.getChunkForWorldPosition(objectPosition); let customMap = false; - if(actor instanceof Player && actor.metadata.customMap) { + if(isPlayer(actor) && actor.metadata.customMap) { customMap = true; const templateMapObject = this.findCustomMapObject(actor, objectId, objectPosition); if(templateMapObject) { @@ -113,7 +114,7 @@ export class World { let tileModifications; let personalTileModifications; - if(actor.isPlayer()) { + if(isPlayer(actor)) { const instance = actor.instance; if (!instance) { @@ -130,7 +131,7 @@ export class World { if(!landscapeObject) { const tileObjects = [ ...tileModifications.mods.spawnedObjects ]; - if(actor.isPlayer()) { + if(isPlayer(actor)) { tileObjects.push(...personalTileModifications.mods.spawnedObjects); } @@ -146,7 +147,7 @@ export class World { const hiddenTileObjects = [ ...tileModifications.mods.hiddenObjects ]; - if(actor.isPlayer()) { + if(isPlayer(actor)) { hiddenTileObjects.push(...personalTileModifications.mods.hiddenObjects); }