class UR_Weap_MegaRox extends UTWeap_RocketLauncher;

var class<Projectile> CenterRocketClass, SubRocketClass;

//We can't use CustomFire with FireModeNum, which is very annoying, so we use ProjectileFire and over-write it instead.
/*simulated function CustomFire()
{
	local int i,j;
   	local vector RealStartLoc, AimDir, YDir, ZDir;
	local Projectile Proj;
	local float Mag;

	IncrementFlashCount();

	if (Role == ROLE_Authority)
	{
		// this is the location where the projectile is spawned
		RealStartLoc = GetPhysicalFireStartLoc();
		// get fire aim direction
		GetAxes(GetAdjustedAim(RealStartLoc),AimDir, YDir, ZDir);

		// special center shard
		Proj = Spawn(CenterRocketClass,,, RealStartLoc);
		if (Proj != None)
		{
			Proj.Init(AimDir);
		}

		// one shard in each of 9 zones (except center)
		for ( i=-1; i<2; i++)
		{
			for ( j=-1; j<2; j++ )
			{
				if ( (i != 0) || (j != 0) )
				{
					Mag = (abs(i)+abs(j) > 1) ? 0.7 : 1.0;
					Proj = Spawn(SubRocketClass,,, RealStartLoc);
					if (Proj != None)
					{
						Proj.Init(AimDir + (0.3 + 0.7*FRand())*Mag*i*SpreadDist*YDir + (0.3 + 0.7*FRand())*Mag*j*SpreadDist*ZDir );
					}
				}
			}
	    }
	}
	`Log("********************custom fire****************************");
	Gotostate('WeaponPlayingFire');
	`Log("********************custom fire load****************************");
}

simulated function Projectile ProjectileFire()
{
	if (AmmoCount < MaxAmmoCount)
	{
		AmmoCount += 1;
	}
`Log("********************projectile fire****************************");
	return none;
}

defaultproperties
{
	WeaponFireTypes(0)=EWFT_Custom
	WeaponFireTypes(1)=EWFT_Projectile
	WeaponProjectiles(0)=class'UTProj_Rocket'//Never used.
	WeaponProjectiles(1)=class'UTProj_Rocket'//Never used.

	//CenterShardClass=class'UR_Proj_MajorRocket'
	//CenterShardClass=class'UR_Proj_MinorRocket'

	ShotCost(0)=1
	ShotCost(1)=-1
}*/

simulated state UR_Reload extends WeaponLoadAmmo
{
	simulated function BeginFire(byte FireModeNum)
	{
		if (Role == ROLE_Authority)
		{
			if (FireModeNum == 1)
			{
				ReloadAmmmoMegaRox();
				GotoState('Active');
			}
			else if (FireModeNum == 0)
			{
				//LoadedFireMode = ERocketFireMode((int(LoadedFireMode) + 1) % RFM_Max);
				//WeaponPlaySound(AltFireModeChangeSound);
				GotoState('UR_FireState');
			}
			global.BeginFire(FireModeNum);
		}
	}

	function ReloadAmmmoMegaRox()
	{
		if (AmmoCount < MaxAmmoCount)
		{
			AmmoCount += 1;
		}
	}
	
	begin :
	if (Role == ROLE_Authority)
	{
		ReloadAmmmoMegaRox();
	}
}

simulated state UR_FireState extends WeaponFiring
{
  simulated function BeginFire(byte FireModeNum)
  {
    if (Role == ROLE_Authority)
	{
		DoFireEffect();
	}
}

function DoFireEffect()
{
    local int p, MaxProj;
    local Vector StartProj, X,Y,Z;
    local Rotator Aim;
    local Vector FireLocation;
	local Projectile spawnRocket;

    Instigator.MakeNoise(1.0);
    self.GetViewAxes(X,Y,Z);

    StartProj = GetPhysicalFireStartLoc();

    if ( WorldInfo.NetMode != NM_DedicatedServer )
	{
		//`Log("Ammo: "@AmmoCount);
		//`Log("Load: "@MaxLoadCount);
	  if (MaxLoadCount > (AmmoCount + 1))
	  {
		MaxProj = (AmmoCount + 1);
	  }
	  else
	  {
		MaxProj = MaxLoadCount;
	  }
	  //`Log("MaxProj"@MaxProj);
      for ( p=0; p<MaxProj; p++ )
      {
 		//Firelocation = StartProj - 2*((Sin(p*2*PI/MaxLoadCount)*8 - 7)*Y - (Cos(p*2*PI/MaxLoadCount)*8 - 7)*Z) - X * 8 * FRand();
		Firelocation = StartProj - 2*((Sin(p*2*PI/MaxProj)*8 - 7)*Y - (Cos(p*2*PI/MaxProj)*8 - 7)*Z) - X * 8 * FRand();
 		Aim = rotator(StartProj + X*FireOffset.X + Y*FireOffset.Y + Z*FireOffset.Z - Firelocation);
        spawnRocket = spawn(LoadedRocketClass,,,FireLocation,Aim);
        spawnRocket.init(vector(aim));
      }
	  if (MaxProj > 1)
	  {
		MaxProj -= 1;
	  }
	  AmmoCount -= (MaxProj);
    }
}

  begin :
  if (Role == ROLE_Authority)
  {
		DoFireEffect();
  }
}

defaultproperties
{
  FireOffset=(X=50.000000,Y=10.000000,Z=0.000000)
  FiringStatesArray(0)=UR_FireState
  FiringStatesArray(1)=UR_Reload
  MaxLoadCount = 10
  AmmoCount=50
  LockerAmmoCount=20
  MaxAmmoCount=70
  ItemName="Mega Rox"
  PickupMessage="Mega Rox"
}