/*********************************************************************************************/
/*                                                                                           */
/* Target Trap Control - TS  - 30/9/98                                                     */
/*                                                                                           */
/*********************************************************************************************/

/*---------------------------------------------------------------------------
 *	Import Headers
\*--------------------------------------------------------------------------*/

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

#include "objects.h"
#include "items.h"
#include "box.h"
#include "lara.h"
#include "laraanim.h"
#include "control.h"
#include "people.h"
#include "traps.h"
#include "missile.h"
//#include "sphere.h"
//#include "effect2.h"
extern int assault_targets;

void InitialiseTarget(sint16 item_number);
void TargetControl(sint16 item_number);

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

void *func[] __attribute__((section(".header"))) = {
	&InitialiseTarget,
	&TargetControl,
};

#endif

/*---------------------------------------------------------------------------
 *	Constants
\*--------------------------------------------------------------------------*/


#define TARGET_UP_TIME 200
#define TARGET_DROP_SPEED (ONE_DEGREE*2)
enum target_anims{TARGET_RISE, TARGET_HIT1, TARGET_HIT2, TARGET_HIT3};
#define HIT1_HP 12
#define HIT2_HP 8
#define HIT3_HP 4

/*---------------------------------------------------------------------------
 *	Externals
\*--------------------------------------------------------------------------*/
//#define DEBUG_TARGET

#ifdef DEBUG_TARGET
extern char exit_message[];
#endif

#ifdef DEBUG_TARGET
static char *TargetStrings[] = {"RISE", "HIT1", "HIT2", "HIT3"};
#endif

/*---------------------------------------------------------------------------
 *	Public Functions
\*--------------------------------------------------------------------------*/
void InitialiseTarget(sint16 item_number)
{
	InitialiseCreature(item_number);
	items[item_number].status = NOT_ACTIVE;
}


void TargetControl(sint16 item_number)
{
	ITEM_INFO *item;
	CREATURE_INFO *target;

	item = &items[item_number];

//	if (item->status == INVISIBLE && item->hit_points != DONT_TARGET)
//		item->status = NOT_ACTIVE;
	if (!CreatureActive(item_number))
		return;

	target = (CREATURE_INFO *)item->data;

	switch (item->current_anim_state)
	{
		case TARGET_RISE:
			if (item->hit_points < HIT1_HP && item->hit_points != DONT_TARGET)
			{
				item->hit_points = HIT1_HP;
				item->goal_anim_state = TARGET_HIT1;
			}
			break;
		case TARGET_HIT1:
			if (item->hit_points < HIT2_HP && item->hit_points != DONT_TARGET)
			{
				item->hit_points = HIT2_HP;
				item->goal_anim_state = TARGET_HIT2;
			}
			break;

		case TARGET_HIT2:
			if (item->hit_points < HIT3_HP && item->hit_points != DONT_TARGET)
			{
				item->hit_points = HIT3_HP;
				item->goal_anim_state = TARGET_HIT3;
			}
			break;

		case TARGET_HIT3:
			if (item->hit_points <= 0 && item->hit_points != DONT_TARGET)
			{
	//			printf("Got here!\n");
				DisableBaddieAI(item_number);
				item->status = DEACTIVATED;
				item->flags |= ONESHOT;
			}
			break;
	}

	item->timer++;
	if (item->timer > TARGET_UP_TIME && item->timer < TARGET_UP_TIME+38)
	{
		item->hit_points = DONT_TARGET;
		item->pos.x_rot += TARGET_DROP_SPEED;
	}
	else if (item->timer >= TARGET_UP_TIME+38)
	{
		DisableBaddieAI(item_number);
		item->status = DEACTIVATED;
		item->flags |= ONESHOT;
	}

#ifdef DEBUG_TARGET
	sprintf(exit_message, "Hit Points: %d", item->hit_points);
	PrintDbug(2, 2, exit_message);
//	sprintf(exit_message, "Mood: %d", cobra->mood);
//	PrintDbug(2, 2, exit_message);
	sprintf(exit_message, "%s", TargetStrings[item->current_anim_state]);
	PrintDbug(2, 3, exit_message);
	sprintf(exit_message, "%s", TargetStrings[item->goal_anim_state]);
	PrintDbug(2, 4, exit_message);
#endif

	AnimateItem(item);

}

