I’m back and working on the next version of the Companion and Monster AI

First one important question, how do I get access to my content on this site from the migrate wizard?

I am well into a version 2.3 with the following changes:

  1. Animal companion and familiar flee options when hurt or facing a challenging enemy.

  2. Animal companion and familiar inventory access.

  3. Updates for Kaedrin’s PrC pack 1.41.4.

  4. Add support for casting free or swift actions in AI. Golem gas breath is now set to use this. (along with unlimited use every 1d4 rounds)

  5. Improved support for stealthed, invisible, and ethereal opponents and allies.

  6. Reworked monster heartbeat animations. Added support for avian, herbivore, and omnivore behavior. Monsters will return to starting location after combat. Added global option for monster heal and rest outside of combat.

  7. Some bugs have been fixed too.

  8. A few other things will probably get added.

At this point I have removed the multiple summons support but may get back to it for a later release.


Ask @Fester_Pot. There used to be a specific thread for that but I can’t remember what it was called and it hasn’t been used in ages.


1 Like

That’s a very good news!
Thanks a lot for your amazing works some of used and are using. :slight_smile:

I just uploaded version 2.3:.https://neverwintervault.org/project/nwn2/script/companion-and-monster-ai-23

I plan on working on supporting metamagic for spontaneous spellcasting classes (bard, sorcerer, favored soul, and spirit shaman). I have prototyped this and believe the game engine will support it with the following restrictions: can’t be used with mixed spellcasting classes except warlock and spells with subspells will not use metamagic. This will require a fair amount of work finish up.

Some other things I noticed while testing – probably should try to do something about pathfinding issues and persistent spell handling. The multi summons support has some bugs in it and requires a fair amount of work to finish so I might delay doing that.

I put the bug and feature list into the downloads. This thread can be used for requests for things to do.


Congrats! I’ve used your AI before. I’ll be sure to check this out.

Implementation of metamagic for spontaneous casters is almost complete. I found one other game limitation – at least one remaining casting for a spell level must exist for ActionCastSpellx to apply metamagic and work vs. stand there and do nothing.

Support for all metamagics done except for quicken – not sure how it should get used. Silent spell is used when silenced, still spell if there is arcane spell failure risk, extend spell for short and medium duration buffs, empower and maximize for damage dealing attacks, inflicting, and healing. Persistent spell is only used during buffs for haste and divine favor treating them as long term buffs. Maximize vs. empower is determined by how much maximize increases damage when the henchspells.2da is created, the damage factor must be 1.65x or greater. The AI will also works around the game issue by trying to use metamagic if only one spell level cast is available – useful for buffs or repeating a good damage spell again and again. Epic auto metamagics are accounted for and if the spell does not have a verbal or somatic component.

I view this as a significant improvement for the AI so will probably release with just this one enhancement. During testing, Qara was much more effective casting maximized fireball, ice storm, and flame arrow and empowered delayed blast fireball and magic missile. The spell selection also seems better, there is a randomness to the earlier talent based system that could skip good spells like protection from good/evil. If possible, using a similar scheme may improve the spell selection for non spontaneous casters – this will be looked at after the release.

There are a few of unrelated bugs and issues to look at before release – dispel is cast too much and one case when it shouldn’t be – target is silenced.


if (is under melee attack by 2+ enemy)


Something like that, but it is probably better to use challenge rating instead. Since quickened spells take a half round (3 seconds) instead of instant like PnP, they are not so useful. The AI framework supports quicken and if some simple rules can be defined it can be put in. I did this with persistent spell where only two spells when buffing are used. This idea came from the nwn2 fandom site: https://nwn2.fandom.com/wiki/Persistent_Spell. Support for quicken metamagic can be always be done later.

The AI framework also checks for the Arcane Scholar of Candlekeep improved metamagic feats. I had to test these out since getting the spell level increase wrong or the game not supporting it correctly will mean the AI controlled creature could stand around trying to cast something it can’t.

  • edited to get quote in.
1 Like

hm, tough one…

question: Are you using any randomization when 2+ spells (metamagic or otherwise) weight roughly equally, or is the algo purely deterministic at present?

( im thinking that Quicken, when appropriate, could be given a slight percentage to cast instead of another, roughly equally appropriate, spell )

There isn’t any randomness in the AI for the effect weighting to decide what spells to use. However, the spell weighting is changed quite a bit already by monster and PC party positions and relative health. As part of testing the AI, I run certain saved game battles and the spells used can vary a fair amount based on how the battle proceeds.

Quicken might be implemented as only certain spells if they would be cast anyway are quickened. Or a boost to the quickened spell weighting could be added to get the spell used more.


sounds good,

So i’ve been wondering, why/when would i cast a quickened spell?

  • when a tough mob is running at me and I want a damage shield (or similar) up fast.
  • when multiple high CR melee-types are hitting on me and I want to increase the odds of making a Concentration check (3sec round vs 6sec round?)
  • or if im a non-spontaneous caster, sometimes i use a metamagic slot for a given spell if all slots of its non-metamagic spell-level are taken …

/just ideas

I just uploaded version 2.4: https://neverwintervault.org/project/nwn2/script/companion-and-monster-ai-24

From the release notes:
New for version 2.4: Metamagic support for spontaneous casters: sorcerers, bards, favored souls and spirit shamans. Spells with sub spells (i.e. protection from alignment) will get cast but with no metamagic due to game limitation. All other metamagic should get used when appropriate except for quicken. Persistent spell is only used for long term buffing outside of combat for haste and divine favor. Rework dispel usage to debuff enemies and reduce chance of TMI errors. Rework handling of area of effect spells. Now more likely to get out of area even if in melee combat and will attempt to move to a better location instead of just directly moving away from area of effect spell.

No quicken metamagic – at least for now. Also one other thing I didn’t do was check downgrading the metamagic if the extra damage isn’t needed. This is a rare case, but the AI does things like this already like using magic missile to finish off an enemy.

I will next look at using a similar casting scheme for casters with memorized spells. It may help with casting of sub spells, something the existing talent based system can fail doing because the talents returned are random.

HenchMeleeAttack() in 'hench_i0_attack'

int bNoTrueSight = !(iPosEffectsOnSelf & HENCH_EFFECT_TYPE_TRUESEEING);

while (GetIsObjectValid(oTestTarget))
    if (bNoTrueSight || !(GetLocalInt(oTestTarget, curPostiveEffectsStr) & HENCH_EFFECT_TYPE_ETHEREAL))
        // calculate 'curThreatRating'

wondering if that’s a typo: as is, it seems to be saying “If i don’t have TrueSight, consider it, or if the threat is not Ethereal, consider it” (ie. consider an Ethereal threat even if I don’t have TrueSight)

instead of “If i have TrueSight, consider it, or if the threat is not Ethereal, consider it”.


This looks correct to me and has to do with the strange way ethereal creatures are handled by the game. Ethereal creatures are returned as seen in the GetNearestCreature and GetObjectSeen calls with true sight but they can’t be directly targeted with ActionAttack or ActionCastSpellAtObject. So the if test is checking the “seen” creature can really be attacked – either no true sight (by far the most common case and the short circuit for the if test) or if the creature is not ethereal with true sight then go ahead and directly attack them. What happens with the AI now is that if the ethereal creature cannot be attacked directly, they are hit with an area of effect spell or are followed closely. I did the test with a red dragon and it would follow and breath fire every 1d4 rounds making for a much more deadly encounter. These changes are part of version 2.3. Before these revisions, the red dragon attempted ActionAttacks with power mode that would fail.


ok that kinda makes sense o.O – i recall that etherealness (or sanctuary i forget atm) could have been handled better in the hardcode …

thanks T.

HenchMeleeAttackBuff() in 'hench_i0_buff'

int spellInfo = GetCurrentSpellSaveType();

if (spellInfo & 0x2) // animal check [#1]
    int nRacialType = GetRacialType(oFriend);
    racialCheck = nRacialType == RACIAL_TYPE_ANIMAL || nRacialType == RACIAL_TYPE_BEAST;
else if (!(spellInfo & 0x4)) // humanoid check [#2]
    racialCheck = GetIsHumanoid(oFriend) || GetSubRace(oFriend) == RACIAL_SUBTYPE_GITHYANKI || GetSubRace(oFriend) == RACIAL_SUBTYPE_GITHZERAI;

if (racialCheck)
    // if(etc.) calculate 'maxEffectWeight' of buff
#1 - 0x2 appears to be HENCH_ATTACK_CHECK_NEG_HEALING in 'hench_i0_attack'

#2 - 0x4 appears to be HENCH_ATTACK_CHECK_NOT_ALREADY_EFFECTED in 'hench_i0_attack'
   - and i don't understand the NOT condition there

unless im misreading things, the HENCH_ATTACK_CHECK_* constants aren’t even bitwised …

I’m just saying please have another look at that function; if it makes sense to you it’s good enough for me :)

ps. ditto re. the reduce-flag (spellInfo & 0x1) etc.

The script looks OK. I reused the attack spell save type in the buff spells for other checks so this one does not match the other uses. There also are not any defines made for this special case with only a few spells. Here is a picture from the utility that creates the AI 2das for these spells:

I didn’t even make a special column in the utility, the raw numbers are used for the bits.

  • Bit 1 Use reduce instead of enlarge

  • Bit 2 Check for animal instead of humanoid

  • Bit 3 Don’t do humanoid check

1 Like

ah, thanks very much

And i guess the ‘trigger’ for those is HENCH_SPELL_INFO_SPELL_TYPE_MELEE_ATTACK_BUFF in the SpellInfo col (SpellType bits)

May I ask for the future version request: Companions (and enemies) to use items that have item properties abilities (i.e. helmet which has ability 3times/day searing light while is equipped on the head)(if the battle situation is appropriate to use this ability)

As far as I know, the game engine does not have script functions that allow the casting of spells from equipped items. I recently reran tests to confirm this behavior (trying various work arounds) and the rules seem to be:

  • No casting for items that can be equipped, determined by base item type. Change the base item type to wand and the same item works from inventory.

  • For magic items that can not be equipped, again determined by base item type (wand, magic rod, miscellaneous items) must have at least one charge to work. Wand of magic missiles with charges – works. Scabbard of Blessing (miscellaneous item with uses per day of aid and bless) does not work unless one charge is added to it. The AI could automatically add one charge to such items, but when examined the single charge is shown (no workaround as far as I know). Could be done as a global option.

  • I have not tested but scrolls and potions probably only work as single charge spells. A protection from alignment scroll will not work because the castable sub spells are not returned.

There is an AI global setting that “allows” monsters to use items that can be equipped. What it does is find equipped items with castable spells and copy the spellcasting properties onto a newly created wand. The wand is given the same name and made non-droppable so it appears as if the item is being used in the combat log and the wand will not show up on the corpse.

Note: NWN EE added some functions that might allow the item casting.

A progress update on the next release

Because of issues with cleric (cure/inflict) and druid (summoning) spontaneous casting, the talent based system will still be used for most spellcasters. A change should allow sub spells to be more reliably returned especially the element protection spells that have five sub spell variations each. Warlocks will use the spontaneous spellcasting system because it will now use the correct spell level and does not have the conflict with cleric and druid spontaneous casting.

I put in a workaround to fix a bug in the game - spell resistance on items only working once. An effect is created for the highest spell resistance item found. Also, the icon that would normally show for the effect by the portrait is removed. This workaround includes various monsters that have the spell resistance on the creature hide (dragons, devils, demons, etc.).

I found out how to cast clerical domain spells but it would be difficult to implement in the AI.

Currently working on scheme that will allow dragons to be set up as sorcerers in the toolset but allowing the clerical spells that some types are allowed to be cast as special abilities. The clerical spell uses would be determined by the corresponding sorcerer spell level uses left. This is an extension of the spontaneous metamagic casting system done for 2.4. As part of testing, the very old red dragon creature template in the toolset had various issues that were fixed – wrong spell resistance, saves, etc. Might also give it some more feats to make up for all the standard ones omitted because they are not implemented in NWN2.