File size: 5,528 Bytes
7fd553e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | // Copyright Epic Games, Inc. All Rights Reserved.
#include "LyraBotCreationComponent.h"
#include "LyraGameMode.h"
#include "Engine/World.h"
#include "GameFramework/PlayerState.h"
#include "GameModes/LyraExperienceManagerComponent.h"
#include "Development/LyraDeveloperSettings.h"
#include "Character/LyraPawnExtensionComponent.h"
#include "AIController.h"
#include "Kismet/GameplayStatics.h"
#include "Character/LyraHealthComponent.h"
#include UE_INLINE_GENERATED_CPP_BY_NAME(LyraBotCreationComponent)
ULyraBotCreationComponent::ULyraBotCreationComponent(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
}
void ULyraBotCreationComponent::BeginPlay()
{
Super::BeginPlay();
// Listen for the experience load to complete
AGameStateBase* GameState = GetGameStateChecked<AGameStateBase>();
ULyraExperienceManagerComponent* ExperienceComponent = GameState->FindComponentByClass<ULyraExperienceManagerComponent>();
check(ExperienceComponent);
ExperienceComponent->CallOrRegister_OnExperienceLoaded_LowPriority(FOnLyraExperienceLoaded::FDelegate::CreateUObject(this, &ThisClass::OnExperienceLoaded));
}
void ULyraBotCreationComponent::OnExperienceLoaded(const ULyraExperienceDefinition* Experience)
{
#if WITH_SERVER_CODE
if (HasAuthority())
{
ServerCreateBots();
}
#endif
}
#if WITH_SERVER_CODE
void ULyraBotCreationComponent::ServerCreateBots_Implementation()
{
if (BotControllerClass == nullptr)
{
return;
}
RemainingBotNames = RandomBotNames;
// Determine how many bots to spawn
int32 EffectiveBotCount = NumBotsToCreate;
// Give the developer settings a chance to override it
if (GIsEditor)
{
const ULyraDeveloperSettings* DeveloperSettings = GetDefault<ULyraDeveloperSettings>();
if (DeveloperSettings->bOverrideBotCount)
{
EffectiveBotCount = DeveloperSettings->OverrideNumPlayerBotsToSpawn;
}
}
// Give the URL a chance to override it
if (AGameModeBase* GameModeBase = GetGameMode<AGameModeBase>())
{
EffectiveBotCount = UGameplayStatics::GetIntOption(GameModeBase->OptionsString, TEXT("NumBots"), EffectiveBotCount);
}
// Create them
for (int32 Count = 0; Count < EffectiveBotCount; ++Count)
{
SpawnOneBot();
}
}
FString ULyraBotCreationComponent::CreateBotName(int32 PlayerIndex)
{
FString Result;
if (RemainingBotNames.Num() > 0)
{
const int32 NameIndex = FMath::RandRange(0, RemainingBotNames.Num() - 1);
Result = RemainingBotNames[NameIndex];
RemainingBotNames.RemoveAtSwap(NameIndex);
}
else
{
//@TODO: PlayerId is only being initialized for players right now
PlayerIndex = FMath::RandRange(260, 260+100);
Result = FString::Printf(TEXT("Tinplate %d"), PlayerIndex);
}
return Result;
}
void ULyraBotCreationComponent::SpawnOneBot()
{
FActorSpawnParameters SpawnInfo;
SpawnInfo.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
SpawnInfo.OverrideLevel = GetComponentLevel();
SpawnInfo.ObjectFlags |= RF_Transient;
AAIController* NewController = GetWorld()->SpawnActor<AAIController>(BotControllerClass, FVector::ZeroVector, FRotator::ZeroRotator, SpawnInfo);
if (NewController != nullptr)
{
ALyraGameMode* GameMode = GetGameMode<ALyraGameMode>();
check(GameMode);
if (NewController->PlayerState != nullptr)
{
NewController->PlayerState->SetPlayerName(CreateBotName(NewController->PlayerState->GetPlayerId()));
}
GameMode->GenericPlayerInitialization(NewController);
GameMode->RestartPlayer(NewController);
if (NewController->GetPawn() != nullptr)
{
if (ULyraPawnExtensionComponent* PawnExtComponent = NewController->GetPawn()->FindComponentByClass<ULyraPawnExtensionComponent>())
{
PawnExtComponent->CheckDefaultInitialization();
}
}
SpawnedBotList.Add(NewController);
}
}
void ULyraBotCreationComponent::RemoveOneBot()
{
if (SpawnedBotList.Num() > 0)
{
// Right now this removes a random bot as they're all the same; could prefer to remove one
// that's high skill or low skill or etc... depending on why you are removing one
const int32 BotToRemoveIndex = FMath::RandRange(0, SpawnedBotList.Num() - 1);
AAIController* BotToRemove = SpawnedBotList[BotToRemoveIndex];
SpawnedBotList.RemoveAtSwap(BotToRemoveIndex);
if (BotToRemove)
{
// If we can find a health component, self-destruct it, otherwise just destroy the actor
if (APawn* ControlledPawn = BotToRemove->GetPawn())
{
if (ULyraHealthComponent* HealthComponent = ULyraHealthComponent::FindHealthComponent(ControlledPawn))
{
// Note, right now this doesn't work quite as desired: as soon as the player state goes away when
// the controller is destroyed, the abilities like the death animation will be interrupted immediately
HealthComponent->DamageSelfDestruct();
}
else
{
ControlledPawn->Destroy();
}
}
// Destroy the controller (will cause it to Logout, etc...)
BotToRemove->Destroy();
}
}
}
#else // !WITH_SERVER_CODE
void ULyraBotCreationComponent::ServerCreateBots_Implementation()
{
ensureMsgf(0, TEXT("Bot functions do not exist in LyraClient!"));
}
void ULyraBotCreationComponent::SpawnOneBot()
{
ensureMsgf(0, TEXT("Bot functions do not exist in LyraClient!"));
}
void ULyraBotCreationComponent::RemoveOneBot()
{
ensureMsgf(0, TEXT("Bot functions do not exist in LyraClient!"));
}
#endif
|