//------------------------------------------------------------------------------------
// Wire trap
//------------------------------------------------------------------------------------

void(vector org, vector vel, float stime) mparticle =
{
	local entity e;
	local float r;

	e = spawn();
	e.movetype = MOVETYPE_NOCLIP;
	e.solid = SOLID_TRIGGER;
	e.velocity = vel;
	e.angles = vectoangles(vel);
	r = random();
	if(r > 0.6)
		e.frame = 2;
	else if(r > 0.3)
		e.frame = 1;
	else
		e.frame = 0;
	setsize(e, '-5 -5 -5', '5 5 5');
	setorigin(e, org);
	setmodel(e, "progs/particle.mdl");
	e.touch = SUB_Remove;
	e.nextthink = time + stime;
	e.think = SUB_Remove;
};

void() TrapThink =
{
	local entity t;
	local vector org;
	local float zone, dir, r;

	if(self.ltime < time)
	{
		centerprint(self.enemy, "Your trap disappeared.\n");
		self.classname = "disappeared trap";
		remove(self);
		return;
	}
	traceline(self.origin, self.origin + self.angles*TRAP_RANGE, FALSE, self);
	if(random() > 0.4)
	{
		if(random() > 0.5)
			dir = 1;
		else
			dir = -1;
		if(trace_fraction > 0.6)
		{
			r = random();
			if(r > 0.9)
				r = 0.9;
			else if(r < 0.1)
				r = 0.1;
			org = self.origin + r*(trace_fraction*TRAP_RANGE)*self.angles;
			mparticle(org, '0 0 0' + dir*self.angles*TRAP_BLINKSPEED, 0.4);
		}
		if(trace_fraction > 0.3)
		{
			r = random();
			if(r > 0.9)
				r = 0.9;
			else if(r < 0.1)
				r = 0.1;
			org = self.origin + r*(trace_fraction*TRAP_RANGE)*self.angles;
			mparticle(org, '0 0 0' + dir*self.angles*TRAP_BLINKSPEED, 0.4);
		}
		r = random();
		if(r > 0.9)
			r = 0.9;
		else if(r < 0.1)
			r = 0.1;
		org = self.origin + r*(trace_fraction*TRAP_RANGE)*self.angles;
		mparticle(org, '0 0 0' + dir*self.angles*TRAP_BLINKSPEED, 0.4);
	}
	if(trace_fraction == 1.0)
		return;
	if(trace_ent.takedamage)
	{
		t = spawn();
		setorigin(t, trace_endpos + self.angles*32 + '0 0 -10');
		zone = DetermineZone(t, trace_ent.origin);
		T_Damage(trace_ent, t, self.enemy, TRAP_DAMAGE + random()*TRAP_DAMAGE_ADDRANDOM, zone, "wire", "");
		if(random() > 0.5)
			sound (trace_ent, CHAN_AUTO, "player/blood.wav", 1, ATTN_NORM);
		else
			sound (trace_ent, CHAN_AUTO, "player/blood2.wav", 1, ATTN_NORM);
		t.nextthink = time + 0.1;
		t.think = SUB_Remove;
		if(trace_ent != self.enemy)
			centerprint(self.enemy, "Somebody ran into your trap.\n");
		self.nextthink = time + 0.1;
		self.think = SUB_Remove;
		self.classname = "expired trap";
		return;
	}
	self.nextthink = time + 0.08;
};

void() W_FireTrap =
{
	local	vector	source;
	local	vector	org;
	local entity t;
	local float d, ot;

	if(!self.items & IT_NAILGUN)
		return;
	if(self.v_angle_x > 20)
		return;

	makevectors (self.angles);
	source = self.origin + '0 0 16';
	traceline (source, source + v_forward*64, FALSE, self);
	if (trace_fraction == 1.0)
	{
		centerprint(self, "Stand near a wall to set a trap.\n");
		return;
	}
	
	org = trace_endpos - v_forward*4;

	if ((trace_ent.takedamage) || (trace_ent.movetype != MOVETYPE_PUSH) || (trace_ent.solid != SOLID_BSP))
	{
		centerprint(self, "Stand near a wall to set a trap.\n");
		return;
	}

	t = find(world, classname, "wire");
	while(t != world)
	{
		if(t.enemy == self)
		{
			ot = 1;
			remove(t);
		}
		t = find(t, classname, "wire");
	}

	t = spawn();
	setorigin(t, org);
	t.angles = normalize('0 0 0' - v_forward*1);
	t.nextthink = time + 1;
	t.think = TrapThink;
//	t.owner = self;
	t.enemy = self;
	t.ltime = time + TRAP_TIME;
	t.classname = "wire";

	self.items = self.items - (self.items & IT_NAILGUN);
	if(ot)
		centerprint(self, "Old trap removed.\nNew trap is set, now get away.");
	else
		centerprint(self, "Trap is set, now get away.");
};

