From 0fa23009ade926ba57f9aea2b761dba966334da6 Mon Sep 17 00:00:00 2001 From: jie Date: Sun, 3 Nov 2024 23:20:58 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8A=BD=E5=87=BA=E6=A0=B9=E6=8D=AEID=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E8=B5=84=E6=BA=90=E5=87=BD=E6=95=B0=EF=BC=8C=20?= =?UTF-8?q?=E9=98=B2=E6=AD=A2=E6=AF=8Ftick=E6=89=BE=E5=88=B0=E7=9A=84?= =?UTF-8?q?=E8=B5=84=E6=BA=90=E9=A1=BA=E5=BA=8F=E4=B8=8D=E4=B8=80=E8=87=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.ts | 2 +- src/modules/builder.ts | 13 +++++++------ src/modules/harvester.ts | 9 +++++---- src/modules/setting.ts | 10 +++++++--- src/modules/upgrader.ts | 9 ++++++--- src/modules/util.ts | 17 +++++++++++++++-- 6 files changed, 41 insertions(+), 19 deletions(-) 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