Skip to content

Commit

Permalink
feat: infrastructure for teleport button
Browse files Browse the repository at this point in the history
  • Loading branch information
RobbeBryssinck committed May 16, 2022
1 parent 01dc637 commit 297d028
Show file tree
Hide file tree
Showing 13 changed files with 172 additions and 5 deletions.
11 changes: 11 additions & 0 deletions Code/client/Services/Generic/OverlayClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <Events/CommandEvent.h>

#include <Messages/SendChatMessageRequest.h>
#include <Messages/TeleportRequest.h>

#include <World.h>

Expand Down Expand Up @@ -69,6 +70,8 @@ bool OverlayClient::OnProcessMessageReceived(CefRefPtr<CefBrowser> browser, CefR
uint32_t aPlayerId = eventArgs->GetInt(0);
World::Get().GetPartyService().ChangePartyLeader(aPlayerId);
}
else if (eventName == "teleportToPlayer")
ProcessTeleportMessage(eventArgs);

return true;
}
Expand Down Expand Up @@ -117,3 +120,11 @@ void OverlayClient::ProcessChatMessage(CefRefPtr<CefListValue> aEventArgs)
}
}
}

void OverlayClient::ProcessTeleportMessage(CefRefPtr<CefListValue> aEventArgs)
{
TeleportRequest request{};
request.PlayerId = aEventArgs->GetInt(0);

m_transport.Send(request);
}
31 changes: 31 additions & 0 deletions Code/client/Services/Generic/OverlayService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
#include <Messages/NotifyPlayerDialogue.h>
#include <Messages/NotifyPlayerLevel.h>
#include <Messages/NotifyPlayerCellChanged.h>
#include <Messages/NotifyTeleport.h>

#include <Structs/GridCellCoords.h>

#include <Events/ConnectedEvent.h>
#include <Events/DisconnectedEvent.h>
Expand Down Expand Up @@ -108,6 +111,7 @@ OverlayService::OverlayService(World& aWorld, TransportService& transport, entt:
m_playerRemovedConnection = m_world.on_destroy<PlayerComponent>().connect<&OverlayService::OnPlayerComponentRemoved>(this);
m_playerLevelConnection = aDispatcher.sink<NotifyPlayerLevel>().connect<&OverlayService::OnPlayerLevel>(this);
m_cellChangedConnection = aDispatcher.sink<NotifyPlayerCellChanged>().connect<&OverlayService::OnPlayerCellChanged>(this);
m_teleportConnection = aDispatcher.sink<NotifyTeleport>().connect<&OverlayService::OnNotifyTeleport>(this);
}

OverlayService::~OverlayService() noexcept
Expand Down Expand Up @@ -379,3 +383,30 @@ void OverlayService::OnPlayerCellChanged(const NotifyPlayerCellChanged& acMessag
pArguments->SetString(3, cellName.c_str());
m_pOverlay->ExecuteAsync("setCell", pArguments);
}

void OverlayService::OnNotifyTeleport(const NotifyTeleport& acMessage) noexcept
{
auto& modSystem = m_world.GetModSystem();

const uint32_t cellId = modSystem.GetGameId(acMessage.CellId);
TESObjectCELL* pCell = Cast<TESObjectCELL>(TESForm::GetById(cellId));
if (!pCell)
{
const uint32_t worldSpaceId = modSystem.GetGameId(acMessage.WorldSpaceId);
TESWorldSpace* pWorldSpace = Cast<TESWorldSpace>(TESForm::GetById(worldSpaceId));
if (pWorldSpace)
{
GridCellCoords coordinates = GridCellCoords::CalculateGridCellCoords(acMessage.Position);
pCell = pWorldSpace->LoadCell(coordinates.X, coordinates.Y);
}

if (!pCell)
{
spdlog::error("Failed to fetch cell to teleport to.");
m_world.GetOverlayService().SendSystemMessage("Teleporting to player failed.");
return;
}
}

PlayerCharacter::Get()->MoveTo(pCell, acMessage.Position);
}
1 change: 1 addition & 0 deletions Code/client/Services/OverlayClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ struct OverlayClient : TiltedPhoques::OverlayClient
void ProcessConnectMessage(CefRefPtr<CefListValue> aEventArgs);
void ProcessDisconnectMessage();
void ProcessChatMessage(CefRefPtr<CefListValue> aEventArgs);
void ProcessTeleportMessage(CefRefPtr<CefListValue> aEventArgs);

TransportService& m_transport;
};
3 changes: 3 additions & 0 deletions Code/client/Services/OverlayService.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ struct NotifyPlayerDialogue;
struct ConnectionErrorEvent;
struct NotifyPlayerLevel;
struct NotifyPlayerCellChanged;
struct NotifyTeleport;


using TiltedPhoques::OverlayApp;
Expand Down Expand Up @@ -75,6 +76,7 @@ struct OverlayService
void OnPlayerLeft(const NotifyPlayerLeft&) noexcept;
void OnPlayerLevel(const NotifyPlayerLevel&) noexcept;
void OnPlayerCellChanged(const NotifyPlayerCellChanged& acMessage) const noexcept;
void OnNotifyTeleport(const NotifyTeleport& acMessage) noexcept;

private:
CefRefPtr<OverlayApp> m_pOverlay{nullptr};
Expand All @@ -99,4 +101,5 @@ struct OverlayService
entt::scoped_connection m_playerRemovedConnection;
entt::scoped_connection m_playerLevelConnection;
entt::scoped_connection m_cellChangedConnection;
entt::scoped_connection m_teleportConnection;
};
3 changes: 2 additions & 1 deletion Code/encoding/Messages/ClientMessageFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
#include <Messages/SubtitleRequest.h>
#include <Messages/PlayerDialogueRequest.h>
#include <Messages/PlayerLevelRequest.h>
#include <Messages/TeleportRequest.h>

using TiltedPhoques::UniquePtr;

Expand All @@ -70,7 +71,7 @@ struct ClientMessageFactory
AddTargetRequest, ScriptAnimationRequest, DrawWeaponRequest, MountRequest, NewPackageRequest,
RequestRespawn, SyncExperienceRequest, RequestEquipmentChanges, SendChatMessageRequest,
TeleportCommandRequest, PlayerRespawnRequest, DialogueRequest, SubtitleRequest, PlayerDialogueRequest,
PlayerLevelRequest>;
PlayerLevelRequest, TeleportRequest>;

return s_visitor(std::forward<T>(func));
}
Expand Down
18 changes: 18 additions & 0 deletions Code/encoding/Messages/NotifyTeleport.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <Messages/NotifyTeleport.h>
#include <TiltedCore/Serialization.hpp>

void NotifyTeleport::SerializeRaw(TiltedPhoques::Buffer::Writer& aWriter) const noexcept
{
CellId.Serialize(aWriter);
Position.Serialize(aWriter);
WorldSpaceId.Serialize(aWriter);
}

void NotifyTeleport::DeserializeRaw(TiltedPhoques::Buffer::Reader& aReader) noexcept
{
ServerMessage::DeserializeRaw(aReader);

CellId.Deserialize(aReader);
Position.Deserialize(aReader);
WorldSpaceId.Deserialize(aReader);
}
32 changes: 32 additions & 0 deletions Code/encoding/Messages/NotifyTeleport.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#pragma once

#include "Message.h"
#include <Structs/Vector3_NetQuantize.h>
#include <Structs/GameId.h>

struct NotifyTeleport final : ServerMessage
{
static constexpr ServerOpcode Opcode = kNotifyTeleport;

NotifyTeleport() :
ServerMessage(Opcode)
{
}

virtual ~NotifyTeleport() = default;

void SerializeRaw(TiltedPhoques::Buffer::Writer& aWriter) const noexcept override;
void DeserializeRaw(TiltedPhoques::Buffer::Reader& aReader) noexcept override;

bool operator==(const NotifyTeleport& acRhs) const noexcept
{
return GetOpcode() == acRhs.GetOpcode() &&
CellId == acRhs.CellId &&
Position == acRhs.Position &&
WorldSpaceId == acRhs.WorldSpaceId;
}

GameId CellId{};
Vector3_NetQuantize Position{};
GameId WorldSpaceId{};
};
4 changes: 3 additions & 1 deletion Code/encoding/Messages/ServerMessageFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
#include <Messages/NotifyPlayerJoined.h>
#include <Messages/NotifyPlayerLevel.h>
#include <Messages/NotifyPlayerCellChanged.h>
#include <Messages/NotifyTeleport.h>

using TiltedPhoques::UniquePtr;

Expand All @@ -70,7 +71,8 @@ struct ServerMessageFactory
NotifyAddTarget, NotifyScriptAnimation, NotifyDrawWeapon, NotifyMount, NotifyNewPackage,
NotifyRespawn, NotifySyncExperience, NotifyEquipmentChanges, NotifyChatMessageBroadcast,
TeleportCommandResponse, NotifyPlayerRespawn, NotifyPlayerLeft, NotifyPlayerJoined,
NotifyDialogue, NotifySubtitle, NotifyPlayerDialogue, NotifyPlayerLevel, NotifyPlayerCellChanged>;
NotifyDialogue, NotifySubtitle, NotifyPlayerDialogue, NotifyPlayerLevel, NotifyPlayerCellChanged,
NotifyTeleport>;

return s_visitor(std::forward<T>(func));
}
Expand Down
13 changes: 13 additions & 0 deletions Code/encoding/Messages/TeleportRequest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <Messages/TeleportRequest.h>

void TeleportRequest::SerializeRaw(TiltedPhoques::Buffer::Writer& aWriter) const noexcept
{
Serialization::WriteVarInt(aWriter, PlayerId);
}

void TeleportRequest::DeserializeRaw(TiltedPhoques::Buffer::Reader& aReader) noexcept
{
ClientMessage::DeserializeRaw(aReader);

PlayerId = Serialization::ReadVarInt(aReader) & 0xFFFFFFFF;
}
24 changes: 24 additions & 0 deletions Code/encoding/Messages/TeleportRequest.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include "Message.h"
#include <Structs/GameId.h>

struct TeleportRequest final : ClientMessage
{
static constexpr ClientOpcode Opcode = kTeleportRequest;

TeleportRequest() : ClientMessage(Opcode)
{
}

void SerializeRaw(TiltedPhoques::Buffer::Writer& aWriter) const noexcept override;
void DeserializeRaw(TiltedPhoques::Buffer::Reader& aReader) noexcept override;

bool operator==(const TeleportRequest& acRhs) const noexcept
{
return GetOpcode() == acRhs.GetOpcode() &&
PlayerId == acRhs.PlayerId;
}

uint16_t PlayerId{};
};
2 changes: 2 additions & 0 deletions Code/encoding/Opcodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ enum ClientOpcode : unsigned char
kSubtitleRequest,
kPlayerDialogueRequest,
kPlayerLevelRequest,
kTeleportRequest,
kClientOpcodeMax
};

Expand Down Expand Up @@ -98,5 +99,6 @@ enum ServerOpcode : unsigned char
kNotifyPlayerJoined,
kNotifyPlayerLevel,
kNotifyPlayerCellChanged,
kNotifyTeleport,
kServerOpcodeMax
};
32 changes: 29 additions & 3 deletions Code/server/Services/OverlayService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include <Messages/SendChatMessageRequest.h>
#include <Messages/PlayerDialogueRequest.h>
#include <Messages/NotifyPlayerDialogue.h>
#include <Messages/TeleportRequest.h>
#include <Messages/NotifyTeleport.h>

#include <regex>

Expand All @@ -14,11 +16,12 @@ OverlayService::OverlayService(World& aWorld, entt::dispatcher& aDispatcher)
{
m_chatMessageConnection = aDispatcher.sink<PacketEvent<SendChatMessageRequest>>().connect<&OverlayService::HandleChatMessage>(this);
m_playerDialogueConnection = aDispatcher.sink<PacketEvent<PlayerDialogueRequest>>().connect<&OverlayService::OnPlayerDialogue>(this);
m_teleportConnection = aDispatcher.sink<PacketEvent<TeleportRequest>>().connect<&OverlayService::OnTeleport>(this);
}

void OverlayService::HandleChatMessage(const PacketEvent<SendChatMessageRequest>& acMessage) const noexcept
{
NotifyChatMessageBroadcast notifyMessage;
NotifyChatMessageBroadcast notifyMessage{};
notifyMessage.PlayerName = acMessage.pPlayer->GetUsername();

// TODO: std regex is slow
Expand All @@ -32,15 +35,38 @@ void OverlayService::OnPlayerDialogue(const PacketEvent<PlayerDialogueRequest>&
{
auto& message = acMessage.Packet;

NotifyPlayerDialogue notify;
NotifyPlayerDialogue notify{};
notify.Text = acMessage.pPlayer->GetUsername() + ": " + message.Text;

auto& party = acMessage.pPlayer->GetParty();

// TODO(cosideci): exclude player?
GameServer::Get()->SendToParty(notify, party);
}

void OverlayService::OnTeleport(const PacketEvent<TeleportRequest>& acMessage) const noexcept
{
Player* pTargetPlayer = m_world.GetPlayerManager().GetById(acMessage.Packet.PlayerId);
if (!pTargetPlayer)
return;

NotifyTeleport response{};

auto character = pTargetPlayer->GetCharacter();
if (character)
{
const auto* pMovementComponent = m_world.try_get<MovementComponent>(*character);
if (pMovementComponent)
{
const auto& cellComponent = pTargetPlayer->GetCellComponent();
response.CellId = cellComponent.Cell;
response.Position = pMovementComponent->Position;
response.WorldSpaceId = cellComponent.WorldSpaceId;
}
}

acMessage.pPlayer->Send(response);
}

#if 0
#include <Components.h>
#include <Events/PlayerEnterWorldEvent.h>
Expand Down
3 changes: 3 additions & 0 deletions Code/server/Services/OverlayService.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
struct World;

struct PlayerDialogueRequest;
struct TeleportRequest;

/**
* @brief Dispatches UI events that modify the UI view of other cients.
Expand All @@ -24,11 +25,13 @@ class OverlayService
void HandleChatMessage(const PacketEvent<SendChatMessageRequest>& acMessage) const noexcept;
void HandlePlayerJoin(const PlayerEnterWorldEvent& acEvent) const noexcept;
void OnPlayerDialogue(const PacketEvent<PlayerDialogueRequest>& acMessage) const noexcept;
void OnTeleport(const PacketEvent<TeleportRequest>& acMessage) const noexcept;

private:
World& m_world;

entt::scoped_connection m_chatMessageConnection;
entt::scoped_connection m_playerEnterWorldConnection;
entt::scoped_connection m_playerDialogueConnection;
entt::scoped_connection m_teleportConnection;
};

0 comments on commit 297d028

Please sign in to comment.