llhudeffectpointat.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /**
  2. * @file llhudeffectpointat.cpp
  3. * @brief LLHUDEffectPointAt class implementation
  4. *
  5. * $LicenseInfo:firstyear=2002&license=viewergpl$
  6. *
  7. * Copyright (c) 2002-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 "llhudeffectpointat.h"
  34. #include "llanimationstates.h"
  35. #include "llgl.h"
  36. #include "llrender.h"
  37. #include "llagent.h"
  38. #include "llappviewer.h"
  39. #include "lldrawable.h"
  40. //MK
  41. #include "mkrlinterface.h"
  42. //mk
  43. #include "llviewerobjectlist.h"
  44. #include "llvoavatar.h"
  45. #include "llmessage.h"
  46. // packet layout
  47. constexpr S32 SOURCE_AVATAR = 0;
  48. constexpr S32 TARGET_OBJECT = 16;
  49. constexpr S32 TARGET_POS = 32;
  50. constexpr S32 POINTAT_TYPE = 56;
  51. constexpr S32 PKT_SIZE = 57;
  52. // Throttle
  53. constexpr F32 DELAY_BETWEEN_SENDS = 0.25f; // seconds
  54. constexpr F32 MIN_DELTAPOS_FOR_UPDATE = 0.05f;
  55. // timeouts
  56. // can't use actual F32_MAX, because we add this to the current frametime
  57. constexpr F32 MAX_TIMEOUT = F32_MAX * 0.25f;
  58. static const F32 POINTAT_TIMEOUTS[POINTAT_NUM_TARGETS] =
  59. {
  60. MAX_TIMEOUT, //POINTAT_TARGET_NONE
  61. MAX_TIMEOUT, //POINTAT_TARGET_SELECT
  62. MAX_TIMEOUT, //POINTAT_TARGET_GRAB
  63. 0.f, //POINTAT_TARGET_CLEAR
  64. };
  65. static const S32 POINTAT_PRIORITIES[POINTAT_NUM_TARGETS] =
  66. {
  67. 0, //POINTAT_TARGET_NONE
  68. 1, //POINTAT_TARGET_SELECT
  69. 2, //POINTAT_TARGET_GRAB
  70. 3, //POINTAT_TARGET_CLEAR
  71. };
  72. // static
  73. bool LLHUDEffectPointAt::sDebugPointAt = false;
  74. LLHUDEffectPointAt::LLHUDEffectPointAt(U8 type)
  75. : LLHUDEffect(type),
  76. mKillTime(0.f),
  77. mLastSendTime(0.f)
  78. {
  79. clearPointAtTarget();
  80. }
  81. void LLHUDEffectPointAt::packData(LLMessageSystem* mesgsys)
  82. {
  83. // Pack the default data
  84. LLHUDEffect::packData(mesgsys);
  85. // Pack the type-specific data which uses a fun packed binary format.
  86. U8 packed_data[PKT_SIZE];
  87. memset(packed_data, 0, PKT_SIZE);
  88. if (mSourceObject)
  89. {
  90. htonmemcpy(&(packed_data[SOURCE_AVATAR]), mSourceObject->mID.mData,
  91. MVT_LLUUID, 16);
  92. }
  93. else
  94. {
  95. htonmemcpy(&(packed_data[SOURCE_AVATAR]), LLUUID::null.mData,
  96. MVT_LLUUID, 16);
  97. }
  98. // pack both target object and position
  99. // position interpreted as offset if target object is non-null
  100. if (mTargetObject)
  101. {
  102. htonmemcpy(&(packed_data[TARGET_OBJECT]), mTargetObject->mID.mData, MVT_LLUUID, 16);
  103. }
  104. else
  105. {
  106. htonmemcpy(&(packed_data[TARGET_OBJECT]), LLUUID::null.mData, MVT_LLUUID, 16);
  107. }
  108. htonmemcpy(&(packed_data[TARGET_POS]), mTargetOffsetGlobal.mdV, MVT_LLVector3d, 24);
  109. U8 pointAtTypePacked = (U8)mTargetType;
  110. htonmemcpy(&(packed_data[POINTAT_TYPE]), &pointAtTypePacked, MVT_U8, 1);
  111. mesgsys->addBinaryDataFast(_PREHASH_TypeData, packed_data, PKT_SIZE);
  112. mLastSendTime = mTimer.getElapsedTimeF32();
  113. }
  114. void LLHUDEffectPointAt::unpackData(LLMessageSystem* mesgsys, S32 blocknum)
  115. {
  116. LLUUID data_id;
  117. mesgsys->getUUIDFast(_PREHASH_Effect, _PREHASH_ID, data_id, blocknum);
  118. // Ignore messages from ourselves
  119. if (gAgent.mPointAt.notNull() && data_id == gAgent.mPointAt->getID())
  120. {
  121. return;
  122. }
  123. LLHUDEffect::unpackData(mesgsys, blocknum);
  124. S32 size = mesgsys->getSizeFast(_PREHASH_Effect, blocknum,
  125. _PREHASH_TypeData);
  126. if (size != PKT_SIZE)
  127. {
  128. llwarns << "PointAt effect with bad size: " << size << " - skipped."
  129. << llendl;
  130. return;
  131. }
  132. U8 packed_data[PKT_SIZE];
  133. mesgsys->getBinaryDataFast(_PREHASH_Effect, _PREHASH_TypeData, packed_data,
  134. PKT_SIZE, blocknum);
  135. LLUUID source_id;
  136. htonmemcpy(source_id.mData, &(packed_data[SOURCE_AVATAR]), MVT_LLUUID, 16);
  137. LLVOAvatar* avatarp = gObjectList.findAvatar(source_id);
  138. if (avatarp)
  139. {
  140. setSourceObject(avatarp);
  141. }
  142. else
  143. {
  144. #if 0
  145. llwarns << "Could not find source avatar for pointat effect" << llendl;
  146. #endif
  147. return;
  148. }
  149. LLUUID target_id;
  150. htonmemcpy(target_id.mData, &(packed_data[TARGET_OBJECT]), MVT_LLUUID, 16);
  151. LLViewerObject* objp = gObjectList.findObject(target_id);
  152. LLVector3d new_target;
  153. htonmemcpy(new_target.mdV, &(packed_data[TARGET_POS]), MVT_LLVector3d, 24);
  154. if (objp)
  155. {
  156. setTargetObjectAndOffset(objp, new_target);
  157. }
  158. else if (target_id.isNull())
  159. {
  160. setTargetPosGlobal(new_target);
  161. }
  162. U8 unpacked_type = 0;
  163. htonmemcpy(&unpacked_type, &(packed_data[POINTAT_TYPE]), MVT_U8, 1);
  164. mTargetType = (EPointAtType)unpacked_type;
  165. #if 0
  166. mKillTime = mTimer.getElapsedTimeF32() + mDuration;
  167. #endif
  168. }
  169. void LLHUDEffectPointAt::setTargetObjectAndOffset(LLViewerObject* objp,
  170. const LLVector3d& offset)
  171. {
  172. mTargetObject = objp;
  173. mTargetOffsetGlobal = offset;
  174. }
  175. void LLHUDEffectPointAt::setTargetPosGlobal(const LLVector3d& target_pos_global)
  176. {
  177. mTargetObject = NULL;
  178. mTargetOffsetGlobal = target_pos_global;
  179. }
  180. bool LLHUDEffectPointAt::setPointAt(EPointAtType target_type,
  181. LLViewerObject* object, LLVector3 position)
  182. {
  183. if (!mSourceObject)
  184. {
  185. return false;
  186. }
  187. if (target_type >= POINTAT_NUM_TARGETS)
  188. {
  189. llwarns << "Bad target_type " << (int)target_type << " - ignoring." << llendl;
  190. return false;
  191. }
  192. // Must be same or higher priority than existing effect
  193. if (POINTAT_PRIORITIES[target_type] < POINTAT_PRIORITIES[mTargetType])
  194. {
  195. return false;
  196. }
  197. F32 current_time = mTimer.getElapsedTimeF32();
  198. // If type of point-at behavior or target object has changed or moved
  199. if (target_type != mTargetType || object != mTargetObject ||
  200. (current_time - mLastSendTime > DELAY_BETWEEN_SENDS &&
  201. dist_vec(position, mLastSentOffsetGlobal) > MIN_DELTAPOS_FOR_UPDATE))
  202. {
  203. mLastSentOffsetGlobal = position;
  204. setDuration(POINTAT_TIMEOUTS[target_type]);
  205. setNeedsSendToSim(true);
  206. }
  207. if (target_type == POINTAT_TARGET_CLEAR)
  208. {
  209. clearPointAtTarget();
  210. }
  211. else
  212. {
  213. mTargetType = target_type;
  214. mTargetObject = object;
  215. if (object)
  216. {
  217. mTargetOffsetGlobal.set(position);
  218. }
  219. else
  220. {
  221. mTargetOffsetGlobal = gAgent.getPosGlobalFromAgent(position);
  222. }
  223. mKillTime = mTimer.getElapsedTimeF32() + mDuration;
  224. }
  225. return true;
  226. }
  227. void LLHUDEffectPointAt::clearPointAtTarget()
  228. {
  229. mTargetObject = NULL;
  230. mTargetOffsetGlobal.clear();
  231. mTargetType = POINTAT_TARGET_NONE;
  232. }
  233. void LLHUDEffectPointAt::markDead()
  234. {
  235. if (mSourceObject.notNull() && mSourceObject->isAvatar())
  236. {
  237. LLVOAvatar* av = (LLVOAvatar*)mSourceObject.get();
  238. av->removeAnimationData("PointAtPoint");
  239. }
  240. clearPointAtTarget();
  241. LLHUDEffect::markDead();
  242. }
  243. void LLHUDEffectPointAt::setSourceObject(LLViewerObject* objectp)
  244. {
  245. // Restrict source objects to avatars
  246. if (objectp && objectp->isAvatar())
  247. {
  248. LLHUDEffect::setSourceObject(objectp);
  249. }
  250. }
  251. void LLHUDEffectPointAt::render()
  252. {
  253. //MK
  254. if (gRLenabled && gRLInterface.mVisionRestricted)
  255. {
  256. return;
  257. }
  258. //mk
  259. if (sDebugPointAt && mTargetType != POINTAT_TARGET_NONE)
  260. {
  261. gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
  262. LLVector3 target = mTargetPos + mSourceObject->getRenderPosition();
  263. gGL.pushMatrix();
  264. gGL.translatef(target.mV[VX], target.mV[VY], target.mV[VZ]);
  265. gGL.scalef(0.3f, 0.3f, 0.3f);
  266. gGL.begin(LLRender::LINES);
  267. {
  268. gGL.color3f(1.f, 0.f, 0.f);
  269. gGL.vertex3f(-1.f, 0.f, 0.f);
  270. gGL.vertex3f(1.f, 0.f, 0.f);
  271. gGL.vertex3f(0.f, -1.f, 0.f);
  272. gGL.vertex3f(0.f, 1.f, 0.f);
  273. gGL.vertex3f(0.f, 0.f, -1.f);
  274. gGL.vertex3f(0.f, 0.f, 1.f);
  275. }
  276. gGL.end();
  277. gGL.popMatrix();
  278. }
  279. }
  280. // Called every frame from LLHUDManager::updateEffects()
  281. //virtual
  282. void LLHUDEffectPointAt::update()
  283. {
  284. // If the target object is dead, set the target object to NULL
  285. if (mTargetObject.notNull() && mTargetObject->isDead())
  286. {
  287. clearPointAtTarget();
  288. }
  289. if (mSourceObject.isNull() || mSourceObject->isDead())
  290. {
  291. markDead();
  292. return;
  293. }
  294. F32 time = mTimer.getElapsedTimeF32();
  295. // Clear out the effect if time is up
  296. if (mKillTime != 0.f && time > mKillTime)
  297. {
  298. mTargetType = POINTAT_TARGET_NONE;
  299. }
  300. if (mSourceObject->isAvatar())
  301. {
  302. LLVOAvatar* av = (LLVOAvatar*)mSourceObject.get();
  303. if (mTargetType == POINTAT_TARGET_NONE)
  304. {
  305. av->removeAnimationData("PointAtPoint");
  306. }
  307. else if (calcTargetPosition())
  308. {
  309. av->startMotion(ANIM_AGENT_EDITING);
  310. }
  311. }
  312. }
  313. // Returns whether we successfully calculated a finite target position.
  314. bool LLHUDEffectPointAt::calcTargetPosition()
  315. {
  316. LLViewerObject* objectp = mTargetObject.get();
  317. LLVector3 local_offset;
  318. if (objectp)
  319. {
  320. local_offset.set(mTargetOffsetGlobal);
  321. }
  322. else
  323. {
  324. local_offset = gAgent.getPosAgentFromGlobal(mTargetOffsetGlobal);
  325. }
  326. if (objectp && objectp->mDrawable.notNull())
  327. {
  328. LLQuaternion rot;
  329. if (objectp->isAvatar())
  330. {
  331. LLVOAvatar* avatarp = (LLVOAvatar*)objectp;
  332. mTargetPos = avatarp->mHeadp->getWorldPosition();
  333. rot = avatarp->mPelvisp->getWorldRotation();
  334. }
  335. else if (objectp->mDrawable->getGeneration() == -1)
  336. {
  337. mTargetPos = objectp->getPositionAgent();
  338. rot = objectp->getWorldRotation();
  339. }
  340. else
  341. {
  342. mTargetPos = objectp->getRenderPosition();
  343. rot = objectp->getRenderRotation();
  344. }
  345. mTargetPos += local_offset * rot;
  346. }
  347. else
  348. {
  349. mTargetPos = local_offset;
  350. }
  351. mTargetPos -= mSourceObject->getRenderPosition();
  352. if (!llfinite(mTargetPos.lengthSquared()))
  353. {
  354. return false;
  355. }
  356. if (mSourceObject->isAvatar())
  357. {
  358. LLVOAvatar* av = (LLVOAvatar*)mSourceObject.get();
  359. av->setAnimationData("PointAtPoint", (void*)&mTargetPos);
  360. }
  361. return true;
  362. }
  363. const LLVector3d LLHUDEffectPointAt::getPointAtPosGlobal()
  364. {
  365. LLVector3d global_pos(mTargetPos);
  366. if (mSourceObject.notNull())
  367. {
  368. global_pos += mSourceObject->getPositionGlobal();
  369. }
  370. return global_pos;
  371. }