#using scripts\shared\spawner_shared;
#using scripts\shared\ai\zombie;
#using scripts\shared\ai\zombie_utility;
#using scripts\shared\ai\archetype_mannequin_interface;
#using scripts\shared\ai\systems\ai_interface;
#using scripts\shared\ai\systems\behavior_tree_utility;

                                                                  	                             	  	                                      
        	                                                                                                                                                	                                                                                                                      	                                                                                     	                                                                                     	                                                            	                                                                                   	                                                                                     	                                                                                               	                                                                                                 	                                                                   	                                                                   	                                                                                                                 	                                                                                                                              	                                                                                                                       	                                                   	                                                                           	                                                                                 	                                       	                             	                                             	                                  	                                                                                 	                                                                                                                                                	      
                                                                                                            	  	  	              	   	                                                                                                                                       	                                                                                                                                                            	                                                                                                                                                                
    	   	                                                                                                                                         	                                	                                                                                                                                                                                                                                                                                                                                                                    	        	     	              	    	   	               	                                              	                                                           	                               	                     	                                                                                                           	                                                                 	                                                              	                                                                                                                                             	                            	                                     	                                       	                                                                                                              	   	                  	       	                                                    	                   	                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            	                                                                                                 	                                    	                                                                                        	           	                        	                                            	                                             	                                                   	                                                             	                                                         	                                                                    	                                                                                                                                                                                                                                	                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  	    	         	                                                              	                                                          	                                   	                                   	                                                    	                                       	                                           	     	                                                                                                                                                                                                                                                      	                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         	                                                               	        	                                                                                                                              	                                                                                                                     	                               	                                                      	                          

#namespace MannequinBehavior;

function autoexec init()
{
	level.zm_variant_type_max						= [];
	level.zm_variant_type_max[ "walk" ]				= [];
	level.zm_variant_type_max[ "run" ]				= [];
	level.zm_variant_type_max[ "sprint" ] 			= [];
	level.zm_variant_type_max[ "walk" ][ "down" ]	= 14;
	level.zm_variant_type_max[ "walk" ][ "up" ] 	= 16;
	level.zm_variant_type_max[ "run" ][ "down" ]	= 13;
	level.zm_variant_type_max[ "run" ][ "up" ]		= 12;
	level.zm_variant_type_max[ "sprint" ][ "down" ]	= 7;
	level.zm_variant_type_max[ "sprint" ][ "up" ]	= 6;

	// INIT BLACKBOARD
	spawner::add_archetype_spawn_function( "mannequin", &ZombieBehavior::ArchetypeZombieBlackboardInit );
	spawner::add_archetype_spawn_function( "mannequin", &ZombieBehavior::ArchetypeZombieDeathOverrideInit );
		
	// INIT ZOMBIE ON SPAWN
	spawner::add_archetype_spawn_function( "mannequin", &zombie_utility::zombieSpawnSetup );
	spawner::add_archetype_spawn_function( "mannequin", &mannequinSpawnSetup );
			
	MannequinInterface::RegisterMannequinInterfaceAttributes();
	
	BehaviorTreeNetworkUtility::RegisterBehaviorTreeScriptAPI("mannequinCollisionService",&mannequinCollisionService);;
	BehaviorTreeNetworkUtility::RegisterBehaviorTreeScriptAPI("mannequinShouldMelee",&mannequinShouldMelee);;
}

function mannequinCollisionService( entity )
{
	if ( IsDefined( entity.enemy ) &&
		DistanceSquared( entity.origin, entity.enemy.origin ) > ( (300) * (300) ) )
	{
		// Allow clipping with other AI's at a distance, this helps when the AI's move diagonally into each other.
		entity PushActors( false );
	}
	else
	{
		// Force AI's to push each other close to their enemy.
		entity PushActors( true );
	}
}

function mannequinSpawnSetup( entity )
{
}




function private mannequinShouldMelee( entity )
{
	if( !IsDefined( entity.enemy ) )
    {
		return false;
	}

	if( IsDefined( entity.marked_for_death ) )
	{
		return false;
	}

	if( ( isdefined( entity.ignoreMelee ) && entity.ignoreMelee ) )
	{
		return false;
	}
	
	if( Distance2DSquared( entity.origin, entity.enemy.origin ) > ( (64) * (64) ) )
	{
		return false;
	}
	
	if( abs( entity.origin[2] - entity.enemy.origin[2] ) > 72 )
	{
		return false;
	}
	
	yawToEnemy = AngleClamp180( entity.angles[ 1 ] - (VectorToAngles(entity.enemy.origin-entity.origin)[1]) );
	if( abs( yawToEnemy ) > 45 )
	{
		return false;
	}
	
	return true;
}
