diff options
| author | Tom Harley | 2019-10-15 02:14:27 +0100 |
|---|---|---|
| committer | Tom Harley | 2019-10-15 02:14:27 +0100 |
| commit | 511fb4e6bd8a5e736f91b1af676eec94257be462 (patch) | |
| tree | 0e0a1c1e7f2da020f0745a994082d90019a49e44 /src/role.builder.js | |
| parent | Extension-aware harvester energy transportation (diff) | |
| download | screeps-ai-511fb4e6bd8a5e736f91b1af676eec94257be462.tar.gz screeps-ai-511fb4e6bd8a5e736f91b1af676eec94257be462.zip | |
Various changes to pathfinding
Diffstat (limited to 'src/role.builder.js')
| -rw-r--r-- | src/role.builder.js | 110 |
1 files changed, 83 insertions, 27 deletions
diff --git a/src/role.builder.js b/src/role.builder.js index f243eac..fd183da 100644 --- a/src/role.builder.js +++ b/src/role.builder.js @@ -1,31 +1,87 @@ +// vim: tw=80 ts=2 sw=2 et + const util = require('creep'); -module.exports = { - run: (creep) => { - if (creep.memory.state === "building") { - if (creep.carry.energy > 0) { - // still building - const targets = creep.room.find(FIND_CONSTRUCTION_SITES); - if (targets.length) { - if (creep.build(targets[0]) == ERR_NOT_IN_RANGE) { - util.move(creep, targets[0]); - } - } - } else { - // just finished building - creep.memory.state = "collecting"; - } - } else { - if (creep.carry.energy < creep.carryCapacity) { - // still filling up - const spawn = Game.spawns['Initial']; - if (creep.withdraw(spawn, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { - util.move(creep, spawn); - } - } else { - // just finished filling up - creep.memory.state = "building"; - } - } +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 }; |
