<commit_before>import { Config } from './../config/config';
import { GameManager } from './../gameManager';
import { CreepManager } from './../components/creeps/creepManager';
import { SpawnManager } from './../components/spawns/spawnManager';
import { SourceManager } from './../components/sources/sourceManager';
import { StructureManager } from './../components/structures/structureManager';
import { ControllerManager } from './../components/controllers/controllerManager';
import { ConstructionSiteManager } from './../components/constructionSites/constructionSiteManager';

// TODO this namespace is as DRY as the ocean
export namespace MemoryManager {

  export var memory: Memory;

  export function loadMemory(): void {
    this.memory = Memory;
  }

  export function updateCreepMemory(): void {
    this.updateSharedCreepMemory();
    this.updateHarvestersMemory();
    this.updateBuildersMemory();
    this.updateRepairersMemory();
    this.updateUpgradersMemory();
  }

  /**
   * Update memory shared by many creeps (i.e. thru CreepAction superclass)
   */
  export function updateSharedCreepMemory(): void {
    _.each(Memory.creeps, (creep: Creep) => {

      if (!creep.memory.renew_station_id) {
        creep.memory.renew_station_id = SpawnManager.getFirstSpawn();
      }

    });
  }


  /**
   * Clean up creep memory. Delete any creeps in memory that no longer exist in
   * the game.
   */
  export function cleanupCreepMemory(): void {
    // refactor: brought in from gameManager
    // clean up memory for deleted creeps
    for (var name in Memory.creeps) {
      if (!Game.creeps[name]) {
        if (Config.VERBOSE) {
          console.log('[MemoryManager] Clearing non-existing creep memory:', name);
        }
        delete Memory.creeps[name];
      }
    }
  }


  /**
   * Update each harvester's target source and/or energy dropoff
   * TODO this is a hack and needs to be un-hacked
   */
  export function updateHarvestersMemory(): void {

    // pull harvesters from GameManager
    _.each(CreepManager.harvesters, (creep: Creep) => {

      // make sure the harvester's target source still exists
      if (!creep.memory.target_source_id || Game.getObjectById(creep.memory.target_source_id) == null) {
        if (Config.VERBOSE)
          console.log('[MemoryManager] Updating outdated source ID for ' + creep.name);

        creep.memory.target_source_id = SourceManager.getFirstSource().id;
      }

      // make sure the harvester's energy dropoff still exists
      if (!creep.memory.target_energy_dropoff_id || Game.getObjectById(creep.memory.target_energy_dropoff_id) == null) {
        if (Config.VERBOSE)
          console.log('[MemoryManager] Updating outdated energy dropoff ID for ' + creep.name);

        creep.memory.target_energy_dropoff_id = StructureManager.getStorageObject().id;
      }

    });
  }


  /**
   * Update each builder's target site and refill point
   * TODO spread out builders. all just default to spawn always, never actually
   * target a container
   */
  export function updateBuildersMemory(): void {

    _.each(CreepManager.builders, (creep: Creep) => {

      // make sure the builder's target construction site still exists
      if (!creep.memory.target_construction_site_id || Game.getObjectById(creep.memory.target_construction_site_id) == null) {
        if (Config.VERBOSE)
          console.log('[MemoryManager] Updating outdated construction site ID for ' + creep.name);

        creep.memory.target_construction_site_id = ConstructionSiteManager.getFirstConstructionSite().id;
      }

      // make sure the builder's target energy station exists
      if (!creep.memory.target_energy_station_id || Game.getObjectById(creep.memory.target_energy_station_id) == null) {
        if (Config.VERBOSE)
          console.log('[MemoryManager] Updating outdated energy station ID for ' + creep.name);

        creep.memory.target_energy_station_id = SpawnManager.getFirstSpawn();
      }

    });

  }


  export function updateRepairersMemory(): void {

    _.each(CreepManager.repairers, (creep: Creep) => {

      // target structure ID exists?
      if (!creep.memory.target_repair_site_id || Game.getObjectById(creep.memory.target_repair_site_id) == null) {
        if (Config.VERBOSE)
          console.log('[MemoryManager] Updating outdated target repair site ID for ' + creep.name);

        // TODO find new target repair site
      }

      // energy station ID exists?
      if (!creep.memory.target_energy_station_id || Game.getObjectById(creep.memory.target_energy_station_id) == null) {
        if (Config.VERBOSE)
          console.log('[MemoryManager] Updating outdated target energy station ID for ' + creep.name);

        creep.memory.target_energy_station_id = SpawnManager.getFirstSpawn();
      }

    });

  }


  export function updateUpgradersMemory(): void {

    _.each(CreepManager.upgraders, (creep: Creep) => {

      if (!creep.memory.target_controller_id || !Game.getObjectById(creep.memory.target_controller_id)) {
        if (Config.VERBOSE)
          console.log('[MemoryManager] Updating outdated controller ID for ' + creep.name);

        creep.memory.target_controller_id = ControllerManager.getController();
      }

      if (!creep.memory.target_energy_station_id || !Game.getObjectById(creep.memory.target_energy_station_id)) {
        if (Config.VERBOSE)
          console.log('[MemoryManager] Updating outdated target energy station ID for ' + creep.name);

        creep.memory.target_energy_station_id = SpawnManager.getFirstSpawn();
      }

    });

  }
}<commit_msg>[MemoryManager] don't use `this`<commit_after>import { Config } from './../config/config';
import { GameManager } from './../gameManager';
import { CreepManager } from './../components/creeps/creepManager';
import { SpawnManager } from './../components/spawns/spawnManager';
import { SourceManager } from './../components/sources/sourceManager';
import { StructureManager } from './../components/structures/structureManager';
import { ControllerManager } from './../components/controllers/controllerManager';
import { ConstructionSiteManager } from './../components/constructionSites/constructionSiteManager';

// TODO this namespace is as DRY as the ocean
export namespace MemoryManager {

  export var memory: Memory;

  export function loadMemory(): void {
    this.memory = Memory;
  }

  /**
   * Clean up creep memory. Delete any creeps in memory that no longer exist in
   * the game.
   */
  export function cleanupCreepMemory(): void {
    // refactor: brought in from gameManager
    // clean up memory for deleted creeps
    for (var name in Memory.creeps) {
      if (!Game.creeps[name]) {
        if (Config.VERBOSE) {
          console.log('[MemoryManager] Clearing non-existing creep memory:', name);
        }
        delete Memory.creeps[name];
      }
    }
  }

  export function updateCreepMemory(): void {
    updateSharedCreepMemory();
    updateHarvestersMemory();
    updateBuildersMemory();
    updateRepairersMemory();
    updateUpgradersMemory();
  }

  /**
   * Update memory shared by many creeps (i.e. thru CreepAction superclass)
   */
  function updateSharedCreepMemory(): void {
    _.each(Memory.creeps, (creep: Creep) => {

      if (!creep.memory.renew_station_id) {
        creep.memory.renew_station_id = SpawnManager.getFirstSpawn();
      }

    });
  }


  /**
   * Update each harvester's target source and/or energy dropoff
   * TODO this is a hack and needs to be un-hacked
   */
  function updateHarvestersMemory(): void {

    // pull harvesters from GameManager
    _.each(CreepManager.harvesters, (creep: Creep) => {

      // make sure the harvester's target source still exists
      if (!creep.memory.target_source_id || Game.getObjectById(creep.memory.target_source_id) == null) {
        if (Config.VERBOSE)
          console.log('[MemoryManager] Updating outdated source ID for ' + creep.name);

        creep.memory.target_source_id = SourceManager.getFirstSource().id;
      }

      // make sure the harvester's energy dropoff still exists
      if (!creep.memory.target_energy_dropoff_id || Game.getObjectById(creep.memory.target_energy_dropoff_id) == null) {
        if (Config.VERBOSE)
          console.log('[MemoryManager] Updating outdated energy dropoff ID for ' + creep.name);

        creep.memory.target_energy_dropoff_id = StructureManager.getStorageObject().id;
      }

    });
  }


  /**
   * Update each builder's target site and refill point
   * TODO spread out builders. all just default to spawn always, never actually
   * target a container
   */
  function updateBuildersMemory(): void {

    _.each(CreepManager.builders, (creep: Creep) => {

      // make sure the builder's target construction site still exists
      if (!creep.memory.target_construction_site_id || Game.getObjectById(creep.memory.target_construction_site_id) == null) {
        if (Config.VERBOSE)
          console.log('[MemoryManager] Updating outdated construction site ID for ' + creep.name);

        creep.memory.target_construction_site_id = ConstructionSiteManager.getFirstConstructionSite().id;
      }

      // make sure the builder's target energy station exists
      if (!creep.memory.target_energy_station_id || Game.getObjectById(creep.memory.target_energy_station_id) == null) {
        if (Config.VERBOSE)
          console.log('[MemoryManager] Updating outdated energy station ID for ' + creep.name);

        creep.memory.target_energy_station_id = SpawnManager.getFirstSpawn();
      }

    });

  }


  function updateRepairersMemory(): void {

    _.each(CreepManager.repairers, (creep: Creep) => {

      // target structure ID exists?
      if (!creep.memory.target_repair_site_id || Game.getObjectById(creep.memory.target_repair_site_id) == null) {
        if (Config.VERBOSE)
          console.log('[MemoryManager] Updating outdated target repair site ID for ' + creep.name);

        // TODO find new target repair site
      }

      // energy station ID exists?
      if (!creep.memory.target_energy_station_id || Game.getObjectById(creep.memory.target_energy_station_id) == null) {
        if (Config.VERBOSE)
          console.log('[MemoryManager] Updating outdated target energy station ID for ' + creep.name);

        creep.memory.target_energy_station_id = SpawnManager.getFirstSpawn();
      }

    });

  }


  function updateUpgradersMemory(): void {

    _.each(CreepManager.upgraders, (creep: Creep) => {

      if (!creep.memory.target_controller_id || !Game.getObjectById(creep.memory.target_controller_id)) {
        if (Config.VERBOSE)
          console.log('[MemoryManager] Updating outdated controller ID for ' + creep.name);

        creep.memory.target_controller_id = ControllerManager.getController();
      }

      if (!creep.memory.target_energy_station_id || !Game.getObjectById(creep.memory.target_energy_station_id)) {
        if (Config.VERBOSE)
          console.log('[MemoryManager] Updating outdated target energy station ID for ' + creep.name);

        creep.memory.target_energy_station_id = SpawnManager.getFirstSpawn();
      }

    });

  }
}
{'fraction_non_alphanumeric': 0.1121461841633823, 'fraction_numerical': 0.0, 'mean_word_length': 3.2021076401957096, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2945894', 'n_tokens_mistral': 3311, 'n_tokens_neox': 3180, 'n_words': 752}