×
Меню
Индекс

MSFD Checking which AI package is currently executed

 
For complex Actor scripting it might be valuable to know which AI mode is currently active and make script actions dependant on it.
 
GetCurrentAIPackage (returns short)
 
If ( GetCurrentAIPackage == 2 )
     [do something]
endif
 
The returned values are:
None       -1
Wander       0
Travel       1
Escort       2
Follow       3
Activate       4
Pursue       5
 
Casey Tucker wrote a few interesting notes  about Morrowind's AI system:
These are a few notes on Morrowind's AI system, as much for myself as for anyone wishing to look them over. The most I noticed the AI is incredibly stubborn. There are quite a few unpredictable script functions that deal with AI. I find that the AIFollow is actually the most stable of them.
 
When working on "The Triumvirate" quest/war mod, if the player went to Dragonhold Castle to join the Legion, I had to have the player's Imperial Centurion, Sensius Tanarii, show the player where his barracks room is.
 
Initially I had Sensius follow the player until you wandered around long enough to find your barracks room. Sensius would confirm the location and disappear, once Proximus Taiatius, (your legionary roommate) noticed you.
 
(GetTarget Player == 1 Usually works - For the desired effect, GetDistance is more accurate.)
 
So, that was sort of the opposite of I wanted. I wanted Sensius to take the player to their room. When I first actually thought of this, I tried thinking of several ways to get an NPC to "AiEscort" through several cells to the given coordinates. Let me tell you of my findings on AIEscort. It's either broken, works only in exterior cells, or I was doing something wrong. Regardless, I noticed that the NPC did not budge at all from their location, though they WERE set to guard the player. Upon further research, I saw that during the CharGen process, when the prison ship guard appears to be escorting you to the door, Bethesda actually used AIWander, making checks to see if the player was following or not.
From this I drew the conclusion that AITravel would be so much more convenient and stable. I only use AIEscort to get a stationary actor to guard the player if they came under attack. Useful for battle scenes when you want it to appear that two forces are fighting each other, without having an entire side follow the player. About mass battles... I will lead into that later.
 
First, I must explain how to get an NPC to escort a player, from point A: an interior cell, through several interior and exterior cells, to point B: also an interior cell. First things first.. we don't want the NPC to get stuck. You'll need some pathgrids. A few notes about pathgrids: the NPC doesn't seem to care about the gridpoints' Z coordinate [This doesn’t appear to be correct. See notes below]. That's a very good thing. Pathgrids are like magnetic roads. The NPC is seemingly "magnetically attracted" to that pathgrid, and their AIWandering, AITraveling, and AIFollowing will follow along those grids. To see how these grids work, go in-game, to Ebonheart or another semi-crowded city, and type in the console: 'TPG'. That will show the pathgrids - you'll see the NPC's walking between the two yellow "rails" that are drawn out, as if they were trains. An NPC will reluctantly pull away from a pathgrid if led so by the player, need to walk around something blocking their way, chasing someone/thing, and such scenarios.
 
Now that I got pathgridding done, I noticed another thing. AITravel is not "unstable" per se, but not very reliable. Two factors come into play here, that dictate if the NPC will blatantly ignore it's orders or do as told. The first, I noticed, was the AI distance setting, changeable by FPS optimizer or in-game menu. Makes sense. However, the next one is rather annoying. The NPC cannot get distracted or their travel will stop. This means you'll have to set their "Hello" distance to 0 during their little stroll, otherwise they will stop and greet the player instead.
 
Now we come to the point where the NPC reaches a load door through cells. The game will CTD if the NPC activates it. Here is a snippet of an "escort" script I wrote, that gets the NPC through a load door.
 
Begin Sensius_Escort_Script
Short EscortOnce
Float Timer
;In my theory, a load door is simply a PositionCell function referencing to the PlayerSaveGame once the door is
;activated. (By the player.) For an NPC to activate a load door would, as this "door script" seems hard-coded
;strictly to the player, would call an error that would cause a CTD. This script creates an artificial load door, by
;replacing the "Player" function references to the NPC in question. As this is concerning a REAL load door, I cannot
;have the NPC activate it. Rather, the NPC teleports when they are close enough to the door for activation, and
;a certain variable is set, accompanied by a 3D sound to finalize the effect. NPC load doors are possible. The only
;problem is that the NPC disappears rather than shows an animation of entering the door - but it's not much worse
;than in Oblivion, when the NPC just fades.
 
If ( MenuMode == 1 )
Return
Endif
 
If ( EscortOnce == 0 )
"Sensius Tanarii"->AiTravel 4990.000 5200.000 15456.000
Set EscortOnce to 1
Endif
 
If ( EscortOnce == 1 )
If ( "Sensius Tanarii"->GetPos, X <= 5060 ) ;I am "boxing in" the coordinates. When he stands in this area, the door is activated.
If ( "Sensius Tanarii"->GetPos, X >= 4930 )
If ( "Sensius Tanarii"->GetPos, Y <= 5360 )
If ( "Sensius Tanarii"->GetPos, Y >= 5177 )
"Sensius Tanarii"->PlaySound3D "Door Latched Two Open" ;
"Sensius Tanarii"->PositionCell 3186 5191 15050 0 "Dragonhold Keep - Main Entry Hall"
Set EscortOnce to 2
Endif
Endif
Endif
Endif
Endif
 
 
That should explain how it would work. Coordinates are often more reliable than a "GetAIPackageDone" function, mainly because that function is easily broken by several factors.
 
Some notes on the notes (!)
•     It's been reported by others (and I've found myself) that the z-pos of pathgrid nodes does matter (at least in interiors): if a node isn't well above the floor the actor can get "stuck" at that point and refuse to move past it.
•     AIEscort has been used successfully by a number of modders (and in Bethesda's scripts, e.g. CharGenWalkNPC). That's not to say it's without problems, but it is certainly useable. However, since AIEscort may not be viable in some circumstances, the above notes may help you develop an alternative approach should one be necessary.