diff --git a/src/RoomEntities.ts b/src/RoomEntities.ts index 77331dd..d0b3ae8 100644 --- a/src/RoomEntities.ts +++ b/src/RoomEntities.ts @@ -1,4 +1,3 @@ - export default class RoomEntities { public readonly room: Room; public readonly spawns: StructureSpawn[]; diff --git a/src/actionnableEntities/ActionnableEntity.ts b/src/actionnableEntities/ActionnableEntity.ts index 71a87c2..71bd864 100644 --- a/src/actionnableEntities/ActionnableEntity.ts +++ b/src/actionnableEntities/ActionnableEntity.ts @@ -1,6 +1,5 @@ export abstract class ActionnableEntity { - - constructor(baseObject: baseClassType) { + constructor(public readonly baseObject: baseClassType) { this.extendParentPrototype(baseObject); } diff --git a/src/actionnableEntities/ActionnableEntityMutationFacility.ts b/src/actionnableEntities/ActionnableEntityMutationFacility.ts new file mode 100644 index 0000000..5cbe7a4 --- /dev/null +++ b/src/actionnableEntities/ActionnableEntityMutationFacility.ts @@ -0,0 +1,22 @@ +export default class ActionnableEntityMutationFacility { + public static mutateStructureLinkIntoLink() { + const prototype = StructureLink.prototype as any; + prototype.do = function (task: string, target: Link): boolean { + switch (task) { + case 'transferEnergy': + return this.doTransferEnergy(target); + default: + return false; + } + }; + prototype.isEmpty = function (): boolean { return !this.energy; }; + prototype.isFull = function (): boolean { return this.energy === this.energyCapacity; }; + // private methods + prototype.doTransferEnergy = function (target: Link): boolean { + if (this.isFull) { + return this.transferEnergy(target) === OK; + } + return false; + }; + } +} diff --git a/src/actionnableEntities/Link.ts b/src/actionnableEntities/Link.ts deleted file mode 100644 index 61bad20..0000000 --- a/src/actionnableEntities/Link.ts +++ /dev/null @@ -1,30 +0,0 @@ -import { ActionnableEntity } from "./ActionnableEntity"; - -interface Link extends StructureLink { } -class Link extends ActionnableEntity { - get isEmpty(): boolean { return !this.energy; } - get isFull(): boolean { return this.energy === this.energyCapacity; } - - constructor(structureLink: StructureLink) { - super(structureLink); - } - - public do(task: string, target: Link): boolean { - switch (task) { - case 'transferEnergy': - return this.doTransferEnergy(target); - default: - return false; - } - } - - private doTransferEnergy(target: Link): boolean { - if (this.isFull) { - return this.transferEnergy(Game.getObjectById(target.id) as StructureLink) === OK; - } - - return false; - } -} - -export default Link; diff --git a/src/main.ts b/src/main.ts index e1c1655..1fc7970 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,5 +1,6 @@ import RoomStrategy from "strategies/RoomStrategy"; import { ErrorMapper } from "utils/ErrorMapper"; +import ActionnableEntityMutationFacility from "actionnableEntities/ActionnableEntityMutationFacility"; // When compiling TS to JS and bundling with rollup, the line numbers and file names in error messages change // This utility uses source maps to get the line numbers and file names of the original, TS source code @@ -7,6 +8,7 @@ export const loop = ErrorMapper.wrapLoop(() => { initMemory(); cleanMemory(); + ActionnableEntityMutationFacility.mutateStructureLinkIntoLink(); incrementWallHitsTarget(); const roomStrategy = new RoomStrategy(); roomStrategy.execute(); diff --git a/src/strategies/EnergyTeleportationStrategy.ts b/src/strategies/EnergyTeleportationStrategy.ts index 0252bc5..986621c 100644 --- a/src/strategies/EnergyTeleportationStrategy.ts +++ b/src/strategies/EnergyTeleportationStrategy.ts @@ -1,23 +1,21 @@ -import Link from "actionnableEntities/Link"; import RoomEntities from "RoomEntities"; +import ActionnableEntityMutationFacility from "actionnableEntities/ActionnableEntityMutationFacility"; export default class EnergyTeleportationStrategy implements Strategy { - private storageAdjacentLink?: StructureLink; - private sourceAdjacentLinks: StructureLink[]; + private storageAdjacentLink?: Link; + private sourceAdjacentLinks: Link[]; constructor({ storageAdjacentLink, sourceAdjacentLinks }: RoomEntities) { - this.storageAdjacentLink = storageAdjacentLink; - this.sourceAdjacentLinks = sourceAdjacentLinks; + this.storageAdjacentLink = storageAdjacentLink as Link; + this.sourceAdjacentLinks = sourceAdjacentLinks as Link[]; } public execute() { - if (this.storageAdjacentLink) { - const storageAdjacentLink = new Link(this.storageAdjacentLink); - - if (storageAdjacentLink.isEmpty) { - this.sourceAdjacentLinks.forEach((structuredLink) => { + const storageAdjacentLink = this.storageAdjacentLink; + if (storageAdjacentLink) { + if (storageAdjacentLink.isEmpty()) { + this.sourceAdjacentLinks.forEach((link) => { // todo: if storage is not full then we shall ask links to transfert - const link = new Link(structuredLink); link.do('transferEnergy', storageAdjacentLink); }); } diff --git a/src/types.d.ts b/src/types.d.ts index fb75a65..47994f5 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -85,3 +85,14 @@ type Tasks = Task[]; interface Strategy { execute(): void; } + +interface DecoratedBuilderCreep extends Creep, ActionnableEntity> { } + +interface Link extends StructureLink, ActionnableEntity { + isEmpty(): boolean; + isFull(): boolean; +} + +interface ActionnableEntity { + do(task: string, target: T): boolean; +}