From 9b28c28286cc312da6b8f084f74cabed0e08a596 Mon Sep 17 00:00:00 2001 From: Tom Harley Date: Tue, 15 Oct 2019 02:28:12 +0100 Subject: Rearrange role files --- src/role/builder.js | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/role/harvester.js | 25 +++++++++++++++ src/role/offender.js | 14 +++++++++ src/role/spawner.js | 53 +++++++++++++++++++++++++++++++ src/role/upgrader.js | 25 +++++++++++++++ 5 files changed, 204 insertions(+) create mode 100644 src/role/builder.js create mode 100644 src/role/harvester.js create mode 100644 src/role/offender.js create mode 100644 src/role/spawner.js create mode 100644 src/role/upgrader.js (limited to 'src/role') diff --git a/src/role/builder.js b/src/role/builder.js new file mode 100644 index 0000000..fd183da --- /dev/null +++ b/src/role/builder.js @@ -0,0 +1,87 @@ +// vim: tw=80 ts=2 sw=2 et + +const util = require('creep'); + +const states = { + IDLE: 1, + COLLECTING: "collecting", + MAINTINING: 3, + BUILDING: 4 +}; + +/* + * these three functions are a bit repetitve, might be enough to use the state + * value as a key to the right method to use. + * TODO + */ +function findWork(creep) { + + /* + * first, fill up on power, if we're not full + */ + if (!creep.isFull()) { + const target = creep.room.find(FIND_SOURCES_ACTIVE)[0]; + + creep.memory.target = target; + creep.memory.state = states.COLLECTING; + creep.log( + `collecting energy from ${target}`, + `${target.pos.x},${target.pos.y}`); + return creep.collect(); + } + + /* + * if we're full, then find a building to repair + */ + const damagedBuildings = creep.room.find(FIND_MY_STRUCTURES, { + filter: (s) => s.hits < s.hitsMax * 0.5, + }); + if (damagedBuildings.length) { + const target = damagedBuildings[0]; + creep.memory.target = target; + creep.memory.state = states.MAINTAINING; + creep.log( + `repairing ${target}`, + `${target.pos.x},${target.pos.y}`); + return creep.maintain(); + } + + /* + * if there's nothing to fix, find something to construct + */ + const constructionSites = creep.room.find(FIND_MY_CONSTRUCTION_SITES); + if (constructionSites.length) { + const target = constructionSites[0]; + creep.memory.target = target; + creep.memory.state = states.BUILDING; + creep.log(`building ${target}`, `${target.pos.x},${target.pos.y}`); + return creep.construct(); + } + + /* + * if there's nothing else to do, continue being idle + */ +}; + +function run(creep) { + switch (creep.memory.state) { + case states.BUILDING: + creep.construct(); + break; + case states.COLLECTING: + creep.collect(); + break; + case states.MAINTAINING: + creep.maintain(); + break; + case states.IDLE: + findWork(creep); + break; + default: + creep.error(`error: state ${creep.memory.state} unrecognised`); + } +}; + +module.exports = { + run: run +}; diff --git a/src/role/harvester.js b/src/role/harvester.js new file mode 100644 index 0000000..5c6f85c --- /dev/null +++ b/src/role/harvester.js @@ -0,0 +1,25 @@ +// vim: tw=80 ts=2 sw=2 et + +const util = require('creep'); + +module.exports = { + run: (creep) => { + if (creep.carry.energy < creep.carryCapacity) { + const source = creep.pos.findClosestByPath(FIND_SOURCES); + if (creep.harvest(source) == ERR_NOT_IN_RANGE) { + creep.moveTo(source); + } + } else { + const spawn = creep.pos.findClosestByPath(FIND_MY_STRUCTURES, { + filter: (structure) => { + return (structure.structureType == STRUCTURE_EXTENSION + || structure.structureType == STRUCTURE_SPAWN) + && structure.energy < structure.energyCapacity; + } + }); + if (creep.transfer(spawn, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { + creep.moveTo(spawn); + } + } + } +}; diff --git a/src/role/offender.js b/src/role/offender.js new file mode 100644 index 0000000..8cb95ac --- /dev/null +++ b/src/role/offender.js @@ -0,0 +1,14 @@ +// vim: tw=80 ts=2 sw=2 et + +const util = require('creep'); + +module.exports = { + run: (creep) => { + const hostiles = creep.room.find(FIND_HOSTILE_CREEPS); + if (hostiles.length > 0) { + if (creep.attack(hostiles[0]) == ERR_NOT_IN_RANGE) { + util.move(creep, hostiles[0]); + } + } + } +}; diff --git a/src/role/spawner.js b/src/role/spawner.js new file mode 100644 index 0000000..9d1eebf --- /dev/null +++ b/src/role/spawner.js @@ -0,0 +1,53 @@ +// vim: tw=80 ts=2 sw=2 et + +const creeps = new Map([ + [ 'upgrader', 1 ], + [ 'offender', 0 ], + [ 'builder', 3 ], + [ 'harvester', 5 ] +]); + +const spawnCreep = (spawn, job, tools) => { + const name = job + Game.time; + const new_memory = { + job: job, + path: {}, + + /* + * TODO: Is it better to keep the spawner's name (for retreival via + * Game.spawns[]) or to keep its id (for retrieval via + * Game.getObjectById())? + */ + spawned_from: spawn.name + }; + + if (spawn.spawnCreep(tools, name, { memory: new_memory }) == OK) { + spawn.log(`spawning a new ${job}`); + Memory.counts[job]++; + } +}; + +const creepSpawnerFunctions = { + harvester: (spawn) => { + spawnCreep(spawn, 'harvester', [WORK, MOVE, CARRY]); + }, + upgrader: (spawn) => { + spawnCreep(spawn, 'upgrader', [WORK, MOVE, CARRY]); + }, + offender: (spawn) => { + spawnCreep(spawn, 'offender', [ATTACK, MOVE]); + }, + builder: (spawn) => { + spawnCreep(spawn, 'builder', [MOVE, MOVE, WORK, CARRY]); + } +}; + +module.exports = { + run: (spawn) => { + creeps.forEach((count, job) => { + if (Memory.counts[job] < count) { + creepSpawnerFunctions[job](spawn); + } + }); + } +}; diff --git a/src/role/upgrader.js b/src/role/upgrader.js new file mode 100644 index 0000000..baf8ffd --- /dev/null +++ b/src/role/upgrader.js @@ -0,0 +1,25 @@ +// vim: tw=80 ts=2 sw=2 et + +const util = require('creep'); + +module.exports = { + run: (creep) => { + if (creep.memory.state === "withdrawing") { + const source = Game.spawns[creep.memory.spawned_from]; + if (creep.withdraw(source, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { + creep.moveTo(source); + } + if (creep.carry.energy == creep.carryCapacity) { + creep.memory.state = "upgrading"; + } + } else { + const controller = creep.room.controller; + if (creep.upgradeController(controller) == ERR_NOT_IN_RANGE) { + creep.moveTo(controller, { range: 3 }); + } + if (creep.carry.energy == 0) { + creep.memory.state = "withdrawing"; + } + } + } +}; -- cgit v1.2.1