Files
UnrealEngineUWP/Engine/Source/Developer/FunctionalTesting/Private/FunctionalAITest.cpp
Ben Marsh 4ba423868f Copying //UE4/Dev-Build to //UE4/Dev-Main (Source: //UE4/Dev-Build @ 3209340)
#lockdown Nick.Penwarden
#rb none

==========================
MAJOR FEATURES + CHANGES
==========================

Change 3209340 on 2016/11/23 by Ben.Marsh

	Convert UE4 codebase to an "include what you use" model - where every header just includes the dependencies it needs, rather than every source file including large monolithic headers like Engine.h and UnrealEd.h.

	Measured full rebuild times around 2x faster using XGE on Windows, and improvements of 25% or more for incremental builds and full rebuilds on most other platforms.

	  * Every header now includes everything it needs to compile.
	        * There's a CoreMinimal.h header that gets you a set of ubiquitous types from Core (eg. FString, FName, TArray, FVector, etc...). Most headers now include this first.
	        * There's a CoreTypes.h header that sets up primitive UE4 types and build macros (int32, PLATFORM_WIN64, etc...). All headers in Core include this first, as does CoreMinimal.h.
	  * Every .cpp file includes its matching .h file first.
	        * This helps validate that each header is including everything it needs to compile.
	  * No engine code includes a monolithic header such as Engine.h or UnrealEd.h any more.
	        * You will get a warning if you try to include one of these from the engine. They still exist for compatibility with game projects and do not produce warnings when included there.
	        * There have only been minor changes to our internal games down to accommodate these changes. The intent is for this to be as seamless as possible.
	  * No engine code explicitly includes a precompiled header any more.
	        * We still use PCHs, but they're force-included on the compiler command line by UnrealBuildTool instead. This lets us tune what they contain without breaking any existing include dependencies.
	        * PCHs are generated by a tool to get a statistical amount of coverage for the source files using it, and I've seeded the new shared PCHs to contain any header included by > 15% of source files.

	Tool used to generate this transform is at Engine\Source\Programs\IncludeTool.

[CL 3209342 by Ben Marsh in Main branch]
2016-11-23 15:48:37 -05:00

374 lines
9.2 KiB
C++

// Copyright 1998-2016 Epic Games, Inc. All Rights Reserved.
#include "FunctionalAITest.h"
#include "TimerManager.h"
#include "Engine/World.h"
#include "FuncTestManager.h"
#include "FunctionalTestingManager.h"
#include "AI/Navigation/NavigationSystem.h"
#include "AIController.h"
#include "Blueprint/AIBlueprintHelperLibrary.h"
AFunctionalAITest::AFunctionalAITest( const FObjectInitializer& ObjectInitializer )
: Super(ObjectInitializer)
, CurrentSpawnSetIndex(INDEX_NONE)
, bSingleSetRun(false)
{
SpawnLocationRandomizationRange = 0.f;
bWaitForNavMesh = true;
}
bool AFunctionalAITest::IsOneOfSpawnedPawns(AActor* Actor)
{
APawn* Pawn = Cast<APawn>(Actor);
return Pawn != NULL && SpawnedPawns.Contains(Pawn);
}
void AFunctionalAITest::BeginPlay()
{
// do a post-load step and remove all disabled spawn sets
for(int32 Index = SpawnSets.Num()-1; Index >= 0; --Index)
{
FAITestSpawnSet& SpawnSet = SpawnSets[Index];
if (SpawnSet.bEnabled == false)
{
UE_LOG(LogFunctionalTest, Log, TEXT("Removing disabled spawn set \'%s\'."), *SpawnSets[Index].Name.ToString());
SpawnSets.RemoveAt(Index, 1, false);
}
else
{
// update all spawn info that doesn't have spawn location set, and set spawn set name
for (int32 SpawnIndex = 0; SpawnIndex < SpawnSet.SpawnInfoContainer.Num(); ++SpawnIndex)
{
FAITestSpawnInfo& SpawnInfo = SpawnSet.SpawnInfoContainer[SpawnIndex];
SpawnInfo.SpawnSetName = SpawnSet.Name;
if (SpawnInfo.SpawnLocation == NULL)
{
SpawnInfo.SpawnLocation = SpawnSet.FallbackSpawnLocation ? SpawnSet.FallbackSpawnLocation : this;
}
}
}
}
SpawnSets.Shrink();
Super::BeginPlay();
}
bool AFunctionalAITest::RunTest(const TArray<FString>& Params)
{
KillOffSpawnedPawns();
ClearPendingDelayedSpawns();
RandomNumbersStream.Reset();
bSingleSetRun = Params.Num() > 0;
if (bSingleSetRun)
{
TTypeFromString<int32>::FromString(CurrentSpawnSetIndex, *Params[0]);
}
else
{
++CurrentSpawnSetIndex;
}
if (!SpawnSets.IsValidIndex(CurrentSpawnSetIndex))
{
return false;
}
StartSpawning();
return Super::RunTest(Params);
}
bool AFunctionalAITest::IsReady_Implementation()
{
return Super::IsReady_Implementation() && IsNavMeshReady();
}
void AFunctionalAITest::StartSpawning()
{
if (bWaitForNavMesh && !IsNavMeshReady())
{
GetWorldTimerManager().SetTimer(NavmeshDelayTimer, this, &AFunctionalAITest::StartSpawning, 0.5f, false);
return;
}
UWorld* World = GetWorld();
check(World);
const FAITestSpawnSet& SpawnSet = SpawnSets[CurrentSpawnSetIndex];
bool bSuccessfullySpawnedAll = true;
// NOTE: even if some pawns fail to spawn we don't stop spawning to find all spawns that will fails.
// all spawned pawns get filled off in case of failure.
CurrentSpawnSetName = SpawnSet.Name.ToString();
for (int32 SpawnIndex = 0; SpawnIndex < SpawnSet.SpawnInfoContainer.Num(); ++SpawnIndex)
{
const FAITestSpawnInfo& SpawnInfo = SpawnSet.SpawnInfoContainer[SpawnIndex];
if (SpawnInfo.IsValid())
{
if (SpawnInfo.PreSpawnDelay > 0)
{
FPendingDelayedSpawn PendingSpawnInfo(SpawnInfo);
PendingSpawnInfo.TimeToNextSpawn = SpawnInfo.PreSpawnDelay;
PendingSpawnInfo.NumberToSpawnLeft = SpawnInfo.NumberToSpawn;
PendingDelayedSpawns.Add(PendingSpawnInfo);
}
else if (SpawnInfo.SpawnDelay == 0.0)
{
for (int32 SpawnedCount = 0; SpawnedCount < SpawnInfo.NumberToSpawn; ++SpawnedCount)
{
bSuccessfullySpawnedAll &= SpawnInfo.Spawn(this);
}
}
else
{
bSuccessfullySpawnedAll &= SpawnInfo.Spawn(this);
if (SpawnInfo.NumberToSpawn > 1)
{
PendingDelayedSpawns.Add(SpawnInfo);
}
}
}
else
{
const FString SpawnFailureMessage = FString::Printf(TEXT("Spawn set \'%s\' contains invalid entry at index %d")
, *SpawnSet.Name.ToString()
, SpawnIndex);
UE_LOG(LogFunctionalTest, Warning, TEXT("%s"), *SpawnFailureMessage);
bSuccessfullySpawnedAll = false;
}
}
if (bSuccessfullySpawnedAll == false)
{
KillOffSpawnedPawns();
// wait a bit if it's in the middle of StartTest call
FTimerHandle DummyHandle;
World->GetTimerManager().SetTimer(DummyHandle, this, &AFunctionalAITest::OnSpawningFailure, 0.1f, false);
}
else
{
if (PendingDelayedSpawns.Num() > 0)
{
SetActorTickEnabled(true);
}
}
}
void AFunctionalAITest::OnSpawningFailure()
{
FinishTest(EFunctionalTestResult::Failed, TEXT("Unable to spawn AI"));
}
bool AFunctionalAITest::WantsToRunAgain() const
{
return bSingleSetRun == false && CurrentSpawnSetIndex + 1 < SpawnSets.Num();
}
void AFunctionalAITest::GatherRelevantActors(TArray<AActor*>& OutActors) const
{
Super::GatherRelevantActors(OutActors);
for (auto SpawnSet : SpawnSets)
{
if (SpawnSet.FallbackSpawnLocation)
{
OutActors.AddUnique(SpawnSet.FallbackSpawnLocation);
}
for (auto SpawnInfo : SpawnSet.SpawnInfoContainer)
{
if (SpawnInfo.SpawnLocation)
{
OutActors.AddUnique(SpawnInfo.SpawnLocation);
}
}
}
for (auto Pawn : SpawnedPawns)
{
if (Pawn)
{
OutActors.Add(Pawn);
}
}
}
void AFunctionalAITest::CleanUp()
{
Super::CleanUp();
CurrentSpawnSetIndex = INDEX_NONE;
KillOffSpawnedPawns();
ClearPendingDelayedSpawns();
}
FString AFunctionalAITest::GetAdditionalTestFinishedMessage(EFunctionalTestResult TestResult) const
{
FString ResultStr;
if (SpawnedPawns.Num() > 0)
{
if (CurrentSpawnSetName.Len() > 0 && CurrentSpawnSetName != TEXT("None"))
{
ResultStr = FString::Printf(TEXT("spawn set \'%s\', pawns: "), *CurrentSpawnSetName);
}
else
{
ResultStr = TEXT("pawns: ");
}
for (int32 PawnIndex = 0; PawnIndex < SpawnedPawns.Num(); ++PawnIndex)
{
ResultStr += FString::Printf(TEXT("%s, "), *GetNameSafe(SpawnedPawns[PawnIndex]));
}
}
return ResultStr;
}
FString AFunctionalAITest::GetReproString() const
{
return FString::Printf(TEXT("%s%s%d"), *(GetFName().ToString())
, FFunctionalTesting::ReproStringParamsSeparator
, CurrentSpawnSetIndex);
}
void AFunctionalAITest::KillOffSpawnedPawns()
{
for (int32 PawnIndex = 0; PawnIndex < SpawnedPawns.Num(); ++PawnIndex)
{
if (SpawnedPawns[PawnIndex])
{
SpawnedPawns[PawnIndex]->Destroy();
}
}
SpawnedPawns.Reset();
}
void AFunctionalAITest::ClearPendingDelayedSpawns()
{
SetActorTickEnabled(false);
PendingDelayedSpawns.Reset();
}
void AFunctionalAITest::Tick(float DeltaSeconds)
{
Super::Tick(DeltaSeconds);
for (auto& DelayedSpawn : PendingDelayedSpawns)
{
DelayedSpawn.Tick(DeltaSeconds, this);
}
}
void AFunctionalAITest::AddSpawnedPawn(APawn& SpawnedPawn)
{
SpawnedPawns.Add(&SpawnedPawn);
OnAISpawned.Broadcast(Cast<AAIController>(SpawnedPawn.GetController()), &SpawnedPawn);
}
FVector AFunctionalAITest::GetRandomizedLocation(const FVector& Location) const
{
return Location + FVector(RandomNumbersStream.FRandRange(-SpawnLocationRandomizationRange, SpawnLocationRandomizationRange), RandomNumbersStream.FRandRange(-SpawnLocationRandomizationRange, SpawnLocationRandomizationRange), 0);
}
bool AFunctionalAITest::IsNavMeshReady() const
{
UNavigationSystem* NavSys = UNavigationSystem::GetCurrent(GetWorld());
if (NavSys && NavSys->NavDataSet.Num() > 0 && !NavSys->IsNavigationBuildInProgress())
{
return true;
}
return false;
}
//----------------------------------------------------------------------//
// FAITestSpawnInfo
//----------------------------------------------------------------------//
bool FAITestSpawnInfo::Spawn(AFunctionalAITest* AITest) const
{
check(AITest);
bool bSuccessfullySpawned = false;
APawn* SpawnedPawn = UAIBlueprintHelperLibrary::SpawnAIFromClass(AITest->GetWorld(), PawnClass, BehaviorTree
, AITest->GetRandomizedLocation(SpawnLocation->GetActorLocation())
, SpawnLocation->GetActorRotation()
, /*bNoCollisionFail=*/true);
if (SpawnedPawn == NULL)
{
FString FailureMessage = FString::Printf(TEXT("Failed to spawn \'%s\' pawn (\'%s\' set) ")
, *GetNameSafe(PawnClass)
, *SpawnSetName.ToString());
UE_LOG(LogFunctionalTest, Warning, TEXT("%s"), *FailureMessage);
}
else if (SpawnedPawn->GetController() == NULL)
{
FString FailureMessage = FString::Printf(TEXT("Spawned Pawn %s (\'%s\' set) has no controller ")
, *GetNameSafe(SpawnedPawn)
, *SpawnSetName.ToString());
UE_LOG(LogFunctionalTest, Warning, TEXT("%s"), *FailureMessage);
}
else
{
IGenericTeamAgentInterface* TeamAgent = Cast<IGenericTeamAgentInterface>(SpawnedPawn);
if (TeamAgent == nullptr)
{
TeamAgent = Cast<IGenericTeamAgentInterface>(SpawnedPawn->GetController());
}
if (TeamAgent != nullptr)
{
TeamAgent->SetGenericTeamId(TeamID);
}
AITest->AddSpawnedPawn(*SpawnedPawn);
bSuccessfullySpawned = true;
}
return bSuccessfullySpawned;
}
//----------------------------------------------------------------------//
//
//----------------------------------------------------------------------//
FPendingDelayedSpawn::FPendingDelayedSpawn(const FAITestSpawnInfo& Source)
: NumberToSpawnLeft(0), bFinished(false)
{
*((FAITestSpawnInfo*)this) = Source;
TimeToNextSpawn = Source.SpawnDelay;
NumberToSpawnLeft = Source.NumberToSpawn - 1;
}
void FPendingDelayedSpawn::Tick(float TimeDelta, AFunctionalAITest* AITest)
{
if (bFinished)
{
return;
}
TimeToNextSpawn -= TimeDelta;
if (TimeToNextSpawn <= 0)
{
Spawn(AITest);
TimeToNextSpawn = SpawnDelay;
bFinished = (--NumberToSpawnLeft <= 0);
}
}