Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix circular deps with minimal changes #448

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/engine/action/action-pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';


/**
Expand Down Expand Up @@ -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;
}
Expand Down
5 changes: 3 additions & 2 deletions src/engine/action/hook/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down Expand Up @@ -149,8 +150,8 @@ export class TaskExecutor<T> {
public getDetails(): TaskDetails<T> {
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
};
Expand Down
11 changes: 2 additions & 9 deletions src/engine/world/actor/actor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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. :)
Expand Down Expand Up @@ -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;
}
Expand Down
5 changes: 3 additions & 2 deletions src/engine/world/actor/npc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand All @@ -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 });
})
}
});
Expand Down
3 changes: 2 additions & 1 deletion src/engine/world/actor/pathfinding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}

}
4 changes: 1 addition & 3 deletions src/engine/world/actor/player/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down
7 changes: 4 additions & 3 deletions src/engine/world/actor/player/sync/actor-sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';


/**
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
3 changes: 2 additions & 1 deletion src/engine/world/actor/player/sync/npc-sync-task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';


/**
Expand Down Expand Up @@ -146,7 +147,7 @@ export class NpcSyncTask extends SyncTask<void> {
} 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).
Expand Down
3 changes: 2 additions & 1 deletion src/engine/world/actor/player/sync/player-sync-task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';


/**
Expand Down Expand Up @@ -173,7 +174,7 @@ export class PlayerSyncTask extends SyncTask<void> {
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).
Expand Down
7 changes: 4 additions & 3 deletions src/engine/world/actor/skills.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down
6 changes: 6 additions & 0 deletions src/engine/world/actor/util.ts
Original file line number Diff line number Diff line change
@@ -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';
9 changes: 5 additions & 4 deletions src/engine/world/actor/walking-queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';


/**
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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) {
Expand All @@ -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);
}
Expand Down
9 changes: 5 additions & 4 deletions src/engine/world/world.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand All @@ -113,7 +114,7 @@ export class World {
let tileModifications;
let personalTileModifications;

if(actor.isPlayer()) {
if(isPlayer(actor)) {
const instance = actor.instance;

if (!instance) {
Expand All @@ -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);
}

Expand All @@ -146,7 +147,7 @@ export class World {

const hiddenTileObjects = [ ...tileModifications.mods.hiddenObjects ];

if(actor.isPlayer()) {
if(isPlayer(actor)) {
hiddenTileObjects.push(...personalTileModifications.mods.hiddenObjects);
}

Expand Down