class Car_R extends ONSWheeledCraft
CacheExempt
	config(Cars_R);

// Skin information
struct VehicleSkinInfo
{
	var() config string Description;
	var() config string SkinName;
};

var (Setup_Car) config array<VehicleSkinInfo> VehicleSkins;
var (Setup_Car) config int	CurrentSkin;
var int			SkinOffset;
var int ServerCurrentSkin;

// Attachments
var Actor			Attachment;
var vector			AttachmentOffset;

var material originalskin;

var array<string> VehicleDesc[];
var array<string> VehicleSkin[];

var Material		DesiredSkin;

// Quick adjustment value to tweak vehicle preview's vertical location
var() float			SpinnyAdjuster;
var() float			SpinnyScaler;

var bool                bRearview;

//Camera position chase,far chase,bumpercamera
var int cameraPosition;
//Bumpercamera creation and offset
var Camera_bumper bcamera;
var vector bumpercamoffset;
var vector rearbumpercamoffset;

var localized string ResumeCountString, ResumeReadyString;

// Control resetting
var int 			ResetTimer;
// Control freezing
var int             ResetFrozen;

var ScriptedTexture STEnvMap;
var bool bUpdateEnvMapHolder;
var bool bEnvmap;
var Material Skin;

var Shadow_CarProjector StaticShadow;

//waypoints
var Actor			LastWaypoint;

//activepowerup
var Pickup_Default activepowerup;

//max speed for speedpowerp
var float maxratio;
//old gearratio after end of speedpowerup
var float oldratio;

var bool bFrozen;
var bool bReset;
var bool bLock;

replication
{
    reliable if(Role == ROLE_Authority)
        Skinoffset,DesiredSkin,ServerCurrentSkin,Originalskin,bFrozen,bReset,bLock,resettimer,resetfrozen;
}

simulated function postbeginplay()
{
        bDrawVehicleShadow=true;
        StaticShadow = Spawn(class'Shadow_CarProjector', self, '', Location);
        StaticShadow.ShadowActor       = Self;
        StaticShadow.bBlobShadow       = false;
        StaticShadow.LightDirection    = Normal(vect(1,1,6));
        StaticShadow.LightDistance     = 1200;
        StaticShadow.MaxTraceDistance  = ShadowMaxTraceDist;
        StaticShadow.CullDistance      = ShadowCullDistance;
        StaticShadow.InitShadow();

        bcamera = spawn(class'Camera_bumper', self,, Location + (bumpercamoffset >> Rotation) );
        bcamera.SetBase(self);
        bcamera.SetRelativeRotation( rot(0,0,0) );

        oldratio=gearratios[4];
        NetUpdateTime=-1;

        if (STEnvMap == None)
{
			STEnvMap = new(None) class'ScriptedTexture';
			STEnvMap.SetSize(512, 512);
			bUpdateEnvMapHolder = True;
}
STEnvMap.Client = Self;

}



function SetEnvMapSkin()
{
	Modifier(Combiner(Skins[0]).Material1).Material = STEnvMap;  //Specular instead of material

	return;
}

simulated function RenderTexture(ScriptedTexture Tex)
{
	local rotator R;

	if (Tex != STEnvMap) return;

	R = Controller.Rotation;
	R.Yaw -= 32768;
	R.Roll += 32768;

	//Tex.DrawPortal(0, 128, 256, 256, Self, Location, R, 135, True);
    Tex.DrawPortal(0, 0, 512,512, Self, Location + vect(0,0,50), R, 115, True);

	return;
}

function SetSkin(Material NewSkin)
{
	if (!NewSkin.IsA('Combiner')) bEnvMap = False;
	if (!bEnvMap && NewSkin.IsA('Combiner'))
	{
		Skins[0] = NewSkin.FallbackMaterial;
		Skin = NewSkin.FallbackMaterial;
		log(skins[0]);
	}
	else
	{
		Skins[0] = NewSkin;
		Skin = NewSkin;
		SetEnvMapSkin();
	}

	return;
}

simulated function vector GetAttachOffset()
{
	local Coords AttachCoords;

	AttachCoords = GetBoneCoords('Attachment');

    return AttachCoords.Origin;
}

simulated function PostNetBeginPlay()
{
Super.PostNetBeginPlay();

ServerCurrentSkin = default.CurrentSkin;

Super.PostNetBeginPlay();

	Skins[default.SkinOffset] = Material(DynamicLoadObject(Default.VehicleSkin[ServerCurrentSkin], class'Material'));
	if (DesiredSkin != None)
		Skins[default.SkinOffset] = DesiredSkin;

repskin = skins[default.SkinOffset];
log(repskin);

}

function bool TryToDrive(Pawn P)
{
	if (FlipTimeLeft > 0)
		return false;

	if (NeedsFlip())
	{
		Flip(vector(P.Rotation), 1);
		return false;
	}

	P.PlayerReplicationInfo.CurrentVehicle = Self;

	bTeamLocked = False;

	return Super(SVehicle).TryToDrive(P);
}

exec function Changecamera()
{
bRearview=false;
if (cameraPosition < 3)
{
cameraPosition += 1;
}
else
{
cameraPosition = 1;
}
}

exec function Rearview()
{
bRearview=true;
}

exec function ResetCar()
{
	local vector NewLoc;
	local rotator NewRot;
	local PlayerStart Spot;

	if (LastWaypoint != None)
		NewLoc = LastWaypoint.Location;
	else
	{
		foreach AllActors(class'PlayerStart', Spot)
		{
			if (Level.Game.bTeamGame)
				if (PlayerReplicationInfo.Team.TeamIndex != Spot.TeamNumber)
					continue;
			NewLoc = Spot.Location;
			LastWaypoint = Spot;
			Break;
		}
	}

	NewRot = Rotation;
	NewRot.Roll = 0;
	NewRot.Pitch = 0;

	NewLoc.Z = LastWayPoint.Location.Z + 50;

	if (LastWaypoint.IsA('PlayerStart'))
		NewRot = LastWaypoint.Rotation;

	OldRotation = NewRot;
	LastOnGroundLocation = NewLoc;
	LastOnGroundTime = Level.TimeSeconds + 3;

	//Game_Deathmatch(Level.Game).PlayerReset(Controller, Self);
    NetUpdateTime=-1;
    bFrozen=false;
    bReset=true;
	SetPhysics(PHYS_None);
	SetLocation(NewLoc);
	SetRotation(NewRot);
	ResetTimer = 105;
	SetTimer(1, True);
}

simulated event Tick(float deltatime)
{


Super.Tick(DeltaTime);

 // ensure that the client updates envmap
	if (bEnvMap && (Skins.Length > 0) && (Skins[0] != None) && (Modifier(Combiner(Skins[0]).Material1).Material != STEnvMap) && (STEnvMap != None)) Modifier(Combiner(Skins[0]).Material1).Material = STEnvMap;
	if (bEnvMap && (STEnvMap != None)) STEnvMap.Revision++;

 if (bFrozen)
 {
 SetPhysics(PHYS_None);
 setlocation(self.location);
 }
 else if (!bReset)
 {
  SetPhysics(PHYS_Karma);
 }
 if (bLock)
 {
  Steering=0;
 }
}

simulated function Timer()
{
	if (ResetTimer >= 0)
	{
		if (ResetTimer != 108)
		{
		    ReceiveLocalizedMessage( class'Message_Gameplay', ResetTimer, PlayerReplicationInfo );
			ResetTimer ++;
		}
		else
		{
			ReceiveLocalizedMessage( class'Message_Gameplay', ResetTimer, PlayerReplicationInfo );
			SetPhysics(PHYS_Karma);
			SVehicleUpdateParams();
			bReset=false;
			ResetTimer=-1;
			setTimer(0.5,true);
		}
	}
	if (ResetFrozen >=0)
	{
	  if (ResetFrozen != 112)
	  {
	     ReceiveLocalizedMessage( class'Message_Gameplay', ResetFrozen, PlayerReplicationInfo );
	     ResetFrozen ++;
	  }
	  else
	  {
 	      ReceiveLocalizedMessage( class'Message_Gameplay', ResetFrozen, PlayerReplicationInfo );
	      SetPhysics(PHYS_Karma);
	      SVehicleUpdateParams();
	      bFrozen=false;
	      resetFrozen=-1;
	      setTimer(0.5,true);
	  }
	}
	if (bLock)
	{
	bLock=false;
	}
}

function Suicide()
{
	Super.Suicide();
	Destroy();
}

simulated event FellOutOfWorld(eKillZType KillType)
{
    Destroy();
}

defaultproperties
{
     NetUpdateTime=-1;
     SpinnyAdjuster=500.000000
     SpinnyScaler=2.000000
     cameraPosition=1
     bumpercamoffset=(X=40.000000,Z=20.000000)
     ResetTimer=-1
     ResetFrozen=-1
     maxratio=4.500000
     WheelPenScale=2.500000
     WheelSuspensionOffset=-3.000000
     EngineRPMSoundRange=5500.000000
     DustSlipRate=0
     DustSlipThresh=0
     IdleSound=Sound'Car_Engine.engine_r'
     StartUpSound=Sound'ONSVehicleSounds-S.RV.RVStart01'
     ShutDownSound=Sound'ONSVehicleSounds-S.RV.RVStop01'
     StartUpForce="RVStartUp"
     ImpactDamageSounds(0)=None
     ImpactDamageSounds(1)=None
     ImpactDamageSounds(2)=None
     ImpactDamageSounds(3)=None
     ImpactDamageSounds(4)=None
     ImpactDamageSounds(5)=None
     ImpactDamageSounds(6)=None
     CrosshairTexture=None
     ControllerClass=None
     bIgnoreOutOfWorld=True
     bEnvMap=True
     SparkEffectClass=none
}
