#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_doombat (userEntity_t *self);

///////////////////////////////////////////////////////////////////////////////////
//	doombat specific funcitons
///////////////////////////////////////////////////////////////////////////////////

static	int	doombat_count = 0;

////////////////////////////////
//	doombat_init_frametable
//
////////////////////////////////

void	doombat_init_frametable (userEntity_t *self)
{
	playerHook_t	*hook = (playerHook_t *) self->userHook;
	
	ai_set_sequence_data (self, hook->frames->atak [0], 0, "monsters/doombat/doombatattack1.wav", -1, NULL, 0, -1, 0, -1, FRAME_ONCE);

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

////////////////////////////////
//  doombat_pitch_back
//
//	go back to a 0 pitch after killing enemy
////////////////////////////////

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

	if (self->ideal_ang [0] == self->s.angles [0])
	{
		ai_set_think (self, hook->begin_wander, "doombat_pitch_back");

		//	pop off everything thru TASK_CHASE for this enemy
		ai_killed_enemy (self);
	}
	else
	{
		com->ChangePitch (self);

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

////////////////////////////////
//	doombat_begin_sight
//
//	FIXME: play a transition from hanging to flying
////////////////////////////////

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

	hook->continue_think = self->think;

	self->s.angles [0] = 0;
	self->s.angles [1] = com->AngleMod (self->s.angles [1] + 180);

	ai_set_think (self, hook->continue_think, "doombat_begin_sight");
}

////////////////////////////////
//  doombat_attack
//
//	ai for following/approaching an enemy
////////////////////////////////

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

	//ai_face_enemy (self);
	hook->dflags |= DFL_FACEENEMY;

	if (self->movetype == MOVETYPE_FLY)
	{
		temp = Vector (self->enemy->s.origin) - Vector (self->s.origin);
		temp.Normalize ();
		com->VecToAngles (temp, temp);
		self->ideal_ang [0] = temp.x;

		com->ChangePitch (self);
	}

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

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

		ai_fire_curWeapon (self, Vector (self->s.origin) + Vector (self->view_ofs), Vector (self->enemy->s.origin) + Vector (self->enemy->view_ofs), 0.064, 0.064);
	}
	
	if (hook->frame_flags & FRAME_STATIC)
	{
		hook->frame_flags -= FRAME_STATIC;

		if (self->enemy->deadflag != DEAD_NO)
		{
			if (self->movetype == MOVETYPE_FLY)
			{
				self->ideal_ang [0] = 0.0;
				doombat_pitch_back (self);
			}
			else
			{
				ai_set_think (self, hook->begin_wander, "doombat_attack");

				//	pop off everything thru TASK_CHASE for this enemy
				ai_killed_enemy (self);
			}

			return;
		}
		else if (dist > hook->attack_dist + 64.0 || !com->Visible (self, self->enemy))
		{
			//	leaving, so pop off TASK_ATTACK
			task_pop (&hook->task_stack);

			ai_should_follow (self);

			return;
		}
		else
		{
			if (self->movetype == MOVETYPE_FLY)
				ai_init_sequence (self, hook->frames->atak [0], SEQUENCE_ATAK, 0);
			else
				ai_init_sequence (self, hook->frames->walk [0], SEQUENCE_WALK, 0);
		}
	}

	ai_attack_move (self);

	if (!(hook->ai_flags & AI_JUMP) && !(hook->ai_flags & AI_TURN))
		ai_set_think (self, doombat_attack, "doombat_attack");
}

////////////////////////////////
//	doombat_begin_attack
//
//	ai for following/approaching an enemy
////////////////////////////////

void	doombat_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 + (hook->endFrame - hook->endFrame) * 0.1;

	if (self->movetype == MOVETYPE_FLY)
	{
		ai_init_sequence (self, hook->frames->atak [0], SEQUENCE_ATAK, 0);

		doombat_attack (self);
	}
	else
	{
		ai_init_sequence (self, hook->frames->walk [0], SEQUENCE_WALK, 0);

		doombat_attack (self);
	}
}

////////////////////////////////
//	monster_doombat
//
//	creates the superfly entity and sets him up to smack down
//	everything that gets in his way.
////////////////////////////////

void	monster_doombat (userEntity_t *self)
{
	playerHook_t		*hook;
	int					i;
	float				s;

//	doombat_count++;
	if (doombat_count > 1)
		return;

	hook = ai_init_monster (self, TYPE_DOOMBAT);

	self->className = "monster_doombat";
	self->netname = "doom bat";

	gstate->Precache_Sound ("monsters/dbat/dbatattack1.wav");
	gstate->Precache_Sound ("monsters/dbat/dbatattack2.wav");
	gstate->Precache_Sound ("monsters/dbat/dbatdeath1.wav");
	gstate->Precache_Sound ("monsters/dbat/dbatdeath2.wav");
	gstate->Precache_Sound ("monsters/dbat/dbatpain1.wav");
	gstate->Precache_Sound ("monsters/dbat/dbatpain2.wav");
	gstate->Precache_Sound ("monsters/dbat/dbatsight1.wav");
	gstate->Precache_Sound ("monsters/dbat/dbatsight2.wav");
	gstate->Precache_Sound ("monsters/dbat/dbatwander1.wav");
	gstate->Precache_Sound ("monsters/dbat/dbatwander2.wav");

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

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

	ai_get_sequences (self);

	doombat_init_frametable (self);

	//	demo crap

	com->free (hook->frames->stand [0]);
	hook->frames->stand [0] = hook->frames->fly [0];

	com->SetVector (self->s.render_scale, 1.3, 1.3, 1.3);

	//	end demo crap

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

	hook->begin_attack = doombat_begin_attack;

	hook->run_speed = 240;
	hook->walk_speed = 120;
	hook->attack_speed = 120;
	hook->pain_chance = 20;

//	hook->dflags |= DFL_RANGEDATTACK;
	hook->attack_dist = 64;

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

	self->translucent = 0;

	self->movetype = MOVETYPE_FLY;
	hook->dflags |= DFL_FLYSWOOP;

	hook->fly_goal_offset = 32.0;

	//	rotate upside down and flip to face other direction
//	self->s.angles [0] = -180.0;
//	self->s.angles [1] = com->AngleMod (self->s.angles [1] + 180.0);

	//////////////////////////////////////////////
	//	give chaingunner his chaingun
	//////////////////////////////////////////////
	self->curWeapon = ai_init_weapon (self, 5, 7, 0, 0, 0, hook->attack_dist, Vector (0, 0, 0), "punch", melee_punch, WEAPON_TRACE);
}

