2023-12-14 07:33:49 +00:00
|
|
|
#pragma once
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
#include "RakNetTypes.h"
|
|
|
|
#include "dCommonVars.h"
|
|
|
|
#include "NiPoint3.h"
|
|
|
|
#include "Behavior.h"
|
|
|
|
#include "dpWorld.h"
|
|
|
|
#include "dpEntity.h"
|
|
|
|
#include "Component.h"
|
2023-03-04 07:16:37 +00:00
|
|
|
#include "eReplicaComponentType.h"
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include <map>
|
|
|
|
|
|
|
|
class MovementAIComponent;
|
|
|
|
class Entity;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The current state of the AI
|
|
|
|
*/
|
|
|
|
enum class AiState : int {
|
|
|
|
idle = 0, // Doing nothing
|
|
|
|
aggro, // Waiting for an enemy to cross / running back to spawn
|
|
|
|
tether, // Chasing an enemy
|
|
|
|
spawn, // Spawning into the world
|
|
|
|
dead // Killed
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents a skill that can be cast by this enemy, including its cooldowns, which determines how often the skill
|
|
|
|
* may be cast.
|
|
|
|
*/
|
|
|
|
struct AiSkillEntry
|
|
|
|
{
|
|
|
|
uint32_t skillId;
|
|
|
|
|
|
|
|
float cooldown;
|
|
|
|
|
|
|
|
float abilityCooldown;
|
|
|
|
|
|
|
|
Behavior* behavior;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles the AI of entities, making them wander, tether and attack their enemies
|
|
|
|
*/
|
|
|
|
class BaseCombatAIComponent : public Component {
|
|
|
|
public:
|
2023-10-23 01:08:49 +00:00
|
|
|
inline static const eReplicaComponentType ComponentType = eReplicaComponentType::BASE_COMBAT_AI;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
BaseCombatAIComponent(Entity* parentEntity, uint32_t id);
|
|
|
|
~BaseCombatAIComponent() override;
|
|
|
|
|
|
|
|
void Update(float deltaTime) override;
|
2023-08-10 21:33:15 +00:00
|
|
|
void Serialize(RakNet::BitStream* outBitStream, bool bIsInitialUpdate) override;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the current behavioral state of the enemy
|
|
|
|
* @return the current state
|
|
|
|
*/
|
|
|
|
AiState GetState() const { return m_State; }
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
/**
|
|
|
|
* Set the current behavioral state of the enemy
|
|
|
|
* @param state the state to change to
|
|
|
|
*/
|
2021-12-05 17:54:36 +00:00
|
|
|
void SetState(AiState state) { m_State = state; }
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
/**
|
|
|
|
* Checks if the target may be an enemy of this entity
|
|
|
|
* @param target the target to check for
|
|
|
|
* @return whether the target is a valid enemy for this entity or not
|
|
|
|
*/
|
2021-12-05 17:54:36 +00:00
|
|
|
bool IsEnemy(LWOOBJID target) const;
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
/**
|
|
|
|
* Gets the current target ID that this entity will attack
|
|
|
|
* @return the current target ID of this entity
|
|
|
|
*/
|
2021-12-05 17:54:36 +00:00
|
|
|
LWOOBJID GetTarget() const { return m_Target; }
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
/**
|
|
|
|
* Sets the target that this entity will attack
|
|
|
|
* @param target the target to set
|
|
|
|
*/
|
2021-12-05 17:54:36 +00:00
|
|
|
void SetTarget(LWOOBJID target);
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
/**
|
|
|
|
* Gets the current target entity that this entity will attack
|
|
|
|
* @return the current target entity of this entity
|
|
|
|
*/
|
2021-12-05 17:54:36 +00:00
|
|
|
Entity* GetTargetEntity() const;
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
/**
|
|
|
|
* Taunts this entity, making it a higher or lower threat for this entity. Increasing or decreasing the chance to
|
|
|
|
* be attacked.
|
|
|
|
* @param offender the entity that triggered the taunt
|
|
|
|
* @param threat how high to increase the threat for the offender
|
|
|
|
*/
|
2021-12-05 17:54:36 +00:00
|
|
|
void Taunt(LWOOBJID offender, float threat);
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
/**
|
|
|
|
* Gets the current threat level for an offending entity
|
|
|
|
* @param offender the entity to get the threat for
|
|
|
|
* @return the current threat level of the offending entity, 0 if the entity is not a threat
|
|
|
|
*/
|
2021-12-05 17:54:36 +00:00
|
|
|
float GetThreat(LWOOBJID offender);
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
/**
|
|
|
|
* Sets the threat level for an entity
|
|
|
|
* @param offender the entity to set the threat level for
|
|
|
|
* @param threat the threat level to set
|
|
|
|
*/
|
2021-12-05 17:54:36 +00:00
|
|
|
void SetThreat(LWOOBJID offender, float threat);
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
/**
|
|
|
|
* Gets the position where the entity spawned
|
|
|
|
* @return the position where the entity spawned
|
|
|
|
*/
|
2021-12-05 17:54:36 +00:00
|
|
|
const NiPoint3& GetStartPosition() const;
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
/**
|
|
|
|
* Removes all threats for this entities, and thus chances for it attacking other entities
|
|
|
|
*/
|
2021-12-05 17:54:36 +00:00
|
|
|
void ClearThreat();
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
/**
|
|
|
|
* Makes the entity continue to wander to a random point around it's starting position
|
|
|
|
*/
|
2021-12-05 17:54:36 +00:00
|
|
|
void Wander();
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
/**
|
|
|
|
* Continues a step in the aggro state, making sure that the entity is around its start position, if an entity
|
|
|
|
* crosses its aggro range this will set the state to tether.
|
|
|
|
*/
|
2021-12-05 17:54:36 +00:00
|
|
|
void OnAggro();
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
/**
|
|
|
|
* Continues a step in the tether state, making the entity run towards its target, if the target is outside of its
|
|
|
|
* tether range, this will change the state to aggro
|
|
|
|
*/
|
2021-12-05 17:54:36 +00:00
|
|
|
void OnTether();
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
/**
|
|
|
|
* Gets whether or not the entity is currently stunned
|
|
|
|
* @return whether the entity is currently stunned
|
|
|
|
*/
|
|
|
|
bool GetStunned() const;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
/**
|
|
|
|
* (un)stuns the entity, determining whether it'll be able to attack other entities
|
|
|
|
* @param value whether the enemy is stunned
|
|
|
|
*/
|
2021-12-05 17:54:36 +00:00
|
|
|
void SetStunned(bool value);
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
/**
|
|
|
|
* Gets if this entity may be stunned
|
|
|
|
* @return if this entity may be stunned
|
|
|
|
*/
|
2021-12-05 17:54:36 +00:00
|
|
|
bool GetStunImmune() const;
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
/**
|
|
|
|
* Set the stun immune value, determining if the entity may be stunned
|
|
|
|
* @param value
|
|
|
|
*/
|
2021-12-05 17:54:36 +00:00
|
|
|
void SetStunImmune(bool value);
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
/**
|
|
|
|
* Gets the current speed at which an entity runs when tethering
|
|
|
|
* @return the current speed at which an entity runs when tethering
|
|
|
|
*/
|
2021-12-05 17:54:36 +00:00
|
|
|
float GetTetherSpeed() const;
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
/**
|
|
|
|
* Sets the speed at which an entity will tether
|
|
|
|
* @param value the new tether speed
|
|
|
|
*/
|
2021-12-05 17:54:36 +00:00
|
|
|
void SetTetherSpeed(float value);
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
/**
|
|
|
|
* Stuns the entity for a certain amount of time, will not work if the entity is stun immune
|
|
|
|
* @param time the time to stun the entity, if stunnable
|
|
|
|
*/
|
2021-12-05 17:54:36 +00:00
|
|
|
void Stun(float time);
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
/**
|
|
|
|
* Gets the radius that will cause this entity to get aggro'd, causing a target chase
|
|
|
|
* @return the aggro radius of the entity
|
|
|
|
*/
|
2021-12-05 17:54:36 +00:00
|
|
|
float GetAggroRadius() const;
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
/**
|
|
|
|
* Sets the aggro radius, causing the entity to start chasing enemies in this range
|
|
|
|
* @param value the aggro radius to set
|
|
|
|
*/
|
2021-12-05 17:54:36 +00:00
|
|
|
void SetAggroRadius(float value);
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
/**
|
|
|
|
* Makes the entity look at a certain point in space
|
|
|
|
* @param point the point to look at
|
|
|
|
*/
|
2021-12-05 17:54:36 +00:00
|
|
|
void LookAt(const NiPoint3& point);
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
/**
|
|
|
|
* (dis)ables the AI, causing it to stop/start attacking enemies
|
|
|
|
* @param value
|
|
|
|
*/
|
2021-12-05 17:54:36 +00:00
|
|
|
void SetDisabled(bool value);
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
/**
|
|
|
|
* Gets the current state of the AI, whether or not it's looking for enemies to attack
|
|
|
|
* @return
|
|
|
|
*/
|
2021-12-05 17:54:36 +00:00
|
|
|
bool GetDistabled() const;
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
/**
|
|
|
|
* Turns the entity asleep, stopping updates to its physics volumes
|
|
|
|
*/
|
2021-12-05 17:54:36 +00:00
|
|
|
void Sleep();
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
/**
|
|
|
|
* Wakes the entity, allowing updates to its physics volumes
|
|
|
|
*/
|
2021-12-05 17:54:36 +00:00
|
|
|
void Wake();
|
|
|
|
|
|
|
|
private:
|
2022-07-28 13:39:57 +00:00
|
|
|
/**
|
|
|
|
* Returns the current target or the target that currently is the largest threat to this entity
|
|
|
|
* @return the current highest priority enemy of this entity
|
|
|
|
*/
|
2021-12-05 17:54:36 +00:00
|
|
|
LWOOBJID FindTarget();
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
/**
|
|
|
|
* Handles anything attack related for the game loop, e.g.: finding targets, sticking with targets and attacking
|
|
|
|
* them, depending on cooldowns.
|
|
|
|
* @param deltaTime the time since the last game tick
|
|
|
|
*/
|
2021-12-05 17:54:36 +00:00
|
|
|
void CalculateCombat(float deltaTime);
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
/**
|
|
|
|
* Gets all the targets that are in the aggro collision phantom of this entity
|
|
|
|
* @return the targets within the aggro range of this entity
|
|
|
|
*/
|
2021-12-05 17:54:36 +00:00
|
|
|
std::vector<LWOOBJID> GetTargetWithinAggroRange() const;
|
|
|
|
|
2023-01-03 17:22:04 +00:00
|
|
|
/**
|
|
|
|
* @brief Sets the AiState and prepares the entity for serialization next frame.
|
2023-03-04 07:16:37 +00:00
|
|
|
*
|
2023-01-03 17:22:04 +00:00
|
|
|
*/
|
|
|
|
void SetAiState(AiState newState);
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
/**
|
|
|
|
* The current state of the AI
|
|
|
|
*/
|
2021-12-05 17:54:36 +00:00
|
|
|
AiState m_State;
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
/**
|
|
|
|
* The target this entity is currently trying to attack
|
|
|
|
*/
|
2021-12-05 17:54:36 +00:00
|
|
|
LWOOBJID m_Target;
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
/**
|
|
|
|
* The aggro physics volumes of this entity
|
|
|
|
*/
|
2021-12-05 17:54:36 +00:00
|
|
|
dpEntity* m_dpEntity;
|
|
|
|
dpEntity* m_dpEntityEnemy;
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
/**
|
|
|
|
* The max radius of this entity to an enemy allowing it to be chased
|
|
|
|
*/
|
2021-12-05 17:54:36 +00:00
|
|
|
float m_HardTetherRadius = 100;
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
/**
|
|
|
|
* A soft radius for the tether, currently unused
|
|
|
|
*/
|
2021-12-05 17:54:36 +00:00
|
|
|
float m_SoftTetherRadius = 25;
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
/**
|
|
|
|
* The speed at which this entity chases enemies
|
|
|
|
*/
|
2021-12-05 17:54:36 +00:00
|
|
|
float m_PursuitSpeed = 2;
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
/**
|
|
|
|
* The radius that can cause enemies to aggro this entity
|
|
|
|
*/
|
2021-12-05 17:54:36 +00:00
|
|
|
float m_AggroRadius = 25;
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
/**
|
|
|
|
* The speed at which an enemy wanders around
|
|
|
|
*/
|
2021-12-05 17:54:36 +00:00
|
|
|
float m_TetherSpeed = 4;
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
/**
|
|
|
|
* How close this entity needs to be to an enemy to allow attacks
|
|
|
|
*/
|
2021-12-05 17:54:36 +00:00
|
|
|
float m_AttackRadius = 5.0f;
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
/**
|
|
|
|
* Timer before we start attacking others
|
|
|
|
*/
|
2021-12-05 17:54:36 +00:00
|
|
|
float m_Timer = 0.0f;
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
/**
|
|
|
|
* Timer to serializing this entity
|
|
|
|
*/
|
2021-12-05 17:54:36 +00:00
|
|
|
float m_SoftTimer = 0.0f;
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
/**
|
|
|
|
* The skills this entity can cast on enemies
|
|
|
|
*/
|
2021-12-05 17:54:36 +00:00
|
|
|
std::vector<AiSkillEntry> m_SkillEntries;
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
/**
|
|
|
|
* The current enemies and their respective threats to this entity
|
|
|
|
*/
|
2021-12-05 17:54:36 +00:00
|
|
|
std::map<LWOOBJID, float> m_ThreatEntries;
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
/**
|
|
|
|
* The component that handles movement AI, also owned by this entity
|
|
|
|
*/
|
2021-12-05 17:54:36 +00:00
|
|
|
MovementAIComponent* m_MovementAI;
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
/**
|
|
|
|
* The position at which this entity spawned
|
|
|
|
*/
|
2021-12-05 17:54:36 +00:00
|
|
|
NiPoint3 m_StartPosition;
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
/**
|
|
|
|
* For how long this entity has been stunned
|
|
|
|
*/
|
2021-12-05 17:54:36 +00:00
|
|
|
float m_StunTime = 0;
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
/**
|
|
|
|
* If this entity is stunned
|
|
|
|
*/
|
2021-12-05 17:54:36 +00:00
|
|
|
bool m_Stunned = false;
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
/**
|
|
|
|
* If this entity is immune to stunds
|
|
|
|
*/
|
2021-12-05 17:54:36 +00:00
|
|
|
bool m_StunImmune = false;
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
/**
|
|
|
|
* How long this entity needs to execute its skill
|
|
|
|
*/
|
2021-12-05 17:54:36 +00:00
|
|
|
float m_SkillTime = 0;
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
/**
|
|
|
|
* If the entity is currently showing the exclamation mark icon above its head
|
|
|
|
*/
|
2021-12-05 17:54:36 +00:00
|
|
|
bool m_TetherEffectActive = false;
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
/**
|
|
|
|
* How long the tether effect will remain active
|
|
|
|
*/
|
2021-12-05 17:54:36 +00:00
|
|
|
float m_TetherTime = 0;
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
/**
|
|
|
|
* How long until we will consider this entity out of combat, resetting its health and armor
|
|
|
|
*/
|
2021-12-05 17:54:36 +00:00
|
|
|
float m_OutOfCombatTime = 0;
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
/**
|
|
|
|
* If the entity is currently out of combat, resetting its health and armor if it just came out of combat
|
|
|
|
*/
|
2021-12-05 17:54:36 +00:00
|
|
|
bool m_OutOfCombat = false;
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
/**
|
|
|
|
* If the AI is currently disabled
|
|
|
|
*/
|
2021-12-05 17:54:36 +00:00
|
|
|
bool m_Disabled = false;
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
/**
|
|
|
|
* If the threat list should be updated
|
|
|
|
*/
|
2021-12-05 17:54:36 +00:00
|
|
|
bool m_DirtyThreat = false;
|
|
|
|
|
2023-01-03 17:22:04 +00:00
|
|
|
/**
|
|
|
|
* Whether or not the Component has dirty information and should update next frame
|
2023-03-04 07:16:37 +00:00
|
|
|
*
|
2023-01-03 17:22:04 +00:00
|
|
|
*/
|
|
|
|
bool m_DirtyStateOrTarget = false;
|
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
/**
|
|
|
|
* Whether the current entity is a mech enemy, needed as mechs tether radius works differently
|
|
|
|
* @return whether this entity is a mech
|
|
|
|
*/
|
|
|
|
bool IsMech();
|
|
|
|
};
|