2023-06-08 17:10:14 -04:00
|
|
|
// Copyright Epic Games, Inc. All Rights Reserved.
|
|
|
|
|
|
2025-03-17 18:04:55 -04:00
|
|
|
#include "LuprexGameModeBase.h"
|
2026-02-27 17:00:46 -05:00
|
|
|
#include "LockedWrapper.h"
|
2023-09-07 03:57:29 -04:00
|
|
|
#include "lpx-drvutil.hpp"
|
2026-02-25 17:22:26 -05:00
|
|
|
#include "Misc/Paths.h"
|
2023-09-26 17:00:30 -04:00
|
|
|
#include "Tangible.h"
|
2023-09-02 01:33:11 -04:00
|
|
|
#include "TangibleManager.h"
|
2025-04-08 18:53:26 -04:00
|
|
|
#include "LuaCall.h"
|
2025-04-02 23:22:16 -04:00
|
|
|
#include "Blueprint/UserWidget.h"
|
2025-04-15 15:48:33 -04:00
|
|
|
#include "Blueprint/WidgetBlueprintLibrary.h"
|
2025-04-07 16:48:27 -04:00
|
|
|
#include "Kismet/GameplayStatics.h"
|
2026-02-27 13:31:06 -05:00
|
|
|
#include "Engine/GameInstance.h"
|
2025-04-02 23:22:16 -04:00
|
|
|
|
2026-02-09 13:54:00 -05:00
|
|
|
#include "Common.h"
|
2023-09-11 03:44:57 -04:00
|
|
|
#include "AnimQueue.h"
|
2023-06-08 17:10:14 -04:00
|
|
|
#include <string>
|
|
|
|
|
#include <string_view>
|
|
|
|
|
|
2026-02-09 13:54:00 -05:00
|
|
|
using namespace LpxCommonTypes;
|
2025-03-28 23:31:44 -04:00
|
|
|
|
2026-02-27 17:49:29 -05:00
|
|
|
ALuprexGameModeBase::ALuprexGameModeBase() {}
|
|
|
|
|
ALuprexGameModeBase::~ALuprexGameModeBase() {}
|
2023-06-08 17:10:14 -04:00
|
|
|
|
2023-06-22 15:17:49 -04:00
|
|
|
|
2025-03-17 18:04:55 -04:00
|
|
|
void ALuprexGameModeBase::ResetToInitialState()
|
2023-06-09 16:47:46 -04:00
|
|
|
{
|
2023-09-06 23:25:37 -04:00
|
|
|
Playing = false;
|
2025-08-27 20:57:12 -04:00
|
|
|
TickEnabled = true;
|
2023-09-06 23:25:37 -04:00
|
|
|
|
2026-02-27 17:49:29 -05:00
|
|
|
// Stop the tick function.
|
2025-08-27 20:57:12 -04:00
|
|
|
FWorldDelegates::OnWorldPostActorTick.Remove(OnWorldPostActorTickHandle);
|
|
|
|
|
OnWorldPostActorTickHandle.Reset();
|
|
|
|
|
|
2026-02-27 17:00:46 -05:00
|
|
|
FlxLockedWrapper w;
|
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
|
|
|
}
|
|
|
|
|
|
2026-02-27 17:00:46 -05:00
|
|
|
// Clear the lua call assembly buffer.
|
|
|
|
|
UlxEngineWrapper::GetLuaCallBuffer().clear();
|
|
|
|
|
|
2024-11-22 23:01:05 -05:00
|
|
|
// Stop trapping log errors to the debugger.
|
|
|
|
|
BreakToDebuggerLogVerbosityDevice.Reset();
|
|
|
|
|
|
2024-09-11 16:07:47 -04:00
|
|
|
// Clear the PlayerID
|
|
|
|
|
PlayerId = 0;
|
|
|
|
|
|
2024-09-24 18:02:33 -04:00
|
|
|
// Clear the look-at state;
|
2025-01-14 18:37:31 -05:00
|
|
|
CurrentLookAt.Init();
|
2025-04-15 15:48:33 -04:00
|
|
|
MustCallLookAtChanged = false;
|
2024-09-11 16:07:47 -04:00
|
|
|
|
2023-06-23 16:27:23 -04:00
|
|
|
// Reset the clocks.
|
2024-09-11 16:07:47 -04:00
|
|
|
EngineSeconds = 0.0;
|
2023-06-23 16:27:23 -04:00
|
|
|
}
|
|
|
|
|
|
2025-03-17 18:04:55 -04:00
|
|
|
void ALuprexGameModeBase::UpdateConsoleOutput() {
|
2023-09-05 03:20:11 -04:00
|
|
|
// Copy Luprex Stdout into the console.
|
2026-02-27 17:00:46 -05:00
|
|
|
FlxLockedWrapper lockedwrap;
|
2023-09-06 23:25:37 -04:00
|
|
|
if (Playing) {
|
2026-02-25 17:07:36 -05:00
|
|
|
FString Text = lockedwrap.ChannelPrints();
|
2025-12-09 02:42:13 -05:00
|
|
|
if (!Text.IsEmpty())
|
|
|
|
|
{
|
|
|
|
|
ConsoleAddOutput(Text);
|
|
|
|
|
}
|
2023-06-08 17:10:14 -04:00
|
|
|
}
|
2023-09-05 03:20:11 -04:00
|
|
|
}
|
|
|
|
|
|
2023-09-25 18:00:34 -04:00
|
|
|
#pragma optimize("", off)
|
2025-03-17 18:04:55 -04:00
|
|
|
void ALuprexGameModeBase::UpdateTangibles() {
|
2023-09-28 14:32:48 -04:00
|
|
|
double radius = 1000.0; // Hardwired for now.
|
2023-09-26 19:26:09 -04:00
|
|
|
using TanArray = UlxTangibleManager::TanArray;
|
2023-09-06 23:25:37 -04:00
|
|
|
if (!Playing) return;
|
2026-02-25 14:59:54 -05:00
|
|
|
UlxTangibleManager *TM = GetGameInstance()->GetSubsystem<UlxTangibleManager>();
|
2024-02-13 16:49:58 -05:00
|
|
|
TanArray alltans;
|
|
|
|
|
{
|
2026-02-27 17:00:46 -05:00
|
|
|
FlxLockedWrapper w;
|
2024-02-13 16:49:58 -05:00
|
|
|
PlayerId = w.GetActor();
|
|
|
|
|
IdView nearids = w.GetNear(PlayerId, radius, radius, radius);
|
2026-02-25 14:59:54 -05:00
|
|
|
TM->UpdateNearAccordingToLuprex(nearids);
|
|
|
|
|
alltans = TM->GetAllTangibles();
|
|
|
|
|
IdArray allids = TM->GetIds(alltans);
|
2024-02-13 16:49:58 -05:00
|
|
|
StringViewVec allqueues = w.GetAnimationQueues(allids);
|
|
|
|
|
for (int i = 0; i < alltans.Num(); i++) {
|
|
|
|
|
alltans[i]->UpdateAnimationQueue(allqueues[i]);
|
|
|
|
|
}
|
2025-07-02 16:01:18 -04:00
|
|
|
// 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()));
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-02-13 16:49:58 -05:00
|
|
|
}
|
|
|
|
|
// 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++) {
|
2024-02-13 16:49:58 -05:00
|
|
|
alltans[i]->MaybeExecuteAnimStateChanged();
|
2023-09-12 15:11:47 -04:00
|
|
|
}
|
2026-02-25 14:59:54 -05:00
|
|
|
TM->RecalcNearAccordingToUnreal(PlayerId, radius);
|
|
|
|
|
TM->DeleteFarawayTangibles();
|
2023-09-05 03:20:11 -04:00
|
|
|
}
|
|
|
|
|
|
2025-03-17 18:04:55 -04:00
|
|
|
void ALuprexGameModeBase::UpdatePossessedTangible() {
|
2026-02-25 14:59:54 -05:00
|
|
|
UlxTangibleManager *TM = GetGameInstance()->GetSubsystem<UlxTangibleManager>();
|
|
|
|
|
UlxTangible *ptan = TM->GetPossessedTangible();
|
|
|
|
|
UlxTangible *tan = TM->GetTangible(PlayerId);
|
2024-09-24 22:13:56 -04:00
|
|
|
APlayerController *ctrl = GetWorld()->GetFirstPlayerController();
|
|
|
|
|
APawn *pawn = nullptr;
|
|
|
|
|
if (tan != nullptr) pawn = Cast<APawn>(tan->GetActor());
|
|
|
|
|
if (pawn == nullptr) {
|
2024-09-11 16:07:47 -04:00
|
|
|
if (ptan != nullptr) {
|
2026-02-25 14:59:54 -05:00
|
|
|
TM->SetPossessedTangible(nullptr);
|
2026-03-02 17:17:23 -05:00
|
|
|
ctrl->UnPossess();
|
2024-09-11 16:07:47 -04:00
|
|
|
}
|
2024-09-24 22:13:56 -04:00
|
|
|
} else {
|
|
|
|
|
if (ptan != tan) {
|
2026-02-25 14:59:54 -05:00
|
|
|
TM->SetPossessedTangible(tan);
|
2024-09-24 22:13:56 -04:00
|
|
|
ctrl->Possess(pawn);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-09-11 16:07:47 -04:00
|
|
|
}
|
|
|
|
|
|
2026-02-27 17:49:29 -05:00
|
|
|
void ALuprexGameModeBase::UpdateLuaSourceCode() {
|
|
|
|
|
FlxLockedWrapper lockedwrap;
|
|
|
|
|
if (lockedwrap->get_rescan_lua_source(lockedwrap.Get()))
|
|
|
|
|
{
|
|
|
|
|
drvutil::ostringstream srcpak;
|
|
|
|
|
FString LuprexRoot = FPaths::Combine(FPaths::ProjectDir(), TEXT("luprex"));
|
|
|
|
|
std::string srcpakerr = drvutil::package_lua_source(TCHAR_TO_UTF8(*LuprexRoot), &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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ALuprexGameModeBase::UpdateSocketsAndLua()
|
|
|
|
|
{
|
|
|
|
|
FlxLockedWrapper lockedwrap;
|
|
|
|
|
Sockets->Update(lockedwrap);
|
|
|
|
|
lockedwrap->play_update(lockedwrap.Get(), EngineSeconds);
|
|
|
|
|
Sockets->Update(lockedwrap);
|
|
|
|
|
}
|
2025-12-09 15:51:35 -05:00
|
|
|
|
2026-02-27 17:49:29 -05:00
|
|
|
void ALuprexGameModeBase::OnWorldPostActorTick(UWorld* InWorld, ELevelTick InLevelTick, float deltaseconds)
|
2023-09-06 23:25:37 -04:00
|
|
|
{
|
2025-08-27 20:57:12 -04:00
|
|
|
if(Playing && TickEnabled && (GetWorld() == InWorld) && (InLevelTick == LEVELTICK_All))
|
2023-09-12 15:11:47 -04:00
|
|
|
{
|
|
|
|
|
EngineSeconds += deltaseconds;
|
2026-02-27 17:49:29 -05:00
|
|
|
UpdateLuaSourceCode();
|
|
|
|
|
UpdateSocketsAndLua();
|
2024-02-16 15:48:22 -05:00
|
|
|
UpdateConsoleOutput();
|
|
|
|
|
UpdateTangibles();
|
2024-09-11 16:07:47 -04:00
|
|
|
UpdatePossessedTangible();
|
2024-09-24 18:02:33 -04:00
|
|
|
UpdateLookAt();
|
2023-09-12 15:11:47 -04:00
|
|
|
}
|
2024-02-13 16:49:58 -05:00
|
|
|
}
|
2023-09-12 15:11:47 -04:00
|
|
|
|
2025-03-17 18:04:55 -04:00
|
|
|
void ALuprexGameModeBase::BeginPlay()
|
2023-06-09 16:47:46 -04:00
|
|
|
{
|
2023-09-03 03:40:44 -04:00
|
|
|
ResetToInitialState();
|
2025-03-28 23:31:44 -04:00
|
|
|
InitializeGlobalState();
|
|
|
|
|
Super::BeginPlay();
|
|
|
|
|
}
|
2023-09-03 03:40:44 -04:00
|
|
|
|
2025-03-28 23:31:44 -04:00
|
|
|
void ALuprexGameModeBase::InitializeGlobalState()
|
|
|
|
|
{
|
2026-02-27 17:00:46 -05:00
|
|
|
FlxLockedWrapper w;
|
2023-09-03 02:01:32 -04:00
|
|
|
|
2023-09-03 03:40:44 -04:00
|
|
|
// Sanity checks. Make sure everything is clean.
|
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
|
|
|
|
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
|
|
|
{
|
2025-03-28 23:31:44 -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
|
|
|
{
|
2025-06-16 19:58:26 -04:00
|
|
|
w->play_initialize(w.Get(), "lpxclient", "");
|
|
|
|
|
if (w->error[0])
|
2023-06-23 12:45:23 -04:00
|
|
|
{
|
2025-06-16 19:58:26 -04:00
|
|
|
FString FMessage((const UTF8CHAR *)w->error);
|
|
|
|
|
UE_LOG(LogLuprexIntegration, Error, TEXT("Calling Luprex play_initialize: %s"), *FMessage);
|
2023-06-23 12:45:23 -04:00
|
|
|
}
|
2025-06-16 19:58:26 -04:00
|
|
|
if (w->engine != nullptr) {
|
|
|
|
|
UE_LOG(LogLuprexIntegration, Verbose, TEXT("Luprex initialization success."));
|
|
|
|
|
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
|
|
|
|
2025-06-19 18:01:05 -04:00
|
|
|
// 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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-27 17:49:29 -05:00
|
|
|
// If we successfully created a luprex engine, create a socket system.
|
2023-09-06 23:25:37 -04:00
|
|
|
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());
|
2023-06-08 17:10:14 -04:00
|
|
|
}
|
2023-08-29 20:05:10 -04:00
|
|
|
|
2025-08-27 20:57:12 -04:00
|
|
|
if (Playing) {
|
|
|
|
|
OnWorldPostActorTickHandle = FWorldDelegates::OnWorldPostActorTick.AddUObject(this, &ALuprexGameModeBase::OnWorldPostActorTick);
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-22 23:01:05 -05:00
|
|
|
// If somebody generates a log message that's severe enough, break to debugger.
|
|
|
|
|
BreakToDebuggerLogVerbosityDevice.Reset(
|
2026-02-14 01:25:04 -05:00
|
|
|
new FlxBreakToDebuggerOutputDevice(BreakToDebuggerLogVerbosity));
|
2023-06-08 17:10:14 -04:00
|
|
|
}
|
|
|
|
|
|
2025-03-17 18:04:55 -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();
|
2025-09-09 17:14:14 -04:00
|
|
|
Super::EndPlay(EndPlayReason);
|
2023-06-09 16:47:46 -04:00
|
|
|
}
|
|
|
|
|
|
2025-03-17 18:04:55 -04:00
|
|
|
int64 ALuprexGameModeBase::GetPlayerId() {
|
2024-02-02 15:48:27 -05:00
|
|
|
return PlayerId;
|
|
|
|
|
}
|
2024-08-31 16:42:07 -04:00
|
|
|
|
2025-04-01 22:31:27 -04:00
|
|
|
ALuprexGameModeBase *ALuprexGameModeBase::FromContext(const UObject *context) {
|
2025-03-17 18:04:55 -04:00
|
|
|
ALuprexGameModeBase *result = context->GetWorld()->GetAuthGameMode<ALuprexGameModeBase>();
|
2024-08-31 16:42:07 -04:00
|
|
|
if (result == nullptr) {
|
2025-03-17 15:35:51 -04:00
|
|
|
UE_LOG(LogBlueprint, Fatal, TEXT("Not currently using a Luprex Game Mode."));
|
2024-08-31 16:42:07 -04:00
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-15 15:48:33 -04:00
|
|
|
|
2025-04-07 16:48:27 -04:00
|
|
|
void ALuprexGameModeBase::SetLookAt(const UObject *Context, const FHitResult &HitResult)
|
|
|
|
|
{
|
|
|
|
|
ALuprexGameModeBase *Mode = FromContext(Context);
|
2025-04-15 15:48:33 -04:00
|
|
|
if (Mode->CurrentLookAt.HitObjectHandle != HitResult.HitObjectHandle)
|
|
|
|
|
{
|
|
|
|
|
Mode->MustCallLookAtChanged = true;
|
|
|
|
|
}
|
2025-04-07 16:48:27 -04:00
|
|
|
Mode->CurrentLookAt = HitResult;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-27 18:21:00 -04:00
|
|
|
void ALuprexGameModeBase::SetLookAtChanged(const UObject *Context)
|
|
|
|
|
{
|
|
|
|
|
ALuprexGameModeBase *Mode = FromContext(Context);
|
|
|
|
|
Mode->MustCallLookAtChanged = true;
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-15 15:48:33 -04:00
|
|
|
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;
|
2025-04-15 15:48:33 -04:00
|
|
|
if (!UGameplayStatics::ProjectWorldToScreen(pc, Mode->CurrentLookAt.Location, ScreenPosition, false))
|
|
|
|
|
{
|
|
|
|
|
return FVector2D();
|
|
|
|
|
}
|
2025-04-07 16:48:27 -04:00
|
|
|
return ScreenPosition;
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-17 18:04:55 -04:00
|
|
|
void ALuprexGameModeBase::UpdateLookAt() {
|
2024-09-24 18:02:33 -04:00
|
|
|
// Make sure the world is fully configured before we attempt to cast rays.
|
2026-02-25 14:59:54 -05:00
|
|
|
UlxTangible *possessed = GetGameInstance()->GetSubsystem<UlxTangibleManager>()->GetPossessedTangible();
|
2024-09-24 18:02:33 -04:00
|
|
|
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;
|
|
|
|
|
|
2025-03-17 15:35:51 -04:00
|
|
|
CalculateLookAt(pc);
|
|
|
|
|
|
2025-04-15 15:48:33 -04:00
|
|
|
if (MustCallLookAtChanged) {
|
|
|
|
|
MustCallLookAtChanged = false;
|
2025-03-17 15:35:51 -04:00
|
|
|
LookAtChanged();
|
|
|
|
|
}
|
2024-09-17 17:22:47 -04:00
|
|
|
}
|
|
|
|
|
|