Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class FlockingControllerAI extends AIController;
- var Pawn Player;
- var float MoveSpeed;
- var float SeparationRadius;
- var float CohesionRadius;
- var float NeutralRadius;
- var int TimerForCalc;
- var int TimerIncremetal;
- var vector vVelocity;
- var vector vLocation;
- auto state Flocking {
- //Look for a target
- event SeePlayer(pawn p)
- {
- if (FlockingPawnAI(p) != none) {
- Player = p;
- }
- }
- event Tick(float deltaTime){
- local vector v;
- super.Tick(deltaTime);
- vVelocity = Normal(Pawn.Velocity);
- if(FlockingPawnAI(Pawn).isLeader){
- v += vVelocity;
- }
- else{
- v += RuleCohesion(); //Move toward eachother
- v += RuleAlignment(); //look in the same direction
- v += RuleSeparation(); //Move from eachother
- }
- vVelocity = vVelocity + v;
- vVelocity.Z = 0; //Block Z-Axis movement
- Pawn.Velocity = vVelocity; //Define movement speed
- Pawn.SetRotation(Rotator(vVelocity));
- }
- Begin:
- }
- function vector RuleAlignment(){
- local Controller C;
- local int botCount;
- local float distancePlayerBot;
- local vector totalVelocity;
- local vector forceDir;
- local Vector v;
- botCount = 0;
- foreach WorldInfo.AllControllers(class 'Controller', C){
- if (C.bIsPlayer && C.IsA('FlockingControllerAI') && C.Pawn != none){
- distancePlayerBot = VSize(C.Pawn.Location - Pawn.Location);
- if(distancePlayerBot < CohesionRadius && C != self){
- totalVelocity += C.Pawn.Velocity;
- botCount++;
- }
- }
- }
- if(botCount > 0){
- v = totalVelocity/botCount;
- // forceDir = Normal(v);
- // v = Normal(0.1*forceDir + V);
- }
- return v;
- }
- function Vector RuleCohesion(){
- local Controller C;
- local Vector totalPosition;
- local vector averagePosition;
- local Vector v;
- local int botCount;
- local float distancePlayerBot;
- botCount = 0;
- foreach WorldInfo.AllControllers(class 'Controller', C){
- if (C.bIsPlayer && C.IsA('FlockingControllerAI') && C.Pawn != none){
- distancePlayerBot = VSize(C.Pawn.Location - Pawn.Location);
- if(distancePlayerBot < CohesionRadius && distancePlayerBot > SeparationRadius && C != self){
- totalPosition += C.Pawn.Location;
- if(FlockingPawnAI(C.Pawn).isLeader){
- totalPosition += C.Pawn.Location;
- }
- botCount++;
- }
- }
- }
- if(botCount > 0){
- averagePosition = totalPosition / botCount;
- v = averagePosition - Pawn.Location;
- }
- return v;
- }
- function vector RuleSeparation(){
- local Controller C;
- local float distancePlayerBot;
- local int botCount;
- local Vector v;
- botCount = 0;
- foreach WorldInfo.AllControllers(class 'Controller', C){
- if (C.bIsPlayer && (C.IsA('FlockingControllerAI') || C.IsA('ThirdPersonViewController')) && C.Pawn != none){
- distancePlayerBot = Abs(VSize(Pawn.Location - C.Pawn.Location));
- if(distancePlayerBot < NeutralRadius && distancePlayerBot > 0){
- v += (Pawn.location - C.Pawn.location) * (1/distancePlayerBot);
- botCount++;
- }
- }
- }
- `log("Separation: "$v);
- return v;
- }
- DefaultProperties
- {
- bAdjustFromWalls=true;
- bIsPlayer=true;
- SeparationRadius = 100;
- CohesionRadius = 1000;
- NeutralRadius = 150;
- Physics=PHYS_Walking;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement