/indra/newview/llworld.cpp

https://bitbucket.org/lindenlab/viewer-beta/ · C++ · 1252 lines · 953 code · 193 blank · 106 comment · 144 complexity · d5c9f0695c2ff37537564c06f8f6c545 MD5 · raw file

  1. /**
  2. * @file llworld.cpp
  3. * @brief Initial test structure to organize viewer regions
  4. *
  5. * $LicenseInfo:firstyear=2001&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2010, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. #include "llviewerprecompiledheaders.h"
  27. #include "llworld.h"
  28. #include "llrender.h"
  29. #include "indra_constants.h"
  30. #include "llstl.h"
  31. #include "llagent.h"
  32. #include "llviewercontrol.h"
  33. #include "lldrawpool.h"
  34. #include "llglheaders.h"
  35. #include "llhttpnode.h"
  36. #include "llregionhandle.h"
  37. #include "llsurface.h"
  38. #include "lltrans.h"
  39. #include "llviewercamera.h"
  40. #include "llviewertexture.h"
  41. #include "llviewertexturelist.h"
  42. #include "llviewernetwork.h"
  43. #include "llviewerobjectlist.h"
  44. #include "llviewerparceloverlay.h"
  45. #include "llviewerregion.h"
  46. #include "llviewerstats.h"
  47. #include "llvlcomposition.h"
  48. #include "llvoavatar.h"
  49. #include "llvocache.h"
  50. #include "llvowater.h"
  51. #include "message.h"
  52. #include "pipeline.h"
  53. #include "llappviewer.h" // for do_disconnect()
  54. #include <deque>
  55. #include <queue>
  56. #include <map>
  57. #include <cstring>
  58. //
  59. // Globals
  60. //
  61. U32 gAgentPauseSerialNum = 0;
  62. //
  63. // Constants
  64. //
  65. const S32 MAX_NUMBER_OF_CLOUDS = 750;
  66. const S32 WORLD_PATCH_SIZE = 16;
  67. extern LLColor4U MAX_WATER_COLOR;
  68. const U32 LLWorld::mWidth = 256;
  69. // meters/point, therefore mWidth * mScale = meters per edge
  70. const F32 LLWorld::mScale = 1.f;
  71. const F32 LLWorld::mWidthInMeters = mWidth * mScale;
  72. //
  73. // Functions
  74. //
  75. // allocate the stack
  76. LLWorld::LLWorld() :
  77. mLandFarClip(DEFAULT_FAR_PLANE),
  78. mLastPacketsIn(0),
  79. mLastPacketsOut(0),
  80. mLastPacketsLost(0),
  81. mSpaceTimeUSec(0)
  82. {
  83. for (S32 i = 0; i < 8; i++)
  84. {
  85. mEdgeWaterObjects[i] = NULL;
  86. }
  87. LLPointer<LLImageRaw> raw = new LLImageRaw(1,1,4);
  88. U8 *default_texture = raw->getData();
  89. *(default_texture++) = MAX_WATER_COLOR.mV[0];
  90. *(default_texture++) = MAX_WATER_COLOR.mV[1];
  91. *(default_texture++) = MAX_WATER_COLOR.mV[2];
  92. *(default_texture++) = MAX_WATER_COLOR.mV[3];
  93. mDefaultWaterTexturep = LLViewerTextureManager::getLocalTexture(raw.get(), FALSE);
  94. gGL.getTexUnit(0)->bind(mDefaultWaterTexturep);
  95. mDefaultWaterTexturep->setAddressMode(LLTexUnit::TAM_CLAMP);
  96. }
  97. void LLWorld::destroyClass()
  98. {
  99. mHoleWaterObjects.clear();
  100. gObjectList.destroy();
  101. for(region_list_t::iterator region_it = mRegionList.begin(); region_it != mRegionList.end(); )
  102. {
  103. LLViewerRegion* region_to_delete = *region_it++;
  104. removeRegion(region_to_delete->getHost());
  105. }
  106. if(LLVOCache::hasInstance())
  107. {
  108. LLVOCache::getInstance()->destroyClass() ;
  109. }
  110. LLViewerPartSim::getInstance()->destroyClass();
  111. mDefaultWaterTexturep = NULL ;
  112. for (S32 i = 0; i < 8; i++)
  113. {
  114. mEdgeWaterObjects[i] = NULL;
  115. }
  116. }
  117. LLViewerRegion* LLWorld::addRegion(const U64 &region_handle, const LLHost &host)
  118. {
  119. LLMemType mt(LLMemType::MTYPE_REGIONS);
  120. llinfos << "Add region with handle: " << region_handle << " on host " << host << llendl;
  121. LLViewerRegion *regionp = getRegionFromHandle(region_handle);
  122. if (regionp)
  123. {
  124. llinfos << "Region exists, removing it " << llendl;
  125. LLHost old_host = regionp->getHost();
  126. // region already exists!
  127. if (host == old_host && regionp->isAlive())
  128. {
  129. // This is a duplicate for the same host and it's alive, don't bother.
  130. return regionp;
  131. }
  132. if (host != old_host)
  133. {
  134. llwarns << "LLWorld::addRegion exists, but old host " << old_host
  135. << " does not match new host " << host << llendl;
  136. }
  137. if (!regionp->isAlive())
  138. {
  139. llwarns << "LLWorld::addRegion exists, but isn't alive" << llendl;
  140. }
  141. // Kill the old host, and then we can continue on and add the new host. We have to kill even if the host
  142. // matches, because all the agent state for the new camera is completely different.
  143. removeRegion(old_host);
  144. }
  145. U32 iindex = 0;
  146. U32 jindex = 0;
  147. from_region_handle(region_handle, &iindex, &jindex);
  148. S32 x = (S32)(iindex/mWidth);
  149. S32 y = (S32)(jindex/mWidth);
  150. llinfos << "Adding new region (" << x << ":" << y << ")" << llendl;
  151. llinfos << "Host: " << host << llendl;
  152. LLVector3d origin_global;
  153. origin_global = from_region_handle(region_handle);
  154. regionp = new LLViewerRegion(region_handle,
  155. host,
  156. mWidth,
  157. WORLD_PATCH_SIZE,
  158. getRegionWidthInMeters() );
  159. if (!regionp)
  160. {
  161. llerrs << "Unable to create new region!" << llendl;
  162. }
  163. mRegionList.push_back(regionp);
  164. mActiveRegionList.push_back(regionp);
  165. mCulledRegionList.push_back(regionp);
  166. // Find all the adjacent regions, and attach them.
  167. // Generate handles for all of the adjacent regions, and attach them in the correct way.
  168. // connect the edges
  169. F32 adj_x = 0.f;
  170. F32 adj_y = 0.f;
  171. F32 region_x = 0.f;
  172. F32 region_y = 0.f;
  173. U64 adj_handle = 0;
  174. F32 width = getRegionWidthInMeters();
  175. LLViewerRegion *neighborp;
  176. from_region_handle(region_handle, &region_x, &region_y);
  177. // Iterate through all directions, and connect neighbors if there.
  178. S32 dir;
  179. for (dir = 0; dir < 8; dir++)
  180. {
  181. adj_x = region_x + width * gDirAxes[dir][0];
  182. adj_y = region_y + width * gDirAxes[dir][1];
  183. to_region_handle(adj_x, adj_y, &adj_handle);
  184. neighborp = getRegionFromHandle(adj_handle);
  185. if (neighborp)
  186. {
  187. //llinfos << "Connecting " << region_x << ":" << region_y << " -> " << adj_x << ":" << adj_y << llendl;
  188. regionp->connectNeighbor(neighborp, dir);
  189. }
  190. }
  191. updateWaterObjects();
  192. return regionp;
  193. }
  194. void LLWorld::removeRegion(const LLHost &host)
  195. {
  196. F32 x, y;
  197. LLViewerRegion *regionp = getRegion(host);
  198. if (!regionp)
  199. {
  200. llwarns << "Trying to remove region that doesn't exist!" << llendl;
  201. return;
  202. }
  203. if (regionp == gAgent.getRegion())
  204. {
  205. for (region_list_t::iterator iter = mRegionList.begin();
  206. iter != mRegionList.end(); ++iter)
  207. {
  208. LLViewerRegion* reg = *iter;
  209. llwarns << "RegionDump: " << reg->getName()
  210. << " " << reg->getHost()
  211. << " " << reg->getOriginGlobal()
  212. << llendl;
  213. }
  214. llwarns << "Agent position global " << gAgent.getPositionGlobal()
  215. << " agent " << gAgent.getPositionAgent()
  216. << llendl;
  217. llwarns << "Regions visited " << gAgent.getRegionsVisited() << llendl;
  218. llwarns << "gFrameTimeSeconds " << gFrameTimeSeconds << llendl;
  219. llwarns << "Disabling region " << regionp->getName() << " that agent is in!" << llendl;
  220. LLAppViewer::instance()->forceDisconnect(LLTrans::getString("YouHaveBeenDisconnected"));
  221. regionp->saveObjectCache() ; //force to save objects here in case that the object cache is about to be destroyed.
  222. return;
  223. }
  224. from_region_handle(regionp->getHandle(), &x, &y);
  225. llinfos << "Removing region " << x << ":" << y << llendl;
  226. mRegionList.remove(regionp);
  227. mActiveRegionList.remove(regionp);
  228. mCulledRegionList.remove(regionp);
  229. mVisibleRegionList.remove(regionp);
  230. delete regionp;
  231. updateWaterObjects();
  232. //double check all objects of this region are removed.
  233. gObjectList.clearAllMapObjectsInRegion(regionp) ;
  234. //llassert_always(!gObjectList.hasMapObjectInRegion(regionp)) ;
  235. }
  236. LLViewerRegion* LLWorld::getRegion(const LLHost &host)
  237. {
  238. for (region_list_t::iterator iter = mRegionList.begin();
  239. iter != mRegionList.end(); ++iter)
  240. {
  241. LLViewerRegion* regionp = *iter;
  242. if (regionp->getHost() == host)
  243. {
  244. return regionp;
  245. }
  246. }
  247. return NULL;
  248. }
  249. LLViewerRegion* LLWorld::getRegionFromPosAgent(const LLVector3 &pos)
  250. {
  251. return getRegionFromPosGlobal(gAgent.getPosGlobalFromAgent(pos));
  252. }
  253. LLViewerRegion* LLWorld::getRegionFromPosGlobal(const LLVector3d &pos)
  254. {
  255. for (region_list_t::iterator iter = mRegionList.begin();
  256. iter != mRegionList.end(); ++iter)
  257. {
  258. LLViewerRegion* regionp = *iter;
  259. if (regionp->pointInRegionGlobal(pos))
  260. {
  261. return regionp;
  262. }
  263. }
  264. return NULL;
  265. }
  266. LLVector3d LLWorld::clipToVisibleRegions(const LLVector3d &start_pos, const LLVector3d &end_pos)
  267. {
  268. if (positionRegionValidGlobal(end_pos))
  269. {
  270. return end_pos;
  271. }
  272. LLViewerRegion* regionp = getRegionFromPosGlobal(start_pos);
  273. if (!regionp)
  274. {
  275. return start_pos;
  276. }
  277. LLVector3d delta_pos = end_pos - start_pos;
  278. LLVector3d delta_pos_abs;
  279. delta_pos_abs.setVec(delta_pos);
  280. delta_pos_abs.abs();
  281. LLVector3 region_coord = regionp->getPosRegionFromGlobal(end_pos);
  282. F64 clip_factor = 1.0;
  283. F32 region_width = regionp->getWidth();
  284. if (region_coord.mV[VX] < 0.f)
  285. {
  286. if (region_coord.mV[VY] < region_coord.mV[VX])
  287. {
  288. // clip along y -
  289. clip_factor = -(region_coord.mV[VY] / delta_pos_abs.mdV[VY]);
  290. }
  291. else
  292. {
  293. // clip along x -
  294. clip_factor = -(region_coord.mV[VX] / delta_pos_abs.mdV[VX]);
  295. }
  296. }
  297. else if (region_coord.mV[VX] > region_width)
  298. {
  299. if (region_coord.mV[VY] > region_coord.mV[VX])
  300. {
  301. // clip along y +
  302. clip_factor = (region_coord.mV[VY] - region_width) / delta_pos_abs.mdV[VY];
  303. }
  304. else
  305. {
  306. //clip along x +
  307. clip_factor = (region_coord.mV[VX] - region_width) / delta_pos_abs.mdV[VX];
  308. }
  309. }
  310. else if (region_coord.mV[VY] < 0.f)
  311. {
  312. // clip along y -
  313. clip_factor = -(region_coord.mV[VY] / delta_pos_abs.mdV[VY]);
  314. }
  315. else if (region_coord.mV[VY] > region_width)
  316. {
  317. // clip along y +
  318. clip_factor = (region_coord.mV[VY] - region_width) / delta_pos_abs.mdV[VY];
  319. }
  320. // clamp to within region dimensions
  321. LLVector3d final_region_pos = LLVector3d(region_coord) - (delta_pos * clip_factor);
  322. final_region_pos.mdV[VX] = llclamp(final_region_pos.mdV[VX], 0.0,
  323. (F64)(region_width - F_ALMOST_ZERO));
  324. final_region_pos.mdV[VY] = llclamp(final_region_pos.mdV[VY], 0.0,
  325. (F64)(region_width - F_ALMOST_ZERO));
  326. final_region_pos.mdV[VZ] = llclamp(final_region_pos.mdV[VZ], 0.0,
  327. (F64)(LLWorld::getInstance()->getRegionMaxHeight() - F_ALMOST_ZERO));
  328. return regionp->getPosGlobalFromRegion(LLVector3(final_region_pos));
  329. }
  330. LLViewerRegion* LLWorld::getRegionFromHandle(const U64 &handle)
  331. {
  332. for (region_list_t::iterator iter = mRegionList.begin();
  333. iter != mRegionList.end(); ++iter)
  334. {
  335. LLViewerRegion* regionp = *iter;
  336. if (regionp->getHandle() == handle)
  337. {
  338. return regionp;
  339. }
  340. }
  341. return NULL;
  342. }
  343. void LLWorld::updateAgentOffset(const LLVector3d &offset_global)
  344. {
  345. #if 0
  346. for (region_list_t::iterator iter = mRegionList.begin();
  347. iter != mRegionList.end(); ++iter)
  348. {
  349. LLViewerRegion* regionp = *iter;
  350. regionp->setAgentOffset(offset_global);
  351. }
  352. #endif
  353. }
  354. BOOL LLWorld::positionRegionValidGlobal(const LLVector3d &pos_global)
  355. {
  356. for (region_list_t::iterator iter = mRegionList.begin();
  357. iter != mRegionList.end(); ++iter)
  358. {
  359. LLViewerRegion* regionp = *iter;
  360. if (regionp->pointInRegionGlobal(pos_global))
  361. {
  362. return TRUE;
  363. }
  364. }
  365. return FALSE;
  366. }
  367. // Allow objects to go up to their radius underground.
  368. F32 LLWorld::getMinAllowedZ(LLViewerObject* object, const LLVector3d &global_pos)
  369. {
  370. F32 land_height = resolveLandHeightGlobal(global_pos);
  371. F32 radius = 0.5f * object->getScale().length();
  372. return land_height - radius;
  373. }
  374. LLViewerRegion* LLWorld::resolveRegionGlobal(LLVector3 &pos_region, const LLVector3d &pos_global)
  375. {
  376. LLViewerRegion *regionp = getRegionFromPosGlobal(pos_global);
  377. if (regionp)
  378. {
  379. pos_region = regionp->getPosRegionFromGlobal(pos_global);
  380. return regionp;
  381. }
  382. return NULL;
  383. }
  384. LLViewerRegion* LLWorld::resolveRegionAgent(LLVector3 &pos_region, const LLVector3 &pos_agent)
  385. {
  386. LLVector3d pos_global = gAgent.getPosGlobalFromAgent(pos_agent);
  387. LLViewerRegion *regionp = getRegionFromPosGlobal(pos_global);
  388. if (regionp)
  389. {
  390. pos_region = regionp->getPosRegionFromGlobal(pos_global);
  391. return regionp;
  392. }
  393. return NULL;
  394. }
  395. F32 LLWorld::resolveLandHeightAgent(const LLVector3 &pos_agent)
  396. {
  397. LLVector3d pos_global = gAgent.getPosGlobalFromAgent(pos_agent);
  398. return resolveLandHeightGlobal(pos_global);
  399. }
  400. F32 LLWorld::resolveLandHeightGlobal(const LLVector3d &pos_global)
  401. {
  402. LLViewerRegion *regionp = getRegionFromPosGlobal(pos_global);
  403. if (regionp)
  404. {
  405. return regionp->getLand().resolveHeightGlobal(pos_global);
  406. }
  407. return 0.0f;
  408. }
  409. // Takes a line defined by "point_a" and "point_b" and determines the closest (to point_a)
  410. // point where the the line intersects an object or the land surface. Stores the results
  411. // in "intersection" and "intersection_normal" and returns a scalar value that represents
  412. // the normalized distance along the line from "point_a" to "intersection".
  413. //
  414. // Currently assumes point_a and point_b only differ in z-direction,
  415. // but it may eventually become more general.
  416. F32 LLWorld::resolveStepHeightGlobal(const LLVOAvatar* avatarp, const LLVector3d &point_a, const LLVector3d &point_b,
  417. LLVector3d &intersection, LLVector3 &intersection_normal,
  418. LLViewerObject **viewerObjectPtr)
  419. {
  420. // initialize return value to null
  421. if (viewerObjectPtr)
  422. {
  423. *viewerObjectPtr = NULL;
  424. }
  425. LLViewerRegion *regionp = getRegionFromPosGlobal(point_a);
  426. if (!regionp)
  427. {
  428. // We're outside the world
  429. intersection = 0.5f * (point_a + point_b);
  430. intersection_normal.setVec(0.0f, 0.0f, 1.0f);
  431. return 0.5f;
  432. }
  433. // calculate the length of the segment
  434. F32 segment_length = (F32)((point_a - point_b).length());
  435. if (0.0f == segment_length)
  436. {
  437. intersection = point_a;
  438. intersection_normal.setVec(0.0f, 0.0f, 1.0f);
  439. return segment_length;
  440. }
  441. // get land height
  442. // Note: we assume that the line is parallel to z-axis here
  443. LLVector3d land_intersection = point_a;
  444. F32 normalized_land_distance;
  445. land_intersection.mdV[VZ] = regionp->getLand().resolveHeightGlobal(point_a);
  446. normalized_land_distance = (F32)(point_a.mdV[VZ] - land_intersection.mdV[VZ]) / segment_length;
  447. intersection = land_intersection;
  448. intersection_normal = resolveLandNormalGlobal(land_intersection);
  449. if (avatarp && !avatarp->mFootPlane.isExactlyClear())
  450. {
  451. LLVector3 foot_plane_normal(avatarp->mFootPlane.mV);
  452. LLVector3 start_pt = avatarp->getRegion()->getPosRegionFromGlobal(point_a);
  453. // added 0.05 meters to compensate for error in foot plane reported by Havok
  454. F32 norm_dist_from_plane = ((start_pt * foot_plane_normal) - avatarp->mFootPlane.mV[VW]) + 0.05f;
  455. norm_dist_from_plane = llclamp(norm_dist_from_plane / segment_length, 0.f, 1.f);
  456. if (norm_dist_from_plane < normalized_land_distance)
  457. {
  458. // collided with object before land
  459. normalized_land_distance = norm_dist_from_plane;
  460. intersection = point_a;
  461. intersection.mdV[VZ] -= norm_dist_from_plane * segment_length;
  462. intersection_normal = foot_plane_normal;
  463. }
  464. else
  465. {
  466. intersection = land_intersection;
  467. intersection_normal = resolveLandNormalGlobal(land_intersection);
  468. }
  469. }
  470. return normalized_land_distance;
  471. }
  472. LLSurfacePatch * LLWorld::resolveLandPatchGlobal(const LLVector3d &pos_global)
  473. {
  474. // returns a pointer to the patch at this location
  475. LLViewerRegion *regionp = getRegionFromPosGlobal(pos_global);
  476. if (!regionp)
  477. {
  478. return NULL;
  479. }
  480. return regionp->getLand().resolvePatchGlobal(pos_global);
  481. }
  482. LLVector3 LLWorld::resolveLandNormalGlobal(const LLVector3d &pos_global)
  483. {
  484. LLViewerRegion *regionp = getRegionFromPosGlobal(pos_global);
  485. if (!regionp)
  486. {
  487. return LLVector3::z_axis;
  488. }
  489. return regionp->getLand().resolveNormalGlobal(pos_global);
  490. }
  491. void LLWorld::updateVisibilities()
  492. {
  493. F32 cur_far_clip = LLViewerCamera::getInstance()->getFar();
  494. // Go through the culled list and check for visible regions (region is visible if land is visible)
  495. for (region_list_t::iterator iter = mCulledRegionList.begin();
  496. iter != mCulledRegionList.end(); )
  497. {
  498. region_list_t::iterator curiter = iter++;
  499. LLViewerRegion* regionp = *curiter;
  500. LLSpatialPartition* part = regionp->getSpatialPartition(LLViewerRegion::PARTITION_TERRAIN);
  501. if (part)
  502. {
  503. LLSpatialGroup* group = (LLSpatialGroup*) part->mOctree->getListener(0);
  504. if (LLViewerCamera::getInstance()->AABBInFrustum(group->mBounds[0], group->mBounds[1]))
  505. {
  506. mCulledRegionList.erase(curiter);
  507. mVisibleRegionList.push_back(regionp);
  508. }
  509. }
  510. }
  511. // Update all of the visible regions
  512. for (region_list_t::iterator iter = mVisibleRegionList.begin();
  513. iter != mVisibleRegionList.end(); )
  514. {
  515. region_list_t::iterator curiter = iter++;
  516. LLViewerRegion* regionp = *curiter;
  517. if (!regionp->getLand().hasZData())
  518. {
  519. continue;
  520. }
  521. LLSpatialPartition* part = regionp->getSpatialPartition(LLViewerRegion::PARTITION_TERRAIN);
  522. if (part)
  523. {
  524. LLSpatialGroup* group = (LLSpatialGroup*) part->mOctree->getListener(0);
  525. if (LLViewerCamera::getInstance()->AABBInFrustum(group->mBounds[0], group->mBounds[1]))
  526. {
  527. regionp->calculateCameraDistance();
  528. regionp->getLand().updatePatchVisibilities(gAgent);
  529. }
  530. else
  531. {
  532. mVisibleRegionList.erase(curiter);
  533. mCulledRegionList.push_back(regionp);
  534. }
  535. }
  536. }
  537. // Sort visible regions
  538. mVisibleRegionList.sort(LLViewerRegion::CompareDistance());
  539. LLViewerCamera::getInstance()->setFar(cur_far_clip);
  540. }
  541. void LLWorld::updateRegions(F32 max_update_time)
  542. {
  543. LLMemType mt_ur(LLMemType::MTYPE_IDLE_UPDATE_REGIONS);
  544. LLTimer update_timer;
  545. BOOL did_one = FALSE;
  546. // Perform idle time updates for the regions (and associated surfaces)
  547. for (region_list_t::iterator iter = mRegionList.begin();
  548. iter != mRegionList.end(); ++iter)
  549. {
  550. LLViewerRegion* regionp = *iter;
  551. F32 max_time = max_update_time - update_timer.getElapsedTimeF32();
  552. if (did_one && max_time <= 0.f)
  553. break;
  554. max_time = llmin(max_time, max_update_time*.1f);
  555. did_one |= regionp->idleUpdate(max_update_time);
  556. }
  557. }
  558. void LLWorld::updateParticles()
  559. {
  560. LLViewerPartSim::getInstance()->updateSimulation();
  561. }
  562. void LLWorld::renderPropertyLines()
  563. {
  564. S32 region_count = 0;
  565. S32 vertex_count = 0;
  566. for (region_list_t::iterator iter = mVisibleRegionList.begin();
  567. iter != mVisibleRegionList.end(); ++iter)
  568. {
  569. LLViewerRegion* regionp = *iter;
  570. region_count++;
  571. vertex_count += regionp->renderPropertyLines();
  572. }
  573. }
  574. void LLWorld::updateNetStats()
  575. {
  576. F32 bits = 0.f;
  577. U32 packets = 0;
  578. for (region_list_t::iterator iter = mActiveRegionList.begin();
  579. iter != mActiveRegionList.end(); ++iter)
  580. {
  581. LLViewerRegion* regionp = *iter;
  582. regionp->updateNetStats();
  583. bits += regionp->mBitStat.getCurrent();
  584. packets += llfloor( regionp->mPacketsStat.getCurrent() );
  585. }
  586. S32 packets_in = gMessageSystem->mPacketsIn - mLastPacketsIn;
  587. S32 packets_out = gMessageSystem->mPacketsOut - mLastPacketsOut;
  588. S32 packets_lost = gMessageSystem->mDroppedPackets - mLastPacketsLost;
  589. S32 actual_in_bits = gMessageSystem->mPacketRing.getAndResetActualInBits();
  590. S32 actual_out_bits = gMessageSystem->mPacketRing.getAndResetActualOutBits();
  591. LLViewerStats::getInstance()->mActualInKBitStat.addValue(actual_in_bits/1024.f);
  592. LLViewerStats::getInstance()->mActualOutKBitStat.addValue(actual_out_bits/1024.f);
  593. LLViewerStats::getInstance()->mKBitStat.addValue(bits/1024.f);
  594. LLViewerStats::getInstance()->mPacketsInStat.addValue(packets_in);
  595. LLViewerStats::getInstance()->mPacketsOutStat.addValue(packets_out);
  596. LLViewerStats::getInstance()->mPacketsLostStat.addValue(gMessageSystem->mDroppedPackets);
  597. if (packets_in)
  598. {
  599. LLViewerStats::getInstance()->mPacketsLostPercentStat.addValue(100.f*((F32)packets_lost/(F32)packets_in));
  600. }
  601. else
  602. {
  603. LLViewerStats::getInstance()->mPacketsLostPercentStat.addValue(0.f);
  604. }
  605. mLastPacketsIn = gMessageSystem->mPacketsIn;
  606. mLastPacketsOut = gMessageSystem->mPacketsOut;
  607. mLastPacketsLost = gMessageSystem->mDroppedPackets;
  608. }
  609. void LLWorld::printPacketsLost()
  610. {
  611. llinfos << "Simulators:" << llendl;
  612. llinfos << "----------" << llendl;
  613. LLCircuitData *cdp = NULL;
  614. for (region_list_t::iterator iter = mActiveRegionList.begin();
  615. iter != mActiveRegionList.end(); ++iter)
  616. {
  617. LLViewerRegion* regionp = *iter;
  618. cdp = gMessageSystem->mCircuitInfo.findCircuit(regionp->getHost());
  619. if (cdp)
  620. {
  621. LLVector3d range = regionp->getCenterGlobal() - gAgent.getPositionGlobal();
  622. llinfos << regionp->getHost() << ", range: " << range.length()
  623. << " packets lost: " << cdp->getPacketsLost() << llendl;
  624. }
  625. }
  626. }
  627. void LLWorld::processCoarseUpdate(LLMessageSystem* msg, void** user_data)
  628. {
  629. LLViewerRegion* region = LLWorld::getInstance()->getRegion(msg->getSender());
  630. if( region )
  631. {
  632. region->updateCoarseLocations(msg);
  633. }
  634. }
  635. F32 LLWorld::getLandFarClip() const
  636. {
  637. return mLandFarClip;
  638. }
  639. void LLWorld::setLandFarClip(const F32 far_clip)
  640. {
  641. static S32 const rwidth = (S32)REGION_WIDTH_U32;
  642. S32 const n1 = (llceil(mLandFarClip) - 1) / rwidth;
  643. S32 const n2 = (llceil(far_clip) - 1) / rwidth;
  644. bool need_water_objects_update = n1 != n2;
  645. mLandFarClip = far_clip;
  646. if (need_water_objects_update)
  647. {
  648. updateWaterObjects();
  649. }
  650. }
  651. // Some region that we're connected to, but not the one we're in, gave us
  652. // a (possibly) new water height. Update it in our local copy.
  653. void LLWorld::waterHeightRegionInfo(std::string const& sim_name, F32 water_height)
  654. {
  655. for (region_list_t::iterator iter = mRegionList.begin(); iter != mRegionList.end(); ++iter)
  656. {
  657. if ((*iter)->getName() == sim_name)
  658. {
  659. (*iter)->setWaterHeight(water_height);
  660. break;
  661. }
  662. }
  663. }
  664. void LLWorld::updateWaterObjects()
  665. {
  666. if (!gAgent.getRegion())
  667. {
  668. return;
  669. }
  670. if (mRegionList.empty())
  671. {
  672. llwarns << "No regions!" << llendl;
  673. return;
  674. }
  675. // First, determine the min and max "box" of water objects
  676. S32 min_x = 0;
  677. S32 min_y = 0;
  678. S32 max_x = 0;
  679. S32 max_y = 0;
  680. U32 region_x, region_y;
  681. S32 rwidth = 256;
  682. // We only want to fill in water for stuff that's near us, say, within 256 or 512m
  683. S32 range = LLViewerCamera::getInstance()->getFar() > 256.f ? 512 : 256;
  684. LLViewerRegion* regionp = gAgent.getRegion();
  685. from_region_handle(regionp->getHandle(), &region_x, &region_y);
  686. min_x = (S32)region_x - range;
  687. min_y = (S32)region_y - range;
  688. max_x = (S32)region_x + range;
  689. max_y = (S32)region_y + range;
  690. for (region_list_t::iterator iter = mRegionList.begin();
  691. iter != mRegionList.end(); ++iter)
  692. {
  693. LLViewerRegion* regionp = *iter;
  694. LLVOWater* waterp = regionp->getLand().getWaterObj();
  695. if (waterp)
  696. {
  697. gObjectList.updateActive(waterp);
  698. }
  699. }
  700. for (std::list<LLVOWater*>::iterator iter = mHoleWaterObjects.begin();
  701. iter != mHoleWaterObjects.end(); ++ iter)
  702. {
  703. LLVOWater* waterp = *iter;
  704. gObjectList.killObject(waterp);
  705. }
  706. mHoleWaterObjects.clear();
  707. // Now, get a list of the holes
  708. S32 x, y;
  709. F32 water_height = gAgent.getRegion()->getWaterHeight() + 256.f;
  710. for (x = min_x; x <= max_x; x += rwidth)
  711. {
  712. for (y = min_y; y <= max_y; y += rwidth)
  713. {
  714. U64 region_handle = to_region_handle(x, y);
  715. if (!getRegionFromHandle(region_handle))
  716. {
  717. LLVOWater* waterp = (LLVOWater *)gObjectList.createObjectViewer(LLViewerObject::LL_VO_WATER, gAgent.getRegion());
  718. waterp->setUseTexture(FALSE);
  719. waterp->setPositionGlobal(LLVector3d(x + rwidth/2,
  720. y + rwidth/2,
  721. water_height));
  722. waterp->setScale(LLVector3((F32)rwidth, (F32)rwidth, 512.f));
  723. gPipeline.createObject(waterp);
  724. mHoleWaterObjects.push_back(waterp);
  725. }
  726. }
  727. }
  728. // Update edge water objects
  729. S32 wx, wy;
  730. S32 center_x, center_y;
  731. wx = (max_x - min_x) + rwidth;
  732. wy = (max_y - min_y) + rwidth;
  733. center_x = min_x + (wx >> 1);
  734. center_y = min_y + (wy >> 1);
  735. S32 add_boundary[4] = {
  736. 512 - (max_x - region_x),
  737. 512 - (max_y - region_y),
  738. 512 - (region_x - min_x),
  739. 512 - (region_y - min_y) };
  740. S32 dir;
  741. for (dir = 0; dir < 8; dir++)
  742. {
  743. S32 dim[2] = { 0 };
  744. switch (gDirAxes[dir][0])
  745. {
  746. case -1: dim[0] = add_boundary[2]; break;
  747. case 0: dim[0] = wx; break;
  748. default: dim[0] = add_boundary[0]; break;
  749. }
  750. switch (gDirAxes[dir][1])
  751. {
  752. case -1: dim[1] = add_boundary[3]; break;
  753. case 0: dim[1] = wy; break;
  754. default: dim[1] = add_boundary[1]; break;
  755. }
  756. // Resize and reshape the water objects
  757. const S32 water_center_x = center_x + llround((wx + dim[0]) * 0.5f * gDirAxes[dir][0]);
  758. const S32 water_center_y = center_y + llround((wy + dim[1]) * 0.5f * gDirAxes[dir][1]);
  759. LLVOWater* waterp = mEdgeWaterObjects[dir];
  760. if (!waterp || waterp->isDead())
  761. {
  762. // The edge water objects can be dead because they're attached to the region that the
  763. // agent was in when they were originally created.
  764. mEdgeWaterObjects[dir] = (LLVOWater *)gObjectList.createObjectViewer(LLViewerObject::LL_VO_VOID_WATER,
  765. gAgent.getRegion());
  766. waterp = mEdgeWaterObjects[dir];
  767. waterp->setUseTexture(FALSE);
  768. waterp->setIsEdgePatch(TRUE);
  769. gPipeline.createObject(waterp);
  770. }
  771. waterp->setRegion(gAgent.getRegion());
  772. LLVector3d water_pos(water_center_x, water_center_y, water_height) ;
  773. LLVector3 water_scale((F32) dim[0], (F32) dim[1], 512.f);
  774. //stretch out to horizon
  775. water_scale.mV[0] += fabsf(2048.f * gDirAxes[dir][0]);
  776. water_scale.mV[1] += fabsf(2048.f * gDirAxes[dir][1]);
  777. water_pos.mdV[0] += 1024.f * gDirAxes[dir][0];
  778. water_pos.mdV[1] += 1024.f * gDirAxes[dir][1];
  779. waterp->setPositionGlobal(water_pos);
  780. waterp->setScale(water_scale);
  781. gObjectList.updateActive(waterp);
  782. }
  783. }
  784. void LLWorld::shiftRegions(const LLVector3& offset)
  785. {
  786. for (region_list_t::const_iterator i = getRegionList().begin(); i != getRegionList().end(); ++i)
  787. {
  788. LLViewerRegion* region = *i;
  789. region->updateRenderMatrix();
  790. }
  791. LLViewerPartSim::getInstance()->shift(offset);
  792. }
  793. LLViewerTexture* LLWorld::getDefaultWaterTexture()
  794. {
  795. return mDefaultWaterTexturep;
  796. }
  797. void LLWorld::setSpaceTimeUSec(const U64 space_time_usec)
  798. {
  799. mSpaceTimeUSec = space_time_usec;
  800. }
  801. U64 LLWorld::getSpaceTimeUSec() const
  802. {
  803. return mSpaceTimeUSec;
  804. }
  805. void LLWorld::requestCacheMisses()
  806. {
  807. for (region_list_t::iterator iter = mRegionList.begin();
  808. iter != mRegionList.end(); ++iter)
  809. {
  810. LLViewerRegion* regionp = *iter;
  811. regionp->requestCacheMisses();
  812. }
  813. }
  814. void LLWorld::getInfo(LLSD& info)
  815. {
  816. LLSD region_info;
  817. for (region_list_t::iterator iter = mRegionList.begin();
  818. iter != mRegionList.end(); ++iter)
  819. {
  820. LLViewerRegion* regionp = *iter;
  821. regionp->getInfo(region_info);
  822. info["World"].append(region_info);
  823. }
  824. }
  825. void LLWorld::disconnectRegions()
  826. {
  827. LLMessageSystem* msg = gMessageSystem;
  828. for (region_list_t::iterator iter = mRegionList.begin();
  829. iter != mRegionList.end(); ++iter)
  830. {
  831. LLViewerRegion* regionp = *iter;
  832. if (regionp == gAgent.getRegion())
  833. {
  834. // Skip the main agent
  835. continue;
  836. }
  837. llinfos << "Sending AgentQuitCopy to: " << regionp->getHost() << llendl;
  838. msg->newMessageFast(_PREHASH_AgentQuitCopy);
  839. msg->nextBlockFast(_PREHASH_AgentData);
  840. msg->addUUIDFast(_PREHASH_AgentID, gAgent.getID());
  841. msg->addUUIDFast(_PREHASH_SessionID, gAgent.getSessionID());
  842. msg->nextBlockFast(_PREHASH_FuseBlock);
  843. msg->addU32Fast(_PREHASH_ViewerCircuitCode, gMessageSystem->mOurCircuitCode);
  844. msg->sendMessage(regionp->getHost());
  845. }
  846. }
  847. static LLFastTimer::DeclareTimer FTM_ENABLE_SIMULATOR("Enable Sim");
  848. void process_enable_simulator(LLMessageSystem *msg, void **user_data)
  849. {
  850. LLFastTimer t(FTM_ENABLE_SIMULATOR);
  851. // enable the appropriate circuit for this simulator and
  852. // add its values into the gSimulator structure
  853. U64 handle;
  854. U32 ip_u32;
  855. U16 port;
  856. msg->getU64Fast(_PREHASH_SimulatorInfo, _PREHASH_Handle, handle);
  857. msg->getIPAddrFast(_PREHASH_SimulatorInfo, _PREHASH_IP, ip_u32);
  858. msg->getIPPortFast(_PREHASH_SimulatorInfo, _PREHASH_Port, port);
  859. // which simulator should we modify?
  860. LLHost sim(ip_u32, port);
  861. // Viewer trusts the simulator.
  862. msg->enableCircuit(sim, TRUE);
  863. LLWorld::getInstance()->addRegion(handle, sim);
  864. // give the simulator a message it can use to get ip and port
  865. llinfos << "simulator_enable() Enabling " << sim << " with code " << msg->getOurCircuitCode() << llendl;
  866. msg->newMessageFast(_PREHASH_UseCircuitCode);
  867. msg->nextBlockFast(_PREHASH_CircuitCode);
  868. msg->addU32Fast(_PREHASH_Code, msg->getOurCircuitCode());
  869. msg->addUUIDFast(_PREHASH_SessionID, gAgent.getSessionID());
  870. msg->addUUIDFast(_PREHASH_ID, gAgent.getID());
  871. msg->sendReliable(sim);
  872. }
  873. class LLEstablishAgentCommunication : public LLHTTPNode
  874. {
  875. LOG_CLASS(LLEstablishAgentCommunication);
  876. public:
  877. virtual void describe(Description& desc) const
  878. {
  879. desc.shortInfo("seed capability info for a region");
  880. desc.postAPI();
  881. desc.input(
  882. "{ seed-capability: ..., sim-ip: ..., sim-port }");
  883. desc.source(__FILE__, __LINE__);
  884. }
  885. virtual void post(ResponsePtr response, const LLSD& context, const LLSD& input) const
  886. {
  887. if (!input["body"].has("agent-id") ||
  888. !input["body"].has("sim-ip-and-port") ||
  889. !input["body"].has("seed-capability"))
  890. {
  891. llwarns << "invalid parameters" << llendl;
  892. return;
  893. }
  894. LLHost sim(input["body"]["sim-ip-and-port"].asString());
  895. LLViewerRegion* regionp = LLWorld::getInstance()->getRegion(sim);
  896. if (!regionp)
  897. {
  898. llwarns << "Got EstablishAgentCommunication for unknown region "
  899. << sim << llendl;
  900. return;
  901. }
  902. regionp->setSeedCapability(input["body"]["seed-capability"]);
  903. }
  904. };
  905. // disable the circuit to this simulator
  906. // Called in response to "DisableSimulator" message.
  907. void process_disable_simulator(LLMessageSystem *mesgsys, void **user_data)
  908. {
  909. LLHost host = mesgsys->getSender();
  910. //llinfos << "Disabling simulator with message from " << host << llendl;
  911. LLWorld::getInstance()->removeRegion(host);
  912. mesgsys->disableCircuit(host);
  913. }
  914. void process_region_handshake(LLMessageSystem* msg, void** user_data)
  915. {
  916. LLHost host = msg->getSender();
  917. LLViewerRegion* regionp = LLWorld::getInstance()->getRegion(host);
  918. if (!regionp)
  919. {
  920. llwarns << "Got region handshake for unknown region "
  921. << host << llendl;
  922. return;
  923. }
  924. regionp->unpackRegionHandshake();
  925. }
  926. void send_agent_pause()
  927. {
  928. // *NOTE:Mani Pausing the mainloop timeout. Otherwise a long modal event may cause
  929. // the thread monitor to timeout.
  930. LLAppViewer::instance()->pauseMainloopTimeout();
  931. // Note: used to check for LLWorld initialization before it became a singleton.
  932. // Rather than just remove this check I'm changing it to assure that the message
  933. // system has been initialized. -MG
  934. if (!gMessageSystem)
  935. {
  936. return;
  937. }
  938. gMessageSystem->newMessageFast(_PREHASH_AgentPause);
  939. gMessageSystem->nextBlockFast(_PREHASH_AgentData);
  940. gMessageSystem->addUUIDFast(_PREHASH_AgentID, gAgentID);
  941. gMessageSystem->addUUIDFast(_PREHASH_SessionID, gAgentSessionID);
  942. gAgentPauseSerialNum++;
  943. gMessageSystem->addU32Fast(_PREHASH_SerialNum, gAgentPauseSerialNum);
  944. for (LLWorld::region_list_t::const_iterator iter = LLWorld::getInstance()->getRegionList().begin();
  945. iter != LLWorld::getInstance()->getRegionList().end(); ++iter)
  946. {
  947. LLViewerRegion* regionp = *iter;
  948. gMessageSystem->sendReliable(regionp->getHost());
  949. }
  950. gObjectList.mWasPaused = TRUE;
  951. }
  952. void send_agent_resume()
  953. {
  954. // Note: used to check for LLWorld initialization before it became a singleton.
  955. // Rather than just remove this check I'm changing it to assure that the message
  956. // system has been initialized. -MG
  957. if (!gMessageSystem)
  958. {
  959. return;
  960. }
  961. gMessageSystem->newMessageFast(_PREHASH_AgentResume);
  962. gMessageSystem->nextBlockFast(_PREHASH_AgentData);
  963. gMessageSystem->addUUIDFast(_PREHASH_AgentID, gAgentID);
  964. gMessageSystem->addUUIDFast(_PREHASH_SessionID, gAgentSessionID);
  965. gAgentPauseSerialNum++;
  966. gMessageSystem->addU32Fast(_PREHASH_SerialNum, gAgentPauseSerialNum);
  967. for (LLWorld::region_list_t::const_iterator iter = LLWorld::getInstance()->getRegionList().begin();
  968. iter != LLWorld::getInstance()->getRegionList().end(); ++iter)
  969. {
  970. LLViewerRegion* regionp = *iter;
  971. gMessageSystem->sendReliable(regionp->getHost());
  972. }
  973. // Reset the FPS counter to avoid an invalid fps
  974. LLViewerStats::getInstance()->mFPSStat.start();
  975. LLAppViewer::instance()->resumeMainloopTimeout();
  976. }
  977. static LLVector3d unpackLocalToGlobalPosition(U32 compact_local, const LLVector3d& region_origin)
  978. {
  979. LLVector3d pos_global;
  980. LLVector3 pos_local;
  981. U8 bits;
  982. bits = compact_local & 0xFF;
  983. pos_local.mV[VZ] = F32(bits) * 4.f;
  984. compact_local >>= 8;
  985. bits = compact_local & 0xFF;
  986. pos_local.mV[VY] = (F32)bits;
  987. compact_local >>= 8;
  988. bits = compact_local & 0xFF;
  989. pos_local.mV[VX] = (F32)bits;
  990. pos_global.setVec( pos_local );
  991. pos_global += region_origin;
  992. return pos_global;
  993. }
  994. void LLWorld::getAvatars(uuid_vec_t* avatar_ids, std::vector<LLVector3d>* positions, const LLVector3d& relative_to, F32 radius) const
  995. {
  996. F32 radius_squared = radius * radius;
  997. if(avatar_ids != NULL)
  998. {
  999. avatar_ids->clear();
  1000. }
  1001. if(positions != NULL)
  1002. {
  1003. positions->clear();
  1004. }
  1005. // get the list of avatars from the character list first, so distances are correct
  1006. // when agent is above 1020m and other avatars are nearby
  1007. for (std::vector<LLCharacter*>::iterator iter = LLCharacter::sInstances.begin();
  1008. iter != LLCharacter::sInstances.end(); ++iter)
  1009. {
  1010. LLVOAvatar* pVOAvatar = (LLVOAvatar*) *iter;
  1011. if(!pVOAvatar->isDead() && !pVOAvatar->isSelf())
  1012. {
  1013. LLUUID uuid = pVOAvatar->getID();
  1014. if(!uuid.isNull())
  1015. {
  1016. LLVector3d pos_global = pVOAvatar->getPositionGlobal();
  1017. if(dist_vec_squared(pos_global, relative_to) <= radius_squared)
  1018. {
  1019. if(positions != NULL)
  1020. {
  1021. positions->push_back(pos_global);
  1022. }
  1023. if(avatar_ids !=NULL)
  1024. {
  1025. avatar_ids->push_back(uuid);
  1026. }
  1027. }
  1028. }
  1029. }
  1030. }
  1031. // region avatars added for situations where radius is greater than RenderFarClip
  1032. for (LLWorld::region_list_t::const_iterator iter = LLWorld::getInstance()->getRegionList().begin();
  1033. iter != LLWorld::getInstance()->getRegionList().end(); ++iter)
  1034. {
  1035. LLViewerRegion* regionp = *iter;
  1036. const LLVector3d& origin_global = regionp->getOriginGlobal();
  1037. S32 count = regionp->mMapAvatars.count();
  1038. for (S32 i = 0; i < count; i++)
  1039. {
  1040. LLVector3d pos_global = unpackLocalToGlobalPosition(regionp->mMapAvatars.get(i), origin_global);
  1041. if(dist_vec_squared(pos_global, relative_to) <= radius_squared)
  1042. {
  1043. LLUUID uuid = regionp->mMapAvatarIDs.get(i);
  1044. // if this avatar doesn't already exist in the list, add it
  1045. if(uuid.notNull() && avatar_ids!=NULL && std::find(avatar_ids->begin(), avatar_ids->end(), uuid) == avatar_ids->end())
  1046. {
  1047. if(positions != NULL)
  1048. {
  1049. positions->push_back(pos_global);
  1050. }
  1051. avatar_ids->push_back(uuid);
  1052. }
  1053. }
  1054. }
  1055. }
  1056. }
  1057. LLHTTPRegistration<LLEstablishAgentCommunication>
  1058. gHTTPRegistrationEstablishAgentCommunication(
  1059. "/message/EstablishAgentCommunication");