Use random source got building

This commit is contained in:
Anton Vakhrushev 2020-07-21 10:44:39 +03:00
parent 1fa5ab1e4e
commit 574204a837
2 changed files with 23 additions and 7 deletions

View File

@ -1,33 +1,49 @@
interface BuilderMemory extends CreepMemory {
building: boolean | undefined;
sourceNum: number | undefined;
}
function randomIntInRange(min: number, max: number): number {
const delta = max - min;
return Math.floor(Math.random() * delta) + min;
}
export function runAsBuilder(creep: Creep) {
const memory = creep.memory as CreepMemory & { building: boolean | undefined };
const memory = creep.memory as BuilderMemory;
if (memory.building && creep.store[RESOURCE_ENERGY] === 0) {
memory.building = false;
creep.say('🔄 harvest');
}
if (!memory.building && creep.store.getFreeCapacity() === 0) {
memory.building = true;
creep.say('🚧 build');
}
if (memory.sourceNum === undefined) {
const sources = creep.room.find(FIND_SOURCES);
memory.sourceNum = randomIntInRange(0, sources.length);
}
if (memory.building) {
const targets = creep.room.find(FIND_CONSTRUCTION_SITES);
if (targets.length > 0) {
if (creep.build(targets[0]) == ERR_NOT_IN_RANGE) {
creep.say('🚧 build');
creep.moveTo(targets[0], { visualizePathStyle: { stroke: '#ffffff' } });
}
} else {
const spawns = creep.room.find(FIND_MY_SPAWNS);
if (spawns.length > 0) {
creep.say('to spawn');
creep.say('To spawn');
creep.moveTo(spawns[0]);
}
}
} else {
const sources = creep.room.find(FIND_SOURCES);
if (creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) {
creep.moveTo(sources[0], { visualizePathStyle: { stroke: '#ffaa00' } });
const sourceNum = memory.sourceNum;
if (creep.harvest(sources[sourceNum]) == ERR_NOT_IN_RANGE) {
creep.say('🔄 harvest ' + sourceNum);
creep.moveTo(sources[sourceNum], { visualizePathStyle: { stroke: '#ffaa00' } });
}
}
}

View File

@ -39,7 +39,7 @@ export const loop = ErrorMapper.wrapLoop(() => {
makeCreep(CreepRole.HARVESTER, 1);
makeCreep(CreepRole.UPGRADER, 3);
makeCreep(CreepRole.BUILDER, 2);
makeCreep(CreepRole.BUILDER, 4);
// Process current creeps
for (let name in Game.creeps) {