Debian Codegraph @

prboom2/src/p_floor.c

raw
1 /* Emacs style mode select -*- C -*-
2 *-----------------------------------------------------------------------------
3 *
4 *
5 * PrBoom: a Doom port merged with LxDoom and LSDLDoom
6 * based on BOOM, a modified and improved DOOM engine
7 * Copyright (C) 1999 by
8 * id Software, Chi Hoang, Lee Killough, Jim Flynn, Rand Phares, Ty Halderman
9 * Copyright (C) 1999-2000 by
10 * Jess Haas, Nicolas Kalkhof, Colin Phipps, Florian Schulze
11 * Copyright 2005, 2006 by
12 * Florian Schulze, Colin Phipps, Neil Stevens, Andrey Budko
13 *
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 * 02111-1307, USA.
28 *
29 * DESCRIPTION:
30 * General plane mover and floor mover action routines
31 * Floor motion, pure changer types, raising stairs. donuts, elevators
32 *
33 *-----------------------------------------------------------------------------*/
34
35 #include "doomstat.h"
36 #include "r_main.h"
37 #include "p_map.h"
38 #include "p_spec.h"
39 #include "p_tick.h"
40 #include "s_sound.h"
41 #include "sounds.h"
42 #include "lprintf.h"
43 #include "g_overflow.h"
44 #include "e6y.h"//e6y
45
46 #include "dsda/id_list.h"
47 #include "dsda/map_format.h"
48
49 #include "hexen/p_acs.h"
50 #include "hexen/sn_sonix.h"
51
52 ///////////////////////////////////////////////////////////////////////
53 //
54 // Floor motion and Elevator action routines
55 //
56 ///////////////////////////////////////////////////////////////////////
57
58 //
59 // T_MoveFloorPlane()
60 //
61 // Move a floor plane and check for crushing. Called
62 // every tick by all actions that move floors.
63 //
64 // Passed the sector to move a plane in, the speed to move it at,
65 // the dest height it is to achieve, whether it crushes obstacles,
66 // and the direction up or down to move.
67 //
68 // Returns a result_e:
69 // ok - plane moved normally, has not achieved destination yet
70 // pastdest - plane moved normally and is now at destination height
71 // crushed - plane encountered an obstacle, is holding until removed
72 //
73 result_e T_MoveFloorPlane
74 ( sector_t* sector,
75 fixed_t speed,
76 fixed_t dest,
77 int crush,
78 int direction,
79 dboolean hexencrush )
80 {
81 dboolean flag;
82 fixed_t lastpos;
83 fixed_t destheight; //jff 02/04/98 used to keep floors from moving thru each other
84
85 if (V_IsOpenGLMode())
86 {
87 gld_UpdateSplitData(sector);
88 }
89
90 switch(direction)
91 {
92 case -1:
93 // Moving a floor down
94 if (sector->floorheight - speed < dest)
95 {
96 lastpos = sector->floorheight;
97 sector->floorheight = dest;
98 flag = P_CheckSector(sector,crush); //jff 3/19/98 use faster chk
99 if (flag == true)
100 {
101 sector->floorheight =lastpos;
102 P_CheckSector(sector,crush); //jff 3/19/98 use faster chk
103 }
104 return pastdest;
105 }
106 else
107 {
108 lastpos = sector->floorheight;
109 sector->floorheight -= speed;
110 flag = P_CheckSector(sector,crush); //jff 3/19/98 use faster chk
111 /* cph - make more compatible with original Doom, by
112 * reintroducing this code. This means floors can't lower
113 * if objects are stuck in the ceiling */
114 if ((flag == true) && comp[comp_floors]) {
115 sector->floorheight = lastpos;
116 P_ChangeSector(sector,crush);
117 return crushed;
118 }
119 }
120 break;
121
122 case 1:
123 // Moving a floor up
124 // jff 02/04/98 keep floor from moving thru ceilings
125 // jff 2/22/98 weaken check to demo_compatibility
126 destheight = (comp[comp_floors] || dest<sector->ceilingheight)?
127 dest : sector->ceilingheight;
128 if (sector->floorheight + speed > destheight)
129 {
130 lastpos = sector->floorheight;
131 sector->floorheight = destheight;
132 flag = P_CheckSector(sector,crush); //jff 3/19/98 use faster chk
133 if (flag == true)
134 {
135 sector->floorheight = lastpos;
136 P_CheckSector(sector,crush); //jff 3/19/98 use faster chk
137 }
138 return pastdest;
139 }
140 else
141 {
142 // crushing is possible
143 lastpos = sector->floorheight;
144 sector->floorheight += speed;
145 flag = P_CheckSector(sector,crush); //jff 3/19/98 use faster chk
146 if (flag == true)
147 {
148 /* jff 1/25/98 fix floor crusher */
149 if (!hexencrush && comp[comp_floors]) {
150
151 //e6y: warning about potential desynch
152 if (crush == STAIRS_UNINITIALIZED_CRUSH_FIELD_VALUE)
153 {
154 lprintf(LO_WARN, "T_MoveFloorPlane: Stairs which can potentially crush may lead to desynch in compatibility mode.\n");
155 lprintf(LO_WARN, " gametic: %d, sector: %d, complevel: %d\n", gametic, sector->iSectorID, compatibility_level);
156 }
157
158 if (crush >= 0)
159 return crushed;
160 }
161 sector->floorheight = lastpos;
162 P_CheckSector(sector,crush); //jff 3/19/98 use faster chk
163 return crushed;
164 }
165 }
166 break;
167 }
168
169 return ok;
170 }
171
172 //
173 // T_MoveFloor()
174 //
175 // Move a floor to it's destination (up or down).
176 // Called once per tick for each moving floor.
177 //
178 // Passed a floormove_t structure that contains all pertinent info about the
179 // move. See P_SPEC.H for fields.
180 // No return.
181 //
182 // jff 02/08/98 all cases with labels beginning with gen added to support
183 // generalized line type behaviors.
184
185 void T_MoveCompatibleFloor(floormove_t * floor)
186 {
187 result_e res;
188
189 // [RH] Handle resetting stairs
190 if (floor->type == floorBuildStair || floor->type == floorWaitStair)
191 {
192 if (floor->resetDelayCount)
193 {
194 floor->resetDelayCount--;
195 if (!floor->resetDelayCount)
196 {
197 floor->floordestheight = floor->resetHeight;
198 floor->direction = -floor->direction;
199 floor->type = floorResetStair;
200 floor->delayCount = 0;
201 }
202 }
203 if (floor->delayCount)
204 {
205 floor->delayCount--;
206 return;
207 }
208
209 if (floor->type == floorWaitStair)
210 return;
211 }
212
213 res = T_MoveFloorPlane
214 (
215 floor->sector,
216 floor->speed,
217 floor->floordestheight,
218 floor->crush,
219 floor->direction,
220 floor->hexencrush
221 );
222
223 if (floor->delayTotal && floor->type == floorBuildStair)
224 {
225 if (
226 (floor->direction == 1 && floor->sector->floorheight >= floor->stairsDelayHeight) ||
227 (floor->direction == -1 && floor->sector->floorheight <= floor->stairsDelayHeight)
228 )
229 {
230 floor->delayCount = floor->delayTotal;
231 floor->stairsDelayHeight += floor->stairsDelayHeightDelta;
232 }
233 }
234
235 if (!(leveltime&7)) // make the floormove sound
236 S_LoopSectorSound(floor->sector, g_sfx_stnmov, 8);
237
238 if (res == pastdest) // if destination height is reached
239 {
240 if (heretic && floor->type == buildStair)
241 {
242 S_StartSectorSound(floor->sector, heretic_sfx_pstop);
243 }
244
245 if (floor->type == floorBuildStair)
246 floor->type = floorWaitStair;
247
248 if (floor->type != floorWaitStair || !floor->resetDelayCount)
249 {
250 if (floor->direction == 1) // going up
251 {
252 switch(floor->type) // handle texture/type changes
253 {
254 case donutRaise:
255 case genFloorChgT:
256 case genFloorChg0:
257 P_TransferSpecial(floor->sector, &floor->newspecial);
258 //fall thru
259 case genFloorChg:
260 floor->sector->floorpic = floor->texture;
261 break;
262 default:
263 break;
264 }
265 }
266 else if (floor->direction == -1) // going down
267 {
268 switch(floor->type) // handle texture/type changes
269 {
270 case floorLowerAndChange:
271 case lowerAndChange:
272 case genFloorChgT:
273 case genFloorChg0:
274 P_TransferSpecial(floor->sector, &floor->newspecial);
275 //fall thru
276 case genFloorChg:
277 floor->sector->floorpic = floor->texture;
278 break;
279 default:
280 break;
281 }
282 }
283
284 floor->sector->floordata = NULL; //jff 2/22/98
285 P_RemoveThinker(&floor->thinker);//remove this floor from list of movers
286
287 //jff 2/26/98 implement stair retrigger lockout while still building
288 // note this only applies to the retriggerable generalized stairs
289
290 if (floor->sector->stairlock == -2) // if this sector is stairlocked
291 {
292 sector_t *sec = floor->sector;
293 sec->stairlock = -1; // thinker done, promote lock to -1
294
295 while (sec->prevsec != -1 && sectors[sec->prevsec].stairlock != -2)
296 sec = &sectors[sec->prevsec]; // search for a non-done thinker
297 if (sec->prevsec == -1) // if all thinkers previous are done
298 {
299 sec = floor->sector; // search forward
300 while (sec->nextsec != -1 && sectors[sec->nextsec].stairlock != -2)
301 sec = &sectors[sec->nextsec];
302 if (sec->nextsec == -1) // if all thinkers ahead are done too
303 {
304 while (sec->prevsec != -1) // clear all locks
305 {
306 sec->stairlock = 0;
307 sec = &sectors[sec->prevsec];
308 }
309 sec->stairlock = 0;
310 }
311 }
312 }
313
314 // Moving floors (but not plats) in versions <= v1.2 did not
315 // make floor stop sound
316 if (compatibility_level > doom_12_compatibility)
317 S_StartSectorSound(floor->sector, sfx_pstop);
318 }
319 }
320 }
321
322 void T_MoveHexenFloor(floormove_t * floor)
323 {
324 result_e res;
325
326 if (floor->resetDelayCount)
327 {
328 floor->resetDelayCount--;
329 if (!floor->resetDelayCount)
330 {
331 floor->floordestheight = floor->resetHeight;
332 floor->direction = -floor->direction;
333 floor->resetDelay = 0;
334 floor->delayCount = 0;
335 floor->delayTotal = 0;
336 }
337 }
338 if (floor->delayCount)
339 {
340 floor->delayCount--;
341 if (!floor->delayCount && floor->textureChange)
342 {
343 floor->sector->floorpic += floor->textureChange;
344 }
345 return;
346 }
347
348 res = T_MoveFloorPlane(floor->sector, floor->speed,
349 floor->floordestheight, floor->crush,
350 floor->direction, true);
351
352 if (floor->type == FLEV_RAISEBUILDSTEP)
353 {
354 if ((floor->direction == 1 && floor->sector->floorheight >=
355 floor->stairsDelayHeight) || (floor->direction == -1 &&
356 floor->sector->floorheight <=
357 floor->stairsDelayHeight))
358 {
359 floor->delayCount = floor->delayTotal;
360 floor->stairsDelayHeight += floor->stairsDelayHeightDelta;
361 }
362 }
363 if (res == pastdest)
364 {
365 SN_StopSequence((mobj_t *) & floor->sector->soundorg);
366 if (floor->delayTotal)
367 {
368 floor->delayTotal = 0;
369 }
370 if (floor->resetDelay)
371 {
372 return;
373 }
374 floor->sector->floordata = NULL;
375 if (floor->textureChange)
376 {
377 floor->sector->floorpic -= floor->textureChange;
378 }
379 P_TagFinished(floor->sector->tag);
380 P_RemoveThinker(&floor->thinker);
381 }
382 }
383
384 void T_MoveFloor(floormove_t* floor)
385 {
386 map_format.t_move_floor(floor);
387 }
388
389 //
390 // T_MoveElevator()
391 //
392 // Move an elevator to it's destination (up or down)
393 // Called once per tick for each moving floor.
394 //
395 // Passed an elevator_t structure that contains all pertinent info about the
396 // move. See P_SPEC.H for fields.
397 // No return.
398 //
399 // jff 02/22/98 added to support parallel floor/ceiling motion
400 //
401 void T_MoveElevator(elevator_t* elevator)
402 {
403 result_e res;
404
405 if (elevator->direction<0) // moving down
406 {
407 res = T_MoveCeilingPlane //jff 4/7/98 reverse order of ceiling/floor
408 (
409 elevator->sector,
410 elevator->speed,
411 elevator->ceilingdestheight,
412 NO_CRUSH,
413 elevator->direction,
414 false
415 );
416 if (res==ok || res==pastdest) // jff 4/7/98 don't move ceil if blocked
417 T_MoveFloorPlane
418 (
419 elevator->sector,
420 elevator->speed,
421 elevator->floordestheight,
422 NO_CRUSH,
423 elevator->direction,
424 false
425 );
426 }
427 else // up
428 {
429 res = T_MoveFloorPlane //jff 4/7/98 reverse order of ceiling/floor
430 (
431 elevator->sector,
432 elevator->speed,
433 elevator->floordestheight,
434 NO_CRUSH,
435 elevator->direction,
436 false
437 );
438 if (res==ok || res==pastdest) // jff 4/7/98 don't move floor if blocked
439 T_MoveCeilingPlane
440 (
441 elevator->sector,
442 elevator->speed,
443 elevator->ceilingdestheight,
444 NO_CRUSH,
445 elevator->direction,
446 false
447 );
448 }
449
450 // make floor move sound
451 if (!(leveltime&7))
452 S_LoopSectorSound(elevator->sector, sfx_stnmov, 8);
453
454 if (res == pastdest) // if destination height acheived
455 {
456 elevator->sector->floordata = NULL; //jff 2/22/98
457 elevator->sector->ceilingdata = NULL; //jff 2/22/98
458 P_RemoveThinker(&elevator->thinker); // remove elevator from actives
459
460 // make floor stop sound
461 S_StartSectorSound(elevator->sector, sfx_pstop);
462 }
463 }
464
465 ///////////////////////////////////////////////////////////////////////
466 //
467 // Floor motion linedef handlers
468 //
469 ///////////////////////////////////////////////////////////////////////
470
471 //
472 // EV_DoFloor()
473 //
474 // Handle regular and extended floor types
475 //
476 // Passed the line that activated the floor and the type of floor motion
477 // Returns true if a thinker was created.
478 //
479 int EV_DoFloor
480 ( line_t* line,
481 floor_e floortype )
482 {
483 const int *id_p;
484 int rtn;
485 int i;
486 sector_t* sec;
487 floormove_t* floor;
488
489 rtn = 0;
490
491 // move all floors with the same tag as the linedef
492 FIND_SECTORS(id_p, line->tag)
493 {
494 sec = &sectors[*id_p];
495
496 // Don't start a second thinker on the same floor
497 if (P_FloorActive(sec)) //jff 2/23/98
498 continue;
499
500 // new floor thinker
501 rtn = 1;
502 floor = Z_MallocLevel (sizeof(*floor));
503 memset(floor, 0, sizeof(*floor));
504 P_AddThinker (&floor->thinker);
505 sec->floordata = floor; //jff 2/22/98
506 floor->thinker.function = T_MoveFloor;
507 floor->type = floortype;
508 floor->crush = NO_CRUSH;
509
510 // setup the thinker according to the linedef type
511 switch(floortype)
512 {
513 case lowerFloor:
514 floor->direction = -1;
515 floor->sector = sec;
516 floor->speed = FLOORSPEED;
517 floor->floordestheight = P_FindHighestFloorSurrounding(sec);
518 break;
519
520 //jff 02/03/30 support lowering floor by 24 absolute
521 case lowerFloor24:
522 floor->direction = -1;
523 floor->sector = sec;
524 floor->speed = FLOORSPEED;
525 floor->floordestheight = floor->sector->floorheight + 24 * FRACUNIT;
526 break;
527
528 //jff 02/03/30 support lowering floor by 32 absolute (fast)
529 case lowerFloor32Turbo:
530 floor->direction = -1;
531 floor->sector = sec;
532 floor->speed = FLOORSPEED*4;
533 floor->floordestheight = floor->sector->floorheight + 32 * FRACUNIT;
534 break;
535
536 case lowerFloorToLowest:
537 floor->direction = -1;
538 floor->sector = sec;
539 floor->speed = FLOORSPEED;
540 floor->floordestheight = P_FindLowestFloorSurrounding(sec);
541 break;
542
543 //jff 02/03/30 support lowering floor to next lowest floor
544 case lowerFloorToNearest:
545 floor->direction = -1;
546 floor->sector = sec;
547 floor->speed = FLOORSPEED;
548 floor->floordestheight =
549 P_FindNextLowestFloor(sec,floor->sector->floorheight);
550 break;
551
552 case turboLower:
553 floor->direction = -1;
554 floor->sector = sec;
555 floor->speed = FLOORSPEED * 4;
556 floor->floordestheight = P_FindHighestFloorSurrounding(sec);
557 if (compatibility_level == doom_12_compatibility ||
558 floor->floordestheight != sec->floorheight)
559 floor->floordestheight += 8*FRACUNIT;
560 break;
561
562 case raiseFloorCrush:
563 floor->crush = DOOM_CRUSH;
564 // fallthrough
565 case raiseFloor:
566 floor->direction = 1;
567 floor->sector = sec;
568 floor->speed = FLOORSPEED;
569 floor->floordestheight = P_FindLowestCeilingSurrounding(sec);
570 if (floor->floordestheight > sec->ceilingheight)
571 floor->floordestheight = sec->ceilingheight;
572 floor->floordestheight -= (8*FRACUNIT)*(floortype == raiseFloorCrush);
573 break;
574
575 case raiseFloorTurbo:
576 floor->direction = 1;
577 floor->sector = sec;
578 floor->speed = FLOORSPEED*4;
579 floor->floordestheight = P_FindNextHighestFloor(sec,sec->floorheight);
580 break;
581
582 case raiseFloorToNearest:
583 floor->direction = 1;
584 floor->sector = sec;
585 floor->speed = FLOORSPEED;
586 floor->floordestheight = P_FindNextHighestFloor(sec,sec->floorheight);
587 break;
588
589 case raiseFloor24:
590 floor->direction = 1;
591 floor->sector = sec;
592 floor->speed = FLOORSPEED;
593 floor->floordestheight = floor->sector->floorheight + 24 * FRACUNIT;
594 break;
595
596 // jff 2/03/30 support straight raise by 32 (fast)
597 case raiseFloor32Turbo:
598 floor->direction = 1;
599 floor->sector = sec;
600 floor->speed = FLOORSPEED*4;
601 floor->floordestheight = floor->sector->floorheight + 32 * FRACUNIT;
602 break;
603
604 case raiseFloor512:
605 floor->direction = 1;
606 floor->sector = sec;
607 floor->speed = FLOORSPEED;
608 floor->floordestheight = floor->sector->floorheight + 512 * FRACUNIT;
609 break;
610
611 case raiseFloor24AndChange:
612 floor->direction = 1;
613 floor->sector = sec;
614 floor->speed = FLOORSPEED;
615 floor->floordestheight = floor->sector->floorheight + 24 * FRACUNIT;
616 sec->floorpic = line->frontsector->floorpic;
617 P_CopySectorSpecial(sec, line->frontsector);
618 break;
619
620 case raiseToTexture:
621 {
622 int minsize = INT_MAX;
623 side_t* side;
624
625 /* jff 3/13/98 no ovf */
626 if (!comp[comp_model]) minsize = 32000<<FRACBITS;
627 floor->direction = 1;
628 floor->sector = sec;
629 floor->speed = FLOORSPEED;
630 for (i = 0; i < sec->linecount; i++)
631 {
632 if (twoSided (*id_p, i) )
633 {
634 side = getSide(*id_p,i,0);
635 // jff 8/14/98 don't scan texture 0, its not real
636 if (side->bottomtexture > 0 ||
637 (comp[comp_model] && !side->bottomtexture))
638 if (textureheight[side->bottomtexture] < minsize)
639 minsize = textureheight[side->bottomtexture];
640 side = getSide(*id_p,i,1);
641 // jff 8/14/98 don't scan texture 0, its not real
642 if (side->bottomtexture > 0 ||
643 (comp[comp_model] && !side->bottomtexture))
644 if (textureheight[side->bottomtexture] < minsize)
645 minsize = textureheight[side->bottomtexture];
646 }
647 }
648 if (comp[comp_model])
649 floor->floordestheight = floor->sector->floorheight + minsize;
650 else
651 {
652 floor->floordestheight =
653 (floor->sector->floorheight>>FRACBITS) + (minsize>>FRACBITS);
654 if (floor->floordestheight>32000)
655 floor->floordestheight = 32000; //jff 3/13/98 do not
656 floor->floordestheight<<=FRACBITS; // allow height overflow
657 }
658 }
659 break;
660
661 case lowerAndChange:
662 floor->direction = -1;
663 floor->sector = sec;
664 floor->speed = FLOORSPEED;
665 floor->floordestheight = P_FindLowestFloorSurrounding(sec);
666 floor->texture = sec->floorpic;
667
668 // jff 1/24/98 make sure floor->newspecial gets initialized
669 // in case no surrounding sector is at floordestheight
670 // --> should not affect compatibility <--
671 P_CopyTransferSpecial(&floor->newspecial, sec);
672
673 //jff 5/23/98 use model subroutine to unify fixes and handling
674 sec = P_FindModelFloorSector(floor->floordestheight,sec->iSectorID);
675 if (sec)
676 {
677 floor->texture = sec->floorpic;
678 P_CopyTransferSpecial(&floor->newspecial, sec);
679 }
680 break;
681 default:
682 break;
683 }
684 }
685 return rtn;
686 }
687
688 //
689 // EV_DoChange()
690 //
691 // Handle pure change types. These change floor texture and sector type
692 // by trigger or numeric model without moving the floor.
693 //
694 // The linedef causing the change and the type of change is passed
695 // Returns true if any sector changes
696 //
697 // jff 3/15/98 added to better support generalized sector types
698 //
699 int EV_DoChange
700 ( line_t* line,
701 change_e changetype,
702 int tag )
703 {
704 const int *id_p;
705 int rtn;
706 sector_t* sec;
707 sector_t* secm;
708
709 rtn = 0;
710 // change all sectors with the same tag as the linedef
711 FIND_SECTORS(id_p, tag)
712 {
713 sec = &sectors[*id_p];
714
715 rtn = 1;
716
717 // handle trigger or numeric change type
718 switch(changetype)
719 {
720 case trigChangeOnly:
721 if (line)
722 {
723 sec->floorpic = line->frontsector->floorpic;
724 P_CopySectorSpecial(sec, line->frontsector);
725 }
726 break;
727 case numChangeOnly:
728 secm = P_FindModelFloorSector(sec->floorheight,*id_p);
729 if (secm) // if no model, no change
730 {
731 sec->floorpic = secm->floorpic;
732 P_CopySectorSpecial(sec, secm);
733 }
734 break;
735 default:
736 break;
737 }
738 }
739 return rtn;
740 }
741
742 /*
743 * EV_BuildStairs()
744 *
745 * Handles staircase building. A sequence of sectors chosen by algorithm
746 * rise at a speed indicated to a height that increases by the stepsize
747 * each step.
748 *
749 * Passed the linedef triggering the stairs and the type of stair rise
750 * Returns true if any thinkers are created
751 *
752 * cph 2001/09/21 - compatibility nightmares again
753 * There are three different ways this function has, during its history, stepped
754 * through all the stairs to be triggered by the single switch
755 * - original Doom used a linear search, but failed to preserve
756 * the index of the previous sector found, so instead it would restart its
757 * linear search from the last sector of the previous staircase
758 * - cl11-13 with comp_stairs failed to emulate this with its chained hash search.
759 * It started following the hash chain from the last sector of the previous
760 * staircase, which would (probably) have the wrong tag, so it missed further stairs
761 * - Boom fixed the bug, cl11-13 without comp_stairs works right, and cl14+ works right
762 */
763
764 int EV_BuildStairs
765 ( line_t* line,
766 stair_e type )
767 {
768 /* cph 2001/09/22 - cleaned up this function to save my sanity. A separate
769 * outer loop index makes the logic much cleared, and local variables moved
770 * into the inner blocks helps too */
771 const int *id_p;
772 int rtn = 0;
773
774 //e6y
775 int oldsecnum;
776 sector_t* sec;
777
778 // start a stair at each sector tagged the same as the linedef
779 FIND_SECTORS(id_p, line->tag)
780 {
781 //e6y sector_t*
782 sec = &sectors[*id_p];
783
784 oldsecnum = *id_p;
785
786 // don't start a stair if the first step's floor is already moving
787 if (!P_FloorActive(sec)) { //jff 2/22/98
788 floormove_t* floor;
789 int texture, height;
790 fixed_t stairsize;
791 fixed_t speed;
792 int crush;
793 int ok;
794
795 // create new floor thinker for first step
796 rtn = 1;
797 floor = Z_MallocLevel (sizeof(*floor));
798 memset(floor, 0, sizeof(*floor));
799 P_AddThinker (&floor->thinker);
800 sec->floordata = floor;
801 floor->thinker.function = T_MoveFloor;
802 floor->direction = 1;
803 floor->sector = sec;
804 floor->type = buildStair; //jff 3/31/98 do not leave uninited
805 floor->crush = NO_CRUSH;
806 crush = floor->crush;
807
808 // set up the speed and stepsize according to the stairs type
809 switch(type)
810 {
811 default: // killough -- prevent compiler warning
812 case build8:
813 speed = FLOORSPEED/4;
814 stairsize = 8*FRACUNIT;
815 if (!demo_compatibility)
816 crush = NO_CRUSH; //jff 2/27/98 fix uninitialized crush field
817 // e6y
818 // Uninitialized crush field will not be equal to 0 or 1 (true)
819 // with high probability. So, initialize it with any other value
820 // There is no more desync on icarus.wad/ic29uv.lmp
821 // http://competn.doom2.net/pub/sda/i-o/icuvlmps.zip
822 // http://www.doomworld.com/idgames/index.php?id=5191
823 else
824 {
825 if (!prboom_comp[PC_UNINITIALIZE_CRUSH_FIELD_FOR_STAIRS].state)
826 crush = STAIRS_UNINITIALIZED_CRUSH_FIELD_VALUE;
827 }
828
829 break;
830 case turbo16:
831 speed = FLOORSPEED*4;
832 stairsize = 16*FRACUNIT;
833 if (!demo_compatibility)
834 crush = DOOM_CRUSH; //jff 2/27/98 fix uninitialized crush field
835 // e6y
836 // Uninitialized crush field will not be equal to 0 or 1 (true)
837 // with high probability. So, initialize it with any other value
838 else
839 {
840 if (!prboom_comp[PC_UNINITIALIZE_CRUSH_FIELD_FOR_STAIRS].state)
841 crush = STAIRS_UNINITIALIZED_CRUSH_FIELD_VALUE;
842 }
843
844 break;
845 case heretic_build8:
846 speed = FLOORSPEED;
847 stairsize = 8 * FRACUNIT;
848 crush = STAIRS_UNINITIALIZED_CRUSH_FIELD_VALUE; // heretic_note: I guess
849
850 break;
851 case heretic_turbo16:
852 speed = FLOORSPEED;
853 stairsize = 16 * FRACUNIT;
854 crush = STAIRS_UNINITIALIZED_CRUSH_FIELD_VALUE; // heretic_note: I guess
855
856 break;
857 }
858 floor->speed = speed;
859 floor->crush = crush;
860 height = sec->floorheight + stairsize;
861 floor->floordestheight = height;
862
863 texture = sec->floorpic;
864
865 // Find next sector to raise
866 // 1. Find 2-sided line with same sector side[0] (lowest numbered)
867 // 2. Other side is the next sector to raise
868 // 3. Unless already moving, or different texture, then stop building
869 do
870 {
871 int i;
872 ok = 0;
873
874 for (i = 0;i < sec->linecount;i++)
875 {
876 sector_t* tsec = (sec->lines[i])->frontsector;
877 int newsecnum;
878 if ( !((sec->lines[i])->flags & ML_TWOSIDED) )
879 continue;
880
881 newsecnum = tsec->iSectorID;
882
883 if (oldsecnum != newsecnum)
884 continue;
885
886 tsec = (sec->lines[i])->backsector;
887 if (!tsec) continue; //jff 5/7/98 if no backside, continue
888 newsecnum = tsec->iSectorID;
889
890 // if sector's floor is different texture, look for another
891 if (tsec->floorpic != texture)
892 continue;
893
894 /* jff 6/19/98 prevent double stepsize
895 * killough 10/98: intentionally left this way [MBF comment]
896 * cph 2001/02/06: stair bug fix should be controlled by comp_stairs,
897 * except if we're emulating MBF which perversly reverted the fix
898 */
899 if (comp[comp_stairs] || (compatibility_level == mbf_compatibility))
900 height += stairsize; // jff 6/28/98 change demo compatibility
901
902 // if sector's floor already moving, look for another
903 if (P_FloorActive(tsec)) //jff 2/22/98
904 continue;
905
906 /* cph - see comment above - do this iff we didn't do so above */
907 if (!comp[comp_stairs] && (compatibility_level != mbf_compatibility))
908 height += stairsize;
909
910 sec = tsec;
911 oldsecnum = newsecnum;
912
913 // create and initialize a thinker for the next step
914 floor = Z_MallocLevel (sizeof(*floor));
915 memset(floor, 0, sizeof(*floor));
916 P_AddThinker (&floor->thinker);
917
918 sec->floordata = floor; //jff 2/22/98
919 floor->thinker.function = T_MoveFloor;
920 floor->direction = 1;
921 floor->sector = sec;
922 floor->speed = speed;
923 floor->floordestheight = height;
924 floor->type = buildStair; //jff 3/31/98 do not leave uninited
925 floor->crush = crush; //jff 2/27/98 fix uninitialized crush field
926
927 ok = 1;
928 break;
929 }
930 } while(ok); // continue until no next step is found
931 }
932 /* killough 10/98: compatibility option */
933 if (comp[comp_stairs]) {
934 id_p = dsda_FindSectorsFromID(line->tag);
935
936 /* cph 2001/09/22 - emulate buggy MBF comp_stairs for demos, with logic
937 * reversed since we now have a separate outer loop index.
938 * DEMOSYNC - what about boom_compatibility_compatibility?
939 */
940 if (
941 compatibility_level >= mbf_compatibility &&
942 compatibility_level < prboom_3_compatibility
943 ) {
944 // Trash outer loop index
945 for (; *id_p >= 0; id_p++)
946 if (*id_p == oldsecnum || id_p[1] < 0)
947 break;
948 }
949 else {
950 /* cph 2001/09/22 - now the correct comp_stairs - Doom used a linear
951 * search from the last secnum, so we set that as a minimum value and do
952 * a fresh tag search
953 */
954 for (; *id_p >= 0; id_p++)
955 if (id_p[1] > oldsecnum || id_p[1] < 0)
956 break;
957 }
958 }
959 }
960 return rtn;
961 }
962
963 //
964 // EV_DoDonut()
965 //
966 // Handle donut function: lower pillar, raise surrounding pool, both to height,
967 // texture and type of the sector surrounding the pool.
968 //
969 // Passed the linedef that triggered the donut
970 // Returns whether a thinker was created
971 //
972
973 int P_SpawnDonut(int secnum, line_t *line, fixed_t pillarspeed, fixed_t slimespeed)
974 {
975 int i;
976 int rtn = 0;
977 sector_t *s1;
978 sector_t *s2;
979 sector_t *s3;
980 short s3_floorpic;
981 fixed_t s3_floorheight;
982 floormove_t *floor;
983
984 s1 = &sectors[secnum]; // s1 is pillar's sector
985
986 // do not start the donut if the pillar is already moving
987 if (P_FloorActive(s1))
988 return 0;
989
990 // heretic_note: rtn = 1; // probably doesn't matter?
991
992 s2 = getNextSector(s1->lines[0], s1); // s2 is pool's sector
993
994 // note lowest numbered line around
995 // pillar must be two-sided
996 if (!s2)
997 {
998 if (demo_compatibility)
999 {
1000 lprintf(LO_ERROR,
1001 "EV_DoDonut: lowest numbered line (linedef: %d) "
1002 "around pillar (sector: %d) must be two-sided. "
1003 "Unexpected behavior may occur in Vanilla Doom.\n",
1004 s1->lines[0]->iLineID, s1->iSectorID);
1005 return 0;
1006 }
1007 else
1008 {
1009 return 0;
1010 }
1011 }
1012
1013 /* do not start the donut if the pool is already moving
1014 * cph - DEMOSYNC - was !compatibility */
1015 if (!comp[comp_floors] && P_FloorActive(s2))
1016 return 0;
1017
1018 // find a two sided line around the pool whose other side isn't the pillar
1019 for (i = 0; i < s2->linecount; i++)
1020 {
1021 //jff 3/29/98 use true two-sidedness, not the flag
1022 // killough 4/5/98: changed demo_compatibility to compatibility
1023 if (comp[comp_model])
1024 {
1025 // original code: !s2->lines[i]->flags & ML_TWOSIDED
1026 // equivalent to: (!s2->lines[i]->flags) & ML_TWOSIDED , i.e. 0
1027 // should be: !(s2->lines[i]->flags & ML_TWOSIDED)
1028 if (((!s2->lines[i]->flags) & ML_TWOSIDED) || (s2->lines[i]->backsector == s1))
1029 continue;
1030 }
1031 else if (!s2->lines[i]->backsector || s2->lines[i]->backsector == s1)
1032 continue;
1033
1034 rtn = 1; //jff 1/26/98 no donut action - no switch change on return
1035
1036 s3 = s2->lines[i]->backsector; // s3 is model sector for changes
1037
1038 if (!s3)
1039 {
1040 // e6y
1041 // s3->floorheight is an int at 0000:0000
1042 // s3->floorpic is a short at 0000:0008
1043 // Trying to emulate
1044 lprintf(LO_ERROR,
1045 "EV_DoDonut: Access violation at linedef %d, sector %d. "
1046 "Unexpected behavior may occur in Vanilla Doom.\n",
1047 line->iLineID, s1->iSectorID);
1048 if (DonutOverrun(&s3_floorheight, &s3_floorpic))
1049 {
1050 lprintf(LO_WARN, "EV_DoDonut: Emulated with floorheight %d, floor pic %d.\n",
1051 s3_floorheight >> 16, s3_floorpic);
1052 }
1053 else
1054 {
1055 lprintf(LO_WARN, "EV_DoDonut: Not emulated.\n");
1056 break;
1057 }
1058 }
1059 else
1060 {
1061 s3_floorheight = s3->floorheight;
1062 s3_floorpic = s3->floorpic;
1063 }
1064
1065 // Spawn rising slime
1066 floor = Z_MallocLevel(sizeof(*floor));
1067 memset(floor, 0, sizeof(*floor));
1068 P_AddThinker(&floor->thinker);
1069 s2->floordata = floor; //jff 2/22/98
1070 floor->thinker.function = T_MoveFloor;
1071 floor->type = donutRaise;
1072 floor->crush = NO_CRUSH;
1073 floor->direction = 1;
1074 floor->sector = s2;
1075 floor->speed = slimespeed;
1076 floor->texture = s3_floorpic;
1077 floor->floordestheight = s3_floorheight;
1078
1079 // Spawn lowering donut-hole pillar
1080 floor = Z_MallocLevel(sizeof(*floor));
1081 memset(floor, 0, sizeof(*floor));
1082 P_AddThinker(&floor->thinker);
1083 s1->floordata = floor; //jff 2/22/98
1084 floor->thinker.function = T_MoveFloor;
1085 floor->type = lowerFloor;
1086 floor->crush = NO_CRUSH;
1087 floor->direction = -1;
1088 floor->sector = s1;
1089 floor->speed = pillarspeed;
1090 floor->floordestheight = s3_floorheight;
1091 break;
1092 }
1093
1094 return rtn;
1095 }
1096
1097 int EV_DoDonut(line_t *line)
1098 {
1099 const int *id_p;
1100 int rtn = 0;
1101
1102 // do function on all sectors with same tag as linedef
1103 FIND_SECTORS(id_p, line->tag)
1104 rtn |= P_SpawnDonut(*id_p, line, FLOORSPEED / 2, FLOORSPEED / 2);
1105
1106 return rtn;
1107 }
1108
1109 int EV_DoZDoomDonut(int tag, line_t *line, fixed_t pillarspeed, fixed_t slimespeed)
1110 {
1111 const int *id_p;
1112 int rtn = 0;
1113
1114 FIND_SECTORS2(id_p, tag, line)
1115 rtn |= P_SpawnDonut(*id_p, line, pillarspeed, slimespeed);
1116
1117 return rtn;
1118 }
1119
1120 //
1121 // EV_DoElevator
1122 //
1123 // Handle elevator linedef types
1124 //
1125 // Passed the linedef that triggered the elevator and the elevator action
1126 //
1127 // jff 2/22/98 new type to move floor and ceiling in parallel
1128 //
1129
1130 void P_SpawnElevator(sector_t *sec, line_t *line, elevator_e type, fixed_t speed, fixed_t height)
1131 {
1132 elevator_t *elevator;
1133
1134 elevator = Z_MallocLevel(sizeof(*elevator));
1135 memset(elevator, 0, sizeof(*elevator));
1136 P_AddThinker(&elevator->thinker);
1137 sec->floordata = elevator; //jff 2/22/98
1138 sec->ceilingdata = elevator; //jff 2/22/98
1139 elevator->thinker.function = T_MoveElevator;
1140 elevator->type = type;
1141 elevator->speed = speed;
1142 elevator->sector = sec;
1143
1144 switch (type)
1145 {
1146 case elevateDown:
1147 elevator->direction = -1;
1148 elevator->floordestheight = P_FindNextLowestFloor(sec, sec->floorheight);
1149 elevator->ceilingdestheight = sec->ceilingheight +
1150 elevator->floordestheight - sec->floorheight;
1151 break;
1152 case elevateUp:
1153 elevator->direction = 1;
1154 elevator->floordestheight = P_FindNextHighestFloor(sec, sec->floorheight);
1155 elevator->ceilingdestheight = sec->ceilingheight +
1156 elevator->floordestheight - sec->floorheight;
1157 break;
1158 case elevateCurrent:
1159 elevator->floordestheight = line->frontsector->floorheight;
1160 elevator->ceilingdestheight = sec->ceilingheight +
1161 elevator->floordestheight - sec->floorheight;
1162 elevator->direction = elevator->floordestheight > sec->floorheight ? 1 : -1;
1163 break;
1164 case elevateRaise:
1165 elevator->direction = 1;
1166 elevator->floordestheight = sec->floorheight + height;
1167 elevator->ceilingdestheight = sec->ceilingheight + height;
1168 break;
1169 case elevateLower:
1170 elevator->direction = -1;
1171 elevator->floordestheight = sec->floorheight - height;
1172 elevator->ceilingdestheight = sec->ceilingheight - height;
1173 break;
1174 }
1175 }
1176
1177 int EV_DoZDoomElevator(line_t *line, elevator_e type, fixed_t speed, fixed_t height, int tag)
1178 {
1179 const int *id_p;
1180 int rtn = 0;
1181 sector_t *sec;
1182
1183 height *= FRACUNIT;
1184
1185 if (!line && type == elevateCurrent)
1186 return 0;
1187
1188 FIND_SECTORS2(id_p, tag, line)
1189 {
1190 sec = &sectors[*id_p];
1191
1192 if (P_FloorActive(sec) || P_CeilingActive(sec))
1193 continue;
1194
1195 rtn = 1;
1196 P_SpawnElevator(sec, line, type, speed, height);
1197 }
1198
1199 return rtn;
1200 }
1201
1202 int EV_DoElevator
1203 ( line_t* line,
1204 elevator_e elevtype )
1205 {
1206 const int *id_p;
1207 int rtn;
1208 sector_t* sec;
1209
1210 rtn = 0;
1211 // act on all sectors with the same tag as the triggering linedef
1212 FIND_SECTORS(id_p, line->tag)
1213 {
1214 sec = &sectors[*id_p];
1215
1216 // If either floor or ceiling is already activated, skip it
1217 if (sec->floordata || sec->ceilingdata) //jff 2/22/98
1218 continue;
1219
1220 rtn = 1;
1221 P_SpawnElevator(sec, line, elevtype, ELEVATORSPEED, 0);
1222 }
1223 return rtn;
1224 }
1225
1226 // hexen
1227
1228 static void P_SetFloorChangeType(floormove_t *floor, sector_t *sec, int change)
1229 {
1230 floor->texture = sec->floorpic;
1231
1232 switch (change & 3)
1233 {
1234 case 0:
1235 break;
1236 case 1:
1237 P_ResetTransferSpecial(&floor->newspecial);
1238 floor->type = genFloorChg0;
1239 break;
1240 case 2:
1241 floor->type = genFloorChg;
1242 break;
1243 case 3:
1244 P_CopyTransferSpecial(&floor->newspecial, sec);
1245 floor->type = genFloorChgT;
1246 break;
1247 }
1248 }
1249
1250 static void P_SpawnZDoomFloor(sector_t *sec, floor_e floortype, line_t *line,
1251 fixed_t speed, fixed_t height, int crush, int change,
1252 dboolean hexencrush, dboolean hereticlower)
1253 {
1254 floormove_t *floor;
1255
1256 floor = Z_MallocLevel (sizeof(*floor));
1257 memset(floor, 0, sizeof(*floor));
1258 P_AddThinker(&floor->thinker);
1259 sec->floordata = floor;
1260 floor->thinker.function = T_MoveFloor;
1261 floor->type = floortype;
1262 floor->crush = crush;
1263 floor->speed = speed;
1264 floor->sector = sec;
1265 floor->hexencrush = hexencrush;
1266
1267 switch (floortype)
1268 {
1269 case floorLowerToHighest:
1270 floor->direction = -1;
1271 floor->floordestheight = P_FindHighestFloorSurrounding(sec);
1272 if (hereticlower || floor->floordestheight != sec->floorheight)
1273 floor->floordestheight += height;
1274 break;
1275 case floorLowerToLowest:
1276 floor->direction = -1;
1277 floor->floordestheight = P_FindLowestFloorSurrounding(sec);
1278 break;
1279 case floorLowerToNearest:
1280 floor->direction = -1;
1281 floor->floordestheight = P_FindNextLowestFloor(sec, sec->floorheight);
1282 break;
1283 case floorLowerInstant:
1284 floor->speed = height;
1285 case floorLowerByValue:
1286 floor->direction = -1;
1287 floor->floordestheight = sec->floorheight - height;
1288 break;
1289 case floorRaiseInstant:
1290 floor->speed = height;
1291 case floorRaiseByValue:
1292 floor->direction = 1;
1293 floor->floordestheight = sec->floorheight + height;
1294 break;
1295 case floorMoveToValue:
1296 floor->floordestheight = height;
1297 floor->direction = (floor->floordestheight > sec->floorheight) ? 1 : -1;
1298 break;
1299 case floorRaiseAndCrushDoom:
1300 height = 8 * FRACUNIT;
1301 case floorRaiseToLowestCeiling:
1302 floor->direction = 1;
1303 floor->floordestheight = P_FindLowestCeilingSurrounding(sec) - height;
1304 if (floor->floordestheight > sec->ceilingheight)
1305 floor->floordestheight = sec->ceilingheight - height;
1306 break;
1307 case floorRaiseToHighest:
1308 floor->direction = 1;
1309 floor->floordestheight = P_FindHighestFloorSurrounding(sec);
1310 break;
1311 case floorRaiseToNearest:
1312 floor->direction = 1;
1313 floor->floordestheight = P_FindNextHighestFloor(sec, sec->floorheight);
1314 break;
1315 case floorRaiseToLowest:
1316 floor->direction = 1;
1317 floor->floordestheight = P_FindLowestFloorSurrounding(sec);
1318 break;
1319 case floorRaiseAndCrush:
1320 height = 8 * FRACUNIT;
1321 case floorRaiseToCeiling:
1322 floor->direction = 1;
1323 floor->floordestheight = sec->ceilingheight - height;
1324 break;
1325 case floorLowerToLowestCeiling:
1326 floor->direction = -1;
1327 floor->floordestheight = P_FindLowestCeilingSurrounding(sec);
1328 break;
1329 case floorLowerByTexture:
1330 floor->direction = -1;
1331 floor->floordestheight = sec->floorheight - P_FindShortestTextureAround(sec->iSectorID);
1332 break;
1333 case floorRaiseByTexture:
1334 floor->direction = 1;
1335 floor->floordestheight = sec->floorheight + P_FindShortestTextureAround(sec->iSectorID);
1336 break;
1337 case floorLowerToCeiling:
1338 floor->direction = -1;
1339 floor->floordestheight = sec->ceilingheight - height;
1340 break;
1341 case floorRaiseAndChange:
1342 floor->direction = 1;
1343 floor->floordestheight = sec->floorheight + height;
1344 if (line)
1345 {
1346 sec->floorpic = line->frontsector->floorpic;
1347 P_CopySectorSpecial(sec, line->frontsector);
1348 }
1349 else
1350 {
1351 P_ResetSectorSpecial(sec);
1352 }
1353 break;
1354 case floorLowerAndChange:
1355 {
1356 sector_t *modelsec;
1357
1358 floor->direction = -1;
1359 floor->floordestheight = P_FindLowestFloorSurrounding(sec);
1360 floor->texture = sec->floorpic;
1361 P_CopyTransferSpecial(&floor->newspecial, sec);
1362
1363 modelsec = P_FindModelFloorSector(floor->floordestheight, sec->iSectorID);
1364 if (modelsec)
1365 {
1366 floor->texture = modelsec->floorpic;
1367 P_CopyTransferSpecial(&floor->newspecial, modelsec);
1368 }
1369 }
1370 break;
1371 default:
1372 break;
1373 }
1374
1375 // hexen and zdoom emit sound at this point, but not doom
1376
1377 if (change & 3)
1378 {
1379 // [RH] Need to do some transferring
1380 if (change & 4)
1381 {
1382 // Numeric model change
1383 sector_t *modelsec;
1384
1385 modelsec = (
1386 floortype == floorRaiseToLowestCeiling ||
1387 floortype == floorLowerToLowestCeiling ||
1388 floortype == floorRaiseToCeiling ||
1389 floortype == floorLowerToCeiling
1390 ) ? P_FindModelCeilingSector(floor->floordestheight, sec->iSectorID)
1391 : P_FindModelFloorSector(floor->floordestheight, sec->iSectorID);
1392
1393 if (modelsec)
1394 {
1395 P_SetFloorChangeType(floor, modelsec, change);
1396 }
1397 }
1398 else if (line)
1399 {
1400 // Trigger model change
1401 P_SetFloorChangeType(floor, line->frontsector, change);
1402 }
1403 }
1404 }
1405
1406 int EV_DoZDoomFloor(floor_e floortype, line_t *line, int tag, fixed_t speed, fixed_t height,
1407 int crush, int change, dboolean hexencrush, dboolean hereticlower)
1408 {
1409 sector_t *sec;
1410 const int *id_p;
1411 int retcode = 0;
1412
1413 speed *= FRACUNIT / 8;
1414 height *= FRACUNIT;
1415
1416 FIND_SECTORS2(id_p, tag, line)
1417 {
1418 sec = &sectors[*id_p];
1419 if (sec->floordata)
1420 {
1421 continue;
1422 }
1423 retcode = 1;
1424 P_SpawnZDoomFloor(sec, floortype, line, speed, height,
1425 crush, change, hexencrush, hereticlower);
1426 }
1427
1428 return retcode;
1429 }
1430
1431 int Hexen_EV_DoFloor(line_t * line, byte * args, floor_e floortype)
1432 {
1433 const int *id_p;
1434 int rtn;
1435 sector_t *sec;
1436 floormove_t *floor = NULL;
1437
1438 rtn = 0;
1439 FIND_SECTORS(id_p, args[0])
1440 {
1441 sec = &sectors[*id_p];
1442
1443 // ALREADY MOVING? IF SO, KEEP GOING...
1444 if (sec->floordata || sec->ceilingdata)
1445 continue;
1446
1447 //
1448 // new floor thinker
1449 //
1450 rtn = 1;
1451 floor = Z_MallocLevel(sizeof(*floor));
1452 memset(floor, 0, sizeof(*floor));
1453 P_AddThinker(&floor->thinker);
1454 sec->floordata = floor;
1455 floor->thinker.function = T_MoveFloor;
1456 floor->type = floortype;
1457 floor->crush = NO_CRUSH;
1458 floor->speed = args[1] * (FRACUNIT / 8);
1459 if (floortype == FLEV_LOWERTIMES8INSTANT ||
1460 floortype == FLEV_RAISETIMES8INSTANT)
1461 {
1462 floor->speed = 2000 << FRACBITS;
1463 }
1464 switch (floortype)
1465 {
1466 case FLEV_LOWERFLOOR:
1467 floor->direction = -1;
1468 floor->sector = sec;
1469 floor->floordestheight = P_FindHighestFloorSurrounding(sec);
1470 break;
1471 case FLEV_LOWERFLOORTOLOWEST:
1472 floor->direction = -1;
1473 floor->sector = sec;
1474 floor->floordestheight = P_FindLowestFloorSurrounding(sec);
1475 break;
1476 case FLEV_LOWERFLOORBYVALUE:
1477 floor->direction = -1;
1478 floor->sector = sec;
1479 floor->floordestheight = floor->sector->floorheight -
1480 args[2] * FRACUNIT;
1481 break;
1482 case FLEV_LOWERTIMES8INSTANT:
1483 case FLEV_LOWERBYVALUETIMES8:
1484 floor->direction = -1;
1485 floor->sector = sec;
1486 floor->floordestheight = floor->sector->floorheight -
1487 args[2] * FRACUNIT * 8;
1488 break;
1489 case FLEV_RAISEFLOORCRUSH:
1490 floor->crush = P_ConvertHexenCrush(args[2]); // arg[2] = crushing value
1491 floor->direction = 1;
1492 floor->sector = sec;
1493 floor->floordestheight = sec->ceilingheight - 8 * FRACUNIT;
1494 break;
1495 case FLEV_RAISEFLOOR:
1496 floor->direction = 1;
1497 floor->sector = sec;
1498 floor->floordestheight = P_FindLowestCeilingSurrounding(sec);
1499 if (floor->floordestheight > sec->ceilingheight)
1500 floor->floordestheight = sec->ceilingheight;
1501 break;
1502 case FLEV_RAISEFLOORTONEAREST:
1503 floor->direction = 1;
1504 floor->sector = sec;
1505 floor->floordestheight =
1506 P_FindNextHighestFloor(sec, sec->floorheight);
1507 break;
1508 case FLEV_RAISEFLOORBYVALUE:
1509 floor->direction = 1;
1510 floor->sector = sec;
1511 floor->floordestheight = floor->sector->floorheight +
1512 args[2] * FRACUNIT;
1513 break;
1514 case FLEV_RAISETIMES8INSTANT:
1515 case FLEV_RAISEBYVALUETIMES8:
1516 floor->direction = 1;
1517 floor->sector = sec;
1518 floor->floordestheight = floor->sector->floorheight +
1519 args[2] * FRACUNIT * 8;
1520 break;
1521 case FLEV_MOVETOVALUETIMES8:
1522 floor->sector = sec;
1523 floor->floordestheight = args[2] * FRACUNIT * 8;
1524 if (args[3])
1525 {
1526 floor->floordestheight = -floor->floordestheight;
1527 }
1528 if (floor->floordestheight > floor->sector->floorheight)
1529 {
1530 floor->direction = 1;
1531 }
1532 else if (floor->floordestheight < floor->sector->floorheight)
1533 {
1534 floor->direction = -1;
1535 }
1536 else
1537 { // already at lowest position
1538 rtn = 0;
1539 }
1540 break;
1541 default:
1542 rtn = 0;
1543 break;
1544 }
1545 }
1546 if (rtn)
1547 {
1548 SN_StartSequence((mobj_t *) & floor->sector->soundorg,
1549 SEQ_PLATFORM + floor->sector->seqType);
1550 }
1551 return rtn;
1552 }
1553
1554 int EV_DoFloorAndCeiling(line_t * line, byte * args, dboolean raise)
1555 {
1556 dboolean floor, ceiling;
1557 const int *id_p;
1558 sector_t *sec;
1559
1560 if (raise)
1561 {
1562 floor = Hexen_EV_DoFloor(line, args, FLEV_RAISEFLOORBYVALUE);
1563 FIND_SECTORS(id_p, args[0])
1564 {
1565 sec = &sectors[*id_p];
1566 sec->floordata = NULL;
1567 sec->ceilingdata = NULL;
1568 }
1569 ceiling = Hexen_EV_DoCeiling(line, args, CLEV_RAISEBYVALUE);
1570 }
1571 else
1572 {
1573 floor = Hexen_EV_DoFloor(line, args, FLEV_LOWERFLOORBYVALUE);
1574 FIND_SECTORS(id_p, args[0])
1575 {
1576 sec = &sectors[*id_p];
1577 sec->floordata = NULL;
1578 sec->ceilingdata = NULL;
1579 }
1580 ceiling = Hexen_EV_DoCeiling(line, args, CLEV_LOWERBYVALUE);
1581 }
1582 return (floor | ceiling);
1583 }
1584
1585 #define STAIR_SECTOR_TYPE 26
1586 #define STAIR_QUEUE_SIZE 32
1587
1588 struct
1589 {
1590 sector_t *sector;
1591 int type;
1592 int height;
1593 } StairQueue[STAIR_QUEUE_SIZE];
1594
1595 static int QueueHead;
1596 static int QueueTail;
1597
1598 static int StepDelta;
1599 static int Direction;
1600 static int Speed;
1601 static int Texture;
1602 static int StartDelay;
1603 static int StartDelayDelta;
1604 static int TextureChange;
1605 static int StartHeight;
1606
1607 static void QueueStairSector(sector_t * sec, int type, int height)
1608 {
1609 if ((QueueTail + 1) % STAIR_QUEUE_SIZE == QueueHead)
1610 {
1611 I_Error("BuildStairs: Too many branches located.\n");
1612 }
1613 StairQueue[QueueTail].sector = sec;
1614 StairQueue[QueueTail].type = type;
1615 StairQueue[QueueTail].height = height;
1616
1617 QueueTail = (QueueTail + 1) % STAIR_QUEUE_SIZE;
1618 }
1619
1620 static sector_t *DequeueStairSector(int *type, int *height)
1621 {
1622 sector_t *sec;
1623
1624 if (QueueHead == QueueTail)
1625 { // queue is empty
1626 return NULL;
1627 }
1628 *type = StairQueue[QueueHead].type;
1629 *height = StairQueue[QueueHead].height;
1630 sec = StairQueue[QueueHead].sector;
1631 QueueHead = (QueueHead + 1) % STAIR_QUEUE_SIZE;
1632
1633 return sec;
1634 }
1635
1636 static void ProcessStairSector(sector_t * sec, int type, int height,
1637 stairs_e stairsType, int delay, int resetDelay)
1638 {
1639 int i;
1640 sector_t *tsec;
1641 floormove_t *floor;
1642
1643 //
1644 // new floor thinker
1645 //
1646 height += StepDelta;
1647 floor = Z_MallocLevel(sizeof(*floor));
1648 memset(floor, 0, sizeof(*floor));
1649 P_AddThinker(&floor->thinker);
1650 sec->floordata = floor;
1651 floor->thinker.function = T_MoveFloor;
1652 floor->type = FLEV_RAISEBUILDSTEP;
1653 floor->direction = Direction;
1654 floor->sector = sec;
1655 floor->floordestheight = height;
1656 floor->crush = NO_CRUSH;
1657 switch (stairsType)
1658 {
1659 case STAIRS_NORMAL:
1660 floor->speed = Speed;
1661 if (delay)
1662 {
1663 floor->delayTotal = delay;
1664 floor->stairsDelayHeight = sec->floorheight + StepDelta;
1665 floor->stairsDelayHeightDelta = StepDelta;
1666 }
1667 floor->resetDelay = resetDelay;
1668 floor->resetDelayCount = resetDelay;
1669 floor->resetHeight = sec->floorheight;
1670 break;
1671 case STAIRS_SYNC:
1672 floor->speed = FixedMul(Speed, FixedDiv(height - StartHeight,
1673 StepDelta));
1674 floor->resetDelay = delay; //arg4
1675 floor->resetDelayCount = delay;
1676 floor->resetHeight = sec->floorheight;
1677 break;
1678 default:
1679 break;
1680 }
1681 SN_StartSequence((mobj_t *) & sec->soundorg, SEQ_PLATFORM + sec->seqType);
1682 //
1683 // Find next sector to raise
1684 // Find nearby sector with sector special equal to type
1685 //
1686 for (i = 0; i < sec->linecount; i++)
1687 {
1688 if (!((sec->lines[i])->flags & ML_TWOSIDED))
1689 {
1690 continue;
1691 }
1692 tsec = (sec->lines[i])->frontsector;
1693 if (tsec->special == type + STAIR_SECTOR_TYPE && !tsec->floordata && !tsec->ceilingdata
1694 && tsec->floorpic == Texture && tsec->validcount != validcount)
1695 {
1696 QueueStairSector(tsec, type ^ 1, height);
1697 tsec->validcount = validcount;
1698 //tsec->special = 0;
1699 }
1700 tsec = (sec->lines[i])->backsector;
1701 if (tsec->special == type + STAIR_SECTOR_TYPE && !tsec->floordata && !tsec->ceilingdata
1702 && tsec->floorpic == Texture && tsec->validcount != validcount)
1703 {
1704 QueueStairSector(tsec, type ^ 1, height);
1705 tsec->validcount = validcount;
1706 //tsec->special = 0;
1707 }
1708 }
1709 }
1710
1711 static sector_t *P_NextSpecialSector(sector_t *sec, int special)
1712 {
1713 int i;
1714 sector_t *tsec;
1715
1716 for (i = 0; i < sec->linecount; i++)
1717 {
1718 if (!((sec->lines[i])->flags & ML_TWOSIDED))
1719 {
1720 continue;
1721 }
1722
1723 tsec = sec->lines[i]->frontsector;
1724 if (tsec->special == special && tsec->validcount != validcount)
1725 {
1726 tsec->validcount = validcount;
1727 return tsec;
1728 }
1729
1730 tsec = sec->lines[i]->backsector;
1731 if (tsec->special == special && tsec->validcount != validcount)
1732 {
1733 tsec->validcount = validcount;
1734 return tsec;
1735 }
1736 }
1737
1738 return NULL;
1739 }
1740
1741 static void P_SpawnZDoomStair(sector_t *sec, stair_e type, fixed_t stairstep,
1742 fixed_t speed, fixed_t height, int delay, int reset, int usespecials)
1743 {
1744 floormove_t *floor;
1745
1746 floor = Z_MallocLevel(sizeof(*floor));
1747 memset(floor, 0, sizeof(*floor));
1748 P_AddThinker(&floor->thinker);
1749 sec->floordata = floor;
1750 floor->thinker.function = T_MoveFloor;
1751 floor->direction = (type == stairBuildUp) ? 1 : -1;
1752 floor->sector = sec;
1753 floor->type = floorBuildStair;
1754
1755 floor->crush = (
1756 (!(usespecials & STAIR_USE_SPECIALS) && speed == 4 * FRACUNIT) ||
1757 usespecials & STAIR_CRUSH
1758 ) ? DOOM_CRUSH : NO_CRUSH;
1759 floor->hexencrush = false;
1760
1761 if (usespecials & STAIR_SYNC)
1762 {
1763 floor->speed = FixedMul(speed, FixedDiv(height - sec->floorheight, stairstep));
1764 }
1765 else
1766 {
1767 floor->speed = speed;
1768 }
1769
1770 floor->floordestheight = height;
1771
1772 // [RH] Set up delay values
1773 floor->delayTotal = delay;
1774 floor->stairsDelayHeight = sec->floorheight + stairstep;
1775 floor->stairsDelayHeightDelta = stairstep;
1776
1777 floor->resetDelayCount = reset; // [RH] Tics until reset (0 if never)
1778 floor->resetHeight = sec->floorheight; // [RH] Height to reset to
1779 }
1780
1781 int EV_BuildZDoomStairs(int tag, stair_e type, line_t *line, fixed_t stairsize,
1782 fixed_t speed, int delay, int reset, int igntxt, int usespecials)
1783 {
1784 const int *id_p;
1785 int oldsecnum;
1786 int newsecnum;
1787 dboolean rtn = 0;
1788 fixed_t height;
1789 fixed_t stairstep;
1790 int texture;
1791 int ok;
1792 sector_t *sec;
1793 sector_t *tsec;
1794
1795 if (!speed)
1796 return 0;
1797
1798 stairsize *= FRACUNIT;
1799
1800 validcount++;
1801
1802 FIND_SECTORS2(id_p, tag, line)
1803 {
1804 sec = &sectors[*id_p];
1805
1806 // ALREADY MOVING? IF SO, KEEP GOING...
1807 // jff 2/26/98 add special lockout condition to wait for entire
1808 // staircase to build before retriggering
1809 if (P_FloorActive(sec) || sec->stairlock)
1810 {
1811 continue;
1812 }
1813
1814 rtn = 1;
1815 texture = sec->floorpic;
1816 stairstep = (type == stairBuildUp) ? stairsize : -stairsize;
1817 height = sec->floorheight + stairstep;
1818
1819 P_SpawnZDoomStair(sec, type, stairstep, speed, height, delay, reset, usespecials & ~STAIR_SYNC);
1820
1821 // jff 2/26/98 set up lock on current sector
1822 sec->stairlock = -2;
1823 sec->nextsec = -1;
1824 sec->prevsec = -1;
1825 sec->validcount = validcount;
1826
1827 oldsecnum = *id_p;
1828
1829 // Find next sector to raise
1830 // 1. Find 2-sided line with same sector side[0] (lowest numbered)
1831 // 2. Other side is the next sector to raise
1832 // 3. Unless already moving, or different texture, then stop building
1833 do
1834 {
1835 ok = false;
1836
1837 if (usespecials & STAIR_USE_SPECIALS)
1838 {
1839 // [RH] Find the next sector by scanning for special
1840 tsec = P_NextSpecialSector(sec,
1841 sec->special == zs_stairs_special1 ?
1842 zs_stairs_special2 :
1843 zs_stairs_special1);
1844
1845 ok = (tsec != NULL);
1846 if (ok)
1847 {
1848 height += stairstep;
1849
1850 // if sector's floor already moving, look for another
1851 // jff 2/26/98 special lockout condition for retriggering
1852 if (P_FloorActive(tsec) || tsec->stairlock)
1853 {
1854 sec = tsec;
1855 continue;
1856 }
1857
1858 newsecnum = tsec - sectors;
1859 }
1860 }
1861 else
1862 {
1863 int i;
1864
1865 for (i = 0; i < sec->linecount; i++)
1866 {
1867 if (!(sec->lines[i]->flags & ML_TWOSIDED))
1868 continue;
1869
1870 tsec = sec->lines[i]->frontsector;
1871 newsecnum = tsec->iSectorID;
1872
1873 if (oldsecnum != newsecnum)
1874 continue;
1875
1876 tsec = sec->lines[i]->backsector;
1877 if (!tsec) continue; //jff 5/7/98 if no backside, continue
1878 newsecnum = tsec->iSectorID;
1879
1880 // if sector's floor is different texture, look for another
1881 if (!igntxt && tsec->floorpic != texture)
1882 continue;
1883
1884 // if sector's floor already moving, look for another
1885 // jff 2/26/98 special lockout condition for retriggering
1886 if (P_FloorActive(tsec) || tsec->stairlock)
1887 continue;
1888
1889 height += stairstep;
1890 ok = true;
1891 break;
1892 }
1893 }
1894
1895 if (ok)
1896 {
1897 // jff 2/26/98
1898 // link the stair chain in both directions
1899 // lock the stair sector until building complete
1900 sec->nextsec = newsecnum; // link step to next
1901 tsec->prevsec = oldsecnum; // link next back
1902 tsec->nextsec = -1; // set next forward link as end
1903 tsec->stairlock = -2; // lock the step
1904
1905 sec = tsec;
1906 oldsecnum = newsecnum;
1907
1908 P_SpawnZDoomStair(sec, type, stairstep, speed, height, delay, reset, usespecials);
1909 }
1910 } while (ok);
1911
1912 // [RH] make sure the first sector doesn't point to a previous one, otherwise
1913 // it can infinite loop when the first sector stops moving.
1914 sectors[*id_p].prevsec = -1;
1915 }
1916 return rtn;
1917 }
1918
1919 int Hexen_EV_BuildStairs(line_t * line, byte * args, int direction, stairs_e stairsType)
1920 {
1921 const int *id_p;
1922 int height;
1923 int delay;
1924 int resetDelay;
1925 sector_t *sec;
1926 sector_t *qSec;
1927 int type;
1928
1929 // Set global stairs variables
1930 TextureChange = 0;
1931 Direction = direction;
1932 StepDelta = Direction * (args[2] * FRACUNIT);
1933 Speed = args[1] * (FRACUNIT / 8);
1934 resetDelay = args[4];
1935 delay = args[3];
1936 if (stairsType == STAIRS_PHASED)
1937 {
1938 StartDelayDelta = args[3];
1939 StartDelay = StartDelayDelta;
1940 resetDelay = StartDelayDelta;
1941 delay = 0;
1942 TextureChange = args[4];
1943 }
1944
1945 validcount++;
1946 FIND_SECTORS(id_p, args[0])
1947 {
1948 sec = &sectors[*id_p];
1949
1950 Texture = sec->floorpic;
1951 StartHeight = sec->floorheight;
1952
1953 // ALREADY MOVING? IF SO, KEEP GOING...
1954 if (sec->floordata || sec->ceilingdata)
1955 continue;
1956
1957 QueueStairSector(sec, 0, sec->floorheight);
1958 P_ClearNonGeneralizedSectorSpecial(sec);
1959 }
1960 while ((qSec = DequeueStairSector(&type, &height)) != NULL)
1961 {
1962 ProcessStairSector(qSec, type, height, stairsType, delay, resetDelay);
1963 }
1964 return (1);
1965 }
1966
1967 void T_BuildHexenPillar(pillar_t * pillar)
1968 {
1969 result_e res1;
1970 result_e res2;
1971
1972 // First, raise the floor
1973 res1 = T_MoveFloorPlane(pillar->sector, pillar->floorSpeed, pillar->floordest,
1974 pillar->crush, pillar->direction, true);
1975 // Then, lower the ceiling
1976 res2 = T_MoveCeilingPlane(pillar->sector, pillar->ceilingSpeed, pillar->ceilingdest,
1977 pillar->crush, -pillar->direction, true);
1978 if (res1 == pastdest && res2 == pastdest)
1979 {
1980 pillar->sector->floordata = NULL;
1981 SN_StopSequence((mobj_t *) & pillar->sector->soundorg);
1982 P_TagFinished(pillar->sector->tag);
1983 P_RemoveThinker(&pillar->thinker);
1984 }
1985 }
1986
1987 void T_BuildZDoomPillar(pillar_t * pillar)
1988 {
1989 result_e res1, res2;
1990 fixed_t oldfloorheight, oldceilingheight;
1991
1992 oldfloorheight = pillar->sector->floorheight;
1993 oldceilingheight = pillar->sector->ceilingheight;
1994
1995 res1 = !pillar->floorSpeed ? pastdest :
1996 T_MoveFloorPlane(pillar->sector, pillar->floorSpeed, pillar->floordest,
1997 pillar->crush, pillar->direction, pillar->hexencrush);
1998
1999 res2 = !pillar->ceilingSpeed ? pastdest :
2000 T_MoveCeilingPlane(pillar->sector, pillar->ceilingSpeed, pillar->ceilingdest,
2001 pillar->crush, -pillar->direction, pillar->hexencrush);
2002
2003 if (!(leveltime & 7))
2004 S_LoopSectorSound(pillar->sector, g_sfx_stnmov, 8);
2005
2006 if (res1 == pastdest && res2 == pastdest)
2007 {
2008 pillar->sector->floordata = NULL;
2009 pillar->sector->ceilingdata = NULL;
2010 P_RemoveThinker(&pillar->thinker);
2011
2012 // make floor stop sound
2013 S_StartSectorSound(pillar->sector, sfx_pstop);
2014 }
2015 else
2016 {
2017 if (res1 == crushed)
2018 {
2019 T_MoveFloorPlane(pillar->sector, pillar->floorSpeed, oldfloorheight,
2020 NO_CRUSH, -1, pillar->hexencrush);
2021 }
2022
2023 if (res2 == crushed)
2024 {
2025 T_MoveCeilingPlane(pillar->sector, pillar->ceilingSpeed, oldceilingheight,
2026 NO_CRUSH, 1, pillar->hexencrush);
2027 }
2028 }
2029 }
2030
2031 void T_BuildPillar(pillar_t * pillar)
2032 {
2033 map_format.t_build_pillar(pillar);
2034 }
2035
2036 void P_SpawnZDoomPillar(sector_t *sec, pillar_e type, fixed_t speed,
2037 fixed_t floordist, fixed_t ceilingdist, int crush, dboolean hexencrush)
2038 {
2039 pillar_t *pillar;
2040
2041 pillar = Z_MallocLevel(sizeof(*pillar));
2042 memset(pillar, 0, sizeof(*pillar));
2043 sec->floordata = pillar;
2044 sec->ceilingdata = pillar;
2045 P_AddThinker(&pillar->thinker);
2046 pillar->thinker.function = T_BuildPillar;
2047 pillar->sector = sec;
2048 pillar->direction = (type == pillarBuild ? 1 : -1);
2049 pillar->crush = crush;
2050 pillar->hexencrush = hexencrush;
2051
2052 if (type == pillarBuild)
2053 {
2054 // If the pillar height is 0, have the floor and ceiling meet halfway
2055 if (!floordist)
2056 {
2057 floordist = (sec->ceilingheight - sec->floorheight) / 2;
2058 }
2059
2060 pillar->floordest = sec->floorheight + floordist;
2061 pillar->ceilingdest = pillar->floordest;
2062 ceilingdist = sec->ceilingheight - pillar->ceilingdest;
2063 }
2064 else
2065 {
2066 // If one of the heights is 0, figure it out based on the
2067 // surrounding sectors
2068 if (!floordist)
2069 {
2070 floordist = sec->floorheight - P_FindLowestFloorSurrounding(sec);
2071 }
2072 pillar->floordest = sec->floorheight - floordist;
2073
2074 if (!ceilingdist)
2075 {
2076 ceilingdist = P_FindHighestCeilingSurrounding(sec) - sec->ceilingheight;
2077 }
2078 pillar->ceilingdest = sec->ceilingheight + ceilingdist;
2079 }
2080
2081 // The speed parameter applies to whichever part of the pillar
2082 // travels the farthest. The other part's speed is then set so
2083 // that it arrives at its destination at the same time.
2084 if (floordist > ceilingdist)
2085 {
2086 pillar->floorSpeed = speed;
2087 pillar->ceilingSpeed = FixedMul(speed, FixedDiv(ceilingdist, floordist));
2088 }
2089 else
2090 {
2091 pillar->ceilingSpeed = speed;
2092 pillar->floorSpeed = FixedMul(speed, FixedDiv(floordist, ceilingdist));
2093 }
2094 }
2095
2096 int EV_DoZDoomPillar(pillar_e type, line_t *line, int tag, fixed_t speed,
2097 fixed_t floordist, fixed_t ceilingdist, int crush, dboolean hexencrush)
2098 {
2099 const int *id_p;
2100 sector_t *sec;
2101 dboolean rtn = 0;
2102
2103 floordist *= FRACUNIT;
2104 ceilingdist *= FRACUNIT;
2105
2106 FIND_SECTORS2(id_p, tag, line)
2107 {
2108 sec = &sectors[*id_p];
2109
2110 if (P_FloorActive(sec) || P_CeilingActive(sec))
2111 continue;
2112
2113 if (type == pillarBuild && sec->floorheight == sec->ceilingheight)
2114 continue;
2115
2116 if (type == pillarOpen && sec->floorheight != sec->ceilingheight)
2117 continue;
2118
2119 rtn = 1;
2120
2121 P_SpawnZDoomPillar(sec, type, speed, floordist, ceilingdist, crush, hexencrush);
2122 }
2123
2124 return rtn;
2125 }
2126
2127 int EV_BuildPillar(line_t * line, byte * args, int crush)
2128 {
2129 const int *id_p;
2130 sector_t *sec;
2131 pillar_t *pillar;
2132 int newHeight;
2133 int rtn;
2134
2135 rtn = 0;
2136 FIND_SECTORS(id_p, args[0])
2137 {
2138 sec = &sectors[*id_p];
2139 if (sec->floordata || sec->ceilingdata)
2140 continue; // already moving
2141 if (sec->floorheight == sec->ceilingheight)
2142 { // pillar is already closed
2143 continue;
2144 }
2145 rtn = 1;
2146 if (!args[2])
2147 {
2148 newHeight = sec->floorheight +
2149 ((sec->ceilingheight - sec->floorheight) / 2);
2150 }
2151 else
2152 {
2153 newHeight = sec->floorheight + (args[2] << FRACBITS);
2154 }
2155
2156 pillar = Z_MallocLevel(sizeof(*pillar));
2157 memset(pillar, 0, sizeof(*pillar));
2158 sec->floordata = pillar;
2159 P_AddThinker(&pillar->thinker);
2160 pillar->thinker.function = T_BuildPillar;
2161 pillar->sector = sec;
2162 if (!args[2])
2163 {
2164 pillar->ceilingSpeed = pillar->floorSpeed =
2165 args[1] * (FRACUNIT / 8);
2166 }
2167 else if (newHeight - sec->floorheight >
2168 sec->ceilingheight - newHeight)
2169 {
2170 pillar->floorSpeed = args[1] * (FRACUNIT / 8);
2171 pillar->ceilingSpeed = FixedMul(sec->ceilingheight - newHeight,
2172 FixedDiv(pillar->floorSpeed,
2173 newHeight -
2174 sec->floorheight));
2175 }
2176 else
2177 {
2178 pillar->ceilingSpeed = args[1] * (FRACUNIT / 8);
2179 pillar->floorSpeed = FixedMul(newHeight - sec->floorheight,
2180 FixedDiv(pillar->ceilingSpeed,
2181 sec->ceilingheight -
2182 newHeight));
2183 }
2184 pillar->floordest = newHeight;
2185 pillar->ceilingdest = newHeight;
2186 pillar->direction = 1;
2187 pillar->crush = P_ConvertHexenCrush(crush * args[3]);
2188 SN_StartSequence((mobj_t *) & pillar->sector->soundorg,
2189 SEQ_PLATFORM + pillar->sector->seqType);
2190 }
2191 return rtn;
2192 }
2193
2194 int EV_OpenPillar(line_t * line, byte * args)
2195 {
2196 const int *id_p;
2197 sector_t *sec;
2198 pillar_t *pillar;
2199 int rtn;
2200
2201 rtn = 0;
2202 FIND_SECTORS(id_p, args[0])
2203 {
2204 sec = &sectors[*id_p];
2205 if (sec->floordata || sec->ceilingdata)
2206 continue; // already moving
2207 if (sec->floorheight != sec->ceilingheight)
2208 { // pillar isn't closed
2209 continue;
2210 }
2211 rtn = 1;
2212 pillar = Z_MallocLevel(sizeof(*pillar));
2213 memset(pillar, 0, sizeof(*pillar));
2214 sec->floordata = pillar;
2215 P_AddThinker(&pillar->thinker);
2216 pillar->thinker.function = T_BuildPillar;
2217 pillar->sector = sec;
2218 pillar->crush = NO_CRUSH;
2219 if (!args[2])
2220 {
2221 pillar->floordest = P_FindLowestFloorSurrounding(sec);
2222 }
2223 else
2224 {
2225 pillar->floordest = sec->floorheight - (args[2] << FRACBITS);
2226 }
2227 if (!args[3])
2228 {
2229 pillar->ceilingdest = P_FindHighestCeilingSurrounding(sec);
2230 }
2231 else
2232 {
2233 pillar->ceilingdest = sec->ceilingheight + (args[3] << FRACBITS);
2234 }
2235 if (sec->floorheight - pillar->floordest >= pillar->ceilingdest -
2236 sec->ceilingheight)
2237 {
2238 pillar->floorSpeed = args[1] * (FRACUNIT / 8);
2239 pillar->ceilingSpeed = FixedMul(sec->ceilingheight -
2240 pillar->ceilingdest,
2241 FixedDiv(pillar->floorSpeed,
2242 pillar->floordest -
2243 sec->floorheight));
2244 }
2245 else
2246 {
2247 pillar->ceilingSpeed = args[1] * (FRACUNIT / 8);
2248 pillar->floorSpeed =
2249 FixedMul(pillar->floordest - sec->floorheight,
2250 FixedDiv(pillar->ceilingSpeed,
2251 sec->ceilingheight - pillar->ceilingdest));
2252 }
2253 pillar->direction = -1; // open the pillar
2254 SN_StartSequence((mobj_t *) & pillar->sector->soundorg,
2255 SEQ_PLATFORM + pillar->sector->seqType);
2256 }
2257 return rtn;
2258 }
2259
2260 int EV_ZDoomFloorStop(int tag, line_t *line)
2261 {
2262 const int *id_p;
2263
2264 FIND_SECTORS2(id_p, tag, line)
2265 {
2266 sector_t *sec = &sectors[*id_p];
2267 floormove_t *floor = (floormove_t *) sec->floordata;
2268
2269 if (floor)
2270 {
2271 sec->floordata = NULL;
2272 P_RemoveThinker(&floor->thinker);
2273 }
2274 }
2275
2276 return 1;
2277 }
2278
2279 int EV_ZDoomFloorCrushStop(int tag)
2280 {
2281 const int *id_p;
2282
2283 FIND_SECTORS(id_p, tag)
2284 {
2285 sector_t *sec = &sectors[*id_p];
2286 floormove_t *floor = (floormove_t *) sec->floordata;
2287
2288 if (floor &&
2289 floor->thinker.function == T_MoveFloor &&
2290 floor->type == floorRaiseAndCrush)
2291 {
2292 sec->floordata = NULL;
2293 P_RemoveThinker(&floor->thinker);
2294 }
2295 }
2296
2297 return 1;
2298 }
2299
2300 int EV_FloorCrushStop(line_t * line, byte * args)
2301 {
2302 thinker_t *think;
2303 floormove_t *floor;
2304 dboolean rtn;
2305
2306 rtn = 0;
2307 for (think = thinkercap.next; think != &thinkercap; think = think->next)
2308 {
2309 if (think->function != T_MoveFloor)
2310 {
2311 continue;
2312 }
2313 floor = (floormove_t *) think;
2314 if (floor->type != FLEV_RAISEFLOORCRUSH)
2315 {
2316 continue;
2317 }
2318 // Completely remove the crushing floor
2319 SN_StopSequence((mobj_t *) & floor->sector->soundorg);
2320 floor->sector->floordata = NULL;
2321 P_TagFinished(floor->sector->tag);
2322 P_RemoveThinker(&floor->thinker);
2323 rtn = 1;
2324 }
2325 return rtn;
2326 }
2327
2328 #define WGLSTATE_EXPAND 1
2329 #define WGLSTATE_STABLE 2
2330 #define WGLSTATE_REDUCE 3
2331
2332 extern fixed_t FloatBobOffsets[64];
2333
2334 static void T_PlaneWaggle(planeWaggle_t * waggle, fixed_t * planeheight, void ** planedata)
2335 {
2336 switch (waggle->state)
2337 {
2338 case WGLSTATE_EXPAND:
2339 if ((waggle->scale += waggle->scaleDelta) >= waggle->targetScale)
2340 {
2341 waggle->scale = waggle->targetScale;
2342 waggle->state = WGLSTATE_STABLE;
2343 }
2344 break;
2345 case WGLSTATE_REDUCE:
2346 if ((waggle->scale -= waggle->scaleDelta) <= 0)
2347 { // Remove
2348 (*planeheight) = waggle->originalHeight;
2349 P_ChangeSector(waggle->sector, true);
2350 (*planedata) = NULL;
2351 P_TagFinished(waggle->sector->tag);
2352 P_RemoveThinker(&waggle->thinker);
2353 return;
2354 }
2355 break;
2356 case WGLSTATE_STABLE:
2357 if (waggle->ticker != -1)
2358 {
2359 if (!--waggle->ticker)
2360 {
2361 waggle->state = WGLSTATE_REDUCE;
2362 }
2363 }
2364 break;
2365 }
2366 waggle->accumulator += waggle->accDelta;
2367 (*planeheight) = waggle->originalHeight +
2368 FixedMul(FloatBobOffsets[(waggle->accumulator >> FRACBITS) & 63], waggle->scale);
2369 P_ChangeSector(waggle->sector, true);
2370 }
2371
2372 void T_FloorWaggle(planeWaggle_t * waggle)
2373 {
2374 T_PlaneWaggle(waggle, &waggle->sector->floorheight, &waggle->sector->floordata);
2375 }
2376
2377 void T_CeilingWaggle(planeWaggle_t * waggle)
2378 {
2379 T_PlaneWaggle(waggle, &waggle->sector->ceilingheight, &waggle->sector->ceilingdata);
2380 }
2381
2382 static void P_SpawnPlaneWaggle(sector_t *sector, int height, int speed,
2383 int offset, int timer, dboolean ceiling)
2384 {
2385 planeWaggle_t *waggle;
2386
2387 waggle = Z_MallocLevel(sizeof(*waggle));
2388 memset(waggle, 0, sizeof(*waggle));
2389 if (ceiling)
2390 {
2391 sector->ceilingdata = waggle;
2392 waggle->thinker.function = T_CeilingWaggle;
2393 waggle->originalHeight = sector->ceilingheight;
2394 }
2395 else
2396 {
2397 sector->floordata = waggle;
2398 waggle->thinker.function = T_FloorWaggle;
2399 waggle->originalHeight = sector->floorheight;
2400 }
2401 waggle->sector = sector;
2402 waggle->accumulator = offset * FRACUNIT;
2403 waggle->accDelta = speed << 10;
2404 waggle->scale = 0;
2405 waggle->targetScale = height << 10;
2406 waggle->scaleDelta = waggle->targetScale / (35 + ((3 * 35) * height) / 255);
2407 waggle->ticker = timer ? timer * 35 : -1;
2408 waggle->state = WGLSTATE_EXPAND;
2409 P_AddThinker(&waggle->thinker);
2410 }
2411
2412 dboolean EV_StartPlaneWaggle(int tag, line_t *line, int height,
2413 int speed, int offset, int timer, dboolean ceiling)
2414 {
2415 const int *id_p;
2416 sector_t *sector;
2417 dboolean retCode;
2418
2419 retCode = false;
2420 FIND_SECTORS2(id_p, tag, line)
2421 {
2422 sector = &sectors[*id_p];
2423 if (ceiling ? P_CeilingActive(sector) : P_FloorActive(sector))
2424 { // Already busy with another thinker
2425 continue;
2426 }
2427 retCode = true;
2428 P_SpawnPlaneWaggle(sector, height, speed, offset, timer, ceiling);
2429 }
2430
2431 return retCode;
2432 }
2433
2434 //==========================================================================
2435 //
2436 // EV_StartFloorWaggle
2437 //
2438 //==========================================================================
2439
2440 dboolean EV_StartFloorWaggle(int tag, int height, int speed, int offset,
2441 int timer)
2442 {
2443 const int *id_p;
2444 sector_t *sector;
2445 dboolean retCode;
2446
2447 retCode = false;
2448 FIND_SECTORS(id_p, tag)
2449 {
2450 sector = &sectors[*id_p];
2451 if (sector->floordata || sector->ceilingdata)
2452 { // Already busy with another thinker
2453 continue;
2454 }
2455 retCode = true;
2456 P_SpawnPlaneWaggle(sector, height, speed, offset, timer, false);
2457 }
2458 return retCode;
2459 }