diff --git a/src/creep/role/builder.ts b/src/creep/role/builder.ts index e5dd7a5..ad0ff18 100644 --- a/src/creep/role/builder.ts +++ b/src/creep/role/builder.ts @@ -3,6 +3,7 @@ import { createCreep, changeCreepActivity, upgradeCommonCreep, + upgradeCollectorCreep, } from '../../utils/creep'; import { Role, COMMON_CREEP, Activity } from '../../constants/creep'; import roleRepairer from './repairer'; @@ -31,8 +32,8 @@ const roleBuilder = { if (creep.build(target) == ERR_NOT_IN_RANGE) { creep.moveTo(target, { visualizePathStyle: { stroke: '#ffffff' }, - maxOps: 5000, reusePath: 15 - + maxOps: 5000, + reusePath: 15, }); } return Activity.BUILD; @@ -42,6 +43,10 @@ const roleBuilder = { } else if (creep.room.memory['wallsAndRamparts'].length > 0) { roleWallRepairer.run(creep); } else { + if (creep.room.memory['storages'].length) { + const storage = creep.room.memory['storages'][0]; + creep.memory['collectTarget'] = storage; + } return collectOrHarvest(creep); } }, @@ -64,7 +69,7 @@ const roleBuilder = { ) { createCreep(spawn, Role.BUILDER, [ ...COMMON_CREEP, - ...upgradeCommonCreep(spawn), + ...upgradeCollectorCreep(spawn), ]); } }, diff --git a/src/creep/role/collector.ts b/src/creep/role/collector.ts index 265e3d3..6e3f80e 100644 --- a/src/creep/role/collector.ts +++ b/src/creep/role/collector.ts @@ -83,14 +83,15 @@ const roleCollector = { if ( target && - creep.withdraw(target, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE + creep.withdraw(target, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE && + creep.memory['storeTarget'] != target.id ) { creep.memory['storeTarget'] = undefined; creep.memory['collectTarget'] = target.id; creep.moveTo(target, { visualizePathStyle: { stroke: '#ffaa00' }, - maxOps: 5000, reusePath: 15 - + maxOps: 5000, + reusePath: 15, }); return Activity.COLLECT; } @@ -124,7 +125,8 @@ const roleCollector = { ) as any; if ( tower.store.getUsedCapacity(RESOURCE_ENERGY) <= - tower.store.getCapacity(RESOURCE_ENERGY) / 2 + tower.store.getCapacity(RESOURCE_ENERGY) / 2 || + hostiles.length ) { target = tower; targetHasFreeCapacity = @@ -143,7 +145,7 @@ const roleCollector = { } if ( - target.id != creep.memory['collectTarget'] && + creep.memory['storeTarget'] != creep.memory['collectTarget'] && targetHasFreeCapacity ) { creep.memory['storeTarget'] = target.id; @@ -152,14 +154,14 @@ const roleCollector = { ) { creep.moveTo(target, { visualizePathStyle: { stroke: '#ffffff' }, - maxOps: 5000, reusePath: 15 - + maxOps: 5000, + reusePath: 15, }); } creep.memory['collectTarget'] = undefined; return Activity.STORE; } - return Activity.NONE; + return Activity.COLLECT; } return Activity.NONE; diff --git a/src/creep/role/repairer.ts b/src/creep/role/repairer.ts index 3781df5..7195a4d 100644 --- a/src/creep/role/repairer.ts +++ b/src/creep/role/repairer.ts @@ -8,6 +8,7 @@ import { import roleHarvester from './harvester'; import roleBuilder from './builder'; import roleWallRepairer from './wallRepairer'; +import roleCollector from './collector'; const roleRepairer = { run: function(creep: Creep) { @@ -39,8 +40,8 @@ const roleRepairer = { if (creep.repair(closestDamagedStructure) == ERR_NOT_IN_RANGE) { creep.moveTo(closestDamagedStructure, { visualizePathStyle: { stroke: '#ffffff' }, - maxOps: 5000, reusePath: 15 - + maxOps: 5000, + reusePath: 15, }); } return Activity.REPAIR; @@ -49,7 +50,7 @@ const roleRepairer = { } else if (creep.room.memory['wallsAndRamparts'].length > 0) { roleWallRepairer.run(creep); } else { - return roleHarvester.run(creep); + return roleCollector.run(creep); } } else if ( creep.room.memory['damagedStructures'].length == 0 && @@ -57,9 +58,13 @@ const roleRepairer = { ) { return roleBuilder.run(creep); } else if (creep.room.memory['damagedStructures'].length > 0) { + if (creep.room.memory['storages'].length) { + const storage = creep.room.memory['storages'][0]; + creep.memory['collectTarget'] = storage; + } return collectOrHarvest(creep); } else { - return roleHarvester.run(creep); + return roleCollector.run(creep); } }, generate: function(spawn: StructureSpawn) { diff --git a/src/creep/role/wallRepairer.ts b/src/creep/role/wallRepairer.ts index f7a5c4e..4d2fffe 100644 --- a/src/creep/role/wallRepairer.ts +++ b/src/creep/role/wallRepairer.ts @@ -64,9 +64,13 @@ const roleWallRepairer = { } return Activity.REPAIR; } else { - return roleHarvester.run(creep); + return collectOrHarvest(creep); } } else if (creep.room.memory['wallsAndRamparts'].length > 0) { + if (creep.room.memory['storages'].length) { + const storage = creep.room.memory['storages'][0]; + creep.memory['collectTarget'] = storage; + } return collectOrHarvest(creep); } else { return roleCollector.run(creep); diff --git a/src/room/handler.ts b/src/room/handler.ts index e230a92..7c3dd6e 100644 --- a/src/room/handler.ts +++ b/src/room/handler.ts @@ -5,120 +5,130 @@ function handleRooms() { for (const roomName in Game.rooms) { const room = Game.rooms[roomName]; - // # MEMORY - - // Resources - // --------------------------------------------- - const containers = room.find(FIND_STRUCTURES, { - filter: structure => structure.structureType == STRUCTURE_CONTAINER, - }); - - room.memory['containers'] = containers.map(c => c.id); - - room.memory['filledContainers'] = containers - // @ts-ignore - .filter(c => c.store.getUsedCapacity(RESOURCE_ENERGY) > 0) - .sort( - (a: any, b: any) => - b.store.getUsedCapacity(RESOURCE_ENERGY) - - a.store.getUsedCapacity(RESOURCE_ENERGY) - ) - .map(c => c.id); - - const storages = room.find(FIND_STRUCTURES, { - filter: structure => structure.structureType == STRUCTURE_STORAGE, - }); - - room.memory['storages'] = storages.map(s => s.id); - - const tombstones = room - .find(FIND_TOMBSTONES) - .filter(t => t.store[RESOURCE_ENERGY] > 0); - room.memory['tombstones'] = tombstones.map(t => t.id); - - const ruinsWithEnergy = room - // @ts-ignore - .find(FIND_RUINS) - .filter((r: any) => r.store && r.store[RESOURCE_ENERGY] > 0); - room.memory['ruinsWithEnergy'] = ruinsWithEnergy.map((r: any) => r.id); - - // STRUCTURES - // -------------------------------------------- - - room.memory['towers'] = room - .find(FIND_STRUCTURES) - .filter(s => s.structureType == STRUCTURE_TOWER) - .sort( - (a: any, b: any) => - a.store.getUsedCapacity(RESOURCE_ENERGY) - - b.store.getUsedCapacity(RESOURCE_ENERGY) - ) - .map(s => s.id); - - room.memory['constructionSites'] = room - .find(FIND_CONSTRUCTION_SITES) - .map(c => c.id); - - room.memory['damagedStructures'] = room - .find(FIND_STRUCTURES) - .filter( - s => - s.hits < s.hitsMax && - s.structureType != STRUCTURE_RAMPART && - s.structureType != STRUCTURE_WALL - ) - .sort((a, b) => a.hits - b.hits) - .map(s => s.id); - - room.memory['wallsAndRamparts'] = room - .find(FIND_STRUCTURES) - .filter( - s => - s.hits < MAX_WALL_HITS && - (s.structureType == STRUCTURE_RAMPART || - s.structureType == STRUCTURE_WALL) - ) - .sort((a, b) => { - if (a.structureType == STRUCTURE_RAMPART) { - return a.hits - 1000 - b.hits; - } - return a.hits - b.hits; - }) - .map(s => s.id); - - room.memory['extensions'] = room + const isOwnedRoom = room .find(FIND_MY_STRUCTURES) - .filter(s => s.structureType === STRUCTURE_EXTENSION) - .sort( - (a: any, b: any) => - a.store.getUsedCapacity(RESOURCE_ENERGY) - - b.store.getUsedCapacity(RESOURCE_ENERGY) - ) - .map(e => e.id); + .some(s => s.structureType == STRUCTURE_CONTROLLER); + + if (isOwnedRoom) { + // # MEMORY + + // Resources + // --------------------------------------------- + const containers = room.find(FIND_STRUCTURES, { + filter: structure => + structure.structureType == STRUCTURE_CONTAINER, + }); + + room.memory['containers'] = containers.map(c => c.id); + + room.memory['filledContainers'] = containers + // @ts-ignore + .filter(c => c.store.getUsedCapacity(RESOURCE_ENERGY) > 0) + .sort( + (a: any, b: any) => + b.store.getUsedCapacity(RESOURCE_ENERGY) - + a.store.getUsedCapacity(RESOURCE_ENERGY) + ) + .map(c => c.id); + + const storages = room.find(FIND_STRUCTURES, { + filter: structure => + structure.structureType == STRUCTURE_STORAGE, + }); + + room.memory['storages'] = storages.map(s => s.id); + + const tombstones = room + .find(FIND_TOMBSTONES) + .filter(t => t.store[RESOURCE_ENERGY] > 0); + room.memory['tombstones'] = tombstones.map(t => t.id); + + const ruinsWithEnergy = room + // @ts-ignore + .find(FIND_RUINS) + .filter((r: any) => r.store && r.store[RESOURCE_ENERGY] > 0); + room.memory['ruinsWithEnergy'] = ruinsWithEnergy.map( + (r: any) => r.id + ); + + // STRUCTURES + // -------------------------------------------- + + room.memory['towers'] = room + .find(FIND_STRUCTURES) + .filter(s => s.structureType == STRUCTURE_TOWER) + .sort( + (a: any, b: any) => + a.store.getUsedCapacity(RESOURCE_ENERGY) - + b.store.getUsedCapacity(RESOURCE_ENERGY) + ) + .map(s => s.id); + + room.memory['constructionSites'] = room + .find(FIND_CONSTRUCTION_SITES) + .map(c => c.id); + + room.memory['damagedStructures'] = room + .find(FIND_STRUCTURES) + .filter( + s => + s.hits < s.hitsMax && + s.structureType != STRUCTURE_RAMPART && + s.structureType != STRUCTURE_WALL + ) + .sort((a, b) => a.hits - b.hits) + .map(s => s.id); + + room.memory['wallsAndRamparts'] = room + .find(FIND_STRUCTURES) + .filter( + s => + s.hits < MAX_WALL_HITS && + (s.structureType == STRUCTURE_RAMPART || + s.structureType == STRUCTURE_WALL) + ) + .sort((a, b) => { + if (a.structureType == STRUCTURE_RAMPART) { + return a.hits - 1000 - b.hits; + } + return a.hits - b.hits; + }) + .map(s => s.id); + + room.memory['extensions'] = room + .find(FIND_MY_STRUCTURES) + .filter(s => s.structureType === STRUCTURE_EXTENSION) + .sort( + (a: any, b: any) => + a.store.getUsedCapacity(RESOURCE_ENERGY) - + b.store.getUsedCapacity(RESOURCE_ENERGY) + ) + .map(e => e.id); + + // CREEPS + // --------------------------------------------- + + room.memory['creeps'] = {}; + + for (const role in Role) { + room.memory['creeps'][Role[role]] = []; + } - // CREEPS - // --------------------------------------------- + room.memory['woundedCreeps'] = []; - room.memory['creeps'] = {}; + room.find(FIND_MY_CREEPS).forEach(creep => { + const role = creep.memory['role']; + if (role) { + room.memory['creeps'][role].push(creep.id); + } + if (creep.hits < creep.hitsMax) { + room.memory['woundedCreeps'].push(creep.id); + } + }); - for (const role in Role) { - room.memory['creeps'][Role[role]] = []; + const hostiles = room.find(FIND_HOSTILE_CREEPS); + room.memory['hostiles'] = hostiles.map(h => h.id); } - - room.memory['woundedCreeps'] = []; - - room.find(FIND_MY_CREEPS).forEach(creep => { - const role = creep.memory['role']; - if (role) { - room.memory['creeps'][role].push(creep.id); - } - if (creep.hits < creep.hitsMax) { - room.memory['woundedCreeps'].push(creep.id); - } - }); - - const hostiles = room.find(FIND_HOSTILE_CREEPS); - room.memory['hostiles'] = hostiles.map(h => h.id); } }