void(entity client, string s1, string s2, string s3, string s4, string s5, string s6, string s7) gencenterprint7 = #73;  // sprint, but in middle
float() NumberOfPlayers;
void() T_MissileTouch;
void() info_player_start;
void(entity targ, entity attacker) ClientObituary;
float() mrand;
void(entity bleeder, float timer, vector ang, vector org) SpawnBloodFountain; //hehe

void() monster_death_use;

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

/*
============
CanDamage

Returns true if the inflictor can directly damage the target.  Used for
explosions and melee attacks.
============
*/
float(entity targ, entity inflictor) CanDamage =
{
// bmodels need special checking because their origin is 0,0,0
	if (targ.movetype == MOVETYPE_PUSH)
	{
		traceline(inflictor.origin, 0.5 * (targ.absmin + targ.absmax), TRUE, self);
		if (trace_fraction == 1)
			return TRUE;
		if (trace_ent == targ)
			return TRUE;
		return FALSE;
	}
	
	traceline(inflictor.origin, targ.origin, TRUE, self);
	if (trace_fraction == 1)
		return TRUE;
	traceline(inflictor.origin, targ.origin + '15 15 0', TRUE, self);
	if (trace_fraction == 1)
		return TRUE;
	traceline(inflictor.origin, targ.origin + '-15 -15 0', TRUE, self);
	if (trace_fraction == 1)
		return TRUE;
	traceline(inflictor.origin, targ.origin + '-15 15 0', TRUE, self);
	if (trace_fraction == 1)
		return TRUE;
	traceline(inflictor.origin, targ.origin + '15 -15 0', TRUE, self);
	if (trace_fraction == 1)
		return TRUE;

	return FALSE;
};


/*
============
Killed
============
*/
void(entity targ, entity attacker) Killed =
{
	local entity oself;
	local string t;

	oself = self;
	self = targ;
	
	if (self.health < -99)
		self.health = -99;		// don't let sbar look bad if a player
	
	if (self.movetype == MOVETYPE_PUSH || self.movetype == MOVETYPE_NONE)
	{	// doors, triggers, etc
		self.th_die ();
		self = oself;
		return;
	}
/*
	if((self != attacker) && (self.flags & FL_CLIENT) && (attacker.flags & FL_CLIENT))
	{
		t = ftos(attacker.health);
//		if(attacker.armorvalue)
//			gencenterprint7(self, attacker.netname, " had ", t, " health and ", ftos(attacker.armorvalue), " armor.", "");
//		else
			gencenterprint7(self, attacker.netname, " had ", t, " health.", "", "", "");
		if(NumberOfPlayers() > 2)
			gencenterprint7(attacker, "You killed ", self.netname, ".", "", "", "", "");
	}
*/
	self.enemy = attacker;

// bump the monster counter
	if (self.flags & FL_MONSTER)
	{
		killed_monsters = killed_monsters + 1;
		WriteByte (MSG_ALL, SVC_KILLEDMONSTER);
	}

	ClientObituary(self, attacker);
	
	self.takedamage = DAMAGE_NO;
	self.touch = SUB_Null;

	monster_death_use();
	self.th_die ();
	
	self = oself;
};


/*
============
T_Damage

The damage is coming from inflictor, but get mad at attacker
This should be the only function that ever reduces health.
============
*/
void(entity targ, entity inflictor, entity attacker, float damage, float zone, string dt, string dan) T_Damage =
{
	local	vector	dir, add;
	local	entity	oldself, e;
	local	float	save;
	local	float	take, flying;

	if (!targ.takedamage)
		return;

	flying = 0;	

	if(dt != "")
		targ.deathtype = dt;

	targ.lasthit = time;
	targ.deathan = dan;

	if((dt == "fire") || (dt == "burnarrow"))
		dan = "";

	if((random() > 0.8) && 
	   (zone == Z_HEAD) && 
	   (dan == ""))
		targ.deathan = "headshot";
	else if (zone == Z_HEAD)
	{
		if((random() > 0.5) && (dt == "burnarrow"))
		{
			targ.deathan = "headfly";
			inflictor.count = 1;
		}
		else
			targ.deathan = "gibhead";
		if(targ.classname == "player")
			sprint(attacker, "░хеадсхот║▒\n");	//means: Headshot!
	}

	if((random() > 0.8) && 
	   (dt == "sword") && 
	   (dan == "") &&
	   (targ.flags & FL_ONGROUND))
		targ.deathan = "middle";

// used by buttons and triggers to set activator for target firing
	damage_attacker = attacker;

// check for quad damage powerup on the attacker
//	if (attacker.super_damage_finished > time)
//		damage = damage * 4;
	if(zone)
	{
		damage = damage * zone;
		zone = 0;
	}

// save damage based on the target's armor level

	if(!((dt == "sword") || 		//GeN: armor does not help if hit with sword =)
             (dt == "throwingsword")))
	save = ceil(targ.armortype*damage);
	if (save >= targ.armorvalue)
	{
		save = targ.armorvalue;
		targ.armortype = 0;	// lost all armor
		targ.items = targ.items - (targ.items & (IT_ARMOR1 | IT_ARMOR2 | IT_ARMOR3));
	}
	
	targ.armorvalue = targ.armorvalue - save;
	take = ceil(damage-save);

// add to the damage total for clients, which will be sent as a single
// message at the end of the frame
// FIXME: remove after combining shotgun blasts?
	if (targ.flags & FL_CLIENT)
	{
		targ.dmg_take = targ.dmg_take + take;
		targ.dmg_save = targ.dmg_save + save;
		targ.dmg_inflictor = inflictor;
	}

// figure momentum add
	if ((inflictor != world) && 
	    (targ.movetype == MOVETYPE_WALK) && 
	    (targ.deathtype != "knife") &&
	    (targ.deathtype != "shuriken") &&
	    (targ.deathtype != "arrow") &&
	    (targ.deathtype != "dagger") &&
	    (targ.deathtype != "burnarrow") &&
	    (targ.deathtype != "gib") &&
	    (targ.deathan != "middle"))
	{
/* swordfly removed
		if((targ.sword != world) && (targ.deathan == "tswordfly") && ((targ.health - take) <= 0))
		{
			setsize(targ, '0 0 0', '0 0 0');
			targ.velocity = targ.sword.velocity;
			targ.stick = 1;
			targ.angles = vectoangles(targ.sword.velocity);
			targ.flags = targ.flags - (targ.flags & FL_ONGROUND);
		}
		else
		{
*/
			dir = targ.origin - (inflictor.absmin + inflictor.absmax) * 0.5;
			dir = normalize(dir);
//			targ.velocity = targ.velocity + dir*damage*8;
			targ.flags = targ.flags - (targ.flags & FL_ONGROUND);
			add = '0 0 0';
			add_z = damage;
			targ.velocity = targ.velocity + dir*damage*12 + (add*5);
			flying = 1;
	}

// check for godmode or invincibility
	if (targ.flags & FL_GODMODE)
		return;
	if (targ.invincible_finished >= time)
	{
		if (self.invincible_sound < time)
		{
			sound (targ, CHAN_ITEM, "items/protect3.wav", 1, ATTN_NORM);
			self.invincible_sound = time + 2;
		}
		return;
	}

// team play damage avoidance
	if ( (teamplay == 1) && (targ.team > 0)&&(targ.team == attacker.team) )
		return;
		
// do the damage
	targ.health = targ.health - take;
			
	if (targ.health <= 0)
	{
		if((random() > 0.3) && (targ.deathan == "") && flying)
			targ.avelocity_y = mrand()*(500 + mrand()*(damage*3));
		if(targ.classname == "bomb")
			targ.owner = attacker;
		Killed (targ, attacker);
		if(((targ.deathtype == "sword") ||
		    (targ.deathtype == "knife")) &&
		    (random() > 0.7))
			SpawnBloodFountain(targ, 1, '0 0 0', '0 0 0');
		return;
	}
	else if((targ.health < 10) && (take > 70))
	{
		sprint(targ, "░сериоус инйуры║▒\n");	//means: SERIOUS INJURY!
		sound (targ, CHAN_BODY, "player/breathe.wav", 1, ATTN_NORM);
	}
	if((targ.health < BERSERKHEALTH) && (targ.health > 0))
		targ.mode = M_BERSERK;

// react to the damage
	oldself = self;
	self = targ;

	if ( (self.flags & FL_MONSTER) && attacker != world)
	{
	// get mad unless of the same class (except for soldiers)
		if (self != attacker && attacker != self.enemy)
		{
			if ( (self.classname != attacker.classname) 
			|| (self.classname == "monster_army" ) )
			{
				if (self.enemy.classname == "player")
					self.oldenemy = self.enemy;
				self.enemy = attacker;
				FoundTarget ();
			}
		}
	}

	if (self.th_pain)
	{
		self.th_pain (attacker, take);
	// nightmare mode monsters don't go into pain frames often
		if (skill == 3)
			self.pain_finished = time + 5;		
	}
	self = oldself;
};

/*
============
T_RadiusDamage
============
*/
void(entity inflictor, entity attacker, float damage, entity ignore, string dt, string dan) T_RadiusDamage =
{
	local	float 	points;
	local	entity	head;
	local	vector	org;
// 1998-07-07 T_RadiusDamage workaround by Robert Field  start
        local   float   attacker_damaged;
        local   float   attacker_points;
// 1998-07-07 T_RadiusDamage workaround by Robert Field  end

	head = findradius(inflictor.origin, damage+40);
	
	while (head)
	{
		if (head != ignore)
		{
			if (head.takedamage)
			{
				org = head.origin + (head.mins + head.maxs)*0.5;
				points = 0.5*vlen (inflictor.origin - org);
				if (points < 0)
					points = 0;
				points = damage - points;
// 1998-07-07 T_RadiusDamage workaround by Robert Field  start
/*
                                if (head == attacker)
                                        points = points * 0.5;
*/
// 1998-07-07 T_RadiusDamage workaround by Robert Field  end
				if (points > 0)
				{
					if (CanDamage (head, inflictor))
					{	
// 1998-07-07 T_RadiusDamage workaround by Robert Field  start
                                                if (head != attacker)
                                                {
// 1998-07-07 T_RadiusDamage workaround by Robert Field  end
						// shambler takes half damage from all explosions
						if (head.classname == "monster_shambler")						
							T_Damage (head, inflictor, attacker, points*0.5, 0, dt, dan);
						else
							T_Damage (head, inflictor, attacker, points, 0, dt, dan);
// 1998-07-07 T_RadiusDamage workaround by Robert Field  start
                                                }
                                                else
                                                {
                                                        attacker_damaged = TRUE;
                                                        attacker_points = points * 0.5;
                                                }
// 1998-07-07 T_RadiusDamage workaround by Robert Field  end
					}
				}
			}
		}
		head = head.chain;
	}
// 1998-07-07 T_RadiusDamage workaround by Robert Field  start
        if (attacker_damaged)
                T_Damage (attacker, inflictor, attacker, attacker_points, 0, dt, dan);
// 1998-07-07 T_RadiusDamage workaround by Robert Field  end
};

/*
============
T_BeamDamage
============
*/
void(entity attacker, float damage) T_BeamDamage =
{
	local	float 	points;
	local	entity	head;
	
	head = findradius(attacker.origin, damage+40);
	
	while (head)
	{
		if (head.takedamage)
		{
			points = 0.5*vlen (attacker.origin - head.origin);
			if (points < 0)
				points = 0;
			points = damage - points;
			if (head == attacker)
				points = points * 0.5;
			if (points > 0)
			{
				if (CanDamage (head, attacker))
				{
					if (head.classname == "monster_shambler")						
						T_Damage (head, attacker, attacker, points*0.5, 0, "", "");
					else
						T_Damage (head, attacker, attacker, points, 0, "", "");
				}
			}
		}
		head = head.chain;
	}
};

