Refactoring

This commit is contained in:
Anton Vakhrushev 2020-07-23 18:46:55 +03:00
parent 29f653f77a
commit 9dac06627f

View File

@ -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);
}
}
}
});