Enemy AI

by Forum_account
Enemy AI
A sequence of examples that start with simple enemy AI and introduce new features one at a time. [More]
To download this demo for your Linux/Mac installation, enter this on your command line:

DreamDownload byond://Forum_account.EnemyAI##version=4

Emulator users, in the BYOND pager go to File | Open Location and enter this URL:


Version 4
Date added: Jun 27 2010
Last updated: Jan 14 2011
26 fans
This isn't exactly a demo. It's a sequence of examples that start with simple enemy AI and show how to add features of increasing complexity.

The package includes 15 examples. Each example shows how to implement a distinct feature useful for enemy AI. The concepts are explained in detail.

The examples cover things like:

• How to manage AI loops efficiently with thousands of mobs. (NEW!)

• How to create mobs that attack only when provoked and mobs that don't.

• How to use a simple state machine to create mobs that have a chance to run away when they are weak.

• How to create mobs that call for help so their allies will join them in combat.

• How to evaluate the utility of a situation and various actions to determine which action yields the largest increase in utility.

• How to couple utility functions with state machines to create mobs that can decide what action to perform and execute a multi-step action.

• How to make mobs run away to good hiding spots when they get weak.

• How to organize your AI code so that it doesn't get out of hand too quickly.

The package also includes ten exercises you can try to test your understanding of the concepts (complete solutions are provided for three of them).

Check out this blog post for instructions on how to run the examples.


Sphinxe1: (Nov 3 2015, 7:48 am)
Thank You Sir!

Was trying to get a cleaner understanding of properly applying loops... and this sure has helped!
Ganite: (Jul 22 2015, 11:40 am)
Thank you this helped bunches
FIREking: (Jan 9 2013, 3:44 am)
If you kill a mob right in front of a hiding space, it is possible that the mob will try to choose this hiding space to hide in. Here's some code that can make it choose the farthest away hiding spot:

if(dead) return

if(state == NORMAL)

if(health < 10)
world << "[name] runs and hides!"

// hiding_spots is a global list of the locations of
// all instances of /obj/hiding_spot.
var/dist_max = 0
for(var/spot in hiding_spots)
var/this_dist = get_dist(src, spot)
if(this_dist > dist_max)
dist_max = this_dist
spot_choose = spot
hiding_spot = spot_choose
state = HIDE
return ai()
Phat T: (Oct 29 2012, 11:41 pm)
hello there. I`m wondering if you can make tutorial how to prevent AI from walking diagonally. And how to make AI use projectile. Ty for youre respond!
Forum_account: (Jul 21 2011, 6:48 am)
Phoestre wrote:
Explains a lot if you are experienced with DM but not AI. However ai proc seems not to be efficient, still a good starting point for AI beginners.

Thanks for the positive feedback!

There is an example in this demo (I forget what number it is) that shows how to improve efficiency by deactivating the AI loops for mobs when a player isn't nearby.