From 1fa5ab1e4e055f33142f0f3ea5a105a875d46c5f Mon Sep 17 00:00:00 2001 From: Anton Vakhrushev Date: Tue, 21 Jul 2020 10:12:47 +0300 Subject: [PATCH] Refactor creep creation --- src/builder.ts | 33 +++++++++++++++++++ src/harvester.ts | 59 ---------------------------------- src/main.ts | 74 ++++++++++++++++--------------------------- src/upgrader.ts | 24 ++++++++++++++ src/utils/Identity.ts | 15 +++++++++ 5 files changed, 99 insertions(+), 106 deletions(-) create mode 100644 src/builder.ts create mode 100644 src/upgrader.ts create mode 100644 src/utils/Identity.ts diff --git a/src/builder.ts b/src/builder.ts new file mode 100644 index 0000000..65d23a9 --- /dev/null +++ b/src/builder.ts @@ -0,0 +1,33 @@ +export function runAsBuilder(creep: Creep) { + const memory = creep.memory as CreepMemory & { building: boolean | undefined }; + + 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.building) { + const targets = creep.room.find(FIND_CONSTRUCTION_SITES); + if (targets.length > 0) { + if (creep.build(targets[0]) == ERR_NOT_IN_RANGE) { + 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.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' } }); + } + } +} diff --git a/src/harvester.ts b/src/harvester.ts index 507b9b5..4a0c568 100644 --- a/src/harvester.ts +++ b/src/harvester.ts @@ -31,62 +31,3 @@ export function runAsHarvester(creep: Creep) { } } } - -export function runAsBuilder(creep: Creep) { - const memory = creep.memory as CreepMemory & { building: boolean | undefined }; - - 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.building) { - const targets = creep.room.find(FIND_CONSTRUCTION_SITES); - if (targets.length > 0) { - if (creep.build(targets[0]) == ERR_NOT_IN_RANGE) { - 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.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' } }); - } - } -} - -export function runAsUpgrader(creep: Creep) { - const memory = creep.memory as CreepMemory & { upgrading: boolean | undefined }; - - if (memory.upgrading && creep.store[RESOURCE_ENERGY] === 0) { - memory.upgrading = false; - creep.say('🔄 harvest'); - } - - if (!memory.upgrading && creep.store.getFreeCapacity() === 0) { - memory.upgrading = true; - creep.say('⚡ upgrade'); - } - - if (memory.upgrading) { - if (creep.room.controller && creep.upgradeController(creep.room.controller) == ERR_NOT_IN_RANGE) { - creep.moveTo(creep.room.controller, { visualizePathStyle: { stroke: '#ffffff' } }); - } - } else { - const sources = creep.room.find(FIND_SOURCES); - if (creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) { - creep.moveTo(sources[0], { visualizePathStyle: { stroke: '#ffaa00' } }); - } - } -} diff --git a/src/main.ts b/src/main.ts index 2e89b9c..307b6b4 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,26 +1,30 @@ import { ErrorMapper } from 'utils/ErrorMapper'; -import { runAsBuilder, runAsHarvester, runAsUpgrader } from './harvester'; +import { runAsHarvester } from './harvester'; +import { runAsBuilder } from './builder'; +import { runAsUpgrader } from './upgrader'; +import { uniqId } from './utils/Identity'; -const ALPHABET = 'abcdefghijklmnopqrstuvwxyz1234567890'; -const ALPHABET_LENGTH = ALPHABET.length - 1; +function makeName(role: string): string { + return uniqId(role + '_'); +} -function generateId(count: number): string { - let str = ''; - for (let i = 0; i < count; ++i) { - let symbolIndex = Math.floor(Math.random() * ALPHABET_LENGTH); - str += ALPHABET[symbolIndex]; +enum CreepRole { + HARVESTER = 'harvester', + UPGRADER = 'upgrader', + BUILDER = 'builder' +} + +function makeCreep(role: CreepRole, count: number) { + const creeps = Object.values(Game.creeps).filter((c) => c.memory.role === role); + if (creeps.length < count) { + const firstSpawn = _.first(Object.values(Game.spawns)); + const name = makeName(role); + const memory = { role: role } as CreepMemory; + const err = firstSpawn.spawnCreep([WORK, CARRY, MOVE], name, { memory }); + console.log(`Make creep "${role}" err`, err); } - return str; } -export function uniqId(prefix: string = 'id'): string { - return prefix + generateId(16); -} - -const ROLE_HARVESTER = 'harvester'; -const ROLE_UPGRADER = 'upgrader'; -const ROLE_BUILDER = 'builder'; - // When compiling TS to JS and bundling with rollup, the line numbers and file names in error messages change // This utility uses source maps to get the line numbers and file names of the original, TS source code export const loop = ErrorMapper.wrapLoop(() => { @@ -33,44 +37,20 @@ export const loop = ErrorMapper.wrapLoop(() => { } } - // Create new creeps - const HARVESTER_CREEP_COUNT = 1; - const harvesterCreeps = Object.values(Game.creeps).filter((c) => c.memory.role === ROLE_HARVESTER); - if (harvesterCreeps.length < HARVESTER_CREEP_COUNT) { - const firstSpawn = _.first(Object.values(Game.spawns)); - const name = uniqId(ROLE_HARVESTER); - const err = firstSpawn.spawnCreep([WORK, CARRY, MOVE], name, { memory: { role: ROLE_HARVESTER } as CreepMemory }); - console.log('Harvester Err', err); - } - - const UPGRADER_CREEP_COUNT = 3; - const upgraderCreeps = Object.values(Game.creeps).filter((c) => c.memory.role === ROLE_UPGRADER); - if (upgraderCreeps.length < UPGRADER_CREEP_COUNT) { - const firstSpawn = _.first(Object.values(Game.spawns)); - const name = uniqId(ROLE_UPGRADER); - const err = firstSpawn.spawnCreep([WORK, CARRY, MOVE], name, { memory: { role: ROLE_UPGRADER } as CreepMemory }); - console.log('Upgrader Err', err); - } - - const BUILDER_CREEP_COUNT = 2; - const builderCreeps = Object.values(Game.creeps).filter((c) => c.memory.role === ROLE_BUILDER); - if (builderCreeps.length < BUILDER_CREEP_COUNT) { - const firstSpawn = _.first(Object.values(Game.spawns)); - const name = uniqId(ROLE_BUILDER); - const err = firstSpawn.spawnCreep([WORK, CARRY, MOVE], name, { memory: { role: ROLE_BUILDER } as CreepMemory }); - console.log('Builder Err', err); - } + makeCreep(CreepRole.HARVESTER, 1); + makeCreep(CreepRole.UPGRADER, 3); + makeCreep(CreepRole.BUILDER, 2); // Process current creeps for (let name in Game.creeps) { const creep = Game.creeps[name]; - if (creep.memory.role === ROLE_HARVESTER) { + if (creep.memory.role === CreepRole.HARVESTER) { runAsHarvester(creep); } - if (creep.memory.role === ROLE_UPGRADER) { + if (creep.memory.role === CreepRole.UPGRADER) { runAsUpgrader(creep); } - if (creep.memory.role === ROLE_BUILDER) { + if (creep.memory.role === CreepRole.BUILDER) { runAsBuilder(creep); } } diff --git a/src/upgrader.ts b/src/upgrader.ts new file mode 100644 index 0000000..ce93549 --- /dev/null +++ b/src/upgrader.ts @@ -0,0 +1,24 @@ +export function runAsUpgrader(creep: Creep) { + const memory = creep.memory as CreepMemory & { upgrading: boolean | undefined }; + + if (memory.upgrading && creep.store[RESOURCE_ENERGY] === 0) { + memory.upgrading = false; + creep.say('🔄 harvest'); + } + + if (!memory.upgrading && creep.store.getFreeCapacity() === 0) { + memory.upgrading = true; + creep.say('⚡ upgrade'); + } + + if (memory.upgrading) { + if (creep.room.controller && creep.upgradeController(creep.room.controller) == ERR_NOT_IN_RANGE) { + creep.moveTo(creep.room.controller, { visualizePathStyle: { stroke: '#ffffff' } }); + } + } else { + const sources = creep.room.find(FIND_SOURCES); + if (creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) { + creep.moveTo(sources[0], { visualizePathStyle: { stroke: '#ffaa00' } }); + } + } +} diff --git a/src/utils/Identity.ts b/src/utils/Identity.ts new file mode 100644 index 0000000..3135929 --- /dev/null +++ b/src/utils/Identity.ts @@ -0,0 +1,15 @@ +const ALPHABET = 'abcdefghijklmnopqrstuvwxyz1234567890'; +const ALPHABET_LENGTH = ALPHABET.length - 1; + +function generateId(count: number): string { + let str = ''; + for (let i = 0; i < count; ++i) { + let symbolIndex = Math.floor(Math.random() * ALPHABET_LENGTH); + str += ALPHABET[symbolIndex]; + } + return str; +} + +export function uniqId(prefix: string = 'id', len: number = 8): string { + return prefix + generateId(len); +}