//------------------------------------------------------------------------------------
// Flashbombs
//------------------------------------------------------------------------------------

void(entity own) RemoveOldFt =
{
	local entity e, old;
	old = world;
	e = find(world, classname, "flashtimer");
	while(e)
	{
		if(e.owner == own)
		{
			old = e;
		}
		e = find(e, classname, "flashtimer");
		if(old != world)
		{
			remove(old);
			old = world;
		}
	}
};
void() FlashtimerThink =
{
	local float intense;
        local string si;

	if(self.owner.health < 1)
	{
		remove(self);
		return;
	}

	if (self.count > (self.jumpheight + self.ninja))	// fading out
		intense = (FLASH_INTENSITY / self.jumptime) * (self.ltime - self.count);
	else if (self.count > self.jumpheight)			// full intense
		intense = FLASH_INTENSITY;
	else if(self.count <= self.jumpheight)			// fading in
		intense = FLASH_INTENSITY / self.jumpheight * self.count;

	if(intense > 255)
		intense = 255;
	if(intense < 0)
		intense = 0;
        intense = ceil(intense);
        si = ftos(intense);
        stuffcmd(self.owner,"v_cshift ");
        stuffcmd(self.owner, FLASH_COLOR);
        stuffcmd(self.owner," ");
        stuffcmd(self.owner, si);
	stuffcmd(self.owner, "\n");

	self.count = self.count + 1;
	if(self.count > self.ltime)
	{
		stuffcmd(self.owner,"v_cshift 0 0 0 0\n");
		remove(self);
		return;
	}
	self.nextthink = time + FLASH_SMOOTHNESS;
};
	
void(entity e) Flash =
{
        local   entity flashtimer;

	RemoveOldFt (e);
	flashtimer = spawn();
	flashtimer.classname = "flashtimer";
	flashtimer.owner = e;
	flashtimer.ltime = (FLASH_FADEIN_TIME + FLASH_FADEOUT_TIME + FLASH_TIME) / FLASH_SMOOTHNESS;
	flashtimer.count = 0;
	flashtimer.think = FlashtimerThink;
	flashtimer.nextthink = time;
	flashtimer.jumpheight = FLASH_FADEIN_TIME / FLASH_SMOOTHNESS;  //fadein steps
	flashtimer.ninja = FLASH_TIME / FLASH_SMOOTHNESS;  //full intense steps
	flashtimer.jumptime = FLASH_FADEOUT_TIME / FLASH_SMOOTHNESS;   //fadeout steps
};

void() W_Flashbomb = 
{
	local entity e, oself;

	if(!self.items & IT_SUPER_SHOTGUN)
		return;
	if(self.ammo_rockets < 1)
		return;

	sound (self, CHAN_WEAPON, "weapons/flash.wav", 1, ATTN_NORM);
	e = world;
	e = find(world, classname, "player");
	while(e != world)
	{
		if(e != self)
		{
			oself = self;
			self = e;
			if((infront(oself)) && (visible(oself)))
			{
				centerprint(self,"You are dazzled");
				Flash(self);
			}
			self = oself;
		}
		e = find(e, classname, "player");
		
	}
	sprint(self, "You used a Flashbomb.\n");
	self.ammo_rockets = self.ammo_rockets - 1;
	if(self.ammo_rockets < 1)
		self.items = self.items - (self.items & IT_SUPER_SHOTGUN);
};

