diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 00000000..ae7501e8 --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,33 @@ +name: Cmake CI + +on: + push: + branches: [ main ] + pull_request: + branches: [ main ] + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + with: + submodules: true + - name: cmake + uses: lukka/run-cmake@v10 + with: + configurePreset: 'default' + buildPreset: 'default' + - name: artifacts + uses: actions/upload-artifact@v2 + with: + name: linux-build + path: | + build + !build/CMakeCache.txt + !build/build.ninja + !build/_deps + !build/cmake_install.cmake + !build/*.a diff --git a/CMakePresets.json b/CMakePresets.json new file mode 100644 index 00000000..c892287a --- /dev/null +++ b/CMakePresets.json @@ -0,0 +1,26 @@ +{ + "version": 3, + "cmakeMinimumRequired": { + "major": 3, + "minor": 12, + "patch": 0 + }, + "configurePresets": [ + { + "name": "default", + "displayName": "Default Configure Settings", + "description": "Sets build and install directories", + "binaryDir": "${sourceDir}/build", + "generator": "Unix Makefiles" + } + ], + "buildPresets": [ + { + "name": "default", + "configurePreset": "default", + "displayName": "Default Build", + "description": "Default Build", + "jobs": 2 + } + ] +} diff --git a/dDatabase/Tables/CDLootMatrixTable.cpp b/dDatabase/Tables/CDLootMatrixTable.cpp index 6d5a232d..17fd8313 100644 --- a/dDatabase/Tables/CDLootMatrixTable.cpp +++ b/dDatabase/Tables/CDLootMatrixTable.cpp @@ -57,6 +57,6 @@ std::vector CDLootMatrixTable::Query(std::function CDLootMatrixTable::GetEntries(void) const { +const std::vector& CDLootMatrixTable::GetEntries(void) const { return this->entries; } diff --git a/dDatabase/Tables/CDLootMatrixTable.h b/dDatabase/Tables/CDLootMatrixTable.h index dc9410e9..f306324f 100644 --- a/dDatabase/Tables/CDLootMatrixTable.h +++ b/dDatabase/Tables/CDLootMatrixTable.h @@ -50,7 +50,7 @@ public: /*! \return The entries */ - std::vector GetEntries(void) const; + const std::vector& GetEntries(void) const; }; diff --git a/dDatabase/Tables/CDLootTableTable.cpp b/dDatabase/Tables/CDLootTableTable.cpp index 8f05fb80..62727ade 100644 --- a/dDatabase/Tables/CDLootTableTable.cpp +++ b/dDatabase/Tables/CDLootTableTable.cpp @@ -54,6 +54,6 @@ std::vector CDLootTableTable::Query(std::function CDLootTableTable::GetEntries(void) const { +const std::vector& CDLootTableTable::GetEntries(void) const { return this->entries; } diff --git a/dDatabase/Tables/CDLootTableTable.h b/dDatabase/Tables/CDLootTableTable.h index c210073c..750adcb4 100644 --- a/dDatabase/Tables/CDLootTableTable.h +++ b/dDatabase/Tables/CDLootTableTable.h @@ -46,7 +46,7 @@ public: /*! \return The entries */ - std::vector GetEntries(void) const; + const std::vector& GetEntries(void) const; }; diff --git a/dDatabase/Tables/CDRarityTableTable.cpp b/dDatabase/Tables/CDRarityTableTable.cpp index f5b09b76..da41e3f3 100644 --- a/dDatabase/Tables/CDRarityTableTable.cpp +++ b/dDatabase/Tables/CDRarityTableTable.cpp @@ -52,6 +52,6 @@ std::vector CDRarityTableTable::Query(std::function CDRarityTableTable::GetEntries(void) const { +const std::vector& CDRarityTableTable::GetEntries(void) const { return this->entries; } diff --git a/dDatabase/Tables/CDRarityTableTable.h b/dDatabase/Tables/CDRarityTableTable.h index c2de03a1..0a57e903 100644 --- a/dDatabase/Tables/CDRarityTableTable.h +++ b/dDatabase/Tables/CDRarityTableTable.h @@ -66,7 +66,7 @@ public: /*! \return The entries */ - std::vector GetEntries(void) const; + const std::vector& GetEntries(void) const; }; diff --git a/dGame/dComponents/DestroyableComponent.cpp b/dGame/dComponents/DestroyableComponent.cpp index cd67b845..c84e906a 100644 --- a/dGame/dComponents/DestroyableComponent.cpp +++ b/dGame/dComponents/DestroyableComponent.cpp @@ -783,12 +783,12 @@ void DestroyableComponent::Smash(const LWOOBJID source, const eKillType killType if (member == nullptr) continue; - Loot::DropLoot(member, m_Parent, GetLootMatrixID(), GetMinCoins(), GetMaxCoins()); + LootGenerator::Instance().DropLoot(member, m_Parent, GetLootMatrixID(), GetMinCoins(), GetMaxCoins()); } } else { - Loot::DropLoot(owner, m_Parent, GetLootMatrixID(), GetMinCoins(), GetMaxCoins()); + LootGenerator::Instance().DropLoot(owner, m_Parent, GetLootMatrixID(), GetMinCoins(), GetMaxCoins()); } } } @@ -815,7 +815,7 @@ void DestroyableComponent::Smash(const LWOOBJID source, const eKillType killType coinsTotal -= coinsToLoose; - Loot::DropLoot(m_Parent, m_Parent, -1, coinsToLoose, coinsToLoose); + LootGenerator::Instance().DropLoot(m_Parent, m_Parent, -1, coinsToLoose, coinsToLoose); } character->SetCoins(coinsTotal); diff --git a/dGame/dComponents/RacingControlComponent.cpp b/dGame/dComponents/RacingControlComponent.cpp index e7fd02c8..bba7a892 100644 --- a/dGame/dComponents/RacingControlComponent.cpp +++ b/dGame/dComponents/RacingControlComponent.cpp @@ -317,7 +317,9 @@ void RacingControlComponent::OnRequestDie(Entity *player) { return; } - racingPlayer.smashedTimes++; + if (!racingPlayer.noSmashOnReload) { + racingPlayer.smashedTimes++; + } // Reset player to last checkpoint GameMessages::SendRacingSetPlayerResetInfo( @@ -390,7 +392,7 @@ void RacingControlComponent::HandleMessageBoxResponse(Entity *player, // Calculate the score, different loot depending on player count const auto score = m_LoadedPlayers * 10 + data->finished; - Loot::GiveActivityLoot(player, m_Parent, m_ActivityID, score); + LootGenerator::Instance().GiveActivityLoot(player, m_Parent, m_ActivityID, score); // Giving rewards GameMessages::SendNotifyRacingClient( diff --git a/dGame/dComponents/RebuildComponent.cpp b/dGame/dComponents/RebuildComponent.cpp index d7106228..7d4ae926 100644 --- a/dGame/dComponents/RebuildComponent.cpp +++ b/dGame/dComponents/RebuildComponent.cpp @@ -452,7 +452,7 @@ void RebuildComponent::CompleteRebuild(Entity* user) { missionComponent->Progress(MissionTaskType::MISSION_TASK_TYPE_ACTIVITY, m_ActivityId); } - Loot::DropActivityLoot(builder, m_Parent, m_ActivityId, 1); + LootGenerator::Instance().DropActivityLoot(builder, m_Parent, m_ActivityId, 1); } m_Builder = LWOOBJID_EMPTY; diff --git a/dGame/dComponents/ScriptedActivityComponent.cpp b/dGame/dComponents/ScriptedActivityComponent.cpp index 1dadd205..54aee019 100644 --- a/dGame/dComponents/ScriptedActivityComponent.cpp +++ b/dGame/dComponents/ScriptedActivityComponent.cpp @@ -524,7 +524,7 @@ void ActivityInstance::RewardParticipant(Entity* participant) { maxCoins = currencyTable[0].maxvalue; } - Loot::DropLoot(participant, m_Parent, activityRewards[0].LootMatrixIndex, minCoins, maxCoins); + LootGenerator::Instance().DropLoot(participant, m_Parent, activityRewards[0].LootMatrixIndex, minCoins, maxCoins); } } diff --git a/dGame/dInventory/Item.cpp b/dGame/dInventory/Item.cpp index 97b6e038..20f5321a 100644 --- a/dGame/dInventory/Item.cpp +++ b/dGame/dInventory/Item.cpp @@ -333,14 +333,14 @@ bool Item::UseNonEquip() { std::unordered_map result {}; - Loot::CalculateLootMatrix(pack.LootMatrixIndex, entityParent, result); + result = LootGenerator::Instance().RollLootMatrix(entityParent, pack.LootMatrixIndex); if (!inventory->GetComponent()->HasSpaceForLoot(result)) { return false; } - Loot::GiveLoot(inventory->GetComponent()->GetParent(), result); + LootGenerator::Instance().GiveLoot(inventory->GetComponent()->GetParent(), result); } inventory->GetComponent()->RemoveItem(lot, 1); diff --git a/dGame/dUtilities/Loot.cpp b/dGame/dUtilities/Loot.cpp index 836961bc..4ec056a5 100644 --- a/dGame/dUtilities/Loot.cpp +++ b/dGame/dUtilities/Loot.cpp @@ -1,384 +1,389 @@ -#include "Loot.h" -#include "GameMessages.h" - -#include "CDClientManager.h" -#include "CDLootMatrixTable.h" -#include "CDLootTableTable.h" - -#include "SimplePhysicsComponent.h" -#include "ControllablePhysicsComponent.h" -#include "DestroyableComponent.h" -#include "MissionComponent.h" -#include "CharacterComponent.h" -#include "TeamManager.h" - #include -std::vector Loot::GetLootOfRarity(const std::vector &lootTable, uint32_t rarity) { - std::vector refinedLoot; - for (auto loot : lootTable) { - CDItemComponent item = Inventory::FindItemComponent(loot.itemid); - if (item.rarity == rarity) { - refinedLoot.push_back(loot); - } - else if (item.rarity == 0) { - refinedLoot.push_back(loot); // powerups - } - } +#include "Loot.h" - return refinedLoot; +#include "CDComponentsRegistryTable.h" +#include "CDItemComponentTable.h" +#include "CDLootMatrixTable.h" +#include "CDLootTableTable.h" +#include "CDRarityTableTable.h" +#include "Character.h" +#include "Entity.h" +#include "GameMessages.h" +#include "GeneralUtils.h" +#include "InventoryComponent.h" +#include "MissionComponent.h" + +LootGenerator::LootGenerator() { + CDLootTableTable* lootTableTable = CDClientManager::Instance()->GetTable("LootTable"); + CDComponentsRegistryTable* componentsRegistryTable = CDClientManager::Instance()->GetTable("ComponentsRegistry"); + CDItemComponentTable* itemComponentTable = CDClientManager::Instance()->GetTable("ItemComponent"); + CDLootMatrixTable* lootMatrixTable = CDClientManager::Instance()->GetTable("LootMatrix"); + CDRarityTableTable* rarityTableTable = CDClientManager::Instance()->GetTable("RarityTable"); + + // ============================== + // Cache Item Rarities + // ============================== + + std::vector 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, COMPONENT_TYPE_ITEM); + const CDItemComponent& item = itemComponentTable->GetItemComponentByID(itemComponentID); + + m_ItemRarities.insert({itemID, item.rarity}); + } + + // ============================== + // Cache Rarity Tables + // ============================== + + std::vector 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 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); + } + + // 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 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 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 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 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}); + } } -void Loot::GiveLoot(Entity* user, uint32_t lootMatrixID) { - user = user->GetOwner(); // If the owner is overwritten, we collect that here +std::unordered_map LootGenerator::RollLootMatrix(Entity* player, uint32_t matrixIndex) { + auto* missionComponent = player->GetComponent(); - std::unordered_map result {}; + std::unordered_map drops; - CalculateLootMatrix(lootMatrixID, user, result); + if (missionComponent == nullptr) { + return drops; + } - GiveLoot(user, result); + const LootMatrix& matrix = m_LootMatrices[matrixIndex]; + + for (const LootMatrixEntry& entry : matrix) { + if (GeneralUtils::GenerateRandomNumber(0, 1) < entry.percent) { + const LootTable& lootTable = m_LootTables[entry.lootTableIndex]; + const RarityTable& rarityTable = m_RarityTables[entry.rarityTableIndex]; + + uint32_t dropCount = GeneralUtils::GenerateRandomNumber(entry.minDrop, entry.maxDrop); + for (uint32_t i = 0; i < dropCount; ++i) { + uint32_t maxRarity = 1; + + float rarityRoll = GeneralUtils::GenerateRandomNumber(0, 1); + + for (const RarityTableEntry& rarity : rarityTable) { + if (rarity.randMax >= rarityRoll) { + maxRarity = rarity.rarity; + } else { + break; + } + } + + bool rarityFound = false; + std::vector possibleDrops; + + for (const LootTableEntry& loot : lootTable) { + uint32_t rarity = m_ItemRarities[loot.itemID]; + + if (rarity == maxRarity) { + possibleDrops.push_back(loot); + rarityFound = true; + } else if (rarity < maxRarity && !rarityFound) { + possibleDrops.push_back(loot); + maxRarity = rarity; + } + } + + if (possibleDrops.size() > 0) { + LootTableEntry drop = possibleDrops[GeneralUtils::GenerateRandomNumber(0, possibleDrops.size() - 1)]; + + // filter out uneeded mission items + if (drop.isMissionDrop && !missionComponent->RequiresItem(drop.itemID)) + continue; + + // convert faction token proxy + if (drop.itemID == 13763) { + if (missionComponent->GetMissionState(545) == MissionState::MISSION_STATE_COMPLETE) + drop.itemID = 8318; // "Assembly Token" + else if (missionComponent->GetMissionState(556) == MissionState::MISSION_STATE_COMPLETE) + drop.itemID = 8321; // "Venture League Token" + else if (missionComponent->GetMissionState(567) == MissionState::MISSION_STATE_COMPLETE) + drop.itemID = 8319; // "Sentinels Token" + else if (missionComponent->GetMissionState(578) == MissionState::MISSION_STATE_COMPLETE) + drop.itemID = 8320; // "Paradox Token" + } + + if (drop.itemID == 13763) { + continue; + } // check if we aren't in faction + + if (drops.find(drop.itemID) == drops.end()) { + drops.insert({drop.itemID, 1}); + } else { + ++drops[drop.itemID]; + } + } + } + } + } + + return drops; } -void Loot::DropLoot(Entity* user, Entity* killedObject, uint32_t lootMatrixID, uint32_t minCoins, uint32_t maxCoins) { - user = user->GetOwner(); // If the owner is overwritten, we collect that here +std::unordered_map LootGenerator::RollLootMatrix(uint32_t matrixIndex) { + std::unordered_map drops; - auto* inventoryComponent = user->GetComponent(); + const LootMatrix& matrix = m_LootMatrices[matrixIndex]; - if (inventoryComponent == nullptr) { - return; - } + for (const LootMatrixEntry& entry : matrix) { + if (GeneralUtils::GenerateRandomNumber(0, 1) < entry.percent) { + const LootTable& lootTable = m_LootTables[entry.lootTableIndex]; + const RarityTable& rarityTable = m_RarityTables[entry.rarityTableIndex]; - std::unordered_map result {}; + uint32_t dropCount = GeneralUtils::GenerateRandomNumber(entry.minDrop, entry.maxDrop); + for (uint32_t i = 0; i < dropCount; ++i) { + uint32_t maxRarity = 1; - CalculateLootMatrix(lootMatrixID, user, result); + float rarityRoll = GeneralUtils::GenerateRandomNumber(0, 1); - DropLoot(user, killedObject, result, minCoins, maxCoins); + for (const RarityTableEntry& rarity : rarityTable) { + if (rarity.randMax >= rarityRoll) { + maxRarity = rarity.rarity; + } else { + break; + } + } + + bool rarityFound = false; + std::vector possibleDrops; + + for (const LootTableEntry& loot : lootTable) { + uint32_t rarity = m_ItemRarities[loot.itemID]; + + 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 LootTableEntry& drop = possibleDrops[GeneralUtils::GenerateRandomNumber(0, possibleDrops.size() - 1)]; + + if (drops.find(drop.itemID) == drops.end()) { + drops.insert({drop.itemID, 1}); + } else { + ++drops[drop.itemID]; + } + } + } + } + } + + return drops; } -void Loot::GiveLoot(Entity* user, std::unordered_map& result) { - user = user->GetOwner(); // If the owner is overwritten, we collect that here +void LootGenerator::GiveLoot(Entity* player, uint32_t matrixIndex) { + player = player->GetOwner(); // If the owner is overwritten, we collect that here - auto* inventoryComponent = user->GetComponent(); + std::unordered_map result = RollLootMatrix(player, matrixIndex); - if (inventoryComponent == nullptr) { - return; - } - - for (const auto& pair : result) { - inventoryComponent->AddItem(pair.first, pair.second); - } + GiveLoot(player, result); } -void Loot::DropLoot(Entity* user, Entity* killedObject, std::unordered_map& result, uint32_t minCoins, uint32_t maxCoins) { - user = user->GetOwner(); // If the owner is overwritten, we collect that here +void LootGenerator::GiveLoot(Entity* player, std::unordered_map& result) { + player = player->GetOwner(); // if the owner is overwritten, we collect that here - auto* inventoryComponent = user->GetComponent(); + auto* inventoryComponent = player->GetComponent(); - if (inventoryComponent == nullptr) { - return; - } + if (!inventoryComponent) + return; - const auto spawnPosition = killedObject->GetPosition(); - - const auto source = killedObject->GetObjectID(); - - for (const auto& pair : result) { - for (int i = 0; i < pair.second; ++i) { - GameMessages::SendDropClientLoot(user, source, pair.first, 0, spawnPosition, 1); - } - } - - uint32_t coins = (int)(minCoins + GeneralUtils::GenerateRandomNumber(0, 1) * (maxCoins - minCoins)); - - GameMessages::SendDropClientLoot(user, source, LOT_NULL, coins, spawnPosition); + for (const auto& pair : result) { + inventoryComponent->AddItem(pair.first, pair.second); + } } -void Loot::DropActivityLoot(Entity* user, Entity* source, uint32_t activityID, int32_t rating) -{ - CDActivityRewardsTable* activityRewardsTable = CDClientManager::Instance()->GetTable("ActivityRewards"); - std::vector activityRewards = activityRewardsTable->Query([=](CDActivityRewards entry) { return (entry.objectTemplate == activityID); }); +void LootGenerator::GiveActivityLoot(Entity* player, Entity* source, uint32_t activityID, int32_t rating) { + CDActivityRewardsTable* activityRewardsTable = CDClientManager::Instance()->GetTable("ActivityRewards"); + std::vector activityRewards = activityRewardsTable->Query([activityID](CDActivityRewards entry) { return (entry.objectTemplate == activityID); }); - const CDActivityRewards* selectedReward = nullptr; - for (const auto& activityReward : activityRewards) - { - if (activityReward.activityRating <= rating && (selectedReward == nullptr || activityReward.activityRating > selectedReward->activityRating)) - { - selectedReward = &activityReward; - } - } + const CDActivityRewards* selectedReward = nullptr; + for (const auto& activityReward : activityRewards) { + if (activityReward.activityRating <= rating && (selectedReward == nullptr || activityReward.activityRating > selectedReward->activityRating)) { + selectedReward = &activityReward; + } + } - if (selectedReward == nullptr) - { - return; - } + if (!selectedReward) + return; - uint32_t minCoins = 0; - uint32_t maxCoins = 0; + uint32_t minCoins = 0; + uint32_t maxCoins = 0; - CDCurrencyTableTable* currencyTableTable = CDClientManager::Instance()->GetTable("CurrencyTable"); - std::vector currencyTable = currencyTableTable->Query([=](CDCurrencyTable entry) { return (entry.currencyIndex == selectedReward->CurrencyIndex && entry.npcminlevel == 1); }); + CDCurrencyTableTable* currencyTableTable = CDClientManager::Instance()->GetTable("CurrencyTable"); + std::vector currencyTable = currencyTableTable->Query([selectedReward](CDCurrencyTable entry) { return (entry.currencyIndex == selectedReward->CurrencyIndex && entry.npcminlevel == 1); }); - if (currencyTable.size() > 0) { - minCoins = currencyTable[0].minvalue; - maxCoins = currencyTable[0].maxvalue; - } + if (currencyTable.size() > 0) { + minCoins = currencyTable[0].minvalue; + maxCoins = currencyTable[0].maxvalue; + } - Loot::DropLoot(user, source, selectedReward->LootMatrixIndex, minCoins, maxCoins); + GiveLoot(player, selectedReward->LootMatrixIndex); + + uint32_t coins = (int)(minCoins + GeneralUtils::GenerateRandomNumber(0, 1) * (maxCoins - minCoins)); + + auto* character = player->GetCharacter(); + + character->SetCoins(character->GetCoins() + coins); } -void Loot::GiveActivityLoot(Entity* user, Entity* source, uint32_t activityID, int32_t rating) -{ - CDActivityRewardsTable* activityRewardsTable = CDClientManager::Instance()->GetTable("ActivityRewards"); - std::vector activityRewards = activityRewardsTable->Query([=](CDActivityRewards entry) { return (entry.objectTemplate == activityID); }); +void LootGenerator::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 - const CDActivityRewards* selectedReward = nullptr; - for (const auto& activityReward : activityRewards) - { - if (activityReward.activityRating <= rating && (selectedReward == nullptr || activityReward.activityRating > selectedReward->activityRating)) - { - selectedReward = &activityReward; - } - } + auto* inventoryComponent = player->GetComponent(); - if (selectedReward == nullptr) - { - return; - } + if (!inventoryComponent) + return; - uint32_t minCoins = 0; - uint32_t maxCoins = 0; + std::unordered_map result = RollLootMatrix(player, matrixIndex); - CDCurrencyTableTable* currencyTableTable = CDClientManager::Instance()->GetTable("CurrencyTable"); - std::vector currencyTable = currencyTableTable->Query([=](CDCurrencyTable entry) { return (entry.currencyIndex == selectedReward->CurrencyIndex && entry.npcminlevel == 1); }); - - if (currencyTable.size() > 0) { - minCoins = currencyTable[0].minvalue; - maxCoins = currencyTable[0].maxvalue; - } - - Loot::GiveLoot(user, selectedReward->LootMatrixIndex); - - uint32_t coins = (int)(minCoins + GeneralUtils::GenerateRandomNumber(0, 1) * (maxCoins - minCoins)); - - auto* charactert = user->GetCharacter(); - - charactert->SetCoins(charactert->GetCoins() + coins); + DropLoot(player, killedObject, result, minCoins, maxCoins); } -void Loot::CalculateLootMatrix(uint32_t lootMatrixID, Entity* user, std::unordered_map& result) -{ - user = user->GetOwner(); +void LootGenerator::DropLoot(Entity* player, Entity* killedObject, std::unordered_map& result, uint32_t minCoins, uint32_t maxCoins) { + player = player->GetOwner(); // if the owner is overwritten, we collect that here - auto* missionComponent = user->GetComponent(); + auto* inventoryComponent = player->GetComponent(); - // Get our loot for this LOT's lootMatrixID: - CDLootMatrixTable* lootMatrixTable = CDClientManager::Instance()->GetTable("LootMatrix"); - CDLootTableTable* lootTableTable = CDClientManager::Instance()->GetTable("LootTable"); - CDRarityTableTable* rarityTableTable = CDClientManager::Instance()->GetTable("RarityTable"); + if (!inventoryComponent) + return; - std::vector lootMatrix = lootMatrixTable->Query([lootMatrixID](CDLootMatrix entry) { return (entry.LootMatrixIndex == lootMatrixID); }); + const auto spawnPosition = killedObject->GetPosition(); - // Now, loop through each entry - for (uint32_t i = 0; i < lootMatrix.size(); ++i) { - // Now, determine whether or not we should drop this - float chanceToDrop = 1.0 - lootMatrix[i].percent; - float shouldDrop = GeneralUtils::GenerateRandomNumber(0, 1); + const auto source = killedObject->GetObjectID(); - const auto rarityTableIndex = lootMatrix[i].RarityTableIndex; + for (const auto& pair : result) { + for (int i = 0; i < pair.second; ++i) { + GameMessages::SendDropClientLoot(player, source, pair.first, 0, spawnPosition, 1); + } + } - std::vector rarityTable = rarityTableTable->Query([rarityTableIndex](CDRarityTable entry) { return (entry.RarityTableIndex == rarityTableIndex); }); - - std::sort(rarityTable.begin(), rarityTable.end()); + uint32_t coins = (int)(minCoins + GeneralUtils::GenerateRandomNumber(0, 1) * (maxCoins - minCoins)); - if (shouldDrop < chanceToDrop) { - // We are not able to drop this item, so continue - continue; - } - - // If we reached here, we are able to drop the item - uint32_t minToDrop = lootMatrix[i].minToDrop; - uint32_t maxToDrop = lootMatrix[i].maxToDrop; - - // Now determine the number we will drop of items from this table - uint32_t numToDrop = GeneralUtils::GenerateRandomNumber(minToDrop, maxToDrop); - - // Now, query the loot matrix index - const auto lootTableIndex = lootMatrix[i].LootTableIndex; - - std::vector lootTable = lootTableTable->Query([lootTableIndex](CDLootTable entry) { return (entry.LootTableIndex == lootTableIndex); }); - - // Now randomize these entries - if (lootTable.size() > 1) { - std::shuffle(std::begin(lootTable), std::end(lootTable), Game::randomEngine); - } - - uint32_t addedItems = 0; - - if (lootTable.empty()) continue; - - while (addedItems < numToDrop) { - addedItems++; - - float rarityRoll = GeneralUtils::GenerateRandomNumber(0, 1); - - // im sorry - uint32_t highestRarity = 1; // LOOT_COMMON - float highestRandMax = 0.0f; - for (const auto& rarity : rarityTable) { - if (rarityRoll > rarity.randmax && rarity.randmax > highestRandMax) { - highestRandMax = rarity.randmax; - highestRarity = rarity.rarity + 1; - } - } - - std::vector refinedLoot; - - if (lootTable.size() == 1) - { - refinedLoot = lootTable; - } - else - { - refinedLoot = GetLootOfRarity(lootTable, highestRarity); - - bool continueLoop = false; - while (refinedLoot.empty()) - { - if (highestRarity == 1) - { - continueLoop = true; - break; - } - - highestRarity -= 1; - - refinedLoot = GetLootOfRarity(lootTable, highestRarity); - - if (!refinedLoot.empty()) - { - break; - } - } - - if (continueLoop) continue; - } - - int randomTable = GeneralUtils::GenerateRandomNumber(0, refinedLoot.size() - 1); - - const auto& selectedTable = refinedLoot[randomTable]; - - uint32_t itemLOT = selectedTable.itemid; - bool isMissionItem = selectedTable.MissionDrop; - - if (isMissionItem && missionComponent != nullptr) - { - // TODO: this executes a query in a hot path, might be worth refactoring away - if (!missionComponent->RequiresItem(itemLOT)) - { - continue; - } - } - - if (lootTable.size() > numToDrop) - { - for (size_t i = 0; i < lootTable.size(); i++) - { - if (lootTable[i].id == selectedTable.id) - { - lootTable.erase(lootTable.begin() + i); - - break; - } - } - } - - const auto& it = result.find(itemLOT); - if (it != result.end()) { - it->second++; - } - else { - result.emplace(itemLOT, 1); - } - } - } - - int32_t tokenCount = 0; - - const auto& tokens = result.find(13763); - - if (tokens != result.end()) { - tokenCount = tokens->second; - - result.erase(tokens); - } - - if (tokenCount == 0 || user == nullptr) { - return; - } - - if (missionComponent == nullptr) { - return; - } - - LOT tokenId = -1; - - if (missionComponent->GetMissionState(545) == MissionState::MISSION_STATE_COMPLETE) // "Join Assembly!" - { - tokenId = 8318; // "Assembly Token" - } - - if (missionComponent->GetMissionState(556) == MissionState::MISSION_STATE_COMPLETE) // "Join Venture League!" - { - tokenId = 8321; // "Venture League Token" - } - - if (missionComponent->GetMissionState(567) == MissionState::MISSION_STATE_COMPLETE) // "Join The Sentinels!" - { - tokenId = 8319; // "Sentinels Token" - } - - if (missionComponent->GetMissionState(578) == MissionState::MISSION_STATE_COMPLETE) // "Join Paradox!" - { - tokenId = 8320; // "Paradox Token" - } - - if (tokenId != -1) - { - result.emplace(tokenId, tokenCount); - } + GameMessages::SendDropClientLoot(player, source, LOT_NULL, coins, spawnPosition); } -void Loot::DropItem(Entity* user, Entity* sourceObject, LOT item, int32_t currency, int32_t count, bool useTeam, bool freeForAll) -{ - if (sourceObject == nullptr) - { - return; - } +void LootGenerator::DropActivityLoot(Entity* player, Entity* source, uint32_t activityID, int32_t rating) { + CDActivityRewardsTable* activityRewardsTable = CDClientManager::Instance()->GetTable("ActivityRewards"); + std::vector activityRewards = activityRewardsTable->Query([activityID](CDActivityRewards entry) { return (entry.objectTemplate == activityID); }); - const auto sourceID = sourceObject->GetObjectID(); - const auto sourcePosition = sourceObject->GetPosition(); + const CDActivityRewards* selectedReward = nullptr; + for (const auto& activityReward : activityRewards) { + if (activityReward.activityRating <= rating && (selectedReward == nullptr || activityReward.activityRating > selectedReward->activityRating)) { + selectedReward = &activityReward; + } + } - // If useTeam, drop the item once for each team member. - auto* team = TeamManager::Instance()->GetTeam(user->GetObjectID()); + if (selectedReward == nullptr) { + return; + } - if (team != nullptr && useTeam) - { - for (const auto& memberID : team->members) - { - // Get the team member from its ID. - auto* member = EntityManager::Instance()->GetEntity(memberID); + uint32_t minCoins = 0; + uint32_t maxCoins = 0; - if (member == nullptr) - { - continue; - } + CDCurrencyTableTable* currencyTableTable = CDClientManager::Instance()->GetTable("CurrencyTable"); + std::vector currencyTable = currencyTableTable->Query([selectedReward](CDCurrencyTable entry) { return (entry.currencyIndex == selectedReward->CurrencyIndex && entry.npcminlevel == 1); }); - // Drop the item. - GameMessages::SendDropClientLoot(member, sourceID, item, currency, sourcePosition, count); - } + if (currencyTable.size() > 0) { + minCoins = currencyTable[0].minvalue; + maxCoins = currencyTable[0].maxvalue; + } - return; - } - - GameMessages::SendDropClientLoot(user, sourceID, item, currency, sourcePosition, count); + DropLoot(player, source, selectedReward->LootMatrixIndex, minCoins, maxCoins); } diff --git a/dGame/dUtilities/Loot.h b/dGame/dUtilities/Loot.h index de232f77..23c6463d 100644 --- a/dGame/dUtilities/Loot.h +++ b/dGame/dUtilities/Loot.h @@ -1,31 +1,62 @@ #pragma once + #include "dCommonVars.h" +#include +#include "Singleton.h" #include -#include "CDClientManager.h" + class Entity; +struct RarityTableEntry { + uint32_t rarity; + float randMax; +}; + +typedef std::vector RarityTable; + +struct LootMatrixEntry { + uint32_t lootTableIndex; + uint32_t rarityTableIndex; + float percent; + uint32_t minDrop; + uint32_t maxDrop; +}; + +typedef std::vector LootMatrix; + +struct LootTableEntry { + LOT itemID; + bool isMissionDrop; +}; + +typedef std::vector LootTable; + +// used for glue code with Entity and Player classes namespace Loot { - struct Info { - LWOOBJID id; - LOT lot; - uint32_t count; - }; + struct Info { + LWOOBJID id; + LOT lot; + uint32_t count; + }; +} - std::vector GetLootOfRarity(const std::vector &lootTable, uint32_t rarity); - void DropActivityLoot(Entity* user, Entity* source, uint32_t activityID, int32_t rating = 0); +class LootGenerator : public Singleton { + public: + LootGenerator(); - void GiveActivityLoot(Entity* user, Entity* source, uint32_t activityID, int32_t rating = 0); + std::unordered_map RollLootMatrix(Entity* player, uint32_t matrixIndex); + std::unordered_map RollLootMatrix(uint32_t matrixIndex); + void GiveLoot(Entity* player, uint32_t matrixIndex); + void GiveLoot(Entity* player, std::unordered_map& result); + 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& result, uint32_t minCoins, uint32_t maxCoins); + void DropActivityLoot(Entity* player, Entity* source, uint32_t activityID, int32_t rating = 0); - void CalculateLootMatrix(uint32_t lootMatrixID, Entity* user, std::unordered_map& result); - - void GiveLoot(Entity* user, uint32_t lootMatrixID); - - void DropLoot(Entity* user, Entity* killedObject, uint32_t lootMatrixID, uint32_t minCoins, uint32_t maxCoins); - - void GiveLoot(Entity* user, std::unordered_map& result); - - void DropLoot(Entity* user, Entity* killedObject, std::unordered_map& result, uint32_t minCoins, uint32_t maxCoins); - - void DropItem(Entity* user, Entity* sourceObject, LOT item, int32_t currency, int32_t count, bool useTeam = false, bool freeForAll = false); -}; \ No newline at end of file + private: + std::unordered_map m_ItemRarities; + std::unordered_map m_RarityTables; + std::unordered_map m_LootMatrices; + std::unordered_map m_LootTables; +}; diff --git a/dGame/dUtilities/SlashCommandHandler.cpp b/dGame/dUtilities/SlashCommandHandler.cpp index c5951fdc..8be99612 100644 --- a/dGame/dUtilities/SlashCommandHandler.cpp +++ b/dGame/dUtilities/SlashCommandHandler.cpp @@ -1691,6 +1691,43 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit return; } + if (chatCommand == "rollloot" && entity->GetGMLevel() >= GAME_MASTER_LEVEL_OPERATOR && args.size() >= 3) { + uint32_t lootMatrixIndex = 0; + uint32_t targetLot = 0; + uint32_t loops = 1; + + if (!GeneralUtils::TryParse(args[0], lootMatrixIndex)) return; + if (!GeneralUtils::TryParse(args[1], targetLot)) return; + if (!GeneralUtils::TryParse(args[2], loops)) return; + + uint64_t totalRuns = 0; + + for (uint32_t i = 0; i < loops; i++) { + while (true) { + auto lootRoll = LootGenerator::Instance().RollLootMatrix(lootMatrixIndex); + totalRuns += 1; + bool doBreak = false; + for (const auto& kv : lootRoll) { + if ((uint32_t)kv.first == targetLot) { + doBreak = true; + } + } + if (doBreak) break; + } + } + + std::u16string message = u"Ran loot drops looking for " + + GeneralUtils::to_u16string(targetLot) + + u", " + + GeneralUtils::to_u16string(loops) + + u" times. It ran " + + GeneralUtils::to_u16string(totalRuns) + + u" times. Averaging out at " + + GeneralUtils::to_u16string((float) totalRuns / loops); + + ChatPackets::SendSystemMessage(sysAddr, message); + } + if (chatCommand == "inspect" && entity->GetGMLevel() >= GAME_MASTER_LEVEL_DEVELOPER && args.size() >= 1) { Entity* closest = nullptr; diff --git a/dMasterServer/InstanceManager.cpp b/dMasterServer/InstanceManager.cpp index 218d21b6..41d34cfc 100644 --- a/dMasterServer/InstanceManager.cpp +++ b/dMasterServer/InstanceManager.cpp @@ -43,7 +43,8 @@ Instance * InstanceManager::GetInstance(LWOMAPID mapID, bool isFriendTransfer, L maxPlayers = GetHardCap(mapID); } - instance = new Instance(mExternalIP, ++m_LastPort, mapID, ++m_LastInstanceID, cloneID, softCap, maxPlayers); + uint32_t port = GetFreePort(); + instance = new Instance(mExternalIP, port, mapID, ++m_LastInstanceID, cloneID, softCap, maxPlayers); //Start the actual process: #ifdef _WIN32 @@ -59,7 +60,7 @@ Instance * InstanceManager::GetInstance(LWOMAPID mapID, bool isFriendTransfer, L cmd.append(std::to_string(mapID)); cmd.append(" -port "); - cmd.append(std::to_string(m_LastPort)); + cmd.append(std::to_string(port)); cmd.append(" -instance "); cmd.append(std::to_string(m_LastInstanceID)); cmd.append(" -maxclients "); @@ -74,8 +75,6 @@ Instance * InstanceManager::GetInstance(LWOMAPID mapID, bool isFriendTransfer, L system(cmd.c_str()); - m_LastPort++; //Increment it again because the next port is for World<->Server comm. - m_LastPort++; //Increment it again because the next port is for World<->Chat comm. m_Instances.push_back(instance); if (instance) { @@ -97,6 +96,25 @@ bool InstanceManager::IsPortInUse(uint32_t port) { return false; } +uint32_t InstanceManager::GetFreePort() { + uint32_t port = m_LastPort; + std::vector usedPorts; + for (Instance* i : m_Instances) { + usedPorts.push_back(i->GetPort()); + } + + std::sort(usedPorts.begin(), usedPorts.end()); + + int portIdx = 0; + while (portIdx < usedPorts.size() && port == usedPorts[portIdx]) { + //increment by 3 since each instance uses 3 ports (instance, world-server, world-chat) + port += 3; + portIdx++; + } + + return port; +} + void InstanceManager::AddPlayer(SystemAddress systemAddr, LWOMAPID mapID, LWOINSTANCEID instanceID) { Instance* inst = FindInstance(mapID, instanceID); if (inst) { @@ -291,7 +309,8 @@ Instance* InstanceManager::CreatePrivateInstance(LWOMAPID mapID, LWOCLONEID clon int maxPlayers = 999; - instance = new Instance(mExternalIP, ++m_LastPort, mapID, ++m_LastInstanceID, cloneID, maxPlayers, maxPlayers, true, password); + uint32_t port = GetFreePort(); + instance = new Instance(mExternalIP, port, mapID, ++m_LastInstanceID, cloneID, maxPlayers, maxPlayers, true, password); //Start the actual process: std::string cmd = "start ./WorldServer.exe -zone "; @@ -302,7 +321,7 @@ Instance* InstanceManager::CreatePrivateInstance(LWOMAPID mapID, LWOCLONEID clon cmd.append(std::to_string(mapID)); cmd.append(" -port "); - cmd.append(std::to_string(m_LastPort)); + cmd.append(std::to_string(port)); cmd.append(" -instance "); cmd.append(std::to_string(m_LastInstanceID)); cmd.append(" -maxclients "); @@ -317,8 +336,6 @@ Instance* InstanceManager::CreatePrivateInstance(LWOMAPID mapID, LWOCLONEID clon system(cmd.c_str()); - m_LastPort++; //Increment it again because the next port is for World<->Server comm. - m_LastPort++; //Increment it again because the next port is for World<->Chat comm. m_Instances.push_back(instance); if (instance) return instance; diff --git a/dMasterServer/InstanceManager.h b/dMasterServer/InstanceManager.h index 4475f11c..5dc93849 100644 --- a/dMasterServer/InstanceManager.h +++ b/dMasterServer/InstanceManager.h @@ -102,6 +102,7 @@ public: Instance* GetInstance(LWOMAPID mapID, bool isFriendTransfer, LWOCLONEID cloneID); //Creates an instance if none found bool IsPortInUse(uint32_t port); + uint32_t GetFreePort(); void AddPlayer(SystemAddress systemAddr, LWOMAPID mapID, LWOINSTANCEID instanceID); void RemovePlayer(SystemAddress systemAddr, LWOMAPID mapID, LWOINSTANCEID instanceID); diff --git a/dMasterServer/MasterServer.cpp b/dMasterServer/MasterServer.cpp index af585272..e59c136a 100644 --- a/dMasterServer/MasterServer.cpp +++ b/dMasterServer/MasterServer.cpp @@ -47,9 +47,12 @@ namespace Game { bool shutdownSequenceStarted = false; void ShutdownSequence(); dLogger* SetupLogger(); +void StartAuthServer(); +void StartChatServer(); void HandlePacket(Packet* packet); std::map activeSessions; bool shouldShutdown = false; +SystemAddress chatServerMasterPeerSysAddr; int main(int argc, char** argv) { Diagnostics::SetProcessName("Master"); @@ -188,35 +191,12 @@ int main(int argc, char** argv) { //Depending on the config, start up servers: if (config.GetValue("prestart_servers") != "" && config.GetValue("prestart_servers") == "1") { -#ifdef __APPLE__ - //macOS doesn't need sudo to run on ports < 1024 - system("./ChatServer&"); -#elif _WIN32 - system("start ./ChatServer.exe"); -#else - if (std::atoi(Game::config->GetValue("use_sudo_chat").c_str())) { - system("sudo ./ChatServer&"); - } - else { - system("./ChatServer&"); - } -#endif + StartChatServer(); Game::im->GetInstance(0, false, 0)->SetIsReady(true); Game::im->GetInstance(1000, false, 0)->SetIsReady(true); -#ifdef __APPLE__ - system("./AuthServer&"); -#elif _WIN32 - system("start ./AuthServer.exe"); -#else - if (std::atoi(Game::config->GetValue("use_sudo_auth").c_str())) { - system("sudo ./AuthServer&"); - } - else { - system("./AuthServer&"); - } -#endif + StartAuthServer(); } auto t = std::chrono::high_resolution_clock::now(); @@ -347,6 +327,10 @@ void HandlePacket(Packet* packet) { if (instance) { Game::im->RemoveInstance(instance); //Delete the old } + + if (packet->systemAddress == chatServerMasterPeerSysAddr && !shouldShutdown) { + StartChatServer(); + } } if (packet->data[0] == ID_CONNECTION_LOST) { @@ -359,6 +343,10 @@ void HandlePacket(Packet* packet) { Game::im->RemoveInstance(instance); //Delete the old //Game::im->GetInstance(zoneID.GetMapID(), false, 0); //Create the new } + + if (packet->systemAddress == chatServerMasterPeerSysAddr && !shouldShutdown) { + StartChatServer(); + } } if (packet->data[1] == MASTER) { @@ -447,6 +435,14 @@ void HandlePacket(Packet* packet) { } } + if (theirServerType == ServerType::Chat) { + SystemAddress copy; + copy.binaryAddress = packet->systemAddress.binaryAddress; + copy.port = packet->systemAddress.port; + + chatServerMasterPeerSysAddr = copy; + } + Game::logger->Log("MasterServer", "Received server info, instance: %i port: %i\n", theirInstanceID, theirPort); break; @@ -666,6 +662,37 @@ void HandlePacket(Packet* packet) { } } +void StartChatServer() { +#ifdef __APPLE__ + //macOS doesn't need sudo to run on ports < 1024 + system("./ChatServer&"); +#elif _WIN32 + system("start ./ChatServer.exe"); +#else + if (std::atoi(Game::config->GetValue("use_sudo_chat").c_str())) { + system("sudo ./ChatServer&"); + } + else { + system("./ChatServer&"); + } +#endif +} + +void StartAuthServer() { +#ifdef __APPLE__ + system("./AuthServer&"); +#elif _WIN32 + system("start ./AuthServer.exe"); +#else + if (std::atoi(Game::config->GetValue("use_sudo_auth").c_str())) { + system("sudo ./AuthServer&"); + } + else { + system("./AuthServer&"); + } +#endif +} + void ShutdownSequence() { if (shutdownSequenceStarted) { return; diff --git a/dScripts/ActivityManager.cpp b/dScripts/ActivityManager.cpp index eeddb192..f84963df 100644 --- a/dScripts/ActivityManager.cpp +++ b/dScripts/ActivityManager.cpp @@ -70,7 +70,7 @@ void ActivityManager::StopActivity(Entity *self, const LWOOBJID playerID, const SetActivityValue(self, playerID, 1, value1); SetActivityValue(self, playerID, 2, value2); - Loot::GiveActivityLoot(player, self, gameID, CalculateActivityRating(self, playerID)); + LootGenerator::Instance().GiveActivityLoot(player, self, gameID, CalculateActivityRating(self, playerID)); // Save the new score to the leaderboard and show the leaderboard to the player LeaderboardManager::SaveScore(playerID, gameID, score, value1); diff --git a/dScripts/AgPicnicBlanket.cpp b/dScripts/AgPicnicBlanket.cpp index 96ae6b9b..3d13cb40 100644 --- a/dScripts/AgPicnicBlanket.cpp +++ b/dScripts/AgPicnicBlanket.cpp @@ -8,7 +8,7 @@ void AgPicnicBlanket::OnUse(Entity *self, Entity *user) { self->SetVar(u"active", true); auto lootTable = std::unordered_map {{935, 3}}; - Loot::DropLoot(user, self, lootTable, 0, 0); + LootGenerator::Instance().DropLoot(user, self, lootTable, 0, 0); self->AddCallbackTimer(5.0f, [self]() { self->SetVar(u"active", false); diff --git a/dScripts/BaseInteractDropLootServer.cpp b/dScripts/BaseInteractDropLootServer.cpp index 20cc2f1b..0dbce047 100644 --- a/dScripts/BaseInteractDropLootServer.cpp +++ b/dScripts/BaseInteractDropLootServer.cpp @@ -25,7 +25,7 @@ void BaseInteractDropLootServer::BaseUse(Entity* self, Entity* user) self->SetNetworkVar(u"bInUse", true); - Loot::DropLoot(user, self, lootMatrix, 0, 0); + LootGenerator::Instance().DropLoot(user, self, lootMatrix, 0, 0); self->AddCallbackTimer(cooldownTime, [this, self] () { self->SetNetworkVar(u"bInUse", false); diff --git a/dScripts/BaseRandomServer.cpp b/dScripts/BaseRandomServer.cpp index cf408d26..182544be 100644 --- a/dScripts/BaseRandomServer.cpp +++ b/dScripts/BaseRandomServer.cpp @@ -91,7 +91,7 @@ void BaseRandomServer::SetSpawnerNetwork(Entity* self, const std::string& spawne if (spawnerName == "Named_Enemies") { - //spawner->Reset(); + spawner->SoftReset(); } spawner->Activate(); @@ -173,12 +173,12 @@ void BaseRandomServer::NamedEnemyDeath(Entity* self, Spawner* spawner) void BaseRandomServer::SpawnersUp(Entity* self) { - + } void BaseRandomServer::SpawnersDown(Entity* self) { - + } void BaseRandomServer::BaseOnTimerDone(Entity* self, const std::string& timerName) diff --git a/dScripts/BaseRandomServer.h b/dScripts/BaseRandomServer.h index e67df365..d04414f1 100644 --- a/dScripts/BaseRandomServer.h +++ b/dScripts/BaseRandomServer.h @@ -28,7 +28,7 @@ public: void SpawnersUp(Entity* self); void SpawnersDown(Entity* self); void BaseOnTimerDone(Entity* self, const std::string& timerName); - + void NotifySpawnerOfDeath(Entity* self, Spawner* spawner); void NamedEnemyDeath(Entity* self, Spawner* spawner); diff --git a/dScripts/BootyDigServer.cpp b/dScripts/BootyDigServer.cpp index 93e6449c..eeb3bfd4 100644 --- a/dScripts/BootyDigServer.cpp +++ b/dScripts/BootyDigServer.cpp @@ -41,7 +41,7 @@ BootyDigServer::OnFireEventServerSide(Entity *self, Entity *sender, std::string if (renderComponent != nullptr) renderComponent->PlayEffect(7730, u"cast", "bootyshine"); - Loot::DropLoot(player, self, 231, 75, 75); + LootGenerator::Instance().DropLoot(player, self, 231, 75, 75); } } } else if (args == "ChestDead") { diff --git a/dScripts/BuccaneerValiantShip.cpp b/dScripts/BuccaneerValiantShip.cpp index 2c24b7d4..1cc1e633 100644 --- a/dScripts/BuccaneerValiantShip.cpp +++ b/dScripts/BuccaneerValiantShip.cpp @@ -3,20 +3,15 @@ #include "dLogger.h" void BuccaneerValiantShip::OnStartup(Entity* self) { - const auto skill = 982; - const auto behavior = 20577; - const auto skillCastTimer = 1.0F; - - self->AddCallbackTimer(skillCastTimer, [self]() { + self->AddCallbackTimer(1.0F, [self]() { auto* skillComponent = self->GetComponent(); auto* owner = self->GetOwner(); if (skillComponent != nullptr && owner != nullptr) { - skillComponent->CalculateBehavior(skill, behavior, LWOOBJID_EMPTY, true, false, owner->GetObjectID()); + skillComponent->CalculateBehavior(982, 20577, LWOOBJID_EMPTY, true, false, owner->GetObjectID()); // Kill self if missed - const auto selfSmashTimer = 1.1F; - self->AddCallbackTimer(selfSmashTimer, [self]() { + self->AddCallbackTimer(1.1F, [self]() { self->Kill(); }); } diff --git a/dScripts/CppScripts.cpp b/dScripts/CppScripts.cpp index 1db8b8dc..53cbf425 100644 --- a/dScripts/CppScripts.cpp +++ b/dScripts/CppScripts.cpp @@ -261,6 +261,7 @@ #include "PersonalFortress.h" #include "PropertyDevice.h" #include "ImaginationBackpackHealServer.h" +#include "LegoDieRoll.h" #include "BuccaneerValiantShip.h" // Survival scripts @@ -775,8 +776,10 @@ CppScripts::Script* CppScripts::GetScript(Entity* parent, const std::string& scr script = new PropertyDevice(); else if (scriptName == "scripts\\02_server\\Map\\General\\L_IMAG_BACKPACK_HEALS_SERVER.lua") script = new ImaginationBackpackHealServer(); - else if (scriptName == "scripts\\EquipmentScripts\\BuccaneerValiantShip.lua") - script = new BuccaneerValiantShip(); + else if (scriptName == "scripts\\ai\\GENERAL\\L_LEGO_DIE_ROLL.lua") + script = new LegoDieRoll(); + else if (scriptName == "scripts\\EquipmentScripts\\BuccaneerValiantShip.lua") + script = new BuccaneerValiantShip(); //Ignore these scripts: else if (scriptName == "scripts\\02_server\\Enemy\\General\\L_SUSPEND_LUA_AI.lua") diff --git a/dScripts/CppScripts.h b/dScripts/CppScripts.h index 2f4ecd24..9cd014b5 100644 --- a/dScripts/CppScripts.h +++ b/dScripts/CppScripts.h @@ -6,6 +6,7 @@ #include "MissionState.h" #include "Game.h" #include "dLogger.h" +#include "Loot.h" class User; class Entity; diff --git a/dScripts/GrowingFlower.cpp b/dScripts/GrowingFlower.cpp index e6ca587d..1acb2455 100644 --- a/dScripts/GrowingFlower.cpp +++ b/dScripts/GrowingFlower.cpp @@ -10,7 +10,7 @@ void GrowingFlower::OnSkillEventFired(Entity *self, Entity *target, const std::s const auto mission1 = self->GetVar(u"missionID"); const auto mission2 = self->GetVar(u"missionID2"); - Loot::DropActivityLoot(target, self, self->GetLOT(), 0); + LootGenerator::Instance().DropActivityLoot(target, self, self->GetLOT(), 0); auto* missionComponent = target->GetComponent(); if (missionComponent != nullptr) { diff --git a/dScripts/LegoDieRoll.cpp b/dScripts/LegoDieRoll.cpp new file mode 100644 index 00000000..4b375a38 --- /dev/null +++ b/dScripts/LegoDieRoll.cpp @@ -0,0 +1,55 @@ +#include "LegoDieRoll.h" +#include "Entity.h" +#include "dLogger.h" +#include "GameMessages.h" + +void LegoDieRoll::OnStartup(Entity* self) { + self->AddTimer("DoneRolling", 10.0f); + self->AddTimer("ThrowDice", LegoDieRoll::animTime); +} + +void LegoDieRoll::OnTimerDone(Entity* self, std::string timerName) { + if (timerName == "DoneRolling") { + self->Smash(self->GetObjectID(), SILENT); + } + else if (timerName == "ThrowDice") { + int dieRoll = GeneralUtils::GenerateRandomNumber(1, 6); + + switch (dieRoll) + { + case 1: + GameMessages::SendPlayAnimation(self, u"roll-die-1"); + break; + case 2: + GameMessages::SendPlayAnimation(self, u"roll-die-2"); + break; + case 3: + GameMessages::SendPlayAnimation(self, u"roll-die-3"); + break; + case 4: + GameMessages::SendPlayAnimation(self, u"roll-die-4"); + break; + case 5: + GameMessages::SendPlayAnimation(self, u"roll-die-5"); + break; + case 6: + { + GameMessages::SendPlayAnimation(self, u"roll-die-6"); + // tracking the It's Truly Random Achievement + auto* owner = self->GetOwner(); + auto* missionComponent = owner->GetComponent(); + + if (missionComponent != nullptr) { + const auto rollMissionState = missionComponent->GetMissionState(756); + if (rollMissionState == MissionState::MISSION_STATE_ACTIVE) { + missionComponent->ForceProgress(756, 1103, 1); + } + } + break; + } + default: + Game::logger->LogDebug("LegoDieRoll", "Invalid animation: roll-die-%i\n", dieRoll); + break; + } + } +} \ No newline at end of file diff --git a/dScripts/LegoDieRoll.h b/dScripts/LegoDieRoll.h new file mode 100644 index 00000000..3b28d529 --- /dev/null +++ b/dScripts/LegoDieRoll.h @@ -0,0 +1,11 @@ +#pragma once +#include "CppScripts.h" + +class LegoDieRoll : public CppScripts::Script { +public: + void OnStartup(Entity* self); + void OnTimerDone(Entity* self, std::string timerName); +private: + constexpr static const float animTime = 2.0f; +}; + diff --git a/dScripts/MinigameTreasureChestServer.cpp b/dScripts/MinigameTreasureChestServer.cpp index 3380aaae..8f89de54 100644 --- a/dScripts/MinigameTreasureChestServer.cpp +++ b/dScripts/MinigameTreasureChestServer.cpp @@ -22,10 +22,10 @@ void MinigameTreasureChestServer::OnUse(Entity *self, Entity *user) { for (const auto& teamMemberID : team->members) { auto* teamMember = EntityManager::Instance()->GetEntity(teamMemberID); if (teamMember != nullptr) - Loot::DropActivityLoot(teamMember, self, sac->GetActivityID(), CalculateActivityRating(self, teamMemberID)); + LootGenerator::Instance().DropActivityLoot(teamMember, self, sac->GetActivityID(), CalculateActivityRating(self, teamMemberID)); } } else { - Loot::DropActivityLoot(user, self, sac->GetActivityID(), CalculateActivityRating(self, user->GetObjectID())); + LootGenerator::Instance().DropActivityLoot(user, self, sac->GetActivityID(), CalculateActivityRating(self, user->GetObjectID())); } sac->PlayerRemove(user->GetObjectID()); diff --git a/dScripts/NjDragonEmblemChestServer.cpp b/dScripts/NjDragonEmblemChestServer.cpp index 717e68b2..ea699c55 100644 --- a/dScripts/NjDragonEmblemChestServer.cpp +++ b/dScripts/NjDragonEmblemChestServer.cpp @@ -10,6 +10,6 @@ void NjDragonEmblemChestServer::OnUse(Entity *self, Entity *user) { auto* destroyable = self->GetComponent(); if (destroyable != nullptr) { - Loot::DropLoot(user, self, destroyable->GetLootMatrixID(), 0, 0); + LootGenerator::Instance().DropLoot(user, self, destroyable->GetLootMatrixID(), 0, 0); } } diff --git a/dScripts/SGCannon.cpp b/dScripts/SGCannon.cpp index 59373b5d..a691443e 100644 --- a/dScripts/SGCannon.cpp +++ b/dScripts/SGCannon.cpp @@ -423,7 +423,7 @@ void SGCannon::SpawnNewModel(Entity *self) { if (lootMatrix != 0) { std::unordered_map toDrop = {}; - Loot::CalculateLootMatrix(lootMatrix, player, toDrop); + toDrop = LootGenerator::Instance().RollLootMatrix(player, lootMatrix); for (auto drop : toDrop) { rewardModel->OnFireEventServerSide(self, ModelToBuildEvent, drop.first); @@ -581,7 +581,7 @@ void SGCannon::StopGame(Entity *self, bool cancel) { ); } - Loot::GiveActivityLoot(player, self, GetGameID(self), self->GetVar(TotalScoreVariable)); + LootGenerator::Instance().GiveActivityLoot(player, self, GetGameID(self), self->GetVar(TotalScoreVariable)); StopActivity(self, player->GetObjectID(), self->GetVar(TotalScoreVariable), self->GetVar(MaxStreakVariable), percentage); diff --git a/dScripts/ScriptedPowerupSpawner.cpp b/dScripts/ScriptedPowerupSpawner.cpp index bd85eeef..5fc013c1 100644 --- a/dScripts/ScriptedPowerupSpawner.cpp +++ b/dScripts/ScriptedPowerupSpawner.cpp @@ -24,7 +24,7 @@ void ScriptedPowerupSpawner::OnTimerDone(Entity *self, std::string message) { renderComponent->PlayEffect(0, u"cast", "N_cast"); } - Loot::DropItem(owner, self, itemLOT, 0, 1, true, true); + LootGenerator::Instance().DropLoot(owner, self, itemLOT, 0, 1); } // Increment the current cycle diff --git a/dScripts/TreasureChestDragonServer.cpp b/dScripts/TreasureChestDragonServer.cpp index 582b634e..d0e8eaec 100644 --- a/dScripts/TreasureChestDragonServer.cpp +++ b/dScripts/TreasureChestDragonServer.cpp @@ -39,12 +39,12 @@ void TreasureChestDragonServer::OnUse(Entity* self, Entity* user) if (memberObject == nullptr) continue; - Loot::DropActivityLoot(memberObject, self, scriptedActivityComponent->GetActivityID(), rating); + LootGenerator::Instance().DropActivityLoot(memberObject, self, scriptedActivityComponent->GetActivityID(), rating); } } else { - Loot::DropActivityLoot(user, self, scriptedActivityComponent->GetActivityID(), rating); + LootGenerator::Instance().DropActivityLoot(user, self, scriptedActivityComponent->GetActivityID(), rating); } self->Smash(self->GetObjectID()); diff --git a/dScripts/VeMissionConsole.cpp b/dScripts/VeMissionConsole.cpp index 7519b9ec..4a506dac 100644 --- a/dScripts/VeMissionConsole.cpp +++ b/dScripts/VeMissionConsole.cpp @@ -4,7 +4,7 @@ #include "GameMessages.h" void VeMissionConsole::OnUse(Entity *self, Entity *user) { - Loot::DropActivityLoot(user, self, 12551); + LootGenerator::Instance().DropActivityLoot(user, self, 12551); auto* inventoryComponent = user->GetComponent(); if (inventoryComponent != nullptr) { diff --git a/dScripts/WishingWellServer.cpp b/dScripts/WishingWellServer.cpp index 78181672..36bca973 100644 --- a/dScripts/WishingWellServer.cpp +++ b/dScripts/WishingWellServer.cpp @@ -24,7 +24,7 @@ void WishingWellServer::OnUse(Entity* self, Entity* user) GameMessages::SendPlayNDAudioEmitter(self, user->GetSystemAddress(), audio); } - Loot::DropActivityLoot( + LootGenerator::Instance().DropActivityLoot( user, self, static_cast(scriptedActivity->GetActivityID()), diff --git a/dWorldServer/WorldServer.cpp b/dWorldServer/WorldServer.cpp index ed751f41..520907ff 100644 --- a/dWorldServer/WorldServer.cpp +++ b/dWorldServer/WorldServer.cpp @@ -73,6 +73,7 @@ namespace Game { } bool chatDisabled = false; +bool chatConnected = false; bool worldShutdownSequenceStarted = false; bool worldShutdownSequenceComplete = false; void WorldShutdownSequence(); @@ -89,6 +90,7 @@ struct tempSessionInfo { std::map m_PendingUsers; int instanceID = 0; int g_CloneID = 0; +std::string databaseChecksum = ""; int main(int argc, char** argv) { Diagnostics::SetProcessName("World"); @@ -186,6 +188,7 @@ int main(int argc, char** argv) { ObjectIDManager::Instance()->Initialize(); UserManager::Instance()->Initialize(); + LootGenerator::Instance(); Game::chatFilter = new dChatFilter("./res/chatplus_en_us", bool(std::stoi(config.GetValue("dont_generate_dcf")))); Game::server = new dServer(masterIP, ourPort, instanceID, maxClients, false, true, Game::logger, masterIP, masterPort, ServerType::World, zoneID); @@ -210,6 +213,7 @@ int main(int argc, char** argv) { Packet* packet = nullptr; int framesSinceLastFlush = 0; int framesSinceMasterDisconnect = 0; + int framesSinceChatDisconnect = 0; int framesSinceLastUsersSave = 0; int framesSinceLastSQLPing = 0; int framesSinceLastUser = 0; @@ -235,6 +239,47 @@ int main(int argc, char** argv) { Game::physicsWorld = &dpWorld::Instance(); //just in case some old code references it dZoneManager::Instance()->Initialize(LWOZONEID(zoneID, instanceID, cloneID)); g_CloneID = cloneID; + + // pre calculate the FDB checksum + if (Game::config->GetValue("check_fdb") == "1") { + std::ifstream fileStream; + + static const std::vector aliases = { + "res/CDServers.fdb", + "res/cdserver.fdb", + "res/CDClient.fdb", + "res/cdclient.fdb", + }; + + for (const auto& file : aliases) { + fileStream.open(file, std::ios::binary | std::ios::in); + if (fileStream.is_open()) { + break; + } + } + + const int bufferSize = 1024; + MD5* md5 = new MD5(); + + char fileStreamBuffer[1024] = {}; + + while (!fileStream.eof()) { + memset(fileStreamBuffer, 0, bufferSize); + fileStream.read(fileStreamBuffer, bufferSize); + md5->update(fileStreamBuffer, fileStream.gcount()); + } + + fileStream.close(); + + const char* nullTerminateBuffer = "\0"; + md5->update(nullTerminateBuffer, 1); // null terminate the data + md5->finalize(); + databaseChecksum = md5->hexdigest(); + + delete md5; + + Game::logger->Log("WorldServer", "FDB Checksum calculated as: %s\n", databaseChecksum.c_str()); + } } while (true) { @@ -276,6 +321,19 @@ int main(int argc, char** argv) { } else framesSinceMasterDisconnect = 0; + // Check if we're still connected to chat: + if (!chatConnected) { + framesSinceChatDisconnect++; + + // Attempt to reconnect every 30 seconds. + if (framesSinceChatDisconnect >= 2000) { + framesSinceChatDisconnect = 0; + + Game::chatServer->Connect(masterIP.c_str(), chatPort, "3.25 ND1", 8); + } + } + else framesSinceChatDisconnect = 0; + //In world we'd update our other systems here. if (zoneID != 0 && deltaTime > 0.0f) { @@ -516,11 +574,15 @@ dLogger * SetupLogger(int zoneID, int instanceID) { void HandlePacketChat(Packet* packet) { if (packet->data[0] == ID_DISCONNECTION_NOTIFICATION || packet->data[0] == ID_CONNECTION_LOST) { Game::logger->Log("WorldServer", "Lost our connection to chat.\n"); + + chatConnected = false; } if (packet->data[0] == ID_CONNECTION_REQUEST_ACCEPTED) { Game::logger->Log("WorldServer", "Established connection to chat\n"); Game::chatSysAddr = packet->systemAddress; + + chatConnected = true; } if (packet->data[0] == ID_USER_PACKET_ENUM) { @@ -841,7 +903,33 @@ void HandlePacket(Packet* packet) { case MSG_WORLD_CLIENT_VALIDATION: { std::string username = PacketUtils::ReadString(0x08, packet, true); std::string sessionKey = PacketUtils::ReadString(74, packet, true); + std::string clientDatabaseChecksum = PacketUtils::ReadString(packet->length - 33, packet, false); + // sometimes client puts a null terminator at the end of the checksum and sometimes doesn't, weird + clientDatabaseChecksum = clientDatabaseChecksum.substr(0, 32); + + // If the check is turned on, validate the client's database checksum. + if (Game::config->GetValue("check_fdb") == "1" && !databaseChecksum.empty()) { + uint32_t gmLevel = 0; + auto* stmt = Database::CreatePreppedStmt("SELECT gm_level FROM accounts WHERE name=? LIMIT 1;"); + stmt->setString(1, username.c_str()); + + auto* res = stmt->executeQuery(); + while (res->next()) { + gmLevel = res->getInt(1); + } + + delete stmt; + delete res; + + // Developers may skip this check + if (gmLevel < 8 && clientDatabaseChecksum != databaseChecksum) { + Game::logger->Log("WorldServer", "Client's database checksum does not match the server's, aborting connection.\n"); + Game::server->Disconnect(packet->systemAddress, SERVER_DISCON_KICK); + return; + } + } + //Request the session info from Master: CBITSTREAM; PacketUtils::WriteHeader(bitStream, MASTER, MSG_MASTER_REQUEST_SESSION_KEY); diff --git a/dZoneManager/Level.cpp b/dZoneManager/Level.cpp index 0e9318ca..b678ed95 100644 --- a/dZoneManager/Level.cpp +++ b/dZoneManager/Level.cpp @@ -11,6 +11,8 @@ #include "GeneralUtils.h" #include "Entity.h" #include "EntityManager.h" +#include "CDFeatureGatingTable.h" +#include "CDClientManager.h" Level::Level(Zone* parentZone, const std::string& filepath) { m_ParentZone = parentZone; diff --git a/resources/worldconfig.ini b/resources/worldconfig.ini index e30aa865..d8d8ef87 100644 --- a/resources/worldconfig.ini +++ b/resources/worldconfig.ini @@ -50,3 +50,6 @@ solo_racing=0 # Disables the anti-speedhack system. If you get kicked randomly you might want to disable this, as it might just be lag disable_anti_speedhack=0 + +# 0 or 1, check server fdb (res/CDServer.fdb) against clients +check_fdb=0 \ No newline at end of file