class Player_Controller extends xPlayer;

var Spot_Info Spot_Info;

var vector cloc;
var float maxdistance, elevation;
var bool flag;
var bool bMouseSteering;
var string dbug;

var bool bLemmeGoPls;

var config class<Car_R> VehicleClass;

var Car_R Rc;
var bool bSelectingCar;

var vector StartOffset;

replication
{
reliable if(Role==ROLE_Authority )
        VehicleClass, maxdistance, Rc;
reliable if ( Role < ROLE_Authority )
		SetVehicleClass, GetCar;
}

function PostBeginPlay()
{
    super.PostBeginPlay();
    cloc = Location;
	elevation=100;
    cloc.y-=elevation;
    cloc.Z+=elevation;
    NetUpdateTime=level.TimeSeconds-1;
    SetTimer(0.5, True);
}

exec function Celebrate()
{
    TriggerEvent('Celebrate', Self, None);
}

function bool IsFieldSpectating()
{
	return ( IsInState('PlayerSpectating'));
}

function Timer()
{
	if (!bSelectingCar && PlayerReplicationInfo.bWaitingPlayer && (Rc == None))
		SelectCar();
   	if (Rc != None)
		SetTimer(0, False);
}

function BecomeSpectator()
{
if (Rc != None)
	{
		Game_Deathmatch(Level.Game).BecomeSpectator(Self);
		bLemmeGoPls = True;
		Rc.KDriverLeave(True);
		Rc.Destroy();
		bLemmeGoPls = False;
	}
	Rc = None;
	PreviousPawnClass = Default.PawnClass;
	PawnClass = Default.PawnClass;
	Super.BecomeSpectator();
}

function PawnDied(Pawn P)
{
	Rc = None;
	PreviousPawnClass = Default.PawnClass;
	PawnClass = Default.PawnClass;
    Super.PawnDied(P);
}

function Restart()
{
	Super.Restart();

	if (Rc == None)
		GetCar();

   	if ((Rc != None) && (Vehicle(Pawn) == None))
	{
		Rc.TryToDrive(Pawn);
		Rc.SetPhysics(PHYS_karma);
		//Rc.SetSkin(Rc.Skins[Rc.SkinOffset]);
	}
}

function GetCar()
{
	local PlayerStart Spot;
	local vector SLoc;
	local rotator SRot;

	Spot = PlayerStart(StartSpot);

    SRot = Spot.Rotation;
	SLoc = Spot.Location + (StartOffset >> SRot);

	Rc = Spawn(VehicleClass, , , SLoc, SRot);
	Rc.bEjectPassengersWhenFlipped = false;
	Rc.bCanPickupInventory = true;
	Rc.bEjectDriver = false;
}

function SetVehicleClass(class<Car_R> NewCar)
{
	VehicleClass = NewCar;
}

function SelectCar()
{
    bSelectingCar = True;
	ClientOpenMenu("R.Menu_CarSelection");
}

event PlayerCalcView(out actor ViewActor, out vector CameraLocation, out rotator CameraRotation )
{
    local vector step;

    if (Rc != none)
    {
       if (!LineOfSight(curcam, rc))
       {
          if (maxdistance > 80 && elevation > 50)
          {
          maxdistance = maxdistance - 0.5;
          elevation = elevation - 0.5;
          CameraSwivel = CameraTrack(rc, 0);
          }
       }
       else
       {
           if (maxdistance < 181 && elevation < 101)
           {
           maxdistance = maxdistance + 0.5;
           elevation = elevation + 0.5;
           CameraSwivel = CameraTrack(rc, 0);
           }
       }
       if (Rc.cameraPosition == 1)
       {
           bBehindView=true;

           ViewActor = ViewTarget;
           step = vector(Normalize(rotator(ViewActor.Location-cloc ))) * maxdistance;
            if( vsize(ViewActor.Location-cloc ) > maxdistance)
            {
            cloc= ViewActor.Location-step;
            }
            CameraLocation=cloc;
            CameraLocation.z+=elevation;
            if (bMouseSteering == false)
            {
            CameraRotation=rotator( ViewActor.Location - CameraLocation);
            }
       }
       if (Rc.cameraPosition == 2)
       {
       bBehindView=true;

       ViewActor = ViewTarget;

       step = vector(Normalize(rotator(ViewActor.Location-cloc ))) * 250;

       if( vsize(ViewActor.Location-cloc ) > maxdistance)
       {
       cloc= ViewActor.Location-step;
       }
       CameraLocation=cloc;
       CameraLocation.z+=80;
       if (bMouseSteering == false)
       {
       CameraRotation=rotator( ViewActor.Location - CameraLocation);
       }
      }
      if (Rc.cameraPosition == 3)
      {
       CameraLocation = rc.bcamera.Location;
       CameraRotation = rc.bcamera.Rotation;
      }
    }
    else
    {
       bBehindView=true;

       ViewActor = ViewTarget;
       step = vector(Normalize(rotator(ViewActor.Location-cloc ))) * maxdistance;
       if( vsize(ViewActor.Location-cloc ) > maxdistance)
       {
           cloc= ViewActor.Location-step;
       }
       CameraLocation=cloc;
       CameraLocation.z+=elevation;
       if (bMouseSteering == false)
       {
       CameraRotation=rotator( ViewActor.Location - CameraLocation);
       }
    }
     if (Rc != none)
    {
      if (Rc.bRearview)
      {
         ViewActor = ViewTarget;
         CameraLocation = Rc.Location + (Rc.rearbumpercamoffset >> Rc.Rotation);
         CameraLocation.Z += 80;
         CameraRotation.yaw-=32000;
         if (bMouseSteering == false)
         {
          CameraRotation=rotator( ViewActor.Location - CameraLocation);
         }
      }
    }
}

defaultproperties
{
     maxdistance=180.000000
     elevation=100.000000
     VehicleClass=Class'R.Car_Cardette'
     StartOffset=(X=125.000000)
     bBehindView=True
     bFreeCamSwivel=True
     bGodMode=True
}
