home *** CD-ROM | disk | FTP | other *** search
/ Game Level Design / GLDesign.bin / Software / UnrealEngine2Runtime / UE2Runtime-22262001_Demo.exe / Gameplay / Classes / ScriptedController.uc < prev    next >
Text File  |  2003-12-11  |  10KB  |  438 lines

  1. // ScriptedController
  2. // AI controller which is controlling the pawn through a scripted sequence specified by 
  3. // an AIScript
  4.  
  5. class ScriptedController extends AIController;
  6.  
  7. var controller PendingController;    // controller which will get this pawn after scripted sequence is complete
  8. var int ActionNum;
  9. var int AnimsRemaining;
  10. var ScriptedSequence SequenceScript;
  11. var LatentScriptedAction CurrentAction;
  12. var Action_PLAYANIM CurrentAnimation;
  13. var bool bBroken;
  14. var bool bShootTarget;
  15. var bool bShootSpray;
  16. var bool bPendingShoot;
  17. var bool bFakeShot;            // FIXME - this is currently a hack
  18. var bool bUseScriptFacing;
  19. var        bool        bPendingDoubleJump;
  20. var        bool        bFineWeaponControl;
  21.  
  22. var Actor ScriptedFocus;
  23. var PlayerController MyPlayerController;
  24. var int NumShots;
  25. var name FiringMode;
  26. var int IterationCounter;
  27. var int IterationSectionStart;
  28.  
  29. function bool WeaponFireAgain(float RefireRate, bool bFinishedFire)
  30. {
  31.     return false;
  32. }
  33.  
  34. event NotifyJumpApex()
  35. {
  36.     local actor HitActor;
  37.     local vector HitNormal,HitLocation, HalfHeight,Start;
  38.     
  39.     // double jump
  40.     if ( bPendingDoubleJump )
  41.     {
  42.         Pawn.bWantsToCrouch = false;
  43.         Pawn.DoDoubleJump(false);
  44.         bPendingDoubleJump = false;
  45.     }
  46.     else if ( bJumpOverWall )
  47.     {
  48.         // double jump if haven't cleared obstacle
  49.         Pawn.Acceleration = Destination - Pawn.Location;
  50.         Pawn.Acceleration.Z = 0;
  51.         HalfHeight = Pawn.GetCollisionExtent();
  52.         HalfHeight.Z *= 0.5;
  53.         Start = Pawn.Location - Pawn.CollisionHeight * vect(0,0,0.5);
  54.         HitActor = Pawn.Trace(HitLocation, HitNormal, Start + 8 * Normal(Pawn.Acceleration), Start, true,HalfHeight);
  55.         if ( HitActor != None )
  56.         {
  57.             Pawn.bWantsToCrouch = false;
  58.             Pawn.DoDoubleJump(false);
  59.         }
  60.     }
  61. }
  62.  
  63. function TakeControlOf(Pawn aPawn)
  64. {
  65.     if ( Pawn != aPawn )
  66.     {
  67.         aPawn.PossessedBy(self);
  68.         Pawn = aPawn;
  69.     }
  70.     GotoState('Scripting');
  71. }
  72.  
  73. function SetEnemyReaction(int AlertnessLevel);
  74.  
  75. function DestroyPawn()
  76. {
  77.     if ( Pawn != None )
  78.         Pawn.Destroy();
  79.     Destroy();
  80. }
  81.  
  82. function Pawn GetMyPlayer()
  83. {
  84.     if ( (MyPlayerController == None) || (MyPlayerController.Pawn == None) )
  85.         ForEach DynamicActors(class'PlayerController',MyPlayerController)
  86.             if ( MyPlayerController.Pawn != None )
  87.                 break;
  88.     if ( MyPlayerController == None )
  89.         return None;
  90.     return MyPlayerController.Pawn;
  91. }
  92.  
  93. function Pawn GetInstigator()
  94. {
  95.     if ( Pawn != None )
  96.         return Pawn;
  97.     return Instigator;
  98. }
  99.  
  100. function Actor GetSoundSource()
  101. {
  102.     if ( Pawn != None )
  103.         return Pawn;
  104.     return SequenceScript;
  105. }
  106.  
  107. function bool CheckIfNearPlayer(float Distance)
  108. {
  109.     local Pawn MyPlayer;
  110.  
  111.     MyPlayer = GetMyPlayer();
  112.     return ( (MyPlayer != None) && (VSize(Pawn.Location - MyPlayer.Location) < Distance+CollisionRadius+MyPlayer.CollisionRadius ) && Pawn.PlayerCanSeeMe() );
  113. }
  114.  
  115. function ClearScript()
  116. {
  117.     ActionNum = 0;
  118.     CurrentAction = None;
  119.     CurrentAnimation = None;
  120.     ScriptedFocus = None;
  121.     Pawn.SetWalking(false);
  122.     Pawn.ShouldCrouch(false);
  123. }
  124.  
  125. function SetNewScript(ScriptedSequence NewScript)
  126. {
  127.     MyScript = NewScript;
  128.     SequenceScript = NewScript;
  129.     Focus = None;
  130.     ClearScript();
  131.     SetEnemyReaction(3);
  132.     SequenceScript.SetActions(self);
  133. }
  134.  
  135. function ClearAnimation()
  136. {
  137.     AnimsRemaining = 0;
  138.     bControlAnimations = false;
  139.     CurrentAnimation = None;
  140.     Pawn.PlayWaiting();
  141. }
  142.  
  143. function int SetFireYaw(int FireYaw)
  144. {
  145.     FireYaw = FireYaw & 65535;
  146.  
  147.     if ( (Abs(FireYaw - (Rotation.Yaw & 65535)) > 8192)
  148.         && (Abs(FireYaw - (Rotation.Yaw & 65535)) < 57343) )
  149.     {
  150.         if ( FireYaw ClockwiseFrom Rotation.Yaw )
  151.             FireYaw = Rotation.Yaw + 8192;
  152.         else
  153.             FireYaw = Rotation.Yaw - 8192;
  154.     }
  155.     return FireYaw;
  156. }
  157.  
  158. function rotator AdjustAim(Ammunition FiredAmmunition, vector projStart, int AimError)
  159. {
  160.     local rotator LookDir;
  161.  
  162.     // make sure bot has a valid target
  163.     if ( Target == None )
  164.         Target = ScriptedFocus;
  165.     if ( Target == None )
  166.     {
  167.         Target = Enemy;
  168.         if ( Target == None )
  169.         {
  170.             bFire = 0;
  171.             bAltFire = 0;
  172.             return Pawn.Rotation;
  173.         }
  174.     }
  175.     LookDir = rotator(Target.Location - projStart);
  176.     LookDir.Yaw = SetFireYaw(LookDir.Yaw);
  177.     return LookDir;
  178. }
  179.  
  180. function LeaveScripting();
  181.  
  182. state Scripting
  183. {
  184.     function DisplayDebug(Canvas Canvas, out float YL, out float YPos)
  185.     {
  186.         Super.DisplayDebug(Canvas,YL,YPos);
  187.         Canvas.DrawText("AIScript "$SequenceScript$" ActionNum "$ActionNum, false);
  188.         YPos += YL;
  189.         Canvas.SetPos(4,YPos);
  190.         CurrentAction.DisplayDebug(Canvas,YL,YPos);
  191.     }
  192.  
  193.     /* UnPossess()
  194.     scripted sequence is over - return control to PendingController
  195.     */
  196.     function UnPossess()
  197.     {
  198.         Pawn.UnPossessed();
  199.         if ( (Pawn != None) && (PendingController != None) )
  200.         {
  201.             PendingController.bStasis = false;
  202.             PendingController.Possess(Pawn);
  203.         }
  204.         Pawn = None;
  205.         Destroy();
  206.     }
  207.  
  208.     function LeaveScripting()
  209.     {
  210.         UnPossess();
  211.     }
  212.  
  213.     function InitForNextAction()
  214.     {
  215.         SequenceScript.SetActions(self);
  216.         if ( CurrentAction == None )
  217.         {
  218.             LeaveScripting();
  219.             return;
  220.         }
  221.         MyScript = SequenceScript;
  222.         if ( CurrentAnimation == None )
  223.             ClearAnimation();
  224.     }
  225.  
  226.     function Trigger( actor Other, pawn EventInstigator )
  227.     {
  228.         if ( CurrentAction.CompleteWhenTriggered() )
  229.             CompleteAction();
  230.     }
  231.  
  232.     function Timer()
  233.     {
  234.         if ( CurrentAction.WaitForPlayer() && CheckIfNearPlayer(CurrentAction.GetDistance()) )
  235.             CompleteAction();
  236.         else if ( CurrentAction.CompleteWhenTimer() )
  237.             CompleteAction();
  238.     }
  239.  
  240.     function AnimEnd(int Channel)
  241.     {
  242.         if ( CurrentAction.CompleteOnAnim(Channel) )
  243.         {
  244.             CompleteAction();
  245.             return;
  246.         }
  247.         if ( Channel == 0 )
  248.         {
  249.             if ( (CurrentAnimation == None) || !CurrentAnimation.PawnPlayBaseAnim(self,false) )
  250.                 ClearAnimation();
  251.         }
  252.         else 
  253.         {
  254.             // FIXME - support for CurrentAnimation play on other channels
  255.             Pawn.AnimEnd(Channel);
  256.         }
  257.     }
  258.  
  259.     function CompleteAction()
  260.     {
  261.         ActionNum++;
  262.         GotoState('Scripting','Begin');
  263.     }
  264.  
  265.     function SetMoveTarget()
  266.     {
  267.         local Actor NextMoveTarget;
  268.  
  269.         Focus = ScriptedFocus;
  270.         NextMoveTarget = CurrentAction.GetMoveTargetFor(self);
  271.         if ( NextMoveTarget == None )
  272.         {
  273.             GotoState('Broken');
  274.             return;
  275.         }
  276.         if ( Focus == None )
  277.             Focus = NextMoveTarget;
  278.         MoveTarget = NextMoveTarget;
  279.         if ( !ActorReachable(MoveTarget) )
  280.         {
  281.             MoveTarget = FindPathToward(MoveTarget,false);
  282.             if ( Movetarget == None )
  283.             {
  284.                 AbortScript();
  285.                 return;
  286.             }
  287.             if ( Focus == NextMoveTarget )
  288.                 Focus = MoveTarget;                
  289.         }
  290.     }
  291.  
  292.     function AbortScript()
  293.     {
  294.         LeaveScripting();
  295.     }
  296.     /* WeaponFireAgain()
  297.     Notification from weapon when it is ready to fire (either just finished firing,
  298.     or just finished coming up/reloading).
  299.     Returns true if weapon should fire.
  300.     If it returns false, can optionally set up a weapon change
  301.     */
  302.     function bool WeaponFireAgain(float RefireRate, bool bFinishedFire)
  303.     {
  304.         if ( Pawn.bIgnorePlayFiring )
  305.         {
  306.             Pawn.bIgnorePlayFiring = false;
  307.             return false;
  308.         }
  309.         if ( NumShots < 0 )
  310.         {
  311.             bShootTarget = false;
  312.             bShootSpray = false;
  313.             StopFiring();
  314.             return false;
  315.         }
  316.         if ( bShootTarget && (ScriptedFocus != None) && !ScriptedFocus.bDeleteMe )
  317.         {
  318.             Target = ScriptedFocus;
  319.             if ( (!bShootSpray && ((Pawn.Weapon.RefireRate() < 0.99) && !Pawn.Weapon.CanAttack(Target)))
  320.                 || !Pawn.Weapon.BotFire(bFinishedFire,FiringMode) )
  321.             {
  322.                 Enable('Tick'); //FIXME - use multiple timer for this instead
  323.                 bPendingShoot = true;
  324.                 return false;
  325.             }
  326.             if ( NumShots > 0 )
  327.             {
  328.                 NumShots--;
  329.                 if ( NumShots == 0 )
  330.                     NumShots = -1;
  331.             }
  332.             return true;
  333.         }
  334.         StopFiring();
  335.         return false;
  336.     }
  337.  
  338.     function Tick(float DeltaTime)
  339.     {
  340.         if ( bPendingShoot )
  341.         {
  342.             bPendingShoot = false;
  343.             MayShootTarget();
  344.         }
  345.         if ( !bPendingShoot
  346.             && ((CurrentAction == None) || !CurrentAction.StillTicking(self,DeltaTime)) )
  347.             disable('Tick');
  348.     }
  349.  
  350.     function MayShootAtEnemy();
  351.  
  352.     function MayShootTarget()
  353.     {
  354.         WeaponFireAgain(0,false);
  355.     }
  356.  
  357.     function EndState()
  358.     {
  359.         bUseScriptFacing = true;
  360.         bFakeShot = false;
  361.     }
  362.  
  363. Begin:
  364.     InitforNextAction();
  365.     if ( bBroken )
  366.         GotoState('Broken');
  367.     if ( CurrentAction.TickedAction() )
  368.         enable('Tick');
  369.     if ( !bFineWeaponControl )
  370.     {
  371.     if ( !bShootTarget )
  372.     {
  373.         bFire = 0;
  374.         bAltFire = 0;
  375.     }
  376.     else
  377.     {
  378.         Pawn.Weapon.RateSelf();
  379.         if ( bShootSpray )
  380.             MayShootTarget();
  381.     }
  382.     }
  383.     if ( CurrentAction.MoveToGoal() )
  384.     {
  385.         Pawn.SetMovementPhysics();
  386.         WaitForLanding();
  387. KeepMoving:
  388.         SetMoveTarget();
  389.         MayShootTarget();
  390.         MoveToward(MoveTarget, Focus,,,Pawn.bIsWalking);
  391.         if ( (MoveTarget != CurrentAction.GetMoveTargetFor(self))
  392.             || !Pawn.ReachedDestination(CurrentAction.GetMoveTargetFor(self)) )
  393.             Goto('KeepMoving');
  394.         CompleteAction();
  395.     }
  396.     else if ( CurrentAction.TurnToGoal() )
  397.     {
  398.         Pawn.SetMovementPhysics();
  399.         Focus = CurrentAction.GetMoveTargetFor(self);
  400.         if ( Focus == None )
  401.             FocalPoint = Pawn.Location + 1000 * vector(SequenceScript.Rotation);
  402.         FinishRotation();
  403.         CompleteAction();
  404.     }
  405.     else
  406.     {
  407.         //Pawn.SetPhysics(PHYS_RootMotion);
  408.         Pawn.Acceleration = vect(0,0,0);
  409.         Focus = ScriptedFocus;
  410.         if ( !bUseScriptFacing )
  411.             FocalPoint = Pawn.Location + 1000 * vector(Pawn.Rotation);
  412.         else if ( Focus == None )
  413.         {
  414.             MayShootAtEnemy();
  415.             FocalPoint = Pawn.Location + 1000 * vector(SequenceScript.Rotation);
  416.         }
  417.         FinishRotation();
  418.         MayShootTarget();
  419.     }
  420. }
  421.  
  422. // Broken scripted sequence - for debugging
  423. State Broken
  424. {
  425. Begin:
  426.     warn(Pawn$" Scripted Sequence BROKEN "$SequenceScript$" ACTION "$CurrentAction);
  427.     Pawn.bPhysicsAnimUpdate = false;
  428.     Pawn.StopAnimating();
  429.     if ( GetMyPlayer() != None )
  430.         PlayerController(GetMyPlayer().Controller).SetViewTarget(Pawn);
  431. }
  432.  
  433. defaultproperties
  434. {
  435.     bUseScriptFacing=true
  436.     IterationSectionStart=-1
  437. }
  438.