Refactor creep creation
This commit is contained in:
parent
a073110143
commit
1fa5ab1e4e
33
src/builder.ts
Normal file
33
src/builder.ts
Normal file
@ -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' } });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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' } });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
72
src/main.ts
72
src/main.ts
@ -1,25 +1,29 @@
|
|||||||
import { ErrorMapper } from 'utils/ErrorMapper';
|
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';
|
function makeName(role: string): string {
|
||||||
const ALPHABET_LENGTH = ALPHABET.length - 1;
|
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];
|
|
||||||
}
|
|
||||||
return str;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function uniqId(prefix: string = 'id'): string {
|
enum CreepRole {
|
||||||
return prefix + generateId(16);
|
HARVESTER = 'harvester',
|
||||||
|
UPGRADER = 'upgrader',
|
||||||
|
BUILDER = 'builder'
|
||||||
}
|
}
|
||||||
|
|
||||||
const ROLE_HARVESTER = 'harvester';
|
function makeCreep(role: CreepRole, count: number) {
|
||||||
const ROLE_UPGRADER = 'upgrader';
|
const creeps = Object.values(Game.creeps).filter((c) => c.memory.role === role);
|
||||||
const ROLE_BUILDER = 'builder';
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// When compiling TS to JS and bundling with rollup, the line numbers and file names in error messages change
|
// 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
|
// This utility uses source maps to get the line numbers and file names of the original, TS source code
|
||||||
@ -33,44 +37,20 @@ export const loop = ErrorMapper.wrapLoop(() => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create new creeps
|
makeCreep(CreepRole.HARVESTER, 1);
|
||||||
const HARVESTER_CREEP_COUNT = 1;
|
makeCreep(CreepRole.UPGRADER, 3);
|
||||||
const harvesterCreeps = Object.values(Game.creeps).filter((c) => c.memory.role === ROLE_HARVESTER);
|
makeCreep(CreepRole.BUILDER, 2);
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Process current creeps
|
// Process current creeps
|
||||||
for (let name in Game.creeps) {
|
for (let name in Game.creeps) {
|
||||||
const creep = Game.creeps[name];
|
const creep = Game.creeps[name];
|
||||||
if (creep.memory.role === ROLE_HARVESTER) {
|
if (creep.memory.role === CreepRole.HARVESTER) {
|
||||||
runAsHarvester(creep);
|
runAsHarvester(creep);
|
||||||
}
|
}
|
||||||
if (creep.memory.role === ROLE_UPGRADER) {
|
if (creep.memory.role === CreepRole.UPGRADER) {
|
||||||
runAsUpgrader(creep);
|
runAsUpgrader(creep);
|
||||||
}
|
}
|
||||||
if (creep.memory.role === ROLE_BUILDER) {
|
if (creep.memory.role === CreepRole.BUILDER) {
|
||||||
runAsBuilder(creep);
|
runAsBuilder(creep);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
24
src/upgrader.ts
Normal file
24
src/upgrader.ts
Normal file
@ -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' } });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
15
src/utils/Identity.ts
Normal file
15
src/utils/Identity.ts
Normal file
@ -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);
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user