Change energy restore priority

This commit is contained in:
Anton Vakhrushev 2020-07-21 19:20:42 +03:00
parent 390819a97f
commit c8fba64a2f
2 changed files with 40 additions and 29 deletions

View File

@ -1,5 +1,7 @@
import { selectSource } from './common'; import { selectSource } from './common';
const TARGET_TYPES = [STRUCTURE_EXTENSION, STRUCTURE_SPAWN, STRUCTURE_TOWER];
interface HarvesterMemory extends CreepMemory { interface HarvesterMemory extends CreepMemory {
sourceId: Id<Source> | undefined; sourceId: Id<Source> | undefined;
} }
@ -18,28 +20,40 @@ export function runAsHarvester(creep: Creep) {
creep.moveTo(source, { visualizePathStyle: { stroke: '#ffaa00' } }); creep.moveTo(source, { visualizePathStyle: { stroke: '#ffaa00' } });
} }
} else { } else {
let targets = creep.room.find(FIND_STRUCTURES, { const target = findTarget(creep);
if (target) {
moveEnergyToTarget(creep, target);
} else {
moveToSpawn(creep);
}
}
}
function findTarget(creep: Creep): Structure | undefined {
for (let type of TARGET_TYPES) {
const targets = creep.room.find(FIND_STRUCTURES, {
filter: (structure) => { filter: (structure) => {
return ( return structure.structureType === type && structure.store.getFreeCapacity(RESOURCE_ENERGY) > 0;
(structure.structureType === STRUCTURE_EXTENSION ||
structure.structureType === STRUCTURE_SPAWN ||
structure.structureType === STRUCTURE_TOWER) &&
structure.store.getFreeCapacity(RESOURCE_ENERGY) > 0
);
} }
}); });
if (targets.length) {
if (targets.length > 0) { return targets[0];
if (creep.transfer(targets[0], RESOURCE_ENERGY) === ERR_NOT_IN_RANGE) {
creep.say('🚛 transfer');
creep.moveTo(targets[0], { visualizePathStyle: { stroke: '#ffffff' } });
} }
} else { }
return undefined;
}
function moveEnergyToTarget(creep: Creep, target: Structure) {
if (creep.transfer(target, RESOURCE_ENERGY) === ERR_NOT_IN_RANGE) {
creep.say('🚛 transfer');
creep.moveTo(target, { visualizePathStyle: { stroke: '#ffffff' } });
}
}
function moveToSpawn(creep: Creep) {
const spawns = creep.room.find(FIND_MY_SPAWNS); const spawns = creep.room.find(FIND_MY_SPAWNS);
if (spawns.length > 0) { if (spawns.length > 0) {
creep.say('to spawn'); creep.say('to spawn');
creep.moveTo(spawns[0]); creep.moveTo(spawns[0]);
} }
}
}
} }

View File

@ -21,7 +21,7 @@ function makeCreep(role: CreepRole, count: number) {
const firstSpawn = _.first(Object.values(Game.spawns)); const firstSpawn = _.first(Object.values(Game.spawns));
const name = makeName(); const name = makeName();
const memory = { role: role } as CreepMemory; const memory = { role: role } as CreepMemory;
const err = firstSpawn.spawnCreep([WORK, CARRY, MOVE], name, { memory }); const err = firstSpawn.spawnCreep([WORK, WORK, CARRY, CARRY, MOVE], name, { memory });
console.log(`Make creep "${role}"`, 'err', err); console.log(`Make creep "${role}"`, 'err', err);
} }
} }
@ -56,14 +56,11 @@ export const loop = ErrorMapper.wrapLoop(() => {
} }
} }
const harvCount = 4; callHarvestersFromOthers(2);
const minHarvCount = 2;
makeCreep(CreepRole.HARVESTER, harvCount); makeCreep(CreepRole.HARVESTER, 2);
makeCreep(CreepRole.UPGRADER, 8); makeCreep(CreepRole.UPGRADER, 4);
makeCreep(CreepRole.BUILDER, 8); makeCreep(CreepRole.BUILDER, 4);
callHarvestersFromOthers(minHarvCount);
// Process current creeps // Process current creeps
for (let name in Game.creeps) { for (let name in Game.creeps) {