p_floor.c 71.6 KB
Newer Older
Alam Ed Arias committed
1 2 3 4
// SONIC ROBO BLAST 2
//-----------------------------------------------------------------------------
// Copyright (C) 1993-1996 by id Software, Inc.
// Copyright (C) 1998-2000 by DooM Legacy Team.
James R. committed
5
// Copyright (C) 1999-2020 by Sonic Team Junior.
Alam Ed Arias committed
6 7 8 9 10 11 12 13 14 15
//
// This program is free software distributed under the
// terms of the GNU General Public License, version 2.
// See the 'LICENSE' file for more details.
//-----------------------------------------------------------------------------
/// \file  p_floor.c
/// \brief Floor animation, elevators

#include "doomdef.h"
#include "doomstat.h"
16
#include "m_random.h"
Alam Ed Arias committed
17
#include "p_local.h"
toaster committed
18
#include "p_slopes.h"
Alam Ed Arias committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32
#include "r_state.h"
#include "s_sound.h"
#include "z_zone.h"
#include "g_game.h"
#include "r_main.h"

// ==========================================================================
//                              FLOORS
// ==========================================================================

//
// Move a plane (floor or ceiling) and check for crushing
//
result_e T_MovePlane(sector_t *sector, fixed_t speed, fixed_t dest, boolean crush,
33
	boolean ceiling, INT32 direction)
Alam Ed Arias committed
34 35 36 37 38
{
	fixed_t lastpos;
	fixed_t destheight; // used to keep floors/ceilings from moving through each other
	sector->moved = true;

39
	if (ceiling)
Alam Ed Arias committed
40
	{
41 42 43 44 45 46 47 48 49 50 51 52
		lastpos = sector->ceilingheight;
		// moving a ceiling
		switch (direction)
		{
			case -1:
				// moving a ceiling down
				// keep ceiling from moving through floors
				destheight = (dest > sector->floorheight) ? dest : sector->floorheight;
				if (sector->ceilingheight - speed < destheight)
				{
					sector->ceilingheight = destheight;
					if (P_CheckSector(sector, crush))
Alam Ed Arias committed
53
					{
54 55
						sector->ceilingheight = lastpos;
						P_CheckSector(sector, crush);
Alam Ed Arias committed
56
					}
57 58 59 60 61 62 63
					return pastdest;
				}
				else
				{
					// crushing is possible
					sector->ceilingheight -= speed;
					if (P_CheckSector(sector, crush))
Alam Ed Arias committed
64
					{
65 66 67
						sector->ceilingheight = lastpos;
						P_CheckSector(sector, crush);
						return crushed;
Alam Ed Arias committed
68
					}
69 70
				}
				break;
Alam Ed Arias committed
71

72 73 74 75 76 77
			case 1:
				// moving a ceiling up
				if (sector->ceilingheight + speed > dest)
				{
					sector->ceilingheight = dest;
					if (P_CheckSector(sector, crush) && sector->numattached)
Alam Ed Arias committed
78
					{
79 80
						sector->ceilingheight = lastpos;
						P_CheckSector(sector, crush);
Alam Ed Arias committed
81
					}
82 83 84 85 86 87
					return pastdest;
				}
				else
				{
					sector->ceilingheight += speed;
					if (P_CheckSector(sector, crush) && sector->numattached)
Alam Ed Arias committed
88
					{
89 90 91
						sector->ceilingheight = lastpos;
						P_CheckSector(sector, crush);
						return crushed;
Alam Ed Arias committed
92
					}
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
				}
				break;
		}
	}
	else
	{
		lastpos = sector->floorheight;
		// moving a floor
		switch (direction)
		{
			case -1:
				// Moving a floor down
				if (sector->floorheight - speed < dest)
				{
					sector->floorheight = dest;
					if (P_CheckSector(sector, crush) && sector->numattached)
Alam Ed Arias committed
109
					{
110 111
						sector->floorheight = lastpos;
						P_CheckSector(sector, crush);
Alam Ed Arias committed
112
					}
113 114 115 116 117 118
					return pastdest;
				}
				else
				{
					sector->floorheight -= speed;
					if (P_CheckSector(sector, crush) && sector->numattached)
Alam Ed Arias committed
119
					{
120 121 122
						sector->floorheight = lastpos;
						P_CheckSector(sector, crush);
						return crushed;
Alam Ed Arias committed
123
					}
124 125
				}
				break;
Alam Ed Arias committed
126

127 128 129 130 131 132 133 134
			case 1:
				// Moving a floor up
				// keep floor from moving through ceilings
				destheight = (dest < sector->ceilingheight) ? dest : sector->ceilingheight;
				if (sector->floorheight + speed > destheight)
				{
					sector->floorheight = destheight;
					if (P_CheckSector(sector, crush))
Alam Ed Arias committed
135
					{
136 137
						sector->floorheight = lastpos;
						P_CheckSector(sector, crush);
Alam Ed Arias committed
138
					}
139 140 141 142 143 144 145
					return pastdest;
				}
				else
				{
					// crushing is possible
					sector->floorheight += speed;
					if (P_CheckSector(sector, crush))
Alam Ed Arias committed
146
					{
147 148 149
						sector->floorheight = lastpos;
						P_CheckSector(sector, crush);
						return crushed;
Alam Ed Arias committed
150
					}
151 152 153
				}
				break;
		}
Alam Ed Arias committed
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
	}

	return ok;
}

//
// MOVE A FLOOR TO ITS DESTINATION (UP OR DOWN)
//
void T_MoveFloor(floormove_t *movefloor)
{
	result_e res = 0;
	boolean dontupdate = false;

	if (movefloor->delaytimer)
	{
		movefloor->delaytimer--;
		return;
	}

	res = T_MovePlane(movefloor->sector,
	                  movefloor->speed,
	                  movefloor->floordestheight,
176
	                  movefloor->crush, false, movefloor->direction);
Alam Ed Arias committed
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199

	if (movefloor->type == bounceFloor)
	{
		const fixed_t origspeed = FixedDiv(movefloor->origspeed,(ELEVATORSPEED/2));
		const fixed_t fs = abs(movefloor->sector->floorheight - lines[movefloor->texture].frontsector->floorheight);
		const fixed_t bs = abs(movefloor->sector->floorheight - lines[movefloor->texture].backsector->floorheight);
		if (fs < bs)
			movefloor->speed = FixedDiv(fs,25*FRACUNIT) + FRACUNIT/4;
		else
			movefloor->speed = FixedDiv(bs,25*FRACUNIT) + FRACUNIT/4;

		movefloor->speed = FixedMul(movefloor->speed,origspeed);
	}

	if (res == pastdest)
	{
		if (movefloor->direction == 1)
		{
			switch (movefloor->type)
			{
				case moveFloorByFrontSector:
					if (movefloor->texture < -1) // chained linedef executing
						P_LinedefExecute((INT16)(movefloor->texture + INT16_MAX + 2), NULL, NULL);
200
					/* FALLTHRU */
Alam Ed Arias committed
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
				case instantMoveFloorByFrontSector:
					if (movefloor->texture > -1) // flat changing
						movefloor->sector->floorpic = movefloor->texture;
					break;
				case bounceFloor: // Graue 03-12-2004
					if (movefloor->floordestheight == lines[movefloor->texture].frontsector->floorheight)
						movefloor->floordestheight = lines[movefloor->texture].backsector->floorheight;
					else
						movefloor->floordestheight = lines[movefloor->texture].frontsector->floorheight;
					movefloor->direction = (movefloor->floordestheight < movefloor->sector->floorheight) ? -1 : 1;
					movefloor->sector->floorspeed = movefloor->speed * movefloor->direction;
					movefloor->delaytimer = movefloor->delay;
					P_RecalcPrecipInSector(movefloor->sector);
					return; // not break, why did this work? Graue 04-03-2004
				case bounceFloorCrush: // Graue 03-27-2004
					if (movefloor->floordestheight == lines[movefloor->texture].frontsector->floorheight)
					{
						movefloor->floordestheight = lines[movefloor->texture].backsector->floorheight;
						movefloor->speed = movefloor->origspeed = FixedDiv(abs(lines[movefloor->texture].dy),4*FRACUNIT); // return trip, use dy
					}
					else
					{
						movefloor->floordestheight = lines[movefloor->texture].frontsector->floorheight;
						movefloor->speed = movefloor->origspeed = FixedDiv(abs(lines[movefloor->texture].dx),4*FRACUNIT); // forward again, use dx
					}
					movefloor->direction = (movefloor->floordestheight < movefloor->sector->floorheight) ? -1 : 1;
					movefloor->sector->floorspeed = movefloor->speed * movefloor->direction;
					movefloor->delaytimer = movefloor->delay;
					P_RecalcPrecipInSector(movefloor->sector);
					return; // not break, why did this work? Graue 04-03-2004
				case crushFloorOnce:
					movefloor->floordestheight = lines[movefloor->texture].frontsector->floorheight;
					movefloor->direction = -1;
					movefloor->sector->soundorg.z = movefloor->sector->floorheight;
					S_StartSound(&movefloor->sector->soundorg,sfx_pstop);
					P_RecalcPrecipInSector(movefloor->sector);
					return;
				default:
					break;
			}
		}
		else if (movefloor->direction == -1)
		{
			switch (movefloor->type)
			{
				case moveFloorByFrontSector:
					if (movefloor->texture < -1) // chained linedef executing
						P_LinedefExecute((INT16)(movefloor->texture + INT16_MAX + 2), NULL, NULL);
249
					/* FALLTHRU */
Alam Ed Arias committed
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
				case instantMoveFloorByFrontSector:
					if (movefloor->texture > -1) // flat changing
						movefloor->sector->floorpic = movefloor->texture;
					break;
				case bounceFloor: // Graue 03-12-2004
					if (movefloor->floordestheight == lines[movefloor->texture].frontsector->floorheight)
						movefloor->floordestheight = lines[movefloor->texture].backsector->floorheight;
					else
						movefloor->floordestheight = lines[movefloor->texture].frontsector->floorheight;
					movefloor->direction = (movefloor->floordestheight < movefloor->sector->floorheight) ? -1 : 1;
					movefloor->sector->floorspeed = movefloor->speed * movefloor->direction;
					movefloor->delaytimer = movefloor->delay;
					P_RecalcPrecipInSector(movefloor->sector);
					return; // not break, why did this work? Graue 04-03-2004
				case bounceFloorCrush: // Graue 03-27-2004
					if (movefloor->floordestheight == lines[movefloor->texture].frontsector->floorheight)
					{
						movefloor->floordestheight = lines[movefloor->texture].backsector->floorheight;
						movefloor->speed = movefloor->origspeed = FixedDiv(abs(lines[movefloor->texture].dy),4*FRACUNIT); // return trip, use dy
					}
					else
					{
						movefloor->floordestheight = lines[movefloor->texture].frontsector->floorheight;
						movefloor->speed = movefloor->origspeed = FixedDiv(abs(lines[movefloor->texture].dx),4*FRACUNIT); // forward again, use dx
					}
					movefloor->direction = (movefloor->floordestheight < movefloor->sector->floorheight) ? -1 : 1;
					movefloor->sector->floorspeed = movefloor->speed * movefloor->direction;
					movefloor->delaytimer = movefloor->delay;
					P_RecalcPrecipInSector(movefloor->sector);
					return; // not break, why did this work? Graue 04-03-2004
				case crushFloorOnce:
					movefloor->sector->floordata = NULL; // Clear up the thinker so others can use it
					P_RemoveThinker(&movefloor->thinker);
					movefloor->sector->floorspeed = 0;
					P_RecalcPrecipInSector(movefloor->sector);
					return;
				default:
					break;
			}
		}

		movefloor->sector->floordata = NULL; // Clear up the thinker so others can use it
		movefloor->sector->floorspeed = 0;
		P_RemoveThinker(&movefloor->thinker);
		dontupdate = true;
	}
	if (!dontupdate)
		movefloor->sector->floorspeed = movefloor->speed*movefloor->direction;
	else
		movefloor->sector->floorspeed = 0;

	P_RecalcPrecipInSector(movefloor->sector);
}

//
// T_MoveElevator
//
// Move an elevator to it's destination (up or down)
// Called once per tick for each moving floor.
//
// Passed an elevator_t structure that contains all pertinent info about the
// move. See p_spec.h for fields.
// No return.
//
// The function moves the planes differently based on direction, so if it's
// traveling really fast, the floor and ceiling won't hit each other and
// stop the lift.
void T_MoveElevator(elevator_t *elevator)
{
	result_e res1 = 0, res2 = 0, res = 0;
	boolean dontupdate = false;
	fixed_t oldfloor, oldceiling;

	if (elevator->delaytimer)
	{
		elevator->delaytimer--;
		return;
	}

	if (elevator->direction < 0) // moving down
	{
		if (elevator->type == elevateContinuous)
		{
			const fixed_t origspeed = FixedDiv(elevator->origspeed,(ELEVATORSPEED/2));
			const fixed_t wh = abs(elevator->sector->floorheight - elevator->floorwasheight);
			const fixed_t dh = abs(elevator->sector->floorheight - elevator->floordestheight);

			// Slow down when reaching destination Tails 12-06-2000
			if (wh < dh)
				elevator->speed = FixedDiv(wh,25*FRACUNIT) + FRACUNIT/4;
			else
				elevator->speed = FixedDiv(dh,25*FRACUNIT) + FRACUNIT/4;

			if (elevator->origspeed)
			{
				elevator->speed = FixedMul(elevator->speed,origspeed);
				if (elevator->speed > elevator->origspeed)
					elevator->speed = (elevator->origspeed);
				if (elevator->speed < 1)
					elevator->speed = 1;
			}
			else
			{
				if (elevator->speed > 3*FRACUNIT)
					elevator->speed = 3*FRACUNIT;
				if (elevator->speed < 1)
					elevator->speed = 1;
			}
		}

		oldfloor = elevator->sector->floorheight;
		oldceiling = elevator->sector->ceilingheight;

		res1 = T_MovePlane             //jff 4/7/98 reverse order of ceiling/floor
		(
			elevator->sector,
			elevator->speed,
			elevator->ceilingdestheight,
			elevator->distance,
369
			true,                          // move ceiling
Alam Ed Arias committed
370 371 372 373 374 375 376 377 378
			elevator->direction
		);

		res2 = T_MovePlane
		(
			elevator->sector,
			elevator->speed,
			elevator->floordestheight,
			elevator->distance,
379
			false,                        // move floor
Alam Ed Arias committed
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
			elevator->direction
		);

		if (elevator->distance && (res1 == crushed || res2 == crushed))
		{
			res = crushed;
			elevator->sector->floorheight = oldfloor;
			elevator->sector->ceilingheight = oldceiling;
		}
		else
			res = res1;
	}
	else // moving up
	{
		if (elevator->type == elevateContinuous)
		{
			const fixed_t origspeed = FixedDiv(elevator->origspeed,(ELEVATORSPEED/2));
			const fixed_t wc = abs(elevator->sector->ceilingheight - elevator->ceilingwasheight);
			const fixed_t dc = abs(elevator->sector->ceilingheight - elevator->ceilingdestheight);
			// Slow down when reaching destination Tails 12-06-2000
			if (wc < dc)
				elevator->speed = FixedDiv(wc,25*FRACUNIT) + FRACUNIT/4;
			else
				elevator->speed = FixedDiv(dc,25*FRACUNIT) + FRACUNIT/4;

			if (elevator->origspeed)
			{
				elevator->speed = FixedMul(elevator->speed,origspeed);
				if (elevator->speed > elevator->origspeed)
					elevator->speed = (elevator->origspeed);
				if (elevator->speed < 1)
					elevator->speed = 1;
			}
			else
			{
				if (elevator->speed > 3*FRACUNIT)
					elevator->speed = 3*FRACUNIT;
				if (elevator->speed < 1)
					elevator->speed = 1;
			}
		}

		oldfloor = elevator->sector->floorheight;
		oldceiling = elevator->sector->ceilingheight;

		res1 = T_MovePlane             //jff 4/7/98 reverse order of ceiling/floor
		(
			elevator->sector,
			elevator->speed,
			elevator->floordestheight,
			elevator->distance,
431
			false,                          // move floor
Alam Ed Arias committed
432 433 434 435 436 437 438 439 440 441 442
			elevator->direction
		);

		if (res1 != crushed)
		{
			res2 = T_MovePlane
			(
				elevator->sector,
				elevator->speed,
				elevator->ceilingdestheight,
				elevator->distance,
443
				true,                        // move ceiling
Alam Ed Arias committed
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
				elevator->direction
			);
		}

		if (elevator->distance && (res1 == crushed || res2 == crushed))
		{
			res = crushed;
			elevator->sector->floorheight = oldfloor;
			elevator->sector->ceilingheight = oldceiling;
		}
		else
			res = res1;
	}
/*
	// make floor move sound
	if (!(leveltime&7))
		S_StartSound(&elevator->sector->soundorg, sfx_stnmov);
*/
	if (res == pastdest || res == crushed)            // if destination height acheived
	{
		if (elevator->type == elevateContinuous)
		{
			if (elevator->direction > 0)
			{
				elevator->high = 1;
				elevator->low = 0;
				elevator->direction = -1;

				if (elevator->origspeed)
					elevator->speed = elevator->origspeed;
				else
					elevator->speed = 3*FRACUNIT;

				elevator->floorwasheight = elevator->floordestheight;
				elevator->ceilingwasheight = elevator->ceilingdestheight;

				if (elevator->low)
				{
					elevator->floordestheight =
						P_FindNextHighestFloor(elevator->sector, elevator->sector->floorheight);
					elevator->ceilingdestheight =
						elevator->floordestheight + elevator->sector->ceilingheight - elevator->sector->floorheight;
				}
				else
				{
					elevator->floordestheight =
						P_FindNextLowestFloor(elevator->sector,elevator->sector->floorheight);
					elevator->ceilingdestheight =
						elevator->floordestheight + elevator->sector->ceilingheight - elevator->sector->floorheight;
				}
//				T_MoveElevator(elevator);
			}
			else
			{
				elevator->high = 0;
				elevator->low = 1;
				elevator->direction = 1;

				if (elevator->origspeed)
					elevator->speed = elevator->origspeed;
				else
					elevator->speed = 3*FRACUNIT;

				elevator->floorwasheight = elevator->floordestheight;
				elevator->ceilingwasheight = elevator->ceilingdestheight;

				if (elevator->low)
				{
					elevator->floordestheight =
						P_FindNextHighestFloor(elevator->sector, elevator->sector->floorheight);
					elevator->ceilingdestheight =
						elevator->floordestheight + elevator->sector->ceilingheight - elevator->sector->floorheight;
				}
				else
				{
					elevator->floordestheight =
						P_FindNextLowestFloor(elevator->sector,elevator->sector->floorheight);
					elevator->ceilingdestheight =
						elevator->floordestheight + elevator->sector->ceilingheight - elevator->sector->floorheight;
				}
//				T_MoveElevator(elevator);
			}
			elevator->delaytimer = elevator->delay;
		}
		else
		{
			elevator->sector->floordata = NULL;     //jff 2/22/98
			elevator->sector->ceilingdata = NULL;   //jff 2/22/98
			elevator->sector->ceilspeed = 0;
			elevator->sector->floorspeed = 0;
			P_RemoveThinker(&elevator->thinker);    // remove elevator from actives
			dontupdate = true;
		}
		// make floor stop sound
		// S_StartSound(&elevator->sector->soundorg, sfx_pstop);
	}
	if (!dontupdate)
	{
		elevator->sector->floorspeed = elevator->speed*elevator->direction;
		elevator->sector->ceilspeed = 42;
	}
	else
	{
		elevator->sector->floorspeed = 0;
		elevator->sector->ceilspeed = 0;
		elevator->sector->floordata = NULL;
		elevator->sector->ceilingdata = NULL;
	}
}

//
// T_ContinuousFalling
//
// A sector that continuously falls until its ceiling
// is below that of its actionsector's floor, then
// it instantly returns to its original position and
// falls again.
//
// Useful for things like intermittent falling lava.
//
564
void T_ContinuousFalling(continuousfall_t *faller)
Alam Ed Arias committed
565
{
566 567
	faller->sector->ceilingheight += faller->speed*faller->direction;
	faller->sector->floorheight += faller->speed*faller->direction;
Alam Ed Arias committed
568 569 570

	P_CheckSector(faller->sector, false);

571 572
	if ((faller->direction == -1 && faller->sector->ceilingheight <= faller->destheight)
		|| (faller->direction == 1 && faller->sector->floorheight >= faller->destheight))
Alam Ed Arias committed
573
	{
574 575
		faller->sector->ceilingheight = faller->ceilingstartheight;
		faller->sector->floorheight = faller->floorstartheight;
Alam Ed Arias committed
576 577
	}

toaster committed
578 579
	P_CheckSector(faller->sector, false); // you might think this is irrelevant. you would be wrong

Alam Ed Arias committed
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
	faller->sector->floorspeed = faller->speed*faller->direction;
	faller->sector->ceilspeed = 42;
	faller->sector->moved = true;
}

//
// P_SectorCheckWater
//
// Like P_MobjCheckWater, but takes a sector instead of a mobj.
static fixed_t P_SectorCheckWater(sector_t *analyzesector,
	sector_t *elevatorsec)
{
	fixed_t watertop;

	// Default if no water exists.
	watertop = analyzesector->floorheight - 512*FRACUNIT;

	// see if we are in water, and set some flags for later
	if (analyzesector->ffloors)
	{
		ffloor_t *rover;

		for (rover = analyzesector->ffloors; rover; rover = rover->next)
		{
			if (!(rover->flags & FF_EXISTS) || !(rover->flags & FF_SWIMMABLE) || rover->flags & FF_SOLID)
				continue;

			// If the sector is below the water, don't bother.
			if ((elevatorsec->ceilingheight + elevatorsec->floorheight)>>1 < *rover->bottomheight)
				continue;

			// Do the same as above if the water is too shallow.
			if (*rover->topheight < analyzesector->floorheight + abs((elevatorsec->ceilingheight - elevatorsec->floorheight)>>1))
				continue;

			if (*rover->topheight > watertop) // highest water block is the one to go for
				watertop = *rover->topheight;
		}
	}

	return watertop;
}

//////////////////////////////////////////////////
// T_BounceCheese ////////////////////////////////
//////////////////////////////////////////////////
// Bounces a floating cheese

628
void T_BounceCheese(bouncecheese_t *bouncer)
Alam Ed Arias committed
629
{
630
	fixed_t sectorheight;
Alam Ed Arias committed
631 632 633 634
	fixed_t halfheight;
	fixed_t waterheight;
	fixed_t floorheight;
	sector_t *actionsector;
635
	boolean remove;
Alam Ed Arias committed
636
	INT32 i;
637
	mtag_t tag = Tag_FGet(&bouncer->sourceline->tags);
638
	TAG_ITER_DECLARECOUNTER(0);
Alam Ed Arias committed
639

640 641
	if (bouncer->sector->crumblestate == CRUMBLE_RESTORE || bouncer->sector->crumblestate == CRUMBLE_WAIT
		|| bouncer->sector->crumblestate == CRUMBLE_ACTIVATED) // Oops! Crumbler says to remove yourself!
Alam Ed Arias committed
642
	{
643
		bouncer->sector->crumblestate = CRUMBLE_WAIT;
Alam Ed Arias committed
644 645 646 647 648 649 650 651 652
		bouncer->sector->ceilingdata = NULL;
		bouncer->sector->ceilspeed = 0;
		bouncer->sector->floordata = NULL;
		bouncer->sector->floorspeed = 0;
		P_RemoveThinker(&bouncer->thinker); // remove bouncer from actives
		return;
	}

	// You can use multiple target sectors, but at your own risk!!!
653
	TAG_ITER_SECTORS(0, tag, i)
Alam Ed Arias committed
654 655 656 657
	{
		actionsector = &sectors[i];
		actionsector->moved = true;

658 659
		sectorheight = abs(bouncer->sector->ceilingheight - bouncer->sector->floorheight);
		halfheight = sectorheight/2;
Alam Ed Arias committed
660 661 662

		waterheight = P_SectorCheckWater(actionsector, bouncer->sector); // sorts itself out if there's no suitable water in the sector

663 664 665
		floorheight = P_FloorzAtPos(actionsector->soundorg.x, actionsector->soundorg.y, bouncer->sector->floorheight, sectorheight);

		remove = false;
Alam Ed Arias committed
666 667 668 669 670

		// Water level is up to the ceiling.
		if (waterheight > bouncer->sector->ceilingheight - halfheight && bouncer->sector->ceilingheight >= actionsector->ceilingheight) // Tails 01-08-2004
		{
			bouncer->sector->ceilingheight = actionsector->ceilingheight;
671 672
			bouncer->sector->floorheight = actionsector->ceilingheight - sectorheight;
			remove = true;
Alam Ed Arias committed
673 674 675 676
		}
		// Water level is too shallow.
		else if (waterheight < bouncer->sector->floorheight + halfheight && bouncer->sector->floorheight <= floorheight)
		{
677
			bouncer->sector->ceilingheight = floorheight + sectorheight;
Alam Ed Arias committed
678
			bouncer->sector->floorheight = floorheight;
679 680 681 682 683 684 685 686 687 688
			remove = true;
		}
		else
		{
			bouncer->ceilingwasheight = waterheight + halfheight;
			bouncer->floorwasheight = waterheight - halfheight;
		}

		if (remove)
		{
689 690
			T_MovePlane(bouncer->sector, 0, bouncer->sector->ceilingheight, false, true, -1); // update things on ceiling
			T_MovePlane(bouncer->sector, 0, bouncer->sector->floorheight, false, false, -1); // update things on floor
Alam Ed Arias committed
691 692 693 694 695 696 697 698 699 700
			P_RecalcPrecipInSector(actionsector);
			bouncer->sector->ceilingdata = NULL;
			bouncer->sector->floordata = NULL;
			bouncer->sector->floorspeed = 0;
			bouncer->sector->ceilspeed = 0;
			bouncer->sector->moved = true;
			P_RemoveThinker(&bouncer->thinker); // remove bouncer from actives
			return;
		}

701 702 703 704 705 706 707 708 709 710 711 712 713 714
		if (bouncer->speed >= 0) // move floor first to fix height desync and any bizarre bugs following that
		{
			T_MovePlane(bouncer->sector, bouncer->speed/2, bouncer->sector->floorheight - 70*FRACUNIT,
				false, false, -1); // move floor
			T_MovePlane(bouncer->sector, bouncer->speed/2, bouncer->sector->ceilingheight -
				70*FRACUNIT, false, true, -1); // move ceiling
		}
		else
		{
			T_MovePlane(bouncer->sector, bouncer->speed/2, bouncer->sector->ceilingheight -
				70*FRACUNIT, false, true, -1); // move ceiling
			T_MovePlane(bouncer->sector, bouncer->speed/2, bouncer->sector->floorheight - 70*FRACUNIT,
				false, false, -1); // move floor
		}
Alam Ed Arias committed
715 716 717 718

		bouncer->sector->floorspeed = -bouncer->speed/2;
		bouncer->sector->ceilspeed = 42;

719
		if ((bouncer->sector->ceilingheight < bouncer->ceilingwasheight && !bouncer->low) // Down
720
			|| (bouncer->sector->ceilingheight > bouncer->ceilingwasheight && bouncer->low)) // Up
Alam Ed Arias committed
721 722 723 724 725 726
		{
			if (abs(bouncer->speed) < 6*FRACUNIT)
				bouncer->speed -= bouncer->speed/3;
			else
				bouncer->speed -= bouncer->speed/2;

727
			bouncer->low = !bouncer->low;
Alam Ed Arias committed
728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744
			if (abs(bouncer->speed) > 6*FRACUNIT)
			{
				mobj_t *mp = (void *)&actionsector->soundorg;
				actionsector->soundorg.z = bouncer->sector->floorheight;
				S_StartSound(mp, sfx_splash);
			}
		}

		if (bouncer->sector->ceilingheight < bouncer->ceilingwasheight) // Down
		{
			bouncer->speed -= bouncer->distance;
		}
		else if (bouncer->sector->ceilingheight > bouncer->ceilingwasheight) // Up
		{
			bouncer->speed += gravity;
		}

Alam Ed Arias committed
745 746
		if (abs(bouncer->speed) < 2*FRACUNIT
		&& abs(bouncer->sector->ceilingheight-bouncer->ceilingwasheight) < FRACUNIT/4)
Alam Ed Arias committed
747 748 749
		{
			bouncer->sector->floorheight = bouncer->floorwasheight;
			bouncer->sector->ceilingheight = bouncer->ceilingwasheight;
750 751
			T_MovePlane(bouncer->sector, 0, bouncer->sector->ceilingheight, false, true, -1); // update things on ceiling
			T_MovePlane(bouncer->sector, 0, bouncer->sector->floorheight, false, false, -1); // update things on floor
Alam Ed Arias committed
752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771
			bouncer->sector->ceilingdata = NULL;
			bouncer->sector->floordata = NULL;
			bouncer->sector->floorspeed = 0;
			bouncer->sector->ceilspeed = 0;
			bouncer->sector->moved = true;
			P_RemoveThinker(&bouncer->thinker);    // remove bouncer from actives
		}

		if (bouncer->distance > 0)
			bouncer->distance--;

		if (actionsector)
			P_RecalcPrecipInSector(actionsector);
	}
}

//////////////////////////////////////////////////
// T_StartCrumble ////////////////////////////////
//////////////////////////////////////////////////
// Crumbling platform Tails 03-11-2002
772
void T_StartCrumble(crumble_t *crumble)
Alam Ed Arias committed
773 774 775 776
{
	ffloor_t *rover;
	sector_t *sector;
	INT32 i;
777
	mtag_t tag = Tag_FGet(&crumble->sourceline->tags);
778
	TAG_ITER_DECLARECOUNTER(0);
Alam Ed Arias committed
779 780 781

	// Once done, the no-return thinker just sits there,
	// constantly 'returning'... kind of an oxymoron, isn't it?
782 783 784
	if ((((crumble->flags & CF_REVERSE) && crumble->direction == -1)
		|| (!(crumble->flags & CF_REVERSE) && crumble->direction == 1))
		&& !(crumble->flags & CF_RETURN))
Alam Ed Arias committed
785
	{
786 787
		crumble->sector->ceilspeed = 0;
		crumble->sector->floorspeed = 0;
Alam Ed Arias committed
788 789 790
		return;
	}

791
	if (crumble->timer != 0)
Alam Ed Arias committed
792
	{
793
		if (crumble->timer > 0) // Count down the timer
Alam Ed Arias committed
794
		{
795 796
			if (--crumble->timer <= 0)
				crumble->timer = -15*TICRATE; // Timer until platform returns to original position.
Alam Ed Arias committed
797 798 799
			else
			{
				// Timer isn't up yet, so just keep waiting.
800 801
				crumble->sector->ceilspeed = 0;
				crumble->sector->floorspeed = 0;
Alam Ed Arias committed
802 803 804
				return;
			}
		}
805
		else if (++crumble->timer == 0) // Reposition back to original spot
Alam Ed Arias committed
806
		{
807
			TAG_ITER_SECTORS(0, tag, i)
Alam Ed Arias committed
808 809 810 811 812
			{
				sector = &sectors[i];

				for (rover = sector->ffloors; rover; rover = rover->next)
				{
813 814
					if (!(rover->flags & FF_CRUMBLE))
						continue;
Alam Ed Arias committed
815

816 817 818 819 820 821 822 823 824 825
					if (!(rover->flags & FF_FLOATBOB))
						continue;

					if (rover->master != crumble->sourceline)
						continue;

					rover->alpha = crumble->origalpha;

					if (rover->alpha == 0xff)
						rover->flags &= ~FF_TRANSLUCENT;
Alam Ed Arias committed
826 827 828 829
				}
			}

			// Up!
830
			if (crumble->flags & CF_REVERSE)
831
				crumble->direction = -1;
Alam Ed Arias committed
832
			else
833
				crumble->direction = 1;
Alam Ed Arias committed
834

835 836
			crumble->sector->ceilspeed = 0;
			crumble->sector->floorspeed = 0;
Alam Ed Arias committed
837 838 839 840
			return;
		}

		// Flash to indicate that the platform is about to return.
841
		if (crumble->timer > -224 && (leveltime % ((abs(crumble->timer)/8) + 1) == 0))
Alam Ed Arias committed
842
		{
843
			TAG_ITER_SECTORS(0, tag, i)
Alam Ed Arias committed
844 845 846 847 848
			{
				sector = &sectors[i];

				for (rover = sector->ffloors; rover; rover = rover->next)
				{
849 850 851 852 853 854 855 856 857 858 859 860 861
					if (rover->flags & FF_NORETURN)
						continue;

					if (!(rover->flags & FF_CRUMBLE))
						continue;

					if (!(rover->flags & FF_FLOATBOB))
						continue;

					if (rover->master != crumble->sourceline)
						continue;

					if (rover->alpha == crumble->origalpha)
Alam Ed Arias committed
862
					{
863 864 865 866
						rover->flags |= FF_TRANSLUCENT;
						rover->alpha = 0x00;
					}
					else
Alam Ed Arias committed
867
					{
868 869 870 871
						rover->alpha = crumble->origalpha;

						if (rover->alpha == 0xff)
							rover->flags &= ~FF_TRANSLUCENT;
Alam Ed Arias committed
872 873 874 875 876 877 878 879
					}
				}
			}
		}

		// We're about to go back to the original position,
		// so set this to let other thinkers know what is
		// about to happen.
880
		if (crumble->timer < 0 && crumble->timer > -3)
881
			crumble->sector->crumblestate = CRUMBLE_RESTORE; // makes T_BounceCheese remove itself
Alam Ed Arias committed
882 883
	}

884 885
	if ((!(crumble->flags & CF_REVERSE) && crumble->direction == -1)
		|| ((crumble->flags & CF_REVERSE) && crumble->direction == 1)) // Down
Alam Ed Arias committed
886
	{
887
		crumble->sector->crumblestate = CRUMBLE_FALL; // Allow floating now.
Alam Ed Arias committed
888 889

		// Only fall like this if it isn't meant to float on water
890
		if (!(crumble->flags & CF_FLOATBOB))
Alam Ed Arias committed
891
		{
892
			crumble->speed += gravity; // Gain more and more speed
Alam Ed Arias committed
893

894 895
			if ((!(crumble->flags & CF_REVERSE) && crumble->sector->ceilingheight >= -16384*FRACUNIT)
				|| ((crumble->flags & CF_REVERSE) && crumble->sector->ceilingheight <= 16384*FRACUNIT))
Alam Ed Arias committed
896 897 898
			{
				T_MovePlane             //jff 4/7/98 reverse order of ceiling/floor
				(
899 900
				  crumble->sector,
				  crumble->speed,
901
				  crumble->sector->ceilingheight + crumble->direction*crumble->speed*2,
902 903
				  false,
				  true, // move ceiling
904
				  crumble->direction
Alam Ed Arias committed
905 906
				);

907 908 909 910 911 912 913 914
				T_MovePlane
				(
				  crumble->sector,
				  crumble->speed,
				  crumble->sector->floorheight + crumble->direction*crumble->speed*2,
				  false,
				  false, // move floor
				  crumble->direction
Alam Ed Arias committed
915 916
				);

917 918
				crumble->sector->ceilspeed = 42;
				crumble->sector->floorspeed = crumble->speed*crumble->direction;
Alam Ed Arias committed
919 920 921 922 923
			}
		}
	}
	else // Up (restore to original position)
	{
924 925 926 927 928 929 930 931 932
		crumble->sector->crumblestate = CRUMBLE_WAIT;
		crumble->sector->ceilingheight = crumble->ceilingwasheight;
		crumble->sector->floorheight = crumble->floorwasheight;
		crumble->sector->floordata = NULL;
		crumble->sector->ceilingdata = NULL;
		crumble->sector->ceilspeed = 0;
		crumble->sector->floorspeed = 0;
		crumble->sector->moved = true;
		P_RemoveThinker(&crumble->thinker);
Alam Ed Arias committed
933 934
	}

935
	TAG_ITER_SECTORS(0, tag, i)
Alam Ed Arias committed
936 937 938 939 940 941 942 943 944 945 946 947
	{
		sector = &sectors[i];
		sector->moved = true;
		P_RecalcPrecipInSector(sector);
	}
}

//////////////////////////////////////////////////
// T_MarioBlock //////////////////////////////////
//////////////////////////////////////////////////
// Mario hits a block!
//
948
void T_MarioBlock(mariothink_t *block)
Alam Ed Arias committed
949 950
{
	INT32 i;
951
	TAG_ITER_DECLARECOUNTER(0);
Alam Ed Arias committed
952 953 954 955 956 957

	T_MovePlane
	(
	  block->sector,
	  block->speed,
	  block->sector->ceilingheight + 70*FRACUNIT * block->direction,
958 959
	  false,
	  true, // move ceiling
Alam Ed Arias committed
960 961 962 963 964 965 966 967
	  block->direction
	);

	T_MovePlane
	(
	  block->sector,
	  block->speed,
	  block->sector->floorheight + 70*FRACUNIT * block->direction,
968 969
	  false,
	  false, // move floor
Alam Ed Arias committed
970 971 972
	  block->direction
	);

973 974 975
	if (block->sector->ceilingheight >= block->ceilingstartheight + 32*FRACUNIT) // Go back down now..
		block->direction *= -1;
	else if (block->sector->ceilingheight <= block->ceilingstartheight)
Alam Ed Arias committed
976
	{
977 978
		block->sector->ceilingheight = block->ceilingstartheight;
		block->sector->floorheight = block->floorstartheight;
Alam Ed Arias committed
979 980 981 982 983
		P_RemoveThinker(&block->thinker);
		block->sector->floordata = NULL;
		block->sector->ceilingdata = NULL;
		block->sector->floorspeed = 0;
		block->sector->ceilspeed = 0;
toaster committed
984
		block->direction = 0;
Alam Ed Arias committed
985
	}
986
	TAG_ITER_SECTORS(0, (INT16)block->tag, i)
Alam Ed Arias committed
987 988 989
		P_RecalcPrecipInSector(&sectors[i]);
}

MascaraSnake committed
990
void T_FloatSector(floatthink_t *floater)
Alam Ed Arias committed
991 992
{
	fixed_t cheeseheight;
MascaraSnake committed
993
	fixed_t waterheight;
Alam Ed Arias committed
994 995 996 997 998
	sector_t *actionsector;
	INT32 secnum;

	// Just find the first sector with the tag.
	// Doesn't work with multiple sectors that have different floor/ceiling heights.
999
	if ((secnum = Tag_Iterate_Sectors((INT16)floater->tag, 0)) < 0)
MascaraSnake committed
1000 1001
		return;
	actionsector = &sectors[secnum];
Alam Ed Arias committed
1002

MascaraSnake committed
1003
	cheeseheight = (floater->sector->ceilingheight + floater->sector->floorheight)>>1;
Alam Ed Arias committed
1004

MascaraSnake committed
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
	//boolean floatanyway = false; // Ignore the crumblestate setting.
	waterheight = P_SectorCheckWater(actionsector, floater->sector); // find the highest suitable water block around

	if (waterheight == cheeseheight) // same height, no floating needed
		return;

	if (floater->sector->floorheight == actionsector->floorheight && waterheight < cheeseheight) // too low
		return;

	if (floater->sector->ceilingheight == actionsector->ceilingheight && waterheight > cheeseheight) // too high
		return;

	// we have something to float in! Or we're for some reason above the ground, let's fall anyway
	if (floater->sector->crumblestate == CRUMBLE_NONE || floater->sector->crumblestate >= CRUMBLE_FALL/* || floatanyway*/)
		EV_BounceSector(floater->sector, FRACUNIT, floater->sourceline);
Alam Ed Arias committed
1020 1021 1022 1023 1024
}

static mobj_t *SearchMarioNode(msecnode_t *node)
{
	mobj_t *thing = NULL;
1025
	for (; node; node = node->m_thinglist_next)
Alam Ed Arias committed
1026 1027 1028 1029 1030 1031
	{
		// Things which should NEVER be ejected from a MarioBlock, by type.
		switch (node->m_thing->type)
		{
		case MT_NULL:
		case MT_UNKNOWN:
1032
		case MT_TAILSOVERLAY:
Alam Ed Arias committed
1033 1034 1035 1036
		case MT_THOK:
		case MT_GHOST:
		case MT_OVERLAY:
		case MT_EMERALDSPAWN:
1037 1038 1039
		case MT_ELEMENTAL_ORB:
		case MT_ATTRACT_ORB:
		case MT_FORCE_ORB:
1040 1041
		case MT_ARMAGEDDON_ORB:
		case MT_WHIRLWIND_ORB:
1042 1043 1044 1045
		case MT_PITY_ORB:
		case MT_FLAMEAURA_ORB:
		case MT_BUBBLEWRAP_ORB:
		case MT_THUNDERCOIN_ORB:
Alam Ed Arias committed
1046 1047 1048 1049 1050
		case MT_IVSP:
		case MT_SUPERSPARK:
		case MT_RAIN:
		case MT_SNOWFLAKE:
		case MT_SPLISH:
1051
		case MT_LAVASPLISH:
Alam Ed Arias committed
1052 1053 1054 1055 1056 1057 1058 1059 1060
		case MT_SMOKE:
		case MT_SMALLBUBBLE:
		case MT_MEDIUMBUBBLE:
		case MT_TFOG:
		case MT_SEED:
		case MT_PARTICLE:
		case MT_SCORE:
		case MT_DROWNNUMBERS:
		case MT_GOTEMERALD:
1061
		case MT_LOCKON:
Alam Ed Arias committed
1062
		case MT_TAG:
toaster committed
1063
		case MT_GOTFLAG:
Alam Ed Arias committed
1064 1065 1066 1067 1068 1069 1070 1071 1072
		case MT_HOOP:
		case MT_HOOPCOLLIDE:
		case MT_NIGHTSCORE:
		case MT_NAMECHECK: // DEFINITELY not this, because it is client-side.
			continue;
		default:
			break;
		}
		// Ignore popped monitors, too.
1073 1074
		if (node->m_thing->health == 0 // this only really applies for monitors
		|| (!(node->m_thing->flags & MF_MONITOR) && (mobjinfo[node->m_thing->type].flags & MF_MONITOR))) // gold monitor support
Alam Ed Arias committed
1075 1076 1077 1078 1079 1080 1081 1082 1083 1084
			continue;
		// Okay, we found something valid.
		if (!thing // take either the first thing
		|| node->m_thing->x < thing->x // or the thing with the lowest x value (left to right item order)
		|| node->m_thing->y < thing->y) // or the thing with the lowest y value (top to bottom item order)
			thing = node->m_thing;
	}
	return thing;
}

1085
void T_MarioBlockChecker(mariocheck_t *block)
Alam Ed Arias committed
1086 1087 1088
{
	line_t *masterline = block->sourceline;
	if (SearchMarioNode(block->sector->touching_thinglist))
1089 1090
	{
		sides[masterline->sidenum[0]].midtexture = sides[masterline->sidenum[0]].bottomtexture; // Update textures
1091 1092
		if (masterline->backsector)
			block->sector->ceilingpic = block->sector->floorpic = masterline->backsector->ceilingpic; // Update flats to be backside's ceiling
1093
	}
Alam Ed Arias committed
1094
	else
1095
	{
Alam Ed Arias committed
1096
		sides[masterline->sidenum[0]].midtexture = sides[masterline->sidenum[0]].toptexture;
1097 1098
		if (masterline->backsector)
			block->sector->ceilingpic = block->sector->floorpic = masterline->backsector->floorpic; // Update flats to be backside's floor
1099
	}
Alam Ed Arias committed
1100 1101
}

MascaraSnake committed
1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
static boolean P_IsPlayerValid(size_t playernum)
{
	if (!playeringame[playernum])
		return false;

	if (!players[playernum].mo)
		return false;

	if (players[playernum].mo->health <= 0)
		return false;

	if (players[playernum].spectator)
		return false;

	return true;
}

Alam Ed Arias committed
1119 1120
// This is the Thwomp's 'brain'. It looks around for players nearby, and if
// it finds any, **SMASH**!!! Muahahhaa....
MascaraSnake committed
1121
void T_ThwompSector(thwomp_t *thwomp)
Alam Ed Arias committed
1122 1123 1124
{
	fixed_t thwompx, thwompy;
	sector_t *actionsector;
1125
	ffloor_t *rover = NULL;
Alam Ed Arias committed
1126
	INT32 secnum;
MascaraSnake committed
1127
	fixed_t speed;
Alam Ed Arias committed
1128 1129

	// If you just crashed down, wait a second before coming back up.
MascaraSnake committed
1130
	if (--thwomp->delay > 0)
Alam Ed Arias committed
1131 1132 1133 1134
		return;

	// Just find the first sector with the tag.
	// Doesn't work with multiple sectors that have different floor/ceiling heights.
1135 1136
	if ((secnum = Tag_Iterate_Sectors((INT16)thwomp->tag, 0)) < 0)
		return;
Alam Ed Arias committed
1137

MascaraSnake committed
1138 1139 1140 1141
	actionsector = &sectors[secnum];

	// Look for thwomp FOF
	for (rover = actionsector->ffloors; rover; rover = rover->next)
1142
	{
MascaraSnake committed
1143 1144 1145 1146 1147 1148
		if (rover->master == thwomp->sourceline)
			break;
	}

	if (!rover)
		return; // Didn't find any FOFs, so bail out
1149

MascaraSnake committed
1150 1151
	thwompx = actionsector->soundorg.x;
	thwompy = actionsector->soundorg.y;
1152

MascaraSnake committed
1153 1154 1155
	if (thwomp->direction == 0) // Not going anywhere, so look for players.
	{
		if (rover->flags & FF_EXISTS)
1156
		{
MascaraSnake committed
1157 1158 1159 1160 1161 1162 1163 1164 1165 1166
			UINT8 i;
			// scan the players to find victims!
			for (i = 0; i < MAXPLAYERS; i++)
			{
				if (!P_IsPlayerValid(i))
					continue;

				if (players[i].mo->z > thwomp->sector->ceilingheight)
					continue;

1167
				if (P_AproxDistance(thwompx - players[i].mo->x, thwompy - players[i].mo->y) > 96*FRACUNIT)
MascaraSnake committed
1168 1169 1170
					continue;

				thwomp->direction = -1;
1171
				break;
MascaraSnake committed
1172
			}
1173
		}
MascaraSnake committed
1174 1175 1176

		thwomp->sector->ceilspeed = 0;
		thwomp->sector->floorspeed = 0;
1177
	}
Alam Ed Arias committed
1178 1179 1180 1181
	else
	{
		result_e res = 0;

MascaraSnake committed
1182 1183 1184 1185
		if (thwomp->direction > 0) //Moving back up..
		{
			// Set the texture from the lower one (normal)
			sides[thwomp->sourceline->sidenum[0]].midtexture = sides[thwomp->sourceline->sidenum[0]].bottomtexture;
Alam Ed Arias committed
1186

MascaraSnake committed
1187
			speed = thwomp->retractspeed;
Alam Ed Arias committed
1188

MascaraSnake committed
1189
			res = T_MovePlane
Alam Ed Arias committed
1190 1191
			(
				thwomp->sector,           // sector
MascaraSnake committed
1192 1193 1194 1195
				speed,                    // speed
				thwomp->floorstartheight, // dest
				false,                    // crush
				false,                    // ceiling?
Alam Ed Arias committed
1196 1197 1198
				thwomp->direction         // direction
			);

MascaraSnake committed
1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
			if (res == ok || res == pastdest)
				T_MovePlane
				(
					thwomp->sector,             // sector
					speed,                      // speed
					thwomp->ceilingstartheight, // dest
					false,                      // crush
					true,                       // ceiling?
					thwomp->direction           // direction
				);
Alam Ed Arias committed
1209

MascaraSnake committed
1210 1211 1212 1213 1214 1215 1216
			if (res == pastdest)
				thwomp->direction = 0; // stop moving
			}
		else // Crashing down!
		{
			// Set the texture from the upper one (angry)
			sides[thwomp->sourceline->sidenum[0]].midtexture = sides[thwomp->sourceline->sidenum[0]].toptexture;
Alam Ed Arias committed
1217

MascaraSnake committed
1218
			speed = thwomp->crushspeed;
Alam Ed Arias committed
1219

MascaraSnake committed
1220
			res = T_MovePlane
Alam Ed Arias committed
1221 1222
			(
				thwomp->sector,   // sector
MascaraSnake committed
1223
				speed,            // speed
Alam Ed Arias committed
1224
				P_FloorzAtPos(thwompx, thwompy, thwomp->sector->floorheight,
MascaraSnake committed
1225 1226 1227
					thwomp->sector->ceilingheight - thwomp->sector->floorheight), // dest
				false,              // crush
				false,              // ceiling?
Alam Ed Arias committed
1228 1229 1230
				thwomp->direction // direction
			);

MascaraSnake committed
1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244
			if (res == ok || res == pastdest)
				T_MovePlane
				(
					thwomp->sector,   // sector
					speed,            // speed
					P_FloorzAtPos(thwompx, thwompy, thwomp->sector->floorheight,
						thwomp->sector->ceilingheight
						- (thwomp->sector->floorheight + speed))
						+ (thwomp->sector->ceilingheight
						- (thwomp->sector->floorheight + speed/2)), // dest
					false,             // crush
					true,              // ceiling?
					thwomp->direction // direction
				);
Alam Ed Arias committed
1245

MascaraSnake committed
1246
			if (res == pastdest)
1247
			{
MascaraSnake committed
1248 1249
				if (rover->flags & FF_EXISTS)
					S_StartSound((void *)&actionsector->soundorg, thwomp->sound);
Alam Ed Arias committed
1250

MascaraSnake committed
1251 1252
				thwomp->direction = 1; // start heading back up
				thwomp->delay = TICRATE; // but only after a small delay
Alam Ed Arias committed
1253 1254 1255 1256
			}
		}

		thwomp->sector->ceilspeed = 42;
MascaraSnake committed
1257
		thwomp->sector->floorspeed = speed*thwomp->direction;
Alam Ed Arias committed
1258 1259 1260 1261 1262
	}

	P_RecalcPrecipInSector(actionsector);
}

1263 1264 1265 1266 1267
static boolean T_SectorHasEnemies(sector_t *sec)
{
	msecnode_t *node = sec->touching_thinglist; // things touching this sector
	mobj_t *mo;
	while (node)
Alam Ed Arias committed
1268
	{
1269
		mo = node->m_thing;
1270

1271 1272 1273 1274 1275
		if ((mo->flags & (MF_ENEMY|MF_BOSS))
			&& mo->health > 0
			&& mo->z < sec->ceilingheight
			&& mo->z + mo->height > sec->floorheight)
			return true;
Alam Ed Arias committed
1276

1277
		node = node->m_thinglist_next;
Alam Ed Arias committed
1278 1279
	}

1280
	return false;
Alam Ed Arias committed
1281 1282 1283 1284 1285 1286 1287 1288
}

//
// T_NoEnemiesThinker
//
// Runs a linedef exec when no more MF_ENEMY/MF_BOSS objects with health are in the area
// \sa P_AddNoEnemiesThinker
//
1289
void T_NoEnemiesSector(noenemies_t *nobaddies)
Alam Ed Arias committed
1290 1291
{
	size_t i;
1292 1293 1294
	sector_t *sec = NULL;
	INT32 secnum = -1;
	boolean FOFsector = false;
1295
	mtag_t tag = Tag_FGet(&nobaddies->sourceline->tags);
1296
	TAG_ITER_DECLARECOUNTER(0);
Alam Ed Arias committed
1297

1298
	TAG_ITER_SECTORS(0, tag, secnum)
Alam Ed Arias committed
1299
	{
1300 1301 1302 1303 1304 1305
		sec = &sectors[secnum];

		FOFsector = false;

		// Check the lines of this sector, to see if it is a FOF control sector.
		for (i = 0; i < sec->linecount; i++)
Alam Ed Arias committed
1306
		{
1307
			INT32 targetsecnum = -1;
1308
			mtag_t tag2 = Tag_FGet(&sec->lines[i]->tags);
1309
			TAG_ITER_DECLARECOUNTER(1);
Alam Ed Arias committed
1310

1311 1312 1313 1314
			if (sec->lines[i]->special < 100 || sec->lines[i]->special >= 300)
				continue;

			FOFsector = true;
Alam Ed Arias committed
1315

1316
			TAG_ITER_SECTORS(1, tag2, targetsecnum)
Alam Ed Arias committed
1317
			{
1318
				if (T_SectorHasEnemies(&sectors[targetsecnum]))
1319 1320 1321
					return;
			}
		}
1322 1323 1324

		if (!FOFsector && T_SectorHasEnemies(sec))
			return;
1325
	}
Alam Ed Arias committed
1326

1327
	CONS_Debug(DBG_GAMELOGIC, "Running no-more-enemies exec with tag of %d\n", tag);
Alam Ed Arias committed
1328

1329 1330
	// No enemies found, run the linedef exec and terminate this thinker
	P_RunTriggerLinedef(nobaddies->sourceline, NULL, NULL);
Alam Ed Arias committed
1331 1332 1333
	P_RemoveThinker(&nobaddies->thinker);
}

1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360
//
// P_IsObjectOnRealGround
//
// Helper function for T_EachTimeThinker
// Like P_IsObjectOnGroundIn, except ONLY THE REAL GROUND IS CONSIDERED, NOT FOFS
// I'll consider whether to make this a more globally accessible function or whatever in future
// -- Monster Iestyn
//
static boolean P_IsObjectOnRealGround(mobj_t *mo, sector_t *sec)
{
	// Is the object in reverse gravity?
	if (mo->eflags & MFE_VERTICALFLIP)
	{
		// Detect if the player is on the ceiling.
		if (mo->z+mo->height >= P_GetSpecialTopZ(mo, sec, sec))
			return true;
	}
	// Nope!
	else
	{
		// Detect if the player is on the floor.
		if (mo->z <= P_GetSpecialBottomZ(mo, sec, sec))
			return true;
	}
	return false;
}

1361
static boolean P_IsMobjTouchingSector(mobj_t *mo, sector_t *sec)
Alam Ed Arias committed
1362
{
1363
	msecnode_t *node;
Alam Ed Arias committed
1364

1365 1366
	if (mo->subsector->sector == sec)
		return true;
Alam Ed Arias committed
1367

1368 1369
	if (!(sec->flags & SF_TRIGGERSPECIAL_TOUCH))
		return false;
Alam Ed Arias committed
1370

1371 1372 1373 1374
	for (node = mo->touching_sectorlist; node; node = node->m_sectorlist_next)
	{
		if (node->m_sector == sec)
			return true;
Alam Ed Arias committed
1375 1376
	}

1377
	return false;
Alam Ed Arias committed
1378 1379 1380 1381 1382 1383 1384 1385 1386 1387
}

//
// T_EachTimeThinker
//
// Runs a linedef exec whenever a player enters an area.
// Keeps track of players currently in the area and notices any changes.
//
// \sa P_AddEachTimeThinker
//
1388
void T_EachTimeThinker(eachtime_t *eachtime)
Alam Ed Arias committed
1389 1390 1391 1392 1393
{
	size_t i, j;
	sector_t *sec = NULL;
	sector_t *targetsec = NULL;
	INT32 secnum = -1;
Alam Ed Arias committed
1394 1395 1396 1397
	boolean oldPlayersInArea[MAXPLAYERS];
	boolean oldPlayersOnArea[MAXPLAYERS];
	boolean *oldPlayersArea;
	boolean *playersArea;
Alam Ed Arias committed
1398 1399
	boolean FOFsector = false;
	boolean floortouch = false;
1400
	fixed_t bottomheight, topheight;
1401
	ffloor_t *rover;
1402
	mtag_t tag = Tag_FGet(&eachtime->sourceline->tags);
1403
	TAG_ITER_DECLARECOUNTER(0);
Alam Ed Arias committed
1404 1405 1406

	for (i = 0; i < MAXPLAYERS; i++)
	{
1407 1408 1409 1410
		oldPlayersInArea[i] = eachtime->playersInArea[i];
		oldPlayersOnArea[i] = eachtime->playersOnArea[i];
		eachtime->playersInArea[i] = false;
		eachtime->playersOnArea[i] = false;
Alam Ed Arias committed
1411 1412
	}

1413
	TAG_ITER_SECTORS(0, tag, secnum)
Alam Ed Arias committed
1414 1415 1416
	{
		sec = &sectors[secnum];

Alam Ed Arias committed
1417 1418 1419 1420 1421 1422 1423 1424 1425
		FOFsector = false;

		if (GETSECSPECIAL(sec->special, 2) == 3 || GETSECSPECIAL(sec->special, 2) == 5)
			floortouch = true;
		else if (GETSECSPECIAL(sec->special, 2) >= 1 && GETSECSPECIAL(sec->special, 2) <= 8)
			floortouch = false;
		else
			continue;

Alam Ed Arias committed
1426 1427 1428 1429
		// Check the lines of this sector, to see if it is a FOF control sector.
		for (i = 0; i < sec->linecount; i++)
		{
			INT32 targetsecnum = -1;
1430
			mtag_t tag2 = Tag_FGet(&sec->lines[i]->tags);
1431
			TAG_ITER_DECLARECOUNTER(1);
Alam Ed Arias committed
1432 1433 1434 1435 1436 1437

			if (sec->lines[i]->special < 100 || sec->lines[i]->special >= 300)
				continue;

			FOFsector = true;

1438
			TAG_ITER_SECTORS(1, tag2, targetsecnum)
Alam Ed Arias committed
1439 1440 1441
			{
				targetsec = &sectors[targetsecnum];

1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454
				// Find the FOF corresponding to the control linedef
				for (rover = targetsec->ffloors; rover; rover = rover->next)
				{
					if (rover->master == sec->lines[i])
						break;
				}

				if (!rover) // This should be impossible, but don't complain if it is the case somehow
					continue;

				if (!(rover->flags & FF_EXISTS)) // If the FOF does not "exist", we pretend that nobody's there
					continue;

Alam Ed Arias committed
1455 1456
				for (j = 0; j < MAXPLAYERS; j++)
				{
1457
					if (!P_IsPlayerValid(j))
Alam Ed Arias committed
1458 1459
						continue;

1460
					if (!P_IsMobjTouchingSector(players[j].mo, targetsec))
Alam Ed Arias committed
1461 1462
						continue;

1463 1464 1465 1466
					topheight = P_GetSpecialTopZ(players[j].mo, sec, targetsec);
					bottomheight = P_GetSpecialBottomZ(players[j].mo, sec, targetsec);

					if (players[j].mo->z > topheight)
Alam Ed Arias committed
1467 1468
						continue;

1469
					if (players[j].mo->z + players[j].mo->height < bottomheight)
Alam Ed Arias committed
1470 1471
						continue;

1472
					if (floortouch && P_IsObjectOnGroundIn(players[j].mo, targetsec))
1473
						eachtime->playersOnArea[j] = true;
Alam Ed Arias committed
1474
					else
1475
						eachtime->playersInArea[j] = true;
Alam Ed Arias committed
1476 1477 1478 1479 1480 1481 1482 1483
				}
			}
		}

		if (!FOFsector)
		{
			for (i = 0; i < MAXPLAYERS; i++)
			{
1484
				if (!P_IsPlayerValid(i))
Alam Ed Arias committed
1485 1486
					continue;

1487
				if (!P_IsMobjTouchingSector(players[i].mo, sec))
1488
					continue;
1489 1490 1491

				if (!(players[i].mo->subsector->sector == sec
					|| P_PlayerTouchingSectorSpecial(&players[i], 2, (GETSECSPECIAL(sec->special, 2))) == sec))
Alam Ed Arias committed
1492 1493
					continue;

1494
				if (floortouch && P_IsObjectOnRealGround(players[i].mo, sec))
1495
					eachtime->playersOnArea[i] = true;
Alam Ed Arias committed
1496
				else
1497
					eachtime->playersInArea[i] = true;
Alam Ed Arias committed
1498 1499 1500 1501 1502 1503 1504
			}
		}
	}

	// Check if a new player entered.
	// If not, check if a player hit the floor.
	// If either condition is true, execute.
1505
	if (floortouch)
Alam Ed Arias committed
1506
	{
1507
		playersArea = eachtime->playersOnArea;
Alam Ed Arias committed
1508 1509 1510 1511
		oldPlayersArea = oldPlayersOnArea;
	}
	else
	{
1512
		playersArea = eachtime->playersInArea;
Alam Ed Arias committed
1513 1514 1515
		oldPlayersArea = oldPlayersInArea;
	}

1516 1517 1518 1519 1520 1521
	// Easy check... nothing has changed
	if (!memcmp(playersArea, oldPlayersArea, sizeof(boolean)*MAXPLAYERS))
		return;

	// If sector has an "all players" trigger type, all players need to be in area
	if (GETSECSPECIAL(sec->special, 2) == 2 || GETSECSPECIAL(sec->special, 2) == 3)
Alam Ed Arias committed
1522
	{
1523
		for (i = 0; i < MAXPLAYERS; i++)
Alam Ed Arias committed
1524
		{
1525 1526 1527 1528
			if (P_IsPlayerValid(i) && playersArea[i])
				continue;
		}
	}
Alam Ed Arias committed
1529

1530 1531 1532 1533 1534
	// Trigger for every player who has entered (and exited, if triggerOnExit)
	for (i = 0; i < MAXPLAYERS; i++)
	{
		if (playersArea[i] == oldPlayersArea[i])
			continue;
Alam Ed Arias committed
1535

1536 1537 1538
		// If player has just left, check if still valid
		if (!playersArea[i] && (!eachtime->triggerOnExit || !P_IsPlayerValid(i)))
			continue;
Alam Ed Arias committed
1539

1540
		CONS_Debug(DBG_GAMELOGIC, "Trying to activate each time executor with tag %d\n", tag);
Alam Ed Arias committed
1541

Alam Ed Arias committed
1542 1543 1544 1545
		// 03/08/14 -Monster Iestyn
		// No more stupid hacks involving changing eachtime->sourceline's tag or special or whatever!
		// This should now run ONLY the stuff for eachtime->sourceline itself, instead of all trigger linedefs sharing the same tag.
		// Makes much more sense doing it this way, honestly.
1546
		P_RunTriggerLinedef(eachtime->sourceline, players[i].mo, sec);
Alam Ed Arias committed
1547 1548 1549

		if (!eachtime->sourceline->special) // this happens only for "Trigger on X calls" linedefs
			P_RemoveThinker(&eachtime->thinker);
Alam Ed Arias committed
1550 1551 1552 1553 1554 1555 1556 1557 1558
	}
}

//
// T_RaiseSector
//
// Rises up to its topmost position when a
// player steps on it. Lowers otherwise.
//
1559
void T_RaiseSector(raise_t *raise)
Alam Ed Arias committed
1560 1561 1562 1563 1564
{
	msecnode_t *node;
	mobj_t *thing;
	sector_t *sector;
	INT32 i;
1565
	boolean playeronme = false, active = false;
1566
	boolean moveUp;
Alam Ed Arias committed
1567
	fixed_t ceilingdestination, floordestination;
1568
	fixed_t speed, origspeed;
1569
	fixed_t distToNearestEndpoint;
1570
	INT32 direction;
Alam Ed Arias committed
1571
	result_e res = 0;
1572
	mtag_t tag = raise->tag;
1573
	TAG_ITER_DECLARECOUNTER(0);
Alam Ed Arias committed
1574

1575
	if (raise->sector->crumblestate >= CRUMBLE_FALL || raise->sector->ceilingdata)
Alam Ed Arias committed
1576 1577
		return;

1578
	TAG_ITER_SECTORS(0, tag, i)
Alam Ed Arias committed
1579 1580 1581 1582
	{
		sector = &sectors[i];

		// Is a player standing on me?
1583
		for (node = sector->touching_thinglist; node; node = node->m_thinglist_next)
Alam Ed Arias committed
1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594
		{
			thing = node->m_thing;

			if (!thing->player)
				continue;

			// Ignore spectators.
			if (thing->player && thing->player->spectator)
				continue;

			// Option to require spindashing.
1595
			if (raise->flags & RF_SPINDASH && !(thing->player->pflags & PF_STARTDASH))
Alam Ed Arias committed
1596 1597
				continue;

1598
			if (!(thing->z == P_GetSpecialTopZ(thing, raise->sector, sector)))
Alam Ed Arias committed
1599 1600 1601 1602 1603 1604
				continue;

			playeronme = true;
			break;
		}
	}
James R. committed
1605

1606
	if (raise->flags & RF_DYNAMIC) // Dynamically Sinking Platform^tm