Files
integration/Source/Integration/LuprexGameModeBase.cpp

406 lines
12 KiB
C++
Raw Normal View History

2023-06-08 17:10:14 -04:00
// Copyright Epic Games, Inc. All Rights Reserved.
#include "LuprexGameModeBase.h"
#include "lpx-drvutil.hpp"
2023-10-16 16:54:41 -04:00
#include "lpx-paths.hpp"
#include "ConsoleOutput.h"
#include "Tangible.h"
2023-09-02 01:33:11 -04:00
#include "TangibleManager.h"
#include "LuaCall.h"
#include "Blueprint/UserWidget.h"
#include "Blueprint/WidgetBlueprintLibrary.h"
2025-04-07 16:48:27 -04:00
#include "Kismet/GameplayStatics.h"
#include "CommonTypes.h"
#include "AnimQueue.h"
2023-06-08 17:10:14 -04:00
#include <string>
#include <string_view>
using namespace CommonTypes;
2023-09-04 03:21:23 -04:00
DEFINE_LOG_CATEGORY(LogLuprex);
DEFINE_LOG_CATEGORY(LogLuprexIntegration);
ALuprexGameModeBase::ALuprexGameModeBase()
2023-06-09 16:47:46 -04:00
{
TangibleManager = nullptr;
AssetLookup = nullptr;
PlayerId = 0;
2023-06-23 12:45:23 -04:00
EngineSeconds = 0.0;
MustCallLookAtChanged = false;
2023-06-08 17:10:14 -04:00
//PrimaryActorTick.bCanEverTick = true; // Probably wrong
//PrimaryActorTick.bTickEvenWhenPaused = true; // Probably wrong
//PrimaryActorTick.TickGroup = TG_PrePhysics; // Probably wrong
SetActorTickEnabled(true);
2023-07-03 15:28:14 -04:00
SetActorTickInterval(0.0f);
ResetToInitialState();
OnWorldPreActorTickHandle = FWorldDelegates::OnWorldPreActorTick.AddUObject(this, &ALuprexGameModeBase::OnWorldPreActorTick);
OnWorldPostActorTickHandle = FWorldDelegates::OnWorldPostActorTick.AddUObject(this, &ALuprexGameModeBase::OnWorldPostActorTick);
FString LuprexServer;
FParse::Value(FCommandLine::Get(), TEXT("-LuprexServer="), LuprexServer);
UE_LOG(LogTemp, Display, TEXT("LuprexServer = %s"), *LuprexServer)
}
ALuprexGameModeBase::~ALuprexGameModeBase()
{
2023-06-23 16:27:23 -04:00
ResetToInitialState();
FWorldDelegates::OnWorldPreActorTick.Remove(OnWorldPreActorTickHandle);
FWorldDelegates::OnWorldPostActorTick.Remove(OnWorldPostActorTickHandle);
2023-06-08 17:10:14 -04:00
}
// This method runs in the background thread,
// at the moment we trigger it.
//
uint32 ALuprexGameModeBase::Run() {
2023-09-15 13:28:18 -04:00
FlxLockedWrapper lockedwrap(LockableWrapper);
Sockets->Update(lockedwrap);
lockedwrap->play_update(lockedwrap.Get(), EngineSeconds);
Sockets->Update(lockedwrap);
return 0;
}
void ALuprexGameModeBase::ResetToInitialState()
2023-06-09 16:47:46 -04:00
{
Playing = false;
2023-09-25 14:25:24 -04:00
if (TangibleManager != nullptr) {
TangibleManager->ConditionalBeginDestroy();
TangibleManager = nullptr;
}
if (AssetLookup != nullptr) {
AssetLookup->ConditionalBeginDestroy();
AssetLookup = nullptr;
}
// Shut down the thread
LuprexUpdateTask.Shutdown();
2023-06-23 16:27:23 -04:00
2023-09-03 03:40:44 -04:00
// Now that the thread's gone, we should be able to
// just claim and hold the lock on the wrapper.
2023-09-15 13:28:18 -04:00
FlxLockedWrapper w(LockableWrapper);
2023-09-03 03:40:44 -04:00
2023-07-03 15:28:14 -04:00
// Release and close all sockets.
2023-09-03 02:01:32 -04:00
if (Sockets != nullptr)
{
Sockets->ForceCloseEverything(w);
Sockets.Reset();
}
2023-07-03 15:28:14 -04:00
2023-06-23 16:27:23 -04:00
// Delete the engine.
2023-09-03 02:01:32 -04:00
if (w->release != nullptr)
2023-06-23 16:27:23 -04:00
{
2023-09-03 02:01:32 -04:00
w->release(w.Get());
2023-06-23 16:27:23 -04:00
}
// Stop trapping log errors to the debugger.
BreakToDebuggerLogVerbosityDevice.Reset();
2024-08-31 16:42:07 -04:00
// Clear the lua call assembly buffer.
LuaCallBuffer.clear();
// Clear the PlayerID
PlayerId = 0;
// Clear the look-at state;
CurrentLookAt.Init();
MustCallLookAtChanged = false;
2023-06-23 16:27:23 -04:00
// Reset the clocks.
EngineSeconds = 0.0;
NextRotateCube = 1.0;
2023-06-23 16:27:23 -04:00
}
void ALuprexGameModeBase::UpdateConsoleOutput() {
// Copy Luprex Stdout into the console.
2023-09-15 13:28:18 -04:00
FlxLockedWrapper lockedwrap(LockableWrapper);
if (Playing) {
ConsoleOutput.Append(lockedwrap.FetchStdout());
}
2023-06-09 16:47:46 -04:00
// If the Console text has changed, update the widget.
if (ConsoleOutput.IsDirty()) {
2023-06-08 17:10:14 -04:00
ConsoleSetOutput(ConsoleOutput.Get());
ConsoleOutput.ClearDirty();
}
}
#pragma optimize("", off)
void ALuprexGameModeBase::UpdateTangibles() {
double radius = 1000.0; // Hardwired for now.
2023-09-26 19:26:09 -04:00
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]);
}
}
// 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.
2023-09-26 19:26:09 -04:00
for (int i = 0; i < alltans.Num(); i++) {
alltans[i]->MaybeExecuteAnimStateChanged();
}
TangibleManager->RecalcNearAccordingToUnreal(PlayerId, radius);
TangibleManager->DeleteFarawayTangibles();
}
void ALuprexGameModeBase::UpdatePossessedTangible() {
2024-09-24 22:13:56 -04:00
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) {
2024-09-24 22:13:56 -04:00
TangibleManager->SetPossessedTangible(nullptr);
ctrl->Possess(nullptr);
}
2024-09-24 22:13:56 -04:00
} else {
if (ptan != tan) {
TangibleManager->SetPossessedTangible(tan);
ctrl->Possess(pawn);
}
}
}
UlxLuaValues *ALuprexGameModeBase::LuaCallEnd(InvocationKind kind, int64 place_id) {
2024-08-31 16:42:07 -04:00
std::string_view datapk = LuaCallBuffer.view();
FlxLockedWrapper w(LockableWrapper);
2024-09-04 23:15:13 -04:00
if (place_id == 0) place_id = w.GetActor();
uint32_t retpklen;
const char *retpk;
w->play_call_function(w.Get(), kind, place_id, datapk.size(), datapk.data(), &retpklen, &retpk);
if (kind == InvocationKind::LUA_PROBE)
{
UlxLuaValues *Result = NewObject<UlxLuaValues>(this);
Result->Initialize(std::string_view(retpk, retpklen));
return Result;
}
else return nullptr;
2024-09-04 23:15:13 -04:00
}
UlxLuaValues *ALuprexGameModeBase::LuaCallEnd(InvocationKind kind) {
return LuaCallEnd(kind, int64(0));
2024-09-04 23:15:13 -04:00
}
UlxLuaValues *ALuprexGameModeBase::LuaCallEnd(InvocationKind kind, AActor *place) {
2024-09-04 23:15:13 -04:00
if (place == nullptr) {
return LuaCallEnd(kind, int64(0));
2024-09-04 23:15:13 -04:00
} else {
UlxTangible *tan = UlxTangible::GetActorTangibleOrLog(place);
if (tan == nullptr) {
return NewObject<UlxLuaValues>(this);
} else {
return LuaCallEnd(kind, tan->TangibleId);
}
}
}
void ALuprexGameModeBase::ExecuteDebuggingCommand(FlxLockedWrapper &w, const FString &fs) {
// Nothing here right now.
}
void ALuprexGameModeBase::ConsoleSendInput(const FString& fs)
2023-06-09 16:47:46 -04:00
{
if (fs.IsEmpty()) {
return;
}
2023-09-15 13:28:18 -04:00
FlxLockedWrapper w(LockableWrapper);
2023-09-03 02:01:32 -04:00
if (w->engine != nullptr)
2023-06-23 12:45:23 -04:00
{
ConsoleOutput.AppendLine(FString("> ") + fs);
// This is a bad way to do this. The problem is that if some
// lua code contains '\\', we'll catch it instead of passing it
// through. There's no simple solution, though.
if (fs[0] == '\\') {
ExecuteDebuggingCommand(w, fs);
} else {
FTCHARToUTF8 utf8fs(fs);
std::string ufs(utf8fs.Get(), utf8fs.Length());
ufs = ufs + "\n";
w->play_recv_incoming(w.Get(), 0, ufs.size(), ufs.c_str());
2023-06-08 17:10:14 -04:00
}
}
}
void ALuprexGameModeBase::OnWorldPreActorTick(UWorld* InWorld, ELevelTick InLevelTick, float deltaseconds)
{
if(Playing && (GetWorld() == InWorld) && (InLevelTick == LEVELTICK_All))
{
LuprexUpdateTask.Wait();
EngineSeconds += deltaseconds;
2024-02-16 15:48:22 -05:00
UpdateConsoleOutput();
UpdateTangibles();
UpdatePossessedTangible();
UpdateLookAt();
}
}
void ALuprexGameModeBase::OnWorldPostActorTick(UWorld* InWorld, ELevelTick InLevelTick, float deltaseconds)
{
if(Playing && (GetWorld() == InWorld) && (InLevelTick == LEVELTICK_All))
{
LuprexUpdateTask.Trigger();
}
}
void ALuprexGameModeBase::Tick(float deltaseconds)
{
Super::Tick(deltaseconds);
}
void ALuprexGameModeBase::BeginPlay()
2023-06-09 16:47:46 -04:00
{
2023-09-03 03:40:44 -04:00
ResetToInitialState();
InitializeGlobalState();
Super::BeginPlay();
}
2023-09-03 03:40:44 -04:00
void ALuprexGameModeBase::InitializeGlobalState()
{
2023-09-15 13:28:18 -04:00
FlxLockedWrapper w(LockableWrapper);
2023-09-03 02:01:32 -04:00
2023-09-03 03:40:44 -04:00
// Sanity checks. Make sure everything is clean.
checkf(!LuprexUpdateTask.IsRunning(), TEXT("There should be no thread here."));
2023-09-03 02:01:32 -04:00
checkf(w->engine == nullptr, TEXT("There should be no engine here."));
2023-06-23 12:45:23 -04:00
// Try to initialize the wrapper.
2023-09-03 02:01:32 -04:00
w.InitWrapper();
2023-06-23 16:27:23 -04:00
// If we failed to initialize the wrapper, print an error message.
2023-09-03 02:01:32 -04:00
if (w->play_initialize == nullptr)
2023-06-23 16:27:23 -04:00
{
UE_LOG(LogLuprexIntegration, Error, TEXT("Luprex wrapper initialization failed"));
2023-06-08 17:10:14 -04:00
}
2023-06-23 12:45:23 -04:00
// If wrapper is initialized, try to initialize the luprex engine.
2023-09-03 02:01:32 -04:00
if (w->play_initialize != nullptr)
2023-06-23 12:45:23 -04:00
{
drvutil::ostringstream srcpak;
2023-10-16 16:54:41 -04:00
std::string srcpakerr = drvutil::package_lua_source(LUPREX_ROOT_PATH, &srcpak);
2023-06-23 12:45:23 -04:00
if (!srcpakerr.empty())
{
FString FMessage((const UTF8CHAR *)(srcpakerr.c_str()));
UE_LOG(LogLuprexIntegration, Error, TEXT("Trying to read Lua source: %s"), *FMessage);
2023-06-23 12:45:23 -04:00
}
2023-10-16 16:54:41 -04:00
else
2023-06-23 12:45:23 -04:00
{
2023-10-16 16:54:41 -04:00
std::string_view srcpakv = srcpak.view();
char* argv[1];
2024-03-05 14:47:16 -05:00
argv[0] = const_cast<char*>("lpxclient");
2023-10-16 16:54:41 -04:00
w->play_initialize(w.Get(), 1, argv, srcpakv.size(), srcpakv.data(), "");
if (w->error[0])
{
FString FMessage((const UTF8CHAR *)w->error);
UE_LOG(LogLuprexIntegration, Error, TEXT("Calling Luprex play_initialize: %s"), *FMessage);
2023-10-16 16:54:41 -04:00
}
if (w->engine != nullptr) {
UE_LOG(LogLuprexIntegration, Verbose, TEXT("Luprex initialization success."));
2023-10-16 16:54:41 -04:00
Playing = true;
}
2023-06-23 12:45:23 -04:00
}
2023-06-08 17:10:14 -04:00
}
2023-06-23 12:45:23 -04:00
2023-06-23 16:27:23 -04:00
// If we successfully created a luprex engine, create a socket system and a worker thread.
if (Playing) {
2023-09-15 13:28:18 -04:00
Sockets.Reset(FlxSockets::Create(w));
2023-07-03 15:28:14 -04:00
std::string error = Sockets->GetError();
check(error.empty());
LuprexUpdateTask.Startup(this);
2023-06-08 17:10:14 -04:00
}
2023-08-29 20:05:10 -04:00
// 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 FlxDebugBlueprintErrorsOutputDevice(BreakToDebuggerLogVerbosity));
2023-06-08 17:10:14 -04:00
}
void ALuprexGameModeBase::EndPlay(const EEndPlayReason::Type EndPlayReason)
2023-06-23 12:45:23 -04:00
{
2023-06-23 16:27:23 -04:00
ResetToInitialState();
2023-06-09 16:47:46 -04:00
}
int64 ALuprexGameModeBase::GetPlayerId() {
return PlayerId;
}
2024-08-31 16:42:07 -04:00
ALuprexGameModeBase *ALuprexGameModeBase::FromContext(const UObject *context) {
ALuprexGameModeBase *result = context->GetWorld()->GetAuthGameMode<ALuprexGameModeBase>();
2024-08-31 16:42:07 -04:00
if (result == nullptr) {
UE_LOG(LogBlueprint, Fatal, TEXT("Not currently using a Luprex Game Mode."));
2024-08-31 16:42:07 -04:00
}
return result;
}
2025-04-07 16:48:27 -04:00
void ALuprexGameModeBase::SetLookAt(const UObject *Context, const FHitResult &HitResult)
{
ALuprexGameModeBase *Mode = FromContext(Context);
if (Mode->CurrentLookAt.HitObjectHandle != HitResult.HitObjectHandle)
{
Mode->MustCallLookAtChanged = true;
}
2025-04-07 16:48:27 -04:00
Mode->CurrentLookAt = HitResult;
}
void ALuprexGameModeBase::SetLookAtChanged(const UObject *Context)
{
ALuprexGameModeBase *Mode = FromContext(Context);
Mode->MustCallLookAtChanged = true;
}
FVector2D ALuprexGameModeBase::GetLookAtPixel(const UObject *Context)
2025-04-07 16:48:27 -04:00
{
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();
}
2025-04-07 16:48:27 -04:00
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();
}
2024-09-17 17:22:47 -04:00
}