BVB Source Codes

CRYENGINE Show ProceduralBreakingPlaybackListener.cpp Source code

Return Download CRYENGINE: download ProceduralBreakingPlaybackListener.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: plazback of procedural 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 "ProceduralBreakingPlaybackListener.h"
  18.         #include "BreakReplicator.h"
  19.         #include "NetworkCVars.h"
  20.  
  21. CProceduralBreakingPlaybackListener::CProceduralBreakingPlaybackListener(_smart_ptr<CProceduralBreakingPlaybackStream> pStream, INetBreakagePlaybackPtr pBreakage)
  22.         : CProceduralBreakingBaseListener(pStream->m_pBreakType)
  23.         , m_pStream(pStream)
  24.         , m_pBreakage(pBreakage)
  25.         #if BREAK_HIERARCHICAL_TRACKING
  26.         , m_playbackFrame(0)
  27.         #endif
  28. {
  29.         m_gibIdx = 0;
  30. }
  31.  
  32. void CProceduralBreakingPlaybackListener::UpdateJointBreaks()
  33. {
  34.         DynArray<SJointBreakRec>& jbs = m_pStream->m_jointBreaks;
  35.  
  36.         #if BREAK_HIERARCHICAL_TRACKING
  37.         if (CNetworkCVars::Get().BreakageLog)
  38.                 CryLog("[brk] playback frame %d", m_playbackFrame);
  39.         #endif
  40.  
  41.         // replay breaks
  42.         for (size_t i = 0; i < jbs.size(); ++i)
  43.         {
  44.         #if BREAK_HIERARCHICAL_TRACKING
  45.                 if (m_createdObjects.find(jbs[i].idxRef) == m_createdObjects.end())
  46.                         continue;
  47.                 if (m_playbackFrame != jbs[i].frame)
  48.                         continue;
  49.         #endif
  50.  
  51.                 CObjectSelector obj = GetObjectSelector(jbs[i].idxRef);
  52.                 const char* desc = CObjectSelector::GetDescription(jbs[i].idxRef).c_str();
  53.  
  54.         #if BREAK_HIERARCHICAL_TRACKING
  55.                 if (CNetworkCVars::Get().BreakageLog)
  56.                         CryLog("[brk] break joint %d on ref %d (%s) [reached joint frame %d]", jbs[i].id, jbs[i].idxRef, desc, int(jbs[i].frame));
  57.         #else
  58.                 if (CNetworkCVars::Get().BreakageLog)
  59.                         CryLog("[brk] break joint %d on ref %d (%s)", jbs[i].id, jbs[i].idxRef, desc);
  60.         #endif
  61.  
  62.                 pe_params_structural_joint pj;
  63.                 pj.partidEpicenter = jbs[i].epicenter;
  64.                 if (jbs[i].id >= 0)
  65.                 {
  66.                         pj.id = jbs[i].id;
  67.                         pj.bBroken = true;
  68.                         pj.bReplaceExisting = true;
  69.                 }
  70.                 else
  71.                 {
  72.                         pj.idx = -1;
  73.                 }
  74.                 bool succeeded = false;
  75.                 if (IPhysicalEntity* pEnt = obj.Find())
  76.                 {
  77.                         if (!pEnt->SetParams(&pj))
  78.                                 GameWarning("Failed to break joint-id %d on %s", pj.id, desc);
  79.                         else
  80.                                 succeeded = true;
  81.                 }
  82.                 else
  83.                         GameWarning("Unable to find object %s to break joint-id %d on", desc, pj.id);
  84.  
  85.                 if (succeeded)
  86.                 {
  87.                         jbs.erase(i);
  88.                         --i;
  89.                 }
  90.         }
  91. }
  92.  
  93. void CProceduralBreakingPlaybackListener::Start()
  94. {
  95.         char message[256];
  96.         cry_sprintf(message, "begin playback %d", m_pStream->m_magicId);
  97.         DumpSpawnRecs(m_pStream->m_spawnRecs, m_pStream->m_jointBreaks, message);
  98.         #if BREAK_HIERARCHICAL_TRACKING
  99.         DynArray<SJointBreakRec>& jbs = m_pStream->m_jointBreaks;
  100.         for (size_t i = 0; i < jbs.size(); ++i)
  101.                 if (jbs[i].idxRef < 0)
  102.                         m_createdObjects.insert(jbs[i].idxRef);
  103.         #endif
  104.         UpdateJointBreaks();
  105.         CompleteStepFrom(m_pStream->m_spawnRecs.begin());
  106. }
  107.  
  108. int CProceduralBreakingPlaybackListener::GotBreakOp(ENetBreakOperation op, int idxRef, int partid, EntityId to)
  109. {
  110.         DynArray<SProceduralSpawnRec>& spawnRecs = m_pStream->m_spawnRecs;
  111.         bool priorAreInvalidated = true;
  112.  
  113.         for (DynArray<SProceduralSpawnRec>::iterator it = spawnRecs.begin(); it != spawnRecs.end(); ++it)
  114.         {
  115.                 bool priorWereInvalidated = priorAreInvalidated;
  116.                 priorAreInvalidated &= (it->op == eNBO_NUM_OPERATIONS);
  117.                 if (it->op != op)
  118.                         continue;
  119.                 if ((BIT(op) & OPS_REFERENCING_ENTS))
  120.                         if (it->idxRef != idxRef)
  121.                                 continue;
  122.                 if ((BIT(op) & OPS_WITH_PARTIDS))
  123.                         if (it->partid != partid)
  124.                                 continue;
  125.         #if BREAK_HIERARCHICAL_TRACKING
  126.                 if (!priorWereInvalidated)
  127.                 {
  128.                         if (op == eNBO_PostStep)
  129.                                 break;
  130.                         else if (BIT(op) & OPS_JOINT_FRAME_COUNTERS)
  131.                                 it->op = eNBO_FrameCounterFinished;
  132.                 }
  133.                 if (it->op == op) // still
  134.                 {
  135.                         it->op = eNBO_NUM_OPERATIONS; // invalidate
  136.                         if (BIT(op) & OPS_JOINT_FRAME_COUNTERS)
  137.                                 IncrementFrame();
  138.                 }
  139.         #else
  140.                 it->op = eNBO_NUM_OPERATIONS;
  141.         #endif
  142.                 m_pBreakage->SpawnedEntity(it->idx, to);
  143.                 if (priorWereInvalidated)
  144.                         CompleteStepFrom(it + 1);
  145.                 return it->idx;
  146.         }
  147.         #if BREAK_HIERARCHICAL_TRACKING
  148.         if (op != eNBO_PostStep)
  149.         #endif
  150.         GameWarning("CProceduralBreakingPlaybackListener: mismatched spawn event %d:%d[part=%d]->%d", op, idxRef, partid, to);
  151.         return -1;
  152. }
  153.  
  154.         #if BREAK_HIERARCHICAL_TRACKING
  155. void CProceduralBreakingPlaybackListener::IncrementFrame()
  156. {
  157.         m_playbackFrame++;
  158.         UpdateJointBreaks();
  159. }
  160.         #endif
  161.  
  162. void CProceduralBreakingPlaybackListener::CompleteStepFrom(DynArray<SProceduralSpawnRec>::iterator it)
  163. {
  164.         DynArray<SProceduralSpawnRec>& spawnRecs = m_pStream->m_spawnRecs;
  165.         while (it != spawnRecs.end())
  166.         {
  167.                 switch (it->op)
  168.                 {
  169.                 case eNBO_AbsorbStep:
  170.                         GetBreakType()->AbsorbStep();
  171.                         it->op = eNBO_NUM_OPERATIONS;
  172.                         break;
  173.         #if BREAK_HIERARCHICAL_TRACKING
  174.                 case eNBO_FrameCounterFinished:
  175.                         it->op = eNBO_NUM_OPERATIONS;
  176.                         IncrementFrame();
  177.                         break;
  178.         #endif
  179.                 }
  180.                 if (it->op == eNBO_NUM_OPERATIONS)
  181.                         ++it;
  182.                 else
  183.                         break;
  184.         }
  185. }
  186.  
  187. void CProceduralBreakingPlaybackListener::FinishBreakOp(ENetBreakOperation op, int idxRef, EntityId to, int virtId)
  188. {
  189.         #if BREAK_HIERARCHICAL_TRACKING
  190.         if (m_createdObjects.insert(virtId).second)
  191.                 UpdateJointBreaks();
  192.         #endif
  193. }
  194.  
  195. void CProceduralBreakingPlaybackListener::GotJointBreakParticle(const EventPhysCreateEntityPart* pEvent)
  196. {
  197.         // This is experimental serialisation of the gib particle velocity
  198.         const unsigned int i = m_gibIdx;
  199.         DynArray<SJointBreakParticleRec>& gibs = m_pStream->m_gibs;
  200.         if (i < (unsigned)gibs.size())
  201.         {
  202.                 LOGBREAK("UpdateJointBreaks: applying gib Vel (%f, %f, %f)", XYZ(gibs[i].vel));
  203.                 pe_action_set_velocity setVel;
  204.                 setVel.v = gibs[i].vel;
  205.                 pEvent->pEntNew->Action(&setVel);
  206.         }
  207.         m_gibIdx++;
  208. }
  209.  
  210. void CProceduralBreakingPlaybackListener::Complete()
  211. {
  212.         char message[256];
  213.         cry_sprintf(message, "end playback %d", m_pStream->m_magicId);
  214.         DumpSpawnRecs(m_pStream->m_spawnRecs, m_pStream->m_jointBreaks, message);
  215. }
  216.  
  217. bool CProceduralBreakingPlaybackListener::AllowComplete()
  218. {
  219.         if (!m_pStream->m_jointBreaks.empty())
  220.                 return false;
  221.         for (size_t i = 0; i < m_pStream->m_spawnRecs.size(); i++)
  222.                 if (m_pStream->m_spawnRecs[i].op != eNBO_NUM_OPERATIONS)
  223.                         return false;
  224.         return true;
  225. }
  226.  
  227. CProceduralBreakingPlaybackStream::CProceduralBreakingPlaybackStream(IProceduralBreakTypePtr pBreakType) : m_pBreakType(pBreakType), m_magicId(-1)
  228. {
  229. }
  230.  
  231. CProceduralBreakingPlaybackStream::~CProceduralBreakingPlaybackStream()
  232. {
  233. }
  234.  
  235. bool CProceduralBreakingPlaybackStream::SetMagicId(int id)
  236. {
  237.         if (m_magicId >= 0 || id < 0)
  238.                 return false;
  239.         m_magicId = id;
  240.         return true;
  241. }
  242.  
  243. bool CProceduralBreakingPlaybackStream::GotExplosiveObjectState(const SExplosiveObjectState* state)
  244. {
  245.         return m_pBreakType->GotExplosiveObjectState(state);
  246. }
  247.  
  248. bool CProceduralBreakingPlaybackStream::GotJointBreakRec(const SJointBreakRec* rec)
  249. {
  250.         m_jointBreaks.push_back(*rec);
  251.         return true;
  252. }
  253.  
  254. bool CProceduralBreakingPlaybackStream::GotJointBreakParticleRec(const SJointBreakParticleRec* rec)
  255. {
  256.         m_gibs.push_back(*rec);
  257.         return true;
  258. }
  259.  
  260. bool CProceduralBreakingPlaybackStream::GotProceduralSpawnRec(const SProceduralSpawnRec* rec)
  261. {
  262.         CRY_ASSERT(rec->op >= eNBO_Create && rec->op < eNBO_NUM_OPERATIONS);
  263.         m_spawnRecs.push_back(*rec);
  264.         return true;
  265. }
  266.  
  267. IBreakReplicatorListenerPtr CProceduralBreakingPlaybackStream::Playback(CBreakReplicator* pReplicator, INetBreakagePlaybackPtr pBreak)
  268. {
  269.         bool hasJointBreaks = !m_jointBreaks.empty();
  270.         _smart_ptr<CProceduralBreakingPlaybackListener> pListener = new CProceduralBreakingPlaybackListener(this, pBreak);
  271.         pReplicator->AddListener(&*pListener, CNetworkCVars::Get().BreakTimeoutFrames);
  272.         m_pBreakType->PreparePlayback();
  273.         pListener->Start();
  274.         m_pBreakType->BeginPlayback(hasJointBreaks);
  275.         return &*pListener;
  276. }
  277.  
  278. bool CProceduralBreakingPlaybackStream::AttemptAbsorb(const IProceduralBreakTypePtr& pBT)
  279. {
  280.         return m_pBreakType->AttemptAbsorb(pBT);
  281. }
  282.  
  283. #endif // !NET_USE_SIMPLE_BREAKAGE
  284.  
downloadProceduralBreakingPlaybackListener.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