2023-06-08 17:10:14 -04:00
|
|
|
// Copyright Epic Games, Inc. All Rights Reserved.
|
|
|
|
|
|
2023-06-09 14:36:47 -04:00
|
|
|
#include "IntegrationGameModeBase.h"
|
2023-09-07 03:57:29 -04:00
|
|
|
#include "lpx-drvutil.hpp"
|
2023-10-16 16:54:41 -04:00
|
|
|
#include "lpx-paths.hpp"
|
2023-09-04 03:27:31 -04:00
|
|
|
#include "DebugPrint.h"
|
2023-09-26 17:00:30 -04:00
|
|
|
#include "Tangible.h"
|
2023-09-02 01:33:11 -04:00
|
|
|
#include "TangibleManager.h"
|
2023-09-11 03:44:57 -04:00
|
|
|
#include "CommonTypes.h"
|
|
|
|
|
#include "AnimQueue.h"
|
2023-06-08 17:10:14 -04:00
|
|
|
#include <string>
|
|
|
|
|
#include <string_view>
|
|
|
|
|
|
2023-09-04 03:21:23 -04:00
|
|
|
using namespace DebugPrint;
|
2023-09-11 03:44:57 -04:00
|
|
|
using namespace CommonTypes;
|
2023-09-04 03:21:23 -04:00
|
|
|
|
2023-06-09 16:47:46 -04:00
|
|
|
AIntegrationGameModeBase::AIntegrationGameModeBase()
|
|
|
|
|
{
|
2023-09-26 17:00:30 -04:00
|
|
|
TangibleManager = NewObject<UlxTangibleManager>();
|
2024-02-02 15:48:27 -05:00
|
|
|
PlayerId = 0;
|
2023-06-23 12:45:23 -04:00
|
|
|
EngineSeconds = 0.0;
|
2023-06-08 17:10:14 -04:00
|
|
|
//PrimaryActorTick.bCanEverTick = true; // Probably wrong
|
|
|
|
|
//PrimaryActorTick.bTickEvenWhenPaused = true; // Probably wrong
|
|
|
|
|
//PrimaryActorTick.TickGroup = TG_PrePhysics; // Probably wrong
|
2023-06-22 15:17:49 -04:00
|
|
|
SetActorTickEnabled(true);
|
2023-07-03 15:28:14 -04:00
|
|
|
SetActorTickInterval(0.0f);
|
2023-09-04 03:21:23 -04:00
|
|
|
DebugPrintControl::EnableCollection();
|
2023-09-06 23:25:37 -04:00
|
|
|
ResetToInitialState();
|
2024-02-13 16:49:58 -05:00
|
|
|
OnWorldPreActorTickHandle = FWorldDelegates::OnWorldPreActorTick.AddUObject(this, &AIntegrationGameModeBase::OnWorldPreActorTick);
|
|
|
|
|
OnWorldPostActorTickHandle = FWorldDelegates::OnWorldPostActorTick.AddUObject(this, &AIntegrationGameModeBase::OnWorldPostActorTick);
|
2023-06-22 15:17:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AIntegrationGameModeBase::~AIntegrationGameModeBase()
|
|
|
|
|
{
|
2023-06-23 16:27:23 -04:00
|
|
|
ResetToInitialState();
|
2024-02-13 16:49:58 -05:00
|
|
|
FWorldDelegates::OnWorldPreActorTick.Remove(OnWorldPreActorTickHandle);
|
|
|
|
|
FWorldDelegates::OnWorldPostActorTick.Remove(OnWorldPostActorTickHandle);
|
2023-06-08 17:10:14 -04:00
|
|
|
}
|
|
|
|
|
|
2023-09-04 23:19:10 -04:00
|
|
|
// This method runs in the background thread,
|
|
|
|
|
// at the moment we trigger it.
|
|
|
|
|
//
|
|
|
|
|
uint32 AIntegrationGameModeBase::Run() {
|
2023-09-15 13:28:18 -04:00
|
|
|
FlxLockedWrapper lockedwrap(LockableWrapper);
|
2023-09-04 23:19:10 -04:00
|
|
|
Sockets->Update(lockedwrap);
|
|
|
|
|
lockedwrap->play_invoke_event_update(lockedwrap.Get(), EngineSeconds);
|
|
|
|
|
Sockets->Update(lockedwrap);
|
2023-06-22 15:17:49 -04:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-23 16:27:23 -04:00
|
|
|
void AIntegrationGameModeBase::ResetToInitialState()
|
2023-06-09 16:47:46 -04:00
|
|
|
{
|
2023-09-06 23:25:37 -04:00
|
|
|
Playing = false;
|
|
|
|
|
|
2023-09-25 14:25:24 -04:00
|
|
|
if (TangibleManager != nullptr) {
|
|
|
|
|
TangibleManager->ConditionalBeginDestroy();
|
|
|
|
|
TangibleManager = nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-04 23:19:10 -04:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Reset the clocks.
|
|
|
|
|
EngineSeconds = 0;
|
2023-09-12 15:11:47 -04:00
|
|
|
NextRotateCube = 1.0;
|
2023-06-23 16:27:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-09-05 03:20:11 -04:00
|
|
|
void AIntegrationGameModeBase::UpdateConsoleOutput() {
|
|
|
|
|
// Copy Luprex Stdout into the console.
|
2023-09-15 13:28:18 -04:00
|
|
|
FlxLockedWrapper lockedwrap(LockableWrapper);
|
2023-09-06 23:25:37 -04:00
|
|
|
if (Playing) {
|
|
|
|
|
ConsoleOutput.Append(lockedwrap.FetchStdout());
|
|
|
|
|
}
|
2023-06-09 16:47:46 -04:00
|
|
|
|
2023-09-05 03:20:11 -04:00
|
|
|
// Copy Debugging Prints into the console.
|
2023-09-04 03:21:23 -04:00
|
|
|
TArray<FString> prints = DebugPrintControl::GetStored();
|
2023-06-22 15:17:49 -04:00
|
|
|
for (const FString& fs : prints) {
|
2023-06-08 17:10:14 -04:00
|
|
|
ConsoleOutput.AppendLine(fs);
|
|
|
|
|
}
|
2023-09-05 03:20:11 -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();
|
|
|
|
|
}
|
2023-09-05 03:20:11 -04:00
|
|
|
}
|
|
|
|
|
|
2023-09-25 18:00:34 -04:00
|
|
|
#pragma optimize("", off)
|
2023-09-06 23:25:37 -04:00
|
|
|
void AIntegrationGameModeBase::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;
|
2024-02-13 16:49:58 -05:00
|
|
|
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]);
|
|
|
|
|
}
|
2024-03-25 14:50:06 -04:00
|
|
|
|
|
|
|
|
// Maybe call 'BecomePossessed' on some tangible.
|
|
|
|
|
UlxTangible *poss = TangibleManager->SetPossessedTangible(PlayerId);
|
|
|
|
|
if (poss != nullptr) {
|
|
|
|
|
IlxTangibleInterface::Execute_BecomePossessed(poss->GetActor());
|
|
|
|
|
}
|
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
|
|
|
}
|
2024-02-02 15:48:27 -05:00
|
|
|
TangibleManager->RecalcNearAccordingToUnreal(PlayerId, radius);
|
2023-09-28 14:32:48 -04:00
|
|
|
TangibleManager->DeleteFarawayTangibles();
|
2023-09-05 03:20:11 -04:00
|
|
|
}
|
|
|
|
|
|
2024-02-16 15:48:22 -05:00
|
|
|
void AIntegrationGameModeBase::InvokeEngioMove(const FString &action, const FVector &xyz, double facing) {
|
2024-02-13 16:49:58 -05:00
|
|
|
FTCHARToUTF8 utf8action(action);
|
|
|
|
|
std::string uaction(utf8action.Get(), utf8action.Length());
|
|
|
|
|
FlxStreamBuffer sb;
|
|
|
|
|
sb.write_string("move"); // Function name within engio
|
|
|
|
|
sb.write_simple_dynamic_tag(SimpleDynamicTag::STRING);
|
|
|
|
|
sb.write_string(uaction);
|
|
|
|
|
sb.write_simple_dynamic_tag(SimpleDynamicTag::VECTOR);
|
|
|
|
|
sb.write_fvector(xyz);
|
2024-02-16 15:48:22 -05:00
|
|
|
sb.write_simple_dynamic_tag(SimpleDynamicTag::NUMBER);
|
|
|
|
|
sb.write_double(facing);
|
2024-02-13 16:49:58 -05:00
|
|
|
std::string_view datapk = sb.view();
|
|
|
|
|
FlxLockedWrapper w(LockableWrapper);
|
|
|
|
|
int64 player = w.GetActor();
|
|
|
|
|
w->play_invoke_engio(w.Get(), player, datapk.size(), datapk.data());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AIntegrationGameModeBase::ExecuteDebuggingCommand(FlxLockedWrapper &w, const FString &fs) {
|
2023-10-24 01:44:09 -04:00
|
|
|
if (fs == "\\invokeplayer") {
|
2023-10-24 17:12:03 -04:00
|
|
|
DPrint(TEXT("Trying to invoke 'myfunction' in lua"));
|
|
|
|
|
FlxStreamBuffer sb;
|
|
|
|
|
sb.write_string("myfunction");
|
|
|
|
|
sb.write_simple_dynamic_tag(SimpleDynamicTag::NUMBER);
|
|
|
|
|
sb.write_double(3.0);
|
|
|
|
|
sb.write_simple_dynamic_tag(SimpleDynamicTag::STRING);
|
|
|
|
|
sb.write_string("Howdy");
|
2023-10-31 13:33:00 -04:00
|
|
|
sb.write_simple_dynamic_tag(SimpleDynamicTag::VECTOR);
|
|
|
|
|
sb.write_fvector(FVector(2,3,4));
|
2023-10-24 17:12:03 -04:00
|
|
|
std::string_view datapk = sb.view();
|
|
|
|
|
int64 player = w.GetActor();
|
|
|
|
|
w->play_invoke_engio(w.Get(), player, datapk.size(), datapk.data());
|
2023-10-24 01:44:09 -04:00
|
|
|
} else {
|
|
|
|
|
ConsoleOutput.AppendLine(TEXT("Unknown Command"));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-06-09 16:47:46 -04:00
|
|
|
void AIntegrationGameModeBase::ConsoleSendInput(const FString& fs)
|
|
|
|
|
{
|
2024-02-02 15:48:27 -05: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
|
|
|
{
|
2023-10-24 01:44:09 -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] == '\\') {
|
2024-02-13 16:49:58 -05:00
|
|
|
ExecuteDebuggingCommand(w, fs);
|
2023-10-24 01:44:09 -04:00
|
|
|
} else {
|
2024-02-13 16:49:58 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-13 16:49:58 -05:00
|
|
|
void AIntegrationGameModeBase::OnWorldPreActorTick(UWorld* InWorld, ELevelTick InLevelTick, float deltaseconds)
|
2023-09-06 23:25:37 -04:00
|
|
|
{
|
2024-02-13 16:49:58 -05:00
|
|
|
if(Playing && (GetWorld() == InWorld) && (InLevelTick == LEVELTICK_All))
|
2023-09-12 15:11:47 -04:00
|
|
|
{
|
2024-02-13 16:49:58 -05:00
|
|
|
LuprexUpdateTask.Wait();
|
2023-09-12 15:11:47 -04:00
|
|
|
EngineSeconds += deltaseconds;
|
2024-02-16 15:48:22 -05:00
|
|
|
UpdateConsoleOutput();
|
|
|
|
|
UpdateTangibles();
|
2023-09-12 15:11:47 -04:00
|
|
|
}
|
2024-02-13 16:49:58 -05:00
|
|
|
}
|
2023-09-12 15:11:47 -04:00
|
|
|
|
2024-02-13 16:49:58 -05:00
|
|
|
void AIntegrationGameModeBase::OnWorldPostActorTick(UWorld* InWorld, ELevelTick InLevelTick, float deltaseconds)
|
|
|
|
|
{
|
|
|
|
|
if(Playing && (GetWorld() == InWorld) && (InLevelTick == LEVELTICK_All))
|
|
|
|
|
{
|
|
|
|
|
LuprexUpdateTask.Trigger();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void AIntegrationGameModeBase::Tick(float deltaseconds)
|
|
|
|
|
{
|
|
|
|
|
Super::Tick(deltaseconds);
|
2023-09-06 23:25:37 -04:00
|
|
|
UpdateConsoleOutput();
|
|
|
|
|
UpdateTangibles();
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-09 16:47:46 -04:00
|
|
|
void AIntegrationGameModeBase::BeginPlay()
|
|
|
|
|
{
|
2023-06-08 17:10:14 -04:00
|
|
|
Super::BeginPlay();
|
2023-06-23 12:45:23 -04:00
|
|
|
|
2023-09-03 03:40:44 -04:00
|
|
|
// Make sure we're starting from a clean slate.
|
|
|
|
|
// Note: this claims the wrapper lock, so don't claim
|
|
|
|
|
// the lock before calling this.
|
|
|
|
|
ResetToInitialState();
|
|
|
|
|
|
|
|
|
|
// Now we're just going to claim the wrapper
|
|
|
|
|
// lock for the remainder. When we create the thread,
|
|
|
|
|
// the thread will hang until we release this lock.
|
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.
|
2023-09-04 23:19:10 -04:00
|
|
|
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
|
|
|
{
|
2023-09-04 03:21:23 -04:00
|
|
|
DPrint("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())
|
|
|
|
|
{
|
2023-09-04 03:21:23 -04:00
|
|
|
DPrint(srcpakerr.c_str());
|
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])
|
|
|
|
|
{
|
|
|
|
|
DPrint(w->error);
|
|
|
|
|
}
|
|
|
|
|
if (w->engine != nullptr) {
|
|
|
|
|
DPrint("Luprex initialize 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
|
|
|
|
2023-06-23 16:27:23 -04:00
|
|
|
// If we successfully created a luprex engine, create a socket system and a worker thread.
|
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-09-04 23:19:10 -04:00
|
|
|
LuprexUpdateTask.Startup(this);
|
2023-06-08 17:10:14 -04:00
|
|
|
}
|
2023-08-29 20:05:10 -04:00
|
|
|
|
2023-09-06 23:25:37 -04:00
|
|
|
// Initialize the tangible manager.
|
2023-09-26 17:00:30 -04:00
|
|
|
TangibleManager = NewObject<UlxTangibleManager>();
|
2024-02-02 15:48:27 -05:00
|
|
|
TangibleManager->Init(GetWorld(), this);
|
2023-06-08 17:10:14 -04:00
|
|
|
}
|
|
|
|
|
|
2023-06-23 12:45:23 -04:00
|
|
|
void AIntegrationGameModeBase::EndPlay(const EEndPlayReason::Type EndPlayReason)
|
|
|
|
|
{
|
2023-06-23 16:27:23 -04:00
|
|
|
ResetToInitialState();
|
2023-06-09 16:47:46 -04:00
|
|
|
}
|
|
|
|
|
|
2024-02-02 15:48:27 -05:00
|
|
|
int64 AIntegrationGameModeBase::GetPlayerId() {
|
|
|
|
|
return PlayerId;
|
|
|
|
|
}
|