Skip to content

Commit

Permalink
Tweaks: add NWNX_TWEAKS_RULESET_BARBARIAN_DR_CHANGE_FOR_PC_ONLY
Browse files Browse the repository at this point in the history
  • Loading branch information
hendrikgit committed Oct 31, 2024
1 parent f1a4cbb commit a2ee7ac
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ https://github.com/nwnxee/unified/compare/build8193.36.12...HEAD
- NoStack: Added `NWNX_NOSTACK_IGNORE_SUPERNATURAL_INNATE` to ignore effects created by the Feat, Race and SkillRanks plugins when stacking.
- Tweaks: added `NWNX_TWEAKS_CUTSCENE_MODE_NO_TURD` to not drop a TURD when SetCutsceneMode() is called.
- Tweaks: added `NWNX_TWEAKS_CAN_USE_ITEMS_WHILE_POLYMORPHED` to allow all items to be used while polymorphed.
- Tweaks: added `NWNX_TWEAKS_RULESET_BARBARIAN_DR_CHANGE_FOR_PC_ONLY` to apply changes in ruleset.2da to BARBARIAN_DAMAGE_REDUCTION only to PCs. |

##### New Plugins
- Store: Enables getting and setting store data.
Expand Down
2 changes: 2 additions & 0 deletions NWNXLib/API/FunctionsLinux.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,6 @@ NWNXLIB_FUNCTION(_ZN23CNWSItemPropertyHandler24InitializeItemPropertiesEv)
NWNXLIB_FUNCTION(_ZN23CNWSItemPropertyHandler21OnItemPropertyAppliedEP8CNWSItemP15CNWItemPropertyP12CNWSCreatureji)
NWNXLIB_FUNCTION(_ZN23CNWSItemPropertyHandler21OnItemPropertyRemovedEP8CNWSItemP15CNWItemPropertyP12CNWSCreaturej)

NWNXLIB_FUNCTION(_ZN10CNWSObject17DoDamageReductionEP12CNWSCreatureihiii)

// ***
3 changes: 2 additions & 1 deletion Plugins/Tweaks/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@ add_plugin(Tweaks
"SetAreaCallsSetPosition.cpp"
"EquipUnequipEventTweaks.cpp"
"CutsceneModeNoTURD.cpp"
"CanUseItemsWhilePolymorphed.cpp")
"CanUseItemsWhilePolymorphed.cpp"
"RulesetBarbarianDRChangeForPCOnly.cpp")
1 change: 1 addition & 0 deletions Plugins/Tweaks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Tweaks stuff. See below.
| `NWNX_TWEAKS_DONT_DELAY_EQUIP_EVENT` | true or false | Fixes Unequip/Equip events being out of sync if an item is equipped/unequipped multiple times per server tick |
| `NWNX_TWEAKS_CUTSCENE_MODE_NO_TURD` | true or false | SetCutsceneMode() will not cause a TURD to be dropped. |
| `NWNX_TWEAKS_CAN_USE_ITEMS_WHILE_POLYMORPHED` | true or false | Allow all items to be used while polymorphed. |
| `NWNX_TWEAKS_RULESET_BARBARIAN_DR_CHANGE_FOR_PC_ONLY` | true or false | Changes in ruleset.2da to BARBARIAN_DAMAGE_REDUCTION apply only to PCs. |

## Environment variable values

Expand Down
59 changes: 59 additions & 0 deletions Plugins/Tweaks/RulesetBarbarianDRChangeForPCOnly.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include "nwnx.hpp"

#include "API/CNWSObject.hpp"
#include "API/CNWSCreature.hpp"
#include "API/CNWRules.hpp"

namespace Tweaks {

using namespace NWNXLib;
using namespace NWNXLib::API;

void RulesetBarbarianDRChangeForPCOnly() __attribute__((constructor));
void RulesetBarbarianDRChangeForPCOnly()
{
if (!Config::Get<bool>("RULESET_BARBARIAN_DR_CHANGE_FOR_PC_ONLY", false)) return;

LOG_INFO("Changes in ruleset.2da to BARBARIAN_DAMAGE_REDUCTION apply only to PCs.");

static Hooks::Hook s_DoDamageReductionHook = Hooks::HookFunction(Functions::_ZN10CNWSObject17DoDamageReductionEP12CNWSCreatureihiii,
+[](CNWSObject *thisPtr, CNWSCreature *pDamager, int32_t nDamage, uint8_t nDamagePower, BOOL bSimulation, BOOL bCombatDamage, BOOL bRangedAttack) -> int32_t
{
const auto *thisCreature = Utils::AsNWSCreature(thisPtr);
bool useDefaultValues = thisCreature && !thisCreature->m_pStats->m_bIsPC && (
thisCreature->m_pStats->HasFeat(Constants::Feat::DamageReduction) ||
thisCreature->m_pStats->HasFeat(Constants::Feat::DamageReduction2) ||
thisCreature->m_pStats->HasFeat(Constants::Feat::DamageReduction3) ||
thisCreature->m_pStats->HasFeat(Constants::Feat::DamageReduction4) ||
thisCreature->m_pStats->HasFeat(Constants::Feat::EpicBarbarianDamageReduction)
);

if (useDefaultValues)
{
const auto dr1 = Globals::Rules()->m_ruleset_2da_cache[CRULES_HASHEDSTR("BARBARIAN_DAMAGE_REDUCTION_LEVEL_1")];
const auto dr2 = Globals::Rules()->m_ruleset_2da_cache[CRULES_HASHEDSTR("BARBARIAN_DAMAGE_REDUCTION_LEVEL_2")];
const auto dr3 = Globals::Rules()->m_ruleset_2da_cache[CRULES_HASHEDSTR("BARBARIAN_DAMAGE_REDUCTION_LEVEL_3")];
const auto dr4 = Globals::Rules()->m_ruleset_2da_cache[CRULES_HASHEDSTR("BARBARIAN_DAMAGE_REDUCTION_LEVEL_4")];
const auto dr5 = Globals::Rules()->m_ruleset_2da_cache[CRULES_HASHEDSTR("EPIC_BARBARIAN_DAMAGE_REDUCTION")];
Globals::Rules()->m_ruleset_2da_cache[CRULES_HASHEDSTR("BARBARIAN_DAMAGE_REDUCTION_LEVEL_1")] = CachedRulesetEntry {.s = "1", .i = 1, .f = 1};
Globals::Rules()->m_ruleset_2da_cache[CRULES_HASHEDSTR("BARBARIAN_DAMAGE_REDUCTION_LEVEL_2")] = CachedRulesetEntry {.s = "2", .i = 2, .f = 2};
Globals::Rules()->m_ruleset_2da_cache[CRULES_HASHEDSTR("BARBARIAN_DAMAGE_REDUCTION_LEVEL_3")] = CachedRulesetEntry {.s = "3", .i = 3, .f = 3};
Globals::Rules()->m_ruleset_2da_cache[CRULES_HASHEDSTR("BARBARIAN_DAMAGE_REDUCTION_LEVEL_4")] = CachedRulesetEntry {.s = "4", .i = 4, .f = 4};
Globals::Rules()->m_ruleset_2da_cache[CRULES_HASHEDSTR("EPIC_BARBARIAN_DAMAGE_REDUCTION")] = CachedRulesetEntry {.s = "5", .i = 5, .f = 5};

const auto retVal = s_DoDamageReductionHook->CallOriginal<int32_t>(thisPtr, pDamager, nDamage, nDamagePower, bSimulation, bCombatDamage, bRangedAttack);

Globals::Rules()->m_ruleset_2da_cache[CRULES_HASHEDSTR("BARBARIAN_DAMAGE_REDUCTION_LEVEL_1")] = dr1;
Globals::Rules()->m_ruleset_2da_cache[CRULES_HASHEDSTR("BARBARIAN_DAMAGE_REDUCTION_LEVEL_2")] = dr2;
Globals::Rules()->m_ruleset_2da_cache[CRULES_HASHEDSTR("BARBARIAN_DAMAGE_REDUCTION_LEVEL_3")] = dr3;
Globals::Rules()->m_ruleset_2da_cache[CRULES_HASHEDSTR("BARBARIAN_DAMAGE_REDUCTION_LEVEL_4")] = dr4;
Globals::Rules()->m_ruleset_2da_cache[CRULES_HASHEDSTR("EPIC_BARBARIAN_DAMAGE_REDUCTION")] = dr5;

return retVal;
}

return s_DoDamageReductionHook->CallOriginal<int32_t>(thisPtr, pDamager, nDamage, nDamagePower, bSimulation, bCombatDamage, bRangedAttack);
}, Hooks::Order::Early);
}

}

0 comments on commit a2ee7ac

Please sign in to comment.