class FoundryWife extends KF_StoryNPC_Spawnable_SE;

#exec OBJ LOAD FILE=KF_Solider9_Trip_T.utx
#exec OBJ LOAD FILE=KF_FemaleVoiceOne.uax

var Actor OriginalScriptedFocus;
var transient float TimeTillNextAttack;

var() localized string strFocusOnUser;

simulated function PostBeginPlay()
{
	super.PostBeginPlay();
	
	ShieldStrength=200; // ShieldStrength is transient, so can't be set in defaultproperties
}

simulated function Tick(float DeltaTime)
{
    Super.Tick(DeltaTime);
	
	if ( TimeTillNextAttack > 0 ) {
		TimeTillNextAttack -= DeltaTime;
		if ( TimeTillNextAttack <= 0 )
			AttackEnemy();	
	}
}

function TakeDamage(int Damage, Pawn instigatedBy, Vector hitlocation, Vector momentum, class<DamageType> damageType, optional int HitIdx )
{
	//local class<KFWeaponDamageType> KFDamType;
	local ScriptedController SC;
	
	super.TakeDamage( Damage, instigatedBy, hitlocation, momentum, damageType, HitIdx );
	
	if ( Health <= 0 )
		return;
		
	SC = ScriptedController(Controller);
	if ( Monster(instigatedBy) != none && SC != none && ( SC.Enemy == none || frand() < 0.5) ) {
		// shoot that bitch
		SC.Enemy = instigatedBy;
		AttackEnemy();
	}
	// else if ( KFHumanPawn(instigatedBy) != none && PlayerController(instigatedBy.Controller) != none 
			// && instigatedBy.Controller.bIsPlayer ) 
	// {
		// KFDamType = KFWeaponDamageType(damageType);
		// if ( KFDamType != none ) {
			// Follow the player who slapped my the butt
			// if ( KFDamType.default.bCheckForHeadShots )
				// SC.MyPlayerController = PlayerController(instigatedBy.Controller);				
		// }
	// }
}



function RestoreScriptedFocus()
{
	local ScriptedController SC;
	
	SC = ScriptedController(Controller);
	if ( SC != none && OriginalScriptedFocus != none ) {
		SC.ScriptedFocus = OriginalScriptedFocus;
		OriginalScriptedFocus = none;
	}
}

// this is lame, but it is better than nothing
// todo: make a scripted controller, which can act like a bot and return to the script
function AttackEnemy()
{
	local ScriptedController SC;
	
	SC = ScriptedController(Controller);
	if ( SC == none )
		return;
		
	if ( SC.Enemy == none || SC.Enemy.Controller == none 
		|| SC.Enemy.bDeleteMe || SC.Enemy.Health <= 0 
		|| (!SC.CanSee(SC.Enemy) && !SC.Enemy.Controller.CanSee(self)) ) 
	{
		if ( SC.ScriptedFocus == SC.Enemy )
			RestoreScriptedFocus();	
		SC.Enemy = none;
		SC.NumShots = -1;
		return;
	}
	
	if ( SC.NumShots > 0 )
		return; // already shooting
		
	if ( Weapon == none || !Weapon.CanAttack(SC.Enemy) ) {
		SC.SwitchToBestWeapon();
	}
	
	// 20% chance to use other weapon
	// while ( Weapon != none && frand() < 0.1 )
		// Weapon = Weapon.NextWeapon(Weapon, Weapon);
		
	if ( Weapon == none || !Weapon.CanAttack(SC.Enemy) ) {
		// screw that enemy - let players to deal with it
		if ( SC.ScriptedFocus == SC.Enemy )
			RestoreScriptedFocus();
		SC.Enemy = none;
		SC.NumShots = -1;
		return;
	}

	//bIgnorePlayFiring = false;
	if ( SC.ScriptedFocus != SC.Enemy ) {
		if ( OriginalScriptedFocus == none ) {
			OriginalScriptedFocus = SC.ScriptedFocus;
		}
		SC.ScriptedFocus = SC.Enemy;
	}

	SC.bShootTarget = true;
	SC.NumShots = 1;
	// if ( SC.NumShots <= 1 ) {
		// if ( KFMeleeGun(Weapon) != none ) {
			// SC.NumShots = RandRange(1, 3);
			// SC.bShootSpray = false;
		// }
		// else {
			// SC.NumShots = RandRange(1, 10);
			// SC.bShootSpray = true;
		// }
	// }
	SC.WeaponFireAgain(0,false);
	TimeTillNextAttack = Weapon.RefireRate();
}


function UsedBy( Pawn user )
{
	local ScriptedController SC;
	
	if ( !bActive )
		return;
	
	super.UsedBy(user);
	
	SC = ScriptedController(Controller);
	if ( SC != none && SC.MyPlayerController != none && SC.MyPlayerController != user.Controller 
			&& KFHumanPawn(user) != none && PlayerController(user.Controller) != none ) 
	{
		SC.MyPlayerController = PlayerController(user.Controller);
		if ( strFocusOnUser != "" )
			user.ClientMessage( strFocusOnUser, 'CriticalEvent' );		
	}
}

defaultproperties
{
     strFocusOnUser="Now I will follow you, my darling!"
     bBleed=True
     MinBleedDamage=5.000000
     MaxBleedDamage=20.000000
     bActivateOnHealing=True
     bActivateOnHealingOnlyOnce=True
     HealthPctToActivate=0.800000
     DeathEvent="FoundryWifeDied"
     UseEvent="FoundryWifeUsed"
     bHealthBarOnlyIfVisible=True
     bUseCoverAnim=False
     TouchMessage="Press <USE> to interact"
     TouchMessageType="CriticalEvent"
     bTouchable=True
     bUsable=True
     BleedingSounds(0)=SoundGroup'KF_FemaleVoiceOne.Automatic_Commands.Auto_Dying'
     BleedingSounds(1)=SoundGroup'KF_FemaleVoiceOne.Automatic_Commands.Auto_Dying'
     BleedingSounds(2)=SoundGroup'KF_FemaleVoiceOne.SUPPORT.MEDIC'
     BleedingSounds(3)=SoundGroup'KF_FemaleVoiceOne.SUPPORT.MEDIC'
     BleedingSounds(4)=SoundGroup'KF_FemaleVoiceOne.SUPPORT.MEDIC'
     MinTimeBetweenBleedSounds=12.000000
     BleedOutInterval=5.000000
     NPCName="Jessica Aldridge"
     BaseAIThreatRating=0.000000
     bInitialFireAtWill=True
     bShowHealthBar=False
     NPCHealth=500.000000
     StartingHealthPct=0.750000
     ActivationEvent="FoundryWifeHealed"
     MaxCarryWeight=25.000000
     ShieldStrengthMax=200.000000
     bNoDefaultInventory=False
     AIScriptTag="FoundryWifeAI"
     GroundSpeed=120.000000
     HealthMax=500.000000
     Health=375
     Mesh=SkeletalMesh'KF_Soldier_Trip.Female_Soldier'
     Skins(0)=FinalBlend'KF_Solider9_Trip_T.Female_Soldier.FinalBlend_Female_Hair_fb'
     Skins(1)=Combiner'KF_Solider9_Trip_T.Female_Soldier.Female_Soldier_CMB'
}
