| 1 | // |
| 2 | // Copyright (c) 2009-2010 Mikko Mononen [email protected] |
| 3 | // |
| 4 | // This software is provided 'as-is', without any express or implied |
| 5 | // warranty. In no event will the authors be held liable for any damages |
| 6 | // arising from the use of this software. |
| 7 | // Permission is granted to anyone to use this software for any purpose, |
| 8 | // including commercial applications, and to alter it and redistribute it |
| 9 | // freely, subject to the following restrictions: |
| 10 | // 1. The origin of this software must not be misrepresented; you must not |
| 11 | // claim that you wrote the original software. If you use this software |
| 12 | // in a product, an acknowledgment in the product documentation would be |
| 13 | // appreciated but is not required. |
| 14 | // 2. Altered source versions must be plainly marked as such, and must not be |
| 15 | // misrepresented as being the original software. |
| 16 | // 3. This notice may not be removed or altered from any source distribution. |
| 17 | // |
| 18 | |
| 19 | #include <string.h> |
| 20 | #include "DetourPathQueue.h" |
| 21 | #include "DetourNavMesh.h" |
| 22 | #include "DetourNavMeshQuery.h" |
| 23 | #include "DetourAlloc.h" |
| 24 | #include "DetourCommon.h" |
| 25 | |
| 26 | |
| 27 | dtPathQueue::dtPathQueue() : |
| 28 | m_nextHandle(1), |
| 29 | m_maxPathSize(0), |
| 30 | m_queueHead(0), |
| 31 | m_navquery(0) |
| 32 | { |
| 33 | for (int i = 0; i < MAX_QUEUE; ++i) |
| 34 | m_queue[i].path = 0; |
| 35 | } |
| 36 | |
| 37 | dtPathQueue::~dtPathQueue() |
| 38 | { |
| 39 | purge(); |
| 40 | } |
| 41 | |
| 42 | void dtPathQueue::purge() |
| 43 | { |
| 44 | dtFreeNavMeshQuery(m_navquery); |
| 45 | m_navquery = 0; |
| 46 | for (int i = 0; i < MAX_QUEUE; ++i) |
| 47 | { |
| 48 | dtFree(m_queue[i].path); |
| 49 | m_queue[i].path = 0; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | bool dtPathQueue::init(const int maxPathSize, const int maxSearchNodeCount, dtNavMesh* nav) |
| 54 | { |
| 55 | purge(); |
| 56 | |
| 57 | m_navquery = dtAllocNavMeshQuery(); |
| 58 | if (!m_navquery) |
| 59 | return false; |
| 60 | if (dtStatusFailed(m_navquery->init(nav, maxSearchNodeCount))) |
| 61 | return false; |
| 62 | |
| 63 | m_maxPathSize = maxPathSize; |
| 64 | for (int i = 0; i < MAX_QUEUE; ++i) |
| 65 | { |
| 66 | m_queue[i].ref = DT_PATHQ_INVALID; |
| 67 | m_queue[i].path = (dtPolyRef*)dtAlloc(sizeof(dtPolyRef)*m_maxPathSize, DT_ALLOC_PERM); |
| 68 | if (!m_queue[i].path) |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | m_queueHead = 0; |
| 73 | |
| 74 | return true; |
| 75 | } |
| 76 | |
| 77 | void dtPathQueue::update(const int maxIters) |
| 78 | { |
| 79 | static const int MAX_KEEP_ALIVE = 2; // in update ticks. |
| 80 | |
| 81 | // Update path request until there is nothing to update |
| 82 | // or upto maxIters pathfinder iterations has been consumed. |
| 83 | int iterCount = maxIters; |
| 84 | |
| 85 | for (int i = 0; i < MAX_QUEUE; ++i) |
| 86 | { |
| 87 | PathQuery& q = m_queue[m_queueHead % MAX_QUEUE]; |
| 88 | |
| 89 | // Skip inactive requests. |
| 90 | if (q.ref == DT_PATHQ_INVALID) |
| 91 | { |
| 92 | m_queueHead++; |
| 93 | continue; |
| 94 | } |
| 95 | |
| 96 | // Handle completed request. |
| 97 | if (dtStatusSucceed(q.status) || dtStatusFailed(q.status)) |
| 98 | { |
| 99 | // If the path result has not been read in few frames, free the slot. |
| 100 | q.keepAlive++; |
| 101 | if (q.keepAlive > MAX_KEEP_ALIVE) |
| 102 | { |
| 103 | q.ref = DT_PATHQ_INVALID; |
| 104 | q.status = 0; |
| 105 | } |
| 106 | |
| 107 | m_queueHead++; |
| 108 | continue; |
| 109 | } |
| 110 | |
| 111 | // Handle query start. |
| 112 | if (q.status == 0) |
| 113 | { |
| 114 | q.status = m_navquery->initSlicedFindPath(q.startRef, q.endRef, q.startPos, q.endPos, q.filter); |
| 115 | } |
| 116 | // Handle query in progress. |
| 117 | if (dtStatusInProgress(q.status)) |
| 118 | { |
| 119 | int iters = 0; |
| 120 | q.status = m_navquery->updateSlicedFindPath(iterCount, &iters); |
| 121 | iterCount -= iters; |
| 122 | } |
| 123 | if (dtStatusSucceed(q.status)) |
| 124 | { |
| 125 | q.status = m_navquery->finalizeSlicedFindPath(q.path, &q.npath, m_maxPathSize); |
| 126 | } |
| 127 | |
| 128 | if (iterCount <= 0) |
| 129 | break; |
| 130 | |
| 131 | m_queueHead++; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | dtPathQueueRef dtPathQueue::request(dtPolyRef startRef, dtPolyRef endRef, |
| 136 | const float* startPos, const float* endPos, |
| 137 | const dtQueryFilter* filter) |
| 138 | { |
| 139 | // Find empty slot |
| 140 | int slot = -1; |
| 141 | for (int i = 0; i < MAX_QUEUE; ++i) |
| 142 | { |
| 143 | if (m_queue[i].ref == DT_PATHQ_INVALID) |
| 144 | { |
| 145 | slot = i; |
| 146 | break; |
| 147 | } |
| 148 | } |
| 149 | // Could not find slot. |
| 150 | if (slot == -1) |
| 151 | return DT_PATHQ_INVALID; |
| 152 | |
| 153 | dtPathQueueRef ref = m_nextHandle++; |
| 154 | if (m_nextHandle == DT_PATHQ_INVALID) m_nextHandle++; |
| 155 | |
| 156 | PathQuery& q = m_queue[slot]; |
| 157 | q.ref = ref; |
| 158 | dtVcopy(q.startPos, startPos); |
| 159 | q.startRef = startRef; |
| 160 | dtVcopy(q.endPos, endPos); |
| 161 | q.endRef = endRef; |
| 162 | |
| 163 | q.status = 0; |
| 164 | q.npath = 0; |
| 165 | q.filter = filter; |
| 166 | q.keepAlive = 0; |
| 167 | |
| 168 | return ref; |
| 169 | } |
| 170 | |
| 171 | dtStatus dtPathQueue::getRequestStatus(dtPathQueueRef ref) const |
| 172 | { |
| 173 | for (int i = 0; i < MAX_QUEUE; ++i) |
| 174 | { |
| 175 | if (m_queue[i].ref == ref) |
| 176 | return m_queue[i].status; |
| 177 | } |
| 178 | return DT_FAILURE; |
| 179 | } |
| 180 | |
| 181 | dtStatus dtPathQueue::getPathResult(dtPathQueueRef ref, dtPolyRef* path, int* pathSize, const int maxPath) |
| 182 | { |
| 183 | for (int i = 0; i < MAX_QUEUE; ++i) |
| 184 | { |
| 185 | if (m_queue[i].ref == ref) |
| 186 | { |
| 187 | PathQuery& q = m_queue[i]; |
| 188 | dtStatus details = q.status & DT_STATUS_DETAIL_MASK; |
| 189 | // Free request for reuse. |
| 190 | q.ref = DT_PATHQ_INVALID; |
| 191 | q.status = 0; |
| 192 | // Copy path |
| 193 | int n = dtMin(q.npath, maxPath); |
| 194 | memcpy(path, q.path, sizeof(dtPolyRef)*n); |
| 195 | *pathSize = n; |
| 196 | return details | DT_SUCCESS; |
| 197 | } |
| 198 | } |
| 199 | return DT_FAILURE; |
| 200 | } |