/*********************************************************************************************/
/*                                                                                           */
/* T-Rex																	   */
/*                                                                                           */
/*********************************************************************************************/

#include "game.h"

/*********************************** TYPE DEFINITIONS ****************************************/

/* Just touching the dino results in damage per frame (trample damage is when dino hits while running) */
#define DINO_TOUCH_DAMAGE 1
#define DINO_TRAMPLE_DAMAGE 10
#define DINO_BITE_DAMAGE 10000

#define DINO_TOUCH (0x3000)

enum dino_anims {DINO_LINK, DINO_STOP, DINO_WALK, DINO_RUN, DINO_ATTACK1, DINO_DEATH, DINO_ROAR,
	DINO_ATTACK2, DINO_KILL};

#define DINO_ROAR_CHANCE 0x200

#define DINO_RUN_TURN  (ONE_DEGREE*4)
#define DINO_WALK_TURN (ONE_DEGREE*2)

#define DINO_RUN_RANGE SQUARE(WALL_L*5)
#define DINO_ATTACK_RANGE SQUARE(WALL_L*4)
#define DINO_BITE_RANGE SQUARE(1500)

#define DINO_KILL_ANIM 11

/****************** BIG BAD TREX ************************/

void DinoControl(sint16 item_number)
{
	ITEM_INFO *item;
	CREATURE_INFO *dino;
	sint16 head, angle;
	AI_INFO info;

	if (!CreatureActive(item_number))
		return;

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

	/* Has dino been killed? */
	if (item->hit_points <= 0)
	{
		if (item->current_anim_state == DINO_STOP)
			item->goal_anim_state = DINO_DEATH;
		else
			item->goal_anim_state = DINO_STOP;
	}
	else
	{
		CreatureAIInfo(item, &info);

		if (info.ahead)
			head = info.angle;

		CreatureMood(item, &info, VIOLENT);

		angle = CreatureTurn(item, dino->maximum_turn);

		/* An enemy touching the dino takes damage just from that */
		if (item->touch_bits)
			lara_item->hit_points -= (item->current_anim_state == DINO_RUN)? DINO_TRAMPLE_DAMAGE : DINO_TOUCH_DAMAGE;

		/* Flag if enemy behind and facing us - stop and turn else they can chase in rings */
		dino->flags = (dino->mood != ESCAPE_MOOD && !info.ahead &&
						 info.enemy_facing > -FRONT_ARC && info.enemy_facing < FRONT_ARC);

		/* Flag if enemy is close ahead - walk to bite range */
		if (!dino->flags && info.distance > DINO_BITE_RANGE && info.distance < DINO_ATTACK_RANGE && info.bite)
			dino->flags = 1;

		/* Decide on dino's next move */
	 	switch (item->current_anim_state)
		{
			case DINO_STOP:
				if (item->required_anim_state)
					item->goal_anim_state = item->required_anim_state;
				else if (info.distance < DINO_BITE_RANGE && info.bite)
					item->goal_anim_state = DINO_ATTACK2;
				else if (dino->mood == BORED_MOOD || dino->flags)
					item->goal_anim_state = DINO_WALK;
				else
					item->goal_anim_state = DINO_RUN;
				break;

			case DINO_WALK:
				dino->maximum_turn = DINO_WALK_TURN;

				if (dino->mood != BORED_MOOD && !dino->flags)
					item->goal_anim_state = DINO_STOP;
				else if (info.ahead && GetRandomControl() < DINO_ROAR_CHANCE)
				{
					item->required_anim_state = DINO_ROAR;
					item->goal_anim_state = DINO_STOP;
				}
				break;

			case DINO_RUN:
				dino->maximum_turn = DINO_RUN_TURN;

				if (info.distance < DINO_RUN_RANGE && info.bite)
					item->goal_anim_state = DINO_STOP;
				else if (dino->flags)
					item->goal_anim_state = DINO_STOP;
				else if (dino->mood != ESCAPE_MOOD && info.ahead && GetRandomControl() < DINO_ROAR_CHANCE)
				{
					item->required_anim_state = DINO_ROAR;
					item->goal_anim_state = DINO_STOP;
				}
				else if (dino->mood == BORED_MOOD)
					item->goal_anim_state = DINO_STOP;
				break;

			case DINO_ATTACK2:
				if (item->touch_bits & DINO_TOUCH)
				{
					lara_item->hit_points -= DINO_BITE_DAMAGE;
					lara_item->hit_status = 1;

					/* Bye, bye Lara */
					CreatureKill(item, DINO_KILL_ANIM, DINO_KILL, EXTRA_DINOKILL);
					return;
				}

				/* Make dino walk if fail to kill Lara (avoids repeat miss attack) */
				item->required_anim_state = DINO_WALK;
				break;
		}
	}

	/* Split dino's head movement between head and neck */
	CreatureHead(item, (sint16)(head>>1));
	dino->neck_rotation = dino->head_rotation;

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

