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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//=============================================================================//
#include "cbase.h"
#include "vehicle_base.h"
#include "engine/IEngineSound.h"
#include "in_buttons.h"
#include "soundenvelope.h"
#include "soundent.h"
#include "physics_saverestore.h"
#include "vphysics/constraints.h"
#include "vcollide_parse.h"
#include "ndebugoverlay.h"
#include "npc_vehicledriver.h"
#include "vehicle_crane.h"
#include "hl2_player.h"
#include "rumble_shared.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
#define VEHICLE_HITBOX_DRIVER 1
extern ConVar g_debug_vehicledriver;
// Crane spring constants
#define CRANE_SPRING_CONSTANT_HANGING 2e5f
#define CRANE_SPRING_CONSTANT_INITIAL_RAISING (CRANE_SPRING_CONSTANT_HANGING * 0.5)
#define CRANE_SPRING_CONSTANT_LOWERING 30.0f
#define CRANE_SPRING_DAMPING 2e5f
#define CRANE_SPRING_RELATIVE_DAMPING 2
// Crane bones that have physics followers
static const char *pCraneFollowerBoneNames[] =
{
"base",
"arm",
"platform",
};
// Crane tip
LINK_ENTITY_TO_CLASS( crane_tip, CCraneTip );
BEGIN_DATADESC( CCraneTip )
DEFINE_PHYSPTR( m_pSpring ),
END_DATADESC()
// Crane
LINK_ENTITY_TO_CLASS( prop_vehicle_crane, CPropCrane );
BEGIN_DATADESC( CPropCrane )
// Inputs
DEFINE_INPUTFUNC( FIELD_VOID, "Lock", InputLock ),
DEFINE_INPUTFUNC( FIELD_VOID, "Unlock", InputUnlock ),
DEFINE_INPUTFUNC( FIELD_VOID, "ForcePlayerIn", InputForcePlayerIn ),
// Keys
DEFINE_EMBEDDED( m_ServerVehicle ),
DEFINE_EMBEDDED( m_BoneFollowerManager ),
DEFINE_FIELD( m_hPlayer, FIELD_EHANDLE ),
DEFINE_FIELD( m_bMagnetOn, FIELD_BOOLEAN ),
DEFINE_FIELD( m_hNPCDriver, FIELD_EHANDLE ),
DEFINE_FIELD( m_nNPCButtons, FIELD_INTEGER ),
DEFINE_FIELD( m_bLocked, FIELD_BOOLEAN ),
DEFINE_FIELD( m_bEnterAnimOn, FIELD_BOOLEAN ),
DEFINE_FIELD( m_bExitAnimOn, FIELD_BOOLEAN ),
DEFINE_FIELD( m_vecEyeExitEndpoint, FIELD_POSITION_VECTOR ),
DEFINE_OUTPUT( m_playerOn, "PlayerOn" ),
DEFINE_OUTPUT( m_playerOff, "PlayerOff" ),
DEFINE_FIELD( m_iTurning, FIELD_INTEGER ),
DEFINE_FIELD( m_bStartSoundAtCrossover, FIELD_BOOLEAN ),
DEFINE_FIELD( m_flTurn, FIELD_FLOAT ),
DEFINE_FIELD( m_bExtending, FIELD_BOOLEAN ),
DEFINE_FIELD( m_flExtension, FIELD_FLOAT ),
DEFINE_FIELD( m_flExtensionRate, FIELD_FLOAT ),
DEFINE_FIELD( m_bDropping, FIELD_BOOLEAN ),
//DEFINE_FIELD( m_flNextDangerSoundTime, FIELD_TIME ),
//DEFINE_FIELD( m_flNextCreakSound, FIELD_TIME ),
DEFINE_FIELD( m_flNextDropAllowedTime, FIELD_TIME ),
DEFINE_FIELD( m_flSlowRaiseTime, FIELD_TIME ),
DEFINE_FIELD( m_flMaxExtensionSpeed, FIELD_FLOAT ),
DEFINE_FIELD( m_flMaxTurnSpeed, FIELD_FLOAT ),
DEFINE_FIELD( m_flExtensionAccel, FIELD_FLOAT ),
DEFINE_FIELD( m_flExtensionDecel, FIELD_FLOAT ),
DEFINE_FIELD( m_flTurnAccel, FIELD_FLOAT ),
DEFINE_FIELD( m_flTurnDecel, FIELD_FLOAT ),
DEFINE_KEYFIELD( m_iszMagnetName, FIELD_STRING, "magnetname" ),
DEFINE_FIELD( m_hCraneMagnet, FIELD_EHANDLE ),
DEFINE_FIELD( m_hCraneTip, FIELD_EHANDLE ),
DEFINE_FIELD( m_hRope, FIELD_EHANDLE ),
DEFINE_PHYSPTR( m_pConstraintGroup ),
DEFINE_KEYFIELD( m_vehicleScript, FIELD_STRING, "vehiclescript" ),
END_DATADESC()
IMPLEMENT_SERVERCLASS_ST(CPropCrane, DT_PropCrane)
SendPropEHandle(SENDINFO(m_hPlayer)),
SendPropBool(SENDINFO(m_bMagnetOn)),
SendPropBool(SENDINFO(m_bEnterAnimOn)),
SendPropBool(SENDINFO(m_bExitAnimOn)),
SendPropVector(SENDINFO(m_vecEyeExitEndpoint), -1, SPROP_COORD),
END_SEND_TABLE();
//------------------------------------------------
// Precache
//------------------------------------------------
void CPropCrane::Precache( void )
{
BaseClass::Precache();
m_ServerVehicle.Initialize( STRING(m_vehicleScript) );
}
//------------------------------------------------
// Spawn
//------------------------------------------------
void CPropCrane::Spawn( void )
{
Precache();
SetModel( STRING( GetModelName() ) );
SetCollisionGroup( COLLISION_GROUP_VEHICLE );
BaseClass::Spawn();
SetSolid( SOLID_BBOX );
AddSolidFlags( FSOLID_NOT_SOLID );
SetMoveType( MOVETYPE_NOCLIP );
m_takedamage = DAMAGE_EVENTS_ONLY;
m_flTurn = 0;
m_flExtension = 0;
m_flNextDangerSoundTime = 0;
m_flNextCreakSound = 0;
m_flNextDropAllowedTime = 0;
m_flSlowRaiseTime = 0;
m_bDropping = false;
m_bMagnetOn = false;
InitCraneSpeeds();
SetPoseParameter( "armextensionpose", m_flExtension );
CreateVPhysics();
SetNextThink( gpGlobals->curtime );
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CPropCrane::Activate( void )
{
BaseClass::Activate();
// If we load a game, we don't need to set this all up again.
if ( m_hCraneMagnet )
return;
// Find our magnet
if ( m_iszMagnetName == NULL_STRING )
{
Warning( "prop_vehicle_crane %s has no magnet entity specified!\n", STRING(GetEntityName()) );
UTIL_Remove( this );
return;
}
m_hCraneMagnet = dynamic_cast<CPhysMagnet *>(gEntList.FindEntityByName( NULL, STRING(m_iszMagnetName) ));
if ( !m_hCraneMagnet )
{
Warning( "prop_vehicle_crane %s failed to find magnet %s.\n", STRING(GetEntityName()), STRING(m_iszMagnetName) );
UTIL_Remove( this );
return;
}
// We want the magnet to cast a long shadow
m_hCraneMagnet->SetShadowCastDistance( 2048 );
// Create our constraint group
constraint_groupparams_t group;
group.Defaults();
m_pConstraintGroup = physenv->CreateConstraintGroup( group );
m_hCraneMagnet->SetConstraintGroup( m_pConstraintGroup );
// Create our crane tip
Vector vecOrigin;
QAngle vecAngles;
GetCraneTipPosition( &vecOrigin, &vecAngles );
m_hCraneTip = CCraneTip::Create( m_hCraneMagnet, m_pConstraintGroup, vecOrigin, vecAngles );
if ( !m_hCraneTip )
{
UTIL_Remove( this );
return;
}
m_pConstraintGroup->Activate();
// Make a rope to connect 'em
int iIndex = m_hCraneMagnet->LookupAttachment("magnetcable_a");
m_hRope = CRopeKeyframe::Create( this, m_hCraneMagnet, 1, iIndex );
if ( m_hRope )
{
m_hRope->m_Width = 3;
m_hRope->m_nSegments = ROPE_MAX_SEGMENTS / 2;
m_hRope->EnableWind( false );
m_hRope->SetupHangDistance( 0 );
m_hRope->m_RopeLength = (m_hCraneMagnet->GetAbsOrigin() - m_hCraneTip->GetAbsOrigin()).Length() * 1.1;
}
// Start with the magnet off
TurnMagnetOff();
}
//-----------------------------------------------------------------------------
// Purpose:
// Output : Returns true on success, false on failure.
//-----------------------------------------------------------------------------
bool CPropCrane::CreateVPhysics( void )
{
BaseClass::CreateVPhysics();
m_BoneFollowerManager.InitBoneFollowers( this, ARRAYSIZE(pCraneFollowerBoneNames), pCraneFollowerBoneNames );
return true;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CPropCrane::UpdateOnRemove( void )
{
m_BoneFollowerManager.DestroyBoneFollowers();
BaseClass::UpdateOnRemove();
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CPropCrane::InitCraneSpeeds( void )
{
m_flMaxExtensionSpeed = CRANE_EXTENSION_RATE_MAX * 2;
m_flMaxTurnSpeed = CRANE_TURN_RATE_MAX * 2;
m_flExtensionAccel = CRANE_EXTENSION_ACCEL * 2;
m_flExtensionDecel = CRANE_EXTENSION_DECEL * 2;
m_flTurnAccel = CRANE_TURN_ACCEL * 2;
m_flTurnDecel = CRANE_DECEL * 2;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CPropCrane::TraceAttack( const CTakeDamageInfo &info, const Vector &vecDir, trace_t *ptr, CDmgAccumulator *pAccumulator )
{
if ( ptr->hitbox == VEHICLE_HITBOX_DRIVER )
{
if ( m_hPlayer != NULL )
{
m_hPlayer->TakeDamage( info );
}
}
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
int CPropCrane::OnTakeDamage( const CTakeDamageInfo &inputInfo )
{
//Do scaled up physics damage to the car
CTakeDamageInfo info = inputInfo;
info.ScaleDamage( 25 );
// reset the damage
info.SetDamage( inputInfo.GetDamage() );
//Check to do damage to driver
if ( m_hPlayer != NULL )
{
//Take no damage from physics damages
if ( info.GetDamageType() & DMG_CRUSH )
return 0;
//Take the damage
m_hPlayer->TakeDamage( info );
}
return 0;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
Vector CPropCrane::BodyTarget( const Vector &posSrc, bool bNoisy )
{
Vector shotPos;
matrix3x4_t matrix;
int eyeAttachmentIndex = LookupAttachment("vehicle_driver_eyes");
GetAttachment( eyeAttachmentIndex, matrix );
MatrixGetColumn( matrix, 3, shotPos );
if ( bNoisy )
{
shotPos[0] += random->RandomFloat( -8.0f, 8.0f );
shotPos[1] += random->RandomFloat( -8.0f, 8.0f );
shotPos[2] += random->RandomFloat( -8.0f, 8.0f );
}
return shotPos;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CPropCrane::Think(void)
{
SetNextThink( gpGlobals->curtime + 0.1 );
if ( GetDriver() )
{
BaseClass::Think();
if ( m_hNPCDriver )
{
GetServerVehicle()->NPC_DriveVehicle();
}
// play enter animation
StudioFrameAdvance();
// If the enter or exit animation has finished, tell the server vehicle
if ( IsSequenceFinished() && (m_bExitAnimOn || m_bEnterAnimOn) )
{
if ( m_bEnterAnimOn )
{
// Finished entering, display the hint for using the crane
UTIL_HudHintText( m_hPlayer, "#Valve_Hint_CraneKeys" );
}
GetServerVehicle()->HandleEntryExitFinish( m_bExitAnimOn, true );
}
}
else
{
// Run the crane's movement
RunCraneMovement( 0.1 );
}
// Update follower bones
m_BoneFollowerManager.UpdateBoneFollowers(this);
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : *player -
//-----------------------------------------------------------------------------
void CPropCrane::ItemPostFrame( CBasePlayer *player )
{
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CPropCrane::Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value )
{
CBasePlayer *pPlayer = ToBasePlayer( pActivator );
if ( !pPlayer )
return;
ResetUseKey( pPlayer );
GetServerVehicle()->HandlePassengerEntry( pPlayer, (value>0) );
}
//-----------------------------------------------------------------------------
// Purpose: Return true of the player's allowed to enter / exit the vehicle
//-----------------------------------------------------------------------------
bool CPropCrane::CanEnterVehicle( CBaseEntity *pEntity )
{
// Prevent entering if the vehicle's being driven by an NPC
if ( GetDriver() && GetDriver() != pEntity )
return false;
// Prevent entering if the vehicle's locked
return ( !m_bLocked );
}
//-----------------------------------------------------------------------------
// Purpose: Return true of the player's allowed to enter / exit the vehicle
//-----------------------------------------------------------------------------
bool CPropCrane::CanExitVehicle( CBaseEntity *pEntity )
{
// Prevent exiting if the vehicle's locked, or rotating
// Adrian: Check also if I'm currently jumping in or out.
return ( !m_bLocked && (GetLocalAngularVelocity() == vec3_angle) && m_bExitAnimOn == false && m_bEnterAnimOn == false );
}
//-----------------------------------------------------------------------------
// Purpose: Override base class to add display
//-----------------------------------------------------------------------------
void CPropCrane::DrawDebugGeometryOverlays(void)
{
// Draw if BBOX is on
if ( m_debugOverlays & OVERLAY_BBOX_BIT )
{
Vector vecPoint = m_hCraneMagnet->GetAbsOrigin();
int iIndex = m_hCraneMagnet->LookupAttachment("magnetcable_a");
if ( iIndex >= 0 )
{
m_hCraneMagnet->GetAttachment( iIndex, vecPoint );
}
NDebugOverlay::Line( m_hCraneTip->GetAbsOrigin(), vecPoint, 255,255,255, true, 0.1 );
}
BaseClass::DrawDebugGeometryOverlays();
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CPropCrane::EnterVehicle( CBaseCombatCharacter *pPassenger )
{
if ( pPassenger == NULL )
return;
CBasePlayer *pPlayer = ToBasePlayer( pPassenger );
if ( pPlayer != NULL )
{
// Remove any player who may be in the vehicle at the moment
if ( m_hPlayer )
{
ExitVehicle( VEHICLE_ROLE_DRIVER );
}
m_hPlayer = pPlayer;
m_playerOn.FireOutput( pPlayer, this, 0 );
m_hPlayer->RumbleEffect( RUMBLE_FLAT_BOTH, 0, RUMBLE_FLAG_LOOP );
m_hPlayer->RumbleEffect( RUMBLE_FLAT_BOTH, 10, RUMBLE_FLAG_UPDATE_SCALE );
m_ServerVehicle.SoundStart();
}
else
{
// NPCs not yet supported - jdw
Assert( 0 );
}
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CPropCrane::ExitVehicle( int nRole )
{
CBasePlayer *pPlayer = m_hPlayer;
if ( !pPlayer )
return;
m_hPlayer = NULL;
ResetUseKey( pPlayer );
m_playerOff.FireOutput( pPlayer, this, 0 );
m_bEnterAnimOn = false;
m_ServerVehicle.SoundShutdown( 1.0 );
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CPropCrane::ResetUseKey( CBasePlayer *pPlayer )
{
pPlayer->m_afButtonPressed &= ~IN_USE;
}
//-----------------------------------------------------------------------------
// Purpose: Pass player movement into the crane's driving system
//-----------------------------------------------------------------------------
void CPropCrane::SetupMove( CBasePlayer *player, CUserCmd *ucmd, IMoveHelper *pHelper, CMoveData *move )
{
// If the player's entering/exiting the vehicle, prevent movement
if ( !m_bEnterAnimOn && !m_bExitAnimOn )
{
int buttons = ucmd->buttons;
if ( !(buttons & (IN_MOVELEFT|IN_MOVERIGHT)) )
{
if ( ucmd->sidemove < 0 )
{
buttons |= IN_MOVELEFT;
}
else if ( ucmd->sidemove > 0 )
{
buttons |= IN_MOVERIGHT;
}
}
DriveCrane( buttons, player->m_afButtonPressed );
}
// Run the crane's movement
RunCraneMovement( gpGlobals->frametime );
}
//-----------------------------------------------------------------------------
// Purpose: Crane rotates around with +left and +right, and extends/retracts
// the cable with +forward and +back.
//-----------------------------------------------------------------------------
void CPropCrane::DriveCrane( int iDriverButtons, int iButtonsPressed, float flNPCSteering )
{
bool bWasExtending = m_bExtending;
// Handle rotation of the crane
if ( iDriverButtons & IN_MOVELEFT )
{
// NPCs may cheat and set the steering
if ( flNPCSteering )
{
m_flTurn = flNPCSteering;
}
else
{
// Try adding some randomness to make it feel shaky?
float flTurnAdd = m_flTurnAccel;
// If we're turning back on ourselves, use decel speed
if ( m_flTurn < 0 )
{
flTurnAdd = MAX( flTurnAdd, m_flTurnDecel );
}
m_flTurn = UTIL_Approach( m_flMaxTurnSpeed, m_flTurn, flTurnAdd * gpGlobals->frametime );
}
m_iTurning = TURNING_LEFT;
}
else if ( iDriverButtons & IN_MOVERIGHT )
{
// NPCs may cheat and set the steering
if ( flNPCSteering )
{
m_flTurn = flNPCSteering;
}
else
{
// Try adding some randomness to make it feel shaky?
float flTurnAdd = m_flTurnAccel;
// If we're turning back on ourselves, increase the rate
if ( m_flTurn > 0 )
{
flTurnAdd = MAX( flTurnAdd, m_flTurnDecel );
}
m_flTurn = UTIL_Approach( -m_flMaxTurnSpeed, m_flTurn, flTurnAdd * gpGlobals->frametime );
}
m_iTurning = TURNING_RIGHT;
}
else
{
m_flTurn = UTIL_Approach( 0, m_flTurn, m_flTurnDecel * gpGlobals->frametime );
m_iTurning = TURNING_NOT;
}
if ( m_hPlayer )
{
float maxTurn = GetMaxTurnRate();
static float maxRumble = 0.35f;
static float minRumble = 0.1f;
float rumbleRange = maxRumble - minRumble;
float rumble;
float factor = fabs(m_flTurn) / maxTurn;
factor = MIN( factor, 1.0f );
rumble = minRumble + (rumbleRange * factor);
m_hPlayer->RumbleEffect( RUMBLE_FLAT_BOTH, (int)(rumble * 100), RUMBLE_FLAG_UPDATE_SCALE );
}
SetLocalAngularVelocity( QAngle(0,m_flTurn * 10,0) );
// Handle extension / retraction of the arm
if ( iDriverButtons & IN_FORWARD )
{
m_flExtensionRate = UTIL_Approach( m_flMaxExtensionSpeed, m_flExtensionRate, m_flExtensionAccel * gpGlobals->frametime );
m_bExtending = true;
}
else if ( iDriverButtons & IN_BACK )
{
m_flExtensionRate = UTIL_Approach( -m_flMaxExtensionSpeed, m_flExtensionRate, m_flExtensionAccel * gpGlobals->frametime );
m_bExtending = true;
}
else
{
m_flExtensionRate = UTIL_Approach( 0, m_flExtensionRate, m_flExtensionDecel * gpGlobals->frametime );
m_bExtending = false;
}
//Msg("Turn: %f\nExtensionRate: %f\n", m_flTurn, m_flExtensionRate );
//If we're holding down an attack button, update our state
if ( iButtonsPressed & (IN_ATTACK | IN_ATTACK2) )
{
// If we have something on the magnet, turn the magnet off
if ( m_hCraneMagnet->GetTotalMassAttachedObjects() )
{
TurnMagnetOff();
}
else if ( !m_bDropping && m_flNextDropAllowedTime < gpGlobals->curtime )
{
TurnMagnetOn();
// Drop the magnet till it hits something
m_bDropping = true;
m_hCraneMagnet->ResetHasHitSomething();
m_hCraneTip->m_pSpring->SetSpringConstant( CRANE_SPRING_CONSTANT_LOWERING );
m_ServerVehicle.PlaySound( VS_MISC1 );
}
}
float flSpeedPercentage = clamp( fabs(m_flTurn) / m_flMaxTurnSpeed, 0, 1 );
vbs_sound_update_t params;
params.Defaults();
params.bThrottleDown = (m_iTurning != TURNING_NOT);
params.flCurrentSpeedFraction = flSpeedPercentage;
params.flWorldSpaceSpeed = 0;
m_ServerVehicle.SoundUpdate( params );
// Play sounds for arm extension / retraction
if ( m_bExtending && !bWasExtending )
{
m_ServerVehicle.StopSound( VS_ENGINE2_STOP );
m_ServerVehicle.PlaySound( VS_ENGINE2_START );
}
else if ( !m_bExtending && bWasExtending )
{
m_ServerVehicle.StopSound( VS_ENGINE2_START );
m_ServerVehicle.PlaySound( VS_ENGINE2_STOP );
}
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CPropCrane::RecalculateCraneTip( void )
{
Vector vecOrigin;
QAngle vecAngles;
GetCraneTipPosition( &vecOrigin, &vecAngles );
m_hCraneTip->SetAbsOrigin( vecOrigin );
// NOTE: We need to do this because we're not using Physics...
if ( m_hCraneTip->VPhysicsGetObject() )
{
m_hCraneTip->VPhysicsGetObject()->UpdateShadow( vecOrigin, vec3_angle, true, TICK_INTERVAL * 2.0f );
}
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : *pPlayer -
// *pMoveData -
//-----------------------------------------------------------------------------
void CPropCrane::RunCraneMovement( float flTime )
{
if ( m_flExtensionRate )
{
// Extend / Retract the crane
m_flExtension = clamp( m_flExtension + (m_flExtensionRate * 10 * flTime), 0, 2 );
SetPoseParameter( "armextensionpose", m_flExtension );
StudioFrameAdvance();
}
// Drop the magnet until it hits the ground
if ( m_bDropping )
{
// Drop until the magnet hits something
if ( m_hCraneMagnet->HasHitSomething() )
{
// We hit the ground, stop dropping
m_hCraneTip->m_pSpring->SetSpringConstant( CRANE_SPRING_CONSTANT_INITIAL_RAISING );
m_bDropping = false;
m_flNextDropAllowedTime = gpGlobals->curtime + 3.0;
m_flSlowRaiseTime = gpGlobals->curtime;
m_ServerVehicle.PlaySound( VS_MISC2 );
}
}
else if ( (m_flSlowRaiseTime + CRANE_SLOWRAISE_TIME) > gpGlobals->curtime )
{
float flDelta = (gpGlobals->curtime - m_flSlowRaiseTime);
flDelta = clamp( flDelta, 0, CRANE_SLOWRAISE_TIME );
float flCurrentSpringConstant = RemapVal( flDelta, 0, CRANE_SLOWRAISE_TIME, CRANE_SPRING_CONSTANT_INITIAL_RAISING, CRANE_SPRING_CONSTANT_HANGING );
m_hCraneTip->m_pSpring->SetSpringConstant( flCurrentSpringConstant );
}
// If we've moved in any way, update the tip
if ( m_bDropping || m_flExtensionRate || GetLocalAngularVelocity() != vec3_angle )
{
RecalculateCraneTip();
}
// Make danger sounds underneath the magnet if we have something attached to it
/*
if ( (m_flNextDangerSoundTime < gpGlobals->curtime) && (m_hCraneMagnet->GetTotalMassAttachedObjects() > 0) )
{
// Trace down from the magnet and make a danger sound on the ground
trace_t tr;
Vector vecSource = m_hCraneMagnet->GetAbsOrigin();
UTIL_TraceLine( vecSource, vecSource - Vector(0,0,2048), MASK_SOLID_BRUSHONLY, m_hCraneMagnet, 0, &tr );
if ( tr.fraction < 1.0 )
{
// Make the volume proportional to the amount of mass on the magnet
float flVolume = clamp( (m_hCraneMagnet->GetTotalMassAttachedObjects() * 0.5), 100.f, 600.f );
CSoundEnt::InsertSound( SOUND_DANGER, tr.endpos, flVolume, 0.2, this );
//Msg("Total: %.2f Volume: %.2f\n", m_hCraneMagnet->GetTotalMassAttachedObjects(), flVolume );
//Vector vecVolume = Vector(flVolume,flVolume,flVolume) * 0.5;
//NDebugOverlay::Box( tr.endpos, -vecVolume, vecVolume, 255,0,0, false, 0.3 );
//NDebugOverlay::Cross3D( tr.endpos, -Vector(10,10,10), Vector(10,10,10), 255,0,0, false, 0.3 );
}
m_flNextDangerSoundTime = gpGlobals->curtime + 0.3;
}
*/
// Play creak sounds on the magnet if there's heavy weight on it
if ( (m_flNextCreakSound < gpGlobals->curtime) && (m_hCraneMagnet->GetTotalMassAttachedObjects() > 100) )
{
// Randomly play creaks from the magnet, and increase the chance based on the turning speed
float flSpeedPercentage = clamp( fabs(m_flTurn) / m_flMaxTurnSpeed, 0, 1 );
if ( RandomFloat(0,1) > (0.95 - (0.1 * flSpeedPercentage)) )
{
if ( m_ServerVehicle.m_vehicleSounds.iszSound[VS_MISC4] != NULL_STRING )
{
CPASAttenuationFilter filter( m_hCraneMagnet );
EmitSound_t ep;
ep.m_nChannel = CHAN_VOICE;
ep.m_pSoundName = STRING(m_ServerVehicle.m_vehicleSounds.iszSound[VS_MISC4]);
ep.m_flVolume = 1.0f;
ep.m_SoundLevel = SNDLVL_NORM;
CBaseEntity::EmitSound( filter, m_hCraneMagnet->entindex(), ep );
}
m_flNextCreakSound = gpGlobals->curtime + 5.0;
}
}
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CPropCrane::TurnMagnetOn( void )
{
if ( !m_hCraneMagnet->IsOn() )
{
variant_t emptyVariant;
m_hCraneMagnet->AcceptInput( "Toggle", this, this, emptyVariant, USE_TOGGLE );
m_ServerVehicle.PlaySound( VS_MISC3 );
m_bMagnetOn = true;
}
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CPropCrane::TurnMagnetOff( void )
{
if ( m_hCraneMagnet->IsOn() )
{
variant_t emptyVariant;
m_hCraneMagnet->AcceptInput( "Toggle", this, this, emptyVariant, USE_TOGGLE );
m_ServerVehicle.PlaySound( VS_MISC3 );
m_bMagnetOn = false;
}
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
const Vector &CPropCrane::GetCraneTipPosition( void )
{
return m_hCraneTip->GetAbsOrigin();
}
//-----------------------------------------------------------------------------
// Purpose: Fills out the values with the desired position of the crane's tip
//-----------------------------------------------------------------------------
void CPropCrane::GetCraneTipPosition( Vector *vecOrigin, QAngle *vecAngles )
{
GetAttachment( "cable_tip", *vecOrigin, *vecAngles );
}
//-----------------------------------------------------------------------------
// Purpose: Vehicles are permanently oriented off angle for vphysics.
//-----------------------------------------------------------------------------
void CPropCrane::GetVectors(Vector* pForward, Vector* pRight, Vector* pUp) const
{
// This call is necessary to cause m_rgflCoordinateFrame to be recomputed
const matrix3x4_t &entityToWorld = EntityToWorldTransform();
if (pForward != NULL)
{
MatrixGetColumn( entityToWorld, 1, *pForward );
}
if (pRight != NULL)
{
MatrixGetColumn( entityToWorld, 0, *pRight );
}
if (pUp != NULL)
{
MatrixGetColumn( entityToWorld, 2, *pUp );
}
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
CBaseEntity *CPropCrane::GetDriver( void )
{
if ( m_hNPCDriver )
return m_hNPCDriver;
return m_hPlayer;
}
//-----------------------------------------------------------------------------
// Purpose: Prevent the player from entering / exiting the vehicle
//-----------------------------------------------------------------------------
void CPropCrane::InputLock( inputdata_t &inputdata )
{
m_bLocked = true;
}
//-----------------------------------------------------------------------------
// Purpose: Allow the player to enter / exit the vehicle
//-----------------------------------------------------------------------------
void CPropCrane::InputUnlock( inputdata_t &inputdata )
{
m_bLocked = false;
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : &inputdata -
//-----------------------------------------------------------------------------
void CPropCrane::InputForcePlayerIn( inputdata_t &inputdata )
{
CBasePlayer *pPlayer = UTIL_PlayerByIndex( 1 );
if ( pPlayer && !m_hPlayer )
{
GetServerVehicle()->HandlePassengerEntry( pPlayer, 0 );
}
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CPropCrane::SetNPCDriver( CNPC_VehicleDriver *pDriver )
{
m_hNPCDriver = pDriver;
m_nNPCButtons = 0;
if ( pDriver )
{
m_flMaxExtensionSpeed = CRANE_EXTENSION_RATE_MAX * 1.5;
m_flMaxTurnSpeed = CRANE_TURN_RATE_MAX * 1.5;
m_flExtensionAccel = CRANE_EXTENSION_ACCEL * 2;
m_flExtensionDecel = CRANE_EXTENSION_DECEL * 20; // Npcs stop quickly to make them more accurate
m_flTurnAccel = CRANE_TURN_ACCEL * 2;
m_flTurnDecel = CRANE_DECEL * 10; // Npcs stop quickly to make them more accurate
// Set our owner entity to be the NPC, so it can path check without hitting us
SetOwnerEntity( pDriver );
}
else
{
// Restore player crane speeds
InitCraneSpeeds();
SetOwnerEntity( NULL );
// Shutdown the crane's sounds
m_ServerVehicle.SoundShutdown( 1.0 );
}
}
//-----------------------------------------------------------------------------
// Purpose: Allows us to turn off the rumble
//-----------------------------------------------------------------------------
void CPropCrane::PreExitVehicle( CBaseCombatCharacter *pPlayer, int nRole )
{
if ( pPlayer != m_hPlayer )
return;
if ( m_hPlayer != NULL )
{
// Stop rumbles
m_hPlayer->RumbleEffect( RUMBLE_FLAT_BOTH, 0, RUMBLE_FLAG_STOP );
}
}
//========================================================================================================================================
// CRANE VEHICLE SERVER VEHICLE
//========================================================================================================================================
CPropCrane *CCraneServerVehicle::GetCrane( void )
{
return (CPropCrane*)GetDrivableVehicle();
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CCraneServerVehicle::GetVehicleViewPosition( int nRole, Vector *pAbsOrigin, QAngle *pAbsAngles, float *pFOV /*= NULL*/ )
{
// FIXME: This needs to be reconciled with the other versions of this function!
Assert( nRole == VEHICLE_ROLE_DRIVER );
CBasePlayer *pPlayer = ToBasePlayer( GetDrivableVehicle()->GetDriver() );
Assert( pPlayer );
*pAbsAngles = pPlayer->EyeAngles(); // yuck. this is an in/out parameter.
float flPitchFactor = 1.0;
matrix3x4_t vehicleEyePosToWorld;
Vector vehicleEyeOrigin;
QAngle vehicleEyeAngles;
GetCrane()->GetAttachment( "vehicle_driver_eyes", vehicleEyeOrigin, vehicleEyeAngles );
AngleMatrix( vehicleEyeAngles, vehicleEyePosToWorld );
// Compute the relative rotation between the unperterbed eye attachment + the eye angles
matrix3x4_t cameraToWorld;
AngleMatrix( *pAbsAngles, cameraToWorld );
matrix3x4_t worldToEyePos;
MatrixInvert( vehicleEyePosToWorld, worldToEyePos );
matrix3x4_t vehicleCameraToEyePos;
ConcatTransforms( worldToEyePos, cameraToWorld, vehicleCameraToEyePos );
// Now perterb the attachment point
vehicleEyeAngles.x = RemapAngleRange( PITCH_CURVE_ZERO * flPitchFactor, PITCH_CURVE_LINEAR, vehicleEyeAngles.x );
vehicleEyeAngles.z = RemapAngleRange( ROLL_CURVE_ZERO * flPitchFactor, ROLL_CURVE_LINEAR, vehicleEyeAngles.z );
AngleMatrix( vehicleEyeAngles, vehicleEyeOrigin, vehicleEyePosToWorld );
// Now treat the relative eye angles as being relative to this new, perterbed view position...
matrix3x4_t newCameraToWorld;
ConcatTransforms( vehicleEyePosToWorld, vehicleCameraToEyePos, newCameraToWorld );
// output new view abs angles
MatrixAngles( newCameraToWorld, *pAbsAngles );
// UNDONE: *pOrigin would already be correct in single player if the HandleView() on the server ran after vphysics
MatrixGetColumn( newCameraToWorld, 3, *pAbsOrigin );
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CCraneServerVehicle::NPC_SetDriver( CNPC_VehicleDriver *pDriver )
{
GetCrane()->SetNPCDriver( pDriver );
if ( pDriver )
{
SetVehicleVolume( 1.0 ); // Vehicles driven by NPCs are louder
GetCrane()->SetSimulatedEveryTick( false );
}
else
{
SetVehicleVolume( 0.5 );
GetCrane()->SetSimulatedEveryTick( true );
}
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CCraneServerVehicle::NPC_DriveVehicle( void )
{
if ( g_debug_vehicledriver.GetInt() )
{
if ( m_nNPCButtons )
{
Vector vecForward, vecRight;
GetCrane()->GetVectors( &vecForward, &vecRight, NULL );
if ( m_nNPCButtons & IN_FORWARD )
{
NDebugOverlay::Line( GetCrane()->GetAbsOrigin(), GetCrane()->GetAbsOrigin() + vecForward * 200, 0,255,0, true, 0.1 );
}
if ( m_nNPCButtons & IN_BACK )
{
NDebugOverlay::Line( GetCrane()->GetAbsOrigin(), GetCrane()->GetAbsOrigin() - vecForward * 200, 0,255,0, true, 0.1 );
}
if ( m_nNPCButtons & IN_MOVELEFT )
{
NDebugOverlay::Line( GetCrane()->GetAbsOrigin(), GetCrane()->GetAbsOrigin() - vecRight * 200, 0,255,0, true, 0.1 );
}
if ( m_nNPCButtons & IN_MOVERIGHT )
{
NDebugOverlay::Line( GetCrane()->GetAbsOrigin(), GetCrane()->GetAbsOrigin() + vecRight * 200, 0,255,0, true, 0.1 );
}
if ( m_nNPCButtons & IN_JUMP )
{
NDebugOverlay::Box( GetCrane()->GetAbsOrigin(), -Vector(20,20,20), Vector(20,20,20), 0,255,0, true, 0.1 );
}
}
}
GetCrane()->DriveCrane( m_nNPCButtons, m_nNPCButtons, m_flTurnDegrees );
// Clear out attack buttons each frame
m_nNPCButtons &= ~IN_ATTACK;
m_nNPCButtons &= ~IN_ATTACK2;
// Run the crane's movement
GetCrane()->RunCraneMovement( 0.1 );
}
//===============================================================================================================================
// CRANE CABLE TIP
//===============================================================================================================================
//-----------------------------------------------------------------------------
// Purpose: To by usable by the constraint system, this needs to have a phys model.
//-----------------------------------------------------------------------------
void CCraneTip::Spawn( void )
{
Precache();
SetModel( "models/props_junk/cardboard_box001a.mdl" );
AddEffects( EF_NODRAW );
// We don't want this to be solid, because we don't want it to collide with the hydra.
SetSolid( SOLID_VPHYSICS );
AddSolidFlags( FSOLID_NOT_SOLID );
VPhysicsInitShadow( false, false );
// Disable movement on this sucker, we're going to move him manually
SetMoveType( MOVETYPE_NONE );
BaseClass::Spawn();
m_pSpring = NULL;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CCraneTip::Precache( void )
{
PrecacheModel( "models/props_junk/cardboard_box001a.mdl" );
BaseClass::Precache();
}
//-----------------------------------------------------------------------------
// Purpose: Activate/create the constraint
//-----------------------------------------------------------------------------
bool CCraneTip::CreateConstraint( CBaseAnimating *pCraneMagnet, IPhysicsConstraintGroup *pGroup )
{
IPhysicsObject *pPhysObject = VPhysicsGetObject();
IPhysicsObject *pCraneMagnetPhysObject = pCraneMagnet->VPhysicsGetObject();
if ( !pCraneMagnetPhysObject )
{
Msg(" Error: Tried to create a crane_tip with a crane magnet that has no physics model.\n" );
return false;
}
Assert( pPhysObject );
// Check to see if it's got an attachment point to connect to
Vector vecPoint = pCraneMagnet->GetAbsOrigin();
int iIndex = pCraneMagnet->LookupAttachment("magnetcable_a");
if ( iIndex >= 0 )
{
pCraneMagnet->GetAttachment( iIndex, vecPoint );
}
// Create our spring
/*
constraint_lengthparams_t length;
length.Defaults();
length.InitWorldspace( pPhysObject, pCraneMagnetPhysObject, GetAbsOrigin(), vecPoint );
length.constraint.Defaults();
m_pConstraint = physenv->CreateLengthConstraint( pPhysObject, pCraneMagnetPhysObject, pGroup, length );
*/
springparams_t spring;
spring.constant = CRANE_SPRING_CONSTANT_HANGING;
spring.damping = CRANE_SPRING_DAMPING;
spring.naturalLength = (GetAbsOrigin() - vecPoint).Length();
spring.relativeDamping = CRANE_SPRING_RELATIVE_DAMPING;
spring.startPosition = GetAbsOrigin();
spring.endPosition = vecPoint;
spring.useLocalPositions = false;
spring.onlyStretch = true;
m_pSpring = physenv->CreateSpring( pPhysObject, pCraneMagnetPhysObject, &spring );
return true;
}
//-----------------------------------------------------------------------------
// Purpose: Create a Hydra Impale between the hydra and the entity passed in
//-----------------------------------------------------------------------------
CCraneTip *CCraneTip::Create( CBaseAnimating *pCraneMagnet, IPhysicsConstraintGroup *pGroup, const Vector &vecOrigin, const QAngle &vecAngles )
{
CCraneTip *pCraneTip = (CCraneTip *)CBaseEntity::Create( "crane_tip", vecOrigin, vecAngles );
if ( !pCraneTip )
return NULL;
if ( !pCraneTip->CreateConstraint( pCraneMagnet, pGroup ) )
return NULL;
return pCraneTip;
}
|