class sgRURecovery extends Mutator config;

var config bool RURecovery;
var bool	bInitialized;
var float	SavedRU[32];
var string	SavedInfo[32];
var pawn	p;
var int		iTotal;
 
function PostBeginPlay()
{
	if (bInitialized || !RURecovery) return;
	bInitialized=true;
	Level.Game.RegisterMessageMutator(self);
	SetTimer(5,True);
}

function RecoverRU(PlayerPawn pp)
{
	local string sInfo;
	local int i;

	if (pp == none || !pp.bIsPlayer || pp.IsA('Spectator') || pp.IsA('Bot') || pp.PlayerReplicationInfo == none || pp.PlayerReplicationInfo.PlayerName == "Player" )
		return;

	sInfo = GetPlayerInfo(pp);

	for(i=0;i<iTotal;i++)
		if (sInfo == SavedInfo[i])
		{
			sgPRI(pp.PlayerReplicationInfo).RU = SavedRU[i];
			AnnounceAll("Siege RU-Recovery:"@int(SavedRU[i])@"RU recovered for"@pp.PlayerReplicationInfo.PlayerName);
			return;
		}

	SavedInfo[iTotal]=sInfo;
	iTotal++;
	if (iTotal>31) iTotal=31;
}

function Timer()
{
	local int i;
	local string sInfo;

	for(p=Level.PawnList; p!=none; p=p.nextPawn)
		if( PlayerPawn(p)!=none && p.bIsPlayer && !p.IsA('Spectator') && !p.IsA('Bot') && p.PlayerReplicationInfo!=none && p.PlayerReplicationInfo.PlayerName!="Player" )
		{
			sInfo = GetPlayerInfo(p);
			for (i=0;i<iTotal;i++)
				if (sInfo == SavedInfo[i]) {
					SavedRU[i]=sgPRI(p.PlayerReplicationInfo).RU; i = 32; }
		}	
}

function string GetPlayerInfo(Pawn pp)
{
   return pp.PlayerReplicationInfo.PlayerName;

}

function AnnounceAll(string sMessage)
{
    local Pawn p;

    for ( p = Level.PawnList; p != None; p = p.nextPawn )
	    if ( (p.bIsPlayer || p.IsA('MessagingSpectator')) &&
          p.PlayerReplicationInfo != None  )
		    p.ClientMessage(sMessage);
}

defaultproperties
{
    RURecovery=True
}
