/***********************************************************************

ai_monster_demon_pinky.script

monster_demon_pinky

***********************************************************************/

#define PINKY_ROAR_RATE			5
#define PINKY_DODGE_RATE		5

object monster_demon_pinky : monster_base {
	float		nextDodge;
	float		nextRoar;
	entity		combat_node;
	
	// States
	void		state_Begin();
	void		state_Idle();

	// attacks
	float		check_attacks();
	void		do_attack( float attack_flags );
	void		combat_roar();
	void		combat_melee();
	void		combat_dodge_left();
	void		combat_dodge_right();

	void		init();
	
	// torso anim states
	void		Torso_Idle();
	void		Torso_Pain();
	void		Torso_MeleeAttack();
	void		Torso_Roar();
	
	// legs anim states
	void		Legs_Idle();
	void		Legs_Walk();
	void		Legs_Run();
	void		Legs_DodgeLeft();
	void		Legs_DodgeRight();
};

/***********************************************************************

	Torso animation control

***********************************************************************/

void monster_demon_pinky::Torso_Idle() {
	idleAnim( ANIMCHANNEL_TORSO, "stand" );
	
	eachFrame {
		if ( AI_PAIN ) { animState( ANIMCHANNEL_TORSO, "Torso_Pain", 0 ); }
	}
}

void monster_demon_pinky::Torso_Pain() {
	string animname;
	float nextpain;
	float currenttime;
	
	animname = getPainAnim();
	playAnim( ANIMCHANNEL_TORSO, animname );

	nextpain = sys.getTime() + 0.25;
	
	while( !animDone( ANIMCHANNEL_TORSO, 2 ) ) {
		if ( AI_PAIN ) {
			currenttime = sys.getTime();
			if ( currenttime > nextpain ) {
				animState( ANIMCHANNEL_TORSO, "Torso_Pain", 0 );
			}
		}
		waitFrame();
	}

	finishAction( "pain" );
	animState( ANIMCHANNEL_TORSO, "Torso_Idle", 2 );
}

void monster_demon_pinky::Torso_MeleeAttack() {
	playAnim( ANIMCHANNEL_TORSO, "melee_attack" );

	while( !animDone( ANIMCHANNEL_TORSO, 4 ) ) {
		waitFrame();
	}
	
	finishAction( "melee_attack" );
	animState( ANIMCHANNEL_TORSO, "Torso_Idle", 4 );
}

void monster_demon_pinky::Torso_Roar() {
	disablePain();

	playAnim( ANIMCHANNEL_TORSO, "roar" );
	while( !animDone( ANIMCHANNEL_TORSO, 4 ) ) {
		waitFrame();
	}

	finishAction( "roar" );
	animState( ANIMCHANNEL_TORSO, "Torso_Idle", 4 );
}

/***********************************************************************

	Legs animation control

***********************************************************************/

void monster_demon_pinky::Legs_Idle() {
	idleAnim( ANIMCHANNEL_LEGS, "idle" );

	eachFrame {
		if ( run && AI_FORWARD )	{ animState( ANIMCHANNEL_LEGS, "Legs_Run", 4 ); }
		if ( AI_FORWARD )			{ animState( ANIMCHANNEL_LEGS, "Legs_Walk", 4 ); }
	}
}

void monster_demon_pinky::Legs_Walk() {
	playCycle( ANIMCHANNEL_LEGS, "walk" );
	
	eachFrame {
		if ( run && AI_FORWARD )	{ animState( ANIMCHANNEL_LEGS, "Legs_Run", 4 ); }
		if ( !AI_FORWARD )			{ animState( ANIMCHANNEL_LEGS, "Legs_Idle", 4 ); }
	}
}

void monster_demon_pinky::Legs_Run() {
	playCycle( ANIMCHANNEL_LEGS, "run" );
	
	eachFrame {
		if ( !run && AI_FORWARD )	{ animState( ANIMCHANNEL_LEGS, "Legs_Walk", 4 ); }
		if ( !AI_FORWARD )			{ animState( ANIMCHANNEL_LEGS, "Legs_Idle", 4 ); }
	}
}

void monster_demon_pinky::Legs_DodgeLeft() {
	playAnim( ANIMCHANNEL_LEGS, "evade_left" );

	while( !animDone( ANIMCHANNEL_LEGS, 4 ) ) {
		waitFrame();
	}
	
	finishAction( "strafe" );
	animState( ANIMCHANNEL_LEGS, "Legs_Idle", 4 );
}

void monster_demon_pinky::Legs_DodgeRight() {
	playAnim( ANIMCHANNEL_LEGS, "evade_right" );

	while( !animDone( ANIMCHANNEL_LEGS, 4 ) ) {
		waitFrame();
	}

	finishAction( "strafe" );
	animState( ANIMCHANNEL_LEGS, "Legs_Idle", 4 );
}

/***********************************************************************

	AI

***********************************************************************/

/*
=====================
monster_demon_pinky::init
=====================
*/
void monster_demon_pinky::init() {
	setState( "state_Begin" );
}

/***********************************************************************

	States

***********************************************************************/

/*
=====================
monster_demon_pinky::state_Begin
=====================
*/
void monster_demon_pinky::state_Begin() {
	animState( ANIMCHANNEL_TORSO, "Torso_Idle", 0 );
	animState( ANIMCHANNEL_LEGS, "Legs_Idle", 0 );

	monster_begin();

	setMoveType( MOVETYPE_ANIM );	
	setState( "state_Idle" );
}

/*
=====================
monster_demon_pinky::state_Idle
=====================
*/
void monster_demon_pinky::state_Idle() {
	wait_for_enemy();

	nextRoar	= RandomTime( PINKY_ROAR_RATE );
	nextDodge	= RandomTime( PINKY_DODGE_RATE );

	setState( "state_Combat" );
}

/***********************************************************************

	attacks

***********************************************************************/

/*
=====================
monster_demon_pinky::do_attack
=====================
*/
void monster_demon_pinky::do_attack( float attack_flags ) {
	if ( attack_flags & ATTACK_DODGE_LEFT ) {
		combat_dodge_left();
	} else if ( attack_flags & ATTACK_DODGE_RIGHT ) {
		combat_dodge_right();
	} else if ( attack_flags & ATTACK_COMBAT_NODE ) {
		combat_ainode( combat_node );
	} else if ( attack_flags & ATTACK_MELEE ) {
		combat_melee();
	} else if ( attack_flags & ATTACK_MISSILE ) {
		combat_roar();
	}
}

/*
=====================
monster_demon_pinky::check_attacks
=====================
*/
float monster_demon_pinky::check_attacks() {
	float currentTime;
	float canMelee;
	float attack_flags;
	
	attack_flags = 0;
	
	canMelee = testMeleeAttack();
	currentTime = sys.getTime();
	if ( !canMelee ) {
		if ( AI_PAIN && ( currentTime >= nextDodge ) ) {
			if ( testAnimMove( "evade_left" ) ) {
				attack_flags |= ATTACK_DODGE_LEFT;
			}
			if ( testAnimMove( "evade_right" ) ) {
				attack_flags |= ATTACK_DODGE_RIGHT;
				
				// if we can dodge either direction, pick one
				if ( attack_flags & ATTACK_DODGE_LEFT ) {
					if ( sys.random( 100 ) < 50 ) {
						attack_flags &= ~ATTACK_DODGE_RIGHT;
					} else {
						attack_flags &= ~ATTACK_DODGE_LEFT;
					}
				}
			}
		}
	
		combat_node = getCombatNode();
		if ( combat_node ) {
			attack_flags |= ATTACK_COMBAT_NODE;
		}
	}
	
	if ( canMelee ) {
		attack_flags |= ATTACK_MELEE;
	}

	if ( AI_ENEMY_IN_FOV ) {
		if ( currentTime >= nextRoar ) {
			attack_flags |= ATTACK_MISSILE;
		}
	}
	
	return attack_flags;
}

/*
=====================
monster_demon_pinky::combat_roar
=====================
*/
void monster_demon_pinky::combat_roar() {
	faceEnemy();
	stopMove();

	animState( ANIMCHANNEL_TORSO, "Torso_Roar", 4 );
	waitAction( "roar" );

	// don't roar for a bit
	nextRoar = DelayTime( PINKY_ROAR_RATE );
}

/*
=====================
monster_demon_pinky::combat_melee
=====================
*/
void monster_demon_pinky::combat_melee() {
	stopMove();
	animState( ANIMCHANNEL_TORSO, "Torso_MeleeAttack", 5 );
	waitAction( "melee_attack" );
}

/*
=====================
monster_demon_pinky::combat_dodge_left
=====================
*/
void monster_demon_pinky::combat_dodge_left() {
	stopMove();
	faceEnemy();
	animState( ANIMCHANNEL_LEGS, "Legs_DodgeLeft", 2 );
	waitAction( "strafe" );
	nextDodge = DelayTime( PINKY_DODGE_RATE );
}

/*
=====================
monster_demon_pinky::combat_dodge_right
=====================
*/
void monster_demon_pinky::combat_dodge_right() {
	stopMove();
	faceEnemy();
	animState( ANIMCHANNEL_LEGS, "Legs_DodgeRight", 2 );	
	waitAction( "strafe" );
	nextDodge = DelayTime( PINKY_DODGE_RATE );
}
