/*
	--------------------------------------------------------------
	ObjCondition_InventoryInstigator
	--------------------------------------------------------------

    A Condition which is marked complete when instigator is 
	carrying an inventory item of the specified class.

	Author :  			PooSH

	--------------------------------------------------------------
*/
class ObjCondition_Inventory_SE extends ObjCondition_Inventory
editinlinenew;

/* Array of pawns which are currently holding this item in iventory */
var protected array<Pawn>             PawnInstigators;

/**
	EItemDestoyMode			Allows to auto-destroy the item in inventory. 
							Item will be destroyer only from Instigator's inventory.
							If multiple paws have the same inventory, and you want to destroy
							all of them, use ACTION_DropInventory instead
	
	IDM_DontDestroy			Default setting. Inventory will not be auto-destroyed.
	IDM_DestroyOnComplete	Inventory will be destroyed on completion. 
							Can be used when condition has DependentConditions, 
							e.g. bring inventory to the area
	IDM_DestroyOnTrigger	Inventory will be destroyed on trigger received.
							Can be used when this condition is in DependentConditions 
							of parent condition, and parent condition triggers the event
							on completion.
							e.g. use an actor while holding the inventory
*/	
enum	EItemDestoyMode
{
    IDM_DontDestroy,				// default setting - do not destroy inventory
    IDM_DestroyOnComplete, 			// auto destroy inventory on condition completion. 
    IDM_DestroyOnTrigger    		// destroy inventory on trigger, if condition complete
};
var(ObjCondition_Inventory) 	EItemDestoyMode ItemDestoyMode;

// call DependentConditions[].FindInstigator()?
var(KF_ObjectiveCondition)	bool					bCheckDependentsInstigator; 


function Trigger(actor Other, pawn EventInstigator)
{
	super.Trigger(Other, EventInstigator); 
	
	if ( ItemDestoyMode == IDM_DestroyOnTrigger && ConditionIsValid() 
			&& DestroyInventoryItem(EventInstigator) ) 
		ConditionTick(0);
}

function Reset()
{
    Super.Reset();
	PawnInstigators.length = 0;
}


function bool DestroyInventoryItem(Pawn Holder)
{
	local Inventory DelItem;
	
	DelItem = FindDesiredItem(Holder);
	if (DelItem == none)
		return false;
		
	if ( KFHumanPawn_Story(Holder) != none)
		KFHumanPawn_Story(Holder).SetHasStoryItem( false );

	if(KF_StoryInventoryItem(DelItem) != none)
		KF_StoryInventoryItem(DelItem).UpdateHeldMaterial(Holder, none);

	DelItem.DetachFromPawn(Holder);
	Holder.DeleteInventory(DelItem);
	DelItem.Destroy();	
	
	return true;
}

function array<Pawn> GetInstigatorList()
{
	return PawnInstigators;
}

function ConditionTick(float DeltaTime)
{
    local Controller C;
	local Pawn MyInstigator;
	local bool bInstigatorFound;

    PawnInstigators.length = 0;
	MyInstigator = GetInstigator();
	
    for ( C=GetObjOwner().Level.ControllerList; C!=None; C=C.NextController ) {
	    if(PlayerController(C) != none && C.Pawn != none && FindDesiredItem(C.Pawn) != none ) {
			PawnInstigators[PawnInstigators.length] = C.Pawn;
			bInstigatorFound = bInstigatorFound || MyInstigator == C.Pawn;
	    }
	}
	bHeld = PawnInstigators.length > 0;
	if ( bHeld && !bInstigatorFound ) {
		MyInstigator = PawnInstigators[0];
		SetTargetActor(InstigatorName, MyInstigator);
	}
	
    Super(KF_ObjectiveCondition).ConditionTick(DeltaTime);
	
	if ( bComplete && ItemDestoyMode == IDM_DestroyOnComplete ) 
		DestroyInventoryItem(GetInstigator());
}



function Inventory FindDesiredItem(Pawn Holder)
{
	local Inventory Inv;
	
	if ( Holder == none )
		return none;
	
	for( Inv = Holder.Inventory; Inv != None ; Inv = Inv.Inventory )	{
		if( ClassIsChildOf(Inv.class,DesiredItemClass) 
				&& (DesiredItemTag == '' ||	Inv.tag == DesiredItemTag) )
			return Inv;
	}
	return none;
}

/* Overriden to skip inactive dependencies  -- PooSH */
function    bool AllowCompletion()
{
    local int i;
	local array<Pawn> MyInstigators;

	if ( DependentConditions.length > 0 ) {
		MyInstigators = GetInstigatorList();
		for(i = 0 ; i < DependentConditions.length ; i ++) {
			if ( DependentConditions[i].ConditionIsActive() ) {
				if( !DependentConditions[i].bComplete 
					|| (bCheckDependentsInstigator && !DependentConditions[i].FindInstigator(MyInstigators)) ) 
				{
					return false;
				}
			}
		}
	}
    return true;
}

defaultproperties
{
     bCheckDependentsInstigator=True
}
