From 124affe38967ba1d7fcc4c2e476d2073ac22b1e2 Mon Sep 17 00:00:00 2001 From: Flavius Poenaru Date: Mon, 18 Sep 2023 16:51:11 -0600 Subject: [PATCH] remove: old resource system --- packages/common/network/modules.ts | 5 +- packages/common/types/map.d.ts | 4 - .../game/entity/character/player/player.ts | 23 +-- .../character/player/skill/impl/fishing.ts | 2 +- .../character/player/skill/impl/foraging.ts | 2 +- .../character/player/skill/impl/mining.ts | 4 +- packages/server/src/game/entity/entity.ts | 30 ++++ .../entity/objects/resource/impl/fishspot.ts | 10 ++ .../entity/objects/resource/impl/foraging.ts | 10 ++ .../game/entity/objects/resource/impl/rock.ts | 10 ++ packages/server/src/game/globals/fishspots.ts | 24 --- packages/server/src/game/globals/foraging.ts | 24 --- packages/server/src/game/globals/globals.ts | 44 ----- .../server/src/game/globals/impl/resource.ts | 170 ------------------ packages/server/src/game/globals/resources.ts | 144 --------------- packages/server/src/game/globals/rocks.ts | 24 --- packages/server/src/game/globals/trees.ts | 24 --- packages/server/src/game/map/map.ts | 19 +- packages/server/src/game/map/region.ts | 38 ---- packages/server/src/game/map/regions.ts | 54 +----- packages/tools/map/parser/parser.ts | 157 +--------------- 21 files changed, 78 insertions(+), 744 deletions(-) create mode 100644 packages/server/src/game/entity/objects/resource/impl/fishspot.ts create mode 100644 packages/server/src/game/entity/objects/resource/impl/foraging.ts create mode 100644 packages/server/src/game/entity/objects/resource/impl/rock.ts delete mode 100644 packages/server/src/game/globals/fishspots.ts delete mode 100644 packages/server/src/game/globals/foraging.ts delete mode 100644 packages/server/src/game/globals/impl/resource.ts delete mode 100644 packages/server/src/game/globals/resources.ts delete mode 100644 packages/server/src/game/globals/rocks.ts delete mode 100644 packages/server/src/game/globals/trees.ts diff --git a/packages/common/network/modules.ts b/packages/common/network/modules.ts index f0029aa773..dc4b2f9a2e 100644 --- a/packages/common/network/modules.ts +++ b/packages/common/network/modules.ts @@ -40,7 +40,10 @@ export enum EntityType { Pet, LootBag, Effect, - Tree + Tree, + Rock, + Foraging, + FishSpot } export enum Interfaces { diff --git a/packages/common/types/map.d.ts b/packages/common/types/map.d.ts index 35cec90f71..722dd845ae 100644 --- a/packages/common/types/map.d.ts +++ b/packages/common/types/map.d.ts @@ -170,8 +170,4 @@ export interface ProcessedMap { obstructing?: number[]; areas: { [name: string]: ProcessedArea[] }; cursors: { [tileId: number]: string }; - trees: ProcessedResource[]; - rocks: ProcessedResource[]; - fishSpots: ProcessedResource[]; - foraging: ProcessedResource[]; } diff --git a/packages/server/src/game/entity/character/player/player.ts b/packages/server/src/game/entity/character/player/player.ts index 3e9d70f2f2..8fabc34b72 100644 --- a/packages/server/src/game/entity/character/player/player.ts +++ b/packages/server/src/game/entity/character/player/player.ts @@ -51,7 +51,6 @@ import type World from '../../../world'; import type Area from '../../../map/areas/area'; import type Regions from '../../../map/regions'; import type Connection from '../../../../network/connection'; -import type Resource from '../../../globals/impl/resource'; import type Minigame from '../../../minigames/minigame'; import type Entities from '../../../../controllers/entities'; import type Packet from '@kaetram/common/network/packet'; @@ -158,7 +157,6 @@ export default class Player extends Character { // Region data public regionsLoaded: number[] = []; - public resourcesLoaded: { [instance: string]: Modules.ResourceState } = {}; public lightsLoaded: number[] = []; // NPC talking @@ -1855,15 +1853,6 @@ export default class Player extends Character { this.regionsLoaded.push(region); } - /** - * Adds a resource to our loaded resource instances. - * @param resource The resource we are adding. - */ - - public loadResource(resource: Resource): void { - this.resourcesLoaded[resource.instance] = resource.state; - } - /** * @param region Region id of the region we are checking. * @returns Whether or not the region has been added to the list of loaded regions. @@ -1874,18 +1863,10 @@ export default class Player extends Character { } /** - * Checks if the resource is within our loaded resources and that the state matches. - * @param resource The resource we are chceking. - * @returns If the resource is loaded and the state matches. + * @param light The light that we are checking if it exists in the list of loaded lights. + * @returns Whether or not the light has been added to the list of loaded lights. */ - public hasLoadedResource(resource: Resource): boolean { - return ( - resource.instance in this.resourcesLoaded && - this.resourcesLoaded[resource.instance] === resource.state - ); - } - public hasLoadedLight(light: number): boolean { return this.lightsLoaded.includes(light); } diff --git a/packages/server/src/game/entity/character/player/skill/impl/fishing.ts b/packages/server/src/game/entity/character/player/skill/impl/fishing.ts index a1df844e37..0b89a371bc 100644 --- a/packages/server/src/game/entity/character/player/skill/impl/fishing.ts +++ b/packages/server/src/game/entity/character/player/skill/impl/fishing.ts @@ -6,7 +6,7 @@ import ResourceText from '@kaetram/common/text/en/resource'; import { Modules } from '@kaetram/common/network'; import type Player from '../../player'; -import type Resource from '../../../../../globals/impl/resource'; +import type Resource from '../../../../../entity/objects/resource/resource'; export default class Fishing extends ResourceSkill { // We want to randomize the depletion of the fishing spots. diff --git a/packages/server/src/game/entity/character/player/skill/impl/foraging.ts b/packages/server/src/game/entity/character/player/skill/impl/foraging.ts index ca4af2cd61..cc168cc0bb 100644 --- a/packages/server/src/game/entity/character/player/skill/impl/foraging.ts +++ b/packages/server/src/game/entity/character/player/skill/impl/foraging.ts @@ -4,7 +4,7 @@ import ForagingSpots from '../../../../../../../data/foraging.json'; import { Modules } from '@kaetram/common/network'; import type Player from '../../player'; -import type Resource from '../../../../../globals/impl/resource'; +import type Resource from '../../../../../entity/objects/resource/resource'; import type { ResourceInfo } from '@kaetram/common/types/resource'; export default class Foraging extends ResourceSkill { diff --git a/packages/server/src/game/entity/character/player/skill/impl/mining.ts b/packages/server/src/game/entity/character/player/skill/impl/mining.ts index ca5bd3fb85..1310994946 100644 --- a/packages/server/src/game/entity/character/player/skill/impl/mining.ts +++ b/packages/server/src/game/entity/character/player/skill/impl/mining.ts @@ -7,7 +7,7 @@ import ResourceText from '@kaetram/common/text/en/resource'; import { Modules } from '@kaetram/common/network'; import type Player from '../../player'; -import type Resource from '../../../../../globals/impl/resource'; +import type Resource from '../../../../../entity/objects/resource/resource'; export default class Mining extends ResourceSkill { public constructor() { @@ -25,7 +25,7 @@ export default class Mining extends ResourceSkill { private handleExhaust(player: Player, resource?: Resource): void { // Use the superclass logic to handle the random item selection. - super.handleRandomItems(player, Rocks[resource!.type as keyof typeof Rocks]); + super.handleRandomItems(player, Rocks[resource!.key as keyof typeof Rocks]); } /** diff --git a/packages/server/src/game/entity/entity.ts b/packages/server/src/game/entity/entity.ts index 0132241d83..02e535a073 100644 --- a/packages/server/src/game/entity/entity.ts +++ b/packages/server/src/game/entity/entity.ts @@ -13,6 +13,9 @@ import type Player from './character/player/player'; import type { EntityData, EntityDisplayInfo } from '@kaetram/common/types/entity'; import type LootBag from './objects/lootbag'; import type Tree from './objects/resource/impl/tree'; +import type Rock from './objects/resource/impl/rock'; +import type FishSpot from './objects/resource/impl/fishspot'; +import type Foraging from './objects/resource/impl/foraging'; type MovementCallback = (x: number, y: number) => void; @@ -304,6 +307,33 @@ abstract class Entity { return this.type === (Modules.EntityType.Tree as number); } + /** + * Checks whether or not the entity is a rock. + * @returns Whether the type is equal to the EntityType rock. + */ + + public isRock(): this is Rock { + return this.type === (Modules.EntityType.Rock as number); + } + + /** + * Checks whether or not the entity is a fish spot. + * @returns Whether the type is equal to the EntityType fish spot. + */ + + public isFishSpot(): this is FishSpot { + return this.type === (Modules.EntityType.FishSpot as number); + } + + /** + * Checks whether or not the entity is a foraging spot. + * @returns Whether the type is equal to the EntityType foraging spot. + */ + + public isForaging(): this is Foraging { + return this.type === (Modules.EntityType.Foraging as number); + } + /** * This is entity superclass serialization. It provides * the absolute most basic data about the entity. Entities diff --git a/packages/server/src/game/entity/objects/resource/impl/fishspot.ts b/packages/server/src/game/entity/objects/resource/impl/fishspot.ts new file mode 100644 index 0000000000..3af86d3b62 --- /dev/null +++ b/packages/server/src/game/entity/objects/resource/impl/fishspot.ts @@ -0,0 +1,10 @@ +import Resource from '../resource'; + +import Utils from '@kaetram/common/util/utils'; +import { Modules } from '@kaetram/common/network'; + +export default class FishSpot extends Resource { + public constructor(key: string, x: number, y: number) { + super(Utils.createInstance(Modules.EntityType.FishSpot), key, x, y); + } +} diff --git a/packages/server/src/game/entity/objects/resource/impl/foraging.ts b/packages/server/src/game/entity/objects/resource/impl/foraging.ts new file mode 100644 index 0000000000..7ff42c4c78 --- /dev/null +++ b/packages/server/src/game/entity/objects/resource/impl/foraging.ts @@ -0,0 +1,10 @@ +import Resource from '../resource'; + +import Utils from '@kaetram/common/util/utils'; +import { Modules } from '@kaetram/common/network'; + +export default class Foraging extends Resource { + public constructor(key: string, x: number, y: number) { + super(Utils.createInstance(Modules.EntityType.Foraging), key, x, y); + } +} diff --git a/packages/server/src/game/entity/objects/resource/impl/rock.ts b/packages/server/src/game/entity/objects/resource/impl/rock.ts new file mode 100644 index 0000000000..d5581f7371 --- /dev/null +++ b/packages/server/src/game/entity/objects/resource/impl/rock.ts @@ -0,0 +1,10 @@ +import Resource from '../resource'; + +import Utils from '@kaetram/common/util/utils'; +import { Modules } from '@kaetram/common/network'; + +export default class Rock extends Resource { + public constructor(key: string, x: number, y: number) { + super(Utils.createInstance(Modules.EntityType.Rock), key, x, y); + } +} diff --git a/packages/server/src/game/globals/fishspots.ts b/packages/server/src/game/globals/fishspots.ts deleted file mode 100644 index 781c186c5f..0000000000 --- a/packages/server/src/game/globals/fishspots.ts +++ /dev/null @@ -1,24 +0,0 @@ -import Resources from './resources'; - -import log from '@kaetram/common/util/log'; - -import type World from '../world'; - -export default class FishSpots extends Resources { - public constructor(world: World) { - super(world, world.map.fishSpots); - } - - /** - * Override for the resource loading function to display - * debug message of how many trees were loaded. - */ - - protected override load(): void { - super.load(); - - let amount = Object.keys(this.resources).length; - - log.info(`Loaded ${amount} fishing spot${amount > 1 ? 's' : ''}.`); - } -} diff --git a/packages/server/src/game/globals/foraging.ts b/packages/server/src/game/globals/foraging.ts deleted file mode 100644 index 75bfbe630a..0000000000 --- a/packages/server/src/game/globals/foraging.ts +++ /dev/null @@ -1,24 +0,0 @@ -import Resources from './resources'; - -import log from '@kaetram/common/util/log'; - -import type World from '../world'; - -export default class Foraging extends Resources { - public constructor(world: World) { - super(world, world.map.foraging); - } - - /** - * Override for the resource loading function to display - * debug message of how many trees were loaded. - */ - - protected override load(): void { - super.load(); - - let amount = Object.keys(this.resources).length; - - log.info(`Loaded ${amount} foraging spot${amount > 1 ? 's' : ''}.`); - } -} diff --git a/packages/server/src/game/globals/globals.ts b/packages/server/src/game/globals/globals.ts index 59cefb4740..e924d3cca9 100644 --- a/packages/server/src/game/globals/globals.ts +++ b/packages/server/src/game/globals/globals.ts @@ -1,9 +1,5 @@ import Lights from './lights'; import Signs from './signs'; -import Trees from './trees'; -import Rocks from './rocks'; -import FishSpots from './fishspots'; -import Foraging from './foraging'; import type World from '../world'; @@ -13,54 +9,14 @@ import type World from '../world'; */ export default class Globals { - private trees: Trees; - private rocks: Rocks; - private fishSpots: FishSpots; - private foraging: Foraging; private lights: Lights; private signs: Signs; public constructor(private world: World) { - this.trees = new Trees(this.world); - this.rocks = new Rocks(this.world); - this.fishSpots = new FishSpots(this.world); - this.foraging = new Foraging(this.world); this.lights = new Lights(this.world.map); this.signs = new Signs(this.world.map); } - /** - * @returns The trees handler object. - */ - - public getTrees(): Trees { - return this.trees; - } - - /** - * @returns The rocks handler object. - */ - - public getRocks(): Rocks { - return this.rocks; - } - - /** - * @returns The fishing spots handler object. - */ - - public getFishingSpots(): FishSpots { - return this.fishSpots; - } - - /** - * @returns The foraging handler object. - */ - - public getForaging(): Foraging { - return this.foraging; - } - /** * @returns Returns the signs handler object. */ diff --git a/packages/server/src/game/globals/impl/resource.ts b/packages/server/src/game/globals/impl/resource.ts deleted file mode 100644 index a4c526b38e..0000000000 --- a/packages/server/src/game/globals/impl/resource.ts +++ /dev/null @@ -1,170 +0,0 @@ -import Trees from '../../../../data/trees.json'; -import Rocks from '../../../../data/rocks.json'; - -import log from '@kaetram/common/util/log'; -import Utils from '@kaetram/common/util/utils'; -import { Modules } from '@kaetram/common/network'; - -import type { ResourceData } from '@kaetram/common/types/resource'; -import type { ProcessedResource, Tile } from '@kaetram/common/types/map'; - -export default class Resource { - public instance = Utils.createInstance(Modules.EntityType.Object); - - // Amount of time it takes for the resource to respawn. - protected respawnTime: number = Modules.Constants.RESOURCE_RESPAWN; - - // Data contains original tile data from the map - public data: { [index: number]: Tile } = {}; - - // Tile data containing information after the resource has been depleted. - private depleted: { [index: number]: Tile } = {}; - - // The state of the resource - public state: Modules.ResourceState = Modules.ResourceState.Default; - - private respawnTimeout?: NodeJS.Timeout | undefined; - private stateCallback?: () => void; - - public constructor(public type: string) {} - - /** - * Takes information from the `info` parameter and determines - * if a tile is either a base or just resource data. If it's resource data, - * we remove the resource information. If it's a base, we replace the - * base with the tileId of the depleted resource. We store this data for later. - * A base is an interactable resource tile that the player can click on. - * @param info The resource information based on the resource's type. - */ - - public load(info: ProcessedResource): void { - // Iterate through all the tile and its indexes in the resource. - for (let index in this.data) { - let flatTile = [this.data[index]].flat(); - - // Why would you put a resource in the void? How are you even near the resource? - if (!Array.isArray(flatTile)) - return log.warning(`[${index}] Could not parse tile data for tree.`); - - // Find if the tile contains data or base data. - let dataIntersect = flatTile.filter((tile) => info.data.includes(tile)), - stumpIntersect = flatTile.filter((tile) => info.base.includes(tile)); - - // Tile contains data that is also a stump. - if (dataIntersect.length > 0 && stumpIntersect.length > 0) { - /** - * `baseIndex` is the index of the current base in the info data. - * `dataBaseIndex` is the index of the base in the tile data. - * `cloneTile` is a tile created to prevent changes to original data. - */ - - let baseIndex = info.base.indexOf(stumpIntersect[0]), - dataBaseIndex = flatTile.indexOf(stumpIntersect[0]), - cloneTile = [...flatTile]; - - // Replace the stump with the cut stump. - cloneTile[dataBaseIndex] = info.depleted[baseIndex]; - - // Store the cloned data. - this.depleted[index] = cloneTile as Tile; - } else if (dataIntersect.length > 0) - // Remove tree data. - this.depleted[index] = flatTile.filter( - (tile) => !dataIntersect.includes(tile) - ) as Tile; - - // Set tile data to 0 indicating nothing there instead of empty array '[]' - if ([this.depleted[index]].flat().length === 0) this.depleted[index] = 0; - } - - // Apply the respawn time if specified. - this.setRespawnTime(); - } - - /** - * Depletes a resource by updating its state. We create a respawn - * timeout that respawns the resource and updates the nearby - * region when the timeout expires. - */ - - public deplete(): void { - // Cannot cut a tree that's already cut. - if (this.respawnTimeout) return; - - this.setState(Modules.ResourceState.Depleted); - - // Reset the tree once the timeout expires. - this.respawnTimeout = setTimeout(() => { - this.setState(Modules.ResourceState.Default); - - this.respawnTimeout = undefined; - }, this.respawnTime); - } - - /** - * Attempts to grab the respawn time for a given resource. If no respawn time - * is specified in the JSON file then we use the default specified in Modules.Constants. - * @returns The respawn time for this particular resource. - */ - - public getRespawnTime(): number { - let info = - this.type in Trees - ? (Trees as ResourceData)[this.type] - : (Rocks as ResourceData)[this.type]; - - // Return the default respawn time if not specified. - if (!info?.respawnTime) return this.respawnTime; - - return info.respawnTime; - } - - /** - * Checks whether the resource is depleted or not. - * @returns If the current state is that of a depleted resource state. - */ - - public isDepleted(): boolean { - return this.state === Modules.ResourceState.Depleted; - } - - /** - * Updates the state of the resource and creates a callback. - * @param state The new state of the resource. - */ - - private setState(state: Modules.ResourceState): void { - this.state = state; - - this.stateCallback?.(); - } - - /** - * Updates the respawn time of the resource. - * @param time New time (in milliseconds) for the resource to respawn. - */ - - public setRespawnTime(time = this.getRespawnTime()): void { - this.respawnTime = time; - } - - /** - * Iterates through each tile in the data (depending on the state of the resource). - * @param callback The data tile alongside its parsed number index. - */ - - public forEachTile(callback: (tile: Tile, index: number) => void): void { - // Data depends on the state of the resource. - let data = this.isDepleted() ? this.depleted : this.data; - - for (let index in data) callback(data[index], parseInt(index)); - } - - /** - * Callback for when a resource undergoes a state change. - */ - - public onStateChange(callback: () => void): void { - this.stateCallback = callback; - } -} diff --git a/packages/server/src/game/globals/resources.ts b/packages/server/src/game/globals/resources.ts deleted file mode 100644 index 6af0c43984..0000000000 --- a/packages/server/src/game/globals/resources.ts +++ /dev/null @@ -1,144 +0,0 @@ -import Resource from './impl/resource'; - -import log from '@kaetram/common/util/log'; - -import type Map from '../map/map'; -import type World from '../world'; -import type Regions from '../map/regions'; -import type { ProcessedResource } from '@kaetram/common/types/map'; - -export default class Resources { - private map: Map; - private regions: Regions; - - protected resources: { [instance: string]: Resource } = {}; - - /** - * @param data Contains the resource data we are creating resources with. Trees - * in the case of trees, rocks in the case of mining, etc. - */ - - public constructor( - private world: World, - private data: ProcessedResource[] - ) { - this.map = this.world.map; - this.regions = this.world.map.regions; - - this.load(); - } - - /** - * Iterates through the map data and removes tiles which contain - * resource information. We use this resource data later to create dynamic - * tiles. - */ - - protected load(): void { - for (let index in this.map.data) { - let tile = this.map.data[index]; - - this.map.forEachTile(tile, (tileId: number) => { - let info = this.getResource(tileId); - - if (!info) return; - - // Create the tree. - this.createResource(info, parseInt(index)); - }); - } - } - - /** - * Creates a resource object and initializes all the data associated with it. This creates - * a resource object and adds it to our dictionary of resources. - * @param info The raw resource data we are initializing based off of. - * @param index The index where we first found the resource (the anchor). - */ - - protected createResource(info: ProcessedResource, index: number): void { - let resource = new Resource(info.type), - coords = this.map.indexToCoord(index), - regionIndex = this.regions.getRegion(coords.x, coords.y), - region = this.regions.get(regionIndex); - - // Load actual resource tile data. - this.search(info, resource, index); - - // Initialize cut resource's data. - resource.load(info); - - // Add resource to the region. - if (region) region.addResource(resource); - - /** - * This check is used for anomalies in resource structures. If two resources are too - * close together, then they will be recursively identified as one. This may cause - * issues. This is what this warning represents. It is ideal to make sure there is - * at least one tile in between resource. - */ - - if (Object.keys(resource.data).length !== info.data.length) - log.warning(`Resource x: ${coords.x} y: ${coords.y} contains partial data.`); - - // Add our resource to our list of resources. - this.resources[resource.instance] = resource; - - // Send an update when a resource's state undergoes a change. - resource.onStateChange(() => this.regions.sendUpdate(regionIndex)); - } - - /** - * Recursive iteration scanning the nearby tiles until all resource - * data tiles are removed and search is exhausted. This removes - * the data from the cloned map data. - * @param info The raw resource data we are using to compare against tiles. - * @param resource The resource data we are adding info onto. - * @param index Index where we start the search. - */ - - private search(info: ProcessedResource, resource: Resource, index: number): boolean { - let intersection = [this.map.data[index]].flat().filter((tile) => info.data.includes(tile)); - - // If we find no intersection, then tile contains no resource data. - if (intersection.length === 0) return false; - - // Add the entire tile data onto the resource. - resource.data[index] = structuredClone(this.map.data[index]); - - // Remove all tiles from the map data. - this.map.data[index] = -1; - - // Search for tiles recursively right, left, down, up respectively. - if (info.data.length > 1) { - if (this.search(info, resource, index + 1)) return true; - if (this.search(info, resource, index - 1)) return true; - if (this.search(info, resource, index + this.map.width)) return true; - if (this.search(info, resource, index - this.map.width)) return true; - } - - return false; - } - - /** - * Searches the resource instances and finds which resource contains - * the index data in its data. - * @param index The index coordinate we are searching for. - * @returns A resource object if found, otherwise undefined. - */ - - public findResource(index: number): Resource | undefined { - return Object.values(this.resources).find((resource) => index in resource.data); - } - - /** - * Looks through all the resources in the map and - * finds if the `tileId` is contained within their - * data. - * @param tileId The tileId we are checking. - */ - - private getResource(tileId: number): ProcessedResource | undefined { - return this.data.find((resource) => resource.data.includes(tileId)); - } -} diff --git a/packages/server/src/game/globals/rocks.ts b/packages/server/src/game/globals/rocks.ts deleted file mode 100644 index dfceaec26b..0000000000 --- a/packages/server/src/game/globals/rocks.ts +++ /dev/null @@ -1,24 +0,0 @@ -import Resources from './resources'; - -import log from '@kaetram/common/util/log'; - -import type World from '../world'; - -export default class Rocks extends Resources { - public constructor(world: World) { - super(world, world.map.rocks); - } - - /** - * Override for the resource loading function to display - * debug message of how many trees were loaded. - */ - - protected override load(): void { - super.load(); - - let amount = Object.keys(this.resources).length; - - log.info(`Loaded ${amount} rock${amount > 1 ? 's' : ''}.`); - } -} diff --git a/packages/server/src/game/globals/trees.ts b/packages/server/src/game/globals/trees.ts deleted file mode 100644 index ddb065c5e6..0000000000 --- a/packages/server/src/game/globals/trees.ts +++ /dev/null @@ -1,24 +0,0 @@ -import Resources from './resources'; - -import log from '@kaetram/common/util/log'; - -import type World from '../world'; - -export default class Trees extends Resources { - public constructor(world: World) { - super(world, world.map.trees); - } - - /** - * Override for the resource loading function to display - * debug message of how many trees were loaded. - */ - - protected override load(): void { - super.load(); - - let amount = Object.keys(this.resources).length; - - log.info(`Loaded ${amount} tree${amount > 1 ? 's' : ''}.`); - } -} diff --git a/packages/server/src/game/map/map.ts b/packages/server/src/game/map/map.ts index b727883368..3f0a81a47d 100644 --- a/packages/server/src/game/map/map.ts +++ b/packages/server/src/game/map/map.ts @@ -9,13 +9,7 @@ import { Modules } from '@kaetram/common/network'; import type World from '../world'; import type Areas from './areas/areas'; import type Player from '../entity/character/player/player'; -import type { - ProcessedArea, - ProcessedDoor, - ProcessedMap, - ProcessedResource, - Tile -} from '@kaetram/common/types/map'; +import type { ProcessedArea, ProcessedDoor, ProcessedMap, Tile } from '@kaetram/common/types/map'; let map = mapData as ProcessedMap; @@ -40,10 +34,6 @@ export default class Map { public cursors: { [tileId: number]: string } = map.cursors; public doors: { [index: number]: ProcessedDoor } = {}; public warps: ProcessedArea[] = map.areas.warps || []; - public trees: ProcessedResource[] = map.trees || []; - public rocks: ProcessedResource[] = map.rocks || []; - public fishSpots: ProcessedResource[] = map.fishSpots || []; - public foraging: ProcessedResource[] = map.foraging || []; public lights: ProcessedArea[] = map.areas.lights || []; public signs: ProcessedArea[] = map.areas.signs || []; @@ -252,13 +242,6 @@ export default class Map { } } - // Check whether a resource has collision properties. - if (region.hasResources()) { - let resource = region.getResource(index); - - if (resource) return this.isCollision(resource.data[index] as Tile); - } - // Check the collision at a specified index. return this.isCollisionIndex(index); } diff --git a/packages/server/src/game/map/region.ts b/packages/server/src/game/map/region.ts index 97207cd179..3bfcb285bd 100644 --- a/packages/server/src/game/map/region.ts +++ b/packages/server/src/game/map/region.ts @@ -1,7 +1,6 @@ import type Area from './areas/area'; import type Entity from '../entity/entity'; import type Light from '../globals/impl/light'; -import type Resource from '../globals/impl/resource'; import type Player from '../entity/character/player/player'; import type { RegionTileData } from '@kaetram/common/types/map'; @@ -12,7 +11,6 @@ export default class Region { private players: string[] = []; // A list of instance ids for players. private joining: Entity[] = []; // Used for sending spawn positions. private dynamicAreas: Area[] = []; - private resources: Resource[] = []; private lights: Light[] = []; public constructor( @@ -96,23 +94,6 @@ export default class Region { return this.dynamicAreas.length > 0; } - /** - * Adds a resource to the region. - * @param resource The resource we are adding to the region. - */ - - public addResource(resource: Resource): void { - this.resources.push(resource); - } - - /** - * @returns If the amount of resources in the array is greater than 0. - */ - - public hasResources(): boolean { - return this.resources.length > 0; - } - /** * Adds a light object to the region. * @param light The light object we are adding. @@ -162,16 +143,6 @@ export default class Region { return undefined; } - /** - * Attempts to grab a resource from a region based on the index in the map. - * @param index The index at which we are trying to grab the resource. - * @returns The resource object if it exists. - */ - - public getResource(index: number): Resource | undefined { - for (let resource of this.resources) if (index in resource.data) return resource; - } - /** * Grab a list of entity instances and remove the `reject` from the list. * @param player Player object used to check dynamic visibility of the entities. @@ -226,15 +197,6 @@ export default class Region { for (let entity of Object.values(this.entities)) callback(entity); } - /** - * Iterates through all the resources and returns each resource. - * @param callback Resource that is being iterated. - */ - - public forEachResource(callback: (resource: Resource) => void): void { - for (let resource of this.resources) callback(resource); - } - /** * Iterates through all the lights in the regions and returns each light. * @param callback Contains the light that is being iterated. diff --git a/packages/server/src/game/map/regions.ts b/packages/server/src/game/map/regions.ts index ceb6202e24..f2a6e4bb75 100644 --- a/packages/server/src/game/map/regions.ts +++ b/packages/server/src/game/map/regions.ts @@ -9,13 +9,12 @@ import config from '@kaetram/common/config'; import { Modules, Opcodes } from '@kaetram/common/network'; import { ListPacket, MapPacket, SpawnPacket, UpdatePacket } from '@kaetram/common/network/impl'; -import type Player from '../entity/character/player/player'; -import type Entity from '../entity/entity'; -import type Resource from '../globals/impl/resource'; +import type Map from './map'; import type World from '../world'; import type Area from './areas/area'; +import type Entity from '../entity/entity'; import type Dynamic from './areas/impl/dynamic'; -import type Map from './map'; +import type Player from '../entity/character/player/player'; import type { EntityDisplayInfo } from '@kaetram/common/types/entity'; import type { RegionCache, RegionData, RegionTileData, Tile } from '@kaetram/common/types/map'; @@ -509,10 +508,6 @@ export default class Regions { // Initialize empty array for the region tile data. data[surroundingRegion] = []; - // Parse and send resource data. - if (region.hasResources()) - data[surroundingRegion].push(...this.getRegionResourceData(region, player)); - // Parse and send dynamic areas. if (region.hasDynamicAreas()) data[surroundingRegion].push(...this.getRegionTileData(region, true, player)); @@ -567,49 +562,6 @@ export default class Regions { return tileData; } - /** - * Parses through all the resources within the region specified and - * grabs tile data from them. It returns a RegionTileData array. - * @param region The region we are looking for resources in. - * @returns RegionTileData containing resource information. - */ - - private getRegionResourceData(region: Region, player: Player): RegionTileData[] { - let tileData: RegionTileData[] = []; - - // Iterate through all the resources in the region. - region.forEachResource((resource: Resource) => { - // No need to reload resources that haven't changed. - if (player.hasLoadedResource(resource)) return; - - // Parse resource tiles. - tileData.push(...this.getResourceData(resource)); - - player.loadResource(resource); - }); - - return tileData; - } - - /** - * Grabs individual resource data from a specified resource. - * @param resource The resource we are grabbing data for. - * @returns RegionTileData array containing resource information. - */ - - private getResourceData(resource: Resource): RegionTileData[] { - let tileData: RegionTileData[] = []; - - resource.forEachTile((data: Tile, index: number) => { - // Perhaps we can optimize further by storing this directly in the resource? - let coord = this.map.indexToCoord(index); - - tileData.push(this.buildTile(coord.x, coord.y, index, data)); - }); - - return tileData; - } - /** * Iterates through the surrounding regions and updates all the entities * with their custom data (if any). diff --git a/packages/tools/map/parser/parser.ts b/packages/tools/map/parser/parser.ts index 5ba7f53f4b..f2565fd2fc 100755 --- a/packages/tools/map/parser/parser.ts +++ b/packages/tools/map/parser/parser.ts @@ -3,27 +3,13 @@ import zlib from 'node:zlib'; import { Modules } from '@kaetram/common/network'; import log from '@kaetram/common/util/log'; -import type { - ProcessedAnimation, - ProcessedMap, - ProcessedResource, - ProcessedTileset -} from '@kaetram/common/types/map'; +import type { ProcessedAnimation, ProcessedMap, ProcessedTileset } from '@kaetram/common/types/map'; import type { Animation, Layer, LayerObject, MapData, Property, Tileset } from './mapdata'; -interface Resources { - [key: string]: ProcessedResource; -} - export default class ProcessMap { private map: ProcessedMap; private tilesetEntities: { [tileId: number]: string } = {}; - private trees: Resources = {}; - private rocks: Resources = {}; - private fishSpots: Resources = {}; - private foraging: Resources = {}; - /** * We create the skeleton file for the ExportedMap. * @param data The raw data from the Tiled map JSON file. @@ -59,11 +45,7 @@ export default class ProcessMap { obstructing: [], objects: [], areas: {}, - cursors: {}, - trees: [], - rocks: [], - fishSpots: [], - foraging: [] + cursors: {} }; this.parseTilesets(); @@ -105,16 +87,6 @@ export default class ProcessMap { this.parseTileset(tileset); } - - // As the last step of the tileset processing, we parse the resources and add them to the map. - this.parseResources(this.trees, (tree: ProcessedResource) => this.map.trees.push(tree)); - this.parseResources(this.rocks, (rock: ProcessedResource) => this.map.rocks.push(rock)); - this.parseResources(this.fishSpots, (fishSpot: ProcessedResource) => - this.map.fishSpots.push(fishSpot) - ); - this.parseResources(this.foraging, (forage: ProcessedResource) => - this.map.foraging.push(forage) - ); } /** @@ -219,56 +191,6 @@ export default class ProcessMap { cursors[tileId] = value; break; } - - // Properties fo resource classification. - case 'tree': { - return this.parseResourceProperty(this.trees, 'data', tileId, value); - } - - case 'stump': { - return this.parseResourceProperty(this.trees, 'base', tileId, value); - } - - case 'cutstump': - case 'stumpcut': { - return this.parseResourceProperty(this.trees, 'depleted', tileId, value); - } - - // Mining - case 'rock': { - return this.parseResourceProperty(this.rocks, 'data', tileId, value); - } - - case 'rockbase': { - return this.parseResourceProperty(this.rocks, 'base', tileId, value); - } - - case 'rockempty': { - return this.parseResourceProperty(this.rocks, 'depleted', tileId, value); - } - - // Fishing - case 'fish': - case 'fishspot': { - // Fish spots share the same base and data tiles. - this.parseResourceProperty(this.fishSpots, 'base', tileId, value); - return this.parseResourceProperty(this.fishSpots, 'data', tileId, value); - } - - case 'fishempty': { - return this.parseResourceProperty(this.fishSpots, 'depleted', tileId, value); - } - - // Foraging - case 'forage': { - // Foraging spots share the same base and data tiles. - this.parseResourceProperty(this.foraging, 'base', tileId, value); - return this.parseResourceProperty(this.foraging, 'data', tileId, value); - } - - case 'forageempty': { - return this.parseResourceProperty(this.foraging, 'depleted', tileId, value); - } } } @@ -410,69 +332,6 @@ export default class ProcessMap { } } - /** - * Generic implementation for parsing a resource property. This may be a tree, or a rock - * or anything else in the future. When we pass properties onto this function, we ensure - * they use the standard `data,` `base,` and `depleted` properties. - * @param resourceType The type of resource we are adding data onto (trees, rocks, etc.) - * @param name The name of the property (data, base, depleted) - * @param tileId The tileId being processed currently (the tile data). - * @param value The value represents the resource's identifier. - */ - - private parseResourceProperty( - resourceType: Resources, - name: string, - tileId: number, - value: never - ): void { - // Create a new resource type if it does not exist. - if (!(value in resourceType)) - resourceType[value] = { - data: [], - base: [], - depleted: [], - type: value - }; - - // Organize resource data into their respective arrays. - switch (name) { - case 'data': { - resourceType[value].data.push(tileId); - break; - } - - case 'base': { - resourceType[value].base.push(tileId); - break; - } - - case 'depleted': { - resourceType[value].depleted.push(tileId); - break; - } - } - } - - /** - * Parses through a specified resource and creates a callback after it has been validated. - * @param resources The list of processed resources to look through. - * @param callback Contains resource currently being processed. - */ - - private parseResources( - resources: Resources, - callback: (resource: ProcessedResource) => void - ): void { - for (let resource of Object.values(resources)) { - // Determine whether the normal and exhausted resource match lengths, otherwise skip. - if (resource.base.length !== resource.depleted.length) - return log.error(`${resource.type} has a base and depleted length mismatch.`); - - callback(resource); - } - } - /** * Looks through all the tiles in the map and finds which one contain a hidden * tile at their uppermost layer. We remove the layers behind the hidden tile. @@ -671,11 +530,7 @@ export default class ProcessMap { high, objects, cursors, - entities, - trees, - rocks, - fishSpots, - foraging + entities } = this.map; return JSON.stringify({ @@ -690,11 +545,7 @@ export default class ProcessMap { high, objects, cursors, - entities, - trees, - rocks, - fishSpots, - foraging + entities }); }