#using scripts\shared\clientfield_shared;
#using scripts\shared\ai\systems\gib;
#using scripts\shared\ai_shared;

                                                                                                            	  	  	              	   	                                                                                                                                       	                                                                                                                                                            	                                                                                                                                                                
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         	   	                                                                                                                                                                                                                                  	                                                                                                                                                                                                                                                                                                                                                                                                                                                   
                                                                                                           	                                      	                	                       	              	              	                          	                                                                                                                                                                                                          
                                                                                                                                                                                                          	     	                                                                                   

#precache( "client_fx", "zombie/fx_val_chest_burst");

#precache( "client_fx", "fire/fx_fire_ai_human_arm_left_loop_optim");
#precache( "client_fx", "fire/fx_fire_ai_human_arm_right_loop_optim");
#precache( "client_fx", "fire/fx_fire_ai_human_arm_left_loop_optim");
#precache( "client_fx", "fire/fx_fire_ai_human_arm_right_loop_optim");
#precache( "client_fx", "fire/fx_fire_ai_human_torso_loop_optim");
#precache( "client_fx", "fire/fx_fire_ai_human_hip_left_loop_optim");
#precache( "client_fx", "fire/fx_fire_ai_human_hip_right_loop_optim");
#precache( "client_fx", "fire/fx_fire_ai_human_leg_left_loop_optim");
#precache( "client_fx", "fire/fx_fire_ai_human_leg_right_loop_optim");
#precache( "client_fx", "fire/fx_fire_ai_human_head_loop_optim");

function autoexec precache()
{
	
}

function autoexec main()
{
	level._effect["zombie_special_day_effect"]		= "zombie/fx_val_chest_burst";
	
	ai::add_archetype_spawn_function( "zombie", &ZombieClientUtils::zombie_override_burn_fx );
	
	clientfield::register(
		"actor",
		"zombie",
		1,
		1,
		"int",
		&ZombieClientUtils::zombieHandler,
		!true,
		!true);
	
	clientfield::register(
		"actor",
		"zombie_special_day",
		6001,
		1,
		"counter",
		&ZombieClientUtils::zombieSpecialDayEffectsHandler,
		!true,
		!true);
}

#namespace ZombieClientUtils;

function zombieHandler( localClientNum, oldValue, newValue, bNewEnt, bInitialSnap, fieldName, wasDemoJump )
{
	entity = self;
	
	if ( IsDefined( entity.archetype ) && entity.archetype != "zombie" )
	{
		return;
	}
	
	if ( !IsDefined( entity.initializedGibCallbacks ) || !entity.initializedGibCallbacks )
	{
		entity.initializedGibCallbacks = true;

		GibClientUtils::AddGibCallback( localClientNum, entity, 8, &_gibCallback );
		GibClientUtils::AddGibCallback( localClientNum, entity, 16, &_gibCallback );
		GibClientUtils::AddGibCallback( localClientNum, entity, 32, &_gibCallback );
		GibClientUtils::AddGibCallback( localClientNum, entity, 128, &_gibCallback );
		GibClientUtils::AddGibCallback( localClientNum, entity, 256, &_gibCallback );
	}
}

function private _gibCallback( localClientNum, entity, gibFlag )
{
	switch (gibFlag)
	{
	case 8:
		playsound(0, "zmb_zombie_head_gib", self.origin + (0,0,60));
		break;
	case 16:
	case 32:
	case 128:
	case 256:
		playsound(0, "zmb_death_gibs", self.origin + (0,0,30));
		break;
	}
}

function zombieSpecialDayEffectsHandler( localClientNum, oldValue, newValue, bNewEnt, bInitialSnap, fieldName, wasDemoJump )
{
	entity = self;
	
	if ( IsDefined( entity.archetype ) && entity.archetype != "zombie" )
	{
		return;
	}
	
	origin = entity GetTagOrigin( "j_spine4" );
	
	fx = PlayFX( localClientNum, level._effect["zombie_special_day_effect"], origin );
	SetFXIgnorePause( localClientNum, fx, true );
}

function zombie_override_burn_fx( localClientNum )
{
	if( SessionModeIsZombiesGame() )
	{
		if( !isdefined( self._effect ))
		{
			self._effect = [];
		}
		level._effect["fire_zombie_j_elbow_le_loop"]		= "fire/fx_fire_ai_human_arm_left_loop_optim";	// hand and forearm fires
		level._effect["fire_zombie_j_elbow_ri_loop"]		= "fire/fx_fire_ai_human_arm_right_loop_optim";
		level._effect["fire_zombie_j_shoulder_le_loop"]	= "fire/fx_fire_ai_human_arm_left_loop_optim";	// upper arm fires
		level._effect["fire_zombie_j_shoulder_ri_loop"]	= "fire/fx_fire_ai_human_arm_right_loop_optim";
		level._effect["fire_zombie_j_spine4_loop"]		= "fire/fx_fire_ai_human_torso_loop_optim";		// upper torso fires
		level._effect["fire_zombie_j_hip_le_loop"]		= "fire/fx_fire_ai_human_hip_left_loop_optim";	// thigh fires
		level._effect["fire_zombie_j_hip_ri_loop"]		= "fire/fx_fire_ai_human_hip_right_loop_optim";
		level._effect["fire_zombie_j_knee_le_loop"]		= "fire/fx_fire_ai_human_leg_left_loop_optim";	// shin fires
		level._effect["fire_zombie_j_knee_ri_loop"]		= "fire/fx_fire_ai_human_leg_right_loop_optim";
		level._effect["fire_zombie_j_head_loop"] 		= "fire/fx_fire_ai_human_head_loop_optim";		// head fire
	}
}
