-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathsv_phys.c
2755 lines (2341 loc) · 76.7 KB
/
sv_phys.c
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-1997 Id Software, Inc.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
// sv_phys.c
#include "quakedef.h"
#if !defined(CLIENTONLY) || defined(CSQC_DAT)
#include "pr_common.h"
/*
pushmove objects do not obey gravity, and do not interact with each other or trigger fields, but block normal movement and push normal objects when they move.
onground is set for toss objects when they come to a complete rest. it is set for steping or walking objects
doors, plats, etc are SOLID_BSP, and MOVETYPE_PUSH
bonus items are SOLID_TRIGGER touch, and MOVETYPE_TOSS
corpses are SOLID_NOT and MOVETYPE_TOSS
crates are SOLID_BBOX and MOVETYPE_TOSS
walking monsters are SOLID_SLIDEBOX and MOVETYPE_STEP
flying/floating monsters are SOLID_SLIDEBOX and MOVETYPE_FLY
solid_edge items only clip against bsp models.
*/
cvar_t sv_maxvelocity = CVAR("sv_maxvelocity","10000");
cvar_t sv_gravity = CVAR( "sv_gravity", "800");
cvar_t sv_stopspeed = CVAR( "sv_stopspeed", "100");
cvar_t sv_maxspeed = CVAR( "sv_maxspeed", "320");
cvar_t sv_spectatormaxspeed = CVAR( "sv_spectatormaxspeed", "500");
cvar_t sv_accelerate = CVAR( "sv_accelerate", "10");
cvar_t sv_airaccelerate = CVAR( "sv_airaccelerate", "0.7");
cvar_t sv_wateraccelerate = CVAR( "sv_wateraccelerate", "10");
cvar_t sv_friction = CVAR( "sv_friction", "4");
cvar_t sv_waterfriction = CVAR( "sv_waterfriction", "4");
cvar_t sv_wallfriction = CVARD( "sv_wallfriction", "1", "Additional friction when running into walls");
cvar_t sv_gameplayfix_noairborncorpse = CVAR( "sv_gameplayfix_noairborncorpse", "0");
cvar_t sv_gameplayfix_multiplethinks = CVARAD("sv_gameplayfix_multiplethinks", "1", /*dp*/"sv_gameplayfix_multiplethinksperframe", "Enables multiple thinks per entity per frame so small nextthink times are accurate. QuakeWorld mods expect a value of 1, while NQ expects 0.");
cvar_t sv_gameplayfix_stepdown = CVARD( "sv_gameplayfix_stepdown", "0", "Attempt to step down steps, instead of only up them. Affects non-predicted movetype_walk.");
cvar_t sv_gameplayfix_bouncedownslopes = CVARD( "sv_gameplayfix_grenadebouncedownslopes", "0", "MOVETYPE_BOUNCE speeds are calculated relative to the impacted surface, instead of the vertical, reducing the chance of grenades just sitting there on slopes.");
cvar_t sv_gameplayfix_trappedwithin = CVARD( "sv_gameplayfix_trappedwithin", "0", "Blocks further entity movement when an entity is already inside another entity. This ensures that bsp precision issues cannot allow the entity to completely pass through eg the world.");
//cvar_t sv_gameplayfix_radialmaxvelocity = CVARD( "sv_gameplayfix_radialmaxvelocity", "0", "Applies maxvelocity radially instead of axially.");
#if !defined(CLIENTONLY) && defined(NQPROT) && defined(HAVE_LEGACY)
cvar_t sv_gameplayfix_spawnbeforethinks = CVARD( "sv_gameplayfix_spawnbeforethinks", "0", "Fixes an issue where player thinks (including Pre+Post) can be called before PutClientInServer. Unfortunately at least one mod depends upon PreThink being called first in order to correctly determine spawn positions.");
#endif
cvar_t dpcompat_noretouchground = CVARD( "dpcompat_noretouchground", "0", "Prevents entities that are already standing on an entity from touching the same entity again.");
cvar_t sv_sound_watersplash = CVAR( "sv_sound_watersplash", "misc/h2ohit1.wav");
cvar_t sv_sound_land = CVAR( "sv_sound_land", "demon/dland2.wav");
cvar_t sv_stepheight = CVARAFD("pm_stepheight", "", /*dp*/"sv_stepheight", CVAR_SERVERINFO, "If empty, the value "STRINGIFY(PM_DEFAULTSTEPHEIGHT)" will be used instead. This is the size of the step you can step up or down.");
cvar_t pm_ktjump = CVARF("pm_ktjump", "", CVAR_SERVERINFO);
cvar_t pm_bunnyspeedcap = CVARFD("pm_bunnyspeedcap", "", CVAR_SERVERINFO, "0 or 1, ish. If the player is traveling faster than this speed while turning, their velocity will be gracefully reduced to match their current maxspeed. You can still rocket-jump to gain high velocity, but turning will reduce your speed back to the max. This can be used to disable bunny hopping.");
cvar_t pm_watersinkspeed = CVARFD("pm_watersinkspeed", "", CVAR_SERVERINFO, "This is the speed that players will sink at while inactive in water. Empty means 60.");
cvar_t pm_flyfriction = CVARFD("pm_flyfriction", "", CVAR_SERVERINFO, "Amount of friction that applies in fly or 6dof mode. Empty means 4.");
cvar_t pm_slidefix = CVARFD("pm_slidefix", "", CVAR_SERVERINFO, "Fixes an issue when walking down slopes (ie: so they act more like slopes and not a series of steps)");
cvar_t pm_slidyslopes = CVARFD("pm_slidyslopes", "", CVAR_SERVERINFO, "Replicates NQ behaviour, where players will slowly slide down ramps");
cvar_t pm_autobunny = CVARFD("pm_autobunny", "", CVAR_SERVERINFO, "Players will continue jumping without needing to release the jump button.");
cvar_t pm_airstep = CVARAFD("pm_airstep", "", /*dp*/"sv_jumpstep", CVAR_SERVERINFO, "Allows players to step up while jumping. This makes stairs more graceful but also increases potential jump heights.");
cvar_t pm_pground = CVARFD("pm_pground", "", CVAR_SERVERINFO, "Use persisten onground state instead of recalculating every frame."CON_WARNING"Do NOT use with nq mods, as most nq mods will interfere with onground state, resulting in glitches.");
cvar_t pm_stepdown = CVARFD("pm_stepdown", "", CVAR_SERVERINFO, "Causes physics to stick to the ground, instead of constantly losing traction whiloe going down steps.");
cvar_t pm_walljump = CVARFD("pm_walljump", "", CVAR_SERVERINFO, "Allows the player to bounce off walls while arborne.");
cvar_t pm_edgefriction = CVARAFD("pm_edgefriction", "", /*nq*/"edgefriction", CVAR_SERVERINFO, "Default value of 2");
#define cvargroup_serverphysics "server physics variables"
void WPhys_Init(void)
{
Cvar_Register (&sv_maxvelocity, cvargroup_serverphysics);
Cvar_Register (&sv_gravity, cvargroup_serverphysics);
Cvar_Register (&sv_stopspeed, cvargroup_serverphysics);
Cvar_Register (&sv_maxspeed, cvargroup_serverphysics);
Cvar_Register (&sv_spectatormaxspeed, cvargroup_serverphysics);
Cvar_Register (&sv_accelerate, cvargroup_serverphysics);
Cvar_Register (&sv_airaccelerate, cvargroup_serverphysics);
Cvar_Register (&sv_wateraccelerate, cvargroup_serverphysics);
Cvar_Register (&sv_friction, cvargroup_serverphysics);
Cvar_Register (&sv_waterfriction, cvargroup_serverphysics);
Cvar_Register (&sv_wallfriction, cvargroup_serverphysics);
Cvar_Register (&sv_sound_watersplash, cvargroup_serverphysics);
Cvar_Register (&sv_sound_land, cvargroup_serverphysics);
Cvar_Register (&sv_stepheight, cvargroup_serverphysics);
Cvar_Register (&sv_gameplayfix_noairborncorpse, cvargroup_serverphysics);
Cvar_Register (&sv_gameplayfix_multiplethinks, cvargroup_serverphysics);
Cvar_Register (&sv_gameplayfix_stepdown, cvargroup_serverphysics);
Cvar_Register (&sv_gameplayfix_bouncedownslopes, cvargroup_serverphysics);
Cvar_Register (&sv_gameplayfix_trappedwithin, cvargroup_serverphysics);
Cvar_Register (&dpcompat_noretouchground, cvargroup_serverphysics);
#if !defined(CLIENTONLY) && defined(NQPROT) && defined(HAVE_LEGACY)
Cvar_Register (&sv_gameplayfix_spawnbeforethinks, cvargroup_serverphysics);
#endif
}
#define MOVE_EPSILON 0.01
static void WPhys_Physics_Toss (world_t *w, wedict_t *ent);
/*
================
SV_CheckAllEnts
================
static void SV_CheckAllEnts (void)
{
int e;
edict_t *check;
// see if any solid entities are inside the final position
for (e=1 ; e<sv.world.num_edicts ; e++)
{
check = EDICT_NUM(svprogfuncs, e);
if (check->isfree)
continue;
if (check->v->movetype == MOVETYPE_PUSH
|| check->v->movetype == MOVETYPE_NONE
|| check->v->movetype == MOVETYPE_FOLLOW
|| check->v->movetype == MOVETYPE_NOCLIP
|| check->v->movetype == MOVETYPE_ANGLENOCLIP)
continue;
if (World_TestEntityPosition (&sv.world, (wedict_t*)check))
Con_Printf ("entity in invalid position\n");
}
}
*/
/*
================
SV_CheckVelocity
================
*/
void WPhys_CheckVelocity (world_t *w, wedict_t *ent)
{
int i;
#ifdef HAVE_SERVER
extern cvar_t sv_nqplayerphysics;
if (sv_nqplayerphysics.ival)
{ //bound axially (like vanilla)
for (i=0 ; i<3 ; i++)
{
if (IS_NAN(ent->v->velocity[i]))
{
Con_DPrintf ("Got a NaN velocity on entity %i (%s)\n", ent->entnum, PR_GetString(w->progs, ent->v->classname));
ent->v->velocity[i] = 0;
}
if (IS_NAN(ent->v->origin[i]))
{
Con_Printf ("Got a NaN origin on entity %i (%s)\n", ent->entnum, PR_GetString(w->progs, ent->v->classname));
ent->v->origin[i] = 0;
}
if (ent->v->velocity[i] > sv_maxvelocity.value)
ent->v->velocity[i] = sv_maxvelocity.value;
else if (ent->v->velocity[i] < -sv_maxvelocity.value)
ent->v->velocity[i] = -sv_maxvelocity.value;
}
}
else
#endif
{ //bound radially (for sanity)
for (i=0 ; i<3 ; i++)
{
if (IS_NAN(ent->v->velocity[i]))
{
Con_DPrintf ("Got a NaN velocity on entity %i (%s)\n", ent->entnum, PR_GetString(w->progs, ent->v->classname));
ent->v->velocity[i] = 0;
}
if (IS_NAN(ent->v->origin[i]))
{
Con_Printf ("Got a NaN origin on entity %i (%s)\n", ent->entnum, PR_GetString(w->progs, ent->v->classname));
ent->v->origin[i] = 0;
}
}
if (Length(ent->v->velocity) > sv_maxvelocity.value)
{
// Con_DPrintf("Slowing %s\n", PR_GetString(w->progs, ent->v->classname));
VectorScale (ent->v->velocity, sv_maxvelocity.value/Length(ent->v->velocity), ent->v->velocity);
}
}
}
/*
=============
SV_RunThink
Runs thinking code if time. There is some play in the exact time the think
function will be called, because it is called before any movement is done
in a frame. Not used for pushmove objects, because they must be exact.
Returns false if the entity removed itself.
=============
*/
qboolean WPhys_RunThink (world_t *w, wedict_t *ent)
{
float thinktime;
if (!sv_gameplayfix_multiplethinks.ival) //try and imitate nq as closeley as possible
{
thinktime = ent->v->nextthink;
if (thinktime <= 0 || thinktime > w->physicstime + host_frametime)
return true;
if (thinktime < w->physicstime)
thinktime = w->physicstime; // don't let things stay in the past.
// it is possible to start that way
// by a trigger with a local time.
ent->v->nextthink = 0;
*w->g.time = thinktime;
w->Event_Think(w, ent);
return !ED_ISFREE(ent);
}
do
{
thinktime = ent->v->nextthink;
if (thinktime <= 0)
return true;
if (thinktime > w->physicstime + host_frametime)
return true;
if (thinktime < w->physicstime)
thinktime = w->physicstime; // don't let things stay in the past.
// it is possible to start that way
// by a trigger with a local time.
ent->v->nextthink = 0;
*w->g.time = thinktime;
w->Event_Think(w, ent);
if (ED_ISFREE(ent))
return false;
if (ent->v->nextthink <= thinktime) //hmm... infinate loop was possible here.. Quite a few non-QW mods do this.
return true;
} while (1);
return true;
}
/*
==================
SV_Impact
Two entities have touched, so run their touch functions
==================
*/
static void WPhys_Impact (world_t *w, wedict_t *e1, trace_t *trace)
{
wedict_t *e2 = trace->ent;
*w->g.time = w->physicstime;
if (e1->v->touch && e1->v->solid != SOLID_NOT)
{
w->Event_Touch(w, e1, e2, trace);
}
if (e2->v->touch && e2->v->solid != SOLID_NOT)
{
w->Event_Touch(w, e2, e1, trace);
}
}
/*
==================
ClipVelocity
Slide off of the impacting object
==================
*/
#define STOP_EPSILON 0.1
//courtesy of darkplaces, it's just more efficient.
static void ClipVelocity (vec3_t in, vec3_t normal, vec3_t out, float overbounce)
{
int i;
float backoff;
backoff = -DotProduct (in, normal) * overbounce;
VectorMA(in, backoff, normal, out);
for (i = 0;i < 3;i++)
if (out[i] > -STOP_EPSILON && out[i] < STOP_EPSILON)
out[i] = 0;
}
static void WPhys_PortalTransform(world_t *w, wedict_t *ent, wedict_t *portal, vec3_t org, vec3_t move)
{
int oself = *w->g.self;
void *pr_globals = PR_globals(w->progs, PR_CURRENT);
*w->g.self = EDICT_TO_PROG(w->progs, portal);
//transform origin+velocity etc
VectorCopy(org, G_VECTOR(OFS_PARM0));
VectorCopy(ent->v->angles, G_VECTOR(OFS_PARM1));
VectorCopy(ent->v->velocity, w->g.v_forward);
VectorCopy(move, w->g.v_right);
VectorCopy(ent->xv->gravitydir, w->g.v_up);
if (!DotProduct(w->g.v_up, w->g.v_up))
w->g.v_up[2] = -1;
PR_ExecuteProgram (w->progs, portal->xv->camera_transform);
VectorCopy(G_VECTOR(OFS_RETURN), org);
VectorCopy(w->g.v_forward, ent->v->velocity);
VectorCopy(w->g.v_right, move);
// VectorCopy(w->g.v_up, ent->xv->gravitydir);
//monsters get their gravitydir set if it isn't already, to ensure that they still work (angle issues).
if ((int)ent->v->flags & FL_MONSTER)
if (!ent->xv->gravitydir[0] && !ent->xv->gravitydir[1] && !ent->xv->gravitydir[2])
ent->xv->gravitydir[2] = -1;
//transform the angles too
VectorCopy(org, G_VECTOR(OFS_PARM0));
#ifndef CLIENTONLY
if (w == &sv.world && ent->entnum <= svs.allocated_client_slots)
{
VectorCopy(ent->v->v_angle, ent->v->angles);
}
else
#endif
ent->v->angles[0] *= r_meshpitch.value;
VectorCopy(ent->v->angles, G_VECTOR(OFS_PARM1));
AngleVectors(ent->v->angles, w->g.v_forward, w->g.v_right, w->g.v_up);
PR_ExecuteProgram (w->progs, portal->xv->camera_transform);
VectorAngles(w->g.v_forward, w->g.v_up, ent->v->angles, true);
#ifndef CLIENTONLY
if (ent->entnum > 0 && ent->entnum <= svs.allocated_client_slots)
{
client_t *cl = &svs.clients[ent->entnum-1];
ent->v->angles[0] *= r_meshpitch.value;
VectorCopy(ent->v->angles, ent->v->v_angle);
ent->v->angles[0] *= r_meshpitch.value;
SV_SendFixAngle(cl, NULL, FIXANGLE_AUTO, true);
}
#endif
/*
avelocity is horribly dependant upon eular angles. trying to treat it as a matrix is folly.
if (DotProduct(ent->v->avelocity, ent->v->avelocity))
{
ent->v->avelocity[0] *= r_meshpitch.value;
AngleVectors(ent->v->avelocity, w->g.v_forward, w->g.v_right, w->g.v_up);
PR_ExecuteProgram (w->progs, portal->xv->camera_transform);
VectorAngles(w->g.v_forward, w->g.v_up, ent->v->avelocity);
ent->v->avelocity[0] *= r_meshpitch.value;
}
*/
*w->g.self = oself;
}
/*
============
SV_FlyMove
The basic solid body movement clip that slides along multiple planes
Returns the clipflags if the velocity was modified (hit something solid)
1 = floor
2 = wall / step
4 = dead stop
If steptrace is not NULL, the trace of any vertical wall hit will be stored
============
*/
#define MAX_CLIP_PLANES 5
static int WPhys_FlyMove (world_t *w, wedict_t *ent, const vec3_t gravitydir, float time, trace_t *steptrace)
{
int bumpcount, numbumps;
vec3_t dir;
float d;
int numplanes;
vec3_t planes[MAX_CLIP_PLANES];
vec3_t primal_velocity, original_velocity, new_velocity;
int i, j;
trace_t trace;
vec3_t end;
float time_left;
int blocked;
wedict_t *impact;
vec3_t diff;
numbumps = 4;
blocked = 0;
VectorCopy (ent->v->velocity, original_velocity);
VectorCopy (ent->v->velocity, primal_velocity);
numplanes = 0;
time_left = time;
for (bumpcount=0 ; bumpcount<numbumps ; bumpcount++)
{
for (i=0 ; i<3 ; i++)
end[i] = ent->v->origin[i] + time_left * ent->v->velocity[i];
trace = World_Move (w, ent->v->origin, ent->v->mins, ent->v->maxs, end, MOVE_NORMAL, (wedict_t*)ent);
impact = trace.ent;
if (impact && impact->v->solid == SOLID_PORTAL)
{
vec3_t move;
vec3_t from;
VectorCopy(trace.endpos, from); //just in case
VectorSubtract(end, trace.endpos, move);
WPhys_PortalTransform(w, ent, impact, from, move);
VectorAdd(from, move, end);
//if we follow the portal, then we basically need to restart from the other side.
time_left -= time_left * trace.fraction;
VectorCopy (ent->v->velocity, primal_velocity);
VectorCopy (ent->v->velocity, original_velocity);
numplanes = 0;
trace = World_Move (w, from, ent->v->mins, ent->v->maxs, end, MOVE_NORMAL, (wedict_t*)ent);
impact = trace.ent;
}
if (trace.allsolid)//should be (trace.startsolid), but that breaks compat. *sigh*
{ // entity is trapped in another solid
VectorClear (ent->v->velocity);
return 3;
}
if (trace.fraction > 0)
{ // actually covered some distance
VectorCopy (trace.endpos, ent->v->origin);
VectorCopy (ent->v->velocity, original_velocity);
numplanes = 0;
}
if (trace.fraction == 1)
break; // moved the entire distance
if (!trace.ent)
Host_Error ("SV_FlyMove: !trace.ent");
if (dpcompat_noretouchground.ival)
{ //note: also sets onground AFTER the touch event.
if (!((int)ent->v->flags&FL_ONGROUND) || ent->v->groundentity!=EDICT_TO_PROG(w->progs, trace.ent))
WPhys_Impact (w, ent, &trace);
}
if (-DotProduct(gravitydir, trace.plane.normal) > 0.7)
{
blocked |= 1; // floor
if (((wedict_t *)trace.ent)->v->solid == SOLID_BSP || dpcompat_noretouchground.ival)
{
ent->v->flags = (int)ent->v->flags | FL_ONGROUND;
ent->v->groundentity = EDICT_TO_PROG(w->progs, trace.ent);
}
}
if (!DotProduct(gravitydir, trace.plane.normal))
{
blocked |= 2; // step
if (steptrace)
*steptrace = trace; // save for player extrafriction
}
//
// run the impact function
//
if (!dpcompat_noretouchground.ival)
WPhys_Impact (w, ent, &trace);
if (ED_ISFREE(ent))
break; // removed by the impact function
time_left -= time_left * trace.fraction;
// cliped to another plane
if (numplanes >= MAX_CLIP_PLANES)
{ // this shouldn't really happen
VectorClear (ent->v->velocity);
if (steptrace)
*steptrace = trace; // save for player extrafriction
return 3;
}
if (0)
{
ClipVelocity(ent->v->velocity, trace.plane.normal, ent->v->velocity, 1);
break;
}
else
{
if (numplanes)
{
VectorSubtract(planes[0], trace.plane.normal, diff);
if (Length(diff) < 0.01)
continue; //hit this plane already
}
VectorCopy (trace.plane.normal, planes[numplanes]);
numplanes++;
//
// modify original_velocity so it parallels all of the clip planes
//
for (i=0 ; i<numplanes ; i++)
{
ClipVelocity (original_velocity, planes[i], new_velocity, 1);
for (j=0 ; j<numplanes ; j++)
if (j != i)
{
if (DotProduct (new_velocity, planes[j]) < 0)
break; // not ok
}
if (j == numplanes)
break;
}
if (i != numplanes)
{ // go along this plane
// Con_Printf ("%5.1f %5.1f %5.1f ",ent->v->velocity[0], ent->v->velocity[1], ent->v->velocity[2]);
VectorCopy (new_velocity, ent->v->velocity);
// Con_Printf ("%5.1f %5.1f %5.1f\n",ent->v->velocity[0], ent->v->velocity[1], ent->v->velocity[2]);
}
else
{ // go along the crease
if (numplanes != 2)
{
// Con_Printf ("clip velocity, numplanes == %i\n",numplanes);
// Con_Printf ("%5.1f %5.1f %5.1f ",ent->v->velocity[0], ent->v->velocity[1], ent->v->velocity[2]);
VectorClear (ent->v->velocity);
// Con_Printf ("%5.1f %5.1f %5.1f\n",ent->v->velocity[0], ent->v->velocity[1], ent->v->velocity[2]);
return 7;
}
// Con_Printf ("%5.1f %5.1f %5.1f ",ent->v->velocity[0], ent->v->velocity[1], ent->v->velocity[2]);
CrossProduct (planes[0], planes[1], dir);
VectorNormalize(dir); //fixes slow falling in corners
d = DotProduct (dir, ent->v->velocity);
VectorScale (dir, d, ent->v->velocity);
// Con_Printf ("%5.1f %5.1f %5.1f\n",ent->v->velocity[0], ent->v->velocity[1], ent->v->velocity[2]);
}
}
//
// if original velocity is against the original velocity, stop dead
// to avoid tiny occilations in sloping corners
//
if (DotProduct (ent->v->velocity, primal_velocity) <= 0)
{
VectorClear (ent->v->velocity);
return blocked;
}
}
return blocked;
}
/*
============
SV_AddGravity
============
*/
static void WPhys_AddGravity (world_t *w, wedict_t *ent, const float *gravitydir)
{
float scale = ent->xv->gravity;
if (!scale)
scale = (ent->v->movetype == MOVETYPE_BOUNCEMISSILE)?0.5:1.0;
VectorMA(ent->v->velocity, scale * movevars.gravity * host_frametime, gravitydir, ent->v->velocity);
}
/*
===============================================================================
PUSHMOVE
===============================================================================
*/
/*
============
SV_PushEntity
Does not change the entities velocity at all
============
*/
static trace_t WPhys_PushEntity (world_t *w, wedict_t *ent, vec3_t push, unsigned int traceflags)
{
trace_t trace;
vec3_t end;
wedict_t *impact;
VectorAdd (ent->v->origin, push, end);
if ((int)ent->v->flags&FLQW_LAGGEDMOVE)
traceflags |= MOVE_LAGGED;
if (ent->v->movetype == MOVETYPE_FLYMISSILE)
traceflags |= MOVE_MISSILE;
else if (ent->v->solid == SOLID_TRIGGER || ent->v->solid == SOLID_NOT)
// only clip against bmodels
traceflags |= MOVE_NOMONSTERS;
else
traceflags |= MOVE_NORMAL;
trace = World_Move (w, ent->v->origin, ent->v->mins, ent->v->maxs, end, traceflags, (wedict_t*)ent);
impact = trace.ent;
if (impact && impact->v->solid == SOLID_PORTAL)
{
vec3_t move;
vec3_t from;
float firstfrac = trace.fraction;
VectorCopy(trace.endpos, from); //just in case
VectorSubtract(end, trace.endpos, move);
WPhys_PortalTransform(w, ent, impact, from, move);
VectorAdd(from, move, end);
trace = World_Move (w, from, ent->v->mins, ent->v->maxs, end, traceflags, (wedict_t*)ent);
trace.fraction = firstfrac + (1-firstfrac)*trace.fraction;
}
/*hexen2's movetype_swim does not allow swimming entities to move out of water. this implementation is quite hacky, but matches hexen2 well enough*/
if (ent->v->movetype == MOVETYPE_H2SWIM)
{
if (!(w->worldmodel->funcs.PointContents(w->worldmodel, NULL, trace.endpos) & (FTECONTENTS_WATER|FTECONTENTS_SLIME|FTECONTENTS_LAVA)))
{
VectorCopy(ent->v->origin, trace.endpos);
trace.fraction = 0;
trace.ent = w->edicts;
}
}
// if (trace.ent)
// VectorMA(trace.endpos, sv_impactpush.value, trace.plane.normal, ent->v->origin);
// else
VectorCopy (trace.endpos, ent->v->origin);
World_LinkEdict (w, ent, true);
if (trace.ent)
WPhys_Impact (w, ent, &trace);
return trace;
}
typedef struct
{
wedict_t *ent;
vec3_t origin;
vec3_t angles;
} pushed_t;
static pushed_t pushed[1024], *pushed_p;
/*
============
SV_Push
Objects need to be moved back on a failed push,
otherwise riders would continue to slide.
============
*/
static qboolean WPhys_PushAngles (world_t *w, wedict_t *pusher, vec3_t move, vec3_t amove)
{
int i, e;
wedict_t *check, *block;
vec3_t mins, maxs;
//float oldsolid;
pushed_t *p;
vec3_t org, org2, move2, forward, right, up;
short yawchange;
yawchange = (amove[PITCH]||amove[ROLL])?0:ANGLE2SHORT(amove[YAW]);
pushed_p = pushed;
// find the bounding box
for (i=0 ; i<3 ; i++)
{
mins[i] = pusher->v->absmin[i] + move[i];
maxs[i] = pusher->v->absmax[i] + move[i];
}
// we need this for pushing things later
VectorNegate (amove, org);
AngleVectors (org, forward, right, up);
// save the pusher's original position
pushed_p->ent = pusher;
VectorCopy (pusher->v->origin, pushed_p->origin);
VectorCopy (pusher->v->angles, pushed_p->angles);
pushed_p++;
// move the pusher to it's final position
VectorAdd (pusher->v->origin, move, pusher->v->origin);
VectorAdd (pusher->v->angles, amove, pusher->v->angles);
World_LinkEdict (w, pusher, false);
// see if any solid entities are inside the final position
if (pusher->v->movetype != MOVETYPE_H2PUSHPULL)
for (e = 1; e < w->num_edicts; e++)
{
check = WEDICT_NUM_PB(w->progs, e);
if (ED_ISFREE(check))
continue;
if (check->v->movetype == MOVETYPE_PUSH
|| check->v->movetype == MOVETYPE_NONE
|| check->v->movetype == MOVETYPE_NOCLIP
|| check->v->movetype == MOVETYPE_ANGLENOCLIP)
continue;
/*
oldsolid = pusher->v->solid;
pusher->v->solid = SOLID_NOT;
block = World_TestEntityPosition (w, check);
pusher->v->solid = oldsolid;
if (block)
continue;
*/
// if the entity is standing on the pusher, it will definitely be moved
if ( ! ( ((int)check->v->flags & FL_ONGROUND)
&& PROG_TO_WEDICT(w->progs, check->v->groundentity) == pusher) )
{
// see if the ent needs to be tested
if ( check->v->absmin[0] >= maxs[0]
|| check->v->absmin[1] >= maxs[1]
|| check->v->absmin[2] >= maxs[2]
|| check->v->absmax[0] <= mins[0]
|| check->v->absmax[1] <= mins[1]
|| check->v->absmax[2] <= mins[2] )
continue;
// see if the ent's bbox is inside the pusher's final position
if (!World_TestEntityPosition (w, (wedict_t*)check))
continue;
}
if ((pusher->v->movetype == MOVETYPE_PUSH) || (PROG_TO_WEDICT(w->progs, check->v->groundentity) == pusher))
{
if (pushed_p == (pushed+(sizeof(pushed)/sizeof(pushed[0]))))
continue;
// move this entity
pushed_p->ent = check;
VectorCopy (check->v->origin, pushed_p->origin);
VectorCopy (check->v->angles, pushed_p->angles);
pushed_p++;
// try moving the contacted entity
VectorAdd (check->v->origin, move, check->v->origin);
VectorAdd (check->v->angles, amove, check->v->angles);
if (check->entnum>0&&(check->entnum)<=sv.allocated_client_slots)
svs.clients[check->entnum-1].baseangles[YAW] += yawchange;
// figure movement due to the pusher's amove
VectorSubtract (check->v->origin, pusher->v->origin, org);
org2[0] = DotProduct (org, forward);
org2[1] = -DotProduct (org, right);
org2[2] = DotProduct (org, up);
VectorSubtract (org2, org, move2);
VectorAdd (check->v->origin, move2, check->v->origin);
if (check->v->movetype != MOVETYPE_WALK)
check->v->flags = (int)check->v->flags & ~FL_ONGROUND;
// may have pushed them off an edge
if (PROG_TO_WEDICT(w->progs, check->v->groundentity) != pusher)
check->v->groundentity = 0;
block = World_TestEntityPosition (w, check);
if (!block)
{ // pushed ok
World_LinkEdict (w, check, false);
// impact?
continue;
}
// if it is ok to leave in the old position, do it
// this is only relevent for riding entities, not pushed
// FIXME: this doesn't acount for rotation
VectorCopy (pushed_p[-1].origin, check->v->origin);
block = World_TestEntityPosition (w, check);
if (!block)
{
pushed_p--;
continue;
}
//okay, that didn't work, try pushing the against stuff
WPhys_PushEntity(w, check, move, 0);
block = World_TestEntityPosition (w, check);
if (!block)
continue;
VectorCopy(check->v->origin, move);
for (i = 0; i < 8 && block; i++)
{
//precision errors can strike when you least expect it. lets try and reduce them.
check->v->origin[0] = move[0] + ((i&1)?-1:1)/8.0;
check->v->origin[1] = move[1] + ((i&2)?-1:1)/8.0;
check->v->origin[2] = move[2] + ((i&4)?-1:1)/8.0;
block = World_TestEntityPosition (w, check);
}
if (!block)
{
World_LinkEdict (w, check, false);
continue;
}
}
// if it is sitting on top. Do not block.
if (check->v->mins[0] == check->v->maxs[0])
{
World_LinkEdict (w, check, false);
continue;
}
//some pushers are contents brushes, and are not solid. water cannot crush. the player just enters the water.
//but, the player will be moved along with the water if possible.
if (pusher->v->skin < 0)
continue;
if (check->v->solid == SOLID_NOT || check->v->solid == SOLID_TRIGGER)
{ // corpse
check->v->mins[0] = check->v->mins[1] = 0;
VectorCopy (check->v->mins, check->v->maxs);
World_LinkEdict (w, check, false);
continue;
}
// Con_Printf("Pusher hit %s\n", PR_GetString(w->progs, check->v->classname));
if (pusher->v->blocked)
{
*w->g.self = EDICT_TO_PROG(w->progs, pusher);
*w->g.other = EDICT_TO_PROG(w->progs, check);
#ifdef VM_Q1
if (w==&sv.world && svs.gametype == GT_Q1QVM)
Q1QVM_Blocked();
else
#endif
PR_ExecuteProgram (w->progs, pusher->v->blocked);
}
// move back any entities we already moved
// go backwards, so if the same entity was pushed
// twice, it goes back to the original position
for (p=pushed_p-1 ; p>=pushed ; p--)
{
VectorCopy (p->origin, p->ent->v->origin);
VectorCopy (p->angles, p->ent->v->angles);
World_LinkEdict (w, p->ent, false);
if (p->ent->entnum>0&&(p->ent->entnum)<=sv.allocated_client_slots)
svs.clients[p->ent->entnum-1].baseangles[YAW] -= yawchange;
}
return false;
}
//FIXME: is there a better way to handle this?
// see if anything we moved has touched a trigger
for (p=pushed_p-1 ; p>=pushed ; p--)
World_TouchAllLinks (w, p->ent);
return true;
}
/*
============
SV_Push
============
*/
qboolean WPhys_Push (world_t *w, wedict_t *pusher, vec3_t move, vec3_t amove)
{
#define PUSHABLE_LIMIT 8192
int i, e;
wedict_t *check, *block;
vec3_t mins, maxs;
vec3_t pushorig;
int num_moved;
wedict_t *moved_edict[PUSHABLE_LIMIT];
vec3_t moved_from[PUSHABLE_LIMIT];
float oldsolid;
if ((amove[0] || amove[1] || amove[2]) && !w->remasterlogic)
{
return WPhys_PushAngles(w, pusher, move, amove);
}
for (i=0 ; i<3 ; i++)
{
mins[i] = pusher->v->absmin[i] + move[i]-(1/32.0);
maxs[i] = pusher->v->absmax[i] + move[i]+(1/32.0);
}
VectorCopy (pusher->v->origin, pushorig);
// move the pusher to it's final position
VectorAdd (pusher->v->origin, move, pusher->v->origin);
World_LinkEdict (w, pusher, false);
// see if any solid entities are inside the final position
num_moved = 0;
for (e=1 ; e<w->num_edicts ; e++)
{
check = WEDICT_NUM_PB(w->progs, e);
if (ED_ISFREE(check))
continue;
if (check->v->movetype == MOVETYPE_PUSH
|| check->v->movetype == MOVETYPE_NONE
|| check->v->movetype == MOVETYPE_FOLLOW
|| check->v->movetype == MOVETYPE_NOCLIP
|| check->v->movetype == MOVETYPE_ANGLENOCLIP)
continue;
// if the entity is standing on the pusher, it will definately be moved
if ( ! ( ((int)check->v->flags & FL_ONGROUND)
&&
PROG_TO_WEDICT(w->progs, check->v->groundentity) == pusher) )
{
if ( check->v->absmin[0] >= maxs[0]
|| check->v->absmin[1] >= maxs[1]
|| check->v->absmin[2] >= maxs[2]
|| check->v->absmax[0] <= mins[0]
|| check->v->absmax[1] <= mins[1]
|| check->v->absmax[2] <= mins[2] )
continue;
// see if the ent's bbox is inside the pusher's final position
if (!World_TestEntityPosition (w, check))
continue;
}
oldsolid = pusher->v->solid;
pusher->v->solid = SOLID_NOT;
block = World_TestEntityPosition (w, check);
pusher->v->solid = oldsolid;
if (block)
continue;
if (num_moved == PUSHABLE_LIMIT)
break;
VectorCopy (check->v->origin, moved_from[num_moved]);
moved_edict[num_moved] = check;
num_moved++;
if (check->v->groundentity != pusher->entnum)
check->v->flags = (int)check->v->flags & ~FL_ONGROUND;
// try moving the contacted entity
VectorAdd (check->v->origin, move, check->v->origin);
if (pusher->v->skin < 0)
{
pusher->v->solid = SOLID_NOT;
block = World_TestEntityPosition (w, check);
pusher->v->solid = oldsolid;
}
else
block = World_TestEntityPosition (w, check);
if (!block)
{ // pushed ok
World_LinkEdict (w, check, false);
continue;
}
if (block)
{
//try to nudge it forward by an epsilon to avoid precision issues
float movelen = VectorLength(move);
VectorMA(check->v->origin, (1/8.0)/movelen, move, check->v->origin);
block = World_TestEntityPosition (w, check);
if (!block)
{ //okay, that got it. we're all good.
World_LinkEdict (w, check, false);
continue;