Skip to main content

AI Theory

I've been working on the AI for the game the last couple of days.

[Don't worry, every thing is OK...]
In my smaller games AI was pretty simple. They had a simple game mechanic, try to walk in to the player and kill them. So basically they have a single type of behavior.

[I'll eat your brains!]
That's fine for simple enemies like zombies, but what about more complex characters?
Well, I've written a couple of different behaviors, we could call them:
  1. roaming
  2. seeking
  3. fleeing
  4. fighting
  5. following
  6. waiting
[while: guarding]

The behaviors are encompassed in a finite state machine. Each state can transition in to others, depending on the behavior. So a waiting agent sees an enemy they become alerted and try to attack, they transition to the seeking state.

After testing I'm confident I've got the bugs out of each of them.
But there's a problem. How to choose which behavior to use at any one time? How to know which state to enter next?

[trolls ahoy!]

If an agent sees the enemy, obviously they will do something different depending on whether they are an archer, a wizard, a dragon or a troll or whatever. When they lose sight of the enemy, which state should they return to? Clearly no single set of 6 states can fit all those scenarios.

Right away I'm back to the previous situation of wanting a state machine to manage my state machine. But again, I don't want to go down that road. Once I start looking for the complex solution, I'll get bogged down and never finish.

One simple possibility would be to have character attributes like self.default_state or self.alerted state, but then behavior becomes split between the states and the character so adding new characters means partly writing new behavior. I want all behavior to be managed from within the state machine. I don't want behavior entangled with specific agents types, but rather modular and easy to assign even during game play.

So I'm going to use inheritance to solve the problem.

[Do I need to draw you a picture?]
Firstly, some of the above states can be merged. Seeking, Fleeing and Roaming are all the same behavior, only one finds the closest tile to the target, one finds the tile furthest away from the target, and one chooses a random unvisited tile. So I now have a single state, Navigation.
[Navigation; You are here!]
Then I go back and create new states like Roaming(Navigation).
This inherits all the behavior from navigation but uses a different rule for deciding the next square. Fleeing(Navigation) and Seeking(Navigation) do likewise. *



The next step is to create specific versions of those states for a particular AI type. For example ArcherRoaming(Navigation) with some tweaks to the __init__ function to tailor how it transitions to other states. There's also a special custom exit check, to see if there's some special reason to not be roaming around. This reduces it down to just a few lines of code, rather than re-writing a specific behavior for archers.

After that I have to plan how those states will interact and which states are needed for each AI archetype. I'm using flowcharts for that:
[Inside the mind of a dungeon guard]

A little more complex now than:
try to walk in to the player and kill them.
Of course It's not difficult to go further, if I have archers who patrol instead of guarding I can reuse most of the states from archer, but subclass them again as ArcherRoaming instead of ArcherWaiting.

Where this gets really useful is being able to have switchable behaviors. By having a custom exit check we can give some AI archetypes the ability to switch to a different archetype. If we have a party of heroes and I want to give them orders I can do so through dialog choices or through hotkeys. It just asks them to switch to a different behavior archetype.


Become an archer, cast magic, follow me!
Scout ahead, serve drinks, sacrifice yourself for the good of the party!



* At this point I also diverged in to two different kinds of navigation, single tile navigation for close in to the target avoiding obstacles and tile chunk navigation which gives smoother movement (because it uses a shorter route to the target) but is worse at avoiding smaller obstacles. Then I get Attacking(TileNavigation)and Hunting(ChunkNavigation) two different behaviors for use when at different ranges from the target. However I don't want to add to the confusion here.

Comments

Popular posts from this blog

Upstairs / Downstairs.

I've decided to make my prefabs multilevel. Later this should allow me to add pit traps and other great stuff. It also makes it easier to line up stairs so that you can exit them on the same co-ordinates where you entered them. The prefab editor is pretty much finished, it just needs some code for loading up prefabs from a saved dictionary, so that they can be checked or edited. The entries will need to be forwards compatible, so I'll be loading each tile and then translating the indexes to a new array, that way if I add extra indexes or extra info (like traps or puzzles) I'll be able to update existing prefabs to work with the new standard. Click for a video.

Automating Level imports from Blender to Godot

  Recently I've been making some levels in Blender an importing them into Godot. There are only about 7 or 8 shaders for each level, not counting dynamic objects which will be added later. But to improve rendering performance, it can be a good idea to split the meshes up into sections. At that point you might be faced with a list like this: Or it might be even more chaotic, if you didn't use simple names for the objects in your level. So it can take a long time to sort out all the meshes, make them unique and add textures and so on. Blender imports with simple Blender textures, or with placeholder materials. This is sometimes OK, but if your Godot shaders are very different to those used by Blender, it means applying new materials to every mesh object in the level when you import the scene. I found that during the design process, I was importing and readying a level several times before I was happy with the final layout. So at first I was wasting a lot of time. In Blender, I us

Advice needed on tilesets...

I need some advice on which is the best way to handle building the dungeon. Right now I'm using prefabs for my dungeon, they have a north south east and west section for each "room": The basic tileset. This has several advantages, and also several disadvantages. Firstly I can have curved rooms, I can have tunnels and other interesting shapes. The tilesets can look quite nice with a little work. On the other hand I can't easily get the navigation data before building the map and once the map has been built I can't make changes to the layout, like having active pit traps or believable secret doors. Although the rooms are interesting, they are quite repetitive, and it takes a lot of effort to make even a few different variations. Also rooms are constrained to one size. A newer version of the tileset with a lot of variant parts for making more interesting rooms. To create a tile set is a real headache too. Planning how to lay out the UVs, trying to cra