抽出根据ID获取资源函数, 防止每tick找到的资源顺序不一致

This commit is contained in:
jie 2024-11-03 23:20:58 +08:00
parent 542d127eea
commit 0fa23009ad
6 changed files with 41 additions and 19 deletions

View File

@ -20,7 +20,7 @@ export const loop = errorMapper(() => {
} else if (creep.memory.role == "Upgrader") { } else if (creep.memory.role == "Upgrader") {
Upgrade(creep); Upgrade(creep);
} else if (creep.memory.role == "Builder") { } else if (creep.memory.role == "Builder") {
Build(creep); Build(creep, STRUCTURE_CONTAINER);
} }
} }
}) })

View File

@ -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) { if (creep.memory.working && creep.store[RESOURCE_ENERGY] == 0) {
creep.memory.working = false; creep.memory.working = false;
creep.say("harvest") creep.say("harvest")
@ -10,7 +11,7 @@ export function Build(creep: Creep) {
creep.say("builder") creep.say("builder")
} }
if (creep.memory.working) { if (creep.memory.working) {
let target = creep.room.find(FIND_CONSTRUCTION_SITES); let target = GetConstructureSet(creep, structureType);
if (target.length > 0) { if (target.length > 0) {
if (creep.build(target[0]) == ERR_NOT_IN_RANGE) { if (creep.build(target[0]) == ERR_NOT_IN_RANGE) {
creep.moveTo(target[0], { visualizePathStyle: { stroke: "#ffaa00" } }); creep.moveTo(target[0], { visualizePathStyle: { stroke: "#ffaa00" } });
@ -26,9 +27,9 @@ export function Build(creep: Creep) {
} }
} else { } else {
creep.memory.working = false creep.memory.working = false
let sources = creep.room.find(FIND_SOURCES); let source = GetSource(MAIN_SOURCE_ID);
if (creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) { if (creep.harvest(source) == ERR_NOT_IN_RANGE) {
creep.moveTo(sources[0], { visualizePathStyle: { stroke: "#ffaa00" } }); creep.moveTo(source, { visualizePathStyle: { stroke: "#ffaa00" } });
} }
} }
} }

View File

@ -1,10 +1,11 @@
import { FindCapacity } from "./util" import { FindCapacity, GetSource } from "./util"
import { MAIN_SOURCE_ID } from "./setting";
export function Harvest(creep: Creep) { export function Harvest(creep: Creep) {
if (creep.store.getFreeCapacity() > 0) { if (creep.store.getFreeCapacity() > 0) {
let sources: Source[] = creep.room.find(FIND_SOURCES); let source: Source = GetSource(MAIN_SOURCE_ID);
if (creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) { if (creep.harvest(source) == ERR_NOT_IN_RANGE) {
creep.moveTo(sources[0], { visualizePathStyle: { stroke: "#ffaa00" } }); creep.moveTo(source, { visualizePathStyle: { stroke: "#ffaa00" } });
} }
} else { } else {
let targets = FindCapacity(creep); let targets = FindCapacity(creep);

View File

@ -1,9 +1,13 @@
let HARVESTER_COUNT: number = 4; let HARVESTER_COUNT: number = 2;
let BUILDER_COUNT: number = 3; let BUILDER_COUNT: number = 3;
let UPGRADER_COUNT: number = 6; let UPGRADER_COUNT: number = 5;
let MAIN_SOURCE_ID: Id<Source> = "ef990774d80108c" as Id<Source>;
export { export {
HARVESTER_COUNT, HARVESTER_COUNT,
BUILDER_COUNT, BUILDER_COUNT,
UPGRADER_COUNT UPGRADER_COUNT,
MAIN_SOURCE_ID,
} }

View File

@ -1,3 +1,6 @@
import { MAIN_SOURCE_ID } from "./setting";
import { GetSource } from "./util";
/**@param {Creep} creep */ /**@param {Creep} creep */
export function Upgrade(creep: Creep) { export function Upgrade(creep: Creep) {
if (creep.memory.working && creep.store[RESOURCE_ENERGY] == 0) { if (creep.memory.working && creep.store[RESOURCE_ENERGY] == 0) {
@ -15,9 +18,9 @@ export function Upgrade(creep: Creep) {
} }
} }
else { else {
var sources = creep.room.find(FIND_SOURCES); var source = GetSource(MAIN_SOURCE_ID);
if (creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) { if (creep.harvest(source) == ERR_NOT_IN_RANGE) {
creep.moveTo(sources[0], { visualizePathStyle: { stroke: '#ffaa00' } }); creep.moveTo(source, { visualizePathStyle: { stroke: '#ffaa00' } });
} }
} }
} }

View File

@ -1,12 +1,25 @@
export function FindCapacity(creep: Creep) { export function FindCapacity(creep: Creep): AnyStructure[] {
return creep.room.find(FIND_STRUCTURES, { return creep.room.find(FIND_STRUCTURES, {
filter: (structure: StructureSpawn | StructureExtension) => { filter: (structure: StructureSpawn | StructureExtension) => {
let type = structure.structureType; let type = structure.structureType;
return ( return (
(type == STRUCTURE_EXTENSION || (type == STRUCTURE_EXTENSION ||
type == STRUCTURE_SPAWN) && type == STRUCTURE_SPAWN) &&
structure.store.getFreeCapacity(RESOURCE_ENERGY) as number > 0 structure.store.getFreeCapacity(RESOURCE_ENERGY) as number > 0
) )
} }
}) })
} }
export function GetSource(sourceId: Id<Source> | null): Source {
return Game.getObjectById(sourceId);
}
export function GetConstructureSet(creep: Creep, structureType: string | null = null): ConstructionSite<BuildableStructureConstant>[] {
return creep.room.find(FIND_CONSTRUCTION_SITES, {
filter: (structure: Structure) => {
if (structureType == null) return true;
return structure.structureType == structureType;
}
})
}