import { Kernel } from './kernel' ; import { ColonyProcess } from 'processTypes/colony/colony' ; /** Main Process Class */ export class Process { /** Has the process been run this tick? */ ticked = false ; /** Is the process complete? If it is it will not be passed to the next tick */ completed = false ; /** The processes priority. The higher the priority the more likely it will be run this tick. */ priority : number ; /** The name of the process. Should be unique */ name : string ; /** An object of meta data. Should be serializeable into JSON */ metaData : any ; /** Process type, used to re-infalte on the next tick */ type : string ; /** The kernel that started this process. */ kernel : Kernel ; /**
    Should the process be suspended?
  
    If `false` the process will be run.
    If `true` the process is suspending until something manually un suspends it.
    If a number the suspend counter will be reduced until it reaches 0 and then will be set back to false.
    If a string this process is suspended untill the named process is finished.
    */ suspend : string | number | boolean = false ; /** The Processes Parent Process */ parent : Process | undefined ; /** Messages */ messages : { [ name ] : any ; } = { } ; /** Creates a new Process from the entry supplied */ constructor ( entry , kernel ) { this . priority = entry . priority ; this . name = entry . name ; this . metaData = entry . metaData ; this . kernel = kernel ; this . suspend = entry . suspend ; if ( entry . parent ) { this . parent = this . kernel . processTable [ entry . parent ] ; } } /** Run the process */ run ( kernel ) { console . log ( 'Process ' + this . name + ' did not have a type.' ) ; this . completed = true ; kernel . noop ( ) ; } /** Serialize this process */ serialize ( ) { let parent ; if ( this . parent ) { parent = this . parent . name ; } return < SerializedProcess > { priority : this . priority , name : this . name , metaData : this . metaData , type : this . type , suspend : this . suspend , parent : parent } ; } /** Create a new process on the kernel with this process as its parent and suspend the current process until it completes */ fork < T extends ProcessTypes > ( processType , name , priority , meta ) { this . kernel . addProcess ( processType , name , priority , meta , this . name ) ; this . suspend = name ; } /** Send the process a message */ sendMessage ( name , data ) { this . messages [ name ] = data ; } /** Resume the process */ resume ( thisTick = false ) { this . suspend = false ; if ( thisTick ) { this . ticked = false ; } } /** Resume the parent if the process has a parent */ resumeParent ( thisTick = false ) { if ( this . parent ) { this . parent . resume ( thisTick ) ; } } /** Returns the room Data */ roomData ( ) { return this . kernel . data . roomData [ this . metaData . roomName ] ; } /** Returns the room instance */ room ( ) { return Game . rooms [ this . metaData . roomName ] ; } /** Returns the flag for this process */ flag ( ) { return Game . flags [ this . metaData . flag ] ; } /** Use the Kernels Logger */ log ( message ) { this . kernel . log ( this , message ) ; } /** Get IPC messages for this process */ getIpc ( ) { return this . kernel . getIpc ( this . name ) ; } } export class LifetimeProcess extends Process { metaData : CreepMetaData ; /** Returns the creep if it is alive, or completes the process */ getCreep ( ) { if ( Game . creeps [ this . metaData . creep ] ) { return Game . creeps [ this . metaData . creep ] ; } else { this . completed = true ; return false ; } } } export class ColonySubProcess extends Process { metaData : ColonySubProcessMetaData ; _colonyProcess : ColonyProcess ; /** Returns the current colony process */ colonyProcess ( ) { if ( ! this . _colonyProcess ) { let proc = this . kernel . getProcess ( AOS_COLONY_PROCESS , this . metaData . colonyProcessName ) ; if ( proc ) { this . _colonyProcess = < ColonyProcess > proc ; } } return this . _colonyProcess ; } }