//////////////////////////////////////////////////////////////////////
//	the frog!!!
//
//////////////////////////////////////////////////////////////////////

#include	"world.h"

#include	"ai.h"
#include	"ai_utils.h"

#include	"ai_move.h"
#include	"ai_weapons.h"
#include	"ai_task.h"
#include	"ai_frames.h"

#include	"skat.h"
#include	"nodelist.h"

////////////////////////////////////////////////////////////////////////////////
// exports
////////////////////////////////////////////////////////////////////////////////

DllExport	void	monster_froginator (userEntity_t *self);
DllExport	void	frog_message (userEntity_t *self);

///////////////////////////////////////////////////////////////////////////////////
// frog specific funcitons
///////////////////////////////////////////////////////////////////////////////////

void	frog_close_melee (userEntity_t *self);
void	frog_far_melee (userEntity_t *self);
void	frog_jump_attack (userEntity_t *self);

////////////////////////////////
//	frog_init_frametable
//
////////////////////////////////

void	frog_init_frametable (userEntity_t *self)
{
	playerHook_t	*hook = (playerHook_t *) self->userHook;
	int				i;

	/////////////////////////////////////////////////////////////////////////////
	//	attacks
	/////////////////////////////////////////////////////////////////////////////
	
	//	tongue attack
	ai_set_sequence_data (self, hook->frames->atak [0], -1, NULL, 10, "monsters/froginator/frogattack1.wav", 0, -1, 0, -1, FRAME_ONCE);
	//	biting attack
	ai_set_sequence_data (self, hook->frames->atak [1], 9, "monsters/froginator/frogattack2.wav", -1, NULL, 0, -1, 0, -1, FRAME_ONCE);

	//	jumping attack
	ai_set_sequence_data (self, hook->frames->jump [0], 10, NULL, 20, "monster/froginator/frogattack1.wav", 0, -1, 0, -1, FRAME_ONCE);

	/////////////////////////////////////////////////////////////////////////////
	//	ambients
	/////////////////////////////////////////////////////////////////////////////

	ai_set_sequence_data (self, hook->frames->stand [0], -1, NULL, -1, NULL, 0, -1, 0, -1, FRAME_LOOP);
	//	tongue lick
	ai_set_sequence_data (self, hook->frames->stand [1], -1, NULL, -1, NULL, 0, -1, 0, -1, FRAME_ONCE);
}

////////////////////////////////
//	frog_set_attack_seq
//
////////////////////////////////

void	frog_set_attack_seq (userEntity_t *self)
{
	playerHook_t	*hook = (playerHook_t *) self->userHook;
	float			dist;
	int				i;
	Vector			temp;
	
	dist = qv_dist (self->s.origin, self->enemy->s.origin);

	if (dist > 128 && hook->dflags & DFL_JUMPATTACK)
	{
		ai_init_sequence (self, hook->frames->jump [0], SEQUENCE_JUMP, 0);
		frog_jump_attack (self);
	}
	else if (dist > 56)
	{
		ai_init_sequence (self, hook->frames->atak [0], SEQUENCE_ATAK, 0);
		frog_far_melee (self);
	}
	else
	{
		ai_init_sequence (self, hook->frames->atak [1], SEQUENCE_ATAK, 1);
		frog_close_melee (self);
	}
}

////////////////////////////////////////
//	frog_hop
//
//	for simplicity's sake, frog doesn't check where it's hopping
//	also saves us some trace lines
////////////////////////////////////////

void	frog_hop (userEntity_t *self, float yaw_add)
{
	playerHook_t	*hook = (playerHook_t *) self->userHook;
	float			hop_yaw;
	Vector			hop_angles;
	
	hop_yaw = self->s.angles [1];
	if (rnd () < 0.5)
		hop_yaw += yaw_add;
	else
		hop_yaw -= yaw_add;

	hop_angles = Vector (0, hop_yaw, 0);
	hop_angles.AngleVectors (forward, right, up);

	self->gravity = 2.0;

	forward = forward * 150.0;
	forward.set (self->velocity);

	self->velocity [2] = hook->upward_vel;

	ai_init_sequence (self, hook->frames->jump [0], SEQUENCE_JUMP, 0);
}

///////////////////////////
//	frog_jump_attack
//
///////////////////////////

void	frog_jump_attack (userEntity_t *self)
{
	playerHook_t	*hook = (playerHook_t *) self->userHook;
	Vector			temp;
	float			dist;

 	hook->dflags |= DFL_FACEENEMY;

	dist = qv_dist (self->s.origin, self->enemy->s.origin);

	if (hook->frame_flags & FRAME_PLAYSOUND1)
	{
		hook->frame_flags = hook->frame_flags - FRAME_PLAYSOUND1;

		///////////////////////////////////////////////////////////////////
		//	give jumping velocity to frog here
		///////////////////////////////////////////////////////////////////

		temp = Vector (self->s.angles);
		temp.AngleVectors (forward, right, up);

		temp = forward * hook->run_speed * 3.0;
		temp.z = hook->upward_vel;
		temp.set (self->velocity);

		self->gravity = 1.0;
		
//		ai_fire_curWeapon (self, Vector (self->s.origin), Vector (self->enemy->s.origin), 0, 0);
	}
	if (hook->frame_flags & FRAME_PLAYSOUND2)
	{
		hook->frame_flags = hook->frame_flags - FRAME_PLAYSOUND2;
	
//		ai_fire_curWeapon (self, Vector (self->s.origin), Vector (self->enemy->s.origin), 0, 0);
	}

	// attack without any transitions
	if (hook->frame_flags & FRAME_STATIC)
	{
		hook->frame_flags -= FRAME_STATIC;	

		self->gravity = 1.0;		

		if (self->enemy->deadflag != DEAD_NO)
		{
			//	pop off everything thru TASK_CHASE for this enemy
			ai_killed_enemy (self);

			return;
		}
		else if (dist > hook->attack_dist || !com->Visible (self, self->enemy))
		{
			ai_should_follow (self);

			//	leaving, so pop off TASK_ATTACK
			task_pop (&hook->task_stack);

			return;
		}
		else
		{
			frog_set_attack_seq (self);

			return;
		}
	}

	ai_set_think (self, frog_jump_attack, "frog_jump_attack");
}

///////////////////////////
//	frog_close_melee
//
///////////////////////////

void	frog_close_melee (userEntity_t *self)
{
	playerHook_t	*hook = (playerHook_t *) self->userHook;
	Vector			temp;
	float			dist;
	
	hook->dflags |= DFL_FACEENEMY;

	dist = qv_dist (self->s.origin, self->enemy->s.origin);

	// are we in a transition sequence?
	if (hook->frame_flags & FRAME_TRANS)
	{
		if (hook->frame_flags & FRAME_STATIC)
			ai_post_trans (self, FRAME_LOOP + FRAME_NEEDTRANS);
	}
	else
	{
		if (hook->frame_flags & FRAME_PLAYSOUND1)
		{
			hook->frame_flags = hook->frame_flags - FRAME_PLAYSOUND1;
					
			ai_fire_curWeapon (self, Vector (self->s.origin), Vector (self->enemy->s.origin), 0, 0);
		}
		
		if (hook->frame_flags & FRAME_PLAYSOUND2)
		{
			hook->frame_flags = hook->frame_flags - FRAME_PLAYSOUND2;
					
			ai_fire_curWeapon (self, Vector (self->s.origin), Vector (self->enemy->s.origin), 0, 0);
		}
		
		if (hook->frame_flags & FRAME_LAST)
		{
			hook->frame_flags -= FRAME_LAST;

			if (self->enemy->deadflag != DEAD_NO)
			{
				ai_killed_enemy (self);

				return;
			}
			else if (dist > hook->attack_dist || !com->Visible (self, self->enemy))
			{
				ai_should_follow (self);

				//	leaving, so pop off TASK_ATTACK
				task_pop (&hook->task_stack);

				return;
			}
			else
			{
				frog_set_attack_seq (self);

				return;
			}
		}
	}

	ai_set_think (self, frog_close_melee, "frog_close_melee");
}

///////////////////////////
//	frog_far_melee
//
///////////////////////////

void	frog_far_melee (userEntity_t *self)
{
	playerHook_t	*hook = (playerHook_t *) self->userHook;
	Vector			temp;
	float			dist;
/*	
	temp = self->goalentity->s.origin;
	com->CopyVector (self->goalentity->s.origin, self->enemy->s.origin);
	ai_face_goal (self);
	temp.set (self->goalentity->s.origin);
*/
	hook->dflags |= DFL_FACEENEMY;

	dist = qv_dist (self->s.origin, self->enemy->s.origin);

	if (hook->frame_flags & FRAME_PLAYSOUND2)
	{
		hook->frame_flags = hook->frame_flags - FRAME_PLAYSOUND2;
	
		ai_fire_curWeapon (self, Vector (self->s.origin), Vector (self->enemy->s.origin), 0, 0);
	}

	// attack without any transitions
	if (hook->frame_flags & FRAME_STATIC)
	{
		hook->frame_flags -= FRAME_STATIC;	

		if (self->enemy->deadflag != DEAD_NO)
		{
			//	pop off everything thru TASK_CHASE for this enemy
			ai_killed_enemy (self);

			return;
		}
		else if (dist > hook->attack_dist || !com->Visible (self, self->enemy))
		{
			ai_should_follow (self);

			//	leaving, so pop off TASK_ATTACK
			task_pop (&hook->task_stack);

			return;
		}
		else
		{
			frog_set_attack_seq (self);

			return;
		}
	}

	ai_set_think (self, frog_far_melee, "frog_far_melee");
}

////////////////////////////
//	frog_begin_attack
//
////////////////////////////

void	frog_begin_attack (userEntity_t *self)
{
	playerHook_t	*hook = (playerHook_t *) self->userHook;

	com->ZeroVector (self->velocity);

	hook->th_state = TH_ATTACK;
	hook->attack_finished = gstate->time + 1.0;

	frog_set_attack_seq (self);
}

///////////////////////////////
//	monster_froginator
//
//	spawns a frog during level load
///////////////////////////////

void	monster_froginator (userEntity_t *self)
{
	playerHook_t		*hook;
	int					i;

	hook = ai_init_monster (self, TYPE_FROGINATOR);

	self->className = "monster_froginator";
	self->netname = "RoboCo Froginator";
/*
	gstate->Precache_Sound ("monsters/frog/frogattack1.wav");
	gstate->Precache_Sound ("monsters/frog/frogattack2.wav");
	gstate->Precache_Sound ("monsters/frog/frogdeath1.wav");
	gstate->Precache_Sound ("monsters/frog/frogdeath2.wav");
	gstate->Precache_Sound ("monsters/frog/frogpain1.wav");
	gstate->Precache_Sound ("monsters/frog/frogpain2.wav");
	gstate->Precache_Sound ("monsters/frog/frogsight1.wav");
	gstate->Precache_Sound ("monsters/frog/frogsight2.wav");
	gstate->Precache_Sound ("monsters/frog/frogwander1.wav");
	gstate->Precache_Sound ("monsters/frog/frogwander2.wav");
*/
	gstate->Precache_Sound ("monsters/froginator/frogscream1.wav");
	gstate->Precache_Sound ("monsters/froginator/frogattack1.wav");

	self->s.modelindex = gstate->ModelIndex ("models/frog/frog.md2");
	gstate->SetSize (self, -16.0, -16.0, -24.0, 16.0, 16.0, 32.0);

	////////////////////////////////////////////////////////////////
	// set up animation stuff
	////////////////////////////////////////////////////////////////

	ai_get_sequences (self);

	frog_init_frametable (self);

	///////////////////////////////////////////////////////////
	// set up pointers to this creature's ai functions
	///////////////////////////////////////////////////////////

//	hook->find_target = ai_find_target_monster_bot;
	hook->begin_attack = frog_begin_attack;

	hook->run_speed = 100;
	hook->walk_speed = 100;
	hook->attack_dist = 128;

	self->health = 100;
	hook->max_health = 100;
	hook->pain_chance = 5;
	hook->jump_chance = 90;
	self->mass = 1.5;
	self->gravity = 2.0;
	self->ang_speed [YAW] = 150;

	self->think = ai_get_spawn_node;
	self->nextthink = gstate->time + 0.2;

	self->translucent = 0;

	//////////////////////////////////////////////
	//	give frog its melee weapon
	//////////////////////////////////////////////

/*
	self->curWeapon = ai_weapon_create ("punch", melee_punch, ai_weapon_command);
	i = 4;
	ai_weapon_command (self->curWeapon, "base_damage", &i);
	ai_weapon_command (self->curWeapon, "rnd_damage", &i);
*/

	self->curWeapon = ai_init_weapon (self, 4, 4, 0, 0, 0, 128.0, Vector (0, 0, 0), "punch", melee_punch, WEAPON_TRACE);
}
