forked from hitmen047/Source-PlusPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhaptic_utils.cpp
443 lines (366 loc) · 10.2 KB
/
haptic_utils.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
#include "cbase.h"
#ifdef CLIENT_DLL
#include "tier3/tier3.h"
#include "iviewrender.h"
#include "inputsystem/iinputsystem.h"
#include "vgui/IInputInternal.h"
#include "c_basecombatweapon.h"
#include "c_baseplayer.h"
#include "haptics/ihaptics.h"
#include "hud_macros.h"
#include "iclientvehicle.h"
#include "c_prop_vehicle.h"
#include "prediction.h"
#include "activitylist.h"
#ifdef TERROR
#include "ClientTerrorPlayer.h"
#endif
extern vgui::IInputInternal *g_InputInternal;
#else
#include "usermessages.h"
#endif
#include "haptics/haptic_utils.h"
#include "haptics/haptic_msgs.h"
#ifndef TERROR
#ifndef FCVAR_RELEASE
#define FCVAR_RELEASE 0
#endif
#endif
#ifdef CLIENT_DLL
ConVar hap_HasDevice ( "hap_HasDevice", "0", FCVAR_USERINFO/*|FCVAR_HIDDEN*/, "falcon is connected" );
// damage scale on a title basis. Convar referenced in the haptic dll.
#ifdef PORTAL
#define HAP_DEFAULT_DAMAGE_SCALE_GAME "0.75"
#elif TF_CLIENT_DLL
#define HAP_DEFAULT_DAMAGE_SCALE_GAME "0.3"
#else
#define HAP_DEFAULT_DAMAGE_SCALE_GAME "1.0"
#endif
ConVar hap_damagescale_game("hap_damagescale_game", HAP_DEFAULT_DAMAGE_SCALE_GAME);
#undef HAP_DEFAULT_DAMAGE_SCALE_GAME
#endif
void HapticSendWeaponAnim(CBaseCombatWeapon* weapon, int iActivity)
{
//ignore idle
if(iActivity == ACT_VM_IDLE)
return;
#if defined( CLIENT_DLL )
//if(hap_PrintEvents.GetBool())
// Msg("Client Activity :%s %s %s\n",weapon->GetName(),"Activities",VarArgs("%i",iActivity));
if ( weapon->IsPredicted() )
haptics->ProcessHapticWeaponActivity(weapon->GetName(),iActivity);
#else
if( !weapon->IsPredicted() && weapon->GetOwner() && weapon->GetOwner()->IsPlayer())
{
HapticMsg_SendWeaponAnim( ToBasePlayer(weapon->GetOwner()), iActivity );
}
#endif
}
void HapticSetDrag(CBasePlayer* pPlayer, float drag)
{
#ifdef CLIENT_DLL
haptics->SetDrag(drag);
#else
HapticMsg_SetDrag( pPlayer, drag );
#endif
}
#ifdef CLIENT_DLL
void HapticsHandleMsg_HapSetDrag( float drag )
{
haptics->SetDrag(drag);
}
#endif
void HapticSetConstantForce(CBasePlayer* pPlayer, Vector force)
{
#ifdef CLIENT_DLL
haptics->SetConstantForce(force);
#else
HapticMsg_SetConstantForce( pPlayer, force );
#endif
}
#ifdef CLIENT_DLL
#ifndef HAPTICS_TEST_PREFIX
#define HAPTICS_TEST_PREFIX
#endif
static CSysModule *pFalconModule =0;
void ConnectHaptics(CreateInterfaceFn appFactory)
{
bool success = false;
// NVNT load haptics module
pFalconModule = Sys_LoadModule( HAPTICS_TEST_PREFIX HAPTICS_DLL_NAME );
if(pFalconModule)
{
CreateInterfaceFn factory = Sys_GetFactory( pFalconModule );
if(factory)
{
haptics = reinterpret_cast< IHaptics* >( factory( HAPTICS_INTERFACE_VERSION, NULL ) );
if(haptics &&
haptics->Initialize(engine,
view,
g_InputInternal,
gpGlobals,
appFactory,
g_pVGuiInput->GetIMEWindow(),
filesystem,
enginevgui,
ActivityList_IndexForName,
ActivityList_NameForIndex))
{
success = true;
hap_HasDevice.SetValue(1);
}
}
}
if(!success)
{
Sys_UnloadModule(pFalconModule);
pFalconModule = 0;
haptics = new CHapticsStubbed;
}
if(haptics->HasDevice())
{
Assert( (void*)haptics == inputsystem->GetHapticsInterfaceAddress() );
}
HookHapticMessages();
}
void DisconnectHaptics()
{
haptics->ShutdownHaptics();
if(pFalconModule)
{
Sys_UnloadModule(pFalconModule);
pFalconModule = 0;
}else{
// delete the stub.
delete haptics;
}
haptics = NULL;
}
//Might be able to handle this better...
void HapticsHandleMsg_HapSetConst( Vector const &constant )
{
//Msg("__MsgFunc_HapSetConst: %f %f %f\n",constant.x,constant.y,constant.z);
haptics->SetConstantForce(constant);
//C_BasePlayer* pPlayer = C_BasePlayer::GetLocalPlayer();
}
//Might be able to handle this better...
void HapticsHandleMsg_SPHapWeapEvent( int iActivity )
{
C_BasePlayer* pPlayer = C_BasePlayer::GetLocalPlayer();
C_BaseCombatWeapon* weap = NULL;
if(pPlayer)
weap = pPlayer->GetActiveWeapon();
if(weap)
haptics->ProcessHapticEvent(4,"Weapons",weap->GetName(),"Activities",VarArgs("%i",iActivity));
}
void HapticsHandleMsg_HapPunch( QAngle const &angle )
{
haptics->HapticsPunch(1,angle);
}
#ifdef TERROR
ConVar hap_zombie_damage_scale("hap_zombie_damage_scale", "0.25", FCVAR_RELEASE|FCVAR_NEVER_AS_STRING);
#endif
void HapticsHandleMsg_HapDmg( float pitch, float yaw, float damage, int damageType )
{
if(!haptics->HasDevice())
return;
#ifdef TERROR
C_TerrorPlayer *pPlayer = C_TerrorPlayer::GetLocalTerrorPlayer();
#else
C_BasePlayer *pPlayer = CBasePlayer::GetLocalPlayer();
#endif
if(pPlayer)
{
Vector damageDirection;
damageDirection.x = cos(pitch*M_PI/180.0)*sin(yaw*M_PI/180.0);
damageDirection.y = -sin(pitch*M_PI/180.0);
damageDirection.z = -(cos(pitch*M_PI/180.0)*cos(yaw*M_PI/180.0));
#ifdef TERROR
if(pPlayer->GetTeamNumber()==TEAM_ZOMBIE)
{
damageDirection *= hap_zombie_damage_scale.GetFloat();
}
#endif
haptics->ApplyDamageEffect(damage, damageType, damageDirection);
}
}
ConVar hap_melee_scale("hap_melee_scale", "0.8", FCVAR_RELEASE|FCVAR_NEVER_AS_STRING);
void HapticsHandleMsg_HapMeleeContact()
{
haptics->HapticsPunch(hap_melee_scale.GetFloat(), QAngle(0,0,0));
}
ConVar hap_noclip_avatar_scale("hap_noclip_avatar_scale", "0.10f", FCVAR_RELEASE|FCVAR_NEVER_AS_STRING);
void UpdateAvatarEffect(void)
{
if(!haptics->HasDevice())
return;
Vector vel;
Vector vvel;
Vector evel;
QAngle eye;
C_BasePlayer* pPlayer = C_BasePlayer::GetLocalPlayer();
if(!pPlayer)
return;
eye = pPlayer->GetAbsAngles();
if(pPlayer->IsInAVehicle() && pPlayer->GetVehicle())
{
pPlayer->GetVehicle()->GetVehicleEnt()->EstimateAbsVelocity(vvel);
eye = pPlayer->GetVehicle()->GetVehicleEnt()->EyeAngles();
if(!Q_stristr(pPlayer->GetVehicle()->GetVehicleEnt()->GetClassname(),"choreo"))
{
eye[YAW] += 90;
}
}
else
{
vel = pPlayer->GetAbsVelocity();
}
Vector PlayerVel = pPlayer->GetAbsVelocity();
//Choreo vehicles use player avatar and don't produce their own velocity
if(!pPlayer->GetVehicle() || fabsf(vvel.Length()) == 0 )
{
vel = PlayerVel;
}
else
vel = vvel;
VectorYawRotate(vel, -90 -eye[YAW], vel );
vel.y = -vel.y;
vel.z = -vel.z;
switch(pPlayer->GetMoveType()) {
case MOVETYPE_NOCLIP:
vel *= hap_noclip_avatar_scale.GetFloat();
break;
default:
break;
}
haptics->UpdateAvatarVelocity(vel);
}
#endif
#ifndef CLIENT_DLL
void HapticsDamage(CBasePlayer* pPlayer, const CTakeDamageInfo &info)
{
#if !defined(TF_DLL) && !defined(CSTRIKE_DLL)
if(!pPlayer->HasHaptics())
return;// do not send to non haptic users.
Vector DamageDirection(0,0,0);
CBaseEntity *eInflictor = info.GetInflictor();
// Pat: nuero toxix crash fix
if(!eInflictor) {
return;
}
// Player Data
Vector playerPosition = pPlayer->GetLocalOrigin();
Vector inflictorPosition = eInflictor->GetLocalOrigin();
Vector posWithDir = playerPosition + (playerPosition - inflictorPosition);
pPlayer->WorldToEntitySpace(posWithDir, &DamageDirection);
QAngle dir(0,0,0);
VectorAngles(DamageDirection, dir);
float yawAngle = dir[YAW];
float pitchAngle = dir[PITCH];
int bitDamageType = info.GetDamageType();
if(bitDamageType & DMG_FALL)
{
pitchAngle = ((float)-90.0); // coming from beneath
}
else if(bitDamageType & DMG_BURN && (bitDamageType & ~DMG_BURN)==0)
{
// just burn, use the z axis here.
pitchAngle = 0.0;
}
#ifdef TERROR
else if(
(bitDamageType & ( DMG_PARALYZE | DMG_NERVEGAS | DMG_POISON | DMG_RADIATION | DMG_DROWNRECOVER | DMG_ACID | DMG_SLOWBURN ) ) &&
(bitDamageType & ~( DMG_PARALYZE | DMG_NERVEGAS | DMG_POISON | DMG_RADIATION | DMG_DROWNRECOVER | DMG_ACID | DMG_SLOWBURN ) )==0 )
{
// it is time based. and should not really do a punch.
return;
}
#endif
float sendDamage = info.GetDamage();
if(sendDamage>0.0f)
{
HapticMsg_HapDmg( pPlayer, pitchAngle, -yawAngle, sendDamage, bitDamageType );
}
#endif
}
void HapticPunch(CBasePlayer* pPlayer, float x, float y, float z)
{
HapticMsg_Punch( pPlayer, x, y, z );
}
void HapticMeleeContact(CBasePlayer* pPlayer)
{
HapticMsg_MeleeContact( pPlayer );
}
#endif // NOT CLIENT_DLL
void HapticProcessSound(const char* soundname, int entIndex)
{
#ifdef CLIENT_DLL
if (prediction->InPrediction() && prediction->IsFirstTimePredicted())
{
bool local = false;
C_BaseEntity *ent = C_BaseEntity::Instance( entIndex );
if(ent)
local = (entIndex == -1 || ent == C_BasePlayer::GetLocalPlayer() || ent == C_BasePlayer::GetLocalPlayer()->GetActiveWeapon());
haptics->ProcessHapticEvent(2,"Sounds",soundname);
}
#endif
}
#ifdef CLIENT_DLL
// NVNT add || defined(OTHER_DEFINITION) if your game uses vehicles.
#if defined( HL2_CLIENT_DLL )
#define HAPTIC_VEHICLE_DEFAULT "1"
#else
#define HAPTIC_VEHICLE_DEFAULT "0"
#endif
// determines weather the vehicles control box option is faded
ConVar hap_ui_vehicles( "hap_ui_vehicles",
HAPTIC_VEHICLE_DEFAULT,
0
);
#undef HAPTIC_VEHICLE_DEFAULT
void HapticsEnteredVehicle(C_BaseEntity* vehicle, C_BaseCombatCharacter *pPassenger )
{
if(!vehicle)
return;
// NVNT notify haptics system of navigation change.
C_PropVehicleDriveable* drivable = dynamic_cast<C_PropVehicleDriveable*>(vehicle);
bool hasgun = false;
if(drivable)
hasgun = drivable->HasGun();
if(Q_stristr(vehicle->GetClassname(),"airboat"))
{
haptics->ProcessHapticEvent(2,"Movement","airboat");
if(hasgun)
haptics->SetNavigationClass("vehicle_gun");
else
haptics->SetNavigationClass("vehicle_airboat");
}
else if(Q_stristr(vehicle->GetClassname(),"jeepepisodic"))
{
haptics->ProcessHapticEvent(2,"Movement","BaseVehicle");
haptics->SetNavigationClass("vehicle_nogun");
}
else if(Q_stristr(vehicle->GetClassname(),"jeep"))
{
haptics->ProcessHapticEvent(2,"Movement","BaseVehicle");
haptics->SetNavigationClass("vehicle_gun");
}
else if(Q_stristr(vehicle->GetClassname(),"choreo"))
{
haptics->ProcessHapticEvent(2,"Movement","ChoreoVehicle");
haptics->SetNavigationClass("vehicle_gun_nofix");//Give this a bit of aiming
}
else
{
haptics->ProcessHapticEvent(2,"Movement","BaseVehicle");
haptics->SetNavigationClass("vehicle_nogun");
}
Msg("VehicleType:%s:\n",vehicle->GetClassname());
}
void HapticsExitedVehicle(C_BaseEntity* vehicle, C_BaseCombatCharacter *pPassenger )
{
// NVNT notify haptics system of navigation change.
haptics->SetNavigationClass("on_foot");
haptics->ProcessHapticEvent(2,"Movement","BasePlayer");
}
#endif