From 0df04d9203658744aa0d65d98141a3ac2f67146b Mon Sep 17 00:00:00 2001 From: Ben Bartlett Date: Thu, 31 Jan 2019 17:15:02 -0800 Subject: [PATCH] add safety tracking to RoomIntel; roadPlanner, queen tweaks; hatchery.overload bugfix --- src/declarations/memory.d.ts | 8 ++++++++ src/hiveClusters/hatchery.ts | 4 +++- src/intel/CombatIntel.ts | 7 +++++-- src/intel/RoomIntel.ts | 34 +++++++++++++++++++++++++++++++++- src/overlords/SwarmOverlord.ts | 5 ----- src/overlords/core/queen.ts | 2 +- src/roomPlanner/RoadPlanner.ts | 6 +++++- 7 files changed, 55 insertions(+), 11 deletions(-) diff --git a/src/declarations/memory.d.ts b/src/declarations/memory.d.ts index 2da3fe74d..291559dee 100644 --- a/src/declarations/memory.d.ts +++ b/src/declarations/memory.d.ts @@ -28,6 +28,7 @@ interface Memory { // suspend?: number; resetBucket?: boolean; haltTick?: number; + combatPlanner: any; } interface StatsMemory { @@ -197,6 +198,13 @@ interface RoomMemory { tick: number; } } + safety?: { + safeFor: number; + unsafeFor: number; + safety1k: number; + safety10k: number; + tick: number; + } prevPositions?: { [creepID: string]: protoPos }; creepsInRoom?: { [tick: number]: string[] }; tick?: number; diff --git a/src/hiveClusters/hatchery.ts b/src/hiveClusters/hatchery.ts index d1fb6499e..fa1b8dab8 100644 --- a/src/hiveClusters/hatchery.ts +++ b/src/hiveClusters/hatchery.ts @@ -358,9 +358,11 @@ export class Hatchery extends HiveCluster { // Spawn all queued creeps that you can while (this.availableSpawns.length > 0) { let result = this.spawnHighestPriorityCreep(); + if (result == ERR_NOT_ENOUGH_ENERGY) { // if you can't spawn something you want to + this.isOverloaded = true; + } if (result != OK && result != ERR_SPECIFIED_SPAWN_BUSY) { // Can't spawn creep right now - this.isOverloaded = true; break; } } diff --git a/src/intel/CombatIntel.ts b/src/intel/CombatIntel.ts index bd60cdbcf..4e3b4df4e 100644 --- a/src/intel/CombatIntel.ts +++ b/src/intel/CombatIntel.ts @@ -60,7 +60,7 @@ export class CombatIntel { } /* Total tower tamage from all towers in room at a given position */ - static towerDamageAtPos(pos: RoomPosition, ignoreEnergy = false): number | undefined { + static towerDamageAtPos(pos: RoomPosition, ignoreEnergy = false): number { if (pos.room) { let expectedDamage = 0; for (let tower of pos.room.towers) { @@ -69,6 +69,9 @@ export class CombatIntel { } } return expectedDamage; + } else { + log.warning(`CombatIntel.towerDamageAtPos: room visibility at ${pos.print}!`); + return 0; } } @@ -532,7 +535,7 @@ export class CombatIntel { return currentRange > previousRange; } - // This method is expensive; use sparingly + // This method is probably expensive; use sparingly static isEdgeDancing(creep: Creep, reentryThreshold = 3): boolean { if (!creep.room.my) { log.warning(`isEdgeDancing should only be called in owned rooms!`); diff --git a/src/intel/RoomIntel.ts b/src/intel/RoomIntel.ts index 3905a8033..8f144cf3a 100644 --- a/src/intel/RoomIntel.ts +++ b/src/intel/RoomIntel.ts @@ -195,6 +195,35 @@ export class RoomIntel { room.memory.creepsInRoom[Game.time] = _.map(room.hostiles, creep => creep.name); } + private static recordSafety(room: Room): void { + if (!room.memory.safety) { + room.memory.safety = { + safeFor : 0, + unsafeFor: 0, + safety1k : 1, + safety10k: 1, + tick : Game.time + }; + } + let safety: number; + if (room.dangerousHostiles.length > 0) { + room.memory.safety.safeFor = 0; + room.memory.safety.unsafeFor += 1; + safety = 0; + } else { + room.memory.safety.safeFor += 1; + room.memory.safety.unsafeFor = 0; + safety = 1; + } + // Compute rolling averages + let dTime = Game.time - room.memory.safety.tick; + room.memory.safety.safety1k = irregularExponentialMovingAverage( + safety, room.memory.safety.safety1k, dTime, 1000); + room.memory.safety.safety10k = irregularExponentialMovingAverage( + safety, room.memory.safety.safety10k, dTime, 10000); + room.memory.safety.tick = Game.time; + } + static isInvasionLikely(room: Room): boolean { const data = room.memory.invasionData; if (!data) return false; @@ -238,11 +267,14 @@ export class RoomIntel { return 0; } + static run(): void { let alreadyComputedScore = false; for (let name in Game.rooms) { - const room = Game.rooms[name]; + const room: Room = Game.rooms[name]; + + this.recordSafety(room); // Track invasion data, harvesting, and casualties for all colony rooms and outposts if (Overmind.colonyMap[room.name]) { // if it is an owned or outpost room diff --git a/src/overlords/SwarmOverlord.ts b/src/overlords/SwarmOverlord.ts index db08fd0f3..b59804e94 100644 --- a/src/overlords/SwarmOverlord.ts +++ b/src/overlords/SwarmOverlord.ts @@ -1,7 +1,6 @@ import {CombatOverlord} from './CombatOverlord'; import {Swarm} from '../zerg/Swarm'; import {CreepSetup} from '../creepSetups/CreepSetup'; -import {Directive} from '../directives/Directive'; import {profile} from '../profiler/decorator'; @profile @@ -10,10 +9,6 @@ export abstract class SwarmOverlord extends CombatOverlord { memory: any; // swarm overlords must have a memory property swarms: { [ref: string]: Swarm }; - constructor(directive: Directive, name: string, priority: number, requiredRCL: number) { - super(directive, name, priority, requiredRCL); - } - /* Wishlist of creeps to simplify spawning logic; includes automatic reporting */ // TODO: at the moment, this assumes that every swarm within an overlord is the same configuration diff --git a/src/overlords/core/queen.ts b/src/overlords/core/queen.ts index 7f96c6d50..e80c3eb5b 100644 --- a/src/overlords/core/queen.ts +++ b/src/overlords/core/queen.ts @@ -26,7 +26,7 @@ export class QueenOverlord extends Overlord { constructor(hatchery: Hatchery, priority = OverlordPriority.core.queen) { super(hatchery, 'supply', priority); this.hatchery = hatchery; - this.queenSetup = this.hatchery.battery || this.colony.storage ? Setups.queens.default : Setups.queens.early; + this.queenSetup = this.colony.storage ? Setups.queens.default : Setups.queens.early; this.queens = this.zerg(Roles.queen); this.settings = { refillTowersBelow: 500, diff --git a/src/roomPlanner/RoadPlanner.ts b/src/roomPlanner/RoadPlanner.ts index 7be31353b..b291c6331 100644 --- a/src/roomPlanner/RoadPlanner.ts +++ b/src/roomPlanner/RoadPlanner.ts @@ -72,10 +72,14 @@ export class RoadPlanner { log.debug(`Recomputing road coverage from ${storagePos.print} to ${destination.print}...`); let roadCoverage = this.computeRoadCoverage(storagePos, destination); if (roadCoverage != undefined) { + // Set expiration to be longer if road is nearly complete + let expiration = roadCoverage.roadCount / roadCoverage.length >= 0.75 + ? getCacheExpiration(RoadPlanner.settings.recomputeCoverageInterval) + : getCacheExpiration(3 * RoadPlanner.settings.recomputeCoverageInterval); this.memory.roadCoverages[destName] = { roadCount: roadCoverage.roadCount, length : roadCoverage.length, - exp : getCacheExpiration(RoadPlanner.settings.recomputeCoverageInterval) + exp : expiration }; log.debug(`Coverage: ${JSON.stringify(roadCoverage)}`); }