Files
integration/Source/Integration/LuprexGameModeBase.cpp

410 lines
12 KiB
C++

// Copyright Epic Games, Inc. All Rights Reserved.
#include "LuprexGameModeBase.h"
#include "lpx-drvutil.hpp"
#include "lpx-paths.hpp"
#include "Tangible.h"
#include "TangibleManager.h"
#include "LuaCall.h"
#include "Blueprint/UserWidget.h"
#include "Blueprint/WidgetBlueprintLibrary.h"
#include "Kismet/GameplayStatics.h"
#include "Common.h"
#include "AnimQueue.h"
#include <string>
#include <string_view>
using namespace LpxCommonTypes;
ALuprexGameModeBase::ALuprexGameModeBase()
{
TangibleManager = nullptr;
AssetLookup = nullptr;
PlayerId = 0;
EngineSeconds = 0.0;
MustCallLookAtChanged = false;
//PrimaryActorTick.bCanEverTick = true; // Probably wrong
//PrimaryActorTick.bTickEvenWhenPaused = true; // Probably wrong
//PrimaryActorTick.TickGroup = TG_PrePhysics; // Probably wrong
ResetToInitialState();
}
ALuprexGameModeBase::~ALuprexGameModeBase()
{
ResetToInitialState();
}
// This method runs in the background thread,
// at the moment we trigger it.
//
uint32 ALuprexGameModeBase::Run() {
FlxLockedWrapper lockedwrap(LockableWrapper);
if (lockedwrap->get_rescan_lua_source(lockedwrap.Get()))
{
drvutil::ostringstream srcpak;
std::string srcpakerr = drvutil::package_lua_source(LUPREX_ROOT_PATH, &srcpak);
if (!srcpakerr.empty())
{
FString FMessage((const UTF8CHAR *)(srcpakerr.c_str()));
UE_LOG(LogLuprexIntegration, Error, TEXT("Trying to read Lua source: %s"), *FMessage);
}
else
{
std::string_view srcpakv = srcpak.view();
lockedwrap->play_access(lockedwrap.Get(), AccessKind::INVOKE_LUA_SOURCE, 0, srcpakv.size(), srcpakv.data(), nullptr, nullptr);
}
}
Sockets->Update(lockedwrap);
lockedwrap->play_update(lockedwrap.Get(), EngineSeconds);
Sockets->Update(lockedwrap);
return 0;
}
void ALuprexGameModeBase::ResetToInitialState()
{
Playing = false;
TickEnabled = true;
if (TangibleManager != nullptr) {
TangibleManager->ConditionalBeginDestroy();
TangibleManager = nullptr;
}
if (AssetLookup != nullptr) {
AssetLookup->ConditionalBeginDestroy();
AssetLookup = nullptr;
}
// Stop the tick functions.
FWorldDelegates::OnWorldPreActorTick.Remove(OnWorldPreActorTickHandle);
FWorldDelegates::OnWorldPostActorTick.Remove(OnWorldPostActorTickHandle);
OnWorldPreActorTickHandle.Reset();
OnWorldPostActorTickHandle.Reset();
// Shut down the thread
LuprexUpdateTask.Shutdown();
// Now that the thread's gone, we should be able to
// just claim and hold the lock on the wrapper.
FlxLockedWrapper w(LockableWrapper);
// Release and close all sockets.
if (Sockets != nullptr)
{
Sockets->ForceCloseEverything(w);
Sockets.Reset();
}
// Delete the engine.
if (w->release != nullptr)
{
w->release(w.Get());
}
// Stop trapping log errors to the debugger.
BreakToDebuggerLogVerbosityDevice.Reset();
// Clear the lua call assembly buffer.
LuaCallBuffer.clear();
// Clear the PlayerID
PlayerId = 0;
// Clear the look-at state;
CurrentLookAt.Init();
MustCallLookAtChanged = false;
// Reset the clocks.
EngineSeconds = 0.0;
NextRotateCube = 1.0;
}
void ALuprexGameModeBase::UpdateConsoleOutput() {
// Copy Luprex Stdout into the console.
FlxLockedWrapper lockedwrap(LockableWrapper);
if (Playing) {
FString Text = lockedwrap.FetchStdout();
if (!Text.IsEmpty())
{
ConsoleAddOutput(Text);
}
}
}
#pragma optimize("", off)
void ALuprexGameModeBase::UpdateTangibles() {
double radius = 1000.0; // Hardwired for now.
using TanArray = UlxTangibleManager::TanArray;
if (!Playing) return;
TanArray alltans;
{
FlxLockedWrapper w(LockableWrapper);
PlayerId = w.GetActor();
IdView nearids = w.GetNear(PlayerId, radius, radius, radius);
TangibleManager->UpdateNearAccordingToLuprex(nearids);
alltans = TangibleManager->GetAllTangibles();
IdArray allids = TangibleManager->GetIds(alltans);
StringViewVec allqueues = w.GetAnimationQueues(allids);
for (int i = 0; i < alltans.Num(); i++) {
alltans[i]->UpdateAnimationQueue(allqueues[i]);
}
// Debugging hook.
volatile bool printaqs = false;
if (printaqs) {
for (int i = 0; i < alltans.Num(); i++) {
UE_LOG(LogLuprex, Display, TEXT("\n--- AQ of %ld ---\n%s\n"), allids[i], *(alltans[i]->AnimTracker.DebugString()));
}
}
}
// This is where we run the blueprint code that updates animation
// states. Be aware that the blueprint code could call back
// into the gamemode, which could in turn access the FlxLockedWrapper.
// This is why we've released the FlxLockedWrapper before doing this.
for (int i = 0; i < alltans.Num(); i++) {
alltans[i]->MaybeExecuteAnimStateChanged();
}
TangibleManager->RecalcNearAccordingToUnreal(PlayerId, radius);
TangibleManager->DeleteFarawayTangibles();
}
void ALuprexGameModeBase::UpdatePossessedTangible() {
UlxTangible *ptan = TangibleManager->GetPossessedTangible();
UlxTangible *tan = TangibleManager->GetTangible(PlayerId);
APlayerController *ctrl = GetWorld()->GetFirstPlayerController();
APawn *pawn = nullptr;
if (tan != nullptr) pawn = Cast<APawn>(tan->GetActor());
if (pawn == nullptr) {
if (ptan != nullptr) {
TangibleManager->SetPossessedTangible(nullptr);
ctrl->Possess(nullptr);
}
} else {
if (ptan != tan) {
TangibleManager->SetPossessedTangible(tan);
ctrl->Possess(pawn);
}
}
}
UlxLuaValues *ALuprexGameModeBase::LuaCallEnd(AccessKind kind, int64 place_id) {
std::string_view datapk = LuaCallBuffer.view();
FlxLockedWrapper w(LockableWrapper);
if (place_id == 0) place_id = w.GetActor();
uint32_t retpklen;
const char *retpk;
w->play_access(w.Get(), kind, place_id, datapk.size(), datapk.data(), &retpklen, &retpk);
if (kind == AccessKind::PROBE_LUA_CALL)
{
UlxLuaValues *Result = NewObject<UlxLuaValues>(this);
Result->Initialize(std::string_view(retpk, retpklen));
return Result;
}
else return nullptr;
}
UlxLuaValues *ALuprexGameModeBase::LuaCallEnd(AccessKind kind) {
return LuaCallEnd(kind, int64(0));
}
UlxLuaValues *ALuprexGameModeBase::LuaCallEnd(AccessKind kind, AActor *place) {
if (place == nullptr) {
return LuaCallEnd(kind, int64(0));
} else {
UlxTangible *tan = UlxTangible::GetActorTangibleOrLog(place);
if (tan == nullptr) {
return NewObject<UlxLuaValues>(this);
} else {
return LuaCallEnd(kind, tan->TangibleId);
}
}
}
FString ALuprexGameModeBase::ValidateLuaExpr(const FString &Code)
{
FTCHARToUTF8 UCode(*Code);
FlxLockedWrapper w(LockableWrapper);
uint32_t retpklen;
const char *retpk;
w->play_access(w.Get(), AccessKind::VALIDATE_LUA_EXPR, 0, UCode.Length(), UCode.Get(), &retpklen, &retpk);
FString Result(retpklen, (const UTF8CHAR*)retpk);
return Result;
}
void ALuprexGameModeBase::InvokeLuaExpr(const FString &Code)
{
FTCHARToUTF8 UCode(*Code);
FlxLockedWrapper w(LockableWrapper);
uint32_t retpklen;
const char *retpk;
w->play_access(w.Get(), AccessKind::INVOKE_LUA_EXPR, 0, UCode.Length(), UCode.Get(), &retpklen, &retpk);
}
void ALuprexGameModeBase::OnWorldPreActorTick(UWorld* InWorld, ELevelTick InLevelTick, float deltaseconds)
{
if(Playing && TickEnabled && (GetWorld() == InWorld) && (InLevelTick == LEVELTICK_All))
{
LuprexUpdateTask.Wait();
EngineSeconds += deltaseconds;
UpdateConsoleOutput();
UpdateTangibles();
UpdatePossessedTangible();
UpdateLookAt();
}
}
void ALuprexGameModeBase::OnWorldPostActorTick(UWorld* InWorld, ELevelTick InLevelTick, float deltaseconds)
{
if(Playing && TickEnabled && (GetWorld() == InWorld) && (InLevelTick == LEVELTICK_All))
{
LuprexUpdateTask.Trigger();
}
}
void ALuprexGameModeBase::BeginPlay()
{
ResetToInitialState();
InitializeGlobalState();
Super::BeginPlay();
}
void ALuprexGameModeBase::InitializeGlobalState()
{
FlxLockedWrapper w(LockableWrapper);
// Sanity checks. Make sure everything is clean.
checkf(!LuprexUpdateTask.IsRunning(), TEXT("There should be no thread here."));
checkf(w->engine == nullptr, TEXT("There should be no engine here."));
// Try to initialize the wrapper.
w.InitWrapper();
// If we failed to initialize the wrapper, print an error message.
if (w->play_initialize == nullptr)
{
UE_LOG(LogLuprexIntegration, Error, TEXT("Luprex wrapper initialization failed"));
}
// If wrapper is initialized, try to initialize the luprex engine.
if (w->play_initialize != nullptr)
{
w->play_initialize(w.Get(), "lpxclient", "");
if (w->error[0])
{
FString FMessage((const UTF8CHAR *)w->error);
UE_LOG(LogLuprexIntegration, Error, TEXT("Calling Luprex play_initialize: %s"), *FMessage);
}
if (w->engine != nullptr) {
UE_LOG(LogLuprexIntegration, Verbose, TEXT("Luprex initialization success."));
Playing = true;
}
}
// Possibly tell the engine to connect to a server.
if (Playing) {
FString LuprexServer;
FParse::Value(FCommandLine::Get(), TEXT("-LuprexServer="), LuprexServer);
LuprexServer = LuprexServer.ToLower();
UE_LOG(LogTemp, Display, TEXT("LuprexServer = %s"), *LuprexServer)
if (LuprexServer != TEXT("standalone"))
{
FTCHARToUTF8 utf8server(LuprexServer);
w->play_access(w.Get(), AccessKind::CONNECT_TO_SERVER, 0, utf8server.Length(), utf8server.Get(), nullptr, nullptr);
}
}
// If we successfully created a luprex engine, create a socket system and a worker thread.
if (Playing) {
Sockets.Reset(FlxSockets::Create(w));
std::string error = Sockets->GetError();
check(error.empty());
LuprexUpdateTask.Startup(this);
}
if (Playing) {
OnWorldPreActorTickHandle = FWorldDelegates::OnWorldPreActorTick.AddUObject(this, &ALuprexGameModeBase::OnWorldPreActorTick);
OnWorldPostActorTickHandle = FWorldDelegates::OnWorldPostActorTick.AddUObject(this, &ALuprexGameModeBase::OnWorldPostActorTick);
}
// Initialize the asset lookup table.
AssetLookup = NewObject<UlxAssetLookup>(this);
AssetLookup->RebuildIndex();
// Initialize the tangible manager.
TangibleManager = NewObject<UlxTangibleManager>(this);
TangibleManager->Init(this);
// If somebody generates a log message that's severe enough, break to debugger.
BreakToDebuggerLogVerbosityDevice.Reset(
new FlxBreakToDebuggerOutputDevice(BreakToDebuggerLogVerbosity));
}
void ALuprexGameModeBase::EndPlay(const EEndPlayReason::Type EndPlayReason)
{
ResetToInitialState();
Super::EndPlay(EndPlayReason);
}
int64 ALuprexGameModeBase::GetPlayerId() {
return PlayerId;
}
ALuprexGameModeBase *ALuprexGameModeBase::FromContext(const UObject *context) {
ALuprexGameModeBase *result = context->GetWorld()->GetAuthGameMode<ALuprexGameModeBase>();
if (result == nullptr) {
UE_LOG(LogBlueprint, Fatal, TEXT("Not currently using a Luprex Game Mode."));
}
return result;
}
void ALuprexGameModeBase::SetLookAt(const UObject *Context, const FHitResult &HitResult)
{
ALuprexGameModeBase *Mode = FromContext(Context);
if (Mode->CurrentLookAt.HitObjectHandle != HitResult.HitObjectHandle)
{
Mode->MustCallLookAtChanged = true;
}
Mode->CurrentLookAt = HitResult;
}
void ALuprexGameModeBase::SetLookAtChanged(const UObject *Context)
{
ALuprexGameModeBase *Mode = FromContext(Context);
Mode->MustCallLookAtChanged = true;
}
FVector2D ALuprexGameModeBase::GetLookAtPixel(const UObject *Context)
{
ALuprexGameModeBase *Mode = FromContext(Context);
APlayerController *pc = Context->GetWorld()->GetFirstPlayerController();
if (pc == nullptr) return FVector2D();
FVector2D ScreenPosition;
if (!UGameplayStatics::ProjectWorldToScreen(pc, Mode->CurrentLookAt.Location, ScreenPosition, false))
{
return FVector2D();
}
return ScreenPosition;
}
void ALuprexGameModeBase::UpdateLookAt() {
// Make sure the world is fully configured before we attempt to cast rays.
UlxTangible *possessed = TangibleManager->GetPossessedTangible();
if (possessed == nullptr) return;
APlayerController *pc = GetWorld()->GetFirstPlayerController();
if (pc == nullptr) return;
APawn *pawn = pc->GetPawn();
if (pawn == nullptr) return;
if (possessed->GetActor() != pawn) return;
APlayerCameraManager *cam = pc->PlayerCameraManager;
if (cam == nullptr) return;
CalculateLookAt(pc);
if (MustCallLookAtChanged) {
MustCallLookAtChanged = false;
LookAtChanged();
}
}