diff --git a/src/managers/rolesManager.ts b/src/managers/rolesManager.ts index 7d247e2..0c12c92 100644 --- a/src/managers/rolesManager.ts +++ b/src/managers/rolesManager.ts @@ -5,17 +5,15 @@ export enum Role { CARRIER = "CARRIER", UPGRADER = "UPGRADER", REPAIRER = "REPAIRER", - BUILDER = "BUILDER", - SAFER = "SAFER" + BUILDER = "BUILDER" } -export const RoleTasks: Record = { - [Role.HARVESTER]: [Task.HARVEST], - [Role.CARRIER]: [Task.CARRY, Task.SPREAD_ENERGY], - [Role.UPGRADER]: [Task.UPGRADE], - [Role.REPAIRER]: [Task.REPAIR], - [Role.BUILDER]: [Task.BUILD], - [Role.SAFER]: [Task.STORE] +export const RoleTasks: Record = { + [Role.HARVESTER]: [[Task.HARVEST]], + [Role.CARRIER]: [[Task.CARRY], [Task.STORE, Task.SPREAD_ENERGY], [Task.SPREAD_ENERGY]], + [Role.UPGRADER]: [[Task.UPGRADE]], + [Role.REPAIRER]: [[Task.REPAIR, Task.BUILD]], + [Role.BUILDER]: [[Task.BUILD, Task.REPAIR]] } export const RoleBodyParts: Record> = { @@ -23,8 +21,7 @@ export const RoleBodyParts: Record> = { [Role.CARRIER]: {[MOVE]: 0.4, [CARRY]: 0.5, [WORK]: 0.1}, [Role.UPGRADER]: {[MOVE]: 0.1, [CARRY]: 0.4, [WORK]: 0.5}, [Role.REPAIRER]: {[MOVE]: 0.3, [CARRY]: 0.3, [WORK]: 0.4}, - [Role.BUILDER]: {[MOVE]: 0.3, [CARRY]: 0.3, [WORK]: 0.4}, - [Role.SAFER]: {[MOVE]: 0.4, [CARRY]: 0.5, [WORK]: 0.1} + [Role.BUILDER]: {[MOVE]: 0.3, [CARRY]: 0.3, [WORK]: 0.4} } export function run(creep: Creep): void { @@ -32,7 +29,7 @@ export function run(creep: Creep): void { return; } - // Self-harvest + // Self-harvest energy if (creep.memory.needEnergy && creep.store.getFreeCapacity() > 0 && _.filter(Game.creeps, c => c.memory.role === Role.CARRIER).length <= 0) { const target: Source | null = creep.pos.findClosestByPath(FIND_SOURCES_ACTIVE); @@ -51,13 +48,23 @@ export function run(creep: Creep): void { } } - if (!creep.memory.task) creep.memory.task = RoleTasks[creep.memory.role][0]; - - const taskStatus = creep.runTask(creep.memory.task); - if (taskStatus === TaskStatus.COMPLETED) { - const currentTaskIndex = RoleTasks[creep.memory.role].indexOf(creep.memory.task); - const nextTaskIndex = currentTaskIndex + 1 > RoleTasks[creep.memory.role].length ? 0 : currentTaskIndex + 1 - creep.memory.task = RoleTasks[creep.memory.role][nextTaskIndex]; + // Assign creep task and run it + const creepTasks = RoleTasks[creep.memory.role]; + if (!creep.memory.task) { + creep.memory.taskIndex = 0; + creep.memory.task = creepTasks[0][0]; + } + switch (creep.runTask(creep.memory.task)) { + case TaskStatus.COMPLETED: + creep.memory.taskIndex += 1; + if (!creepTasks[creep.memory.taskIndex]) creep.memory.taskIndex = 0; + creep.memory.task = creepTasks[creep.memory.taskIndex][0]; + break; + case TaskStatus.FAILED: + let nextSubTaskIndex = Object.values(creepTasks[creep.memory.taskIndex]).indexOf(creep.memory.task)+1; + if (!creepTasks[creep.memory.taskIndex][nextSubTaskIndex]) nextSubTaskIndex = 0; + creep.memory.task = creepTasks[creep.memory.taskIndex][nextSubTaskIndex] + break; } if (Memory.debug.drawRoles) { diff --git a/src/managers/roomManager.ts b/src/managers/roomManager.ts index 83018dc..6c173bf 100644 --- a/src/managers/roomManager.ts +++ b/src/managers/roomManager.ts @@ -21,10 +21,6 @@ Room.prototype.configuration = function () { { role: Role.BUILDER, needed: this.find(FIND_MY_CONSTRUCTION_SITES).length > 0 ? 1 : 0 - }, - { - role: Role.SAFER, - needed: this.sourceStorages().length } ]; } @@ -54,7 +50,6 @@ export function run(): void { if (Game.time % 100 === 0 || !configurations[roomHash]) { configurations[roomHash] = room.configuration(); configurations[roomHash].forEach(conf => subpopulations[roomHash] += conf.needed); - console.log(subpopulations[roomHash]); } } @@ -64,31 +59,28 @@ export function run(): void { const neededRole = room.neededRoles()[0]; if (neededRole) { - console.log(neededRole); // TODO: Look to unused creeps const spawn: StructureSpawn = _.filter(room.closestSpawns(), s => !s.spawning)[0]; if (spawn) { const creepName: string = neededRole + "_" + Math.random().toString(36).substr(2, 5); - const saferCount = _.filter(Game.creeps, c => c.memory.role === Role.SAFER && c.memory.room === spawn.room.name).length; - let energyRemaining: number = saferCount > 0 ? spawn.room.energyCapacityAvailable : spawn.store.getCapacity(RESOURCE_ENERGY); const creepParts: BodyPartConstant[] = []; - const keys = Object.keys(RoleBodyParts[neededRole as Role]); for (const bodyPart in RoleBodyParts[neededRole as Role]) { const percentage = RoleBodyParts[neededRole as Role][bodyPart]; const isLast = keys[keys.length] === bodyPart; - for (let i = 0; i < (isLast ? energyRemaining : energyRemaining * percentage) / BODYPART_COST[bodyPart as BodyPartConstant]; i++) { + for (let i = 0; i < (isLast ? spawn.room.energyCapacityAvailable : spawn.room.energyCapacityAvailable * percentage) / BODYPART_COST[bodyPart as BodyPartConstant]; i++) { creepParts.push(bodyPart as BodyPartConstant); - energyRemaining -= BODYPART_COST[bodyPart as BodyPartConstant]; + spawn.room.energyCapacityAvailable -= BODYPART_COST[bodyPart as BodyPartConstant]; } } const spawnStatus = spawn.spawnCreep(creepParts, creepName, { memory: { role: neededRole, - task: RoleTasks[neededRole][0], + taskIndex: 0, + task: RoleTasks[neededRole][0][0], room: room.name } }); diff --git a/src/tasks/store.ts b/src/tasks/store.ts index ca6419a..80f5da8 100644 --- a/src/tasks/store.ts +++ b/src/tasks/store.ts @@ -7,7 +7,7 @@ export function run(creep: Creep): TaskStatus { return TaskStatus.COMPLETED; } else if (creep.store.getFreeCapacity() <= 0) creep.memory.needEnergy = false; - const storages = creep.room.find(FIND_STRUCTURES, { + const storages = creep.room.find(FIND_MY_STRUCTURES, { filter: (structure) => (structure.structureType === STRUCTURE_SPAWN || structure.structureType === STRUCTURE_EXTENSION || structure.structureType === STRUCTURE_STORAGE || diff --git a/src/types.d.ts b/src/types.d.ts index d614069..85ac96f 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -4,6 +4,7 @@ interface Memory { interface CreepMemory { role: string; + taskIndex: number; task: string; room: string; needEnergy?: boolean;