-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathp_mobj.c
1252 lines (961 loc) · 30.6 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
/* Emacs style mode select -*- C++ -*-
*-----------------------------------------------------------------------------
*
*
* PrBoom: a Doom port merged with LxDoom and LSDLDoom
* based on BOOM, a modified and improved DOOM engine
* Copyright (C) 1999 by
* id Software, Chi Hoang, Lee Killough, Jim Flynn, Rand Phares, Ty Halderman
* Copyright (C) 1999-2004 by
* Jess Haas, Nicolas Kalkhof, Colin Phipps, Florian Schulze
* Copyright 2005, 2006 by
* Florian Schulze, Colin Phipps, Neil Stevens, Andrey Budko
* Copyright 2023, 2024 by
* Frenkel Smeijers
*
* 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.
*
* DESCRIPTION:
* Moving object handling. Spawn functions.
*
*-----------------------------------------------------------------------------*/
#include <stdint.h>
#include "compiler.h"
#include "doomdef.h"
#include "d_player.h"
#include "m_random.h"
#include "r_main.h"
#include "p_maputl.h"
#include "p_map.h"
#include "p_tick.h"
#include "sounds.h"
#include "st_stuff.h"
#include "hu_stuff.h"
#include "s_sound.h"
#include "info.h"
#include "g_game.h"
#include "p_inter.h"
#include "i_system.h"
#include "globdata.h"
void A_CyberAttack(mobj_t __far* actor);
//
// P_SetMobjState
// Returns true if the mobj is still present.
//
boolean P_SetMobjState(mobj_t __far* mobj, statenum_t state)
{
const state_t* st;
do
{
if (state == S_NULL)
{
mobj->state = (state_t *) S_NULL;
P_RemoveMobj (mobj);
return false;
}
st = &states[state];
mobj->state = st;
mobj->tics = st->tics;
mobj->sprite = st->sprite;
mobj->frame = st->frame;
// Modified handling.
// Call action functions when the state is set
if(st->action)
{
if(!(_g_player.cheats & CF_ENEMY_ROCKETS))
{
st->action(mobj);
}
else
{
if(mobjinfo[mobj->type].missilestate && (state >= mobjinfo[mobj->type].missilestate) && (state < mobjinfo[mobj->type].painstate))
A_CyberAttack(mobj);
else
st->action(mobj);
}
}
state = st->nextstate;
} while (!mobj->tics);
return true;
}
//
// P_ExplodeMissile
//
static void P_ExplodeMissile(mobj_t __far* mo)
{
mo->momx = mo->momy = mo->momz = 0;
P_SetMobjState (mo, mobjinfo[mo->type].deathstate);
mo->tics -= P_Random()&3;
if (mo->tics < 1)
mo->tics = 1;
mo->flags &= ~MF_MISSILE;
if (mobjinfo[mo->type].deathsound)
S_StartSound (mo, mobjinfo[mo->type].deathsound);
}
//
// SLIDE MOVE
// Allows the player to slide along any angled walls.
//
static fixed_t bestslidefrac;
static const line_t __far* bestslideline;
static mobj_t __far* slidemo;
static fixed_t tmxmove;
static fixed_t tmymove;
//
// P_HitSlideLine
// Adjusts the xmove / ymove
// so that the next move will slide along the wall.
// If the floor is icy, then you can bounce off a wall. // phares
//
static void P_HitSlideLine(const line_t __far* ld)
{
int16_t side;
angle_t lineangle;
angle_t moveangle;
angle_t deltaangle;
fixed_t movelen;
fixed_t newlen;
// |
// Under icy conditions, if the angle of approach to the wall // V
// is more than 45 degrees, then you'll bounce and lose half
// your momentum. If less than 45 degrees, you'll slide along
// the wall. 45 is arbitrary and is believable.
// Check for the special cases of horz or vert walls.
/* killough 10/98: only bounce if hit hard (prevents wobbling)
* cph - DEMOSYNC - should only affect players in Boom demos? */
if (ld->dy == 0)
{
tmymove = 0; // no more movement in the Y direction
return;
}
if (ld->dx == 0)
{ // phares
tmxmove = 0; // no more movement in the X direction
return;
}
// The wall is angled. Bounce if the angle of approach is // phares
// less than 45 degrees. // phares
side = P_PointOnLineSide (slidemo->x, slidemo->y, ld);
lineangle = R_PointToAngle2 (0,0, (fixed_t)ld->dx<<FRACBITS, (fixed_t)ld->dy<<FRACBITS);
if (side == 1)
lineangle += ANG180;
moveangle = R_PointToAngle2 (0,0, tmxmove, tmymove);
// killough 3/2/98:
// The moveangle+=10 breaks v1.9 demo compatibility in
// some demos, so it needs demo_compatibility switch.
moveangle += 10; // prevents sudden path reversal due to // phares
// rounding error // |
deltaangle = moveangle-lineangle; // V
movelen = P_AproxDistance (tmxmove, tmymove);
// |
// phares
if (deltaangle > ANG180)
deltaangle += ANG180;
// I_Error ("SlideLine: ang>ANG180");
lineangle >>= ANGLETOFINESHIFT;
deltaangle >>= ANGLETOFINESHIFT;
newlen = FixedMulAngle(movelen, finecosine(deltaangle));
tmxmove = FixedMulAngle(newlen, finecosine(lineangle));
tmymove = FixedMulAngle(newlen, finesine( lineangle));
// phares
}
//
// PTR_SlideTraverse
//
static boolean PTR_SlideTraverse (intercept_t* in)
{
const line_t __far* li;
if (!in->isaline)
I_Error ("PTR_SlideTraverse: not a line?");
li = in->d.line;
if ( ! (li->flags & ML_TWOSIDED) )
{
if (P_PointOnLineSide (slidemo->x, slidemo->y, li))
return true; // don't hit the back side
goto isblocking;
}
// set openrange, opentop, openbottom.
// These define a 'window' from one sector to another across a line
P_LineOpening (li);
if (_g_openrange < slidemo->height)
goto isblocking; // doesn't fit
if (_g_opentop - slidemo->z < slidemo->height)
goto isblocking; // mobj is too high
if (_g_openbottom - slidemo->z > 24*FRACUNIT )
goto isblocking; // too big a step up
// this line doesn't block movement
return true;
// the line does block movement,
// see if it is closer than best so far
isblocking:
if (in->frac < bestslidefrac)
{
bestslidefrac = in->frac;
bestslideline = li;
}
return false; // stop
}
//
// P_SlideMove
// 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.
//
// killough 11/98: reformatted
static void P_SlideMove(mobj_t __far* mo)
{
int8_t hitcount = 3;
slidemo = mo; // the object that's sliding
do
{
fixed_t leadx, leady, trailx, traily;
if (!--hitcount)
goto stairstep; // don't loop forever
// trace along the three leading corners
if (mo->momx > 0)
leadx = mo->x + mo->radius, trailx = mo->x - mo->radius;
else
leadx = mo->x - mo->radius, trailx = mo->x + mo->radius;
if (mo->momy > 0)
leady = mo->y + mo->radius, traily = mo->y - mo->radius;
else
leady = mo->y - mo->radius, traily = mo->y + mo->radius;
bestslidefrac = FRACUNIT+1;
P_PathTraverse(leadx, leady, leadx+mo->momx, leady+mo->momy,
PT_ADDLINES, PTR_SlideTraverse);
P_PathTraverse(trailx, leady, trailx+mo->momx, leady+mo->momy,
PT_ADDLINES, PTR_SlideTraverse);
P_PathTraverse(leadx, traily, leadx+mo->momx, traily+mo->momy,
PT_ADDLINES, PTR_SlideTraverse);
// move up to the wall
if (bestslidefrac == FRACUNIT+1)
{
// the move must have hit the middle, so stairstep
stairstep:
/* phares 5/4/98: kill momentum if you can't move at all
* This eliminates player bobbing if pressed against a wall
* while on ice.
*
* killough 10/98: keep buggy code around for old Boom demos
*
* cph 2000/09//23: buggy code was only in Boom v2.01
*/
if (!P_TryMove(mo, mo->x, mo->y + mo->momy))
P_TryMove(mo, mo->x + mo->momx, mo->y);
break;
}
// fudge a bit to make sure it doesn't hit
if ((bestslidefrac -= 0x800) > 0)
{
fixed_t newx = FixedMul(mo->momx, bestslidefrac);
fixed_t newy = FixedMul(mo->momy, bestslidefrac);
if (!P_TryMove(mo, mo->x+newx, mo->y+newy))
goto stairstep;
}
// Now continue along the wall.
// First calculate remainder.
bestslidefrac = FRACUNIT-(bestslidefrac+0x800);
if (bestslidefrac > FRACUNIT)
bestslidefrac = FRACUNIT;
if (bestslidefrac <= 0)
break;
tmxmove = FixedMul(mo->momx, bestslidefrac);
tmymove = FixedMul(mo->momy, bestslidefrac);
P_HitSlideLine(bestslideline); // clip the moves
mo->momx = tmxmove;
mo->momy = tmymove;
/* killough 10/98: affect the bobbing the same way (but not voodoo dolls)
* cph - DEMOSYNC? */
if (P_MobjIsPlayer(mo) && P_MobjIsPlayer(mo)->mo == mo)
{
if (D_abs(P_MobjIsPlayer(mo)->momx) > D_abs(tmxmove))
P_MobjIsPlayer(mo)->momx = tmxmove;
if (D_abs(P_MobjIsPlayer(mo)->momy) > D_abs(tmymove))
P_MobjIsPlayer(mo)->momy = tmymove;
}
}
while (!P_TryMove(mo, mo->x+tmxmove, mo->y+tmymove));
}
static fixed_t FixedMul32OrigFriction(fixed_t a)
{
uint16_t alw = a;
int16_t ahw = a >> FRACBITS;
uint32_t ll = (uint32_t) alw * ORIG_FRICTION;
int32_t hl = ( int32_t) ahw * ORIG_FRICTION;
return (ll >> FRACBITS) + hl;
}
//
// P_XYMovement
//
// Attempts to move something if it has momentum.
//
#define MAXMOVE (30*FRACUNIT)
#define STOPSPEED (FRACUNIT/16)
static void P_XYMovement(mobj_t __far* mo)
{
player_t *player;
fixed_t xmove, ymove;
if (!(mo->momx | mo->momy)) // Any momentum?
{
return;
}
player = P_MobjIsPlayer(mo);
if (mo->momx > MAXMOVE)
mo->momx = MAXMOVE;
else if (mo->momx < -MAXMOVE)
mo->momx = -MAXMOVE;
if (mo->momy > MAXMOVE)
mo->momy = MAXMOVE;
else if (mo->momy < -MAXMOVE)
mo->momy = -MAXMOVE;
xmove = mo->momx;
ymove = mo->momy;
do
{
fixed_t ptryx, ptryy;
// killough 8/9/98: fix bug in original Doom source:
// Large negative displacements were never considered.
// This explains the tendency for Mancubus fireballs
// to pass through walls.
// CPhipps - compatibility optioned
if (xmove > MAXMOVE/2 || ymove > MAXMOVE/2 || ((xmove < -MAXMOVE/2 || ymove < -MAXMOVE/2)))
{
ptryx = mo->x + xmove/2;
ptryy = mo->y + ymove/2;
xmove >>= 1;
ymove >>= 1;
}
else
{
ptryx = mo->x + xmove;
ptryy = mo->y + ymove;
xmove = ymove = 0;
}
if (!P_TryMove (mo, ptryx, ptryy))
{
// blocked move
if (player) // try to slide along it
P_SlideMove (mo);
else
{
if (mo->flags & MF_MISSILE)
{
// explode a missile
if (_g_ceilingline)
{
const sector_t __far* ceilingBackSector = LN_BACKSECTOR(_g_ceilingline);
if(ceilingBackSector && ceilingBackSector->ceilingpic == skyflatnum)
{
if (mo->z > ceilingBackSector->ceilingheight)
{
// Hack to prevent missiles exploding
// against the sky.
// Does not handle sky floors.
P_RemoveMobj (mo);
return;
}
}
}
P_ExplodeMissile (mo);
}
else // whatever else it is, it is now standing still in (x,y)
{
mo->momx = mo->momy = 0;
}
}
}
} while (xmove || ymove);
// slow down
/* no friction for missiles or skulls ever, no friction when airborne */
if (mo->flags & MF_MISSILE || mo->z > mo->floorz)
return;
/* killough 8/11/98: add bouncers
* killough 9/15/98: add objects falling off ledges
* killough 11/98: only include bouncers hanging off ledges
*/
if ((mo->flags & MF_CORPSE) &&
(mo->momx > FRACUNIT/4 || mo->momx < -FRACUNIT/4 ||
mo->momy > FRACUNIT/4 || mo->momy < -FRACUNIT/4) &&
mo->floorz != mo->subsector->sector->floorheight)
return; // do not stop sliding if halfway off a step with some momentum
// killough 11/98:
// Stop voodoo dolls that have come to rest, despite any
// moving corresponding player, except in old demos:
if (mo->momx > -STOPSPEED && mo->momx < STOPSPEED &&
mo->momy > -STOPSPEED && mo->momy < STOPSPEED &&
(!player || !(player->cmd.forwardmove | player->cmd.sidemove) ||
(player->mo != mo)))
{
// if in a walking frame, stop moving
// killough 10/98:
// Don't affect main player when voodoo dolls stop, except in old demos:
if (player && (uint16_t)(player->mo->state - states - S_PLAY_RUN1) < 4
&& (player->mo == mo))
P_SetMobjState(player->mo, S_PLAY);
mo->momx = mo->momy = 0;
/* killough 10/98: kill any bobbing momentum too (except in voodoo dolls)
* cph - DEMOSYNC - needs compatibility check?
*/
if (player && player->mo == mo)
player->momx = player->momy = 0;
}
else
{
/* phares 3/17/98
*
* Friction will have been adjusted by friction thinkers for
* icy or muddy floors. Otherwise it was never touched and
* remained set at ORIG_FRICTION
*
* killough 8/28/98: removed inefficient thinker algorithm,
* instead using touching_sectorlist in P_GetFriction() to
* determine friction (and thus only when it is needed).
*
* killough 10/98: changed to work with new bobbing method.
* Reducing player momentum is no longer needed to reduce
* bobbing, so ice works much better now.
*
* cph - DEMOSYNC - need old code for Boom demos?
*/
mo->momx = FixedMul32OrigFriction(mo->momx);
mo->momy = FixedMul32OrigFriction(mo->momy);
/* killough 10/98: Always decrease player bobbing by ORIG_FRICTION.
* This prevents problems with bobbing on ice, where it was not being
* reduced fast enough, leading to all sorts of kludges being developed.
*/
if (player && player->mo == mo) /* Not voodoo dolls */
{
player->momx = FixedMul32OrigFriction(player->momx);
player->momy = FixedMul32OrigFriction(player->momy);
}
}
}
//
// P_ZMovement
//
// Attempt vertical movement.
#define GRAVITY FRACUNIT
static void P_ZMovement(mobj_t __far* mo)
{
// check for smooth step up
if (P_MobjIsPlayer(mo) &&
P_MobjIsPlayer(mo)->mo == mo && // killough 5/12/98: exclude voodoo dolls
mo->z < mo->floorz)
{
P_MobjIsPlayer(mo)->viewheight -= mo->floorz-mo->z;
P_MobjIsPlayer(mo)->deltaviewheight = (VIEWHEIGHT - P_MobjIsPlayer(mo)->viewheight)>>3;
}
// adjust altitude
mo->z += mo->momz;
// clip movement
if (mo->z <= mo->floorz)
{
// hit the floor
if (mo->momz < 0)
{
if (P_MobjIsPlayer(mo) && /* killough 5/12/98: exclude voodoo dolls */
P_MobjIsPlayer(mo)->mo == mo && mo->momz < -GRAVITY*8)
{
// Squat down.
// Decrease viewheight for a moment
// after hitting the ground (hard),
// and utter appropriate sound.
P_MobjIsPlayer(mo)->deltaviewheight = mo->momz>>3;
if (mo->health > 0) /* cph - prevent "oof" when dead */
S_StartSound (mo, sfx_oof);
}
mo->momz = 0;
}
mo->z = mo->floorz;
if ( (mo->flags & MF_MISSILE) && !(mo->flags & MF_NOCLIP) )
{
P_ExplodeMissile (mo);
return;
}
}
else // still above the floor // phares
if (!(mo->flags & MF_NOGRAVITY))
{
if (!mo->momz)
mo->momz = -GRAVITY;
mo->momz -= GRAVITY;
}
if (mo->z + mo->height > mo->ceilingz)
{
// hit the ceiling
if (mo->momz > 0)
mo->momz = 0;
mo->z = mo->ceilingz - mo->height;
if ( (mo->flags & MF_MISSILE) && !(mo->flags & MF_NOCLIP) )
{
P_ExplodeMissile (mo);
return;
}
}
}
//
// P_NightmareRespawn
//
static void P_NightmareRespawn(mobj_t __far* mobj)
{
fixed_t x;
fixed_t y;
subsector_t __far* ss;
mobj_t __far* mo;
/* haleyjd: stupid nightmare respawning bug fix
*
* 08/09/00: compatibility added, time to ramble :)
* This fixes the notorious nightmare respawning bug that causes monsters
* that didn't spawn at level startup to respawn at the point (0,0)
* regardless of that point's nature. SMMU and Eternity need this for
* script-spawned things like Halif Swordsmythe, as well.
*
* cph - copied from eternity, except comp_respawnfix becomes comp_respawn
* and the logic is reversed (i.e. like the rest of comp_ it *disables*
* the fix)
*/
//ZLB: Everything respawns at its death point.
//The spawnpoint is removed from the mobj.
x = mobj->x;
y = mobj->y;
if(!x && !y)
{
return;
}
// something is occupying its position?
if (!P_CheckPosition (mobj, x, y) )
return; // no respwan
// spawn a teleport fog at old spot
// because of removal of the body?
mo = P_SpawnMobj (mobj->x,
mobj->y,
mobj->subsector->sector->floorheight,
MT_TFOG);
// initiate teleport sound
S_StartSound (mo, sfx_telept);
// spawn a teleport fog at the new spot
ss = R_PointInSubsector (x,y);
mo = P_SpawnMobj (x, y, ss->sector->floorheight , MT_TFOG);
S_StartSound (mo, sfx_telept);
// spawn the new monster
// inherit attributes from deceased one
mo = P_SpawnMobj(x, y, ONFLOORZ, mobj->type);
mo->angle = mobj->angle;
/* killough 11/98: transfer friendliness from deceased */
mo->flags = (mo->flags & ~MF_FRIEND) | (mobj->flags & MF_FRIEND);
mo->reactiontime = 18;
// remove the old monster,
P_RemoveMobj (mobj);
}
void P_MobjThinker (mobj_t __far* mobj)
{
// momentum movement
if (mobj->momx | mobj->momy)
{
P_XYMovement(mobj);
if (mobj->thinker.function != P_MobjThinker) // cph - Must've been removed
return; // killough - mobj was removed
}
if (mobj->z != mobj->floorz || mobj->momz)
{
P_ZMovement(mobj);
if (mobj->thinker.function != P_MobjThinker) // cph - Must've been removed
return; // killough - mobj was removed
}
// cycle through states,
// calling action functions at transitions
if (mobj->tics != -1)
{
mobj->tics--;
// you can cycle through multiple states in a tic
if (!mobj->tics)
if (!P_SetMobjState (mobj, mobj->state->nextstate) )
return; // freed itself
}
else
{
// check for nightmare respawn
if (! (mobj->flags & MF_COUNTKILL) )
return;
if (!_g_respawnmonsters)
return;
mobj->movecount++;
if (mobj->movecount < 12*35)
return;
if (_g_leveltime & 31)
return;
if (P_Random () > 4)
return;
P_NightmareRespawn (mobj);
}
}
//Thinker function for stuff that doesn't need to do anything
//interesting.
//Just cycles through the states. Allows sprite animation to work.
static void P_MobjBrainlessThinker(mobj_t __far* mobj)
{
// cycle through states,
// calling action functions at transitions
if (mobj->tics != -1)
{
mobj->tics--;
// you can cycle through multiple states in a tic
if (!mobj->tics)
P_SetMobjState (mobj, mobj->state->nextstate);
}
}
static think_t P_ThinkerFunctionForType(mobjtype_t type, mobj_t __far* mobj)
{
//Full thinking ability.
if(type < MT_MISC0)
return P_MobjThinker;
//Just state cycles.
if(mobj->tics != -1)
return P_MobjBrainlessThinker;
//No thinking at all.
return NULL;
}
//
// P_SpawnMobj
//
static mobj_t __far* P_NewMobj()
{
mobj_t __far* mobj = NULL;
for(int16_t i = _g_thingPoolSize-1; i >= 0; i--)
{
if(_g_thingPool[i].type == MT_NOTHING)
{
mobj = &_g_thingPool[i];
_fmemset (mobj, 0, sizeof (*mobj));
mobj->flags = MF_POOLED;
return mobj;
}
}
if(mobj == NULL)
{
mobj = Z_MallocLevel(sizeof(*mobj), NULL);
_fmemset (mobj, 0, sizeof (*mobj));
}
return mobj;
}
mobj_t __far* P_SpawnMobj(fixed_t x,fixed_t y,fixed_t z,mobjtype_t type)
{
const state_t* st;
const mobjinfo_t* info;
mobj_t __far* mobj = P_NewMobj();
info = &mobjinfo[type];
mobj->type = type;
mobj->x = x;
mobj->y = y;
mobj->radius = info->radius;
mobj->height = info->height; // phares
mobj->flags |= info->flags;
if (type == MT_PLAYER) // Except in old demos, players
mobj->flags |= MF_FRIEND; // are always friends.
mobj->health = info->spawnhealth;
if (_g_gameskill != sk_nightmare)
mobj->reactiontime = info->reactiontime;
P_Random(); // only to call random for compatibiltiy
// do not set the state with P_SetMobjState,
// because action routines can not be called yet
st = &states[info->spawnstate];
mobj->state = st;
mobj->tics = st->tics;
mobj->sprite = st->sprite;
mobj->frame = st->frame;
mobj->touching_sectorlist = NULL; // NULL head of sector list // phares 3/13/98
// set subsector and/or block links
P_SetThingPosition (mobj);
mobj->dropoffz = /* killough 11/98: for tracking dropoffs */
mobj->floorz = mobj->subsector->sector->floorheight;
mobj->ceilingz = mobj->subsector->sector->ceilingheight;
mobj->z = z == ONFLOORZ ? mobj->floorz : z == ONCEILINGZ ?
mobj->ceilingz - mobj->height : z;
mobj->thinker.function = P_ThinkerFunctionForType(type, mobj);
mobj->target = mobj->tracer = mobj->lastenemy = NULL;
P_AddThinker (&mobj->thinker);
if (!((mobj->flags ^ MF_COUNTKILL) & (MF_FRIEND | MF_COUNTKILL)))
_g_totallive++;
return mobj;
}
//
// P_RemoveMobj
//
void P_RemoveMobj(mobj_t __far* mobj)
{
P_UnsetThingPosition (mobj);
// Delete all nodes on the current sector_list phares 3/16/98
P_DelSeclist();
// stop any playing sound
S_StopSound (mobj);
// killough 11/98:
//
// Remove any references to other mobjs.
//
// Older demos might depend on the fields being left alone, however,
// if multiple thinkers reference each other indirectly before the
// end of the current tic.
// CPhipps - only leave dead references in old demos; I hope lxdoom_1 level
// demos are rare and don't rely on this. I hope.
if (!_g_demoplayback)
{
mobj->target = NULL;
mobj->tracer = NULL;
mobj->lastenemy = NULL;
}
// free block
P_RemoveThing (mobj);
}
/*
* P_FindDoomedNum
*
* Finds a mobj type with a matching doomednum
*
*/
static PUREFUNC int16_t P_FindDoomedNum(int16_t type)
{
// find which type to spawn
for (int16_t i = 0; i < NUMMOBJTYPES; i++)
{
if (type == mobjinfo[i].doomednum)
return i;
}
I_Error("P_FindDoomedNum: unknown thing %i", type);
}
//
// P_SpawnPlayer
// Called when a player is spawned on the level.
// Most of the player structure stays unchanged
// between levels.
//
static void P_SpawnPlayer(int16_t playerx, int16_t playery, int8_t playerangle)
{
player_t* p;
mobj_t __far* mobj;
p = &_g_player;
if (p->playerstate == PST_REBORN)
G_PlayerReborn ();
fixed_t x = ((int32_t)playerx) << FRACBITS;
fixed_t y = ((int32_t)playery) << FRACBITS;
fixed_t z = ONFLOORZ;
mobj = P_SpawnMobj (x,y,z, MT_PLAYER);
// set color translations for player sprites
mobj->angle = ANG45 * playerangle;
mobj->health = p->health;
p->mo = mobj;
p->playerstate = PST_LIVE;
p->refire = 0;
p->message = NULL;
p->damagecount = 0;
p->bonuscount = 0;
p->extralight = 0;
p->fixedcolormap = 0;
p->viewheight = VIEWHEIGHT;
p->momx = p->momy = 0; // killough 10/98: initialize bobbing to 0.
// setup gun psprite
P_SetupPsprites (p);