# Combat AI

This describes how the combat AI controller works.

This is sub-layout for documentation pages

Top

#### 1 Introduction

Unfortunatelly, this turned out to be unexpectedly complicated to implement and make it work.

#### 2 Definitions

Melee attack can be carried out, if 2 characters are next to each other
Ranged attack can be carried out, if 2 characters are in distance smaller or equal to some constant $d$
Character A following character B is an infinite (until stopped) set of events, described by following diagram:

#### 3 Objective

We want following behavior

• Character $A$ can attack character $B$ using melee or ranged attack
• If character is not in melee distance, then he will follow character B
• While $A$ is moving, he can still attack $B$ if rules allow that
• This chase and attack continues until stopped by another user input, or until stopped by game system

#### 4 The solution

In order to make this behavior possible, 2 main classes are used: CharacterController and CombatController.
CharacterController updates the CombatController instance, which repetitivelly tries to attack target (if any is assigned), until stopped. In the same time, however, CharacterController is updated and moves the character around (ideally chases the target).
So CombatController is most of the time unable to attack, because of incorrect distance, but this solution supports the wanted behavior.

##### 4.1 CharacterCommandAttack

Represents command which is inserted to the waiting queue. When this command is unpacked, it initiates CharacterCommandFollow inside, and sets the combat target in CombatController (wo whhich has reference).
It takes care of 2 cases: melee attack, and ranged attack

##### 4.1.1 Unpacking

When CharacterCommandAttack is unpacked, it checks for current Character's combat setup, and extracts the attack style (according to wielded weapon / spell, etc.).
According to the attack style (MELEE / RANGED), the CharacterCommandAttack will have different behavior, explained below...

##### 4.1.2 Melee

This is the easier case.
The only thing needed is to chase target, until stopped from outside, or until target is no longer valid.

##### 4.1.3 Ranged

The hard case to take care of.
In onUpdate, a check id done, whether target is within line of sight. If so, the CharacterCommandFollow is aborted.
In case when target is not within line of sight, then CharacterCommandFollow is updated and target is followed. In case when follow class instance is aborted or not existent, new one is created

The things that happen in one onUpdate call can be seen in following activity diagram: