/
p_mobj.c
1039 lines (874 loc) · 27.3 KB
/
p_mobj.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
/**\file
*\section License
* License: GPL
* Online License Link: http://www.gnu.org/licenses/gpl.html
*
*\author Copyright © 2003-2011 Jaakko Keränen <jaakko.keranen@iki.fi>
*\author Copyright © 2006-2009 Daniel Swanson <danij@dengine.net>
*\author Copyright © 1993-1996 by 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., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
/**
* p_mobj.c: Map Objects
*
* Contains various routines for moving mobjs, collision and Z checking.
*/
// HEADER FILES ------------------------------------------------------------
#include <math.h>
#include "de_base.h"
#include "de_console.h"
#include "de_system.h"
#include "de_network.h"
#include "de_play.h"
#include "de_refresh.h"
#include "de_misc.h"
#include "de_audio.h"
#include "def_main.h"
// MACROS ------------------------------------------------------------------
// Max. distance to move in one call to P_MobjMoveXY.
//#define MAXMOVE 30
// Shortest possible movement step.
//#define MINMOVE (1.0f/128)
//#define MIN_STEP(d) ((d) <= MINMOVE && (d) >= -MINMOVE)
//#define STOPSPEED (0.1f/1.6f)
// TYPES -------------------------------------------------------------------
/*
typedef struct {
mobj_t *mo;
vec2_t box[2];
int flags;
float pos[3];
float height, floorZ, ceilingZ, dropOffZ;
} checkpos_data_t;
*/
// EXTERNAL FUNCTION PROTOTYPES --------------------------------------------
// PUBLIC FUNCTION PROTOTYPES ----------------------------------------------
// PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
// EXTERNAL DATA DECLARATIONS ----------------------------------------------
// PUBLIC DATA DEFINITIONS -------------------------------------------------
// If set to true, P_CheckPosition will skip the mobj hit test.
//boolean dontHitMobjs = false;
//float tmpFloorZ;
//float tmpCeilingZ;
// When a mobj is contacted in PIT_CheckMobj, this pointer is set.
// It's reset to NULL in the beginning of P_CheckPosition.
//mobj_t *blockingMobj;
// PRIVATE DATA DEFINITIONS ------------------------------------------------
// Slide variables.
//static float bestSlideFrac;
//static float secondSlideFrac;
//static linedef_t *bestSlideLine;
//static linedef_t *secondSlideLine;
//static mobj_t *slideMo;
//static boolean noFit;
//static float tmpDropOffZ;
//static float tmpMom[3];
static mobj_t *unusedMobjs = NULL;
// CODE --------------------------------------------------------------------
/**
* Called during map loading.
*/
void P_InitUnusedMobjList(void)
{
// Any zone memory allocated for the mobjs will have already been purged.
unusedMobjs = NULL;
}
/**
* All mobjs must be allocated through this routine. Part of the public API.
*/
mobj_t* P_MobjCreate(think_t function, float x, float y, float z,
angle_t angle, float radius, float height, int ddflags)
{
mobj_t* mo;
if(!function)
Con_Error("P_MobjCreate: Think function invalid, cannot create mobj.");
#ifdef _DEBUG
if(isClient)
{
VERBOSE2( Con_Message("P_MobjCreate: Client creating mobj at %f,%f\n", x, y) );
}
#endif
// Do we have any unused mobjs we can reuse?
if(unusedMobjs)
{
mo = unusedMobjs;
unusedMobjs = unusedMobjs->sNext;
memset(mo, 0, MOBJ_SIZE);
}
else
{ // No, we need to allocate another.
mo = Z_Calloc(MOBJ_SIZE, PU_MAP, NULL);
}
mo->pos[VX] = x;
mo->pos[VY] = y;
mo->pos[VZ] = z;
mo->angle = angle;
mo->visAngle = mo->angle >> 16; // "angle-servo"; smooth actor turning.
mo->radius = radius;
mo->height = height;
mo->ddFlags = ddflags;
mo->thinker.function = function;
if(mo->thinker.function)
{
P_ThinkerAdd(&mo->thinker, true); // Make it public.
}
return mo;
}
/**
* All mobjs must be destroyed through this routine. Part of the public API.
*
* \note Does not actually destroy the mobj. Instead, mobj is marked as
* awaiting removal (which occurs when its turn for thinking comes around).
*/
void P_MobjDestroy(mobj_t* mo)
{
#ifdef _DEBUG
if(mo->ddFlags & DDMF_MISSILE)
{
VERBOSE2( Con_Message("P_MobjDestroy: Destroying missile %i.\n", mo->thinker.id) );
}
#endif
// Unlink from sector and block lists.
P_MobjUnlink(mo);
S_StopSound(0, mo);
P_ThinkerRemove((thinker_t *) mo);
}
/**
* Called when a mobj is actually removed (when it's thinking turn comes around).
* The mobj is moved to the unused list to be reused later.
*/
void P_MobjRecycle(mobj_t* mo)
{
// The sector next link is used as the unused mobj list links.
mo->sNext = unusedMobjs;
unusedMobjs = mo;
}
/**
* 'statenum' must be a valid state (not null!).
*/
void P_MobjSetState(mobj_t* mobj, int statenum)
{
state_t* st = states + statenum;
boolean spawning = (mobj->state == 0);
ded_ptcgen_t* pg;
#if _DEBUG
if(statenum < 0 || statenum >= defs.count.states.num)
Con_Error("P_MobjSetState: statenum %i out of bounds.\n", statenum);
/*
if(mobj->ddFlags & DDMF_MISSILE)
{
Con_Message("P_MobjSetState: Missile %i going to state %i.\n", mobj->thinker.id, statenum);
}
*/
#endif
mobj->state = st;
mobj->tics = st->tics;
mobj->sprite = st->sprite;
mobj->frame = st->frame;
// Check for a ptcgen trigger.
for(pg = statePtcGens[statenum]; pg; pg = pg->stateNext)
{
if(!(pg->flags & PGF_SPAWN_ONLY) || spawning)
{
// We are allowed to spawn the generator.
P_SpawnParticleGen(pg, mobj);
}
}
if(!(mobj->ddFlags & DDMF_REMOTE))
{
if(defs.states[statenum].execute)
Con_Execute(CMDS_DED, defs.states[statenum].execute, true, false);
}
}
/**
* Sets a mobj's position.
*
* @return @c true if successful, @c false otherwise. The object's position is
* not changed if the move fails.
*
* @note Internal to the engine.
*/
boolean P_MobjSetPos(struct mobj_s* mo, float x, float y, float z)
{
assert(gx.MobjTryMove3f != 0);
return gx.MobjTryMove3f(mo, x, y, z);
}
#if 0
/**
* Adjusts tmpFloorZ and tmpCeilingZ as lines are contacted.
*/
boolean PIT_LineCollide(linedef_t* ld, void* parm)
{
checkpos_data_t* tm = parm;
if(tm->box[1][VX] <= ld->bBox[BOXLEFT] ||
tm->box[0][VX] >= ld->bBox[BOXRIGHT] ||
tm->box[1][VY] <= ld->bBox[BOXBOTTOM] ||
tm->box[0][VY] >= ld->bBox[BOXTOP])
// Bounding boxes do not overlap.
return true;
if(P_BoxOnLineSide2(tm->box[0][VX], tm->box[1][VX],
tm->box[0][VY], tm->box[1][VY], ld) != -1)
return true;
// A line has been hit.
tm->mo->wallHit = true;
if(!ld->L_backside)
return false; // One sided line, can't go through.
if(!(tm->mo->ddFlags & DDMF_MISSILE))
{
if(ld->flags & DDLF_BLOCKING)
return false; // explicitly blocking everything
}
// set openrange, opentop, openbottom.
P_LineOpening(ld);
// adjust floor / ceiling heights.
if(opentop < tm->ceilingZ)
tm->ceilingZ = opentop;
if(openbottom > tm->floorZ)
tm->floorZ = openbottom;
if(lowfloor < tm->dropOffZ)
tm->dropOffZ = lowfloor;
tm->mo->wallHit = false;
return true;
}
#endif
#if 0
boolean PIT_MobjCollide(mobj_t* mo, void* parm)
{
checkpos_data_t* tm = parm;
float blockdist;
boolean overlap = false;
// Don't clip against self.
if(mo == tm->mo)
return true;
if(!(mo->ddFlags & DDMF_SOLID))
return true;
blockdist = mo->radius + tm->mo->radius;
// Only players can move under or over other mobjs.
if(tm->pos[VZ] != DDMAXFLOAT &&
(tm->mo->dPlayer || mo->ddFlags & DDMF_NOGRAVITY))
{
if(mo->pos[VZ] > tm->pos[VZ] + tm->height)
{
// We're under it.
return true;
}
else if(mo->pos[VZ] + mo->height < tm->pos[VZ])
{
// We're over it.
return true;
}
overlap = true;
}
if(fabs(mo->pos[VX] - tm->pos[VX]) >= blockdist ||
fabs(mo->pos[VY] - tm->pos[VY]) >= blockdist)
{
// Didn't hit it.
return true;
}
if(overlap)
{
// How are we positioned?
if(tm->pos[VZ] >= mo->pos[VZ] + mo->height - 24)
{
// Above, allowing stepup.
tm->mo->onMobj = mo;
tm->floorZ = mo->pos[VZ] + mo->height;
return true;
}
// This was causing problems in Doom E1M1, where a client was able
// to walk through the barrel obstacle and get the shotgun. -jk
/*
// To prevent getting stuck, don't block if moving away from
// the object.
if(tm->mo->dPlayer &&
P_ApproxDistance(tm->mo->pos[VX] - mo->pos[VX],
tm->mo->pos[VY] - mo->pos[VY]) <
P_ApproxDistance(tm->pos[VX] - mo->pos[VX], tm->pos[VY] - mo->pos[VY]) &&
tm->mo->momz > -12)
{
// The current distance is smaller than the new one would be.
// No blocking needs to occur.
// The Z movement test is done to prevent a 'falling through'
// case when a mo is moving at a high speed.
return true;
}*/
// We're hitting this mobj.
blockingMobj = mo;
}
return false;
}
#endif
#if 0
/**
* @return @c true, if the mobj can be positioned at the
* specified coordinates.
*/
boolean P_CheckPosXYZ(mobj_t *mo, float x, float y, float z)
{
subsector_t *newsubsec;
checkpos_data_t data;
vec2_t point;
boolean result = true;
blockingMobj = NULL;
mo->onMobj = NULL;
mo->wallHit = false;
// Prepare the data struct.
data.mo = mo;
data.flags = mo->ddFlags;
data.pos[VX] = x;
data.pos[VY] = y;
data.pos[VZ] = z;
data.height = mo->height;
// The bounding box is extended by DDMOBJ_RADIUS_MAX because mobj_ts are
// grouped into mapblocks based on their origin point, and can overlap
// into adjacent blocks by up to DDMOBJ_RADIUS_MAX units.
V2_Set(point, x - mo->radius - DDMOBJ_RADIUS_MAX, y - mo->radius - DDMOBJ_RADIUS_MAX);
V2_InitBox(data.box, point);
V2_Set(point, x + mo->radius + DDMOBJ_RADIUS_MAX, y + mo->radius + DDMOBJ_RADIUS_MAX);
V2_AddToBox(data.box, point);
newsubsec = R_PointInSubsector(x, y);
// The base floor / ceiling is from the subsector that contains the
// point. Any contacted lines the step closer together will adjust them.
data.floorZ = data.dropOffZ = newsubsec->sector->SP_floorheight;
data.ceilingZ = newsubsec->sector->SP_ceilheight;
validCount++;
// Check mobjs first, possibly picking stuff up.
if(!dontHitMobjs)
{
if(!P_MobjsBoxIteratorv(data.box, PIT_MobjCollide, &data))
{
result = false;
}
}
// Hit something yet?
if(result)
{ // Nope.
// Try polyobj->lineDefs and lines.
if(!P_AllLinesBoxIteratorv(data.box, PIT_LineCollide, &data))
{
result = false;
}
}
tmpCeilingZ = data.ceilingZ;
tmpFloorZ = data.floorZ;
tmpDropOffZ = data.dropOffZ;
return result;
}
#endif
#if 0
/**
* @return @c true, if the mobj can be positioned at the
* specified coordinates, assuming traditional 2D DOOM
* mobj placement rules.
*/
boolean P_CheckPosXY(mobj_t *mo, float x, float y)
{
return P_CheckPosXYZ(mo, x, y, DDMAXFLOAT);
}
/**
* Attempt to move to a mobj to a new position.
*
* @return @c true, if the move was successful. Both lines and
* and other mobjs are checked for collisions.
*/
boolean P_TryMoveXYZ(mobj_t *mo, float x, float y, float z)
{
int links = 0;
boolean goodPos;
blockingMobj = NULL;
// Is this a real move?
if(mo->pos[VX] == x && mo->pos[VY] == y && mo->pos[VZ] == z)
{ // No move. Of course it's successful.
return true;
}
goodPos = P_CheckPosXYZ(mo, x, y, z);
// Is movement clipping in effect?
if(!mo->dPlayer || !(mo->dPlayer->flags & DDPF_NOCLIP))
{
if(!goodPos)
{
if(!mo->onMobj || mo->wallHit)
return false; // Solid wall or mo.
}
// Does it fit between contacted ceiling and floor?
if(tmpCeilingZ - tmpFloorZ < mo->height)
return false;
if(tmpCeilingZ - z < mo->height)
return false; // mobj must lower itself to fit.
if(mo->dPlayer)
{
// Players are allowed a stepup.
if(tmpFloorZ - z > 24)
return false; // Too big a step up.
}
else
{
// Normals mobjs are not...
if(tmpFloorZ > z)
return false; // Below the floor.
}
}
// The move is OK. First unlink.
links = P_MobjUnlink(mo);
mo->floorZ = tmpFloorZ;
mo->ceilingZ = tmpCeilingZ;
mo->pos[VX] = x;
mo->pos[VY] = y;
mo->pos[VZ] = z;
// Put back to the same links.
P_MobjLink(mo, links);
return true;
}
/**
* Try to do the given move. Returns true if nothing was hit.
*/
boolean P_StepMove(mobj_t *mo, float dx, float dy, float dz)
{
float delta[3];
float step[3];
boolean notHit = true;
delta[VX] = dx;
delta[VY] = dy;
delta[VZ] = dz;
while(delta[VX] || delta[VY] || delta[VZ])
{
step[VX] = delta[VX];
step[VY] = delta[VY];
step[VZ] = delta[VZ];
// Is the step too long?
while(step[VX] > MAXMOVE || step[VX] < -MAXMOVE || step[VY] > MAXMOVE ||
step[VY] < -MAXMOVE || step[VZ] > MAXMOVE || step[VZ] < -MAXMOVE)
{
// Only half that, then.
step[VX] /= 2;
step[VY] /= 2;
step[VZ] /= 2;
}
// If there is no step, we're already there!
if(!(step[VX] == 0 && step[VY] == 0 && step[VZ] == 0))
return notHit;
// Can we do this step?
while(!P_TryMoveXYZ(mo, mo->pos[VX] + step[VX],
mo->pos[VY] + step[VY],
mo->pos[VZ] + step[VZ]))
{
// We hit something!
notHit = false;
// This means even the current step is unreachable.
// Let's make it our intended destination.
delta[VX] = step[VX];
delta[VY] = step[VY];
delta[VZ] = step[VZ];
// Try a smaller step.
step[VX] /= 2;
step[VY] /= 2;
step[VZ] /= 2;
// If we run out of step, we must give up.
if(MIN_STEP(step[VX]) && MIN_STEP(step[VY]) && MIN_STEP(step[VZ]))
{
return false;
}
}
// Subtract from the 'to go' distance.
delta[VX] -= step[VX];
delta[VY] -= step[VY];
delta[VZ] -= step[VZ];
}
return notHit;
}
#endif
#if 0
/**
* Takes a valid mobj and adjusts the mobj->floorZ, mobj->ceilingZ, and
* possibly mobj->z. This is called for all nearby mobjs whenever a sector
* changes height. If the mobj doesn't fit, the z will be set to the lowest
* value and false will be returned.
*/
static boolean heightClip(mobj_t *mo)
{
boolean onfloor;
// During demo playback the player gets preferential treatment.
if(mo->dPlayer == &ddPlayers[consolePlayer].shared && playback)
return true;
onfloor = (mo->pos[VZ] <= mo->floorZ);
P_CheckPosXYZ(mo, mo->pos[VX], mo->pos[VY], mo->pos[VZ]);
mo->floorZ = tmpFloorZ;
mo->ceilingZ = tmpCeilingZ;
if(onfloor)
{
mo->pos[VZ] = mo->floorZ;
}
else
{
// Don't adjust a floating mobj unless forced to.
if(mo->pos[VZ] + mo->height > mo->ceilingZ)
mo->pos[VZ] = mo->ceilingZ - mo->height;
}
/*
// On clientside, players are represented by two mobjs: the real mobj,
// created by the Game, is the one that is visible and modified in this
// function. We'll need to sync the hidden client mobj (that receives
// all the changes from the server) to match the changes.
if(isClient && mo->dPlayer)
{
Cl_UpdatePlayerPos(P_GetDDPlayerIdx(mo->dPlayer));
}
*/
if(mo->ceilingZ - mo->floorZ < mo->height)
return false;
return true;
}
#endif
#if 0
/**
* Allows the player to slide along any angled walls. Adjusts the player's
* momentum vector so that the next move slides along the linedef.
*/
static void wallMomSlide(linedef_t *ld)
{
int side;
uint an;
float movelen, newlen;
angle_t moveangle, lineangle, deltaangle;
// First check the simple cases.
if(ld->slopeType == ST_HORIZONTAL)
{
tmpMom[VY] = 0;
return;
}
if(ld->slopeType == ST_VERTICAL)
{
tmpMom[VX] = 0;
return;
}
side = P_PointOnLinedefSide(slideMo->pos[VX], slideMo->pos[VY], ld);
lineangle = R_PointToAngle2(0, 0, ld->dX, ld->dY);
if(side == 1)
lineangle += ANG180;
moveangle = R_PointToAngle2(0, 0, tmpMom[VX], tmpMom[VY]);
deltaangle = moveangle - lineangle;
if(deltaangle > ANG180)
deltaangle += ANG180;
an = deltaangle >> ANGLETOFINESHIFT;
movelen = P_ApproxDistance(tmpMom[MX], tmpMom[MY]);
newlen = movelen * FIX2FLT(fineCosine[an]);
an = lineangle >> ANGLETOFINESHIFT;
tmpMom[VX] = newlen * FIX2FLT(fineCosine[an]);
tmpMom[VY] = newlen * FIX2FLT(finesine[an]);
}
#endif
#if 0
static boolean slideTraverse(intercept_t *in)
{
linedef_t *li;
if(in->type != ICPT_LINE)
Con_Error("PTR_SlideTraverse: not a line?");
li = in->d.lineDef;
if(!li->L_frontside || !li->L_backside)
{
if(P_PointOnLinedefSide(slideMo->pos[VX],
slideMo->pos[VY], li))
{ // The back side.
return true; // Continue iteration.
}
goto isblocking;
}
// Set openrange, opentop, openbottom.
P_LineOpening(li);
if(openrange < slideMo->height)
goto isblocking; // Doesn't fit.
if(opentop - slideMo->pos[VZ] < slideMo->height)
goto isblocking; // Mobj is too high.
if(openbottom - slideMo->pos[VZ] > 24)
goto isblocking; // Too big a step up.
// This line doesn't block movement.
return true; // Continue iteration.
// The line does block movement, see if it is closer than best so far.
isblocking:
if(in->frac < bestSlideFrac)
{
secondSlideFrac = bestSlideFrac;
secondSlideLine = bestSlideLine;
bestSlideFrac = in->frac;
bestSlideLine = li;
}
return false; // Stop iteration.
}
#endif
#if 0
/**
* The momx / momy move is bad, so try to slide along a wall.
* Find the first line hit, move flush to it, and slide along it
*
* This is a kludgy mess. (No kidding?)
*/
static void mobjSlideMove(mobj_t *mo)
{
float leadPos[2];
float trailPos[2];
float delta[2];
int hitcount;
slideMo = mo;
hitcount = 0;
retry:
if(++hitcount == 3)
goto stairstep; // Don't loop forever.
// Trace along the three leading corners.
if(mo->mom[MX] > 0)
{
leadPos[VX] = mo->pos[VX] + mo->radius;
trailPos[VX] = mo->pos[VX] - mo->radius;
}
else
{
leadPos[VX] = mo->pos[VX] - mo->radius;
trailPos[VX] = mo->pos[VX] + mo->radius;
}
if(mo->mom[MY] > 0)
{
leadPos[VY] = mo->pos[VY] + mo->radius;
trailPos[VY] = mo->pos[VY] - mo->radius;
}
else
{
leadPos[VY] = mo->pos[VY] - mo->radius;
trailPos[VY] = mo->pos[VY] + mo->radius;
}
bestSlideFrac = FIX2FLT(FRACUNIT + 1);
P_PathTraverse(leadPos[VX], leadPos[VY],
leadPos[VX] + mo->mom[MX], leadPos[VY] + mo->mom[MY],
PT_ADDLINES, slideTraverse);
P_PathTraverse(trailPos[VX], leadPos[VY],
trailPos[VX] + mo->mom[MX], leadPos[VY] + mo->mom[MY],
PT_ADDLINES, slideTraverse);
P_PathTraverse(leadPos[VX], trailPos[VY],
leadPos[VX] + mo->mom[MX], trailPos[VY] + mo->mom[MY],
PT_ADDLINES, slideTraverse);
// Move up to the wall.
if(bestSlideFrac == FIX2FLT(FRACUNIT + 1))
{
// The move most have hit the middle, so stairstep.
stairstep:
if(!P_TryMoveXYZ(mo, mo->pos[VX], mo->pos[VY] + mo->mom[MY], mo->pos[VZ]))
P_TryMoveXYZ(mo, mo->pos[VX] + mo->mom[MX], mo->pos[VY], mo->pos[VZ]);
return;
}
// Fudge a bit to make sure it doesn't hit.
bestSlideFrac -= FIX2FLT(0x800);
if(bestSlideFrac > 0)
{
delta[VX] = mo->mom[MX] * bestSlideFrac;
delta[VY] = mo->mom[MY] * bestSlideFrac;
if(!P_TryMoveXYZ(mo, mo->pos[VX] + delta[VX], mo->pos[VY] + delta[VY], mo->pos[VZ]))
goto stairstep;
}
// Now continue along the wall, first calculate remainder.
bestSlideFrac = 1 - (bestSlideFrac + FIX2FLT(0x800));
if(bestSlideFrac > 1)
bestSlideFrac = 1;
if(bestSlideFrac <= 0)
return;
tmpMom[MX] = mo->mom[MX] * bestSlideFrac;
tmpMom[MY] = mo->mom[MY] * bestSlideFrac;
wallMomSlide(bestSlideLine); // Clip the move.
mo->mom[MX] = tmpMom[MX];
mo->mom[MY] = tmpMom[MY];
if(!P_TryMoveXYZ(mo, mo->pos[VX] + tmpMom[MX], mo->pos[VY] + tmpMom[MY],
mo->pos[VZ]))
{
goto retry;
}
}
#endif
#if 0
/**
* After modifying a sectors floor or ceiling height, call this routine
* to adjust the positions of all mobjs that touch the sector.
*
* If anything doesn't fit anymore, true will be returned.
*/
boolean PIT_SectorPlanesChanged(mobj_t *mo, void *data)
{
// Always keep checking.
if(heightClip(mo))
return true;
noFit = true;
return true;
}
/**
* Called whenever a sector's planes are moved. This will update the mobjs
* inside the sector and do crushing.
*
* @param sector Sector number.
*/
boolean P_SectorPlanesChanged(uint sector)
{
noFit = false;
// We'll use validCount to make sure mobjs are only checked once.
validCount++;
P_SectorTouchingMobjsIterator(SECTOR_PTR(sector), PIT_SectorPlanesChanged, 0);
return noFit;
}
void P_MobjMovement(mobj_t *mo)
{
P_MobjMovement2(mo, NULL);
}
/**
* Playmove can be NULL. It's only used with player mobjs.
*/
void P_MobjMovement2(mobj_t *mo, void *pstate)
{
clplayerstate_t *playstate = pstate;
float tryPos[3];
float delta[3];
ddplayer_t *player;
if(mo->mom[MX] == 0 && mo->mom[MY] == 0)
return; // This isn't moving anywhere.
player = mo->dPlayer;
// Make sure we're not trying to move too much.
if(mo->mom[MX] > MAXMOVE)
mo->mom[MX] = MAXMOVE;
else if(mo->mom[MX] < -MAXMOVE)
mo->mom[MX] = -MAXMOVE;
if(mo->mom[MY] > MAXMOVE)
mo->mom[MY] = MAXMOVE;
else if(mo->mom[MY] < -MAXMOVE)
mo->mom[MY] = -MAXMOVE;
// Do the move in progressive steps.
delta[MX] = mo->mom[MX];
delta[MY] = mo->mom[MY];
do
{
if(delta[MX] > MAXMOVE / 2 || delta[MY] > MAXMOVE / 2 ||
delta[MX] < -MAXMOVE / 2 || delta[MY] < -MAXMOVE / 2)
{
tryPos[VX] = mo->pos[VX] + delta[MX] / 2;
tryPos[VY] = mo->pos[VY] + delta[MY] / 2;
delta[MX] /= 2;
delta[MY] /= 2;
}
else
{
tryPos[VX] = mo->pos[VX] + delta[MX];
tryPos[VY] = mo->pos[VY] + delta[MY];
delta[MX] = delta[MY] = 0;
}
if(!P_TryMoveXYZ(mo, tryPos[VX], tryPos[VY], mo->pos[VZ]))
{
// Blocked move.
if(player)
{
if(blockingMobj)
{
// Slide along the side of the mobj.
if(P_TryMoveXYZ(mo, mo->pos[VX], tryPos[VY], mo->pos[VZ]))
{
mo->mom[MX] = 0;
}
else if(P_TryMoveXYZ(mo, tryPos[VX], mo->pos[VY], mo->pos[VZ]))
{
mo->mom[MY] = 0;
}
else
{
// All movement stops here.
mo->mom[MX] = mo->mom[MY] = 0;
}
}
else
{
// Try to slide along it.
mobjSlideMove(mo);
}
}
else
{
// Stop moving.
mo->mom[MX] = mo->mom[MY] = 0;
}
}
} while(delta[MX] != 0 || delta[MY] != 0);
// Apply friction.
if(mo->ddFlags & DDMF_MISSILE)
return; // No friction for missiles, ever.
if(mo->pos[VZ] > mo->floorZ && !mo->onMobj && !(mo->ddFlags & DDMF_FLY))
return; // No friction when airborne.
if(mo->mom[MX] > -STOPSPEED && mo->mom[MX] < STOPSPEED &&
mo->mom[MY] > -STOPSPEED && mo->mom[MY] < STOPSPEED &&
(!playstate || (!playstate->forwardMove && !playstate->sideMove)))
{
mo->mom[MX] = mo->mom[MY] = 0;
}
else
{
float friction;
if(playstate)
friction = FIX2FLT(playstate->friction);
else
friction = DEFAULT_FRICTION;
mo->mom[MX] *= friction;
mo->mom[MY] *= friction;
}
}
void P_MobjZMovement(mobj_t *mo)
{
float gravity = FIX2FLT(mapGravity);
// Adjust height.
mo->pos[VZ] += mo->mom[MZ];
// Clip movement. Another mobj?
if(mo->onMobj && mo->pos[VZ] <= mo->onMobj->pos[VZ] + mo->onMobj->height)
{
if(mo->mom[MZ] < 0)
{
mo->mom[MZ] = 0;
}
if(mo->mom[MZ] == 0)