////////////////////////////////////////////////////////////////////
//	Lab Worker
//
//	sprays a poison mist, can strafe and attack while running
////////////////////////////////////////////////////////////////////

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


////////////////////////////////
//	labworker_init_frametable
//
////////////////////////////////

void	labworker_init_frametable (userEntity_t *self)
{
	playerHook_t	*hook = (playerHook_t *) self->userHook;
	
	//	standing attack
	ai_set_sequence_data (self, hook->frames->atak [0], 0, "monsters/labw/labwattack1.wav", -1, NULL, 0, -1, 0, -1, FRAME_ONCE);
	//	walking attack
	ai_set_sequence_data (self, hook->frames->atak [1], 0, "monsters/labw/labwattack1.wav", -1, NULL, SEQUENCE_TRANS, 0, SEQUENCE_TRANS, 1, FRAME_ONCE);
	//	running attack
	ai_set_sequence_data (self, hook->frames->atak [2], 0, "monsters/labw/labwattack1.wav", -1, NULL, SEQUENCE_TRANS, 0, SEQUENCE_TRANS, 1, FRAME_ONCE);
	//	strafing left attack
	ai_set_sequence_data (self, hook->frames->atak [3], 0, "monsters/labw/labwattack1.wav", -1, NULL, SEQUENCE_TRANS, 0, SEQUENCE_TRANS, 1, FRAME_ONCE);
	//	strafing right attack
	ai_set_sequence_data (self, hook->frames->atak [4], 0, "monsters/labw/labwattack1.wav", -1, NULL, SEQUENCE_TRANS, 0, SEQUENCE_TRANS, 1, FRAME_ONCE);

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

////////////////////////////////
//	labworker_set_attack_seq
//
////////////////////////////////

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

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

///////////////////////////
//	labworker_attack
//
///////////////////////////

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

	//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;
	
		ai_fire_curWeapon (self, Vector (self->s.origin), Vector (self->enemy->s.origin), 0, 0);
	}

	// attack without any transitions
	if (hook->frame_flags & FRAME_STATIC)
	{
		hook->frame_flags -= FRAME_STATIC;	

		if (self->enemy->deadflag != DEAD_NO)
		{
			//	pop off everything thru TASK_CHASE for this enemy
			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;
		}
		else
		{
			//	finished one attack, decide what to do next
			labworker_set_attack_seq (self);
		}
	}

	ai_attack_move (self);

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

////////////////////////////
//	labworker_begin_attack
//
////////////////////////////

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

	labworker_set_attack_seq (self);

	labworker_attack (self);
}

///////////////////////////////
//	monster_labworker
//
//	spawns a labworker during level load
///////////////////////////////

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

	hook = ai_init_monster (self, TYPE_LABWORKER);

	self->className = "monster_labworker";
	self->netname = "lab labworker";
/*
	gstate->Precache_Sound ("monsters/labw/labwattack1.wav");
	gstate->Precache_Sound ("monsters/labw/labwattack2.wav");
	gstate->Precache_Sound ("monsters/labw/labwdeath1.wav");
	gstate->Precache_Sound ("monsters/labw/labwdeath2.wav");
	gstate->Precache_Sound ("monsters/labw/labwpain1.wav");
	gstate->Precache_Sound ("monsters/labw/labwpain2.wav");
	gstate->Precache_Sound ("monsters/labw/labwsight1.wav");
	gstate->Precache_Sound ("monsters/labw/labwsight2.wav");
	gstate->Precache_Sound ("monsters/labw/labwwander1.wav");
	gstate->Precache_Sound ("monsters/labw/labwwander2.wav");
*/
	gstate->Precache_Sound ("monsters/labw/labwscream1.wav");
	gstate->Precache_Sound ("monsters/labw/labwattack1.wav");

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

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

	ai_get_sequences (self);

	labworker_init_frametable (self);

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

	hook->begin_attack = labworker_begin_attack;

	hook->run_speed = 200;
	hook->walk_speed = 55;
	hook->attack_dist = 256;

	self->health = 75;
	hook->max_health = 100;
	hook->pain_chance = 25;
	self->mass = 1.5;
	self->gravity = 1.0;
	self->ang_speed [YAW] = 150;

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

	self->translucent = 0;

	//////////////////////////////////////////////
	//	give labworker its melee weapon
	//////////////////////////////////////////////
/*
	self->curWeapon = ai_weapon_create ("poison mist", melee_punch, 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, 4, 10, 0, 0, 0, hook->attack_dist, Vector (0, 0, 0), "punch", melee_punch, WEAPON_TRACE);
//	gstate->InventoryAddItem (self->inventory, self->curWeapon);
}


