import { Process } from '../../os/process' ; export class HarvestProcess extends Process { metaData : MetaData [ AOS_HARVEST_PROCESS ] ; type = AOS_HARVEST_PROCESS ; run ( ) { let creep = Game . creeps [ this . metaData . creep ] ; if ( ! creep || _ . sum ( creep . carry ) === creep . carryCapacity ) { this . completed = true ; this . resumeParent ( ) ; return ; } let targetPos ; let targetRange ; let sourcePos = this . kernel . memory . posCache . get ( this . metaData . source ) ; if ( ! sourcePos ) { return ; } let colony = this . kernel . memory . empire . getColony ( sourcePos . roomName ) ; if ( colony ) { let sourceContainer = colony . sourceContainer ( this . metaData . source ) ; if ( sourceContainer === false ) { targetPos = sourcePos ; targetRange = 1 ; } else if ( typeof sourceContainer === 'string' ) { let containerPos = this . kernel . memory . posCache . get ( sourceContainer ) ; if ( containerPos ) { targetPos = containerPos ; targetRange = 0 ; } else { targetPos = sourcePos ; targetRange = 1 ; } } else { this . kernel . memory . posCache . set ( sourceContainer ) ; targetPos = sourceContainer . pos ; targetRange = 0 ; } } else { targetPos = sourcePos ; targetRange = 1 ; } if ( ! creep . pos . inRangeTo ( targetPos , targetRange ) ) { this . fork ( AOS_MOVE_PROCESS , creep . name + '-harvest-move' , this . priority + 1 , { creep : creep . name , pos : { x : targetPos . x , y : targetPos . y , roomName : targetPos . roomName } , range : targetRange } ) ; } else { let source = Game . getObjectById < Source > ( this . metaData . source ) ; if ( creep . harvest ( source ! ) === ERR_NOT_ENOUGH_RESOURCES ) { this . suspend = source ! . ticksToRegeneration ; } } } }