Advertisement
Guest User

unrealscriptshitflocking

a guest
Oct 23rd, 2014
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. class FlockingControllerAI extends AIController;
  2.  
  3. var Pawn Player;
  4. var float MoveSpeed;
  5. var float SeparationRadius;
  6. var float CohesionRadius;
  7. var float NeutralRadius;
  8. var int TimerForCalc;
  9. var int TimerIncremetal;
  10. var vector vVelocity;
  11. var vector vLocation;
  12.  
  13. auto state Flocking {
  14. //Look for a target
  15. event SeePlayer(pawn p)
  16. {
  17. if (FlockingPawnAI(p) != none) {
  18. Player = p;
  19. }
  20. }
  21. event Tick(float deltaTime){
  22. local vector v;
  23. super.Tick(deltaTime);
  24.  
  25. vVelocity = Normal(Pawn.Velocity);
  26.  
  27.  
  28. if(FlockingPawnAI(Pawn).isLeader){
  29. v += vVelocity;
  30. }
  31. else{
  32. v += RuleCohesion(); //Move toward eachother
  33. v += RuleAlignment(); //look in the same direction
  34. v += RuleSeparation(); //Move from eachother
  35. }
  36. vVelocity = vVelocity + v;
  37. vVelocity.Z = 0; //Block Z-Axis movement
  38.  
  39.  
  40.  
  41. Pawn.Velocity = vVelocity; //Define movement speed
  42. Pawn.SetRotation(Rotator(vVelocity));
  43.  
  44. }
  45. Begin:
  46. }
  47.  
  48.  
  49. function vector RuleAlignment(){
  50.  
  51. local Controller C;
  52. local int botCount;
  53. local float distancePlayerBot;
  54. local vector totalVelocity;
  55. local vector forceDir;
  56. local Vector v;
  57. botCount = 0;
  58.  
  59. foreach WorldInfo.AllControllers(class 'Controller', C){
  60.  
  61. if (C.bIsPlayer && C.IsA('FlockingControllerAI') && C.Pawn != none){
  62. distancePlayerBot = VSize(C.Pawn.Location - Pawn.Location);
  63.  
  64. if(distancePlayerBot < CohesionRadius && C != self){
  65. totalVelocity += C.Pawn.Velocity;
  66. botCount++;
  67. }
  68. }
  69. }
  70.  
  71. if(botCount > 0){
  72. v = totalVelocity/botCount;
  73. // forceDir = Normal(v);
  74. // v = Normal(0.1*forceDir + V);
  75. }
  76. return v;
  77.  
  78. }
  79.  
  80. function Vector RuleCohesion(){
  81. local Controller C;
  82. local Vector totalPosition;
  83. local vector averagePosition;
  84. local Vector v;
  85. local int botCount;
  86. local float distancePlayerBot;
  87. botCount = 0;
  88.  
  89. foreach WorldInfo.AllControllers(class 'Controller', C){
  90.  
  91. if (C.bIsPlayer && C.IsA('FlockingControllerAI') && C.Pawn != none){
  92. distancePlayerBot = VSize(C.Pawn.Location - Pawn.Location);
  93.  
  94. if(distancePlayerBot < CohesionRadius && distancePlayerBot > SeparationRadius && C != self){
  95. totalPosition += C.Pawn.Location;
  96. if(FlockingPawnAI(C.Pawn).isLeader){
  97. totalPosition += C.Pawn.Location;
  98. }
  99. botCount++;
  100. }
  101. }
  102. }
  103. if(botCount > 0){
  104. averagePosition = totalPosition / botCount;
  105. v = averagePosition - Pawn.Location;
  106. }
  107. return v;
  108. }
  109.  
  110.  
  111. function vector RuleSeparation(){
  112. local Controller C;
  113. local float distancePlayerBot;
  114. local int botCount;
  115. local Vector v;
  116. botCount = 0;
  117. foreach WorldInfo.AllControllers(class 'Controller', C){
  118.  
  119. if (C.bIsPlayer && (C.IsA('FlockingControllerAI') || C.IsA('ThirdPersonViewController')) && C.Pawn != none){
  120. distancePlayerBot = Abs(VSize(Pawn.Location - C.Pawn.Location));
  121. if(distancePlayerBot < NeutralRadius && distancePlayerBot > 0){
  122. v += (Pawn.location - C.Pawn.location) * (1/distancePlayerBot);
  123. botCount++;
  124. }
  125. }
  126. }
  127. `log("Separation: "$v);
  128. return v;
  129. }
  130.  
  131.  
  132. DefaultProperties
  133. {
  134. bAdjustFromWalls=true;
  135. bIsPlayer=true;
  136. SeparationRadius = 100;
  137. CohesionRadius = 1000;
  138. NeutralRadius = 150;
  139. Physics=PHYS_Walking;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement