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

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

static	int	dwarf_count	= 0;

///////////////////////////////////////////////////////////////////////////////////
// dwarf specific funcitons
///////////////////////////////////////////////////////////////////////////////////

void	dwarf_attack (userEntity_t *self);

///////////////////////////////////////////////////////////////////////////////////
//	dwarf_init_frametable
//
///////////////////////////////////////////////////////////////////////////////////

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

//	single swipe
	ai_set_sequence_data (self, hook->frames->atak [0], 12, "monsters/dwarf/dwarfswing.wav", -1, NULL, 0, -1, 0, -1, FRAME_ONCE);
}

////////////////////////////////////////////////////////////////////////////////
//	dwarf_set_attack_seq
//
////////////////////////////////////////////////////////////////////////////////

void	dwarf_set_attack_seq (userEntity_t *self)
{
	playerHook_t	*hook = (playerHook_t *) self->userHook;
	int				i;
//	float			dist;

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

	i = 0;
	ai_init_sequence (self, hook->frames->atak [i], SEQUENCE_ATAK, i);
	hook->frameInc = 1;
	hook->frameTime = 0.035;

	dwarf_attack (self);
}

////////////////////////////////////////////////////////////////////////////////
//	dwarf_attack
//
////////////////////////////////////////////////////////////////////////////////

void	dwarf_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);

	// 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;

			gstate->sound (self, CHAN_AUTO, hook->sound1, 128, ATTN_NORM);
		}
		
		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;
			}

			dwarf_set_attack_seq (self);
		}
	}

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

////////////////////////////////////////////////////////////////////////////////
//	dwarf_begin_attack
//
////////////////////////////////////////////////////////////////////////////////

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

	com->ZeroVector (self->velocity);

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

	dwarf_set_attack_seq (self);
}

///////////////////////////////////////////////////////////////////////////////
//	monster_dwarf
//
//	spawns a skeleton during level load
///////////////////////////////////////////////////////////////////////////////

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

	dwarf_count++;

	hook = ai_init_monster (self, TYPE_DWARF);

	self->className = "monster_dwarf";
	self->netname = "dwarf";

	gstate->SoundIndex ("monsters/dwarf/dwarfattack1.wav");
	gstate->SoundIndex ("monsters/dwarf/dwarfattack2.wav");
	gstate->SoundIndex ("monsters/dwarf/dwarfdeath1.wav");
	gstate->SoundIndex ("monsters/dwarf/dwarfsight1.wav");
	gstate->SoundIndex ("monsters/dwarf/dwarfsight2.wav");
	gstate->SoundIndex ("monsters/dwarf/dwarfwander1.wav");
	gstate->SoundIndex ("monsters/dwarf/dwarfwander2.wav");

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

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

	ai_get_sequences (self);
	dwarf_init_frametable (self);

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

	hook->begin_attack = dwarf_begin_attack;

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

	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 dwarf a weapon
	///////////////////////////////////////////////////////////////////////////

	self->curWeapon = ai_init_weapon (self, 7, 15, 0, 0, 0, hook->attack_dist, Vector (0, 0, 0), "punch", melee_punch, WEAPON_TRACE);
}

