File size: 11,706 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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 | // Copyright Epic Games, Inc. All Rights Reserved.
#include "Teams/LyraTeamSubsystem.h"
#include "AbilitySystemGlobals.h"
#include "GameFramework/Controller.h"
#include "GameFramework/Pawn.h"
#include "LyraLogChannels.h"
#include "LyraTeamAgentInterface.h"
#include "LyraTeamCheats.h"
#include "LyraTeamPrivateInfo.h"
#include "LyraTeamPublicInfo.h"
#include "Player/LyraPlayerState.h"
#include "Teams/LyraTeamInfoBase.h"
#include UE_INLINE_GENERATED_CPP_BY_NAME(LyraTeamSubsystem)
class FSubsystemCollectionBase;
//////////////////////////////////////////////////////////////////////
// FLyraTeamTrackingInfo
void FLyraTeamTrackingInfo::SetTeamInfo(ALyraTeamInfoBase* Info)
{
if (ALyraTeamPublicInfo* NewPublicInfo = Cast<ALyraTeamPublicInfo>(Info))
{
ensure((PublicInfo == nullptr) || (PublicInfo == NewPublicInfo));
PublicInfo = NewPublicInfo;
ULyraTeamDisplayAsset* OldDisplayAsset = DisplayAsset;
DisplayAsset = NewPublicInfo->GetTeamDisplayAsset();
if (OldDisplayAsset != DisplayAsset)
{
OnTeamDisplayAssetChanged.Broadcast(DisplayAsset);
}
}
else if (ALyraTeamPrivateInfo* NewPrivateInfo = Cast<ALyraTeamPrivateInfo>(Info))
{
ensure((PrivateInfo == nullptr) || (PrivateInfo == NewPrivateInfo));
PrivateInfo = NewPrivateInfo;
}
else
{
checkf(false, TEXT("Expected a public or private team info but got %s"), *GetPathNameSafe(Info))
}
}
void FLyraTeamTrackingInfo::RemoveTeamInfo(ALyraTeamInfoBase* Info)
{
if (PublicInfo == Info)
{
PublicInfo = nullptr;
}
else if (PrivateInfo == Info)
{
PrivateInfo = nullptr;
}
else
{
ensureMsgf(false, TEXT("Expected a previously registered team info but got %s"), *GetPathNameSafe(Info));
}
}
//////////////////////////////////////////////////////////////////////
// ULyraTeamSubsystem
ULyraTeamSubsystem::ULyraTeamSubsystem()
{
}
void ULyraTeamSubsystem::Initialize(FSubsystemCollectionBase& Collection)
{
Super::Initialize(Collection);
auto AddTeamCheats = [](UCheatManager* CheatManager)
{
CheatManager->AddCheatManagerExtension(NewObject<ULyraTeamCheats>(CheatManager));
};
CheatManagerRegistrationHandle = UCheatManager::RegisterForOnCheatManagerCreated(FOnCheatManagerCreated::FDelegate::CreateLambda(AddTeamCheats));
}
void ULyraTeamSubsystem::Deinitialize()
{
UCheatManager::UnregisterFromOnCheatManagerCreated(CheatManagerRegistrationHandle);
Super::Deinitialize();
}
bool ULyraTeamSubsystem::RegisterTeamInfo(ALyraTeamInfoBase* TeamInfo)
{
if (!ensure(TeamInfo))
{
return false;
}
const int32 TeamId = TeamInfo->GetTeamId();
if (ensure(TeamId != INDEX_NONE))
{
FLyraTeamTrackingInfo& Entry = TeamMap.FindOrAdd(TeamId);
Entry.SetTeamInfo(TeamInfo);
return true;
}
return false;
}
bool ULyraTeamSubsystem::UnregisterTeamInfo(ALyraTeamInfoBase* TeamInfo)
{
if (!ensure(TeamInfo))
{
return false;
}
const int32 TeamId = TeamInfo->GetTeamId();
if (ensure(TeamId != INDEX_NONE))
{
FLyraTeamTrackingInfo* Entry = TeamMap.Find(TeamId);
// If it couldn't find the entry, this is probably a leftover actor from a previous world, ignore it
if (Entry)
{
Entry->RemoveTeamInfo(TeamInfo);
return true;
}
}
return false;
}
bool ULyraTeamSubsystem::ChangeTeamForActor(AActor* ActorToChange, int32 NewTeamIndex)
{
const FGenericTeamId NewTeamID = IntegerToGenericTeamId(NewTeamIndex);
if (ALyraPlayerState* LyraPS = const_cast<ALyraPlayerState*>(FindPlayerStateFromActor(ActorToChange)))
{
LyraPS->SetGenericTeamId(NewTeamID);
return true;
}
else if (ILyraTeamAgentInterface* TeamActor = Cast<ILyraTeamAgentInterface>(ActorToChange))
{
TeamActor->SetGenericTeamId(NewTeamID);
return true;
}
else
{
return false;
}
}
int32 ULyraTeamSubsystem::FindTeamFromObject(const UObject* TestObject) const
{
// See if it's directly a team agent
if (const ILyraTeamAgentInterface* ObjectWithTeamInterface = Cast<ILyraTeamAgentInterface>(TestObject))
{
return GenericTeamIdToInteger(ObjectWithTeamInterface->GetGenericTeamId());
}
if (const AActor* TestActor = Cast<const AActor>(TestObject))
{
// See if the instigator is a team actor
if (const ILyraTeamAgentInterface* InstigatorWithTeamInterface = Cast<ILyraTeamAgentInterface>(TestActor->GetInstigator()))
{
return GenericTeamIdToInteger(InstigatorWithTeamInterface->GetGenericTeamId());
}
// TeamInfo actors don't actually have the team interface, so they need a special case
if (const ALyraTeamInfoBase* TeamInfo = Cast<ALyraTeamInfoBase>(TestActor))
{
return TeamInfo->GetTeamId();
}
// Fall back to finding the associated player state
if (const ALyraPlayerState* LyraPS = FindPlayerStateFromActor(TestActor))
{
return LyraPS->GetTeamId();
}
}
return INDEX_NONE;
}
const ALyraPlayerState* ULyraTeamSubsystem::FindPlayerStateFromActor(const AActor* PossibleTeamActor) const
{
if (PossibleTeamActor != nullptr)
{
if (const APawn* Pawn = Cast<const APawn>(PossibleTeamActor))
{
//@TODO: Consider an interface instead or have team actors register with the subsystem and have it maintain a map? (or LWC style)
if (ALyraPlayerState* LyraPS = Pawn->GetPlayerState<ALyraPlayerState>())
{
return LyraPS;
}
}
else if (const AController* PC = Cast<const AController>(PossibleTeamActor))
{
if (ALyraPlayerState* LyraPS = Cast<ALyraPlayerState>(PC->PlayerState))
{
return LyraPS;
}
}
else if (const ALyraPlayerState* LyraPS = Cast<const ALyraPlayerState>(PossibleTeamActor))
{
return LyraPS;
}
// Try the instigator
// if (AActor* Instigator = PossibleTeamActor->GetInstigator())
// {
// if (ensure(Instigator != PossibleTeamActor))
// {
// return FindPlayerStateFromActor(Instigator);
// }
// }
}
return nullptr;
}
ELyraTeamComparison ULyraTeamSubsystem::CompareTeams(const UObject* A, const UObject* B, int32& TeamIdA, int32& TeamIdB) const
{
TeamIdA = FindTeamFromObject(Cast<const AActor>(A));
TeamIdB = FindTeamFromObject(Cast<const AActor>(B));
if ((TeamIdA == INDEX_NONE) || (TeamIdB == INDEX_NONE))
{
return ELyraTeamComparison::InvalidArgument;
}
else
{
return (TeamIdA == TeamIdB) ? ELyraTeamComparison::OnSameTeam : ELyraTeamComparison::DifferentTeams;
}
}
ELyraTeamComparison ULyraTeamSubsystem::CompareTeams(const UObject* A, const UObject* B) const
{
int32 TeamIdA;
int32 TeamIdB;
return CompareTeams(A, B, /*out*/ TeamIdA, /*out*/ TeamIdB);
}
void ULyraTeamSubsystem::FindTeamFromActor(const UObject* TestObject, bool& bIsPartOfTeam, int32& TeamId) const
{
TeamId = FindTeamFromObject(TestObject);
bIsPartOfTeam = TeamId != INDEX_NONE;
}
void ULyraTeamSubsystem::AddTeamTagStack(int32 TeamId, FGameplayTag Tag, int32 StackCount)
{
auto FailureHandler = [&](const FString& ErrorMessage)
{
UE_LOG(LogLyraTeams, Error, TEXT("AddTeamTagStack(TeamId: %d, Tag: %s, StackCount: %d) %s"), TeamId, *Tag.ToString(), StackCount, *ErrorMessage);
};
if (FLyraTeamTrackingInfo* Entry = TeamMap.Find(TeamId))
{
if (Entry->PublicInfo)
{
if (Entry->PublicInfo->HasAuthority())
{
Entry->PublicInfo->TeamTags.AddStack(Tag, StackCount);
}
else
{
FailureHandler(TEXT("failed because it was called on a client"));
}
}
else
{
FailureHandler(TEXT("failed because there is no team info spawned yet (called too early, before the experience was ready)"));
}
}
else
{
FailureHandler(TEXT("failed because it was passed an unknown team id"));
}
}
void ULyraTeamSubsystem::RemoveTeamTagStack(int32 TeamId, FGameplayTag Tag, int32 StackCount)
{
auto FailureHandler = [&](const FString& ErrorMessage)
{
UE_LOG(LogLyraTeams, Error, TEXT("RemoveTeamTagStack(TeamId: %d, Tag: %s, StackCount: %d) %s"), TeamId, *Tag.ToString(), StackCount, *ErrorMessage);
};
if (FLyraTeamTrackingInfo* Entry = TeamMap.Find(TeamId))
{
if (Entry->PublicInfo)
{
if (Entry->PublicInfo->HasAuthority())
{
Entry->PublicInfo->TeamTags.RemoveStack(Tag, StackCount);
}
else
{
FailureHandler(TEXT("failed because it was called on a client"));
}
}
else
{
FailureHandler(TEXT("failed because there is no team info spawned yet (called too early, before the experience was ready)"));
}
}
else
{
FailureHandler(TEXT("failed because it was passed an unknown team id"));
}
}
int32 ULyraTeamSubsystem::GetTeamTagStackCount(int32 TeamId, FGameplayTag Tag) const
{
if (const FLyraTeamTrackingInfo* Entry = TeamMap.Find(TeamId))
{
const int32 PublicStackCount = (Entry->PublicInfo != nullptr) ? Entry->PublicInfo->TeamTags.GetStackCount(Tag) : 0;
const int32 PrivateStackCount = (Entry->PrivateInfo != nullptr) ? Entry->PrivateInfo->TeamTags.GetStackCount(Tag) : 0;
return PublicStackCount + PrivateStackCount;
}
else
{
UE_LOG(LogLyraTeams, Verbose, TEXT("GetTeamTagStackCount(TeamId: %d, Tag: %s) failed because it was passed an unknown team id"), TeamId, *Tag.ToString());
return 0;
}
}
bool ULyraTeamSubsystem::TeamHasTag(int32 TeamId, FGameplayTag Tag) const
{
return GetTeamTagStackCount(TeamId, Tag) > 0;
}
bool ULyraTeamSubsystem::DoesTeamExist(int32 TeamId) const
{
return TeamMap.Contains(TeamId);
}
TArray<int32> ULyraTeamSubsystem::GetTeamIDs() const
{
TArray<int32> Result;
TeamMap.GenerateKeyArray(Result);
Result.Sort();
return Result;
}
bool ULyraTeamSubsystem::CanCauseDamage(const UObject* Instigator, const UObject* Target, bool bAllowDamageToSelf) const
{
if (bAllowDamageToSelf)
{
if ((Instigator == Target) || (FindPlayerStateFromActor(Cast<AActor>(Instigator)) == FindPlayerStateFromActor(Cast<AActor>(Target))))
{
return true;
}
}
int32 InstigatorTeamId;
int32 TargetTeamId;
const ELyraTeamComparison Relationship = CompareTeams(Instigator, Target, /*out*/ InstigatorTeamId, /*out*/ TargetTeamId);
if (Relationship == ELyraTeamComparison::DifferentTeams)
{
return true;
}
else if ((Relationship == ELyraTeamComparison::InvalidArgument) && (InstigatorTeamId != INDEX_NONE))
{
// Allow damaging non-team actors for now, as long as they have an ability system component
//@TODO: This is temporary until the target practice dummy has a team assignment
return UAbilitySystemGlobals::GetAbilitySystemComponentFromActor(Cast<const AActor>(Target)) != nullptr;
}
return false;
}
ULyraTeamDisplayAsset* ULyraTeamSubsystem::GetTeamDisplayAsset(int32 TeamId, int32 ViewerTeamId)
{
// Currently ignoring ViewerTeamId
if (FLyraTeamTrackingInfo* Entry = TeamMap.Find(TeamId))
{
return Entry->DisplayAsset;
}
return nullptr;
}
ULyraTeamDisplayAsset* ULyraTeamSubsystem::GetEffectiveTeamDisplayAsset(int32 TeamId, UObject* ViewerTeamAgent)
{
return GetTeamDisplayAsset(TeamId, FindTeamFromObject(ViewerTeamAgent));
}
void ULyraTeamSubsystem::NotifyTeamDisplayAssetModified(ULyraTeamDisplayAsset* /*ModifiedAsset*/)
{
// Broadcasting to all observers when a display asset is edited right now, instead of only the edited one
for (const auto& KVP : TeamMap)
{
const int32 TeamId = KVP.Key;
const FLyraTeamTrackingInfo& TrackingInfo = KVP.Value;
TrackingInfo.OnTeamDisplayAssetChanged.Broadcast(TrackingInfo.DisplayAsset);
}
}
FOnLyraTeamDisplayAssetChangedDelegate& ULyraTeamSubsystem::GetTeamDisplayAssetChangedDelegate(int32 TeamId)
{
return TeamMap.FindOrAdd(TeamId).OnTeamDisplayAssetChanged;
}
|