forked from nillerusr/source-engine
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenericactor.cpp
459 lines (378 loc) · 11.8 KB
/
genericactor.cpp
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//
//=============================================================================//
//=========================================================
// Generic NPC - purely for scripted sequence work.
//=========================================================
#include "cbase.h"
#include "shareddefs.h"
#include "npcevent.h"
#include "ai_basenpc.h"
#include "ai_hull.h"
#include "ai_baseactor.h"
#include "tier1/strtools.h"
#include "vstdlib/random.h"
#include "engine/IEngineSound.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
ConVar flex_looktime( "flex_looktime", "5" );
//---------------------------------------------------------
// Sounds
//---------------------------------------------------------
//=========================================================
// NPC's Anim Events Go Here
//=========================================================
class CGenericActor : public CAI_BaseActor
{
public:
DECLARE_CLASS( CGenericActor, CAI_BaseActor );
void Spawn( void );
void Precache( void );
float MaxYawSpeed( void );
Class_T Classify ( void );
void HandleAnimEvent( animevent_t *pEvent );
int GetSoundInterests ( void );
void TempGunEffect( void );
string_t m_strHullName;
DECLARE_DATADESC();
};
LINK_ENTITY_TO_CLASS( generic_actor, CGenericActor );
BEGIN_DATADESC( CGenericActor )
DEFINE_KEYFIELD(m_strHullName, FIELD_STRING, "hull_name" ),
END_DATADESC()
//=========================================================
// Classify - indicates this NPC's place in the
// relationship table.
//=========================================================
Class_T CGenericActor::Classify ( void )
{
return CLASS_NONE;
}
//=========================================================
// MaxYawSpeed - allows each sequence to have a different
// turn rate associated with it.
//=========================================================
float CGenericActor::MaxYawSpeed ( void )
{
return 90;
}
//=========================================================
// HandleAnimEvent - catches the NPC-specific messages
// that occur when tagged animation frames are played.
//=========================================================
void CGenericActor::HandleAnimEvent( animevent_t *pEvent )
{
BaseClass::HandleAnimEvent( pEvent );
}
//=========================================================
// GetSoundInterests - generic NPC can't hear.
//=========================================================
int CGenericActor::GetSoundInterests ( void )
{
return 0;
}
//=========================================================
// Spawn
//=========================================================
void CGenericActor::Spawn()
{
Precache();
SetModel( STRING( GetModelName() ) );
/*
if ( FStrEq( STRING( GetModelName() ), "models/player.mdl" ) )
UTIL_SetSize(this, VEC_HUMAN_HULL_MIN, VEC_HUMAN_HULL_MAX);
else
UTIL_SetSize(this, VEC_HULL_MIN, VEC_HULL_MAX);
*/
if ( FStrEq( STRING( GetModelName() ), "models/player.mdl" ) ||
FStrEq( STRING( GetModelName() ), "models/holo.mdl" ) ||
FStrEq( STRING( GetModelName() ), "models/blackout.mdl" ) )
{
UTIL_SetSize(this, VEC_HULL_MIN, VEC_HULL_MAX);
}
else
{
UTIL_SetSize(this, NAI_Hull::Mins(HULL_HUMAN), NAI_Hull::Maxs(HULL_HUMAN));
}
if ( !FStrEq( STRING( GetModelName() ), "models/blackout.mdl" ) )
{
SetSolid( SOLID_BBOX );
AddSolidFlags( FSOLID_NOT_STANDABLE );
}
else
{
SetSolid( SOLID_NONE );
}
SetMoveType( MOVETYPE_STEP );
SetBloodColor( BLOOD_COLOR_RED );
m_iHealth = 8;
m_flFieldOfView = 0.5;// indicates the width of this NPC's forward view cone ( as a dotproduct result )
m_NPCState = NPC_STATE_NONE;
CapabilitiesAdd( bits_CAP_MOVE_GROUND | bits_CAP_OPEN_DOORS );
// remove head turn if no eyes or forward attachment
if (LookupAttachment( "eyes" ) > 0 && LookupAttachment( "forward" ) > 0)
{
CapabilitiesAdd( bits_CAP_TURN_HEAD | bits_CAP_ANIMATEDFACE );
}
if (m_strHullName != NULL_STRING)
{
SetHullType( NAI_Hull::LookupId( STRING( m_strHullName ) ) );
}
else
{
SetHullType( HULL_HUMAN );
}
SetHullSizeNormal( );
NPCInit();
}
//=========================================================
// Precache - precaches all resources this NPC needs
//=========================================================
void CGenericActor::Precache()
{
PrecacheModel( STRING( GetModelName() ) );
}
//=========================================================
// AI Schedules Specific to this NPC
//=========================================================
// -----------------------------------------------------------------------
// FIXME: delete this code
class CFlextalkActor : public CGenericActor
{
private:
DECLARE_CLASS( CFlextalkActor, CGenericActor );
public:
DECLARE_DATADESC();
CFlextalkActor() { m_iszSentence = NULL_STRING; m_sentence = 0; }
//void GenericCyclerSpawn(char *szModel, Vector vecMin, Vector vecMax);
//virtual int ObjectCaps( void ) { return (BaseClass::ObjectCaps() | FCAP_IMPULSE_USE); }
//int OnTakeDamage( CBaseEntity *pInflictor, CBaseEntity *pAttacker, float flDamage, int bitsDamageType );
//void Spawn( void );
//void Precache( void );
//void Think( void );
virtual void ProcessSceneEvents( void );
// Don't treat as a live target
//virtual bool IsAlive( void ) { return FALSE; }
float m_flextime;
LocalFlexController_t m_flexnum;
float m_flextarget[64];
float m_blinktime;
float m_looktime;
Vector m_lookTarget;
float m_speaktime;
int m_istalking;
int m_phoneme;
string_t m_iszSentence;
int m_sentence;
void SetFlexTarget( LocalFlexController_t flexnum, float value );
LocalFlexController_t LookupFlex( const char *szTarget );
};
BEGIN_DATADESC( CFlextalkActor )
DEFINE_FIELD( m_flextime, FIELD_TIME ),
DEFINE_FIELD( m_flexnum, FIELD_INTEGER ),
DEFINE_ARRAY( m_flextarget, FIELD_FLOAT, 64 ),
DEFINE_FIELD( m_blinktime, FIELD_TIME ),
DEFINE_FIELD( m_looktime, FIELD_TIME ),
DEFINE_FIELD( m_lookTarget, FIELD_POSITION_VECTOR ),
DEFINE_FIELD( m_speaktime, FIELD_TIME ),
DEFINE_FIELD( m_istalking, FIELD_INTEGER ),
DEFINE_FIELD( m_phoneme, FIELD_INTEGER ),
DEFINE_KEYFIELD( m_iszSentence, FIELD_STRING, "Sentence" ),
DEFINE_FIELD( m_sentence, FIELD_INTEGER ),
END_DATADESC()
LINK_ENTITY_TO_CLASS( cycler_actor, CFlextalkActor );
extern ConVar flex_expression;
extern ConVar flex_talk;
// Cycler member functions
extern const char *predef_flexcontroller_names[];
extern float predef_flexcontroller_values[7][30];
void CFlextalkActor::SetFlexTarget( LocalFlexController_t flexnum, float value )
{
m_flextarget[flexnum] = value;
const char *pszType = GetFlexControllerType( flexnum );
for (LocalFlexController_t i = LocalFlexController_t(0); i < GetNumFlexControllers(); i++)
{
if (i != flexnum)
{
const char *pszOtherType = GetFlexControllerType( i );
if (stricmp( pszType, pszOtherType ) == 0)
{
m_flextarget[i] = 0;
}
}
}
float value2 = value;
if (1 || random->RandomFloat( 0.0, 1.0 ) < 0.2)
{
value2 = random->RandomFloat( value - 0.2, value + 0.2 );
value2 = clamp( value2, 0.0f, 1.0f );
}
// HACK, for now, consider then linked is named "right_" or "left_"
if (strncmp( "right_", GetFlexControllerName( flexnum ), 6 ) == 0)
{
m_flextarget[flexnum+1] = value2;
}
else if (strncmp( "left_", GetFlexControllerName( flexnum ), 5 ) == 0)
{
m_flextarget[flexnum-1] = value2;
}
}
LocalFlexController_t CFlextalkActor::LookupFlex( const char *szTarget )
{
for (LocalFlexController_t i = LocalFlexController_t(0); i < GetNumFlexControllers(); i++)
{
const char *pszFlex = GetFlexControllerName( i );
if (stricmp( szTarget, pszFlex ) == 0)
{
return i;
}
}
return LocalFlexController_t(-1);
}
void CFlextalkActor::ProcessSceneEvents( void )
{
if ( HasSceneEvents() )
{
BaseClass::ProcessSceneEvents( );
return;
}
// only do this if they have more than eyelid movement
if (GetNumFlexControllers() > 2)
{
const char *pszExpression = flex_expression.GetString();
if (pszExpression && pszExpression[0] == '+' && pszExpression[1] != '\0')
{
int i;
int j = atoi( &pszExpression[1] );
for (i = 0; i < GetNumFlexControllers(); i++)
{
m_flextarget[m_flexnum] = 0;
}
for (i = 0; i < 35 && predef_flexcontroller_names[i]; i++)
{
m_flexnum = LookupFlex( predef_flexcontroller_names[i] );
m_flextarget[m_flexnum] = predef_flexcontroller_values[j][i];
// Msg( "%s %.3f\n", predef_flexcontroller_names[i], predef_flexcontroller_values[j][i] );
}
}
else if (pszExpression && pszExpression[0] != '\0' && strcmp(pszExpression, "+") != 0)
{
char szExpression[128];
char szTemp[32];
Q_strncpy( szExpression, pszExpression ,sizeof(szExpression));
char *pszExpression = szExpression;
while (*pszExpression != '\0')
{
if (*pszExpression == '+')
*pszExpression = ' ';
pszExpression++;
}
pszExpression = szExpression;
while (*pszExpression)
{
if (*pszExpression != ' ')
{
if (*pszExpression == '-')
{
for (LocalFlexController_t i = LocalFlexController_t(0); i < GetNumFlexControllers(); i++)
{
m_flextarget[i] = 0;
}
}
else if (*pszExpression == '?')
{
for (LocalFlexController_t i = LocalFlexController_t(0); i < GetNumFlexControllers(); i++)
{
Msg( "\"%s\" ", GetFlexControllerName( i ) );
}
Msg( "\n" );
flex_expression.SetValue( "" );
}
else
{
if (sscanf( pszExpression, "%31s", szTemp ) == 1)
{
m_flexnum = LookupFlex( szTemp );
if (m_flexnum != -1 && m_flextarget[m_flexnum] != 1)
{
m_flextarget[m_flexnum] = 1.0;
// SetFlexTarget( m_flexnum );
}
pszExpression += strlen( szTemp ) - 1;
}
}
}
pszExpression++;
}
}
else if (m_flextime < gpGlobals->curtime)
{
m_flextime = gpGlobals->curtime + random->RandomFloat( 0.3, 0.5 ) * (30.0 / GetNumFlexControllers());
m_flexnum = (LocalFlexController_t)random->RandomInt( 0, GetNumFlexControllers() - 1 );
if (m_flextarget[m_flexnum] == 1)
{
m_flextarget[m_flexnum] = 0;
}
else if (stricmp( GetFlexControllerType( m_flexnum ), "phoneme" ) != 0)
{
if (strstr( GetFlexControllerName( m_flexnum ), "upper_raiser" ) == NULL)
{
Msg( "%s:%s\n", GetFlexControllerType( m_flexnum ), GetFlexControllerName( m_flexnum ) );
SetFlexTarget( m_flexnum, random->RandomFloat( 0.5, 1.0 ) );
}
}
}
// slide it up.
for (LocalFlexController_t i = LocalFlexController_t(0); i < GetNumFlexControllers(); i++)
{
float weight = GetFlexWeight( i );
if (weight != m_flextarget[i])
{
weight = weight + (m_flextarget[i] - weight) / random->RandomFloat( 2.0, 4.0 );
}
weight = clamp( weight, 0.0f, 1.0f );
SetFlexWeight( i, weight );
}
if (flex_talk.GetInt() == -1)
{
m_istalking = 1;
char pszSentence[256];
Q_snprintf( pszSentence,sizeof(pszSentence), "%s%d", STRING(m_iszSentence), m_sentence++ );
int sentenceIndex = engine->SentenceIndexFromName( pszSentence );
if (sentenceIndex >= 0)
{
Msg( "%d : %s\n", sentenceIndex, pszSentence );
CPASAttenuationFilter filter( this );
CBaseEntity::EmitSentenceByIndex( filter, entindex(), CHAN_VOICE, sentenceIndex, 1, SNDLVL_TALKING, 0, PITCH_NORM );
}
else
{
m_sentence = 0;
}
flex_talk.SetValue( "0" );
}
else if (flex_talk.GetInt() == -2)
{
m_flNextEyeLookTime = gpGlobals->curtime + 1000.0;
}
else if (flex_talk.GetInt() == -3)
{
m_flNextEyeLookTime = gpGlobals->curtime;
flex_talk.SetValue( "0" );
}
else if (flex_talk.GetInt() == -4)
{
AddLookTarget( UTIL_PlayerByIndex( 1 ), 0.5, flex_looktime.GetFloat() );
flex_talk.SetValue( "0" );
}
else if (flex_talk.GetInt() == -5)
{
PickLookTarget( true );
flex_talk.SetValue( "0" );
}
}
}