Add upgrader source selection

This commit is contained in:
Anton Vakhrushev 2020-07-21 11:03:01 +03:00
parent 574204a837
commit 60bd33c8d4
4 changed files with 25 additions and 10 deletions

View File

@ -1,13 +1,10 @@
import { randomIntInRange } from './utils/Random';
interface BuilderMemory extends CreepMemory { interface BuilderMemory extends CreepMemory {
building: boolean | undefined; building: boolean | undefined;
sourceNum: number | 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) { export function runAsBuilder(creep: Creep) {
const memory = creep.memory as BuilderMemory; const memory = creep.memory as BuilderMemory;

View File

@ -16,6 +16,7 @@ enum CreepRole {
function makeCreep(role: CreepRole, count: number) { function makeCreep(role: CreepRole, count: number) {
const creeps = Object.values(Game.creeps).filter((c) => c.memory.role === role); const creeps = Object.values(Game.creeps).filter((c) => c.memory.role === role);
console.log('Make creep ', role, 'need', count, 'has', creeps.length);
if (creeps.length < count) { if (creeps.length < count) {
const firstSpawn = _.first(Object.values(Game.spawns)); const firstSpawn = _.first(Object.values(Game.spawns));
const name = makeName(role); const name = makeName(role);

View File

@ -1,24 +1,37 @@
import { randomIntInRange } from './utils/Random';
interface UpgraderMemory extends CreepMemory {
upgrading: boolean | undefined;
sourceNum: number | undefined;
}
export function runAsUpgrader(creep: Creep) { export function runAsUpgrader(creep: Creep) {
const memory = creep.memory as CreepMemory & { upgrading: boolean | undefined }; const memory = creep.memory as UpgraderMemory;
if (memory.upgrading && creep.store[RESOURCE_ENERGY] === 0) { if (memory.upgrading && creep.store[RESOURCE_ENERGY] === 0) {
memory.upgrading = false; memory.upgrading = false;
creep.say('🔄 harvest');
} }
if (!memory.upgrading && creep.store.getFreeCapacity() === 0) { if (!memory.upgrading && creep.store.getFreeCapacity() === 0) {
memory.upgrading = true; memory.upgrading = true;
creep.say('⚡ upgrade'); }
if (memory.sourceNum === undefined) {
const sources = creep.room.find(FIND_SOURCES);
memory.sourceNum = randomIntInRange(0, sources.length);
} }
if (memory.upgrading) { if (memory.upgrading) {
if (creep.room.controller && creep.upgradeController(creep.room.controller) == ERR_NOT_IN_RANGE) { if (creep.room.controller && creep.upgradeController(creep.room.controller) == ERR_NOT_IN_RANGE) {
creep.say('⚡ upgrade');
creep.moveTo(creep.room.controller, { visualizePathStyle: { stroke: '#ffffff' } }); creep.moveTo(creep.room.controller, { visualizePathStyle: { stroke: '#ffffff' } });
} }
} else { } else {
const sources = creep.room.find(FIND_SOURCES); const sources = creep.room.find(FIND_SOURCES);
if (creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) { const sourceNum = memory.sourceNum;
creep.moveTo(sources[0], { visualizePathStyle: { stroke: '#ffaa00' } }); if (creep.harvest(sources[sourceNum]) == ERR_NOT_IN_RANGE) {
creep.say('🔄 harvest ' + sourceNum);
creep.moveTo(sources[sourceNum], { visualizePathStyle: { stroke: '#ffaa00' } });
} }
} }
} }

4
src/utils/Random.ts Normal file
View File

@ -0,0 +1,4 @@
export function randomIntInRange(min: number, max: number): number {
const delta = max - min;
return Math.floor(Math.random() * delta) + min;
}