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.

347 lines
9.2 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Helper methods + classes for file access
  4. //
  5. //===========================================================================//
  6. #include "tier3/choreoutils.h"
  7. #include "tier3/tier3.h"
  8. #include "SoundEmitterSystem/isoundemittersystembase.h"
  9. #include "studio.h"
  10. #include "../game/shared/choreoscene.h"
  11. #include "../game/shared/choreoevent.h"
  12. #include "tier1/KeyValues.h"
  13. #include "bone_setup.h"
  14. #include "soundchars.h"
  15. //-----------------------------------------------------------------------------
  16. // Find sequence by name
  17. //-----------------------------------------------------------------------------
  18. static int LookupSequence( CStudioHdr *pStudioHdr, const char *pSequenceName )
  19. {
  20. for ( int i = 0; i < pStudioHdr->GetNumSeq(); i++ )
  21. {
  22. if ( !Q_stricmp( pSequenceName, pStudioHdr->pSeqdesc( i ).pszLabel() ) )
  23. return i;
  24. }
  25. return -1;
  26. }
  27. //-----------------------------------------------------------------------------
  28. // Returns sequence flags
  29. //-----------------------------------------------------------------------------
  30. static int GetSequenceFlags( CStudioHdr *pStudioHdr, int nSequence )
  31. {
  32. if ( !pStudioHdr || nSequence < 0 || nSequence >= pStudioHdr->GetNumSeq() )
  33. return 0;
  34. mstudioseqdesc_t &seqdesc = pStudioHdr->pSeqdesc( nSequence );
  35. return seqdesc.flags;
  36. }
  37. //-----------------------------------------------------------------------------
  38. // Does a sequence loop?
  39. //-----------------------------------------------------------------------------
  40. static bool DoesSequenceLoop( CStudioHdr *pStudioHdr, int nSequence )
  41. {
  42. int nFlags = GetSequenceFlags( pStudioHdr, nSequence );
  43. bool bLooping = ( nFlags & STUDIO_LOOPING ) ? true : false;
  44. return bLooping;
  45. }
  46. //-----------------------------------------------------------------------------
  47. // Purpose:
  48. //-----------------------------------------------------------------------------
  49. bool AutoAddGestureKeys( CChoreoEvent *e, CStudioHdr *pStudioHdr, float *pPoseParameters, bool bCheckOnly )
  50. {
  51. int iSequence = LookupSequence( pStudioHdr, e->GetParameters() );
  52. if ( iSequence < 0 )
  53. return false;
  54. KeyValues *pSeqKeyValues = new KeyValues( "" );
  55. if ( !pSeqKeyValues->LoadFromBuffer( pStudioHdr->pszName(), Studio_GetKeyValueText( pStudioHdr, iSequence ) ) )
  56. {
  57. pSeqKeyValues->deleteThis();
  58. return false;
  59. }
  60. // Do we have a build point section?
  61. KeyValues *pKVAllFaceposer = pSeqKeyValues->FindKey("faceposer");
  62. if ( !pKVAllFaceposer )
  63. {
  64. pSeqKeyValues->deleteThis();
  65. return false;
  66. }
  67. int nMaxFrame = Studio_MaxFrame( pStudioHdr, iSequence, pPoseParameters ) - 1;
  68. // Start grabbing the sounds and slotting them in
  69. KeyValues *pkvFaceposer;
  70. char szStartLoop[CEventAbsoluteTag::MAX_EVENTTAG_LENGTH] = { "loop" };
  71. char szEndLoop[CEventAbsoluteTag::MAX_EVENTTAG_LENGTH] = { "end" };
  72. char szEntry[CEventAbsoluteTag::MAX_EVENTTAG_LENGTH] = { "apex" };
  73. char szExit[CEventAbsoluteTag::MAX_EVENTTAG_LENGTH] = { "end" };
  74. for ( pkvFaceposer = pKVAllFaceposer->GetFirstSubKey(); pkvFaceposer; pkvFaceposer = pkvFaceposer->GetNextKey() )
  75. {
  76. if ( !Q_stricmp( pkvFaceposer->GetName(), "startloop" ) )
  77. {
  78. Q_strncpy( szStartLoop, pkvFaceposer->GetString(), sizeof(szStartLoop) );
  79. continue;
  80. }
  81. if ( !Q_stricmp( pkvFaceposer->GetName(), "endloop" ) )
  82. {
  83. Q_strncpy( szEndLoop, pkvFaceposer->GetString(), sizeof(szEndLoop) );
  84. continue;
  85. }
  86. if ( !Q_stricmp( pkvFaceposer->GetName(), "entrytag" ) )
  87. {
  88. Q_strncpy( szEntry, pkvFaceposer->GetString(), sizeof(szEntry) );
  89. continue;
  90. }
  91. if ( !Q_stricmp( pkvFaceposer->GetName(), "exittag" ) )
  92. {
  93. Q_strncpy( szExit, pkvFaceposer->GetString(), sizeof(szExit) );
  94. continue;
  95. }
  96. if ( !Q_stricmp( pkvFaceposer->GetName(), "tags" ) )
  97. {
  98. if ( nMaxFrame <= 0 )
  99. continue;
  100. KeyValues *pkvTags;
  101. for ( pkvTags = pkvFaceposer->GetFirstSubKey(); pkvTags; pkvTags = pkvTags->GetNextKey() )
  102. {
  103. float flPercentage = (float)pkvTags->GetInt() / nMaxFrame;
  104. CEventAbsoluteTag *ptag = e->FindAbsoluteTag( CChoreoEvent::ORIGINAL, pkvTags->GetName() );
  105. if (ptag)
  106. {
  107. // reposition tag
  108. ptag->SetPercentage( flPercentage );
  109. }
  110. else
  111. {
  112. e->AddAbsoluteTag( CChoreoEvent::ORIGINAL, pkvTags->GetName(), flPercentage );
  113. e->AddAbsoluteTag( CChoreoEvent::PLAYBACK, pkvTags->GetName(), flPercentage );
  114. }
  115. // lock the original tags so they can't be edited
  116. ptag = e->FindAbsoluteTag( CChoreoEvent::ORIGINAL, pkvTags->GetName() );
  117. Assert( ptag );
  118. ptag->SetLocked( true );
  119. }
  120. e->VerifyTagOrder();
  121. e->PreventTagOverlap();
  122. continue;
  123. }
  124. }
  125. // FIXME: lookup linear tags in sequence data
  126. {
  127. CEventAbsoluteTag *ptag;
  128. ptag = e->FindAbsoluteTag( CChoreoEvent::ORIGINAL, szStartLoop );
  129. if (ptag)
  130. {
  131. ptag->SetLinear( true );
  132. }
  133. ptag = e->FindAbsoluteTag( CChoreoEvent::PLAYBACK, szStartLoop );
  134. if (ptag)
  135. {
  136. ptag->SetLinear( true );
  137. }
  138. ptag = e->FindAbsoluteTag( CChoreoEvent::ORIGINAL, szEndLoop );
  139. if (ptag)
  140. {
  141. ptag->SetLinear( true );
  142. }
  143. ptag = e->FindAbsoluteTag( CChoreoEvent::PLAYBACK, szEndLoop );
  144. if (ptag)
  145. {
  146. ptag->SetLinear( true );
  147. }
  148. ptag = e->FindAbsoluteTag( CChoreoEvent::ORIGINAL, szEntry );
  149. if (ptag)
  150. {
  151. ptag->SetEntry( true );
  152. }
  153. ptag = e->FindAbsoluteTag( CChoreoEvent::PLAYBACK, szEntry );
  154. if (ptag)
  155. {
  156. ptag->SetEntry( true );
  157. }
  158. ptag = e->FindAbsoluteTag( CChoreoEvent::ORIGINAL, szExit );
  159. if (ptag)
  160. {
  161. ptag->SetExit( true );
  162. }
  163. ptag = e->FindAbsoluteTag( CChoreoEvent::PLAYBACK, szExit );
  164. if (ptag)
  165. {
  166. ptag->SetExit( true );
  167. }
  168. }
  169. pSeqKeyValues->deleteThis();
  170. return true;
  171. }
  172. //-----------------------------------------------------------------------------
  173. // Purpose:
  174. //-----------------------------------------------------------------------------
  175. bool UpdateGestureLength( CChoreoEvent *e, CStudioHdr *pStudioHdr, float *pPoseParameters, bool bCheckOnly )
  176. {
  177. Assert( e );
  178. if ( !e )
  179. return false;
  180. if ( e->GetType() != CChoreoEvent::GESTURE )
  181. return false;
  182. int iSequence = LookupSequence( pStudioHdr, e->GetParameters() );
  183. if ( iSequence < 0 )
  184. return false;
  185. bool bChanged = false;
  186. float flSeqDuration = Studio_Duration( pStudioHdr, iSequence, pPoseParameters );
  187. float flCurDuration;
  188. e->GetGestureSequenceDuration( flCurDuration );
  189. if ( flSeqDuration != 0.0f && flSeqDuration != flCurDuration )
  190. {
  191. bChanged = true;
  192. if ( !bCheckOnly )
  193. {
  194. e->SetGestureSequenceDuration( flSeqDuration );
  195. }
  196. }
  197. return bChanged;
  198. }
  199. //-----------------------------------------------------------------------------
  200. // Purpose:
  201. //-----------------------------------------------------------------------------
  202. bool UpdateSequenceLength( CChoreoEvent *e, CStudioHdr *pStudioHdr, float *pPoseParameters, bool bCheckOnly, bool bVerbose )
  203. {
  204. Assert( e );
  205. if ( !e )
  206. return false;
  207. if ( e->GetType() != CChoreoEvent::SEQUENCE )
  208. {
  209. if ( bVerbose )
  210. {
  211. ConMsg( "UpdateSequenceLength: called on non-SEQUENCE event %s\n", e->GetName() );
  212. }
  213. return false;
  214. }
  215. int iSequence = LookupSequence( pStudioHdr, e->GetParameters() );
  216. if ( iSequence < 0 )
  217. return false;
  218. bool bChanged = false;
  219. bool bLooping = DoesSequenceLoop( pStudioHdr, iSequence );
  220. float flSeqDuration = Studio_Duration( pStudioHdr, iSequence, pPoseParameters );
  221. if ( bLooping )
  222. {
  223. if ( e->IsFixedLength() )
  224. {
  225. if ( bCheckOnly )
  226. return true;
  227. if ( bVerbose )
  228. {
  229. ConMsg( "UpdateSequenceLength: %s is looping, removing fixed length flag\n", e->GetName() );
  230. }
  231. bChanged = true;
  232. }
  233. e->SetFixedLength( false );
  234. if ( !e->HasEndTime() )
  235. {
  236. if ( bCheckOnly )
  237. return true;
  238. if ( bVerbose )
  239. {
  240. ConMsg( "CheckSequenceLength: %s is looping, setting default end time\n", e->GetName() );
  241. }
  242. e->SetEndTime( e->GetStartTime() + flSeqDuration );
  243. bChanged = true;
  244. }
  245. return bChanged;
  246. }
  247. if ( !e->IsFixedLength() )
  248. {
  249. if ( bCheckOnly )
  250. return true;
  251. if ( bVerbose )
  252. {
  253. ConMsg( "CheckSequenceLength: %s is fixed length, removing looping flag\n", e->GetName() );
  254. }
  255. bChanged = true;
  256. }
  257. e->SetFixedLength( true );
  258. if ( e->HasEndTime() )
  259. {
  260. float dt = e->GetDuration();
  261. if ( fabs( dt - flSeqDuration ) > 0.01f )
  262. {
  263. if ( bCheckOnly )
  264. return true;
  265. if ( bVerbose )
  266. {
  267. ConMsg( "CheckSequenceLength: %s has wrong duration, changing length from %f to %f seconds\n",
  268. e->GetName(), dt, flSeqDuration );
  269. }
  270. bChanged = true;
  271. }
  272. }
  273. else
  274. {
  275. if ( bCheckOnly )
  276. return true;
  277. if ( bVerbose )
  278. {
  279. ConMsg( "CheckSequenceLength: %s has wrong duration, changing length to %f seconds\n",
  280. e->GetName(), flSeqDuration );
  281. }
  282. bChanged = true;
  283. }
  284. if ( !bCheckOnly )
  285. {
  286. e->SetEndTime( e->GetStartTime() + flSeqDuration );
  287. }
  288. return bChanged;
  289. }
  290. //-----------------------------------------------------------------------------
  291. // Finds sound files associated with events
  292. //-----------------------------------------------------------------------------
  293. const char *GetSoundForEvent( CChoreoEvent *pEvent, CStudioHdr *pStudioHdr )
  294. {
  295. const char *pSoundName = pEvent->GetParameters();
  296. if ( Q_stristr( pSoundName, ".wav" ) )
  297. return PSkipSoundChars( pSoundName );
  298. const char *pFileName = g_pSoundEmitterSystem->GetWavFileForSound( pSoundName, ( pStudioHdr && pStudioHdr->IsValid() ) ? pStudioHdr->pszName() : NULL );
  299. return PSkipSoundChars( pFileName );
  300. }