From 575ba28b6236d26e47357ada91e4dae70e61242c Mon Sep 17 00:00:00 2001 From: KimLS Date: Sun, 6 Aug 2017 20:48:39 -0700 Subject: [PATCH] Bug fixes with fear points and teleport jumps --- zone/fearpath.cpp | 15 +------------- zone/pathfinder_nav_mesh.cpp | 32 ++++++++++++++++++++++-------- zone/pathfinder_waypoint.cpp | 7 +++++++ zone/pathing.cpp | 38 +++++++++++++++++++++++++++++++++--- 4 files changed, 67 insertions(+), 25 deletions(-) diff --git a/zone/fearpath.cpp b/zone/fearpath.cpp index 573783fbc..b2f69f0e9 100644 --- a/zone/fearpath.cpp +++ b/zone/fearpath.cpp @@ -133,20 +133,7 @@ void Mob::CalculateNewFearpoint() if (Node.x != 0.0f || Node.y != 0.0f || Node.z != 0.0f) { ++Node.z; - - glm::vec3 CurrentPosition(GetX(), GetY(), GetZ()); - - auto Route = zone->pathing->FindRoute(CurrentPosition, Node); - - if (!Route.empty()) - { - auto first = (*Route.begin()); - m_FearWalkTarget = glm::vec3(first.pos.x, first.pos.y, first.pos.z); - currently_fleeing = true; - - Log(Logs::Detail, Logs::None, "Feared to node %i (%8.3f, %8.3f, %8.3f)", Node, first.pos.x, first.pos.y, first.pos.z); - return; - } + m_FearWalkTarget = Node; } diff --git a/zone/pathfinder_nav_mesh.cpp b/zone/pathfinder_nav_mesh.cpp index 9ac5402d8..f44e02ec9 100644 --- a/zone/pathfinder_nav_mesh.cpp +++ b/zone/pathfinder_nav_mesh.cpp @@ -6,6 +6,7 @@ #include "pathfinder_nav_mesh.h" #include "zone.h" +#include "water_map.h" #include "client.h" #include "../common/compression.h" @@ -106,12 +107,7 @@ IPathfinder::IPath PathfinderNavmesh::FindRoute(const glm::vec3 &start, const gl node.z = straight_path[i * 3 + 1]; node.y = straight_path[i * 3 + 2]; - if (zone->HasMap()) { - auto best_z = zone->zonemap->FindBestZ(node, nullptr); - if (best_z != BEST_Z_INVALID) { - node.z = best_z; - } - } + Route.push_back(node); unsigned short flag = 0; if (dtStatusSucceed(m_impl->nav_mesh->getPolyFlags(straight_path_polys[i], &flag))) { @@ -119,8 +115,6 @@ IPathfinder::IPath PathfinderNavmesh::FindRoute(const glm::vec3 &start, const gl Route.push_back(true); } } - - Route.push_back(node); } return Route; @@ -134,6 +128,28 @@ IPathfinder::IPath PathfinderNavmesh::FindRoute(const glm::vec3 &start, const gl glm::vec3 PathfinderNavmesh::GetRandomLocation() { + if (!m_impl->nav_mesh) { + return glm::vec3(); + } + + if (!m_impl->query) { + m_impl->query = dtAllocNavMeshQuery(); + m_impl->query->init(m_impl->nav_mesh, 32768); + } + + dtQueryFilter filter; + filter.setIncludeFlags(65535U); + + dtPolyRef randomRef; + float point[3]; + + if (dtStatusSucceed(m_impl->query->findRandomPoint(&filter, []() { + return (float)zone->random.Real(0.0, 1.0); + }, &randomRef, point))) + { + return glm::vec3(point[0], point[2], point[1]); + } + return glm::vec3(); } diff --git a/zone/pathfinder_waypoint.cpp b/zone/pathfinder_waypoint.cpp index c845a4d88..b3859472a 100644 --- a/zone/pathfinder_waypoint.cpp +++ b/zone/pathfinder_waypoint.cpp @@ -187,6 +187,13 @@ IPathfinder::IPath PathfinderWaypoint::FindRoute(const glm::vec3 &start, const g glm::vec3 PathfinderWaypoint::GetRandomLocation() { + if (m_impl->Nodes.size() > 0) { + auto idx = zone->random.Int(0, (int)m_impl->Nodes.size() - 1); + auto &node = m_impl->Nodes[idx]; + + return node.v; + } + return glm::vec3(); } diff --git a/zone/pathing.cpp b/zone/pathing.cpp index 1fc47b549..826160905 100644 --- a/zone/pathing.cpp +++ b/zone/pathing.cpp @@ -2,9 +2,25 @@ #include "client.h" #include "zone.h" +#include "water_map.h" extern Zone *zone; +void AdjustRoute(std::list &nodes, int flymode) { + if (!zone->HasMap() || !zone->HasWaterMap()) { + return; + } + + for (auto &node : nodes) { + if (flymode == 0 || !zone->watermap->InLiquid(node.pos)) { + auto best_z = zone->zonemap->FindBestZ(node.pos, nullptr); + if (best_z != BEST_Z_INVALID) { + node.pos.z = best_z; + } + } + } +} + glm::vec3 Mob::UpdatePath(float ToX, float ToY, float ToZ, float Speed, bool &WaypointChanged, bool &NodeReached) { glm::vec3 To(ToX, ToY, ToZ); @@ -23,6 +39,8 @@ glm::vec3 Mob::UpdatePath(float ToX, float ToY, float ToZ, float Speed, bool &Wa if (Route.empty()) { Route = zone->pathing->FindRoute(From, To); + AdjustRoute(Route, flymode); + PathingDestination = To; WaypointChanged = true; NodeReached = false; @@ -39,10 +57,18 @@ glm::vec3 Mob::UpdatePath(float ToX, float ToY, float ToZ, float Speed, bool &Wa if (!SameDestination) { //We had a route but our target position moved too much Route = zone->pathing->FindRoute(From, To); + AdjustRoute(Route, flymode); + PathingDestination = To; WaypointChanged = true; NodeReached = false; - return (*Route.begin()).pos; + + if (Route.empty()) { + return From; + } + else { + return (*Route.begin()).pos; + } } } @@ -93,14 +119,20 @@ glm::vec3 Mob::UpdatePath(float ToX, float ToY, float ToZ, float Speed, bool &Wa if (Route.empty()) { Route = zone->pathing->FindRoute(From, To); + AdjustRoute(Route, flymode); PathingDestination = To; WaypointChanged = true; - return (*Route.begin()).pos; + + if (Route.empty()) { + return From; + } + else { + return (*Route.begin()).pos; + } } else { auto node = *Route.begin(); if (node.teleport) { - //If is identity node then is teleport node. Route.pop_front(); if (Route.empty()) {