Gambits - Behavioural Decision Patterns For A Game Development AI
Tags: gametheory, gamedev, ai
Published: 2021-03-15 13:58:38

Starting today, I'm kicking off my new series of articles about game development topics. These will mostly be theoretical and talk about concepts and techniques but will at times also include ideas and solutions, depending on the topic at hands such as different ways to go about pathfinding, grids and perspectives in games.

Our first topic will be **Gambits**.
## What Is A Gambit?
> A gambit (from ancient Italian gambetto, meaning "to trip") is a chess opening in which a player sacrifices material with the aim of achieving a subsequent positional advantage.
> [Source: Wikipedia](https://en.wikipedia.org/wiki/Gambit )

<figure>
  <img src="https://i.imgur.com/eHQ7OQR.jpg" alt="Photo by GR Stocks on Unsplash"/>
  <figcaption>Photo by GR Stocks on Unsplash</figcaption>
</figure>

Historically, gambits are risky moves in a strategical play. The term is nowadays used in many contexts, including strategical situations outside of games, even in politics.

But why are we talking about gambit today? Thanks to one game in particular, gambits have become a popular term in
game development circles as well. 

## How Are Gambits Used In Game Design?
When Square Enix released Final Fantasy XII for the PlayStation 2 in 2006, reception of one of the design choices created a lot of talk. To enable the player to play with multiple characters in an MMO style open-world RPG with (pausable) real-time combat, the developers added the gambit system. Using said gambits, the player could set several commands and triggers and the game would check those for a match and execute the respective action.

<figure>
  <img src="https://i.imgur.com/TcBAVAo.jpg" alt="Overview of a magic focused unit's gambits (Final Fantasy XII)"/>
  <figcaption>Overview of a magic focused unit's gambits (Final Fantasy XII)</figcaption>
</figure>

While part of the player base disliked the subjective loss of direct control, the system enabled players to create their own mini AI with simple and easy to understand building blocks.

Subsequentially, gambit systems have been used and talked about in the context of RPGs and there are some other notable examples of games allowing the player to control the AI of his own heroes. 

## Alternatives to Gambits
A common alternative to this gambit system and Something around for a lot longer would be decision modelling trees. These allow a more detailed, flexible and expandable control about the AI's decision processes but at the cost of the player being able to add his own rules with simple tools.

Of course, there are also real AI solutions that implement real learning algorithms and mimic human decision models even more closely but that's a different topic altogether. 

## Pro And Con Of Using Gambits
As mentioned above, the main Con or constraint of a system alike the gambits of Final Fantasy XII is its limiting nature. It is hard to implement command chains (do A, then B and then follow up with C) and you also need to limit the number of rules or gambits. On top of that, you need to teach your players how to use that system.

On the Pro side, we have the player interaction, which can create a positive effect of ownership because the player is the creator of his own rulesets and it can turn into an interesting metagame to find the right solution for a special situation or set of constraints and keeps the player engaged into solving new challenges.

For me personally, the fact that it is a very simple style of programming available to people with no experience in the field is another great boon here. 

<figure>
  <img src="https://i.imgur.com/0a3Kl8I.png" alt="Decisions need to be made by hand... (Final Fantasy Tactics)"/>
  <figcaption>Decisions need to be made by hand... (Final Fantasy Tactics)</figcaption>
</figure>

## Anatomy Of A Gambit
### The Target
Any action needs a target. The target can be yourself, an ally, a foe or maybe even the target of one of your allies.

Depending on your game design, this could also include differentiations such as mercenaries, neutral bystanders, certain objects or even target areas.

### The Condition
Depending on your chosen target, you can add a condition for each gambit. Say, for example, you want to use a healing spell on yourself but only when your own hit points are below 50%. In that case, you would pick target self (or any ally if you want to include your whole team/party)

### Examples
* 
  * **Use**: Attack
  * **On**: closest enemy
* 
  * **Use**: healing spell
  * **On**: ally (hp<=50%)
* 
  * **Use**: potion
  * **On**: self (hp<=25%)
* 
  * **Use**: fire spell
  * **On**: enemy status=weak to fire 

<figure>
  <img src="https://i.imgur.com/n7X2Kit.png" alt="Choosing the right items to use in a gambit (Final Fantasy XII)"/>
  <figcaption>Choosing the right items to use in a gambit (Final Fantasy XII)</figcaption>
</figure>

### Bonus: Trigger Conditions
I'm currently working on my own implementation of a gambit-like system and I've added an extra component, trigger conditions.

Basically, this is nothing else than a second unit/character/enemy to check for a condition to activate the gambit. Say for example you want to use a certain skill only when your mana/energy is above a certain level but target it on an enemy. In that case, you would do the following.

* **Use**: expensive spell
* **On**: target hp>=3000
* **When**: self mp=>75%

Or you want to use a defensive ability when the enemy is enraged

* **Use**: defensive stance
* **On**: self
* **When**: enemy status=enraged

## Gambits In The Wild
The basic idea of gambits for player-controlled units is easy to implement but in a game using gambits, you can take it a step further and use it to model enemy Ai the same way, maybe with a few options not available to the player, to keep it interesting.

You can also use it in more complex games if you implement logic to handle pathfinding, the cost of certain terrains and other stuff like more complex actions. Go wild and be creative. If it fits your game concept, there are lots of interesting combinations you can achieve with them.

## Gambits In My Own Projects
I will soon publish another article on my prototype using gambits. It won't be a full and playable game but an interactive game demo showing how the decision modelling with gambits works and allowing the "player" to experiment with different combinations in different scenarios.

So stay tuned and be sure to follow me and future articles. The prototype should be ready in a week or two.