|
|
|
|
| #include "LyraPlayerBotController.h"
|
|
|
| #include "AbilitySystemComponent.h"
|
| #include "AbilitySystemGlobals.h"
|
| #include "Engine/World.h"
|
| #include "GameFramework/PlayerState.h"
|
| #include "GameModes/LyraGameMode.h"
|
| #include "LyraLogChannels.h"
|
| #include "Perception/AIPerceptionComponent.h"
|
|
|
| #include UE_INLINE_GENERATED_CPP_BY_NAME(LyraPlayerBotController)
|
|
|
| class UObject;
|
|
|
| ALyraPlayerBotController::ALyraPlayerBotController(const FObjectInitializer& ObjectInitializer)
|
| : Super(ObjectInitializer)
|
| {
|
| bWantsPlayerState = true;
|
| bStopAILogicOnUnposses = false;
|
| }
|
|
|
| void ALyraPlayerBotController::OnPlayerStateChangedTeam(UObject* TeamAgent, int32 OldTeam, int32 NewTeam)
|
| {
|
| ConditionalBroadcastTeamChanged(this, IntegerToGenericTeamId(OldTeam), IntegerToGenericTeamId(NewTeam));
|
| }
|
|
|
| void ALyraPlayerBotController::OnPlayerStateChanged()
|
| {
|
|
|
| }
|
|
|
| void ALyraPlayerBotController::BroadcastOnPlayerStateChanged()
|
| {
|
| OnPlayerStateChanged();
|
|
|
|
|
| FGenericTeamId OldTeamID = FGenericTeamId::NoTeam;
|
| if (LastSeenPlayerState != nullptr)
|
| {
|
| if (ILyraTeamAgentInterface* PlayerStateTeamInterface = Cast<ILyraTeamAgentInterface>(LastSeenPlayerState))
|
| {
|
| OldTeamID = PlayerStateTeamInterface->GetGenericTeamId();
|
| PlayerStateTeamInterface->GetTeamChangedDelegateChecked().RemoveAll(this);
|
| }
|
| }
|
|
|
|
|
| FGenericTeamId NewTeamID = FGenericTeamId::NoTeam;
|
| if (PlayerState != nullptr)
|
| {
|
| if (ILyraTeamAgentInterface* PlayerStateTeamInterface = Cast<ILyraTeamAgentInterface>(PlayerState))
|
| {
|
| NewTeamID = PlayerStateTeamInterface->GetGenericTeamId();
|
| PlayerStateTeamInterface->GetTeamChangedDelegateChecked().AddDynamic(this, &ThisClass::OnPlayerStateChangedTeam);
|
| }
|
| }
|
|
|
|
|
| ConditionalBroadcastTeamChanged(this, OldTeamID, NewTeamID);
|
|
|
| LastSeenPlayerState = PlayerState;
|
| }
|
|
|
| void ALyraPlayerBotController::InitPlayerState()
|
| {
|
| Super::InitPlayerState();
|
| BroadcastOnPlayerStateChanged();
|
| }
|
|
|
| void ALyraPlayerBotController::CleanupPlayerState()
|
| {
|
| Super::CleanupPlayerState();
|
| BroadcastOnPlayerStateChanged();
|
| }
|
|
|
| void ALyraPlayerBotController::OnRep_PlayerState()
|
| {
|
| Super::OnRep_PlayerState();
|
| BroadcastOnPlayerStateChanged();
|
| }
|
|
|
| void ALyraPlayerBotController::SetGenericTeamId(const FGenericTeamId& NewTeamID)
|
| {
|
| UE_LOG(LogLyraTeams, Error, TEXT("You can't set the team ID on a player bot controller (%s); it's driven by the associated player state"), *GetPathNameSafe(this));
|
| }
|
|
|
| FGenericTeamId ALyraPlayerBotController::GetGenericTeamId() const
|
| {
|
| if (ILyraTeamAgentInterface* PSWithTeamInterface = Cast<ILyraTeamAgentInterface>(PlayerState))
|
| {
|
| return PSWithTeamInterface->GetGenericTeamId();
|
| }
|
| return FGenericTeamId::NoTeam;
|
| }
|
|
|
| FOnLyraTeamIndexChangedDelegate* ALyraPlayerBotController::GetOnTeamIndexChangedDelegate()
|
| {
|
| return &OnTeamChangedDelegate;
|
| }
|
|
|
|
|
| void ALyraPlayerBotController::ServerRestartController()
|
| {
|
| if (GetNetMode() == NM_Client)
|
| {
|
| return;
|
| }
|
|
|
| ensure((GetPawn() == nullptr) && IsInState(NAME_Inactive));
|
|
|
| if (IsInState(NAME_Inactive) || (IsInState(NAME_Spectating)))
|
| {
|
| ALyraGameMode* const GameMode = GetWorld()->GetAuthGameMode<ALyraGameMode>();
|
|
|
| if ((GameMode == nullptr) || !GameMode->ControllerCanRestart(this))
|
| {
|
| return;
|
| }
|
|
|
|
|
| if (GetPawn() != nullptr)
|
| {
|
| UnPossess();
|
| }
|
|
|
|
|
| ResetIgnoreInputFlags();
|
|
|
| GameMode->RestartPlayer(this);
|
| }
|
| }
|
|
|
| ETeamAttitude::Type ALyraPlayerBotController::GetTeamAttitudeTowards(const AActor& Other) const
|
| {
|
| if (const APawn* OtherPawn = Cast<APawn>(&Other)) {
|
|
|
| if (const ILyraTeamAgentInterface* TeamAgent = Cast<ILyraTeamAgentInterface>(OtherPawn->GetController()))
|
| {
|
| FGenericTeamId OtherTeamID = TeamAgent->GetGenericTeamId();
|
|
|
|
|
| if (OtherTeamID.GetId() != GetGenericTeamId().GetId())
|
| {
|
| return ETeamAttitude::Hostile;
|
| }
|
| else
|
| {
|
| return ETeamAttitude::Friendly;
|
| }
|
| }
|
| }
|
|
|
| return ETeamAttitude::Neutral;
|
| }
|
|
|
| void ALyraPlayerBotController::UpdateTeamAttitude(UAIPerceptionComponent* AIPerception)
|
| {
|
| if (AIPerception)
|
| {
|
| AIPerception->RequestStimuliListenerUpdate();
|
| }
|
| }
|
|
|
| void ALyraPlayerBotController::OnUnPossess()
|
| {
|
|
|
| if (APawn* PawnBeingUnpossessed = GetPawn())
|
| {
|
| if (UAbilitySystemComponent* ASC = UAbilitySystemGlobals::GetAbilitySystemComponentFromActor(PlayerState))
|
| {
|
| if (ASC->GetAvatarActor() == PawnBeingUnpossessed)
|
| {
|
| ASC->SetAvatarActor(nullptr);
|
| }
|
| }
|
| }
|
|
|
| Super::OnUnPossess();
|
| }
|
|
|
|
|