抽出根据ID获取资源函数, 防止每tick找到的资源顺序不一致
This commit is contained in:
parent
542d127eea
commit
0fa23009ad
@ -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);
|
||||
}
|
||||
}
|
||||
})
|
@ -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" } });
|
||||
}
|
||||
}
|
||||
}
|
@ -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);
|
||||
|
@ -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<Source> = "ef990774d80108c" as Id<Source>;
|
||||
|
||||
export {
|
||||
HARVESTER_COUNT,
|
||||
BUILDER_COUNT,
|
||||
UPGRADER_COUNT
|
||||
UPGRADER_COUNT,
|
||||
MAIN_SOURCE_ID,
|
||||
}
|
||||
|
||||
|
@ -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' } });
|
||||
}
|
||||
}
|
||||
}
|
@ -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<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;
|
||||
}
|
||||
})
|
||||
}
|
Loading…
Reference in New Issue
Block a user