/*
* author: Motto73
* description: Gore visuals mod. (*UPDATED*)
*/
//Well, this is just a minimal script for showing more blood and stuff,
//license lets you copy and use ANYTHING from here without credit.	By Pack-Man (Motto73)

//You can swear by making Flyin blood shape to like "SHIT!" and multiplier to 1. ...or 10...

//You can set the shapes to any text and color to any color.

//Watch out for too big multipliers!! ==> L A G


//=============< Modifyable Variables >=============//

public const bool Use_Wall_Sliders = true; //To use the wall sliding blood or not.

public const bool Use_Flying_Blood = false; //To use flying blood or not. (Usually the main source of lag)

public const bool Use_Wall_Stills = true; //To use the wall paint or not.

public const string Flying_Blood_Shape = ",";   //The shape of the blood particles emitted.

public const string Wall_Slider_Blood_Shape = "|";  //The shape of blood on the wall sliding

public const string Wall_Still_Blood_Shape = ".";   //The shape of blood on the wall staying still

public Color Blood_Color = new Color(250, 50, 0);   //The Color of the blood. Can be green if you are alien.

public const int Flying_Blood_Multiplier = 4;   //How many blood particles emitted

public const int Wall_Blood_Multiplier = 8; //How many blood particles in the Bg

public const int Still_Blood_Scale = 2;     //Scale of th still bloods in the bg

public const bool Use_Cloth_Blood = true;	//New option: Whether to use blood on clothes or not.

//=============< Code >=============//

public int bloodNum = 0;
Random rnd = new Random();
public void OnStartup()
{
	var ded = (IObjectOnPlayerDeathTrigger)Game.CreateObject("OnPlayerDeathTrigger");
	ded.SetScriptMethod("Death");
	CreateTimer(250, 0, "Cloth", "");
}
public void Death(TriggerArgs args)
{
	ShowEff((IPlayer)args.Sender, 3, "BLD");
	ShowEff((IPlayer)args.Sender, 5, "TR_B");
	BgBloods((IPlayer)args.Sender);
}
public void BgBloods(IPlayer ply)
{
	Vector2 pos = ply.GetWorldPosition();
	if (Use_Flying_Blood)
	{
		for (int i = 0; i <= Flying_Blood_Multiplier; i++)
		{
			Vector2 newPos = pos + new Vector2(rnd.Next(-16, 16), rnd.Next(-16, 16) + 8);
			var text = (IObjectText)Game.CreateObject("Text", newPos);
			//byte byt = (byte)rnd.Next (-10, 10);		Needs clamping...   ...too lazy xd
			//Color newColor = new Color (((byte)(Blood_Color.R +byt)),((byte)( Blood_Color.G +byt)),((byte)( Blood_Color.B +byt)));
			text.SetBodyType(BodyType.Dynamic);
			text.SetText(Flying_Blood_Shape);
			text.SetTextColor(Blood_Color);
			text.SetLinearVelocity((newPos - pos) / 2);
			text.CustomID = "ScriptMarkedBlood";
		}
	}
	for (int i = 0; i <= Wall_Blood_Multiplier; i++)
	{
		Vector2 newPos = pos + new Vector2(rnd.Next(-16, 16), rnd.Next(-16, 16) + 16);
		if (HasGotBg(newPos))
		{
			if (Use_Wall_Sliders)
			{
				IObjectRailJoint rail = (IObjectRailJoint)Game.CreateObject("RailJoint", pos + new Vector2(0, 80));
				IObjectTargetObjectJoint targ = (IObjectTargetObjectJoint)Game.CreateObject("TargetObjectJoint", pos);
				IObjectText text = (IObjectText)Game.CreateObject("BgText", newPos);
				IObjectRailAttachmentJoint atch = (IObjectRailAttachmentJoint)Game.CreateObject("RailAttachmentJoint", newPos);
				//byte byt = (byte)rnd.Next (-10, 10);
				//Color newColor = new Color (((byte)(Blood_Color.R +byt)),((byte)( Blood_Color.G +byt)),((byte)( Blood_Color.B +byt)));
				bloodNum++;
				atch.SetMotorSpeed(0.1f);
				atch.SetTargetObject(text);
				atch.SetRailJoint(rail);
				atch.SetMotorEnabled(true);
				atch.CustomId = "Bloods" + bloodNum;
				rail.SetTargetObjectJoint(targ);
				rail.CustomId = "Bloods" + bloodNum;
				targ.CustomId = "Bloods" + bloodNum;
				text.SetBodyType(BodyType.Dynamic);
				text.SetText(Wall_Slider_Blood_Shape);
				text.SetTextColor(Blood_Color);
				text.CustomID = "ScriptMarkedBlood";
			}
			if (Use_Wall_Stills)
			{
				IObjectText text2 = (IObjectText)Game.CreateObject("BgText", newPos + new Vector2((float)-Still_Blood_Scale * 2, (float)Still_Blood_Scale * 2));
				text2.SetText(Wall_Still_Blood_Shape);
				text2.SetTextScale(rnd.Next(Still_Blood_Scale / 2, Still_Blood_Scale * 2));
				text2.SetTextColor(Blood_Color);
			}
		}
	}
}
public void Cloth(TriggerArgs M73)
{
	try
	{
		foreach (IPlayer ply in Game.GetPlayers())
		{
			if (ply != null && !ply.IsDead)
			{
				float m = ply.GetMaxHealth() - ply.GetHealth();
				IProfile pro = ply.GetProfile();
				if (m > 25)
				{
					pro.Head = Paint(pro.Head);
					pro.Accesory = Paint(pro.Accesory);
				}
				if (m > 50)
				{
					pro.ChestOver = Paint(pro.ChestOver);
					pro.Feet = Paint(pro.Feet);
				}
				if (m > 75)
				{
					pro.ChestUnder = Paint(pro.ChestUnder);
					pro.Legs = Paint(pro.Legs);
				}
				if (m > 90)
				{
					pro.ChestUnder = Paint(pro.ChestUnder, true);
					pro.Head = Paint(pro.Head, true);
					pro.Hands = Paint(pro.Hands, true);
				}
				ply.SetProfile(pro);
			}
		}
	}
	catch (NullReferenceException exc)
	{
		Game.WriteToConsole(exc.Message);	//Fuck the errors!
	}
}
public IProfileClothingItem Paint(IProfileClothingItem itm, bool also1=false)
{
	if (itm != null)
		itm.Color2=r;
	if (also1)
		itm.Color1 = r;
	return itm;
}
string r = "ClothingRed";
//=============< Helpers >=============//

public void CreateTimer(int interval, int count, string method, string id)
{
	IObjectTimerTrigger timerTrigger = (IObjectTimerTrigger)Game.CreateObject("TimerTrigger");
	timerTrigger.SetIntervalTime(interval);
	timerTrigger.SetRepeatCount(count);
	timerTrigger.SetScriptMethod(method);
	timerTrigger.CustomId = id;
	timerTrigger.Trigger();
}

public void updateText(string label, string text)
{
	((IObjectText)Game.GetSingleObjectByCustomId(label)).SetText(text);
}
public void ShowEff(IObject obj, int count, string eff)
{
	Vector2 pos = obj.GetWorldPosition();
	for (int i = 0; i <= count; i++)
	{
		Game.PlayEffect(eff, pos + new Vector2(rnd.Next(-16, 16), rnd.Next(-16, 16)));
	}
}
public bool HasGotBg(Vector2 pos)
{
	foreach (IObject obj in Game.GetObjectsByArea(new Area(pos.Y + 1, pos.X - 1, pos.Y - 1, pos.X + 1)))
	{
		if (obj.Name.StartsWith("Bg") && !obj.CustomID.Contains("ScriptMarkedBlood"))
			return true;
	}
	return false;
}
//=============< Was not so hard, was it? >=============//