void() LightBombTouch;
void() LightBombExplode;
void() LightBombTimer;
void(entity inflictor, entity attacker, float bounce, entity ignore) T_RadiusLight;
void() ThrowLightBomb =
{
        local   entity missile, mpuff;
        self.currentammo = self.ammo_rockets = self.ammo_rockets - 1;
        sound (self, CHAN_WEAPON, "weapons/grenade.wav", 1, ATTN_NORM);
        self.punchangle_x = -2;
        missile = spawn ();
        missile.owner = self;
        missile.movetype = MOVETYPE_BOUNCE;
        missile.solid = SOLID_BBOX;
        missile.classname = "grenade";
        makevectors (self.v_angle);
        if (self.v_angle_x)
                missile.velocity = v_forward*600 + v_up * 200 + crandom()*v_right*10 + crandom()*v_up*10;
        else
        {
                missile.velocity = aim(self, 100);
                missile.velocity = missile.velocity * 60;
                missile.velocity_z = 200;
        }
        missile.avelocity = '300 300 300';
        missile.angles = vectoangles(missile.velocity);
        missile.touch = LightBombTouch;
        missile.nextthink = time + 1.5;
        missile.think = LightBombExplode;
        setmodel (missile, "progs/nbomb.mdl");
        setsize (missile, '0 0 0', '0 0 0');
        setorigin (missile, self.origin);
};
void() LightBombTouch =
{
        if (other == self.owner)
                return;
        if (other.takedamage == DAMAGE_AIM)
        {
                LightBombExplode();
                return;
        }
        sound (self, CHAN_WEAPON, "weapons/bounce.wav", 1, ATTN_NORM);
        if (self.velocity == '0 0 0')
                self.avelocity = '0 0 0';

};
void() LightBombExplode =
{
        T_RadiusLight (self, self.owner, 240, world);
        WriteByte (MSG_BROADCAST, SVC_TEMPENTITY);
        WriteByte (MSG_BROADCAST, TE_EXPLOSION);
        WriteCoord (MSG_BROADCAST, self.origin_x);
        WriteCoord (MSG_BROADCAST, self.origin_y);
        WriteCoord (MSG_BROADCAST, self.origin_z);
        BecomeExplosion ();
};
void() LightBombTimer =
{
        if (self.health <= 0)
        {
                remove(self);
                return;
        }
        else
        {
        stuffcmd(self.owner, "gamma 0.01\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "gamma 0.02\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "gamma 0.04\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "gamma 0.06\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "gamma 0.08\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "gamma 0.1\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "gamma 0.12\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "gamma 0.14\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "gamma 0.16\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "gamma 0.18\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "gamma 0.2\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "gamma 0.22\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "gamma 0.24\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "gamma 0.26\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "gamma 0.28\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "gamma 0.3\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "gamma 0.32\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "gamma 0.34\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "gamma 0.36\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "gamma 0.38\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "gamma 0.4\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "gamma 0.42\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "gamma 0.44\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "gamma 0.46\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "gamma 0.48\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "gamma 0.50\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "gamma 0.52\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "gamma 0.54\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "gamma 0.56\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "gamma 0.58\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "gamma 0.60\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "gamma 0.62\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "gamma 0.64\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "gamma 0.66\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "gamma 0.68\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "gamma 0.70\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "gamma 0.72\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "gamma 0.74\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "gamma 0.76\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "gamma 0.78\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "gamma 0.80\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "gamma 0.82\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "gamma 0.84\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "gamma 0.86\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "gamma 0.88\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "gamma 0.90\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "gamma 0.92\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "gamma 0.94\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "gamma 0.96\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "gamma 0.98\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "wait\n");
        stuffcmd(self.owner, "gamma 1\n");
        }
};


void(entity inflictor, entity attacker, float bounce, entity ignore) T_RadiusLight =
{
        local   float   points;
        local   entity  head, timer;
        local   vector  org;
        local   string  st;

        head = findradius(inflictor.origin, bounce+40);

        while (head)
        {
                if (head != ignore)
                {
                        if (head.takedamage)
                        {
                                org = head.origin + (head.mins + head.maxs)*0.5;
                                points = 0.5*vlen (org - inflictor.origin);
                                if (points < 0)
                                        points = 0;
                                points = bounce - points;
                                if (points > 0)
                                {
                                        head.velocity = org - inflictor.origin;
                                        head.velocity = head.velocity * (points / 10);

                                        if (head.classname != "player")
                                        {
                                                if(head.flags & FL_ONGROUND)
                                                        head.flags = head.flags - FL_ONGROUND;
                                        }
                                        else
                                        {
                                                stuffcmd(head,"gamma 0.001\n");
                                                timer = spawn();
                                                timer.nextthink = time + 15;
                                                timer.think = LightBombTimer;
                                                timer.owner = head;
                                                timer.health = 100;
                                        }
                                }
                        }
                }
                head = head.chain;
        }
};