Add tower support

This commit is contained in:
Anton Vakhrushev 2020-07-22 15:02:01 +03:00
parent cc7ac0e06e
commit 895888664b

View File

@ -19,16 +19,38 @@ function makeCreep(role: CreepRole, count: number) {
console.log(`Make creep "${role}"`, 'need', count, 'has', creeps.length); console.log(`Make creep "${role}"`, 'need', count, 'has', creeps.length);
if (creeps.length < count) { if (creeps.length < count) {
const firstSpawn = _.first(Object.values(Game.spawns)); const firstSpawn = _.first(Object.values(Game.spawns));
const capacity = firstSpawn.room.energyCapacityAvailable;
const body = selectBody(capacity);
const name = makeName(); const name = makeName();
const memory = { role: role, sourceId: '' } as CreepMemory; const memory = { role: role, sourceId: '' } as CreepMemory;
const err = firstSpawn.spawnCreep([WORK, WORK, CARRY, CARRY, MOVE, MOVE], name, { const err = firstSpawn.spawnCreep(body, name, {
memory, memory,
directions: [BOTTOM, BOTTOM_RIGHT, RIGHT] directions: [BOTTOM, BOTTOM_RIGHT, RIGHT, BOTTOM_LEFT, LEFT]
}); });
console.log(`Make creep "${role}"`, 'err', err); console.log(`Make creep "${role}"`, 'err', err);
} }
} }
function selectBody(capacity: number): Array<BodyPartConstant> {
const bodies = [
[WORK, WORK, CARRY, CARRY, CARRY, MOVE, MOVE, MOVE],
[WORK, WORK, CARRY, CARRY, MOVE, MOVE],
[WORK, CARRY, CARRY, MOVE, MOVE]
];
const fallback = [WORK, CARRY, MOVE];
for (let body of bodies) {
const cost = calcBodyCost(body);
if (cost < capacity) {
return body;
}
}
return fallback;
}
function calcBodyCost(body: Array<BodyPartConstant>): number {
return _.sum(body.map((p) => BODYPART_COST[p]));
}
function callHarvestersFromOthers(minHarvCount: number) { function callHarvestersFromOthers(minHarvCount: number) {
const harvesters = Object.values(Game.creeps).filter((c) => c.memory.role === CreepRole.HARVESTER); const harvesters = Object.values(Game.creeps).filter((c) => c.memory.role === CreepRole.HARVESTER);
if (harvesters.length < minHarvCount) { if (harvesters.length < minHarvCount) {
@ -52,6 +74,11 @@ export const loop = ErrorMapper.wrapLoop(() => {
console.log(''); console.log('');
console.log(`Current game tick is ${Game.time}`); console.log(`Current game tick is ${Game.time}`);
const room = _.first(Object.values(Game.rooms));
if (room) {
console.log('Room', room.name, room.energyCapacityAvailable, room.energyAvailable);
}
// Automatically delete memory of missing creeps // Automatically delete memory of missing creeps
for (const name in Memory.creeps) { for (const name in Memory.creeps) {
if (!(name in Game.creeps)) { if (!(name in Game.creeps)) {
@ -78,4 +105,41 @@ export const loop = ErrorMapper.wrapLoop(() => {
runAsBuilder(creep); runAsBuilder(creep);
} }
} }
for (let room of Object.values(Game.rooms)) {
for (let structure of room.find(FIND_MY_STRUCTURES)) {
if (structure.structureType === STRUCTURE_TOWER) {
runTower(structure);
}
}
}
}); });
function runTower(tower: StructureTower) {
console.log('Tower', tower.id);
const room = tower.room;
const hostile = room.find(FIND_HOSTILE_CREEPS);
if (hostile.length > 0) {
const res = tower.attack(_.first(hostile));
console.log('Attack', res);
return;
}
const creeps = room.find(FIND_MY_CREEPS, {
filter: (c) => c.hits < c.hitsMax
});
if (creeps.length > 0) {
const res = tower.heal(_.first(creeps));
console.log('Heal', res);
return;
}
const buildings = room.find(FIND_MY_STRUCTURES, {
filter: (s) => s.hits < s.hitsMax
});
if (buildings.length > 0) {
const res = tower.repair(_.first(buildings));
console.log('Repair', res);
}
}