DarkflameServer/dGame/dUtilities/Loot.cpp

316 lines
12 KiB
C++
Raw Permalink Normal View History

#include "Loot.h"
#include <algorithm>
#include <unordered_set>
#include "CDComponentsRegistryTable.h"
#include "CDItemComponentTable.h"
#include "CDLootMatrixTable.h"
#include "CDLootTableTable.h"
#include "CDRarityTableTable.h"
#include "CDActivityRewardsTable.h"
#include "CDCurrencyTableTable.h"
#include "Character.h"
#include "Entity.h"
#include "GameMessages.h"
#include "GeneralUtils.h"
#include "InventoryComponent.h"
#include "MissionComponent.h"
#include "eMissionState.h"
#include "eReplicaComponentType.h"
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>();
const auto& matrix = lootMatrixTable->GetMatrix(matrixIndex);
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;
2022-07-28 13:39:57 +00:00
}
}
}
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>();
2022-07-28 13:39:57 +00:00
auto* missionComponent = player->GetComponent<MissionComponent>();
std::unordered_map<LOT, int32_t> drops;
if (missionComponent == nullptr) return drops;
2022-07-28 13:39:57 +00:00
const auto& matrix = lootMatrixTable->GetMatrix(matrixIndex);
2022-07-28 13:39:57 +00:00
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);
2022-07-28 13:39:57 +00:00
uint32_t dropCount = GeneralUtils::GenerateRandomNumber<uint32_t>(entry.minToDrop, entry.maxToDrop);
2022-07-28 13:39:57 +00:00
for (uint32_t i = 0; i < dropCount; ++i) {
uint32_t maxRarity = 1;
float rarityRoll = GeneralUtils::GenerateRandomNumber<float>(0, 1);
for (const auto& rarity : rarityTable) {
if (rarity.randmax >= rarityRoll) {
2022-07-28 13:39:57 +00:00
maxRarity = rarity.rarity;
} else {
break;
}
}
bool rarityFound = false;
std::vector<CDLootTable> possibleDrops;
2022-07-28 13:39:57 +00:00
for (const auto& loot : lootTable) {
uint32_t itemComponentId = componentsRegistryTable->GetByIDAndType(loot.itemid, eReplicaComponentType::ITEM);
uint32_t rarity = itemComponentTable->GetItemComponentByID(itemComponentId).rarity;
2022-07-28 13:39:57 +00:00
if (rarity == maxRarity) {
possibleDrops.push_back(loot);
rarityFound = true;
} else if (rarity < maxRarity && !rarityFound) {
possibleDrops.push_back(loot);
maxRarity = rarity;
}
}
if (possibleDrops.size() > 0) {
const auto& drop = possibleDrops[GeneralUtils::GenerateRandomNumber<uint32_t>(0, possibleDrops.size() - 1)];
2022-07-28 13:39:57 +00:00
// filter out uneeded mission items
if (drop.MissionDrop && !missionComponent->RequiresItem(drop.itemid))
2022-07-28 13:39:57 +00:00
continue;
LOT itemID = drop.itemid;
2022-07-28 13:39:57 +00:00
// convert faction token proxy
if (itemID == 13763) {
if (missionComponent->GetMissionState(545) == eMissionState::COMPLETE)
itemID = 8318; // "Assembly Token"
else if (missionComponent->GetMissionState(556) == eMissionState::COMPLETE)
itemID = 8321; // "Venture League Token"
else if (missionComponent->GetMissionState(567) == eMissionState::COMPLETE)
itemID = 8319; // "Sentinels Token"
else if (missionComponent->GetMissionState(578) == eMissionState::COMPLETE)
itemID = 8320; // "Paradox Token"
2022-07-28 13:39:57 +00:00
}
if (itemID == 13763) {
2022-07-28 13:39:57 +00:00
continue;
} // check if we aren't in faction
// drops[itemID]++; this should work?
if (drops.find(itemID) == drops.end()) {
drops.insert({ itemID, 1 });
2022-07-28 13:39:57 +00:00
} else {
++drops[itemID];
2022-07-28 13:39:57 +00:00
}
}
}
}
}
return drops;
}
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>();
2022-07-28 13:39:57 +00:00
std::unordered_map<LOT, int32_t> drops;
const auto& matrix = lootMatrixTable->GetMatrix(matrixIndex);
2022-07-28 13:39:57 +00:00
for (const auto& entry : matrix) {
2022-07-28 13:39:57 +00:00
if (GeneralUtils::GenerateRandomNumber<float>(0, 1) < entry.percent) {
const auto& lootTable = lootTableTable->GetTable(entry.LootTableIndex);
const auto& rarityTable = rarityTableTable->GetRarityTable(entry.RarityTableIndex);
2022-07-28 13:39:57 +00:00
uint32_t dropCount = GeneralUtils::GenerateRandomNumber<uint32_t>(entry.minToDrop, entry.maxToDrop);
2022-07-28 13:39:57 +00:00
for (uint32_t i = 0; i < dropCount; ++i) {
uint32_t maxRarity = 1;
float rarityRoll = GeneralUtils::GenerateRandomNumber<float>(0, 1);
for (const auto& rarity : rarityTable) {
if (rarity.randmax >= rarityRoll) {
2022-07-28 13:39:57 +00:00
maxRarity = rarity.rarity;
} else {
break;
}
}
bool rarityFound = false;
std::vector<CDLootTable> possibleDrops;
2022-07-28 13:39:57 +00:00
for (const auto& loot : lootTable) {
uint32_t itemComponentId = componentsRegistryTable->GetByIDAndType(loot.itemid, eReplicaComponentType::ITEM);
uint32_t rarity = itemComponentTable->GetItemComponentByID(itemComponentId).rarity;
2022-07-28 13:39:57 +00:00
if (rarity == maxRarity) {
possibleDrops.push_back(loot);
rarityFound = true;
} else if (rarity < maxRarity && !rarityFound) {
possibleDrops.push_back(loot);
maxRarity = rarity;
}
}
if (possibleDrops.size() > 0) {
const auto& drop = possibleDrops[GeneralUtils::GenerateRandomNumber<uint32_t>(0, possibleDrops.size() - 1)];
2022-07-28 13:39:57 +00:00
if (drops.find(drop.itemid) == drops.end()) {
drops.insert({ drop.itemid, 1 });
2022-07-28 13:39:57 +00:00
} else {
++drops[drop.itemid];
2022-07-28 13:39:57 +00:00
}
}
}
}
}
return drops;
}
void Loot::GiveLoot(Entity* player, uint32_t matrixIndex, eLootSourceType lootSourceType) {
2022-07-28 13:39:57 +00:00
player = player->GetOwner(); // If the owner is overwritten, we collect that here
2022-07-28 13:39:57 +00:00
std::unordered_map<LOT, int32_t> result = RollLootMatrix(player, matrixIndex);
2022-07-28 13:39:57 +00:00
GiveLoot(player, result, lootSourceType);
}
void Loot::GiveLoot(Entity* player, std::unordered_map<LOT, int32_t>& result, eLootSourceType lootSourceType) {
2022-07-28 13:39:57 +00:00
player = player->GetOwner(); // if the owner is overwritten, we collect that here
2022-07-28 13:39:57 +00:00
auto* inventoryComponent = player->GetComponent<InventoryComponent>();
2022-07-28 13:39:57 +00:00
if (!inventoryComponent)
return;
2022-07-28 13:39:57 +00:00
for (const auto& pair : result) {
inventoryComponent->AddItem(pair.first, pair.second, lootSourceType);
}
}
void Loot::GiveActivityLoot(Entity* player, Entity* source, uint32_t activityID, int32_t rating) {
CDActivityRewardsTable* activityRewardsTable = CDClientManager::Instance().GetTable<CDActivityRewardsTable>();
2022-07-28 13:39:57 +00:00
std::vector<CDActivityRewards> activityRewards = activityRewardsTable->Query([activityID](CDActivityRewards entry) { return (entry.objectTemplate == activityID); });
2022-07-28 13:39:57 +00:00
const CDActivityRewards* selectedReward = nullptr;
for (const auto& activityReward : activityRewards) {
if (activityReward.activityRating <= rating && (selectedReward == nullptr || activityReward.activityRating > selectedReward->activityRating)) {
selectedReward = &activityReward;
}
}
2022-07-28 13:39:57 +00:00
if (!selectedReward)
return;
2022-07-28 13:39:57 +00:00
uint32_t minCoins = 0;
uint32_t maxCoins = 0;
CDCurrencyTableTable* currencyTableTable = CDClientManager::Instance().GetTable<CDCurrencyTableTable>();
2022-07-28 13:39:57 +00:00
std::vector<CDCurrencyTable> currencyTable = currencyTableTable->Query([selectedReward](CDCurrencyTable entry) { return (entry.currencyIndex == selectedReward->CurrencyIndex && entry.npcminlevel == 1); });
2022-07-28 13:39:57 +00:00
if (currencyTable.size() > 0) {
minCoins = currencyTable[0].minvalue;
maxCoins = currencyTable[0].maxvalue;
}
GiveLoot(player, selectedReward->LootMatrixIndex, eLootSourceType::ACTIVITY);
2022-07-28 13:39:57 +00:00
uint32_t coins = (int)(minCoins + GeneralUtils::GenerateRandomNumber<float>(0, 1) * (maxCoins - minCoins));
2022-07-28 13:39:57 +00:00
auto* character = player->GetCharacter();
character->SetCoins(character->GetCoins() + coins, eLootSourceType::ACTIVITY);
}
void Loot::DropLoot(Entity* player, Entity* killedObject, uint32_t matrixIndex, uint32_t minCoins, uint32_t maxCoins) {
2022-07-28 13:39:57 +00:00
player = player->GetOwner(); // if the owner is overwritten, we collect that here
2022-07-28 13:39:57 +00:00
auto* inventoryComponent = player->GetComponent<InventoryComponent>();
2022-07-28 13:39:57 +00:00
if (!inventoryComponent)
return;
2022-07-28 13:39:57 +00:00
std::unordered_map<LOT, int32_t> result = RollLootMatrix(player, matrixIndex);
2022-07-28 13:39:57 +00:00
DropLoot(player, killedObject, result, minCoins, maxCoins);
}
void Loot::DropLoot(Entity* player, Entity* killedObject, std::unordered_map<LOT, int32_t>& result, uint32_t minCoins, uint32_t maxCoins) {
2022-07-28 13:39:57 +00:00
player = player->GetOwner(); // if the owner is overwritten, we collect that here
2022-07-28 13:39:57 +00:00
auto* inventoryComponent = player->GetComponent<InventoryComponent>();
2022-07-28 13:39:57 +00:00
if (!inventoryComponent)
return;
2022-07-28 13:39:57 +00:00
const auto spawnPosition = killedObject->GetPosition();
2022-07-28 13:39:57 +00:00
const auto source = killedObject->GetObjectID();
2022-07-28 13:39:57 +00:00
for (const auto& pair : result) {
for (int i = 0; i < pair.second; ++i) {
GameMessages::SendDropClientLoot(player, source, pair.first, 0, spawnPosition, 1);
}
}
2022-07-28 13:39:57 +00:00
uint32_t coins = (int)(minCoins + GeneralUtils::GenerateRandomNumber<float>(0, 1) * (maxCoins - minCoins));
2022-07-28 13:39:57 +00:00
GameMessages::SendDropClientLoot(player, source, LOT_NULL, coins, spawnPosition);
}
void Loot::DropActivityLoot(Entity* player, Entity* source, uint32_t activityID, int32_t rating) {
CDActivityRewardsTable* activityRewardsTable = CDClientManager::Instance().GetTable<CDActivityRewardsTable>();
2022-07-28 13:39:57 +00:00
std::vector<CDActivityRewards> activityRewards = activityRewardsTable->Query([activityID](CDActivityRewards entry) { return (entry.objectTemplate == activityID); });
2022-07-28 13:39:57 +00:00
const CDActivityRewards* selectedReward = nullptr;
for (const auto& activityReward : activityRewards) {
if (activityReward.activityRating <= rating && (selectedReward == nullptr || activityReward.activityRating > selectedReward->activityRating)) {
selectedReward = &activityReward;
}
}
2022-07-28 13:39:57 +00:00
if (selectedReward == nullptr) {
return;
}
2022-07-28 13:39:57 +00:00
uint32_t minCoins = 0;
uint32_t maxCoins = 0;
CDCurrencyTableTable* currencyTableTable = CDClientManager::Instance().GetTable<CDCurrencyTableTable>();
2022-07-28 13:39:57 +00:00
std::vector<CDCurrencyTable> currencyTable = currencyTableTable->Query([selectedReward](CDCurrencyTable entry) { return (entry.currencyIndex == selectedReward->CurrencyIndex && entry.npcminlevel == 1); });
2022-07-28 13:39:57 +00:00
if (currencyTable.size() > 0) {
minCoins = currencyTable[0].minvalue;
maxCoins = currencyTable[0].maxvalue;
}
2022-07-28 13:39:57 +00:00
DropLoot(player, source, selectedReward->LootMatrixIndex, minCoins, maxCoins);
}