Jump to content

[Scripting] Perception scripts, reactive NPCs, and stealth


Tchos

Recommended Posts

Early on in my modding, I had heard talk about the On Perception event for creatures. This event fires whenever the creature perceives another creature. It seemed ideal for what I wanted to do, which was to have some NPCs speak up whenever a player character would walk by. It would also be perfect for a more intricate burglary mission for rogues, where guards would be able to react properly to poorly stealthed PCs. I did numerous tests, and here are my results.

 

 

Here we see my rogue PC sneaking around an NPC. The only script involved here is on the NPC's On Perception event, and all it does is speak barks upon certain conditions -- whether the NPC saw the PC, heard the PC, or no longer perceives the PC.

 

In this test, the NPC has good hearing (Listen skill of 10) but poor eyesight (Spot skill of 0). So Lidda sneaks around him, and at one point he says "I hear you," but he does not see her. His Listen check beat her Move Silently check. Around the 15-17 second mark, I click the button to turn off stealth mode when I'm right in front of him, and as soon as I leave stealth, he says "I see you". Then, when I use an item to cast an invisibility spell, he says "I can't see you anymore."

Edited by Tchos
Link to comment
Share on other sites

My next experiment involved using the same perception script for simple background characters, as general townsfolk banter. The perception script is the same as before, doing nothing but firing one of three possible dialogue files depending on whether the PC was seen, heard, or vanished. This script is a simple adaptation from Marshall Vyper's Legends series of plugins, where it originally was designed to store variables in a database. I removed anything unnecessary for this purpose and changed the database calls to look for local variables on the NPC instead.

 

So here we have a row of NPCs, with this one script in their On Perception event, and a local variable pointing to the "seen" dialogue. For NPCs like this, there's no need to use anything other than "seen". They have low Spot skills, from 0-3, and their "Perception Range" property is set to "short". If the perception range is too long, then an NPC might comment on your arrival when your character is still too far away to hear.

 

They all have the same dialogue file, a "common bark" file that I use for generic NPCs. This isn't necessary; Each NPC could have its own dialogue file. I only did it this way for convenience, and the file uses combinations of conditions to determine what they will say. In my module, I use the speaker's tag to give them specific lines, but here I'm using the state of the main quest. The idea is that the townsfolk don't know you, and start out with a poor opinion of you, but as you proceed through the main quest, your reputation in town increases and the townsfolk start to wonder if you're the great hero they've been hearing about, and eventually they all recognise you and treat you with high esteem.

 

So the dialogue file contains several conditions. It checks the main quest in the journal, and if it's at a low stage, they'll choose randomly from a pool of negative comments, which also include animation calls. If it's in the middle, they'll choose from a pool of animated neutral/curious comments. And if you've completed the main quest, they bow and cheer for you.

 

 

I had heard from several people that perception scripts only fire once, and never again, and are thus unsuitable for this kind of purpose, but as you can see, they make comments every time I pass by them, as long as I've moved out of their perception range before I come back, which is what I want. It works this way in my campaign, the Black Scourge of Candle Cove, notably when you pass by the fishing trainer or the undersea tour guide, where I also have them greet the character by name as they approach (if they've met), and ask about the status on any quests they've given you.

Link to comment
Share on other sites

In this example, I wanted to have monsters that would shout exclamations when they see the player, but not always in the broken English typical of fantasy monsters. Monsters often have their own languages, and while some speak several languages, they don't always speak Common. Here, I placed three groups of different monsters: sahuagin, kuo-toas, and water elementals. The sahuagins are speaking my representation of Undercommon, the water elementals are speaking my representation of Aquan, and the kuo-toas are speaking my representation of the Kuo-toan language.

 

 

There's very little in the way of examples of less-popular languages in D&D such as these. To give barks to such monsters, I wrote a script to generate phrases from a list of vocabulary for each of the races. I created a separate dialogue file for each type, and these dialogue files fire scripts that generate a sentence based on several sentence structure types, picking words from a list of vocabulary. The languages are made of whatever real examples I could find (such as suggested common names), as well as online words from other sources, and my own words based on the descriptions of how the languages sound.

 

I set these up so they shout both on perception and on death, and in the video they're set to do it 75% of the time just to show the variety of phrases they can speak, but in the actual module I have them set to shout only part of the time for better variety, so one monster may do it on perception but not on death, another may do it on death but not perception, another may do both, and another may do neither.

 

In this video, you may notice that the final word in their shouts is always rather long. This was an error that I corrected afterward that mashed the two final words together.

 

I used more or less the same system here, but without the monster language generation, for monsters that do speak in English (actually Common), using actual sentences that I wrote rather than picking random words from an array, more akin to the NPCs in the previous example. The way this is set up, I can easily mix and match pre-made sentences versus script-generated sentences, and/or alternate language possibilities for creatures that typically speak more than one language.

 

I don't use this for bosses, since I generally write a unique boss AI for each one these days, which includes its own exclamations and shouts not only at the beginning and end, but at stages within the battle, but it could be used just as well for bosses that don't need special AI but want a little personality.

Link to comment
Share on other sites

Now for a more complex perception application. This uses the same perception script as before, but here I'm using it for a stealth environment, where a guard without any special spotting skill is aided by a guard dog. I try to sneak past them to the door, but for the purpose of the test I removed a ring I was wearing that boosted my stealth skills so that the dog would have a higher chance of detecting me before I got there. Otherwise, the dog would have detected me much further away, because of its high Spot and Listen, which accounts for the dog's Scent ability.

 

 

When the dog hears the rogue, he only growls and looks in her direction, and the guard temporarily starts looking around, too. When the dog actually sees her, then he starts barking, turning to face her whenever she moves, but not attacking. The guard's reaction is tied directly to the dog's perception, and the guard does not actually report seeing the rogue until she becomes visible. Until then, the guard is simply looking in the same direction that the dog is looking when it starts barking, trying to find her.

For a more complete result, I would make it so that instead of the guard threatening the thief when the dog starts barking, the guard would throw some dust of appearance in that direction to make the thief visible, and then go on with any other actions. At the very least, I should temporarily boost the guard's perception skills while the dog is alerted, because the guard then knows exactly where to look, and it should also count as an Aid Another action.

It could start a timer where after issuing a warning, the guard and dog would attack unless the thief backs off, and/or spawn an ipoint to rapidly check proximity and have them attack if the thief gets within a certain distance, destroying the ipoint if the thief backs off. And, of course, any hostile action would have to turn them hostile, too.

The audio's a little quiet on the dog bark. It's a stock NWN2 sound file. Also, when I convert these video captures to a compressed format to upload to Youtube, it delays the audio about a second, so it seems a bit out of sync here. I don't know what format wouldn't do that and still maintain high visual quality.

Edited by Tchos
Link to comment
Share on other sites

Hi Tchos. I have had an interest on this system for a while now. Do you see this working like on most modern RPGs where the NPCs will make their bark just by the PC walking close to them? I mean not only stealth reasons but the general bark mechanic.

Link to comment
Share on other sites

I believe that's how it currently works, yes, though I don't know which modern RPGs you mean. For instance, in the Black Scourge of Candle Cove, when the PC walks along the docks and passes the fishing trainer, and you haven't taken her quest yet, she'll bark "Come learn to fish!". Or, if you're on her quest, or finished her quest, she'll bark other things. And when you pass by the tour guide, he'll bark various things like "Step right up for the tour of a lifetime!" Those are perception scripts.

Link to comment
Share on other sites

The tour guide is especially fun, since if you broke your deal with him during his quest, not only will there be consequences later, but he'll shout to everyone whenever you pass by that you're an untrustworthy scoundrel. :) You can have all kinds of conditional and contextual barks to make your areas feel more lively.

Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...