Author Topic: Simple AI/Logic for chosing enemy skills?  (Read 17559 times)

SomeGuy

  • Rogueliker
  • ***
  • Posts: 64
  • Karma: +0/-0
    • View Profile
    • Email
Simple AI/Logic for chosing enemy skills?
« on: June 12, 2015, 11:05:47 AM »
In the current RL i'm developing, enemies will have two skills as follows:

Main Skill: this will be the long range damaging skill. It can be used when the enemy is far away from the player. Not necessarily the favourite skill.
Secondary Skill: this will be a shorter range skill. It will come either as a support skill (buff/healing/shielding, etc..), as a short range high-damage skill or as a debuffing skill.

But I'm not sure which logic should the enemies follow for choosing one skill or the other. Any idea?

Thanks.

Xecutor

  • 7DRL Reviewer
  • Rogueliker
  • *
  • Posts: 263
  • Karma: +0/-0
    • View Profile
Re: Simple AI/Logic for chosing enemy skills?
« Reply #1 on: June 12, 2015, 12:49:41 PM »
Assuming that these skills have cooldowns, your monsters can use skills on cooldownd. But each time the skills is used you can calculate effective cooldown by adding some random number to the base cooldown.

SomeGuy

  • Rogueliker
  • ***
  • Posts: 64
  • Karma: +0/-0
    • View Profile
    • Email
Re: Simple AI/Logic for chosing enemy skills?
« Reply #2 on: June 12, 2015, 05:00:04 PM »
Assuming that these skills have cooldowns, your monsters can use skills on cooldownd. But each time the skills is used you can calculate effective cooldown by adding some random number to the base cooldown.

Your assumption is wrong.

Aukustus

  • Rogueliker
  • ***
  • Posts: 440
  • Karma: +0/-0
    • View Profile
    • The Temple of Torment
Re: Simple AI/Logic for chosing enemy skills?
« Reply #3 on: June 12, 2015, 07:43:41 PM »
Make it based on the distance

  • If player is far and outside the range of Secondary skill: exclusively Main skill
  • If player is just inside the range of the Secondary skill: 25% chance of Main skill and 75% chance of Secondary skill
  • Else: exclusively Secondary skill

And when it selects the Secondary skill it could have a 33% chance for each type (support/damage/debuff), of course if support is active (for example shielded) then it will be 50/50 damage and debuff and if debuff is active then exclusively damage.

SomeGuy

  • Rogueliker
  • ***
  • Posts: 64
  • Karma: +0/-0
    • View Profile
    • Email
Re: Simple AI/Logic for chosing enemy skills?
« Reply #4 on: June 12, 2015, 08:38:25 PM »
Make it based on the distance

  • If player is far and outside the range of Secondary skill: exclusively Main skill
  • If player is just inside the range of the Secondary skill: 25% chance of Main skill and 75% chance of Secondary skill
  • Else: exclusively Secondary skill

And when it selects the Secondary skill it could have a 33% chance for each type (support/damage/debuff), of course if support is active (for example shielded) then it will be 50/50 damage and debuff and if debuff is active then exclusively damage.

The 3rd option (excusively secondary skill) cannot be entered.

If the enemy is far from player, will always shoot long range spell.
If he is close, the will shot either long or short. The 100% chances are divided among those 2 options, so there is no chance for him to cast anything else.

The only way to enter that option is to be totally out of range, in which case, it will cast secondary skill instead of trying to get closer to the player, which is, I think the most logical way to act.


Whatever, I found a possible solution using your "ranges" idea. The difference is that in either case, if the enemy is unbuffed, he will buff himself with a 45% chance. If the player is not debuffed, he will debuff the player at 33% chance and if he can heal himself, he will do so with a 33% chance
« Last Edit: June 12, 2015, 09:52:37 PM by SomeGuy »

guest509

  • Guest
Re: Simple AI/Logic for chosing enemy skills?
« Reply #5 on: June 13, 2015, 01:55:00 AM »
For AI behavior a lot of people give monster type a set of parameters, a subroutine of actions it will take.

Like I'll start all my monsters in WANDER mode. Then, when they see the player (or smell him or hear him, though I've never implemented that) they activate.

Their active mode will be preset. Some flee straight away, some attack straight towards the player, some will get to a certain distance and try to maintain that distance as a priority, else shoot the player. Some will hit the player once a flee (thieves do this). Some will flee towards their closest ally and only go into attack mode when they are with allies. Some stand and start shooting without moving.

There are many ways to do this, but usually you see each enemy having a certain behavior type (flee, hit and flee, charge, find friends and charge, or ignore and wander forever, etc...). You can reuse these behavior profiles on each different monster type. You can even change their behavior type based on their range from the player. Like less than 5 spaces away then flee, greater than 5 then shoot. Or less than 3 then charge (or use short range weapon) or greater than 3 then use long ranged.

Also, sometimes when wandering the creature will go to sleep, they stop moving and can get hit for huge damage.

A stealth specialist creature might go invisible when they see the player.

I'm probably not answering your question directly, but I hope I've given some ideas. Good luck!

SomeGuy

  • Rogueliker
  • ***
  • Posts: 64
  • Karma: +0/-0
    • View Profile
    • Email
Re: Simple AI/Logic for chosing enemy skills?
« Reply #6 on: June 13, 2015, 08:49:00 AM »
For AI behavior a lot of people give monster type a set of parameters, a subroutine of actions it will take.

Like I'll start all my monsters in WANDER mode. Then, when they see the player (or smell him or hear him, though I've never implemented that) they activate.

Their active mode will be preset. Some flee straight away, some attack straight towards the player, some will get to a certain distance and try to maintain that distance as a priority, else shoot the player. Some will hit the player once a flee (thieves do this). Some will flee towards their closest ally and only go into attack mode when they are with allies. Some stand and start shooting without moving.

There are many ways to do this, but usually you see each enemy having a certain behavior type (flee, hit and flee, charge, find friends and charge, or ignore and wander forever, etc...). You can reuse these behavior profiles on each different monster type. You can even change their behavior type based on their range from the player. Like less than 5 spaces away then flee, greater than 5 then shoot. Or less than 3 then charge (or use short range weapon) or greater than 3 then use long ranged.

Also, sometimes when wandering the creature will go to sleep, they stop moving and can get hit for huge damage.

A stealth specialist creature might go invisible when they see the player.

I'm probably not answering your question directly, but I hope I've given some ideas. Good luck!

That was of great help indeed.
It doesn't any hard to implement.

But anyway, once the monster has chosen to attack instead of flee/wander/stealth, how can he decide to choose attack1 instead attack2, or viceversa?
Should he try to get closer to the player to use the short range attack? Or should use it only if the player approaches? When does the enemy need to heal? when health is critical? when health is non critical, when health <= 50%? etc...
That kind of questions are the ones I don't know the optimal answer.

BTW, I don't want a cheating AI that may know the player health value, attacks and equipment or other info that only the player is supposed to know.

akeley

  • Rogueliker
  • ***
  • Posts: 348
  • Karma: +0/-0
    • View Profile
Re: Simple AI/Logic for chosing enemy skills?
« Reply #7 on: June 13, 2015, 12:51:39 PM »
Depending on how complex your monster design is, you can tie these behaviours to their IQ or particular characteristic. So, a troll-type (dumb) would be more inclined to melee kamikaze bumrushes, ignoring health status etc, while a thief/mage-type (cunning) would be much more, well, cunning - heal up, keep the distance, choose more tactically appropriate skill and so on (having said that, it`d be nice to see these tropes shaken up a bit - college drop-out sorcerers fumbling spells or clever ogres, though that would probably require some flavour communicated).

Furthermore monster types could (should :P) also have different behaviours due to their innate "character" - something you might describe in a bestiary, if stepping out of usual kobold/rat/paladin comfort zone.

Intelligence could also govern monsters` assessment and understanding of the player - which is not necessarily cheating. Some info is just visible - like players general state of health or weapon wielded, while enemies with  psychic or magic abilities could "see" your spells/skills too.

guest509

  • Guest
Re: Simple AI/Logic for chosing enemy skills?
« Reply #8 on: June 14, 2015, 04:55:32 AM »

That was of great help indeed.
It doesn't any hard to implement.

But anyway, once the monster has chosen to attack instead of flee/wander/stealth, how can he decide to choose attack1 instead attack2, or viceversa?
Should he try to get closer to the player to use the short range attack? Or should use it only if the player approaches? When does the enemy need to heal? when health is critical? when health is non critical, when health <= 50%? etc...
That kind of questions are the ones I don't know the optimal answer.

BTW, I don't want a cheating AI that may know the player health value, attacks and equipment or other info that only the player is supposed to know.

Answers to those questions will take some play testing, like when to heal and such. As for what attack to use, well, that can be part of the behavior profile. If you have a good ranged attack then moving away if the player is too close and otherwise attacking is a good behavior profile.

Like for, say, Skeleton Archer who has a good ranged attack, low HP and a weak close attack.

Set_Behavior: Attack at Range

If Player < Ranged_Attack/2 spaces away then
    Move_Away

If Player > Ranged_Attack spaces away then
   Move_Towards

If Player is adjacent AND Monster cannot move away then
   Close_Attack

Else Use_Ranged_Attack


You can set that as the Shoot_From_Range behavior for all of your ranged based enemies.

If you want to set their behavior dynamically, having the program decide how the monster should act, you'll have to have some way of weighing the different attacks they can use and then setting the behavior based on their best attack.

I'd say most people set the behavior in the code, like:
Archer: Stand at range.
Swordman: Chase and bump attack.
Healer: Heal wounded friend, flee 2 turns if attacked.
Javelin Guy: Stand at range 2 turns, then switch to chase and bump attack.

Stuff like that.

You can set a threshhold for using healing, you can set this in all behavior profiles, like:
If Monster has Heals AND Monster_HP < 30% then Cast_Heals.

guest509

  • Guest
Re: Simple AI/Logic for chosing enemy skills?
« Reply #9 on: June 14, 2015, 05:17:28 AM »
Here is the basics of how I did some very simple behavior based on class and range in KlingonRL

Klingon: Wander around randomly, switch to attack mode if a federation ship shows up.
Merchant: Wander. Flee if player decloaks.
Science Vessel: Wander, move at a decloaked player until 2 spaces away, then flee for 10 turns. Repeat.
Fed Cruisers, Pirate, Dreadnaught: Wander, attack/bump the decloaked player.
Starbase: Don't move. Shoot at range.

So really there were only a few behaviors to code:
-Move toward if you see the player
-Move away if you see the player and have no weapons
-Wander if you don't see the player
-Shoot if you see the player (if you have torps, bases only usually)

Easy peasy.

Trystan

  • Rogueliker
  • ***
  • Posts: 164
  • Karma: +0/-0
    • View Profile
    • my blog
Re: Simple AI/Logic for chosing enemy skills?
« Reply #10 on: June 14, 2015, 07:36:24 AM »
It depends on how you program it, but one thing I've found useful is if each actor/creature/whatever asks each ability that it has "what should I do and how likely should i do it?". Each ability would answer with something that has a callback function and how important it is that the action happens.

So if a creature has a heal spell, the chance of it being called might be "100% - current_hp / total_hp". So the lower the current hp, the more likely the heal spell is chosen as the current action. The callback function would be something that heals the caster. The code for the ai of a healing spell (in a javascript-ish language) might like like:

function doAi(caster) {
  return {
    chance: caster.hp /caster.maxHp,
    callback: function() {
      caster.hp = Math.min(caster.maxHp, caster.hp + 10);
    }
  }
}

Then the creature's ai would decide each action to take based on the chances, then call the appropriate callback function. The ai might choose the most likely action, or do a lottery type system, or add a bit of randomness. Even movement could be included as an action in this system.

My previous 7DRL from 2013 (which you can play or watch it play itself in your browser at http://trystans.blogspot.com/2013/09/pugnacious-wizards-2-version-10.html) does this.
See https://github.com/trystan/PugnaciousWizards2/blob/master/src/EnemyWizard.as#L36 for the caster's ai
See https://github.com/trystan/PugnaciousWizards2/blob/master/src/spells/HealAndWeaken.as#L25 for one of the healing spells.



« Last Edit: June 14, 2015, 09:00:13 AM by Trystan »

AgingMinotaur

  • Rogueliker
  • ***
  • Posts: 805
  • Karma: +2/-0
  • Original Discriminating Buffalo Man
    • View Profile
    • Land of Strangers
Re: Simple AI/Logic for chosing enemy skills?
« Reply #11 on: June 16, 2015, 09:14:07 AM »
http://www.roguebasin.com/index.php?title=Roguelike_Intelligence is the classic article series on Roguelike AI, of course. Also, if you dig through the archives over at Ascii Dreams, you might find that Andrew D. has written something nice about this topic (and others of interest for budding RL devs). And here's another take on the subject. And I think some people swear by neural networks.

I use a variant of the state machine AI described in the Roguebasin articles linked to above. Each actor is always in a current state (with a current target), which basically consists of a series of if/else statements. The basic "zombi attack" state would be:

if can_attack(target): attack(target)
elif can_approach(target): approach(target)
else: return_to_previous_state()

Functions like "can_attack", "can_approach", "can_heal", "can_debuff", "can_buff" iterate through the actor's skills and inventory and sees if the appropriate action can be performed. I put a tag on every command/action, so that a healing potion is tagged "heal" and a spell of slow monster is tagget "debuff". That in itself opens up for surprising behaviour, such as a goblin suddenly zapping a wand at you or tossing a potion of speed at one of it's comrades.

For more varied states, add a probability for each action. Say, if the function d100() spits out a random number between 1 and 100:


if can_shoot(target) and d100()<75: shoot(target)
elif can_debuff(target) and d100()<50: debuff(target)


In addition to states, each actor's personality is defined by a list of "bias switches" and a dictionary of "standard states". The "bias switches" are used to observe the playing field and react appropriately. For instance, a "self preservation" bias switch might dictate that whenever another being harms the actor, the actor turns hostile towards that being. Bias switches can also be contained within states, so that a "relaxed wandering" state could include a bias switch to turn and attack any enemy within line of sight. The "standard states" come in a few tactical categories: "attack", "flee", "buff", "relax", and just make the system a bit easier to maintain. For instance, a dog and an archer might share "reptile brain"-bias switches that sets them to "attack" any enemy that comes into sight. But they will have different states listed as their preferred "attack" states. This way, we don't need individually defined "wander" states for every single being.

It's a nice starting point for a system, since it's quite flexible. Let actors know about which group they belong to, where they have their home, and maybe important landmarks, and you can get seemingly complex behaviour, such as hyenas hunting in packs, villagers sacrificing at the temple or having a drink at the local tavern, quest givers behaving properly, etc.

And you can easily expand NPCs by plugging in extra bias switches. A "coward" personality trait might include "flee" as one of the standard "attack" states; a "dog hater" might have bias switches that makes him/her hate dogs on sight, and love people who kill dogs; a "drunk" may get "go to tavern" as a "relax" state, etc.

Remember that you don't need a too subtle system. To see if an actor heals itself, you don't need complex computations based on current hp, relative strength of opponents etc. It's suffient to say something like: "if I'm hurt and have healing remedies, 50% heal self." To the player, the end result will look almost exactly the same.

Also, make sure the beings don't do all their interesting stuff off-screen. It's probably good to make them "sleepy" when they're not actively chasing the player or something like that, and wake them up when they come into view. It's more fun to come across a knight fighting an orc, than just the remains of the battle.

As always,
Minotauros
This matir, as laborintus, Dedalus hous, hath many halkes and hurnes ... wyndynges and wrynkelynges.