/
p_plat.c
576 lines (502 loc) · 15.5 KB
/
p_plat.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
/**\file
*\section License
* License: GPL
* Online License Link: http://www.gnu.org/licenses/gpl.html
*
*\author Copyright © 2003-2009 Jaakko Keränen <jaakko.keranen@iki.fi>
*\author Copyright © 2005-2009 Daniel Swanson <danij@dengine.net>
*\author Copyright © 2006 Martin Eyre <martineyre@btinternet.com>
*\author Copyright © 2003-2005 Samuel Villarreal <svkaiser@gmail.com>
*\author Copyright © 1999 by Chi Hoang, Lee Killough, Jim Flynn, Rand Phares, Ty Halderman (PrBoom 2.2.6)
*\author Copyright © 1999-2000 by Jess Haas, Nicolas Kalkhof, Colin Phipps, Florian Schulze (PrBoom 2.2.6)
*\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_plats.c : Elevators and platforms, raising/lowering.
*/
// HEADER FILES ------------------------------------------------------------
#if __JDOOM__
# include "jdoom.h"
#elif __JDOOM64__
# include "jdoom64.h"
#elif __JHERETIC__
# include "jheretic.h"
#elif __JHEXEN__
# include "jhexen.h"
#endif
#include "dmu_lib.h"
#include "p_mapspec.h"
#include "p_tick.h"
#include "p_plat.h"
// MACROS ------------------------------------------------------------------
// Sounds played by the platforms when changing state or moving.
// jHexen uses sound sequences, so it's are defined as 'SFX_NONE'.
#if __JDOOM__
# define SFX_PLATFORMSTART (SFX_PSTART)
# define SFX_PLATFORMMOVE (SFX_STNMOV)
# define SFX_PLATFORMSTOP (SFX_PSTOP)
#elif __JDOOM64__
# define SFX_PLATFORMSTART (SFX_PSTART)
# define SFX_PLATFORMMOVE (SFX_STNMOV)
# define SFX_PLATFORMSTOP (SFX_PSTOP)
#elif __JHERETIC__
# define SFX_PLATFORMSTART (SFX_PSTART)
# define SFX_PLATFORMMOVE (SFX_STNMOV)
# define SFX_PLATFORMSTOP (SFX_PSTOP)
#elif __JHEXEN__
# define SFX_PLATFORMSTART (SFX_NONE)
# define SFX_PLATFORMMOVE (SFX_NONE)
# define SFX_PLATFORMSTOP (SFX_NONE)
#endif
// TYPES -------------------------------------------------------------------
// EXTERNAL FUNCTION PROTOTYPES --------------------------------------------
// PUBLIC FUNCTION PROTOTYPES ----------------------------------------------
// PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
// EXTERNAL DATA DECLARATIONS ----------------------------------------------
// PUBLIC DATA DEFINITIONS -------------------------------------------------
// PRIVATE DATA DEFINITIONS ------------------------------------------------
// CODE --------------------------------------------------------------------
/**
* Called when a moving plat needs to be removed.
*
* @param plat Ptr to the plat to remove.
*/
static void stopPlat(plat_t* plat)
{
P_ToXSector(plat->sector)->specialData = NULL;
#if __JHEXEN__
P_TagFinished(P_ToXSector(plat->sector)->tag);
#endif
DD_ThinkerRemove(&plat->thinker);
}
/**
* Move a plat up and down.
*
* @param plat Ptr to the plat to be moved.
*/
void T_PlatRaise(plat_t* plat)
{
result_e res;
switch(plat->state)
{
case PS_UP:
res = T_MovePlane(plat->sector, plat->speed, plat->high,
plat->crush, 0, 1);
// Play a "while-moving" sound?
#if __JHERETIC__
if(!(mapTime & 31))
S_SectorSound(plat->sector, SORG_FLOOR, SFX_PLATFORMMOVE);
#endif
#if __JDOOM__ || __JDOOM64__
if(plat->type == PT_RAISEANDCHANGE ||
plat->type == PT_RAISETONEARESTANDCHANGE)
{
if(!(mapTime & 7))
S_SectorSound(plat->sector, SORG_FLOOR, SFX_PLATFORMMOVE);
}
#endif
if(res == crushed && (!plat->crush))
{
plat->count = plat->wait;
plat->state = PS_DOWN;
#if __JHEXEN__
SN_StartSequenceInSec(plat->sector, SEQ_PLATFORM);
#else
# if __JDOOM64__
if(plat->type != PT_DOWNWAITUPDOOR) // jd64 added test
# endif
S_SectorSound(plat->sector, SORG_FLOOR, SFX_PLATFORMSTART);
#endif
}
else
{
if(res == pastdest)
{
plat->count = plat->wait;
plat->state = PS_WAIT;
#if __JHEXEN__
SN_StopSequenceInSec(plat->sector);
#else
S_SectorSound(plat->sector, SORG_FLOOR, SFX_PLATFORMSTOP);
#endif
switch(plat->type)
{
case PT_DOWNWAITUPSTAY:
#if __JHEXEN__
case PT_DOWNBYVALUEWAITUPSTAY:
#else
# if !__JHERETIC__
case PT_DOWNWAITUPSTAYBLAZE:
case PT_RAISETONEARESTANDCHANGE:
# endif
# if __JDOOM64__
case PT_DOWNWAITUPPLUS16STAYBLAZE: // jd64
case PT_DOWNWAITUPDOOR: // jd64
# endif
case PT_RAISEANDCHANGE:
#endif
stopPlat(plat);
break;
default:
break;
}
}
}
break;
case PS_DOWN:
res =
T_MovePlane(plat->sector, plat->speed, plat->low, false, 0, -1);
if(res == pastdest)
{
plat->count = plat->wait;
plat->state = PS_WAIT;
#if __JHEXEN__ || __JDOOM64__
switch(plat->type)
{
# if __JHEXEN__
case PT_UPBYVALUEWAITDOWNSTAY:
# endif
case PT_UPWAITDOWNSTAY:
stopPlat(plat);
break;
default:
break;
}
#endif
#if __JHEXEN__
SN_StopSequenceInSec(plat->sector);
#else
S_SectorSound(plat->sector, SORG_FLOOR, SFX_PLATFORMSTOP);
#endif
}
else
{
// Play a "while-moving" sound?
#if __JHERETIC__
if(!(mapTime & 31))
S_SectorSound(plat->sector, SORG_FLOOR, SFX_PLATFORMMOVE);
#endif
}
break;
case PS_WAIT:
if(!--plat->count)
{
if(P_GetFloatp(plat->sector, DMU_FLOOR_HEIGHT) == plat->low)
plat->state = PS_UP;
else
plat->state = PS_DOWN;
#if __JHEXEN__
SN_StartSequenceInSec(plat->sector, SEQ_PLATFORM);
#else
S_SectorSound(plat->sector, SORG_FLOOR, SFX_PLATFORMSTART);
#endif
}
break;
default:
break;
}
}
#if __JHEXEN__
static int doPlat(linedef_t *line, int tag, byte *args, plattype_e type,
int amount)
#else
static int doPlat(linedef_t *line, int tag, plattype_e type, int amount)
#endif
{
int rtn = 0;
float floorHeight;
plat_t *plat;
sector_t *sec = NULL;
#if !__JHEXEN__
sector_t *frontSector = P_GetPtrp(line, DMU_FRONT_SECTOR);
#endif
xsector_t *xsec;
iterlist_t *list;
list = P_GetSectorIterListForTag(tag, false);
if(!list)
return rtn;
P_IterListResetIterator(list, true);
while((sec = P_IterListIterator(list)) != NULL)
{
xsec = P_ToXSector(sec);
if(xsec->specialData)
continue;
// Find lowest & highest floors around sector
rtn = 1;
plat = Z_Calloc(sizeof(*plat), PU_MAP, 0);
plat->thinker.function = T_PlatRaise;
DD_ThinkerAdd(&plat->thinker);
plat->type = type;
plat->sector = sec;
xsec->specialData = plat;
plat->crush = false;
plat->tag = tag;
#if __JHEXEN__
plat->speed = (float) args[1] * (1.0 / 8);
#endif
floorHeight = P_GetFloatp(sec, DMU_FLOOR_HEIGHT);
switch(type)
{
#if !__JHEXEN__
case PT_RAISETONEARESTANDCHANGE:
plat->speed = PLATSPEED * .5;
P_SetPtrp(sec, DMU_FLOOR_MATERIAL,
P_GetPtrp(frontSector, DMU_FLOOR_MATERIAL));
{
float nextFloor;
if(P_FindSectorSurroundingNextHighestFloor(sec, floorHeight, &nextFloor))
plat->high = nextFloor;
else
plat->high = floorHeight;
}
plat->wait = 0;
plat->state = PS_UP;
// No more damage if applicable.
xsec->special = 0;
S_SectorSound(sec, SORG_FLOOR, SFX_PLATFORMMOVE);
break;
case PT_RAISEANDCHANGE:
plat->speed = PLATSPEED * .5;
P_SetPtrp(sec, DMU_FLOOR_MATERIAL,
P_GetPtrp(frontSector, DMU_FLOOR_MATERIAL));
plat->high = floorHeight + amount;
plat->wait = 0;
plat->state = PS_UP;
S_SectorSound(sec, SORG_FLOOR, SFX_PLATFORMMOVE);
break;
#endif
case PT_DOWNWAITUPSTAY:
P_FindSectorSurroundingLowestFloor(sec,
P_GetFloatp(sec, DMU_FLOOR_HEIGHT), &plat->low);
#if __JHEXEN__
plat->low += 8;
#else
plat->speed = PLATSPEED * 4;
#endif
if(plat->low > floorHeight)
plat->low = floorHeight;
plat->high = floorHeight;
plat->state = PS_DOWN;
#if __JHEXEN__
plat->wait = (int) args[2];
#else
plat->wait = PLATWAIT * TICSPERSEC;
#endif
#if !__JHEXEN__
S_SectorSound(sec, SORG_FLOOR, SFX_PLATFORMSTART);
#endif
break;
#if __JDOOM64__ || __JHEXEN__
case PT_UPWAITDOWNSTAY:
P_FindSectorSurroundingHighestFloor(sec, -500, &plat->high);
if(plat->high < floorHeight)
plat->high = floorHeight;
plat->low = floorHeight;
plat->state = PS_UP;
# if __JHEXEN__
plat->wait = (int) args[2];
# else
plat->wait = PLATWAIT * TICSPERSEC;
# endif
# if __JDOOM64__
plat->speed = PLATSPEED * 8;
S_SectorSound(sec, SORG_FLOOR, SFX_PLATFORMSTART);
# endif
break;
#endif
#if __JDOOM64__
case PT_DOWNWAITUPDOOR: // jd64
plat->speed = PLATSPEED * 8;
P_FindSectorSurroundingLowestFloor(sec,
P_GetFloatp(sec, DMU_FLOOR_HEIGHT), &plat->low);
if(plat->low > floorHeight)
plat->low = floorHeight;
if(plat->low != floorHeight)
plat->low += 6;
plat->high = floorHeight;
plat->wait = 50 * PLATWAIT;
plat->state = PS_DOWN;
break;
#endif
#if __JHEXEN__
case PT_DOWNBYVALUEWAITUPSTAY:
plat->low = floorHeight - (float) args[3] * 8;
if(plat->low > floorHeight)
plat->low = floorHeight;
plat->high = floorHeight;
plat->wait = (int) args[2];
plat->state = PS_DOWN;
break;
case PT_UPBYVALUEWAITDOWNSTAY:
plat->high = floorHeight + (float) args[3] * 8;
if(plat->high < floorHeight)
plat->high = floorHeight;
plat->low = floorHeight;
plat->wait = (int) args[2];
plat->state = PS_UP;
break;
#endif
#if __JDOOM__ || __JDOOM64__
case PT_DOWNWAITUPSTAYBLAZE:
plat->speed = PLATSPEED * 8;
P_FindSectorSurroundingLowestFloor(sec,
P_GetFloatp(sec, DMU_FLOOR_HEIGHT), &plat->low);
if(plat->low > floorHeight)
plat->low = floorHeight;
plat->high = floorHeight;
plat->wait = PLATWAIT * TICSPERSEC;
plat->state = PS_DOWN;
S_SectorSound(sec, SORG_FLOOR, SFX_PLATFORMSTART);
break;
#endif
case PT_PERPETUALRAISE:
P_FindSectorSurroundingLowestFloor(sec,
P_GetFloatp(sec, DMU_FLOOR_HEIGHT), &plat->low);
#if __JHEXEN__
plat->low += 8;
#else
plat->speed = PLATSPEED;
#endif
if(plat->low > floorHeight)
plat->low = floorHeight;
P_FindSectorSurroundingHighestFloor(sec, -500, &plat->high);
if(plat->high < floorHeight)
plat->high = floorHeight;
plat->state = P_Random() & 1;
#if __JHEXEN__
plat->wait = (int) args[2];
#else
plat->wait = PLATWAIT * TICSPERSEC;
#endif
#if !__JHEXEN__
S_SectorSound(sec, SORG_FLOOR, SFX_PLATFORMSTART);
#endif
break;
default:
break;
}
#if __JHEXEN__
SN_StartSequenceInSec(plat->sector, SEQ_PLATFORM);
#endif
}
return rtn;
}
/**
* Do Platforms.
*
* @param amount: is only used for SOME platforms.
*/
#if __JHEXEN__
int EV_DoPlat(linedef_t *line, byte *args, plattype_e type, int amount)
#else
int EV_DoPlat(linedef_t *line, plattype_e type, int amount)
#endif
{
#if __JHEXEN__
return doPlat(line, (int) args[0], args, type, amount);
#else
int rtn = 0;
xline_t* xline = P_ToXLine(line);
// Activate all <type> plats that are in stasis.
switch(type)
{
case PT_PERPETUALRAISE:
rtn = P_PlatActivate(xline->tag);
break;
default:
break;
}
return doPlat(line, xline->tag, type, amount) || rtn;
#endif
}
#if !__JHEXEN__
typedef struct {
short tag;
int count;
} activateplatparams_t;
static boolean activatePlat(thinker_t* th, void* context)
{
plat_t* plat = (plat_t*) th;
activateplatparams_t* params = (activateplatparams_t*) context;
if(plat->tag == (int) params->tag && plat->thinker.inStasis)
{
plat->state = plat->oldState;
DD_ThinkerSetStasis(&plat->thinker, false);
params->count++;
}
return true; // Contiue iteration.
}
/**
* Activate a plat that has been put in stasis
* (stopped perpetual floor, instant floor/ceil toggle)
*
* @param tag Tag of plats that should be reactivated.
*/
int P_PlatActivate(short tag)
{
activateplatparams_t params;
params.tag = tag;
params.count = 0;
DD_IterateThinkers(T_PlatRaise, activatePlat, ¶ms);
return params.count;
}
#endif
typedef struct {
short tag;
int count;
} deactivateplatparams_t;
static boolean deactivatePlat(thinker_t* th, void* context)
{
plat_t* plat = (plat_t*) th;
deactivateplatparams_t* params = (deactivateplatparams_t*) context;
#if __JHEXEN__
// For THE one with the tag.
if(plat->tag == params->tag)
{
// Destroy it.
stopPlat(plat);
params->count++;
return false; // Stop iteration.
}
#else
// For one with the tag and not in stasis.
if(plat->tag == (int) params->tag && !plat->thinker.inStasis)
{
// Put it in stasis.
plat->oldState = plat->state;
DD_ThinkerSetStasis(&plat->thinker, true);
params->count++;
}
#endif
return true; // Continue iteration.
}
/**
* Handler for "stop perpetual floor" linedef type.
*
* @param tag Tag of plats to put into stasis.
*
* @return Number of plats put into stasis.
*/
int P_PlatDeactivate(short tag)
{
deactivateplatparams_t params;
params.tag = tag;
params.count = 0;
DD_IterateThinkers(T_PlatRaise, deactivatePlat, ¶ms);
return params.count;
}