Skip to content

Commit

Permalink
feat: Actor::SetFullContainer()
Browse files Browse the repository at this point in the history
  • Loading branch information
RobbeBryssinck committed Dec 27, 2021
1 parent e1f7c94 commit 1110e42
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 6 deletions.
6 changes: 3 additions & 3 deletions Code/client/Games/ExtraData.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ struct BSExtraDataList
#if TP_FALLOUT4
void* unk0;
#endif
BSExtraData* data;
BSExtraData* data = nullptr;

struct Bitfield
{
Expand All @@ -54,6 +54,6 @@ struct BSExtraDataList
void* unk10;
#endif

Bitfield* bitfield;
mutable BSRecursiveLock lock;
Bitfield* bitfield{};
mutable BSRecursiveLock lock{};
};
38 changes: 38 additions & 0 deletions Code/client/Games/Skyrim/Actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,44 @@ Container Actor::GetFullContainer() const noexcept
return fullContainer;
}

void Actor::SetFullContainer(Container& acContainer) noexcept
{
RemoveAllItems();

std::sort(acContainer.Entries.begin(), acContainer.Entries.end(), [](Container::Entry lhs, Container::Entry rhs) {
return lhs.Count < rhs.Count;
});

Container currentContainer = GetFullContainer();
for (auto currentEntry : currentContainer.Entries)
{
auto& duplicate = std::find_if(acContainer.Entries.begin(), acContainer.Entries.end(), [currentEntry](Container::Entry newEntry) {
return newEntry.CanBeMerged(currentEntry);
});

if (duplicate != std::end(acContainer.Entries))
{
duplicate->Count -= currentEntry.Count;
continue;
}
else
{
acContainer.Entries.push_back(*duplicate);
Container::Entry& back = acContainer.Entries.back();
back.Count *= -1;
}
}

std::remove_if(acContainer.Entries.begin(), acContainer.Entries.end(), [](Container::Entry entry) {
return entry.Count == 0;
});

for (const Container::Entry& entry : acContainer.Entries)
{
AddItem(entry);
}
}

Factions Actor::GetFactions() const noexcept
{
Factions result;
Expand Down
1 change: 1 addition & 0 deletions Code/client/Games/Skyrim/Actor.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ struct Actor : TESObjectREFR
void SetFactionRank(const TESFaction* apFaction, int8_t aRank) noexcept;
void ForcePosition(const NiPoint3& acPosition) noexcept;
void SetWeaponDrawnEx(bool aDraw) noexcept;
void SetFullContainer(Container& acContainer) noexcept;

// Actions
void UnEquipAll() noexcept;
Expand Down
7 changes: 6 additions & 1 deletion Code/client/Games/Skyrim/TESObjectREFR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ int64_t TESObjectREFR::GetItemCountInInventory(TESForm* apItem) const noexcept
return count;
}

void TESObjectREFR::AddItem(Container::Entry& arEntry) noexcept
void TESObjectREFR::AddItem(const Container::Entry& arEntry) noexcept
{
ModSystem& modSystem = World::Get().GetModSystem();

Expand All @@ -166,13 +166,15 @@ void TESObjectREFR::AddItem(Container::Entry& arEntry) noexcept
if (arEntry.ContainsExtraData())
{
pExtraData = Memory::Allocate<BSExtraDataList>();
pExtraData->data = nullptr;
pExtraData->lock.m_counter = pExtraData->lock.m_tid = 0;
pExtraData->bitfield = Memory::Allocate<BSExtraDataList::Bitfield>();
memset(pExtraData->bitfield, 0, 0x18);

if (arEntry.ExtraCharge > 0.f)
{
ExtraCharge* pExtraCharge = Memory::Allocate<ExtraCharge>();
*((uint64_t*)pExtraCharge) = 0x141623AB0;
pExtraCharge->fCharge = arEntry.ExtraCharge;
pExtraData->Add(ExtraData::Charge, pExtraCharge);
}
Expand All @@ -185,6 +187,7 @@ void TESObjectREFR::AddItem(Container::Entry& arEntry) noexcept
if (arEntry.ExtraHealth > 0.f)
{
ExtraHealth* pExtraHealth = Memory::Allocate<ExtraHealth>();
*((uint64_t*)pExtraHealth) = 0x141623A50;
pExtraHealth->fHealth = arEntry.ExtraHealth;
pExtraData->Add(ExtraData::Health, pExtraHealth);
}
Expand All @@ -209,13 +212,15 @@ void TESObjectREFR::AddItem(Container::Entry& arEntry) noexcept
if (arEntry.ExtraSoulLevel > 0 && arEntry.ExtraSoulLevel <= 5)
{
ExtraSoul* pExtraSoul = Memory::Allocate<ExtraSoul>();
*((uint64_t*)pExtraSoul) = 0x141627220;
pExtraSoul->cSoul = static_cast<SOUL_LEVEL>(arEntry.ExtraSoulLevel);
pExtraData->Add(ExtraData::Soul, pExtraSoul);
}

if (!arEntry.ExtraTextDisplayName.empty())
{
ExtraTextDisplayData* pExtraText = Memory::Allocate<ExtraTextDisplayData>();
*((uint64_t*)pExtraText) = 0x1416244D0;
pExtraText->DisplayName = arEntry.ExtraTextDisplayName.c_str();
pExtraData->Add(ExtraData::TextDisplayData, pExtraText);
}
Expand Down
2 changes: 1 addition & 1 deletion Code/client/Games/Skyrim/TESObjectREFR.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ struct TESObjectREFR : TESForm
Lock* CreateLock() noexcept;
void LockChange() noexcept;

void AddItem(Container::Entry& arEntry) noexcept;
void AddItem(const Container::Entry& arEntry) noexcept;

BSHandleRefObject handleRefObject;
uintptr_t unk1C;
Expand Down
3 changes: 2 additions & 1 deletion Code/client/Services/Generic/TestService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ void __declspec(noinline) TestService::PlaceActorInWorld() noexcept
auto pActor = Actor::Create(pPlayerBaseForm);

//pActor->SetInventory(PlayerCharacter::Get()->GetInventory());
pActor->SetFullContainer(PlayerCharacter::Get()->GetFullContainer());

m_actors.emplace_back(pActor);
}
Expand Down Expand Up @@ -183,6 +184,7 @@ void TestService::OnUpdate(const UpdateEvent& acUpdateEvent) noexcept
{
s_f8Pressed = true;

/*
auto fullContainer = PlayerCharacter::Get()->GetFullContainer();
spdlog::info("Full container entries: {}", fullContainer.Entries.size());
Expand All @@ -195,7 +197,6 @@ void TestService::OnUpdate(const UpdateEvent& acUpdateEvent) noexcept
}
}
/*
auto* pActor = (Actor*)TESForm::GetById(0xFF000DA5);
pActor->SetWeaponDrawnEx(true);
Expand Down

0 comments on commit 1110e42

Please sign in to comment.