// Header File
// Created 5/23/2007; 11:31:02 PM
#include "extgraph.h"
#define MAX_SHOT_DISTANCE 6

static inline unsigned long ReturnWallDistance(FATCONFIG *fc,short x,short y)
{
							register short px = fc->cam_xpos;
  						register short py = fc->cam_ypos;
							register short ex = (((x-1)*64)+96);
							register short ey = (((y-1)*64)+96);
							register short dx = px - ex; //Must be this order
							register short dy = py - ey; //Must be this order
							long distance_squared = (long)dx*dx + (long)dy*dy;
							unsigned long distance = FAT_FastSqrt(distance_squared);
							
			return distance;
}

static inline unsigned long ReturnDistance(FATCONFIG *fc,short idx)
{
							FATSPRITE *spr = sprites+idx;
							register short px = fc->cam_xpos;
  						register short py = fc->cam_ypos;
							register short ex = spr->xpos;
							register short ey = spr->ypos;
							register short dx = px - ex; //Must be this order
							register short dy = py - ey; //Must be this order
							long distance_squared = (long)dx*dx + (long)dy*dy;
							unsigned long distance = FAT_FastSqrt(distance_squared);
							
			return distance;
}

int ShotHit(FATCONFIG *fc,FATSPRITE *sprites,TEXCONFIG *enemydeath,SpriteStruct *s)
{
	register short stepx;
	register short	stepy;
	register short x,xt;
	register short y,yt;
	register FATSPRITE *sprite asm("4") = sprites;
	register SpriteStruct *sprite_data asm("a5") = sprites_extra;
	register char* map_data asm("a6") = fc->map_data;

	short i;
	short a;
	x = fc->cam_xpos;
	y = fc->cam_ypos;
	
	stepy = ((FAT_Sin16384(fc->cam_orientation))>>8);
	stepx = ((FAT_Cos16384(fc->cam_orientation))>>8);
	
	for (a=0; a<MAX_SHOT_DISTANCE; a++)
	{	
		
		xt = (x>>6);
		yt = (y>>6);
		
		if (map_data[yt*fc->map_width + xt] == 57)
  	{	
  	   map_data[yt*fc->map_width + xt] = 25;
  	   SetupSwitchComps(fc);
  	}
		else if (map_data[yt*fc->map_width + xt] == 58)
  	{
  	   map_data[yt*fc->map_width + xt] = 26;
  	   SetupSwitchComps(fc);
  	}
	
		for (i=0; i<gg.num_sprites; i++)
		{
			if ((sprite_data[i].staticsprite != 0 && 
					sprite_data[i].staticsprite != 1 &&
					sprite_data[i].staticsprite != -1 &&
					sprite_data[i].staticsprite != -2) ||
					sprite[i].drawmode == SPRITEMODE_UNUSED ||
					sprite_data[i].life <= 0) 
					continue; // no use wasting time on something we can't kill.
					
					
				if ((xt == ((sprite[i].xpos) >> 6)) && (yt == ((sprite[i].ypos) >> 6)))
				{
					if (!RayBlocked(fc,fc->cam_xpos,fc->cam_ypos,sprite[i].xpos,sprite[i].ypos))      
  				{
  					if (sprite[i].textures != (enemydeath+3) 
  	        && sprite[i].drawmode != SPRITEMODE_UNUSED && s[i].staticsprite < 2)
  					return i;
  				}
				}
		}

    		x += stepx;
				y += stepy;
	}
	
return -5;
}


void EnemyShotHit(FATCONFIG* fc,FATSPRITE* sprites, int idx)
{
	register FATSPRITE *sprite asm("4") = sprites;
	register SpriteStruct *sprite_data asm("a5") = sprites_extra;
	
						FATSPRITE* s = sprite + idx;
						SpriteStruct *spr = sprite_data + idx;
						register short orientation;
							

						if (s->textures < spr->src+4)
  					s->textures = spr->src+4;	
							

  					 orientation = (FAT_GetArcTanYX(s->ypos - fc->cam_ypos ,s->xpos - fc->cam_xpos)) + ANGLE_180;
  					
  					 if (gg.difficulty == 1)
  					 {
  					 		if ((s->orientation <= orientation + 12) && (s->orientation >= orientation - 12))
  					 		{
  					 			UpdateDamage(fc,idx);
  					 		}
  					 }
  					 if (gg.difficulty == 2)
  					 {
  					 		if ((s->orientation <= orientation + ANGLE_015) && (s->orientation >= orientation - ANGLE_015))
  					 		{
  					 			UpdateDamage(fc,idx);
  					 		}
  					 }
  					 if (gg.difficulty == 3)
  					 {
  					 		if ((s->orientation <= orientation + ANGLE_030) && (s->orientation >= orientation - ANGLE_030))
  					 		{
  					 			UpdateDamage(fc,idx);
  					 		}
  					 }
  					 if (gg.difficulty == 4)
  					 {
  					 		if ((s->orientation <= orientation + ANGLE_045) && (s->orientation >= orientation - ANGLE_045))
  					 		{
  					 			UpdateDamage(fc,idx);
  					 		}
  					 }
  					 
  					 
}