Author Topic: Scripting a Custom AI? Learner's manual  (Read 509 times)

Legacy_HipMaestro

  • Hero Member
  • *****
  • Posts: 2849
  • Karma: +0/-0
Scripting a Custom AI? Learner's manual
« on: October 30, 2013, 04:25:27 pm »


               I'm looking for an overview, a tutorial, a list of tips, a cause-effect grid... any sort of resource that would make developing a custom AI easier to conceptualize and structure.  I realize this may be a difficult subject to attack but an endeavor needs a beginning.  

A few general answers to questions may help me to understand the scope.  At this point, a shot in the dark...

Are there both "general" AIs, like triggered when entering an area or loading a module and those applied specifically to each creature blueprint?

Does the term AI actually refer to a group of scripts dedicated to the creature's events or does it run in parallel with each other (several events at once) or as a single larger script like an include?  Or perhaps "all of the above" '<img'>

How are conflicts resolved between scripts that may be firing simultaneuosly?  Is there a set priority that the game uses in those situations? (for a bad example perhaps, suppose an OnHeart script fires at the same moment that an OnPerceived event triggers)

It's questions like this that I would need to understand before trying to manipulate a creature's reactions to a defined situation.  As should be quickly revealed by the nature of my questions is how little I know about the subject and how ineffectively my measures have been to control an encounter up to this point.

Any enlightenment and/or linkage to helpful resources are, as usual, greatly appreciated.

P.S. Incidentally, while examining the potential scripting utilities available I discoved the Canadian Scriptease link is dead and don't know if it was ever loaded to another site or what functionality it provides.  I've used Lilac's script utility but it really doesn't help me understand the big picture of AI and consequences of sticking a generated script where it may/may not produce the desired effect(s).
               
               

               
            

Legacy_FunkySwerve

  • Hero Member
  • *****
  • Posts: 2325
  • Karma: +0/-0
Scripting a Custom AI? Learner's manual
« Reply #1 on: October 30, 2013, 08:33:50 pm »


               

HipMaestro wrote...
Are there both "general" AIs, like triggered when entering an area or loading a module and those applied specifically to each creature blueprint?

Up to you. We use an endcombat script on a per-area-group basis, but you can do it modwide, depending on your plans and needs. I favor making them as generalizable as possible. For a while we were using j_ai from the vault (Jasperre's) for our casters, but we wound up coding our own, after finding that x2 ai was generally 'meaner'.

Does the term AI actually refer to a group of scripts dedicated to the creature's events or does it run in parallel with each other (several events at once) or as a single larger script like an include?  Or perhaps "all of the above" '<img'>

It's all based on events, though some of the scripts, especially anything that calls the DetermineCombatRound function, rely on a number of fairly sizable includes, which handle things like target selection.

How are conflicts resolved between scripts that may be firing simultaneuosly?  Is there a set priority that the game uses in those situations? (for a bad example perhaps, suppose an OnHeart script fires at the same moment that an OnPerceived event triggers)

This can't happen. Scripts fire serially in NWN, as its single-threaded. You can queue up a number of things happening nearly simultaenously, however. NWN's default ai generally handles that by either a) checking to see if the critter in question is already busy, or 'B)' clearing its action queue.

P.S. Incidentally, while examining the potential scripting utilities available I discoved the Canadian Scriptease link is dead and don't know if it was ever loaded to another site or what functionality it provides.  I've used Lilac's script utility but it really doesn't help me understand the big picture of AI and consequences of sticking a generated script where it may/may not produce the desired effect(s).


The only way you're going to learn that is by trial and error. Instead of reinventing the wheel, I would decide what it is you dislike about the current ai, and then choose a base ai to work off of. As always, the more you tell us about what it is you're hoping to accomplish, the more helpful we can be.

For my part, I chose to work off of x2 ai, making edits to its targeting functions, as well as its onspawn and ondamaged scripts. Acaos also cut a lot of unnecessary silent shouts, and honed the onconversation code some. The result was a leaner, smarter ai, with a couple of bugs fixed (issues with targeting Greater Sanctuaried creatures are the first to come to mind).

Funky
               
               

               
            

Legacy_Terrorble

  • Sr. Member
  • ****
  • Posts: 370
  • Karma: +0/-0
Scripting a Custom AI? Learner's manual
« Reply #2 on: October 31, 2013, 12:18:40 am »


               I'm in about the same place as you Maestro.  I'd like to improve the AI (using TonyK's currently after finding a few things that Jasperre's did that wasn't working well with my setup), but I've come to the same conclusion as Funky is recommending: determine exactly what it is about the AI that you don't like/want, then go from there.

When I put it to paper, I realized that I only need certain NPCs to do certain things, and the rest of them I can settle with doing what they are doing, how they are doing it.  Magical Master has coded a number of unique NPC abilities/behavioirs into the mod Siege of Heavens.  I've only glanced at it so far, but I think I can learn enough from that to make my own NPCs act more like I want.
               
               

               


                     Modifié par Terrorble, 31 octobre 2013 - 12:19 .
                     
                  


            

Legacy_HipMaestro

  • Hero Member
  • *****
  • Posts: 2849
  • Karma: +0/-0
Scripting a Custom AI? Learner's manual
« Reply #3 on: October 31, 2013, 12:27:14 am »


               Thanks for checking in on this topic, Funky.  Is that x2 AI also on the vault or is it one you made for HG?  

It has been trial and error every step so far.  Mostly error, though.  The experimentation process seems so chaotic to me that I hoped that some framework may have been outined.  I'll check out the Jasperre one.  It may give me a clue as to what works and what doesn't.
               
               

               
            

Legacy_FunkySwerve

  • Hero Member
  • *****
  • Posts: 2325
  • Karma: +0/-0
Scripting a Custom AI? Learner's manual
« Reply #4 on: October 31, 2013, 05:22:59 am »


               The x2 ai is the one that came with HotU, and is really just a revision of the previous two ais from SoU and NWN. It's short for xpansion 2.

If you use Jasperre, be aware that the last version on the vault that I'm aware of tends to hit TMI errors pretty easily in intense combat, due to the high numbers of loops it uses. It's also got some incidental mismatches in declaration vs implementation of functions that cause issues, but which the default compiler won't catch - use skywing's instead.

I guess my advice on how to proceed would be to do what I did - spend a good 4-6 hours reading through the combat ai code, to understand some of what it's doing. Even then, it can be pretty overwhelming, but you should at least have an improved sense of how target selection works, which is among the most complex things it does.

For unique monster coding, we go either with modified spells (and a modified casting code, to include our custom spells in the ai's casting priorities), or with custom endcombatround scripts, which allow you to code most any behavior, or with custom conhits, which allow you to code most anything else not handled by the other two. In a few cases, we use onheartbeat scripts, for things like degrading nearby spell-created walls. I can post a few scripts if you like.

Funky
               
               

               


                     Modifié par FunkySwerve, 31 octobre 2013 - 05:28 .
                     
                  


            

Legacy_HipMaestro

  • Hero Member
  • *****
  • Posts: 2849
  • Karma: +0/-0
Scripting a Custom AI? Learner's manual
« Reply #5 on: October 31, 2013, 08:51:38 pm »


               

FunkySwerve wrote...
with custom endcombatround scripts, which allow you to code most any behavior,

This is probably what I have been attempting to implement so far.  It seems complicated though, usually producing results completely different than intended.  

Is a round the shortest duration combat action that can be addressed?  I was hoping to gain access to each flurry, if possible.  Seems like the custom round takes too long to react (or counter) especially if the opposition is hasted or quickened.

...or with custom conhits, which allow you to code most anything else not handled by the other two.


Dunno what this term is referring to, Funky.  Care to illuminate a bit, plz?

I can post a few scripts if you like.
Funky

Thanks for the offer, Funky, but let's hold off for the time being until I educate myself further.  I'd rather discuss script cause/effects after I've dissected the default AI's a bit more.  

I think the point about limiting the excursion into AI-land to only those aspects that are either "unmanageable" or "ineffective" is a real good point.  If the basic mechanism isn't functioning the way one wants, there is a tendency to remedy the problem by disassembling down to scratch and working the way back up again in the preferred style. But I believe that is one of the reasons that the improvements seem so over-whelming... attempting to reinvent what already works okay.
               
               

               
            

Legacy_FunkySwerve

  • Hero Member
  • *****
  • Posts: 2325
  • Karma: +0/-0
Scripting a Custom AI? Learner's manual
« Reply #6 on: October 31, 2013, 11:30:46 pm »


               

HipMaestro wrote...

FunkySwerve wrote...
with custom endcombatround scripts, which allow you to code most any behavior,

This is probably what I have been attempting to implement so far.  It seems complicated though, usually producing results completely different than intended.  

Is a round the shortest duration combat action that can be addressed?  I was hoping to gain access to each flurry, if possible.  Seems like the custom round takes too long to react (or counter) especially if the opposition is hasted or quickened.

Endcombat round fires every six seconds. If you want an event firing more often, you can either use an onhit (or 'conhit', as I typoed yesterday), or you can set it to fire a function or functions on a delay, effectively making it fire as often as you want.

We use endcombatorund simply because it's convenient - it doesn't really tangle with the rest of the creature's code. You're not really limited by its fire rate at all, though in most cases we find the end of the round to be perfectly fine (and yes, all our pcs are generally tearing around with haste items). You can have it look for a target just as it fires, so there's no real question of reaction time, unless you're concerned with pcs killing them in the first 5 seconds.

Anyway, to address what you said about them being complicated, they really aren't - this is probably the simplest way to add custom-coded behavior, since it's independent of the creature having to use any ability dictated by spell, special, or talent. Endcombats allow you total control of what happens each round. In a sense, they're sort of 'cheating', though, since they will occur in addition to whatever else the creature is doing that round - likely attacking.

...or with custom conhits, which allow you to code most anything else not handled by the other two.


Dunno what this term is referring to, Funky.  Care to illuminate a bit, plz?

Sorry, that was just a typo, for 'onhit'. We run a lot of stuff that logically should depend on the creature hitting the pc on custom onhit scripts. Anything from disease to poison to instant death, and all sorts of other things. We use a variable-based system along with the intelligent weapon onhit script, which acaos hijacked into a very flexible system for applying just about any kind of effect using any kind of check via variables.

Funky
               
               

               


                     Modifié par FunkySwerve, 31 octobre 2013 - 11:31 .
                     
                  


            

Legacy_HipMaestro

  • Hero Member
  • *****
  • Posts: 2849
  • Karma: +0/-0
Scripting a Custom AI? Learner's manual
« Reply #7 on: November 01, 2013, 01:04:20 am »


               

FunkySwerve wrote...
Endcombat round fires every six seconds.

Can the OnHeatbeat event coincide or conflict with this action even by accident?

If you want an event firing more often, you can either use an onhit (or 'conhit', as I typoed yesterday), or you can set it to fire a function or functions on a delay, effectively making it fire as often as you want. 

duh! Should have extrapolated the typo. ':blink:'

Anyway, to address what you said about them being complicated, they really aren't - this is probably the simplest way to add custom-coded behavior, since it's independent of the creature having to use any ability dictated by spell, special, or talent. Endcombats allow you total control of what happens each round. In a sense, they're sort of 'cheating', though, since they will occur in addition to whatever else the creature is doing that round - likely attacking.

I understand what you are saying, Funky.  "Control" can become a relative term, though, with the consequences almost boundless.  But it seems the best way to structure a specific response to a situation. 

Gives me a solid strategy to develop upon.  Now, to begin defining all the possible scenarios.  That should be easy ':pinched:'
               
               

               
            

Legacy_FunkySwerve

  • Hero Member
  • *****
  • Posts: 2325
  • Karma: +0/-0
Scripting a Custom AI? Learner's manual
« Reply #8 on: November 01, 2013, 02:30:24 am »


               
Quote
HipMaestro wrote...

Quote
FunkySwerve wrote...
Endcombat round fires every six seconds.

Can the OnHeatbeat event coincide or conflict with this action even by accident?
Quote



Depends on what you mean by 'conflict'. Like I mentioned, we do use heartbeats in some circumstances, and there's nothing about the events that is naturally conflictual. You could make them conflict, I guess, if you're, for example, making the creatuare uncommandable in one and commandable in the other, but there's no reason to do that. Either should suit, and you can code either to exit if they're paralyzed/petrified/etc., so use whichever you prefer.

Funky
               
               

               
            

Legacy_Shadooow

  • Hero Member
  • *****
  • Posts: 7698
  • Karma: +0/-0
Scripting a Custom AI? Learner's manual
« Reply #9 on: November 01, 2013, 12:07:20 pm »


               Making new AI is pointless.

1) You wont come up with any new concept that hasnt been already done.
2) The default AI concept is working great.
3) If you are not expert in scripting, you better not modify the AI yet. No script editors just won't help here.

I suggest first to determine crucial features you want. Then compare with already existing AIs out there: vanilla, TonyK, Jasperre, Ollander and then start by modifying the AI that is closest to what you need. You won't come up with better mechanisms of how AI works and the vanilla way is absolutely fine (the only problem there is when player resurrect on the spot - then creatures not responding, but that can be fixed from respawn event if you allow respawning on the same spot)

Also, suggest to download my unofficial patch and at least rip off the AI includes (if not whole package) so you are not wasting tens of hours by fixing something that already someone fixed year ago. But if you prefer reinvent the wheel like many out there, your choice. This is also beneficial for custom AIs since they uses these includes anyway, but then you have to merge it since these AI uses their own modified versions and might replace some of the code there with their own.

And to add, there is no point increasing AI fire rate. it does fire when monster perceive target, anytime the monster is attacked and damaged, at the end of the round and anytime the monster is target of a spell (im simplyfing but thats it). Not to mention that NWN is round based so changing targets often results in doing nothing. The vanilla AI works fine, what exactly you want to achieve?
               
               

               
            

Legacy_Rolo Kipp

  • Hero Member
  • *****
  • Posts: 4349
  • Karma: +0/-0
Scripting a Custom AI? Learner's manual
« Reply #10 on: November 01, 2013, 03:12:23 pm »


               <because being contrary...>

While I agree with the main thrust of your post, Shad - Modifying existing AI is the most efficient path to tweaking default behavior - I must argue that making new AI is *not* pointless and there's some very exciting concepts that have not yet been implemented (publically) for NwN.

Behavioral Multi-queues
Agent Learning
and my new favorite (*REALLY* excited about):
Behavior Capture and Architecture

In addition to following these with great interest, I hope to implement independent movement and action queues in jNwN (don't hold your breath) as part of the interface overhaul.

So, actually, I concur with all of your post but the first two lines :-)

<...is part of the curmudgeon creed>
               
               

               
            

Legacy_Shadooow

  • Hero Member
  • *****
  • Posts: 7698
  • Karma: +0/-0
Scripting a Custom AI? Learner's manual
« Reply #11 on: November 01, 2013, 03:43:22 pm »


               I think you misunderstood me. There are AI aspects that are worth to explore, develop like the learning or behaviorism, but that has nothing to do with AI concept itself, i mean how AI works. OP wants to entirely rewrite this part which noone else did in past from one reason, it works well and there is no better way to do that (outside of nwnx of course).

All these AI changes are subroutines. The behavior is also part of the subroutine, aka who to target, what to use, its not new "AI".
               
               

               
            

Legacy_HipMaestro

  • Hero Member
  • *****
  • Posts: 2849
  • Karma: +0/-0
Scripting a Custom AI? Learner's manual
« Reply #12 on: November 01, 2013, 05:30:36 pm »


               ShaDoOw, I think you have misunderstood my initial inquiry.

I am trying, on a very basic level, to decipher how the existing AI creates the observed results.  There ARE limitations to how creatures interact and at the same time ways to exploit those limitations.  I am assuming that the default AI doesn't require a rewrite but am interested in how to augment that basic mechanism.  That's the key for me.  How do the events interact in-game and how do the scripts associated with those events interact?

As a simple example of an issue...  how do I trigger a defensive action by a creature AI that is scripted to focus on offensive actions?  How would I get an attacked creature to retreat to a stronghold area after, say, the HP level has dropped to a certain level or after a specific number of their minions have been eliminated?  This is just an example intended to illustrate how understanding the mechanisms of the current AI might be controlled or modified to produce a scenario I seldom experience.  It may have nothing at all to do with the AI mechanics at all but while it is a grey area, it must be investigated, no?

I consider your patch as a useful resource as I do other customized apps so will be sure to research it futher once I understand the fundamentals of cause-effect better and the factors that contribute to controlling a situation.  I'd rather not just start throwing others' work into the mix helter-skelter without understanding what it does and the consequences of trying to implement it.  I've run into big problems in the past with that approach so am more careful today.

And if "everything has been done before"... that fact has no impact on someone in my position, embarking for the first time on dealing with this issues.  Designers who have been doing this for a very long time may recognize all work that has been done and focus their work on "else".  That's not the case for a learner who is trying to accomplish something with his own hands.  If you acknowledge that fact, you will understand more clearly my level of expertise (and others witha similar level of experience) and be able to contribute more effectively to the educative process.

As Funky has already suggested, by just applying a level of control peripheral to the base AI, I could develop what I am seeking.  This all fall under the topic title... Learning the fundamentals.

Now Rolo has provided other resources that have addressed interaction controls.  I still need to understand what they do even if I can't absorb all the particulars or how they are implemented.
               
               

               


                     Modifié par HipMaestro, 01 novembre 2013 - 05:39 .
                     
                  


            

Legacy_Shadooow

  • Hero Member
  • *****
  • Posts: 7698
  • Karma: +0/-0
Scripting a Custom AI? Learner's manual
« Reply #13 on: November 02, 2013, 04:55:12 am »


               

HipMaestro wrote...

How do the events interact in-game and how do the scripts associated with those events interact?

Ok, so they do not interact. Each event fires the AI engine - function DetermineCombatRound

this function determines if a creature has been already told to do something *other than attack* if yes, nothing happens and the creature events fire this function later when that action is done
if creature doesn't do anything or she is attacking, then AI step into tactic choice, where it partially depending on a class and other things choose what to do with an if else order starting with offensive spells if there are any of various categories and so on, if no spell is found then ai commands to attack where it also checks for distance and ranged weapons in inventory and so on

thats it in brief, I suggest to open and save all default AI scripts (nw_c2_default* set) then nw_i0_generic and add SpawnScriptDebugger() command into DetermineCombatRound function, then recompile all scripts, set up area with one hostile creature and then you can watch what exactly AI does command by command

quite a high NWScripting level is required though
               
               

               
            

Legacy_wyldhunt1

  • Sr. Member
  • ****
  • Posts: 443
  • Karma: +0/-0
Scripting a Custom AI? Learner's manual
« Reply #14 on: November 02, 2013, 08:04:35 am »


               I had an extensive custom AI in the past, which was completely re-written.
I'll say this: If you want to add many of the things that you mention, you may need to do a lot of re-structuring of the base code.

Basically, here is where to start:
On many events, there is an opportunity to DetermineCombatRound(). When a creature sees something, it checks to see if it's hostile. On Hit, obviously. On heartbeat, there may be a check for nearby enemies as a sanity check.

Inside the determine combat round function, you need to check if they are already doing what they should be doing. I think the default NWN code just placed a variable on them to say that they were busy if they were doing anything... You may need to be more specific than that.

Let's say that your NPC has determined that there is at least 1 enemy nearby and is free to do something. Great. First thing should be to clear all actions. This will pull them out of any random walk or anything else that they may have been doing. Now, you have a plethora of choices on how to proceed.

I assigned point scores to the NPC for various things such as melee ability, ranged, magic, etc.
I also assigned the same points to PC's when they logged in. I saved these as local variables on the (N)PC. When a combat round was determined, I'd compare these stats that I assigned them. Each round, the stats were adjusted based on percentage of hp remaining and spells left. If the NPC's ratings were a lot higher than their target, they'd just do basic attacks. Save spells, don't use items, etc. In a challenging encounter, they'd each use a custom function based on their class.
For example: Clerics would first check and heal wounded, then buff, then attack if there was no one to heal or buff. They'd only buff if they weren't already winning.
If the enemy was much more powerful, the NPC would first try to retreat to pre-determined "Safe Spots" that I had placed (Just basic waypoints that I located by name "SAFE_FACTION", where FACTION is the name of their faction). If there was no waypoint, they would move toward whomever was most powerful in their group. If they were the most powerful member of their group, and there was no waypoint, they'd move away from the attacker. I should note that it's dangerous to just move away with the default functions. They get stuck on trees and things. It's better to use some math to find a spot in the direction that you want to move, and then tell them to move to that spot with proper pathfinding.
If they had at least an int of 9, they had a chance of surrendering if their health dropped too low.

In the end, my custom AI was many thousands of lines of code.  I had to keep a good server running to prevent lag. In order to account for every combat spell, item, feat combination, as well as sanity checks to make sure that each is used wisely, I used switches embedded in switches, each filled with a lot of 'if' statements.

You could probably kludge the default code to appear to do things, but doing things in any intelligent manner won't happen without scripting every decision they can make in an intelligent way. Expect a large project.

EDIT:
HipMaestro: How much scripting experience do you have? Do you mostly just need to be pointed at the default code so that you can tweak it, or do you need help interpreting it so that you know where and how to alter it?
If you're a decent scripter, I advise starting by taking apart one of the other custom AI's. They demonstrate all the different techniques that are generally used. I was inspired by several different AI authors when I wrote mine (Pretty much the entire list, above). Looking at how others have taken control of the AI could go a long way to inspiring you. '<img'>
               
               

               


                     Modifié par wyldhunt1, 02 novembre 2013 - 08:14 .