llworld.cpp 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671
  1. /**
  2. * @file llworld.cpp
  3. * @brief Initial test structure to organize viewer regions
  4. *
  5. * $LicenseInfo:firstyear=2001&license=viewergpl$
  6. *
  7. * Copyright (c) 2001-2009, 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 <queue>
  34. #include "llworld.h"
  35. #include "llcorehttplibcurl.h"
  36. #include "llglheaders.h"
  37. #include "llhttpnode.h"
  38. #include "llregionhandle.h"
  39. #include "llrender.h"
  40. #include "llmessage.h"
  41. #include "llagent.h"
  42. #include "llappviewer.h"
  43. #include "lldrawpool.h"
  44. #include "llgridmanager.h"
  45. #include "llpipeline.h"
  46. #include "llsky.h" // For gSky.cleanup()
  47. #include "llsurface.h"
  48. #include "llsurfacepatch.h"
  49. #include "llviewercamera.h"
  50. #include "llviewercontrol.h"
  51. #include "llviewerobjectlist.h"
  52. #include "llviewerparceloverlay.h"
  53. #include "llviewerregion.h"
  54. #include "llviewerstats.h"
  55. #include "llviewertexturelist.h"
  56. #include "llvlcomposition.h"
  57. #include "llvoavatar.h"
  58. #include "llvowater.h"
  59. //
  60. // Globals
  61. //
  62. LLWorld gWorld;
  63. U32 gAgentPauseSerialNum = 0;
  64. // Magnitude along the x and y axis
  65. const S32 gDirAxes[8][2] =
  66. {
  67. { 1, 0 }, // East
  68. { 0, 1 }, // North
  69. { -1, 0 }, // West
  70. { 0, -1 }, // South
  71. { 1, 1 }, // NE
  72. { -1, 1 }, // NW
  73. { -1, -1 }, // SW
  74. { 1, -1 }, // SE
  75. };
  76. constexpr S32 WORLD_PATCH_SIZE = 16;
  77. //
  78. // Functions
  79. //
  80. // allocate the stack
  81. LLWorld::LLWorld()
  82. : mLandFarClip(DEFAULT_FAR_PLANE),
  83. mLastPacketsIn(0),
  84. mLastPacketsOut(0),
  85. mLastPacketsLost(0),
  86. mLastCurlBytes(0),
  87. mLastRegionDisabling(0.f)
  88. {
  89. for (S32 i = 0; i < EDGE_WATER_OBJECTS_COUNT; ++i)
  90. {
  91. mEdgeWaterObjects[i] = NULL;
  92. }
  93. }
  94. void LLWorld::initClass()
  95. {
  96. LLPointer<LLImageRaw> raw = new LLImageRaw(1, 1, 4);
  97. U8* default_texture = raw->getData();
  98. if (default_texture)
  99. {
  100. *(default_texture++) = MAX_WATER_COLOR.mV[0];
  101. *(default_texture++) = MAX_WATER_COLOR.mV[1];
  102. *(default_texture++) = MAX_WATER_COLOR.mV[2];
  103. *(default_texture++) = MAX_WATER_COLOR.mV[3];
  104. }
  105. mDefaultWaterTexturep = LLViewerTextureManager::getLocalTexture(raw.get(),
  106. false);
  107. if (mDefaultWaterTexturep)
  108. {
  109. gGL.getTexUnit(0)->bind(mDefaultWaterTexturep);
  110. mDefaultWaterTexturep->setAddressMode(LLTexUnit::TAM_CLAMP);
  111. }
  112. LLViewerRegion::sVOCacheCullingEnabled =
  113. gSavedSettings.getBool("RequestFullRegionCache");
  114. gViewerPartSim.initClass();
  115. llinfos << "World class initialized" << llendl;
  116. }
  117. void LLWorld::cleanupClass()
  118. {
  119. llinfos << "Shutting down the World class..." << llendl;
  120. gObjectList.cleanupClass();
  121. gSky.cleanup();
  122. llinfos << "Removing regions..." << llendl;
  123. for (region_list_t::iterator region_it = mRegionList.begin();
  124. region_it != mRegionList.end(); )
  125. {
  126. LLViewerRegion* region_to_delete = *region_it++;
  127. removeRegion(region_to_delete->getHost());
  128. }
  129. mRegionList.clear();
  130. mActiveRegionList.clear();
  131. mVisibleRegionList.clear();
  132. mCulledRegionList.clear();
  133. mDisabledRegionList.clear();
  134. gViewerPartSim.cleanupClass();
  135. llinfos << "Removing water edges..." << llendl;
  136. mDefaultWaterTexturep = NULL;
  137. for (S32 i = 0; i < EDGE_WATER_OBJECTS_COUNT; ++i)
  138. {
  139. mEdgeWaterObjects[i] = NULL;
  140. }
  141. // Make all visible drawables invisible.
  142. LLDrawable::incrementVisible();
  143. llinfos << "World class shut down." << llendl;
  144. }
  145. LLViewerRegion* LLWorld::addRegion(const U64& region_handle,
  146. const LLHost& host, U32 width)
  147. {
  148. LLViewerRegion* regionp = getRegionFromHandle(region_handle);
  149. std::string seed_url;
  150. if (regionp)
  151. {
  152. LLHost old_host = regionp->getHost();
  153. // region already exists!
  154. if (host == old_host && regionp->isAlive())
  155. {
  156. // This is a duplicate for the same host and it is alive, do not
  157. // bother.
  158. llinfos << "Region already exists and is alive, using existing region"
  159. << llendl;
  160. mDisabledRegionList.remove(regionp); // cancel any delayed removal
  161. return regionp;
  162. }
  163. if (host != old_host)
  164. {
  165. llwarns << "Region exists, but old host " << old_host
  166. << " does not match new host " << host
  167. << ". Removing old region and creating a new one."
  168. << llendl;
  169. }
  170. if (!regionp->isAlive())
  171. {
  172. llinfos << "Region exists, but is no more alive. Removing old region and creating a new one."
  173. << llendl;
  174. }
  175. // Save capabilities seed URL
  176. seed_url = regionp->getCapability("Seed");
  177. // Kill the old host, and then we can continue on and add the new host.
  178. // We have to kill even if the host matches, because all the agent
  179. // state for the new camera is completely different.
  180. removeRegion(old_host);
  181. }
  182. else
  183. {
  184. LL_DEBUGS("World") << "Region does not exist, creating a new one."
  185. << LL_ENDL;
  186. }
  187. U32 iindex = 0;
  188. U32 jindex = 0;
  189. from_region_handle(region_handle, &iindex, &jindex);
  190. #if 1 // Variable region size support... Unintuitive to say the least...
  191. S32 x = (S32)(iindex / 256); // MegaRegion
  192. S32 y = (S32)(jindex / 256); // MegaRegion
  193. #else
  194. S32 x = (S32)(iindex / width);
  195. S32 y = (S32)(jindex / width);
  196. #endif
  197. llinfos << "Adding new region (" << x << ":" << y << ") on host: " << host
  198. << " - Width: " << width << "m." << llendl;
  199. LLVector3d origin_global;
  200. origin_global = from_region_handle(region_handle);
  201. regionp = new LLViewerRegion(region_handle, host, width, WORLD_PATCH_SIZE,
  202. width);
  203. if (!regionp)
  204. {
  205. llerrs << "Unable to create new region !" << llendl;
  206. }
  207. if (!seed_url.empty())
  208. {
  209. regionp->setCapability("Seed", seed_url);
  210. }
  211. mRegionList.push_back(regionp);
  212. mActiveRegionList.push_back(regionp);
  213. mCulledRegionList.push_back(regionp);
  214. mDisabledRegionList.remove(regionp); // cancel any delayed removal
  215. // Find all the adjacent regions, and attach them in the correct way.
  216. F32 region_x = 0.f;
  217. F32 region_y = 0.f;
  218. from_region_handle(region_handle, &region_x, &region_y);
  219. // Iterate through all directions, and connect neighbors if there.
  220. U64 adj_handle = 0;
  221. for (S32 dir = 0; dir < 8; ++dir)
  222. {
  223. F32 adj_x = region_x + (F32)width * gDirAxes[dir][0];
  224. F32 adj_y = region_y + (F32)width * gDirAxes[dir][1];
  225. to_region_handle(adj_x, adj_y, &adj_handle);
  226. LLViewerRegion* neighborp = getRegionFromHandle(adj_handle);
  227. if (neighborp)
  228. {
  229. LL_DEBUGS("World") << "Connecting " << region_x << ":"
  230. << region_y << " -> " << adj_x << ":" << adj_y
  231. << LL_ENDL;
  232. regionp->connectNeighbor(neighborp, dir);
  233. }
  234. }
  235. updateWaterObjects();
  236. return regionp;
  237. }
  238. void LLWorld::removeRegion(const LLHost& host)
  239. {
  240. LLViewerRegion* regionp = getRegion(host);
  241. if (!regionp)
  242. {
  243. llwarns << "Trying to remove region that does not exist !" << llendl;
  244. return;
  245. }
  246. if (regionp == gAgent.getRegion())
  247. {
  248. llwarns << "Disabling agent region: " << regionp->getName()
  249. << " - Agent positions: global = "
  250. << gAgent.getPositionGlobal() << " / agent = "
  251. << gAgent.getPositionAgent() << " - Regions visited: "
  252. << gAgent.getRegionsVisited() << "\nRegions dump:";
  253. for (region_list_t::iterator iter = mRegionList.begin();
  254. iter != mRegionList.end(); ++iter)
  255. {
  256. LLViewerRegion* reg = *iter;
  257. llcont << "\nRegion: " << reg->getName() << " " << reg->getHost()
  258. << " " << reg->getOriginGlobal();
  259. }
  260. llcont << llendl;
  261. // *TODO: translate
  262. gAppViewerp->forceDisconnect("You have been disconnected from the region you were in.");
  263. return;
  264. }
  265. F32 x, y;
  266. from_region_handle(regionp->getHandle(), &x, &y);
  267. llinfos << "Removing region at " << x << ":" << y << " ("
  268. << regionp->getIdentity() << ")" << llendl;
  269. mRegionList.remove(regionp);
  270. mActiveRegionList.remove(regionp);
  271. mCulledRegionList.remove(regionp);
  272. mVisibleRegionList.remove(regionp);
  273. mDisabledRegionList.remove(regionp);
  274. // Remove all objects in this region from the mapped objects list. Note
  275. // that this is normally automatically done whenever the objects get killed
  276. // via ~LLViewerRegion(), but better safe than sorry. Also, I moved this
  277. // call before the region is destroyed (avoids a "use after free" warning
  278. // by gcc v12+) and changed the method itself (made it simpler and more
  279. // efficient). HB
  280. gObjectList.clearAllMapObjectsInRegion(regionp);
  281. mRegionRemovedSignal(regionp);
  282. // We can now safely destroy the region.
  283. delete regionp;
  284. updateWaterObjects();
  285. }
  286. LLViewerRegion* LLWorld::getRegion(const LLHost& host)
  287. {
  288. for (region_list_t::iterator iter = mRegionList.begin(),
  289. end = mRegionList.end();
  290. iter != end; ++iter)
  291. {
  292. LLViewerRegion* regionp = *iter;
  293. if (regionp->getHost() == host)
  294. {
  295. return regionp;
  296. }
  297. }
  298. return NULL;
  299. }
  300. LLViewerRegion* LLWorld::getRegionFromPosAgent(const LLVector3& pos)
  301. {
  302. return getRegionFromPosGlobal(gAgent.getPosGlobalFromAgent(pos));
  303. }
  304. LLViewerRegion* LLWorld::getRegionFromPosGlobal(const LLVector3d& pos)
  305. {
  306. for (region_list_t::iterator iter = mRegionList.begin(),
  307. end = mRegionList.end();
  308. iter != end; ++iter)
  309. {
  310. LLViewerRegion* regionp = *iter;
  311. if (regionp->pointInRegionGlobal(pos))
  312. {
  313. return regionp;
  314. }
  315. }
  316. return NULL;
  317. }
  318. LLVector3d LLWorld::clipToVisibleRegions(const LLVector3d& start_pos,
  319. const LLVector3d& end_pos)
  320. {
  321. if (positionRegionValidGlobal(end_pos))
  322. {
  323. return end_pos;
  324. }
  325. LLViewerRegion* regionp = getRegionFromPosGlobal(start_pos);
  326. if (!regionp)
  327. {
  328. return start_pos;
  329. }
  330. LLVector3d delta_pos = end_pos - start_pos;
  331. LLVector3d delta_pos_abs;
  332. delta_pos_abs.set(delta_pos);
  333. delta_pos_abs.abs();
  334. LLVector3 region_coord = regionp->getPosRegionFromGlobal(end_pos);
  335. F64 clip_factor = 1.0;
  336. F32 region_width = regionp->getWidth();
  337. if (region_coord.mV[VX] < 0.f)
  338. {
  339. if (region_coord.mV[VY] < region_coord.mV[VX])
  340. {
  341. // Clip along y -
  342. clip_factor = -(region_coord.mV[VY] / delta_pos_abs.mdV[VY]);
  343. }
  344. else
  345. {
  346. // Clip along x -
  347. clip_factor = -(region_coord.mV[VX] / delta_pos_abs.mdV[VX]);
  348. }
  349. }
  350. else if (region_coord.mV[VX] > region_width)
  351. {
  352. if (region_coord.mV[VY] > region_coord.mV[VX])
  353. {
  354. // Clip along y +
  355. clip_factor = (region_coord.mV[VY] - region_width) /
  356. delta_pos_abs.mdV[VY];
  357. }
  358. else
  359. {
  360. //Clip along x +
  361. clip_factor = (region_coord.mV[VX] - region_width) /
  362. delta_pos_abs.mdV[VX];
  363. }
  364. }
  365. else if (region_coord.mV[VY] < 0.f)
  366. {
  367. // Clip along y -
  368. clip_factor = -(region_coord.mV[VY] / delta_pos_abs.mdV[VY]);
  369. }
  370. else if (region_coord.mV[VY] > region_width)
  371. {
  372. // Clip along y +
  373. clip_factor = (region_coord.mV[VY] - region_width) /
  374. delta_pos_abs.mdV[VY];
  375. }
  376. // Clamp to within region dimensions
  377. LLVector3d final_region_pos = LLVector3d(region_coord) -
  378. delta_pos * clip_factor;
  379. final_region_pos.mdV[VX] = llclamp(final_region_pos.mdV[VX], 0.0,
  380. (F64)(region_width - F_ALMOST_ZERO));
  381. final_region_pos.mdV[VY] = llclamp(final_region_pos.mdV[VY], 0.0,
  382. (F64)(region_width - F_ALMOST_ZERO));
  383. final_region_pos.mdV[VZ] = llclamp(final_region_pos.mdV[VZ], 0.0,
  384. (F64)(MAX_OBJECT_Z - F_ALMOST_ZERO));
  385. return regionp->getPosGlobalFromRegion(LLVector3(final_region_pos));
  386. }
  387. LLViewerRegion* LLWorld::getRegionFromHandle(const U64& handle)
  388. {
  389. // Variable region size support
  390. U32 x, y;
  391. from_region_handle(handle, &x, &y);
  392. for (region_list_t::iterator iter = mRegionList.begin(),
  393. end = mRegionList.end();
  394. iter != end; ++iter)
  395. {
  396. LLViewerRegion* regionp = *iter;
  397. #if 1 // Variable region size support
  398. U32 tw = (U32)regionp->getWidth();
  399. U32 tx, ty;
  400. from_region_handle(regionp->getHandle(), &tx, &ty);
  401. if (x >= tx && x < tx + tw &&
  402. y >= ty && y < ty + tw)
  403. #else
  404. if (regionp->getHandle() == handle)
  405. #endif
  406. {
  407. return regionp;
  408. }
  409. }
  410. return NULL;
  411. }
  412. LLViewerRegion* LLWorld::getRegionFromID(const LLUUID& region_id)
  413. {
  414. for (region_list_t::iterator iter = mRegionList.begin(),
  415. end = mRegionList.end();
  416. iter != end; ++iter)
  417. {
  418. LLViewerRegion* regionp = *iter;
  419. if (regionp->getRegionID() == region_id)
  420. {
  421. return regionp;
  422. }
  423. }
  424. return NULL;
  425. }
  426. bool LLWorld::positionRegionValidGlobal(const LLVector3d& pos_global)
  427. {
  428. for (region_list_t::iterator iter = mRegionList.begin(),
  429. end = mRegionList.end();
  430. iter != end; ++iter)
  431. {
  432. LLViewerRegion* regionp = *iter;
  433. if (regionp->pointInRegionGlobal(pos_global))
  434. {
  435. return true;
  436. }
  437. }
  438. return false;
  439. }
  440. // Allow objects to go up to their radius underground.
  441. F32 LLWorld::getMinAllowedZ(LLViewerObject* object)
  442. {
  443. F32 land_height = resolveLandHeightGlobal(object->getPositionGlobal());
  444. F32 radius = 0.5f * object->getScale().length();
  445. return land_height - radius;
  446. }
  447. F32 LLWorld::getMinAllowedZ(LLViewerObject* object,
  448. const LLVector3d& global_pos)
  449. {
  450. F32 land_height = resolveLandHeightGlobal(global_pos);
  451. F32 radius = 0.5f * object->getScale().length();
  452. return land_height - radius;
  453. }
  454. LLViewerRegion* LLWorld::resolveRegionGlobal(LLVector3& pos_region,
  455. const LLVector3d& pos_global)
  456. {
  457. LLViewerRegion* regionp = getRegionFromPosGlobal(pos_global);
  458. if (regionp)
  459. {
  460. pos_region = regionp->getPosRegionFromGlobal(pos_global);
  461. return regionp;
  462. }
  463. return NULL;
  464. }
  465. LLViewerRegion* LLWorld::resolveRegionAgent(LLVector3& pos_region,
  466. const LLVector3& pos_agent)
  467. {
  468. LLVector3d pos_global = gAgent.getPosGlobalFromAgent(pos_agent);
  469. LLViewerRegion* regionp = getRegionFromPosGlobal(pos_global);
  470. if (regionp)
  471. {
  472. pos_region = regionp->getPosRegionFromGlobal(pos_global);
  473. return regionp;
  474. }
  475. return NULL;
  476. }
  477. F32 LLWorld::resolveLandHeightAgent(const LLVector3& pos_agent)
  478. {
  479. LLVector3d pos_global = gAgent.getPosGlobalFromAgent(pos_agent);
  480. return resolveLandHeightGlobal(pos_global);
  481. }
  482. F32 LLWorld::resolveLandHeightGlobal(const LLVector3d& pos_global)
  483. {
  484. LLViewerRegion* regionp = getRegionFromPosGlobal(pos_global);
  485. if (regionp)
  486. {
  487. return regionp->getLand().resolveHeightGlobal(pos_global);
  488. }
  489. return 0.f;
  490. }
  491. // Takes a line defined by "pt_a" and "pt_b" and determines the closest
  492. // (to pt_a) point where the the line intersects an object or the land
  493. // surface. Stores the results in "intersection" and "intersect_norm" and
  494. // returns a scalar value that represents the normalized distance along the
  495. // line from "pt_a" to "intersection".
  496. //
  497. // Currently assumes pt_a and pt_b only differ in z-direction, but it may
  498. // eventually become more general.
  499. F32 LLWorld::resolveStepHeightGlobal(const LLVOAvatar* avatarp,
  500. const LLVector3d& pt_a,
  501. const LLVector3d& pt_b,
  502. LLVector3d& intersection,
  503. LLVector3& intersect_norm,
  504. LLViewerObject** vobjp)
  505. {
  506. // Initialize return value to null
  507. if (vobjp)
  508. {
  509. *vobjp = NULL;
  510. }
  511. LLViewerRegion* regionp = getRegionFromPosGlobal(pt_a);
  512. if (!regionp)
  513. {
  514. // We are outside the world
  515. intersection = 0.5f * (pt_a + pt_b);
  516. intersect_norm.set(0.f, 0.f, 1.f);
  517. return 0.5f;
  518. }
  519. // Calculate the length of the segment
  520. F32 segment_len = (F32)((pt_a - pt_b).length());
  521. if (segment_len == 0.f)
  522. {
  523. intersection = pt_a;
  524. intersect_norm.set(0.f, 0.f, 1.f);
  525. return segment_len;
  526. }
  527. // Get the land height. Note: we assume that the line is parallel to z-axis
  528. // here
  529. LLVector3d land_intersection = pt_a;
  530. land_intersection.mdV[VZ] = regionp->getLand().resolveHeightGlobal(pt_a);
  531. F32 normalized_land_dist = (F32)(pt_a.mdV[VZ] -
  532. land_intersection.mdV[VZ]) / segment_len;
  533. intersection = land_intersection;
  534. intersect_norm = resolveLandNormalGlobal(land_intersection);
  535. if (avatarp && !avatarp->mFootPlane.isExactlyClear())
  536. {
  537. LLVector3 foot_plane_normal(avatarp->mFootPlane.mV);
  538. LLVector3 start_pt =
  539. avatarp->getRegion()->getPosRegionFromGlobal(pt_a);
  540. // Added 0.05 meters to compensate for error in foot plane reported by
  541. // Havok
  542. F32 norm_dist_from_plane = start_pt * foot_plane_normal -
  543. avatarp->mFootPlane.mV[VW] + 0.05f;
  544. norm_dist_from_plane = llclamp(norm_dist_from_plane / segment_len,
  545. 0.f, 1.f);
  546. if (norm_dist_from_plane < normalized_land_dist)
  547. {
  548. // collided with object before land
  549. normalized_land_dist = norm_dist_from_plane;
  550. intersection = pt_a;
  551. intersection.mdV[VZ] -= norm_dist_from_plane * segment_len;
  552. intersect_norm = foot_plane_normal;
  553. }
  554. else
  555. {
  556. intersection = land_intersection;
  557. intersect_norm = resolveLandNormalGlobal(land_intersection);
  558. }
  559. }
  560. return normalized_land_dist;
  561. }
  562. // Returns a pointer to the patch at this location
  563. LLSurfacePatch* LLWorld::resolveLandPatchGlobal(const LLVector3d& pos_global)
  564. {
  565. LLViewerRegion* regionp = getRegionFromPosGlobal(pos_global);
  566. return regionp ? regionp->getLand().resolvePatchGlobal(pos_global) : NULL;
  567. }
  568. LLVector3 LLWorld::resolveLandNormalGlobal(const LLVector3d& pos_global)
  569. {
  570. LLViewerRegion* regionp = getRegionFromPosGlobal(pos_global);
  571. return regionp ? regionp->getLand().resolveNormalGlobal(pos_global)
  572. : LLVector3::z_axis;
  573. }
  574. void LLWorld::updateVisibilities()
  575. {
  576. F32 cur_far_clip = gViewerCamera.getFar();
  577. gViewerCamera.setFar(mLandFarClip);
  578. // Go through the culled list and check for visible regions
  579. for (region_list_t::iterator iter = mCulledRegionList.begin(),
  580. end = mCulledRegionList.end();
  581. iter != end; )
  582. {
  583. region_list_t::iterator curiter = iter++;
  584. LLViewerRegion* regionp = *curiter;
  585. LLSpatialPartition* part =
  586. regionp->getSpatialPartition(LLViewerRegion::PARTITION_TERRAIN);
  587. // PARTITION_TERRAIN cannot be NULL
  588. llassert(part);
  589. LLSpatialGroup* group = (LLSpatialGroup*)part->mOctree->getListener(0);
  590. const LLVector4a* bounds = group->getBounds();
  591. if (gViewerCamera.AABBInFrustum(bounds[0], bounds[1]))
  592. {
  593. mCulledRegionList.erase(curiter);
  594. mVisibleRegionList.push_back(regionp);
  595. }
  596. }
  597. // Update all of the visible regions
  598. for (region_list_t::iterator iter = mVisibleRegionList.begin(),
  599. end = mVisibleRegionList.end();
  600. iter != end; )
  601. {
  602. region_list_t::iterator curiter = iter++;
  603. LLViewerRegion* regionp = *curiter;
  604. if (!regionp->getLand().hasZData())
  605. {
  606. continue;
  607. }
  608. LLSpatialPartition* part =
  609. regionp->getSpatialPartition(LLViewerRegion::PARTITION_TERRAIN);
  610. // PARTITION_TERRAIN cannot be NULL
  611. llassert(part);
  612. LLSpatialGroup* group = (LLSpatialGroup*)part->mOctree->getListener(0);
  613. const LLVector4a* bounds = group->getBounds();
  614. if (gViewerCamera.AABBInFrustum(bounds[0], bounds[1]))
  615. {
  616. regionp->calculateCameraDistance();
  617. regionp->getLand().updatePatchVisibilities();
  618. }
  619. else
  620. {
  621. mVisibleRegionList.erase(curiter);
  622. mCulledRegionList.push_back(regionp);
  623. }
  624. }
  625. // Sort visible regions
  626. mVisibleRegionList.sort(LLViewerRegion::CompareDistance());
  627. gViewerCamera.setFar(cur_far_clip);
  628. }
  629. void LLWorld::updateRegions(F32 max_update_time)
  630. {
  631. LLTimer update_timer;
  632. if (gViewerCamera.isChanged())
  633. {
  634. LLViewerRegion::sLastCameraUpdated =
  635. LLViewerOctreeEntryData::getCurrentFrame() + 1;
  636. }
  637. LLViewerRegion::calcNewObjectCreationThrottle();
  638. static LLCachedControl<U32> region_update_fraction(gSavedSettings,
  639. "RegionUpdateFraction");
  640. F32 fraction = (F32)llclamp((S32)region_update_fraction, 2, 20);
  641. if (LLViewerRegion::isNewObjectCreationThrottleDisabled())
  642. {
  643. // Loosen the time throttle.
  644. max_update_time = 10.f * max_update_time;
  645. }
  646. F32 max_time = llmin((F32)(max_update_time -
  647. update_timer.getElapsedTimeF32()),
  648. max_update_time / fraction);
  649. // Always perform an update on the agent region first.
  650. LLViewerRegion* self_regionp = gAgent.getRegion();
  651. if (self_regionp)
  652. {
  653. self_regionp->idleUpdate(max_time);
  654. }
  655. // Sort regions by its mLastUpdate: smaller mLastUpdate first to make sure
  656. // every region has a chance to get updated.
  657. LLViewerRegion::prio_list_t region_list;
  658. for (region_list_t::iterator iter = mActiveRegionList.begin(),
  659. end = mActiveRegionList.end();
  660. iter != end; ++iter)
  661. {
  662. LLViewerRegion* regionp = *iter;
  663. if (regionp != self_regionp)
  664. {
  665. region_list.insert(regionp);
  666. }
  667. }
  668. // Perform idle time updates for the regions (and associated surfaces)
  669. for (LLViewerRegion::prio_list_t::iterator iter = region_list.begin(),
  670. end = region_list.end();
  671. iter != end; ++iter)
  672. {
  673. LLViewerRegion* regionp = *iter;
  674. if (max_time > 0.f)
  675. {
  676. max_time = llmin((F32)(max_update_time -
  677. update_timer.getElapsedTimeF32()),
  678. max_update_time / fraction);
  679. }
  680. if (max_time > 0.f)
  681. {
  682. regionp->idleUpdate(max_time);
  683. }
  684. else
  685. {
  686. // Perform some necessary but very light updates.
  687. regionp->lightIdleUpdate();
  688. }
  689. }
  690. }
  691. void LLWorld::clearAllVisibleObjects()
  692. {
  693. for (region_list_t::iterator iter = mRegionList.begin(),
  694. end = mRegionList.end();
  695. iter != end; ++iter)
  696. {
  697. // Clear all cached visible objects.
  698. (*iter)->clearCachedVisibleObjects();
  699. }
  700. clearHoleWaterObjects();
  701. clearEdgeWaterObjects();
  702. }
  703. void LLWorld::updateClouds(F32 dt)
  704. {
  705. if (LLPipeline::sFreezeTime || !LLCloudLayer::needClassicClouds())
  706. {
  707. // Do not move clouds in snapshot mode and do not bother updating them
  708. // when not needed...
  709. return;
  710. }
  711. if (mActiveRegionList.size())
  712. {
  713. // Update all the cloud puff positions, and timer based stuff
  714. // such as death decay
  715. for (region_list_t::iterator iter = mActiveRegionList.begin(),
  716. end = mActiveRegionList.end();
  717. iter != end; ++iter)
  718. {
  719. LLViewerRegion* regionp = *iter;
  720. regionp->mCloudLayer.updatePuffs(dt);
  721. }
  722. // Reshuffle who owns which puffs
  723. for (region_list_t::iterator iter = mActiveRegionList.begin(),
  724. end = mActiveRegionList.end();
  725. iter != end; ++iter)
  726. {
  727. LLViewerRegion* regionp = *iter;
  728. regionp->mCloudLayer.updatePuffOwnership();
  729. }
  730. // Add new puffs
  731. for (region_list_t::iterator iter = mActiveRegionList.begin(),
  732. end = mActiveRegionList.end();
  733. iter != end; ++iter)
  734. {
  735. LLViewerRegion* regionp = *iter;
  736. regionp->mCloudLayer.updatePuffCount();
  737. }
  738. }
  739. }
  740. void LLWorld::killClouds()
  741. {
  742. for (region_list_t::iterator iter = mActiveRegionList.begin(),
  743. end = mActiveRegionList.end();
  744. iter != end; ++iter)
  745. {
  746. LLViewerRegion* regionp = *iter;
  747. regionp->mCloudLayer.reset();
  748. }
  749. }
  750. LLCloudGroup* LLWorld::findCloudGroup(const LLCloudPuff& puff)
  751. {
  752. if (mActiveRegionList.size())
  753. {
  754. // Update all the cloud puff positions, and timer based stuff
  755. // such as death decay
  756. for (region_list_t::iterator iter = mActiveRegionList.begin(),
  757. end = mActiveRegionList.end();
  758. iter != end; ++iter)
  759. {
  760. LLViewerRegion* regionp = *iter;
  761. LLCloudGroup* groupp = regionp->mCloudLayer.findCloudGroup(puff);
  762. if (groupp)
  763. {
  764. return groupp;
  765. }
  766. }
  767. }
  768. return NULL;
  769. }
  770. void LLWorld::renderPropertyLines()
  771. {
  772. static LLCachedControl<bool> show_lines(gSavedSettings,
  773. "ShowPropertyLines");
  774. if (!show_lines)
  775. {
  776. return;
  777. }
  778. for (region_list_t::iterator iter = mVisibleRegionList.begin(),
  779. end = mVisibleRegionList.end();
  780. iter != end; ++iter)
  781. {
  782. LLViewerRegion* regionp = *iter;
  783. regionp->renderPropertyLines();
  784. }
  785. }
  786. void LLWorld::updateNetStats()
  787. {
  788. F64 bits = 0.0;
  789. for (region_list_t::iterator iter = mActiveRegionList.begin(),
  790. end = mActiveRegionList.end();
  791. iter != end; ++iter)
  792. {
  793. LLViewerRegion* regionp = *iter;
  794. regionp->updateNetStats();
  795. bits += (F64)regionp->mBitStat.getCurrent();
  796. }
  797. U64 curl_bytes = LLCore::HttpLibcurl::getDownloadedBytes();
  798. bits += 8.0 * (F64)(curl_bytes - mLastCurlBytes);
  799. mLastCurlBytes = curl_bytes;
  800. LLMessageSystem* msg = gMessageSystemp;
  801. S32 packets_in = msg->mPacketsIn - mLastPacketsIn;
  802. S32 packets_out = msg->mPacketsOut - mLastPacketsOut;
  803. S32 packets_lost = msg->mDroppedPackets - mLastPacketsLost;
  804. S32 actual_in_bits = msg->mPacketRing.getAndResetActualInBits();
  805. S32 actual_out_bits = msg->mPacketRing.getAndResetActualOutBits();
  806. gViewerStats.mActualInKBitStat.addValue(actual_in_bits * 0.001f);
  807. gViewerStats.mActualOutKBitStat.addValue(actual_out_bits * 0.001f);
  808. gViewerStats.mKBitStat.addValue((F32)(bits * 0.001));
  809. gViewerStats.mPacketsInStat.addValue(packets_in);
  810. gViewerStats.mPacketsOutStat.addValue(packets_out);
  811. gViewerStats.mPacketsLostStat.addValue(msg->mDroppedPackets);
  812. F32 packets_pct = packets_in ? (F32)(100 * packets_lost) / (F32)packets_in
  813. : 0.f;
  814. gViewerStats.mPacketsLostPercentStat.addValue(packets_pct);
  815. mLastPacketsIn = msg->mPacketsIn;
  816. mLastPacketsOut = msg->mPacketsOut;
  817. mLastPacketsLost = msg->mDroppedPackets;
  818. }
  819. void LLWorld::printPacketsLost()
  820. {
  821. llinfos << "Simulators:" << llendl;
  822. llinfos << "----------" << llendl;
  823. LLCircuitData* cdp = NULL;
  824. for (region_list_t::iterator iter = mActiveRegionList.begin();
  825. iter != mActiveRegionList.end(); ++iter)
  826. {
  827. LLViewerRegion* regionp = *iter;
  828. cdp = gMessageSystemp->mCircuitInfo.findCircuit(regionp->getHost());
  829. if (cdp)
  830. {
  831. LLVector3d range = regionp->getCenterGlobal() -
  832. gAgent.getPositionGlobal();
  833. llinfos << regionp->getHost() << ", range: " << range.length()
  834. << " packets lost: " << cdp->getPacketsLost() << llendl;
  835. }
  836. }
  837. llinfos << "----------" << llendl;
  838. }
  839. void LLWorld::processCoarseUpdate(LLMessageSystem* msg, void**)
  840. {
  841. LLViewerRegion* region = gWorld.getRegion(msg->getSender());
  842. if (region)
  843. {
  844. region->updateCoarseLocations(msg);
  845. }
  846. }
  847. F32 LLWorld::getLandFarClip() const
  848. {
  849. return mLandFarClip;
  850. }
  851. void LLWorld::setLandFarClip(F32 far_clip)
  852. {
  853. // Variable region size support
  854. LLViewerRegion* region = gAgent.getRegion();
  855. F32 rwidth = (S32)(region ? region->getWidth() : REGION_WIDTH_METERS);
  856. const S32 n1 = (llceil(mLandFarClip) - 1) / rwidth;
  857. const S32 n2 = (llceil(far_clip) - 1) / rwidth;
  858. bool need_water_objects_update = n1 != n2;
  859. mLandFarClip = far_clip;
  860. if (need_water_objects_update)
  861. {
  862. updateWaterObjects();
  863. }
  864. }
  865. // Some region that we are connected to, but not the one we are in, gave us
  866. // a (possibly) new water height. Update it in our local copy.
  867. void LLWorld::waterHeightRegionInfo(std::string const& sim_name,
  868. F32 water_height)
  869. {
  870. for (region_list_t::iterator iter = mRegionList.begin(),
  871. end = mRegionList.end();
  872. iter != end; ++iter)
  873. {
  874. if ((*iter)->getName() == sim_name)
  875. {
  876. (*iter)->setWaterHeight(water_height);
  877. break;
  878. }
  879. }
  880. }
  881. void LLWorld::clearHoleWaterObjects()
  882. {
  883. for (water_obj_list_t::iterator iter = mHoleWaterObjects.begin(),
  884. end = mHoleWaterObjects.end();
  885. iter != end; ++iter)
  886. {
  887. LLVOWater* waterp = iter->get();
  888. if (waterp) // Paranoia
  889. {
  890. gObjectList.killObject(waterp);
  891. }
  892. }
  893. mHoleWaterObjects.clear();
  894. }
  895. void LLWorld::clearEdgeWaterObjects()
  896. {
  897. for (S32 i = 0; i < EDGE_WATER_OBJECTS_COUNT; ++i)
  898. {
  899. LLVOWater* waterp = mEdgeWaterObjects[i];
  900. if (waterp) // Paranoia
  901. {
  902. gObjectList.killObject(waterp);
  903. mEdgeWaterObjects[i] = NULL;
  904. }
  905. }
  906. }
  907. void LLWorld::updateWaterObjects()
  908. {
  909. LLViewerRegion* regionp = gAgent.getRegion();
  910. if (!regionp || mRegionList.empty())
  911. {
  912. return;
  913. }
  914. // First, determine the min and max "box" of water objects
  915. // Variable region size support
  916. F32 rwidth = (S32)regionp->getWidth();
  917. // We only want to fill in water for stuff that is near us, say, within 256
  918. // or 512m
  919. S32 range = gViewerCamera.getFar() > 256.f ? 512 : 256;
  920. U32 region_x, region_y;
  921. from_region_handle(regionp->getHandle(), &region_x, &region_y);
  922. S32 min_x = (S32)region_x - range;
  923. S32 min_y = (S32)region_y - range;
  924. S32 max_x = (S32)region_x + range;
  925. S32 max_y = (S32)region_y + range;
  926. for (region_list_t::iterator iter = mRegionList.begin(),
  927. end = mRegionList.end();
  928. iter != end; ++iter)
  929. {
  930. LLViewerRegion* regp = *iter;
  931. if (regp) // Paranoia
  932. {
  933. LLVOWater* waterp = regp->getLand().getWaterObj();
  934. if (waterp)
  935. {
  936. gObjectList.updateActive(waterp);
  937. }
  938. }
  939. }
  940. clearHoleWaterObjects();
  941. // Now, get a list of the holes
  942. F32 water_height = regionp->getWaterHeight() + 256.f;
  943. for (S32 x = min_x; x <= max_x; x += rwidth)
  944. {
  945. for (S32 y = min_y; y <= max_y; y += rwidth)
  946. {
  947. U64 region_handle = to_region_handle(x, y);
  948. if (!getRegionFromHandle(region_handle))
  949. {
  950. LLViewerObject* vobj =
  951. gObjectList.createObjectViewer(LLViewerObject::LL_VO_WATER,
  952. regionp);
  953. LLVOWater* waterp = (LLVOWater*)vobj;
  954. waterp->setUseTexture(false);
  955. waterp->setPositionGlobal(LLVector3d(x + rwidth / 2,
  956. y + rwidth / 2,
  957. water_height));
  958. waterp->setScale(LLVector3((F32)rwidth, (F32)rwidth, 512.f));
  959. gPipeline.createObject(waterp);
  960. mHoleWaterObjects.push_back(waterp);
  961. }
  962. }
  963. }
  964. // Update edge water objects
  965. S32 wx = (max_x - min_x) + rwidth;
  966. S32 wy = (max_y - min_y) + rwidth;
  967. S32 center_x = min_x + (wx >> 1);
  968. S32 center_y = min_y + (wy >> 1);
  969. S32 add_boundary[4] =
  970. {
  971. 512 - (S32)(max_x - region_x),
  972. 512 - (S32)(max_y - region_y),
  973. 512 - (S32)(region_x - min_x),
  974. 512 - (S32)(region_y - min_y)
  975. };
  976. S32 dim[2];
  977. for (S32 dir = 0; dir < 8; ++dir)
  978. {
  979. switch (gDirAxes[dir][0])
  980. {
  981. case -1:
  982. dim[0] = add_boundary[2];
  983. break;
  984. case 0:
  985. dim[0] = wx;
  986. break;
  987. default:
  988. dim[0] = add_boundary[0];
  989. }
  990. switch (gDirAxes[dir][1])
  991. {
  992. case -1:
  993. dim[1] = add_boundary[3];
  994. break;
  995. case 0:
  996. dim[1] = wy;
  997. break;
  998. default:
  999. dim[1] = add_boundary[1];
  1000. }
  1001. // Resize and reshape the water objects
  1002. const S32 water_center_x = center_x +
  1003. ll_round((wx + dim[0]) * 0.5f *
  1004. gDirAxes[dir][0]);
  1005. const S32 water_center_y = center_y +
  1006. ll_round((wy + dim[1]) * 0.5f *
  1007. gDirAxes[dir][1]);
  1008. LLVOWater* waterp = mEdgeWaterObjects[dir];
  1009. if (!waterp || waterp->isDead())
  1010. {
  1011. // The edge water objects can be dead because they are attached to
  1012. // the region that the agent was in when they were originally
  1013. // created.
  1014. LLViewerObject* vobj =
  1015. gObjectList.createObjectViewer(LLViewerObject::LL_VO_WATER,
  1016. regionp);
  1017. mEdgeWaterObjects[dir] = (LLVOWater*)vobj;
  1018. waterp = mEdgeWaterObjects[dir];
  1019. waterp->setUseTexture(false);
  1020. waterp->setIsEdgePatch(true);
  1021. gPipeline.createObject(waterp);
  1022. }
  1023. waterp->setRegion(regionp);
  1024. LLVector3d water_pos(water_center_x, water_center_y, water_height);
  1025. LLVector3 water_scale((F32)dim[0], (F32)dim[1], 512.f);
  1026. // Stretch out to horizon
  1027. water_scale.mV[0] += fabsf(2048.f * gDirAxes[dir][0]);
  1028. water_scale.mV[1] += fabsf(2048.f * gDirAxes[dir][1]);
  1029. water_pos.mdV[0] += 1024.f * gDirAxes[dir][0];
  1030. water_pos.mdV[1] += 1024.f * gDirAxes[dir][1];
  1031. waterp->setPositionGlobal(water_pos);
  1032. waterp->setScale(water_scale);
  1033. gObjectList.updateActive(waterp);
  1034. }
  1035. }
  1036. void LLWorld::precullWaterObjects(LLCamera& camera, LLCullResult* cullp)
  1037. {
  1038. if (mRegionList.empty())
  1039. {
  1040. return;
  1041. }
  1042. for (region_list_t::iterator iter = mRegionList.begin(),
  1043. end = mRegionList.end();
  1044. iter != end; ++iter)
  1045. {
  1046. LLViewerRegion* regionp = *iter;
  1047. LLVOWater* waterp = regionp->getLand().getWaterObj();
  1048. if (waterp && !waterp->isDead() && waterp->mDrawable)
  1049. {
  1050. waterp->mDrawable->setVisible(camera);
  1051. cullp->pushDrawable(waterp->mDrawable);
  1052. }
  1053. }
  1054. for (water_obj_list_t::iterator iter = mHoleWaterObjects.begin(),
  1055. end = mHoleWaterObjects.end();
  1056. iter != end; ++iter)
  1057. {
  1058. LLVOWater* waterp = iter->get();
  1059. if (waterp && !waterp->isDead() && waterp->mDrawable)
  1060. {
  1061. waterp->mDrawable->setVisible(camera);
  1062. cullp->pushDrawable(waterp->mDrawable);
  1063. }
  1064. }
  1065. for (S32 i = 0; i < EDGE_WATER_OBJECTS_COUNT; ++i)
  1066. {
  1067. LLVOWater* waterp = mEdgeWaterObjects[i].get();
  1068. if (waterp && !waterp->isDead() && waterp->mDrawable)
  1069. {
  1070. waterp->mDrawable->setVisible(camera);
  1071. cullp->pushDrawable(waterp->mDrawable);
  1072. }
  1073. }
  1074. }
  1075. void LLWorld::shiftRegions(const LLVector3& offset)
  1076. {
  1077. for (region_list_t::const_iterator i = getRegionList().begin(),
  1078. end = getRegionList().end();
  1079. i != end; ++i)
  1080. {
  1081. LLViewerRegion* regionp = *i;
  1082. regionp->updateRenderMatrix();
  1083. }
  1084. gViewerPartSim.shift(offset);
  1085. }
  1086. void LLWorld::reloadAllSurfacePatches()
  1087. {
  1088. llinfos << "Force-reloading all surface patches to rebuild failed textures."
  1089. << llendl;
  1090. // This inserts a delay before a new automatic reload hack would get
  1091. // triggered... HB
  1092. LLSurfacePatch::allPatchesReloaded();
  1093. for (region_list_t::iterator iter = mRegionList.begin(),
  1094. end = mRegionList.end();
  1095. iter != end; ++iter)
  1096. {
  1097. LLViewerRegion* regionp = *iter;
  1098. LLVLComposition* compp = regionp->getComposition();
  1099. if (compp)
  1100. {
  1101. compp->forceRebuild();
  1102. regionp->getLand().dirtyAllPatches();
  1103. }
  1104. }
  1105. }
  1106. void LLWorld::requestCacheMisses()
  1107. {
  1108. for (region_list_t::iterator iter = mRegionList.begin(),
  1109. end = mRegionList.end();
  1110. iter != end; ++iter)
  1111. {
  1112. LLViewerRegion* regionp = *iter;
  1113. regionp->requestCacheMisses();
  1114. }
  1115. }
  1116. void LLWorld::getInfo(LLSD& info)
  1117. {
  1118. LLSD region_info;
  1119. for (region_list_t::iterator iter = mRegionList.begin(),
  1120. end = mRegionList.end();
  1121. iter != end; ++iter)
  1122. {
  1123. LLViewerRegion* regionp = *iter;
  1124. regionp->getInfo(region_info);
  1125. info["World"].append(region_info);
  1126. }
  1127. }
  1128. void LLWorld::disconnectRegions()
  1129. {
  1130. LLMessageSystem* msg = gMessageSystemp;
  1131. for (region_list_t::iterator iter = mRegionList.begin();
  1132. iter != mRegionList.end(); ++iter)
  1133. {
  1134. LLViewerRegion* regionp = *iter;
  1135. if (regionp == gAgent.getRegion())
  1136. {
  1137. // Skip the main agent
  1138. continue;
  1139. }
  1140. llinfos << "Sending AgentQuitCopy to: " << regionp->getHost()
  1141. << llendl;
  1142. msg->newMessageFast(_PREHASH_AgentQuitCopy);
  1143. msg->nextBlockFast(_PREHASH_AgentData);
  1144. msg->addUUIDFast(_PREHASH_AgentID, gAgentID);
  1145. msg->addUUIDFast(_PREHASH_SessionID, gAgentSessionID);
  1146. msg->nextBlockFast(_PREHASH_FuseBlock);
  1147. msg->addU32Fast(_PREHASH_ViewerCircuitCode, msg->mOurCircuitCode);
  1148. msg->sendMessage(regionp->getHost());
  1149. }
  1150. }
  1151. // Enables the appropriate circuit for this simulator and adds its parameters
  1152. //static
  1153. void LLWorld::processEnableSimulator(LLMessageSystem* msg, void**)
  1154. {
  1155. U64 handle;
  1156. msg->getU64Fast(_PREHASH_SimulatorInfo, _PREHASH_Handle, handle);
  1157. U32 ip_u32;
  1158. msg->getIPAddrFast(_PREHASH_SimulatorInfo, _PREHASH_IP, ip_u32);
  1159. U16 port;
  1160. msg->getIPPortFast(_PREHASH_SimulatorInfo, _PREHASH_Port, port);
  1161. // Which simulator should we modify ?
  1162. LLHost sim(ip_u32, port);
  1163. // Viewer trusts the simulator.
  1164. msg->enableCircuit(sim, true);
  1165. // Variable region size support
  1166. U32 region_size_x = REGION_WIDTH_METERS;
  1167. U32 region_size_y = REGION_WIDTH_METERS;
  1168. if (!gIsInSecondLife)
  1169. {
  1170. msg->getU32Fast(_PREHASH_SimulatorInfo, _PREHASH_RegionSizeX,
  1171. region_size_x);
  1172. if (region_size_x == 0)
  1173. {
  1174. region_size_x = REGION_WIDTH_METERS;
  1175. }
  1176. msg->getU32Fast(_PREHASH_SimulatorInfo, _PREHASH_RegionSizeY,
  1177. region_size_y);
  1178. if (region_size_y == 0)
  1179. {
  1180. region_size_y = region_size_x;
  1181. }
  1182. }
  1183. if (region_size_x != region_size_y)
  1184. {
  1185. llwarns << "RECTANGULAR REGIONS NOT SUPPORTED: expect a crash !"
  1186. << llendl;
  1187. region_size_x = llmax(region_size_x, region_size_y);
  1188. }
  1189. gWorld.addRegion(handle, sim, region_size_x);
  1190. // Give the simulator a message it can use to get ip and port
  1191. U32 circuit_code = msg->getOurCircuitCode();
  1192. static U32 last_ip_u32 = 0;
  1193. static U32 last_circuit_code = 0;
  1194. if (ip_u32 != last_ip_u32 || circuit_code != last_circuit_code)
  1195. {
  1196. last_ip_u32 = ip_u32;
  1197. last_circuit_code = circuit_code;
  1198. llinfos << "Enabling simulator " << sim << " (region handle " << handle
  1199. << ") with code " << circuit_code << llendl;
  1200. }
  1201. msg->newMessageFast(_PREHASH_UseCircuitCode);
  1202. msg->nextBlockFast(_PREHASH_CircuitCode);
  1203. msg->addU32Fast(_PREHASH_Code, circuit_code);
  1204. msg->addUUIDFast(_PREHASH_SessionID, gAgentSessionID);
  1205. msg->addUUIDFast(_PREHASH_ID, gAgentID);
  1206. msg->sendReliable(sim);
  1207. }
  1208. class LLEstablishAgentCommunication final : public LLHTTPNode
  1209. {
  1210. protected:
  1211. LOG_CLASS(LLEstablishAgentCommunication);
  1212. public:
  1213. void describe(Description& desc) const override
  1214. {
  1215. desc.shortInfo("seed capability info for a region");
  1216. desc.postAPI();
  1217. desc.input("{ seed-capability: ..., sim-ip: ..., sim-port }");
  1218. desc.source(__FILE__, __LINE__);
  1219. }
  1220. void post(ResponsePtr response, const LLSD& context,
  1221. const LLSD& input) const override
  1222. {
  1223. if (LLApp::isExiting() || gDisconnected)
  1224. {
  1225. return;
  1226. }
  1227. if (!input["body"].has("agent-id") ||
  1228. !input["body"].has("sim-ip-and-port") ||
  1229. !input["body"].has("seed-capability"))
  1230. {
  1231. llwarns << "Missing parameters" << llendl;
  1232. return;
  1233. }
  1234. LLHost sim(input["body"]["sim-ip-and-port"].asString());
  1235. if (sim.isInvalid())
  1236. {
  1237. llwarns << "Got a response with an invalid host" << llendl;
  1238. return;
  1239. }
  1240. LLViewerRegion* regionp = gWorld.getRegion(sim);
  1241. if (!regionp)
  1242. {
  1243. llwarns << "Got a response for an unknown region: " << sim
  1244. << llendl;
  1245. return;
  1246. }
  1247. regionp->setSeedCapability(input["body"]["seed-capability"]);
  1248. }
  1249. };
  1250. LLHTTPRegistration<LLEstablishAgentCommunication>
  1251. gHTTPRegistrationEstablishAgentCommunication("/message/EstablishAgentCommunication");
  1252. // Disable the circuit to this simulator. Called in response to a
  1253. // "DisableSimulator" message. However, if the last sim disabling happened
  1254. // less than 1 second ago, queue the region for later disabling instead, so to
  1255. // avoid huge 'hiccups' when saving the corresponding objects cache to the hard
  1256. // drive (this is especially true with the new objects caching scheme, the full
  1257. // region objects getting cached, and that is a lot of data to save, which
  1258. // takes quite some time). HB
  1259. //static
  1260. void LLWorld::processDisableSimulator(LLMessageSystem* msg, void**)
  1261. {
  1262. static LLCachedControl<bool> staged_sim_disabling(gSavedSettings,
  1263. "StagedSimDisabling");
  1264. static LLCachedControl<U32> disabling_delay(gSavedSettings,
  1265. "StagedSimDisablingDelay");
  1266. F32 delay = disabling_delay > 0 ? (F32)disabling_delay : 1.f;
  1267. if (!msg) return;
  1268. LLHost host = msg->getSender();
  1269. LLViewerRegion* regionp = gWorld.getRegion(host);
  1270. if (!regionp || !staged_sim_disabling ||
  1271. (gWorld.mDisabledRegionList.empty() &&
  1272. gFrameTimeSeconds - gWorld.mLastRegionDisabling > delay))
  1273. {
  1274. llinfos << "Disabling simulator " << host << llendl;
  1275. gWorld.removeRegion(host);
  1276. msg->disableCircuit(host);
  1277. gWorld.mLastRegionDisabling = gFrameTimeSeconds;
  1278. }
  1279. else
  1280. {
  1281. llinfos << "Queuing simulator " << host << " for delayed removal."
  1282. << llendl;
  1283. gWorld.mDisabledRegionList.push_back(regionp);
  1284. }
  1285. }
  1286. //static
  1287. void LLWorld::idleDisableQueuedSim()
  1288. {
  1289. static LLCachedControl<U32> disabling_delay(gSavedSettings,
  1290. "StagedSimDisablingDelay");
  1291. F32 delay = disabling_delay > 0 ? (F32)disabling_delay : 1.f;
  1292. if (!gWorld.mDisabledRegionList.empty() &&
  1293. gFrameTimeSeconds - gWorld.mLastRegionDisabling > delay)
  1294. {
  1295. LLViewerRegion* regionp = gWorld.mDisabledRegionList.front();
  1296. LLHost host = regionp->getHost();
  1297. llinfos << "Disabling simulator " << host << llendl;
  1298. // Note: removeRegion() also removes the region from
  1299. // mDisabledRegionList
  1300. gWorld.removeRegion(host);
  1301. if (gMessageSystemp)
  1302. {
  1303. gMessageSystemp->disableCircuit(host);
  1304. }
  1305. gWorld.mLastRegionDisabling = gFrameTimeSeconds;
  1306. }
  1307. }
  1308. //static
  1309. void LLWorld::processRegionHandshake(LLMessageSystem* msg, void**)
  1310. {
  1311. const LLHost& host = msg->getSender();
  1312. LLViewerRegion* regionp = gWorld.getRegion(host);
  1313. if (regionp)
  1314. {
  1315. regionp->unpackRegionHandshake();
  1316. }
  1317. else
  1318. {
  1319. llwarns << "Got region handshake for unknown region " << host
  1320. << llendl;
  1321. }
  1322. }
  1323. //static
  1324. void LLWorld::sendAgentPause()
  1325. {
  1326. // Note: used to check for LLWorld initialization before it became a
  1327. // global. Rather than just remove this check I am changing it to assure
  1328. // that the message system has been initialized. -MG
  1329. LLMessageSystem* msg = gMessageSystemp;
  1330. if (!msg)
  1331. {
  1332. return;
  1333. }
  1334. msg->newMessageFast(_PREHASH_AgentPause);
  1335. msg->nextBlockFast(_PREHASH_AgentData);
  1336. msg->addUUIDFast(_PREHASH_AgentID, gAgentID);
  1337. msg->addUUIDFast(_PREHASH_SessionID, gAgentSessionID);
  1338. msg->addU32Fast(_PREHASH_SerialNum, ++gAgentPauseSerialNum);
  1339. for (LLWorld::region_list_t::const_iterator
  1340. iter = gWorld.getRegionList().begin(),
  1341. end = gWorld.getRegionList().end();
  1342. iter != end; ++iter)
  1343. {
  1344. LLViewerRegion* regionp = *iter;
  1345. msg->sendReliable(regionp->getHost());
  1346. }
  1347. gObjectList.mWasPaused = true;
  1348. }
  1349. //static
  1350. void LLWorld::sendAgentResume()
  1351. {
  1352. // Note: used to check for LLWorld initialization before it became a
  1353. // singleton. Rather than just remove this check I'm changing it to assure
  1354. // that the message system has been initialized. -MG
  1355. LLMessageSystem* msg = gMessageSystemp;
  1356. if (!msg)
  1357. {
  1358. return;
  1359. }
  1360. msg->newMessageFast(_PREHASH_AgentResume);
  1361. msg->nextBlockFast(_PREHASH_AgentData);
  1362. msg->addUUIDFast(_PREHASH_AgentID, gAgentID);
  1363. msg->addUUIDFast(_PREHASH_SessionID, gAgentSessionID);
  1364. msg->addU32Fast(_PREHASH_SerialNum, ++gAgentPauseSerialNum);
  1365. for (LLWorld::region_list_t::const_iterator
  1366. iter = gWorld.getRegionList().begin(),
  1367. end = gWorld.getRegionList().end();
  1368. iter != end; ++iter)
  1369. {
  1370. LLViewerRegion* regionp = *iter;
  1371. msg->sendReliable(regionp->getHost());
  1372. }
  1373. // Reset the FPS counter to avoid an invalid fps
  1374. gViewerStats.mFPSStat.start();
  1375. }
  1376. void LLWorld::getAvatars(uuid_vec_t& avatar_ids,
  1377. std::vector<LLVector3d>* positions,
  1378. std::vector<LLColor4>* colors,
  1379. const LLVector3d& relative_to,
  1380. F32 radius) const
  1381. {
  1382. avatar_ids.clear();
  1383. S32 count = LLCharacter::sInstances.size();
  1384. if (positions)
  1385. {
  1386. positions->clear();
  1387. positions->reserve(count);
  1388. }
  1389. if (colors)
  1390. {
  1391. colors->clear();
  1392. colors->reserve(count);
  1393. }
  1394. F32 radius_squared = radius * radius;
  1395. // Get the list of avatars from the character list first, so distances are
  1396. // correct when agent is above 1020m and other avatars are nearby
  1397. for (S32 i = 0; i < count; ++i)
  1398. {
  1399. LLVOAvatar* avatarp = (LLVOAvatar*)LLCharacter::sInstances[i];
  1400. if (avatarp && !avatarp->isDead() && !avatarp->isSelf() &&
  1401. !avatarp->mIsDummy && !avatarp->isOrphaned())
  1402. {
  1403. const LLUUID& id = avatarp->getID();
  1404. if (id.isNull()) continue; // Paranoia
  1405. LLVector3d pos_global = avatarp->getPositionGlobal();
  1406. if (dist_vec_squared(pos_global, relative_to) <= radius_squared)
  1407. {
  1408. avatar_ids.emplace_back(id);
  1409. if (positions)
  1410. {
  1411. positions->emplace_back(pos_global);
  1412. }
  1413. if (colors)
  1414. {
  1415. colors->emplace_back(avatarp->getMinimapColor());
  1416. }
  1417. }
  1418. }
  1419. }
  1420. // Region avatars added for situations where radius is greater than
  1421. // RenderFarClip
  1422. LLVector3d pos_global;
  1423. for (LLWorld::region_list_t::const_iterator it = mActiveRegionList.begin(),
  1424. end = mActiveRegionList.end();
  1425. it != end; ++it)
  1426. {
  1427. LLViewerRegion* regionp = *it;
  1428. if (!regionp) continue; // Paranoia
  1429. // Variable region size support
  1430. F64 scale_factor = (F64)regionp->getWidth() / REGION_WIDTH_METERS;
  1431. const LLVector3d& origin_global = regionp->getOriginGlobal();
  1432. count = regionp->mMapAvatars.size();
  1433. for (S32 i = 0; i < count; ++i)
  1434. {
  1435. const LLUUID& id = regionp->mMapAvatarIDs[i];
  1436. if (id.isNull()) continue; // This can happen !
  1437. // Unpack the 32 bits encoded position and make it global
  1438. U32 compact_local = regionp->mMapAvatars[i];
  1439. pos_global = origin_global;
  1440. pos_global.mdV[VZ] += (F64)((compact_local & 0xFFU) * 4);
  1441. compact_local >>= 8;
  1442. pos_global.mdV[VY] += (F64)(compact_local & 0xFFU) * scale_factor;
  1443. compact_local >>= 8;
  1444. pos_global.mdV[VX] += (F64)(compact_local & 0xFFU) * scale_factor;
  1445. if (dist_vec_squared(pos_global, relative_to) > radius_squared)
  1446. {
  1447. continue;
  1448. }
  1449. bool not_listed = true;
  1450. for (S32 j = 0, count = avatar_ids.size(); j < count; ++j)
  1451. {
  1452. if (id == avatar_ids[j])
  1453. {
  1454. not_listed = false;
  1455. break;
  1456. }
  1457. }
  1458. if (not_listed)
  1459. {
  1460. // If this avatar does not already exist in the list, add it
  1461. avatar_ids.emplace_back(id);
  1462. if (positions)
  1463. {
  1464. positions->emplace_back(pos_global);
  1465. }
  1466. if (colors)
  1467. {
  1468. colors->emplace_back(LLVOAvatar::getMinimapColor(id));
  1469. }
  1470. }
  1471. }
  1472. }
  1473. }
  1474. bool LLWorld::isRegionListed(const LLViewerRegion* region) const
  1475. {
  1476. region_list_t::const_iterator it = find(mRegionList.begin(),
  1477. mRegionList.end(), region);
  1478. return it != mRegionList.end();
  1479. }
  1480. boost::signals2::connection LLWorld::setRegionRemovedCallback(const region_remove_signal_t::slot_type& cb)
  1481. {
  1482. return mRegionRemovedSignal.connect(cb);
  1483. }