BVB Source Codes

CRYENGINE Show PlaneBreak.cpp Source code

Return Download CRYENGINE: download PlaneBreak.cpp Source code - Download CRYENGINE Source code - Type:.cpp
  1. // Copyright 2001-2016 Crytek GmbH / Crytek Group. All rights reserved.
  2.  
  3. /*************************************************************************
  4.    -------------------------------------------------------------------------
  5.    $Id$
  6.    $DateTime$
  7.    Description:  network breakability: Plane breaks
  8.    -------------------------------------------------------------------------
  9.    History:
  10.    - 22/01/2007   10:34 : Created by Craig Tiller
  11. *************************************************************************/
  12.  
  13. #include "StdAfx.h"
  14.  
  15. #if !NET_USE_SIMPLE_BREAKAGE
  16.  
  17.         #include "PlaneBreak.h"
  18.         #include "BreakReplicator.h"
  19.         #include "NetworkCVars.h"
  20.         #include "SerializeBits.h"
  21.         #include "SerializeDirHelper.h"
  22.  
  23. void LogPlaneBreak(const char* from, const SBreakEvent& evt)
  24. {
  25.         /*
  26.            if (!pEnt)
  27.             return;
  28.  
  29.            if (CNetworkCVars::Get().BreakageLog)
  30.            {
  31.             CryLog("[brk] DeformPhysicalEntity on %s @ (%.8f,%.8f,%.8f); n=(%.8f,%.8f,%.8f); energy=%.8f", from, p.x, p.y, p.z, n.x, n.y, n.z, energy);
  32.             CObjectSelector sel(pEnt);
  33.             CryLog("[brk]    selector is %s", sel.GetDescription().c_str());
  34.             switch (pEnt->GetiForeignData())
  35.             {
  36.             case PHYS_FOREIGN_ID_STATIC:
  37.               if (IRenderNode * pRN = (IRenderNode*)pEnt->GetForeignData(PHYS_FOREIGN_ID_STATIC))
  38.               {
  39.                 CryLog("[brk]    name is %s", pRN->GetName());
  40.                 CryLog("[brk]    entity class name is %s", pRN->GetEntityClassName());
  41.                 CryLog("[brk]    debug string is %s", pRN->GetDebugString().c_str());
  42.               }
  43.               break;
  44.             case PHYS_FOREIGN_ID_ENTITY:
  45.               if (IEntity * pEntity = (IEntity*)pEnt->GetForeignData(PHYS_FOREIGN_ID_ENTITY))
  46.               {
  47.                 CryLog("[brk]    name is %s", pEntity->GetName());
  48.                 Matrix34 m = pEntity->GetWorldTM();
  49.                 CryLog("[brk]    world tm:");
  50.                 for (int i=0; i<3; i++)
  51.                 {
  52.                   Vec4 row = m.GetRow4(i);
  53.                   CryLog("[brk]       %+12.8f %+12.8f %+12.8f %+12.8f", row[0], row[1], row[2], row[3]);
  54.                 }
  55.               }
  56.               break;
  57.             }
  58.            }
  59.          */
  60. }
  61.  
  62. static int s_debugId = 1;
  63.  
  64.         #pragma warning(push)
  65.         #pragma warning(disable : 6262)// 32k of stack space of CBitArray
  66. void SPlaneBreakParams::SerializeWith(TSerialize ser)
  67. {
  68.         CBitArray array(&ser);
  69.  
  70.         bool isEnt = breakEvent.itype == PHYS_FOREIGN_ID_ENTITY;
  71.         array.Serialize(breakId, 0, 6); // Must match with MAX_BREAK_STREAMS!
  72.         array.Serialize(isEnt);
  73.  
  74.         breakEvent.itype = isEnt ? PHYS_FOREIGN_ID_ENTITY : PHYS_FOREIGN_ID_STATIC;
  75.         if (isEnt)
  76.         {
  77.                 array.SerializeEntity(breakEvent.idEnt);
  78.                 array.Serialize(breakEvent.pos, 0.f, 1e4f, 20);
  79.                 array.Serialize(breakEvent.rot);
  80.         }
  81.         else
  82.         {
  83.                 array.Serialize(breakEvent.hash, 0, 0xffffffff);
  84.         }
  85.         array.Serialize(breakEvent.pt, 0.f, 1e4f, 22);
  86.  
  87.         breakEvent.partid[1] = (breakEvent.partid[1] / PARTID_CGA) << 8 | (breakEvent.partid[1] % PARTID_CGA);
  88.  
  89.         SerializeDirHelper(array, breakEvent.n, 8, 8);
  90.         array.Serialize(breakEvent.idmat[0], -1, 254);
  91.         array.Serialize(breakEvent.idmat[1], -1, 254);
  92.         array.Serialize(breakEvent.partid[1], 0, 0xffff);
  93.         array.Serialize(breakEvent.mass[0], 1.f, 1000.f, 8);
  94.         SerializeDirVector(array, breakEvent.vloc[0], 20.f, 8, 8, 8);
  95.  
  96.         breakEvent.partid[1] = (breakEvent.partid[1] & 0xff) + (breakEvent.partid[1] >> 8) * PARTID_CGA;
  97.  
  98.         // Looks like we dont need these
  99.         breakEvent.partid[0] = 0;
  100.         breakEvent.scale.Set(1.f, 1.f, 1.f);
  101.         breakEvent.radius = 0.f;
  102.         breakEvent.vloc[1].zero();   // Just assume the hitee is at zero velocity
  103.         breakEvent.energy = 0.f;
  104.         breakEvent.penetration = 0.f;
  105.         breakEvent.mass[1] = 0.f;
  106.         breakEvent.seed = 0;
  107.  
  108.         if (ser.IsWriting()) array.WriteToSerializer();
  109. }
  110.         #pragma warning(pop)
  111.  
  112. void SPlaneBreak::GetAffectedRegion(AABB& rgn)
  113. {
  114.         rgn.min = breakEvents[0].pt - Vec3(20, 20, 20);
  115.         rgn.max = breakEvents[0].pt + Vec3(20, 20, 20);
  116. }
  117.  
  118. void SPlaneBreak::AddSendables(INetSendableSink* pSink, int32 brkId)
  119. {
  120.         if (breakEvents[0].itype == PHYS_FOREIGN_ID_ENTITY)
  121.                 pSink->NextRequiresEntityEnabled(breakEvents[0].idEnt);
  122.         for (int i = 0; i < breakEvents.size(); ++i)
  123.                 CBreakReplicator::SendPlaneBreakWith(SPlaneBreakParams(brkId, breakEvents[i]), pSink);
  124.         AddProceduralSendables(brkId, pSink);
  125. }
  126.  
  127. CPlaneBreak::CPlaneBreak(const SBreakEvent& be) : IProceduralBreakType(ePBTF_ChainBreaking, 'd'), m_pPhysEnt(0)
  128. {
  129.         m_absorbIdx = 1;
  130.         m_bes.push_back(be);
  131.         m_bes.back().iState = eBES_Generated;
  132.         m_bes.back().time = gEnv->pTimer->GetFrameStartTime().GetSeconds();
  133. }
  134.  
  135. bool CPlaneBreak::AttemptAbsorb(const IProceduralBreakTypePtr& pBT)
  136. {
  137.         if (pBT->type == type)
  138.         {
  139.                 CPlaneBreak* pBrk = (CPlaneBreak*)(IProceduralBreakType*)pBT;
  140.                 if ((m_bes[0].hash && m_bes[0].hash == pBrk->m_bes[0].hash) ||
  141.                     GetObjectSelector(1) == pBrk->GetObjectSelector(1))
  142.                 {
  143.                         m_bes.push_back(pBrk->m_bes[0]);
  144.                         m_bes.back().iState = eBES_Generated;
  145.                         m_bes.back().time = gEnv->pTimer->GetFrameStartTime().GetSeconds();
  146.                         return true;
  147.                 }
  148.         }
  149.         return false;
  150. }
  151.  
  152. void CPlaneBreak::AbsorbStep()
  153. {
  154.         if (m_absorbIdx >= m_bes.size())
  155.         {
  156.                 GameWarning("CPlaneBreak::AbsorbStep: too many absorbs for structure (or there was an earlier message and we invalidated m_absorbIdx)");
  157.                 return;
  158.         }
  159.         if (!m_pPhysEnt)
  160.         {
  161.                 GameWarning("CPlaneBreak::AbsorbStep: attempt to deform a null entity");
  162.                 m_absorbIdx = m_bes.size();
  163.                 return;
  164.         }
  165.         CRY_ASSERT(m_absorbIdx < m_bes.size());
  166.         if (m_absorbIdx >= m_bes.size())
  167.                 return;
  168.         PreparePlaybackForEvent(m_absorbIdx);
  169.         if (!m_pPhysEnt)
  170.         {
  171.                 GameWarning("CPlaneBreak::AbsorbStep: attempt to deform a null entity after preparing index %d", m_absorbIdx);
  172.                 m_absorbIdx = m_bes.size();
  173.                 return;
  174.         }
  175.         LogPlaneBreak("CLIENT", m_bes[m_absorbIdx]);
  176.         CActionGame::PerformPlaneBreak(GeneratePhysicsEvent(m_bes[m_absorbIdx]), &m_bes[m_absorbIdx]);
  177.  
  178.         //      if (!gEnv->pPhysicalWorld->DeformPhysicalEntity(m_pPhysEnt, m_bes[m_absorbIdx].pt, -m_bes[m_absorbIdx].n, m_bes[m_absorbIdx].energy))
  179.         //              GameWarning("[brk] DeformPhysicalEntity failed");
  180.  
  181.         ++m_absorbIdx;
  182. }
  183.  
  184. int CPlaneBreak::GetVirtualId(IPhysicalEntity* pEnt)
  185. {
  186.         int iForeignData = pEnt->GetiForeignData();
  187.         void* pForeignData = pEnt->GetForeignData(iForeignData);
  188.         if (iForeignData == PHYS_FOREIGN_ID_ENTITY && m_bes[0].itype == PHYS_FOREIGN_ID_ENTITY)
  189.         {
  190.                 IEntity* pForeignEnt = (IEntity*) pForeignData;
  191.                 if (pForeignEnt->GetId() == m_bes[0].idEnt)
  192.                         return 1;
  193.         }
  194.         else if (iForeignData == PHYS_FOREIGN_ID_STATIC)
  195.         {
  196.                 IRenderNode* rn = (IRenderNode*) pForeignData;
  197.                 if (IsOurStatObj(rn))
  198.                         return 1;
  199.         }
  200.         return 0;
  201. }
  202.  
  203. CObjectSelector CPlaneBreak::GetObjectSelector(int idx)
  204. {
  205.         if (idx == 1)
  206.         {
  207.                 if (m_bes[0].itype == PHYS_FOREIGN_ID_ENTITY)
  208.                         return CObjectSelector(m_bes[0].idEnt);
  209.                 else if (m_bes[0].itype == PHYS_FOREIGN_ID_STATIC)
  210.                         return CObjectSelector(m_bes[0].pt, m_bes[0].hash);
  211.         }
  212.         return CObjectSelector();
  213. }
  214.  
  215. _smart_ptr<SProceduralBreak> CPlaneBreak::CompleteSend()
  216. {
  217.         SPlaneBreak* pPayload = new SPlaneBreak;
  218.         pPayload->breakEvents = m_bes;
  219.         return pPayload;
  220. }
  221.  
  222. void CPlaneBreak::PreparePlayback()
  223. {
  224.         PreparePlaybackForEvent(0);
  225. }
  226.  
  227. void CPlaneBreak::PreparePlaybackForEvent(int event)
  228. {
  229.         m_pPhysEnt = 0;
  230.         if (m_bes[event].itype == PHYS_FOREIGN_ID_ENTITY)
  231.         {
  232.                 IEntity* pEnt = gEnv->pEntitySystem->GetEntity(m_bes[event].idEnt);
  233.                 if (!pEnt)
  234.                 {
  235.                         GameWarning("Unable to find entity to perform Plane break on");
  236.                         return;
  237.                 }
  238.                 Vec3 scale = pEnt->GetScale();
  239.                 pEnt->SetPosRotScale(m_bes[event].pos, m_bes[event].rot, scale);
  240.                 m_pPhysEnt = pEnt->GetPhysics();
  241.         }
  242.         else if (m_bes[event].itype == PHYS_FOREIGN_ID_STATIC)
  243.         {
  244.                 const SBreakEvent& be = m_bes[event];
  245.                 m_pPhysEnt = CObjectSelector::FindPhysicalEntity(be.pt, be.hash, 0.03f);
  246.                 if (!m_pPhysEnt)
  247.                         GameWarning("Unable to find static object to perform Plane break on");
  248.         }
  249. }
  250.  
  251. void CPlaneBreak::BeginPlayback(bool hasJointBreaks)
  252. {
  253.         if (hasJointBreaks && CNetworkCVars::Get().BreakageLog)
  254.                 GameWarning("[brk] Plane break has joint breaks attached");
  255.         if (m_pPhysEnt)
  256.         {
  257.                 LOGBREAK("PlaneBreak::BeginPlayback");
  258.                 CActionGame::PerformPlaneBreak(GeneratePhysicsEvent(m_bes[0]), &m_bes[0]);
  259.         }
  260.         else
  261.                 GameWarning("[brk] No physical entity to deform");
  262. }
  263.  
  264. bool CPlaneBreak::GotExplosiveObjectState(const SExplosiveObjectState* pState)
  265. {
  266.         return false;
  267. }
  268.  
  269. bool CPlaneBreak::AllowComplete(const SProceduralBreakRecordingState& state)
  270. {
  271.         LOGBREAK("CPlaneBreak AllowComplete %d", state.numEmptySteps > 3);
  272.         return state.numEmptySteps > 3;
  273. }
  274.  
  275. const EventPhysCollision& CPlaneBreak::GeneratePhysicsEvent(const SBreakEvent& bev)
  276. {
  277.         static EventPhysCollision epc;
  278.         epc.pEntity[0] = 0;
  279.         epc.iForeignData[0] = -1;
  280.         epc.pEntity[1] = 0;
  281.         epc.iForeignData[1] = -1;
  282.         epc.pt = bev.pt;
  283.         epc.n = bev.n;
  284.         epc.vloc[0] = bev.vloc[0];
  285.         epc.vloc[1] = bev.vloc[1];
  286.         epc.mass[0] = bev.mass[0];
  287.         epc.mass[1] = bev.mass[1];
  288.         epc.idmat[0] = bev.idmat[0];
  289.         epc.idmat[1] = bev.idmat[1];
  290.         epc.partid[0] = bev.partid[0];
  291.         epc.partid[1] = bev.partid[1];
  292.         epc.penetration = bev.penetration;
  293.         epc.radius = bev.radius;
  294.         if (m_pPhysEnt)
  295.         {
  296.                 epc.pEntity[1] = m_pPhysEnt;
  297.                 epc.iForeignData[1] = m_pPhysEnt->GetiForeignData();
  298.                 epc.pForeignData[1] = m_pPhysEnt->GetForeignData(epc.iForeignData[1]);
  299.         }
  300.         return epc;
  301. }
  302.  
  303. #endif // !NET_USE_SIMPLE_BREAKAGE
  304.  
downloadPlaneBreak.cpp Source code - Download CRYENGINE Source code
Related Source Codes/Software:
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top