//============================================================
// TalkingIconMut.uc	- Handles intialization of images and of the SpriteTracker
//============================================================
//	TalkingIcon
//		- A simple mutator for visibly notifying players when someone is typing.
//			Coded on request for Captain_Tea
//
//	Copyright (C) 2008 John "Shambler" Barrett (JBarrett847@Gmail.com or Shambler@OldUnreal.com)
//
//	This program is free software; you can redistribute and/or modify
//	it under the terms of the Open Unreal Mod License version 1.1.
//
//============================================================
//
// Allows admins to setup custom textures to replace the default
// speech bubble texture, and spawns a TalkSpriteTracker, which
// handles the main implementation of this mutator
//
//============================================================
Class TalkingIconMut extends Mutator
	config(TalkingIcon);


// *** Config Variables
var config bool bCustomTalkTextures;

var config string CustomTalkTexture;
var config string CustomTalkTextureRed;
var config string CustomTalkTextureBlue;


// *** Runtime Variables
var TalkSpriteTracker SpriteTracker;



// *** General functions

// Verify the config data
function PostBeginPlay()
{
	local UTUIDataProvider_Mutator DP;
	local Texture2D DefaultTalkTexture;
	local Texture2D RedTalkTexture;
	local Texture2D BlueTalkTexture;

	Super.PostBeginPlay();


	// Check that the data provider for this mutator exists (necessary for viewing the mutator by webadmin)
	DP = new (Class.Outer, string(Class)) Class'UTUIDataProvider_Mutator';

	if (DP.ClassName != PathName(Class))
	{
		LogInternal("Storing data provider for 'TalkingIconMut'", 'TalkingIcon');

		DP.ClassName = PathName(Class);
		Class'UnrealScriptTools14.USTools'.static.SetFriendlyName(DP, "Talking Icon Mutator");
		Class'UnrealScriptTools14.USTools'.static.SetDescription(DP, "Server mutator which displays an icon over a players head when typing");

		DP.SaveConfig();
	}


	// Setup 'RepositionMutator' so that it's called during the next Tick
	SetTimer(0.000001,, 'RepositionMutator');


	// If the admin has setup custom talk textures, load them
	if (bCustomTalkTextures)
	{
		if (CustomTalkTexture != "")
			DefaultTalkTexture = Texture2D(DynamicLoadObject(CustomTalkTexture, Class'Texture2D'));

		if (CustomTalkTextureRed != "")
			RedTalkTexture = Texture2D(DynamicLoadObject(CustomTalkTextureRed, Class'Texture2D'));

		if (CustomTalkTextureBlue != "")
			BlueTalkTexture = Texture2D(DynamicLoadObject(CustomTalkTextureBlue, Class'Texture2D'));
	}


	// N.B. After compilation, I use the MergePackages commandlet to include TalkingIconResources.upk (which includes the SpeechBubble texture) with this .u file
	if (DefaultTalkTexture == none)
		DefaultTalkTexture = Texture2D(DynamicLoadObject(Class.GetPackageName()$".TalkingIconResources.SpeechBubble", Class'Texture2D'));

	if (RedTalkTexture == none)
		RedTalkTexture = Texture2D(DynamicLoadObject(Class.GetPackageName()$".TalkingIconResources.SpeechBubbleRed", Class'Texture2D'));

	if (BlueTalkTexture == none)
		BlueTalkTexture = Texture2D(DynamicLoadObject(Class.GetPackageName()$".TalkingIconResources.SpeechBubbleBlue", Class'Texture2D'));


	// Spawn the clientside sprite tracker and assign its default textures
	SpriteTracker = Spawn(Class'TalkSpriteTracker');

	SpriteTracker.ServerDefaultTalkTexture = DefaultTalkTexture;
	SpriteTracker.ServerRedTalkTexture = RedTalkTexture;
	SpriteTracker.ServerBlueTalkTexture = BlueTalkTexture;
}

function Mutate(string MutateString, PlayerController Sender)
{
	// Hacky (but very efficient) way of finishing off implementation of UTPawn.bIsTyping
	if (Left(MutateString, 1) == Chr(1))
	{
		UTPlayerController(Sender).Typing(bool(Right(MutateString, 1)));
		return;
	}

	Super.Mutate(MutateString, Sender);
}


// Called in the first tick after PostBeginPlay, this moves the mutator to the start of the mutator list (BaseMutator), in order to speed up calls to Mutate
function RepositionMutator()
{
	local Mutator M;

	if (WorldInfo.Game.BaseMutator != Self)
	{
		// Find the mutator which links to this one, and set its NextMutator variable to this mutators NextMutator variable
		M = WorldInfo.Game.BaseMutator;

		while (M.NextMutator!=Self)
			M = M.NextMutator;

		M.NextMutator = NextMutator;


		// Set this mutators NextMutator variable to the base mutator, and then replace the base mutator
		NextMutator = WorldInfo.Game.BaseMutator;
		WorldInfo.Game.BaseMutator = Self;
	}
}


defaultproperties
{
	bCustomTalkTextures=False

	CustomTalkTexture=""
	CustomTalkTextureRed=""
	CustomTalkTextureBlue=""
}