Guest User

Controller.cpp

a guest
Oct 16th, 2017
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.82 KB | None | 0 0
  1. // Fill out your copyright notice in the Description page of Project Settings.
  2.  
  3. #pragma once
  4.  
  5. #include "ZombieController.h"
  6. #include "Runtime/AIModule/Classes/BehaviorTree/BlackboardComponent.h"
  7. #include "Runtime/Engine/Classes/AI/Navigation/NavigationSystem.h"
  8. #include "ZombieCharacter.h"
  9. #include "Engine.h"
  10. #include "random"
  11. #include "time.h"
  12. #include "Target.h"
  13.  
  14. const FName AZombieController::randomLocationKey("RandomLocation");
  15.  
  16. AZombieController::AZombieController()
  17. {
  18.     blackboardComp = CreateDefaultSubobject<UBlackboardComponent>(TEXT("Blackboard component"));
  19.     behaviorTreeComp = CreateDefaultSubobject<UBehaviorTreeComponent>(TEXT("Behavior tree component"));
  20. }
  21.  
  22. void AZombieController::Possess(APawn* pawn)
  23. {
  24.     Super::Possess(pawn);
  25.  
  26.     AZombieCharacter* zombie = (AZombieCharacter*)pawn;
  27.     blackboardComp->InitializeBlackboard(*zombie->behaviorTree->BlackboardAsset);
  28.     zombie->SetController(this);
  29.    
  30.     blackboardComp->SetValueAsVector(randomLocationKey, GetRandomLocation(pawn->GetActorLocation(), 2000, GetWorld(), pawn));
  31.     TArray<AActor*> v;
  32.     UGameplayStatics::GetAllActorsOfClass(GetWorld(), ATarget::StaticClass(), v);
  33.     blackboardComp->SetValueAsObject("Target", v[0]);
  34.     auto res = blackboardComp->GetValueAsVector(randomLocationKey);
  35.  
  36.     GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Yellow, FString::Printf(TEXT("Random location set to: (%f, %f, %f)"), res.X, res.Y, res.Z));
  37.  
  38.     /*
  39.     if (!navSystem->GetRandomReachablePointInRadius(FVector(0, 0, 0), 10000.0f, location))
  40.     {
  41.     GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, TEXT("Could not find a location"));
  42.     behaviorTreeComp->StartTree(*zombie->behaviorTree);
  43.     return;
  44.     }
  45.  
  46.     if (!blackboardComp)
  47.     {
  48.     GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, TEXT("Could not find the blackboard"));
  49.     }
  50.     else
  51.     blackboardComp->SetValueAsVector(randomLocationKey, location.Location);
  52.  
  53.     */
  54.     behaviorTreeComp->StartTree(*zombie->behaviorTree);
  55. }
  56.  
  57. void AZombieController::ChangeRandomLocation(APawn* pawn, UWorld* world)
  58. {
  59.     /*
  60.     FNavLocation location;
  61.  
  62.     auto navSystem = world->GetNavigationSystem();
  63.  
  64.     if (!navSystem->GetRandomReachablePointInRadius(FVector(0, 0, 0), 10000.0f, location))
  65.     {
  66.     GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, TEXT("Could not find a location"));
  67.     return;
  68.     }
  69.     */
  70.  
  71.     if (!blackboardComp)
  72.     {
  73.         GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, TEXT("Could not find the blackboard"));
  74.         return;
  75.     }
  76.     if (!behaviorTreeComp->GetActiveNode())
  77.     {
  78.         GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, TEXT("The behavior tree is fucking broken"));
  79.     }
  80.  
  81.     blackboardComp->SetValueAsVector(randomLocationKey, GetRandomLocation(pawn->GetActorLocation(), 2000, world, pawn));
  82.  
  83.     auto res = blackboardComp->GetValueAsVector(randomLocationKey);
  84.     GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Yellow, FString::Printf(TEXT("Random location set to: (%f, %f, %f)"), res.X, res.Y, res.Z));
  85.  
  86. }
  87.  
  88. FVector AZombieController::GetRandomLocation(const FVector& origin, unsigned int radius, UWorld* world, AActor* character)
  89. {
  90.     //Only generate X and Y
  91.     //Z will be 0
  92.     FVector result(0, 0, 130);
  93.  
  94.     srand(time(0));
  95.  
  96.     int Xdeviation = rand() % (radius * 2) - radius;
  97.     result.X = Xdeviation + origin.X;
  98.  
  99.     int maxYdeviation = sqrt(radius*radius - Xdeviation * Xdeviation);
  100.     result.Y = rand() % (maxYdeviation * 2) - maxYdeviation + origin.Y;
  101.     /*
  102.     do
  103.     {
  104.     GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Yellow, TEXT("Generating path"));
  105.     //First generate X
  106.     int Xdeviation = rand() % (radius * 2) - radius;
  107.     result.X = Xdeviation + origin.X;
  108.  
  109.     int maxYdeviation = sqrt(radius*radius - Xdeviation * Xdeviation);
  110.     result.Y = rand() % (maxYdeviation * 2) - maxYdeviation + origin.Y;
  111.     } while (false);// (!UNavigationSystem::FindPathToLocationSynchronously(world, origin, result, character));
  112.     */
  113.     return result;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment