Guest User

Untitled

a guest
Oct 15th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class ZG_Bot extends UDKBot;
  2.  
  3. // This is a reference to the archetype that holds information about the properties of the bot.
  4. var() const ZG_Bot_Properties BotProperties;
  5. // True if the bot has just landed
  6. var ProtectedWrite bool bHasLanded;
  7. // This is the current enemy. This is acquired by either being hit by this pawn or having seen or heard this pawn
  8. var ProtectedWrite Pawn CurrentEnemy;
  9. // The deltatime for the AI main decision loop
  10. var const float UpdateDeltaTime;
  11.  
  12. /**
  13. * This function is used to initialize the AIController upon the spawning of their pawn
  14. */
  15. function Initialize()
  16. {
  17.    if(Pawn != none)
  18.     {
  19.         if(Pawn.Physics == PHYS_None)
  20.         {
  21.             Pawn.SetMovementPhysics();
  22.         }
  23.         if(Pawn.Physics == PHYS_Falling && DoWaitForLanding())
  24.         {
  25.             return;
  26.         }
  27.     }
  28. }
  29.  
  30. event Possess(Pawn InPawn, bool bVehicleTransition)
  31. {
  32.     Super.Possess(InPawn, bVehicleTransition);
  33.    
  34.     if(Pawn != none)
  35.     {
  36.         SetTimer(UpdateDeltaTime, true, NameOf(WhatToDo));
  37.     }
  38. }
  39.  
  40. function NotifyTakeHit(Controller InstigatedBy, vector HitLocation, int Damage, class<DamageType> damageType, vector Momentum)
  41. {
  42.     if(InstigatedBy != none && InstigatedBy != self)
  43.     {
  44.         if( ShouldSetNewEnemy() && InstigatedBy.Pawn != none )
  45.         {
  46.             CurrentEnemy = InstigatedBy.Pawn;
  47.         }
  48.     }
  49. }
  50.  
  51. /**
  52. * This function returns a vector which causes the pawn to walk towards the targetposition
  53. */
  54. function Vector Seek(Vector TargetPos)
  55. {
  56.     local vector DesiredVelocity;
  57.  
  58.     if(Pawn != none)
  59.     {
  60.         DesiredVelocity = Normal(TargetPos - Pawn.Location) * 1000;
  61.         return (DesiredVelocity - Pawn.Velocity);
  62.     }
  63.    
  64.     return vect(0, 0, 0);
  65. }
  66.  
  67. /**
  68. * This function returns a vector that causes the pawn to walk away from the target position
  69. */
  70. function vector Flee(Vector TargetPos)
  71. {
  72.     local Vector DesiredVelocity;
  73.    
  74.     if(Pawn != none)
  75.     {
  76.         DesiredVelocity = Normal(Pawn.Location - TargetPos) * Pawn.GroundSpeed;
  77.        
  78.         return (DesiredVelocity - Pawn.Velocity);
  79.     }
  80.    
  81.     return vect(0, 0, 0);
  82. }
  83.  
  84. /**
  85. * Calculate the velocity needed to arrive at the target. Note that this makes the bot decelerate as it nears the target.
  86. */
  87. function vector Arrive(Vector TargetPos)
  88. {
  89.     local Vector ToTarget, DesiredVelocity;
  90.     local float Distance;
  91.     local float Speed;
  92.    
  93.     if(Pawn !=  none)
  94.     {
  95.         ToTarget = TargetPos - Pawn.Location;
  96.         Distance = vsize(ToTarget);
  97.        
  98.         if(Distance > 0)
  99.         {
  100.             // calculate speed required to reach the target given the deceleration
  101.             Speed = Distance / (float(BotProperties.DecelerationType) + 1) * BotProperties.TweakDeceleration;
  102.             // Make sure the velocity does not exceed the max
  103.             Speed *= 8;
  104.             Speed = Min(Speed, Pawn.GroundSpeed);
  105.             DesiredVelocity = ToTarget * Speed / Distance;
  106.            
  107.             return (DesiredVelocity - Pawn.Velocity);
  108.         }
  109.     }
  110.    
  111.     return vect(0, 0, 0);
  112. }
  113.  
  114. /**
  115. * This function evaluates whether this bot should set a new enemy.
  116. */
  117. function bool ShouldSetNewEnemy()
  118. {
  119.     return true;
  120. }
  121.  
  122. /**
  123. * If our pawn is falling, lets wait for it to land before doing anything else
  124. */
  125. function bool DoWaitForLanding()
  126. {
  127.     GoToState('WaitingForLanding');
  128.     return true;
  129. }
  130.  
  131. /**
  132. * This is the entry point for AI decision making.
  133. */
  134. function WhatToDo()
  135. {
  136.     if(Pawn == none)
  137.     {
  138.         return;
  139.     }
  140.    
  141.     if(CurrentEnemy != none)
  142.     {
  143.         // If we are falling, wait until we are done
  144.         if(Pawn.Physics == PHYS_Falling && DoWaitForLanding())
  145.         {
  146.             return;
  147.         }
  148.         else
  149.         {  
  150.             Pawn.Velocity = Seek(CurrentEnemy.Location);
  151.             Pawn.SetDesiredRotation( Rotator(Pawn.Velocity), false, true, BotProperties.RotationInterpTime );
  152.         }
  153.     }
  154. }
  155.  
  156. /**
  157. * Display general debug information about the game
  158. */
  159. simulated function DisplayDebug(HUD HUD, out float out_YL, out float out_YPos)
  160. {
  161.     local Canvas Canvas;
  162.    
  163.     Super.DisplayDebug(HUD, out_YL, out_YPos);
  164.    
  165.     Canvas = HUD.Canvas;
  166.    
  167.     Canvas.DrawText("Controller:" $Self);
  168.     out_YPos += out_YL;
  169.     Canvas.SetPos(4, out_YPos);
  170.    
  171.     Canvas.DrawText("Pawn:" $Pawn);
  172.     out_YPos += out_YL;
  173.     Canvas.SetPos(4, out_YPos);
  174.    
  175.     if(Pawn != none)
  176.     {
  177.         Canvas.DrawText("Velocity:" $Pawn.Velocity);
  178.         out_YPos += out_YL;
  179.         Canvas.SetPos(4, out_YPos);
  180.     }
  181.    
  182.     Canvas.DrawText("State:" $GetStateName());
  183.     out_YPos += out_YL;
  184.     Canvas.SetPos(4, out_YPos);
  185. }
  186.  
  187.  
  188.  
  189. /**
  190. * Notify the AIController that the pawn has been respawned. Called from ZG_GameInfo.
  191. */
  192. function NotifyRespawn();
  193.  
  194. /**
  195. * In this state the pawn is falling. Wait for it's landing.
  196. */
  197. State WaitingForLanding
  198. {
  199.     event LongFall()
  200.     {
  201.         RecoverFromLongFall();
  202.     }
  203.    
  204.     event bool NotifyLanded(vector HitNormal, Actor FloorActor)
  205.     {
  206.         bHasLanded = true;
  207.         return false;
  208.     }
  209.    
  210.     function RecoverFromLongFall()
  211.     {
  212.         // Recover from a long fall here
  213.     }
  214.    
  215.     event BeginState(Name PreviousStateName)
  216.     {
  217.         bHasLanded = false;
  218.     }
  219. Begin:
  220.     WaitForLanding();
  221.     Sleep(0.5);
  222.    
  223.     if(bHasLanded)
  224.     {
  225.         // Just exit out of this state for now
  226.         GoToState('');
  227.     }
  228.    
  229.     GoTo('Begin');
  230. }
  231.  
  232. defaultproperties
  233. {
  234.     UpdateDeltaTime=0.05
  235.     BotProperties=ZG_Bot_Properties'ZombieGameAssets.Bots.BotProperties'
  236.    
  237.     bIsPlayer=true
  238. }
Add Comment
Please, Sign In to add comment