-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathg_func.cpp
3651 lines (2924 loc) · 103 KB
/
g_func.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) ZeniMax Media Inc.
// Licensed under the GNU General Public License 2.0.
#include "g_local.h"
/*
=========================================================
PLATS
movement options:
linear
smooth start, hard stop
smooth start, smooth stop
start
end
acceleration
speed
deceleration
begin sound
end sound
target fired when reaching end
wait at end
object characteristics that use move segments
---------------------------------------------
movetype_push, or movetype_stop
action when touched
action when blocked
action when used
disabled?
auto trigger spawning
=========================================================
*/
constexpr spawnflags_t SPAWNFLAG_DOOR_START_OPEN = 1_spawnflag;
constexpr spawnflags_t SPAWNFLAG_DOOR_CRUSHER = 4_spawnflag;
constexpr spawnflags_t SPAWNFLAG_DOOR_NOMONSTER = 8_spawnflag;
constexpr spawnflags_t SPAWNFLAG_DOOR_ANIMATED = 16_spawnflag;
constexpr spawnflags_t SPAWNFLAG_DOOR_TOGGLE = 32_spawnflag;
constexpr spawnflags_t SPAWNFLAG_DOOR_ANIMATED_FAST = 64_spawnflag;
constexpr spawnflags_t SPAWNFLAG_DOOR_ROTATING_X_AXIS = 64_spawnflag;
constexpr spawnflags_t SPAWNFLAG_DOOR_ROTATING_Y_AXIS = 128_spawnflag;
constexpr spawnflags_t SPAWNFLAG_DOOR_ROTATING_INACTIVE = 0x10000_spawnflag; // Paril: moved to non-reserved
constexpr spawnflags_t SPAWNFLAG_DOOR_ROTATING_SAFE_OPEN = 0x20000_spawnflag;
// support routine for setting moveinfo sounds
static inline int32_t G_GetMoveinfoSoundIndex(gentity_t *self, const char *default_value, const char *wanted_value) {
if (!wanted_value) {
if (default_value)
return gi.soundindex(default_value);
return 0;
} else if (!*wanted_value || *wanted_value == '0' || *wanted_value == ' ')
return 0;
return gi.soundindex(wanted_value);
}
void G_SetMoveinfoSounds(gentity_t *self, const char *default_start, const char *default_mid, const char *default_end) {
self->moveinfo.sound_start = G_GetMoveinfoSoundIndex(self, default_start, st.noise_start);
self->moveinfo.sound_middle = G_GetMoveinfoSoundIndex(self, default_mid, st.noise_middle);
self->moveinfo.sound_end = G_GetMoveinfoSoundIndex(self, default_end, st.noise_end);
}
//
// Support routines for movement (changes in origin using velocity)
//
static THINK(Move_Done) (gentity_t *ent) -> void {
ent->velocity = {};
ent->moveinfo.endfunc(ent);
}
static THINK(Move_Final) (gentity_t *ent) -> void {
if (ent->moveinfo.remaining_distance == 0) {
Move_Done(ent);
return;
}
// [Paril-KEX] use exact remaining distance
ent->velocity = (ent->moveinfo.dest - ent->s.origin) * (1.f / gi.frame_time_s);
ent->think = Move_Done;
ent->nextthink = level.time + FRAME_TIME_S;
}
static THINK(Move_Begin) (gentity_t *ent) -> void {
float frames;
if ((ent->moveinfo.speed * gi.frame_time_s) >= ent->moveinfo.remaining_distance) {
Move_Final(ent);
return;
}
ent->velocity = ent->moveinfo.dir * ent->moveinfo.speed;
frames = floor((ent->moveinfo.remaining_distance / ent->moveinfo.speed) / gi.frame_time_s);
ent->moveinfo.remaining_distance -= frames * ent->moveinfo.speed * gi.frame_time_s;
ent->nextthink = level.time + (FRAME_TIME_S * frames);
ent->think = Move_Final;
}
void Think_AccelMove_New(gentity_t *ent);
void Think_AccelMove(gentity_t *ent);
bool Think_AccelMove_MoveInfo(moveinfo_t *moveinfo);
static constexpr float AccelerationDistance(float target, float rate) {
return (target * ((target / rate) + 1) / 2);
}
static inline void Move_Regular(gentity_t *ent, const vec3_t &dest, void(*endfunc)(gentity_t *self)) {
if (level.current_entity == ((ent->flags & FL_TEAMSLAVE) ? ent->teammaster : ent)) {
Move_Begin(ent);
} else {
ent->nextthink = level.time + FRAME_TIME_S;
ent->think = Move_Begin;
}
}
void Move_Calc(gentity_t *ent, const vec3_t &dest, void(*endfunc)(gentity_t *self)) {
ent->velocity = {};
ent->moveinfo.dest = dest;
ent->moveinfo.dir = dest - ent->s.origin;
ent->moveinfo.remaining_distance = ent->moveinfo.dir.normalize();
ent->moveinfo.endfunc = endfunc;
if (ent->moveinfo.speed == ent->moveinfo.accel && ent->moveinfo.speed == ent->moveinfo.decel) {
Move_Regular(ent, dest, endfunc);
} else {
// accelerative
ent->moveinfo.current_speed = 0;
if (gi.tick_rate == 10)
ent->think = Think_AccelMove;
else {
// [Paril-KEX] rewritten to work better at higher tickrates
ent->moveinfo.curve_frame = 0;
ent->moveinfo.num_subframes = (0.1f / gi.frame_time_s) - 1;
float total_dist = ent->moveinfo.remaining_distance;
std::vector<float> distances;
if (ent->moveinfo.num_subframes) {
distances.push_back(0);
ent->moveinfo.curve_frame = 1;
} else
ent->moveinfo.curve_frame = 0;
// simulate 10hz movement
while (ent->moveinfo.remaining_distance) {
if (!Think_AccelMove_MoveInfo(&ent->moveinfo))
break;
ent->moveinfo.remaining_distance -= ent->moveinfo.current_speed;
distances.push_back(total_dist - ent->moveinfo.remaining_distance);
}
if (ent->moveinfo.num_subframes)
distances.push_back(total_dist);
ent->moveinfo.subframe = 0;
ent->moveinfo.curve_ref = ent->s.origin;
ent->moveinfo.curve_positions = make_savable_memory<float, TAG_LEVEL>(distances.size());
std::copy(distances.begin(), distances.end(), ent->moveinfo.curve_positions.ptr);
ent->moveinfo.num_frames_done = 0;
ent->think = Think_AccelMove_New;
}
ent->nextthink = level.time + FRAME_TIME_S;
}
}
THINK(Think_AccelMove_New) (gentity_t *ent) -> void {
float t = 0.f;
float target_dist;
if (ent->moveinfo.num_subframes) {
if (ent->moveinfo.subframe == ent->moveinfo.num_subframes + 1) {
ent->moveinfo.subframe = 0;
ent->moveinfo.curve_frame++;
if (ent->moveinfo.curve_frame == ent->moveinfo.curve_positions.count) {
Move_Final(ent);
return;
}
}
t = (ent->moveinfo.subframe + 1) / ((float)ent->moveinfo.num_subframes + 1);
target_dist = lerp(ent->moveinfo.curve_positions[ent->moveinfo.curve_frame - 1], ent->moveinfo.curve_positions[ent->moveinfo.curve_frame], t);
ent->moveinfo.subframe++;
} else {
if (ent->moveinfo.curve_frame == ent->moveinfo.curve_positions.count) {
Move_Final(ent);
return;
}
target_dist = ent->moveinfo.curve_positions[ent->moveinfo.curve_frame++];
}
ent->moveinfo.num_frames_done++;
vec3_t target_pos = ent->moveinfo.curve_ref + (ent->moveinfo.dir * target_dist);
ent->velocity = (target_pos - ent->s.origin) * (1.f / gi.frame_time_s);
ent->nextthink = level.time + FRAME_TIME_S;
}
//
// Support routines for angular movement (changes in angle using avelocity)
//
static THINK(AngleMove_Done) (gentity_t *ent) -> void {
ent->avelocity = {};
ent->moveinfo.endfunc(ent);
}
static THINK(AngleMove_Final) (gentity_t *ent) -> void {
vec3_t move;
if (ent->moveinfo.state == STATE_UP) {
if (ent->moveinfo.reversing)
move = ent->moveinfo.end_angles_reversed - ent->s.angles;
else
move = ent->moveinfo.end_angles - ent->s.angles;
} else
move = ent->moveinfo.start_angles - ent->s.angles;
if (!move) {
AngleMove_Done(ent);
return;
}
ent->avelocity = move * (1.0f / gi.frame_time_s);
ent->think = AngleMove_Done;
ent->nextthink = level.time + FRAME_TIME_S;
}
static THINK(AngleMove_Begin) (gentity_t *ent) -> void {
vec3_t destdelta;
float len;
float traveltime;
float frames;
// accelerate as needed
if (ent->moveinfo.speed < ent->speed) {
ent->moveinfo.speed += ent->accel;
if (ent->moveinfo.speed > ent->speed)
ent->moveinfo.speed = ent->speed;
}
// set destdelta to the vector needed to move
if (ent->moveinfo.state == STATE_UP) {
if (ent->moveinfo.reversing)
destdelta = ent->moveinfo.end_angles_reversed - ent->s.angles;
else
destdelta = ent->moveinfo.end_angles - ent->s.angles;
} else
destdelta = ent->moveinfo.start_angles - ent->s.angles;
// calculate length of vector
len = destdelta.length();
// divide by speed to get time to reach dest
traveltime = len / ent->moveinfo.speed;
if (traveltime < gi.frame_time_s) {
AngleMove_Final(ent);
return;
}
frames = floor(traveltime / gi.frame_time_s);
// scale the destdelta vector by the time spent traveling to get velocity
ent->avelocity = destdelta * (1.0f / traveltime);
// if we're done accelerating, act as a normal rotation
if (ent->moveinfo.speed >= ent->speed) {
// set nextthink to trigger a think when dest is reached
ent->nextthink = level.time + (FRAME_TIME_S * frames);
ent->think = AngleMove_Final;
} else {
ent->nextthink = level.time + FRAME_TIME_S;
ent->think = AngleMove_Begin;
}
}
static void AngleMove_Calc(gentity_t *ent, void(*endfunc)(gentity_t *self)) {
ent->avelocity = {};
ent->moveinfo.endfunc = endfunc;
// if we're supposed to accelerate, this will tell anglemove_begin to do so
if (ent->accel != ent->speed)
ent->moveinfo.speed = 0;
if (level.current_entity == ((ent->flags & FL_TEAMSLAVE) ? ent->teammaster : ent)) {
AngleMove_Begin(ent);
} else {
ent->nextthink = level.time + FRAME_TIME_S;
ent->think = AngleMove_Begin;
}
}
/*
==============
Think_AccelMove
The team has completed a frame of movement, so
change the speed for the next frame
==============
*/
static void plat_CalcAcceleratedMove(moveinfo_t *moveinfo) {
float accel_dist;
float decel_dist;
if (moveinfo->remaining_distance < moveinfo->accel) {
moveinfo->move_speed = moveinfo->speed;
moveinfo->current_speed = moveinfo->remaining_distance;
return;
}
accel_dist = AccelerationDistance(moveinfo->speed, moveinfo->accel);
decel_dist = AccelerationDistance(moveinfo->speed, moveinfo->decel);
if ((moveinfo->remaining_distance - accel_dist - decel_dist) < 0) {
float f;
f = (moveinfo->accel + moveinfo->decel) / (moveinfo->accel * moveinfo->decel);
moveinfo->move_speed = moveinfo->current_speed =
(-2 + sqrt(4 - 4 * f * (-2 * moveinfo->remaining_distance))) / (2 * f);
decel_dist = AccelerationDistance(moveinfo->move_speed, moveinfo->decel);
} else
moveinfo->move_speed = moveinfo->speed;
moveinfo->decel_distance = decel_dist;
};
static void plat_Accelerate(moveinfo_t *moveinfo) {
// are we decelerating?
if (moveinfo->remaining_distance <= moveinfo->decel_distance) {
if (moveinfo->remaining_distance < moveinfo->decel_distance) {
if (moveinfo->next_speed) {
moveinfo->current_speed = moveinfo->next_speed;
moveinfo->next_speed = 0;
return;
}
if (moveinfo->current_speed > moveinfo->decel) {
moveinfo->current_speed -= moveinfo->decel;
// [Paril-KEX] fix platforms in xdm6, etc
if (fabsf(moveinfo->current_speed) < 0.01f)
moveinfo->current_speed = moveinfo->remaining_distance + 1;
}
}
return;
}
// are we at full speed and need to start decelerating during this move?
if (moveinfo->current_speed == moveinfo->move_speed)
if ((moveinfo->remaining_distance - moveinfo->current_speed) < moveinfo->decel_distance) {
float p1_distance;
float p2_distance;
float distance;
p1_distance = moveinfo->remaining_distance - moveinfo->decel_distance;
p2_distance = moveinfo->move_speed * (1.0f - (p1_distance / moveinfo->move_speed));
distance = p1_distance + p2_distance;
moveinfo->current_speed = moveinfo->move_speed;
moveinfo->next_speed = moveinfo->move_speed - moveinfo->decel * (p2_distance / distance);
return;
}
// are we accelerating?
if (moveinfo->current_speed < moveinfo->speed) {
float old_speed;
float p1_distance;
float p1_speed;
float p2_distance;
float distance;
old_speed = moveinfo->current_speed;
// figure simple acceleration up to move_speed
moveinfo->current_speed += moveinfo->accel;
if (moveinfo->current_speed > moveinfo->speed)
moveinfo->current_speed = moveinfo->speed;
// are we accelerating throughout this entire move?
if ((moveinfo->remaining_distance - moveinfo->current_speed) >= moveinfo->decel_distance)
return;
// during this move we will accelerate from current_speed to move_speed
// and cross over the decel_distance; figure the average speed for the
// entire move
p1_distance = moveinfo->remaining_distance - moveinfo->decel_distance;
p1_speed = (old_speed + moveinfo->move_speed) / 2.0f;
p2_distance = moveinfo->move_speed * (1.0f - (p1_distance / p1_speed));
distance = p1_distance + p2_distance;
moveinfo->current_speed =
(p1_speed * (p1_distance / distance)) + (moveinfo->move_speed * (p2_distance / distance));
moveinfo->next_speed = moveinfo->move_speed - moveinfo->decel * (p2_distance / distance);
return;
}
// we are at constant velocity (move_speed)
return;
}
bool Think_AccelMove_MoveInfo(moveinfo_t *moveinfo) {
if (moveinfo->current_speed == 0) // starting or blocked
plat_CalcAcceleratedMove(moveinfo);
plat_Accelerate(moveinfo);
// will the entire move complete on next frame?
return moveinfo->remaining_distance > moveinfo->current_speed;
}
// Paril: old acceleration code; this is here only to support old save games.
THINK(Think_AccelMove) (gentity_t *ent) -> void {
// [Paril-KEX] calculate distance dynamically
if (ent->moveinfo.state == STATE_UP)
ent->moveinfo.remaining_distance = (ent->moveinfo.start_origin - ent->s.origin).length();
else
ent->moveinfo.remaining_distance = (ent->moveinfo.end_origin - ent->s.origin).length();
// will the entire move complete on next frame?
if (!Think_AccelMove_MoveInfo(&ent->moveinfo)) {
Move_Final(ent);
return;
}
if (ent->moveinfo.remaining_distance <= ent->moveinfo.current_speed) {
Move_Final(ent);
return;
}
ent->velocity = ent->moveinfo.dir * (ent->moveinfo.current_speed * 10);
ent->nextthink = level.time + 10_hz;
ent->think = Think_AccelMove;
}
void plat_go_down(gentity_t *ent);
MOVEINFO_ENDFUNC(plat_hit_top) (gentity_t *ent) -> void {
if (!(ent->flags & FL_TEAMSLAVE)) {
if (ent->moveinfo.sound_end)
gi.sound(ent, CHAN_NO_PHS_ADD | CHAN_VOICE, ent->moveinfo.sound_end, 1, ATTN_STATIC, 0);
}
ent->s.sound = 0;
ent->moveinfo.state = STATE_TOP;
ent->think = plat_go_down;
ent->nextthink = level.time + 3_sec;
}
MOVEINFO_ENDFUNC(plat_hit_bottom) (gentity_t *ent) -> void {
if (!(ent->flags & FL_TEAMSLAVE)) {
if (ent->moveinfo.sound_end)
gi.sound(ent, CHAN_NO_PHS_ADD | CHAN_VOICE, ent->moveinfo.sound_end, 1, ATTN_STATIC, 0);
}
ent->s.sound = 0;
ent->moveinfo.state = STATE_BOTTOM;
plat2_kill_danger_area(ent);
}
THINK(plat_go_down) (gentity_t *ent) -> void {
if (!(ent->flags & FL_TEAMSLAVE)) {
if (ent->moveinfo.sound_start)
gi.sound(ent, CHAN_NO_PHS_ADD | CHAN_VOICE, ent->moveinfo.sound_start, 1, ATTN_STATIC, 0);
}
ent->s.sound = ent->moveinfo.sound_middle;
ent->moveinfo.state = STATE_DOWN;
Move_Calc(ent, ent->moveinfo.end_origin, plat_hit_bottom);
if (g_mover_debug->integer)
gi.Com_PrintFmt("Go down {}\n", *ent);
}
static void plat_go_up(gentity_t *ent) {
if (!(ent->flags & FL_TEAMSLAVE)) {
if (ent->moveinfo.sound_start)
gi.sound(ent, CHAN_NO_PHS_ADD | CHAN_VOICE, ent->moveinfo.sound_start, 1, ATTN_STATIC, 0);
}
ent->s.sound = ent->moveinfo.sound_middle;
ent->moveinfo.state = STATE_UP;
Move_Calc(ent, ent->moveinfo.start_origin, plat_hit_top);
plat2_spawn_danger_area(ent);
if (g_mover_debug->integer)
gi.Com_PrintFmt("Go up {}\n", *ent);
}
MOVEINFO_BLOCKED(plat_blocked) (gentity_t *self, gentity_t *other) -> void {
if (!(other->svflags & SVF_MONSTER) && (!other->client)) {
// give it a chance to go away on it's own terms (like gibs)
T_Damage(other, self, self, vec3_origin, other->s.origin, vec3_origin, 100000, 1, DAMAGE_NONE, MOD_CRUSH);
// if it's still there, nuke it
if (other && other->inuse && other->solid)
BecomeExplosion1(other);
return;
}
// gib dead things
if (other->health < 1)
T_Damage(other, self, self, vec3_origin, other->s.origin, vec3_origin, 100, 1, DAMAGE_NONE, MOD_CRUSH);
T_Damage(other, self, self, vec3_origin, other->s.origin, vec3_origin, self->dmg, 1, DAMAGE_NONE, MOD_CRUSH);
// [Paril-KEX] killed the thing, so don't switch directions
if (!other->inuse || !other->solid)
return;
if (g_mover_debug->integer)
gi.Com_PrintFmt("Blocked {} - speed:{} accel:{} decel:{}\n", *self, self->speed, self->accel, self->decel);
if (self->moveinfo.state == STATE_UP)
plat_go_down(self);
else if (self->moveinfo.state == STATE_DOWN)
plat_go_up(self);
}
constexpr spawnflags_t SPAWNFLAG_PLAT_LOW_TRIGGER = 1_spawnflag;
constexpr spawnflags_t SPAWNFLAG_PLAT_NO_MONSTER = 2_spawnflag;
static USE(Use_Plat) (gentity_t *ent, gentity_t *other, gentity_t *activator) -> void {
// if a monster is using us, then allow the activity when stopped.
if ((other->svflags & SVF_MONSTER) && !(ent->spawnflags & SPAWNFLAG_PLAT_NO_MONSTER)) {
if (ent->moveinfo.state == STATE_TOP)
plat_go_down(ent);
else if (ent->moveinfo.state == STATE_BOTTOM)
plat_go_up(ent);
return;
}
if (g_mover_debug->integer)
gi.Com_PrintFmt("Use {}\n", *ent);
if (ent->think)
return; // already down
plat_go_down(ent);
}
static TOUCH(Touch_Plat_Center) (gentity_t *ent, gentity_t *other, const trace_t &tr, bool other_touching_self) -> void {
if (!other->client)
return;
if (other->health <= 0)
return;
ent = ent->enemy; // now point at the plat, not the trigger
if (ent->moveinfo.state == STATE_BOTTOM)
plat_go_up(ent);
else if (ent->moveinfo.state == STATE_TOP)
ent->nextthink = level.time + 1_sec; // the player is still on the plat, so delay going down
if (g_mover_debug->integer)
gi.Com_PrintFmt("Touch center {} - speed:{} accel:{} decel:{}\n", *ent, ent->speed, ent->accel, ent->decel);
}
// plat2's change the trigger field
gentity_t *plat_spawn_inside_trigger(gentity_t *ent) {
gentity_t *trigger;
vec3_t tmin, tmax;
//
// middle trigger
//
trigger = G_Spawn();
trigger->touch = Touch_Plat_Center;
trigger->movetype = MOVETYPE_NONE;
trigger->solid = SOLID_TRIGGER;
trigger->enemy = ent;
tmin[0] = ent->mins[0] + 25;
tmin[1] = ent->mins[1] + 25;
tmin[2] = ent->mins[2];
tmax[0] = ent->maxs[0] - 25;
tmax[1] = ent->maxs[1] - 25;
tmax[2] = ent->maxs[2] + 8;
tmin[2] = tmax[2] - (ent->pos1[2] - ent->pos2[2] + st.lip);
if (ent->spawnflags.has(SPAWNFLAG_PLAT_LOW_TRIGGER))
tmax[2] = tmin[2] + 8;
if (tmax[0] - tmin[0] <= 0) {
tmin[0] = (ent->mins[0] + ent->maxs[0]) * 0.5f;
tmax[0] = tmin[0] + 1;
}
if (tmax[1] - tmin[1] <= 0) {
tmin[1] = (ent->mins[1] + ent->maxs[1]) * 0.5f;
tmax[1] = tmin[1] + 1;
}
trigger->mins = tmin;
trigger->maxs = tmax;
gi.linkentity(trigger);
return trigger; // PGM 11/17/97
}
/*QUAKED func_plat (0 .5 .8) ? PLAT_LOW_TRIGGER x x x x x x x NOT_EASY NOT_MEDIUM NOT_HARD NOT_DM NOT_COOP
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 triggered, when it will lower and become a normal plat.
"speed" overrides default 200.
"accel" overrides default 500
"lip" overrides default 8 pixel lip
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 SP_func_plat(gentity_t *ent) {
ent->s.angles = {};
ent->solid = SOLID_BSP;
ent->movetype = MOVETYPE_PUSH;
gi.setmodel(ent, ent->model);
ent->moveinfo.blocked = plat_blocked;
if (!ent->speed)
ent->speed = 20;
else
ent->speed *= 0.1f;
if (!ent->accel)
ent->accel = 5;
else
ent->accel *= 0.1f;
if (!ent->decel)
ent->decel = 5;
else
ent->decel *= 0.1f;
#if 0
if (g_mover_speed_scale->value != 1.0f) {
ent->speed = floor(ent->speed * g_mover_speed_scale->value);
ent->accel = floor(ent->accel * g_mover_speed_scale->value);
ent->decel = floor(ent->decel * g_mover_speed_scale->value);
}
#endif
if (g_mover_debug->integer)
gi.Com_PrintFmt("Spawning {} - speed:{} accel:{} decel:{}\n", *ent, ent->speed, ent->accel, ent->decel);
if (!ent->dmg)
ent->dmg = 2;
if (!st.lip)
st.lip = 8;
// pos1 is the top position, pos2 is the bottom
ent->pos1 = ent->s.origin;
ent->pos2 = ent->s.origin;
if (st.height)
ent->pos2[2] -= st.height;
else
ent->pos2[2] -= (ent->maxs[2] - ent->mins[2]) - st.lip;
ent->use = Use_Plat;
plat_spawn_inside_trigger(ent); // the "start moving" trigger
if (ent->targetname) {
ent->moveinfo.state = STATE_UP;
} else {
ent->s.origin = ent->pos2;
gi.linkentity(ent);
ent->moveinfo.state = STATE_BOTTOM;
}
ent->moveinfo.speed = ent->speed;
ent->moveinfo.accel = ent->accel;
ent->moveinfo.decel = ent->decel;
ent->moveinfo.wait = ent->wait;
ent->moveinfo.start_origin = ent->pos1;
ent->moveinfo.start_angles = ent->s.angles;
ent->moveinfo.end_origin = ent->pos2;
ent->moveinfo.end_angles = ent->s.angles;
G_SetMoveinfoSounds(ent, "plats/pt1_strt.wav", "plats/pt1_mid.wav", "plats/pt1_end.wav");
}
/*QUAKED func_plat2 (0 .5 .8) ? PLAT_LOW_TRIGGER PLAT2_TOGGLE PLAT2_TOP PLAT2_START_ACTIVE x BOX_LIFT x x NOT_EASY NOT_MEDIUM NOT_HARD NOT_DM NOT_COOP
speed default 150
PLAT_LOW_TRIGGER - creates a short trigger field at the bottom
PLAT2_TOGGLE - plat will not return to default position.
PLAT2_TOP - plat's default position will the the top.
PLAT2_START_ACTIVE - plat will trigger it's targets each time it hits top
BOX_LIFT - this indicates that the lift is a box, rather than just a platform
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.
"speed" overrides default 200.
"accel" overrides default 500
"lip" no default
If the "height" key is set, that will determine the amount the plat moves, instead of being implicitly determoveinfoned by the model's height.
*/
constexpr spawnflags_t SPAWNFLAGS_PLAT2_TOGGLE = 2_spawnflag;
constexpr spawnflags_t SPAWNFLAGS_PLAT2_TOP = 4_spawnflag;
constexpr spawnflags_t SPAWNFLAGS_PLAT2_START_ACTIVE = 8_spawnflag;
constexpr spawnflags_t SPAWNFLAGS_PLAT2_BOX_LIFT = 32_spawnflag;
void plat2_go_down(gentity_t *ent);
void plat2_go_up(gentity_t *ent);
void plat2_spawn_danger_area(gentity_t *ent) {
vec3_t mins, maxs;
mins = ent->mins;
maxs = ent->maxs;
maxs[2] = ent->mins[2] + 64;
SpawnBadArea(mins, maxs, 0_ms, ent);
}
void plat2_kill_danger_area(gentity_t *ent) {
gentity_t *t;
t = nullptr;
while ((t = G_FindByString<&gentity_t::classname>(t, "bad_area"))) {
if (t->owner == ent)
G_FreeEntity(t);
}
}
MOVEINFO_ENDFUNC(plat2_hit_top) (gentity_t *ent) -> void {
if (!(ent->flags & FL_TEAMSLAVE)) {
if (ent->moveinfo.sound_end)
gi.sound(ent, CHAN_NO_PHS_ADD | CHAN_VOICE, ent->moveinfo.sound_end, 1, ATTN_STATIC, 0);
}
ent->s.sound = 0;
ent->moveinfo.state = STATE_TOP;
if (ent->plat2flags & PLAT2_CALLED) {
ent->plat2flags = PLAT2_WAITING;
if (!ent->spawnflags.has(SPAWNFLAGS_PLAT2_TOGGLE)) {
ent->think = plat2_go_down;
ent->nextthink = level.time + 5_sec;
}
if (deathmatch->integer)
ent->last_move_time = level.time - 1_sec;
else
ent->last_move_time = level.time - 2_sec;
} else if (!(ent->spawnflags & SPAWNFLAGS_PLAT2_TOP) && !ent->spawnflags.has(SPAWNFLAGS_PLAT2_TOGGLE)) {
ent->plat2flags = PLAT2_NONE;
ent->think = plat2_go_down;
ent->nextthink = level.time + 2_sec;
ent->last_move_time = level.time;
} else {
ent->plat2flags = PLAT2_NONE;
ent->last_move_time = level.time;
}
G_UseTargets(ent, ent);
}
MOVEINFO_ENDFUNC(plat2_hit_bottom) (gentity_t *ent) -> void {
if (!(ent->flags & FL_TEAMSLAVE)) {
if (ent->moveinfo.sound_end)
gi.sound(ent, CHAN_NO_PHS_ADD | CHAN_VOICE, ent->moveinfo.sound_end, 1, ATTN_STATIC, 0);
}
ent->s.sound = 0;
ent->moveinfo.state = STATE_BOTTOM;
if (ent->plat2flags & PLAT2_CALLED) {
ent->plat2flags = PLAT2_WAITING;
if (!(ent->spawnflags & SPAWNFLAGS_PLAT2_TOGGLE)) {
ent->think = plat2_go_up;
ent->nextthink = level.time + 5_sec;
}
if (deathmatch->integer)
ent->last_move_time = level.time - 1_sec;
else
ent->last_move_time = level.time - 2_sec;
} else if (ent->spawnflags.has(SPAWNFLAGS_PLAT2_TOP) && !ent->spawnflags.has(SPAWNFLAGS_PLAT2_TOGGLE)) {
ent->plat2flags = PLAT2_NONE;
ent->think = plat2_go_up;
ent->nextthink = level.time + 2_sec;
ent->last_move_time = level.time;
} else {
ent->plat2flags = PLAT2_NONE;
ent->last_move_time = level.time;
}
plat2_kill_danger_area(ent);
G_UseTargets(ent, ent);
}
THINK(plat2_go_down) (gentity_t *ent) -> void {
if (!(ent->flags & FL_TEAMSLAVE)) {
if (ent->moveinfo.sound_start)
gi.sound(ent, CHAN_NO_PHS_ADD | CHAN_VOICE, ent->moveinfo.sound_start, 1, ATTN_STATIC, 0);
}
ent->s.sound = ent->moveinfo.sound_middle;
ent->moveinfo.state = STATE_DOWN;
ent->plat2flags |= PLAT2_MOVING;
Move_Calc(ent, ent->moveinfo.end_origin, plat2_hit_bottom);
}
THINK(plat2_go_up) (gentity_t *ent) -> void {
if (!(ent->flags & FL_TEAMSLAVE)) {
if (ent->moveinfo.sound_start)
gi.sound(ent, CHAN_NO_PHS_ADD | CHAN_VOICE, ent->moveinfo.sound_start, 1, ATTN_STATIC, 0);
}
ent->s.sound = ent->moveinfo.sound_middle;
ent->moveinfo.state = STATE_UP;
ent->plat2flags |= PLAT2_MOVING;
plat2_spawn_danger_area(ent);
Move_Calc(ent, ent->moveinfo.start_origin, plat2_hit_top);
}
static void plat2_operate(gentity_t *ent, gentity_t *other) {
int otherState;
gtime_t pauseTime;
float platCenter;
gentity_t *trigger;
trigger = ent;
ent = ent->enemy; // now point at the plat, not the trigger
if (ent->plat2flags & PLAT2_MOVING)
return;
if ((ent->last_move_time + 2_sec) > level.time)
return;
platCenter = (trigger->absmin[2] + trigger->absmax[2]) / 2;
if (ent->moveinfo.state == STATE_TOP) {
otherState = STATE_TOP;
if (ent->spawnflags.has(SPAWNFLAGS_PLAT2_BOX_LIFT)) {
if (platCenter > other->s.origin[2])
otherState = STATE_BOTTOM;
} else {
if (trigger->absmax[2] > other->s.origin[2])
otherState = STATE_BOTTOM;
}
} else {
otherState = STATE_BOTTOM;
if (other->s.origin[2] > platCenter)
otherState = STATE_TOP;
}
ent->plat2flags = PLAT2_MOVING;
if (deathmatch->integer)
pauseTime = 300_ms;
else
pauseTime = 500_ms;
if (ent->moveinfo.state != otherState) {
ent->plat2flags |= PLAT2_CALLED;
pauseTime = 100_ms;
}
ent->last_move_time = level.time;
if (ent->moveinfo.state == STATE_BOTTOM) {
ent->think = plat2_go_up;
ent->nextthink = level.time + pauseTime;
} else {
ent->think = plat2_go_down;
ent->nextthink = level.time + pauseTime;
}
}
static TOUCH(Touch_Plat_Center2) (gentity_t *ent, gentity_t *other, const trace_t &tr, bool other_touching_self) -> void {
// this requires monsters to actively trigger plats, not just step on them.
// FIXME - commented out for E3
// if (!other->client)
// return;
if (other->health <= 0)
return;
// PMM - don't let non-monsters activate plat2s
if ((!(other->svflags & SVF_MONSTER)) && (!other->client))
return;
plat2_operate(ent, other);
}
MOVEINFO_BLOCKED(plat2_blocked) (gentity_t *self, gentity_t *other) -> void {
if (!(other->svflags & SVF_MONSTER) && (!other->client)) {
// give it a chance to go away on it's own terms (like gibs)
T_Damage(other, self, self, vec3_origin, other->s.origin, vec3_origin, 100000, 1, DAMAGE_NONE, MOD_CRUSH);
// if it's still there, nuke it
if (other && other->inuse && other->solid)
BecomeExplosion1(other);
return;
}
// gib dead things
if (other->health < 1) {
T_Damage(other, self, self, vec3_origin, other->s.origin, vec3_origin, 100, 1, DAMAGE_NONE, MOD_CRUSH);
}
T_Damage(other, self, self, vec3_origin, other->s.origin, vec3_origin, self->dmg, 1, DAMAGE_NONE, MOD_CRUSH);
// [Paril-KEX] killed, so don't change direction
if (!other->inuse || !other->solid)
return;
if (self->moveinfo.state == STATE_UP)
plat2_go_down(self);
else if (self->moveinfo.state == STATE_DOWN)
plat2_go_up(self);
}
static USE(Use_Plat2) (gentity_t *ent, gentity_t *other, gentity_t *activator) -> void {
gentity_t *trigger;
if (ent->moveinfo.state > STATE_BOTTOM)
return;
// [Paril-KEX] disabled this; causes confusing situations
//if ((ent->last_move_time + 2_sec) > level.time)
// return;
uint32_t i;
for (i = 1, trigger = g_entities + 1; i < globals.num_entities; i++, trigger++) {
if (!trigger->inuse)
continue;
if (trigger->touch == Touch_Plat_Center2) {
if (trigger->enemy == ent) {
// Touch_Plat_Center2 (trigger, activator, nullptr, nullptr);
plat2_operate(trigger, activator);
return;
}
}
}
}
static USE(plat2_activate) (gentity_t *ent, gentity_t *other, gentity_t *activator) -> void {
gentity_t *trigger;
// if(ent->targetname)
// ent->targetname[0] = 0;
ent->use = Use_Plat2;
trigger = plat_spawn_inside_trigger(ent); // the "start moving" trigger
trigger->maxs[0] += 10;
trigger->maxs[1] += 10;
trigger->mins[0] -= 10;
trigger->mins[1] -= 10;
gi.linkentity(trigger);
trigger->touch = Touch_Plat_Center2; // Override trigger touch function
plat2_go_down(ent);
}
void SP_func_plat2(gentity_t *ent) {
gentity_t *trigger;
ent->s.angles = {};
ent->solid = SOLID_BSP;
ent->movetype = MOVETYPE_PUSH;
gi.setmodel(ent, ent->model);
ent->moveinfo.blocked = plat2_blocked;
if (!ent->speed)