nwnxee / unified

Binaries available under the Releases tab on Github
https://nwnxee.github.io/unified
GNU General Public License v3.0
131 stars 92 forks source link

NWNX_Creature_GetSpecialAbilityCount doesn't count removed abilities (with id = -1) #1757

Open TomRotfl opened 6 months ago

TomRotfl commented 6 months ago

When I add 2 or more abilities with different id values, if I don't remove them sequentially (last to first), but I remove the first I added as first, and then I try to remove the second, this last one will have an id value of -1 if checked, and can't be removed.

To reproduce:

  1. add a spell ability with id 880 (totally random)
  2. add another spell ability with id 881
  3. loop through the spell abilities and remove the one matching with id 880
  4. loop again, and you won't find the id 881, but you will find an ability with id -1, that can't be removed

NWNX_Creature_RemoveSpecialAbility probably doesn't reorder the list of available abilities, so on the next loop it finds an empty value and stops there (just guessing from a conversation on discord).

Daztek commented 6 months ago

It's because for some reason GetSpecialAbilityCount() doesn't count the removed ones (id = -1), so in your case it'll return an amount of 1 special abilities, which you would assume to be at index 0, but it is in fact at index = 1, since when you "remove" one it just sets the ID to -1 so its spot can be reused at a later time.

So, for your repro steps:

  1. The array looks like this: [880] and GetSpecialAbilityCount() returns 1 and the special ability is at index 0
  2. The array looks like this: [880, 881] and GetSpecialAbilityCount() returns 2 and the special abilities are at index 0 and 1
  3. The array looks like this: [-1, 881] and GetSpecialAbilityCount() returns 1, so you assume the valid spellid is at index 0, but it's actually at index 1.
TomRotfl commented 6 months ago

Interesting! Adjusting the title name to reflect the real bad guy here