//=============================================================================
// XPickupsMut.
//=============================================================================
class XPickupsMut expands Mutator config (XPickups);

var() config bool bRXMode;

var byte Count;

var bool bPassedFirst;

function bool CanProceedWithMut()
{
local XReplacer XRep;
local PickupCharger PcK;

	ForEach AllActors ( Class'XReplacer', XRep)
	{
		if (XRep != None);
			return false;
	}

	ForEach AllActors ( Class'PickupCharger', PcK)
	{
		if (PcK != None);
			return false;
	}

	return true;
}

function PostBeginplay()
{
local Inventory Inv, NewInv;
local Actor A;
local vector HitLocation, HitNormal, X, Y, Z;
local rotator RRoll;


		if (!CanProceedWithMut())
			return;

		//Set Enforcers and such
		SetTimer(0.1, True);

		//Set compatible HUD
		if (Level.Game.IsA('DeathMatchPlus') && !Level.Game.IsA('TeamGamePlus'))
			Level.Game.HUDType = Class'X_DM_HUD';
		else if (Level.Game.IsA('Assault'))
			Level.Game.HUDType = Class'X_AS_HUD';
		else if (Level.Game.IsA('CTFGame'))
			Level.Game.HUDType = Class'X_CTF_HUD';
		else if (Level.Game.IsA('Domination'))
			Level.Game.HUDType = Class'X_DOM_HUD';
		else if (Level.Game.IsA('TeamGamePlus') && !Level.Game.IsA('Assault') 
		&& !Level.Game.IsA('CTFGame') && !Level.Game.IsA('Domination'))
			Level.Game.HUDType = Class'X_TDM_HUD';
		

		//Set chargers to pickups which don't have them and replace standard ammo with compatible one
		ForEach AllActors (Class'Inventory', Inv)
		{
			if ((Inv.IsA('Weapon') || Inv.IsA('Pickup')) && Pawn(Inv.Owner) == None)
			{
				if (!bPassedFirst)
				{
					bPassedFirst = True;
					Spawn(Class'XPickNotifier',,, Inv.Location);
				}

				A = Trace( HitLocation, HitNormal, Inv.Location - vect(0,0,256), Inv.Location, False);

				/*if (A == None)
				{
					HitLocation = Inv.Location;
					HitNormal = vect(0,0,1);
				}*/

				HitNormal = vector(rotator(HitNormal) + rot(-16384,0,0));

				if (HitNormal == vect(0,0,0))
					RRoll.Yaw = Inv.Rotation.Yaw;
				else
					RRoll.Yaw = 0;

				GetAxes(rotator(HitNormal), X, Y, Z);
				NewInv = None;

				if (Inv.Tag == 'BerserkPickup')
				{
					if (A.IsA('Mover') || A == None || Inv.Rotation.Roll!=0)
						NewInv = Spawn(Class'Uberseker',,, Inv.Location, Inv.Rotation);
					else
						NewInv = Spawn(Class'X_UBerseker',,, HitLocation + 5*Z, rotator(HitNormal) + RRoll);
				}
				else if (Inv.Tag == 'DoubleSpeedPickup')
				{
					if (A.IsA('Mover') || A == None || Inv.Rotation.Roll!=0)
						NewInv = Spawn(Class'DoubleSpeed',,, Inv.Location, Inv.Rotation);
					else
						NewInv = Spawn(Class'X_DoubleSpeed',,, HitLocation + 5*Z, rotator(HitNormal) + RRoll);
				}
				else if (Inv.Tag == 'BattleSuitPickup')
				{
					if (A.IsA('Mover') || A == None || Inv.Rotation.Roll!=0)
						NewInv = Spawn(Class'BattleSuit',,, Inv.Location, Inv.Rotation);
					else
						NewInv = Spawn(Class'X_BattleSuit',,, HitLocation + 5*Z, rotator(HitNormal) + RRoll);
				}
				else if (Inv.Tag == 'HelmetPickup')
				{
					if (A.IsA('Mover') || A == None || Inv.Rotation.Roll!=0)
						NewInv = Spawn(Class'Helmet',,, Inv.Location, Inv.Rotation);
					else
						NewInv = Spawn(Class'X_Helmet',,, HitLocation + 3*Z, rotator(HitNormal) + RRoll);
				}
				else if (Inv.Class==Class'Botpack.ChainSaw')
				{
					if (A.IsA('Mover') || A == None || Inv.Rotation.Roll!=0)
						NewInv = Spawn(Class'BersChainSaw',,, Inv.Location, Inv.Rotation);
					else
						NewInv = Spawn(Class'X_ChainSaw',,, HitLocation + 2*Z, rotator(HitNormal) + RRoll);
				}
				else if (Inv.Class==Class'Botpack.Enforcer' || Inv.Class==Class'UnrealShare.AutoMag')
				{
					if (A.IsA('Mover') || A == None || Inv.Rotation.Roll!=0)
						NewInv = Spawn(Class'UBersEnforcer',,, Inv.Location, Inv.Rotation);
					else
						NewInv = Spawn(Class'X_Enforcer',,, HitLocation + 2*Z, rotator(HitNormal) + RRoll);
				}
				else if (Inv.Class==Class'Botpack.ImpactHammer')
				{
					if (A.IsA('Mover') || A == None || Inv.Rotation.Roll!=0)
						NewInv = Spawn(Class'UBersImpactHammer',,, Inv.Location, Inv.Rotation);
					else
						NewInv = Spawn(Class'X_ImpactHammer',,, HitLocation + 2*Z, rotator(HitNormal) + RRoll);
				}
				else if (Inv.Class==Class'Botpack.minigun2' || Inv.Class==Class'UnrealI.Minigun')
				{
					if (A.IsA('Mover') || A == None || Inv.Rotation.Roll!=0)
						NewInv = Spawn(Class'UBersMinigun2',,, Inv.Location, Inv.Rotation);
					else
						NewInv = Spawn(Class'X_Minigun',,, HitLocation + 2*Z, rotator(HitNormal) + RRoll);
				}
				else if (Inv.Class==Class'Botpack.PulseGun')
				{
					if (A.IsA('Mover') || A == None || Inv.Rotation.Roll!=0)
						NewInv = Spawn(Class'UBersPulseGun',,, Inv.Location, Inv.Rotation);
					else
						NewInv = Spawn(Class'X_PulseGun',,, HitLocation + 2*Z, rotator(HitNormal) + RRoll);
				}
				else if (Inv.Class==Class'Botpack.Ripper' || Inv.Class==Class'UnrealI.Razorjack')
				{
					if (A.IsA('Mover') || A == None || Inv.Rotation.Roll!=0)
						NewInv = Spawn(Class'UBersRipper',,, Inv.Location, Inv.Rotation);
					else
						NewInv = Spawn(Class'X_Ripper',,, HitLocation + 2*Z, rotator(HitNormal) + RRoll);
				}
				else if (Inv.Class==Class'Botpack.ShockRifle' || Inv.Class==Class'UnrealShare.ASMD')
				{
					if (A.IsA('Mover') || A == None || Inv.Rotation.Roll!=0)
						NewInv = Spawn(Class'UBersShockRifle',,, Inv.Location, Inv.Rotation);
					else
						NewInv = Spawn(Class'X_ShockRifle',,, HitLocation + 2*Z, rotator(HitNormal) + RRoll);
				}
				else if (Inv.Class==Class'Botpack.SuperShockRifle')
				{
					if (A.IsA('Mover') || A == None || Inv.Rotation.Roll!=0)
						NewInv = Spawn(Class'UBersSuperShockRifle',,, Inv.Location, Inv.Rotation);
					else
						NewInv = Spawn(Class'X_SuperShockRifle',,, HitLocation + 2*Z, rotator(HitNormal) + RRoll);
				}
				else if (Inv.Class==Class'Botpack.SniperRifle' || Inv.Class==Class'UnrealI.Rifle')
				{
					if (A.IsA('Mover') || A == None || Inv.Rotation.Roll!=0)
						NewInv = Spawn(Class'UBersSniperRifle',,, Inv.Location, Inv.Rotation);
					else
						NewInv = Spawn(Class'X_SniperRifle',,, HitLocation + 2*Z, rotator(HitNormal) + RRoll);
				}
				else if (Inv.Class==Class'Botpack.ut_biorifle' || Inv.Class==Class'UnrealI.GESBioRifle')
				{
					if (A.IsA('Mover') || A == None || Inv.Rotation.Roll!=0)
						NewInv = Spawn(Class'UBersUT_BioRifle',,, Inv.Location, Inv.Rotation);
					else
						NewInv = Spawn(Class'X_BioRifle',,, HitLocation + 2*Z, rotator(HitNormal) + RRoll);
				}
				else if (Inv.Class==Class'Botpack.UT_EightBall' || Inv.Class==Class'UnrealShare.Eightball')
				{
					if (A.IsA('Mover') || A == None || Inv.Rotation.Roll!=0)
						NewInv = Spawn(Class'UBersUT_EightBall',,, Inv.Location, Inv.Rotation);
					else
						NewInv = Spawn(Class'X_RocketLauncher',,, HitLocation + 2*Z, rotator(HitNormal) + RRoll);
				}
				else if (Inv.Class==Class'Botpack.UT_FlakCannon' || Inv.Class==Class'UnrealI.FlakCannon')
				{
					if (A.IsA('Mover') || A == None || Inv.Rotation.Roll!=0)
						NewInv = Spawn(Class'UBersUT_FlakCannon',,, Inv.Location, Inv.Rotation);
					else
						NewInv = Spawn(Class'X_FlakCannon',,, HitLocation + 2*Z, rotator(HitNormal) + RRoll);
				}
				else if (Inv.Class==Class'Botpack.WarheadLauncher' && !class'XPickupsMut'.default.bRXMode)
				{
					if (A.IsA('Mover') || A == None || Inv.Rotation.Roll!=0)
						NewInv = Spawn(Class'UBersWarheadLauncher',,, Inv.Location, Inv.Rotation);
					else
						NewInv = Spawn(Class'X_WarheadLauncher',,, HitLocation + 2*Z, rotator(HitNormal) + RRoll);
				}
				else if (Inv.Class==Class'Botpack.Translocator')
					NewInv = Spawn(Class'UbersTranslocator',,, Inv.Location, Inv.Rotation);
				else if (Inv.IsA('Weapon'))
				{
					if (!A.IsA('Mover') && A != None && Inv.Rotation.Roll==0)
					{
						NewInv = Spawn(Class'X_FlakCannon',,, HitLocation + 3*Z, rotator(HitNormal) + RRoll);
						PickupCharger(NewInv).PickupClass = Inv.Class;
					}
				}
				else if (Inv.Class==Class'Botpack.Armor2')
				{
					if (A.IsA('Mover') || A == None || Inv.Rotation.Roll!=0)
						NewInv = Spawn(Class'UBersArmor',,, Inv.Location, Inv.Rotation);
					else
						NewInv = Spawn(Class'X_Armor',,, HitLocation + 3*Z, rotator(HitNormal) + RRoll);
				}
				else if (Inv.Class==Class'Botpack.UT_ShieldBelt' || Inv.Class==Class'UnrealShare.ShieldBelt')
				{
					if (A.IsA('Mover') || A == None || Inv.Rotation.Roll!=0)
						NewInv = Spawn(Class'UBers_ShieldBelt',,, Inv.Location, Inv.Rotation);
					else
						NewInv = Spawn(Class'X_ShieldBelt',,, HitLocation + 3*Z, rotator(HitNormal) + RRoll);
				}
				else if (Inv.Class==Class'Botpack.ThighPads')
				{
					if (A.IsA('Mover') || A == None || Inv.Rotation.Roll!=0)
						NewInv = Spawn(Class'UBersThighPads',,, Inv.Location, Inv.Rotation);
					else
						NewInv = Spawn(Class'X_ThighPads',,, HitLocation + 3*Z, rotator(HitNormal) + RRoll);
				}
				else if (Inv.Class==Class'Botpack.UT_JumpBoots' || Inv.Class==Class'UnrealI.JumpBoots')
				{
					if (A.IsA('Mover') || A == None || Inv.Rotation.Roll!=0)
						NewInv = Spawn(Class'UBersJumpBoots',,, Inv.Location, Inv.Rotation);
					else
						NewInv = Spawn(Class'X_JumpBoots',,, HitLocation + 5*Z, rotator(HitNormal) + RRoll);
				}
				else if (Inv.Class==Class'Botpack.UDamage' || Inv.Class==Class'UnrealShare.Amplifier')
				{
					if (A.IsA('Mover') || A == None || Inv.Rotation.Roll!=0)
						NewInv = Spawn(Class'UBersUDamage',,, Inv.Location, Inv.Rotation);
					else
						NewInv = Spawn(Class'X_UDamage',,, HitLocation + 5*Z, rotator(HitNormal) + RRoll);
				}
				else if (Inv.Class==Class'Botpack.UT_Invisibility' || Inv.Class==Class'UnrealI.Invisibility')
				{
					if (A.IsA('Mover') || A == None || Inv.Rotation.Roll!=0)
						NewInv = Spawn(Class'UBersInvisibility',,, Inv.Location, Inv.Rotation);
					else
						NewInv = Spawn(Class'X_Invisibility',,, HitLocation + 5*Z, rotator(HitNormal) + RRoll);
				}
				else if (Inv.Class==Class'Botpack.UT_Stealth')
				{
					if (A.IsA('Mover') || A == None || Inv.Rotation.Roll!=0)
					{
						NewInv = Spawn(Class'UBersInvisibility',,, Inv.Location, Inv.Rotation);
						NewInv.Charge = 9999999;
					}
					else
					{
						NewInv = Spawn(Class'X_Invisibility',,, HitLocation + 5*Z, rotator(HitNormal) + RRoll);
						X_Invisibility(NewInv).PickupCharge = 9999999;
					}
				}
				else if (Inv.Class==Class'Botpack.MedBox' || Inv.Class==Class'UnrealShare.Health')
				{
					if (A.IsA('Mover') || A == None || Inv.Rotation.Roll!=0)
						NewInv = Spawn(Class'UBersHealthPack',,, Inv.Location, Inv.Rotation);
					else
						NewInv = Spawn(Class'X_MedPack',,, HitLocation + 3*Z, rotator(HitNormal) + RRoll);
				}
				else if (Inv.Class==Class'Botpack.HealthPack' || Inv.Class==Class'UnrealShare.SuperHealth')
				{
					if (A.IsA('Mover') || A == None || Inv.Rotation.Roll!=0)
						NewInv = Spawn(Class'UBersSuperHealth',,, Inv.Location, Inv.Rotation);
					else
						NewInv = Spawn(Class'X_SuperHealth',,, HitLocation + 3*Z, rotator(HitNormal) + RRoll);
				}
				else if (Inv.Class==Class'Botpack.HealthVial')
					NewInv = Spawn(Class'NewVial',,, Inv.Location + vect(0,0,4), Inv.Rotation);
				else if (Inv.Class==Class'Botpack.BioAmmo' || Inv.Class==Class'UnrealI.Sludge')
					NewInv = Spawn(Class'BioAmmoB',,, Inv.Location, Inv.Rotation);
				else if (Inv.Class==Class'Botpack.BladeHopper' || Inv.Class==Class'UnrealI.RazorAmmo')
					NewInv = Spawn(Class'BladeHopperB',,, Inv.Location, Inv.Rotation);
				else if (Inv.Class==Class'Botpack.BulletBox' || Inv.Class==Class'UnrealI.RifleAmmo')
					NewInv = Spawn(Class'BulletBoxB',,, Inv.Location, Inv.Rotation);
				else if (Inv.Class==Class'Botpack.BulletBox' || Inv.Class==Class'UnrealI.RifleAmmo')
					NewInv = Spawn(Class'BulletBoxB',,, Inv.Location, Inv.Rotation);
				else if (Inv.Class==Class'UnrealI.RifleRound')
					NewInv = Spawn(Class'BotPack.RifleShell',,, Inv.Location, Inv.Rotation);
				else if (Inv.Class==Class'Botpack.FlakAmmo' || Inv.Class==Class'UnrealI.FlakBox')
					NewInv = Spawn(Class'FlakAmmoB',,, Inv.Location, Inv.Rotation);
				else if (Inv.Class==Class'Botpack.MiniAmmo' || Inv.Class==Class'UnrealShare.ShellBox')
					NewInv = Spawn(Class'MiniAmmoB',,, Inv.Location, Inv.Rotation);
				else if (Inv.Class==Class'Botpack.EClip' || Inv.Class==Class'UnrealShare.Clip')
					NewInv = Spawn(Class'EClipB',,, Inv.Location, Inv.Rotation);
				else if (Inv.Class==Class'Botpack.PAmmo')
					NewInv = Spawn(Class'PAmmoB',,, Inv.Location, Inv.Rotation);
				else if (Inv.Class==Class'Botpack.RocketPack' || Inv.Class==Class'UnrealShare.RocketCan')
					NewInv = Spawn(Class'RocketPackB',,, Inv.Location, Inv.Rotation);
				else if (Inv.Class==Class'Botpack.ShockCore' || Inv.Class==Class'UnrealShare.ASMDAmmo')
					NewInv = Spawn(Class'ShockCoreB',,, Inv.Location, Inv.Rotation);

				if (Inv.MyMarker != None && NewInv != None)
				{
					NewInv.MyMarker = Inv.MyMarker;
					NewInv.MyMarker.markedItem = NewInv;
					Inv.MyMarker = None;
				}

				if (Inv.IsA('Weapon') && NewInv.IsA('PickupCharger'))
					PickupCharger(NewInv).bForceRot = True;

				if (NewInv != None)
				{
					/*if (NewInv.IsA('PickupCharger'))
						PickupCharger(NewInv).bMutActivated = True;*/

					NewInv.Event = Inv.Event;
					NewInv = None;
					Inv.Destroy();
				}
			}
		}
}

function Timer()
{
	local Pawn P;
	local Weapon newWeapon;

	For ( P=Level.PawnList; P!=None; P=P.nextPawn )
	{
	
	if (Level.Game.IsA('DeathMatchPlus'))
		{

		If (PlayerPawn(P) != None)
		{
		if ( PlayerPawn(P).IsA('Spectator') || PlayerPawn(P).Health <= 0 ||
		(DeathMatchPlus(Level.Game).bRequireReady && (DeathMatchPlus(Level.Game).CountDown > 0)) )
		return;

		}

		newWeapon = None;

		if (Level.Game.DefaultWeapon == Class'ImpactHammer' && P.FindInventoryType(Class'ImpactHammer') != None && !P.IsA('Bot'))
		{
			newWeapon = Weapon(P.FindInventoryType(Class'ImpactHammer'));
			newWeapon.Destroy();
			newWeapon = None;
			newWeapon = Spawn(Class'UBersImpactHammer');
		
		if (newWeapon != None)
		{
			newWeapon.bTossedOut = false;
			newWeapon.Instigator = P;
			newWeapon.RespawnTime = 0.0;
			newWeapon.bHeldItem = true;
			newWeapon.BecomeItem();
			P.AddInventory(newWeapon);
			newWeapon.GiveAmmo(P);
			newWeapon.SetSwitchPriority(P);
			newWeapon.WeaponSet(P);
		}

		}

		newWeapon = None;

		if (P.FindInventoryType(Class'Translocator') != None && !P.IsA('Bot'))
		{
			newWeapon = Weapon(P.FindInventoryType(Class'Translocator'));
			newWeapon.Destroy();
			newWeapon = None;
			newWeapon = Spawn(Class'UBersTranslocator');
		
		if (newWeapon != None)
		{
			newWeapon.bTossedOut = false;
			newWeapon.Instigator = P;
			newWeapon.RespawnTime = 0.0;
			newWeapon.bHeldItem = true;
			newWeapon.BecomeItem();
			P.AddInventory(newWeapon);
			newWeapon.GiveAmmo(P);
			newWeapon.SetSwitchPriority(P);
			newWeapon.WeaponSet(P);
		}

		}

		newWeapon = None;

		if (P.FindInventoryType(Class'ChainSaw') != None && !P.IsA('Bot'))
		{
			newWeapon = Weapon(P.FindInventoryType(Class'ChainSaw'));
			newWeapon.Destroy();
			newWeapon = None;
			newWeapon = Spawn(Class'BersChainSaw');
		
		if (newWeapon != None)
		{
			newWeapon.bTossedOut = false;
			newWeapon.Instigator = P;
			newWeapon.RespawnTime = 0.0;
			newWeapon.bHeldItem = true;
			newWeapon.BecomeItem();
			P.AddInventory(newWeapon);
			newWeapon.GiveAmmo(P);
			newWeapon.SetSwitchPriority(P);
			newWeapon.WeaponSet(P);
		}

		}

		newWeapon = None;
		
		if (P.FindInventoryType(Class'Enforcer') != None)
		{
			newWeapon = Weapon(P.FindInventoryType(Class'Enforcer'));
			newWeapon.Destroy();
			newWeapon = None;
			newWeapon = Spawn(Class'UBersEnforcer');
		
		if (newWeapon != None)
		{
			newWeapon.bTossedOut = false;
			newWeapon.Instigator = P;
			newWeapon.RespawnTime = 0.0;
			newWeapon.bHeldItem = true;
			newWeapon.BecomeItem();
			P.AddInventory(newWeapon);
			newWeapon.GiveAmmo(P);
			newWeapon.SetSwitchPriority(P);
			newWeapon.WeaponSet(P);
		}

		}
	}
	}
}