From 895888664b402c664958608dae448c4e502c1204 Mon Sep 17 00:00:00 2001 From: Anton Vakhrushev Date: Wed, 22 Jul 2020 15:02:01 +0300 Subject: [PATCH] Add tower support --- src/main.ts | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 66 insertions(+), 2 deletions(-) diff --git a/src/main.ts b/src/main.ts index db22187..0187f5d 100644 --- a/src/main.ts +++ b/src/main.ts @@ -19,16 +19,38 @@ function makeCreep(role: CreepRole, count: number) { console.log(`Make creep "${role}"`, 'need', count, 'has', creeps.length); if (creeps.length < count) { const firstSpawn = _.first(Object.values(Game.spawns)); + const capacity = firstSpawn.room.energyCapacityAvailable; + const body = selectBody(capacity); const name = makeName(); 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, - directions: [BOTTOM, BOTTOM_RIGHT, RIGHT] + directions: [BOTTOM, BOTTOM_RIGHT, RIGHT, BOTTOM_LEFT, LEFT] }); console.log(`Make creep "${role}"`, 'err', err); } } +function selectBody(capacity: number): Array { + 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): number { + return _.sum(body.map((p) => BODYPART_COST[p])); +} + function callHarvestersFromOthers(minHarvCount: number) { const harvesters = Object.values(Game.creeps).filter((c) => c.memory.role === CreepRole.HARVESTER); if (harvesters.length < minHarvCount) { @@ -52,6 +74,11 @@ export const loop = ErrorMapper.wrapLoop(() => { console.log(''); 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 for (const name in Memory.creeps) { if (!(name in Game.creeps)) { @@ -78,4 +105,41 @@ export const loop = ErrorMapper.wrapLoop(() => { 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); + } +}