home *** CD-ROM | disk | FTP | other *** search
/ Game Level Design / GLDesign.bin / Software / UnrealEngine2Runtime / UE2Runtime-22262001_Demo.exe / Engine / Classes / SVehicle.uc < prev    next >
Text File  |  2003-06-23  |  15KB  |  559 lines

  1. class SVehicle extends Pawn
  2.     native
  3.     abstract;
  4.  
  5. cpptext
  6. {
  7. #ifdef WITH_KARMA
  8.     // Actor interface.
  9.     virtual void PostBeginPlay();
  10.     virtual void Destroy();
  11.     virtual void PostNetReceive();
  12.     virtual void PostEditChange();
  13.     virtual void setPhysics(BYTE NewPhysics, AActor *NewFloor, FVector NewFloorV);
  14.     virtual void TickSimulated( FLOAT DeltaSeconds );
  15.     virtual void TickAuthoritative( FLOAT DeltaSeconds );
  16.     virtual void physKarma(FLOAT DeltaTime);
  17.     virtual void preContactUpdate();
  18.     virtual void preKarmaStep(FLOAT DeltaTime);
  19.  
  20.     // SVehicle interface.
  21.     virtual void UpdateVehicle(FLOAT DeltaTime);
  22. #endif
  23.  
  24. }
  25.  
  26. var (SVehicle) editinline export    array<SVehicleWheel>        Wheels; // Wheel data
  27.  
  28. // generic controls (set by controller, used by concrete derived classes)
  29. var (SVehicle) float    Steering; // between -1 and 1
  30. var (SVehicle) float    Throttle; // between -1 and 1
  31. var (SVehicle) float    Rise;      // between -1 and 1
  32.  
  33. var               Pawn                Driver;
  34.  
  35. var (SVehicle) array<vector>    ExitPositions;        // Positions (rel to vehicle) to try putting the player when exiting.
  36. var (SVehicle) array<vector>    EntryPositions;        // Positions (rel to vehicle) to create triggers for entry.
  37.  
  38. var (SVehicle) vector    DrivePos;        // Position (rel to vehicle) to put player while driving.
  39. var (SVehicle) rotator    DriveRot;        // Rotation (rel to vehicle) to put driver while driving.
  40. var (SVehicle) name        DriveAnim;        // Animation to play while driving.
  41.  
  42. //// CAMERAS ////
  43. var (SVehicle) bool        bDrawMeshInFP;        // Whether to draw the vehicle mesh when in 1st person mode.
  44. var    (SVehicle) bool        bDrawDriverInTP;    // Whether to draw the driver when in 3rd person mode.
  45. var (SVehicle) bool        bZeroPCRotOnEntry;    // If true, set camera rotation to zero on entering vehicle. If false, set it to the vehicle rotation.
  46.  
  47. var (SVehicle) vector   FPCamPos;    // Position of camera when driving first person.
  48.  
  49. var (SVehicle) vector   TPCamLookat;
  50. var (SVehicle) float    TPCamDistance;
  51.  
  52. var (SVehicle) int        MaxViewYaw; // Maximum amount you can look left and right
  53. var (SVehicle) int        MaxViewPitch; // Maximum amount you can look up and down 
  54.  
  55. //// PHYSICS ////
  56. var (SVehicle) float    VehicleMass;
  57.  
  58. //// EFFECTS ////
  59.  
  60. // Effect spawned when vehicle is destroyed
  61. var (SVehicle) class<Actor>    DestroyEffectClass;
  62.  
  63. // Created in PostNetBeginPlay and destroyed when vehicle is.
  64. var               array<SVehicleTrigger>    EntryTriggers;
  65.  
  66. var               bool     bGetOut;
  67.  
  68. // The factory that created this vehicle.
  69. var               SVehicleFactory    ParentFactory;
  70.  
  71. // Shadow projector
  72. var               ShadowProjector    VehicleShadow;
  73.  
  74. // Useful function for plotting data to real-time graph on screen.
  75. native final function GraphData(string DataName, float DataValue);
  76.  
  77. replication
  78. {
  79.     reliable if(Role==ROLE_Authority)
  80.         ClientKDriverEnter, ClientKDriverLeave;
  81. }
  82.  
  83. // Really simple at the moment!
  84. function TakeDamage(int Damage, Pawn instigatedBy, Vector hitlocation, 
  85.                         Vector momentum, class<DamageType> damageType)
  86. {
  87.     local PlayerController pc;
  88.  
  89.     Health -= Damage;
  90.  
  91.     //Log("Ouch! "$Health);
  92.  
  93.     // The vehicle is dead!
  94.     if(Health <= 0)
  95.     {
  96.         if ( Controller != None )
  97.         {
  98.             pc = PlayerController(Controller);
  99.  
  100.             if( Controller.bIsPlayer && pc != None )
  101.             {
  102.                 ClientKDriverLeave(pc); // Just to reset HUD etc.
  103.  
  104.                 // THIS WAS HOW IT WAS IN UT2003, DONT KNOW ABOUT WARFARE...
  105.                 // pc.PawnDied(self); // This should unpossess the controller and let the player respawn
  106.             }
  107.             else
  108.                 Controller.Destroy();
  109.         }
  110.  
  111.         Destroy(); // Destroy the vehicle itself (see Destroyed below)
  112.     }
  113.  
  114.     //KAddImpulse(momentum, hitlocation);
  115. }
  116.  
  117. // Vehicles dont get telefragged.
  118. event EncroachedBy( actor Other )
  119. {
  120.     Log("SVehicle("$self$") Encroached By: "$Other$".");
  121. }
  122.  
  123. simulated function FaceRotation( rotator NewRotation, float DeltaTime )
  124. {
  125.     // Vehicles ignore 'face rotation'.
  126. }
  127.  
  128. // You got some new info from the server (ie. VehicleState has some new info).
  129. event VehicleStateReceived();
  130.  
  131. // Do script car model stuff here - but DONT create/destroy anything.
  132. native event UpdateVehicle( float DeltaTime );
  133.  
  134. // Do any general vehicle set-up when it gets spawned.
  135. simulated function PostNetBeginPlay()
  136. {
  137.     local vector RotX, RotY, RotZ;
  138.     local int i;
  139.     local SVehicleTrigger NewTrigger;
  140.  
  141.     Super.PostNetBeginPlay();
  142.  
  143.     if(Level.NetMode != NM_Client)
  144.     {
  145.         GetAxes(Rotation,RotX,RotY,RotZ);
  146.  
  147.         EntryTriggers.Length = EntryPositions.Length;
  148.  
  149.         for(i=0; i<EntryPositions.Length; i++)
  150.         {
  151.             // Create triggers for gettting into the hoverbike - only on the server
  152.             NewTrigger = spawn(class'SVehicleTrigger', self,, Location + EntryPositions[i].X * RotX + EntryPositions[i].Y * RotY + EntryPositions[i].Z * RotZ);
  153.             NewTrigger.SetBase(self);
  154.             NewTrigger.SetCollision(true, false, false);
  155.  
  156.             EntryTriggers[i] = NewTrigger;
  157.         }
  158.     }
  159.  
  160.     // Make sure params are up to date.
  161.     SVehicleUpdateParams();
  162. }
  163.  
  164. // Called when a parameter of the overall articulated actor has changed (like PostEditChange)
  165. // The script must then call KUpdateConstraintParams or Actor Karma mutators as appropriate.
  166. simulated event SVehicleUpdateParams()
  167. {
  168.     KSetMass(VehicleMass);
  169. }
  170.  
  171. // The pawn Driver has tried to take control of this vehicle
  172. function TryToDrive(Pawn p)
  173. {
  174.     local Controller C;
  175.     C = p.Controller;
  176.  
  177.     if ( (Driver == None) && (C != None) && C.bIsPlayer && !C.IsInState('PlayerDriving') && p.IsHumanControlled() )
  178.     {        
  179.         KDriverEnter(p);
  180.     }
  181. }
  182.  
  183. // Events called on driver entering/leaving vehicle
  184.  
  185. simulated function ClientKDriverEnter(PlayerController pc)
  186. {
  187.     //log("Enter: "$pc.Pawn);
  188.  
  189.     //pc.myHUD.bCrosshairShow = false;
  190.     //pc.myHUD.bShowWeaponInfo = false;
  191.     //pc.myHUD.bShowPersonalInfo = false;
  192.     //pc.myHUD.bShowPoints = false;
  193.  
  194.     pc.bBehindView = true;
  195.     pc.bFixedCamera = false;
  196.     pc.bFreeCamera = true;
  197.  
  198.     // Set rotation of camera when getting into vehicle based on bZeroPCRotOnEntry
  199.     if(    bZeroPCRotOnEntry )
  200.         pc.SetRotation( rot(0, 0, 0) );
  201.     else
  202.         pc.SetRotation( rotator( vect(1, 0, 0) >> Rotation ) );
  203. }
  204.  
  205. function KDriverEnter(Pawn p)
  206. {
  207.     local PlayerController pc;
  208.     local vector AttachPos;
  209.  
  210.     //log("SVehicle KDriverEnter");
  211.  
  212.     // Set pawns current controller to control the vehicle pawn instead
  213.     Driver = p;
  214.  
  215.     // Move the driver into position, and attach to car.
  216.     Driver.SetCollision(false, false, false);
  217.     Driver.bCollideWorld = false;
  218.     Driver.bPhysicsAnimUpdate = false;
  219.     Driver.Velocity = vect(0,0,0);
  220. //    Driver.SetPhysics(PHYS_None);
  221.  
  222.     AttachPos = Location + (DrivePos >> Rotation);
  223.     Driver.SetLocation(AttachPos);
  224.  
  225.     Driver.SetPhysics(PHYS_None);
  226.  
  227.     Driver.bHardAttach = true;
  228.     Driver.SetBase(None);
  229.     Driver.SetBase(self);
  230.     Driver.SetRelativeRotation(DriveRot);
  231.     Driver.SetPhysics(PHYS_None);
  232.  
  233.     pc = PlayerController(p.Controller);
  234.     //pc.ClientSetBehindView(true);
  235.     //pc.ClientSetFixedCamera(false);
  236.  
  237.     // Disconnect PlyaerController from Driver and connect to SVehicle.
  238.     pc.Unpossess();
  239.     Driver.SetOwner(self); // This keeps the driver relevant.
  240.     pc.Possess(self);
  241.  
  242.     pc.ClientSetViewTarget(self); // Set playercontroller to view the vehicle
  243.  
  244.     // Change controller state to driver
  245.     pc.GotoState('PlayerDriving');
  246.  
  247.     ClientKDriverEnter(pc);
  248. }
  249.  
  250. simulated function ClientKDriverLeave(PlayerController pc)
  251. {
  252.     //local vector exitLookDir;
  253.  
  254.     //log("Leave: "$pc.Pawn);
  255.  
  256.     //pc.bBehindView = false;
  257.     pc.bFixedCamera = true;
  258.     pc.bFreeCamera = false;
  259.  
  260.     // Stop messing with bOwnerNoSee
  261.     Driver.bOwnerNoSee = Driver.default.bOwnerNoSee;
  262.  
  263.     // This removes any 'roll' from the look direction.
  264.     //exitLookDir = Vector(pc.Rotation);
  265.     //pc.SetRotation(Rotator(exitLookDir));
  266.  
  267.     //pc.myHUD.bCrosshairShow = pc.myHUD.default.bCrosshairShow;
  268.     //pc.myHUD.bShowWeaponInfo = pc.myHUD.default.bShowWeaponInfo;
  269.     //pc.myHUD.bShowPersonalInfo = pc.myHUD.default.bShowPersonalInfo;
  270.     //pc.myHUD.bShowPoints = pc.myHUD.default.bShowPoints;
  271. }
  272.  
  273. // Called from the PlayerController when player wants to get out.
  274. function bool KDriverLeave(bool bForceLeave)
  275. {
  276.     local PlayerController pc;
  277.     local int i;
  278.     local bool havePlaced;
  279.     local vector HitLocation, HitNormal, tryPlace;
  280.  
  281.     //log("SVehicle KDriverLeave");
  282.  
  283.     // Do nothing if we're not being driven
  284.     if(Driver == None)
  285.         return false;
  286.  
  287.     // Before we can exit, we need to find a place to put the driver.
  288.     // Iterate over array of possible exit locations.
  289.     
  290.     Driver.bHardAttach = false;
  291.     Driver.bCollideWorld = true;
  292.     Driver.SetCollision(true, true, true);
  293.     
  294.     havePlaced = false;
  295.     for(i=0; i < ExitPositions.Length && havePlaced == false; i++)
  296.     {
  297.         //Log("Trying Exit:"$i);
  298.     
  299.         tryPlace = Location + (ExitPositions[i] >> Rotation);
  300.     
  301.         // First, do a line check (stops us passing through things on exit).
  302.         if( Trace(HitLocation, HitNormal, tryPlace, Location, false) != None )
  303.             continue;
  304.             
  305.         // Then see if we can place the player there.
  306.         if( !Driver.SetLocation(tryPlace) )
  307.             continue;
  308.         
  309.         havePlaced = true;
  310.         
  311.         //Log("SUCCESS!");        
  312.     }
  313.  
  314.     // If we could not find a place to put the driver, leave driver inside as before.
  315.     if(!havePlaced && !bForceLeave)
  316.     {
  317.         Log("Could not place driver.");
  318.     
  319.         Driver.bHardAttach = true;
  320.         Driver.bCollideWorld = false;
  321.         Driver.SetCollision(false, false, false);
  322.     
  323.         return false;
  324.     }
  325.  
  326.     pc = PlayerController(Controller);
  327.  
  328.     //Log("Pre ClientKDriverLeave");
  329.     ClientKDriverLeave(pc);
  330.     //Log("Post ClientKDriverLeave");
  331.  
  332.     // Reconnect PlayerController to Driver.
  333.     pc.Unpossess();
  334.     Driver.SetOwner(pc);
  335.     pc.Possess(Driver);
  336.  
  337.     pc.ClientSetViewTarget(Driver); // Set playercontroller to view the persone that got out
  338.  
  339.     Controller = None;
  340.  
  341.     Driver.PlayWaiting();
  342.     Driver.bPhysicsAnimUpdate = Driver.Default.bPhysicsAnimUpdate;
  343.  
  344.     // Do stuff on client
  345.     //pc.ClientSetBehindView(false);
  346.     //pc.ClientSetFixedCamera(true);
  347.  
  348.     Driver.Acceleration = vect(0, 0, 24000);
  349.     Driver.SetPhysics(PHYS_Falling);
  350.     Driver.SetBase(None);
  351.  
  352.     // Car now has no driver
  353.     Driver = None;
  354.  
  355.     // Put brakes on before you get out :)
  356.     Throttle=0;
  357.     Steering=0;
  358.     Rise=0;
  359.     
  360.     return true;
  361. }
  362.  
  363. simulated function Destroyed()
  364. {
  365.     local int i;
  366.  
  367.     //Log("SVehicle Destroyed");
  368.  
  369.     // Destroy the triggers used for getting in.
  370.     for(i=0; i<EntryTriggers.Length; i++)
  371.     {
  372.         if(EntryTriggers[i] != None)
  373.             EntryTriggers[i].Destroy();
  374.     }
  375.  
  376.     // If there was a driver in the vehicle, destroy him too
  377.     if(Driver != None)
  378.         Driver.Destroy();
  379.  
  380.     // Decrease number of cars active out of parent factory
  381.     if(ParentFactory != None)
  382.         ParentFactory.VehicleCount--;
  383.  
  384.     // Trigger any effects for destruction
  385.     if(DestroyEffectClass != None)
  386.         spawn(DestroyEffectClass, , , Location, Rotation);
  387.  
  388.     // Destroy shadow projector
  389.     if (VehicleShadow != None) 
  390.         VehicleShadow.Destroy();
  391.  
  392.     Super.Destroyed();
  393. }
  394.  
  395. // Just to intercept 'getting out' request.
  396. simulated event Tick(float deltaSeconds)
  397. {
  398.     local bool gotOut;
  399.  
  400.     if(bGetOut && ROLE==Role_Authority)
  401.     {
  402.         gotOut = KDriverLeave(false);
  403.         if(!gotOut )
  404.         {
  405.             Log("Couldn't Leave - staying in!");
  406.         }
  407.     }
  408.     bGetOut = false;
  409. }
  410.  
  411. // This will get called if we couldn't move a pawn out of the way. 
  412. function bool EncroachingOn( actor Other )
  413. {
  414.     if ( Other == None )
  415.         return false;
  416.  
  417.     // If its a non-vehicle pawn, do lots of damage.
  418.     if( Pawn(Other) != None && SVehicle(Other) == None )
  419.     {
  420.         Other.TakeDamage(10000, None, Other.Location, vect(0,0,0), class'Crushed');
  421.         return false;
  422.     }
  423. }
  424.  
  425. // Glue a shadow projector on
  426. simulated function PostBeginPlay()
  427. {
  428.     Super.PostBeginPlay();
  429.  
  430.     if (Level.NetMode != NM_DedicatedServer)
  431.     {
  432.         VehicleShadow = Spawn(class'ShadowProjector', self, '', Location);
  433.         VehicleShadow.ShadowActor = self;
  434.         VehicleShadow.bBlobShadow = false;
  435.         VehicleShadow.LightDirection = Normal(vect(1,1,6));
  436.         VehicleShadow.LightDistance = 1200;
  437.         VehicleShadow.MaxTraceDistance = 350;
  438.         VehicleShadow.InitShadow();
  439.     }
  440. }
  441.  
  442. // Special calc-view for vehicles
  443. simulated function bool SpecialCalcView(out actor ViewActor, out vector CameraLocation, out rotator CameraRotation )
  444. {
  445.     local vector CamLookAt, HitLocation, HitNormal, OffsetVector;
  446.     local PlayerController pc;
  447.     local quat CarQuat, LookQuat, ResultQuat;
  448.  
  449.     pc = PlayerController(Controller);
  450.  
  451.     // Only do this mode we have a playercontroller viewing this vehicle
  452.     if(pc == None || pc.ViewTarget != self)
  453.         return false;
  454.  
  455.     if(pc.bBehindView) ///// THIRD PERSON ///////
  456.     {
  457.         ViewActor = self;
  458.         CamLookAt = Location + (TPCamLookat >> Rotation); 
  459.  
  460.         OffsetVector = vect(0, 0, 0);
  461.         OffsetVector.X = -1.0 * TPCamDistance;
  462.  
  463.         CameraLocation = CamLookAt + (OffsetVector >> CameraRotation);
  464.  
  465.         if( Trace( HitLocation, HitNormal, CameraLocation, CamLookAt, false, vect(10, 10, 10) ) != None )
  466.         {
  467.             CameraLocation = HitLocation;
  468.         }
  469.  
  470.         bOwnerNoSee = false;
  471.  
  472.         if(bDrawDriverInTP)
  473.             Driver.bOwnerNoSee = false;
  474.         else
  475.             Driver.bOwnerNoSee = true;
  476.     }
  477.     else ////// FIRST PERSON //////
  478.     {
  479.         ViewActor = self;
  480.  
  481.         // Camera position is locked to car
  482.         CameraLocation = Location + (FPCamPos >> Rotation);
  483.  
  484.         //Log(CameraRotation);
  485.         CarQuat = QuatFromRotator(Rotation);
  486.  
  487.         // Limit where you can look while driving.
  488.  
  489.         Normalize(CameraRotation); // Puts each element between +/- 32767
  490.  
  491.         if(CameraRotation.Yaw > MaxViewYaw)
  492.             CameraRotation.Yaw = MaxViewYaw;
  493.         else if(CameraRotation.Yaw < -MaxViewYaw)
  494.             CameraRotation.Yaw = -MaxViewYaw;
  495.  
  496.         //if(CameraRotation.Pitch > MaxViewPitch)
  497.         //    CameraRotation.Pitch = MaxViewPitch;
  498.         //else if(CameraRotation.Pitch < -MaxViewPitch)
  499.         //    CameraRotation.Pitch = -MaxViewPitch;
  500.  
  501.         //pc.DesiredRotation = CameraRotation;
  502.         //pc.SetRotation(CameraRotation);
  503.  
  504.         LookQuat = QuatFromRotator(CameraRotation);
  505.         ResultQuat = QuatProduct(LookQuat, CarQuat);
  506.         CameraRotation = QuatToRotator(ResultQuat);
  507.  
  508.         if(bDrawMeshInFP)
  509.             bOwnerNoSee = false;
  510.         else
  511.             bOwnerNoSee = true;
  512.  
  513.         Driver.bOwnerNoSee = true; // In first person, dont draw the driver
  514.     }
  515.  
  516.     return true;
  517. }
  518.  
  519. // Includes properties from KActor
  520. defaultproperties
  521. {
  522.     Steering=0
  523.     Throttle=0
  524.     VehicleMass=1.0
  525.  
  526.     ExitPositions(0)=(X=0,Y=0,Z=0)
  527.  
  528.     DrivePos=(X=0,Y=0,Z=0)
  529.     bZeroPCRotOnEntry=true
  530.  
  531.     TPCamLookat=(X=-100,Y=0,Z=100)
  532.     TPCamDistance=600
  533.  
  534.     MaxViewYaw=16000
  535.     MaxViewPitch=16000
  536.  
  537.     Physics=PHYS_Karma
  538.     bEdShouldSnap=True
  539.     bStatic=False
  540.     bShadowCast=False
  541.     bCollideActors=True
  542.     bCollideWorld=False
  543.     bProjTarget=True
  544.     bBlockActors=True
  545.     bBlockNonZeroExtentTraces=True
  546.     bBlockZeroExtentTraces=True
  547.     bBlockPlayers=True
  548.     bWorldGeometry=False
  549.     bBlockKarma=True
  550.     CollisionHeight=+000001.000000
  551.     CollisionRadius=+000001.000000
  552.     bAcceptsProjectors=True
  553.     bCanBeBaseForPawns=True
  554.     bAlwaysRelevant=True
  555.     RemoteRole=ROLE_SimulatedProxy
  556.     bNetInitialRotation=True
  557.     bSpecialCalcView=True
  558.     //bSpecialHUD=true
  559. }