/*

 LavaGrenade 1.0 Copyright 1996 Niklas Angare (NiAn on undernet) angare@kuai.se.

 This code is released for all to learn from, but please do not abuse my
 goodwill by copying large chunks of it. If you want to distribute my code as a
 part of a package, ask me first!

 Thanks to ReVeNaNT for the idea and a tiny part of the code.

 This is a part of PLUSGrenade.

*/

//=============================================================================

void() BecomeExplosion;
void() W_FireGrenade;
float() crandom;

void() lavaball_touch =
{
	if(!(other.classname == "fireball")) {		//Don't explode if it touches another lavaball
		T_MissileTouch();
	}
};

// This is called when they're going to explode in mid air. It pretends that a wall was hit.
void() lavaball_think =
{
	other = world;
	lavaball_touch();
};

// This spawns a lavaball!
void() lavaball_spawn =
{
        local entity lavaball;   

        lavaball=spawn();
        lavaball.solid = SOLID_TRIGGER;
        lavaball.movetype = MOVETYPE_TOSS;
        lavaball.velocity = '0 0 1000';
        lavaball.velocity_x = (random() * 500) - 250;
        lavaball.velocity_y = (random() * 500) - 250;
        lavaball.velocity_z = 200 + (random() * 500);
	lavaball.classname = "fireball";
	setmodel (lavaball, "progs/lavaball.mdl");
	setsize (lavaball, '0 0 0', '0 0 0');		// not sure what this does
        setorigin(lavaball, self.origin);
	lavaball.owner = self.owner;
	lavaball.touch = lavaball_touch;
	lavaball.think = lavaball_think;		// Makes the balls explode in mid air
	lavaball.nextthink = time + 0.5 + random() * 1.0;// after between 0.5 and 1.5 secs.
};

void() LavaGrenadeExplode =
{
	T_RadiusDamage (self, self.owner, 120, 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);

	local	float	lavaball_counter;
	lavaball_counter = self.owner.lavaball_amount;
	while(lavaball_counter) {
		lavaball_spawn();
		lavaball_counter = lavaball_counter - 1;
	}

	BecomeExplosion ();

};

void() LavaGrenadeTouch =
{
	if (other == self.owner)
		return;		// don't explode on owner
	if (other.takedamage == DAMAGE_AIM)
	{
		LavaGrenadeExplode();
		return;
	}
	sound (self, CHAN_WEAPON, "weapons/bounce.wav", 1, ATTN_NORM);	// bounce sound
	if (self.velocity == '0 0 0')
		self.avelocity = '0 0 0';
};

/*
================
W_FireLavaGrenade
================
*/
void() W_FireLavaGrenade =
{
	local	entity missile, mpuff;
	
	if(!self.lavaball_set)
		self.lavaball_amount = 5;			// 5 balls is the default

	if(self.ammo_cells < self.lavaball_amount * 3) {
		sprint(self, "ammo low\n");
		W_FireGrenade();
		return;
	}
	self.ammo_cells = self.ammo_cells - self.lavaball_amount * 3;

	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";
		
// set missile speed	

	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, 10000);
		missile.velocity = missile.velocity * 600;
		missile.velocity_z = 200;
	}

	missile.avelocity = '300 300 300';

	missile.angles = vectoangles(missile.velocity);
	
	missile.touch = LavaGrenadeTouch;
	
// set missile duration
	missile.nextthink = time + 2.5;
	missile.think = LavaGrenadeExplode;

	setmodel (missile, "progs/grenade.mdl");
//	setmodel (missile, "progs/lavagren.mdl");
	setsize (missile, '0 0 0', '0 0 0');		
	setorigin (missile, self.origin);
};


