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

////////////////////////////////////////////////////////////////////////////////
//	globals
////////////////////////////////////////////////////////////////////////////////

static	int	skel_count	= 0;

///////////////////////////////////////////////////////////////////////////////////
// skel specific funcitons
///////////////////////////////////////////////////////////////////////////////////

////////////////////////////////
//	skel_init_frametable
//
////////////////////////////////

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

	ai_set_sequence_data (self, hook->frames->atak [0], 0, "monsters/skel/skelattack1.wav", -1, NULL, 0, -1, 0, -1, FRAME_ONCE);
	ai_set_sequence_data (self, hook->frames->atak [1], 0, "monsters/skel/skelattack1.wav", -1, NULL, 0, -1, 0, -1, FRAME_ONCE);
	ai_set_sequence_data (self, hook->frames->atak [2], 0, "monsters/skel/skelattack1.wav", -1, NULL, 0, -1, 0, -1, FRAME_ONCE);
	ai_set_sequence_data (self, hook->frames->atak [3], 0, "monsters/skel/skelattack1.wav", -1, NULL, 0, -1, 0, -1, FRAME_ONCE);
	ai_set_sequence_data (self, hook->frames->atak [4], 0, "monsters/skel/skelattack1.wav", -1, NULL, 0, -1, 0, -1, FRAME_ONCE);
	ai_set_sequence_data (self, hook->frames->atak [5], 0, "monsters/skel/skelattack1.wav", -1, NULL, 0, -1, 0, -1, FRAME_ONCE);
	ai_set_sequence_data (self, hook->frames->atak [6], 0, "monsters/skel/skelattack1.wav", -1, NULL, 0, -1, 0, -1, FRAME_ONCE);
	ai_set_sequence_data (self, hook->frames->atak [7], 0, "monsters/skel/skelattack1.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);
}

////////////////////////////////
//	skel_set_attack_seq
//
////////////////////////////////

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

	i = (int) (rnd () * hook->sequence_max [SEQUENCE_ATAK] - 1);

	ai_init_sequence (self, hook->frames->atak [i], SEQUENCE_ATAK, i);
}

///////////////////////////
//	skel_attack
//
///////////////////////////

void	skel_attack (userEntity_t *self)
{
	playerHook_t	*hook = (playerHook_t *) self->userHook;
	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;
				
		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_STATIC)
	{
		hook->frame_flags -= FRAME_STATIC;

		skel_set_attack_seq (self);

		if (self->enemy->deadflag != DEAD_NO)
		{
			//	pop off everything thru TASK_CHASE and begin wandering
			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;
		}
	}

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

////////////////////////////
//	skel_begin_attack
//
////////////////////////////

void	skel_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;

	skel_set_attack_seq (self);

	skel_attack (self);
}

///////////////////////////////
//	monster_skeleton
//
//	spawns a skeleton during level load
///////////////////////////////

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

	skel_count++;

	hook = ai_init_monster (self, TYPE_SKELETON);

	self->className = "monster_skeleton";
	self->netname = "skeleton";

	gstate->Precache_Sound ("monsters/skel/skelattack1.wav");
	gstate->Precache_Sound ("monsters/skel/skelattack2.wav");
	gstate->Precache_Sound ("monsters/skel/skeldeath.wav");
	gstate->Precache_Sound ("monsters/skel/skelpain.wav");
	gstate->Precache_Sound ("monsters/skel/skelsight.wav");
	gstate->Precache_Sound ("monsters/skel/skelwander1.wav");
	gstate->Precache_Sound ("monsters/skel/skelwander2.wav");
	gstate->Precache_Sound ("monsters/skel/skelwander3.wav");

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

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

	ai_get_sequences (self);
	skel_init_frametable (self);

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

	hook->begin_attack = skel_begin_attack;

	hook->run_speed = 250;
	hook->walk_speed = 50;
	hook->attack_speed = 250;

	hook->attack_dist = 64;

	self->health = 200;
	hook->max_health = 200;
	hook->pain_chance = 10;
	self->mass = 2.0;

	self->think = ai_get_spawn_node;
	self->nextthink = gstate->time + 0.2;	// node links are set up 0.2 secs after map load

	self->translucent = 0;

	//////////////////////////////////////////////
	//	give skel its melee weapon
	//////////////////////////////////////////////
/*
	self->curWeapon = ai_weapon_create ("melee", melee_swing, ai_weapon_command);
	i = 10;
	ai_weapon_command (self->curWeapon, "base_damage", &i);
	ai_weapon_command (self->curWeapon, "rnd_damage", &i);
*/

	self->curWeapon = ai_init_weapon (self, 10, 10, 0, 0, 0, hook->attack_dist, Vector (0, 0, 0), "swing", melee_swing, WEAPON_TRACE);
//	gstate->InventoryAddItem (self->inventory, self->curWeapon);

}

