From 9dac06627fc323a278e45f36d41bc027f095c945 Mon Sep 17 00:00:00 2001 From: Anton Vakhrushev Date: Thu, 23 Jul 2020 18:46:55 +0300 Subject: [PATCH] Refactoring --- src/main.ts | 116 ++++++++++++++++++++++++++-------------------------- 1 file changed, 59 insertions(+), 57 deletions(-) diff --git a/src/main.ts b/src/main.ts index b99177e..fcb3ce2 100644 --- a/src/main.ts +++ b/src/main.ts @@ -5,6 +5,65 @@ import { runAsUpgrader } from './creep/upgrader'; import { uniqId } from './utils/Identity'; import { runTower } from './tower'; +// When compiling TS to JS and bundling with rollup, the line numbers and file names in error messages change +// This utility uses source maps to get the line numbers and file names of the original, TS source code +export const loop = ErrorMapper.wrapLoop(() => { + console.log(''); + console.log(`Current game tick is ${Game.time}, ${Game.cpu.bucket}`); + + for (let room of Object.values(Game.rooms)) { + writeRoomInfo(room); + } + + // Automatically delete memory of missing creeps + for (const name in Memory.creeps) { + if (!(name in Game.creeps)) { + delete Memory.creeps[name]; + } + } + + callHarvestersFromOthers(4); + + makeCreep(CreepRole.HARVESTER, 4); + makeCreep(CreepRole.UPGRADER, 4); + makeCreep(CreepRole.BUILDER, 4); + + // Process current creeps + for (let name in Game.creeps) { + const creep = Game.creeps[name]; + if (creep.memory.role === CreepRole.HARVESTER) { + runAsHarvester(creep); + } + if (creep.memory.role === CreepRole.UPGRADER) { + runAsUpgrader(creep); + } + if (creep.memory.role === CreepRole.BUILDER) { + 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 writeRoomInfo(room: Room) { + console.log('Room:', room.name, 'energy', room.energyCapacityAvailable, room.energyAvailable); + const roads = room.find(FIND_STRUCTURES, { + filter: (s) => s.structureType === STRUCTURE_ROAD && s.hits < s.hitsMax + }); + const minRoadHits = _.min(roads.map((r) => r.hits)); + const structures = room.find(FIND_STRUCTURES, { + filter: (s) => s.structureType !== STRUCTURE_ROAD && s.hits < s.hitsMax + }); + const minStructHits = _.min(structures.map((r) => r.hits)); + console.log('Repairs:', 'roads', minRoadHits, 'structures', minStructHits); +} + function makeName(): string { return uniqId(); } @@ -59,7 +118,6 @@ function callHarvestersFromOthers(minHarvCount: number) { if (curHarvCount < minHarvCount) { const others = Object.values(Game.creeps).filter((c) => c.memory.role !== CreepRole.HARVESTER); const required = Math.min(minHarvCount - curHarvCount, others.length); - console.log('Call harvesters', required); let count = 0; for (let creep of others) { if (count >= required) { @@ -70,59 +128,3 @@ function callHarvestersFromOthers(minHarvCount: number) { } } } - -// When compiling TS to JS and bundling with rollup, the line numbers and file names in error messages change -// This utility uses source maps to get the line numbers and file names of the original, TS source code -export const loop = ErrorMapper.wrapLoop(() => { - console.log(''); - console.log(`Current game tick is ${Game.time}, ${Game.cpu.bucket}`); - - const room = _.first(Object.values(Game.rooms)); - if (room) { - console.log('Room', room.name, room.energyCapacityAvailable, room.energyAvailable); - const roads = room.find(FIND_STRUCTURES, { - filter: (s) => s.structureType === STRUCTURE_ROAD && s.hits < s.hitsMax - }); - const minRoadHits = _.min(roads.map((r) => r.hits)); - const structures = room.find(FIND_STRUCTURES, { - filter: (s) => s.structureType !== STRUCTURE_ROAD && s.hits < s.hitsMax - }); - const minStructHits = _.min(structures.map((r) => r.hits)); - console.log('Repairs', 'roads', minRoadHits, 'structs', minStructHits); - } - - // Automatically delete memory of missing creeps - for (const name in Memory.creeps) { - if (!(name in Game.creeps)) { - delete Memory.creeps[name]; - } - } - - callHarvestersFromOthers(4); - - makeCreep(CreepRole.HARVESTER, 4); - makeCreep(CreepRole.UPGRADER, 4); - makeCreep(CreepRole.BUILDER, 4); - - // Process current creeps - for (let name in Game.creeps) { - const creep = Game.creeps[name]; - if (creep.memory.role === CreepRole.HARVESTER) { - runAsHarvester(creep); - } - if (creep.memory.role === CreepRole.UPGRADER) { - runAsUpgrader(creep); - } - if (creep.memory.role === CreepRole.BUILDER) { - 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); - } - } - } -});