
class compassController extends Inventory;

var UTCHUD controlledHUD;
var BRUT controllerMut;
var int iCompassOffset;

replication
{
	reliable if (ROLE == ROLE_Authority)
		iCompassOffset, HUDMutate, HUDMutateNR;
}

// Sends a mutate command to each individual HUD mutator active on the client. This should make other HUD mutators that rely on mutate commands for user-input work online.
simulated exec function HUDMutate(coerce String Param)
{
	local mutator m;
	
	if(role<role_authority || Level.netMode<NM_Client)
		if(controlledHUD==none)
			getControlledHUD();
	else return;

	if(controlledHUD!=none)
	{
		m=controlledHUD.myHUD.HUDMutator;
		while(m!=none)
		{
			m.mutate(Param, PlayerPawn(Owner));
			m=m.nextHUDMutator;
		}
	}
}

//Sends a mutate command to each individual HUD mutator forcibly disabling the mutators' built-in recursing.
simulated exec function HUDMutateNR(coerce String Param)
{
	local mutator m, mTemp;
	
	if(role<role_authority || Level.netMode<NM_Client)
	{
		if(controlledHUD==none) getControlledHUD();
	}
	else return;

	if(controlledHUD!=none)
	{
		m=controlledHUD.myHUD.HUDMutator;
		while(m!=none)
		{
			if(m.nextMutator==none) 
				m.mutate(Param, PlayerPawn(Owner));
			else 
			{
				mTemp = m.nextMutator;
				m.nextMutator = none;
				m.mutate(Param, PlayerPawn(Owner));
				m.nextMutator=mTemp;
			}
			m=m.nextHUDMutator;
		}
	}
}

simulated function Timer()
{
	local mutator m;

	if(controllerMut==none && Role>=role_authority)
	{
		for(m=level.game.baseMutator; m!=none; m=m.nextMutator)
		{
			if(m.isA('BRUT'))
			{
				controllerMut=BRUT(m);
				break;
			}
		}
	}
	if(controlledHUD==none && level.netMode!=NM_DedicatedServer) getControlledHUD();
}

function PostBeginPlay()
{
	local mutator m;
	
	super.postBeginPlay();
	if(Level.netMode==NM_standalone) setTimer(5.0, true);

	postNetBeginPlay();
}

simulated function postNetBeginPlay()
{
	super.postNetBeginPlay();
	if(Level.netMode!=NM_DedicatedServer) setTimer(5.0, true);

}

simulated event setInitialState()
{
	super.setInitialState();
	if(level.netMode!=NM_DedicatedServer) setTimer(5.0, true);
}

simulated function getControlledHUD()
{
	local playerPawn P;
	local challengeHUD H;
	local mutator M;

	if(Level.netMode==NM_DedicatedServer)
		return;
	else {}

	P=PlayerPawn(Instigator);
	if(P==none) P=PlayerPawn(Owner);
	
	if(P!=none)
	{
		H=challengeHUD(P.myHUD);
		if(H!=none) M=H.HudMutator;
		while(M!=none)
		{
			if(M.IsA('UTCHUD'))
			{
				controlledHUD=UTCHUD(M);
				return;
			}
			M=M.nextHUDMutator;
		}
		if(M==none)
		{
			log("Warning: Could not find a UTCompass HUD mutator on this client. Spawning a new one.", class.name);
			controlledHUD=Spawn(class'UTCHUD');
			ControlledHUD.nextHUDMutator=H.HUDMutator;
			H.HudMutator=controlledHUD;
		}
	}
	else 
		log("CompassController could not find Instigator.", class.name);
}

event destroyed()
{
	super.destroyed();
}

defaultproperties
{
   ItemName="compass controller"
}

