Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

10.2 KB
Raw Blame
#include "Rules/VerseGameMode.h"

#include "Damage/VerseCombatStateComponent.h"
#include "Engine/World.h"
#include "EngineUtils.h"
#include "Events/VerseEventHub.h"
#include "GameFramework/Controller.h"
#include "GameFramework/GameStateBase.h"
#include "GameFramework/Pawn.h"
#include "Misc/FileHelper.h"
#include "Rules/VersePlayerState.h"
#include "Rules/VerseRulesetConfig.h"
#include "Rules/VerseRuleset_Deathmatch.h"
#include "Rules/VerseSpawnPoint.h"
#include "UObject/UnrealType.h"
#include "VerseCollision.h"
#include "VerseLog.h"

namespace
{
	/**
	 * Navmesh path length in cm, or -1 when there is no navigation system or no path.
	 * VerseCombat has no NavigationSystem edge in the module DAG (architecture 2), so the static
	 * UFUNCTION UNavigationSystemV1::FindPathToLocationSynchronously is called through
	 * reflection and the returned UNavigationPath's PathPoints are summed (as VerseCrew does).
	 */
	float NavPathLength(UWorld* World, const FVector& From, const FVector& To)
	{
		if (!World || !World->GetNavigationSystem())
		{
			return -1.f;
		}
		static TWeakObjectPtr<UClass> NavClass;
		static TWeakObjectPtr<UFunction> FindPath;
		if (!FindPath.IsValid())
		{
			NavClass = FindObject<UClass>(nullptr, TEXT("/Script/NavigationSystem.NavigationSystemV1"));
			FindPath = NavClass.IsValid() ? NavClass->FindFunctionByName(TEXT("FindPathToLocationSynchronously")) : nullptr;
			if (!FindPath.IsValid())
			{
				return -1.f;
			}
		}
		UFunction* Function = FindPath.Get();
		uint8* Params = static_cast<uint8*>(FMemory::Malloc(FMath::Max<int32>(Function->ParmsSize, 1), 16));
		FMemory::Memzero(Params, Function->ParmsSize);
		for (TFieldIterator<FProperty> It(Function); It && It->HasAnyPropertyFlags(CPF_Parm); ++It)
		{
			It->InitializeValue_InContainer(Params);
		}
		float Length = -1.f;
		FObjectProperty* WorldParam = CastField<FObjectProperty>(Function->FindPropertyByName(TEXT("WorldContextObject")));
		FStructProperty* StartParam = CastField<FStructProperty>(Function->FindPropertyByName(TEXT("PathStart")));
		FStructProperty* EndParam = CastField<FStructProperty>(Function->FindPropertyByName(TEXT("PathEnd")));
		FObjectProperty* ReturnParam = CastField<FObjectProperty>(Function->GetReturnProperty());
		if (WorldParam && StartParam && EndParam && ReturnParam)
		{
			WorldParam->SetObjectPropertyValue_InContainer(Params, World);
			*StartParam->ContainerPtrToValuePtr<FVector>(Params) = From;
			*EndParam->ContainerPtrToValuePtr<FVector>(Params) = To;
			NavClass->GetDefaultObject()->ProcessEvent(Function, Params);
			if (UObject* Path = ReturnParam->GetObjectPropertyValue_InContainer(Params))
			{
				if (FArrayProperty* Points = CastField<FArrayProperty>(Path->GetClass()->FindPropertyByName(TEXT("PathPoints"))))
				{
					FScriptArrayHelper Helper(Points, Points->ContainerPtrToValuePtr<void>(Path));
					if (Helper.Num() >= 2)
					{
						double Sum = 0.0;
						for (int32 I = 1; I < Helper.Num(); ++I)
						{
							Sum += FVector::Dist(*reinterpret_cast<const FVector*>(Helper.GetRawPtr(I - 1)),
								*reinterpret_cast<const FVector*>(Helper.GetRawPtr(I)));
						}
						Length = float(Sum);
					}
				}
			}
		}
		for (TFieldIterator<FProperty> It(Function); It && It->HasAnyPropertyFlags(CPF_Parm); ++It)
		{
			It->DestroyValue_InContainer(Params);
		}
		FMemory::Free(Params);
		return Length;
	}
}

AVerseGameMode::AVerseGameMode()
{
	PlayerStateClass = AVersePlayerState::StaticClass();
	PrimaryActorTick.bCanEverTick = false;
}

const UVerseRulesetConfig* AVerseGameMode::ResolveConfig()
{
	if (!RulesetConfig)
	{
		RulesetConfig = NewObject<UVerseRulesetConfig>(this, TEXT("RulesetConfig_Deathmatch"));
		RulesetConfig->RulesetClass = UVerseRuleset_Deathmatch::StaticClass();
		const FString Path = UVerseRulesetConfig::RulesFilePath(TEXT("deathmatch"));
		FString Text;
		FString Error;
		if (!FFileHelper::LoadFileToString(Text, *Path))
		{
			UE_LOG(LogVerseCombat, Warning, TEXT("Rules: %s not found, using the built-in start values"), *Path);
		}
		else if (!RulesetConfig->ApplyJson(Text, Error))
		{
			UE_LOG(LogVerseCombat, Error, TEXT("Rules: %s: %s; using the built-in start values"), *Path, *Error);
			RulesetConfig = NewObject<UVerseRulesetConfig>(this, TEXT("RulesetConfig_Defaults"));
			RulesetConfig->RulesetClass = UVerseRuleset_Deathmatch::StaticClass();
		}
	}
	return RulesetConfig;
}

void AVerseGameMode::CreateRuleset()
{
	const UVerseRulesetConfig* Config = ResolveConfig();
	TSubclassOf<UVerseRuleset> Class = Config && Config->RulesetClass ? Config->RulesetClass : TSubclassOf<UVerseRuleset>(UVerseRuleset_Deathmatch::StaticClass());
	Ruleset = NewObject<UVerseRuleset>(this, Class);
	Ruleset->SetHost(MakeDefaultHost());
	Ruleset->Init(this, Config);
}

void AVerseGameMode::InitGame(const FString& MapName, const FString& Options, FString& ErrorMessage)
{
	Super::InitGame(MapName, Options, ErrorMessage);
	CreateRuleset();
}

void AVerseGameMode::BeginPlay()
{
	Super::BeginPlay();
	if (!Ruleset)
	{
		CreateRuleset();
	}
	if (UVerseEventHub* Hub = UVerseEventHub::Get(this))
	{
		ArenaHandle = Hub->OnArenaSwitch.AddUObject(this, &AVerseGameMode::HandleArenaSwitch);
		DeathHandle = Hub->OnDeath.AddUObject(this, &AVerseGameMode::HandleDeath);
	}
}

void AVerseGameMode::EndPlay(const EEndPlayReason::Type EndPlayReason)
{
	// Verse quitting mid-set sends the running set as unfinished (9.1 Leaving).
	if (UVerseRuleset_Deathmatch* Deathmatch = Cast<UVerseRuleset_Deathmatch>(Ruleset))
	{
		Deathmatch->EndMatch();
	}
	if (UVerseEventHub* Hub = UVerseEventHub::Get(this))
	{
		Hub->OnArenaSwitch.Remove(ArenaHandle);
		Hub->OnDeath.Remove(DeathHandle);
	}
	Super::EndPlay(EndPlayReason);
}

void AVerseGameMode::HandleArenaSwitch(const FVerseArenaSwitchEvent& E)
{
	if (Ruleset && E.bArenaOn)
	{
		Ruleset->OnMatchStart();
	}
}

void AVerseGameMode::HandleDeath(const FVerseDeathEvent& E)
{
	if (Ruleset)
	{
		Ruleset->OnKill(E);
	}
}

AActor* AVerseGameMode::ChoosePlayerStart_Implementation(AController* Player)
{
	const UVerseRuleset_Deathmatch* Deathmatch = Cast<UVerseRuleset_Deathmatch>(Ruleset);
	if (Ruleset && Player && (!Deathmatch || Deathmatch->IsMatchRunning()))
	{
		if (AVerseSpawnPoint* Spawn = Ruleset->ChooseSpawn(Player->GetPlayerState<AVersePlayerState>()))
		{
			return Spawn;
		}
	}
	return Super::ChoosePlayerStart_Implementation(Player);
}

bool AVerseGameMode::CanDamage(const FVerseDamageContext& Ctx) const
{
	return Ruleset ? Ruleset->CanDamage(Ctx) : true;
}

float AVerseGameMode::ModifyDamage(const FVerseDamageContext& Ctx, float Damage) const
{
	return Ruleset ? Ruleset->ModifyDamage(Ctx, Damage) : Damage;
}

void AVerseGameMode::NotifyPlayerAction(AVersePlayerState* Player, EVersePlayerAction Action)
{
	if (Ruleset)
	{
		Ruleset->OnPlayerAction(Player, Action);
	}
}

void AVerseGameMode::GetLoadout(const AVersePlayerState* Player, TMap<EVerseLoadoutSlot, const UPrimaryDataAsset*>& Out) const
{
	Out.Reset();
	if (Ruleset)
	{
		Ruleset->GetLoadout(Player, Out);
	}
}

bool AVerseGameMode::ShouldResetDestruction(EVerseBoundary Boundary) const
{
	return Ruleset ? Ruleset->ShouldResetDestruction(Boundary) : false;
}

FVerseRulesHost AVerseGameMode::MakeDefaultHost()
{
	FVerseRulesHost Host;
	TWeakObjectPtr<AVerseGameMode> Self(this);
	Host.ServerTime = [Self]() { return Self.IsValid() && Self->GetWorld() ? Self->GetWorld()->GetTimeSeconds() : 0.f; };
	Host.Players = [Self]() {
		TArray<AVersePlayerState*> Out;
		const AGameStateBase* State = Self.IsValid() ? Self->GameState.Get() : nullptr;
		if (State)
		{
			for (APlayerState* P : State->PlayerArray)
			{
				if (AVersePlayerState* V = Cast<AVersePlayerState>(P))
				{
					Out.Add(V);
				}
			}
		}
		return Out;
	};
	Host.SpawnPoints = [Self]() {
		TArray<AVerseSpawnPoint*> Out;
		if (UWorld* World = Self.IsValid() ? Self->GetWorld() : nullptr)
		{
			for (TActorIterator<AVerseSpawnPoint> It(World); It; ++It)
			{
				Out.Add(*It);
			}
		}
		return Out;
	};
	Host.SpawnFloor = [](const AVerseSpawnPoint* S) { return S->GetFloorLocation(); };
	Host.EyePoint = [](const AVersePlayerState* P, FVector& OutEye) {
		const APawn* Pawn = P ? P->GetPawn() : nullptr;
		if (!Pawn)
		{
			return false;
		}
		FRotator Rotation;
		Pawn->GetActorEyesViewPoint(OutEye, Rotation);
		return true;
	};
	Host.LineClear = [Self](const FVector& A, const FVector& B) {
		UWorld* World = Self.IsValid() ? Self->GetWorld() : nullptr;
		if (!World)
		{
			return true;
		}
		// Pawns never hide a spawn: only world geometry and the current piece proxies do.
		FCollisionQueryParams Params(SCENE_QUERY_STAT(VerseSpawnSight), false);
		for (TActorIterator<APawn> It(World); It; ++It)
		{
			Params.AddIgnoredActor(*It);
		}
		return !World->LineTraceTestByChannel(A, B, VerseCollision::Bolt, Params);
	};
	Host.FloorPoint = [](const AVersePlayerState* P, FVector& OutFloor) {
		const APawn* Pawn = P ? P->GetPawn() : nullptr;
		if (!Pawn)
		{
			return false;
		}
		OutFloor = Pawn->GetNavAgentLocation();
		return true;
	};
	// Navmesh path length through reflection; "no path" (-1) falls back to Euclidean (9.2).
	Host.PathLength = [Self](const FVector& A, const FVector& B) {
		return NavPathLength(Self.IsValid() ? Self->GetWorld() : nullptr, A, B);
	};
	Host.PawnNear = [Self](const FVector& Floor, float RadiusCm, const AVersePlayerState* Ignore) {
		UWorld* World = Self.IsValid() ? Self->GetWorld() : nullptr;
		if (!World)
		{
			return false;
		}
		for (TActorIterator<APawn> It(World); It; ++It)
		{
			if (Ignore && It->GetPlayerState() == Ignore)
			{
				continue;
			}
			if (FVector::Dist2D(It->GetActorLocation(), Floor) <= RadiusCm && FMath::Abs(It->GetActorLocation().Z - Floor.Z) <= 300.0)
			{
				return true;
			}
		}
		return false;
	};
	Host.Respawn = [Self](AVersePlayerState* P, AVerseSpawnPoint* Spawn) {
		AController* Controller = P ? P->GetOwningController() : nullptr;
		if (!Self.IsValid() || !Controller)
		{
			return;
		}
		if (APawn* Old = Controller->GetPawn())
		{
			// The body stays for the kill cam and gore, then goes.
			Controller->UnPossess();
			Old->SetLifeSpan(10.f);
		}
		if (Spawn)
		{
			Self->RestartPlayerAtPlayerStart(Controller, Spawn);
		}
		else
		{
			Self->RestartPlayer(Controller);
		}
	};
	Host.CombatState = [](const AVersePlayerState* P) -> UVerseCombatStateComponent* {
		const APawn* Pawn = P ? P->GetPawn() : nullptr;
		return Pawn ? Pawn->FindComponentByClass<UVerseCombatStateComponent>() : nullptr;
	};
	return Host;
}