Skip to content

Commit

Permalink
Merge pull request #199 from nwnxee/master
Browse files Browse the repository at this point in the history
Merge upstream
  • Loading branch information
plenarius authored Nov 28, 2020
2 parents bc90513 + c96d594 commit 8b20080
Show file tree
Hide file tree
Showing 10 changed files with 128 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ https://github.com/nwnxee/unified/compare/build8193.16...HEAD
- Events: added Combat Enter/Exit events to CombatEvents
- Events: Stealth Mode can now bypass or perform Hide in Plain Sight with return values of "0" or "1" respectively
- Events: Added Skippable/Result Changeable Faction Reputation event to FactionEvents
- Experimental: Added `NWNX_EXPERIMENTAL_ADJUST_REPUTATION_FIX` in an effort to correct a crash with factions/reputation
- Tweaks: `NWNX_TWEAKS_HIDE_PLAYERS_ON_CHAR_LIST`
- Tweaks: `NWNX_TWEAKS_FIX_ARMOR_DEX_BONUS_UNDER_ONE`
- Tweaks: `NWNX_TWEAKS_FIX_ITEM_NULLPTR_IN_CITEMREPOSITORY`
Expand Down Expand Up @@ -48,6 +49,8 @@ The following plugins were added:
- Player: CloseStore()
- Race: SetFavoredEnemyFeat()
- Util: GetScriptParamIsSet()
- Util: SetDawnHour()
- Util: SetDuskHour()

### Changed
- Area: ExportGIT() now supports valid custom resource directory aliases.
Expand Down
1 change: 1 addition & 0 deletions Plugins/Experimental/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
add_plugin(Experimental
"Experimental.cpp"
"Experimentals/SuppressPlayerLoginInfo.cpp"
"Experimentals/AdjustReputationFix.cpp"
)
7 changes: 7 additions & 0 deletions Plugins/Experimental/Experimental.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "Experimental.hpp"
#include "Experimentals/SuppressPlayerLoginInfo.hpp"
#include "Experimentals/AdjustReputationFix.hpp"

#include "Services/Config/Config.hpp"

Expand All @@ -24,6 +25,12 @@ Experimental::Experimental(Services::ProxyServiceList* services)
LOG_INFO("EXPERIMENTAL: Suppressing playerlist and player login/logout messages for non DMs.");
m_SuppressPlayerLoginInfo = std::make_unique<SuppressPlayerLoginInfo>(GetServices()->m_hooks.get());
}

if (GetServices()->m_config->Get<bool>("ADJUST_REPUTATION_FIX", false))
{
LOG_INFO("EXPERIMENTAL: Attempting to resolve faction/reputation crash.");
m_AdjustReputationFix = std::make_unique<AdjustReputationFix>(GetServices()->m_hooks.get());
}
}

Experimental::~Experimental()
Expand Down
2 changes: 2 additions & 0 deletions Plugins/Experimental/Experimental.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Experimental {

class SuppressPlayerLoginInfo;
class AdjustReputationFix;

class Experimental : public NWNXLib::Plugin
{
Expand All @@ -14,6 +15,7 @@ class Experimental : public NWNXLib::Plugin

private:
std::unique_ptr<SuppressPlayerLoginInfo> m_SuppressPlayerLoginInfo;
std::unique_ptr<AdjustReputationFix> m_AdjustReputationFix;
};

}
52 changes: 52 additions & 0 deletions Plugins/Experimental/Experimentals/AdjustReputationFix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include "Experimentals/AdjustReputationFix.hpp"

#include "Services/Hooks/Hooks.hpp"

#include "API/CAppManager.hpp"
#include "API/CFactionManager.hpp"
#include "API/CNWSCreature.hpp"
#include "API/CNWSCreatureStats.hpp"
#include "API/CServerExoApp.hpp"
#include "API/CServerExoAppInternal.hpp"
#include "API/Functions.hpp"
#include "API/Globals.hpp"

namespace Experimental {

using namespace NWNXLib;
using namespace NWNXLib::API;

AdjustReputationFix::AdjustReputationFix(Services::HooksProxy* hooker)
{
hooker->RequestExclusiveHook<API::Functions::_ZN12CNWSCreature16AdjustReputationEii, void>(&CNWSCreature__AdjustReputation_Hook);
}

void AdjustReputationFix::CNWSCreature__AdjustReputation_Hook(CNWSCreature *pThis, int32_t nFactionId, int32_t nAdjustment)
{
auto* pFactionManager = Globals::AppManager()->m_pServerExoApp->m_pcExoAppInternal->m_pFactionManager;

if (!pFactionManager->GetIsNPCFaction(nFactionId))
return;

if (pThis->m_bPlayerCharacter || pThis->GetIsPossessedFamiliar())
{
if (pThis->m_pReputation)
{
int32_t index = nFactionId - 1;
if (index >= 0 && index < pThis->m_pReputation->num)
{
pThis->m_pReputation->element[index] += nAdjustment;
pThis->m_pReputation->element[index] = std::clamp(pThis->m_pReputation->element[index], 0, 100);
}
}
}
else
{
if (pThis->m_pStats->m_nFactionId != nFactionId)
{
int32_t rep = pFactionManager->GetNPCFactionReputation(pThis->m_pStats->m_nFactionId, nFactionId);
pFactionManager->SetNPCFactionReputation(pThis->m_pStats->m_nFactionId, nFactionId, (rep + nAdjustment));
}
}
}
}
16 changes: 16 additions & 0 deletions Plugins/Experimental/Experimentals/AdjustReputationFix.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

#include "Common.hpp"

namespace Experimental {

class AdjustReputationFix
{
public:
AdjustReputationFix(NWNXLib::Services::HooksProxy* hooker);

private:
static void CNWSCreature__AdjustReputation_Hook(CNWSCreature*, int32_t, int32_t);
};

}
1 change: 1 addition & 0 deletions Plugins/Experimental/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ The following environmental variable is required to load the plugin:
| Variable Name | Value | Notes |
| ------------- | :----: | ------------------------------------ |
| `NWNX_EXPERIMENTAL_SUPPRESS_PLAYER_LOGIN_INFO` | true/false | Suppresses the playerlist and player login/logout messages for all players except DMs. This functionality is not compatible with NWNX_Rename. |
| `NWNX_EXPERIMENTAL_ADJUST_REPUTATION_FIX` | true/false | Attempts to correct a crash involving faction/reputations. |
24 changes: 24 additions & 0 deletions Plugins/Util/NWScript/nwnx_util.nss
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,14 @@ string NWNX_Util_GetResourceOverride(int nResType, string sName);
/// @return TRUE if the script param is set, FALSE if not or on error.
int NWNX_Util_GetScriptParamIsSet(string sParamName);

/// @brief Set the module dawn hour.
/// @param nDawnHour The new dawn hour
void NWNX_Util_SetDawnHour(int nDawnHour);

/// @brief Set the module dusk hour.
/// @param nDuskHour The new dusk hour
void NWNX_Util_SetDuskHour(int nDuskHour);

/// @}

string NWNX_Util_GetCurrentScriptName(int depth = 0)
Expand Down Expand Up @@ -540,3 +548,19 @@ int NWNX_Util_GetScriptParamIsSet(string sParamName)

return NWNX_GetReturnValueInt(NWNX_Util, sFunc);
}

void NWNX_Util_SetDawnHour(int nDawnHour)
{
string sFunc = "SetDawnHour";

NWNX_PushArgumentInt(NWNX_Util, sFunc, nDawnHour);
NWNX_CallFunction(NWNX_Util, sFunc);
}

void NWNX_Util_SetDuskHour(int nDuskHour)
{
string sFunc = "SetDuskHour";

NWNX_PushArgumentInt(NWNX_Util, sFunc, nDuskHour);
NWNX_CallFunction(NWNX_Util, sFunc);
}
20 changes: 20 additions & 0 deletions Plugins/Util/Util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ Util::Util(Services::ProxyServiceList* services)
REGISTER(SetResourceOverride);
REGISTER(GetResourceOverride);
REGISTER(GetScriptParamIsSet);
REGISTER(SetDawnHour);
REGISTER(SetDuskHour);

#undef REGISTER

Expand Down Expand Up @@ -799,4 +801,22 @@ ArgumentStack Util::GetScriptParamIsSet(ArgumentStack&& args)
return Services::Events::Arguments(retVal);
}

ArgumentStack Util::SetDawnHour(ArgumentStack &&args)
{
const auto dawnHour = Services::Events::ExtractArgument<int32_t>(args);
ASSERT_OR_THROW(dawnHour >= 0);
ASSERT_OR_THROW(dawnHour <= 23);
Utils::GetModule()->m_nDawnHour = dawnHour;
return Services::Events::Arguments();
}

ArgumentStack Util::SetDuskHour(ArgumentStack &&args)
{
const auto duskHour = Services::Events::ExtractArgument<int32_t>(args);
ASSERT_OR_THROW(duskHour >= 0);
ASSERT_OR_THROW(duskHour <= 23);
Utils::GetModule()->m_nDuskHour = duskHour;
return Services::Events::Arguments();
}

}
2 changes: 2 additions & 0 deletions Plugins/Util/Util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ class Util : public NWNXLib::Plugin
ArgumentStack SetResourceOverride (ArgumentStack&& args);
ArgumentStack GetResourceOverride (ArgumentStack&& args);
ArgumentStack GetScriptParamIsSet (ArgumentStack&& args);
ArgumentStack SetDawnHour (ArgumentStack&& args);
ArgumentStack SetDuskHour (ArgumentStack&& args);

size_t m_resRefIndex;
std::vector<std::string> m_listResRefs;
Expand Down

0 comments on commit 8b20080

Please sign in to comment.