#ifdef PSX_VERSION
#include "../spec_psx/typedefs.h"
#else
#include "../specific/stypes.h"
#endif

#include "objects.h"
#include "lara.h"
#include "sound.h"


void InitialiseCobra(sint16 item_number);
void CobraControl(sint16 item_number);

#if defined(PSX_VERSION) && defined(RELOC)

void *func[] = {
	&InitialiseCobra,
	&CobraControl,
};

#endif

/*********************************** TYPE DEFINITIONS ****************************************/
//Cobra - coded by TS 13-7-98


#define COBRA_DAMAGE 80

enum cobra_anims {COBRA_RISE, COBRA_WAIT, COBRA_STRIKE, COBRA_DOWN, COBRA_DEATH};

#define COBRA_TURN (ONE_DEGREE*10)

#define COBRA_FORGET_RADIUS SQUARE(WALL_L*3)
#define COBRA_OUTER_RADIUS SQUARE(WALL_L*3/2)
#define COBRA_INNER_RADIUS SQUARE(WALL_L)

#define COBRA_UPSET_SPEED 15
#define COBRA_HIT_RANGE (STEP_L*2)

#define COBRA_TOUCH 0x2000

#define COBRA_DIE_ANIM 4
#define COBRA_DOWN_ANIM 2

BITE_INFO cobra_hit = {0,0,0,13};

void InitialiseCobra(sint16 item_number)
{
	/* Cobra wants to start in wait anim */
	ITEM_INFO *item;

	InitialiseCreature(item_number);

	item = &items[item_number];
	item->anim_number = objects[item->object_number].anim_index + COBRA_DOWN_ANIM;
	item->frame_number = anims[item->anim_number].frame_base + 45; //Actually coiled up
	item->current_anim_state = item->goal_anim_state = COBRA_DOWN;

}

void CobraControl(sint16 item_number)
{
	ITEM_INFO *item, *enemy;
	CREATURE_INFO *cobra;
	sint16 angle, head, tilt, random;
	AI_INFO info;
	char	buf[80];

	if (!CreatureActive(item_number))
		return;

	item = &items[item_number];
	cobra = (CREATURE_INFO *)item->data;
	head = angle = tilt = 0;

	if (item->hit_points <= 0)
	{
		if (item->current_anim_state != COBRA_DEATH)
		{
			item->anim_number = objects[item->object_number].anim_index + COBRA_DIE_ANIM;
			item->frame_number = anims[item->anim_number].frame_base;
			item->current_anim_state = COBRA_DEATH;
		}
	}
	else
	{
		CreatureAIInfo(item, &info);

		info.angle += 0xC00; // cos origin is offset from head
//		GetCreatureMood(item, &info, VIOLENT);
//		CreatureMood(item, &info, VIOLENT);
		cobra->target.x = lara_item->pos.x_pos;
		cobra->target.z = lara_item->pos.z_pos;

		angle = CreatureTurn(item, cobra->maximum_turn);
		if (info.ahead)
			head = info.angle;
/*
		if (cobra->mood == BORED_MOOD)
			PrintDbug(2, 3, "Bored");
		else if (cobra->mood == ESCAPE_MOOD)
			PrintDbug(2, 3, "Escape");
		else if (cobra->mood == ATTACK_MOOD)
			PrintDbug(2, 3, "Attack");
		else if (cobra->mood == STALK_MOOD)
			PrintDbug(2, 3, "Stalk");

*/
	/*
		if (item->current_anim_state == COBRA_RISE)
			PrintDbug(2, 3, "RISE");
		else if (item->current_anim_state == COBRA_WAIT)
			PrintDbug(2, 3, "WAIT");
		else if (item->current_anim_state == COBRA_STRIKE)
			PrintDbug(2, 3, "STRIKE");
		else if (item->current_anim_state == COBRA_DOWN)
			PrintDbug(2, 3, "DOWN");
		else if (item->current_anim_state == COBRA_DEATH)
			PrintDbug(2, 3, "DEATH");
	*/

		if (abs(info.angle) <  COBRA_TURN)
			item->pos.y_rot += info.angle;
		else if (info.angle < 0) 
			item->pos.y_rot -= COBRA_TURN;
		else 
			item->pos.y_rot += COBRA_TURN;

		switch (item->current_anim_state)
		{
			case COBRA_WAIT:	// Ready for action - make it targetable
				cobra->flags = 0;
				objects[item->object_number].intelligent = 1;

				if (info.distance > COBRA_FORGET_RADIUS)
					item->goal_anim_state = COBRA_DOWN;
				else if ((lara_item->hit_points > 0) && ((info.ahead && info.distance < COBRA_INNER_RADIUS) || item->hit_status || (lara_item->speed > COBRA_UPSET_SPEED)))
					item->goal_anim_state = COBRA_STRIKE;
				break;

			case COBRA_DOWN:	// Coiled - make it untargetable
				cobra->flags = 0;
				objects[item->object_number].intelligent = 0;


				if (info.distance < COBRA_OUTER_RADIUS && lara_item->hit_points > 0)
				{
					item->goal_anim_state = COBRA_RISE;
					objects[item->object_number].intelligent = 1;
				}
				break;

			case COBRA_STRIKE:

					if (cobra->flags != 1 && (item->touch_bits & COBRA_TOUCH))// &&
//						cobra->flags >= cobra_hitframes[item->current_anim_state][0] &&
//						cobra->flags <= cobra_hitframes[item->current_anim_state][1])
					{
						cobra->flags = 1;
						lara_item->hit_points -= COBRA_DAMAGE;
						lara_item->hit_status = 1;
						lara.poisoned = 0x100;	// Set to max poisoning.

						SoundEffect(245, &item->pos, 0);
						CreatureEffect(item, &cobra_hit, DoBloodSplat);
					}

				break;

		}
	}

//	CreatureTilt(item, tilt);
//	CreatureJoint(item, 0, head>>1);
//	CreatureJoint(item, 1, head>>1);

	/* Actually do animation allowing for collisions */
	CreatureAnimation(item_number, angle + 0x2000, 0);
}
