Author Topic: NPC pursuit across an area transition  (Read 578 times)

Legacy_henesua

  • Hero Member
  • *****
  • Posts: 6519
  • Karma: +0/-0
NPC pursuit across an area transition
« on: August 05, 2012, 02:48:09 am »


               What is the secret to enabling NPCs to chase a PC across an area transition?

I do have this set in OnModuleLoad
SetModuleSwitch (MODULE_SWITCH_ENABLE_CROSSAREA_WALKWAYPOINTS, TRUE); 

And I am using a custom Area Transition that has this script in the OnEnter event for the trigger. This is the seamless area transition with a few minor changes which I introduced.

Is there something wrong with my area_trans script?
Or do I need to do something else to the creature's AI?

EDIT -- I wanted to add that I figured out what the problem is.

A default area transition trigger typically has a transition target. Even with other scripts like area_trans (seamless area transitions) added to a default area transition's click or onenter events, as long as the customized area transition has a destination target in the same area that the PC ultimately ends up, the creature/NPC will follow.

Example: create a standard area transition in Area A. In Area B create a way point with tag "dst_testwp". Assign the destination waypoint of the standard area transition to "dst_testwp".

As expected creatures will follow across this transition when in pursuit.

Next add the area_trans script to the trigger's onEnter event. The area_trans script will still work, and it will position the PC in Area B. And a pusuing NPC will also transition when chasing the PC.
               
               

               


                     Modifié par henesua, 05 août 2012 - 07:12 .
                     
                  


            

Legacy_Failed.Bard

  • Hero Member
  • *****
  • Posts: 1409
  • Karma: +0/-0
NPC pursuit across an area transition
« Reply #1 on: August 05, 2012, 03:42:22 am »


               I found in my mod that it's based on how the character is set to react when they lose sight of their target.
 If they're set to move to where they lose sight of it, then they'll step into ground based triggers in doing so, and quite often will go through doors as well.

 If the AI is set to immediately clear all actions and choose a new target, which is the normal AI behaviour, I'm not sure cross area pursuit is possible regardless of settings.
               
               

               
            

Legacy_ffbj

  • Hero Member
  • *****
  • Posts: 1097
  • Karma: +0/-0
NPC pursuit across an area transition
« Reply #2 on: August 05, 2012, 05:25:49 am »


               Yeah it's all in the scripting. I have zombies that will follow you across multiple areas. Sometimes you go to the shop, come out 5 minutes later and there are some zombies. So certainly through scripting you can get those results. It also seems to be related to hot puruit, so if something is already trying to attack you they will follow, whereas at times you will run by someone who is surpirsed and does not follow. It's definitely ai/script related, as FB suggests. So cross area waypoints has nothing to do with this, though it's a good idea to have it on.  That would be for patrols and such that could find waypoints in areas other than the one they are currently located in.
Well here is a copy of my standard zombie script, at leas the hb:

//:://////////////////////////////////////////////////
//:: NW_C2_DEFAULT1
/*
  Default OnHeartbeat script for NPCs.
  This script causes NPCs to perform default animations
  while not otherwise engaged.
  This script duplicates the behavior of the default
  script and just cleans up the code and removes
  redundant conditional checks.
  zombie hb wander around attack chickens if available. ffbj
 */
//:://////////////////////////////////////////////////
//:: Copyright © 2002 Floodgate Entertainment
//:: Created By: Naomi Novik
//:: Created On: 12/22/2002
//:://////////////////////////////////////////////////
#include "nw_i0_generic"
void main()
{
    if (GetIsInCombat()) return;
    //group with others and attack pc
    object oEnemy = GetNearestSeenEnemy();
    object oPC = GetLastPerceived();
    int  iTalentHR = TalentSpellAttack(oEnemy);
      string sWPTag1 = "wp_random";//seed tag of wp. wp's must be placed.
      string sRan = IntToString(d6()); //change dsize for more wp
      string sWPTag = sWPTag1 + sRan; //selects wp_random1-6
      location lWP = GetLocation(GetNearestObjectByTag(sWPTag));
      float fWait = StringToFloat(sRan);
     if (GetAlignmentGoodEvil(oEnemy) != ALIGNMENT_EVIL)
      {
      TalentBuffSelf();
     DelayCommand (fWait,ActionAttack (oEnemy, iTalentHR));
     DelayCommand (7.0, ActionAttack (oEnemy));//slow, methodical, thoughtful, zombies
      }
     else
     DelayCommand (1.0,ActionMoveToLocation(lWP,TRUE));
    object oTarget;
    oTarget = GetNearestObjectByTag("Chicken");
    location lWP1 = GetLocation(oTarget);
    location lWP2 = GetLocation(oEnemy);
     if (GetIsObjectValid(oTarget))
     {
    ActionMoveToLocation(lWP1,TRUE);
    ActionAttack (oTarget);
    return;
     }
      else if ((GetIsObjectValid(oEnemy) && GetDistanceToObject(oEnemy) < 15.0) && !(GetIsInCombat()))
        {
            ActionMoveToLocation(lWP2,TRUE);
            ActionAttack (oEnemy, iTalentHR);
        }
  else if (GetTag(oPC) == ("Zombie"))
  ActionMoveToObject(oPC,FALSE);//group with other zombies
}
Of course there is more to them than this.  For instance they can call rats when wounded, and they try to heal at evil altars.  But usually once they have glommed onto you they are hard to shake off.  
               
               

               


                     Modifié par ffbj, 05 août 2012 - 04:53 .
                     
                  


            

Legacy_henesua

  • Hero Member
  • *****
  • Posts: 6519
  • Karma: +0/-0
NPC pursuit across an area transition
« Reply #3 on: August 05, 2012, 01:20:15 pm »


               

Failed.Bard wrote...
If they're set to move to where they lose sight of it, then they'll step into ground based triggers in doing so, and quite often will go through doors as well.


How do you do this?

I don't know how to get the last location of a vanished object.
               
               

               
            

Legacy_Failed.Bard

  • Hero Member
  • *****
  • Posts: 1409
  • Karma: +0/-0
NPC pursuit across an area transition
« Reply #4 on: August 05, 2012, 01:36:40 pm »


               

henesua wrote...

Failed.Bard wrote...
If they're set to move to where they lose sight of it, then they'll step into ground based triggers in doing so, and quite often will go through doors as well.


How do you do this?

I don't know how to get the last location of a vanished object.


  You use GetLastPerceptionVanished() from inside the OnPerception script.  By default if that triggers, and the object that vanished was its attack target, it'll clear the action queue and select a new target.
  You get the targets location, if it's valid than the object disappeared the conventional way.  If the location is invalid than it's likely transitioning.  Not calling the ClearAllActions() in that case should in itself make it pursue out of the area, since if it's their target they'll move to it to attack still.

  Outside the OnPerception event, I'm not sure how to catch it.  Inside that event, you can track it fairly easily.
 Since you're using a custom transition script, you could also try storing the transition source or destination on the PC when they trigger it, and have the creature move or jump to that point.
               
               

               
            

Legacy_henesua

  • Hero Member
  • *****
  • Posts: 6519
  • Karma: +0/-0
NPC pursuit across an area transition
« Reply #5 on: August 05, 2012, 03:17:30 pm »


               I ran some tests using invisibility potions, hostile creatures, and a modified perception script with the following notifier:

if(bVanished)
{
        if(     GetAttemptedAttackTarget()==oPercep
            ||  GetAttemptedSpellTarget()==oPercep
            ||  GetAttackTarget()==oPercep
          )
            SendMessageToPC(GetFirstPC(),GetName(OBJECT_SELF)+ObjectToString(OBJECT_SELF)+": "+GetName(oPercep)+ObjectToString(oPercep)+" vanished AND I was fighting them.");
        else if(GetIsInCombat())
            SendMessageToPC(GetFirstPC(),GetName(OBJECT_SELF)+ObjectToString(OBJECT_SELF)+": "+GetName(oPercep)+ObjectToString(oPercep)+" vanished AND I am in combat.");
        else
            SendMessageToPC(GetFirstPC(),GetName(OBJECT_SELF)+ObjectToString(OBJECT_SELF)+": "+GetName(oPercep)+ObjectToString(oPercep)+" vanished.");
}

From a creature's Perception event,
I'm notified when an object vanishes, and what that object is
and I am notified whether the creature is in combat at the time
BUT the first instance which evaluates whether the vaniching object was a target in combat never evaluates as TRUE

still working on this....
               
               

               
            

Legacy_Failed.Bard

  • Hero Member
  • *****
  • Posts: 1409
  • Karma: +0/-0
NPC pursuit across an area transition
« Reply #6 on: August 05, 2012, 03:28:39 pm »


               To rule out the obvious, is that after anything that might call ClearAllActions (TRUE), or DetermineCombatRound()?
               
               

               
            

Legacy_henesua

  • Hero Member
  • *****
  • Posts: 6519
  • Karma: +0/-0
NPC pursuit across an area transition
« Reply #7 on: August 05, 2012, 03:42:24 pm »


               Nope.

I just ran another test, and got those three (Attempted Attack, Attempted Spell etc...) on Self, and they are always empty in the perception event. But the GetIsInCombat does accurately define when a creature is in combat.

Also... the perception events no longer seem to execute when no PCs are in the area. Is this a problem with running Test from the Build Menu? Or would I get similar results if I started the module in the player client?
               
               

               
            

Legacy_henesua

  • Hero Member
  • *****
  • Posts: 6519
  • Karma: +0/-0
NPC pursuit across an area transition
« Reply #8 on: August 05, 2012, 04:10:55 pm »


               It gets more interesting...
the perception event only executes based on a particular kind of perception... and there are four possibilities:
Seen, Vanished, Heard or Inaudible

you can dectect which of the four, but only one of the four will evaluate as true
    int bSeen       = GetLastPerceptionSeen();
    int bHeard      = GetLastPerceptionHeard();
    int bVanished   = GetLastPerceptionVanished();
    int bInaudible  = GetLastPerceptionInaudible();

The perception event only executes when a perception state changes.

So changing from Seen to Unseen evaluates as Vanished, while Unseen to Seen evaluates as Seen.
And Heard/Inaudible work the same way together.

I had no idea it worked this way. Now I need to figure out how to take advantage of it to get an NPC to follow a PC across a transition.


Here's the test code:
void main()
{    // * if not runnning normal or better AI then exit for performance reasons
    if (GetAILevel() == AI_LEVEL_VERY_LOW) return;
    object oPercep  = GetLastPerceived();
    if(!GetIsObjectValid(oPercep)) return;
    int bEnemy      = GetIsEnemy(oPercep);
    int bPC         = GetIsPC(oPercep);
    int bSeen       = GetLastPerceptionSeen();
    int bHeard      = GetLastPerceptionHeard();
    int bVanished   = GetLastPerceptionVanished();
    int bInaudible  = GetLastPerceptionInaudible();

    string msg = GetName(OBJECT_SELF)+ObjectToString(OBJECT_SELF)+": perceives("+GetName(oPercep)+ObjectToString(oPercep)+") ";
    if(bSeen)        msg += "seen ";
    if(bHeard)        msg += "heard ";
    if(bVanished)        msg += "vanished ";
    if(bInaudible)        msg += "inaudible ";
    SendMessageToPC(GetFirstPC(),msg);
}
               
               

               


                     Modifié par henesua, 05 août 2012 - 03:16 .
                     
                  


            

Legacy_Failed.Bard

  • Hero Member
  • *****
  • Posts: 1409
  • Karma: +0/-0
NPC pursuit across an area transition
« Reply #9 on: August 05, 2012, 04:22:45 pm »


               That's good to know.  I'm pretty sure I have some parts of my OnPerception with nested checks, which would be pointless if only one condition is ever true.
               
               

               
            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
NPC pursuit across an area transition
« Reply #10 on: August 05, 2012, 04:25:29 pm »


               Question: Are your Area Transitions set up with Triggers that are linked using the areas, Transistion Type of trigger? Or are they just a Generic Trigger with a custom script?

They look to be just Generic triggers with no transistion information set up on them. In that case I dout the MODULE_SWITCH_ENABLE_CROSSAREA_WALKWAYPOINTS will do you any good, since the pathfinding has no way to tell what area a trigger will move the NPC to.

I also do not think the OnPerception event Fires when the PC leaves the perception range. This is basicly what happens when the PC leaves the area.

Even if the NPC is trying to follow the Target there is no way for the pathfinding to follow him since the Atea Transistion information is not there. getting the PC's location in the attempt to follow places the PC in a differant area.

Possiable solution:

When an Object Transistions place a local location(LAST_TRANSITIONED_FROM) on the transitioning object.
In your Combat AI scripts check to see if the LastTarget is still in the same Area or not. If they are not in the same Area and are not Dead, move to the LAST_TRANSITIONED_FROM Location Stored on the Target.

EDIT: You may be able to do the Combat AI part of this using a Special AI script.  Setting the Script  on the creatures with the OnAreaEnter Event when they Spawned/created into this type of area.   
               
               

               


                     Modifié par Lightfoot8, 05 août 2012 - 03:33 .
                     
                  


            

Legacy_henesua

  • Hero Member
  • *****
  • Posts: 6519
  • Karma: +0/-0
NPC pursuit across an area transition
« Reply #11 on: August 05, 2012, 04:33:07 pm »


               Lightfoot, the perception event does fire when a PC turns invisible. It is what I call a VANISHED perception event.

However, I do not think the NPCs perception event executes when the last PC exits the area. It appears that no perception events run on NPCs when the PC is gone. At least that is the case when I Test Module under the Build Menu.

Also, my custom transition triggers are of type area transition, BUT they lack a destination. The OnEnter event of the custom trigger transitions the entering object to the adjacent area, and at coordinates relative to those in the area exited.

After I am finished completely rewriting my perception event to make use of the new information I discovered, I'll look into using the OnCombatRoundEnd event to determine if the NPC needs to follow a PC.
               
               

               
            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
NPC pursuit across an area transition
« Reply #12 on: August 05, 2012, 04:36:42 pm »


               

henesua wrote...

Lightfoot, the perception event does fire when a PC turns invisible. It is what I call a VANISHED perception event.


Correct, I never questioned that.   What I questioned was if the event fired when a PC simply left the perception range.   

Does the Event fire when a PC walks out of range of the NPC ?
               
               

               
            

Legacy_ffbj

  • Hero Member
  • *****
  • Posts: 1097
  • Karma: +0/-0
NPC pursuit across an area transition
« Reply #13 on: August 05, 2012, 04:55:27 pm »


               Yeah it's certainly got something to do with the transition script. Look for instance to see if any of your monsters have these conditions:
if (GetLocalInt(GetLocalObject(oPC, "SpawnedBy"),"DontZone") == 1) return;
Whiich returns the script if either local is present on the oPC, the object which enters the transition.
You may add in a few lines to your npc's perception the vanished part that everyone is talking about. So kludgy thing like placing a Wp_Pursue iinside the transition itself, then have the monsters go to the nearest one, maybe on a random chance, when the PC vanishes, thereby forcing the creatures into the transition. Along the lines Lightfoot8 was going with the local location. I also use getnearestenemy on undead trying to emulate their detect life. You don't have to seen or heard for them to detect you. Though that's not going to force them across custom transitions.

In regards to Lightfoots query, I would say no, but stipulate the conditions.  If the creature is already after the PC, in that they are an attack target, the PC would have to vanish, go into stealth, teleport out, leave the game, transition, or go invisible, for the conditions in the perception script to be called if the PC is aleady perceived.  Also there is this:
 // * don't want creatures wandering too far after noises
            if (GetDistanceToObject(oPercep) <= 7.0)
So this is for something heard, presumably something in stealtth but maybe no line of sight exists.  Anyway the creature will move in the direction of something heard if equal to or under seven meters, but they have vanished, as above.  There is nothing else about range so it all depends on the creatures ability to detect you and whether they are hostile or not.  I think Axe explained it pretty well in another of the myriad discussion about this.  He said something like this: 'perception fires when something enters the creatures perception range the perception script will not fire again for that same creature unless something changes out of the norm like vanishing.'  Not definitive but walking away will not fire it off. I believe 
               
               

               


                     Modifié par ffbj, 05 août 2012 - 04:16 .
                     
                  


            

Legacy_henesua

  • Hero Member
  • *****
  • Posts: 6519
  • Karma: +0/-0
NPC pursuit across an area transition
« Reply #14 on: August 05, 2012, 04:59:11 pm »


               Yes. The types of perception events are as follows from what I can tell via testing:

VANISHED - perceived creature is now unseen by caller
SEEN - perceived creature is seen by caller
HEARD - perceived creature is heard by caller
INAUDIBLE - perceived create is now unheard by caller

Each of these appear to act like an event, yet each only calls the perception event.

However... when a PC leaves the area, I never get a VANISHED event. I've mostly detected NPCs losing site of one another as the walk around at random.