Skip to content

Commit

Permalink
Add a RoomObject.print helper
Browse files Browse the repository at this point in the history
  • Loading branch information
tiennou committed Feb 17, 2024
1 parent f516956 commit 6cec66c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 19 deletions.
1 change: 1 addition & 0 deletions src/declarations/prototypes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ interface Room {
}

interface RoomObject {
print: string;
ref: string;
targetedBy: string[];

Expand Down
33 changes: 14 additions & 19 deletions src/declarations/typeGuards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 (<EnergyStructure>obj).energy != undefined && (<EnergyStructure>obj).energyCapacity != undefined;
// }
//
// export function isStoreStructure(obj: RoomObject): obj is StoreStructure {
// return (<StoreStructure>obj).store != undefined && (<StoreStructure>obj).storeCapacity != undefined;
// }

export function isStructure(obj: RoomObject): obj is Structure {
return (<Structure>obj).structureType != undefined;
}
Expand Down Expand Up @@ -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 (<Source>obj).energy != undefined;
}

Expand All @@ -114,6 +96,19 @@ export function isResource(obj: RoomObject | HasPos): obj is Resource {
return (<Resource>obj).amount != undefined;
}

export function isConstructionSite(obj: RoomObject): obj is ConstructionSite {
const site = (<ConstructionSite>obj);
return site.progress !== undefined && site.structureType !== undefined;
}

export function isMineral(obj: RoomObject): obj is Mineral {
return (<Mineral>obj).mineralType !== undefined;
}

export function isDeposit(obj: RoomObject): obj is Deposit {
return (<Deposit>obj).depositType !== undefined;
}

export function hasPos(obj: HasPos | RoomPosition): obj is HasPos {
return (<HasPos>obj).pos != undefined;
}
Expand Down
39 changes: 39 additions & 0 deletions src/prototypes/RoomObject.ts
Original file line number Diff line number Diff line change
@@ -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 || '';
Expand Down

0 comments on commit 6cec66c

Please sign in to comment.