Skip to content

Commit

Permalink
add safety tracking to RoomIntel; roadPlanner, queen tweaks; hatchery…
Browse files Browse the repository at this point in the history
….overload bugfix
  • Loading branch information
bencbartlett committed Feb 1, 2019
1 parent 8ed9439 commit 0df04d9
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 11 deletions.
8 changes: 8 additions & 0 deletions src/declarations/memory.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ interface Memory {
// suspend?: number;
resetBucket?: boolean;
haltTick?: number;
combatPlanner: any;
}

interface StatsMemory {
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion src/hiveClusters/hatchery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/intel/CombatIntel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -69,6 +69,9 @@ export class CombatIntel {
}
}
return expectedDamage;
} else {
log.warning(`CombatIntel.towerDamageAtPos: room visibility at ${pos.print}!`);
return 0;
}
}

Expand Down Expand Up @@ -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!`);
Expand Down
34 changes: 33 additions & 1 deletion src/intel/RoomIntel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
5 changes: 0 additions & 5 deletions src/overlords/SwarmOverlord.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/overlords/core/queen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 5 additions & 1 deletion src/roomPlanner/RoadPlanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)}`);
}
Expand Down

0 comments on commit 0df04d9

Please sign in to comment.