Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: [1] 2

Author Topic: Context-Based AI -- Special moves and behaviours  (Read 1651 times)

Granite26

  • Bay Watcher
    • View Profile
Context-Based AI -- Special moves and behaviours
« on: September 03, 2009, 04:25:23 pm »

This is only half suggestion, it’s as much ‘discussion of a neat AI strategy as relates to a DF type game for my own amusement’

Context Sensitive AI for various behaviours.

Actors have goals, and have a set of actions they can take to achieve those goals.  In a game where you can create any creature imaginable, and doubly so where an AI is expected to be able to generate these creatures on the fly, it’s important to be able to assign behaviours to an actor in a procedural manner.  A good model for this is Context Sensitive AI.

Overview:   In context sensitive AI, the actor has an overarching goal it’s trying to achieve.  During each cognitive ‘turn’, the AI first determines whether it’s overarching goal is still appropriate.  This is really ‘be a PC follower, civ member (dwarf), animal, raider, or soldier’.  Other than flipping between soldier and civ, the only real change likely to happen is between PC follower and civ member in adventure mode, although if you start a fortress on a dark tower it’s possible to have the AI flip between raider and civ.

The next layer is current activity.  For civ members, this involves whatever civ task they are engaged in (mining, building, farming).  Attacking, retreating and following are the other major categories here.  A check needs to be made that the activity is still valid.  (This is where LOS checks for monsters are performed, checks are made to make sure your task destinations aren’t outside your burrow, and verification is made that the task is still the most important thing the dwarf has to do.)

Finally there is the plan step.  For civ activities, the plan usually consists of the path.  Obviously actors path to an item and remember that path.  During the planning step, a dwarf would recheck that path (quick spot check to make sure the bridge isn’t raised and no doors have been locked, and the path doesn’t take you into violation of any standing orders).  This is NOT a repath, but could become one if necessary.

Layers could obviously be stacked deeper if need be, for instance, a biological layer could be added over everything to handle the need to eat, sleep, and drink.

Basically, what happens is, every cognitive ‘turn’ (which need not be every actor action turn), the actor digs down through sets of behaviours it has available to it.  The behaviours are organized by context, allowing an actor to only compare a small subset of ‘reasonable’ actions each turn.  It also creates a generic ‘behaviour fitness test’ that automatically adds behaviours to a creature as they become appropriate for that creature.  For example, all actors would test for hunger related activities (go get food, hunt, hunt for vermin), but creatures that don’t eat would never find them to be fit activities because they would never be hungry.  The vermin hunter tag would enable that behaviour to be selected.  Imps with the ‘throw fireball’ option will be able to select that combat option from their list (and have it automatically sort for when it’s the best option).



Example:

Goblin Siege behaviour.

As it stands goblins show up with murder in their hearts, path to the nearest dwarf, and then run when they are mutilated.  Let’s see how this works in a context sensitive world.

Step 1:  Arrival.

The goblin shows up at your fort cloaked.  He knows he is a raider.  His first check is to see whether he should still be a raider.  The fitness check is run:  He’s a raider class creature at a site owned by a hostile civ.  Being a civilian, soldier, PC follower all come back 0.  Being an animal comes back rather low, and the raider comes back high.  Yup, he’s still going to act like a raider.

He next decides what to do as a raider.  At a base level, he can ‘Scout’, ‘Assault’, or ‘Retreat’.  He’s cloaked, there are no enemies in sight, and all of his friends are alive and happy.  The numbers come back Scout 90, Assault 50, and Retreat 20 (Goblins are inherently skittish critters).  Scouting it is.

Under the Scouting AI, the only option is ‘path to closest dwarf’.  Later on, we’d like to add a few more options, like ‘ambush specific dwarf’ or ‘disarm traps’, but for now, this is what we’ve got.  Pathing to the nearest dwarf comes up with an action plan (the actual path).

Step 2: Stalking

Our murderous goblin is well on his way.  He takes a few steps down his path.  After he gets a few turns, he stops to rethink things.  Yup, he’s still a raider.  Yup, he’s still scouting, and still pathing to the nearest dwarf.  Checking the path however, there’s a problem.  The dwarf has moved!  So he recalculates his path and continues on.

Step 3: The Attack

Finally, the dwarf is in sight: A lone wood cutter with naught but 2 dogs to guard him.  The goblin reconsiders.  He’s still a raider.  Now, the numbers are closer.  Scout is still 90 (it will be so as long as he’s cloaked.)  Assault has increased to 70 (there is an enemy that is close), and he’s rethinking this whole combat thing now that he’s actually seen a foe (Retreat = 40).  Scouting is still the high number, so he continues to close.

Step 4: Discovered!

Continuing to simply path to the dwarf, the goblin runs into one of the dogs.  He is no longer cloaked.  He’s still a raider.  Scout has dropped to 50 (where it should stay).  Assault is 85 (next to an opponent) and Retreat is 40.  The goblin switches contexts to Assault.  Assault’s context actions simply consider all enemies in LOS as potential targets.  Their potential is a combination of the value of the target, it’s threat rating, and it’s proximity.  In this case, the dwarf is worth 50 to the dogs 25, but the adjacent dogs proximity provides a boost to keep it the preferred target (Dog=75, Dwarf = 65, other Dog = 40)

Now that the goblin knows who to attack, it considers how.  This is where things get REALLY complicated.  It considers a host of combat options, most of which it rejects out of hand (for example, the fitness of the ‘throw fireball’ action is 0).  In the end, it’s only real options are ‘attack with sword’, ‘attack with shield’, ‘drop sword and wrestle’, ‘drop shield and wrestle’, and ‘back away’.  By far, the sword option is preferred, so it does that.

Step 5: Combat

This process continues for several turn, with the other dog closing to melee, and the fight raging on.  Because both dogs are now in melee range, there is nothing to choose between them for attacks.  The goblin does, however, have a preference for continuing to attack the same dog he’s been in combat with, although a dodge that bring him out of contact or a chance for a killing strike at the other dog may change that preference.

Step 6: Retreat

Note that throughout this entire process he’s continuing to evaluate his status as a raider and also considering either fleeing or going back into scout mode (unlikely).  After the dogs are mobbed and killed, the goblin will likely stay in assault mode and attack the next closest dwarf (since everything is still far away, the closer animals won’t have sufficient nearness weights to overcome DWARFDEATH).  At a certain point the changing odds (both through armed dwarves getting closer, and nearby allies dying), the Retreat option will win out over the assault option. 

This is another place where the context sensitive AI has power.  The switch to retreat is not a single binary moment where everyone runs.  Each actor will determine when to retreat based on its own circumstances.  Assuming that one units decision to retreat will affect another’s, this will create a ripple effect on the battlefield.  Additionally, an actor that finds itself outnumbered in an area may retreat back, but finding itself suddenly in the midst of allies, may reacquire its courage.  Imagine a group of goblins attacking with several troll aides.  When the fighting starts to go south, the isolated goblins may flee the field, while those surrounded by their troll allies may fight on.[/size]



“But Granite26”, you say, “All of these behaviours describe exactly what goes on now!”  That is true, and intentional.  The power here is in generalization and decision-making.  If you want to complicate the way siegers go about attacking a fort, you’ve got everything divided up into neat little packages.  Suppose you want to add a new ‘trap disarm’ behaviour for your goblins.  Obviously a goblin in a fight or running isn’t going to stop to disarm traps.  Simply add a ‘trap disarm’ context under scouting.  In the future, all raiders, of every stripe, will consider entering into the trap disarm context.  (and thus looking for traps, or going to traps others have found, and attempting to disarm them).  This AI behaviour will be gated.  Thus, while all raiders will consider it, only raiders with graspers and intelligence will actually try it.  Additionally, the context fitness tests automatically compare this behaviour with all other possible behaviours that are appropriate to the given situation.



Other ways this works:

Hunger:  Tired of your dwarves stopping in mid action because they are hungry?  High level context weights involving how long a task is expected to complete will compete nicely with increasing hunger.  If your dwarf is bored, he’s liable to eat earlier than if he’s in the middle of a task, and a long task that is near to completion may be capable of motivating a dwarf to drive herself to great thirst before stopping for a drink.

Multiple special combat capabilities:  Do you spit acid, or throw a fireball?  Fitness tests innately compare the merits of all special actions, without the need to program special logic for combinations.

Actor motivated behaviour:  When do I run away?  How often do I eat?  Using actor specific variables in the different fitness tests will allow each individual creature to behave in an entirely different fashion, despite the fact that EVERY ACTOR uses the exact same AI routines.

Multiple leverage of behaviours:  There are numerous ways to get into a fight, but they’ve all got different reasons for being there.  Hunters, Raiders and Guards all get into fights, but they’ve got different reasons for being there, and need different checks to get out.  A hierarchical structure allows for ‘Combat AI’ to be generalized nicely.  (For example, a Raider would constantly be checking ‘is it time to run away’, while a Hunter would need to allow animals to escape after it had killed it’s quota)

What vs. How:  Things like ‘which side of the tile to mine’ could be decided after ‘mine this tile’.  (Even if the best way to mine the tile was part of the decision TO mine that particular tile)

Speed:  It may seem like checking ‘Am I still a goblin raiding the fort’ is useless overhead (it’s not, charm skills, etc), but reducing the number of options applicable in a situation WHILE still being able to recognize when a situation has changed (and consider new options) reduces a lot of AI overhead.
« Last Edit: September 03, 2009, 04:35:49 pm by Granite26 »
Logged

Granite26

  • Bay Watcher
    • View Profile
Re: Context-Based AI -- Special moves and behaviours
« Reply #1 on: September 03, 2009, 04:25:55 pm »

TL/DR : System for making AI hierarchical, such that actors decide what they want to do from a set of equally broad options, tunneling down to the simplest level of planned behaviour.

Allows the system to consolidate behaviours into a single entity AI.

Me being a jerk:  Yes this is long and complicated.  I won't be offended if you don't read it.  If all you're going to say is 'it's too complicated' or 'it's too long to read', just don't bother commenting.  Insights on other AI paradigms or facets of context-based AI extremely welcome
« Last Edit: September 03, 2009, 04:30:51 pm by Granite26 »
Logged

Bricks

  • Bay Watcher
  • Because you never need one brick.
    • View Profile
Re: Context-Based AI -- Special moves and behaviours
« Reply #2 on: September 03, 2009, 05:30:23 pm »

That was an interesting read, at least.  I kinda got the whole "that is currently what goes on" feel, so glad to see you caught that.  Still not sure that its altogether different from the current game, just more developed.  I think you sort of missed some other possible benefits:

-From a modder's point of view (assuming a great deal of this could be controlled), it turns certain tags like [NOFEAR] into a weighted property of that race.  So, yeah, maybe your civilization of spiders is generally vicious and refuses to retreat, but that doesn't mean that they lack the good sense to depart when the giant boot civilization starts attacking.  Something like curiosity, which could motivate the booze-guzzlers and theives, could be moderated in the same way.  Race and personality traits become weights, instead of slots on a roulette table.

-Prioritization is currently...lacking.  Lots of associated problems here, but it would certainly be interesting to see how a dwarf, who is on fire, decide whether or not to jump into a pond despite his lack of swimming skills.  A starving noble might not bother with the mandates, or maybe kick them up a notch out of annoyance.  The weird binary "I am a civilian, run away/I am now a soldier, attack!" behavior would definitely improve.

-Animals, monsters, and civilized folk with goals would make adventure mode a bit more alive.  Dialogue would be improved if they could comment on what they are doing at that moment.  Again, game totally undeveloped here.  It's currently very silly that only military units can join up with your adventurer.  If that farmer over there wrestled 200 skeletal carp to death after his wife and children were killed, he should have some interest in taking his vengeance elsewhere.

-Your model has some implications that a civ member could decide, given dire circumstances, to join another civ or go rogue.  Annoying if its too common, but it would make towns more interesting if they had the occasional goblin political refugee, or you could run into feral elves in the wild.
Logged
EMPATHY - being able to feel other peoples' stuff.

lucusLoC

  • Bay Watcher
    • View Profile
Re: Context-Based AI -- Special moves and behaviours
« Reply #3 on: September 03, 2009, 06:40:52 pm »

awsome post. i remember reading about this kind of ai before. wasn't it used in fear 2?

i am sure some of the ai behaves this way, at least on a basic level, but i like the idea to add more weight to a task as it gets closer to compleation, rather than just having a universal hard cutoff (like for hunger).
Logged
Quantum dumps are proof of "memory" being a perfectly normal dimension in DF. ~Gazz

Dwaref

  • Bay Watcher
    • View Profile
Re: Context-Based AI -- Special moves and behaviours
« Reply #4 on: September 03, 2009, 06:59:09 pm »

Your red text makes me upset!
The contents were good though, very soothing, but the texty makes me upset!
Logged
He is somewhat reserved. He prefers to be alone. He doesn't need thrills or risks in life. He is never optimistic or enthusiastic about anything. He has a fertile imagination. He is open-minded to new ideas. He is put off by authority and tradition. He is very straightforward with others. He is very disorganized. He thinks it is incredibly important to strive for excellence. He has very little self-discipline. He takes time when making decisions. He doesn't really care about anything anymore.

Roara Wolf

  • Bay Watcher
    • View Profile
Re: Context-Based AI -- Special moves and behaviours
« Reply #5 on: September 03, 2009, 08:21:10 pm »

I like this, although I think that creatures should only consider options that are ever physically possible.

For example, currently, there is no way for a goblin to throw a fireball. There's no way for a dwarf to fly. Etc.

There could also be memory attached to these things. Like, if you remember something being absolutely impossible a little while ago, you're more likely to not consider it for a while, or until the situation has drastically changed, or something. And one way you'd be able to tell that the situation has changed is by your actual options changing drastically from what they were.

I think this models psychology just a little bit. If what you're doing has worked up until this point, there's little reason to shift gears or consider something different.

And because I am in love with the idea of intellectual abilities affecting AI, things like creativity could determine how often or how easily you look at options that you're pretty sure should be impossible.

In fact, you could probably apply mental characteristics to how often a dwarf checks actually viable options. Although, that said, I think fear is a special case that should sort of force itself to be evaluated, if at all possible, or something. I dunno.

I haven't thought of how viable these ideas are from a programming perspective though. >_>
Logged

Granite26

  • Bay Watcher
    • View Profile
Re: Context-Based AI -- Special moves and behaviours
« Reply #6 on: September 03, 2009, 08:33:00 pm »

I like this, although I think that creatures should only consider options that are ever physically possible.

For example, currently, there is no way for a goblin to throw a fireball. There's no way for a dwarf to fly. Etc.

I haven't thought of how viable these ideas are from a programming perspective though. >_>

Checking to see if something is possible is functionally equivilant to considering it.  Ideally, the 'GetFitnessFireball' function would start with if !Creature.ThrowFireball then return 0

Dwaref

  • Bay Watcher
    • View Profile
Re: Context-Based AI -- Special moves and behaviours
« Reply #7 on: September 03, 2009, 11:17:46 pm »

yeah, say it's a kobold thief who has just stolen your wand of fireballs or something
Logged
He is somewhat reserved. He prefers to be alone. He doesn't need thrills or risks in life. He is never optimistic or enthusiastic about anything. He has a fertile imagination. He is open-minded to new ideas. He is put off by authority and tradition. He is very straightforward with others. He is very disorganized. He thinks it is incredibly important to strive for excellence. He has very little self-discipline. He takes time when making decisions. He doesn't really care about anything anymore.

Granite26

  • Bay Watcher
    • View Profile
Re: Context-Based AI -- Special moves and behaviours
« Reply #8 on: September 04, 2009, 08:23:27 am »

Bastardization:
Ok, so this is really a bastardization of true context-based AI.  True CBAI would have the goblin know that it's fighting the dog, and the 'fight target' context would first check to make sure that fighting the target was still the right thing to be doing.  The advantage to that is that you'd never consider variables that didn't matter in your current context (for example, you'd ignore your biological heartbeat that normally checks hunger and thirst)

It was originally more for Robotics type applications.  Take the Darpa challenge where your robot is racing across the desert.  In the 'wide-open' context, you wouldn't be pathing, you'd just be running full speed towards your target while looking for obstacles.  Not worrying about any sort of pathfinding (just holding to a bearing) allows you more real-time cycles to check your visuals for obstacles in the distance (which is good, because far away makes them smaller).  When you actually catch an obstacle, you drop out of wide open, and enter into an 'object-avoidance' context, which at it's simplest level just drives around the object.  You still have to path around it, which means you start caring about things 10-15 deg or more outside your straight line, and you start running visual processing on them.

So, anyway, adding a top-down manager structure bastardizes the process



Combat manager:

If you wanted to be clever and memory heavy, each unit could track an array of pointers to the combat options it has based on it being it, and based on it's equipment, but that makes picking up a rock a little more complicated  (since from then on, every layer would have to ask the rock if it has any high suitable actions for this situation.  Which'd be kinda cool if your dwarf started thinking about hitting everything with a rock whenever it was in a meeting).

Other funness:

Back to that 'rock in a meeting' example.  Magic item of rage that increased the 'attack things' option, even in civilian life?

Actual rage based tantrums:  Rage builds up, making the tantrum seem like a better option.  When that option is taken, the context knows 'currently in a rage' and so is more likely to continue choosing that, however, it's fitness is continually decreasing.  This is about normal though.  The interesting thing is MORE IMPORTANT STUFF CAN SNAP YOU OUT OF IT.  How many people do you know that will continue to pitch a hissy fit while lava fills the room?

happysad

  • Bay Watcher
    • View Profile
Re: Context-Based AI -- Special moves and behaviours
« Reply #9 on: September 04, 2009, 08:28:26 am »

Quote
Now that the goblin knows who to attack, it considers how.
Good description, but this is not a good way to program AI.
When selecting actions, you always want to score ALL combinations and pick the best one from the list.
For the task Assault, you would score each combination of enemy and attack/weapon. For the task Retreat, you would score how threatened you feel currently.
Consider what would happen if you were armed with a flaming weapon, and near you were a fire imp. If you decided to attack without taking your weapon into account, you would find that Assault was not the best action after all, since your weapon would be ineffective. Retreat was the best action in this case. And if an ice creature stood a bit further away than the fire imp, he would have been the better target for the flaming weapon.

Similarly, when selecting the best hauling job, you need to score all combinations of items, starting points and destinations before you can pick the best.

When the AI scores ALL task types (work/sleep/eat/combat etc.) with ALL relevant combinations of weapons, items and so on, it can make intelligent decisions.

And as you say, this decision code must run regularly to account for changing circumstances. In addition, all dwarves must consider tasks currently undertaken by other dwarves, since they may be better suited for the task (closer, higher skilled, more fit etc.) and be able to 'steal' the task.
Logged

Granite26

  • Bay Watcher
    • View Profile
Re: Context-Based AI -- Special moves and behaviours
« Reply #10 on: September 04, 2009, 08:50:52 am »

Details of scoring fitness weren't covered.  If you consider every possible option, it's just a search tree for actions, but still fits the model presented.  The point is that if a dwarf is standing next to a goblin, he doesn't need to think about being hungry or thirsty or whether any of a hundred different hauling jobs is important.  In his context he can either fight, or run away.

Sure, the decision depends on the armament matchup between them, the position of their allies, perceived skill imbalances, etc.  The point is that by using context you can separate out a lot of useless considerations.

The same is true for a dwarf showing up for work.  When looking for a job, he'll consider all the things the fort needs done, but won't consider punching his neighbor in the face or attacking the hoary marmot across the screen.  Sure he can snag jobs from other dwarves (another dwarf already owning the task is likely to reduce it's fitness by a lot, though), but once he has a task in hand, it'll take a special situation for him to consider stopping (since the 'acquire task' context will score lower than the 'do the task you've got' context).

lucusLoC

  • Bay Watcher
    • View Profile
Re: Context-Based AI -- Special moves and behaviours
« Reply #11 on: September 04, 2009, 11:31:56 am »

I think the handling of job assignments would be better handled by a managment ai. That would simplify thigs a great deal, as you would not have individual unit ais considering the same job.

Targeting ai is still best left at the unit ai level though.
Logged
Quantum dumps are proof of "memory" being a perfectly normal dimension in DF. ~Gazz

Granite26

  • Bay Watcher
    • View Profile
Re: Context-Based AI -- Special moves and behaviours
« Reply #12 on: September 04, 2009, 12:52:20 pm »

Presuming a dwarf considers new jobs with an understanding of what's already taken, is there a danger of this?

Silverionmox

  • Bay Watcher
    • View Profile
Re: Context-Based AI -- Special moves and behaviours
« Reply #13 on: September 04, 2009, 01:38:58 pm »

AI like this could be cut in pieces small enough for players to design themselves and exchange. That will immensely enhance the replay value. You could have a directory of AI variations for all actors to use.. you'd never know what to expect.
Logged
Dwarf Fortress cured my savescumming.

Bricks

  • Bay Watcher
  • Because you never need one brick.
    • View Profile
Re: Context-Based AI -- Special moves and behaviours
« Reply #14 on: September 04, 2009, 02:13:28 pm »

I think the handling of job assignments would be better handled by a managment ai. That would simplify thigs a great deal, as you would not have individual unit ais considering the same job.

Targeting ai is still best left at the unit ai level though.

I'm not sure that they have to be so different.  Dwarves could simply "know" if a job is claimed, much in the same way they "know" that job exists.  Your point is reasonable in real-life, but if we are cutting corners, might as well cut all of them.

You could maintain some managing AI if we could assign management jobs to specific dwarves.  For example, if you have a mining guild master, he could assign jobs to the different miners much in the way that a squad leader directs his squad.  It would also have some influence, apart from shear efficiency/first-come-first-serve, on how jobs are distributed.  Of course, we currently have a manager, who could handle the jobs with yet unspecified guild leaders, so you don't have to immediately assign semi-noble positions when you establish the fort.
Logged
EMPATHY - being able to feel other peoples' stuff.
Pages: [1] 2