BVB Source Codes

CRYENGINE Show DeformingBreak.cpp Source code

Return Download CRYENGINE: download DeformingBreak.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: deforming breaks
  8.    -------------------------------------------------------------------------
  9.    History:
  10.    - 22/01/2007   10:34 : Created by Craig Tiller
  11. *************************************************************************/
  12.  
  13. #include "StdAfx.h"
  14. #include "DeformingBreak.h"
  15. #include "BreakReplicator.h"
  16. #include "NetworkCVars.h"
  17. #include "ObjectSelector.h"
  18.  
  19. void LogDeformPhysicalEntity(const char* from, IPhysicalEntity* pEnt, const Vec3& p, const Vec3& n, float energy)
  20. {
  21. #if DEBUG_NET_BREAKAGE
  22.         if (!pEnt)
  23.                 return;
  24.  
  25.         if (CNetworkCVars::Get().BreakageLog)
  26.         {
  27.                 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);
  28.                 CryLog("[brk]    selector is %s", CObjectSelector::GetDescription(pEnt).c_str());
  29.                 switch (pEnt->GetiForeignData())
  30.                 {
  31.                 case PHYS_FOREIGN_ID_STATIC:
  32.                         if (IRenderNode* pRN = (IRenderNode*)pEnt->GetForeignData(PHYS_FOREIGN_ID_STATIC))
  33.                         {
  34.                                 CryLog("[brk]    name is %s", pRN->GetName());
  35.                                 CryLog("[brk]    entity class name is %s", pRN->GetEntityClassName());
  36.                                 CryLog("[brk]    debug string is %s", pRN->GetDebugString().c_str());
  37.                         }
  38.                         break;
  39.                 case PHYS_FOREIGN_ID_ENTITY:
  40.                         if (IEntity* pEntity = (IEntity*)pEnt->GetForeignData(PHYS_FOREIGN_ID_ENTITY))
  41.                         {
  42.                                 CryLog("[brk]    name is %s", pEntity->GetName());
  43.                                 Matrix34 m = pEntity->GetWorldTM();
  44.                                 CryLog("[brk]    world tm:");
  45.                                 for (int i = 0; i < 3; i++)
  46.                                 {
  47.                                         Vec4 row = m.GetRow4(i);
  48.                                         CryLog("[brk]       %+12.8f %+12.8f %+12.8f %+12.8f", row[0], row[1], row[2], row[3]);
  49.                                 }
  50.                         }
  51.                         break;
  52.                 }
  53.         }
  54. #endif
  55. }
  56.  
  57. #if !NET_USE_SIMPLE_BREAKAGE
  58.  
  59. void SDeformingBreakParams::SerializeWith(TSerialize ser)
  60. {
  61.         LOGBREAK("SDeformingBreakParams: %s", ser.IsReading() ? "Reading:" : "Writing");
  62.  
  63.         ser.Value("breakId", breakId, 'brId');
  64.         bool isEnt = breakEvent.itype == PHYS_FOREIGN_ID_ENTITY;
  65.         ser.Value("isEnt", isEnt, 'bool');
  66.         breakEvent.itype = isEnt ? PHYS_FOREIGN_ID_ENTITY : PHYS_FOREIGN_ID_STATIC;
  67.         if (isEnt)
  68.         {
  69.                 ser.Value("ent", breakEvent.idEnt, 'eid');
  70.                 ser.Value("pos", breakEvent.pos);
  71.                 ser.Value("rot", breakEvent.rot);
  72.                 ser.Value("scale", breakEvent.scale);
  73.         }
  74.         else
  75.         {
  76.                 ser.Value("eventPos", breakEvent.eventPos);
  77.                 ser.Value("hash", breakEvent.hash);
  78.         }
  79.         ser.Value("pt", breakEvent.pt);
  80.         ser.Value("n", breakEvent.n);
  81.         ser.Value("energy", breakEvent.energy);
  82. }
  83.  
  84. void SDeformingBreak::GetAffectedRegion(AABB& rgn)
  85. {
  86.         rgn.min = breakEvents[0].pt - Vec3(20, 20, 20);
  87.         rgn.max = breakEvents[0].pt + Vec3(20, 20, 20);
  88. }
  89.  
  90. void SDeformingBreak::AddSendables(INetSendableSink* pSink, int32 brkId)
  91. {
  92.         if (breakEvents[0].itype == PHYS_FOREIGN_ID_ENTITY)
  93.                 pSink->NextRequiresEntityEnabled(breakEvents[0].idEnt);
  94.         for (int i = 0; i < breakEvents.size(); ++i)
  95.                 CBreakReplicator::SendDeformingBreakWith(SDeformingBreakParams(brkId, breakEvents[i]), pSink);
  96.         AddProceduralSendables(brkId, pSink);
  97. }
  98.  
  99. CDeformingBreak::CDeformingBreak(const SBreakEvent& be) : IProceduralBreakType(ePBTF_ChainBreaking, 'd'), m_pPhysEnt(0)
  100. {
  101.         m_absorbIdx = 1;
  102.         m_bes.push_back(be);
  103.         m_bes.back().iState = eBES_Generated;
  104.         m_bes.back().time = gEnv->pTimer->GetFrameStartTime().GetSeconds();
  105. }
  106.  
  107. bool CDeformingBreak::AttemptAbsorb(const IProceduralBreakTypePtr& pBT)
  108. {
  109.         if (pBT->type == type)
  110.         {
  111.                 CDeformingBreak* pBrk = (CDeformingBreak*)(IProceduralBreakType*)pBT;
  112.                 if (GetObjectSelector(1) == pBrk->GetObjectSelector(1))
  113.                 {
  114.                         m_bes.push_back(pBrk->m_bes[0]);
  115.                         return true;
  116.                 }
  117.         }
  118.         return false;
  119. }
  120.  
  121. void CDeformingBreak::AbsorbStep()
  122. {
  123.         if (m_absorbIdx >= m_bes.size())
  124.         {
  125.                 GameWarning("CDeformingBreak::AbsorbStep: too many absorbs for structure (or there was an earlier message and we invalidated m_absorbIdx)");
  126.                 return;
  127.         }
  128.         if (!m_pPhysEnt)
  129.         {
  130.                 GameWarning("CDeformingBreak::AbsorbStep: attempt to deform a null entity");
  131.                 m_absorbIdx = m_bes.size();
  132.                 return;
  133.         }
  134.         CRY_ASSERT(m_absorbIdx < m_bes.size());
  135.         if (m_absorbIdx >= m_bes.size())
  136.                 return;
  137.         PreparePlaybackForEvent(m_absorbIdx);
  138.         if (!m_pPhysEnt)
  139.         {
  140.                 GameWarning("CDeformingBreak::AbsorbStep: attempt to deform a null entity after preparing index %d", m_absorbIdx);
  141.                 m_absorbIdx = m_bes.size();
  142.                 return;
  143.         }
  144.         LogDeformPhysicalEntity("CLIENT", m_pPhysEnt, m_bes[m_absorbIdx].pt, -m_bes[m_absorbIdx].n, m_bes[m_absorbIdx].energy);
  145.         if (!gEnv->pPhysicalWorld->DeformPhysicalEntity(m_pPhysEnt, m_bes[m_absorbIdx].pt, -m_bes[m_absorbIdx].n, m_bes[m_absorbIdx].energy))
  146.                 GameWarning("[brk] DeformPhysicalEntity failed");
  147.         ++m_absorbIdx;
  148. }
  149.  
  150. int CDeformingBreak::GetVirtualId(IPhysicalEntity* pEnt)
  151. {
  152.         int iForeignData = pEnt->GetiForeignData();
  153.         void* pForeignData = pEnt->GetForeignData(iForeignData);
  154.         if (iForeignData == PHYS_FOREIGN_ID_ENTITY && m_bes[0].itype == PHYS_FOREIGN_ID_ENTITY)
  155.         {
  156.                 IEntity* pForeignEnt = (IEntity*) pForeignData;
  157.                 if (pForeignEnt->GetId() == m_bes[0].idEnt)
  158.                         return 1;
  159.         }
  160.         else if (iForeignData == PHYS_FOREIGN_ID_STATIC)
  161.         {
  162.                 IRenderNode* rn = (IRenderNode*) pForeignData;
  163.                 if (IsOurStatObj(rn))
  164.                         return 1;
  165.         }
  166.         return 0;
  167. }
  168.  
  169. CObjectSelector CDeformingBreak::GetObjectSelector(int idx)
  170. {
  171.         if (idx == 1)
  172.         {
  173.                 if (m_bes[0].itype == PHYS_FOREIGN_ID_ENTITY)
  174.                         return CObjectSelector(m_bes[0].idEnt);
  175.                 else if (m_bes[0].itype == PHYS_FOREIGN_ID_STATIC)
  176.                         return CObjectSelector(m_bes[0].eventPos, m_bes[0].hash);
  177.         }
  178.         return CObjectSelector();
  179. }
  180.  
  181. _smart_ptr<SProceduralBreak> CDeformingBreak::CompleteSend()
  182. {
  183.         SDeformingBreak* pPayload = new SDeformingBreak;
  184.         pPayload->breakEvents = m_bes;
  185.         return pPayload;
  186. }
  187.  
  188. void CDeformingBreak::PreparePlayback()
  189. {
  190.         PreparePlaybackForEvent(0);
  191. }
  192.  
  193. void CDeformingBreak::PreparePlaybackForEvent(int event)
  194. {
  195.         m_pPhysEnt = 0;
  196.         if (m_bes[event].itype == PHYS_FOREIGN_ID_ENTITY)
  197.         {
  198.                 IEntity* pEnt = gEnv->pEntitySystem->GetEntity(m_bes[event].idEnt);
  199.                 if (!pEnt)
  200.                 {
  201.                         GameWarning("Unable to find entity to perform deforming break on");
  202.                         return;
  203.                 }
  204.                 pEnt->SetPosRotScale(m_bes[event].pos, m_bes[event].rot, m_bes[event].scale);
  205.                 m_pPhysEnt = pEnt->GetPhysics();
  206.         }
  207.         else if (m_bes[event].itype == PHYS_FOREIGN_ID_STATIC)
  208.         {
  209.                 m_pPhysEnt = CObjectSelector::FindPhysicalEntity(m_bes[event].eventPos, m_bes[event].hash, CObjectSelector_Eps);
  210.                 if (!m_pPhysEnt)
  211.                         GameWarning("Unable to find static object to perform deforming break on");
  212.         }
  213. }
  214.  
  215. void CDeformingBreak::BeginPlayback(bool hasJointBreaks)
  216. {
  217.         if (hasJointBreaks && CNetworkCVars::Get().BreakageLog)
  218.                 GameWarning("[brk] deforming break has joint breaks attached");
  219.         if (m_pPhysEnt)
  220.         {
  221.                 LogDeformPhysicalEntity("CLIENT", m_pPhysEnt, m_bes[0].pt, -m_bes[0].n, m_bes[0].energy);
  222.                 if (!gEnv->pPhysicalWorld->DeformPhysicalEntity(m_pPhysEnt, m_bes[0].pt, -m_bes[0].n, m_bes[0].energy))
  223.                         GameWarning("[brk] DeformPhysicalEntity failed");
  224.         }
  225.         else
  226.                 GameWarning("[brk] No physical entity to deform");
  227. }
  228.  
  229. bool CDeformingBreak::GotExplosiveObjectState(const SExplosiveObjectState* pState)
  230. {
  231.         return false;
  232. }
  233.  
  234. bool CDeformingBreak::AllowComplete(const SProceduralBreakRecordingState& state)
  235. {
  236.         return state.numEmptySteps > 3;
  237. }
  238.  
  239. #endif // !NET_USE_SIMPLE_BREAKAGE
  240.  
downloadDeformingBreak.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