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

///////////////////////////////////////////////////////////////////////////////////
// satyr specific funcitons
///////////////////////////////////////////////////////////////////////////////////

void	rocketgang_melee_attack (userEntity_t *self);
void	rocketgang_ranged_attack (userEntity_t *self);

///////////////////////////////////////////////////////////////////////////////
//	rocketgang_init_frametable
//
///////////////////////////////////////////////////////////////////////////////

void	rocketgang_init_frametable (userEntity_t *self)
{
	playerHook_t	*hook = (playerHook_t *) self->userHook;
	
	ai_set_sequence_data (self, hook->frames->run [0], -1, NULL, -1, NULL, 0, -1, 0, -1, FRAME_ONCE);

	ai_set_sequence_data (self, hook->frames->atak [0], 11, NULL, -1, NULL, 0, -1, 0, -1, FRAME_ONCE);
	ai_set_sequence_data (self, hook->frames->atak [1], 15, NULL, -1, NULL, 0, -1, 0, -1, FRAME_ONCE);
}

///////////////////////////////////////////////////////////////////////////////
//	rocketgang_set_attack_seq
//
///////////////////////////////////////////////////////////////////////////////

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

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

	if (dist > 80)
	{
		if (rnd () < 0.5)
		{
			ai_init_sequence (self, hook->frames->atak [0], SEQUENCE_ATAK, 0);

			//	pick the rocket from inventory
			self->curWeapon = gstate->InventoryFindItem (self->inventory, "rocket");
		}
		else
		{
			ai_init_sequence (self, hook->frames->atak [1], SEQUENCE_ATAK, 1);
			
			//	pick the rocket from inventory
			self->curWeapon = gstate->InventoryFindItem (self->inventory, "rocket kneeling");
		}

		rocketgang_ranged_attack (self);
	}
	else
	{
		//	FIXME: put melee attack here
		ai_init_sequence (self, hook->frames->atak [0], SEQUENCE_ATAK, 0);

		//	pick the rocket from inventory
		self->curWeapon = gstate->InventoryFindItem (self->inventory, "punch");

		rocketgang_melee_attack (self);
	}

	hook->frame_flags = FRAME_ONCE;
	hook->frameInc = 1;
}

///////////////////////////////////////////////////////////////////////////////
//	rocketgang_ranged_attack
//
///////////////////////////////////////////////////////////////////////////////

void	rocketgang_ranged_attack (userEntity_t *self)
{
	playerHook_t	*hook = (playerHook_t *) self->userHook;
	int				task_type, task_type2, clear_shot = TRUE;
	float			dist;
	taskData_t		*task_data, *task_data2;
	taskStack_t		*stack;

	//ai_face_enemy (self);
	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;

		if (ai_check_projectile_attack (self, self->enemy, (ai_weapon_t *) self->curWeapon, 0))
		{
			if (hook->sound1)
				gstate->sound (self, CHAN_AUTO, hook->sound1, 128, ATTN_NORM);

			ai_fire_curWeapon (self, Vector (self->s.origin) + Vector (self->view_ofs), Vector (self->enemy->s.origin) + Vector (self->enemy->view_ofs), 0, 0);
		}
		else
		{
			hook->ai_flags |= AI_END_ATTACK;
			
			hook->attack_finished = gstate->time + 2.0;	//	won't attack for another 2 seconds

			//	skip kick-back part of firing sequence
			if (hook->cur_seq_num == 0)
				self->s.frame += 12;
			else
				self->s.frame += 21;
		}
	}
	
	if (hook->frame_flags & FRAME_PLAYSOUND2)
	{
		hook->frame_flags = hook->frame_flags - FRAME_PLAYSOUND2;
		
		if (hook->sound2)
			gstate->sound (self, CHAN_AUTO, hook->sound2, 128, ATTN_NORM);

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

		task_current (hook->task_stack, &task_data, &task_type);
		if (task_data->goal_type != GOAL_ENEMY)
			com->Error ("rocketgang: TASK_ATTACK without GOAL_ENEMY.\n");

		if (self->enemy->deadflag != DEAD_NO)
		{
			//	leaving, so pop off TASK_ATTACK
			task_pop (&hook->task_stack);

			ai_killed_enemy (self);

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

			//	check the next task to make sure it's not screwed
			stack = hook->task_stack->next;
			task_current (stack, &task_data, &task_type);
			if (task_type == TASK_CHASE && task_data->goal_type != GOAL_ENEMY)
				com->Error ("rocketgang: TASK_CHASE on stack without GOAL_ENEMY.\n");
	
			//	leaving, so pop off TASK_ATTACK
			task_pop (&hook->task_stack);

			task_current (hook->task_stack, &task_data2, &task_type2);
			if (task_type2 == TASK_CHASE && task_data2->goal_type != GOAL_ENEMY)
				com->Error ("rocketgang: TASK_CHASE without GOAL_ENEMY.\n");

			ai_should_follow (self);

			return;
		}

		rocketgang_set_attack_seq (self);

		return;
	}

//	ai_attack_move (self);

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

///////////////////////////////////////////////////////////////////////////////
//	rocketgang_melee_attack
//
///////////////////////////////////////////////////////////////////////////////


void	rocketgang_melee_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;
				
		if (hook->sound1)
			gstate->sound (self, CHAN_AUTO, hook->sound1, 128, ATTN_NORM);
	}
	
	if (hook->frame_flags & FRAME_PLAYSOUND2)
	{
		hook->frame_flags = hook->frame_flags - FRAME_PLAYSOUND2;

		if (hook->sound2)
			gstate->sound (self, CHAN_AUTO, hook->sound2, 128, ATTN_NORM);
	}
		
	if (hook->frame_flags & FRAME_STATIC)
	{
		hook->frame_flags -= FRAME_STATIC;

		if (self->enemy->deadflag != DEAD_NO)
		{
			//	leaving, so pop off TASK_ATTACK
			task_pop (&hook->task_stack);

			hook->begin_wander (self);

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

			ai_should_follow (self);

			return;
		}
		else
		{
			rocketgang_set_attack_seq (self);

			return;
		}
	}

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

///////////////////////////////////////////////////////////////////////////////
//	rocketgang_begin_attack
//
///////////////////////////////////////////////////////////////////////////////

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

	com->ZeroVector (self->velocity);

	rocketgang_set_attack_seq (self);

	hook->th_state = TH_ATTACK;
	hook->attack_finished = gstate->time + (hook->endFrame - hook->endFrame) * 0.1;
}

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

void	monster_rocketdude (userEntity_t *self)
{
	playerHook_t		*hook;
	int					i;
	float				f;
	Vector				v;

	hook = ai_init_monster (self, TYPE_ROCKETGANG);

	self->className = "monster_rocketgang";
	self->netname = "rocket-launching gang member";

	gstate->Precache_Model ("models/rocketgang.mdl");
	gstate->Precache_Model ("models/v_rocket.mdl");

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

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

	ai_get_sequences (self);

	rocketgang_init_frametable (self);

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

	hook->begin_attack = rocketgang_begin_attack;

	hook->run_speed = 240;
	hook->walk_speed = 90;
	hook->attack_dist = 1000;

	self->health = 200;
	hook->dflags |= DFL_RANGEDATTACK;
	hook->max_health = 200;
	hook->pain_chance = 10;
	self->mass = 1.5;

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

	self->translucent = 0;

	//////////////////////////////////////////////
	//	set up monster's inventory and weapons
	//////////////////////////////////////////////
	
	self->inventory = gstate->InventoryNew ();

	self->curWeapon = ai_init_weapon (self, 80, 0, 0.064, 0.024, 450.0, hook->attack_dist, Vector (12, 16, 39), "rocket", rocket_attack, WEAPON_PROJECTILE | WEAPON_EXPLOSIVE);
	gstate->InventoryAddItem (self->inventory, self->curWeapon);

	self->curWeapon = ai_init_weapon (self, 80, 0, 0.024, 0, 450.0, hook->attack_dist, Vector (12, 16, 26), "rocket kneeling", rocket_attack, WEAPON_PROJECTILE | WEAPON_EXPLOSIVE);
	gstate->InventoryAddItem (self->inventory, self->curWeapon);

	self->curWeapon = ai_init_weapon (self, 4, 10, 0, 0, 0, 80.0, Vector (0, 0, 0), "punch", melee_punch, WEAPON_TRACE);
	gstate->InventoryAddItem (self->inventory, self->curWeapon);
}

