Skip to content

Commit

Permalink
fix: quest items and lock sync bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
RobbeBryssinck committed May 25, 2022
1 parent 2b7c2d9 commit cf582f5
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Code/client/Games/Skyrim/Forms/TESObjectCELL.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <Forms/TESObjectCELL.h>
#include <TESObjectREFR.h>

Vector<TESObjectREFR*> TESObjectCELL::GetRefsByFormTypes(const Vector<FormType>& aFormTypes) noexcept
Vector<TESObjectREFR*> TESObjectCELL::GetRefsByFormTypes(const Vector<FormType>& aFormTypes) const noexcept
{
Vector<TESObjectREFR*> references{};

Expand Down
2 changes: 1 addition & 1 deletion Code/client/Games/Skyrim/Forms/TESObjectCELL.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ struct TESWorldSpace;

struct TESObjectCELL : TESForm
{
Vector<TESObjectREFR*> GetRefsByFormTypes(const Vector<FormType>& aFormTypes) noexcept;
Vector<TESObjectREFR*> GetRefsByFormTypes(const Vector<FormType>& aFormTypes) const noexcept;
void GetCOCPlacementInfo(NiPoint3* aOutPos, NiPoint3* aOutRot, bool aAllowCellLoad) noexcept;

uint8_t pad20[0x40 - 0x20];
Expand Down
11 changes: 9 additions & 2 deletions Code/client/Games/Skyrim/TESObjectREFR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@ int64_t TESObjectREFR::GetItemCountInInventory(TESForm* apItem) const noexcept
return count;
}

TESObjectCELL* TESObjectREFR::GetParentCellEx() const noexcept
{
return parentCell ? parentCell : GetParentCell();
}

void TESObjectREFR::GetItemFromExtraData(Inventory::Entry& arEntry, ExtraDataList* apExtraDataList) noexcept
{
auto& modSystem = World::Get().GetModSystem();
Expand Down Expand Up @@ -528,14 +533,16 @@ void TESObjectREFR::AddOrRemoveItem(const Inventory::Entry& arEntry) noexcept
// It is still recommended that the quest leader loots all quest items.
if (arEntry.IsQuestItem && arEntry.Count > 0)
{
if (IsItemInInventory(objectId))
PlayerCharacter* pPlayer = PlayerCharacter::Get();

if (pPlayer->IsItemInInventory(objectId))
return;

Actor* pActor = Cast<Actor>(this);
if (!pActor || !pActor->GetExtension()->IsRemotePlayer())
return;

PlayerCharacter::Get()->AddOrRemoveItem(arEntry);
pPlayer->AddOrRemoveItem(arEntry);
}
}

Expand Down
1 change: 1 addition & 0 deletions Code/client/Games/Skyrim/TESObjectREFR.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ struct TESObjectREFR : TESForm
Lock* GetLock() noexcept;
TESContainer* GetContainer() const noexcept;
int64_t GetItemCountInInventory(TESForm* apItem) const noexcept;
TESObjectCELL* GetParentCellEx() const noexcept;

void SaveInventory(BGSSaveFormBuffer* apBuffer) const noexcept;
void SaveAnimationVariables(AnimationVariables& aWriter) const noexcept;
Expand Down
3 changes: 2 additions & 1 deletion Code/client/Services/Debug/Views/CellView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ void DebugService::DrawCellView()
{
if (ImGui::CollapsingHeader("Parent cell", ImGuiTreeNodeFlags_DefaultOpen))
{
ImGui::InputScalar("Id", ImGuiDataType_U32, pCell, nullptr, nullptr, "%" PRIx32,
const uint32_t cellId = pCell->formID;
ImGui::InputScalar("Id", ImGuiDataType_U32, (void*)&cellId, nullptr, nullptr, "%" PRIx32,
ImGuiInputTextFlags_ReadOnly | ImGuiInputTextFlags_CharsHexadecimal);

char* pName = (char*)pCell->GetName();
Expand Down
20 changes: 17 additions & 3 deletions Code/client/Services/Debug/Views/FormDebugView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

void DebugService::DrawFormDebugView()
{
static Actor* pRefr = nullptr;
static TESForm* pFetchForm = nullptr;
static TESObjectREFR* pRefr = nullptr;

ImGui::Begin("Form");

Expand All @@ -27,7 +27,7 @@ void DebugService::DrawFormDebugView()
{
pFetchForm = TESForm::GetById(m_formId);
if (pFetchForm)
pRefr = Cast<Actor>(pFetchForm);
pRefr = Cast<TESObjectREFR>(pFetchForm);
}
}

Expand All @@ -39,11 +39,24 @@ void DebugService::DrawFormDebugView()

if (pRefr)
{
if (auto* pParentCell = pRefr->GetParentCell())
{
const uint32_t cellId = pParentCell->formID;
ImGui::InputScalar("GetParentCell", ImGuiDataType_U32, (void*)&cellId, nullptr, nullptr, "%" PRIx32,
ImGuiInputTextFlags_ReadOnly | ImGuiInputTextFlags_CharsHexadecimal);
}

if (auto* pParentCell = pRefr->parentCell)
{
const uint32_t cellId = pParentCell->formID;
ImGui::InputScalar("parentCell", ImGuiDataType_U32, (void*)&cellId, nullptr, nullptr, "%" PRIx32,
ImGuiInputTextFlags_ReadOnly | ImGuiInputTextFlags_CharsHexadecimal);
}

/*
char name[256];
sprintf_s(name, std::size(name), "%s (%x)", pRefr->baseForm->GetName(), pRefr->formID);
ImGui::InputText("Name", name, std::size(name), ImGuiInputTextFlags_ReadOnly);
*/
for (ActiveEffect* pEffect : *pRefr->currentProcess->middleProcess->ActiveEffects)
{
Expand All @@ -61,6 +74,7 @@ void DebugService::DrawFormDebugView()
if (ImGui::Button("Elapse time"))
m_world.GetRunner().Queue([pEffect]() { pEffect->fElapsedSeconds = pEffect->fDuration - 3.f; });
}
*/
}

ImGui::End();
Expand Down
28 changes: 18 additions & 10 deletions Code/client/Services/Generic/ObjectService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,13 @@ void ObjectService::OnCellChange(const CellChangeEvent& acEvent) noexcept
return;

PlayerCharacter* pPlayer = PlayerCharacter::Get();
const TESObjectCELL* pCell = pPlayer->parentCell;

// TODO(cosideci): why isn't the event's cell id being used?
GameId cellId{};
if (!m_world.GetModSystem().GetServerModId(pPlayer->parentCell->formID, cellId))
if (!m_world.GetModSystem().GetServerModId(pCell->formID, cellId))
{
spdlog::error("Server cell id not found for cell form id {:X}", pPlayer->parentCell->formID);
return;
}

TESObjectCELL* pCell = Cast<TESObjectCELL>(TESForm::GetById(pPlayer->parentCell->formID));
if (!pCell)
{
spdlog::error("Cell not found for cell form id {:X}", pPlayer->parentCell->formID);
spdlog::error("Server cell id not found for cell form id {:X}", pCell->formID);
return;
}

Expand Down Expand Up @@ -189,7 +183,14 @@ void ObjectService::OnActivate(const ActivateEvent& acEvent) noexcept
return;
}

if (!m_world.GetModSystem().GetServerModId(acEvent.pObject->parentCell->formID, request.CellId))
TESObjectCELL* pCell = acEvent.pObject->GetParentCellEx();
if (!pCell)
{
spdlog::error("Activated object has no parent cell: {:X}", acEvent.pObject->formID);
return;
}

if (!m_world.GetModSystem().GetServerModId(pCell->formID, request.CellId))
{
spdlog::error("Server cell id not found for cell form id {:X}", acEvent.pObject->parentCell->formID);
return;
Expand Down Expand Up @@ -257,6 +258,13 @@ void ObjectService::OnLockChange(const LockChangeEvent& acEvent) noexcept

const auto* const pObject = Cast<TESObjectREFR>(TESForm::GetById(acEvent.FormId));

TESObjectCELL* pCell = pObject->GetParentCellEx();
if (!pCell)
{
spdlog::error("Activated object has no parent cell: {:X}", pObject->formID);
return;
}

if (!m_world.GetModSystem().GetServerModId(pObject->parentCell->formID, request.CellId))
{
spdlog::error("Server cell id for cell not found, cell form id: {:X}", pObject->parentCell->formID);
Expand Down

0 comments on commit cf582f5

Please sign in to comment.