Any interest in a better approach to AI than state machines & behavior trees?

When making NPC behavior, most developers reach for state machines or behavior trees. These produce rather inflexible, scripted behaviors unless you put a lot of effort in making a large number of states (or behavior nodes).

An alternate approach known as drive reduction produces “smart” behavior without so much prescripting. You give a character a number of concerns, which could be internal (food, thirst, bathroom, rest) or external (need to guard certain rooms/persons/objects, need to take something somewhere, etc.). You define rules for how those concerns are increased or decreased by actions available to the character. And that’s pretty much it — the character starts behaving.

Example 1: suppose you have a museum guard. His concerns would be the various rooms of the museum; whenever he’s not in (or otherwise seeing) a room, his concern about that room increases. But you can also give him a need for rest, a need for coffee, and a need to go to the bathroom. Then just drop him in the game — he will rest at his desk a while, sip coffee, and patrol the museum in whatever path seems most efficient to him. When the need to go to the bathroom gets too great, he’ll do that, but check on some rooms on the way; or maybe do his patrol first, and then hit the bathroom on the way back when he’s sure everything is OK.

When something unusual happens — say, he hears a sound or sees something out of place — you would set his concern for that room to go up substantially (with maybe a bit of spill-over into neighboring rooms). This can cause him to put down his coffee immediately, and go check the room, or rearrange his patrol route to hit that room more often.

Suppose he finds a door locked that shouldn’t be. This would substantially increase his concern for rooms on both sides of the door, but he’s already on one side, so he knows that room’s OK. So he would immediately make his way (by some other route) to the room on the other side to see what’s going on.

Example 2: Enough with the museum guard, what about RPG NPCs? In this case their concerns could be things like food, shelter, rest, maybe companionship, and of course safety. Some of these would be set to change at different rates at different times of day, so for example the character would be more likely to go home and sleep at night. This will result in a daily routine that makes sense for each character. But if a greater concern occurs — like a monster in town — then the NPC would defer sleep and seek the nearest safety (duck inside, cower behind the player character, whatever).

I really think this approach is both easier for the game designer, and results in more flexible, interesting behavior. The drawback, of course, is that the designer no longer has detailed control over what the character does. But in most cases I think the easier development and smarter behavior is worth it.

Does anybody else think this make an interesting asset? What features would you expect to see? How much would this need to be usable without coding (through custom Unity editors), vs. simply an API you talk to with code?

2 Likes

This sounds pretty interesting. Regarding custom editors, I think the user should at least be able to create concerns through the editor, but would mostly be an API. However, I am a coder myself, so custom editors are not as important to me as they might be for others.

1 Like

Hey JoeStrout

We actually stumbled upon a similar style of setting up AI awhile ago for my team’s game “Modern Knights”. We had some coding, but most of the time spent was tweaking the values of the needs. But still, the knights took only 1 day to set up. Because it worked so well, we actually began planning/ building the plugin right after. Custom editor is definitely viable, but it can get messy. There should be a balance between coding and design since AI is a fusion between the game designer and game developer. So it’s good to have this as an api with a custom editor.

P.S.
We did post a WIP called “[WIP] DANI AI” on the forums about it. This may not be exactly what you described, but I thought the concepts on this post was very similar with the project. Cheers!

@Nanofication , thanks for pointing that out. It looks pretty interesting, though I think you’re right, it’s not exactly the same thing.

Hi Joe!

NEEDSIM implements utility-based AI and smart objects. (And it’s one of the reasons I didn’t release my implementation, the other being time.) I’m half surprised that it doesn’t seem to be selling well. But given the time constraints and multiple hats that indie developers wear, they’re often lucky to have time to implement a medium-sized behavior tree, much less tweak smart objects and utility curves. So it kind of makes sense. Heck, I’m an AI programmer, and in a game I’m contributing to I just set up an enemy with a simple FSM.

I am going to try and make this. Im still learning, so i will posting my code here, when i got some, and i will take any advise you may have.

Joe Strout want to make this a shared project?
I dont mind doing the work, just need some one to tell me what im doing right and wrong.

AI is a tricky subject. This is generally the style of approach we are taking with my team’s game. Giving NPC’s things they want to accomplish, and they organize what tasks and actions they can take by weight of what accomplishes what.

Examples include; an NPC might want to kill the player. They also might want to heal themselves. The action of killing the player gets a high precedent, because the player is at 10% health and can be killed easily (actions receiving sets of inputs to determine a weighting), and the NPC gets a medium precedent to heal because the NPC is at 40% health.

From then down it’s organized in a tree-style. The killing the player action has a whole set of varied methods to do that, of which it figures out which ones would work best and be most effective. Choices of casting a spell, or attacking with melee. Neither can kill the player with high probability, so it might find a node up the tree that can change damage (buffing itself, or switching weapons) and calculate the precedence of that.

After it collects the potential actions it can take, and the best methods to accomplish those actions, it picks the best action according to the intelligence of the creature and some fuzzy randomization added in.

AI however, in general, is tricky because a one-size fits all is very difficult to do. It has to be extremely specific to the game at hand in many cases. There’s no way to just ‘do it’ without being able to code & code well, unless you are willing to just settle with the most basic of representations.

Edit: I’ll note that all of this is done by defining these actions entirely separately, and just giving them input parameters, a weighting algorithm (intended to be used by spline curves from the input parameters returning a final weight), and defining what each action does. The tree is built on the fly, based upon the actions an NPC can do. There’s no pre-setup behaviour tree for this.

Do you have any links that explain this more?

Im looking to make it very simple, and then build on it

Hi Tony,

Well, except for all but the most trivial cases, I would expect it to take significantly less time to get decent behavior with smart objects and utility curves than it would to set up a FSM or behavior tree.

But yeah, NEEDSIM looks pretty much like what I had in mind, and it seems to be implemented well.

I suspect part of the problem may be the way it’s presented. People probably see it as good for animals and villagers, but not good for (say) museum guards or other such core characters. This is a mistake, I think, but an understandable one, especially given the content of the NEEDSIM demos.

:smile: I’ve been there too. For very simple behaviors, or Mario-level enemies purposely designed to be easy to understand, an FSM is often the easiest way to go.

But I do think they’re sometimes used in cases where a drive-reduction model would work much better.

@drdenner , I’m happy to help, but let’s not do it here — let’s do that over in your original thread. And, you should check out NEEDSIM and see if that suits you.

This sounds like a pretty sophisticated system, and I bet it works well once you’ve got it all tuned up!

Yes, I agree with that. But there are so many people using FSMs — sometimes, via one of the graphical FSM editors in the asset store, or even Unity’s Animator — in cases where a utility approach would work better. A utility system is quite easy to use once you have the basic infrastructure set up, but that infrastructure may seem daunting to somebody who’s never done it before. And if it’s designed right, it could be quite general, with places for you to plug in your own options.

For sure! To determine weighted results, we use spline curves, specifically, the animation curves. So the GUI was built in to define curve types, or define our own easily. Edited my post above, but each action can just take in any set of parameters, such as current health, target health, number of enemies nearby, etc, and combine the spline curves to get a finalized weight result. (Thanks, by the way. Won’t be done for a while, but it comes together day by day).

I mention it in a sense because these types of structures could be flexible to use generically, as long as the user is willing to define the actions and parameters themselves - but making it easy for them to do so.

I’m not publishing any of our code (too much work to clean it up, and it’s tightly integrated besides, and it’s 2+ years into a 3+ year project… kinda hold onto our code dearly), but hoping to provide some thoughts on how to set up various generic options.

To build on that example above; give an NPC desires, define actions that affect the world, and define the inputs of how important those actions are to complete. If taken to another level, I guess some automation could be done to automate how important they are based on how much it affects them, but that will break down the moment you bring in complexity beyond the simple desires (delegates and functions can be extremely useful here to separate and build it generically, by allowing custom functions to calculate results, or more spline curves that can be set).

There’s tons of ways this can go, and if I’m reading correctly, you were actually considering the opposite; an action affects various desires, so it finds the actions based on which affects the desires the most, versus my approach here which is to define what is important to an action and when to do an action. I would venture to say that mine is more flexible when you cannot define how an action affects the base desires, but your thoughts would be far more intuitive to the average user.

1 Like

I was very interested in NeedSim and had plans to use it for our NPCs. However, when it came out with a $75 price tag, I decided not to buy it. Not because it isn’t worth it but because it is the first item from that developer and I have been so burned in the past by “one time developers”. I would have jumped at something from you, Tony, even at the same or a higher cost.

The other reason is that we have other AI for combat and for animals and I have no idea how these will work together. The integration with other assets that we see coming from you, Opsive, and others really helps one feel more confident that they can use both assets together. I don’t know about NeedSim so cautious.

To be fair, the developer is still active on the forums and did update the asset. I suppose if we asked some of the other asset developers, they might help us to integrate.

I have a feeling that is why it didn’t sell well. I will keep watching his forum thread though and maybe in the future.

2 Likes