Skip to content

Commit

Permalink
fix: awful fix for weapon draw sync not working
Browse files Browse the repository at this point in the history
  • Loading branch information
RobbeBryssinck committed Apr 7, 2022
1 parent 8f0f1bc commit 370c261
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 14 deletions.
4 changes: 2 additions & 2 deletions Code/client/Games/References.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -528,13 +528,13 @@ extern thread_local bool g_forceAnimation;

void Actor::SetWeaponDrawnEx(bool aDraw) noexcept
{
spdlog::debug("Setting weapon drawn: {:X}:{}, current state: {}", formID, aDraw, actorState.IsWeaponDrawn());
spdlog::info("Setting weapon drawn: {:X}:{}, current state: {}", formID, aDraw, actorState.IsWeaponDrawn());

if (actorState.IsWeaponDrawn() == aDraw)
{
actorState.SetWeaponDrawn(!aDraw);

spdlog::debug("Setting weapon drawn after update: {:X}:{}, current state: {}", formID, aDraw, actorState.IsWeaponDrawn());
spdlog::info("Setting weapon drawn after update: {:X}:{}, current state: {}", formID, aDraw, actorState.IsWeaponDrawn());
}

g_forceAnimation = true;
Expand Down
4 changes: 2 additions & 2 deletions Code/client/Games/Skyrim/PlayerCharacter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void PlayerCharacter::AddSkillExperience(int32_t aSkill, float aExperience) noex
float newExperience = GetSkillExperience(skill);
float deltaExperience = newExperience - oldExperience;

spdlog::info("Added {} experience to skill {}", deltaExperience, aSkill);
spdlog::debug("Added {} experience to skill {}", deltaExperience, aSkill);
}

char TP_MAKE_THISCALL(HookPickUpObject, PlayerCharacter, TESObjectREFR* apObject, int32_t aCount, bool aUnk1, bool aUnk2)
Expand Down Expand Up @@ -88,7 +88,7 @@ void TP_MAKE_THISCALL(HookAddSkillExperience, PlayerCharacter, int32_t aSkill, f
float newExperience = apThis->GetSkillExperience(skill);
float deltaExperience = newExperience - oldExperience;

spdlog::info("Skill (AVI): {}, experience: {}", aSkill, deltaExperience);
spdlog::debug("Skill (AVI): {}, experience: {}", aSkill, deltaExperience);

if (combatSkills.contains(aSkill))
{
Expand Down
8 changes: 5 additions & 3 deletions Code/client/Services/CharacterService.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ struct CharacterService
void OnUpdate(const UpdateEvent& acUpdateEvent) noexcept;
void OnConnected(const ConnectedEvent& acConnectedEvent) const noexcept;
void OnDisconnected(const DisconnectedEvent& acDisconnectedEvent) const noexcept;
void OnAssignCharacter(const AssignCharacterResponse& acMessage) const noexcept;
void OnAssignCharacter(const AssignCharacterResponse& acMessage) noexcept;
void OnCharacterSpawn(const CharacterSpawnRequest& acMessage) const noexcept;
void OnReferencesMoveRequest(const ServerReferencesMoveRequest& acMessage) const noexcept;
void OnActionEvent(const ActionEvent& acActionEvent) const noexcept;
void OnFactionsChanges(const NotifyFactionsChanges& acEvent) const noexcept;
void OnOwnershipTransfer(const NotifyOwnershipTransfer& acMessage) const noexcept;
void OnRemoveCharacter(const NotifyRemoveCharacter& acMessage) const noexcept;
void OnRemoteSpawnDataReceived(const NotifySpawnData& acEvent) const noexcept;
void OnRemoteSpawnDataReceived(const NotifySpawnData& acEvent) noexcept;
void OnProjectileLaunchedEvent(const ProjectileLaunchedEvent& acEvent) const noexcept;
void OnNotifyProjectileLaunch(const NotifyProjectileLaunch& acMessage) const noexcept;
void OnMountEvent(const MountEvent& acEvent) const noexcept;
Expand All @@ -75,7 +75,7 @@ struct CharacterService
Actor* CreateCharacterForEntity(entt::entity aEntity) const noexcept;

void RunLocalUpdates() const noexcept;
void RunRemoteUpdates() const noexcept;
void RunRemoteUpdates() noexcept;
void RunFactionsUpdates() const noexcept;
void RunSpawnUpdates() const noexcept;
void RunExperienceUpdates() noexcept;
Expand All @@ -86,6 +86,8 @@ struct CharacterService

float m_cachedExperience = 0.f;

Map<uint32_t, std::pair<double, bool>> m_weaponDrawUpdates{};

entt::scoped_connection m_formIdAddedConnection;
entt::scoped_connection m_formIdRemovedConnection;
entt::scoped_connection m_updateConnection;
Expand Down
42 changes: 35 additions & 7 deletions Code/client/Services/Generic/CharacterService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,27 @@ void CharacterService::OnUpdate(const UpdateEvent& acUpdateEvent) noexcept
RunFactionsUpdates();
RunRemoteUpdates();
RunExperienceUpdates();

std::vector<uint32_t> toRemove{};

for (auto& [cId, cTimer] : m_weaponDrawUpdates)
{
double& timer = const_cast<double&>(cTimer.first);
timer += acUpdateEvent.Delta;
if (timer <= 0.5)
continue;

Actor* pActor = RTTI_CAST(TESForm::GetById(cId), TESForm, Actor);
if (!pActor)
continue;

pActor->SetWeaponDrawnEx(cTimer.second);

toRemove.push_back(cId);
}

for (auto id : toRemove)
m_weaponDrawUpdates.erase(id);
}

void CharacterService::OnConnected(const ConnectedEvent& acConnectedEvent) const noexcept
Expand Down Expand Up @@ -191,7 +212,7 @@ void CharacterService::OnDisconnected(const DisconnectedEvent& acDisconnectedEve
m_world.clear<WaitingForAssignmentComponent, LocalComponent, RemoteComponent>();
}

void CharacterService::OnAssignCharacter(const AssignCharacterResponse& acMessage) const noexcept
void CharacterService::OnAssignCharacter(const AssignCharacterResponse& acMessage) noexcept
{
spdlog::info("Received for cookie {:X}", acMessage.Cookie);

Expand Down Expand Up @@ -231,7 +252,8 @@ void CharacterService::OnAssignCharacter(const AssignCharacterResponse& acMessag
acMessage.IsDead ? pActor->Kill() : pActor->Respawn();

if (pActor->actorState.IsWeaponDrawn() != acMessage.IsWeaponDrawn)
pActor->SetWeaponDrawnEx(acMessage.IsWeaponDrawn);
m_weaponDrawUpdates[formIdComponent->Id] = {0, acMessage.IsWeaponDrawn};
//pActor->SetWeaponDrawnEx(acMessage.IsWeaponDrawn);

return;
}
Expand Down Expand Up @@ -270,7 +292,8 @@ void CharacterService::OnAssignCharacter(const AssignCharacterResponse& acMessag
acMessage.IsDead ? pActor->Kill() : pActor->Respawn();

if (pActor->actorState.IsWeaponDrawn() != acMessage.IsWeaponDrawn)
pActor->SetWeaponDrawnEx(acMessage.IsWeaponDrawn);
m_weaponDrawUpdates[pActor->formID] = {0, acMessage.IsWeaponDrawn};
//pActor->SetWeaponDrawnEx(acMessage.IsWeaponDrawn);

const uint32_t cCellId = World::Get().GetModSystem().GetGameId(acMessage.CellId);
const TESForm* const pCellForm = TESForm::GetById(cCellId);
Expand Down Expand Up @@ -386,7 +409,7 @@ void CharacterService::OnCharacterSpawn(const CharacterSpawnRequest& acMessage)
}

// TODO: verify/simplify this spawn data stuff
void CharacterService::OnRemoteSpawnDataReceived(const NotifySpawnData& acMessage) const noexcept
void CharacterService::OnRemoteSpawnDataReceived(const NotifySpawnData& acMessage) noexcept
{
auto view = m_world.view<RemoteComponent, FormIdComponent>();

Expand All @@ -404,6 +427,7 @@ void CharacterService::OnRemoteSpawnDataReceived(const NotifySpawnData& acMessag
remoteComponent.SpawnRequest.InitialActorValues = acMessage.InitialActorValues;
remoteComponent.SpawnRequest.InventoryContent = acMessage.InitialInventory;
remoteComponent.SpawnRequest.IsDead = acMessage.IsDead;
remoteComponent.SpawnRequest.IsWeaponDrawn = acMessage.IsWeaponDrawn;

auto& formIdComponent = view.get<FormIdComponent>(*itor);
auto* const pForm = TESForm::GetById(formIdComponent.Id);
Expand All @@ -414,6 +438,8 @@ void CharacterService::OnRemoteSpawnDataReceived(const NotifySpawnData& acMessag

pActor->SetActorValues(remoteComponent.SpawnRequest.InitialActorValues);
pActor->SetActorInventory(remoteComponent.SpawnRequest.InventoryContent);
m_weaponDrawUpdates[pActor->formID] = {0, acMessage.IsWeaponDrawn};
//pActor->SetWeaponDrawnEx(acMessage.IsWeaponDrawn);

if (pActor->IsDead() != acMessage.IsDead)
acMessage.IsDead ? pActor->Kill() : pActor->Respawn();
Expand Down Expand Up @@ -1228,7 +1254,7 @@ void CharacterService::RunLocalUpdates() const noexcept
m_transport.Send(message);
}

void CharacterService::RunRemoteUpdates() const noexcept
void CharacterService::RunRemoteUpdates() noexcept
{
// Delay by 120ms to let the interpolation system accumulate interpolation points
const auto tick = m_transport.GetClock().GetCurrentTick() - 120;
Expand Down Expand Up @@ -1301,7 +1327,8 @@ void CharacterService::RunRemoteUpdates() const noexcept
pActor->SetActorInventory(remoteComponent.SpawnRequest.InventoryContent);
pActor->SetFactions(remoteComponent.SpawnRequest.FactionsContent);
pActor->LoadAnimationVariables(remoteComponent.SpawnRequest.LatestAction.Variables);
pActor->SetWeaponDrawnEx(remoteComponent.SpawnRequest.IsWeaponDrawn);
m_weaponDrawUpdates[pActor->formID] = {0, remoteComponent.SpawnRequest.IsWeaponDrawn};
//pActor->SetWeaponDrawnEx(acMessage.IsWeaponDrawn);

if (pActor->IsDead() != remoteComponent.SpawnRequest.IsDead)
remoteComponent.SpawnRequest.IsDead ? pActor->Kill() : pActor->Respawn();
Expand Down Expand Up @@ -1414,5 +1441,6 @@ void CharacterService::RunExperienceUpdates() noexcept
m_cachedExperience = 0.f;

m_transport.Send(message);
spdlog::info("Sending over experience {}", message.Experience);

spdlog::debug("Sending over experience {}", message.Experience);
}

0 comments on commit 370c261

Please sign in to comment.