Clone of PhatAC @ https://github.com/floaterxk/PhatAC

Portal.cpp 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #include "StdAfx.h"
  2. #include "Portal.h"
  3. #include "World.h"
  4. #include "ChatMsgs.h"
  5. #define PORTAL_TRIGGER_DISTANCE 2.0f
  6. #define PORTAL_TRIGGER_FREQUENCY 1.0f
  7. CPortal::CPortal()
  8. {
  9. m_strName = "Portal";
  10. m_wTypeID = 0x82D;
  11. m_wIcon = 0x106B;
  12. m_dwModel = 0x20001B3;
  13. m_PhysicsState = ETHEREAL_PS | REPORT_COLLISIONS_PS | LIGHTING_ON_PS | GRAVITY_PS;
  14. m_ItemType = TYPE_PORTAL;
  15. m_WeenieBitfield = BF_PORTAL | BF_STUCK | BF_ATTACKABLE;
  16. m_Usability = USEABLE_REMOTE;
  17. m_UseDistance = 20.0;
  18. m_bHasDestination = false;
  19. // Arwic, for testing if we want to
  20. m_Destination.origin = loc_t(0xC6A90023, 102.4f, 70.1f, 44.0f);
  21. m_Destination.angles = heading_t(0.70710677f, 0, 0, 0.70710677f);
  22. }
  23. CPortal::~CPortal()
  24. {
  25. }
  26. void CPortal::Precache()
  27. {
  28. // Load entity settings (location, and dynamic properties.)
  29. SetThink(&CPortal::ProximityThink);
  30. m_fNextThink = g_pGlobals->Time() + 0.1f;
  31. }
  32. void CPortal::Teleport(CPhysicsObj *pTarget)
  33. {
  34. if (m_bHasDestination)
  35. {
  36. pTarget->Movement_Teleport(m_Destination.origin, m_Destination.angles);
  37. }
  38. else
  39. {
  40. pTarget->SendNetMessage(ServerText("This portal has no destination set.", 7), PRIVATE_MSG, FALSE, TRUE);
  41. }
  42. }
  43. BOOL CPortal::ProximityThink()
  44. {
  45. // Clear old teleports cache
  46. if ((m_fLastCacheClear + PORTAL_TRIGGER_FREQUENCY) < g_pGlobals->Time())
  47. {
  48. m_RecentlyTeleported.clear();
  49. m_fLastCacheClear = g_pGlobals->Time();
  50. }
  51. std::list<CPhysicsObj *> nearbyObjects;
  52. g_pWorld->EnumNearby(this, PORTAL_TRIGGER_DISTANCE, &nearbyObjects);
  53. for (std::list<CPhysicsObj *>::iterator i = nearbyObjects.begin(); i != nearbyObjects.end(); i++)
  54. {
  55. CPhysicsObj *pOther = *i;
  56. if (pOther->IsPlayer() && !(pOther->m_PhysicsState & PhysicsState::LIGHTING_ON_PS))
  57. {
  58. if (m_RecentlyTeleported.find(pOther->m_dwGUID) == m_RecentlyTeleported.end())
  59. {
  60. Teleport(pOther);
  61. m_RecentlyTeleported.insert(pOther->m_dwGUID);
  62. }
  63. }
  64. }
  65. m_fNextThink = g_pGlobals->Time() + 0.1f;
  66. return TRUE;
  67. }
  68. void CPortal::Use(CPhysicsObj *pOther)
  69. {
  70. if (!pOther->IsPlayer())
  71. {
  72. return;
  73. }
  74. if ((Vector(pOther->m_Origin) - Vector(m_Origin)).Length() < 2.0)
  75. {
  76. Teleport(pOther);
  77. }
  78. }