The AI of DOOM (1993)

The AI of DOOM (1993)

Posted on

AI and Video games is a YouTube sequence made attainable due to crowdfunding on Patreon. Help the present to have your title in video credit, vote for future episode matters, get early entry and unique patron-only movies.

DOOM is the archetype of the first-person shooter style and helped outline a whole technology of video games within the Nineteen Nineties. Whereas not the primary of its variety, nor even the primary by builders id Software program, it was a sport that modified the panorama of the business. And it is an essential matter to discover not simply due to its recognition, however as a result of – very similar to different video games from the 90s we have explored to this point – it achieved a lot utilizing restricted {hardware} and with out lots of the instruments and requirements sport builders and gamers now take without any consideration.

DOOM is, in essence, a dungeon crawler with weapons, during which gamers should navigate a myriad of advanced non-linear environments to search out keys and activate switches that may transfer them one step nearer to the exit. Alongside the way in which, gamers struggle a myriad of hellish and demonic creatures, starting from possessed gun-wielding troopers to fireball tossing imps, flying enemies such because the cacodemon and misplaced souls and the brute power of the pinky Demon and Baron of Hell. The sequel DOOM II, launched in 1994 added much more variation, with Hell Knights, Revenants, Arachnotrons, the Mancubues, Ache Elementals and the Archvile. All of those characters transfer by way of the world, reply to participant behaviour and assault utilizing their very own inner logic. Maybe essentially the most spectacular side is the sheer variety of enemies that the sport can accommodate directly, all of the whereas dealing with pathfinding, line of sight assessments, collision checks, interactions and extra.

However DOOM was no slouch. The sport capitalised on the large modifications occurring within the dwelling PC market of the interval. {Hardware} was changing into extra reasonably priced and the panorama had shifted in a approach that benefitted sport growth. Intel CPUs, notably the 486 from 1989 was changing into more and more reasonably priced, with the 486DX2 launched in 1992 proving much more highly effective. RAM was changing into cheaper and the transfer towards Home windows 3.1 had pushed graphics adapters in chipsets to grow to be extra commonplace. This modification out there led to id software program throwing out just about every little thing they’d constructed for his or her earlier sport: Wolfenstein 3D in 1992 and as we’ll see, there are quite a few optimisations to the code that enables for it to realize issues that related video games of the period would fail to copy.

The Core Logic

DOOM‘s enemy AI is however one class of object within the sport referred to as a ‘thinker’: these are any objects within the sport that must decide that usually will course of or evolve over a interval of a number of frames. The unique DOS construct of the sport runs at 35 frames per second, with every body dealing with the logic for all 4 major lessons of thinker: the primary class is all the actors: which means the participant, non-player characters and the extent itself. Secondly, there’s the standing bar on the underside of the display screen, then the in-game map and lastly the rendering of the HUD itself.

The precise core logic of the enemies is a Finite State Machines: a easy however efficient mechanism to state {that a} character executes a particular behaviour when in a given state, and what the circumstances are that may drive it to alter. Whereas launched a number of years later, Valve’s Half-Life popularised FSMs for contemporary shooters courtesy of the character of its C++ codebase, whereas additionally offering mechanisms for characters to have targets they’ll attempt to fulfill. DOOM operates in the same construction, but it surely lacks lots of the identical thrives, but in addition a number of the extra seamless integration. The large purpose for that is that there is no scripting language within the DOOM engine that enables designers to configure the NPCs to do what they need. As we’ll see shortly, the precise logic is all written within the C programming language, and there are lots of very intelligent tweaks carried out to permit for the number of completely different enemy designs to not point out all of the encounters gamers face within the sport.

The total diagram is proven right here now, and there are some attention-grabbing quirks in the way it all works, so let’s talk about the high-level behaviour after which I’ll burrow down into particular person matters.


Enemies at all times begin within the SPAWN state: a mechanism whereby they’re idle and await an occasion that may drive them to enter a extra energetic behaviour. They’re ready to see or hear one thing that may drive them to behave, and can merely stroll on the spot till then. Gamers of DOOM may have noticed this every so often, although the obvious occasion is in Entryway: the opening stage of DOOM II the place the zombieman have their again to you.

Upon receiving a sensory enter to inform them the enemy is close by, they go into the ‘See’ state, which permits them to maneuver across the map and head in direction of their designated goal. We’ll go into extra element shortly on how all of that works. Offered the state of affairs will enable it, an enemy can then try to assault the participant, leading to them transferring into the Melee or Vary states. Reverting again to the See state as soon as the assault is full.

In the meantime, the monster can solely enter the ache state externally: which is sensible given it ought to solely occur when the participant hurts it. In any other case, that will imply the enemies are basically hurting themselves I assume. It then performs the corresponding frames for rendering, and any sound results and will get again to the core logic. As we’ll see in a second, NPCs do not go into the ache state each time they take injury, however as an alternative, it is based mostly on a likelihood that’s encoded within the enemy’s design.

Lastly, a monster can die in one in every of two methods. There’s the easy DIE state, introduced on by the monster receiving a complete quantity of injury that exceeds its beginning well being. Nevertheless, there is a particular case known as XDIE, which is used when a monster is gibbed and turns right into a pile of goop. As soon as once more these states can solely be transitioned into by exterior logic, quite than the monster itself deciding it ought to flip into freakin’ confetti. The gib will solely happen if the injury obtained on the time of loss of life exceeds the remaining well being of the monster, plus its authentic beginning well being.

So for instance, Imps begin with 60 well being factors. If it has been shot already and is right down to 10 well being, then with a purpose to be gibbed, it has to obtain a minimal of 71 injury on the following assault. Given the decrease beginning well being values of the likes of the zombieman and shotgun man (who’ve 20 and 30 beginning well being respectively), it is comparatively simple to gib them with an exploding barrel, the rocket launcher and even the berserk pack. In the meantime, enemies such because the Cacodemon or Hell Knight which have 400 and 500 well being at begin are virtually unattainable to gib. That mentioned, you’ll be able to gib just about any monster if you happen to can telefrag them (i.e. by teleporting into them, on condition that does 10,000 injury). Similar to in Excellent Hatred, the second mission of DOOM‘s Thy Flesh Consumed episode the place you’ll be able to telefrag the Cyberdemon given it solely has a meagre 4000 well being.

You may discover there’s one final state: RAISE. This was launched in DOOM II courtesy of the Archvile, the demonic priest that may resurrect all enemies with exception of the Misplaced Souls, the Cyberdemon, the Spider Mastermind and different Archviles. As we’ll see in a minute, this truly leads to some bizarre behaviour throughout its precise execution. So let’s get into that subsequent: every enemy adheres to this state machine, however their particular person behaviours are fairly distinct. So how are they outlined and the way do they work?

Defining Actions

To execute the logic, any actor comparable to an enemy is spawned in with a thinker object hooked up to them. Every thinker is a struct written within the C programming language that has inside it a pointer to a operate of sport logic the enemy should execute, alongside a variety of further variables that assist it take form. This operate helps every monster to find out the actual behaviour it ought to exhibit, based mostly on its present state.

However the precise definition of every enemy in DOOM is definitely achieved outdoors of the codebase. A textual content file from the undertaking is imported and compiled into an ‘information’ file, which accommodates inside it all the enemy’s specs in addition to the definitions for all states. The data from the textual content file is compiled right into a struct that carries the next parts:

– Their inner ID quantity within the sport.
– The quantity of well being an imp begins with.
– Their motion velocity.
– Likelihood of ache state interrupts
– Their radius and peak
– The particular properties of the NPC.

Within the case of imps, they’re stable objects you’ll be able to’t stroll by way of (MF_SOLID), they are often shot (MF_SHOOTABLE) and broken and their loss of life counts in direction of the overall variety of enemies within the stage (MF_COUNTKILL).

As talked about beforehand, monsters don’t at all times go into the ache state when damage. As a substitute, it’s influenced by the ache state interrupt likelihood which within the codebase is known as ache probability. Each time an enemy is damage by a weapon, there’s a likelihood verify to determine whether or not the enemy will go into the PAIN state, thereby interrupting its motion and any assaults. The quantity ranges from 0 to 256 and is configured for every enemy sort. So within the case of the imp and shotgun man, these are 200 and 170 respectively. Which means that the imp, anytime it’s damage has a 79.3% probability of being put within the PAIN state, whereas the shotgun man solely has a 67.6% probability. However the trick is that every particular person hit from a weapon counts in direction of this. So whereas a rocket counts as just one hit, whereas the shotgun technically has seven separate assaults courtesy of its unfold. This is the reason the minigun is such a helpful weapon in opposition to the likes of the Cacodemon and Arachnotron, as a result of every monster has a ache probability of 128 (or 50%) however every consecutive bullet fired is one other alternative to set off the ache state (or, because it was referred to within the authentic DOOM handbook, making them “do the chaingun cha-cha”). This has a huge effect on weapon choice throughout gameplay – to a degree that it is so intrinsic to the gunplay of DOOM that it was reimplemented within the 2016 reboot. Some enemies have extremely low ache possibilities – with the Cyberdemon and Archvile each in single digits (5.5% and three.1%), whereas others just like the misplaced souls have a 100% ache probability. Additionally technically the barrels even have a ache probability, but it surely’s set to 0 – which y’know, is sensible as a result of they’re barrels.


On prime of all the details about the behaviour of the enemy itself, there are additionally particular person motion definitions for every state. These are customized definitions for every of the states within the state machine. So we are able to see that the imp makes use of the assault motion for each the melee and ranged FSM states. In the meantime the shotgun man does not have a state outlined for melee assaults.

Now as for the precise actions themselves, these are literally much more sophisticated. Every state is compiled down right into a definition for every particular person motion body that’s executed in that state. So if we take into account the assault for the imp, it blows up into much more data. This contains:

– The household of sprites which are learn from when rendering this behaviour and the frames for use.
– The variety of in-game tics that body executes for.
– The in-game motion within the supply code that it ought to execute on that motion body.
– The subsequent motion body it ought to then execute.


So within the case of the imp attacking (see above) it should…
– Render the primary two sprites for 8 frames every, and run the code that ensures the imp turns to face the participant.
– Then draw the third body for six frames and run the particular TroopAttack operate, which is designed particularly for the Imp.
– As soon as it has completed the third motion body, it should transition again to the primary ‘see’ state for the imp.

And while you have a look at the code for TroopAttack, it highlights why there is not a separate state for melee versus ranged assaults, given the Imp will verify if it is in melee vary after which decide to that, or it should merely launch a missle on the participant. These guidelines are in place for issues comparable to enemy characters attacking the participant, dying or being gibbed after taking injury and animated sprites within the setting. The enjoyable half, is returning again to the RAISE state talked about earlier, any enemy that may be resurrected by the archvile has a set of RAISE motion definitions, which have the very same set of sprite frames used for dying, but it surely performs them in reverse.

One final thing is that for every of those motion definitions, we’d like a separate body of animation, however that hides an extra complexity. Given enemies in DOOM could be rendered from 8 completely different angles relative to the participant’s place. So when one in every of these sprite households is getting used, it at all times pulls the suitable set of sprites for this character based mostly on their angle relative to the participant. So within the case of the Imp: there are 21 distinctive sprite indices. Which means you could have 168 particular person sprites to cowl all 8 angles. Nevertheless, one optimisation to this, is that some characters – such because the imp – are symmetrical. So as an alternative of getting 8 units of sprites, there are solely 5 and if a sprite set for that orientation is lacking, it merely takes the mirror equal. Sadly some characters such because the Cyberdemon, will not be symmetrical and they also nonetheless needed to construct every particular person sprite.

From Fabian Sanglard's 'Game Engine Black Book: DOOM'
From Fabian Sanglard’s ‘Sport Engine Black Ebook: DOOM’

Sight and Motion

Now that we all know the general core logic for the enemies in DOOM, subsequent up I need to give attention to the SEE state. Which is when an enemy has discovered a goal after which locks in on it. Chasing them across the map after which attacking after they can.

As talked about already, the SPAWN state has the enemy standing round and ready for one thing to set off them. This may occur in one in every of two methods: both they see an enemy, or they hear them. In both case, it establishes a goal for the NPC to move in direction of. There are some additional checks right here as as to whether the goal is shootable, which means they do not grow to be skittish and seek out something that makes a noise. But additionally, there’s… truly, no I will inform you the opposite secret in a minute.
As soon as the goal is established, it strikes into the primary SEE state from the diagram. This corresponds to a chase operate within the codebase, whereby the enemy will start the hunt and if it is inside vary for a melee or ranged assault, it should take a shot.

However wait, let’s again up: how do the enemies see or hear you? That is truly an enormous chunk of logic for DOOM given it is tied to how the map is designed. Plus, it is also some of the attention-grabbing facets of how the sport is optimised. In earlier movies on video games comparable to The Final of Us, Splinter Cell and Alien: Isolation, we talked about how imaginative and prescient cones are used for enemy imaginative and prescient. DOOM predates all that stuff, but in addition it is designed for early 90s PCs and very often there are dozens of enemies working sight checks directly. Even when all of the enemies are within the SPAWN state, then every of them is asking the Look() operate within the codebase 35 occasions a second. So not solely does it must work successfully, it additionally must be optimised.

DOOM enemies technically have 180 levels of imaginative and prescient and don’t have any long-distance imaginative and prescient minimize off. If the view between your self and the enemy just isn’t obscured, they are going to see you. Plus, peak differential does not matter. That is as a result of DOOM is a 3D render projected from a two-dimensional ground plan. The road of sight is parallel with the ground, therefore you may discover if you happen to’re above or beneath an enemy, if it is dealing with you then it should at all times discover you even when generally you’ll be able to’t see them in return.


So how does it be certain that an enemy can see you inside its area of view and likewise navigate in direction of you in a approach that is price efficient? The trick right here is that DOOM‘s map is damaged up into chunks utilizing what is named sectors, and people sectors are organised courtesy of a binary house paritioning algorithm or BSP. A BSP permits you to organise objects such that they preserve spatial data. That is used in order that the renderer at all times is aware of what elements of the sport map it wants to attract, by solely rendering the present sector and any connecting sectors in response to the BSP. This optimisation was added by programmer John Carmack to chop down rendering prices, given it saves on efficiency by making certain it does not attempt to render your entire stage – a problem that fashionable sport engines have now rectified. Now from this, some additional optimisations could be made to the AI as properly.

The map being damaged up into sectors implies that, very similar to the BSP, you’ll be able to document details about whether or not it is attainable for one character to see one other based mostly on their location. Visibility between sectors is definitely precomputed in DOOM courtesy of what’s referred to as the REJECT desk. This tells us that if a personality is in a given sector, whether or not they may even doubtlessly see one other character in one other sector. Like on this instance right here, it is virtually unattainable for the character in sector A to see anybody in sectors C and D, but it surely may in concept see you in sector B. So after they’re in search of the participant, the sport shortly does a verify in opposition to the REJECT desk to see whether or not it ought to even hassle working the sight take a look at.


As soon as the participant has been seen and a goal is acquired, then the monster will begin transferring towards you. Technically, there’s no precise pathfinding in DOOM. If there’s a direct path in direction of the participant, then it merely strikes in direction of you. If there is not, then it tries to move in that course and can bounce off partitions altering course because it goes. Within the occasion there is no attainable direct path to you, then it should transfer round randomly as an alternative. In an effort to handle all the enemies (and the participant) colliding with the extent, all the collision knowledge is precomputed as properly. The BLOCKMAP breaks up the map of the extent right into a grid that enables it to shortly verify if that house is navigable.


So yeah, if an enemy sees you it will kick-off and the state of affairs will get somewhat… tense. However what about while you pop right into a room, hearth off the shotgun and every little thing roars in anger at you.

Effectively, here is the humorous factor. Sure, the enemies in DOOM can hear you, however not in the way in which you’d assume. I imply, one may argue that the enemies truly SEE sound. I imply it actually units a sound goal within the Look() operate in any case.

Flood fill of E1M1 for sound propagation. Blocked only by doors (from Sanglard's 'Game Engine Black Book: DOOM')
Flood fill of E1M1 for sound propagation. Blocked solely by doorways (from Sanglard’s ‘Sport Engine Black Ebook: DOOM’)

When the participant fires their weapon, it units the participant as a ‘sound goal’ within the sector of the map that you simply’re in. If an enemy is in a sector the place a sound goal is created, it should wake it up and hunt the sound goal. However that will solely work in a single sector except the sound may journey. DOOM has a quite simple sound propagation, whereby a flood fill algorithm spreads the sound across the map supplied there aren’t any blockers that forestall the sound from dissipating additional. A superb instance of that is in (E2M4) Deimos Lab, given the doorway opens out to a protracted hall on the facet. As quickly as you end preventing the imps and shotgun troopers on the entrance, you may quickly be attacked by imps who heard all of the commotion lots of of meters away, given the gunfire propagated by way of the house.

Doorways between stage chunks will block the sound from travelling additional, but in addition stage designers may place blockers the place they wished to ensure the sound did not journey into areas the place an ambush was being ready (extra on that in a minute).


Some of the memorable facets of DOOM is that the monsters have a behavior of infighting. They will tear chunks out of one another if the temper strikes them. How on earth does that work? Effectively, it is truly fairly easy: there’s logic within the code that states within the occasion an NPC is attacked and damage by one other character, then it’d assign that character as their new goal. Even when it isn’t the participant. Naturally, because of this the demons do not assault each other with out trigger. So you could lure one enemy into the road of fireside of one other, and hope it by accident causes some pleasant hearth.

There are some exceptions to this, given Barons and Hell Knights cannot damage one another with their assaults. Ache elementals technically cannot get caught in infighting as a result of they damage different characters by spewing Misplaced Souls (which is able to then get focused as an alternative). And there is a particular edge case coded into the sport to cease Arch-Vile’s being attacked by different NPCs. This implies you could go and smash his cranium in your self… typical.

Designing Encounters

So we have defined the core logic, how particular person state actions are outlined and the optimisations for pathfinding, visibility and sound checks. However that is not sufficient to make DOOM behave the way in which it really works. The sport is filled with traps, of lifeless ends and rather more that assist create these wealthy and attention-grabbing encounters. The trick to that is two essential elements of the sport’s design.

Initially, the sport has quite a lot of completely different customized actions that may be synced up with the map. In case you’ve ever cracked open a DOOM map you may have observed lots of strains that cover contained in the map. A few of that is the precise geometry, whereas others are basically set off volumes. Which means the sport can detect when the participant crosses into that quantity (it is a quite common follow in fashionable sport engines). One trick DOOM closely employs is the extent editor permits for code snippets to be executed upon coming into a quantity, and it might probably additionally add a tag that references what elements of the map ought to be affected. Therefore you’ll be able to have a hidden door someplace round, stick some enemies in there, all of whom are within the SPAWN state and might’t hear any noise you make as a result of the key door prevents the audio propagation and also you’re good to go.

Nevertheless, every set off can solely have one operate pointing to at least one object. So what if you could have two issues occur directly? An excellent instance of that is the blue key lure within the Toxin Refinery (that is E1M3 btw). The lure includes strolling in to seize the important thing, just for the lights to dim and a secret door with imps inside to open. Effectively, the answer is you simply place two triggers proper subsequent to one another focusing on completely different objects.

There are over 130 of those capabilities within the sport, starting from opening and shutting doorways, elevating or decreasing the ground, locking a door, altering the sunshine ranges, teleporting the participant and extra. Utilizing this you’ll be able to merely arrange an setting, and as soon as the partitions transfer and the enemies can see the participant, or hear a gunshot close by, then we’re good to go! However generally, it results in some much more artistic decisions.

A very good instance of that is within the Navy Base, the key stage within the first episode of DOOM. Wherein the participant can set off a lure by grabbing the rocket launcher sitting atop the pentagram on the bottom. When the participant crosses over the pentagram, it does not change the room the participant is in, however quite, it removes a small wall that has been defending a teleporter in a small room locked off on the facet.


The enemies within the room are already energetic and transferring round however they should wander into the teleporter to ensure that them to look on the pentagram in entrance of the participant. The trick is that there’s a small and largely imperceptible hall of stage that connects the primary room with the pentagram to this tiny room on the facet. That is used in order that any noises that happens in that room will propagate by way of to the hidden room and encourage the monsters to move in direction of the noise, finally strolling into the teleporter. This ‘sound pipe’ is definitely a reasonably frequent tactic and crops up on different events all through the WAD information of each DOOM and DOOM II.

However that is not the solely trick that helps make all of it come collectively. The second trick, is somewhat particular customisation that occurs on a per-demon foundation when the degrees in DOOM are designed. There’s a particular Ambush flag that may be set on a given monster. MF_AMBUSH units a further logic to the beforehand talked about SEE state. As talked about beforehand, if a demon can see the participant or hears the gunshot, then they deal with that location as a goal and transfer in direction of it. MF_AMBUSH modifications this logic in order that even when a demon hears a noise that alerts it, it does not act on this data till it receives visible affirmation. So the demon is awake and energetic, but it surely stays rooted to the spot.

Which means that throughout an encounter, some demons will nonetheless be capable to catch the participant off guard and – as their title implies – create an ambush. It is a easy but extremely efficient trick that mixed with the extent tags and sound pipes, can result in all kinds of attention-grabbing combos!

An excellent instance of that is Containment Space: the second mission of The Shores of Hell. Whereas moving into gunfights typically leads to kiting the odd zombieman or imp towards you, lots of the imps in that stage are utilizing the Ambush flag, which means that even while you kill somebody proper subsequent to them, they nonetheless wait till they obtain a line of sight earlier than they journey to tear your face off.


It is loopy to assume that DOOM is now approaching 30 years outdated. But it surely’s a sport that continues to affect and affect the business in a myriad of the way. And it retains getting ported to just about each system conceivable.

One of many huge causes that DOOM is ready to seem on every little thing from PCs to consoles, cell phones, smartwatches and even ATMs is as a result of the supply code of DOOM has been public data for now for over 20 years. As we have mentioned, the sport is extremely optimised C code, and id Software program put the code on-line in 1997. In case you’re eager to study extra, try the hyperlinks to the supply alongside Fabien Sanglard’s Sport Engine Black Ebook on the topic and the DOOM wiki.