llpathfindingnavmesh.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /**
  2. * @file llpathfindingnavmesh.cpp
  3. * @brief Implementation of llpathfindingnavmesh
  4. *
  5. * $LicenseInfo:firstyear=2012&license=viewergpl$
  6. *
  7. * Copyright (c) 2012, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #include "llviewerprecompiledheaders.h"
  33. #include "llpathfindingnavmesh.h"
  34. #include "llsd.h"
  35. #include "llsdserialize.h"
  36. #include "llpathfindingnavmeshstatus.h"
  37. #define NAVMESH_VERSION_FIELD "navmesh_version"
  38. #define NAVMESH_DATA_FIELD "navmesh_data"
  39. LLPathfindingNavMesh::LLPathfindingNavMesh(const LLUUID& region_id)
  40. : mNavMeshStatus(region_id),
  41. mNavMeshRequestStatus(kNavMeshRequestUnknown),
  42. mNavMeshSignal(),
  43. mNavMeshData()
  44. {
  45. }
  46. LLPathfindingNavMesh::navmesh_slot_t LLPathfindingNavMesh::registerNavMeshListener(navmesh_cb_t callback)
  47. {
  48. return mNavMeshSignal.connect(callback);
  49. }
  50. bool LLPathfindingNavMesh::hasNavMeshVersion(const LLPathfindingNavMeshStatus& status) const
  51. {
  52. return mNavMeshStatus.getVersion() == status.getVersion() &&
  53. (mNavMeshRequestStatus == kNavMeshRequestStarted ||
  54. mNavMeshRequestStatus == kNavMeshRequestCompleted ||
  55. (mNavMeshRequestStatus == kNavMeshRequestChecking &&
  56. !mNavMeshData.empty()));
  57. }
  58. void LLPathfindingNavMesh::handleNavMeshWaitForRegionLoad()
  59. {
  60. setRequestStatus(kNavMeshRequestWaiting);
  61. }
  62. void LLPathfindingNavMesh::handleNavMeshCheckVersion()
  63. {
  64. setRequestStatus(kNavMeshRequestChecking);
  65. }
  66. void LLPathfindingNavMesh::handleRefresh(const LLPathfindingNavMeshStatus& status)
  67. {
  68. if (mNavMeshStatus.getRegionUUID() != status.getRegionUUID())
  69. {
  70. llwarns << "Navmesh status received for another region: ignoring."
  71. << llendl;
  72. return;
  73. }
  74. if (mNavMeshStatus.getVersion() != status.getVersion())
  75. {
  76. llwarns << "Navmesh status received with bad version: ignoring."
  77. << llendl;
  78. return;
  79. }
  80. mNavMeshStatus = status;
  81. if (mNavMeshRequestStatus == kNavMeshRequestChecking)
  82. {
  83. if (!mNavMeshData.empty())
  84. {
  85. setRequestStatus(kNavMeshRequestCompleted);
  86. }
  87. else
  88. {
  89. llwarns << "Empty navmesh data received !" << llendl;
  90. }
  91. }
  92. else
  93. {
  94. sendStatus();
  95. }
  96. }
  97. void LLPathfindingNavMesh::handleNavMeshNewVersion(const LLPathfindingNavMeshStatus& status)
  98. {
  99. if (mNavMeshStatus.getRegionUUID() != status.getRegionUUID())
  100. {
  101. llwarns << "Navmesh version received for another region: ignoring."
  102. << llendl;
  103. return;
  104. }
  105. if (mNavMeshStatus.getVersion() == status.getVersion())
  106. {
  107. mNavMeshStatus = status;
  108. sendStatus();
  109. }
  110. else
  111. {
  112. mNavMeshData.clear();
  113. mNavMeshStatus = status;
  114. setRequestStatus(kNavMeshRequestNeedsUpdate);
  115. }
  116. }
  117. void LLPathfindingNavMesh::handleNavMeshStart(const LLPathfindingNavMeshStatus& status)
  118. {
  119. if (mNavMeshStatus.getRegionUUID() != status.getRegionUUID())
  120. {
  121. llwarns << "Navmesh start signal received for another region: ignoring."
  122. << llendl;
  123. return;
  124. }
  125. mNavMeshStatus = status;
  126. setRequestStatus(kNavMeshRequestStarted);
  127. }
  128. void LLPathfindingNavMesh::handleNavMeshResult(const LLSD& content, U32 version)
  129. {
  130. if (content.has(NAVMESH_VERSION_FIELD) &&
  131. content.get(NAVMESH_VERSION_FIELD).isInteger() &&
  132. content.get(NAVMESH_VERSION_FIELD).asInteger() >= 0)
  133. {
  134. U32 advertized = (U32)content.get(NAVMESH_VERSION_FIELD).asInteger();
  135. if (advertized != version)
  136. {
  137. llwarns << "Mismatch between expected and embedded navmesh versions occurred"
  138. << llendl;
  139. version = advertized;
  140. }
  141. }
  142. else
  143. {
  144. llwarns << "Malformed navmesh data: missing version" << llendl;
  145. }
  146. if (mNavMeshStatus.getVersion() == version)
  147. {
  148. ENavMeshRequestStatus status;
  149. if (content.has(NAVMESH_DATA_FIELD))
  150. {
  151. const LLSD::Binary& value =
  152. content.get(NAVMESH_DATA_FIELD).asBinary();
  153. bool valid = false;
  154. size_t decomp_size = 0;
  155. U8* buffer = unzip_llsdNavMesh(valid, decomp_size, value.data(),
  156. value.size());
  157. if (!valid || !buffer)
  158. {
  159. llwarns << "Unable to decompress the navmesh llsd." << llendl;
  160. status = kNavMeshRequestError;
  161. }
  162. else
  163. {
  164. mNavMeshData.resize(decomp_size);
  165. memcpy(&mNavMeshData[0], &buffer[0], decomp_size);
  166. status = kNavMeshRequestCompleted;
  167. }
  168. if (buffer)
  169. {
  170. free(buffer);
  171. }
  172. }
  173. else
  174. {
  175. llwarns << "No mesh data received" << llendl;
  176. status = kNavMeshRequestError;
  177. }
  178. setRequestStatus(status);
  179. }
  180. }
  181. void LLPathfindingNavMesh::handleNavMeshNotEnabled()
  182. {
  183. mNavMeshData.clear();
  184. setRequestStatus(kNavMeshRequestNotEnabled);
  185. }
  186. void LLPathfindingNavMesh::handleNavMeshError()
  187. {
  188. mNavMeshData.clear();
  189. setRequestStatus(kNavMeshRequestError);
  190. }
  191. void LLPathfindingNavMesh::handleNavMeshError(U32 version)
  192. {
  193. if (mNavMeshStatus.getVersion() == version)
  194. {
  195. handleNavMeshError();
  196. }
  197. }
  198. void LLPathfindingNavMesh::setRequestStatus(ENavMeshRequestStatus statusp)
  199. {
  200. mNavMeshRequestStatus = statusp;
  201. sendStatus();
  202. }
  203. void LLPathfindingNavMesh::sendStatus()
  204. {
  205. mNavMeshSignal(mNavMeshRequestStatus, mNavMeshStatus, mNavMeshData);
  206. }