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