Author Topic: NPCs not using their offensive spells.  (Read 425 times)

Legacy_Ivanovich

  • Full Member
  • ***
  • Posts: 142
  • Karma: +0/-0
NPCs not using their offensive spells.
« on: November 12, 2011, 02:34:42 am »


               I've tried to figure out what the heck could be causing this, but to no avail.  Insight, if any exists, would be most welcome.

I have spell caster NPCs - orcs, minotaurs, etc.  Makes no matter.  When they spawn, they will melee, but not use their spells.  I'm trying to make them sit back and cast offensive spells rather than rush into melee with the npcs that are designed for melee.

I've confirmed the INT and/or WIS of said creature is high enough.  I've set the AI to the creature to High onspawn, just to make sure it wasn't that.  Still, nothing.  Here is the creature's onspawn...

#include "x0_i0_anims"
#include "nw_i0_plot"
#include "NW_I0_GENERIC"
void main()
{

object oNPC = OBJECT_SELF;
object oTarget = GetNearestCreature(PLAYER_CHAR_IS_PC, TRUE, oNPC, 1, CREATURE_TYPE_IS_ALIVE, TRUE, CREATURE_TYPE_DOES_NOT_HAVE_SPELL_EFFECT, 88);
location lTarget;

SetAILevel(OBJECT_SELF, AI_LEVEL_HIGH);

lTarget = GetLocation(oTarget);
ActionMoveToLocation(lTarget,TRUE);
AssignCommand (oNPC, ActionAttack(oTarget));

SetCombatCondition(X0_COMBAT_FLAG_RANGED);
SetLocalInt(GetArea(OBJECT_SELF), "X2_L_WILD_MAGIC", TRUE);
}



Any ideas at all?
               
               

               
            

Legacy_Shadooow

  • Hero Member
  • *****
  • Posts: 7698
  • Karma: +0/-0
NPCs not using their offensive spells.
« Reply #1 on: November 12, 2011, 02:42:08 am »


               AI level is unused by default. Make also sure that your creature have combat casting feat. If that wont help try my unofficial patch that should help.
               
               

               


                     Modifié par ShaDoOoW, 12 novembre 2011 - 02:42 .
                     
                  


            

Legacy_Ivanovich

  • Full Member
  • ***
  • Posts: 142
  • Karma: +0/-0
NPCs not using their offensive spells.
« Reply #2 on: November 12, 2011, 02:50:33 am »


               The creature has combat casting. What about your unofficial patch will address this?  

I had NPCs casting all spells at one point.  Seems they don't now.
               
               

               


                     Modifié par Ivanovich, 12 novembre 2011 - 02:52 .
                     
                  


            

Legacy_Shadooow

  • Hero Member
  • *****
  • Posts: 7698
  • Karma: +0/-0
NPCs not using their offensive spells.
« Reply #3 on: November 12, 2011, 03:01:49 am »


               *sigh* this issue was already discussed there somewhere but due to bad search engine here its hard to find but sorry Im not in mood to waste my time and explain how I fixed it in my patch as its not one but more issues together when you have a chance to install my patch and test it. Or you have some special reason not to use it? If it would not work then I take the effort to find the cause.

Anyway, I just noticed you used a SetCombatCondition(X0_COMBAT_FLAG_RANGED);
Im not sure but I think this will force creatures to use their ranged weapon, not to cast spells
               
               

               


                     Modifié par ShaDoOoW, 12 novembre 2011 - 03:02 .
                     
                  


            

Legacy_Ivanovich

  • Full Member
  • ***
  • Posts: 142
  • Karma: +0/-0
NPCs not using their offensive spells.
« Reply #4 on: November 12, 2011, 03:05:40 am »


               I removed the Combat Flag Ranged, and he will now cast defensive spells, not offensive.  That's odd.  

As for your patch, I am not about to install a patch I have no knowledge about in terms of what it fixes/alters.  I would like to read up on the changes.  Is there a change log somewhere?  If not, I apologize if you think it is rude, but without that knowledge, I'm not going to install an unknown patch to anything on my machine.

Thank you for your help on the ranged aspect, that got me partially there.
               
               

               
            

Legacy_Shadooow

  • Hero Member
  • *****
  • Posts: 7698
  • Karma: +0/-0
NPCs not using their offensive spells.
« Reply #5 on: November 12, 2011, 03:10:29 am »


               As Im looking to your script more closely, where you get it? Why do you setup wild magic area? And why do you telling your creature to attack (melee) target? nw_c2_default9 should work.

As for my patch, here is strict list of changes, more detailed info is in in the documentation link in my signature.
               
               

               


                     Modifié par ShaDoOoW, 12 novembre 2011 - 03:11 .
                     
                  


            

Legacy_wyldhunt1

  • Sr. Member
  • ****
  • Posts: 443
  • Karma: +0/-0
NPCs not using their offensive spells.
« Reply #6 on: November 12, 2011, 03:34:23 am »


               This part is the problem:
---------------------------------------------------------------------------------------
//Here, you grab the location of the PC that is about to be attacked
lTarget = GetLocation(oTarget);
//Now, you move to the location of the PC.
//Note that the AI tends to not cast when it will provoke an AOO.
//Casters need to stay back. Don't tell them to charge.
ActionMoveToLocation(lTarget,TRUE);
//ActionAttack is used to force the NPC to melee attack.
//Since they were just ordered to charge, it makes sense to stab.
//This, however, doesn't work well for mages.
AssignCommand (oNPC, ActionAttack(oTarget));
//This flag is used in the standard AI scripts
//which may not be getting called if the creatures are simply being told to charge and stab.
SetCombatCondition(X0_COMBAT_FLAG_RANGED);
------------------------------------------------------------------------------------
My conclusion is that you need to remove the lines telling the NPC to charge and attack on spawn.
As it is, any time the NPC spawns in the same area as a PC, they will charge and stab.
If you want to trigger your combat AI immediately, you need to do a check to search for enemies nearby and then execute your AI scripts.
You may be able to get away executing your HB script one time at the end of your OnSpawn script.
IE:
---------------------------------------------------------------------------------------
#include "x0_i0_anims"
#include "nw_i0_plot"
#include "NW_I0_GENERIC"
void main()
{
SetAILevel(OBJECT_SELF, AI_LEVEL_HIGH);

SetCombatCondition(X0_COMBAT_FLAG_RANGED);
SetLocalInt(GetArea(OBJECT_SELF), "X2_L_WILD_MAGIC", TRUE);
ExecuteScript("MyHBScript", OBJECT_SELF);
}
------------------------------------------------------------------------------------

Just replace MyHbScript with whatever you're using on HB. That generally checks for enemies nearby and reacts.
               
               

               


                     Modifié par wyldhunt1, 12 novembre 2011 - 04:11 .
                     
                  


            

Legacy_Ivanovich

  • Full Member
  • ***
  • Posts: 142
  • Karma: +0/-0
NPCs not using their offensive spells.
« Reply #7 on: November 12, 2011, 03:41:56 am »


               The reason I tell them to move to the PC is I want them to attack on spawn, whereas if I just stay with the default, they wander aimlessly until they see the PC.  The PC's range to see them is larger than their range to see the PC, even at "long" range set in their properties.

As for wild magic, it's the only way to make it so magic harms everyone (including NPC on NPC).

Thx for the info, and thx to wyldhunt too!
               
               

               
            

Legacy_wyldhunt1

  • Sr. Member
  • ****
  • Posts: 443
  • Karma: +0/-0
NPCs not using their offensive spells.
« Reply #8 on: November 12, 2011, 03:51:08 am »


               I updated my last post.
I'm at work, so I have to post in small chunks. '<img'>
               
               

               
            

Legacy_wyldhunt1

  • Sr. Member
  • ****
  • Posts: 443
  • Karma: +0/-0
NPCs not using their offensive spells.
« Reply #9 on: November 12, 2011, 04:05:17 am »


               Running the heartbeat script on spawn will create an immediate response and AI instead of waiting for the next HB to trigger. I would try that first to see if it's enough. It makes a huge difference.
If you're worried about the creature’s range of vision, then you would need to do a check for the nearest PC, and compare that to the max range that you want the NPC to react to them at. You don't want an NPC spawning on the other side of the dungeon trying to immediately attack. They'll get stuck on a wall or waste all of their spells.
I don't have my toolset right now, so this isn't real code. It would need to do something like this:
-----------------------------------------------------------------------------------
object oTarget = GetNearestCreature(PLAYER_CHAR_IS_PC, TRUE, oNPC, 1, CREATURE_TYPE_IS_ALIVE, TRUE, CREATURE_TYPE_DOES_NOT_HAVE_SPELL_EFFECT, 88);
if (GetDistanceBetween(OBJECT_SELF, oTarget)<30)
{
  //Here, you'll need to open your AI scripts.
  //Most of them use a variable to keep track of the NPC's current target.
  //Find that variable and set it manually here
  SetlLocalObject(OBJECT_SELF, "CurrentTarget", oTarget);

  //Manually trigger the AI script.
  //Note that you may need to trigger a late part of the script, after it checks for targets
  //Otherwise, it may erase your variable if it can't see them.
  //You'll need to see what the script calls after it finds a valid target to attack
  CallCombatAI();
}
               
               

               
            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
NPCs not using their offensive spells.
« Reply #10 on: November 12, 2011, 04:07:12 am »


               

Ivanovich wrote...

The reason I tell them to move to the PC is I want them to attack on spawn, whereas if I just stay with the default, they wander aimlessly until they see the PC. The PC's range to see them is larger than their range to see the PC, even at "long" range set in their properties.

As for wild magic, it's the only way to make it so magic harms everyone (including NPC on NPC).

Thx for the info, and thx to wyldhunt too!



For NPC damage on NPC. you may just want to try the switch in X2_inc_switches:

//------------------------------------------------------------------------------
// * Spell Targeting: Pre Hordes of the underdark, in hardcore mode, creatures would not hurt each other
// * with their AOE spells if they were no PCs. Setting this switch to true, will activate the correct
// * behaviour. Activating this on older modules can break things, unless you know what you are doing!
//------------------------------------------------------------------------------
const string MODULE_SWITCH_ENABLE_NPC_AOE_HURT_ALLIES = "X2_SWITCH_ENABLE_NPC_AOE_HURT_ALLIES";


I have never used it before so can not vouch for the results.  


I also agree that you should not use ActionAttack in a OnSpawn script.  there are really few places it should be used outside of the AI.   using it tells the PC what to do and does not allow your AI to make the decision on what to do.   When you are tempted to use ActionAttacK  and you are not writting an AI script.   Replace it with: DetermineCombatRound(oTarget);   and let the AI take it from there.
               
               

               
            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
NPCs not using their offensive spells.
« Reply #11 on: November 12, 2011, 04:10:58 am »


               

wyldhunt1 wrote...

Running the heartbeat script on spawn will create an immediate response and AI instead of waiting for the next HB to trigger. I would try that first to see if it's enough. It makes a huge difference.
If you're worried about the creature’s range of vision, then you would need to do a check for the nearest PC, and compare that to the max range that you want the NPC to react to them at. You don't want an NPC spawning on the other side of the dungeon trying to immediately attack. They'll get stuck on a wall or waste all of their spells.
I don't have my toolset right now, so this isn't real code. It would need to do something like this:
-----------------------------------------------------------------------------------
object oTarget = GetNearestCreature(PLAYER_CHAR_IS_PC, TRUE, oNPC, 1, CREATURE_TYPE_IS_ALIVE, TRUE, CREATURE_TYPE_DOES_NOT_HAVE_SPELL_EFFECT, 88);
if (GetDistanceBetween(OBJECT_SELF, oTarget)<30)
{
//Here, you'll need to open your AI scripts.
//Most of them use a variable to keep track of the NPC's current target.
//Find that variable and set it manually here
SetlLocalObject(OBJECT_SELF, "CurrentTarget", oTarget);

//Manually trigger the AI script.
//Note that you may need to trigger a late part of the script, after it checks for targets
//Otherwise, it may erase your variable if it can't see them.
//You'll need to see what the script calls after it finds a valid target to attack
CallCombatAI();
}



DetermineCombatRound(oTarget);   Will enter the AI and feed it the correct target.