Skip to content

Commit

Permalink
tweak: take ownership debug button
Browse files Browse the repository at this point in the history
  • Loading branch information
RobbeBryssinck committed May 29, 2022
1 parent b208a1c commit 8631066
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 37 deletions.
2 changes: 2 additions & 0 deletions Code/client/Services/CharacterService.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ struct CharacterService

TP_NOCOPYMOVE(CharacterService);

bool TakeOwnership(const uint32_t acFormId, const uint32_t acServerId, const entt::entity acEntity) const noexcept;

void OnActorAdded(const ActorAddedEvent& acEvent) noexcept;
void OnActorRemoved(const ActorRemovedEvent& acEvent) noexcept;
void OnUpdate(const UpdateEvent& acUpdateEvent) noexcept;
Expand Down
15 changes: 12 additions & 3 deletions Code/client/Services/Debug/Views/EntitiesView.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include <Services/DebugService.h>

#include <Services/CharacterService.h>

#include <AI/AIProcess.h>

#include <World.h>
Expand Down Expand Up @@ -123,7 +125,7 @@ void DebugService::DisplayEntityPanel(entt::entity aEntity) noexcept

if (pFormIdComponent) DisplayFormComponent(*pFormIdComponent);
if (pLocalComponent) DisplayLocalComponent(*pLocalComponent);
if (pRemoteComponent) DisplayRemoteComponent(*pRemoteComponent);
if (pRemoteComponent) DisplayRemoteComponent(*pRemoteComponent, aEntity, pFormIdComponent ? pFormIdComponent->Id : 0);
}

void DebugService::DisplayFormComponent(FormIdComponent& aFormComponent) const noexcept
Expand Down Expand Up @@ -184,10 +186,17 @@ void DebugService::DisplayLocalComponent(LocalComponent& aLocalComponent) const
ImGui::InputScalarN("State", ImGuiDataType_U32, &action.State1, 2, nullptr, nullptr, "%x", ImGuiInputTextFlags_ReadOnly);
}

void DebugService::DisplayRemoteComponent(RemoteComponent& aLocalComponent) const noexcept
void DebugService::DisplayRemoteComponent(RemoteComponent& aRemoteComponent, const entt::entity acEntity, const uint32_t acFormId) const noexcept
{
if (!ImGui::CollapsingHeader("Remote Component", ImGuiTreeNodeFlags_DefaultOpen))
return;

ImGui::InputInt("Server Id", (int*)&aLocalComponent.Id, 0, 0, ImGuiInputTextFlags_ReadOnly | ImGuiInputTextFlags_CharsHexadecimal);
ImGui::InputInt("Server Id", (int*)&aRemoteComponent.Id, 0, 0, ImGuiInputTextFlags_ReadOnly | ImGuiInputTextFlags_CharsHexadecimal);
if (ImGui::Button("Take ownership"))
{
m_world.GetRunner().Queue([acEntity, acFormId]() {
if (auto* pRemoteCompoment = World::Get().try_get<RemoteComponent>(acEntity))
World::Get().GetCharacterService().TakeOwnership(acFormId, pRemoteCompoment->Id, acEntity);
});
}
}
2 changes: 1 addition & 1 deletion Code/client/Services/DebugService.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ struct DebugService
void DisplayEntityPanel(entt::entity aEntity) noexcept;
void DisplayFormComponent(FormIdComponent& aFormComponent) const noexcept;
void DisplayLocalComponent(LocalComponent& aLocalComponent) const noexcept;
void DisplayRemoteComponent(RemoteComponent& aLocalComponent) const noexcept;
void DisplayRemoteComponent(RemoteComponent& aLocalComponent, const entt::entity acEntity, const uint32_t acFormId) const noexcept;

void DrawEntitiesView();
void DrawComponentDebugView();
Expand Down
60 changes: 27 additions & 33 deletions Code/client/Services/Generic/CharacterService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,30 @@ CharacterService::CharacterService(World& aWorld, entt::dispatcher& aDispatcher,
m_actorTeleportConnection = m_dispatcher.sink<NotifyActorTeleport>().connect<&CharacterService::OnNotifyActorTeleport>(this);
}

bool CharacterService::TakeOwnership(const uint32_t acFormId, const uint32_t acServerId, const entt::entity acEntity) const noexcept
{
Actor* const pActor = Cast<Actor>(TESForm::GetById(acFormId));
if (!pActor)
return false;

pActor->GetExtension()->SetRemote(false);

// TODO(cosideci): this should be done differently.
// Send an ownership claim request, and have the server broadcast the result.
// Only then should components be added or removed.
m_world.emplace<LocalComponent>(acEntity, acServerId);
m_world.emplace<LocalAnimationComponent>(acEntity);
m_world.remove<RemoteComponent, InterpolationComponent, RemoteAnimationComponent,
FaceGenComponent, CacheComponent, WaitingFor3D>(acEntity);

RequestOwnershipClaim request;
request.ServerId = acServerId;

m_transport.Send(request);

return true;
}

void CharacterService::OnActorAdded(const ActorAddedEvent& acEvent) noexcept
{
if (acEvent.FormId == 0x14)
Expand Down Expand Up @@ -548,25 +572,9 @@ void CharacterService::OnOwnershipTransfer(const NotifyOwnershipTransfer& acMess
{
auto& formIdComponent = view.get<FormIdComponent>(*itor);

auto* const pActor = Cast<Actor>(TESForm::GetById(formIdComponent.Id));
if (pActor)
if (TakeOwnership(formIdComponent.Id, acMessage.ServerId, *itor))
{
pActor->GetExtension()->SetRemote(false);

// TODO(cosideci): this should be done differently.
// Send an ownership claim request, and have the server broadcast the result.
// Only then should components be added or removed.
m_world.emplace<LocalComponent>(*itor, acMessage.ServerId);
m_world.emplace<LocalAnimationComponent>(*itor);
m_world.remove<RemoteComponent, InterpolationComponent, RemoteAnimationComponent,
FaceGenComponent, CacheComponent, WaitingFor3D>(*itor);

RequestOwnershipClaim request;
request.ServerId = acMessage.ServerId;

m_transport.Send(request);
spdlog::info("Ownership claimed {:X}", request.ServerId);

spdlog::info("Ownership claimed {:X}", acMessage.ServerId);
return;
}
}
Expand Down Expand Up @@ -845,21 +853,7 @@ void CharacterService::OnMountEvent(const MountEvent& acEvent) const noexcept
}

if (m_world.try_get<RemoteComponent>(cMountEntity))
{
const TESForm* pMountForm = TESForm::GetById(acEvent.MountID);
Actor* pMount = Cast<Actor>(pMountForm);
pMount->GetExtension()->SetRemote(false);

m_world.emplace<LocalComponent>(cMountEntity, mountServerIdRes.value());
m_world.emplace<LocalAnimationComponent>(cMountEntity);
m_world.remove<RemoteComponent, InterpolationComponent, RemoteAnimationComponent,
FaceGenComponent, CacheComponent, WaitingFor3D>(cMountEntity);

RequestOwnershipClaim request;
request.ServerId = mountServerIdRes.value();

m_transport.Send(request);
}
TakeOwnership(acEvent.MountID, *mountServerIdRes, cMountEntity);

MountRequest request;
request.MountId = mountServerIdRes.value();
Expand Down
2 changes: 2 additions & 0 deletions Code/client/World.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ struct World : entt::registry
const PartyService& GetPartyService() const noexcept { return ctx().at<const PartyService>(); }
OverlayService& GetOverlayService() noexcept { return ctx().at<OverlayService>(); }
const OverlayService& GetOverlayService() const noexcept { return ctx().at<const OverlayService>(); }
CharacterService& GetCharacterService() noexcept { return ctx().at<CharacterService>(); }
const CharacterService& GetCharacterService() const noexcept { return ctx().at<const CharacterService>(); }
DebugService& GetDebugService() noexcept { return ctx().at<DebugService>(); }
const DebugService& GetDebugService() const noexcept { return ctx().at<const DebugService>(); }

Expand Down

0 comments on commit 8631066

Please sign in to comment.