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

#include "objects.h"
#include "lara.h"
#include "laraanim.h"
#include "effect2.h"
#include "control.h"

extern long wibble;
extern short rcossin_tbl[];

void ControlArea51Laser(sint16 item_number);
void InitialiseArea51Laser(sint16 item_number);

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

void *func[] = {
	&ControlArea51Laser,  	// Main control for rotatey lasers.
	&InitialiseArea51Laser,  	// Main control for rotatey lasers.
};
#endif

enum laser_anims {	LASER_ROTATE	};

void ControlArea51Laser(sint16 item_number)
{
	ITEM_INFO *item;
	FLOOR_INFO *floor;
	long	destx,destz,dx,dz,rad,r,g,b;
	sint16 room_number;

	item = &items[item_number];

	item->current_anim_state = LASER_ROTATE;

	rad = (GetRandomControl()&1)+8;
	r = (GetRandomControl()&3)+24;
	g = (GetRandomControl()&3);
	b = (GetRandomControl()&1);

	TriggerDynamic(item->pos.x_pos,item->pos.y_pos-64,item->pos.z_pos,rad,r,g,b);
  	item->mesh_bits = 0xffffffff - (GetRandomControl()&20);	// Randomly turn off the lasers.

	destx = (item->item_flags[1] & 255) << 9;
	destz = ((item->item_flags[1] >> 8) & 255) << 9;

	dx = destx - item->pos.x_pos;
	dz = destz - item->pos.z_pos;

	if (abs(dx) < 768 && abs(dz) < 768)	// Reached destination ?
	{
		dx = ((item->item_flags[0] & 255) << 9) + ((item->item_flags[2] * -2560 * phd_sin(item->pos.y_rot)) >> W2V_SHIFT);
		dz = (((item->item_flags[0] >> 8) & 255) << 9) + ((item->item_flags[2] * -2560 * phd_cos(item->pos.y_rot)) >> W2V_SHIFT);
		item->item_flags[1] = ((dx>>9)&255) | (((dz>>9)&255)<<8);	// Setup X & Z destination.
		item->shadeB = 32;	// Setup stop timer.
	}


  	if (item->item_flags[2] == 1)
	{
		if (item->shadeB)	// Reached destination and slowing down ?
		{
			if (item->item_flags[3] == 0)
			{
				item->shadeB--;
				if (item->shadeB == 0)
					item->item_flags[2] = -item->item_flags[2];	// Change direction.
			}
			else if (item->item_flags[3] <= 4)
				item->item_flags[3] = 0;
			else
				item->item_flags[3] -= item->item_flags[3]>>2;
		}
		else
		{
			item->item_flags[3]+=5;
			if (item->item_flags[3] > 512)
				item->item_flags[3] = 512;
		}
	}
	else
	{
		if (item->shadeB)	// Reached destination and slowing down ?
		{
			if (item->item_flags[3] == 0)
			{
				item->shadeB--;
				if (item->shadeB == 0)
					item->item_flags[2] = -item->item_flags[2];	// Change direction.
			}
			else if (item->item_flags[3] >= -4)
				item->item_flags[3] = 0;
			else
				item->item_flags[3] -= item->item_flags[3]>>2;
		}
		else
		{
			item->item_flags[3]-=5;
			if (item->item_flags[3] < -512)
				item->item_flags[3] = -512;
		}
	}

	item->pos.z_pos += (item->item_flags[3] * phd_cos(item->pos.y_rot)) >> (W2V_SHIFT+2);
	item->pos.x_pos += (item->item_flags[3] * phd_sin(item->pos.y_rot)) >> (W2V_SHIFT+2);

	room_number = item->room_number;
	floor = GetFloor(item->pos.x_pos, item->pos.y_pos, item->pos.z_pos, &room_number);
	if (room_number != item->room_number)
		ItemNewRoom(item_number, room_number);

	if ( TestBoundsCollide( item, lara_item, 64) )
	{
	 	lara_item->hit_points-=25;
	 	DoLotsOfBloodD( lara_item->pos.x_pos, item->pos.y_pos-(GetRandomControl()&255)-32, lara_item->pos.z_pos, (GetRandomControl()&127)+128, GetRandomControl()<<1, lara_item->room_number , 3);
	}

	AnimateItem(item);
}


void InitialiseArea51Laser(sint16 item_number)
{
	ITEM_INFO *item;
	long		dx,dz;

	item = &items[item_number];

	dx = item->pos.x_pos + ((2560*phd_sin(item->pos.y_rot)) >> W2V_SHIFT);
	dz = item->pos.z_pos + ((2560*phd_cos(item->pos.y_rot)) >> W2V_SHIFT);

	item->item_flags[0] = ((item->pos.x_pos>>9)&255) | (((item->pos.z_pos>>9)&255)<<8);	// Setup X & Z origin.
	item->item_flags[1] = ((dx>>9)&255) | (((dz>>9)&255)<<8);	// Setup X & Z destination.
	item->item_flags[2] = 1;	// Next destination. 1 = with y_rot, -1 = against y_rot.
	item->item_flags[3] = 0;	// Speed, because, get this, you CAN'T dynamically change the 'speed' field of animating items!!! Ha,Ha...
	item->shadeB = 0;	// Clear stop timer.
}

