//------------------------------------------------------------------------------------
// Sword
//------------------------------------------------------------------------------------

void() Sworddamage_touch =
{
local float r;
	if(other.blocktime > time)
		return;
	else if ((other.takedamage) && (other.retouch_time < time) && (other != self.owner))
	{
		makevectors(self.angles);
		if((other.weapon == IT_ROCKET_LAUNCHER) && (Infront(other, self.origin - v_forward*25)) && ((other.button0held > (time - 0.5) ) || ((!other.button0held) && other.button0)))
		{
			if(random() > (1 - KATANA_CANNOTBLOCKCHANCE))
				sprint(other, "ΩΟΥ ΓΟΥΜΔ ΞΟΤ ΒΜΟΓΛ ΤΘΙΣ ΑΤΤΑΓΛ‘\n");	//means: you could not block this attack!
			else 
			{	//perform a block
				stuffcmd(other, "bf\n");
				sprint(other, "ΒΜΟΓΛ‘\n");
				sound(other, CHAN_WEAPON, "weapons/swhitsw.wav", 1, ATTN_NORM);
				other.think = player_block1;
				other.nextthink = time;
				other.blocktime = time + 0.21;
				SpawnSparks(self.origin, 111, 6, 8, 2);
				if((!self.owner.flags & FL_ONGROUND) && (!other.flags & FL_ONGROUND))
				{
					r = random();
					self.owner.velocity = '0 0 0';
					self.owner.velocity_z = r*NINJA_AIRCOMBATUPDRAFT;
					other.velocity = '0 0 0';
					other.velocity_z = r*NINJA_AIRCOMBATUPDRAFT;
				}
				return;
			}
		}
		other.axhitme = 1;
		SpawnBlood(other.origin , VEC_ORIGIN, 20);
		if(self.owner.mode == M_BERSERK)
			T_Damage(other, self.owner, self.owner, 100, 0, "berserksword", "");
		else
			T_Damage(other, self.owner, self.owner, 100, 0, "sword", "");
		sound(other, CHAN_AUTO, "weapons/met2flsh.wav", 1, ATTN_NORM);
		if (!(self.owner.waterlevel >= 1)) 
		{
			self.owner.ninja = 1;
			self.owner.skin = 1;
			self.owner.weaponmodel = "progs/v_katan2.mdl";
		}
		other.retouch_time = self.nextthink + 0.01;
	}
};

void() W_FireKatana =
{
  local float r;
  local vector source;
  local vector org;
  local entity death;

  makevectors(self.v_angle);
  trace_ent = world;
  source = self.origin + '0 0 16';
  traceline(source, source + v_forward * 76, 0, self);

  death = spawn();
  death.classname = "sworddamage";
  death.movetype = MOVETYPE_NONE;
  death.solid = SOLID_TRIGGER;
  death.angles = self.angles;
  setsize (death, self.mins - '15 15 15', self.maxs + '15 15 15');
  setorigin (death, self.origin + v_forward*32);
  death.touch = Sworddamage_touch;
  death.nextthink = time + 0.2;
  death.think = SUB_Remove;
  death.owner = self;

  if (trace_fraction == 1)
    return;
  org = trace_endpos - v_forward * 4;

  if ((trace_ent.takedamage) && (trace_ent.retouch_time < time))
  {
	other = trace_ent;	//sooo lazy...
	if((other.weapon == IT_ROCKET_LAUNCHER) && (Infront(other, self.origin)) && ((other.button0held > (time - 0.5) ) || ((!other.button0held) && other.button0)))
	{
		if(random() > (1 - KATANA_CANNOTBLOCKCHANCE))
			sprint(other, "ΩΟΥ ΓΟΥΜΔ ΞΟΤ ΒΜΟΓΛ ΤΘΙΣ ΑΤΤΑΓΛ‘\n");	//means: you could not block this attack!
		else 
		{	//perform a block
			stuffcmd(other, "bf\n");
			sprint(other, "ΒΜΟΓΛ‘\n");
			sound(other, CHAN_WEAPON, "weapons/swhitsw.wav", 1, ATTN_NORM);
			other.think = player_block1;
			other.nextthink = time;
			other.blocktime = time + 0.21;
			SpawnSparks(org, 111, 6, 8, 2);
			if((!self.flags & FL_ONGROUND) && (!trace_ent.flags & FL_ONGROUND))
			{
				r = random();
				self.velocity = '0 0 0';
				self.velocity_z = r*NINJA_AIRCOMBATUPDRAFT;
				trace_ent.velocity = '0 0 0';
				trace_ent.velocity_z = r*NINJA_AIRCOMBATUPDRAFT;
			}
			return;
		}
	}
    trace_ent.retouch_time = time + 0.21;
    trace_ent.axhitme = 1;
    SpawnBlood(org, VEC_ORIGIN, 20);
	if(self.mode == M_BERSERK)
		T_Damage(trace_ent, self, self, 100, 0, "berserksword", "");
	else
		T_Damage(trace_ent, self, self, 100, 0, "sword", "");
    sound(self, CHAN_AUTO, "weapons/met2flsh.wav", 1, ATTN_NORM);
    if (!(self.waterlevel >= 1)) 
    {
      self.ninja = 1;
      self.skin = 1;
      self.weaponmodel = "progs/v_katan2.mdl";
    }
  }
  else 
  {
    sound(self, CHAN_AUTO, "weapons/hitwall.wav", 1, ATTN_NORM);
    SpawnSparks(org, 111, 6, 8, 2);
  }
};

//------------------------------------------------------------------------------------
// Throwing sword
//------------------------------------------------------------------------------------

void() SwordReturn =
{
	self.flags = self.flags - (self.flags & FL_ONGROUND);
	self.velocity = self.tvec;
	self.oldvel = self.velocity;
	self.angles = vectoangles(self.velocity);
	self.nextthink = time + KATANA_TIME/1.5;
	self.think = SUB_Remove;
};

void() T_katanatouch =
{
	local float rand;
	local float r, zone, fly;
	local entity oself;
	local vector vec, move;

	if ((self.velocity == '0 0 0') && (other.classname == "player"))
	{
		if (other.items & IT_ROCKET_LAUNCHER)
			return;
		sound(other, CHAN_AUTO, "weapons/ching.wav", 1, ATTN_NORM);
		sprint(other, "You got the Katana\n");
		other.items = other.items | IT_ROCKET_LAUNCHER;
		if ((other.owner.waterlevel < 1) && (self.ninja))
		{
			other.ninja = 1;
			other.skin = 1;
		}
		oself = self;
		self = other;
		self.weapon = W_BestWeapon();
		W_SetCurrentAmmo();
		self = oself;
		self.classname = "remove";
		remove(self);
		return;
	}
	if (pointcontents(self.origin) == CONTENT_SKY)
	{
		remove(self);
		return;
	}
	if (other.takedamage)
	{
		if(other == self.owner)
			return;
		if ((self.movetype != MOVETYPE_FLYMISSILE) && (self.movetype != MOVETYPE_TOSS))
			return;
		if(((other.button0held > (time - 0.5) ) || ((!other.button0held) && other.button0)) && (other.weapon == IT_ROCKET_LAUNCHER) && Infront(other, self.origin))
		{
			if(random() > KATANA_CANNOTBLOCKCHANCE)
			{
				self.origin = self.origin - 8*normalize(self.velocity);
				if(random() > (1 - KATANA_OFFENSIVEBLOCKCHANCE))
				{
					// set missile speed
					vec = self.owner.origin + self.owner.mins + self.owner.size * 0.7 - self.origin;
					// calc aproximate time for missile to reach vec
					fly = vlen (vec) / 800;
					// get the entities xy velocity
					move = self.owner.velocity;
					// project the target forward in time
					vec = vec + move * fly;
					vec = normalize(vec);
					self.tvec = vec * 800;

					sprint(other, "ΟΖΖΕΞΣΙΦΕ ΒΜΟΓΛ‘\n");
				}
				else
				{
					self.tvec = self.oldvel * -1;
					sprint(other, "ΒΜΟΓΛ‘\n");
				}
				self.nextthink = time + 0.1;
				self.think = SwordReturn;
				self.flags = self.flags - (self.flags & FL_ONGROUND);
				self.oldvel = self.velocity;
				self.angles = vectoangles(self.velocity);
				sound(other, CHAN_WEAPON, "weapons/swhitsw.wav", 1, ATTN_NORM);
				other.think = player_block1;
				other.nextthink = time;
				SpawnSparks(self.origin, 111, 6, 8, 2);
				self.owner = other;
				stuffcmd(other, "bf\n");
				return;
			}
			else
				sprint(other, "ΩΟΥ ΓΟΥΜΔ ΞΟΤ ΒΜΟΓΛ ΤΘΙΣ ΑΤΤΑΓΛ‘\n");	//means: you could not block this attack!
		}
		zone = DetermineZone(self, other.origin);
		if(!self.count)
			T_Damage(other, self, self.owner, (KATANA_DAMAGE + (random()*KATANA_DAMAGE_ADDRANDOM)), zone, "throwingsword", "");
		else
			T_Damage(other, self, self.enemy, (KATANA_DAMAGE + (random()*KATANA_DAMAGE_ADDRANDOM)), zone, "throwingsword", "");
		self.velocity = self.velocity * -0.1;
		self.movetype = MOVETYPE_TOSS;
		self.flags = self.flags - (self.flags & FL_ONGROUND);
		spawn_touchblood(20);
		sound(self, CHAN_WEAPON, "weapons/met2flsh.wav", 1, ATTN_NORM);
		self.ninja = 1;
		self.skin = 1;
	}
	else if ((other.solid == SOLID_BSP) && (other.movetype == MOVETYPE_PUSH))
	{
		SpawnSparks(self.origin, 111, 6, 8, 150);
		sound(self, CHAN_AUTO, "weapons/hitwall.wav", 1, ATTN_NORM);
		self.count = 1;
		self.velocity = '0 0 0';
		self.movetype = MOVETYPE_NONE;
	}
	else
		remove(self);
};

void() BerserkKatanaTouch =
{
	local float zone;
	stuffcmd(self.owner, "v_cshift 0 0 0 0\n");
	self.origin = self.origin - 8*normalize(self.velocity);
	self.jumpheight = 1;
	self.velocity = '0 0 0';
	self.classname = "smallexplosion";
	if((pointcontents(self.origin) == CONTENT_WATER) || (pointcontents(self.origin) == CONTENT_SLIME))
	{
		self.think = SUB_DeathBubbles;
		self.nextthink = time;
		return;
	}
	self.movetype = MOVETYPE_NOCLIP;
	self.solid = SOLID_NOT;
	setmodel(self, "progs/sphere.mdl");
	self.flags = self.flags - (self.flags & FL_ONGROUND);
	self.avelocity_x = mrand() * 400;
	self.avelocity_y = mrand() * 400;
	self.avelocity_z = mrand() * 400;
	self.nextthink = time + 0.04;
	self.frame = 1;
	self.think = SmallExplosionThink;
	self.deathtype = "berserkthrowingsword";

	sound (self, CHAN_WEAPON, "weapons/r_exp3.wav", 1, ATTN_NORM);
	T_RadiusDamage(self, self.owner, KATANA_BERSERKRADIUS, other, "berserkthrowingsword", "");
	RadiusBurn(self.origin, KATANA_BERSERKFIRERADIUS, self.owner);
	if(other.takedamage)
	{
		if((other.flags & FL_CLIENT) || (other.flags && FL_MONSTER))
			zone = DetermineZone(self, other.origin);
		T_Damage(other, self, self.owner, (KATANA_DAMAGE*2 + (random()*KATANA_DAMAGE_ADDRANDOM)), zone, "berserkthrowingsword", "");
	}
};

void() BerserkScrapThink =
{
	if((self.owner == world) || (self.owner.jumpheight))
	{
		remove(self);
		return;
	}
	self.frame = self.frame + 1;
	if(self.frame > 5)
		self.frame = 0;
	self.nextthink = time + 0.1;
};

void() ThrowKatana =
{
  local entity missile, s;

  if((self.state & S_REALINVIS) || (self.weapon == IT_LIGHTNING))
  {
	sprint(self, "You cannot throw a sword while cloaked.\n");
	return;
  }
  if(self.attack_finished > time)
    return;
  if(!self.items & IT_ROCKET_LAUNCHER)
    return;
  self.items = self.items - (self.items & IT_ROCKET_LAUNCHER);

	if(self.mode == M_BERSERK)
	{
		sound (self, CHAN_WEAPON, "weapons/fireball.wav", 1, ATTN_NORM);
		sound (self, CHAN_BODY, "player/berserk.wav", 1, ATTN_NORM);
	}
	else
		sound (self, CHAN_WEAPON, "weapons/whoosh.wav", 1, ATTN_NORM);
  self.punchangle_x = -4;
  missile = spawn();
  missile.owner = self;
  if (self.waterlevel >= 2)
    missile.movetype = MOVETYPE_BOUNCE;
  else
    missile.movetype = MOVETYPE_FLYMISSILE;
  missile.flags = FL_ITEM;
  missile.solid = SOLID_TRIGGER;
  makevectors(self.v_angle);
  missile.velocity = aim(self, 1000);
  if (self.waterlevel >= 2)
    missile.velocity = missile.velocity * 200 + crandom() * v_right * 150 + crandom() * v_up * 30;
  else if(self.mode == M_BERSERK)
    missile.velocity = missile.velocity * 1000 + crandom() * v_right * 30 + crandom() * v_up * 30;
  else
    missile.velocity = missile.velocity * 800 + crandom() * v_right * 30 + crandom() * v_up * 30;
  missile.oldvel = missile.velocity;
  missile.angles = vectoangles(missile.velocity);
	if(self.mode == M_BERSERK)
		missile.touch = BerserkKatanaTouch;
	else
		missile.touch = T_katanatouch;
  missile.nextthink = time + KATANA_TIME;
  missile.think = SUB_Remove;
  missile.classname = "throwingsword";
  if (self.ninja == 1)
  {
    setmodel(missile, "progs/katana2.mdl");
    missile.ninja = 1;
  }
  else
    setmodel(missile, "progs/katana.mdl");
  setsize (missile, '0 0 0', '0 0 0');
  setorigin(missile, self.origin + v_forward * 8 + '0 0 16');
	missile.jumpheight = 0;
	if(self.mode == M_BERSERK)
	{
		s = spawn();
		s.owner = missile;
		s.think = BerserkScrapThink;
		s.nextthink = time + 0.1;
		setmodel (s, "progs/fireball.mdl");
		setsize(s, '0 0 0', '0 0 0');
		s.movetype = MOVETYPE_NOCLIP;
		s.velocity = missile.velocity;
		s.solid = SOLID_NOT;
		s.origin = missile.origin + normalize(missile.velocity) * 64;
		s.angles = vectoangles(missile.velocity);
		s.frame = 0;
		stuffcmd(self, "v_cshift 255 0 0 80\n");
	}
  self.attack_finished = time + 0.3;
  self.show_hostile = time + 1;
  self.ninja = 0;
  self.skin = 0;
  self.weapon = W_BestWeapon();
  W_SetCurrentAmmo();
};

