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

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

static	int	cyclops_count	= 0;

///////////////////////////////////////////////////////////////////////////////////
// cyclops specific funcitons
///////////////////////////////////////////////////////////////////////////////////

void	cyclops_attack (userEntity_t *self);

////////////////////////////////
//	cyclops_init_frametable
//
////////////////////////////////

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

	//	standing attack
	ai_set_sequence_data (self, hook->frames->atak [0], 18, "monsters/cyclops/cyclopsattack1.wav", 24, "monsters/cyclops/cyclopsattack1.wav", 0, -1, 0, -1, FRAME_ONCE);
	//	walking attack
	ai_set_sequence_data (self, hook->frames->atak [1], -1, NULL, -1, NULL, 0, -1, 0, -1, FRAME_ONCE);
	//	running attack
	ai_set_sequence_data (self, hook->frames->atak [2], -1, NULL, -1, NULL, 0, -1, 0, -1, FRAME_ONCE);
}

////////////////////////////////////////////////////////////////////////////////
//	cyclops_set_attack_seq
//
////////////////////////////////////////////////////////////////////////////////

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

	cyclops_attack (self);
}

////////////////////////////////////////////////////////////////////////////////
//	cyclops_attack
//
////////////////////////////////////////////////////////////////////////////////

void	cyclops_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;
					
			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;
			}

			cyclops_set_attack_seq (self);
		}
	}

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

////////////////////////////////////////////////////////////////////////////////
//	cyclops_begin_attack
//
////////////////////////////////////////////////////////////////////////////////

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

	cyclops_set_attack_seq (self);
}

///////////////////////////////////////////////////////////////////////////////
//	monster_cyclops
//
//	spawns a skeleton during level load
///////////////////////////////////////////////////////////////////////////////

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

	cyclops_count++;

	hook = ai_init_monster (self, TYPE_CYCLOPS);

	self->className = "monster_cyclops";
	self->netname = "cyclops";

	gstate->SoundIndex ("monsters/cyclops/cyclopsattack1.wav");

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

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

	ai_get_sequences (self);
	cyclops_init_frametable (self);

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

	hook->begin_attack = cyclops_begin_attack;

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

	hook->attack_dist = 128;

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

	self->curWeapon = ai_init_weapon (self, 10, 30, 0, 0, 0, 108, Vector (0, 0, 0), "punch", melee_punch, WEAPON_TRACE);
}

