Skip to content

Commit

Permalink
Merge pull request #1386 from Kalopsia-dev/Kalo-SetClassUpdatesLevels
Browse files Browse the repository at this point in the history
SetClassByPosition: Also replace old class references in CNWLevelStats
  • Loading branch information
mtijanic authored Jul 18, 2021
2 parents 66cfda3 + 78dba9e commit a3ce233
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ https://github.com/nwnxee/unified/compare/build8193.29...HEAD
- Object: SetLastTriggered()

### Changed
- N/A
- ***ABI BREAKING*** Creature: SetClassByPosition by default replaces all occurrences of the old class in CNWLevelStats. This can be disabled with the argument 'bUpdateLevels'.

### Deprecated
- Weapon: SetWeaponIsMonkWeapon()
Expand Down
15 changes: 15 additions & 0 deletions Plugins/Creature/Creature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1047,12 +1047,27 @@ NWNX_EXPORT ArgumentStack SetClassByPosition(ArgumentStack&& args)
{
const auto position = args.extract<int32_t>();
const auto classID = args.extract<int32_t>();
const auto bUpdateLevels = args.extract<int32_t>();
ASSERT_OR_THROW(position >= 0);
ASSERT_OR_THROW(position <= 2);
ASSERT_OR_THROW(classID >= Constants::ClassType::MIN);
ASSERT_OR_THROW(classID <= Constants::ClassType::MAX);

// Save the old class id, then replace it with the new one
const auto classIDold = pCreature->m_pStats->GetClass(position);
pCreature->m_pStats->SetClass(position, classID);

if (bUpdateLevels)
{
auto& levelStats = pCreature->m_pStats->m_lstLevelStats;
for (auto *level : levelStats)
{
if (level->m_nClass == classIDold)
{
level->m_nClass = static_cast<uint8_t>(classID);
}
}
}
}
return {};
}
Expand Down
7 changes: 5 additions & 2 deletions Plugins/Creature/NWScript/nwnx_creature.nss
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,9 @@ void NWNX_Creature_SetSkillRank(object creature, int skill, int rank);
/// @param position Should be 0, 1, or 2 depending on how many classes the creature
/// has and which is to be modified.
/// @param classID A valid ID number in classes.2da and between 0 and 255.
void NWNX_Creature_SetClassByPosition(object creature, int position, int classID);
/// @param bUpdateLevels determines whether the method will replace all occurrences
/// of the old class in CNWLevelStats with the new classID.
void NWNX_Creature_SetClassByPosition(object creature, int position, int classID, int bUpdateLevels = TRUE);

/// @brief Set the level at the given position for a creature.
/// @note A creature should already have a class in that position.
Expand Down Expand Up @@ -1462,9 +1464,10 @@ void NWNX_Creature_SetSkillRank(object creature, int skill, int rank)
NWNX_CallFunction(NWNX_Creature, sFunc);
}

void NWNX_Creature_SetClassByPosition(object creature, int position, int classID)
void NWNX_Creature_SetClassByPosition(object creature, int position, int classID, int bUpdateLevels = TRUE)
{
string sFunc = "SetClassByPosition";
NWNX_PushArgumentInt(bUpdateLevels);
NWNX_PushArgumentInt(classID);
NWNX_PushArgumentInt(position);
NWNX_PushArgumentObject(creature);
Expand Down

0 comments on commit a3ce233

Please sign in to comment.