|
|
|
|
@@ -9,6 +9,7 @@
|
|
|
|
|
#include "dpWorld.h"
|
|
|
|
|
#include "EntityManager.h"
|
|
|
|
|
#include "SimplePhysicsComponent.h"
|
|
|
|
|
#include "dZoneManager.h"
|
|
|
|
|
#include "CDClientManager.h"
|
|
|
|
|
|
|
|
|
|
std::map<LOT, float> MovementAIComponent::m_PhysicsSpeedCache = {};
|
|
|
|
|
@@ -16,9 +17,9 @@ std::map<LOT, float> MovementAIComponent::m_PhysicsSpeedCache = {};
|
|
|
|
|
MovementAIComponent::MovementAIComponent(Entity* parent, MovementAIInfo info) : Component(parent) {
|
|
|
|
|
m_Info = std::move(info);
|
|
|
|
|
m_Done = true;
|
|
|
|
|
m_IsPaused = false;
|
|
|
|
|
|
|
|
|
|
m_BaseCombatAI = nullptr;
|
|
|
|
|
|
|
|
|
|
m_BaseCombatAI = reinterpret_cast<BaseCombatAIComponent*>(m_Parent->GetComponent(COMPONENT_TYPE_BASE_COMBAT_AI));
|
|
|
|
|
|
|
|
|
|
//Try and fix the insane values:
|
|
|
|
|
@@ -36,13 +37,21 @@ MovementAIComponent::MovementAIComponent(Entity* parent, MovementAIInfo info) :
|
|
|
|
|
m_Timer = 0;
|
|
|
|
|
m_CurrentSpeed = 0;
|
|
|
|
|
m_Speed = 0;
|
|
|
|
|
|
|
|
|
|
m_TotalTime = 0;
|
|
|
|
|
m_LockRotation = false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
m_MovementPath = nullptr;
|
|
|
|
|
m_isReverse = false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MovementAIComponent::~MovementAIComponent() = default;
|
|
|
|
|
|
|
|
|
|
void MovementAIComponent::Update(const float deltaTime) {
|
|
|
|
|
if (m_IsPaused) return;
|
|
|
|
|
if (m_Interrupted) {
|
|
|
|
|
const auto source = GetCurrentWaypoint();
|
|
|
|
|
|
|
|
|
|
@@ -63,71 +72,64 @@ void MovementAIComponent::Update(const float deltaTime) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (AtFinalWaypoint()) // Are we done?
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (AtFinalWaypoint()) return; // Are we done?
|
|
|
|
|
|
|
|
|
|
if (m_HaltDistance > 0) {
|
|
|
|
|
if (Vector3::DistanceSquared(ApproximateLocation(), GetDestination()) < m_HaltDistance * m_HaltDistance) // Prevent us from hugging the target
|
|
|
|
|
{
|
|
|
|
|
if (Vector3::DistanceSquared(ApproximateLocation(), GetDestination()) < m_HaltDistance * m_HaltDistance) { // Prevent us from hugging the target
|
|
|
|
|
Stop();
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Game::logger->Log("MovementAIComponent", "timer %f", m_Timer);
|
|
|
|
|
if (m_Timer > 0) {
|
|
|
|
|
m_Timer -= deltaTime;
|
|
|
|
|
|
|
|
|
|
if (m_Timer > 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (m_Timer > 0) return;
|
|
|
|
|
m_Timer = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const auto source = GetCurrentWaypoint();
|
|
|
|
|
// We've arrived at a waypoint, do the things if we need to
|
|
|
|
|
|
|
|
|
|
SetPosition(source);
|
|
|
|
|
|
|
|
|
|
NiPoint3 velocity = NiPoint3::ZERO;
|
|
|
|
|
|
|
|
|
|
if (AdvanceWaypointIndex()) // Do we have another waypoint to seek?
|
|
|
|
|
{
|
|
|
|
|
if (AdvanceWaypointIndex()) { // Do we have another waypoint to seek?
|
|
|
|
|
m_NextWaypoint = GetCurrentWaypoint();
|
|
|
|
|
|
|
|
|
|
if (m_NextWaypoint == source) {
|
|
|
|
|
m_Timer = 0;
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
goto nextAction;
|
|
|
|
|
if (m_CurrentSpeed < m_Speed) {
|
|
|
|
|
m_CurrentSpeed += m_Acceleration;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (m_CurrentSpeed > m_Speed) {
|
|
|
|
|
m_CurrentSpeed = m_Speed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const auto speed = m_BaseSpeed;
|
|
|
|
|
|
|
|
|
|
const auto delta = m_NextWaypoint - source;
|
|
|
|
|
|
|
|
|
|
// Normalize the vector
|
|
|
|
|
const auto length = sqrtf(delta.x * delta.x + delta.y * delta.y + delta.z * delta.z);
|
|
|
|
|
|
|
|
|
|
if (length > 0) {
|
|
|
|
|
velocity.x = (delta.x / length) * speed;
|
|
|
|
|
velocity.y = (delta.y / length) * speed;
|
|
|
|
|
velocity.z = (delta.z / length) * speed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Calclute the time it will take to reach the next waypoint with the current speed
|
|
|
|
|
Game::logger->Log("MovementAIComponent", "length %f speed %f", length, speed);
|
|
|
|
|
m_TotalTime = m_Timer = length / speed;
|
|
|
|
|
|
|
|
|
|
SetRotation(NiQuaternion::LookAt(source, m_NextWaypoint));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (m_CurrentSpeed < m_Speed) {
|
|
|
|
|
m_CurrentSpeed += m_Acceleration;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (m_CurrentSpeed > m_Speed) {
|
|
|
|
|
m_CurrentSpeed = m_Speed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const auto speed = m_CurrentSpeed * m_BaseSpeed;
|
|
|
|
|
|
|
|
|
|
const auto delta = m_NextWaypoint - source;
|
|
|
|
|
|
|
|
|
|
// Normalize the vector
|
|
|
|
|
const auto length = sqrtf(delta.x * delta.x + delta.y * delta.y + delta.z * delta.z);
|
|
|
|
|
|
|
|
|
|
if (length > 0) {
|
|
|
|
|
velocity.x = (delta.x / length) * speed;
|
|
|
|
|
velocity.y = (delta.y / length) * speed;
|
|
|
|
|
velocity.z = (delta.z / length) * speed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Calclute the time it will take to reach the next waypoint with the current speed
|
|
|
|
|
m_TotalTime = m_Timer = length / speed;
|
|
|
|
|
|
|
|
|
|
SetRotation(NiQuaternion::LookAt(source, m_NextWaypoint));
|
|
|
|
|
} else {
|
|
|
|
|
// Check if there are more waypoints in the queue, if so set our next destination to the next waypoint
|
|
|
|
|
if (!m_Queue.empty()) {
|
|
|
|
|
@@ -142,8 +144,6 @@ void MovementAIComponent::Update(const float deltaTime) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nextAction:
|
|
|
|
|
|
|
|
|
|
SetVelocity(velocity);
|
|
|
|
|
|
|
|
|
|
EntityManager::Instance()->SerializeEntity(m_Parent);
|
|
|
|
|
@@ -154,21 +154,106 @@ const MovementAIInfo& MovementAIComponent::GetInfo() const {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool MovementAIComponent::AdvanceWaypointIndex() {
|
|
|
|
|
Game::logger->Log("MovementAIComponent", "reached waypoint check");
|
|
|
|
|
|
|
|
|
|
if (m_PathIndex >= m_CurrentPath.size()) {
|
|
|
|
|
if (m_MovementPath){
|
|
|
|
|
if (m_MovementPath->pathBehavior == PathBehavior::Loop){
|
|
|
|
|
m_PathIndex = 0;
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
if (m_MovementPath->pathBehavior == PathBehavior::Bounce){
|
|
|
|
|
m_isReverse = true;
|
|
|
|
|
m_PathIndex--;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
} else if (m_PathIndex <= 0) {
|
|
|
|
|
m_PathIndex = 0;
|
|
|
|
|
m_isReverse = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_PathIndex++;
|
|
|
|
|
if (m_isReverse) m_PathIndex--;
|
|
|
|
|
else m_PathIndex++;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NiPoint3 MovementAIComponent::GetCurrentWaypoint() const {
|
|
|
|
|
Game::logger->Log("MovementAIComponent", "get current waypoint");
|
|
|
|
|
if (m_PathIndex >= m_CurrentPath.size()) {
|
|
|
|
|
return GetCurrentPosition();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return m_CurrentPath[m_PathIndex];
|
|
|
|
|
auto source = GetCurrentPosition();
|
|
|
|
|
auto destination = m_CurrentPath.at(m_PathIndex);
|
|
|
|
|
if (dpWorld::Instance().IsLoaded()) {
|
|
|
|
|
destination.y = dpWorld::Instance().GetNavMesh()->GetHeightAtPoint(destination);
|
|
|
|
|
}
|
|
|
|
|
if (abs(destination.y - source.y) > 3) destination.y = source.y;
|
|
|
|
|
|
|
|
|
|
return destination;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MovementAIComponent::ArrivedAtPathWaypoint(){
|
|
|
|
|
if(!m_MovementPath) return;
|
|
|
|
|
if (m_PathIndex >= m_CurrentPath.size()) return;
|
|
|
|
|
// TODO: Call scripts here
|
|
|
|
|
|
|
|
|
|
PathWaypoint waypoint = m_MovementPath->pathWaypoints.at(m_PathIndex);
|
|
|
|
|
|
|
|
|
|
if (waypoint.config.size() > 0) {
|
|
|
|
|
|
|
|
|
|
for (LDFBaseData* action : waypoint.config) {
|
|
|
|
|
if (action) {
|
|
|
|
|
|
|
|
|
|
// delay: has time as float
|
|
|
|
|
if (action->GetKey() == u"delay"){
|
|
|
|
|
m_Timer += std::stof(action->GetValueAsString());
|
|
|
|
|
SetVelocity(NiPoint3::ZERO);
|
|
|
|
|
EntityManager::Instance()->SerializeEntity(m_Parent);
|
|
|
|
|
|
|
|
|
|
// emote: has name of animation to play
|
|
|
|
|
} else if (action->GetKey() == u"emote"){
|
|
|
|
|
GameMessages::SendPlayAnimation(m_Parent, GeneralUtils::UTF8ToUTF16(action->GetValueAsString()));
|
|
|
|
|
// TODO Get proper animation time and add to wait
|
|
|
|
|
m_Timer += 1;
|
|
|
|
|
SetVelocity(NiPoint3::ZERO);
|
|
|
|
|
EntityManager::Instance()->SerializeEntity(m_Parent);
|
|
|
|
|
|
|
|
|
|
// pathspeed: has pathing speed as a float
|
|
|
|
|
} else if (action->GetKey() == u"pathspeed") {
|
|
|
|
|
m_BaseSpeed = std::stof(action->GetValueAsString());
|
|
|
|
|
|
|
|
|
|
// changeWP: <path to change to>,<waypoint to use> the command and waypoint are optional
|
|
|
|
|
} else if (action->GetKey() == u"changeWP") {
|
|
|
|
|
// use an intermediate value since it can be one or two things
|
|
|
|
|
auto intermed = action->GetValueAsString();
|
|
|
|
|
std::string path_string = "";
|
|
|
|
|
|
|
|
|
|
// sometimes there's a path and what waypoint to start, which are comma separated
|
|
|
|
|
if (intermed.find(",") != std::string::npos){
|
|
|
|
|
auto datas = GeneralUtils::SplitString(intermed, ',');
|
|
|
|
|
path_string = datas[0];
|
|
|
|
|
m_PathIndex = stoi(datas[1]) - 1;
|
|
|
|
|
} else {
|
|
|
|
|
path_string = intermed;
|
|
|
|
|
m_PathIndex = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (path_string != "") {
|
|
|
|
|
SetMovementPath(const_cast<Path*>(dZoneManager::Instance()->GetZone()->GetPath(path_string)));
|
|
|
|
|
} else m_MovementPath = nullptr;
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
// We don't recognize the action, let a dev know
|
|
|
|
|
Game::logger->LogDebug("ControllablePhysicsComponent", "Unhandled action %s", GeneralUtils::UTF16ToWTF8(action->GetKey()).c_str());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NiPoint3 MovementAIComponent::GetNextWaypoint() const {
|
|
|
|
|
@@ -199,6 +284,7 @@ NiPoint3 MovementAIComponent::ApproximateLocation() const {
|
|
|
|
|
if (dpWorld::Instance().IsLoaded()) {
|
|
|
|
|
approximation.y = dpWorld::Instance().GetNavMesh()->GetHeightAtPoint(approximation);
|
|
|
|
|
}
|
|
|
|
|
if (abs(destination.y - source.y) > 3) destination.y = source.y;
|
|
|
|
|
|
|
|
|
|
return approximation;
|
|
|
|
|
}
|
|
|
|
|
@@ -228,10 +314,12 @@ float MovementAIComponent::GetTimer() const {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool MovementAIComponent::AtFinalWaypoint() const {
|
|
|
|
|
|
|
|
|
|
return m_Done;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MovementAIComponent::Stop() {
|
|
|
|
|
Game::logger->Log("MovementAIComponent", "stopped");
|
|
|
|
|
if (m_Done) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
@@ -272,6 +360,22 @@ void MovementAIComponent::SetPath(std::vector<NiPoint3> path) {
|
|
|
|
|
m_Queue.pop();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MovementAIComponent::SetMovementPath(Path* movementPath){
|
|
|
|
|
Game::logger->Log("MovementAIComponent", "setmovementpath %s", movementPath->pathName.c_str());
|
|
|
|
|
m_MovementPath = movementPath;
|
|
|
|
|
|
|
|
|
|
// get waypoints
|
|
|
|
|
std::vector<NiPoint3> pathWaypoints;
|
|
|
|
|
for (const auto& waypoint : movementPath->pathWaypoints) m_CurrentPath.push_back(waypoint.position);
|
|
|
|
|
SetSpeed(m_BaseSpeed);
|
|
|
|
|
m_PathIndex = m_Parent->GetVarAs<int>(u"attached_path_start");
|
|
|
|
|
|
|
|
|
|
m_TotalTime = m_Timer = 0;
|
|
|
|
|
|
|
|
|
|
m_Done = false;
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
float MovementAIComponent::GetBaseSpeed(LOT lot) {
|
|
|
|
|
// Check if the lot is in the cache
|
|
|
|
|
const auto& it = m_PhysicsSpeedCache.find(lot);
|
|
|
|
|
@@ -313,11 +417,13 @@ foundComponent:
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_PhysicsSpeedCache[lot] = speed;
|
|
|
|
|
Game::logger->Log("MovementAIComponent", "speed = %f", speed);
|
|
|
|
|
|
|
|
|
|
return speed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MovementAIComponent::SetPosition(const NiPoint3& value) {
|
|
|
|
|
Game::logger->Log("MovementAIComponent", "set position %f %f %f", value.x, value.y, value.z);
|
|
|
|
|
auto* controllablePhysicsComponent = m_Parent->GetComponent<ControllablePhysicsComponent>();
|
|
|
|
|
|
|
|
|
|
if (controllablePhysicsComponent != nullptr) {
|
|
|
|
|
@@ -354,6 +460,8 @@ void MovementAIComponent::SetRotation(const NiQuaternion& value) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MovementAIComponent::SetVelocity(const NiPoint3& value) {
|
|
|
|
|
Game::logger->Log("MovementAIComponent", "set velocity %f %f %f", value.x, value.y, value.z);
|
|
|
|
|
|
|
|
|
|
auto* controllablePhysicsComponent = m_Parent->GetComponent<ControllablePhysicsComponent>();
|
|
|
|
|
|
|
|
|
|
if (controllablePhysicsComponent != nullptr) {
|
|
|
|
|
@@ -423,7 +531,7 @@ void MovementAIComponent::SetDestination(const NiPoint3& value) {
|
|
|
|
|
m_CurrentPath.push_back(point);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_CurrentPath.push_back(computedPath[computedPath.size() - 1]);
|
|
|
|
|
m_CurrentPath.push_back(computedPath.at(computedPath.size() - 1));
|
|
|
|
|
|
|
|
|
|
m_PathIndex = 0;
|
|
|
|
|
|
|
|
|
|
@@ -437,7 +545,14 @@ NiPoint3 MovementAIComponent::GetDestination() const {
|
|
|
|
|
return GetCurrentPosition();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return m_CurrentPath[m_CurrentPath.size() - 1];
|
|
|
|
|
auto destination = m_CurrentPath.at(m_CurrentPath.size() - 1);
|
|
|
|
|
if (dpWorld::Instance().IsLoaded()) {
|
|
|
|
|
destination.y = dpWorld::Instance().GetNavMesh()->GetHeightAtPoint(destination);
|
|
|
|
|
}
|
|
|
|
|
auto source = ApproximateLocation();
|
|
|
|
|
if (abs(destination.y - source.y) > 3) destination.y = source.y;
|
|
|
|
|
|
|
|
|
|
return destination;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MovementAIComponent::SetSpeed(const float value) {
|
|
|
|
|
|