perf: Loot memory savings (#1165)

* Move away from constructor queries

Fix up other large tables to have proper backup lookups

Revert "idk im just dumb ig"

This reverts commit 5d5be5df53b8959b42b291613d7db749a65a3585.

idk im just dumb ig

* Fix slow components registry lookup

* add define for cdclient cache all

* Huge loot namespace rework

- Remove all excess memory usage
- do not cache components registry
- cache loot matrices on startup of the destroyable component
- convert loot singleton class to a namespace
- rework loot cdclient tables to operate closer to how someone would actually use them (basically doing the previous LootGenerator::LootGenerator caching but in those tables)
- Memory usage reduced by 10%+ across the board

* cache rebuild matrix

* Database: move reading to own function

Also change name of cache to PascalCase

* Database: Move common function rading
This commit is contained in:
David Markowitz
2023-10-09 13:33:22 -07:00
committed by GitHub
parent e96fd56fbd
commit c6087ce77a
30 changed files with 201 additions and 339 deletions

View File

@@ -377,6 +377,7 @@ void Entity::Initialize() {
comp->SetIsSmashable(destCompData[0].isSmashable);
comp->SetLootMatrixID(destCompData[0].LootMatrixIndex);
Loot::CacheMatrix(destCompData[0].LootMatrixIndex);
// Now get currency information
uint32_t npcMinLevel = destCompData[0].level;
@@ -565,6 +566,7 @@ void Entity::Initialize() {
if (activityID > 0) {
comp->SetActivityId(activityID);
Loot::CacheMatrix(activityID);
}
const auto compTime = GetVar<float>(u"compTime");

View File

@@ -735,18 +735,18 @@ void DestroyableComponent::Smash(const LWOOBJID source, const eKillType killType
auto* member = Game::entityManager->GetEntity(specificOwner);
if (member) LootGenerator::Instance().DropLoot(member, m_Parent, lootMatrixId, GetMinCoins(), GetMaxCoins());
if (member) Loot::DropLoot(member, m_Parent, lootMatrixId, GetMinCoins(), GetMaxCoins());
} else {
for (const auto memberId : team->members) { // Free for all
auto* member = Game::entityManager->GetEntity(memberId);
if (member == nullptr) continue;
LootGenerator::Instance().DropLoot(member, m_Parent, lootMatrixId, GetMinCoins(), GetMaxCoins());
Loot::DropLoot(member, m_Parent, lootMatrixId, GetMinCoins(), GetMaxCoins());
}
}
} else { // drop loot for non team user
LootGenerator::Instance().DropLoot(owner, m_Parent, GetLootMatrixID(), GetMinCoins(), GetMaxCoins());
Loot::DropLoot(owner, m_Parent, GetLootMatrixID(), GetMinCoins(), GetMaxCoins());
}
}
} else {
@@ -764,7 +764,7 @@ void DestroyableComponent::Smash(const LWOOBJID source, const eKillType killType
coinsTotal -= coinsToLose;
LootGenerator::Instance().DropLoot(m_Parent, m_Parent, -1, coinsToLose, coinsToLose);
Loot::DropLoot(m_Parent, m_Parent, -1, coinsToLose, coinsToLose);
character->SetCoins(coinsTotal, eLootSourceType::PICKUP);
}
}

View File

@@ -388,7 +388,7 @@ void RacingControlComponent::HandleMessageBoxResponse(Entity* player, int32_t bu
}
const auto score = playersRating * 10 + data->finished;
LootGenerator::Instance().GiveActivityLoot(player, m_Parent, m_ActivityID, score);
Loot::GiveActivityLoot(player, m_Parent, m_ActivityID, score);
// Giving rewards
GameMessages::SendNotifyRacingClient(

View File

@@ -486,7 +486,7 @@ void RebuildComponent::CompleteRebuild(Entity* user) {
auto* missionComponent = builder->GetComponent<MissionComponent>();
if (missionComponent) missionComponent->Progress(eMissionTaskType::ACTIVITY, m_ActivityId);
}
LootGenerator::Instance().DropActivityLoot(builder, m_Parent, m_ActivityId, 1);
Loot::DropActivityLoot(builder, m_Parent, m_ActivityId, 1);
}
// Notify scripts

View File

@@ -576,7 +576,7 @@ void ActivityInstance::RewardParticipant(Entity* participant) {
maxCoins = currencyTable[0].maxvalue;
}
LootGenerator::Instance().DropLoot(participant, m_Parent, activityRewards[0].LootMatrixIndex, minCoins, maxCoins);
Loot::DropLoot(participant, m_Parent, activityRewards[0].LootMatrixIndex, minCoins, maxCoins);
}
}

View File

@@ -42,7 +42,7 @@ void VendorComponent::RefreshInventory(bool isCreation) {
}
auto* lootMatrixTable = CDClientManager::Instance().GetTable<CDLootMatrixTable>();
const auto lootMatrices = lootMatrixTable->Query([=](CDLootMatrix entry) { return (entry.LootMatrixIndex == m_LootMatrixID); });
const auto& lootMatrices = lootMatrixTable->GetMatrix(m_LootMatrixID);
if (lootMatrices.empty()) return;
@@ -51,8 +51,7 @@ void VendorComponent::RefreshInventory(bool isCreation) {
auto* compRegistryTable = CDClientManager::Instance().GetTable<CDComponentsRegistryTable>();
for (const auto& lootMatrix : lootMatrices) {
int lootTableID = lootMatrix.LootTableIndex;
auto vendorItems = lootTableTable->Query([=](CDLootTable entry) { return (entry.LootTableIndex == lootTableID); });
auto vendorItems = lootTableTable->GetTable(lootMatrix.LootTableIndex);
if (lootMatrix.maxToDrop == 0 || lootMatrix.minToDrop == 0) {
for (const auto& item : vendorItems) {
if (!m_HasStandardCostItems || !m_HasMultiCostItems) {

View File

@@ -319,7 +319,7 @@ void Item::UseNonEquip(Item* item) {
// Roll the loot for all the packages then see if it all fits. If it fits, give it to the player, otherwise don't.
std::unordered_map<LOT, int32_t> rolledLoot{};
for (auto& pack : packages) {
auto thisPackage = LootGenerator::Instance().RollLootMatrix(entityParent, pack.LootMatrixIndex);
auto thisPackage = Loot::RollLootMatrix(entityParent, pack.LootMatrixIndex);
for (auto& loot : thisPackage) {
// If we already rolled this lot, add it to the existing one, otherwise create a new entry.
auto existingLoot = rolledLoot.find(loot.first);
@@ -331,7 +331,7 @@ void Item::UseNonEquip(Item* item) {
}
}
if (playerInventoryComponent->HasSpaceForLoot(rolledLoot)) {
LootGenerator::Instance().GiveLoot(playerInventoryComponent->GetParent(), rolledLoot, eLootSourceType::CONSUMPTION);
Loot::GiveLoot(playerInventoryComponent->GetParent(), rolledLoot, eLootSourceType::CONSUMPTION);
item->SetCount(item->GetCount() - 1);
} else {
success = false;

View File

@@ -1,7 +1,8 @@
#include <algorithm>
#include "Loot.h"
#include <algorithm>
#include <unordered_set>
#include "CDComponentsRegistryTable.h"
#include "CDItemComponentTable.h"
#include "CDLootMatrixTable.h"
@@ -18,148 +19,60 @@
#include "eMissionState.h"
#include "eReplicaComponentType.h"
LootGenerator::LootGenerator() {
CDLootTableTable* lootTableTable = CDClientManager::Instance().GetTable<CDLootTableTable>();
namespace {
std::unordered_set<uint32_t> CachedMatrices;
}
void Loot::CacheMatrix(uint32_t matrixIndex) {
if (CachedMatrices.find(matrixIndex) != CachedMatrices.end()) {
return;
}
CachedMatrices.insert(matrixIndex);
CDComponentsRegistryTable* componentsRegistryTable = CDClientManager::Instance().GetTable<CDComponentsRegistryTable>();
CDItemComponentTable* itemComponentTable = CDClientManager::Instance().GetTable<CDItemComponentTable>();
CDLootMatrixTable* lootMatrixTable = CDClientManager::Instance().GetTable<CDLootMatrixTable>();
CDLootTableTable* lootTableTable = CDClientManager::Instance().GetTable<CDLootTableTable>();
CDRarityTableTable* rarityTableTable = CDClientManager::Instance().GetTable<CDRarityTableTable>();
// ==============================
// Cache Item Rarities
// ==============================
const auto& matrix = lootMatrixTable->GetMatrix(matrixIndex);
std::vector<uint32_t> uniqueItems;
for (const CDLootTable& loot : lootTableTable->GetEntries()) {
uniqueItems.push_back(loot.itemid);
}
// filter out duplicates
std::sort(uniqueItems.begin(), uniqueItems.end());
uniqueItems.erase(std::unique(uniqueItems.begin(), uniqueItems.end()), uniqueItems.end());
for (const uint32_t itemID : uniqueItems) {
uint32_t itemComponentID = componentsRegistryTable->GetByIDAndType(itemID, eReplicaComponentType::ITEM);
const CDItemComponent& item = itemComponentTable->GetItemComponentByID(itemComponentID);
m_ItemRarities.insert({ itemID, item.rarity });
}
// ==============================
// Cache Rarity Tables
// ==============================
std::vector<uint32_t> uniqueRarityIndices;
for (const CDRarityTable& rarity : rarityTableTable->GetEntries()) {
uniqueRarityIndices.push_back(rarity.RarityTableIndex);
}
// filter out duplicates
std::sort(uniqueRarityIndices.begin(), uniqueRarityIndices.end());
uniqueRarityIndices.erase(std::unique(uniqueRarityIndices.begin(), uniqueRarityIndices.end()), uniqueRarityIndices.end());
for (const uint32_t index : uniqueRarityIndices) {
std::vector<CDRarityTable> table = rarityTableTable->Query([index](const CDRarityTable& entry) { return entry.RarityTableIndex == index; });
RarityTable rarityTable;
for (const CDRarityTable& entry : table) {
RarityTableEntry rarity{ entry.rarity, entry.randmax };
rarityTable.push_back(rarity);
for (const auto& entry : matrix) {
const auto& lootTable = lootTableTable->GetTable(entry.LootTableIndex);
const auto& rarityTable = rarityTableTable->GetRarityTable(entry.RarityTableIndex);
for (const auto& loot : lootTable) {
uint32_t itemComponentId = componentsRegistryTable->GetByIDAndType(loot.itemid, eReplicaComponentType::ITEM);
uint32_t rarity = itemComponentTable->GetItemComponentByID(itemComponentId).rarity;
}
// sort in descending order based on randMax
std::sort(rarityTable.begin(), rarityTable.end(), [](const RarityTableEntry& x, const RarityTableEntry& y) { return x.randMax > y.randMax; });
m_RarityTables.insert({ index, rarityTable });
}
// ==============================
// Cache Loot Matrices
// ==============================
std::vector<uint32_t> uniqueMatrixIndices;
for (const CDLootMatrix& matrix : lootMatrixTable->GetEntries()) {
uniqueMatrixIndices.push_back(matrix.LootMatrixIndex);
}
// filter out duplicates
std::sort(uniqueMatrixIndices.begin(), uniqueMatrixIndices.end());
uniqueMatrixIndices.erase(std::unique(uniqueMatrixIndices.begin(), uniqueMatrixIndices.end()), uniqueMatrixIndices.end());
for (const uint32_t index : uniqueMatrixIndices) {
std::vector<CDLootMatrix> matrix = lootMatrixTable->Query([index](const CDLootMatrix& entry) { return entry.LootMatrixIndex == index; });
LootMatrix lootMatrix;
for (const CDLootMatrix& entry : matrix) {
LootMatrixEntry matrixEntry{ entry.LootTableIndex, entry.RarityTableIndex, entry.percent, entry.minToDrop, entry.maxToDrop };
lootMatrix.push_back(matrixEntry);
}
m_LootMatrices.insert({ index, lootMatrix });
}
// ==============================
// Cache Loot Tables
// ==============================
std::vector<uint32_t> uniqueTableIndices;
for (const CDLootTable& entry : lootTableTable->GetEntries()) {
uniqueTableIndices.push_back(entry.LootTableIndex);
}
// filter out duplicates
std::sort(uniqueTableIndices.begin(), uniqueTableIndices.end());
uniqueTableIndices.erase(std::unique(uniqueTableIndices.begin(), uniqueTableIndices.end()), uniqueTableIndices.end());
for (const uint32_t index : uniqueTableIndices) {
std::vector<CDLootTable> entries = lootTableTable->Query([index](const CDLootTable& entry) { return entry.LootTableIndex == index; });
LootTable lootTable;
for (const CDLootTable& entry : entries) {
LootTableEntry tableEntry{ (LOT)entry.itemid, entry.MissionDrop };
lootTable.push_back(tableEntry);
}
// sort by item rarity descending
std::sort(lootTable.begin(), lootTable.end(), [&](const LootTableEntry& x, const LootTableEntry& y) {
return m_ItemRarities[x.itemID] > m_ItemRarities[y.itemID];
});
m_LootTables.insert({ index, lootTable });
}
}
std::unordered_map<LOT, int32_t> LootGenerator::RollLootMatrix(Entity* player, uint32_t matrixIndex) {
std::unordered_map<LOT, int32_t> Loot::RollLootMatrix(Entity* player, uint32_t matrixIndex) {
CDComponentsRegistryTable* componentsRegistryTable = CDClientManager::Instance().GetTable<CDComponentsRegistryTable>();
CDItemComponentTable* itemComponentTable = CDClientManager::Instance().GetTable<CDItemComponentTable>();
CDLootMatrixTable* lootMatrixTable = CDClientManager::Instance().GetTable<CDLootMatrixTable>();
CDLootTableTable* lootTableTable = CDClientManager::Instance().GetTable<CDLootTableTable>();
CDRarityTableTable* rarityTableTable = CDClientManager::Instance().GetTable<CDRarityTableTable>();
auto* missionComponent = player->GetComponent<MissionComponent>();
std::unordered_map<LOT, int32_t> drops;
if (missionComponent == nullptr) {
return drops;
}
if (missionComponent == nullptr) return drops;
const LootMatrix& matrix = m_LootMatrices[matrixIndex];
const auto& matrix = lootMatrixTable->GetMatrix(matrixIndex);
for (const LootMatrixEntry& entry : matrix) {
if (GeneralUtils::GenerateRandomNumber<float>(0, 1) < entry.percent) {
const LootTable& lootTable = m_LootTables[entry.lootTableIndex];
const RarityTable& rarityTable = m_RarityTables[entry.rarityTableIndex];
for (const auto& entry : matrix) {
if (GeneralUtils::GenerateRandomNumber<float>(0, 1) < entry.percent) { // GetTable
const auto& lootTable = lootTableTable->GetTable(entry.LootTableIndex);
const auto& rarityTable = rarityTableTable->GetRarityTable(entry.RarityTableIndex);
uint32_t dropCount = GeneralUtils::GenerateRandomNumber<uint32_t>(entry.minDrop, entry.maxDrop);
uint32_t dropCount = GeneralUtils::GenerateRandomNumber<uint32_t>(entry.minToDrop, entry.maxToDrop);
for (uint32_t i = 0; i < dropCount; ++i) {
uint32_t maxRarity = 1;
float rarityRoll = GeneralUtils::GenerateRandomNumber<float>(0, 1);
for (const RarityTableEntry& rarity : rarityTable) {
if (rarity.randMax >= rarityRoll) {
for (const auto& rarity : rarityTable) {
if (rarity.randmax >= rarityRoll) {
maxRarity = rarity.rarity;
} else {
break;
@@ -167,10 +80,11 @@ std::unordered_map<LOT, int32_t> LootGenerator::RollLootMatrix(Entity* player, u
}
bool rarityFound = false;
std::vector<LootTableEntry> possibleDrops;
std::vector<CDLootTable> possibleDrops;
for (const LootTableEntry& loot : lootTable) {
uint32_t rarity = m_ItemRarities[loot.itemID];
for (const auto& loot : lootTable) {
uint32_t itemComponentId = componentsRegistryTable->GetByIDAndType(loot.itemid, eReplicaComponentType::ITEM);
uint32_t rarity = itemComponentTable->GetItemComponentByID(itemComponentId).rarity;
if (rarity == maxRarity) {
possibleDrops.push_back(loot);
@@ -182,32 +96,34 @@ std::unordered_map<LOT, int32_t> LootGenerator::RollLootMatrix(Entity* player, u
}
if (possibleDrops.size() > 0) {
LootTableEntry drop = possibleDrops[GeneralUtils::GenerateRandomNumber<uint32_t>(0, possibleDrops.size() - 1)];
const auto& drop = possibleDrops[GeneralUtils::GenerateRandomNumber<uint32_t>(0, possibleDrops.size() - 1)];
// filter out uneeded mission items
if (drop.isMissionDrop && !missionComponent->RequiresItem(drop.itemID))
if (drop.MissionDrop && !missionComponent->RequiresItem(drop.itemid))
continue;
LOT itemID = drop.itemid;
// convert faction token proxy
if (drop.itemID == 13763) {
if (itemID == 13763) {
if (missionComponent->GetMissionState(545) == eMissionState::COMPLETE)
drop.itemID = 8318; // "Assembly Token"
itemID = 8318; // "Assembly Token"
else if (missionComponent->GetMissionState(556) == eMissionState::COMPLETE)
drop.itemID = 8321; // "Venture League Token"
itemID = 8321; // "Venture League Token"
else if (missionComponent->GetMissionState(567) == eMissionState::COMPLETE)
drop.itemID = 8319; // "Sentinels Token"
itemID = 8319; // "Sentinels Token"
else if (missionComponent->GetMissionState(578) == eMissionState::COMPLETE)
drop.itemID = 8320; // "Paradox Token"
itemID = 8320; // "Paradox Token"
}
if (drop.itemID == 13763) {
if (itemID == 13763) {
continue;
} // check if we aren't in faction
if (drops.find(drop.itemID) == drops.end()) {
drops.insert({ drop.itemID, 1 });
// drops[itemID]++; this should work?
if (drops.find(itemID) == drops.end()) {
drops.insert({ itemID, 1 });
} else {
++drops[drop.itemID];
++drops[itemID];
}
}
}
@@ -217,24 +133,29 @@ std::unordered_map<LOT, int32_t> LootGenerator::RollLootMatrix(Entity* player, u
return drops;
}
std::unordered_map<LOT, int32_t> LootGenerator::RollLootMatrix(uint32_t matrixIndex) {
std::unordered_map<LOT, int32_t> Loot::RollLootMatrix(uint32_t matrixIndex) {
CDComponentsRegistryTable* componentsRegistryTable = CDClientManager::Instance().GetTable<CDComponentsRegistryTable>();
CDItemComponentTable* itemComponentTable = CDClientManager::Instance().GetTable<CDItemComponentTable>();
CDLootMatrixTable* lootMatrixTable = CDClientManager::Instance().GetTable<CDLootMatrixTable>();
CDLootTableTable* lootTableTable = CDClientManager::Instance().GetTable<CDLootTableTable>();
CDRarityTableTable* rarityTableTable = CDClientManager::Instance().GetTable<CDRarityTableTable>();
std::unordered_map<LOT, int32_t> drops;
const LootMatrix& matrix = m_LootMatrices[matrixIndex];
const auto& matrix = lootMatrixTable->GetMatrix(matrixIndex);
for (const LootMatrixEntry& entry : matrix) {
for (const auto& entry : matrix) {
if (GeneralUtils::GenerateRandomNumber<float>(0, 1) < entry.percent) {
const LootTable& lootTable = m_LootTables[entry.lootTableIndex];
const RarityTable& rarityTable = m_RarityTables[entry.rarityTableIndex];
const auto& lootTable = lootTableTable->GetTable(entry.LootTableIndex);
const auto& rarityTable = rarityTableTable->GetRarityTable(entry.RarityTableIndex);
uint32_t dropCount = GeneralUtils::GenerateRandomNumber<uint32_t>(entry.minDrop, entry.maxDrop);
uint32_t dropCount = GeneralUtils::GenerateRandomNumber<uint32_t>(entry.minToDrop, entry.maxToDrop);
for (uint32_t i = 0; i < dropCount; ++i) {
uint32_t maxRarity = 1;
float rarityRoll = GeneralUtils::GenerateRandomNumber<float>(0, 1);
for (const RarityTableEntry& rarity : rarityTable) {
if (rarity.randMax >= rarityRoll) {
for (const auto& rarity : rarityTable) {
if (rarity.randmax >= rarityRoll) {
maxRarity = rarity.rarity;
} else {
break;
@@ -242,10 +163,11 @@ std::unordered_map<LOT, int32_t> LootGenerator::RollLootMatrix(uint32_t matrixIn
}
bool rarityFound = false;
std::vector<LootTableEntry> possibleDrops;
std::vector<CDLootTable> possibleDrops;
for (const LootTableEntry& loot : lootTable) {
uint32_t rarity = m_ItemRarities[loot.itemID];
for (const auto& loot : lootTable) {
uint32_t itemComponentId = componentsRegistryTable->GetByIDAndType(loot.itemid, eReplicaComponentType::ITEM);
uint32_t rarity = itemComponentTable->GetItemComponentByID(itemComponentId).rarity;
if (rarity == maxRarity) {
possibleDrops.push_back(loot);
@@ -257,12 +179,12 @@ std::unordered_map<LOT, int32_t> LootGenerator::RollLootMatrix(uint32_t matrixIn
}
if (possibleDrops.size() > 0) {
const LootTableEntry& drop = possibleDrops[GeneralUtils::GenerateRandomNumber<uint32_t>(0, possibleDrops.size() - 1)];
const auto& drop = possibleDrops[GeneralUtils::GenerateRandomNumber<uint32_t>(0, possibleDrops.size() - 1)];
if (drops.find(drop.itemID) == drops.end()) {
drops.insert({ drop.itemID, 1 });
if (drops.find(drop.itemid) == drops.end()) {
drops.insert({ drop.itemid, 1 });
} else {
++drops[drop.itemID];
++drops[drop.itemid];
}
}
}
@@ -272,7 +194,7 @@ std::unordered_map<LOT, int32_t> LootGenerator::RollLootMatrix(uint32_t matrixIn
return drops;
}
void LootGenerator::GiveLoot(Entity* player, uint32_t matrixIndex, eLootSourceType lootSourceType) {
void Loot::GiveLoot(Entity* player, uint32_t matrixIndex, eLootSourceType lootSourceType) {
player = player->GetOwner(); // If the owner is overwritten, we collect that here
std::unordered_map<LOT, int32_t> result = RollLootMatrix(player, matrixIndex);
@@ -280,7 +202,7 @@ void LootGenerator::GiveLoot(Entity* player, uint32_t matrixIndex, eLootSourceTy
GiveLoot(player, result, lootSourceType);
}
void LootGenerator::GiveLoot(Entity* player, std::unordered_map<LOT, int32_t>& result, eLootSourceType lootSourceType) {
void Loot::GiveLoot(Entity* player, std::unordered_map<LOT, int32_t>& result, eLootSourceType lootSourceType) {
player = player->GetOwner(); // if the owner is overwritten, we collect that here
auto* inventoryComponent = player->GetComponent<InventoryComponent>();
@@ -293,7 +215,7 @@ void LootGenerator::GiveLoot(Entity* player, std::unordered_map<LOT, int32_t>& r
}
}
void LootGenerator::GiveActivityLoot(Entity* player, Entity* source, uint32_t activityID, int32_t rating) {
void Loot::GiveActivityLoot(Entity* player, Entity* source, uint32_t activityID, int32_t rating) {
CDActivityRewardsTable* activityRewardsTable = CDClientManager::Instance().GetTable<CDActivityRewardsTable>();
std::vector<CDActivityRewards> activityRewards = activityRewardsTable->Query([activityID](CDActivityRewards entry) { return (entry.objectTemplate == activityID); });
@@ -327,7 +249,7 @@ void LootGenerator::GiveActivityLoot(Entity* player, Entity* source, uint32_t ac
character->SetCoins(character->GetCoins() + coins, eLootSourceType::ACTIVITY);
}
void LootGenerator::DropLoot(Entity* player, Entity* killedObject, uint32_t matrixIndex, uint32_t minCoins, uint32_t maxCoins) {
void Loot::DropLoot(Entity* player, Entity* killedObject, uint32_t matrixIndex, uint32_t minCoins, uint32_t maxCoins) {
player = player->GetOwner(); // if the owner is overwritten, we collect that here
auto* inventoryComponent = player->GetComponent<InventoryComponent>();
@@ -340,7 +262,7 @@ void LootGenerator::DropLoot(Entity* player, Entity* killedObject, uint32_t matr
DropLoot(player, killedObject, result, minCoins, maxCoins);
}
void LootGenerator::DropLoot(Entity* player, Entity* killedObject, std::unordered_map<LOT, int32_t>& result, uint32_t minCoins, uint32_t maxCoins) {
void Loot::DropLoot(Entity* player, Entity* killedObject, std::unordered_map<LOT, int32_t>& result, uint32_t minCoins, uint32_t maxCoins) {
player = player->GetOwner(); // if the owner is overwritten, we collect that here
auto* inventoryComponent = player->GetComponent<InventoryComponent>();
@@ -363,7 +285,7 @@ void LootGenerator::DropLoot(Entity* player, Entity* killedObject, std::unordere
GameMessages::SendDropClientLoot(player, source, LOT_NULL, coins, spawnPosition);
}
void LootGenerator::DropActivityLoot(Entity* player, Entity* source, uint32_t activityID, int32_t rating) {
void Loot::DropActivityLoot(Entity* player, Entity* source, uint32_t activityID, int32_t rating) {
CDActivityRewardsTable* activityRewardsTable = CDClientManager::Instance().GetTable<CDActivityRewardsTable>();
std::vector<CDActivityRewards> activityRewards = activityRewardsTable->Query([activityID](CDActivityRewards entry) { return (entry.objectTemplate == activityID); });

View File

@@ -2,61 +2,23 @@
#include "dCommonVars.h"
#include <unordered_map>
#include "Singleton.h"
#include <vector>
class Entity;
struct RarityTableEntry {
uint32_t rarity;
float randMax;
};
typedef std::vector<RarityTableEntry> RarityTable;
struct LootMatrixEntry {
uint32_t lootTableIndex;
uint32_t rarityTableIndex;
float percent;
uint32_t minDrop;
uint32_t maxDrop;
};
typedef std::vector<LootMatrixEntry> LootMatrix;
struct LootTableEntry {
LOT itemID;
bool isMissionDrop;
};
typedef std::vector<LootTableEntry> LootTable;
// used for glue code with Entity and Player classes
namespace Loot {
struct Info {
LWOOBJID id;
LOT lot;
uint32_t count;
LWOOBJID id = 0;
LOT lot = 0;
uint32_t count = 0;
};
}
class LootGenerator : public Singleton<LootGenerator> {
public:
LootGenerator();
std::unordered_map<LOT, int32_t> RollLootMatrix(Entity* player, uint32_t matrixIndex);
std::unordered_map<LOT, int32_t> RollLootMatrix(uint32_t matrixIndex);
void CacheMatrix(const uint32_t matrixIndex);
void GiveLoot(Entity* player, uint32_t matrixIndex, eLootSourceType lootSourceType = eLootSourceType::NONE);
void GiveLoot(Entity* player, std::unordered_map<LOT, int32_t>& result, eLootSourceType lootSourceType = eLootSourceType::NONE);
void GiveActivityLoot(Entity* player, Entity* source, uint32_t activityID, int32_t rating = 0);
void DropLoot(Entity* player, Entity* killedObject, uint32_t matrixIndex, uint32_t minCoins, uint32_t maxCoins);
void DropLoot(Entity* player, Entity* killedObject, std::unordered_map<LOT, int32_t>& result, uint32_t minCoins, uint32_t maxCoins);
void DropActivityLoot(Entity* player, Entity* source, uint32_t activityID, int32_t rating = 0);
private:
std::unordered_map<uint32_t, uint8_t> m_ItemRarities;
std::unordered_map<uint32_t, RarityTable> m_RarityTables;
std::unordered_map<uint32_t, LootMatrix> m_LootMatrices;
std::unordered_map<uint32_t, LootTable> m_LootTables;
};

View File

@@ -1790,7 +1790,7 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit
for (uint32_t i = 0; i < loops; i++) {
while (true) {
auto lootRoll = LootGenerator::Instance().RollLootMatrix(lootMatrixIndex);
auto lootRoll = Loot::RollLootMatrix(lootMatrixIndex);
totalRuns += 1;
bool doBreak = false;
for (const auto& kv : lootRoll) {