From 4f18578c1f2733737f8540b22aea50f4e761ec8f Mon Sep 17 00:00:00 2001 From: Aaron Kimbrell Date: Sat, 28 Nov 2026 11:44:44 -0600 Subject: [PATCH] feat(scripts): L_ELEMENTAL_CHESTS_SERVER The Fire Temple chests drop their loot matrix and coins from currency table 132 at level 1 for the player, and set the flag their precondition targets. Co-Authored-By: Claude Opus 5.5 --- dScripts/02_server/Map/General/CMakeLists.txt | 1 + .../Map/General/ElementalChestsServer.cpp | 41 +++++++++++++++++++ .../Map/General/ElementalChestsServer.h | 13 ++++++ dScripts/CppScripts.cpp | 2 + tests/dGameTests/FireTempleScriptsTests.cpp | 12 ++++++ 5 files changed, 69 insertions(+) create mode 100644 dScripts/02_server/Map/General/ElementalChestsServer.cpp create mode 100644 dScripts/02_server/Map/General/ElementalChestsServer.h diff --git a/dScripts/02_server/Map/General/CMakeLists.txt b/dScripts/02_server/Map/General/CMakeLists.txt index ed8920ee0..c2142666c 100644 --- a/dScripts/02_server/Map/General/CMakeLists.txt +++ b/dScripts/02_server/Map/General/CMakeLists.txt @@ -3,6 +3,7 @@ set(DSCRIPTS_SOURCES_02_SERVER_MAP_GENERAL "BaseInteractDropLootServer.cpp" "Binoculars.cpp" "DoorIndicatorServer.cpp" + "ElementalChestsServer.cpp" "EnemyClearThreat.cpp" "ExplodingAsset.cpp" "FrictionVolumeServer.cpp" diff --git a/dScripts/02_server/Map/General/ElementalChestsServer.cpp b/dScripts/02_server/Map/General/ElementalChestsServer.cpp new file mode 100644 index 000000000..8c1b50d4f --- /dev/null +++ b/dScripts/02_server/Map/General/ElementalChestsServer.cpp @@ -0,0 +1,41 @@ +#include "ElementalChestsServer.h" +#include "Entity.h" +#include "Character.h" +#include "CDClientDatabase.h" +#include "CDClientManager.h" +#include "CDCurrencyTableTable.h" +#include "DestroyableComponent.h" +#include "GeneralUtils.h" +#include "Loot.h" + +namespace { + // RollCurrency{ iTable = 132, iLevel = 1 } + constexpr uint32_t CurrencyTable = 132; + constexpr uint32_t CurrencyLevel = 1; +} + +void ElementalChestsServer::OnUse(Entity* self, Entity* user) { + if (!user) return; + auto* const destroyable = self->GetComponent(); + const uint32_t lootMatrix = destroyable ? destroyable->GetLootMatrixID() : 0; + const auto currencyRows = CDClientManager::GetTable()->Query([](const CDCurrencyTable& row) { + return row.currencyIndex == CurrencyTable; + }); + const auto [minCoins, maxCoins] = Loot::GetActivityCoinRange(currencyRows, CurrencyLevel); + Loot::DropLoot(user, self->GetObjectID(), lootMatrix, minCoins, maxCoins); + + if (!self->HasVar(u"CheckPrecondition")) return; + const auto flag = PreconditionFlag(self->GetVarAsString(u"CheckPrecondition")); + auto* const character = user->GetCharacter(); + if (flag && character) character->SetPlayerFlag(*flag, true); +} + +std::optional ElementalChestsServer::PreconditionFlag(const std::string& checkPrecondition) { + const auto precondition = GeneralUtils::TryParse(checkPrecondition); + if (!precondition) return std::nullopt; + auto query = CDClientDatabase::CreatePreppedStmt("SELECT targetLOT FROM Preconditions WHERE id = ?;"); + query.bind(1, static_cast(*precondition)); + auto result = query.execQuery(); + if (result.eof()) return std::nullopt; + return GeneralUtils::TryParse(result.getStringField(0)); +} diff --git a/dScripts/02_server/Map/General/ElementalChestsServer.h b/dScripts/02_server/Map/General/ElementalChestsServer.h new file mode 100644 index 000000000..6d266d4e2 --- /dev/null +++ b/dScripts/02_server/Map/General/ElementalChestsServer.h @@ -0,0 +1,13 @@ +#pragma once +#include "CppScripts.h" +#include + +// L_ELEMENTAL_CHESTS_SERVER.lua (Fire Temple chests, LOT 16837-16841): used (its CheckPrecondition is checked on use +// as for any object), it drops its loot matrix and coins from currency table 132 at level 1 for the player, and sets +// the flag its precondition targets. +class ElementalChestsServer : public CppScripts::Script { +public: + void OnUse(Entity* self, Entity* user) override; + // The flag a CheckPrecondition value targets (the precondition's targetLOT), or nothing + static std::optional PreconditionFlag(const std::string& checkPrecondition); +}; diff --git a/dScripts/CppScripts.cpp b/dScripts/CppScripts.cpp index d16409874..32acbd5b6 100644 --- a/dScripts/CppScripts.cpp +++ b/dScripts/CppScripts.cpp @@ -279,6 +279,7 @@ #include "KillCounterUtil.h" #include "SpinnersVol.h" #include "DoorIndicatorServer.h" +#include "ElementalChestsServer.h" #include "SimpleOnceOnlyMoverSpinnerEvent.h" #include "ImaginationShrineServer.h" #include "Lieutenant.h" @@ -697,6 +698,7 @@ namespace { {"scripts\\02_server\\Map\\firetemple\\L_BSPINNERS_VOL.lua", []() {return new BSpinnersVol();}}, {"scripts\\02_server\\Map\\firetemple\\L_DSPINNERS_VOL.lua", []() {return new DSpinnersVol();}}, {"scripts\\02_server\\Map\\firetemple\\L_DOOR_INDICATOR_SERVER.lua", []() {return new DoorIndicatorServer();}}, + {"scripts\\02_server\\Map\\firetemple\\L_ELEMENTAL_CHESTS_SERVER.lua", []() {return new ElementalChestsServer();}}, {"scripts\\02_server\\Map\\General\\L_KILL_COUNTER_UTIL.lua", []() {return new KillCounterUtil();}}, {"scripts\\02_server\\Enemy\\General\\L_ENEMY_NJ_BUFF_STUN_IMMUNITY.lua", []() {return new EnemyNjBuff();}}, {"scripts\\02_server\\Map\\njhub\\L_IMAGINATION_SHRINE_SERVER.lua", []() {return new ImaginationShrineServer();}}, diff --git a/tests/dGameTests/FireTempleScriptsTests.cpp b/tests/dGameTests/FireTempleScriptsTests.cpp index 4a6a7dbbd..37b7b1655 100644 --- a/tests/dGameTests/FireTempleScriptsTests.cpp +++ b/tests/dGameTests/FireTempleScriptsTests.cpp @@ -6,6 +6,8 @@ #include "02_server/Map/General/KillCounterUtil.h" #include "02_server/Map/General/SpinnersVol.h" #include "02_server/Map/General/DoorIndicatorServer.h" +#include "02_server/Map/General/ElementalChestsServer.h" +#include "CDClientDatabase.h" namespace { // A kill counter extension that records its limit events @@ -94,3 +96,13 @@ TEST_F(FireTempleScriptsTest, DoorIndicatorLightsOnEitherEvent) { indicator.OnFireEventServerSide(&entity, &entity, "DoorClose", 0, 0, 0); EXPECT_TRUE(entity.GetNetworkVar(u"FlameOn")); } + +// L_ELEMENTAL_CHESTS_SERVER: the flag set on use is the chest's precondition's target +TEST_F(FireTempleScriptsTest, ElementalChestFlagIsItsPreconditionsTarget) { + CDClientDatabase::Connect(":memory:"); + CDClientDatabase::ExecuteDML("CREATE TABLE Preconditions (id INTEGER, type INTEGER, targetLOT TEXT, targetCount INTEGER, validContexts INTEGER);"); + CDClientDatabase::ExecuteDML("INSERT INTO Preconditions VALUES (321, 23, '2063', 1, 0);"); + EXPECT_EQ(ElementalChestsServer::PreconditionFlag("321"), 2063u); + EXPECT_EQ(ElementalChestsServer::PreconditionFlag("322"), std::nullopt); + EXPECT_EQ(ElementalChestsServer::PreconditionFlag(""), std::nullopt); +}