Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tweaks: Add NWNX_TWEAKS_RANGED_WEAPONS_USE_ON_HIT_CAST_SPELL_ITEM_PROPERTIES #1641

Merged
merged 4 commits into from
Apr 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
https://github.com/nwnxee/unified/compare/build8193.35.37...HEAD

### Added
- Tweaks: added `NWNX_TWEAKS_RANGED_WEAPONS_USE_ON_HIT_CAST_SPELL_ITEM_PROPERTIES` which makes all bows, crossbows, and slings use On Hit: Cast Spell item properties (in addition to their ammunition).
- Tweaks: added `NWNX_TWEAKS_CAST_ALL_ON_HIT_CAST_SPELL_ITEM_PROPERTIES` which casts all On Hit: Cast Spell item properties on hit, instead of only the first property.
- Events: added skippable event `NWNX_ON_BROADCAST_SAFE_PROJECTILE_{BEFORE|AFTER}` which fires whenever a projectile VFX is created for ranged weapons and spells.
- Events: added skippable event `NWNX_ON_BROADCAST_ATTACK_OF_OPPORTUNITY_{BEFORE|AFTER}` which allows skipping a creature provoking attacks of opportunities from enemies.
Expand Down
1 change: 1 addition & 0 deletions Plugins/Tweaks/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ add_plugin(Tweaks
"UnhardcodeShields.cpp"
"UncapDamageResistanceDamageFlags.cpp"
"PreventAttackBonusBypassingReductions.cpp"
"RangedWeaponsUseOnHitCastSpellItemProperties.cpp"
"CastAllOnHitCastSpellItemProperties.cpp")
2 changes: 1 addition & 1 deletion Plugins/Tweaks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ Tweaks stuff. See below.
| `NWNX_TWEAKS_UNCAP_DAMAGE_RESISTANCE_DAMAGE_FLAGS` | true or false | Uncaps the compounded damage flags of EffectDamageResistance. |
| `NWNX_TWEAKS_FIX_RESOLVE_SPECIAL_ATTACK_DAMAGE` | true or false | Stops damage from being resolved when a special attack misses. |
| `NWNX_TWEAKS_FIX_TURD_EFFECT_UNLINKING` | true or false | Stops effects from unlinking when restored from a TURD due to their effect ID changing. |
| `NWNX_TWEAKS_RANGED_WEAPONS_USE_ON_HIT_CAST_SPELL_ITEM_PROPERTIES` | true or false | Makes all bows, crossbows, and slings use On Hit: Cast Spell item properties (in addition to their ammunition). |
| `NWNX_TWEAKS_CAST_ALL_ON_HIT_CAST_SPELL_ITEM_PROPERTIES` | true or false | Casts all On Hit: Cast Spell item properties on hit, instead of only the first property. |
|

## Environment variable values

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

#include "API/CNWSCreature.hpp"
#include "API/CNWSCombatRound.hpp"
#include "API/CNWSItem.hpp"
#include "API/CNWBaseItemArray.hpp"
#include "API/CNWBaseItem.hpp"
#include "API/CNWRules.hpp"

namespace Tweaks {

using namespace NWNXLib;
using namespace NWNXLib::API;

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

LOG_INFO("Ranged weapons will use On Hit: Cast Spell item properties");

static Hooks::Hook s_ResolveItemCastSpellHook = Hooks::HookFunction(&CNWSCreature::ResolveItemCastSpell,
(void*)+[](CNWSCreature *pCreature, CNWSObject *pTarget) -> void
{
if (auto *pCombatRound = pCreature->m_pcCombatRound)
{
if (auto *pWeapon = pCombatRound->GetCurrentAttackWeapon(pCombatRound->GetWeaponAttackType()))
{
auto nBaseItem = pWeapon->m_nBaseItem;

if (nBaseItem == Constants::BaseItem::HeavyCrossbow ||
nBaseItem == Constants::BaseItem::LightCrossbow ||
nBaseItem == Constants::BaseItem::Longbow ||
nBaseItem == Constants::BaseItem::Shortbow ||
nBaseItem == Constants::BaseItem::Sling)
{
pWeapon->m_nBaseItem = Constants::BaseItem::Shortsword;
s_ResolveItemCastSpellHook->CallOriginal<void>(pCreature, pTarget);
pWeapon->m_nBaseItem = nBaseItem;
}
}
}

s_ResolveItemCastSpellHook->CallOriginal<void>(pCreature, pTarget);

}, Hooks::Order::Earliest);
}

}