Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

294 lines
7.9 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include <assert.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include "choreoactor.h"
  11. #include "choreochannel.h"
  12. #include "choreoscene.h"
  13. #include "tier1/utlbuffer.h"
  14. // memdbgon must be the last include file in a .cpp file!!!
  15. #include "tier0/memdbgon.h"
  16. //-----------------------------------------------------------------------------
  17. // Purpose:
  18. //-----------------------------------------------------------------------------
  19. CChoreoActor::CChoreoActor( void )
  20. {
  21. Init();
  22. }
  23. //-----------------------------------------------------------------------------
  24. // Purpose:
  25. // Input : *name -
  26. //-----------------------------------------------------------------------------
  27. CChoreoActor::CChoreoActor( const char *name )
  28. {
  29. Init();
  30. SetName( name );
  31. }
  32. //-----------------------------------------------------------------------------
  33. // Purpose: // Assignment
  34. // Input : src -
  35. // Output : CChoreoActor&
  36. //-----------------------------------------------------------------------------
  37. CChoreoActor& CChoreoActor::operator=( const CChoreoActor& src )
  38. {
  39. m_bActive = src.m_bActive;
  40. Q_strncpy( m_szName, src.m_szName, sizeof( m_szName ) );
  41. Q_strncpy( m_szFacePoserModelName, src.m_szFacePoserModelName, sizeof( m_szFacePoserModelName ) );
  42. for ( int i = 0; i < src.m_Channels.Size(); i++ )
  43. {
  44. CChoreoChannel *c = src.m_Channels[ i ];
  45. CChoreoChannel *newChannel = new CChoreoChannel();
  46. newChannel->SetActor( this );
  47. *newChannel = *c;
  48. AddChannel( newChannel );
  49. }
  50. return *this;
  51. }
  52. //-----------------------------------------------------------------------------
  53. // Purpose:
  54. //-----------------------------------------------------------------------------
  55. void CChoreoActor::Init( void )
  56. {
  57. m_szName[ 0 ] = 0;
  58. m_szFacePoserModelName[ 0 ] = 0;
  59. m_bActive = true;
  60. }
  61. //-----------------------------------------------------------------------------
  62. // Purpose:
  63. // Input : *name -
  64. //-----------------------------------------------------------------------------
  65. void CChoreoActor::SetName( const char *name )
  66. {
  67. assert( strlen( name ) < MAX_ACTOR_NAME );
  68. Q_strncpy( m_szName, name, sizeof( m_szName ) );
  69. }
  70. //-----------------------------------------------------------------------------
  71. // Purpose:
  72. // Output : const char
  73. //-----------------------------------------------------------------------------
  74. const char *CChoreoActor::GetName( void )
  75. {
  76. return m_szName;
  77. }
  78. //-----------------------------------------------------------------------------
  79. // Purpose:
  80. // Output : int
  81. //-----------------------------------------------------------------------------
  82. int CChoreoActor::GetNumChannels( void )
  83. {
  84. return m_Channels.Size();
  85. }
  86. //-----------------------------------------------------------------------------
  87. // Purpose:
  88. // Input : channel -
  89. // Output : CChoreoChannel
  90. //-----------------------------------------------------------------------------
  91. CChoreoChannel *CChoreoActor::GetChannel( int channel )
  92. {
  93. if ( channel < 0 || channel >= m_Channels.Size() )
  94. {
  95. return NULL;
  96. }
  97. return m_Channels[ channel ];
  98. }
  99. //-----------------------------------------------------------------------------
  100. // Purpose:
  101. // Input : *channel -
  102. //-----------------------------------------------------------------------------
  103. void CChoreoActor::AddChannel( CChoreoChannel *channel )
  104. {
  105. m_Channels.AddToTail( channel );
  106. }
  107. //-----------------------------------------------------------------------------
  108. // Purpose:
  109. // Input : *channel -
  110. //-----------------------------------------------------------------------------
  111. void CChoreoActor::RemoveChannel( CChoreoChannel *channel )
  112. {
  113. int idx = FindChannelIndex( channel );
  114. if ( idx == -1 )
  115. return;
  116. m_Channels.Remove( idx );
  117. }
  118. //-----------------------------------------------------------------------------
  119. // Purpose:
  120. //-----------------------------------------------------------------------------
  121. void CChoreoActor::RemoveAllChannels()
  122. {
  123. m_Channels.RemoveAll();
  124. }
  125. //-----------------------------------------------------------------------------
  126. // Purpose:
  127. // Input : c1 -
  128. // c2 -
  129. //-----------------------------------------------------------------------------
  130. void CChoreoActor::SwapChannels( int c1, int c2 )
  131. {
  132. CChoreoChannel *temp;
  133. temp = m_Channels[ c1 ];
  134. m_Channels[ c1 ] = m_Channels[ c2 ];
  135. m_Channels[ c2 ] = temp;
  136. }
  137. //-----------------------------------------------------------------------------
  138. // Purpose:
  139. // Input : *channel -
  140. // Output : int
  141. //-----------------------------------------------------------------------------
  142. int CChoreoActor::FindChannelIndex( CChoreoChannel *channel )
  143. {
  144. for ( int i = 0; i < m_Channels.Size(); i++ )
  145. {
  146. if ( channel == m_Channels[ i ] )
  147. {
  148. return i;
  149. }
  150. }
  151. return -1;
  152. }
  153. //-----------------------------------------------------------------------------
  154. // Purpose:
  155. // Input : *name -
  156. //-----------------------------------------------------------------------------
  157. void CChoreoActor::SetFacePoserModelName( const char *name )
  158. {
  159. Q_strncpy( m_szFacePoserModelName, name, sizeof( m_szFacePoserModelName ) );
  160. }
  161. //-----------------------------------------------------------------------------
  162. // Purpose:
  163. // Output : char const
  164. //-----------------------------------------------------------------------------
  165. const char *CChoreoActor::GetFacePoserModelName( void ) const
  166. {
  167. return m_szFacePoserModelName;
  168. }
  169. //-----------------------------------------------------------------------------
  170. // Purpose:
  171. // Input : active -
  172. //-----------------------------------------------------------------------------
  173. void CChoreoActor::SetActive( bool active )
  174. {
  175. m_bActive = active;
  176. }
  177. //-----------------------------------------------------------------------------
  178. // Purpose:
  179. // Output : Returns true on success, false on failure.
  180. //-----------------------------------------------------------------------------
  181. bool CChoreoActor::GetActive( void ) const
  182. {
  183. return m_bActive;
  184. }
  185. //-----------------------------------------------------------------------------
  186. // Purpose:
  187. //-----------------------------------------------------------------------------
  188. void CChoreoActor::MarkForSaveAll( bool mark )
  189. {
  190. SetMarkedForSave( mark );
  191. int c = GetNumChannels();
  192. for ( int i = 0; i < c; i++ )
  193. {
  194. CChoreoChannel *channel = GetChannel( i );
  195. channel->MarkForSaveAll( mark );
  196. }
  197. }
  198. //-----------------------------------------------------------------------------
  199. // Purpose:
  200. // Input : *name -
  201. // Output : CChoreoChannel
  202. //-----------------------------------------------------------------------------
  203. CChoreoChannel *CChoreoActor::FindChannel( const char *name )
  204. {
  205. int c = GetNumChannels();
  206. for ( int i = 0; i < c; i++ )
  207. {
  208. CChoreoChannel *channel = GetChannel( i );
  209. if ( !Q_stricmp( channel->GetName(), name ) )
  210. return channel;
  211. }
  212. return NULL;
  213. }
  214. void CChoreoActor::SaveToBuffer( CUtlBuffer& buf, CChoreoScene *pScene, IChoreoStringPool *pStringPool )
  215. {
  216. buf.PutShort( pStringPool->FindOrAddString( GetName() ) );
  217. int c = GetNumChannels();
  218. Assert( c <= 255 );
  219. buf.PutUnsignedChar( c );
  220. for ( int i = 0; i < c; i++ )
  221. {
  222. CChoreoChannel *channel = GetChannel( i );
  223. Assert( channel );
  224. channel->SaveToBuffer( buf, pScene, pStringPool );
  225. }
  226. /*
  227. if ( Q_strlen( a->GetFacePoserModelName() ) > 0 )
  228. {
  229. FilePrintf( buf, level + 1, "faceposermodel \"%s\"\n", a->GetFacePoserModelName() );
  230. }
  231. */
  232. buf.PutChar( GetActive() ? 1 : 0 );
  233. }
  234. bool CChoreoActor::RestoreFromBuffer( CUtlBuffer& buf, CChoreoScene *pScene, IChoreoStringPool *pStringPool )
  235. {
  236. char sz[ 256 ];
  237. pStringPool->GetString( buf.GetShort(), sz, sizeof( sz ) );
  238. SetName( sz );
  239. int i;
  240. int c = buf.GetUnsignedChar();
  241. for ( i = 0; i < c; i++ )
  242. {
  243. CChoreoChannel *channel = pScene->AllocChannel();
  244. Assert( channel );
  245. if ( channel->RestoreFromBuffer( buf, pScene, this, pStringPool ) )
  246. {
  247. AddChannel( channel );
  248. channel->SetActor( this );
  249. continue;
  250. }
  251. return false;
  252. }
  253. SetActive( buf.GetChar() == 1 ? true : false );
  254. return true;
  255. }