How enemy AI works in Dicey Dungeons

The next weblog publish, until differently famous, used to be written through a member of Gamasutra’s neighborhood.
The ideas and reviews expressed are the ones of the author and no longer Gamasutra or its mum or dad corporate.


Hi there all! For the previous month or so, I have been tackling one of the crucial greatest technical issues in my new sport, Dicey Dungeons – making improvements to the enemy AI sufficient for the general unencumber of the sport. It is been beautiful fascinating, and plenty of it used to be new to me, so I assumed I might write slightly bit about it. 

First up, a type of disclaimer: I am not a pc scientist – I am simply a kind of individuals who realized sufficient about programming to make video video games, after which stopped studying anything else I did not have to be told. I will be able to normally litter via, however an actual programmer more than likely should not have approached all this the best way I did.

I attempted to put in writing all this in a slightly prime degree way in thoughts, in order that with a bit of luck the fundamental concepts all make sense to different non-programmers. However I am evidently no professional on all these things, and if I have gotten any of the main points incorrect in explaining the idea, let me know within the feedback – satisfied to make corrections!

Let’s get started through explaining the issue!

The issue

If you happen to’ve no longer performed Dicey Dungeons, here is a crash route: it is a deckbuilding RPG, the place every enemy has a number of apparatus playing cards that do various things. Additionally, they roll cube! They then position the ones cube at the apparatus to do injury, or motive more than a few standing results, or heal, or defend themselves from injury, or plenty of different issues. Here is a easy instance of a tiny frog the use of a large sword and slightly defend:

A extra difficult instance: this Handyman has a spanner, which permits it so as to add two cube in combination (so three + 2 would come up with a unmarried five, and a four + five would come up with 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 does not do a lot injury, however which has a “countdown” which persists throughout turns.

Yet one more vital complication: there are standing results which alternate what you’ll do. An important of those are “Surprise”, which disables apparatus at random till you unshock it through the use of an additional cube on it, or “Burn”, which units your cube on fireplace. When your cube are on fireplace, you’ll nonetheless use them – however it’s going to value you 2 well being issues. Here is what a suave Handyman does once I surprise and burn all his apparatus and cube:

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

So, the issue: how do you’re making an AI that may work out the most efficient factor to do on it is 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 vital apparatus?

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 apparatus from left to proper, found out the most efficient cube to make use of on it, and used it. This labored nice, till it did not. So, I added extra regulations.

As an example, I handled stunning through having a look on the unshocked apparatus, and deciding what cube I might wish to use on it when it used to be unshocked, then marking that cube as “reserved” for later. I handled burning cube through simply checking if I had sufficient well being to extinguish them, and opting for whether or not or to not do it through random probability.

Rule after rule after rule to maintain the entirety I may just call to mind, and ended up with an AI that sorta kinda labored! In fact, it is superb how smartly this hodge-podge of regulations held in combination – the AI in Dicey Dungeons may no longer have at all times achieved the suitable factor, nevertheless it used to be surely satisfactory. A minimum of, for a sport that is nonetheless a piece in growth.

However over the years, the program of including an increasing number of regulations to the AI truly began to wreck on the seams. Other folks found out constant exploits to get the AI to do silly issues. With the suitable setup, one of the crucial bosses may well be tricked into by no means if truth be told attacking you, as an example. The extra regulations I added to take a look at to make things better, the extra bizarre issues would occur, as regulations began to struggle with different regulations, and edge instances began to crop up.

In fact, one solution to repair this used to be to simply follow extra regulations – paintings via every drawback one at a time, and upload a brand new if observation to catch it. However I believe that might have simply been kicking the issue additional down the street. The limitation the program had used to be that it used to be best ever all for this query: “What’s my subsequent transfer?”. It will by no means glance forward, and work out what may occur from a selected suave aggregate.

So, I made up our minds to start out over.

The vintage resolution

Glance up AI stuff for video games, and most likely the primary resolution you’ll be able to come throughout is a vintage choice making set of rules referred to as Minimax. Here is a video that explains how it is implemented to designing a Chess AI:

Enforcing Minimax works like this:

First, you create a light-weight, summary model of your sport, which has all of the related knowledge for a selected second in time of the sport. We’re going to name this the Board. For Chess, this will be the present place of all of the items. For Dicey Dungeons, it is a listing of cube, apparatus, and standing results.

Subsequent, you get a hold of a worth serve as – a solution to measure how smartly the sport goes for a selected configuration of the sport – i.e. for a selected board. For Chess, perhaps a board the place all of the items are of their preliminary positions is value zero issues. A board the place you have got captured an enemy Pawn is perhaps value 1 level – and perhaps a board the place you have got misplaced certainly one of your personal Pawns is value -1 issues. A board the place you have got your opponent in checkmate is value infinity issues. Or one thing like that!

Then, from this summary board. you simulate taking part in all of the conceivable strikes you’ll make, which will provide you with a brand new summary board. Then, you simulate taking part in all of the conceivable strikes from the ones forums, and so forth, for as many steps as you wish to have. This is a very good representation of that from

What we are doing is making a graph of all of the conceivable strikes each gamers could make, and the use of our price serve as to measure how the sport goes.

This is the place Dicey Dungeons splits from Minimax: Minimax comes from mathematical sport concept, and it is designed to determine the most efficient sequence of strikes in a global the place your opponent is making an attempt to maximize their rating. It is so named as a result of it is about seeking to minimise your loss when your opponent performs so that you can as to maximize their acquire.

However for Dicey Dungeons? I if truth be told do not care what my opponent is doing. For the sport to be a laugh, you simply need the AI do make strikes that make sense – to determine the easiest way to play their cube on their apparatus to make it an excellent battle. In different phrases, all I care about is the Max, no longer the Min.

This means that: for the Dicey Dungeons AI to make a smart decision, all I wish to do is create this graph of conceivable strikes, and search for the board which has the most efficient rating – then make the strikes that result in that time.

A easy enemy flip

Good enough, examples! Let’s take a look at this frog once more! How does it make a decision what to do? How does it know that it is selected motion is the most efficient one?

It principally simply has has two choices. Position the 1 at the broadsword and the three at the defend, or do it the opposite direction round. It clearly makes a decision that it is at an advantage striking that three at the sword than the 1. However why? Neatly, as it checked out all of the results:

Position the 1 at the sword and you find yourself with a rating of 438. Position the three on it, and you find yourself with a rating of 558. Nice, adequate! Then, I am getting a greater rating through striking the three at the Sword, achieved.

The place’s that rating coming from? Neatly, the Dicey Dungeons scoring device these days considers:

  • Injury: An important case – 100 issues for each level of wear and tear dealt.
  • Poison: A very powerful standing impact that the AI considers virtually as vital as injury – 90 issues for every poison.
  • Causing different Standing results: Like Surprise, Burn, Weaken, and many others. Each and every any such is value 50 issues.
  • Bonus standing results: Causing your self with certain standing results like Protect, and many others, is value 40 issues every.
  • The use of apparatus: The use of any piece of kit is value 10 issues – as a result of if all else fails, the AI will have to simply attempt to use the entirety.
  • Lowering countdowns: Some apparatus (just like the Pea Shooter) simply wishes a complete worth of cube to turn on. So, the AI will get 10 issues for each countdown level it reduces.
  • Cube Pips: The AI will get five issues for each unused Cube Pip – so a 1 is value five, and a 6 is value 30. That is supposed to make the AI want to not use cube it does not wish to use, and does so much to make its strikes glance extra human like.
  • Period: The AI loses 1 level in line with transfer, making it in order that lengthy strikes have very moderately decrease ratings than quick ones. That is in order that if there are two strikes that might differently have the similar rating, the AI will pick out the shorter one.
  • Therapeutic: Price simply 1 level in line with well being level healed, as a result of whilst I need the AI to believe it in a tie spoil, I are not looking for it to be preoccupied with it. Different issues are at all times extra vital!
  • Bonus rating: Bonus rating can also be implemented to any transfer, to trick the AI into doing one thing they won’t differently make a decision to do. Used very sparingly.

After all, there is additionally two particular instances – if the objective of the assault is out of well being, that is value 1,000,000 issues. If the AI is out of well being, that is value minus 1,000,000 issues. Those imply that the AI won’t ever by chance kill themselves (through extinguishing a cube when they’ve very low well being, say), or by no means cross up a transfer that might kill the participant.

Those numbers are not highest, evidently – take, as an example, those these days open problems: #640, #642, #649 – nevertheless it if truth be told does not topic that a lot. Even kind of correct numbers are sufficient to incentivise the AI to kind of do the suitable factor.

Tougher enemy turns

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

It is choice tree is, uh, slightly extra difficult:

Sadly, even somewhat easy instances explode in complexity beautiful temporarily. On this case, we finally end up with 2,670 nodes on our choice graph to discover, which takes relatively just a little longer to determine than the frog did – perhaps up to a 2nd or two.

A large number of that is combinatorial complexity – as an example, it isn’t important which of the 2s we use to unshock the apparatus first of all, this set of rules considers them as two separate selections, and creates a complete tree of branching selections for each. This finally ends up with a department that is a wholly needless reproduction. The are an identical aggregate issues of deciding which cube to extinguish, which apparatus to unshock, what cube to make use of in what order.

However even recognizing needless branches like this and optimising them (which I have been doing to some degree), there’s at all times going to be some degree the place the complexity of the conceivable variations of selections results in massive, sluggish choice timber that take ceaselessly to determine. So, that is one serious problem with this way. This is every other:

This vital piece of kit (and issues find it irresistible) motive an issue for the AI, as a result of they’ve an unsure end result. If I put a six on this, perhaps I’m going to get a 5 and a one, or I may get a 4 and two, or perhaps I’m going to get two threes. I may not know till I do it, so it is truly exhausting to make a plan that takes this into consideration.

Fortunately, there’s a just right option to either one of those issues that Dicey Dungeons makes use of!

The trendy resolution

Monte Carlo Tree Seek (or MCTS, for brief) is a probabilistic choice making set of rules. Here’s a, uh, moderately atypical video which however explains the speculation at the back of Monte Carlo primarily based choice making truly smartly:

Mainly, as an alternative of graphing out each unmarried conceivable transfer we will make, MCTS works through checking out sequences of random strikes, after which maintaining a tally of those that went the most efficient. It may well magically make a decision which branches of our choice tree are the “maximum promising” because of a formulation referred to as the Higher Self assurance Certain set of rules:

That formulation, through the best way, is from this very useful article on Monte Carlo Tree Searches. Do not question me the way it works!

The beauty of MCTS is that it will possibly normally in finding the most efficient choice with no need to brute power the entirety, and you’ll use it on the similar summary board/transfer simulation device as minimax. So, you’ll kinda do each. Which is what I have ended up doing for Dicey Dungeons. First, it tries to do an exhaustive growth of the verdict tree, which normally does not take very lengthy and results in the most efficient end result – but when that is having a look too large, it falls again to the use of MCTS.

MCTS has two truly cool homes that make it nice for Dicey Dungeons:

One – it is nice at coping with uncertainty. As a result of it is operating over and over, aggregating information from every run, I simply let it simulate unsure strikes like the use of a lockpick naturally, and over repeated runs, it’s going to get a hold of a lovely just right vary of ratings of the way smartly that transfer will figure out.

Two – it can provide me a partial resolution. You’ll principally do as many simulations as you favor with MCTS. Actually, in concept, for those who let it run ceaselessly, it will have to converge on precisely the similar outcome as Minimax. Extra to the purpose for me, although – I will be able to use MCTS to normally get a just right choice out of a restricted quantity of pondering time. The extra searches you do, the simpler the “choice” you’ll be able to in finding – however for Dicey Dungeons, it is incessantly just right sufficient to simply do a couple of hundred searches, which best takes a fragment of a 2nd.

Some cool tangents

So, that is how the enemies in Dicey Dungeons make a decision the way to kill you! I sit up for introducing this within the upcoming model v0.15 of the sport!

Listed below are some tangential ideas that I do not truly know the place to place:

The ones graphs I have been appearing gifs of? Together with this one on twitter:

Positive, the neighbours appear to be truly playing their birthday party, however the REAL a laugh is occurring right here: spent the night hacking in combination a GraphML exporter for Dicey Dungeons’ new AI! Now I will be able to discover enemy strikes and if truth be told see what is going on step by step! #screenshotsaturday

— Terry (@terrycavanagh) November 25, 2018

I created those through writing an exporter for GraphML, which is an open supply graph document structure that may be learn with many various gear. (I have been the use of yEd, which is excellent and which I will be able to suggest so much.)

Additionally! A part of making this all paintings used to be working out the way to let the AI simulate strikes, which used to be a large puzzle in and of itself. So, I stopped up enforcing an motion scripting device. Now, while you use a work of kit, it runs those tiny little scripts that appear to be this:

Those little scripts are done through hscript, a haxe primarily based expression parser and interpreter. This used to be surely more or less a ache to enforce, however the payoff is excellent: it makes the sport tremendous, tremendous modable. I hope that after this sport in the end comes out, folks will have the ability to use the program to design their very own apparatus that may do principally any cool factor they are able to suppose up. And, even higher, for the reason that AI makes sense sufficient to judge any motion you give it, enemies will have the ability to work out the way to do no matter bizarre modded apparatus you give it!

Thank you for studying! Satisfied to respond to any questions or to elucidate any of this within the feedback beneath!

(And, in the end, in case you are curious about taking part in Dicey Dungeons, you’ll get alpha get right of entry to on at this time, or for those who want, wishlist us on steam, which can ship you slightly reminder when the sport comes out.)

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: