Development tips enemy AI enemy design game design Guest posts indie developer Tech

How Enemy AI Works In Dicey Dungeons

How Enemy AI Works In Dicey Dungeons

Editor’s Notice: this submit was initially revealed by Terry Cavanagh, indie recreation designer at present engaged on Dicey Dungeons. To remain updated with what Terry is at present engaged on, your can discover him on Twitter, or comply with his different tasks on his web site right here. 

For the previous month or so, I’ve been tackling one of many largest technical issues in my new recreation, Dicey Dungeons – enhancing the enemy AI sufficient for the ultimate launch of the sport. It’s been fairly fascinating, and plenty of it was new to me, so I assumed I’d write just a little bit about it.

First up, a type of disclaimer: I’m not a pc scientist – I’m simply a type of individuals who discovered sufficient about programming to make video video games, after which stopped studying something I didn’t should study. I can often muddle via, however an actual programmer in all probability wouldn’t have approached all this the best way I did.

I attempted to write down all this in a reasonably excessive degree strategy in thoughts, in order that hopefully the essential concepts all make sense to different non-programmers. However I’m for positive no skilled on all these things, and if I’ve gotten any of the small print fallacious in explaining the idea, let me know within the feedback – completely happy to make corrections!

Let’s begin by explaining the issue!

The issue

Should you’ve not performed Dicey Dungeons, right here’s a crash course: it’s a deckbuilding RPG, the place every enemy has a choice of gear playing cards that do various things. Additionally, they roll cube! They then place these cube on the gear to do injury, or trigger numerous standing results, or heal, or defend themselves from injury, or numerous different issues. Right here’s a easy instance of a tiny frog utilizing an enormous sword and somewhat defend:

A extra difficult instance: this Handyman has a spanner, which permits it so as to add two cube collectively (so three + 2 would offer you a single 5, and a four + 5 would offer you a 6 and a three). It additionally has a Hammer, which “shocks” the participant in the event that they use a six on it, and a Pea Shooter, which doesn’t do a lot injury, however which has a “countdown” which persists throughout turns.

Another essential complication: there are standing results which change what you are able to do. An important of those are “Shock”, which disables gear at random till you unshock it through the use of an additional cube on it, or “Burn”, which units your cube on hearth. When your cube are on hearth, you’ll be able to nonetheless use them – nevertheless it’ll value you 2 well being factors. Right here’s what a intelligent Handyman does once I shock and burn all his gear and cube:

There’s extra to it than that, in fact, however that’s principally the gist of it!

So, the issue: how do you make an AI that may work out the perfect factor to do on it’s flip? How does it know which burning cube to extinguish, which cube to make use of for unshocking and which cube to save lots of for essential gear?

What it used to do

For a very long time, my AI in Dicey Dungeons simply had one rule: It checked out all of the gear from left to proper, found out the most effective cube to make use of on it, and used it. This labored nice, till it didn’t. So, I added extra guidelines.

For instance, I handled surprising by wanting on the unshocked gear, and deciding what cube I might need to use on it when it was unshocked, then marking that cube as “reserved” for later. I handled burning cube by simply checking if I had sufficient well being to extinguish them, and selecting whether or not or to not do it by random probability.

Rule after rule after rule to cope with every part I might consider, and ended up with an AI that sorta kinda labored! Truly, it’s superb how properly this hodge-podge of guidelines held collectively – the AI in Dicey Dungeons won’t have all the time achieved the fitting factor, however it was undoubtedly satisfactory. A minimum of, for a recreation that’s nonetheless a piece in progress.

However over time, this technique of including increasingly more guidelines to the AI actually began to interrupt on the seams. Individuals found constant exploits to get the AI to do silly issues. With the appropriate setup, one of many bosses might be tricked into by no means truly attacking you, for instance. The extra guidelines I added to attempt to make things better, the extra bizarre issues would occur, as guidelines began to battle with different guidelines, and edge instances began to crop up.

In fact, one solution to repair this was to only apply extra guidelines – work by means of every drawback one after the other, and add a brand new if assertion to catch it. However I feel that may have simply been kicking the issue additional down the street. The limitation this technique had was that it was solely ever involved with this query: “What’s my subsequent transfer?”. It might by no means look forward, and work out what may occur from a specific intelligent mixture.

So, I made a decision to start out over.

The basic answer

Lookup AI stuff for video games, and certain the primary answer you’ll come throughout is a basic determination making algorithm referred to as Minimax. Right here’s a video that explains the way it’s utilized to designing a Chess AI:

Implementing Minimax works like this:

First, you create a light-weight, summary model of your recreation, which has all of the related info for a specific second in time of the sport. We’ll name this the Board. For Chess, this may be the present place of all of the items. For Dicey Dungeons, it’s an inventory of cube, gear, and standing results.

Subsequent, you provide you with a worth perform – a option to measure how nicely the sport goes for a specific configuration of the sport – i.e. for a specific board. For Chess, perhaps a board the place all of the items are of their preliminary positions is value zero factors. A board the place you have got captured an enemy Pawn is perhaps value 1 level – and perhaps a board the place you’ve misplaced one among your personal Pawns is value -1 factors. A board the place you will have your opponent in checkmate is value infinity factors. Or one thing like that!

Then, from this summary board. you simulate enjoying all of the potential strikes you can also make, which provides you a brand new summary board. Then, you simulate enjoying all of the potential strikes from these boards, and so forth, for as many steps as you need. Right here’s a superb illustration of that from

What we’re doing is making a graph of all of the potential strikes each gamers could make, and utilizing our worth perform to measure how the sport goes.

Right here’s the place Dicey Dungeons splits from Minimax: Minimax comes from mathematical recreation concept, and it’s designed to determine the perfect collection of strikes in a world the place your opponent is making an attempt to maximise their rating. It’s so named as a result of it’s about making an attempt to minimise your loss when your opponent performs so to as to maximise their achieve.

However for Dicey Dungeons? I truly don’t care what my opponent is doing. For the sport to be enjoyable, you simply need the AI do make strikes that make sense – to determine the easiest way to play their cube on their gear to make it a good battle. In different phrases, all I care about is the Max, not the Min.

Which suggests: for the Dicey Dungeons AI to make a superb transfer, all I have to do is create this graph of attainable strikes, and search for the board which has the most effective rating – then make the strikes that result in that time.

A easy enemy flip

Okay, examples! Let’s take a look at this frog once more! How does it determine what to do? How does it know that it’s chosen motion is the most effective one?

It principally simply has has two choices. Place the 1 on the broadsword and the three on the defend, or do it the opposite means round. It clearly decides that it’s higher off placing that three on the sword than the 1. However why? Properly, as a result of it checked out all of the outcomes:

Place the 1 on the sword and you find yourself with a rating of 438. Place the three on it, and you find yourself with a rating of 558. Nice, okay! Then, I get a greater rating by putting the three on the Sword, accomplished.

The place’s that rating coming from? Nicely, the Dicey Dungeons scoring system at present considers:

  • Injury: Crucial case – 100 factors for each level of injury dealt.
  • Poison: An necessary standing impact that the AI considers virtually as essential as injury – 90 factors for every poison.
  • Inflicting different Standing results: Like Shock, Burn, Weaken, and so forth. Every one in every of these is value 50 factors.
  • Bonus standing results: Inflicting your self with constructive standing results like Defend, and so forth, is value 40 factors every.
  • Utilizing gear: Utilizing any piece of kit is value 10 factors – as a result of if all else fails, the AI ought to simply attempt to use every part.
  • Decreasing countdowns: Some gear (just like the Pea Shooter) simply wants a complete worth of cube to activate. So, the AI will get 10 factors for each countdown level it reduces.
  • Cube Pips: The AI will get 5 factors for each unused Cube Pip – so a 1 is value 5, and a 6 is value 30. That is meant to make the AI want to not use cube it doesn’t want to make use of, and does so much to make its strikes look extra human like.
  • Size: The AI loses 1 level per transfer, making it in order that lengthy strikes have very barely decrease scores than brief ones. That is in order that if there are two strikes that might in any other case have the identical rating, the AI will decide the shorter one.
  • Therapeutic: Value simply 1 level per well being level healed, as a result of whereas I would like the AI to think about it in a tie break, I don’t need it to be preoccupied with it. Different issues are all the time extra necessary!
  • Bonus rating: Bonus rating might be utilized to any transfer, to trick the AI into doing one thing they may not in any other case determine to do. Used very sparingly.

Lastly, there’s additionally two particular instances – if the goal of the assault is out of well being, that’s value one million factors. If the AI is out of well being, that’s value minus one million factors. These imply that the AI won’t ever by chance kill themselves (by extinguishing a cube once they have very low well being, say), or by no means cross up a transfer that might kill the participant.

These numbers aren’t good, for positive – take, for instance, these presently open points: #640, #642, #649 – nevertheless it truly doesn’t matter that a lot. Even roughly correct numbers are sufficient to incentivise the AI to kind of do the appropriate factor.

More durable enemy turns

The frog case is straightforward sufficient that even my shoddy code can work out each single risk in zero.017 seconds. However, then issues get a bit extra difficult. Let’s take a look at that Handyman once more.

It’s choice tree is, uh, somewhat extra difficult:

Sadly, even comparatively easy instances explode in complexity fairly shortly. On this case, we find yourself with 2,670 nodes on our choice graph to discover, which takes fairly a bit longer to determine than the frog did – perhaps as a lot as a second or two.

Numerous that is combinatorial complexity – for instance, it doesn’t matter which of the 2s we use to unshock the gear initially, this algorithm considers them as two separate selections, and creates an entire tree of branching selections for each. This finally ends up with a department that’s a completely pointless duplicate. The are comparable mixture issues with deciding which cube to extinguish, which gear to unshock, what cube to make use of in what order.

However even recognizing pointless branches like this and optimising them (which I’ve been doing to some extent), there’s all the time going to be some extent the place the complexity of the attainable permutations of selections results in large, sluggish choice timber that take ceaselessly to determine. So, that’s one main drawback with this strategy. Right here’s one other:

This essential piece of kit (and issues prefer it) trigger an issue for the AI, as a result of they’ve an unsure consequence. If I put a six on this, perhaps I’ll get a 5 and a one, or I’d get a 4 and two, or perhaps I’ll get two threes. I gained’t know till I do it, so it’s actually arduous to make a plan that takes this under consideration.

Fortunately, there’s a good answer to each of those issues that Dicey Dungeons makes use of!

The fashionable answer

Monte Carlo Tree Search (or MCTS, for brief) is a probabilistic determination making algorithm. Here’s a, uh, barely odd video which however explains the thought behind Monte Carlo based mostly determination making rather well:

Principally, as an alternative of graphing out each single potential transfer we will make, MCTS works by making an attempt out sequences of random strikes, after which retaining monitor of those that went the most effective. It could magically determine which branches of our determination tree are the “most promising” because of a formulation referred to as the Higher Confidence Sure algorithm:

That components, by the best way, is from this very useful article on Monte Carlo Tree Searches. Don’t ask me the way it works!

The beauty of MCTS is that it may possibly often discover one of the best choice with out having to brute pressure the whole lot, and you may apply it to the identical summary board/transfer simulation system as minimax. So, you’ll be able to kinda do each. Which is what I’ve ended up doing for Dicey Dungeons. First, it tries to do an exhaustive enlargement of the choice tree, which often doesn’t take very lengthy and results in one of the best consequence – but when that’s wanting too massive, it falls again to utilizing MCTS.

MCTS has two actually cool properties that make it nice for Dicey Dungeons:

  • One – it’s nice at coping with uncertainty. As a result of it’s operating again and again, aggregating knowledge from every run, I simply let it simulate unsure strikes like utilizing a lockpick naturally, and over repeated runs, it’ll provide you with a reasonably good vary of scores of how nicely that transfer will work out.
  • Two – it may give me a partial answer. You’ll be able to principally do as many simulations as you want with MCTS. Actually, in principle, should you let it run eternally, it ought to converge on precisely the identical end result as Minimax. Extra to the purpose for me, although – I can use MCTS to usually get a superb choice out of a restricted quantity of considering time. The extra searches you do, the higher the “determination” you’ll discover – however for Dicey Dungeons, it’s typically ok to only do a number of hundred searches, which solely takes a fraction of a second.

Some cool tangents

So, that’s how the enemies in Dicey Dungeons determine learn how to kill you! I look ahead to introducing this within the upcoming model v0.15 of the sport!

Listed here are some tangential ideas that I don’t actually know the place to place:

These graphs I’ve been displaying gifs of? Together with this one on twitter:

Positive, the neighbours appear to be actually having fun with their social gathering, however the REAL enjoyable is occurring right here: spent the night hacking collectively a GraphML exporter for Dicey Dungeons’ new AI! Now I can discover enemy strikes and truly see what’s happening step-by-step!

— Terry (@terrycavanagh) November 25, 2018

I created these by writing an exporter for GraphML, which is an open supply graph file format that may be learn with many various instruments. (I’ve been utilizing yEd, which is nice and which I can advocate lots.)

Additionally! A part of making this all work was determining learn how to let the AI simulate strikes, which was an enormous puzzle in and of itself. So, I ended up implementing an motion scripting system. Now, if you use a bit of kit, it runs these tiny little scripts that seem like this:

These little scripts are executed by hscript, a haxe based mostly expression parser and interpreter. This was undoubtedly type of a ache to implement, however the payoff is nice: it makes the sport tremendous, tremendous modable. I’m hoping that when this recreation lastly comes out, individuals will be capable of use this technique to design their very own gear that may do principally any cool factor they will assume up. And, even higher, as a result of the AI is sensible sufficient to guage any motion you give it, enemies will have the ability to work out how one can do no matter bizarre modded gear you give it!

Thanks for studying! Pleased to reply any questions or to make clear any of this within the feedback under!

(And, lastly, in the event you’re all in favour of enjoying Dicey Dungeons, you will get alpha entry on proper now, or should you want, wishlist us on steam, which can ship you just a little reminder when the sport comes out.)