/*
	--------------------------------------------------------------
	Condition_Use_SE
	--------------------------------------------------------------

	Enhanced version if ObjCondition_Multi.
	
    This Condition is marked complete when a player presses the 'Use'
    key while in range of its owning actor.
	
	Enhancements:
	- Automatically skips invalid conditions (e.g. which didn't matched player counts)
	- Option to skip inactive conditions (i.e. 
	  it can be completed even if inactive conditons are incomplete)
	- Allow to enable/disable child condition on trigger
	
	

	Author :  			PooSH
	Original Author :  	Alex Quick

	--------------------------------------------------------------
*/
class ObjCondition_Multi_SE extends ObjCondition_Multi
editinlinenew;

var (ObjCondition_Multi) bool bSkipInactive;
var (ObjCondition_Multi) KFStoryGameInfo.EConditionActivationMethod ChildActivationMethod;

function ConditionTick(float DeltaTime)
{
    local int i;
    local array<KF_ObjectiveCondition>    ValidConditions;

    NumConditions = 0;

    for(i = 0 ; i < ChildConditions.length ; i ++)
    {
        if(ChildConditions[i].ConditionIsRelevant() && ChildConditions[i].ConditionIsValid() 
			&& (!bSkipInactive || ChildConditions[i].ConditionIsActive()) )
        {
            ValidConditions[ValidConditions.length] = ChildConditions[i];
        }
    }

    NumConditions = ValidConditions.length;
    CompleteConditions.length = ValidConditions.length;

    for(i = 0 ; i < ValidConditions.length ; i ++)
    {
        if(ValidConditions[i].bComplete)
        {
            CompleteConditions[i] = 1 ;
        }
        else
        {
            CompleteConditions[i] = 0;
        }
    }

    NumCompleted  = 0;
    for(i = 0 ; i < NumConditions ; i ++)
    {
        if(CompleteConditions[i] == 1)
        {
            NumCompleted ++ ;
        }
    }

    Super(KF_ObjectiveCondition).ConditionTick(DeltaTime);
}

function       float        GetCompletionPct()
{
	if ( NumCompleted >= NumConditions )
		return 1.f;
		
    return float(NumCompleted) / float(NumConditions) ;
}


function Trigger(actor Other, pawn EventInstigator)
{
	local int i;
	
	super.Trigger(Other, EventInstigator);
	
    if( ConditionIsActive() && ConditionIsValid() && ChildActivationMethod != Null )
    {
		if ( ChildActivationMethod == RandomlyActivate ) {
			warn("RandomlyActivate can not be used as ChildActivationMethod");
			return;
		}
        
		for(i = 0 ; i < ChildConditions.length ; i ++)
		{
			if( !ChildConditions[i].ConditionIsValid() )
				continue;
				
			switch(ChildActivationMethod)
			{
				case TriggerToggled :
					ChildConditions[i].SetTargetActor(InstigatorName, EventInstigator); 
					if(!ChildConditions[i].bActive)
					{
						log(name $ " Activating Child Condition By Trigger - " $ ChildConditions[i].name);
						GetObjOwner().ActivateCondition(ChildConditions[i]);
						ChildConditions[i].ConditionActivated(EventInstigator);
					}
					else
					{
						log(name $ " Deactivating Child Condition By Trigger - " $ ChildConditions[i].name);
						GetObjOwner().DeActivateCondition(ChildConditions[i]);
					}
					break;
					
				case TriggerActivates :
					if(!ChildConditions[i].bActive)
					{
						log(name $ " Activating Child Condition By Trigger - " $ ChildConditions[i].name);
						ChildConditions[i].SetTargetActor(InstigatorName, EventInstigator); 
						GetObjOwner().ActivateCondition(ChildConditions[i]);
						ChildConditions[i].ConditionActivated(EventInstigator);
					}
					break;
					
				case TriggerDeActivates :
					if(ChildConditions[i].bActive)
					{
						log(name $ " Deactivating Child Condition By Trigger - " $ ChildConditions[i].name);
						ChildConditions[i].SetTargetActor(InstigatorName, EventInstigator); 
						GetObjOwner().DeActivateCondition(ChildConditions[i]);
					}
					break;
			}		
		}
    }
}

defaultproperties
{
}
