From 57277eff2d8b8fa71c8c08060b2dd4e9e98fcd50 Mon Sep 17 00:00:00 2001 From: jie Date: Mon, 4 Nov 2024 00:02:42 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E5=A4=A7=E7=9A=84=E6=94=B6=E9=9B=86?= =?UTF-8?q?=E8=80=85=E6=94=B6=E9=9B=86=E9=80=BB=E8=BE=91=EF=BC=8C=20Contai?= =?UTF-8?q?ner=E8=8E=B7=E5=8F=96=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.ts | 2 +- src/modules/builder.ts | 8 ++++---- src/modules/harvester.ts | 18 +++++++++++++++--- src/modules/setting.ts | 6 +++--- src/modules/upgrader.ts | 10 +++++----- src/modules/util.ts | 14 +++++++++++++- 6 files changed, 41 insertions(+), 17 deletions(-) diff --git a/src/main.ts b/src/main.ts index 16bb479..ea76509 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, STRUCTURE_CONTAINER); + Build(creep); } } }) \ No newline at end of file diff --git a/src/modules/builder.ts b/src/modules/builder.ts index c3c8b0d..3bfcabe 100644 --- a/src/modules/builder.ts +++ b/src/modules/builder.ts @@ -1,5 +1,5 @@ import { MAIN_SOURCE_ID } from "./setting"; -import { FindCapacity, GetConstructureSet, GetSource } from "./util"; +import { FindCapacity, GetConstructureSet, GetContainer, GetSource } from "./util"; export function Build(creep: Creep, structureType: string | null = null) { if (creep.memory.working && creep.store[RESOURCE_ENERGY] == 0) { @@ -27,9 +27,9 @@ export function Build(creep: Creep, structureType: string | null = null) { } } else { creep.memory.working = false - let source = GetSource(MAIN_SOURCE_ID); - if (creep.harvest(source) == ERR_NOT_IN_RANGE) { - creep.moveTo(source, { visualizePathStyle: { stroke: "#ffaa00" } }); + var target = GetContainer(creep); + if (creep.withdraw(target[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { + creep.moveTo(target[0], { visualizePathStyle: { stroke: '#ffaa00' } }); } } } \ No newline at end of file diff --git a/src/modules/harvester.ts b/src/modules/harvester.ts index ebe02aa..cb4e00c 100644 --- a/src/modules/harvester.ts +++ b/src/modules/harvester.ts @@ -1,8 +1,11 @@ -import { FindCapacity, GetSource } from "./util" +import { FindCapacity, GetContainer, GetSource } from "./util" import { MAIN_SOURCE_ID } from "./setting"; export function Harvest(creep: Creep) { - if (creep.store.getFreeCapacity() > 0) { + if (creep.store.getFreeCapacity() == null) { + BigHarvest(creep); + } + else if (creep.store.getFreeCapacity() > 0) { let source: Source = GetSource(MAIN_SOURCE_ID); if (creep.harvest(source) == ERR_NOT_IN_RANGE) { creep.moveTo(source, { visualizePathStyle: { stroke: "#ffaa00" } }); @@ -10,9 +13,18 @@ export function Harvest(creep: Creep) { } else { let targets = FindCapacity(creep); if (targets.length > 0) { - if(creep.transfer(targets[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE){ + if (creep.transfer(targets[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { creep.moveTo(targets[0], { visualizePathStyle: { stroke: "#ffffff" } }); } } } +} + +export function BigHarvest(creep: Creep) { + let source: Source = GetSource(MAIN_SOURCE_ID); + let container = GetContainer(creep); + if (container[0].store.getFreeCapacity(RESOURCE_ENERGY) == 0) { } + else if (creep.harvest(source) == ERR_NOT_IN_RANGE) { + creep.moveTo(container[0], { visualizePathStyle: { stroke: "#ffaa00" } }); + } } \ No newline at end of file diff --git a/src/modules/setting.ts b/src/modules/setting.ts index e740195..610e4d7 100644 --- a/src/modules/setting.ts +++ b/src/modules/setting.ts @@ -1,6 +1,6 @@ -let HARVESTER_COUNT: number = 2; -let BUILDER_COUNT: number = 3; -let UPGRADER_COUNT: number = 5; +let HARVESTER_COUNT: number = 0; +let BUILDER_COUNT: number = 1; +let UPGRADER_COUNT: number = 6; let MAIN_SOURCE_ID: Id = "ef990774d80108c" as Id; diff --git a/src/modules/upgrader.ts b/src/modules/upgrader.ts index c170709..c52a2d4 100644 --- a/src/modules/upgrader.ts +++ b/src/modules/upgrader.ts @@ -1,11 +1,11 @@ import { MAIN_SOURCE_ID } from "./setting"; -import { GetSource } from "./util"; +import { GetContainer, GetSource } from "./util"; /**@param {Creep} creep */ export function Upgrade(creep: Creep) { if (creep.memory.working && creep.store[RESOURCE_ENERGY] == 0) { creep.memory.working = false; - creep.say('🔄 harvest'); + creep.say('🔄 carry'); } if (!creep.memory.working && creep.store.getFreeCapacity() == 0) { creep.memory.working = true; @@ -18,9 +18,9 @@ export function Upgrade(creep: Creep) { } } else { - var source = GetSource(MAIN_SOURCE_ID); - if (creep.harvest(source) == ERR_NOT_IN_RANGE) { - creep.moveTo(source, { visualizePathStyle: { stroke: '#ffaa00' } }); + var target = GetContainer(creep); + if (creep.withdraw(target[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { + creep.moveTo(target[0], { visualizePathStyle: { stroke: '#ffaa00' } }); } } } \ No newline at end of file diff --git a/src/modules/util.ts b/src/modules/util.ts index 2f91288..c2d23c4 100644 --- a/src/modules/util.ts +++ b/src/modules/util.ts @@ -4,7 +4,8 @@ export function FindCapacity(creep: Creep): AnyStructure[] { let type = structure.structureType; return ( (type == STRUCTURE_EXTENSION || - type == STRUCTURE_SPAWN) && + type == STRUCTURE_SPAWN|| + type == STRUCTURE_CONTAINER) && structure.store.getFreeCapacity(RESOURCE_ENERGY) as number > 0 ) } @@ -22,4 +23,15 @@ export function GetConstructureSet(creep: Creep, structureType: string | null = return structure.structureType == structureType; } }) +} + +export function GetConstructure(creep: Creep, structureType: string, ){ + return creep.room.find(FIND_STRUCTURES, {filter:(structure: AnyStructure)=>{ + return structure.structureType == structureType; + }}) +} + +export function GetContainer(creep: Creep): StructureContainer[]{ + let target = GetConstructure(creep, STRUCTURE_CONTAINER) as StructureContainer[]; + return target } \ No newline at end of file