-
Notifications
You must be signed in to change notification settings - Fork 599
/
plats.cpp
2285 lines (1876 loc) · 55.4 KB
/
plats.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
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
/***
*
* Copyright (c) 1996-2001, Valve LLC. All rights reserved.
*
* This product contains software technology licensed from Id
* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc.
* All Rights Reserved.
*
* Use, distribution, and modification of this source code and/or resulting
* object code is restricted to non-commercial enhancements to products from
* Valve LLC. All other use, distribution, or modification is prohibited
* without written permission from Valve LLC.
*
****/
/*
===== plats.cpp ========================================================
spawn, think, and touch functions for trains, etc
*/
#include "extdll.h"
#include "util.h"
#include "cbase.h"
#include "trains.h"
#include "saverestore.h"
static void PlatSpawnInsideTrigger(entvars_t* pevPlatform);
#define SF_PLAT_TOGGLE 0x0001
class CBasePlatTrain : public CBaseToggle
{
public:
virtual int ObjectCaps( void ) { return CBaseEntity :: ObjectCaps() & ~FCAP_ACROSS_TRANSITION; }
void KeyValue( KeyValueData* pkvd);
void Precache( void );
// This is done to fix spawn flag collisions between this class and a derived class
virtual BOOL IsTogglePlat( void ) { return (pev->spawnflags & SF_PLAT_TOGGLE) ? TRUE : FALSE; }
virtual int Save( CSave &save );
virtual int Restore( CRestore &restore );
static TYPEDESCRIPTION m_SaveData[];
BYTE m_bMoveSnd; // sound a plat makes while moving
BYTE m_bStopSnd; // sound a plat makes when it stops
float m_volume; // Sound volume
};
TYPEDESCRIPTION CBasePlatTrain::m_SaveData[] =
{
DEFINE_FIELD( CBasePlatTrain, m_bMoveSnd, FIELD_CHARACTER ),
DEFINE_FIELD( CBasePlatTrain, m_bStopSnd, FIELD_CHARACTER ),
DEFINE_FIELD( CBasePlatTrain, m_volume, FIELD_FLOAT ),
};
IMPLEMENT_SAVERESTORE( CBasePlatTrain, CBaseToggle );
void CBasePlatTrain :: KeyValue( KeyValueData *pkvd )
{
if (FStrEq(pkvd->szKeyName, "lip"))
{
m_flLip = atof(pkvd->szValue);
pkvd->fHandled = TRUE;
}
else if (FStrEq(pkvd->szKeyName, "wait"))
{
m_flWait = atof(pkvd->szValue);
pkvd->fHandled = TRUE;
}
else if (FStrEq(pkvd->szKeyName, "height"))
{
m_flHeight = atof(pkvd->szValue);
pkvd->fHandled = TRUE;
}
else if (FStrEq(pkvd->szKeyName, "rotation"))
{
m_vecFinalAngle.x = atof(pkvd->szValue);
pkvd->fHandled = TRUE;
}
else if (FStrEq(pkvd->szKeyName, "movesnd"))
{
m_bMoveSnd = atof(pkvd->szValue);
pkvd->fHandled = TRUE;
}
else if (FStrEq(pkvd->szKeyName, "stopsnd"))
{
m_bStopSnd = atof(pkvd->szValue);
pkvd->fHandled = TRUE;
}
else if (FStrEq(pkvd->szKeyName, "volume"))
{
m_volume = atof(pkvd->szValue);
pkvd->fHandled = TRUE;
}
else
CBaseToggle::KeyValue( pkvd );
}
#define noiseMoving noise
#define noiseArrived noise1
void CBasePlatTrain::Precache( void )
{
// set the plat's "in-motion" sound
switch (m_bMoveSnd)
{
case 0:
pev->noiseMoving = MAKE_STRING("common/null.wav");
break;
case 1:
PRECACHE_SOUND ("plats/bigmove1.wav");
pev->noiseMoving = MAKE_STRING("plats/bigmove1.wav");
break;
case 2:
PRECACHE_SOUND ("plats/bigmove2.wav");
pev->noiseMoving = MAKE_STRING("plats/bigmove2.wav");
break;
case 3:
PRECACHE_SOUND ("plats/elevmove1.wav");
pev->noiseMoving = MAKE_STRING("plats/elevmove1.wav");
break;
case 4:
PRECACHE_SOUND ("plats/elevmove2.wav");
pev->noiseMoving = MAKE_STRING("plats/elevmove2.wav");
break;
case 5:
PRECACHE_SOUND ("plats/elevmove3.wav");
pev->noiseMoving = MAKE_STRING("plats/elevmove3.wav");
break;
case 6:
PRECACHE_SOUND ("plats/freightmove1.wav");
pev->noiseMoving = MAKE_STRING("plats/freightmove1.wav");
break;
case 7:
PRECACHE_SOUND ("plats/freightmove2.wav");
pev->noiseMoving = MAKE_STRING("plats/freightmove2.wav");
break;
case 8:
PRECACHE_SOUND ("plats/heavymove1.wav");
pev->noiseMoving = MAKE_STRING("plats/heavymove1.wav");
break;
case 9:
PRECACHE_SOUND ("plats/rackmove1.wav");
pev->noiseMoving = MAKE_STRING("plats/rackmove1.wav");
break;
case 10:
PRECACHE_SOUND ("plats/railmove1.wav");
pev->noiseMoving = MAKE_STRING("plats/railmove1.wav");
break;
case 11:
PRECACHE_SOUND ("plats/squeekmove1.wav");
pev->noiseMoving = MAKE_STRING("plats/squeekmove1.wav");
break;
case 12:
PRECACHE_SOUND ("plats/talkmove1.wav");
pev->noiseMoving = MAKE_STRING("plats/talkmove1.wav");
break;
case 13:
PRECACHE_SOUND ("plats/talkmove2.wav");
pev->noiseMoving = MAKE_STRING("plats/talkmove2.wav");
break;
default:
pev->noiseMoving = MAKE_STRING("common/null.wav");
break;
}
// set the plat's 'reached destination' stop sound
switch (m_bStopSnd)
{
case 0:
pev->noiseArrived = MAKE_STRING("common/null.wav");
break;
case 1:
PRECACHE_SOUND ("plats/bigstop1.wav");
pev->noiseArrived = MAKE_STRING("plats/bigstop1.wav");
break;
case 2:
PRECACHE_SOUND ("plats/bigstop2.wav");
pev->noiseArrived = MAKE_STRING("plats/bigstop2.wav");
break;
case 3:
PRECACHE_SOUND ("plats/freightstop1.wav");
pev->noiseArrived = MAKE_STRING("plats/freightstop1.wav");
break;
case 4:
PRECACHE_SOUND ("plats/heavystop2.wav");
pev->noiseArrived = MAKE_STRING("plats/heavystop2.wav");
break;
case 5:
PRECACHE_SOUND ("plats/rackstop1.wav");
pev->noiseArrived = MAKE_STRING("plats/rackstop1.wav");
break;
case 6:
PRECACHE_SOUND ("plats/railstop1.wav");
pev->noiseArrived = MAKE_STRING("plats/railstop1.wav");
break;
case 7:
PRECACHE_SOUND ("plats/squeekstop1.wav");
pev->noiseArrived = MAKE_STRING("plats/squeekstop1.wav");
break;
case 8:
PRECACHE_SOUND ("plats/talkstop1.wav");
pev->noiseArrived = MAKE_STRING("plats/talkstop1.wav");
break;
default:
pev->noiseArrived = MAKE_STRING("common/null.wav");
break;
}
}
//
//====================== PLAT code ====================================================
//
#define noiseMovement noise
#define noiseStopMoving noise1
class CFuncPlat : public CBasePlatTrain
{
public:
void Spawn( void );
void Precache( void );
void Setup( void );
virtual void Blocked( CBaseEntity *pOther );
void EXPORT PlatUse( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value );
void EXPORT CallGoDown( void ) { GoDown(); }
void EXPORT CallHitTop( void ) { HitTop(); }
void EXPORT CallHitBottom( void ) { HitBottom(); }
virtual void GoUp( void );
virtual void GoDown( void );
virtual void HitTop( void );
virtual void HitBottom( void );
};
LINK_ENTITY_TO_CLASS( func_plat, CFuncPlat );
// UNDONE: Need to save this!!! It needs class & linkage
class CPlatTrigger : public CBaseEntity
{
public:
virtual int ObjectCaps( void ) { return (CBaseEntity :: ObjectCaps() & ~FCAP_ACROSS_TRANSITION) | FCAP_DONT_SAVE; }
void SpawnInsideTrigger( CFuncPlat *pPlatform );
void Touch( CBaseEntity *pOther );
CFuncPlat *m_pPlatform;
};
/*QUAKED func_plat (0 .5 .8) ? PLAT_LOW_TRIGGER
speed default 150
Plats are always drawn in the extended position, so they will light correctly.
If the plat is the target of another trigger or button, it will start out disabled in
the extended position until it is trigger, when it will lower and become a normal plat.
If the "height" key is set, that will determine the amount the plat moves, instead of
being implicitly determined by the model's height.
Set "sounds" to one of the following:
1) base fast
2) chain slow
*/
void CFuncPlat :: Setup( void )
{
//pev->noiseMovement = MAKE_STRING("plats/platmove1.wav");
//pev->noiseStopMoving = MAKE_STRING("plats/platstop1.wav");
if (m_flTLength == 0)
m_flTLength = 80;
if (m_flTWidth == 0)
m_flTWidth = 10;
pev->angles = g_vecZero;
pev->solid = SOLID_BSP;
pev->movetype = MOVETYPE_PUSH;
UTIL_SetOrigin(pev, pev->origin); // set size and link into world
UTIL_SetSize(pev, pev->mins, pev->maxs);
SET_MODEL(ENT(pev), STRING(pev->model) );
// vecPosition1 is the top position, vecPosition2 is the bottom
m_vecPosition1 = pev->origin;
m_vecPosition2 = pev->origin;
if (m_flHeight != 0)
m_vecPosition2.z = pev->origin.z - m_flHeight;
else
m_vecPosition2.z = pev->origin.z - pev->size.z + 8;
if (pev->speed == 0)
pev->speed = 150;
if ( m_volume == 0 )
m_volume = 0.85;
}
void CFuncPlat :: Precache( )
{
CBasePlatTrain::Precache();
//PRECACHE_SOUND("plats/platmove1.wav");
//PRECACHE_SOUND("plats/platstop1.wav");
if ( !IsTogglePlat() )
PlatSpawnInsideTrigger( pev ); // the "start moving" trigger
}
void CFuncPlat :: Spawn( )
{
Setup();
Precache();
// If this platform is the target of some button, it starts at the TOP position,
// and is brought down by that button. Otherwise, it starts at BOTTOM.
if ( !FStringNull(pev->targetname) )
{
UTIL_SetOrigin (pev, m_vecPosition1);
m_toggle_state = TS_AT_TOP;
SetUse( &CFuncPlat::PlatUse );
}
else
{
UTIL_SetOrigin (pev, m_vecPosition2);
m_toggle_state = TS_AT_BOTTOM;
}
}
static void PlatSpawnInsideTrigger(entvars_t* pevPlatform)
{
GetClassPtr( (CPlatTrigger *)NULL)->SpawnInsideTrigger( GetClassPtr( (CFuncPlat *)pevPlatform ) );
}
//
// Create a trigger entity for a platform.
//
void CPlatTrigger :: SpawnInsideTrigger( CFuncPlat *pPlatform )
{
m_pPlatform = pPlatform;
// Create trigger entity, "point" it at the owning platform, give it a touch method
pev->solid = SOLID_TRIGGER;
pev->movetype = MOVETYPE_NONE;
pev->origin = pPlatform->pev->origin;
// Establish the trigger field's size
Vector vecTMin = m_pPlatform->pev->mins + Vector ( 25 , 25 , 0 );
Vector vecTMax = m_pPlatform->pev->maxs + Vector ( 25 , 25 , 8 );
vecTMin.z = vecTMax.z - ( m_pPlatform->m_vecPosition1.z - m_pPlatform->m_vecPosition2.z + 8 );
if (m_pPlatform->pev->size.x <= 50)
{
vecTMin.x = (m_pPlatform->pev->mins.x + m_pPlatform->pev->maxs.x) / 2;
vecTMax.x = vecTMin.x + 1;
}
if (m_pPlatform->pev->size.y <= 50)
{
vecTMin.y = (m_pPlatform->pev->mins.y + m_pPlatform->pev->maxs.y) / 2;
vecTMax.y = vecTMin.y + 1;
}
UTIL_SetSize ( pev, vecTMin, vecTMax );
}
//
// When the platform's trigger field is touched, the platform ???
//
void CPlatTrigger :: Touch( CBaseEntity *pOther )
{
// Ignore touches by non-players
entvars_t* pevToucher = pOther->pev;
if ( !FClassnameIs (pevToucher, "player") )
return;
// Ignore touches by corpses
if (!pOther->IsAlive()||!m_pPlatform||!m_pPlatform->pev)
return;
// Make linked platform go up/down.
if (m_pPlatform->m_toggle_state == TS_AT_BOTTOM)
m_pPlatform->GoUp();
else if (m_pPlatform->m_toggle_state == TS_AT_TOP)
m_pPlatform->pev->nextthink = m_pPlatform->pev->ltime + 1;// delay going down
}
//
// Used by SUB_UseTargets, when a platform is the target of a button.
// Start bringing platform down.
//
void CFuncPlat :: PlatUse( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value )
{
if ( IsTogglePlat() )
{
// Top is off, bottom is on
BOOL on = (m_toggle_state == TS_AT_BOTTOM) ? TRUE : FALSE;
if ( !ShouldToggle( useType, on ) )
return;
if (m_toggle_state == TS_AT_TOP)
GoDown();
else if ( m_toggle_state == TS_AT_BOTTOM )
GoUp();
}
else
{
SetUse( NULL );
if (m_toggle_state == TS_AT_TOP)
GoDown();
}
}
//
// Platform is at top, now starts moving down.
//
void CFuncPlat :: GoDown( void )
{
if(pev->noiseMovement)
EMIT_SOUND(ENT(pev), CHAN_STATIC, (char*)STRING(pev->noiseMovement), m_volume, ATTN_NORM);
ASSERT(m_toggle_state == TS_AT_TOP || m_toggle_state == TS_GOING_UP);
m_toggle_state = TS_GOING_DOWN;
SetMoveDone(&CFuncPlat::CallHitBottom);
LinearMove(m_vecPosition2, pev->speed);
}
//
// Platform has hit bottom. Stops and waits forever.
//
void CFuncPlat :: HitBottom( void )
{
if(pev->noiseMovement)
STOP_SOUND(ENT(pev), CHAN_STATIC, (char*)STRING(pev->noiseMovement));
if (pev->noiseStopMoving)
EMIT_SOUND(ENT(pev), CHAN_WEAPON, (char*)STRING(pev->noiseStopMoving), m_volume, ATTN_NORM);
ASSERT(m_toggle_state == TS_GOING_DOWN);
m_toggle_state = TS_AT_BOTTOM;
}
//
// Platform is at bottom, now starts moving up
//
void CFuncPlat :: GoUp( void )
{
if (pev->noiseMovement)
EMIT_SOUND(ENT(pev), CHAN_STATIC, (char*)STRING(pev->noiseMovement), m_volume, ATTN_NORM);
ASSERT(m_toggle_state == TS_AT_BOTTOM || m_toggle_state == TS_GOING_DOWN);
m_toggle_state = TS_GOING_UP;
SetMoveDone(&CFuncPlat::CallHitTop);
LinearMove(m_vecPosition1, pev->speed);
}
//
// Platform has hit top. Pauses, then starts back down again.
//
void CFuncPlat :: HitTop( void )
{
if(pev->noiseMovement)
STOP_SOUND(ENT(pev), CHAN_STATIC, (char*)STRING(pev->noiseMovement));
if (pev->noiseStopMoving)
EMIT_SOUND(ENT(pev), CHAN_WEAPON, (char*)STRING(pev->noiseStopMoving), m_volume, ATTN_NORM);
ASSERT(m_toggle_state == TS_GOING_UP);
m_toggle_state = TS_AT_TOP;
if ( !IsTogglePlat() )
{
// After a delay, the platform will automatically start going down again.
SetThink( &CFuncPlat::CallGoDown );
pev->nextthink = pev->ltime + 3;
}
}
void CFuncPlat :: Blocked( CBaseEntity *pOther )
{
ALERT( at_aiconsole, "%s Blocked by %s\n", STRING(pev->classname), STRING(pOther->pev->classname) );
// Hurt the blocker a little
pOther->TakeDamage(pev, pev, 1, DMG_CRUSH);
if(pev->noiseMovement)
STOP_SOUND(ENT(pev), CHAN_STATIC, (char*)STRING(pev->noiseMovement));
// Send the platform back where it came from
ASSERT(m_toggle_state == TS_GOING_UP || m_toggle_state == TS_GOING_DOWN);
if (m_toggle_state == TS_GOING_UP)
GoDown();
else if (m_toggle_state == TS_GOING_DOWN)
GoUp ();
}
class CFuncPlatRot : public CFuncPlat
{
public:
void Spawn( void );
void SetupRotation( void );
virtual void GoUp( void );
virtual void GoDown( void );
virtual void HitTop( void );
virtual void HitBottom( void );
void RotMove( Vector &destAngle, float time );
virtual int Save( CSave &save );
virtual int Restore( CRestore &restore );
static TYPEDESCRIPTION m_SaveData[];
Vector m_end, m_start;
};
LINK_ENTITY_TO_CLASS( func_platrot, CFuncPlatRot );
TYPEDESCRIPTION CFuncPlatRot::m_SaveData[] =
{
DEFINE_FIELD( CFuncPlatRot, m_end, FIELD_VECTOR ),
DEFINE_FIELD( CFuncPlatRot, m_start, FIELD_VECTOR ),
};
IMPLEMENT_SAVERESTORE( CFuncPlatRot, CFuncPlat );
void CFuncPlatRot :: SetupRotation( void )
{
if ( m_vecFinalAngle.x != 0 ) // This plat rotates too!
{
CBaseToggle :: AxisDir( pev );
m_start = pev->angles;
m_end = pev->angles + pev->movedir * m_vecFinalAngle.x;
}
else
{
m_start = g_vecZero;
m_end = g_vecZero;
}
if ( !FStringNull(pev->targetname) ) // Start at top
{
pev->angles = m_end;
}
}
void CFuncPlatRot :: Spawn( void )
{
CFuncPlat :: Spawn();
SetupRotation();
}
void CFuncPlatRot :: GoDown( void )
{
CFuncPlat :: GoDown();
RotMove( m_start, pev->nextthink - pev->ltime );
}
//
// Platform has hit bottom. Stops and waits forever.
//
void CFuncPlatRot :: HitBottom( void )
{
CFuncPlat :: HitBottom();
pev->avelocity = g_vecZero;
pev->angles = m_start;
}
//
// Platform is at bottom, now starts moving up
//
void CFuncPlatRot :: GoUp( void )
{
CFuncPlat :: GoUp();
RotMove( m_end, pev->nextthink - pev->ltime );
}
//
// Platform has hit top. Pauses, then starts back down again.
//
void CFuncPlatRot :: HitTop( void )
{
CFuncPlat :: HitTop();
pev->avelocity = g_vecZero;
pev->angles = m_end;
}
void CFuncPlatRot :: RotMove( Vector &destAngle, float time )
{
// set destdelta to the vector needed to move
Vector vecDestDelta = destAngle - pev->angles;
// Travel time is so short, we're practically there already; so make it so.
if ( time >= 0.1)
pev->avelocity = vecDestDelta / time;
else
{
pev->avelocity = vecDestDelta;
pev->nextthink = pev->ltime + 1;
}
}
//
//====================== TRAIN code ==================================================
//
class CFuncTrain : public CBasePlatTrain
{
public:
void Spawn( void );
void Precache( void );
void Activate( void );
void OverrideReset( void );
void Blocked( CBaseEntity *pOther );
void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value );
void KeyValue( KeyValueData *pkvd );
void EXPORT Wait( void );
void EXPORT Next( void );
virtual int Save( CSave &save );
virtual int Restore( CRestore &restore );
static TYPEDESCRIPTION m_SaveData[];
entvars_t *m_pevCurrentTarget;
int m_sounds;
BOOL m_activated;
};
LINK_ENTITY_TO_CLASS( func_train, CFuncTrain );
TYPEDESCRIPTION CFuncTrain::m_SaveData[] =
{
DEFINE_FIELD( CFuncTrain, m_sounds, FIELD_INTEGER ),
DEFINE_FIELD( CFuncTrain, m_pevCurrentTarget, FIELD_EVARS ),
DEFINE_FIELD( CFuncTrain, m_activated, FIELD_BOOLEAN ),
};
IMPLEMENT_SAVERESTORE( CFuncTrain, CBasePlatTrain );
void CFuncTrain :: KeyValue( KeyValueData *pkvd )
{
if (FStrEq(pkvd->szKeyName, "sounds"))
{
m_sounds = atoi(pkvd->szValue);
pkvd->fHandled = TRUE;
}
else
CBasePlatTrain::KeyValue( pkvd );
}
void CFuncTrain :: Blocked( CBaseEntity *pOther )
{
if ( gpGlobals->time < m_flActivateFinished)
return;
m_flActivateFinished = gpGlobals->time + 0.5;
pOther->TakeDamage(pev, pev, pev->dmg, DMG_CRUSH);
}
void CFuncTrain :: Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value )
{
if ( pev->spawnflags & SF_TRAIN_WAIT_RETRIGGER )
{
// Move toward my target
pev->spawnflags &= ~SF_TRAIN_WAIT_RETRIGGER;
Next();
}
else
{
pev->spawnflags |= SF_TRAIN_WAIT_RETRIGGER;
// Pop back to last target if it's available
if ( pev->enemy )
pev->target = pev->enemy->v.targetname;
pev->nextthink = 0;
pev->velocity = g_vecZero;
if ( pev->noiseStopMoving )
EMIT_SOUND (ENT(pev), CHAN_VOICE, (char*)STRING(pev->noiseStopMoving), m_volume, ATTN_NORM);
}
}
void CFuncTrain :: Wait( void )
{
// Fire the pass target if there is one
if ( m_pevCurrentTarget->message )
{
FireTargets( STRING(m_pevCurrentTarget->message), this, this, USE_TOGGLE, 0 );
if ( FBitSet( m_pevCurrentTarget->spawnflags, SF_CORNER_FIREONCE ) )
m_pevCurrentTarget->message = 0;
}
// need pointer to LAST target.
if ( FBitSet (m_pevCurrentTarget->spawnflags , SF_TRAIN_WAIT_RETRIGGER ) || ( pev->spawnflags & SF_TRAIN_WAIT_RETRIGGER ) )
{
pev->spawnflags |= SF_TRAIN_WAIT_RETRIGGER;
// clear the sound channel.
if ( pev->noiseMovement )
STOP_SOUND( edict(), CHAN_STATIC, (char*)STRING(pev->noiseMovement) );
if ( pev->noiseStopMoving )
EMIT_SOUND (ENT(pev), CHAN_VOICE, (char*)STRING(pev->noiseStopMoving), m_volume, ATTN_NORM);
pev->nextthink = 0;
return;
}
// ALERT ( at_console, "%f\n", m_flWait );
if (m_flWait != 0)
{// -1 wait will wait forever!
pev->nextthink = pev->ltime + m_flWait;
if ( pev->noiseMovement )
STOP_SOUND( edict(), CHAN_STATIC, (char*)STRING(pev->noiseMovement) );
if ( pev->noiseStopMoving )
EMIT_SOUND (ENT(pev), CHAN_VOICE, (char*)STRING(pev->noiseStopMoving), m_volume, ATTN_NORM);
SetThink( &CFuncTrain::Next );
}
else
{
Next();// do it RIGHT now!
}
}
//
// Train next - path corner needs to change to next target
//
void CFuncTrain :: Next( void )
{
CBaseEntity *pTarg;
// now find our next target
pTarg = GetNextTarget();
if ( !pTarg )
{
if ( pev->noiseMovement )
STOP_SOUND( edict(), CHAN_STATIC, (char*)STRING(pev->noiseMovement) );
// Play stop sound
if ( pev->noiseStopMoving )
EMIT_SOUND (ENT(pev), CHAN_VOICE, (char*)STRING(pev->noiseStopMoving), m_volume, ATTN_NORM);
return;
}
// Save last target in case we need to find it again
pev->message = pev->target;
pev->target = pTarg->pev->target;
m_flWait = pTarg->GetDelay();
if ( m_pevCurrentTarget && m_pevCurrentTarget->speed != 0 )
{// don't copy speed from target if it is 0 (uninitialized)
pev->speed = m_pevCurrentTarget->speed;
ALERT( at_aiconsole, "Train %s speed to %4.2f\n", STRING(pev->targetname), pev->speed );
}
m_pevCurrentTarget = pTarg->pev;// keep track of this since path corners change our target for us.
pev->enemy = pTarg->edict();//hack
if(FBitSet(m_pevCurrentTarget->spawnflags, SF_CORNER_TELEPORT))
{
// Path corner has indicated a teleport to the next corner.
SetBits(pev->effects, EF_NOINTERP);
UTIL_SetOrigin(pev, pTarg->pev->origin - (pev->mins + pev->maxs)* 0.5);
Wait(); // Get on with doing the next path corner.
}
else
{
// Normal linear move.
// CHANGED this from CHAN_VOICE to CHAN_STATIC around OEM beta time because trains should
// use CHAN_STATIC for their movement sounds to prevent sound field problems.
// this is not a hack or temporary fix, this is how things should be. (sjb).
if ( pev->noiseMovement )
STOP_SOUND( edict(), CHAN_STATIC, (char*)STRING(pev->noiseMovement) );
if ( pev->noiseMovement )
EMIT_SOUND (ENT(pev), CHAN_STATIC, (char*)STRING(pev->noiseMovement), m_volume, ATTN_NORM);
ClearBits(pev->effects, EF_NOINTERP);
SetMoveDone( &CFuncTrain::Wait );
LinearMove (pTarg->pev->origin - (pev->mins + pev->maxs)* 0.5, pev->speed);
}
}
void CFuncTrain :: Activate( void )
{
// Not yet active, so teleport to first target
if ( !m_activated )
{
m_activated = TRUE;
entvars_t *pevTarg = VARS( FIND_ENTITY_BY_TARGETNAME (NULL, STRING(pev->target) ) );
pev->target = pevTarg->target;
m_pevCurrentTarget = pevTarg;// keep track of this since path corners change our target for us.
UTIL_SetOrigin (pev, pevTarg->origin - (pev->mins + pev->maxs) * 0.5 );
if ( FStringNull(pev->targetname) )
{ // not triggered, so start immediately
pev->nextthink = pev->ltime + 0.1;
SetThink( &CFuncTrain::Next );
}
else
pev->spawnflags |= SF_TRAIN_WAIT_RETRIGGER;
}
}
/*QUAKED func_train (0 .5 .8) ?
Trains are moving platforms that players can ride.
The targets origin specifies the min point of the train at each corner.
The train spawns at the first target it is pointing at.
If the train is the target of a button or trigger, it will not begin moving until activated.
speed default 100
dmg default 2
sounds
1) ratchet metal
*/
void CFuncTrain :: Spawn( void )
{
Precache();
if (pev->speed == 0)
pev->speed = 100;
if ( FStringNull(pev->target) )
ALERT(at_console, "FuncTrain with no target");
if (pev->dmg == 0)
pev->dmg = 2;
pev->movetype = MOVETYPE_PUSH;
if ( FBitSet (pev->spawnflags, SF_TRACKTRAIN_PASSABLE) )
pev->solid = SOLID_NOT;
else
pev->solid = SOLID_BSP;
SET_MODEL( ENT(pev), STRING(pev->model) );
UTIL_SetSize (pev, pev->mins, pev->maxs);
UTIL_SetOrigin(pev, pev->origin);
m_activated = FALSE;
if ( m_volume == 0 )
m_volume = 0.85;
}
void CFuncTrain::Precache( void )
{
CBasePlatTrain::Precache();
#if 0 // obsolete
// otherwise use preset sound
switch (m_sounds)
{
case 0:
pev->noise = 0;
pev->noise1 = 0;
break;
case 1:
PRECACHE_SOUND ("plats/train2.wav");
PRECACHE_SOUND ("plats/train1.wav");
pev->noise = MAKE_STRING("plats/train2.wav");
pev->noise1 = MAKE_STRING("plats/train1.wav");
break;
case 2:
PRECACHE_SOUND ("plats/platmove1.wav");
PRECACHE_SOUND ("plats/platstop1.wav");
pev->noise = MAKE_STRING("plats/platstop1.wav");
pev->noise1 = MAKE_STRING("plats/platmove1.wav");
break;
}
#endif
}
void CFuncTrain::OverrideReset( void )
{
CBaseEntity *pTarg;
// Are we moving?
if ( pev->velocity != g_vecZero && pev->nextthink != 0 )
{
pev->target = pev->message;
// now find our next target
pTarg = GetNextTarget();
if ( !pTarg )
{
pev->nextthink = 0;
pev->velocity = g_vecZero;
}
else // Keep moving for 0.1 secs, then find path_corner again and restart
{
SetThink( &CFuncTrain::Next );
pev->nextthink = pev->ltime + 0.1;
}
}
}
// ---------------------------------------------------------------------
//
// Track Train
//
// ---------------------------------------------------------------------
TYPEDESCRIPTION CFuncTrackTrain::m_SaveData[] =
{
DEFINE_FIELD( CFuncTrackTrain, m_ppath, FIELD_CLASSPTR ),
DEFINE_FIELD( CFuncTrackTrain, m_length, FIELD_FLOAT ),
DEFINE_FIELD( CFuncTrackTrain, m_height, FIELD_FLOAT ),
DEFINE_FIELD( CFuncTrackTrain, m_speed, FIELD_FLOAT ),
DEFINE_FIELD( CFuncTrackTrain, m_dir, FIELD_FLOAT ),
DEFINE_FIELD( CFuncTrackTrain, m_startSpeed, FIELD_FLOAT ),
DEFINE_FIELD( CFuncTrackTrain, m_controlMins, FIELD_VECTOR ),
DEFINE_FIELD( CFuncTrackTrain, m_controlMaxs, FIELD_VECTOR ),
DEFINE_FIELD( CFuncTrackTrain, m_sounds, FIELD_INTEGER ),
DEFINE_FIELD( CFuncTrackTrain, m_flVolume, FIELD_FLOAT ),
DEFINE_FIELD( CFuncTrackTrain, m_flBank, FIELD_FLOAT ),
DEFINE_FIELD( CFuncTrackTrain, m_oldSpeed, FIELD_FLOAT ),
};
IMPLEMENT_SAVERESTORE( CFuncTrackTrain, CBaseEntity );
LINK_ENTITY_TO_CLASS( func_tracktrain, CFuncTrackTrain );
void CFuncTrackTrain :: KeyValue( KeyValueData *pkvd )
{
if (FStrEq(pkvd->szKeyName, "wheels"))
{
m_length = atof(pkvd->szValue);
pkvd->fHandled = TRUE;
}
else if (FStrEq(pkvd->szKeyName, "height"))
{
m_height = atof(pkvd->szValue);
pkvd->fHandled = TRUE;
}
else if (FStrEq(pkvd->szKeyName, "startspeed"))
{
m_startSpeed = atof(pkvd->szValue);
pkvd->fHandled = TRUE;
}
else if (FStrEq(pkvd->szKeyName, "sounds"))
{
m_sounds = atoi(pkvd->szValue);
pkvd->fHandled = TRUE;
}
else if (FStrEq(pkvd->szKeyName, "volume"))
{
m_flVolume = (float) (atoi(pkvd->szValue));
m_flVolume *= 0.1;
pkvd->fHandled = TRUE;
}
else if (FStrEq(pkvd->szKeyName, "bank"))
{
m_flBank = atof(pkvd->szValue);
pkvd->fHandled = TRUE;
}
else
CBaseEntity::KeyValue( pkvd );
}
void CFuncTrackTrain :: NextThink( float thinkTime, BOOL alwaysThink )
{
if ( alwaysThink )
pev->flags |= FL_ALWAYSTHINK;
else
pev->flags &= ~FL_ALWAYSTHINK;