import { isRoomVisible } from "@open-screeps/is-room-visible" ; import { isObjectVisible } from "@open-screeps/is-object-visible" ; import { Empire } from "./empire" ; import { Bunker } from '../lib/classes/bunker' ; import { POSCache } from './pos-cache' ; /**
 * The colony class. Contains functions and data for the colony.
 */ export class Colony { coreRoom : Room ; empire : Empire ; rooms : Room [ ] ; sources : { [ id ] : Source ; } ; bunker : Bunker ; spawnCache : StructureSpawn [ ] ; posCache : POSCache ; constructor ( coreRoomName , empire ) { this . empire = empire ; this . posCache = new POSCache ( ) ; if ( isRoomVisible ( coreRoomName ) ) { this . coreRoom = Game . rooms [ coreRoomName ] ; this . rooms = [ this . coreRoom ] ; this . sources = { } ; this . buildData ( ) ; this . buildBunker ( ) ; } else { console . log ( '[Colony Constructor] room ' + coreRoomName + ' does not exist' ) ; } } buildBunker ( ) { if ( ! Memory . arcos . colonies [ this . coreRoom . name ] . bunkerBase ) { if ( Memory . bunkers [ this . coreRoom . name ] . bunkerBase ) { Memory . arcos . colonies [ this . coreRoom . name ] . bunkerBase = Memory . bunkers [ this . coreRoom . name ] . bunkerBase ; } } else { this . bunker = new Bunker ( new RoomPosition ( Memory . arcos . colonies [ this . coreRoom . name ] . bunkerBase ! . x , Memory . arcos . colonies [ this . coreRoom . name ] . bunkerBase ! . y , Memory . arcos . colonies [ this . coreRoom . name ] . bunkerBase ! . roomName ) ) ; } } buildData ( ) { if ( ! Memory . arcos . colonies [ this . coreRoom . name ] ) { Memory . arcos . colonies [ this . coreRoom . name ] = { } ; } if ( ! Memory . arcos . colonies [ this . coreRoom . name ] . sourceContainers ) { Memory . arcos . colonies [ this . coreRoom . name ] . sourceContainers = { } ; } if ( ! Memory . arcos . colonies [ this . coreRoom . name ] . sourceLinks ) { Memory . arcos . colonies [ this . coreRoom . name ] . sourceLinks = { } ; } this . inflateRooms ( ) ; this . inflateSources ( ) ; } /**
     * Inflate the room list from memory
     */ inflateRooms ( ) { if ( ! Memory . arcos . colonies [ this . coreRoom . name ] . rooms ) { Memory . arcos . colonies [ this . coreRoom . name ] . rooms = [ this . coreRoom . name ] ; } Memory . arcos . colonies [ this . coreRoom . name ] . rooms ! . forEach ( ( roomName ) => { if ( isRoomVisible ( roomName ) ) { this . rooms . push ( Game . rooms [ roomName ] ) ; } } ) ; } /**
     * Run the supplied function in each room.
     */ inEachRoom ( cb ) { this . rooms . forEach ( cb ) ; } param id source ( id ) { if ( this . sources [ id ] ) { return this . sources [ id ] ; } this . findSources ( ) ; if ( this . sources [ id ] ) { return this . sources [ id ] ; } return false ; } /**
     * Find all the sources in all the rooms in the colony
     */ findSources ( ) { this . inEachRoom ( ( room ) => { let sources = room . find ( FIND_SOURCES ) ; sources . forEach ( ( source ) => { this . sources [ source . id ] = source ; this . posCache . set ( source ) ; if ( ! _ . includes ( Memory . arcos . colonies [ this . coreRoom . name ] . sources ! , source . id ) ) { Memory . arcos . colonies [ this . coreRoom . name ] . sources ! . push ( source . id ) ; } } ) ; } ) ; } /**
     * Turns the source IDs into `Source`s in the `Colony` object.
     */ inflateSources ( ) { if ( ! Memory . arcos . colonies [ this . coreRoom . name ] . sources ) { Memory . arcos . colonies [ this . coreRoom . name ] . sources = [ ] ; } Memory . arcos . colonies [ this . coreRoom . name ] . sources ! . forEach ( ( sourceId ) => { let source = Game . getObjectById < Source > ( sourceId ) ; if ( source ) { this . posCache . set ( source ) ; this . sources [ sourceId ] = source ; } } ) ; } param sourceId sourceContainer ( sourceId ) { if ( Memory . arcos . colonies [ this . coreRoom . name ] . sourceContainers ! [ sourceId ] ) { if ( isObjectVisible ( Memory . arcos . colonies [ this . coreRoom . name ] . sourceContainers ! [ sourceId ] ) ) { let container = Game . getObjectById < StructureContainer > ( Memory . arcos . colonies [ this . coreRoom . name ] . sourceContainers ! [ sourceId ] ) ; if ( container !== null ) { this . posCache . set ( container ) ; return container ; } } else { if ( isObjectVisible ( sourceId ) ) { delete Memory . arcos . colonies [ this . coreRoom . name ] . sourceContainers ! [ sourceId ] ; } else { return Memory . arcos . colonies [ this . coreRoom . name ] . sourceContainers ! [ sourceId ] ; } } } let source = this . source ( sourceId ) ; if ( source ) { let containers = source . room . find ( FIND_STRUCTURES , { filter : ( structure ) => { return structure . structureType === STRUCTURE_CONTAINER ; } } ) ; containers . forEach ( ( container ) => { let sources = container . pos . findInRange ( FIND_SOURCES , 1 ) ; if ( sources . length > 0 ) { Memory . arcos . colonies [ this . coreRoom . name ] . sourceContainers ! [ sources [ 0 ] . id ] = container . id ; } } ) ; } if ( Memory . arcos . colonies [ this . coreRoom . name ] . sourceContainers ! [ sourceId ] ) { let container = Game . getObjectById < StructureContainer > ( Memory . arcos . colonies [ this . coreRoom . name ] . sourceContainers ! [ sourceId ] ) ; if ( container ) { this . posCache . set ( container ) ; return container ; } } return false ; } /**
     * Find the core link for this colony
     */ coreLink ( ) { if ( this . coreRoom . storage ) { let links = this . coreRoom . storage . pos . findInRange ( FIND_STRUCTURES , 2 , { filter : ( structure ) => { return structure . structureType === STRUCTURE_LINK ; } } ) ; if ( links [ 0 ] ) { return < StructureLink > links [ 0 ] ; } } return false ; } param sourceId sourceLink ( sourceId ) { if ( Memory . arcos . colonies [ this . coreRoom . name ] . sourceLinks ! [ sourceId ] ) { if ( isObjectVisible ( Memory . arcos . colonies [ this . coreRoom . name ] . sourceLinks ! [ sourceId ] ) ) { let link = Game . getObjectById < StructureLink > ( Memory . arcos . colonies [ this . coreRoom . name ] . sourceLinks ! [ sourceId ] ) ; if ( link !== null ) { return link ; } } else { if ( isObjectVisible ( sourceId ) ) { delete Memory . arcos . colonies [ this . coreRoom . name ] . sourceLinks ! [ sourceId ] ; } else { return true ; } } } let sourceContainer = this . sourceContainer ( sourceId ) ; if ( sourceContainer === false || typeof sourceContainer === 'string' ) { return false ; } let links = sourceContainer . pos . findInRange ( FIND_STRUCTURES , 1 , { filter : ( structure ) => { return structure . structureType === STRUCTURE_LINK ; } } ) ; if ( links [ 0 ] ) { Memory . arcos . colonies [ this . coreRoom . name ] . sourceLinks ! [ sourceId ] = links [ 0 ] . id ; return < StructureLink > links [ 0 ] ; } return false ; } param roomName hasRoom ( roomName ) { return _ . includes ( Memory . arcos . colonies [ this . coreRoom . name ] . rooms ! , roomName ) ; } param roomName addRoom ( roomName ) { if ( ! _ . includes ( Memory . arcos . colonies [ this . coreRoom . name ] . rooms ! , roomName ) ) { Memory . arcos . colonies [ this . coreRoom . name ] . rooms ! . push ( roomName ) ; } if ( isRoomVisible ( roomName ) ) { this . rooms . push ( Game . rooms [ roomName ] ) ; } } /** Calculates the total income of this colony in a single source cycle. */ totalIncome ( ) { return Object . keys ( this . sources ) . length * SOURCE_ENERGY_CAPACITY ; } /** Required Income */ requiredIncome ( ) { let required = { 0 : 0 , 1 : ( SOURCE_ENERGY_CAPACITY * 2 ) , 2 : ( SOURCE_ENERGY_CAPACITY * 2 ) , 3 : ( SOURCE_ENERGY_CAPACITY * 3 ) , 4 : ( SOURCE_ENERGY_CAPACITY * 4 ) , 5 : ( SOURCE_ENERGY_CAPACITY * 6 ) , 6 : ( SOURCE_ENERGY_CAPACITY * 8 ) , 7 : ( SOURCE_ENERGY_CAPACITY * 10 ) , 8 : ( SOURCE_ENERGY_CAPACITY * 12 ) , } ; return required [ this . coreRoom . controller ! . level ] ; } param key param type param name param priority addToSpawnQueue ( key , type , name , priority ) { if ( ! Memory . arcos . colonies [ this . coreRoom . name ] . spawnQueue ) { Memory . arcos . colonies [ this . coreRoom . name ] . spawnQueue = [ ] ; } if ( this . checkQueue ( key ) === false ) { Memory . arcos . colonies [ this . coreRoom . name ] . spawnQueue ! . push ( { type : type , priority : priority , key : key , name : name } ) ; } } removeFromSpawnQueue ( key ) { Memory . arcos . colonies [ this . coreRoom . name ] . spawnQueue = _ . filter ( Memory . arcos . colonies [ this . coreRoom . name ] . spawnQueue ! , ( entry ) => { return entry . key !== key ; } ) ; } param key checkQueue ( key ) { if ( Memory . arcos . colonies [ this . coreRoom . name ] . spawnQueue ) { let results = _ . filter ( Memory . arcos . colonies [ this . coreRoom . name ] . spawnQueue ! , ( entry ) => { return entry . key === key ; } ) ; if ( results . length > 0 ) { return STATE_WAIT ; } let result = _ . filter ( this . spawns ( ) , ( spawn ) => { return ( spawn . memory . key === key && spawn . spawning !== null ) ; } ) [ 0 ] ; if ( result ) { return STATE_SPAWNING ; } } return false ; } /**
     * Returns the next object in the spawn queue.
     */ nextInSpawnQueue ( ) { return _ . sortBy ( Memory . arcos . colonies [ this . coreRoom . name ] . spawnQueue ! , 'priority' ) [ 0 ] ; } /**
     * Returns all the spawns in the core room.
     */ spawns ( ) { if ( this . spawnCache ) { return this . spawnCache ; } else { let spawns = this . coreRoom . find ( FIND_MY_SPAWNS ) ; this . spawnCache = spawns ; return spawns ; } } }