From 6cec66cf1d1eb3ff179508969ddb716022dcc1fe Mon Sep 17 00:00:00 2001 From: Etienne Samson Date: Mon, 5 Feb 2024 17:05:02 +0100 Subject: [PATCH] Add a RoomObject.print helper --- src/declarations/prototypes.d.ts | 1 + src/declarations/typeGuards.ts | 33 ++++++++++++--------------- src/prototypes/RoomObject.ts | 39 ++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 19 deletions(-) diff --git a/src/declarations/prototypes.d.ts b/src/declarations/prototypes.d.ts index 67515e0bd..a554903dc 100644 --- a/src/declarations/prototypes.d.ts +++ b/src/declarations/prototypes.d.ts @@ -134,6 +134,7 @@ interface Room { } interface RoomObject { + print: string; ref: string; targetedBy: string[]; diff --git a/src/declarations/typeGuards.ts b/src/declarations/typeGuards.ts index 7dc7b72db..c89e4447e 100644 --- a/src/declarations/typeGuards.ts +++ b/src/declarations/typeGuards.ts @@ -8,24 +8,6 @@ import {NeuralZerg} from '../zerg/NeuralZerg'; import {PowerZerg} from '../zerg/PowerZerg'; import {Zerg} from '../zerg/Zerg'; -// export interface EnergyStructure extends Structure { -// energy: number; -// energyCapacity: number; -// } - -// export interface StoreStructure extends Structure { -// store: StoreDefinition; -// storeCapacity: number; -// } - -// export function isEnergyStructure(obj: RoomObject): obj is EnergyStructure { -// return (obj).energy != undefined && (obj).energyCapacity != undefined; -// } -// -// export function isStoreStructure(obj: RoomObject): obj is StoreStructure { -// return (obj).store != undefined && (obj).storeCapacity != undefined; -// } - export function isStructure(obj: RoomObject): obj is Structure { return (obj).structureType != undefined; } @@ -98,7 +80,7 @@ export function isFactory(structure: Structure): structure is StructureFactory { return structure.structureType == STRUCTURE_FACTORY; } -export function isSource(obj: Source | Mineral): obj is Source { +export function isSource(obj: RoomObject): obj is Source { return (obj).energy != undefined; } @@ -114,6 +96,19 @@ export function isResource(obj: RoomObject | HasPos): obj is Resource { return (obj).amount != undefined; } +export function isConstructionSite(obj: RoomObject): obj is ConstructionSite { + const site = (obj); + return site.progress !== undefined && site.structureType !== undefined; +} + +export function isMineral(obj: RoomObject): obj is Mineral { + return (obj).mineralType !== undefined; +} + +export function isDeposit(obj: RoomObject): obj is Deposit { + return (obj).depositType !== undefined; +} + export function hasPos(obj: HasPos | RoomPosition): obj is HasPos { return (obj).pos != undefined; } diff --git a/src/prototypes/RoomObject.ts b/src/prototypes/RoomObject.ts index b63375f81..65481ee42 100644 --- a/src/prototypes/RoomObject.ts +++ b/src/prototypes/RoomObject.ts @@ -1,5 +1,44 @@ +import { + isConstructionSite, + isCreep, + isDeposit, + isMineral, + isResource, + isRuin, + isSource, + isStructure, + isTombstone, +} from "declarations/typeGuards"; + // RoomObject prototypes +export function roomObjectType(obj: RoomObject) { + let type; + if (isRuin(obj)) type = "ruin"; + else if (isTombstone(obj)) type = "tombstone"; + else if (isResource(obj)) type = "resource"; + else if (isStructure(obj)) type = obj.structureType; + else if (isSource(obj)) type = "source"; + else if (isMineral(obj)) type = `mineral of ${obj.mineralType}`; + else if (isDeposit(obj)) type = `deposit of ${obj.depositType}`; + else if (isConstructionSite(obj)) type = `${obj.structureType} (site)`; + else if (obj instanceof PowerCreep) { + type = `powercreep ${obj.name} (owned by ${obj.owner.username})`; + } else if (isCreep(obj)) { + type = `creep ${obj.name} (owned by ${obj.owner.username})`; + } else { + type = "unknown"; + } + return type; +} + +Object.defineProperty(RoomObject.prototype, "print", { + get(this: RoomObject) { + return `${roomObjectType(this)} ${this.pos.print}`; + }, + configurable: true, +}); + Object.defineProperty(RoomObject.prototype, 'ref', { // reference object; see globals.deref (which includes Creep) get : function() { return this.id || this.name || '';