diff --git a/src/main.ts b/src/main.ts index ea76509..16bb479 100644 --- a/src/main.ts +++ b/src/main.ts @@ -20,7 +20,7 @@ export const loop = errorMapper(() => { } else if (creep.memory.role == "Upgrader") { Upgrade(creep); } else if (creep.memory.role == "Builder") { - Build(creep); + Build(creep, STRUCTURE_CONTAINER); } } }) \ No newline at end of file diff --git a/src/modules/builder.ts b/src/modules/builder.ts index aacac22..c3c8b0d 100644 --- a/src/modules/builder.ts +++ b/src/modules/builder.ts @@ -1,6 +1,7 @@ -import { FindCapacity } from "./util"; +import { MAIN_SOURCE_ID } from "./setting"; +import { FindCapacity, GetConstructureSet, GetSource } from "./util"; -export function Build(creep: Creep) { +export function Build(creep: Creep, structureType: string | null = null) { if (creep.memory.working && creep.store[RESOURCE_ENERGY] == 0) { creep.memory.working = false; creep.say("harvest") @@ -10,7 +11,7 @@ export function Build(creep: Creep) { creep.say("builder") } if (creep.memory.working) { - let target = creep.room.find(FIND_CONSTRUCTION_SITES); + let target = GetConstructureSet(creep, structureType); if (target.length > 0) { if (creep.build(target[0]) == ERR_NOT_IN_RANGE) { creep.moveTo(target[0], { visualizePathStyle: { stroke: "#ffaa00" } }); @@ -26,9 +27,9 @@ export function Build(creep: Creep) { } } else { creep.memory.working = false - let sources = creep.room.find(FIND_SOURCES); - if (creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) { - creep.moveTo(sources[0], { visualizePathStyle: { stroke: "#ffaa00" } }); + let source = GetSource(MAIN_SOURCE_ID); + if (creep.harvest(source) == ERR_NOT_IN_RANGE) { + creep.moveTo(source, { visualizePathStyle: { stroke: "#ffaa00" } }); } } } \ No newline at end of file diff --git a/src/modules/harvester.ts b/src/modules/harvester.ts index 9c4f4a7..ebe02aa 100644 --- a/src/modules/harvester.ts +++ b/src/modules/harvester.ts @@ -1,10 +1,11 @@ -import { FindCapacity } from "./util" +import { FindCapacity, GetSource } from "./util" +import { MAIN_SOURCE_ID } from "./setting"; export function Harvest(creep: Creep) { if (creep.store.getFreeCapacity() > 0) { - let sources: Source[] = creep.room.find(FIND_SOURCES); - if (creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) { - creep.moveTo(sources[0], { visualizePathStyle: { stroke: "#ffaa00" } }); + let source: Source = GetSource(MAIN_SOURCE_ID); + if (creep.harvest(source) == ERR_NOT_IN_RANGE) { + creep.moveTo(source, { visualizePathStyle: { stroke: "#ffaa00" } }); } } else { let targets = FindCapacity(creep); diff --git a/src/modules/setting.ts b/src/modules/setting.ts index c863a2c..e740195 100644 --- a/src/modules/setting.ts +++ b/src/modules/setting.ts @@ -1,9 +1,13 @@ -let HARVESTER_COUNT: number = 4; +let HARVESTER_COUNT: number = 2; let BUILDER_COUNT: number = 3; -let UPGRADER_COUNT: number = 6; +let UPGRADER_COUNT: number = 5; + +let MAIN_SOURCE_ID: Id = "ef990774d80108c" as Id; + export { HARVESTER_COUNT, BUILDER_COUNT, - UPGRADER_COUNT + UPGRADER_COUNT, + MAIN_SOURCE_ID, } diff --git a/src/modules/upgrader.ts b/src/modules/upgrader.ts index c3cb227..c170709 100644 --- a/src/modules/upgrader.ts +++ b/src/modules/upgrader.ts @@ -1,3 +1,6 @@ +import { MAIN_SOURCE_ID } from "./setting"; +import { GetSource } from "./util"; + /**@param {Creep} creep */ export function Upgrade(creep: Creep) { if (creep.memory.working && creep.store[RESOURCE_ENERGY] == 0) { @@ -15,9 +18,9 @@ export function Upgrade(creep: Creep) { } } else { - var sources = creep.room.find(FIND_SOURCES); - if (creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) { - creep.moveTo(sources[0], { visualizePathStyle: { stroke: '#ffaa00' } }); + var source = GetSource(MAIN_SOURCE_ID); + if (creep.harvest(source) == ERR_NOT_IN_RANGE) { + creep.moveTo(source, { visualizePathStyle: { stroke: '#ffaa00' } }); } } } \ No newline at end of file diff --git a/src/modules/util.ts b/src/modules/util.ts index 9e4c898..2f91288 100644 --- a/src/modules/util.ts +++ b/src/modules/util.ts @@ -1,12 +1,25 @@ -export function FindCapacity(creep: Creep) { +export function FindCapacity(creep: Creep): AnyStructure[] { return creep.room.find(FIND_STRUCTURES, { filter: (structure: StructureSpawn | StructureExtension) => { let type = structure.structureType; return ( (type == STRUCTURE_EXTENSION || type == STRUCTURE_SPAWN) && - structure.store.getFreeCapacity(RESOURCE_ENERGY) as number > 0 + structure.store.getFreeCapacity(RESOURCE_ENERGY) as number > 0 ) } }) +} + +export function GetSource(sourceId: Id | null): Source { + return Game.getObjectById(sourceId); +} + +export function GetConstructureSet(creep: Creep, structureType: string | null = null): ConstructionSite[] { + return creep.room.find(FIND_CONSTRUCTION_SITES, { + filter: (structure: Structure) => { + if (structureType == null) return true; + return structure.structureType == structureType; + } + }) } \ No newline at end of file